x86_prof.c 27 KB

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