3
|
1 #include <mycrypt.h> |
|
2 |
|
3 #define KTIMES 25 |
|
4 #define TIMES 100000 |
|
5 |
|
6 struct list { |
|
7 int id; |
|
8 unsigned long spd1, spd2, avg; |
|
9 } results[100]; |
|
10 |
|
11 int no_results; |
|
12 |
|
13 int sorter(const void *a, const void *b) |
|
14 { |
|
15 const struct list *A, *B; |
|
16 A = a; |
|
17 B = b; |
|
18 if (A->avg < B->avg) return -1; |
|
19 if (A->avg > B->avg) return 1; |
|
20 return 0; |
|
21 } |
|
22 |
|
23 void tally_results(int type) |
|
24 { |
|
25 int x; |
|
26 |
|
27 // qsort the results |
|
28 qsort(results, no_results, sizeof(struct list), &sorter); |
|
29 |
|
30 printf("\n"); |
|
31 if (type == 0) { |
|
32 for (x = 0; x < no_results; x++) { |
|
33 printf("%-20s: Schedule at %6lu\n", cipher_descriptor[results[x].id].name, (unsigned long)results[x].spd1); |
|
34 } |
|
35 } else if (type == 1) { |
|
36 for (x = 0; x < no_results; x++) { |
|
37 printf |
|
38 ("%-20s: Encrypt at %5lu, Decrypt at %5lu\n", cipher_descriptor[results[x].id].name, results[x].spd1, results[x].spd2); |
|
39 } |
|
40 } else { |
|
41 for (x = 0; x < no_results; x++) { |
|
42 printf |
|
43 ("%-20s: Process at %5lu\n", hash_descriptor[results[x].id].name, results[x].spd1 / 1000); |
|
44 } |
|
45 } |
|
46 } |
|
47 |
|
48 /* RDTSC from Scott Duplichan */ |
|
49 static ulong64 rdtsc (void) |
|
50 { |
|
51 #if defined __GNUC__ |
143
|
52 #if defined(__i386__) || defined(__x86_64__) |
|
53 unsigned long long a; |
|
54 __asm__ __volatile__ ("rdtsc\nmovl %%eax,%0\nmovl %%edx,4+%0\n"::"m"(a):"%eax","%edx"); |
3
|
55 return a; |
|
56 #else /* gcc-IA64 version */ |
|
57 unsigned long result; |
|
58 __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); |
|
59 while (__builtin_expect ((int) result == -1, 0)) |
|
60 __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); |
|
61 return result; |
|
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 #error need rdtsc function for this build |
|
76 #endif |
|
77 } |
|
78 |
|
79 ulong64 timer, skew = 0; |
|
80 prng_state prng; |
|
81 |
|
82 void t_start(void) |
|
83 { |
|
84 timer = rdtsc(); |
|
85 } |
|
86 |
|
87 ulong64 t_read(void) |
|
88 { |
|
89 return rdtsc() - timer; |
|
90 } |
|
91 |
|
92 void init_timer(void) |
|
93 { |
|
94 ulong64 c1, c2, t1, t2, t3; |
|
95 unsigned long y1; |
|
96 |
|
97 c1 = c2 = (ulong64)-1; |
|
98 for (y1 = 0; y1 < TIMES*100; y1++) { |
|
99 t_start(); |
|
100 t1 = t_read(); |
|
101 t3 = t_read(); |
|
102 t2 = t_read() - t1; |
|
103 |
|
104 c1 = (c1 > t1) ? t1 : c1; |
|
105 c2 = (c2 > t2) ? t2 : c2; |
|
106 } |
|
107 skew = c2 - c1; |
|
108 printf("Clock Skew: %lu\n", (unsigned long)skew); |
|
109 } |
|
110 |
|
111 void reg_algs(void) |
|
112 { |
143
|
113 int err; |
3
|
114 #ifdef RIJNDAEL |
|
115 register_cipher (&aes_desc); |
|
116 #endif |
|
117 #ifdef BLOWFISH |
|
118 register_cipher (&blowfish_desc); |
|
119 #endif |
|
120 #ifdef XTEA |
|
121 register_cipher (&xtea_desc); |
|
122 #endif |
|
123 #ifdef RC5 |
|
124 register_cipher (&rc5_desc); |
|
125 #endif |
|
126 #ifdef RC6 |
|
127 register_cipher (&rc6_desc); |
|
128 #endif |
|
129 #ifdef SAFERP |
|
130 register_cipher (&saferp_desc); |
|
131 #endif |
|
132 #ifdef TWOFISH |
|
133 register_cipher (&twofish_desc); |
|
134 #endif |
|
135 #ifdef SAFER |
|
136 register_cipher (&safer_k64_desc); |
|
137 register_cipher (&safer_sk64_desc); |
|
138 register_cipher (&safer_k128_desc); |
|
139 register_cipher (&safer_sk128_desc); |
|
140 #endif |
|
141 #ifdef RC2 |
|
142 register_cipher (&rc2_desc); |
|
143 #endif |
|
144 #ifdef DES |
|
145 register_cipher (&des_desc); |
|
146 register_cipher (&des3_desc); |
|
147 #endif |
|
148 #ifdef CAST5 |
|
149 register_cipher (&cast5_desc); |
|
150 #endif |
|
151 #ifdef NOEKEON |
|
152 register_cipher (&noekeon_desc); |
|
153 #endif |
|
154 #ifdef SKIPJACK |
|
155 register_cipher (&skipjack_desc); |
|
156 #endif |
|
157 |
|
158 #ifdef TIGER |
|
159 register_hash (&tiger_desc); |
|
160 #endif |
|
161 #ifdef MD2 |
|
162 register_hash (&md2_desc); |
|
163 #endif |
|
164 #ifdef MD4 |
|
165 register_hash (&md4_desc); |
|
166 #endif |
|
167 #ifdef MD5 |
|
168 register_hash (&md5_desc); |
|
169 #endif |
|
170 #ifdef SHA1 |
|
171 register_hash (&sha1_desc); |
|
172 #endif |
|
173 #ifdef SHA224 |
|
174 register_hash (&sha224_desc); |
|
175 #endif |
|
176 #ifdef SHA256 |
|
177 register_hash (&sha256_desc); |
|
178 #endif |
|
179 #ifdef SHA384 |
|
180 register_hash (&sha384_desc); |
|
181 #endif |
|
182 #ifdef SHA512 |
|
183 register_hash (&sha512_desc); |
|
184 #endif |
|
185 #ifdef RIPEMD128 |
|
186 register_hash (&rmd128_desc); |
|
187 #endif |
|
188 #ifdef RIPEMD160 |
|
189 register_hash (&rmd160_desc); |
|
190 #endif |
|
191 #ifdef WHIRLPOOL |
|
192 register_hash (&whirlpool_desc); |
|
193 #endif |
143
|
194 #ifdef CHC_HASH |
|
195 register_hash(&chc_desc); |
|
196 if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) { |
|
197 printf("chc_register error: %s\n", error_to_string(err)); |
|
198 exit(EXIT_FAILURE); |
|
199 } |
|
200 #endif |
3
|
201 |
143
|
202 |
|
203 #ifndef YARROW |
|
204 #error This demo requires Yarrow. |
|
205 #endif |
3
|
206 register_prng(&yarrow_desc); |
143
|
207 #ifdef FORTUNA |
|
208 register_prng(&fortuna_desc); |
|
209 #endif |
|
210 #ifdef RC4 |
|
211 register_prng(&rc4_desc); |
|
212 #endif |
|
213 #ifdef SOBER128 |
|
214 register_prng(&sober128_desc); |
|
215 #endif |
|
216 |
3
|
217 rng_make_prng(128, find_prng("yarrow"), &prng, NULL); |
|
218 } |
|
219 |
|
220 int time_keysched(void) |
|
221 { |
143
|
222 unsigned long x, y1; |
3
|
223 ulong64 t1, c1; |
|
224 symmetric_key skey; |
|
225 int kl; |
|
226 int (*func) (const unsigned char *, int , int , symmetric_key *); |
|
227 unsigned char key[MAXBLOCKSIZE]; |
|
228 |
|
229 printf ("\n\nKey Schedule Time Trials for the Symmetric Ciphers:\n(Times are cycles per key)\n"); |
|
230 no_results = 0; |
|
231 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
|
232 #define DO1(k) func(k, kl, 0, &skey); |
|
233 |
|
234 func = cipher_descriptor[x].setup; |
|
235 kl = cipher_descriptor[x].min_key_length; |
|
236 c1 = (ulong64)-1; |
|
237 for (y1 = 0; y1 < KTIMES; y1++) { |
|
238 yarrow_read(key, kl, &prng); |
|
239 t_start(); |
|
240 DO1(key); |
|
241 t1 = t_read(); |
|
242 c1 = (t1 > c1) ? c1 : t1; |
|
243 } |
|
244 t1 = c1 - skew; |
|
245 results[no_results].spd1 = results[no_results].avg = t1; |
|
246 results[no_results++].id = x; |
|
247 printf("."); fflush(stdout); |
|
248 |
|
249 #undef DO1 |
|
250 } |
|
251 tally_results(0); |
|
252 |
|
253 return 0; |
|
254 } |
|
255 |
|
256 int time_cipher(void) |
|
257 { |
|
258 unsigned long x, y1; |
|
259 ulong64 t1, t2, c1, c2, a1, a2; |
|
260 symmetric_key skey; |
|
261 void (*func) (const unsigned char *, unsigned char *, symmetric_key *); |
|
262 unsigned char key[MAXBLOCKSIZE], pt[MAXBLOCKSIZE]; |
143
|
263 int err; |
3
|
264 |
|
265 printf ("\n\nECB Time Trials for the Symmetric Ciphers:\n"); |
|
266 no_results = 0; |
|
267 for (x = 0; cipher_descriptor[x].name != NULL; x++) { |
|
268 cipher_descriptor[x].setup (key, cipher_descriptor[x].min_key_length, 0, |
|
269 &skey); |
|
270 |
143
|
271 /* sanity check on cipher */ |
|
272 if ((err = cipher_descriptor[x].test()) != CRYPT_OK) { |
|
273 fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err)); |
|
274 exit(EXIT_FAILURE); |
|
275 } |
|
276 |
3
|
277 #define DO1 func(pt,pt,&skey); |
|
278 #define DO2 DO1 DO1 |
|
279 |
|
280 func = cipher_descriptor[x].ecb_encrypt; |
|
281 c1 = c2 = (ulong64)-1; |
|
282 for (y1 = 0; y1 < TIMES; y1++) { |
|
283 t_start(); |
|
284 DO1; |
|
285 t1 = t_read(); |
|
286 DO2; |
|
287 t2 = t_read(); |
|
288 t2 -= t1; |
|
289 |
|
290 c1 = (t1 > c1 ? c1 : t1); |
|
291 c2 = (t2 > c2 ? c2 : t2); |
|
292 } |
|
293 a1 = c2 - c1 - skew; |
|
294 |
|
295 |
|
296 func = cipher_descriptor[x].ecb_decrypt; |
|
297 c1 = c2 = (ulong64)-1; |
|
298 for (y1 = 0; y1 < TIMES; y1++) { |
|
299 t_start(); |
|
300 DO1; |
|
301 t1 = t_read(); |
|
302 DO2; |
|
303 t2 = t_read(); |
|
304 t2 -= t1; |
|
305 |
|
306 c1 = (t1 > c1 ? c1 : t1); |
|
307 c2 = (t2 > c2 ? c2 : t2); |
|
308 } |
|
309 a2 = c2 - c1 - skew; |
|
310 |
|
311 results[no_results].id = x; |
|
312 results[no_results].spd1 = a1/cipher_descriptor[x].block_length; |
|
313 results[no_results].spd2 = a2/cipher_descriptor[x].block_length;; |
|
314 results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2; |
|
315 ++no_results; |
|
316 printf("."); fflush(stdout); |
|
317 |
|
318 #undef DO2 |
|
319 #undef DO1 |
|
320 } |
|
321 tally_results(1); |
|
322 |
|
323 return 0; |
|
324 } |
|
325 |
|
326 int time_hash(void) |
|
327 { |
|
328 unsigned long x, y1, len; |
|
329 ulong64 t1, t2, c1, c2; |
|
330 hash_state md; |
143
|
331 int (*func)(hash_state *, const unsigned char *, unsigned long), err; |
3
|
332 unsigned char pt[MAXBLOCKSIZE]; |
|
333 |
|
334 |
|
335 printf ("\n\nHASH Time Trials for:\n"); |
|
336 no_results = 0; |
|
337 for (x = 0; hash_descriptor[x].name != NULL; x++) { |
143
|
338 |
|
339 /* sanity check on hash */ |
|
340 if ((err = hash_descriptor[x].test()) != CRYPT_OK) { |
|
341 fprintf(stderr, "\n\nERROR: Hash %s failed self-test %s\n", hash_descriptor[x].name, error_to_string(err)); |
|
342 exit(EXIT_FAILURE); |
|
343 } |
|
344 |
3
|
345 hash_descriptor[x].init(&md); |
|
346 |
|
347 #define DO1 func(&md,pt,len); |
|
348 #define DO2 DO1 DO1 |
|
349 |
|
350 func = hash_descriptor[x].process; |
|
351 len = hash_descriptor[x].blocksize; |
|
352 |
|
353 c1 = c2 = (ulong64)-1; |
|
354 for (y1 = 0; y1 < TIMES; y1++) { |
|
355 t_start(); |
|
356 DO1; |
|
357 t1 = t_read(); |
|
358 DO2; |
|
359 t2 = t_read() - t1; |
|
360 c1 = (t1 > c1) ? c1 : t1; |
|
361 c2 = (t2 > c2) ? c2 : t2; |
|
362 } |
|
363 t1 = c2 - c1 - skew; |
|
364 t1 = ((t1 * CONST64(1000))) / ((ulong64)hash_descriptor[x].blocksize); |
|
365 results[no_results].id = x; |
|
366 results[no_results].spd1 = results[no_results].avg = t1; |
|
367 ++no_results; |
|
368 printf("."); fflush(stdout); |
|
369 #undef DO2 |
|
370 #undef DO1 |
|
371 } |
|
372 tally_results(2); |
|
373 |
|
374 return 0; |
|
375 } |
|
376 |
143
|
377 void time_mult(void) |
|
378 { |
|
379 ulong64 t1, t2; |
|
380 unsigned long x, y; |
|
381 mp_int a, b, c; |
|
382 |
|
383 printf("Timing Multiplying:\n"); |
|
384 mp_init_multi(&a,&b,&c,NULL); |
|
385 for (x = 128/DIGIT_BIT; x <= 1024/DIGIT_BIT; x += 128/DIGIT_BIT) { |
|
386 mp_rand(&a, x); |
|
387 mp_rand(&b, x); |
|
388 |
|
389 #define DO1 mp_mul(&a, &b, &c); |
|
390 #define DO2 DO1; DO1; |
|
391 |
|
392 t2 = -1; |
|
393 for (y = 0; y < TIMES; y++) { |
|
394 t_start(); |
|
395 t1 = t_read(); |
|
396 DO2; |
|
397 t1 = (t_read() - t1)>>1; |
|
398 if (t1 < t2) t2 = t1; |
|
399 } |
|
400 printf("%3lu digits: %9llu cycles\n", x, t2); |
|
401 } |
|
402 mp_clear_multi(&a,&b,&c,NULL); |
|
403 |
|
404 #undef DO1 |
|
405 #undef DO2 |
|
406 } |
|
407 |
|
408 void time_sqr(void) |
|
409 { |
|
410 ulong64 t1, t2; |
|
411 unsigned long x, y; |
|
412 mp_int a, b; |
|
413 |
|
414 printf("Timing Squaring:\n"); |
|
415 mp_init_multi(&a,&b,NULL); |
|
416 for (x = 128/DIGIT_BIT; x <= 1024/DIGIT_BIT; x += 128/DIGIT_BIT) { |
|
417 mp_rand(&a, x); |
|
418 |
|
419 #define DO1 mp_sqr(&a, &b); |
|
420 #define DO2 DO1; DO1; |
|
421 |
|
422 t2 = -1; |
|
423 for (y = 0; y < TIMES; y++) { |
|
424 t_start(); |
|
425 t1 = t_read(); |
|
426 DO2; |
|
427 t1 = (t_read() - t1)>>1; |
|
428 if (t1 < t2) t2 = t1; |
|
429 } |
|
430 printf("%3lu digits: %9llu cycles\n", x, t2); |
|
431 } |
|
432 mp_clear_multi(&a,&b,NULL); |
|
433 |
|
434 #undef DO1 |
|
435 #undef DO2 |
|
436 } |
|
437 |
|
438 void time_prng(void) |
|
439 { |
|
440 ulong64 t1, t2; |
|
441 unsigned char buf[4096]; |
|
442 prng_state tprng; |
|
443 unsigned long x, y; |
|
444 int err; |
|
445 |
|
446 printf("Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n"); |
|
447 for (x = 0; prng_descriptor[x].name != NULL; x++) { |
|
448 |
|
449 /* sanity check on prng */ |
|
450 if ((err = prng_descriptor[x].test()) != CRYPT_OK) { |
|
451 fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor[x].name, error_to_string(err)); |
|
452 exit(EXIT_FAILURE); |
|
453 } |
|
454 |
|
455 prng_descriptor[x].start(&tprng); |
|
456 zeromem(buf, 256); |
|
457 prng_descriptor[x].add_entropy(buf, 256, &tprng); |
|
458 prng_descriptor[x].ready(&tprng); |
|
459 t2 = -1; |
|
460 |
|
461 #define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { printf("\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); } |
|
462 #define DO2 DO1 DO1 |
|
463 for (y = 0; y < 10000; y++) { |
|
464 t_start(); |
|
465 t1 = t_read(); |
|
466 DO2; |
|
467 t1 = (t_read() - t1)>>1; |
|
468 if (t1 < t2) t2 = t1; |
|
469 } |
|
470 printf("%20s: %5llu ", prng_descriptor[x].name, t2>>12); |
|
471 #undef DO2 |
|
472 #undef DO1 |
|
473 |
|
474 #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); |
|
475 #define DO2 DO1 DO1 |
|
476 for (y = 0; y < 10000; y++) { |
|
477 t_start(); |
|
478 t1 = t_read(); |
|
479 DO2; |
|
480 t1 = (t_read() - t1)>>1; |
|
481 if (t1 < t2) t2 = t1; |
|
482 } |
|
483 printf("%5llu\n", t2); |
|
484 #undef DO2 |
|
485 #undef DO1 |
|
486 |
|
487 } |
|
488 } |
|
489 |
|
490 /* time various RSA operations */ |
|
491 void time_rsa(void) |
|
492 { |
|
493 rsa_key key; |
|
494 ulong64 t1, t2; |
|
495 unsigned char buf[2][4096]; |
|
496 unsigned long x, y, z, zzz; |
|
497 int err, zz; |
|
498 |
|
499 for (x = 1024; x <= 2048; x += 512) { |
|
500 t2 = 0; |
|
501 for (y = 0; y < 16; y++) { |
|
502 t_start(); |
|
503 t1 = t_read(); |
|
504 if ((err = rsa_make_key(&prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) { |
|
505 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)); |
|
506 exit(EXIT_FAILURE); |
|
507 } |
|
508 t1 = t_read() - t1; |
|
509 t2 += t1; |
|
510 |
|
511 if (y < 15) { |
|
512 rsa_free(&key); |
|
513 } |
|
514 } |
|
515 t2 >>= 4; |
|
516 printf("RSA-%lu make_key took %15llu cycles\n", x, t2); |
|
517 |
|
518 t2 = 0; |
|
519 for (y = 0; y < 16; y++) { |
|
520 t_start(); |
|
521 t1 = t_read(); |
|
522 z = sizeof(buf[1]); |
|
523 if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, "testprog", 8, &prng, |
|
524 find_prng("yarrow"), find_hash("sha1"), |
|
525 &key)) != CRYPT_OK) { |
|
526 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)); |
|
527 exit(EXIT_FAILURE); |
|
528 } |
|
529 t1 = t_read() - t1; |
|
530 t2 += t1; |
|
531 } |
|
532 t2 >>= 4; |
|
533 printf("RSA-%lu encrypt_key took %15llu cycles\n", x, t2); |
|
534 |
|
535 t2 = 0; |
|
536 for (y = 0; y < 16; y++) { |
|
537 t_start(); |
|
538 t1 = t_read(); |
|
539 zzz = sizeof(buf[0]); |
|
540 if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, "testprog", 8, &prng, |
|
541 find_prng("yarrow"), find_hash("sha1"), |
|
542 &zz, &key)) != CRYPT_OK) { |
|
543 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)); |
|
544 exit(EXIT_FAILURE); |
|
545 } |
|
546 t1 = t_read() - t1; |
|
547 t2 += t1; |
|
548 } |
|
549 t2 >>= 4; |
|
550 printf("RSA-%lu decrypt_key took %15llu cycles\n", x, t2); |
|
551 |
|
552 |
|
553 rsa_free(&key); |
|
554 } |
|
555 } |
|
556 |
|
557 /* time various ECC operations */ |
|
558 void time_ecc(void) |
|
559 { |
|
560 ecc_key key; |
|
561 ulong64 t1, t2; |
|
562 unsigned char buf[2][4096]; |
|
563 unsigned long i, x, y, z; |
|
564 int err; |
|
565 static unsigned long sizes[] = {160/8, 256/8, 521/8, 100000}; |
|
566 |
|
567 for (x = sizes[i=0]; x < 100000; x = sizes[++i]) { |
|
568 t2 = 0; |
|
569 for (y = 0; y < 16; y++) { |
|
570 t_start(); |
|
571 t1 = t_read(); |
|
572 if ((err = ecc_make_key(&prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) { |
|
573 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)); |
|
574 exit(EXIT_FAILURE); |
|
575 } |
|
576 t1 = t_read() - t1; |
|
577 t2 += t1; |
|
578 |
|
579 if (y < 15) { |
|
580 ecc_free(&key); |
|
581 } |
|
582 } |
|
583 t2 >>= 4; |
|
584 printf("ECC-%lu make_key took %15llu cycles\n", x*8, t2); |
|
585 |
|
586 t2 = 0; |
|
587 for (y = 0; y < 16; y++) { |
|
588 t_start(); |
|
589 t1 = t_read(); |
|
590 z = sizeof(buf[1]); |
|
591 if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &prng, find_prng("yarrow"), find_hash("sha1"), |
|
592 &key)) != CRYPT_OK) { |
|
593 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)); |
|
594 exit(EXIT_FAILURE); |
|
595 } |
|
596 t1 = t_read() - t1; |
|
597 t2 += t1; |
|
598 } |
|
599 t2 >>= 4; |
|
600 printf("ECC-%lu encrypt_key took %15llu cycles\n", x*8, t2); |
|
601 ecc_free(&key); |
|
602 } |
|
603 } |
|
604 |
|
605 /* time various DH operations */ |
|
606 void time_dh(void) |
|
607 { |
|
608 dh_key key; |
|
609 ulong64 t1, t2; |
|
610 unsigned char buf[2][4096]; |
|
611 unsigned long i, x, y, z; |
|
612 int err; |
|
613 static unsigned long sizes[] = {768/8, 1024/8, 1536/8, 2048/8, 3072/8, 4096/8, 100000}; |
|
614 |
|
615 for (x = sizes[i=0]; x < 100000; x = sizes[++i]) { |
|
616 t2 = 0; |
|
617 for (y = 0; y < 16; y++) { |
|
618 t_start(); |
|
619 t1 = t_read(); |
|
620 if ((err = dh_make_key(&prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) { |
|
621 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)); |
|
622 exit(EXIT_FAILURE); |
|
623 } |
|
624 t1 = t_read() - t1; |
|
625 t2 += t1; |
|
626 |
|
627 if (y < 15) { |
|
628 dh_free(&key); |
|
629 } |
|
630 } |
|
631 t2 >>= 4; |
|
632 printf("DH-%4lu make_key took %15llu cycles\n", x*8, t2); |
|
633 |
|
634 t2 = 0; |
|
635 for (y = 0; y < 16; y++) { |
|
636 t_start(); |
|
637 t1 = t_read(); |
|
638 z = sizeof(buf[1]); |
|
639 if ((err = dh_encrypt_key(buf[0], 20, buf[1], &z, &prng, find_prng("yarrow"), find_hash("sha1"), |
|
640 &key)) != CRYPT_OK) { |
|
641 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)); |
|
642 exit(EXIT_FAILURE); |
|
643 } |
|
644 t1 = t_read() - t1; |
|
645 t2 += t1; |
|
646 } |
|
647 t2 >>= 4; |
|
648 printf("DH-%4lu encrypt_key took %15llu cycles\n", x*8, t2); |
|
649 dh_free(&key); |
|
650 } |
|
651 } |
|
652 |
|
653 #define MAC_SIZE 32 |
|
654 void time_macs(void) |
|
655 { |
|
656 unsigned char *buf, key[16], tag[16]; |
|
657 ulong64 t1, t2; |
|
658 unsigned long x, z; |
|
659 int err, cipher_idx, hash_idx; |
|
660 |
|
661 printf("\nMAC Timings (cycles/byte on %dKB blocks):\n", MAC_SIZE); |
|
662 |
|
663 buf = XMALLOC(MAC_SIZE*1024); |
|
664 if (buf == NULL) { |
|
665 fprintf(stderr, "\n\nout of heap yo\n\n"); |
|
666 exit(EXIT_FAILURE); |
|
667 } |
|
668 |
|
669 cipher_idx = find_cipher("aes"); |
|
670 hash_idx = find_hash("md5"); |
|
671 |
|
672 yarrow_read(buf, MAC_SIZE*1024, &prng); |
|
673 yarrow_read(key, 16, &prng); |
|
674 |
|
675 t2 = -1; |
|
676 for (x = 0; x < 10000; x++) { |
|
677 t_start(); |
|
678 t1 = t_read(); |
|
679 z = 16; |
|
680 if ((err = omac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) { |
|
681 fprintf(stderr, "\n\nomac error... %s\n", error_to_string(err)); |
|
682 exit(EXIT_FAILURE); |
|
683 } |
|
684 t1 = t_read() - t1; |
|
685 if (t1 < t2) t2 = t1; |
|
686 } |
|
687 printf("OMAC-AES\t\t%9llu\n", t2/(MAC_SIZE*1024)); |
|
688 |
|
689 t2 = -1; |
|
690 for (x = 0; x < 10000; x++) { |
|
691 t_start(); |
|
692 t1 = t_read(); |
|
693 z = 16; |
|
694 if ((err = pmac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) { |
|
695 fprintf(stderr, "\n\npmac error... %s\n", error_to_string(err)); |
|
696 exit(EXIT_FAILURE); |
|
697 } |
|
698 t1 = t_read() - t1; |
|
699 if (t1 < t2) t2 = t1; |
|
700 } |
|
701 printf("PMAC-AES\t\t%9llu\n", t2/(MAC_SIZE*1024)); |
|
702 |
|
703 t2 = -1; |
|
704 for (x = 0; x < 10000; x++) { |
|
705 t_start(); |
|
706 t1 = t_read(); |
|
707 z = 16; |
|
708 if ((err = hmac_memory(hash_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) { |
|
709 fprintf(stderr, "\n\nhmac error... %s\n", error_to_string(err)); |
|
710 exit(EXIT_FAILURE); |
|
711 } |
|
712 t1 = t_read() - t1; |
|
713 if (t1 < t2) t2 = t1; |
|
714 } |
|
715 printf("HMAC-MD5\t\t%9llu\n", t2/(MAC_SIZE*1024)); |
|
716 |
|
717 XFREE(buf); |
|
718 } |
|
719 |
3
|
720 int main(void) |
|
721 { |
|
722 reg_algs(); |
|
723 |
|
724 printf("Timings for ciphers and hashes. Times are listed as cycles per byte processed.\n\n"); |
|
725 |
|
726 // init_timer(); |
143
|
727 time_mult(); |
|
728 time_sqr(); |
|
729 time_rsa(); |
|
730 time_dh(); |
|
731 time_ecc(); |
|
732 time_prng(); |
3
|
733 time_cipher(); |
|
734 time_keysched(); |
|
735 time_hash(); |
143
|
736 time_macs(); |
3
|
737 |
|
738 return EXIT_SUCCESS; |
|
739 } |
|
740 |