comparison testprof/x86_prof.c @ 191:1c15b283127b libtomcrypt-orig

Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 May 2005 13:23:02 +0000
parents
children 39d5d58461d6
comparison
equal deleted inserted replaced
143:5d99163f7e32 191:1c15b283127b
1 #include <tomcrypt_test.h>
2
3 prng_state yarrow_prng;
4
5 struct list results[100];
6 int no_results;
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
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 printf("\n");
25 if (type == 0) {
26 for (x = 0; x < no_results; x++) {
27 printf("%-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 */
43 ulong64 rdtsc (void)
44 {
45 #if defined __GNUC__
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(__ia64__) /* gcc-IA64 version */
55 unsigned long result;
56 __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
57 while (__builtin_expect ((int) result == -1, 0))
58 __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
59 return result;
60 #else
61 return XCLOCK();
62 #endif
63
64 // Microsoft and Intel Windows compilers
65 #elif defined _M_IX86
66 __asm rdtsc
67 #elif defined _M_AMD64
68 return __rdtsc ();
69 #elif defined _M_IA64
70 #if defined __INTEL_COMPILER
71 #include <ia64intrin.h>
72 #endif
73 return __getReg (3116);
74 #else
75 return XCLOCK();
76 #endif
77 }
78
79 static ulong64 timer, skew = 0;
80
81 void t_start(void)
82 {
83 timer = rdtsc();
84 }
85
86 ulong64 t_read(void)
87 {
88 return rdtsc() - timer;
89 }
90
91 void init_timer(void)
92 {
93 ulong64 c1, c2, t1, t2, t3;
94 unsigned long y1;
95
96 c1 = c2 = (ulong64)-1;
97 for (y1 = 0; y1 < TIMES*100; y1++) {
98 t_start();
99 t1 = t_read();
100 t3 = t_read();
101 t2 = (t_read() - t1)>>1;
102
103 c1 = (t1 > c1) ? t1 : c1;
104 c2 = (t2 > c2) ? t2 : c2;
105 }
106 skew = c2 - c1;
107 printf("Clock Skew: %lu\n", (unsigned long)skew);
108 }
109
110 void reg_algs(void)
111 {
112 int err;
113 #ifdef RIJNDAEL
114 register_cipher (&aes_desc);
115 #endif
116 #ifdef BLOWFISH
117 register_cipher (&blowfish_desc);
118 #endif
119 #ifdef XTEA
120 register_cipher (&xtea_desc);
121 #endif
122 #ifdef RC5
123 register_cipher (&rc5_desc);
124 #endif
125 #ifdef RC6
126 register_cipher (&rc6_desc);
127 #endif
128 #ifdef SAFERP
129 register_cipher (&saferp_desc);
130 #endif
131 #ifdef TWOFISH
132 register_cipher (&twofish_desc);
133 #endif
134 #ifdef SAFER
135 register_cipher (&safer_k64_desc);
136 register_cipher (&safer_sk64_desc);
137 register_cipher (&safer_k128_desc);
138 register_cipher (&safer_sk128_desc);
139 #endif
140 #ifdef RC2
141 register_cipher (&rc2_desc);
142 #endif
143 #ifdef DES
144 register_cipher (&des_desc);
145 register_cipher (&des3_desc);
146 #endif
147 #ifdef CAST5
148 register_cipher (&cast5_desc);
149 #endif
150 #ifdef NOEKEON
151 register_cipher (&noekeon_desc);
152 #endif
153 #ifdef SKIPJACK
154 register_cipher (&skipjack_desc);
155 #endif
156 #ifdef KHAZAD
157 register_cipher (&khazad_desc);
158 #endif
159 #ifdef ANUBIS
160 register_cipher (&anubis_desc);
161 #endif
162
163 #ifdef TIGER
164 register_hash (&tiger_desc);
165 #endif
166 #ifdef MD2
167 register_hash (&md2_desc);
168 #endif
169 #ifdef MD4
170 register_hash (&md4_desc);
171 #endif
172 #ifdef MD5
173 register_hash (&md5_desc);
174 #endif
175 #ifdef SHA1
176 register_hash (&sha1_desc);
177 #endif
178 #ifdef SHA224
179 register_hash (&sha224_desc);
180 #endif
181 #ifdef SHA256
182 register_hash (&sha256_desc);
183 #endif
184 #ifdef SHA384
185 register_hash (&sha384_desc);
186 #endif
187 #ifdef SHA512
188 register_hash (&sha512_desc);
189 #endif
190 #ifdef RIPEMD128
191 register_hash (&rmd128_desc);
192 #endif
193 #ifdef RIPEMD160
194 register_hash (&rmd160_desc);
195 #endif
196 #ifdef WHIRLPOOL
197 register_hash (&whirlpool_desc);
198 #endif
199 #ifdef CHC_HASH
200 register_hash(&chc_desc);
201 if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) {
202 printf("chc_register error: %s\n", error_to_string(err));
203 exit(EXIT_FAILURE);
204 }
205 #endif
206
207
208 #ifndef YARROW
209 #error This demo requires Yarrow.
210 #endif
211 register_prng(&yarrow_desc);
212 #ifdef FORTUNA
213 register_prng(&fortuna_desc);
214 #endif
215 #ifdef RC4
216 register_prng(&rc4_desc);
217 #endif
218 #ifdef SOBER128
219 register_prng(&sober128_desc);
220 #endif
221
222 rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL);
223 }
224
225 int time_keysched(void)
226 {
227 unsigned long x, y1;
228 ulong64 t1, c1;
229 symmetric_key skey;
230 int kl;
231 int (*func) (const unsigned char *, int , int , symmetric_key *);
232 unsigned char key[MAXBLOCKSIZE];
233
234 printf ("\n\nKey Schedule Time Trials for the Symmetric Ciphers:\n(Times are cycles per key)\n");
235 no_results = 0;
236 for (x = 0; cipher_descriptor[x].name != NULL; x++) {
237 #define DO1(k) func(k, kl, 0, &skey);
238
239 func = cipher_descriptor[x].setup;
240 kl = cipher_descriptor[x].min_key_length;
241 c1 = (ulong64)-1;
242 for (y1 = 0; y1 < KTIMES; y1++) {
243 yarrow_read(key, kl, &yarrow_prng);
244 t_start();
245 DO1(key);
246 t1 = t_read();
247 c1 = (t1 > c1) ? c1 : t1;
248 }
249 t1 = c1 - skew;
250 results[no_results].spd1 = results[no_results].avg = t1;
251 results[no_results++].id = x;
252 printf("."); fflush(stdout);
253
254 #undef DO1
255 }
256 tally_results(0);
257
258 return 0;
259 }
260
261 int time_cipher(void)
262 {
263 unsigned long x, y1;
264 ulong64 t1, t2, c1, c2, a1, a2;
265 symmetric_ECB ecb;
266 unsigned char key[MAXBLOCKSIZE], pt[4096];
267 int err;
268
269 printf ("\n\nECB Time Trials for the Symmetric Ciphers:\n");
270 no_results = 0;
271 for (x = 0; cipher_descriptor[x].name != NULL; x++) {
272 ecb_start(x, key, cipher_descriptor[x].min_key_length, 0, &ecb);
273
274 /* sanity check on cipher */
275 if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
276 fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
277 exit(EXIT_FAILURE);
278 }
279
280 #define DO1 ecb_encrypt(pt, pt, sizeof(pt), &ecb);
281 #define DO2 DO1 DO1
282
283 c1 = c2 = (ulong64)-1;
284 for (y1 = 0; y1 < 100; y1++) {
285 t_start();
286 DO1;
287 t1 = t_read();
288 DO2;
289 t2 = t_read();
290 t2 -= t1;
291
292 c1 = (t1 > c1 ? c1 : t1);
293 c2 = (t2 > c2 ? c2 : t2);
294 }
295 a1 = c2 - c1 - skew;
296
297 #undef DO1
298 #undef DO2
299 #define DO1 ecb_decrypt(pt, pt, sizeof(pt), &ecb);
300 #define DO2 DO1 DO1
301
302 c1 = c2 = (ulong64)-1;
303 for (y1 = 0; y1 < 100; y1++) {
304 t_start();
305 DO1;
306 t1 = t_read();
307 DO2;
308 t2 = t_read();
309 t2 -= t1;
310
311 c1 = (t1 > c1 ? c1 : t1);
312 c2 = (t2 > c2 ? c2 : t2);
313 }
314 a2 = c2 - c1 - skew;
315
316 results[no_results].id = x;
317 results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
318 results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
319 results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
320 ++no_results;
321 printf("."); fflush(stdout);
322
323 #undef DO2
324 #undef DO1
325 }
326 tally_results(1);
327
328 return 0;
329 }
330
331 #ifdef CBC
332 int time_cipher2(void)
333 {
334 unsigned long x, y1;
335 ulong64 t1, t2, c1, c2, a1, a2;
336 symmetric_CBC cbc;
337 unsigned char key[MAXBLOCKSIZE], pt[4096];
338 int err;
339
340 printf ("\n\nCBC Time Trials for the Symmetric Ciphers:\n");
341 no_results = 0;
342 for (x = 0; cipher_descriptor[x].name != NULL; x++) {
343 cbc_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, &cbc);
344
345 /* sanity check on cipher */
346 if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
347 fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
348 exit(EXIT_FAILURE);
349 }
350
351 #define DO1 cbc_encrypt(pt, pt, sizeof(pt), &cbc);
352 #define DO2 DO1 DO1
353
354 c1 = c2 = (ulong64)-1;
355 for (y1 = 0; y1 < 100; y1++) {
356 t_start();
357 DO1;
358 t1 = t_read();
359 DO2;
360 t2 = t_read();
361 t2 -= t1;
362
363 c1 = (t1 > c1 ? c1 : t1);
364 c2 = (t2 > c2 ? c2 : t2);
365 }
366 a1 = c2 - c1 - skew;
367
368 #undef DO1
369 #undef DO2
370 #define DO1 cbc_decrypt(pt, pt, sizeof(pt), &cbc);
371 #define DO2 DO1 DO1
372
373 c1 = c2 = (ulong64)-1;
374 for (y1 = 0; y1 < 100; y1++) {
375 t_start();
376 DO1;
377 t1 = t_read();
378 DO2;
379 t2 = t_read();
380 t2 -= t1;
381
382 c1 = (t1 > c1 ? c1 : t1);
383 c2 = (t2 > c2 ? c2 : t2);
384 }
385 a2 = c2 - c1 - skew;
386
387 results[no_results].id = x;
388 results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
389 results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
390 results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
391 ++no_results;
392 printf("."); fflush(stdout);
393
394 #undef DO2
395 #undef DO1
396 }
397 tally_results(1);
398
399 return 0;
400 }
401 #else
402 int time_cipher2(void) { printf("NO CBC\n"); return 0; }
403 #endif
404
405 #ifdef CTR
406 int time_cipher3(void)
407 {
408 unsigned long x, y1;
409 ulong64 t1, t2, c1, c2, a1, a2;
410 symmetric_CTR ctr;
411 unsigned char key[MAXBLOCKSIZE], pt[4096];
412 int err;
413
414 printf ("\n\nCTR Time Trials for the Symmetric Ciphers:\n");
415 no_results = 0;
416 for (x = 0; cipher_descriptor[x].name != NULL; x++) {
417 ctr_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, &ctr);
418
419 /* sanity check on cipher */
420 if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
421 fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
422 exit(EXIT_FAILURE);
423 }
424
425 #define DO1 ctr_encrypt(pt, pt, sizeof(pt), &ctr);
426 #define DO2 DO1 DO1
427
428 c1 = c2 = (ulong64)-1;
429 for (y1 = 0; y1 < 100; y1++) {
430 t_start();
431 DO1;
432 t1 = t_read();
433 DO2;
434 t2 = t_read();
435 t2 -= t1;
436
437 c1 = (t1 > c1 ? c1 : t1);
438 c2 = (t2 > c2 ? c2 : t2);
439 }
440 a1 = c2 - c1 - skew;
441
442 #undef DO1
443 #undef DO2
444 #define DO1 ctr_decrypt(pt, pt, sizeof(pt), &ctr);
445 #define DO2 DO1 DO1
446
447 c1 = c2 = (ulong64)-1;
448 for (y1 = 0; y1 < 100; y1++) {
449 t_start();
450 DO1;
451 t1 = t_read();
452 DO2;
453 t2 = t_read();
454 t2 -= t1;
455
456 c1 = (t1 > c1 ? c1 : t1);
457 c2 = (t2 > c2 ? c2 : t2);
458 }
459 a2 = c2 - c1 - skew;
460
461 results[no_results].id = x;
462 results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
463 results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
464 results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
465 ++no_results;
466 printf("."); fflush(stdout);
467
468 #undef DO2
469 #undef DO1
470 }
471 tally_results(1);
472
473 return 0;
474 }
475 #else
476 int time_cipher3(void) { printf("NO CTR\n"); return 0; }
477 #endif
478
479 int time_hash(void)
480 {
481 unsigned long x, y1, len;
482 ulong64 t1, t2, c1, c2;
483 hash_state md;
484 int (*func)(hash_state *, const unsigned char *, unsigned long), err;
485 unsigned char pt[MAXBLOCKSIZE];
486
487
488 printf ("\n\nHASH Time Trials for:\n");
489 no_results = 0;
490 for (x = 0; hash_descriptor[x].name != NULL; x++) {
491
492 /* sanity check on hash */
493 if ((err = hash_descriptor[x].test()) != CRYPT_OK) {
494 fprintf(stderr, "\n\nERROR: Hash %s failed self-test %s\n", hash_descriptor[x].name, error_to_string(err));
495 exit(EXIT_FAILURE);
496 }
497
498 hash_descriptor[x].init(&md);
499
500 #define DO1 func(&md,pt,len);
501 #define DO2 DO1 DO1
502
503 func = hash_descriptor[x].process;
504 len = hash_descriptor[x].blocksize;
505
506 c1 = c2 = (ulong64)-1;
507 for (y1 = 0; y1 < TIMES; y1++) {
508 t_start();
509 DO1;
510 t1 = t_read();
511 DO2;
512 t2 = t_read() - t1;
513 c1 = (t1 > c1) ? c1 : t1;
514 c2 = (t2 > c2) ? c2 : t2;
515 }
516 t1 = c2 - c1 - skew;
517 t1 = ((t1 * CONST64(1000))) / ((ulong64)hash_descriptor[x].blocksize);
518 results[no_results].id = x;
519 results[no_results].spd1 = results[no_results].avg = t1;
520 ++no_results;
521 printf("."); fflush(stdout);
522 #undef DO2
523 #undef DO1
524 }
525 tally_results(2);
526
527 return 0;
528 }
529
530 #ifdef MPI
531 void time_mult(void)
532 {
533 ulong64 t1, t2;
534 unsigned long x, y;
535 mp_int a, b, c;
536
537 printf("Timing Multiplying:\n");
538 mp_init_multi(&a,&b,&c,NULL);
539 for (x = 128/DIGIT_BIT; x <= 1536/DIGIT_BIT; x += 128/DIGIT_BIT) {
540 mp_rand(&a, x);
541 mp_rand(&b, x);
542
543 #define DO1 mp_mul(&a, &b, &c);
544 #define DO2 DO1; DO1;
545
546 t2 = -1;
547 for (y = 0; y < TIMES; y++) {
548 t_start();
549 t1 = t_read();
550 DO2;
551 t1 = (t_read() - t1)>>1;
552 if (t1 < t2) t2 = t1;
553 }
554 printf("%4lu bits: %9llu cycles\n", x*DIGIT_BIT, t2);
555 }
556 mp_clear_multi(&a,&b,&c,NULL);
557
558 #undef DO1
559 #undef DO2
560 }
561
562 void time_sqr(void)
563 {
564 ulong64 t1, t2;
565 unsigned long x, y;
566 mp_int a, b;
567
568 printf("Timing Squaring:\n");
569 mp_init_multi(&a,&b,NULL);
570 for (x = 128/DIGIT_BIT; x <= 1536/DIGIT_BIT; x += 128/DIGIT_BIT) {
571 mp_rand(&a, x);
572
573 #define DO1 mp_sqr(&a, &b);
574 #define DO2 DO1; DO1;
575
576 t2 = -1;
577 for (y = 0; y < TIMES; y++) {
578 t_start();
579 t1 = t_read();
580 DO2;
581 t1 = (t_read() - t1)>>1;
582 if (t1 < t2) t2 = t1;
583 }
584 printf("%4lu bits: %9llu cycles\n", x*DIGIT_BIT, t2);
585 }
586 mp_clear_multi(&a,&b,NULL);
587
588 #undef DO1
589 #undef DO2
590 }
591 #else
592 void time_mult(void) { printf("NO MULT\n"); }
593 void time_sqr(void) { printf("NO SQR\n"); }
594 #endif
595
596 void time_prng(void)
597 {
598 ulong64 t1, t2;
599 unsigned char buf[4096];
600 prng_state tprng;
601 unsigned long x, y;
602 int err;
603
604 printf("Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n");
605 for (x = 0; prng_descriptor[x].name != NULL; x++) {
606
607 /* sanity check on prng */
608 if ((err = prng_descriptor[x].test()) != CRYPT_OK) {
609 fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor[x].name, error_to_string(err));
610 exit(EXIT_FAILURE);
611 }
612
613 prng_descriptor[x].start(&tprng);
614 zeromem(buf, 256);
615 prng_descriptor[x].add_entropy(buf, 256, &tprng);
616 prng_descriptor[x].ready(&tprng);
617 t2 = -1;
618
619 #define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { printf("\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
620 #define DO2 DO1 DO1
621 for (y = 0; y < 10000; y++) {
622 t_start();
623 t1 = t_read();
624 DO2;
625 t1 = (t_read() - t1)>>1;
626 if (t1 < t2) t2 = t1;
627 }
628 printf("%20s: %5llu ", prng_descriptor[x].name, t2>>12);
629 #undef DO2
630 #undef DO1
631
632 #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);
633 #define DO2 DO1 DO1
634 for (y = 0; y < 10000; y++) {
635 t_start();
636 t1 = t_read();
637 DO2;
638 t1 = (t_read() - t1)>>1;
639 if (t1 < t2) t2 = t1;
640 }
641 printf("%5llu\n", t2);
642 #undef DO2
643 #undef DO1
644
645 }
646 }
647
648 #ifdef MRSA
649 /* time various RSA operations */
650 void time_rsa(void)
651 {
652 rsa_key key;
653 ulong64 t1, t2;
654 unsigned char buf[2][4096];
655 unsigned long x, y, z, zzz;
656 int err, zz;
657
658 for (x = 1024; x <= 2048; x += 512) {
659 t2 = 0;
660 for (y = 0; y < 16; y++) {
661 t_start();
662 t1 = t_read();
663 if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) {
664 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));
665 exit(EXIT_FAILURE);
666 }
667 t1 = t_read() - t1;
668 t2 += t1;
669
670 if (y < 15) {
671 rsa_free(&key);
672 }
673 }
674 t2 >>= 4;
675 printf("RSA-%lu make_key took %15llu cycles\n", x, t2);
676
677 t2 = 0;
678 for (y = 0; y < 16; y++) {
679 t_start();
680 t1 = t_read();
681 z = sizeof(buf[1]);
682 if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, "testprog", 8, &yarrow_prng,
683 find_prng("yarrow"), find_hash("sha1"),
684 &key)) != CRYPT_OK) {
685 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));
686 exit(EXIT_FAILURE);
687 }
688 t1 = t_read() - t1;
689 t2 += t1;
690 }
691 t2 >>= 4;
692 printf("RSA-%lu encrypt_key took %15llu cycles\n", x, t2);
693
694 t2 = 0;
695 for (y = 0; y < 16; y++) {
696 t_start();
697 t1 = t_read();
698 zzz = sizeof(buf[0]);
699 if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, "testprog", 8, find_hash("sha1"),
700 &zz, &key)) != CRYPT_OK) {
701 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));
702 exit(EXIT_FAILURE);
703 }
704 t1 = t_read() - t1;
705 t2 += t1;
706 }
707 t2 >>= 4;
708 printf("RSA-%lu decrypt_key took %15llu cycles\n", x, t2);
709
710
711 rsa_free(&key);
712 }
713 }
714 #else
715 void time_rsa(void) { printf("NO RSA\n"); }
716 #endif
717
718 #ifdef MECC
719 /* time various ECC operations */
720 void time_ecc(void)
721 {
722 ecc_key key;
723 ulong64 t1, t2;
724 unsigned char buf[2][4096];
725 unsigned long i, x, y, z;
726 int err;
727 static unsigned long sizes[] = {160/8, 256/8, 521/8, 100000};
728
729 for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
730 t2 = 0;
731 for (y = 0; y < 16; y++) {
732 t_start();
733 t1 = t_read();
734 if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
735 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));
736 exit(EXIT_FAILURE);
737 }
738 t1 = t_read() - t1;
739 t2 += t1;
740
741 if (y < 15) {
742 ecc_free(&key);
743 }
744 }
745 t2 >>= 4;
746 printf("ECC-%lu make_key took %15llu cycles\n", x*8, t2);
747
748 t2 = 0;
749 for (y = 0; y < 16; y++) {
750 t_start();
751 t1 = t_read();
752 z = sizeof(buf[1]);
753 if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
754 &key)) != CRYPT_OK) {
755 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));
756 exit(EXIT_FAILURE);
757 }
758 t1 = t_read() - t1;
759 t2 += t1;
760 }
761 t2 >>= 4;
762 printf("ECC-%lu encrypt_key took %15llu cycles\n", x*8, t2);
763 ecc_free(&key);
764 }
765 }
766 #else
767 void time_ecc(void) { printf("NO ECC\n"); }
768 #endif
769
770 #ifdef MDH
771 /* time various DH operations */
772 void time_dh(void)
773 {
774 dh_key key;
775 ulong64 t1, t2;
776 unsigned char buf[2][4096];
777 unsigned long i, x, y, z;
778 int err;
779 static unsigned long sizes[] = {768/8, 1024/8, 1536/8, 2048/8, 3072/8, 4096/8, 100000};
780
781 for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
782 t2 = 0;
783 for (y = 0; y < 16; y++) {
784 t_start();
785 t1 = t_read();
786 if ((err = dh_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
787 fprintf(stderr, "\n\ndh_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
788 exit(EXIT_FAILURE);
789 }
790 t1 = t_read() - t1;
791 t2 += t1;
792
793 if (y < 15) {
794 dh_free(&key);
795 }
796 }
797 t2 >>= 4;
798 printf("DH-%4lu make_key took %15llu cycles\n", x*8, t2);
799
800 t2 = 0;
801 for (y = 0; y < 16; y++) {
802 t_start();
803 t1 = t_read();
804 z = sizeof(buf[1]);
805 if ((err = dh_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
806 &key)) != CRYPT_OK) {
807 fprintf(stderr, "\n\ndh_encrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
808 exit(EXIT_FAILURE);
809 }
810 t1 = t_read() - t1;
811 t2 += t1;
812 }
813 t2 >>= 4;
814 printf("DH-%4lu encrypt_key took %15llu cycles\n", x*8, t2);
815 dh_free(&key);
816 }
817 }
818 #else
819 void time_dh(void) { printf("NO DH\n"); }
820 #endif
821
822 void time_macs_(unsigned long MAC_SIZE)
823 {
824 unsigned char *buf, key[16], tag[16];
825 ulong64 t1, t2;
826 unsigned long x, z;
827 int err, cipher_idx, hash_idx;
828
829 printf("\nMAC Timings (cycles/byte on %dKB blocks):\n", MAC_SIZE);
830
831 buf = XMALLOC(MAC_SIZE*1024);
832 if (buf == NULL) {
833 fprintf(stderr, "\n\nout of heap yo\n\n");
834 exit(EXIT_FAILURE);
835 }
836
837 cipher_idx = find_cipher("aes");
838 hash_idx = find_hash("md5");
839
840 yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
841 yarrow_read(key, 16, &yarrow_prng);
842
843 #ifdef OMAC
844 t2 = -1;
845 for (x = 0; x < 10000; x++) {
846 t_start();
847 t1 = t_read();
848 z = 16;
849 if ((err = omac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
850 fprintf(stderr, "\n\nomac error... %s\n", error_to_string(err));
851 exit(EXIT_FAILURE);
852 }
853 t1 = t_read() - t1;
854 if (t1 < t2) t2 = t1;
855 }
856 printf("OMAC-AES\t\t%9llu\n", t2/(MAC_SIZE*1024));
857 #endif
858
859 #ifdef PMAC
860 t2 = -1;
861 for (x = 0; x < 10000; x++) {
862 t_start();
863 t1 = t_read();
864 z = 16;
865 if ((err = pmac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
866 fprintf(stderr, "\n\npmac error... %s\n", error_to_string(err));
867 exit(EXIT_FAILURE);
868 }
869 t1 = t_read() - t1;
870 if (t1 < t2) t2 = t1;
871 }
872 printf("PMAC-AES\t\t%9llu\n", t2/(MAC_SIZE*1024));
873 #endif
874
875 #ifdef PELICAN
876 t2 = -1;
877 for (x = 0; x < 10000; x++) {
878 t_start();
879 t1 = t_read();
880 z = 16;
881 if ((err = pelican_memory(key, 16, buf, MAC_SIZE*1024, tag)) != CRYPT_OK) {
882 fprintf(stderr, "\n\npelican error... %s\n", error_to_string(err));
883 exit(EXIT_FAILURE);
884 }
885 t1 = t_read() - t1;
886 if (t1 < t2) t2 = t1;
887 }
888 printf("PELICAN \t\t%9llu\n", t2/(MAC_SIZE*1024));
889 #endif
890
891 #ifdef HMAC
892 t2 = -1;
893 for (x = 0; x < 10000; x++) {
894 t_start();
895 t1 = t_read();
896 z = 16;
897 if ((err = hmac_memory(hash_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
898 fprintf(stderr, "\n\nhmac error... %s\n", error_to_string(err));
899 exit(EXIT_FAILURE);
900 }
901 t1 = t_read() - t1;
902 if (t1 < t2) t2 = t1;
903 }
904 printf("HMAC-MD5\t\t%9llu\n", t2/(MAC_SIZE*1024));
905 #endif
906
907 XFREE(buf);
908 }
909
910 void time_macs(void)
911 {
912 time_macs_(1);
913 time_macs_(4);
914 time_macs_(32);
915 }
916
917 void time_encmacs_(unsigned long MAC_SIZE)
918 {
919 unsigned char *buf, IV[16], key[16], tag[16];
920 ulong64 t1, t2;
921 unsigned long x, z;
922 int err, cipher_idx;
923
924 printf("\nENC+MAC Timings (zero byte AAD, 16 byte IV, cycles/byte on %dKB blocks):\n", MAC_SIZE);
925
926 buf = XMALLOC(MAC_SIZE*1024);
927 if (buf == NULL) {
928 fprintf(stderr, "\n\nout of heap yo\n\n");
929 exit(EXIT_FAILURE);
930 }
931
932 cipher_idx = find_cipher("aes");
933
934 yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
935 yarrow_read(key, 16, &yarrow_prng);
936 yarrow_read(IV, 16, &yarrow_prng);
937
938 #ifdef EAX_MODE
939 t2 = -1;
940 for (x = 0; x < 10000; x++) {
941 t_start();
942 t1 = t_read();
943 z = 16;
944 if ((err = eax_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
945 fprintf(stderr, "\nEAX error... %s\n", error_to_string(err));
946 exit(EXIT_FAILURE);
947 }
948 t1 = t_read() - t1;
949 if (t1 < t2) t2 = t1;
950 }
951 printf("EAX \t\t%9llu\n", t2/(MAC_SIZE*1024));
952 #endif
953
954 #ifdef OCB_MODE
955 t2 = -1;
956 for (x = 0; x < 10000; x++) {
957 t_start();
958 t1 = t_read();
959 z = 16;
960 if ((err = ocb_encrypt_authenticate_memory(cipher_idx, key, 16, IV, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
961 fprintf(stderr, "\nOCB error... %s\n", error_to_string(err));
962 exit(EXIT_FAILURE);
963 }
964 t1 = t_read() - t1;
965 if (t1 < t2) t2 = t1;
966 }
967 printf("OCB \t\t%9llu\n", t2/(MAC_SIZE*1024));
968 #endif
969
970 #ifdef CCM_MODE
971 t2 = -1;
972 for (x = 0; x < 10000; x++) {
973 t_start();
974 t1 = t_read();
975 z = 16;
976 if ((err = ccm_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
977 fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
978 exit(EXIT_FAILURE);
979 }
980 t1 = t_read() - t1;
981 if (t1 < t2) t2 = t1;
982 }
983 printf("CCM \t\t%9llu\n", t2/(MAC_SIZE*1024));
984 #endif
985
986 #ifdef GCM_MODE
987 t2 = -1;
988 for (x = 0; x < 100; x++) {
989 t_start();
990 t1 = t_read();
991 z = 16;
992 if ((err = gcm_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, GCM_ENCRYPT)) != CRYPT_OK) {
993 fprintf(stderr, "\nGCM error... %s\n", error_to_string(err));
994 exit(EXIT_FAILURE);
995 }
996 t1 = t_read() - t1;
997 if (t1 < t2) t2 = t1;
998 }
999 printf("GCM (no-precomp)\t%9llu\n", t2/(MAC_SIZE*1024));
1000
1001 {
1002 gcm_state gcm;
1003
1004 if ((err = gcm_init(&gcm, cipher_idx, key, 16)) != CRYPT_OK) { printf("gcm_init: %s\n", error_to_string(err)); exit(EXIT_FAILURE); }
1005 t2 = -1;
1006 for (x = 0; x < 10000; x++) {
1007 t_start();
1008 t1 = t_read();
1009 z = 16;
1010 if ((err = gcm_reset(&gcm)) != CRYPT_OK) {
1011 fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
1012 exit(EXIT_FAILURE);
1013 }
1014 if ((err = gcm_add_iv(&gcm, IV, 16)) != CRYPT_OK) {
1015 fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
1016 exit(EXIT_FAILURE);
1017 }
1018 if ((err = gcm_add_aad(&gcm, NULL, 0)) != CRYPT_OK) {
1019 fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
1020 exit(EXIT_FAILURE);
1021 }
1022 if ((err = gcm_process(&gcm, buf, MAC_SIZE*1024, buf, GCM_ENCRYPT)) != CRYPT_OK) {
1023 fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
1024 exit(EXIT_FAILURE);
1025 }
1026
1027 if ((err = gcm_done(&gcm, tag, &z)) != CRYPT_OK) {
1028 fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
1029 exit(EXIT_FAILURE);
1030 }
1031 t1 = t_read() - t1;
1032 if (t1 < t2) t2 = t1;
1033 }
1034 printf("GCM (precomp)\t%9llu\n", t2/(MAC_SIZE*1024));
1035 }
1036
1037 #endif
1038
1039 }
1040
1041 void time_encmacs(void)
1042 {
1043 time_encmacs_(1);
1044 time_encmacs_(4);
1045 time_encmacs_(32);
1046 }