x86_prof.c 19 KB

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