3
|
1 #include <mycrypt.h> |
|
2 |
|
3 void reg_algs(void) |
|
4 { |
143
|
5 int err; |
|
6 |
3
|
7 #ifdef RIJNDAEL |
|
8 register_cipher (&aes_desc); |
|
9 #endif |
|
10 #ifdef BLOWFISH |
|
11 register_cipher (&blowfish_desc); |
|
12 #endif |
|
13 #ifdef XTEA |
|
14 register_cipher (&xtea_desc); |
|
15 #endif |
|
16 #ifdef RC5 |
|
17 register_cipher (&rc5_desc); |
|
18 #endif |
|
19 #ifdef RC6 |
|
20 register_cipher (&rc6_desc); |
|
21 #endif |
|
22 #ifdef SAFERP |
|
23 register_cipher (&saferp_desc); |
|
24 #endif |
|
25 #ifdef TWOFISH |
|
26 register_cipher (&twofish_desc); |
|
27 #endif |
|
28 #ifdef SAFER |
|
29 register_cipher (&safer_k64_desc); |
|
30 register_cipher (&safer_sk64_desc); |
|
31 register_cipher (&safer_k128_desc); |
|
32 register_cipher (&safer_sk128_desc); |
|
33 #endif |
|
34 #ifdef RC2 |
|
35 register_cipher (&rc2_desc); |
|
36 #endif |
|
37 #ifdef DES |
|
38 register_cipher (&des_desc); |
|
39 register_cipher (&des3_desc); |
|
40 #endif |
|
41 #ifdef CAST5 |
|
42 register_cipher (&cast5_desc); |
|
43 #endif |
|
44 #ifdef NOEKEON |
|
45 register_cipher (&noekeon_desc); |
|
46 #endif |
|
47 #ifdef SKIPJACK |
|
48 register_cipher (&skipjack_desc); |
|
49 #endif |
|
50 |
|
51 #ifdef TIGER |
|
52 register_hash (&tiger_desc); |
|
53 #endif |
|
54 #ifdef MD2 |
|
55 register_hash (&md2_desc); |
|
56 #endif |
|
57 #ifdef MD4 |
|
58 register_hash (&md4_desc); |
|
59 #endif |
|
60 #ifdef MD5 |
|
61 register_hash (&md5_desc); |
|
62 #endif |
|
63 #ifdef SHA1 |
|
64 register_hash (&sha1_desc); |
|
65 #endif |
|
66 #ifdef SHA224 |
|
67 register_hash (&sha224_desc); |
|
68 #endif |
|
69 #ifdef SHA256 |
|
70 register_hash (&sha256_desc); |
|
71 #endif |
|
72 #ifdef SHA384 |
|
73 register_hash (&sha384_desc); |
|
74 #endif |
|
75 #ifdef SHA512 |
|
76 register_hash (&sha512_desc); |
|
77 #endif |
|
78 #ifdef RIPEMD128 |
|
79 register_hash (&rmd128_desc); |
|
80 #endif |
|
81 #ifdef RIPEMD160 |
|
82 register_hash (&rmd160_desc); |
|
83 #endif |
|
84 #ifdef WHIRLPOOL |
|
85 register_hash (&whirlpool_desc); |
|
86 #endif |
143
|
87 #ifdef CHC_HASH |
|
88 register_hash(&chc_desc); |
|
89 if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) { |
|
90 printf("chc_register error: %s\n", error_to_string(err)); |
|
91 exit(EXIT_FAILURE); |
|
92 } |
|
93 #endif |
|
94 |
3
|
95 } |
|
96 |
|
97 void hash_gen(void) |
|
98 { |
143
|
99 unsigned char md[MAXBLOCKSIZE], *buf; |
3
|
100 unsigned long outlen, x, y, z; |
|
101 FILE *out; |
143
|
102 int err; |
3
|
103 |
|
104 out = fopen("hash_tv.txt", "w"); |
143
|
105 if (out == NULL) { |
|
106 perror("can't open hash_tv"); |
|
107 } |
3
|
108 |
|
109 fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n"); |
|
110 for (x = 0; hash_descriptor[x].name != NULL; x++) { |
143
|
111 buf = XMALLOC(2 * hash_descriptor[x].blocksize + 1); |
|
112 if (buf == NULL) { |
|
113 perror("can't alloc mem"); |
|
114 exit(EXIT_FAILURE); |
|
115 } |
3
|
116 fprintf(out, "Hash: %s\n", hash_descriptor[x].name); |
|
117 for (y = 0; y <= (hash_descriptor[x].blocksize * 2); y++) { |
|
118 for (z = 0; z < y; z++) { |
|
119 buf[z] = (unsigned char)(z & 255); |
|
120 } |
|
121 outlen = sizeof(md); |
143
|
122 if ((err = hash_memory(x, buf, y, md, &outlen)) != CRYPT_OK) { |
|
123 printf("hash_memory error: %s\n", error_to_string(err)); |
|
124 exit(EXIT_FAILURE); |
|
125 } |
3
|
126 fprintf(out, "%3lu: ", y); |
|
127 for (z = 0; z < outlen; z++) { |
|
128 fprintf(out, "%02X", md[z]); |
|
129 } |
|
130 fprintf(out, "\n"); |
|
131 } |
|
132 fprintf(out, "\n"); |
143
|
133 XFREE(buf); |
3
|
134 } |
|
135 fclose(out); |
|
136 } |
|
137 |
|
138 void cipher_gen(void) |
|
139 { |
143
|
140 unsigned char *key, pt[MAXBLOCKSIZE]; |
3
|
141 unsigned long x, y, z, w; |
143
|
142 int err, kl, lastkl; |
3
|
143 FILE *out; |
|
144 symmetric_key skey; |
|
145 |
|
146 out = fopen("cipher_tv.txt", "w"); |
|
147 |
|
148 fprintf(out, |
|
149 "Cipher Test Vectors\n\nThese are test encryptions with key of nn bytes '00 01 02 03 .. (nn-1)' and original PT of the same style.\n" |
|
150 "The output of step N is used as the key and plaintext for step N+1 (key bytes repeated as required to fill the key)\n\n"); |
|
151 |
|
152 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
|
153 fprintf(out, "Cipher: %s\n", cipher_descriptor[x].name); |
|
154 |
|
155 /* three modes, smallest, medium, large keys */ |
|
156 lastkl = 10000; |
|
157 for (y = 0; y < 3; y++) { |
|
158 switch (y) { |
|
159 case 0: kl = cipher_descriptor[x].min_key_length; break; |
|
160 case 1: kl = (cipher_descriptor[x].min_key_length + cipher_descriptor[x].max_key_length)/2; break; |
|
161 case 2: kl = cipher_descriptor[x].max_key_length; break; |
|
162 } |
143
|
163 if ((err = cipher_descriptor[x].keysize(&kl)) != CRYPT_OK) { |
|
164 printf("keysize error: %s\n", error_to_string(err)); |
|
165 exit(EXIT_FAILURE); |
|
166 } |
3
|
167 if (kl == lastkl) break; |
|
168 lastkl = kl; |
|
169 fprintf(out, "Key Size: %d bytes\n", kl); |
|
170 |
143
|
171 key = XMALLOC(kl); |
|
172 if (key == NULL) { |
|
173 perror("can't malloc memory"); |
|
174 exit(EXIT_FAILURE); |
|
175 } |
|
176 |
3
|
177 for (z = 0; (int)z < kl; z++) { |
|
178 key[z] = (unsigned char)z; |
|
179 } |
143
|
180 if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) { |
|
181 printf("setup error: %s\n", error_to_string(err)); |
|
182 exit(EXIT_FAILURE); |
|
183 } |
3
|
184 |
|
185 for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) { |
|
186 pt[z] = (unsigned char)z; |
|
187 } |
|
188 for (w = 0; w < 50; w++) { |
|
189 cipher_descriptor[x].ecb_encrypt(pt, pt, &skey); |
|
190 fprintf(out, "%2lu: ", w); |
|
191 for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) { |
|
192 fprintf(out, "%02X", pt[z]); |
|
193 } |
|
194 fprintf(out, "\n"); |
|
195 |
|
196 /* reschedule a new key */ |
|
197 for (z = 0; z < (unsigned long)kl; z++) { |
|
198 key[z] = pt[z % cipher_descriptor[x].block_length]; |
|
199 } |
143
|
200 if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) { |
|
201 printf("cipher setup2 error: %s\n", error_to_string(err)); |
|
202 exit(EXIT_FAILURE); |
|
203 } |
3
|
204 } |
|
205 fprintf(out, "\n"); |
143
|
206 XFREE(key); |
3
|
207 } |
|
208 fprintf(out, "\n"); |
|
209 } |
|
210 fclose(out); |
|
211 } |
|
212 |
|
213 void hmac_gen(void) |
|
214 { |
143
|
215 unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], *input; |
|
216 int x, y, z, err; |
3
|
217 FILE *out; |
|
218 unsigned long len; |
|
219 |
|
220 out = fopen("hmac_tv.txt", "w"); |
|
221 |
|
222 fprintf(out, |
|
223 "HMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed. The initial key is\n" |
|
224 "of the same format (the same length as the HASH output size). The HMAC key in step N+1 is the HMAC output of\n" |
|
225 "step N.\n\n"); |
|
226 |
|
227 for (x = 0; hash_descriptor[x].name != NULL; x++) { |
|
228 fprintf(out, "HMAC-%s\n", hash_descriptor[x].name); |
|
229 |
|
230 /* initial key */ |
|
231 for (y = 0; y < (int)hash_descriptor[x].hashsize; y++) { |
|
232 key[y] = (y&255); |
|
233 } |
143
|
234 |
|
235 input = XMALLOC(hash_descriptor[x].blocksize * 2 + 1); |
|
236 if (input == NULL) { |
|
237 perror("Can't malloc memory"); |
|
238 exit(EXIT_FAILURE); |
|
239 } |
3
|
240 |
|
241 for (y = 0; y <= (int)(hash_descriptor[x].blocksize * 2); y++) { |
|
242 for (z = 0; z < y; z++) { |
|
243 input[z] = (unsigned char)(z & 255); |
|
244 } |
|
245 len = sizeof(output); |
|
246 if ((err = hmac_memory(x, key, hash_descriptor[x].hashsize, input, y, output, &len)) != CRYPT_OK) { |
|
247 printf("Error hmacing: %s\n", error_to_string(err)); |
|
248 exit(EXIT_FAILURE); |
|
249 } |
|
250 fprintf(out, "%3d: ", y); |
|
251 for (z = 0; z <(int) len; z++) { |
|
252 fprintf(out, "%02X", output[z]); |
|
253 } |
|
254 fprintf(out, "\n"); |
|
255 |
|
256 /* forward the key */ |
|
257 memcpy(key, output, hash_descriptor[x].hashsize); |
|
258 } |
143
|
259 XFREE(input); |
3
|
260 fprintf(out, "\n"); |
|
261 } |
|
262 fclose(out); |
|
263 } |
|
264 |
|
265 void omac_gen(void) |
|
266 { |
|
267 unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2]; |
|
268 int err, x, y, z, kl; |
|
269 FILE *out; |
|
270 unsigned long len; |
|
271 |
|
272 out = fopen("omac_tv.txt", "w"); |
|
273 |
|
274 fprintf(out, |
|
275 "OMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n" |
|
276 "of the same format (length specified per cipher). The OMAC key in step N+1 is the OMAC output of\n" |
|
277 "step N (repeated as required to fill the array).\n\n"); |
|
278 |
|
279 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
|
280 kl = cipher_descriptor[x].block_length; |
|
281 |
|
282 /* skip ciphers which do not have 64 or 128 bit block sizes */ |
|
283 if (kl != 8 && kl != 16) continue; |
|
284 |
|
285 if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) { |
|
286 kl = cipher_descriptor[x].max_key_length; |
|
287 } |
|
288 fprintf(out, "OMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl); |
|
289 |
|
290 /* initial key/block */ |
|
291 for (y = 0; y < kl; y++) { |
|
292 key[y] = (y & 255); |
|
293 } |
|
294 |
|
295 for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) { |
|
296 for (z = 0; z < y; z++) { |
|
297 input[z] = (unsigned char)(z & 255); |
|
298 } |
|
299 len = sizeof(output); |
|
300 if ((err = omac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) { |
|
301 printf("Error omacing: %s\n", error_to_string(err)); |
|
302 exit(EXIT_FAILURE); |
|
303 } |
|
304 fprintf(out, "%3d: ", y); |
|
305 for (z = 0; z <(int)len; z++) { |
|
306 fprintf(out, "%02X", output[z]); |
|
307 } |
|
308 fprintf(out, "\n"); |
|
309 |
|
310 /* forward the key */ |
|
311 for (z = 0; z < kl; z++) { |
|
312 key[z] = output[z % len]; |
|
313 } |
|
314 } |
|
315 fprintf(out, "\n"); |
|
316 } |
|
317 fclose(out); |
|
318 } |
|
319 |
|
320 void pmac_gen(void) |
|
321 { |
|
322 unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2]; |
|
323 int err, x, y, z, kl; |
|
324 FILE *out; |
|
325 unsigned long len; |
|
326 |
|
327 out = fopen("pmac_tv.txt", "w"); |
|
328 |
|
329 fprintf(out, |
|
330 "PMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n" |
|
331 "of the same format (length specified per cipher). The OMAC key in step N+1 is the OMAC output of\n" |
|
332 "step N (repeated as required to fill the array).\n\n"); |
|
333 |
|
334 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
|
335 kl = cipher_descriptor[x].block_length; |
|
336 |
|
337 /* skip ciphers which do not have 64 or 128 bit block sizes */ |
|
338 if (kl != 8 && kl != 16) continue; |
|
339 |
|
340 if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) { |
|
341 kl = cipher_descriptor[x].max_key_length; |
|
342 } |
|
343 fprintf(out, "PMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl); |
|
344 |
|
345 /* initial key/block */ |
|
346 for (y = 0; y < kl; y++) { |
|
347 key[y] = (y & 255); |
|
348 } |
|
349 |
|
350 for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) { |
|
351 for (z = 0; z < y; z++) { |
|
352 input[z] = (unsigned char)(z & 255); |
|
353 } |
|
354 len = sizeof(output); |
|
355 if ((err = pmac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) { |
|
356 printf("Error omacing: %s\n", error_to_string(err)); |
|
357 exit(EXIT_FAILURE); |
|
358 } |
|
359 fprintf(out, "%3d: ", y); |
|
360 for (z = 0; z <(int)len; z++) { |
|
361 fprintf(out, "%02X", output[z]); |
|
362 } |
|
363 fprintf(out, "\n"); |
|
364 |
|
365 /* forward the key */ |
|
366 for (z = 0; z < kl; z++) { |
|
367 key[z] = output[z % len]; |
|
368 } |
|
369 } |
|
370 fprintf(out, "\n"); |
|
371 } |
|
372 fclose(out); |
|
373 } |
|
374 |
|
375 void eax_gen(void) |
|
376 { |
|
377 int err, kl, x, y1, z; |
|
378 FILE *out; |
|
379 unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], header[MAXBLOCKSIZE*2], |
|
380 plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE]; |
|
381 unsigned long len; |
|
382 |
|
383 out = fopen("eax_tv.txt", "w"); |
|
384 fprintf(out, "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n" |
|
385 "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n" |
|
386 "step repeated sufficiently.\n\n"); |
|
387 |
|
388 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
|
389 kl = cipher_descriptor[x].block_length; |
|
390 |
|
391 /* skip ciphers which do not have 64 or 128 bit block sizes */ |
|
392 if (kl != 8 && kl != 16) continue; |
|
393 |
|
394 if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) { |
|
395 kl = cipher_descriptor[x].max_key_length; |
|
396 } |
|
397 fprintf(out, "EAX-%s (%d byte key)\n", cipher_descriptor[x].name, kl); |
|
398 |
|
399 /* the key */ |
|
400 for (z = 0; z < kl; z++) { |
|
401 key[z] = (z & 255); |
|
402 } |
|
403 |
|
404 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){ |
|
405 for (z = 0; z < y1; z++) { |
|
406 plaintext[z] = (unsigned char)(z & 255); |
|
407 nonce[z] = (unsigned char)(z & 255); |
|
408 header[z] = (unsigned char)(z & 255); |
|
409 } |
|
410 len = sizeof(tag); |
|
411 if ((err = eax_encrypt_authenticate_memory(x, key, kl, nonce, y1, header, y1, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) { |
|
412 printf("Error EAX'ing: %s\n", error_to_string(err)); |
|
413 exit(EXIT_FAILURE); |
|
414 } |
|
415 fprintf(out, "%3d: ", y1); |
|
416 for (z = 0; z < y1; z++) { |
|
417 fprintf(out, "%02X", plaintext[z]); |
|
418 } |
|
419 fprintf(out, ", "); |
|
420 for (z = 0; z <(int)len; z++) { |
|
421 fprintf(out, "%02X", tag[z]); |
|
422 } |
|
423 fprintf(out, "\n"); |
|
424 |
|
425 /* forward the key */ |
|
426 for (z = 0; z < kl; z++) { |
|
427 key[z] = tag[z % len]; |
|
428 } |
|
429 } |
|
430 fprintf(out, "\n"); |
|
431 } |
|
432 fclose(out); |
|
433 } |
|
434 |
|
435 void ocb_gen(void) |
|
436 { |
|
437 int err, kl, x, y1, z; |
|
438 FILE *out; |
|
439 unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], |
|
440 plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE]; |
|
441 unsigned long len; |
|
442 |
|
443 out = fopen("ocb_tv.txt", "w"); |
|
444 fprintf(out, "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n" |
|
445 "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n" |
|
446 "step repeated sufficiently. The nonce is fixed throughout.\n\n"); |
|
447 |
|
448 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
|
449 kl = cipher_descriptor[x].block_length; |
|
450 |
|
451 /* skip ciphers which do not have 64 or 128 bit block sizes */ |
|
452 if (kl != 8 && kl != 16) continue; |
|
453 |
|
454 if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) { |
|
455 kl = cipher_descriptor[x].max_key_length; |
|
456 } |
|
457 fprintf(out, "OCB-%s (%d byte key)\n", cipher_descriptor[x].name, kl); |
|
458 |
|
459 /* the key */ |
|
460 for (z = 0; z < kl; z++) { |
|
461 key[z] = (z & 255); |
|
462 } |
|
463 |
|
464 /* fixed nonce */ |
|
465 for (z = 0; z < cipher_descriptor[x].block_length; z++) { |
|
466 nonce[z] = z; |
|
467 } |
|
468 |
|
469 for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){ |
|
470 for (z = 0; z < y1; z++) { |
|
471 plaintext[z] = (unsigned char)(z & 255); |
|
472 } |
|
473 len = sizeof(tag); |
|
474 if ((err = ocb_encrypt_authenticate_memory(x, key, kl, nonce, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) { |
|
475 printf("Error OCB'ing: %s\n", error_to_string(err)); |
|
476 exit(EXIT_FAILURE); |
|
477 } |
|
478 fprintf(out, "%3d: ", y1); |
|
479 for (z = 0; z < y1; z++) { |
|
480 fprintf(out, "%02X", plaintext[z]); |
|
481 } |
|
482 fprintf(out, ", "); |
|
483 for (z = 0; z <(int)len; z++) { |
|
484 fprintf(out, "%02X", tag[z]); |
|
485 } |
|
486 fprintf(out, "\n"); |
|
487 |
|
488 /* forward the key */ |
|
489 for (z = 0; z < kl; z++) { |
|
490 key[z] = tag[z % len]; |
|
491 } |
|
492 } |
|
493 fprintf(out, "\n"); |
|
494 } |
|
495 fclose(out); |
|
496 } |
|
497 |
|
498 void base64_gen(void) |
|
499 { |
|
500 FILE *out; |
|
501 unsigned char dst[256], src[32]; |
|
502 unsigned long x, y, len; |
|
503 |
|
504 out = fopen("base64_tv.txt", "w"); |
|
505 fprintf(out, "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n"); |
|
506 for (x = 0; x <= 32; x++) { |
|
507 for (y = 0; y < x; y++) { |
|
508 src[y] = y; |
|
509 } |
|
510 len = sizeof(dst); |
|
511 base64_encode(src, x, dst, &len); |
|
512 fprintf(out, "%2lu: %s\n", x, dst); |
|
513 } |
|
514 fclose(out); |
|
515 } |
|
516 |
|
517 int main(void) |
|
518 { |
|
519 reg_algs(); |
|
520 printf("Generating hash vectors..."); fflush(stdout); hash_gen(); printf("done\n"); |
|
521 printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n"); |
|
522 printf("Generating HMAC vectors..."); fflush(stdout); hmac_gen(); printf("done\n"); |
|
523 printf("Generating OMAC vectors..."); fflush(stdout); omac_gen(); printf("done\n"); |
|
524 printf("Generating PMAC vectors..."); fflush(stdout); pmac_gen(); printf("done\n"); |
|
525 printf("Generating EAX vectors..."); fflush(stdout); eax_gen(); printf("done\n"); |
|
526 printf("Generating OCB vectors..."); fflush(stdout); ocb_gen(); printf("done\n"); |
|
527 printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n"); |
|
528 return 0; |
|
529 } |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |