1 | /*
|
---|
2 | * Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | /* The AES_ige_* functions are deprecated, so we suppress warnings about them */
|
---|
11 | #define OPENSSL_SUPPRESS_DEPRECATED
|
---|
12 |
|
---|
13 | #include <openssl/crypto.h>
|
---|
14 | #include <openssl/aes.h>
|
---|
15 | #include <openssl/rand.h>
|
---|
16 | #include <stdio.h>
|
---|
17 | #include <string.h>
|
---|
18 | #include "internal/nelem.h"
|
---|
19 | #include "testutil.h"
|
---|
20 |
|
---|
21 | #ifndef OPENSSL_NO_DEPRECATED_3_0
|
---|
22 |
|
---|
23 | # define TEST_SIZE 128
|
---|
24 | # define BIG_TEST_SIZE 10240
|
---|
25 |
|
---|
26 | # if BIG_TEST_SIZE < TEST_SIZE
|
---|
27 | # error BIG_TEST_SIZE is smaller than TEST_SIZE
|
---|
28 | # endif
|
---|
29 |
|
---|
30 | static unsigned char rkey[16];
|
---|
31 | static unsigned char rkey2[16];
|
---|
32 | static unsigned char plaintext[BIG_TEST_SIZE];
|
---|
33 | static unsigned char saved_iv[AES_BLOCK_SIZE * 4];
|
---|
34 |
|
---|
35 | # define MAX_VECTOR_SIZE 64
|
---|
36 |
|
---|
37 | struct ige_test {
|
---|
38 | const unsigned char key[16];
|
---|
39 | const unsigned char iv[32];
|
---|
40 | const unsigned char in[MAX_VECTOR_SIZE];
|
---|
41 | const unsigned char out[MAX_VECTOR_SIZE];
|
---|
42 | const size_t length;
|
---|
43 | const int encrypt;
|
---|
44 | };
|
---|
45 |
|
---|
46 | static struct ige_test const ige_test_vectors[] = {
|
---|
47 | {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
---|
48 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, /* key */
|
---|
49 | {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
---|
50 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
---|
51 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
---|
52 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, /* iv */
|
---|
53 | {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
54 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
55 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
56 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* in */
|
---|
57 | {0x1a, 0x85, 0x19, 0xa6, 0x55, 0x7b, 0xe6, 0x52,
|
---|
58 | 0xe9, 0xda, 0x8e, 0x43, 0xda, 0x4e, 0xf4, 0x45,
|
---|
59 | 0x3c, 0xf4, 0x56, 0xb4, 0xca, 0x48, 0x8a, 0xa3,
|
---|
60 | 0x83, 0xc7, 0x9c, 0x98, 0xb3, 0x47, 0x97, 0xcb}, /* out */
|
---|
61 | 32, AES_ENCRYPT}, /* test vector 0 */
|
---|
62 |
|
---|
63 | {{0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
|
---|
64 | 0x61, 0x6e, 0x20, 0x69, 0x6d, 0x70, 0x6c, 0x65}, /* key */
|
---|
65 | {0x6d, 0x65, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f,
|
---|
66 | 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x49, 0x47, 0x45,
|
---|
67 | 0x20, 0x6d, 0x6f, 0x64, 0x65, 0x20, 0x66, 0x6f,
|
---|
68 | 0x72, 0x20, 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53}, /* iv */
|
---|
69 | {0x4c, 0x2e, 0x20, 0x4c, 0x65, 0x74, 0x27, 0x73,
|
---|
70 | 0x20, 0x68, 0x6f, 0x70, 0x65, 0x20, 0x42, 0x65,
|
---|
71 | 0x6e, 0x20, 0x67, 0x6f, 0x74, 0x20, 0x69, 0x74,
|
---|
72 | 0x20, 0x72, 0x69, 0x67, 0x68, 0x74, 0x21, 0x0a}, /* in */
|
---|
73 | {0x99, 0x70, 0x64, 0x87, 0xa1, 0xcd, 0xe6, 0x13,
|
---|
74 | 0xbc, 0x6d, 0xe0, 0xb6, 0xf2, 0x4b, 0x1c, 0x7a,
|
---|
75 | 0xa4, 0x48, 0xc8, 0xb9, 0xc3, 0x40, 0x3e, 0x34,
|
---|
76 | 0x67, 0xa8, 0xca, 0xd8, 0x93, 0x40, 0xf5, 0x3b}, /* out */
|
---|
77 | 32, AES_DECRYPT}, /* test vector 1 */
|
---|
78 | };
|
---|
79 |
|
---|
80 | struct bi_ige_test {
|
---|
81 | const unsigned char key1[32];
|
---|
82 | const unsigned char key2[32];
|
---|
83 | const unsigned char iv[64];
|
---|
84 | const unsigned char in[MAX_VECTOR_SIZE];
|
---|
85 | const unsigned char out[MAX_VECTOR_SIZE];
|
---|
86 | const size_t keysize;
|
---|
87 | const size_t length;
|
---|
88 | const int encrypt;
|
---|
89 | };
|
---|
90 |
|
---|
91 | static struct bi_ige_test const bi_ige_test_vectors[] = {
|
---|
92 | {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
---|
93 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, /* key1 */
|
---|
94 | {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
---|
95 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, /* key2 */
|
---|
96 | {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
---|
97 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
---|
98 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
---|
99 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
|
---|
100 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
|
---|
101 | 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
|
---|
102 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
|
---|
103 | 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f}, /* iv */
|
---|
104 | {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
105 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
106 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
107 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* in */
|
---|
108 | {0x14, 0x40, 0x6f, 0xae, 0xa2, 0x79, 0xf2, 0x56,
|
---|
109 | 0x1f, 0x86, 0xeb, 0x3b, 0x7d, 0xff, 0x53, 0xdc,
|
---|
110 | 0x4e, 0x27, 0x0c, 0x03, 0xde, 0x7c, 0xe5, 0x16,
|
---|
111 | 0x6a, 0x9c, 0x20, 0x33, 0x9d, 0x33, 0xfe, 0x12}, /* out */
|
---|
112 | 16, 32, AES_ENCRYPT}, /* test vector 0 */
|
---|
113 | {{0x58, 0x0a, 0x06, 0xe9, 0x97, 0x07, 0x59, 0x5c,
|
---|
114 | 0x9e, 0x19, 0xd2, 0xa7, 0xbb, 0x40, 0x2b, 0x7a,
|
---|
115 | 0xc7, 0xd8, 0x11, 0x9e, 0x4c, 0x51, 0x35, 0x75,
|
---|
116 | 0x64, 0x28, 0x0f, 0x23, 0xad, 0x74, 0xac, 0x37}, /* key1 */
|
---|
117 | {0xd1, 0x80, 0xa0, 0x31, 0x47, 0xa3, 0x11, 0x13,
|
---|
118 | 0x86, 0x26, 0x9e, 0x6d, 0xff, 0xaf, 0x72, 0x74,
|
---|
119 | 0x5b, 0xa2, 0x35, 0x81, 0xd2, 0xa6, 0x3d, 0x21,
|
---|
120 | 0x67, 0x7b, 0x58, 0xa8, 0x18, 0xf9, 0x72, 0xe4}, /* key2 */
|
---|
121 | {0x80, 0x3d, 0xbd, 0x4c, 0xe6, 0x7b, 0x06, 0xa9,
|
---|
122 | 0x53, 0x35, 0xd5, 0x7e, 0x71, 0xc1, 0x70, 0x70,
|
---|
123 | 0x74, 0x9a, 0x00, 0x28, 0x0c, 0xbf, 0x6c, 0x42,
|
---|
124 | 0x9b, 0xa4, 0xdd, 0x65, 0x11, 0x77, 0x7c, 0x67,
|
---|
125 | 0xfe, 0x76, 0x0a, 0xf0, 0xd5, 0xc6, 0x6e, 0x6a,
|
---|
126 | 0xe7, 0x5e, 0x4c, 0xf2, 0x7e, 0x9e, 0xf9, 0x20,
|
---|
127 | 0x0e, 0x54, 0x6f, 0x2d, 0x8a, 0x8d, 0x7e, 0xbd,
|
---|
128 | 0x48, 0x79, 0x37, 0x99, 0xff, 0x27, 0x93, 0xa3}, /* iv */
|
---|
129 | {0xf1, 0x54, 0x3d, 0xca, 0xfe, 0xb5, 0xef, 0x1c,
|
---|
130 | 0x4f, 0xa6, 0x43, 0xf6, 0xe6, 0x48, 0x57, 0xf0,
|
---|
131 | 0xee, 0x15, 0x7f, 0xe3, 0xe7, 0x2f, 0xd0, 0x2f,
|
---|
132 | 0x11, 0x95, 0x7a, 0x17, 0x00, 0xab, 0xa7, 0x0b,
|
---|
133 | 0xbe, 0x44, 0x09, 0x9c, 0xcd, 0xac, 0xa8, 0x52,
|
---|
134 | 0xa1, 0x8e, 0x7b, 0x75, 0xbc, 0xa4, 0x92, 0x5a,
|
---|
135 | 0xab, 0x46, 0xd3, 0x3a, 0xa0, 0xd5, 0x35, 0x1c,
|
---|
136 | 0x55, 0xa4, 0xb3, 0xa8, 0x40, 0x81, 0xa5, 0x0b}, /* in */
|
---|
137 | {0x42, 0xe5, 0x28, 0x30, 0x31, 0xc2, 0xa0, 0x23,
|
---|
138 | 0x68, 0x49, 0x4e, 0xb3, 0x24, 0x59, 0x92, 0x79,
|
---|
139 | 0xc1, 0xa5, 0xcc, 0xe6, 0x76, 0x53, 0xb1, 0xcf,
|
---|
140 | 0x20, 0x86, 0x23, 0xe8, 0x72, 0x55, 0x99, 0x92,
|
---|
141 | 0x0d, 0x16, 0x1c, 0x5a, 0x2f, 0xce, 0xcb, 0x51,
|
---|
142 | 0xe2, 0x67, 0xfa, 0x10, 0xec, 0xcd, 0x3d, 0x67,
|
---|
143 | 0xa5, 0xe6, 0xf7, 0x31, 0x26, 0xb0, 0x0d, 0x76,
|
---|
144 | 0x5e, 0x28, 0xdc, 0x7f, 0x01, 0xc5, 0xa5, 0x4c}, /* out */
|
---|
145 | 32, 64, AES_ENCRYPT}, /* test vector 1 */
|
---|
146 |
|
---|
147 | };
|
---|
148 |
|
---|
149 | static int test_ige_vectors(int n)
|
---|
150 | {
|
---|
151 | const struct ige_test *const v = &ige_test_vectors[n];
|
---|
152 | AES_KEY key;
|
---|
153 | unsigned char buf[MAX_VECTOR_SIZE];
|
---|
154 | unsigned char iv[AES_BLOCK_SIZE * 2];
|
---|
155 | int testresult = 1;
|
---|
156 |
|
---|
157 | if (!TEST_int_le(v->length, MAX_VECTOR_SIZE))
|
---|
158 | return 0;
|
---|
159 |
|
---|
160 | if (v->encrypt == AES_ENCRYPT)
|
---|
161 | AES_set_encrypt_key(v->key, 8 * sizeof(v->key), &key);
|
---|
162 | else
|
---|
163 | AES_set_decrypt_key(v->key, 8 * sizeof(v->key), &key);
|
---|
164 | memcpy(iv, v->iv, sizeof(iv));
|
---|
165 | AES_ige_encrypt(v->in, buf, v->length, &key, iv, v->encrypt);
|
---|
166 |
|
---|
167 | if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
|
---|
168 | TEST_info("IGE test vector %d failed", n);
|
---|
169 | test_output_memory("key", v->key, sizeof(v->key));
|
---|
170 | test_output_memory("iv", v->iv, sizeof(v->iv));
|
---|
171 | test_output_memory("in", v->in, v->length);
|
---|
172 | testresult = 0;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /* try with in == out */
|
---|
176 | memcpy(iv, v->iv, sizeof(iv));
|
---|
177 | memcpy(buf, v->in, v->length);
|
---|
178 | AES_ige_encrypt(buf, buf, v->length, &key, iv, v->encrypt);
|
---|
179 |
|
---|
180 | if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
|
---|
181 | TEST_info("IGE test vector %d failed (with in == out)", n);
|
---|
182 | test_output_memory("key", v->key, sizeof(v->key));
|
---|
183 | test_output_memory("iv", v->iv, sizeof(v->iv));
|
---|
184 | test_output_memory("in", v->in, v->length);
|
---|
185 | testresult = 0;
|
---|
186 | }
|
---|
187 |
|
---|
188 | return testresult;
|
---|
189 | }
|
---|
190 |
|
---|
191 | static int test_bi_ige_vectors(int n)
|
---|
192 | {
|
---|
193 | const struct bi_ige_test *const v = &bi_ige_test_vectors[n];
|
---|
194 | AES_KEY key1;
|
---|
195 | AES_KEY key2;
|
---|
196 | unsigned char buf[MAX_VECTOR_SIZE];
|
---|
197 |
|
---|
198 | if (!TEST_int_le(v->length, MAX_VECTOR_SIZE))
|
---|
199 | return 0;
|
---|
200 |
|
---|
201 | if (v->encrypt == AES_ENCRYPT) {
|
---|
202 | AES_set_encrypt_key(v->key1, 8 * v->keysize, &key1);
|
---|
203 | AES_set_encrypt_key(v->key2, 8 * v->keysize, &key2);
|
---|
204 | } else {
|
---|
205 | AES_set_decrypt_key(v->key1, 8 * v->keysize, &key1);
|
---|
206 | AES_set_decrypt_key(v->key2, 8 * v->keysize, &key2);
|
---|
207 | }
|
---|
208 |
|
---|
209 | AES_bi_ige_encrypt(v->in, buf, v->length, &key1, &key2, v->iv,
|
---|
210 | v->encrypt);
|
---|
211 |
|
---|
212 | if (!TEST_mem_eq(v->out, v->length, buf, v->length)) {
|
---|
213 | test_output_memory("key 1", v->key1, sizeof(v->key1));
|
---|
214 | test_output_memory("key 2", v->key2, sizeof(v->key2));
|
---|
215 | test_output_memory("iv", v->iv, sizeof(v->iv));
|
---|
216 | test_output_memory("in", v->in, v->length);
|
---|
217 | return 0;
|
---|
218 | }
|
---|
219 |
|
---|
220 | return 1;
|
---|
221 | }
|
---|
222 |
|
---|
223 | static int test_ige_enc_dec(void)
|
---|
224 | {
|
---|
225 | AES_KEY key;
|
---|
226 | unsigned char iv[AES_BLOCK_SIZE * 4];
|
---|
227 | unsigned char ciphertext[BIG_TEST_SIZE];
|
---|
228 | unsigned char checktext[BIG_TEST_SIZE];
|
---|
229 |
|
---|
230 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
231 | AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
232 | AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE, &key, iv, AES_ENCRYPT);
|
---|
233 |
|
---|
234 | AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
235 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
236 | AES_ige_encrypt(ciphertext, checktext, TEST_SIZE, &key, iv, AES_DECRYPT);
|
---|
237 |
|
---|
238 | return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE);
|
---|
239 | }
|
---|
240 |
|
---|
241 | static int test_ige_enc_chaining(void)
|
---|
242 | {
|
---|
243 | AES_KEY key;
|
---|
244 | unsigned char iv[AES_BLOCK_SIZE * 4];
|
---|
245 | unsigned char ciphertext[BIG_TEST_SIZE];
|
---|
246 | unsigned char checktext[BIG_TEST_SIZE];
|
---|
247 |
|
---|
248 | AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
249 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
250 | AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE / 2, &key, iv,
|
---|
251 | AES_ENCRYPT);
|
---|
252 | AES_ige_encrypt(plaintext + TEST_SIZE / 2,
|
---|
253 | ciphertext + TEST_SIZE / 2, TEST_SIZE / 2,
|
---|
254 | &key, iv, AES_ENCRYPT);
|
---|
255 |
|
---|
256 | AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
257 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
258 | AES_ige_encrypt(ciphertext, checktext, TEST_SIZE, &key, iv, AES_DECRYPT);
|
---|
259 |
|
---|
260 | return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE);
|
---|
261 | }
|
---|
262 |
|
---|
263 | static int test_ige_dec_chaining(void)
|
---|
264 | {
|
---|
265 | AES_KEY key;
|
---|
266 | unsigned char iv[AES_BLOCK_SIZE * 4];
|
---|
267 | unsigned char ciphertext[BIG_TEST_SIZE];
|
---|
268 | unsigned char checktext[BIG_TEST_SIZE];
|
---|
269 |
|
---|
270 | AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
271 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
272 | AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE / 2, &key, iv,
|
---|
273 | AES_ENCRYPT);
|
---|
274 | AES_ige_encrypt(plaintext + TEST_SIZE / 2,
|
---|
275 | ciphertext + TEST_SIZE / 2, TEST_SIZE / 2,
|
---|
276 | &key, iv, AES_ENCRYPT);
|
---|
277 |
|
---|
278 | AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
279 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
280 | AES_ige_encrypt(ciphertext, checktext, TEST_SIZE / 2, &key, iv,
|
---|
281 | AES_DECRYPT);
|
---|
282 | AES_ige_encrypt(ciphertext + TEST_SIZE / 2,
|
---|
283 | checktext + TEST_SIZE / 2, TEST_SIZE / 2, &key, iv,
|
---|
284 | AES_DECRYPT);
|
---|
285 |
|
---|
286 | return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE);
|
---|
287 | }
|
---|
288 |
|
---|
289 | static int test_ige_garble_forwards(void)
|
---|
290 | {
|
---|
291 | AES_KEY key;
|
---|
292 | unsigned char iv[AES_BLOCK_SIZE * 4];
|
---|
293 | unsigned char ciphertext[BIG_TEST_SIZE];
|
---|
294 | unsigned char checktext[BIG_TEST_SIZE];
|
---|
295 | unsigned int n;
|
---|
296 | int testresult = 1;
|
---|
297 | const size_t ctsize = sizeof(checktext);
|
---|
298 | size_t matches;
|
---|
299 |
|
---|
300 | AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
301 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
302 | AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv,
|
---|
303 | AES_ENCRYPT);
|
---|
304 |
|
---|
305 | /* corrupt halfway through */
|
---|
306 | ++ciphertext[sizeof(ciphertext) / 2];
|
---|
307 | AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
308 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
309 | AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv,
|
---|
310 | AES_DECRYPT);
|
---|
311 |
|
---|
312 | matches = 0;
|
---|
313 | for (n = 0; n < sizeof(checktext); ++n)
|
---|
314 | if (checktext[n] == plaintext[n])
|
---|
315 | ++matches;
|
---|
316 |
|
---|
317 | /* Fail if there is more than 51% matching bytes */
|
---|
318 | if (!TEST_size_t_le(matches, ctsize / 2 + ctsize / 100))
|
---|
319 | testresult = 0;
|
---|
320 |
|
---|
321 | /* Fail if the garble goes backwards */
|
---|
322 | if (!TEST_size_t_gt(matches, ctsize / 2))
|
---|
323 | testresult = 0;
|
---|
324 | return testresult;
|
---|
325 | }
|
---|
326 |
|
---|
327 | static int test_bi_ige_enc_dec(void)
|
---|
328 | {
|
---|
329 | AES_KEY key, key2;
|
---|
330 | unsigned char iv[AES_BLOCK_SIZE * 4];
|
---|
331 | unsigned char ciphertext[BIG_TEST_SIZE];
|
---|
332 | unsigned char checktext[BIG_TEST_SIZE];
|
---|
333 |
|
---|
334 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
335 | AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
336 | AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
|
---|
337 | AES_bi_ige_encrypt(plaintext, ciphertext, TEST_SIZE, &key, &key2, iv,
|
---|
338 | AES_ENCRYPT);
|
---|
339 |
|
---|
340 | AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
341 | AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
|
---|
342 | AES_bi_ige_encrypt(ciphertext, checktext, TEST_SIZE, &key, &key2, iv,
|
---|
343 | AES_DECRYPT);
|
---|
344 |
|
---|
345 | return TEST_mem_eq(checktext, TEST_SIZE, plaintext, TEST_SIZE);
|
---|
346 | }
|
---|
347 |
|
---|
348 | static int test_bi_ige_garble1(void)
|
---|
349 | {
|
---|
350 | AES_KEY key, key2;
|
---|
351 | unsigned char iv[AES_BLOCK_SIZE * 4];
|
---|
352 | unsigned char ciphertext[BIG_TEST_SIZE];
|
---|
353 | unsigned char checktext[BIG_TEST_SIZE];
|
---|
354 | unsigned int n;
|
---|
355 | size_t matches;
|
---|
356 |
|
---|
357 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
358 | AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
359 | AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
|
---|
360 | AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv,
|
---|
361 | AES_ENCRYPT);
|
---|
362 |
|
---|
363 | /* corrupt halfway through */
|
---|
364 | ++ciphertext[sizeof(ciphertext) / 2];
|
---|
365 | AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
366 | AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
|
---|
367 | AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv,
|
---|
368 | AES_DECRYPT);
|
---|
369 |
|
---|
370 | matches = 0;
|
---|
371 | for (n = 0; n < sizeof(checktext); ++n)
|
---|
372 | if (checktext[n] == plaintext[n])
|
---|
373 | ++matches;
|
---|
374 |
|
---|
375 | /* Fail if there is more than 1% matching bytes */
|
---|
376 | return TEST_size_t_le(matches, sizeof(checktext) / 100);
|
---|
377 | }
|
---|
378 |
|
---|
379 | static int test_bi_ige_garble2(void)
|
---|
380 | {
|
---|
381 | AES_KEY key, key2;
|
---|
382 | unsigned char iv[AES_BLOCK_SIZE * 4];
|
---|
383 | unsigned char ciphertext[BIG_TEST_SIZE];
|
---|
384 | unsigned char checktext[BIG_TEST_SIZE];
|
---|
385 | unsigned int n;
|
---|
386 | size_t matches;
|
---|
387 |
|
---|
388 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
389 | AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
390 | AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
|
---|
391 | AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv,
|
---|
392 | AES_ENCRYPT);
|
---|
393 |
|
---|
394 | /* corrupt right at the end */
|
---|
395 | ++ciphertext[sizeof(ciphertext) - 1];
|
---|
396 | AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
397 | AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
|
---|
398 | AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv,
|
---|
399 | AES_DECRYPT);
|
---|
400 |
|
---|
401 | matches = 0;
|
---|
402 | for (n = 0; n < sizeof(checktext); ++n)
|
---|
403 | if (checktext[n] == plaintext[n])
|
---|
404 | ++matches;
|
---|
405 |
|
---|
406 | /* Fail if there is more than 1% matching bytes */
|
---|
407 | return TEST_size_t_le(matches, sizeof(checktext) / 100);
|
---|
408 | }
|
---|
409 |
|
---|
410 | static int test_bi_ige_garble3(void)
|
---|
411 | {
|
---|
412 | AES_KEY key, key2;
|
---|
413 | unsigned char iv[AES_BLOCK_SIZE * 4];
|
---|
414 | unsigned char ciphertext[BIG_TEST_SIZE];
|
---|
415 | unsigned char checktext[BIG_TEST_SIZE];
|
---|
416 | unsigned int n;
|
---|
417 | size_t matches;
|
---|
418 |
|
---|
419 | memcpy(iv, saved_iv, sizeof(iv));
|
---|
420 | AES_set_encrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
421 | AES_set_encrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
|
---|
422 | AES_ige_encrypt(plaintext, ciphertext, sizeof(plaintext), &key, iv,
|
---|
423 | AES_ENCRYPT);
|
---|
424 |
|
---|
425 | /* corrupt right at the start */
|
---|
426 | ++ciphertext[0];
|
---|
427 | AES_set_decrypt_key(rkey, 8 * sizeof(rkey), &key);
|
---|
428 | AES_set_decrypt_key(rkey2, 8 * sizeof(rkey2), &key2);
|
---|
429 | AES_ige_encrypt(ciphertext, checktext, sizeof(checktext), &key, iv,
|
---|
430 | AES_DECRYPT);
|
---|
431 |
|
---|
432 | matches = 0;
|
---|
433 | for (n = 0; n < sizeof(checktext); ++n)
|
---|
434 | if (checktext[n] == plaintext[n])
|
---|
435 | ++matches;
|
---|
436 |
|
---|
437 | /* Fail if there is more than 1% matching bytes */
|
---|
438 | return TEST_size_t_le(matches, sizeof(checktext) / 100);
|
---|
439 | }
|
---|
440 | #endif
|
---|
441 |
|
---|
442 | int setup_tests(void)
|
---|
443 | {
|
---|
444 | #ifndef OPENSSL_NO_DEPRECATED_3_0
|
---|
445 | RAND_bytes(rkey, sizeof(rkey));
|
---|
446 | RAND_bytes(rkey2, sizeof(rkey2));
|
---|
447 | RAND_bytes(plaintext, sizeof(plaintext));
|
---|
448 | RAND_bytes(saved_iv, sizeof(saved_iv));
|
---|
449 |
|
---|
450 | ADD_TEST(test_ige_enc_dec);
|
---|
451 | ADD_TEST(test_ige_enc_chaining);
|
---|
452 | ADD_TEST(test_ige_dec_chaining);
|
---|
453 | ADD_TEST(test_ige_garble_forwards);
|
---|
454 | ADD_TEST(test_bi_ige_enc_dec);
|
---|
455 | ADD_TEST(test_bi_ige_garble1);
|
---|
456 | ADD_TEST(test_bi_ige_garble2);
|
---|
457 | ADD_TEST(test_bi_ige_garble3);
|
---|
458 | ADD_ALL_TESTS(test_ige_vectors, OSSL_NELEM(ige_test_vectors));
|
---|
459 | ADD_ALL_TESTS(test_bi_ige_vectors, OSSL_NELEM(bi_ige_test_vectors));
|
---|
460 | #endif
|
---|
461 | return 1;
|
---|
462 | }
|
---|