comparison demos/tv_gen.c @ 0:d7da3b1e1540 libtomcrypt

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