x86_prof.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. #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
  59. __asm rdtsc
  60. #elif defined _M_AMD64
  61. return __rdtsc ();
  62. #elif defined _M_IA64
  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. printf("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. printf("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. printf ("\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. printf("."); 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. printf ("\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. printf("."); 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. printf ("\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. printf("."); fflush(stdout);
  348. #undef DO2
  349. #undef DO1
  350. }
  351. tally_results(1);
  352. return 0;
  353. }
  354. #else
  355. int time_cipher2(void) { printf("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. printf ("\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);
  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. printf("."); fflush(stdout);
  410. #undef DO2
  411. #undef DO1
  412. }
  413. tally_results(1);
  414. return 0;
  415. }
  416. #else
  417. int time_cipher3(void) { printf("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. printf ("\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. printf("."); 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. printf("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. printf("%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. printf("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. printf("%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) { printf("NO MULT\n"); }
  515. void time_sqr(void) { printf("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. printf("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) { printf("\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. printf("%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. printf("%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. printf("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. printf("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. printf("RSA-%lu decrypt_key took %15llu cycles\n", x, t2);
  619. rsa_free(&key);
  620. }
  621. }
  622. #else
  623. void time_rsa(void) { printf("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[] = {160/8, 256/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. printf("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. printf("ECC-%lu encrypt_key took %15llu cycles\n", x*8, t2);
  667. ecc_free(&key);
  668. }
  669. }
  670. #else
  671. void time_ecc(void) { printf("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. printf("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. printf("DH-%4lu encrypt_key took %15llu cycles\n", x*8, t2);
  715. dh_free(&key);
  716. }
  717. }
  718. #else
  719. void time_dh(void) { printf("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. printf("\nMAC Timings (cycles/byte on %dKB 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. printf("OMAC-AES\t\t%9llu\n", t2/(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. printf("PMAC-AES\t\t%9llu\n", t2/(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. printf("PELICAN \t\t%9llu\n", t2/(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. printf("HMAC-MD5\t\t%9llu\n", t2/(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. printf("\nENC+MAC Timings (zero byte AAD, 16 byte IV, cycles/byte on %dKB 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. printf("EAX \t\t%9llu\n", t2/(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. printf("OCB \t\t%9llu\n", t2/(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. printf("CCM \t\t%9llu\n", t2/(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. printf("GCM (no-precomp)\t%9llu\n", t2/(MAC_SIZE*1024));
  880. {
  881. gcm_state gcm;
  882. if ((err = gcm_init(&gcm, cipher_idx, key, 16)) != CRYPT_OK) { printf("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. printf("GCM (precomp)\t%9llu\n", t2/(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. }