x86_prof.c 18 KB

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