1 #include <tomcrypt_test.h>
2 
3 prng_state yarrow_prng;
4 
5 struct list results[100];
6 int no_results;
sorter(const void * a,const void * b)7 int sorter(const void *a, const void *b)
8 {
9    const struct list *A, *B;
10    A = a;
11    B = b;
12    if (A->avg < B->avg) return -1;
13    if (A->avg > B->avg) return 1;
14    return 0;
15 }
16 
tally_results(int type)17 void tally_results(int type)
18 {
19    int x;
20 
21    /* qsort the results */
22    qsort(results, no_results, sizeof(struct list), &sorter);
23 
24    fprintf(stderr, "\n");
25    if (type == 0) {
26       for (x = 0; x < no_results; x++) {
27          fprintf(stderr, "%-20s: Schedule at %6lu\n", cipher_descriptor[results[x].id].name, (unsigned long)results[x].spd1);
28       }
29    } else if (type == 1) {
30       for (x = 0; x < no_results; x++) {
31         printf
32           ("%-20s[%3d]: Encrypt at %5lu, Decrypt at %5lu\n", cipher_descriptor[results[x].id].name, cipher_descriptor[results[x].id].ID, results[x].spd1, results[x].spd2);
33       }
34    } else {
35       for (x = 0; x < no_results; x++) {
36         printf
37           ("%-20s: Process at %5lu\n", hash_descriptor[results[x].id].name, results[x].spd1 / 1000);
38       }
39    }
40 }
41 
42 /* RDTSC from Scott Duplichan */
rdtsc(void)43 ulong64 rdtsc (void)
44    {
45    #if defined __GNUC__ && !defined(LTC_NO_ASM)
46       #ifdef INTEL_CC
47 			ulong64 a;
48 			asm ( " rdtsc ":"=A"(a));
49          return a;
50       #elif defined(__i386__) || defined(__x86_64__)
51          ulong64 a;
52          asm __volatile__ ("rdtsc\nmovl %%eax,(%0)\nmovl %%edx,4(%0)\n"::"r"(&a):"%eax","%edx");
53          return a;
54       #elif defined(LTC_PPC32) || defined(TFM_PPC32)
55          unsigned long a, b;
56          __asm__ __volatile__ ("mftbu %1 \nmftb %0\n":"=r"(a), "=r"(b));
57          return (((ulong64)b) << 32ULL) | ((ulong64)a);
58       #elif defined(__ia64__)  /* gcc-IA64 version */
59          unsigned long result;
60          __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
61          while (__builtin_expect ((int) result == -1, 0))
62          __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
63          return result;
64       #elif defined(__sparc__)
65          #if defined(__arch64__)
66            ulong64 a;
67            asm volatile("rd %%tick,%0" : "=r" (a));
68            return a;
69          #else
70            register unsigned long x, y;
71            __asm__ __volatile__ ("rd %%tick, %0; clruw %0, %1; srlx %0, 32, %0" : "=r" (x), "=r" (y) : "0" (x), "1" (y));
72            return ((unsigned long long) x << 32) | y;
73          #endif
74       #else
75          return XCLOCK();
76       #endif
77 
78    /* Microsoft and Intel Windows compilers */
79    #elif defined _M_IX86 && !defined(LTC_NO_ASM)
80      __asm rdtsc
81    #elif defined _M_AMD64 && !defined(LTC_NO_ASM)
82      return __rdtsc ();
83    #elif defined _M_IA64 && !defined(LTC_NO_ASM)
84      #if defined __INTEL_COMPILER
85        #include <ia64intrin.h>
86      #endif
87       return __getReg (3116);
88    #else
89      return XCLOCK();
90    #endif
91    }
92 
93 static ulong64 timer, skew = 0;
94 
t_start(void)95 void t_start(void)
96 {
97    timer = rdtsc();
98 }
99 
t_read(void)100 ulong64 t_read(void)
101 {
102    return rdtsc() - timer;
103 }
104 
init_timer(void)105 void init_timer(void)
106 {
107    ulong64 c1, c2, t1, t2, t3;
108    unsigned long y1;
109 
110    c1 = c2 = (ulong64)-1;
111    for (y1 = 0; y1 < TIMES*100; y1++) {
112       t_start();
113       t1 = t_read();
114       t3 = t_read();
115       t2 = (t_read() - t1)>>1;
116 
117       c1 = (t1 > c1) ? t1 : c1;
118       c2 = (t2 > c2) ? t2 : c2;
119    }
120    skew = c2 - c1;
121    fprintf(stderr, "Clock Skew: %lu\n", (unsigned long)skew);
122 }
123 
reg_algs(void)124 void reg_algs(void)
125 {
126   int err;
127 #ifdef LTC_RIJNDAEL
128   register_cipher (&aes_desc);
129 #endif
130 #ifdef LTC_BLOWFISH
131   register_cipher (&blowfish_desc);
132 #endif
133 #ifdef LTC_XTEA
134   register_cipher (&xtea_desc);
135 #endif
136 #ifdef LTC_RC5
137   register_cipher (&rc5_desc);
138 #endif
139 #ifdef LTC_RC6
140   register_cipher (&rc6_desc);
141 #endif
142 #ifdef LTC_SAFERP
143   register_cipher (&saferp_desc);
144 #endif
145 #ifdef LTC_TWOFISH
146   register_cipher (&twofish_desc);
147 #endif
148 #ifdef LTC_SAFER
149   register_cipher (&safer_k64_desc);
150   register_cipher (&safer_sk64_desc);
151   register_cipher (&safer_k128_desc);
152   register_cipher (&safer_sk128_desc);
153 #endif
154 #ifdef LTC_RC2
155   register_cipher (&rc2_desc);
156 #endif
157 #ifdef LTC_DES
158   register_cipher (&des_desc);
159   register_cipher (&des3_desc);
160 #endif
161 #ifdef LTC_CAST5
162   register_cipher (&cast5_desc);
163 #endif
164 #ifdef LTC_NOEKEON
165   register_cipher (&noekeon_desc);
166 #endif
167 #ifdef LTC_SKIPJACK
168   register_cipher (&skipjack_desc);
169 #endif
170 #ifdef LTC_KHAZAD
171   register_cipher (&khazad_desc);
172 #endif
173 #ifdef LTC_ANUBIS
174   register_cipher (&anubis_desc);
175 #endif
176 #ifdef LTC_KSEED
177   register_cipher (&kseed_desc);
178 #endif
179 #ifdef LTC_KASUMI
180   register_cipher (&kasumi_desc);
181 #endif
182 #ifdef LTC_MULTI2
183   register_cipher (&multi2_desc);
184 #endif
185 
186 #ifdef LTC_TIGER
187   register_hash (&tiger_desc);
188 #endif
189 #ifdef LTC_MD2
190   register_hash (&md2_desc);
191 #endif
192 #ifdef LTC_MD4
193   register_hash (&md4_desc);
194 #endif
195 #ifdef LTC_MD5
196   register_hash (&md5_desc);
197 #endif
198 #ifdef LTC_SHA1
199   register_hash (&sha1_desc);
200 #endif
201 #ifdef LTC_SHA224
202   register_hash (&sha224_desc);
203 #endif
204 #ifdef LTC_SHA256
205   register_hash (&sha256_desc);
206 #endif
207 #ifdef LTC_SHA384
208   register_hash (&sha384_desc);
209 #endif
210 #ifdef LTC_SHA512
211   register_hash (&sha512_desc);
212 #endif
213 #ifdef LTC_RIPEMD128
214   register_hash (&rmd128_desc);
215 #endif
216 #ifdef LTC_RIPEMD160
217   register_hash (&rmd160_desc);
218 #endif
219 #ifdef LTC_RIPEMD256
220   register_hash (&rmd256_desc);
221 #endif
222 #ifdef LTC_RIPEMD320
223   register_hash (&rmd320_desc);
224 #endif
225 #ifdef LTC_WHIRLPOOL
226   register_hash (&whirlpool_desc);
227 #endif
228 #ifdef LTC_CHC_HASH
229   register_hash(&chc_desc);
230   if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) {
231      fprintf(stderr, "chc_register error: %s\n", error_to_string(err));
232      exit(EXIT_FAILURE);
233   }
234 #endif
235 
236 
237 #ifndef LTC_YARROW
238    #error This demo requires Yarrow.
239 #endif
240 register_prng(&yarrow_desc);
241 #ifdef LTC_FORTUNA
242 register_prng(&fortuna_desc);
243 #endif
244 #ifdef LTC_RC4
245 register_prng(&rc4_desc);
246 #endif
247 #ifdef LTC_SOBER128
248 register_prng(&sober128_desc);
249 #endif
250 
251    if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
252       fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
253       exit(EXIT_FAILURE);
254    }
255 
256 }
257 
time_keysched(void)258 int time_keysched(void)
259 {
260   unsigned long x, y1;
261   ulong64 t1, c1;
262   symmetric_key skey;
263   int kl;
264   int    (*func) (const unsigned char *, int , int , symmetric_key *);
265   unsigned char key[MAXBLOCKSIZE];
266 
267   fprintf(stderr, "\n\nKey Schedule Time Trials for the Symmetric Ciphers:\n(Times are cycles per key)\n");
268   no_results = 0;
269  for (x = 0; cipher_descriptor[x].name != NULL; x++) {
270 #define DO1(k)   func(k, kl, 0, &skey);
271 
272     func = cipher_descriptor[x].setup;
273     kl   = cipher_descriptor[x].min_key_length;
274     c1 = (ulong64)-1;
275     for (y1 = 0; y1 < KTIMES; y1++) {
276        yarrow_read(key, kl, &yarrow_prng);
277        t_start();
278        DO1(key);
279        t1 = t_read();
280        c1 = (t1 > c1) ? c1 : t1;
281     }
282     t1 = c1 - skew;
283     results[no_results].spd1 = results[no_results].avg = t1;
284     results[no_results++].id = x;
285     fprintf(stderr, "."); fflush(stdout);
286 
287 #undef DO1
288    }
289    tally_results(0);
290 
291    return 0;
292 }
293 
time_cipher(void)294 int time_cipher(void)
295 {
296   unsigned long x, y1;
297   ulong64  t1, t2, c1, c2, a1, a2;
298   symmetric_ECB ecb;
299   unsigned char key[MAXBLOCKSIZE], pt[4096];
300   int err;
301 
302   fprintf(stderr, "\n\nECB Time Trials for the Symmetric Ciphers:\n");
303   no_results = 0;
304   for (x = 0; cipher_descriptor[x].name != NULL; x++) {
305     ecb_start(x, key, cipher_descriptor[x].min_key_length, 0, &ecb);
306 
307     /* sanity check on cipher */
308     if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
309        fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
310        exit(EXIT_FAILURE);
311     }
312 
313 #define DO1   ecb_encrypt(pt, pt, sizeof(pt), &ecb);
314 #define DO2   DO1 DO1
315 
316     c1 = c2 = (ulong64)-1;
317     for (y1 = 0; y1 < 100; y1++) {
318         t_start();
319         DO1;
320         t1 = t_read();
321         DO2;
322         t2 = t_read();
323         t2 -= t1;
324 
325         c1 = (t1 > c1 ? c1 : t1);
326         c2 = (t2 > c2 ? c2 : t2);
327     }
328     a1 = c2 - c1 - skew;
329 
330 #undef DO1
331 #undef DO2
332 #define DO1   ecb_decrypt(pt, pt, sizeof(pt), &ecb);
333 #define DO2   DO1 DO1
334 
335     c1 = c2 = (ulong64)-1;
336     for (y1 = 0; y1 < 100; y1++) {
337         t_start();
338         DO1;
339         t1 = t_read();
340         DO2;
341         t2 = t_read();
342         t2 -= t1;
343 
344         c1 = (t1 > c1 ? c1 : t1);
345         c2 = (t2 > c2 ? c2 : t2);
346     }
347     a2 = c2 - c1 - skew;
348     ecb_done(&ecb);
349 
350     results[no_results].id = x;
351     results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
352     results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
353     results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
354     ++no_results;
355     fprintf(stderr, "."); fflush(stdout);
356 
357 #undef DO2
358 #undef DO1
359    }
360    tally_results(1);
361 
362    return 0;
363 }
364 
365 #ifdef LTC_CBC_MODE
time_cipher2(void)366 int time_cipher2(void)
367 {
368   unsigned long x, y1;
369   ulong64  t1, t2, c1, c2, a1, a2;
370   symmetric_CBC cbc;
371   unsigned char key[MAXBLOCKSIZE], pt[4096];
372   int err;
373 
374   fprintf(stderr, "\n\nCBC Time Trials for the Symmetric Ciphers:\n");
375   no_results = 0;
376   for (x = 0; cipher_descriptor[x].name != NULL; x++) {
377     cbc_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, &cbc);
378 
379     /* sanity check on cipher */
380     if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
381        fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
382        exit(EXIT_FAILURE);
383     }
384 
385 #define DO1   cbc_encrypt(pt, pt, sizeof(pt), &cbc);
386 #define DO2   DO1 DO1
387 
388     c1 = c2 = (ulong64)-1;
389     for (y1 = 0; y1 < 100; y1++) {
390         t_start();
391         DO1;
392         t1 = t_read();
393         DO2;
394         t2 = t_read();
395         t2 -= t1;
396 
397         c1 = (t1 > c1 ? c1 : t1);
398         c2 = (t2 > c2 ? c2 : t2);
399     }
400     a1 = c2 - c1 - skew;
401 
402 #undef DO1
403 #undef DO2
404 #define DO1   cbc_decrypt(pt, pt, sizeof(pt), &cbc);
405 #define DO2   DO1 DO1
406 
407     c1 = c2 = (ulong64)-1;
408     for (y1 = 0; y1 < 100; y1++) {
409         t_start();
410         DO1;
411         t1 = t_read();
412         DO2;
413         t2 = t_read();
414         t2 -= t1;
415 
416         c1 = (t1 > c1 ? c1 : t1);
417         c2 = (t2 > c2 ? c2 : t2);
418     }
419     a2 = c2 - c1 - skew;
420     cbc_done(&cbc);
421 
422     results[no_results].id = x;
423     results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
424     results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
425     results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
426     ++no_results;
427     fprintf(stderr, "."); fflush(stdout);
428 
429 #undef DO2
430 #undef DO1
431    }
432    tally_results(1);
433 
434    return 0;
435 }
436 #else
time_cipher2(void)437 int time_cipher2(void) { fprintf(stderr, "NO CBC\n"); return 0; }
438 #endif
439 
440 #ifdef LTC_CTR_MODE
time_cipher3(void)441 int time_cipher3(void)
442 {
443   unsigned long x, y1;
444   ulong64  t1, t2, c1, c2, a1, a2;
445   symmetric_CTR ctr;
446   unsigned char key[MAXBLOCKSIZE], pt[4096];
447   int err;
448 
449   fprintf(stderr, "\n\nCTR Time Trials for the Symmetric Ciphers:\n");
450   no_results = 0;
451   for (x = 0; cipher_descriptor[x].name != NULL; x++) {
452     ctr_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr);
453 
454     /* sanity check on cipher */
455     if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
456        fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
457        exit(EXIT_FAILURE);
458     }
459 
460 #define DO1   ctr_encrypt(pt, pt, sizeof(pt), &ctr);
461 #define DO2   DO1 DO1
462 
463     c1 = c2 = (ulong64)-1;
464     for (y1 = 0; y1 < 100; y1++) {
465         t_start();
466         DO1;
467         t1 = t_read();
468         DO2;
469         t2 = t_read();
470         t2 -= t1;
471 
472         c1 = (t1 > c1 ? c1 : t1);
473         c2 = (t2 > c2 ? c2 : t2);
474     }
475     a1 = c2 - c1 - skew;
476 
477 #undef DO1
478 #undef DO2
479 #define DO1   ctr_decrypt(pt, pt, sizeof(pt), &ctr);
480 #define DO2   DO1 DO1
481 
482     c1 = c2 = (ulong64)-1;
483     for (y1 = 0; y1 < 100; y1++) {
484         t_start();
485         DO1;
486         t1 = t_read();
487         DO2;
488         t2 = t_read();
489         t2 -= t1;
490 
491         c1 = (t1 > c1 ? c1 : t1);
492         c2 = (t2 > c2 ? c2 : t2);
493     }
494     a2 = c2 - c1 - skew;
495     ctr_done(&ctr);
496 
497     results[no_results].id = x;
498     results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
499     results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
500     results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
501     ++no_results;
502     fprintf(stderr, "."); fflush(stdout);
503 
504 #undef DO2
505 #undef DO1
506    }
507    tally_results(1);
508 
509    return 0;
510 }
511 #else
time_cipher3(void)512 int time_cipher3(void) { fprintf(stderr, "NO CTR\n"); return 0; }
513 #endif
514 
515 #ifdef LTC_LRW_MODE
time_cipher4(void)516 int time_cipher4(void)
517 {
518   unsigned long x, y1;
519   ulong64  t1, t2, c1, c2, a1, a2;
520   symmetric_LRW lrw;
521   unsigned char key[MAXBLOCKSIZE], pt[4096];
522   int err;
523 
524   fprintf(stderr, "\n\nLRW Time Trials for the Symmetric Ciphers:\n");
525   no_results = 0;
526   for (x = 0; cipher_descriptor[x].name != NULL; x++) {
527     if (cipher_descriptor[x].block_length != 16) continue;
528     lrw_start(x, pt, key, cipher_descriptor[x].min_key_length, key, 0, &lrw);
529 
530     /* sanity check on cipher */
531     if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
532        fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
533        exit(EXIT_FAILURE);
534     }
535 
536 #define DO1   lrw_encrypt(pt, pt, sizeof(pt), &lrw);
537 #define DO2   DO1 DO1
538 
539     c1 = c2 = (ulong64)-1;
540     for (y1 = 0; y1 < 100; y1++) {
541         t_start();
542         DO1;
543         t1 = t_read();
544         DO2;
545         t2 = t_read();
546         t2 -= t1;
547 
548         c1 = (t1 > c1 ? c1 : t1);
549         c2 = (t2 > c2 ? c2 : t2);
550     }
551     a1 = c2 - c1 - skew;
552 
553 #undef DO1
554 #undef DO2
555 #define DO1   lrw_decrypt(pt, pt, sizeof(pt), &lrw);
556 #define DO2   DO1 DO1
557 
558     c1 = c2 = (ulong64)-1;
559     for (y1 = 0; y1 < 100; y1++) {
560         t_start();
561         DO1;
562         t1 = t_read();
563         DO2;
564         t2 = t_read();
565         t2 -= t1;
566 
567         c1 = (t1 > c1 ? c1 : t1);
568         c2 = (t2 > c2 ? c2 : t2);
569     }
570     a2 = c2 - c1 - skew;
571 
572     lrw_done(&lrw);
573 
574     results[no_results].id = x;
575     results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
576     results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
577     results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
578     ++no_results;
579     fprintf(stderr, "."); fflush(stdout);
580 
581 #undef DO2
582 #undef DO1
583    }
584    tally_results(1);
585 
586    return 0;
587 }
588 #else
time_cipher4(void)589 int time_cipher4(void) { fprintf(stderr, "NO LRW\n"); return 0; }
590 #endif
591 
592 
time_hash(void)593 int time_hash(void)
594 {
595   unsigned long x, y1, len;
596   ulong64 t1, t2, c1, c2;
597   hash_state md;
598   int    (*func)(hash_state *, const unsigned char *, unsigned long), err;
599   unsigned char pt[MAXBLOCKSIZE];
600 
601 
602   fprintf(stderr, "\n\nHASH Time Trials for:\n");
603   no_results = 0;
604   for (x = 0; hash_descriptor[x].name != NULL; x++) {
605 
606     /* sanity check on hash */
607     if ((err = hash_descriptor[x].test()) != CRYPT_OK) {
608        fprintf(stderr, "\n\nERROR: Hash %s failed self-test %s\n", hash_descriptor[x].name, error_to_string(err));
609        exit(EXIT_FAILURE);
610     }
611 
612     hash_descriptor[x].init(&md);
613 
614 #define DO1   func(&md,pt,len);
615 #define DO2   DO1 DO1
616 
617     func = hash_descriptor[x].process;
618     len  = hash_descriptor[x].blocksize;
619 
620     c1 = c2 = (ulong64)-1;
621     for (y1 = 0; y1 < TIMES; y1++) {
622        t_start();
623        DO1;
624        t1 = t_read();
625        DO2;
626        t2 = t_read() - t1;
627        c1 = (t1 > c1) ? c1 : t1;
628        c2 = (t2 > c2) ? c2 : t2;
629     }
630     t1 = c2 - c1 - skew;
631     t1 = ((t1 * CONST64(1000))) / ((ulong64)hash_descriptor[x].blocksize);
632     results[no_results].id = x;
633     results[no_results].spd1 = results[no_results].avg = t1;
634     ++no_results;
635     fprintf(stderr, "."); fflush(stdout);
636 #undef DO2
637 #undef DO1
638    }
639    tally_results(2);
640 
641    return 0;
642 }
643 
644 #undef MPI
645 /*#warning you need an mp_rand!!!*/
646 
647 #ifdef MPI
time_mult(void)648 void time_mult(void)
649 {
650    ulong64 t1, t2;
651    unsigned long x, y;
652    void  *a, *b, *c;
653 
654    fprintf(stderr, "Timing Multiplying:\n");
655    mp_init_multi(&a,&b,&c,NULL);
656    for (x = 128/DIGIT_BIT; x <= 1536/DIGIT_BIT; x += 128/DIGIT_BIT) {
657        mp_rand(&a, x);
658        mp_rand(&b, x);
659 
660 #define DO1 mp_mul(&a, &b, &c);
661 #define DO2 DO1; DO1;
662 
663        t2 = -1;
664        for (y = 0; y < TIMES; y++) {
665            t_start();
666            t1 = t_read();
667            DO2;
668            t1 = (t_read() - t1)>>1;
669            if (t1 < t2) t2 = t1;
670        }
671        fprintf(stderr, "%4lu bits: %9llu cycles\n", x*DIGIT_BIT, t2);
672    }
673    mp_clear_multi(&a,&b,&c,NULL);
674 
675 #undef DO1
676 #undef DO2
677 }
678 
time_sqr(void)679 void time_sqr(void)
680 {
681    ulong64 t1, t2;
682    unsigned long x, y;
683    mp_int  a, b;
684 
685    fprintf(stderr, "Timing Squaring:\n");
686    mp_init_multi(&a,&b,NULL);
687    for (x = 128/DIGIT_BIT; x <= 1536/DIGIT_BIT; x += 128/DIGIT_BIT) {
688        mp_rand(&a, x);
689 
690 #define DO1 mp_sqr(&a, &b);
691 #define DO2 DO1; DO1;
692 
693        t2 = -1;
694        for (y = 0; y < TIMES; y++) {
695            t_start();
696            t1 = t_read();
697            DO2;
698            t1 = (t_read() - t1)>>1;
699            if (t1 < t2) t2 = t1;
700        }
701        fprintf(stderr, "%4lu bits: %9llu cycles\n", x*DIGIT_BIT, t2);
702    }
703    mp_clear_multi(&a,&b,NULL);
704 
705 #undef DO1
706 #undef DO2
707 }
708 #else
time_mult(void)709 void time_mult(void) { fprintf(stderr, "NO MULT\n"); }
time_sqr(void)710 void time_sqr(void) { fprintf(stderr, "NO SQR\n"); }
711 #endif
712 
time_prng(void)713 void time_prng(void)
714 {
715    ulong64 t1, t2;
716    unsigned char buf[4096];
717    prng_state tprng;
718    unsigned long x, y;
719    int           err;
720 
721    fprintf(stderr, "Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n");
722    for (x = 0; prng_descriptor[x].name != NULL; x++) {
723 
724       /* sanity check on prng */
725       if ((err = prng_descriptor[x].test()) != CRYPT_OK) {
726          fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor[x].name, error_to_string(err));
727          exit(EXIT_FAILURE);
728       }
729 
730       prng_descriptor[x].start(&tprng);
731       zeromem(buf, 256);
732       prng_descriptor[x].add_entropy(buf, 256, &tprng);
733       prng_descriptor[x].ready(&tprng);
734       t2 = -1;
735 
736 #define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { fprintf(stderr, "\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
737 #define DO2 DO1 DO1
738       for (y = 0; y < 10000; y++) {
739          t_start();
740          t1 = t_read();
741          DO2;
742          t1 = (t_read() - t1)>>1;
743          if (t1 < t2) t2 = t1;
744       }
745       fprintf(stderr, "%20s: %5llu ", prng_descriptor[x].name, t2>>12);
746 #undef DO2
747 #undef DO1
748 
749 #define DO1 prng_descriptor[x].start(&tprng); prng_descriptor[x].add_entropy(buf, 32, &tprng); prng_descriptor[x].ready(&tprng); prng_descriptor[x].done(&tprng);
750 #define DO2 DO1 DO1
751       for (y = 0; y < 10000; y++) {
752          t_start();
753          t1 = t_read();
754          DO2;
755          t1 = (t_read() - t1)>>1;
756          if (t1 < t2) t2 = t1;
757       }
758       fprintf(stderr, "%5llu\n", t2);
759 #undef DO2
760 #undef DO1
761 
762    }
763 }
764 
765 #ifdef LTC_MDSA
766 /* time various DSA operations */
time_dsa(void)767 void time_dsa(void)
768 {
769    dsa_key       key;
770    ulong64       t1, t2;
771    unsigned long x, y;
772    int           err;
773 static const struct {
774    int group, modulus;
775 } groups[] = {
776 { 20, 96  },
777 { 20, 128 },
778 { 24, 192 },
779 { 28, 256 },
780 { 32, 512 }
781 };
782 
783    for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
784        t2 = 0;
785        for (y = 0; y < 4; y++) {
786            t_start();
787            t1 = t_read();
788            if ((err = dsa_make_key(&yarrow_prng, find_prng("yarrow"), groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) {
789               fprintf(stderr, "\n\ndsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
790               exit(EXIT_FAILURE);
791            }
792            t1 = t_read() - t1;
793            t2 += t1;
794 
795 #ifdef LTC_PROFILE
796        t2 <<= 2;
797        break;
798 #endif
799            if (y < 3) {
800               dsa_free(&key);
801            }
802        }
803        t2 >>= 2;
804        fprintf(stderr, "DSA-(%lu, %lu) make_key    took %15llu cycles\n", (unsigned long)groups[x].group*8, (unsigned long)groups[x].modulus*8, t2);
805    }
806 }
807 #endif
808 
809 
810 #ifdef LTC_MRSA
811 /* time various RSA operations */
time_rsa(void)812 void time_rsa(void)
813 {
814    rsa_key       key;
815    ulong64       t1, t2;
816    unsigned char buf[2][2048];
817    unsigned long x, y, z, zzz;
818    int           err, zz, stat;
819 
820    for (x = 1024; x <= 2048; x += 256) {
821        t2 = 0;
822        for (y = 0; y < 4; y++) {
823            t_start();
824            t1 = t_read();
825            if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) {
826               fprintf(stderr, "\n\nrsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
827               exit(EXIT_FAILURE);
828            }
829            t1 = t_read() - t1;
830            t2 += t1;
831 
832 #ifdef LTC_PROFILE
833        t2 <<= 2;
834        break;
835 #endif
836 
837            if (y < 3) {
838               rsa_free(&key);
839            }
840        }
841        t2 >>= 2;
842        fprintf(stderr, "RSA-%lu make_key    took %15llu cycles\n", x, t2);
843 
844        t2 = 0;
845        for (y = 0; y < 16; y++) {
846            t_start();
847            t1 = t_read();
848            z = sizeof(buf[1]);
849            if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, (const unsigned char *)"testprog", 8, &yarrow_prng,
850                                       find_prng("yarrow"), find_hash("sha1"),
851                                       &key)) != CRYPT_OK) {
852               fprintf(stderr, "\n\nrsa_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
853               exit(EXIT_FAILURE);
854            }
855            t1 = t_read() - t1;
856            t2 += t1;
857 #ifdef LTC_PROFILE
858        t2 <<= 4;
859        break;
860 #endif
861        }
862        t2 >>= 4;
863        fprintf(stderr, "RSA-%lu encrypt_key took %15llu cycles\n", x, t2);
864 
865        t2 = 0;
866        for (y = 0; y < 2048; y++) {
867            t_start();
868            t1 = t_read();
869            zzz = sizeof(buf[0]);
870            if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, (const unsigned char *)"testprog", 8,  find_hash("sha1"),
871                                       &zz, &key)) != CRYPT_OK) {
872               fprintf(stderr, "\n\nrsa_decrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
873               exit(EXIT_FAILURE);
874            }
875            t1 = t_read() - t1;
876            t2 += t1;
877 #ifdef LTC_PROFILE
878        t2 <<= 11;
879        break;
880 #endif
881        }
882        t2 >>= 11;
883        fprintf(stderr, "RSA-%lu decrypt_key took %15llu cycles\n", x, t2);
884 
885        t2 = 0;
886        for (y = 0; y < 256; y++) {
887           t_start();
888           t1 = t_read();
889           z = sizeof(buf[1]);
890           if ((err = rsa_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
891                                    find_prng("yarrow"), find_hash("sha1"), 8, &key)) != CRYPT_OK) {
892               fprintf(stderr, "\n\nrsa_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
893               exit(EXIT_FAILURE);
894            }
895            t1 = t_read() - t1;
896            t2 += t1;
897 #ifdef LTC_PROFILE
898        t2 <<= 8;
899        break;
900 #endif
901 	}
902         t2 >>= 8;
903         fprintf(stderr, "RSA-%lu sign_hash took   %15llu cycles\n", x, t2);
904 
905        t2 = 0;
906        for (y = 0; y < 2048; y++) {
907           t_start();
908           t1 = t_read();
909           if ((err = rsa_verify_hash(buf[1], z, buf[0], 20, find_hash("sha1"), 8, &stat, &key)) != CRYPT_OK) {
910               fprintf(stderr, "\n\nrsa_verify_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
911               exit(EXIT_FAILURE);
912           }
913           if (stat == 0) {
914              fprintf(stderr, "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n", x, y);
915              exit(EXIT_FAILURE);
916           }
917           t1 = t_read() - t1;
918           t2 += t1;
919 #ifdef LTC_PROFILE
920        t2 <<= 11;
921        break;
922 #endif
923 	}
924         t2 >>= 11;
925         fprintf(stderr, "RSA-%lu verify_hash took %15llu cycles\n", x, t2);
926        fprintf(stderr, "\n\n");
927        rsa_free(&key);
928   }
929 }
930 #else
time_rsa(void)931 void time_rsa(void) { fprintf(stderr, "NO RSA\n"); }
932 #endif
933 
934 #ifdef MKAT
935 /* time various KAT operations */
time_katja(void)936 void time_katja(void)
937 {
938    katja_key key;
939    ulong64 t1, t2;
940    unsigned char buf[2][4096];
941    unsigned long x, y, z, zzz;
942    int           err, zz;
943 
944    for (x = 1024; x <= 2048; x += 256) {
945        t2 = 0;
946        for (y = 0; y < 4; y++) {
947            t_start();
948            t1 = t_read();
949            if ((err = katja_make_key(&yarrow_prng, find_prng("yarrow"), x/8, &key)) != CRYPT_OK) {
950               fprintf(stderr, "\n\nkatja_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
951               exit(EXIT_FAILURE);
952            }
953            t1 = t_read() - t1;
954            t2 += t1;
955 
956            if (y < 3) {
957               katja_free(&key);
958            }
959        }
960        t2 >>= 2;
961        fprintf(stderr, "Katja-%lu make_key    took %15llu cycles\n", x, t2);
962 
963        t2 = 0;
964        for (y = 0; y < 16; y++) {
965            t_start();
966            t1 = t_read();
967            z = sizeof(buf[1]);
968            if ((err = katja_encrypt_key(buf[0], 32, buf[1], &z, "testprog", 8, &yarrow_prng,
969                                       find_prng("yarrow"), find_hash("sha1"),
970                                       &key)) != CRYPT_OK) {
971               fprintf(stderr, "\n\nkatja_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
972               exit(EXIT_FAILURE);
973            }
974            t1 = t_read() - t1;
975            t2 += t1;
976        }
977        t2 >>= 4;
978        fprintf(stderr, "Katja-%lu encrypt_key took %15llu cycles\n", x, t2);
979 
980        t2 = 0;
981        for (y = 0; y < 2048; y++) {
982            t_start();
983            t1 = t_read();
984            zzz = sizeof(buf[0]);
985            if ((err = katja_decrypt_key(buf[1], z, buf[0], &zzz, "testprog", 8,  find_hash("sha1"),
986                                       &zz, &key)) != CRYPT_OK) {
987               fprintf(stderr, "\n\nkatja_decrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
988               exit(EXIT_FAILURE);
989            }
990            t1 = t_read() - t1;
991            t2 += t1;
992        }
993        t2 >>= 11;
994        fprintf(stderr, "Katja-%lu decrypt_key took %15llu cycles\n", x, t2);
995 
996 
997        katja_free(&key);
998   }
999 }
1000 #else
time_katja(void)1001 void time_katja(void) { fprintf(stderr, "NO Katja\n"); }
1002 #endif
1003 
1004 #ifdef LTC_MECC
1005 /* time various ECC operations */
time_ecc(void)1006 void time_ecc(void)
1007 {
1008    ecc_key key;
1009    ulong64 t1, t2;
1010    unsigned char buf[2][256];
1011    unsigned long i, w, x, y, z;
1012    int           err, stat;
1013    static unsigned long sizes[] = {
1014 #ifdef ECC112
1015 112/8,
1016 #endif
1017 #ifdef ECC128
1018 128/8,
1019 #endif
1020 #ifdef ECC160
1021 160/8,
1022 #endif
1023 #ifdef ECC192
1024 192/8,
1025 #endif
1026 #ifdef ECC224
1027 224/8,
1028 #endif
1029 #ifdef ECC256
1030 256/8,
1031 #endif
1032 #ifdef ECC384
1033 384/8,
1034 #endif
1035 #ifdef ECC521
1036 521/8,
1037 #endif
1038 100000};
1039 
1040    for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
1041        t2 = 0;
1042        for (y = 0; y < 256; y++) {
1043            t_start();
1044            t1 = t_read();
1045            if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
1046               fprintf(stderr, "\n\necc_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
1047               exit(EXIT_FAILURE);
1048            }
1049            t1 = t_read() - t1;
1050            t2 += t1;
1051 
1052 #ifdef LTC_PROFILE
1053        t2 <<= 8;
1054        break;
1055 #endif
1056 
1057            if (y < 255) {
1058               ecc_free(&key);
1059            }
1060        }
1061        t2 >>= 8;
1062        fprintf(stderr, "ECC-%lu make_key    took %15llu cycles\n", x*8, t2);
1063 
1064        t2 = 0;
1065        for (y = 0; y < 256; y++) {
1066            t_start();
1067            t1 = t_read();
1068            z = sizeof(buf[1]);
1069            if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
1070                                       &key)) != CRYPT_OK) {
1071               fprintf(stderr, "\n\necc_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
1072               exit(EXIT_FAILURE);
1073            }
1074            t1 = t_read() - t1;
1075            t2 += t1;
1076 #ifdef LTC_PROFILE
1077        t2 <<= 8;
1078        break;
1079 #endif
1080        }
1081        t2 >>= 8;
1082        fprintf(stderr, "ECC-%lu encrypt_key took %15llu cycles\n", x*8, t2);
1083 
1084        t2 = 0;
1085        for (y = 0; y < 256; y++) {
1086            t_start();
1087            t1 = t_read();
1088            w = 20;
1089            if ((err = ecc_decrypt_key(buf[1], z, buf[0], &w, &key)) != CRYPT_OK) {
1090               fprintf(stderr, "\n\necc_decrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
1091               exit(EXIT_FAILURE);
1092            }
1093            t1 = t_read() - t1;
1094            t2 += t1;
1095 #ifdef LTC_PROFILE
1096        t2 <<= 8;
1097        break;
1098 #endif
1099        }
1100        t2 >>= 8;
1101        fprintf(stderr, "ECC-%lu decrypt_key took %15llu cycles\n", x*8, t2);
1102 
1103        t2 = 0;
1104        for (y = 0; y < 256; y++) {
1105           t_start();
1106           t1 = t_read();
1107           z = sizeof(buf[1]);
1108           if ((err = ecc_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
1109                                    find_prng("yarrow"), &key)) != CRYPT_OK) {
1110               fprintf(stderr, "\n\necc_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
1111               exit(EXIT_FAILURE);
1112            }
1113            t1 = t_read() - t1;
1114            t2 += t1;
1115 #ifdef LTC_PROFILE
1116        t2 <<= 8;
1117        break;
1118 #endif
1119 	}
1120         t2 >>= 8;
1121         fprintf(stderr, "ECC-%lu sign_hash took   %15llu cycles\n", x*8, t2);
1122 
1123        t2 = 0;
1124        for (y = 0; y < 256; y++) {
1125           t_start();
1126           t1 = t_read();
1127           if ((err = ecc_verify_hash(buf[1], z, buf[0], 20, &stat, &key)) != CRYPT_OK) {
1128               fprintf(stderr, "\n\necc_verify_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
1129               exit(EXIT_FAILURE);
1130           }
1131           if (stat == 0) {
1132              fprintf(stderr, "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n", x*8, y);
1133              exit(EXIT_FAILURE);
1134           }
1135           t1 = t_read() - t1;
1136           t2 += t1;
1137 #ifdef LTC_PROFILE
1138        t2 <<= 8;
1139        break;
1140 #endif
1141 	}
1142         t2 >>= 8;
1143         fprintf(stderr, "ECC-%lu verify_hash took %15llu cycles\n", x*8, t2);
1144 
1145        fprintf(stderr, "\n\n");
1146        ecc_free(&key);
1147   }
1148 }
1149 #else
time_ecc(void)1150 void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
1151 #endif
1152 
time_macs_(unsigned long MAC_SIZE)1153 void time_macs_(unsigned long MAC_SIZE)
1154 {
1155    unsigned char *buf, key[16], tag[16];
1156    ulong64 t1, t2;
1157    unsigned long x, z;
1158    int err, cipher_idx, hash_idx;
1159 
1160    fprintf(stderr, "\nMAC Timings (cycles/byte on %luKB blocks):\n", MAC_SIZE);
1161 
1162    buf = XMALLOC(MAC_SIZE*1024);
1163    if (buf == NULL) {
1164       fprintf(stderr, "\n\nout of heap yo\n\n");
1165       exit(EXIT_FAILURE);
1166    }
1167 
1168    cipher_idx = find_cipher("aes");
1169    hash_idx   = find_hash("sha1");
1170 
1171    if (cipher_idx == -1 || hash_idx == -1) {
1172       fprintf(stderr, "Warning the MAC tests requires AES and LTC_SHA1 to operate... so sorry\n");
1173       return;
1174    }
1175 
1176    yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
1177    yarrow_read(key, 16, &yarrow_prng);
1178 
1179 #ifdef LTC_OMAC
1180    t2 = -1;
1181    for (x = 0; x < 10000; x++) {
1182         t_start();
1183         t1 = t_read();
1184         z = 16;
1185         if ((err = omac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
1186            fprintf(stderr, "\n\nomac error... %s\n", error_to_string(err));
1187            exit(EXIT_FAILURE);
1188         }
1189         t1 = t_read() - t1;
1190         if (t1 < t2) t2 = t1;
1191    }
1192    fprintf(stderr, "LTC_OMAC-%s\t\t%9llu\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
1193 #endif
1194 
1195 #ifdef LTC_XCBC
1196    t2 = -1;
1197    for (x = 0; x < 10000; x++) {
1198         t_start();
1199         t1 = t_read();
1200         z = 16;
1201         if ((err = xcbc_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
1202            fprintf(stderr, "\n\nxcbc error... %s\n", error_to_string(err));
1203            exit(EXIT_FAILURE);
1204         }
1205         t1 = t_read() - t1;
1206         if (t1 < t2) t2 = t1;
1207    }
1208    fprintf(stderr, "XCBC-%s\t\t%9llu\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
1209 #endif
1210 
1211 #ifdef LTC_F9_MODE
1212    t2 = -1;
1213    for (x = 0; x < 10000; x++) {
1214         t_start();
1215         t1 = t_read();
1216         z = 16;
1217         if ((err = f9_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
1218            fprintf(stderr, "\n\nF9 error... %s\n", error_to_string(err));
1219            exit(EXIT_FAILURE);
1220         }
1221         t1 = t_read() - t1;
1222         if (t1 < t2) t2 = t1;
1223    }
1224    fprintf(stderr, "F9-%s\t\t\t%9llu\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
1225 #endif
1226 
1227 #ifdef LTC_PMAC
1228    t2 = -1;
1229    for (x = 0; x < 10000; x++) {
1230         t_start();
1231         t1 = t_read();
1232         z = 16;
1233         if ((err = pmac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
1234            fprintf(stderr, "\n\npmac error... %s\n", error_to_string(err));
1235            exit(EXIT_FAILURE);
1236         }
1237         t1 = t_read() - t1;
1238         if (t1 < t2) t2 = t1;
1239    }
1240    fprintf(stderr, "PMAC-AES\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
1241 #endif
1242 
1243 #ifdef LTC_PELICAN
1244    t2 = -1;
1245    for (x = 0; x < 10000; x++) {
1246         t_start();
1247         t1 = t_read();
1248         z = 16;
1249         if ((err = pelican_memory(key, 16, buf, MAC_SIZE*1024, tag)) != CRYPT_OK) {
1250            fprintf(stderr, "\n\npelican error... %s\n", error_to_string(err));
1251            exit(EXIT_FAILURE);
1252         }
1253         t1 = t_read() - t1;
1254         if (t1 < t2) t2 = t1;
1255    }
1256    fprintf(stderr, "LTC_PELICAN \t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
1257 #endif
1258 
1259 #ifdef LTC_HMAC
1260    t2 = -1;
1261    for (x = 0; x < 10000; x++) {
1262         t_start();
1263         t1 = t_read();
1264         z = 16;
1265         if ((err = hmac_memory(hash_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
1266            fprintf(stderr, "\n\nhmac error... %s\n", error_to_string(err));
1267            exit(EXIT_FAILURE);
1268         }
1269         t1 = t_read() - t1;
1270         if (t1 < t2) t2 = t1;
1271    }
1272    fprintf(stderr, "LTC_HMAC-%s\t\t%9llu\n", hash_descriptor[hash_idx].name, t2/(ulong64)(MAC_SIZE*1024));
1273 #endif
1274 
1275    XFREE(buf);
1276 }
1277 
time_macs(void)1278 void time_macs(void)
1279 {
1280    time_macs_(1);
1281    time_macs_(4);
1282    time_macs_(32);
1283 }
1284 
time_encmacs_(unsigned long MAC_SIZE)1285 void time_encmacs_(unsigned long MAC_SIZE)
1286 {
1287    unsigned char *buf, IV[16], key[16], tag[16];
1288    ulong64 t1, t2;
1289    unsigned long x, z;
1290    int err, cipher_idx;
1291    symmetric_key skey;
1292 
1293    fprintf(stderr, "\nENC+MAC Timings (zero byte AAD, 16 byte IV, cycles/byte on %luKB blocks):\n", MAC_SIZE);
1294 
1295    buf = XMALLOC(MAC_SIZE*1024);
1296    if (buf == NULL) {
1297       fprintf(stderr, "\n\nout of heap yo\n\n");
1298       exit(EXIT_FAILURE);
1299    }
1300 
1301    cipher_idx = find_cipher("aes");
1302 
1303    yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
1304    yarrow_read(key, 16, &yarrow_prng);
1305    yarrow_read(IV, 16, &yarrow_prng);
1306 
1307 #ifdef LTC_EAX_MODE
1308    t2 = -1;
1309    for (x = 0; x < 10000; x++) {
1310         t_start();
1311         t1 = t_read();
1312         z = 16;
1313         if ((err = eax_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
1314            fprintf(stderr, "\nEAX error... %s\n", error_to_string(err));
1315            exit(EXIT_FAILURE);
1316         }
1317         t1 = t_read() - t1;
1318         if (t1 < t2) t2 = t1;
1319    }
1320    fprintf(stderr, "EAX \t\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
1321 #endif
1322 
1323 #ifdef LTC_OCB_MODE
1324    t2 = -1;
1325    for (x = 0; x < 10000; x++) {
1326         t_start();
1327         t1 = t_read();
1328         z = 16;
1329         if ((err = ocb_encrypt_authenticate_memory(cipher_idx, key, 16, IV, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
1330            fprintf(stderr, "\nOCB error... %s\n", error_to_string(err));
1331            exit(EXIT_FAILURE);
1332         }
1333         t1 = t_read() - t1;
1334         if (t1 < t2) t2 = t1;
1335    }
1336    fprintf(stderr, "OCB \t\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
1337 #endif
1338 
1339 #ifdef LTC_CCM_MODE
1340    t2 = -1;
1341    for (x = 0; x < 10000; x++) {
1342         t_start();
1343         t1 = t_read();
1344         z = 16;
1345         if ((err = ccm_memory(cipher_idx, key, 16, NULL, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
1346            fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
1347            exit(EXIT_FAILURE);
1348         }
1349         t1 = t_read() - t1;
1350         if (t1 < t2) t2 = t1;
1351    }
1352    fprintf(stderr, "CCM (no-precomp) \t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
1353 
1354    cipher_descriptor[cipher_idx].setup(key, 16, 0, &skey);
1355    t2 = -1;
1356    for (x = 0; x < 10000; x++) {
1357         t_start();
1358         t1 = t_read();
1359         z = 16;
1360         if ((err = ccm_memory(cipher_idx, key, 16, &skey, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
1361            fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
1362            exit(EXIT_FAILURE);
1363         }
1364         t1 = t_read() - t1;
1365         if (t1 < t2) t2 = t1;
1366    }
1367    fprintf(stderr, "CCM (precomp) \t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
1368    cipher_descriptor[cipher_idx].done(&skey);
1369 #endif
1370 
1371 #ifdef LTC_GCM_MODE
1372    t2 = -1;
1373    for (x = 0; x < 100; x++) {
1374         t_start();
1375         t1 = t_read();
1376         z = 16;
1377         if ((err = gcm_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, GCM_ENCRYPT)) != CRYPT_OK) {
1378            fprintf(stderr, "\nGCM error... %s\n", error_to_string(err));
1379            exit(EXIT_FAILURE);
1380         }
1381         t1 = t_read() - t1;
1382         if (t1 < t2) t2 = t1;
1383    }
1384    fprintf(stderr, "GCM (no-precomp)\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
1385 
1386    {
1387    gcm_state gcm
1388 #ifdef LTC_GCM_TABLES_SSE2
1389 __attribute__ ((aligned (16)))
1390 #endif
1391 ;
1392 
1393    if ((err = gcm_init(&gcm, cipher_idx, key, 16)) != CRYPT_OK) { fprintf(stderr, "gcm_init: %s\n", error_to_string(err)); exit(EXIT_FAILURE); }
1394    t2 = -1;
1395    for (x = 0; x < 10000; x++) {
1396         t_start();
1397         t1 = t_read();
1398         z = 16;
1399         if ((err = gcm_reset(&gcm)) != CRYPT_OK) {
1400             fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
1401            exit(EXIT_FAILURE);
1402         }
1403         if ((err = gcm_add_iv(&gcm, IV, 16)) != CRYPT_OK) {
1404             fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
1405            exit(EXIT_FAILURE);
1406         }
1407         if ((err = gcm_add_aad(&gcm, NULL, 0)) != CRYPT_OK) {
1408             fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
1409            exit(EXIT_FAILURE);
1410         }
1411         if ((err = gcm_process(&gcm, buf, MAC_SIZE*1024, buf, GCM_ENCRYPT)) != CRYPT_OK) {
1412             fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
1413            exit(EXIT_FAILURE);
1414         }
1415 
1416         if ((err = gcm_done(&gcm, tag, &z)) != CRYPT_OK) {
1417             fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
1418            exit(EXIT_FAILURE);
1419         }
1420         t1 = t_read() - t1;
1421         if (t1 < t2) t2 = t1;
1422    }
1423    fprintf(stderr, "GCM (precomp)\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
1424    }
1425 
1426 #endif
1427 
1428 }
1429 
time_encmacs(void)1430 void time_encmacs(void)
1431 {
1432    time_encmacs_(1);
1433    time_encmacs_(4);
1434    time_encmacs_(32);
1435 }
1436 
1437 /* $Source$ */
1438 /* $Revision$ */
1439 /* $Date$ */
1440