x86_prof.c 26 KB

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