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