x86_prof.c 29 KB

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