x86_prof.c 18 KB

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