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