xref: /openbsd/regress/lib/libcrypto/ige/igetest.c (revision 657b055c)
1 /*	$OpenBSD: igetest.c,v 1.4 2018/07/17 17:06:49 tb Exp $	*/
2 /* ====================================================================
3  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    openssl-core@openssl.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  */
51 
52 #include <assert.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include <openssl/aes.h>
58 
59 #define TEST_SIZE	128
60 #define BIG_TEST_SIZE 10240
61 
hexdump(FILE * f,const char * title,const unsigned char * s,int l)62 static void hexdump(FILE *f,const char *title,const unsigned char *s,int l)
63     {
64     int n=0;
65 
66     fprintf(f,"%s",title);
67     for( ; n < l ; ++n)
68 		{
69 		if((n%16) == 0)
70 			fprintf(f,"\n%04x",n);
71 		fprintf(f," %02x",s[n]);
72 		}
73     fprintf(f,"\n");
74     }
75 
76 #define MAX_VECTOR_SIZE	64
77 
78 struct ige_test
79 	{
80 	const unsigned char key[16];
81 	const unsigned char iv[32];
82 	const unsigned char in[MAX_VECTOR_SIZE];
83 	const unsigned char out[MAX_VECTOR_SIZE];
84 	const size_t length;
85 	const int encrypt;
86 	};
87 
88 static struct ige_test const ige_test_vectors[] = {
89 { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
90     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, /* key */
91   { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
92     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
93     0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
94     0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }, /* iv */
95   { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* in */
99   { 0x1a, 0x85, 0x19, 0xa6, 0x55, 0x7b, 0xe6, 0x52,
100     0xe9, 0xda, 0x8e, 0x43, 0xda, 0x4e, 0xf4, 0x45,
101     0x3c, 0xf4, 0x56, 0xb4, 0xca, 0x48, 0x8a, 0xa3,
102     0x83, 0xc7, 0x9c, 0x98, 0xb3, 0x47, 0x97, 0xcb }, /* out */
103   32, AES_ENCRYPT }, /* test vector 0 */
104 
105 { { 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
106     0x61, 0x6e, 0x20, 0x69, 0x6d, 0x70, 0x6c, 0x65 }, /* key */
107   { 0x6d, 0x65, 0x6e, 0x74, 0x61, 0x74, 0x69, 0x6f,
108     0x6e, 0x20, 0x6f, 0x66, 0x20, 0x49, 0x47, 0x45,
109     0x20, 0x6d, 0x6f, 0x64, 0x65, 0x20, 0x66, 0x6f,
110     0x72, 0x20, 0x4f, 0x70, 0x65, 0x6e, 0x53, 0x53 }, /* iv */
111   { 0x4c, 0x2e, 0x20, 0x4c, 0x65, 0x74, 0x27, 0x73,
112     0x20, 0x68, 0x6f, 0x70, 0x65, 0x20, 0x42, 0x65,
113     0x6e, 0x20, 0x67, 0x6f, 0x74, 0x20, 0x69, 0x74,
114     0x20, 0x72, 0x69, 0x67, 0x68, 0x74, 0x21, 0x0a }, /* in */
115   { 0x99, 0x70, 0x64, 0x87, 0xa1, 0xcd, 0xe6, 0x13,
116     0xbc, 0x6d, 0xe0, 0xb6, 0xf2, 0x4b, 0x1c, 0x7a,
117     0xa4, 0x48, 0xc8, 0xb9, 0xc3, 0x40, 0x3e, 0x34,
118     0x67, 0xa8, 0xca, 0xd8, 0x93, 0x40, 0xf5, 0x3b }, /* out */
119   32, AES_DECRYPT }, /* test vector 1 */
120 };
121 
run_test_vectors(void)122 static int run_test_vectors(void)
123 	{
124 	unsigned int n;
125 	int errs = 0;
126 
127 	for(n=0 ; n < sizeof(ige_test_vectors)/sizeof(ige_test_vectors[0]) ; ++n)
128 		{
129 		const struct ige_test * const v = &ige_test_vectors[n];
130 		AES_KEY key;
131 		unsigned char buf[MAX_VECTOR_SIZE];
132 		unsigned char iv[AES_BLOCK_SIZE*2];
133 
134 		assert(v->length <= MAX_VECTOR_SIZE);
135 
136 		if(v->encrypt == AES_ENCRYPT)
137 			AES_set_encrypt_key(v->key, 8*sizeof v->key, &key);
138 		else
139 			AES_set_decrypt_key(v->key, 8*sizeof v->key, &key);
140 		memcpy(iv, v->iv, sizeof iv);
141 		AES_ige_encrypt(v->in, buf, v->length, &key, iv, v->encrypt);
142 
143 		if(memcmp(v->out, buf, v->length))
144 			{
145 			printf("IGE test vector %d failed\n", n);
146 			hexdump(stdout, "key", v->key, sizeof v->key);
147 			hexdump(stdout, "iv", v->iv, sizeof v->iv);
148 			hexdump(stdout, "in", v->in, v->length);
149 			hexdump(stdout, "expected", v->out, v->length);
150 			hexdump(stdout, "got", buf, v->length);
151 
152 			++errs;
153 			}
154 
155                 /* try with in == out */
156 		memcpy(iv, v->iv, sizeof iv);
157                 memcpy(buf, v->in, v->length);
158 		AES_ige_encrypt(buf, buf, v->length, &key, iv, v->encrypt);
159 
160 		if(memcmp(v->out, buf, v->length))
161 			{
162 			printf("IGE test vector %d failed (with in == out)\n", n);
163 			hexdump(stdout, "key", v->key, sizeof v->key);
164 			hexdump(stdout, "iv", v->iv, sizeof v->iv);
165 			hexdump(stdout, "in", v->in, v->length);
166 			hexdump(stdout, "expected", v->out, v->length);
167 			hexdump(stdout, "got", buf, v->length);
168 
169 			++errs;
170 			}
171 		}
172 
173 	return errs;
174 	}
175 
main(int argc,char ** argv)176 int main(int argc, char **argv)
177 	{
178 	unsigned char rkey[16];
179 	unsigned char rkey2[16];
180 	AES_KEY key;
181 	AES_KEY key2;
182 	unsigned char plaintext[BIG_TEST_SIZE];
183 	unsigned char ciphertext[BIG_TEST_SIZE];
184 	unsigned char checktext[BIG_TEST_SIZE];
185 	unsigned char iv[AES_BLOCK_SIZE*4];
186 	unsigned char saved_iv[AES_BLOCK_SIZE*4];
187 	int err = 0;
188 	unsigned int n;
189 	unsigned matches;
190 
191 	assert(BIG_TEST_SIZE >= TEST_SIZE);
192 
193 	arc4random_buf(rkey, sizeof(rkey));
194 	arc4random_buf(plaintext, sizeof(plaintext));
195 	arc4random_buf(iv, sizeof(iv));
196 	memcpy(saved_iv, iv, sizeof(saved_iv));
197 
198 	/* Forward IGE only... */
199 
200 	/* Straight encrypt/decrypt */
201 	AES_set_encrypt_key(rkey, 8*sizeof rkey, &key);
202 	AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE, &key, iv,
203 					AES_ENCRYPT);
204 
205 	AES_set_decrypt_key(rkey, 8*sizeof rkey, &key);
206 	memcpy(iv, saved_iv, sizeof iv);
207 	AES_ige_encrypt(ciphertext, checktext, TEST_SIZE, &key, iv,
208 					AES_DECRYPT);
209 
210 	if(memcmp(checktext, plaintext, TEST_SIZE))
211 		{
212 		printf("Encrypt+decrypt doesn't match\n");
213 		hexdump(stdout, "Plaintext", plaintext, TEST_SIZE);
214 		hexdump(stdout, "Checktext", checktext, TEST_SIZE);
215 		++err;
216 		}
217 
218 	/* Now check encrypt chaining works */
219 	AES_set_encrypt_key(rkey, 8*sizeof rkey, &key);
220 	memcpy(iv, saved_iv, sizeof iv);
221 	AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE/2, &key, iv,
222 					AES_ENCRYPT);
223 	AES_ige_encrypt(plaintext+TEST_SIZE/2,
224 					ciphertext+TEST_SIZE/2, TEST_SIZE/2,
225 					&key, iv, AES_ENCRYPT);
226 
227 	AES_set_decrypt_key(rkey, 8*sizeof rkey, &key);
228 	memcpy(iv, saved_iv, sizeof iv);
229 	AES_ige_encrypt(ciphertext, checktext, TEST_SIZE, &key, iv,
230 					AES_DECRYPT);
231 
232 	if(memcmp(checktext, plaintext, TEST_SIZE))
233 		{
234 		printf("Chained encrypt+decrypt doesn't match\n");
235 		hexdump(stdout, "Plaintext", plaintext, TEST_SIZE);
236 		hexdump(stdout, "Checktext", checktext, TEST_SIZE);
237 		++err;
238 		}
239 
240 	/* And check decrypt chaining */
241 	AES_set_encrypt_key(rkey, 8*sizeof rkey, &key);
242 	memcpy(iv, saved_iv, sizeof iv);
243 	AES_ige_encrypt(plaintext, ciphertext, TEST_SIZE/2, &key, iv,
244 					AES_ENCRYPT);
245 	AES_ige_encrypt(plaintext+TEST_SIZE/2,
246 					ciphertext+TEST_SIZE/2, TEST_SIZE/2,
247 					&key, iv, AES_ENCRYPT);
248 
249 	AES_set_decrypt_key(rkey, 8*sizeof rkey, &key);
250 	memcpy(iv, saved_iv, sizeof iv);
251 	AES_ige_encrypt(ciphertext, checktext, TEST_SIZE/2, &key, iv,
252 					AES_DECRYPT);
253 	AES_ige_encrypt(ciphertext+TEST_SIZE/2,
254 					checktext+TEST_SIZE/2, TEST_SIZE/2, &key, iv,
255 					AES_DECRYPT);
256 
257 	if(memcmp(checktext, plaintext, TEST_SIZE))
258 		{
259 		printf("Chained encrypt+chained decrypt doesn't match\n");
260 		hexdump(stdout, "Plaintext", plaintext, TEST_SIZE);
261 		hexdump(stdout, "Checktext", checktext, TEST_SIZE);
262 		++err;
263 		}
264 
265 	/* make sure garble extends forwards only */
266 	AES_set_encrypt_key(rkey, 8*sizeof rkey, &key);
267 	memcpy(iv, saved_iv, sizeof iv);
268 	AES_ige_encrypt(plaintext, ciphertext, sizeof plaintext, &key, iv,
269 					AES_ENCRYPT);
270 
271 	/* corrupt halfway through */
272 	++ciphertext[sizeof ciphertext/2];
273 	AES_set_decrypt_key(rkey, 8*sizeof rkey, &key);
274 	memcpy(iv, saved_iv, sizeof iv);
275 	AES_ige_encrypt(ciphertext, checktext, sizeof checktext, &key, iv,
276 					AES_DECRYPT);
277 
278 	matches=0;
279 	for(n=0 ; n < sizeof checktext ; ++n)
280 		if(checktext[n] == plaintext[n])
281 			++matches;
282 
283 	if(matches > sizeof checktext/2+sizeof checktext/100)
284 		{
285 		printf("More than 51%% matches after garbling\n");
286 		++err;
287 		}
288 
289 	if(matches < sizeof checktext/2)
290 		{
291 		printf("Garble extends backwards!\n");
292 		++err;
293 		}
294 
295 	/* make sure garble extends both ways */
296 	AES_set_encrypt_key(rkey, 8*sizeof rkey, &key);
297 	AES_set_encrypt_key(rkey2, 8*sizeof rkey2, &key2);
298 	AES_ige_encrypt(plaintext, ciphertext, sizeof plaintext, &key, iv,
299 					AES_ENCRYPT);
300 
301 	/* corrupt halfway through */
302 	++ciphertext[sizeof ciphertext/2];
303 	AES_set_decrypt_key(rkey, 8*sizeof rkey, &key);
304 	AES_set_decrypt_key(rkey2, 8*sizeof rkey2, &key2);
305 	AES_ige_encrypt(ciphertext, checktext, sizeof checktext, &key, iv,
306 					AES_DECRYPT);
307 
308 	matches=0;
309 	for(n=0 ; n < sizeof checktext ; ++n)
310 		if(checktext[n] == plaintext[n])
311 			++matches;
312 
313 	if(matches > sizeof checktext/100)
314 		{
315 		printf("More than 1%% matches after bidirectional garbling\n");
316 		++err;
317 		}
318 
319 	/* make sure garble extends both ways (2) */
320 	AES_set_encrypt_key(rkey, 8*sizeof rkey, &key);
321 	AES_set_encrypt_key(rkey2, 8*sizeof rkey2, &key2);
322 	AES_ige_encrypt(plaintext, ciphertext, sizeof plaintext, &key, iv,
323 					AES_ENCRYPT);
324 
325 	/* corrupt right at the end */
326 	++ciphertext[sizeof ciphertext-1];
327 	AES_set_decrypt_key(rkey, 8*sizeof rkey, &key);
328 	AES_set_decrypt_key(rkey2, 8*sizeof rkey2, &key2);
329 	AES_ige_encrypt(ciphertext, checktext, sizeof checktext, &key, iv,
330 					AES_DECRYPT);
331 
332 	matches=0;
333 	for(n=0 ; n < sizeof checktext ; ++n)
334 		if(checktext[n] == plaintext[n])
335 			++matches;
336 
337 	if(matches > sizeof checktext/100)
338 		{
339 		printf("More than 1%% matches after bidirectional garbling (2)\n");
340 		++err;
341 		}
342 
343 	/* make sure garble extends both ways (3) */
344 	AES_set_encrypt_key(rkey, 8*sizeof rkey, &key);
345 	AES_set_encrypt_key(rkey2, 8*sizeof rkey2, &key2);
346 	AES_ige_encrypt(plaintext, ciphertext, sizeof plaintext, &key, iv,
347 					AES_ENCRYPT);
348 
349 	/* corrupt right at the start */
350 	++ciphertext[0];
351 	AES_set_decrypt_key(rkey, 8*sizeof rkey, &key);
352 	AES_set_decrypt_key(rkey2, 8*sizeof rkey2, &key2);
353 	AES_ige_encrypt(ciphertext, checktext, sizeof checktext, &key, iv,
354 					AES_DECRYPT);
355 
356 	matches=0;
357 	for(n=0 ; n < sizeof checktext ; ++n)
358 		if(checktext[n] == plaintext[n])
359 			++matches;
360 
361 	if(matches > sizeof checktext/100)
362 		{
363 		printf("More than 1%% matches after bidirectional garbling (3)\n");
364 		++err;
365 		}
366 
367 	err += run_test_vectors();
368 
369 	return err;
370 	}
371