x86_prof.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. #include <tomcrypt_test.h>
  2. prng_state yarrow_prng;
  3. struct list results[100];
  4. int no_results;
  5. int sorter(const void *a, const void *b)
  6. {
  7. const struct list *A, *B;
  8. A = a;
  9. B = b;
  10. if (A->avg < B->avg) return -1;
  11. if (A->avg > B->avg) return 1;
  12. return 0;
  13. }
  14. void tally_results(int type)
  15. {
  16. int x;
  17. // qsort the results
  18. qsort(results, no_results, sizeof(struct list), &sorter);
  19. printf("\n");
  20. if (type == 0) {
  21. for (x = 0; x < no_results; x++) {
  22. printf("%-20s: Schedule at %6lu\n", cipher_descriptor[results[x].id].name, (unsigned long)results[x].spd1);
  23. }
  24. } else if (type == 1) {
  25. for (x = 0; x < no_results; x++) {
  26. printf
  27. ("%-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);
  28. }
  29. } else {
  30. for (x = 0; x < no_results; x++) {
  31. printf
  32. ("%-20s: Process at %5lu\n", hash_descriptor[results[x].id].name, results[x].spd1 / 1000);
  33. }
  34. }
  35. }
  36. /* RDTSC from Scott Duplichan */
  37. ulong64 rdtsc (void)
  38. {
  39. #if defined __GNUC__
  40. #ifdef INTEL_CC
  41. ulong64 a;
  42. asm ( " rdtsc ":"=A"(a));
  43. return a;
  44. #elif defined(__i386__) || defined(__x86_64__)
  45. ulong64 a;
  46. asm __volatile__ ("rdtsc\nmovl %%eax,(%0)\nmovl %%edx,4(%0)\n"::"r"(&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. static ulong64 timer, skew = 0;
  70. void t_start(void)
  71. {
  72. timer = rdtsc();
  73. }
  74. ulong64 t_read(void)
  75. {
  76. return rdtsc() - timer;
  77. }
  78. void init_timer(void)
  79. {
  80. ulong64 c1, c2, t1, t2, t3;
  81. unsigned long y1;
  82. c1 = c2 = (ulong64)-1;
  83. for (y1 = 0; y1 < TIMES*100; y1++) {
  84. t_start();
  85. t1 = t_read();
  86. t3 = t_read();
  87. t2 = (t_read() - t1)>>1;
  88. c1 = (t1 > c1) ? t1 : c1;
  89. c2 = (t2 > c2) ? t2 : c2;
  90. }
  91. skew = c2 - c1;
  92. printf("Clock Skew: %lu\n", (unsigned long)skew);
  93. }
  94. void reg_algs(void)
  95. {
  96. int err;
  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 KHAZAD
  141. register_cipher (&khazad_desc);
  142. #endif
  143. #ifdef ANUBIS
  144. register_cipher (&anubis_desc);
  145. #endif
  146. #ifdef TIGER
  147. register_hash (&tiger_desc);
  148. #endif
  149. #ifdef MD2
  150. register_hash (&md2_desc);
  151. #endif
  152. #ifdef MD4
  153. register_hash (&md4_desc);
  154. #endif
  155. #ifdef MD5
  156. register_hash (&md5_desc);
  157. #endif
  158. #ifdef SHA1
  159. register_hash (&sha1_desc);
  160. #endif
  161. #ifdef SHA224
  162. register_hash (&sha224_desc);
  163. #endif
  164. #ifdef SHA256
  165. register_hash (&sha256_desc);
  166. #endif
  167. #ifdef SHA384
  168. register_hash (&sha384_desc);
  169. #endif
  170. #ifdef SHA512
  171. register_hash (&sha512_desc);
  172. #endif
  173. #ifdef RIPEMD128
  174. register_hash (&rmd128_desc);
  175. #endif
  176. #ifdef RIPEMD160
  177. register_hash (&rmd160_desc);
  178. #endif
  179. #ifdef WHIRLPOOL
  180. register_hash (&whirlpool_desc);
  181. #endif
  182. #ifdef CHC_HASH
  183. register_hash(&chc_desc);
  184. if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) {
  185. printf("chc_register error: %s\n", error_to_string(err));
  186. exit(EXIT_FAILURE);
  187. }
  188. #endif
  189. #ifndef YARROW
  190. #error This demo requires Yarrow.
  191. #endif
  192. register_prng(&yarrow_desc);
  193. #ifdef FORTUNA
  194. register_prng(&fortuna_desc);
  195. #endif
  196. #ifdef RC4
  197. register_prng(&rc4_desc);
  198. #endif
  199. #ifdef SOBER128
  200. register_prng(&sober128_desc);
  201. #endif
  202. rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL);
  203. }
  204. int time_keysched(void)
  205. {
  206. unsigned long x, y1;
  207. ulong64 t1, c1;
  208. symmetric_key skey;
  209. int kl;
  210. int (*func) (const unsigned char *, int , int , symmetric_key *);
  211. unsigned char key[MAXBLOCKSIZE];
  212. printf ("\n\nKey Schedule Time Trials for the Symmetric Ciphers:\n(Times are cycles per key)\n");
  213. no_results = 0;
  214. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  215. #define DO1(k) func(k, kl, 0, &skey);
  216. func = cipher_descriptor[x].setup;
  217. kl = cipher_descriptor[x].min_key_length;
  218. c1 = (ulong64)-1;
  219. for (y1 = 0; y1 < KTIMES; y1++) {
  220. yarrow_read(key, kl, &yarrow_prng);
  221. t_start();
  222. DO1(key);
  223. t1 = t_read();
  224. c1 = (t1 > c1) ? c1 : t1;
  225. }
  226. t1 = c1 - skew;
  227. results[no_results].spd1 = results[no_results].avg = t1;
  228. results[no_results++].id = x;
  229. printf("."); fflush(stdout);
  230. #undef DO1
  231. }
  232. tally_results(0);
  233. return 0;
  234. }
  235. int time_cipher(void)
  236. {
  237. unsigned long x, y1;
  238. ulong64 t1, t2, c1, c2, a1, a2;
  239. symmetric_ECB ecb;
  240. unsigned char key[MAXBLOCKSIZE], pt[4096];
  241. int err;
  242. printf ("\n\nECB Time Trials for the Symmetric Ciphers:\n");
  243. no_results = 0;
  244. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  245. ecb_start(x, key, cipher_descriptor[x].min_key_length, 0, &ecb);
  246. /* sanity check on cipher */
  247. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  248. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  249. exit(EXIT_FAILURE);
  250. }
  251. #define DO1 ecb_encrypt(pt, pt, sizeof(pt), &ecb);
  252. #define DO2 DO1 DO1
  253. c1 = c2 = (ulong64)-1;
  254. for (y1 = 0; y1 < 100; y1++) {
  255. t_start();
  256. DO1;
  257. t1 = t_read();
  258. DO2;
  259. t2 = t_read();
  260. t2 -= t1;
  261. c1 = (t1 > c1 ? c1 : t1);
  262. c2 = (t2 > c2 ? c2 : t2);
  263. }
  264. a1 = c2 - c1 - skew;
  265. #undef DO1
  266. #undef DO2
  267. #define DO1 ecb_decrypt(pt, pt, sizeof(pt), &ecb);
  268. #define DO2 DO1 DO1
  269. c1 = c2 = (ulong64)-1;
  270. for (y1 = 0; y1 < 100; y1++) {
  271. t_start();
  272. DO1;
  273. t1 = t_read();
  274. DO2;
  275. t2 = t_read();
  276. t2 -= t1;
  277. c1 = (t1 > c1 ? c1 : t1);
  278. c2 = (t2 > c2 ? c2 : t2);
  279. }
  280. a2 = c2 - c1 - skew;
  281. results[no_results].id = x;
  282. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  283. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  284. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  285. ++no_results;
  286. printf("."); fflush(stdout);
  287. #undef DO2
  288. #undef DO1
  289. }
  290. tally_results(1);
  291. return 0;
  292. }
  293. #ifdef CBC
  294. int time_cipher2(void)
  295. {
  296. unsigned long x, y1;
  297. ulong64 t1, t2, c1, c2, a1, a2;
  298. symmetric_CBC cbc;
  299. unsigned char key[MAXBLOCKSIZE], pt[4096];
  300. int err;
  301. printf ("\n\nCBC Time Trials for the Symmetric Ciphers:\n");
  302. no_results = 0;
  303. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  304. cbc_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, &cbc);
  305. /* sanity check on cipher */
  306. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  307. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  308. exit(EXIT_FAILURE);
  309. }
  310. #define DO1 cbc_encrypt(pt, pt, sizeof(pt), &cbc);
  311. #define DO2 DO1 DO1
  312. c1 = c2 = (ulong64)-1;
  313. for (y1 = 0; y1 < 100; y1++) {
  314. t_start();
  315. DO1;
  316. t1 = t_read();
  317. DO2;
  318. t2 = t_read();
  319. t2 -= t1;
  320. c1 = (t1 > c1 ? c1 : t1);
  321. c2 = (t2 > c2 ? c2 : t2);
  322. }
  323. a1 = c2 - c1 - skew;
  324. #undef DO1
  325. #undef DO2
  326. #define DO1 cbc_decrypt(pt, pt, sizeof(pt), &cbc);
  327. #define DO2 DO1 DO1
  328. c1 = c2 = (ulong64)-1;
  329. for (y1 = 0; y1 < 100; y1++) {
  330. t_start();
  331. DO1;
  332. t1 = t_read();
  333. DO2;
  334. t2 = t_read();
  335. t2 -= t1;
  336. c1 = (t1 > c1 ? c1 : t1);
  337. c2 = (t2 > c2 ? c2 : t2);
  338. }
  339. a2 = c2 - c1 - skew;
  340. results[no_results].id = x;
  341. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  342. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  343. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  344. ++no_results;
  345. printf("."); fflush(stdout);
  346. #undef DO2
  347. #undef DO1
  348. }
  349. tally_results(1);
  350. return 0;
  351. }
  352. #else
  353. int time_cipher2(void) { printf("NO CBC\n"); return 0; }
  354. #endif
  355. #ifdef CTR
  356. int time_cipher3(void)
  357. {
  358. unsigned long x, y1;
  359. ulong64 t1, t2, c1, c2, a1, a2;
  360. symmetric_CTR ctr;
  361. unsigned char key[MAXBLOCKSIZE], pt[4096];
  362. int err;
  363. printf ("\n\nCTR Time Trials for the Symmetric Ciphers:\n");
  364. no_results = 0;
  365. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  366. ctr_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, &ctr);
  367. /* sanity check on cipher */
  368. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  369. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  370. exit(EXIT_FAILURE);
  371. }
  372. #define DO1 ctr_encrypt(pt, pt, sizeof(pt), &ctr);
  373. #define DO2 DO1 DO1
  374. c1 = c2 = (ulong64)-1;
  375. for (y1 = 0; y1 < 100; y1++) {
  376. t_start();
  377. DO1;
  378. t1 = t_read();
  379. DO2;
  380. t2 = t_read();
  381. t2 -= t1;
  382. c1 = (t1 > c1 ? c1 : t1);
  383. c2 = (t2 > c2 ? c2 : t2);
  384. }
  385. a1 = c2 - c1 - skew;
  386. #undef DO1
  387. #undef DO2
  388. #define DO1 ctr_decrypt(pt, pt, sizeof(pt), &ctr);
  389. #define DO2 DO1 DO1
  390. c1 = c2 = (ulong64)-1;
  391. for (y1 = 0; y1 < 100; y1++) {
  392. t_start();
  393. DO1;
  394. t1 = t_read();
  395. DO2;
  396. t2 = t_read();
  397. t2 -= t1;
  398. c1 = (t1 > c1 ? c1 : t1);
  399. c2 = (t2 > c2 ? c2 : t2);
  400. }
  401. a2 = c2 - c1 - skew;
  402. results[no_results].id = x;
  403. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  404. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  405. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  406. ++no_results;
  407. printf("."); fflush(stdout);
  408. #undef DO2
  409. #undef DO1
  410. }
  411. tally_results(1);
  412. return 0;
  413. }
  414. #else
  415. int time_cipher3(void) { printf("NO CTR\n"); return 0; }
  416. #endif
  417. int time_hash(void)
  418. {
  419. unsigned long x, y1, len;
  420. ulong64 t1, t2, c1, c2;
  421. hash_state md;
  422. int (*func)(hash_state *, const unsigned char *, unsigned long), err;
  423. unsigned char pt[MAXBLOCKSIZE];
  424. printf ("\n\nHASH Time Trials for:\n");
  425. no_results = 0;
  426. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  427. /* sanity check on hash */
  428. if ((err = hash_descriptor[x].test()) != CRYPT_OK) {
  429. fprintf(stderr, "\n\nERROR: Hash %s failed self-test %s\n", hash_descriptor[x].name, error_to_string(err));
  430. exit(EXIT_FAILURE);
  431. }
  432. hash_descriptor[x].init(&md);
  433. #define DO1 func(&md,pt,len);
  434. #define DO2 DO1 DO1
  435. func = hash_descriptor[x].process;
  436. len = hash_descriptor[x].blocksize;
  437. c1 = c2 = (ulong64)-1;
  438. for (y1 = 0; y1 < TIMES; y1++) {
  439. t_start();
  440. DO1;
  441. t1 = t_read();
  442. DO2;
  443. t2 = t_read() - t1;
  444. c1 = (t1 > c1) ? c1 : t1;
  445. c2 = (t2 > c2) ? c2 : t2;
  446. }
  447. t1 = c2 - c1 - skew;
  448. t1 = ((t1 * CONST64(1000))) / ((ulong64)hash_descriptor[x].blocksize);
  449. results[no_results].id = x;
  450. results[no_results].spd1 = results[no_results].avg = t1;
  451. ++no_results;
  452. printf("."); fflush(stdout);
  453. #undef DO2
  454. #undef DO1
  455. }
  456. tally_results(2);
  457. return 0;
  458. }
  459. #ifdef MPI
  460. void time_mult(void)
  461. {
  462. ulong64 t1, t2;
  463. unsigned long x, y;
  464. mp_int a, b, c;
  465. printf("Timing Multiplying:\n");
  466. mp_init_multi(&a,&b,&c,NULL);
  467. for (x = 128/DIGIT_BIT; x <= 1536/DIGIT_BIT; x += 128/DIGIT_BIT) {
  468. mp_rand(&a, x);
  469. mp_rand(&b, x);
  470. #define DO1 mp_mul(&a, &b, &c);
  471. #define DO2 DO1; DO1;
  472. t2 = -1;
  473. for (y = 0; y < TIMES; y++) {
  474. t_start();
  475. t1 = t_read();
  476. DO2;
  477. t1 = (t_read() - t1)>>1;
  478. if (t1 < t2) t2 = t1;
  479. }
  480. printf("%4lu bits: %9llu cycles\n", x*DIGIT_BIT, t2);
  481. }
  482. mp_clear_multi(&a,&b,&c,NULL);
  483. #undef DO1
  484. #undef DO2
  485. }
  486. void time_sqr(void)
  487. {
  488. ulong64 t1, t2;
  489. unsigned long x, y;
  490. mp_int a, b;
  491. printf("Timing Squaring:\n");
  492. mp_init_multi(&a,&b,NULL);
  493. for (x = 128/DIGIT_BIT; x <= 1536/DIGIT_BIT; x += 128/DIGIT_BIT) {
  494. mp_rand(&a, x);
  495. #define DO1 mp_sqr(&a, &b);
  496. #define DO2 DO1; DO1;
  497. t2 = -1;
  498. for (y = 0; y < TIMES; y++) {
  499. t_start();
  500. t1 = t_read();
  501. DO2;
  502. t1 = (t_read() - t1)>>1;
  503. if (t1 < t2) t2 = t1;
  504. }
  505. printf("%4lu bits: %9llu cycles\n", x*DIGIT_BIT, t2);
  506. }
  507. mp_clear_multi(&a,&b,NULL);
  508. #undef DO1
  509. #undef DO2
  510. }
  511. #else
  512. void time_mult(void) { printf("NO MULT\n"); }
  513. void time_sqr(void) { printf("NO SQR\n"); }
  514. #endif
  515. void time_prng(void)
  516. {
  517. ulong64 t1, t2;
  518. unsigned char buf[4096];
  519. prng_state tprng;
  520. unsigned long x, y;
  521. int err;
  522. printf("Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n");
  523. for (x = 0; prng_descriptor[x].name != NULL; x++) {
  524. /* sanity check on prng */
  525. if ((err = prng_descriptor[x].test()) != CRYPT_OK) {
  526. fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor[x].name, error_to_string(err));
  527. exit(EXIT_FAILURE);
  528. }
  529. prng_descriptor[x].start(&tprng);
  530. zeromem(buf, 256);
  531. prng_descriptor[x].add_entropy(buf, 256, &tprng);
  532. prng_descriptor[x].ready(&tprng);
  533. t2 = -1;
  534. #define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { printf("\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
  535. #define DO2 DO1 DO1
  536. for (y = 0; y < 10000; y++) {
  537. t_start();
  538. t1 = t_read();
  539. DO2;
  540. t1 = (t_read() - t1)>>1;
  541. if (t1 < t2) t2 = t1;
  542. }
  543. printf("%20s: %5llu ", prng_descriptor[x].name, t2>>12);
  544. #undef DO2
  545. #undef DO1
  546. #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);
  547. #define DO2 DO1 DO1
  548. for (y = 0; y < 10000; y++) {
  549. t_start();
  550. t1 = t_read();
  551. DO2;
  552. t1 = (t_read() - t1)>>1;
  553. if (t1 < t2) t2 = t1;
  554. }
  555. printf("%5llu\n", t2);
  556. #undef DO2
  557. #undef DO1
  558. }
  559. }
  560. #ifdef MRSA
  561. /* time various RSA operations */
  562. void time_rsa(void)
  563. {
  564. rsa_key key;
  565. ulong64 t1, t2;
  566. unsigned char buf[2][4096];
  567. unsigned long x, y, z, zzz;
  568. int err, zz;
  569. for (x = 1024; x <= 2048; x += 512) {
  570. t2 = 0;
  571. for (y = 0; y < 16; y++) {
  572. t_start();
  573. t1 = t_read();
  574. if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) {
  575. 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));
  576. exit(EXIT_FAILURE);
  577. }
  578. t1 = t_read() - t1;
  579. t2 += t1;
  580. if (y < 15) {
  581. rsa_free(&key);
  582. }
  583. }
  584. t2 >>= 4;
  585. printf("RSA-%lu make_key took %15llu cycles\n", x, t2);
  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 = rsa_encrypt_key(buf[0], 32, buf[1], &z, "testprog", 8, &yarrow_prng,
  592. find_prng("yarrow"), find_hash("sha1"),
  593. &key)) != CRYPT_OK) {
  594. 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));
  595. exit(EXIT_FAILURE);
  596. }
  597. t1 = t_read() - t1;
  598. t2 += t1;
  599. }
  600. t2 >>= 4;
  601. printf("RSA-%lu encrypt_key took %15llu cycles\n", x, t2);
  602. t2 = 0;
  603. for (y = 0; y < 16; y++) {
  604. t_start();
  605. t1 = t_read();
  606. zzz = sizeof(buf[0]);
  607. if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, "testprog", 8, find_hash("sha1"),
  608. &zz, &key)) != CRYPT_OK) {
  609. 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));
  610. exit(EXIT_FAILURE);
  611. }
  612. t1 = t_read() - t1;
  613. t2 += t1;
  614. }
  615. t2 >>= 4;
  616. printf("RSA-%lu decrypt_key took %15llu cycles\n", x, t2);
  617. rsa_free(&key);
  618. }
  619. }
  620. #else
  621. void time_rsa(void) { printf("NO RSA\n"); }
  622. #endif
  623. #ifdef MECC
  624. /* time various ECC operations */
  625. void time_ecc(void)
  626. {
  627. ecc_key key;
  628. ulong64 t1, t2;
  629. unsigned char buf[2][4096];
  630. unsigned long i, x, y, z;
  631. int err;
  632. static unsigned long sizes[] = {160/8, 256/8, 521/8, 100000};
  633. for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
  634. t2 = 0;
  635. for (y = 0; y < 16; y++) {
  636. t_start();
  637. t1 = t_read();
  638. if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
  639. 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));
  640. exit(EXIT_FAILURE);
  641. }
  642. t1 = t_read() - t1;
  643. t2 += t1;
  644. if (y < 15) {
  645. ecc_free(&key);
  646. }
  647. }
  648. t2 >>= 4;
  649. printf("ECC-%lu make_key took %15llu cycles\n", x*8, t2);
  650. t2 = 0;
  651. for (y = 0; y < 16; y++) {
  652. t_start();
  653. t1 = t_read();
  654. z = sizeof(buf[1]);
  655. if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
  656. &key)) != CRYPT_OK) {
  657. 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));
  658. exit(EXIT_FAILURE);
  659. }
  660. t1 = t_read() - t1;
  661. t2 += t1;
  662. }
  663. t2 >>= 4;
  664. printf("ECC-%lu encrypt_key took %15llu cycles\n", x*8, t2);
  665. ecc_free(&key);
  666. }
  667. }
  668. #else
  669. void time_ecc(void) { printf("NO ECC\n"); }
  670. #endif
  671. #ifdef MDH
  672. /* time various DH operations */
  673. void time_dh(void)
  674. {
  675. dh_key key;
  676. ulong64 t1, t2;
  677. unsigned char buf[2][4096];
  678. unsigned long i, x, y, z;
  679. int err;
  680. static unsigned long sizes[] = {768/8, 1024/8, 1536/8, 2048/8, 3072/8, 4096/8, 100000};
  681. for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
  682. t2 = 0;
  683. for (y = 0; y < 16; y++) {
  684. t_start();
  685. t1 = t_read();
  686. if ((err = dh_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
  687. 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));
  688. exit(EXIT_FAILURE);
  689. }
  690. t1 = t_read() - t1;
  691. t2 += t1;
  692. if (y < 15) {
  693. dh_free(&key);
  694. }
  695. }
  696. t2 >>= 4;
  697. printf("DH-%4lu make_key took %15llu cycles\n", x*8, t2);
  698. t2 = 0;
  699. for (y = 0; y < 16; y++) {
  700. t_start();
  701. t1 = t_read();
  702. z = sizeof(buf[1]);
  703. if ((err = dh_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
  704. &key)) != CRYPT_OK) {
  705. 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));
  706. exit(EXIT_FAILURE);
  707. }
  708. t1 = t_read() - t1;
  709. t2 += t1;
  710. }
  711. t2 >>= 4;
  712. printf("DH-%4lu encrypt_key took %15llu cycles\n", x*8, t2);
  713. dh_free(&key);
  714. }
  715. }
  716. #else
  717. void time_dh(void) { printf("NO DH\n"); }
  718. #endif
  719. void time_macs_(unsigned long MAC_SIZE)
  720. {
  721. unsigned char *buf, key[16], tag[16];
  722. ulong64 t1, t2;
  723. unsigned long x, z;
  724. int err, cipher_idx, hash_idx;
  725. printf("\nMAC Timings (cycles/byte on %dKB blocks):\n", MAC_SIZE);
  726. buf = XMALLOC(MAC_SIZE*1024);
  727. if (buf == NULL) {
  728. fprintf(stderr, "\n\nout of heap yo\n\n");
  729. exit(EXIT_FAILURE);
  730. }
  731. cipher_idx = find_cipher("aes");
  732. hash_idx = find_hash("md5");
  733. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  734. yarrow_read(key, 16, &yarrow_prng);
  735. #ifdef OMAC
  736. t2 = -1;
  737. for (x = 0; x < 10000; x++) {
  738. t_start();
  739. t1 = t_read();
  740. z = 16;
  741. if ((err = omac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  742. fprintf(stderr, "\n\nomac error... %s\n", error_to_string(err));
  743. exit(EXIT_FAILURE);
  744. }
  745. t1 = t_read() - t1;
  746. if (t1 < t2) t2 = t1;
  747. }
  748. printf("OMAC-AES\t\t%9llu\n", t2/(MAC_SIZE*1024));
  749. #endif
  750. #ifdef PMAC
  751. t2 = -1;
  752. for (x = 0; x < 10000; x++) {
  753. t_start();
  754. t1 = t_read();
  755. z = 16;
  756. if ((err = pmac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  757. fprintf(stderr, "\n\npmac error... %s\n", error_to_string(err));
  758. exit(EXIT_FAILURE);
  759. }
  760. t1 = t_read() - t1;
  761. if (t1 < t2) t2 = t1;
  762. }
  763. printf("PMAC-AES\t\t%9llu\n", t2/(MAC_SIZE*1024));
  764. #endif
  765. #ifdef PELICAN
  766. t2 = -1;
  767. for (x = 0; x < 10000; x++) {
  768. t_start();
  769. t1 = t_read();
  770. z = 16;
  771. if ((err = pelican_memory(key, 16, buf, MAC_SIZE*1024, tag)) != CRYPT_OK) {
  772. fprintf(stderr, "\n\npelican error... %s\n", error_to_string(err));
  773. exit(EXIT_FAILURE);
  774. }
  775. t1 = t_read() - t1;
  776. if (t1 < t2) t2 = t1;
  777. }
  778. printf("PELICAN \t\t%9llu\n", t2/(MAC_SIZE*1024));
  779. #endif
  780. #ifdef HMAC
  781. t2 = -1;
  782. for (x = 0; x < 10000; x++) {
  783. t_start();
  784. t1 = t_read();
  785. z = 16;
  786. if ((err = hmac_memory(hash_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  787. fprintf(stderr, "\n\nhmac error... %s\n", error_to_string(err));
  788. exit(EXIT_FAILURE);
  789. }
  790. t1 = t_read() - t1;
  791. if (t1 < t2) t2 = t1;
  792. }
  793. printf("HMAC-MD5\t\t%9llu\n", t2/(MAC_SIZE*1024));
  794. #endif
  795. XFREE(buf);
  796. }
  797. void time_macs(void)
  798. {
  799. time_macs_(1);
  800. time_macs_(4);
  801. time_macs_(32);
  802. }
  803. void time_encmacs_(unsigned long MAC_SIZE)
  804. {
  805. unsigned char *buf, IV[16], key[16], tag[16];
  806. ulong64 t1, t2;
  807. unsigned long x, z;
  808. int err, cipher_idx;
  809. printf("\nENC+MAC Timings (zero byte AAD, 16 byte IV, cycles/byte on %dKB blocks):\n", MAC_SIZE);
  810. buf = XMALLOC(MAC_SIZE*1024);
  811. if (buf == NULL) {
  812. fprintf(stderr, "\n\nout of heap yo\n\n");
  813. exit(EXIT_FAILURE);
  814. }
  815. cipher_idx = find_cipher("aes");
  816. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  817. yarrow_read(key, 16, &yarrow_prng);
  818. yarrow_read(IV, 16, &yarrow_prng);
  819. #ifdef EAX_MODE
  820. t2 = -1;
  821. for (x = 0; x < 10000; x++) {
  822. t_start();
  823. t1 = t_read();
  824. z = 16;
  825. if ((err = eax_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  826. fprintf(stderr, "\nEAX error... %s\n", error_to_string(err));
  827. exit(EXIT_FAILURE);
  828. }
  829. t1 = t_read() - t1;
  830. if (t1 < t2) t2 = t1;
  831. }
  832. printf("EAX \t\t%9llu\n", t2/(MAC_SIZE*1024));
  833. #endif
  834. #ifdef OCB_MODE
  835. t2 = -1;
  836. for (x = 0; x < 10000; x++) {
  837. t_start();
  838. t1 = t_read();
  839. z = 16;
  840. if ((err = ocb_encrypt_authenticate_memory(cipher_idx, key, 16, IV, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  841. fprintf(stderr, "\nOCB error... %s\n", error_to_string(err));
  842. exit(EXIT_FAILURE);
  843. }
  844. t1 = t_read() - t1;
  845. if (t1 < t2) t2 = t1;
  846. }
  847. printf("OCB \t\t%9llu\n", t2/(MAC_SIZE*1024));
  848. #endif
  849. #ifdef CCM_MODE
  850. t2 = -1;
  851. for (x = 0; x < 10000; x++) {
  852. t_start();
  853. t1 = t_read();
  854. z = 16;
  855. if ((err = ccm_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  856. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  857. exit(EXIT_FAILURE);
  858. }
  859. t1 = t_read() - t1;
  860. if (t1 < t2) t2 = t1;
  861. }
  862. printf("CCM \t\t%9llu\n", t2/(MAC_SIZE*1024));
  863. #endif
  864. #ifdef GCM_MODE
  865. t2 = -1;
  866. for (x = 0; x < 100; x++) {
  867. t_start();
  868. t1 = t_read();
  869. z = 16;
  870. if ((err = gcm_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, GCM_ENCRYPT)) != CRYPT_OK) {
  871. fprintf(stderr, "\nGCM error... %s\n", error_to_string(err));
  872. exit(EXIT_FAILURE);
  873. }
  874. t1 = t_read() - t1;
  875. if (t1 < t2) t2 = t1;
  876. }
  877. printf("GCM (no-precomp)\t%9llu\n", t2/(MAC_SIZE*1024));
  878. {
  879. gcm_state gcm;
  880. if ((err = gcm_init(&gcm, cipher_idx, key, 16)) != CRYPT_OK) { printf("gcm_init: %s\n", error_to_string(err)); exit(EXIT_FAILURE); }
  881. t2 = -1;
  882. for (x = 0; x < 10000; x++) {
  883. t_start();
  884. t1 = t_read();
  885. z = 16;
  886. if ((err = gcm_reset(&gcm)) != CRYPT_OK) {
  887. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  888. exit(EXIT_FAILURE);
  889. }
  890. if ((err = gcm_add_iv(&gcm, IV, 16)) != CRYPT_OK) {
  891. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  892. exit(EXIT_FAILURE);
  893. }
  894. if ((err = gcm_add_aad(&gcm, NULL, 0)) != CRYPT_OK) {
  895. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  896. exit(EXIT_FAILURE);
  897. }
  898. if ((err = gcm_process(&gcm, buf, MAC_SIZE*1024, buf, GCM_ENCRYPT)) != CRYPT_OK) {
  899. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  900. exit(EXIT_FAILURE);
  901. }
  902. if ((err = gcm_done(&gcm, tag, &z)) != CRYPT_OK) {
  903. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  904. exit(EXIT_FAILURE);
  905. }
  906. t1 = t_read() - t1;
  907. if (t1 < t2) t2 = t1;
  908. }
  909. printf("GCM (precomp)\t%9llu\n", t2/(MAC_SIZE*1024));
  910. }
  911. #endif
  912. }
  913. void time_encmacs(void)
  914. {
  915. time_encmacs_(1);
  916. time_encmacs_(4);
  917. time_encmacs_(32);
  918. }