• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <tomcrypt.h>
2 
reg_algs(void)3 void reg_algs(void)
4 {
5   int err;
6 
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 #ifdef ANUBIS
51   register_cipher (&anubis_desc);
52 #endif
53 #ifdef KHAZAD
54   register_cipher (&khazad_desc);
55 #endif
56 
57 #ifdef TIGER
58   register_hash (&tiger_desc);
59 #endif
60 #ifdef MD2
61   register_hash (&md2_desc);
62 #endif
63 #ifdef MD4
64   register_hash (&md4_desc);
65 #endif
66 #ifdef MD5
67   register_hash (&md5_desc);
68 #endif
69 #ifdef SHA1
70   register_hash (&sha1_desc);
71 #endif
72 #ifdef SHA224
73   register_hash (&sha224_desc);
74 #endif
75 #ifdef SHA256
76   register_hash (&sha256_desc);
77 #endif
78 #ifdef SHA384
79   register_hash (&sha384_desc);
80 #endif
81 #ifdef SHA512
82   register_hash (&sha512_desc);
83 #endif
84 #ifdef RIPEMD128
85   register_hash (&rmd128_desc);
86 #endif
87 #ifdef RIPEMD160
88   register_hash (&rmd160_desc);
89 #endif
90 #ifdef WHIRLPOOL
91   register_hash (&whirlpool_desc);
92 #endif
93 #ifdef CHC_HASH
94   register_hash(&chc_desc);
95   if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) {
96      printf("chc_register error: %s\n", error_to_string(err));
97      exit(EXIT_FAILURE);
98   }
99 #endif
100 
101 #ifdef USE_LTM
102    ltc_mp = ltm_desc;
103 #elif defined(USE_TFM)
104    ltc_mp = tfm_desc;
105 #elif defined(USE_GMP)
106    ltc_mp = gmp_desc;
107 #else
108    extern ltc_math_descriptor EXT_MATH_LIB;
109    ltc_mp = EXT_MATH_LIB;
110 #endif
111 
112 
113 }
114 
hash_gen(void)115 void hash_gen(void)
116 {
117    unsigned char md[MAXBLOCKSIZE], *buf;
118    unsigned long outlen, x, y, z;
119    FILE *out;
120    int   err;
121 
122    out = fopen("hash_tv.txt", "w");
123    if (out == NULL) {
124       perror("can't open hash_tv");
125    }
126 
127    fprintf(out, "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n");
128    for (x = 0; hash_descriptor[x].name != NULL; x++) {
129       buf = XMALLOC(2 * hash_descriptor[x].blocksize + 1);
130       if (buf == NULL) {
131          perror("can't alloc mem");
132          exit(EXIT_FAILURE);
133       }
134       fprintf(out, "Hash: %s\n", hash_descriptor[x].name);
135       for (y = 0; y <= (hash_descriptor[x].blocksize * 2); y++) {
136          for (z = 0; z < y; z++) {
137             buf[z] = (unsigned char)(z & 255);
138          }
139          outlen = sizeof(md);
140          if ((err = hash_memory(x, buf, y, md, &outlen)) != CRYPT_OK) {
141             printf("hash_memory error: %s\n", error_to_string(err));
142             exit(EXIT_FAILURE);
143          }
144          fprintf(out, "%3lu: ", y);
145          for (z = 0; z < outlen; z++) {
146             fprintf(out, "%02X", md[z]);
147          }
148          fprintf(out, "\n");
149       }
150       fprintf(out, "\n");
151       XFREE(buf);
152    }
153    fclose(out);
154 }
155 
cipher_gen(void)156 void cipher_gen(void)
157 {
158    unsigned char *key, pt[MAXBLOCKSIZE];
159    unsigned long x, y, z, w;
160    int err, kl, lastkl;
161    FILE *out;
162    symmetric_key skey;
163 
164    out = fopen("cipher_tv.txt", "w");
165 
166    fprintf(out,
167 "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"
168 "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");
169 
170    for (x = 0; cipher_descriptor[x].name != NULL; x++) {
171       fprintf(out, "Cipher: %s\n", cipher_descriptor[x].name);
172 
173       /* three modes, smallest, medium, large keys */
174       lastkl = 10000;
175       for (y = 0; y < 3; y++) {
176          switch (y) {
177             case 0: kl = cipher_descriptor[x].min_key_length; break;
178             case 1: kl = (cipher_descriptor[x].min_key_length + cipher_descriptor[x].max_key_length)/2; break;
179             case 2: kl = cipher_descriptor[x].max_key_length; break;
180          }
181          if ((err = cipher_descriptor[x].keysize(&kl)) != CRYPT_OK) {
182             printf("keysize error: %s\n", error_to_string(err));
183             exit(EXIT_FAILURE);
184          }
185          if (kl == lastkl) break;
186          lastkl = kl;
187          fprintf(out, "Key Size: %d bytes\n", kl);
188 
189          key = XMALLOC(kl);
190          if (key == NULL) {
191             perror("can't malloc memory");
192             exit(EXIT_FAILURE);
193          }
194 
195          for (z = 0; (int)z < kl; z++) {
196              key[z] = (unsigned char)z;
197          }
198          if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {
199             printf("setup error: %s\n", error_to_string(err));
200             exit(EXIT_FAILURE);
201          }
202 
203          for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {
204             pt[z] = (unsigned char)z;
205          }
206          for (w = 0; w < 50; w++) {
207              cipher_descriptor[x].ecb_encrypt(pt, pt, &skey);
208              fprintf(out, "%2lu: ", w);
209              for (z = 0; (int)z < cipher_descriptor[x].block_length; z++) {
210                 fprintf(out, "%02X", pt[z]);
211              }
212              fprintf(out, "\n");
213 
214              /* reschedule a new key */
215              for (z = 0; z < (unsigned long)kl; z++) {
216                  key[z] = pt[z % cipher_descriptor[x].block_length];
217              }
218              if ((err = cipher_descriptor[x].setup(key, kl, 0, &skey)) != CRYPT_OK) {
219                 printf("cipher setup2 error: %s\n", error_to_string(err));
220                 exit(EXIT_FAILURE);
221              }
222          }
223          fprintf(out, "\n");
224          XFREE(key);
225      }
226      fprintf(out, "\n");
227   }
228   fclose(out);
229 }
230 
hmac_gen(void)231 void hmac_gen(void)
232 {
233    unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], *input;
234    int x, y, z, err;
235    FILE *out;
236    unsigned long len;
237 
238    out = fopen("hmac_tv.txt", "w");
239 
240    fprintf(out,
241 "HMAC Tests.  In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed.  The initial key is\n"
242 "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"
243 "step N.\n\n");
244 
245    for (x = 0; hash_descriptor[x].name != NULL; x++) {
246       fprintf(out, "HMAC-%s\n", hash_descriptor[x].name);
247 
248       /* initial key */
249       for (y = 0; y < (int)hash_descriptor[x].hashsize; y++) {
250           key[y] = (y&255);
251       }
252 
253       input = XMALLOC(hash_descriptor[x].blocksize * 2 + 1);
254       if (input == NULL) {
255          perror("Can't malloc memory");
256          exit(EXIT_FAILURE);
257       }
258 
259       for (y = 0; y <= (int)(hash_descriptor[x].blocksize * 2); y++) {
260          for (z = 0; z < y; z++) {
261             input[z] = (unsigned char)(z & 255);
262          }
263          len = sizeof(output);
264          if ((err = hmac_memory(x, key, hash_descriptor[x].hashsize, input, y, output, &len)) != CRYPT_OK) {
265             printf("Error hmacing: %s\n", error_to_string(err));
266             exit(EXIT_FAILURE);
267          }
268          fprintf(out, "%3d: ", y);
269          for (z = 0; z <(int) len; z++) {
270             fprintf(out, "%02X", output[z]);
271          }
272          fprintf(out, "\n");
273 
274          /* forward the key */
275          memcpy(key, output, hash_descriptor[x].hashsize);
276       }
277       XFREE(input);
278       fprintf(out, "\n");
279    }
280    fclose(out);
281 }
282 
omac_gen(void)283 void omac_gen(void)
284 {
285    unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
286    int err, x, y, z, kl;
287    FILE *out;
288    unsigned long len;
289 
290    out = fopen("omac_tv.txt", "w");
291 
292    fprintf(out,
293 "OMAC Tests.  In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed.  The initial key is\n"
294 "of the same format (length specified per cipher).  The OMAC key in step N+1 is the OMAC output of\n"
295 "step N (repeated as required to fill the array).\n\n");
296 
297    for (x = 0; cipher_descriptor[x].name != NULL; x++) {
298       kl = cipher_descriptor[x].block_length;
299 
300       /* skip ciphers which do not have 64 or 128 bit block sizes */
301       if (kl != 8 && kl != 16) continue;
302 
303       if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
304          kl = cipher_descriptor[x].max_key_length;
305       }
306       fprintf(out, "OMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
307 
308       /* initial key/block */
309       for (y = 0; y < kl; y++) {
310           key[y] = (y & 255);
311       }
312 
313       for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
314          for (z = 0; z < y; z++) {
315             input[z] = (unsigned char)(z & 255);
316          }
317          len = sizeof(output);
318          if ((err = omac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
319             printf("Error omacing: %s\n", error_to_string(err));
320             exit(EXIT_FAILURE);
321          }
322          fprintf(out, "%3d: ", y);
323          for (z = 0; z <(int)len; z++) {
324             fprintf(out, "%02X", output[z]);
325          }
326          fprintf(out, "\n");
327 
328          /* forward the key */
329          for (z = 0; z < kl; z++) {
330              key[z] = output[z % len];
331          }
332       }
333       fprintf(out, "\n");
334    }
335    fclose(out);
336 }
337 
pmac_gen(void)338 void pmac_gen(void)
339 {
340    unsigned char key[MAXBLOCKSIZE], output[MAXBLOCKSIZE], input[MAXBLOCKSIZE*2+2];
341    int err, x, y, z, kl;
342    FILE *out;
343    unsigned long len;
344 
345    out = fopen("pmac_tv.txt", "w");
346 
347    fprintf(out,
348 "PMAC Tests.  In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed.  The initial key is\n"
349 "of the same format (length specified per cipher).  The OMAC key in step N+1 is the OMAC output of\n"
350 "step N (repeated as required to fill the array).\n\n");
351 
352    for (x = 0; cipher_descriptor[x].name != NULL; x++) {
353       kl = cipher_descriptor[x].block_length;
354 
355       /* skip ciphers which do not have 64 or 128 bit block sizes */
356       if (kl != 8 && kl != 16) continue;
357 
358       if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
359          kl = cipher_descriptor[x].max_key_length;
360       }
361       fprintf(out, "PMAC-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
362 
363       /* initial key/block */
364       for (y = 0; y < kl; y++) {
365           key[y] = (y & 255);
366       }
367 
368       for (y = 0; y <= (int)(cipher_descriptor[x].block_length*2); y++) {
369          for (z = 0; z < y; z++) {
370             input[z] = (unsigned char)(z & 255);
371          }
372          len = sizeof(output);
373          if ((err = pmac_memory(x, key, kl, input, y, output, &len)) != CRYPT_OK) {
374             printf("Error omacing: %s\n", error_to_string(err));
375             exit(EXIT_FAILURE);
376          }
377          fprintf(out, "%3d: ", y);
378          for (z = 0; z <(int)len; z++) {
379             fprintf(out, "%02X", output[z]);
380          }
381          fprintf(out, "\n");
382 
383          /* forward the key */
384          for (z = 0; z < kl; z++) {
385              key[z] = output[z % len];
386          }
387       }
388       fprintf(out, "\n");
389    }
390    fclose(out);
391 }
392 
eax_gen(void)393 void eax_gen(void)
394 {
395    int err, kl, x, y1, z;
396    FILE *out;
397    unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2], header[MAXBLOCKSIZE*2],
398                  plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
399    unsigned long len;
400 
401    out = fopen("eax_tv.txt", "w");
402    fprintf(out, "EAX Test Vectors.  Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key.  The outputs\n"
403                 "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"
404                 "step repeated sufficiently.\n\n");
405 
406    for (x = 0; cipher_descriptor[x].name != NULL; x++) {
407       kl = cipher_descriptor[x].block_length;
408 
409       /* skip ciphers which do not have 64 or 128 bit block sizes */
410       if (kl != 8 && kl != 16) continue;
411 
412       if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
413          kl = cipher_descriptor[x].max_key_length;
414       }
415       fprintf(out, "EAX-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
416 
417       /* the key */
418       for (z = 0; z < kl; z++) {
419           key[z] = (z & 255);
420       }
421 
422       for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
423          for (z = 0; z < y1; z++) {
424             plaintext[z] = (unsigned char)(z & 255);
425             nonce[z]     = (unsigned char)(z & 255);
426             header[z]    = (unsigned char)(z & 255);
427          }
428          len = sizeof(tag);
429          if ((err = eax_encrypt_authenticate_memory(x, key, kl, nonce, y1, header, y1, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
430             printf("Error EAX'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 
ocb_gen(void)453 void ocb_gen(void)
454 {
455    int err, kl, x, y1, z;
456    FILE *out;
457    unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
458                  plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
459    unsigned long len;
460 
461    out = fopen("ocb_tv.txt", "w");
462    fprintf(out, "OCB Test Vectors.  Uses the 00010203...NN-1 pattern for nonce/plaintext/key.  The outputs\n"
463                 "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"
464                 "step repeated sufficiently.  The nonce is fixed throughout.\n\n");
465 
466    for (x = 0; cipher_descriptor[x].name != NULL; x++) {
467       kl = cipher_descriptor[x].block_length;
468 
469       /* skip ciphers which do not have 64 or 128 bit block sizes */
470       if (kl != 8 && kl != 16) continue;
471 
472       if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
473          kl = cipher_descriptor[x].max_key_length;
474       }
475       fprintf(out, "OCB-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
476 
477       /* the key */
478       for (z = 0; z < kl; z++) {
479           key[z] = (z & 255);
480       }
481 
482       /* fixed nonce */
483       for (z = 0; z < cipher_descriptor[x].block_length; z++) {
484           nonce[z] = z;
485       }
486 
487       for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
488          for (z = 0; z < y1; z++) {
489             plaintext[z] = (unsigned char)(z & 255);
490          }
491          len = sizeof(tag);
492          if ((err = ocb_encrypt_authenticate_memory(x, key, kl, nonce, plaintext, y1, plaintext, tag, &len)) != CRYPT_OK) {
493             printf("Error OCB'ing: %s\n", error_to_string(err));
494             exit(EXIT_FAILURE);
495          }
496          fprintf(out, "%3d: ", y1);
497          for (z = 0; z < y1; z++) {
498             fprintf(out, "%02X", plaintext[z]);
499          }
500          fprintf(out, ", ");
501          for (z = 0; z <(int)len; z++) {
502             fprintf(out, "%02X", tag[z]);
503          }
504          fprintf(out, "\n");
505 
506          /* forward the key */
507          for (z = 0; z < kl; z++) {
508              key[z] = tag[z % len];
509          }
510       }
511       fprintf(out, "\n");
512    }
513    fclose(out);
514 }
515 
516 
ccm_gen(void)517 void ccm_gen(void)
518 {
519    int err, kl, x, y1, z;
520    FILE *out;
521    unsigned char key[MAXBLOCKSIZE], nonce[MAXBLOCKSIZE*2],
522                  plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
523    unsigned long len;
524 
525    out = fopen("ccm_tv.txt", "w");
526    fprintf(out, "CCM Test Vectors.  Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key.  The outputs\n"
527                 "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"
528                 "step repeated sufficiently.  The nonce is fixed throughout at 13 bytes 000102...\n\n");
529 
530    for (x = 0; cipher_descriptor[x].name != NULL; x++) {
531       kl = cipher_descriptor[x].block_length;
532 
533       /* skip ciphers which do not have 128 bit block sizes */
534       if (kl != 16) continue;
535 
536       if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
537          kl = cipher_descriptor[x].max_key_length;
538       }
539       fprintf(out, "CCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
540 
541       /* the key */
542       for (z = 0; z < kl; z++) {
543           key[z] = (z & 255);
544       }
545 
546       /* fixed nonce */
547       for (z = 0; z < cipher_descriptor[x].block_length; z++) {
548           nonce[z] = z;
549       }
550 
551       for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
552          for (z = 0; z < y1; z++) {
553             plaintext[z] = (unsigned char)(z & 255);
554          }
555          len = sizeof(tag);
556          if ((err = ccm_memory(x, key, kl, NULL, nonce, 13, plaintext, y1, plaintext, y1, plaintext, tag, &len, CCM_ENCRYPT)) != CRYPT_OK) {
557             printf("Error CCM'ing: %s\n", error_to_string(err));
558             exit(EXIT_FAILURE);
559          }
560          fprintf(out, "%3d: ", y1);
561          for (z = 0; z < y1; z++) {
562             fprintf(out, "%02X", plaintext[z]);
563          }
564          fprintf(out, ", ");
565          for (z = 0; z <(int)len; z++) {
566             fprintf(out, "%02X", tag[z]);
567          }
568          fprintf(out, "\n");
569 
570          /* forward the key */
571          for (z = 0; z < kl; z++) {
572              key[z] = tag[z % len];
573          }
574       }
575       fprintf(out, "\n");
576    }
577    fclose(out);
578 }
579 
gcm_gen(void)580 void gcm_gen(void)
581 {
582    int err, kl, x, y1, z;
583    FILE *out;
584    unsigned char key[MAXBLOCKSIZE], plaintext[MAXBLOCKSIZE*2], tag[MAXBLOCKSIZE];
585    unsigned long len;
586 
587    out = fopen("gcm_tv.txt", "w");
588    fprintf(out, "GCM Test Vectors.  Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key.  The outputs\n"
589                 "are of the form ciphertext,tag for a given NN.  The key for step N>1 is the tag of the previous\n"
590                 "step repeated sufficiently.  The nonce is fixed throughout at 13 bytes 000102...\n\n");
591 
592    for (x = 0; cipher_descriptor[x].name != NULL; x++) {
593       kl = cipher_descriptor[x].block_length;
594 
595       /* skip ciphers which do not have 128 bit block sizes */
596       if (kl != 16) continue;
597 
598       if (cipher_descriptor[x].keysize(&kl) != CRYPT_OK) {
599          kl = cipher_descriptor[x].max_key_length;
600       }
601       fprintf(out, "GCM-%s (%d byte key)\n", cipher_descriptor[x].name, kl);
602 
603       /* the key */
604       for (z = 0; z < kl; z++) {
605           key[z] = (z & 255);
606       }
607 
608       for (y1 = 0; y1 <= (int)(cipher_descriptor[x].block_length*2); y1++){
609          for (z = 0; z < y1; z++) {
610             plaintext[z] = (unsigned char)(z & 255);
611          }
612          len = sizeof(tag);
613          if ((err = gcm_memory(x, key, kl, plaintext, y1, plaintext, y1, plaintext, y1, plaintext, tag, &len, GCM_ENCRYPT)) != CRYPT_OK) {
614             printf("Error GCM'ing: %s\n", error_to_string(err));
615             exit(EXIT_FAILURE);
616          }
617          fprintf(out, "%3d: ", y1);
618          for (z = 0; z < y1; z++) {
619             fprintf(out, "%02X", plaintext[z]);
620          }
621          fprintf(out, ", ");
622          for (z = 0; z <(int)len; z++) {
623             fprintf(out, "%02X", tag[z]);
624          }
625          fprintf(out, "\n");
626 
627          /* forward the key */
628          for (z = 0; z < kl; z++) {
629              key[z] = tag[z % len];
630          }
631       }
632       fprintf(out, "\n");
633    }
634    fclose(out);
635 }
636 
base64_gen(void)637 void base64_gen(void)
638 {
639    FILE *out;
640    unsigned char dst[256], src[32];
641    unsigned long x, y, len;
642 
643    out = fopen("base64_tv.txt", "w");
644    fprintf(out, "Base64 vectors.  These are the base64 encodings of the strings 00,01,02...NN-1\n\n");
645    for (x = 0; x <= 32; x++) {
646        for (y = 0; y < x; y++) {
647            src[y] = y;
648        }
649        len = sizeof(dst);
650        base64_encode(src, x, dst, &len);
651        fprintf(out, "%2lu: %s\n", x, dst);
652    }
653    fclose(out);
654 }
655 
math_gen(void)656 void math_gen(void)
657 {
658 }
659 
ecc_gen(void)660 void ecc_gen(void)
661 {
662    FILE         *out;
663    unsigned char str[512];
664    void          *k, *order, *modulus;
665    ecc_point    *G, *R;
666    int           x;
667 
668    out = fopen("ecc_tv.txt", "w");
669    fprintf(out, "ecc vectors.  These are for kG for k=1,3,9,27,...,3**n until k > order of the curve outputs are <k,x,y> triplets\n\n");
670    G = ltc_ecc_new_point();
671    R = ltc_ecc_new_point();
672    mp_init(&k);
673    mp_init(&order);
674    mp_init(&modulus);
675 
676    for (x = 0; ltc_ecc_sets[x].size != 0; x++) {
677         fprintf(out, "ECC-%d\n", ltc_ecc_sets[x].size*8);
678         mp_set(k, 1);
679 
680         mp_read_radix(order,   (char *)ltc_ecc_sets[x].order, 16);
681         mp_read_radix(modulus, (char *)ltc_ecc_sets[x].prime, 16);
682         mp_read_radix(G->x,    (char *)ltc_ecc_sets[x].Gx,    16);
683         mp_read_radix(G->y,    (char *)ltc_ecc_sets[x].Gy,    16);
684         mp_set(G->z, 1);
685 
686         while (mp_cmp(k, order) == LTC_MP_LT) {
687             ltc_mp.ecc_ptmul(k, G, R, modulus, 1);
688             mp_tohex(k,    (char*)str); fprintf(out, "%s, ", (char*)str);
689             mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str);
690             mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str);
691             mp_mul_d(k, 3, k);
692         }
693    }
694    mp_clear_multi(k, order, modulus, NULL);
695    ltc_ecc_del_point(G);
696    ltc_ecc_del_point(R);
697    fclose(out);
698 }
699 
lrw_gen(void)700 void lrw_gen(void)
701 {
702    FILE *out;
703    unsigned char tweak[16], key[16], iv[16], buf[1024];
704    int x, y, err;
705    symmetric_LRW lrw;
706 
707    /* initialize default key and tweak */
708    for (x = 0; x < 16; x++) {
709       tweak[x] = key[x] = iv[x] = x;
710    }
711 
712    out = fopen("lrw_tv.txt", "w");
713    for (x = 16; x < (int)(sizeof(buf)); x += 16) {
714        if ((err = lrw_start(find_cipher("aes"), iv, key, 16, tweak, 0, &lrw)) != CRYPT_OK) {
715           fprintf(stderr, "Error starting LRW-AES: %s\n", error_to_string(err));
716           exit(EXIT_FAILURE);
717        }
718 
719        /* encrypt incremental */
720        for (y = 0; y < x; y++) {
721            buf[y] = y & 255;
722        }
723 
724        if ((err = lrw_encrypt(buf, buf, x, &lrw)) != CRYPT_OK) {
725           fprintf(stderr, "Error encrypting with LRW-AES: %s\n", error_to_string(err));
726           exit(EXIT_FAILURE);
727        }
728 
729        /* display it */
730        fprintf(out, "%d:", x);
731        for (y = 0; y < x; y++) {
732           fprintf(out, "%02x", buf[y]);
733        }
734        fprintf(out, "\n");
735 
736        /* reset IV */
737        if ((err = lrw_setiv(iv, 16, &lrw)) != CRYPT_OK) {
738           fprintf(stderr, "Error setting IV: %s\n", error_to_string(err));
739           exit(EXIT_FAILURE);
740        }
741 
742        /* copy new tweak, iv and key */
743        for (y = 0; y < 16; y++) {
744           key[y]   = buf[y];
745           iv[y]    = buf[(y+16)%x];
746           tweak[y] = buf[(y+32)%x];
747        }
748 
749        if ((err = lrw_decrypt(buf, buf, x, &lrw)) != CRYPT_OK) {
750           fprintf(stderr, "Error decrypting with LRW-AES: %s\n", error_to_string(err));
751           exit(EXIT_FAILURE);
752        }
753 
754        /* display it */
755        fprintf(out, "%d:", x);
756        for (y = 0; y < x; y++) {
757           fprintf(out, "%02x", buf[y]);
758        }
759        fprintf(out, "\n");
760        lrw_done(&lrw);
761    }
762    fclose(out);
763 }
764 
main(void)765 int main(void)
766 {
767    reg_algs();
768    printf("Generating hash   vectors..."); fflush(stdout); hash_gen();   printf("done\n");
769    printf("Generating cipher vectors..."); fflush(stdout); cipher_gen(); printf("done\n");
770    printf("Generating HMAC   vectors..."); fflush(stdout); hmac_gen();   printf("done\n");
771    printf("Generating OMAC   vectors..."); fflush(stdout); omac_gen();   printf("done\n");
772    printf("Generating PMAC   vectors..."); fflush(stdout); pmac_gen();   printf("done\n");
773    printf("Generating EAX    vectors..."); fflush(stdout); eax_gen();    printf("done\n");
774    printf("Generating OCB    vectors..."); fflush(stdout); ocb_gen();    printf("done\n");
775    printf("Generating CCM    vectors..."); fflush(stdout); ccm_gen();    printf("done\n");
776    printf("Generating GCM    vectors..."); fflush(stdout); gcm_gen();    printf("done\n");
777    printf("Generating BASE64 vectors..."); fflush(stdout); base64_gen(); printf("done\n");
778    printf("Generating MATH   vectors..."); fflush(stdout); math_gen();   printf("done\n");
779    printf("Generating ECC    vectors..."); fflush(stdout); ecc_gen();    printf("done\n");
780    printf("Generating LRW    vectors..."); fflush(stdout); lrw_gen();    printf("done\n");
781    return 0;
782 }
783 
784 /* $Source: /cvs/libtom/libtomcrypt/demos/tv_gen.c,v $ */
785 /* $Revision: 1.15 $ */
786 /* $Date: 2006/06/09 22:10:27 $ */
787