x86_prof.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
  219. fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
  220. exit(EXIT_FAILURE);
  221. }
  222. }
  223. int time_keysched(void)
  224. {
  225. unsigned long x, y1;
  226. ulong64 t1, c1;
  227. symmetric_key skey;
  228. int kl;
  229. int (*func) (const unsigned char *, int , int , symmetric_key *);
  230. unsigned char key[MAXBLOCKSIZE];
  231. fprintf(stderr, "\n\nKey Schedule Time Trials for the Symmetric Ciphers:\n(Times are cycles per key)\n");
  232. no_results = 0;
  233. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  234. #define DO1(k) func(k, kl, 0, &skey);
  235. func = cipher_descriptor[x].setup;
  236. kl = cipher_descriptor[x].min_key_length;
  237. c1 = (ulong64)-1;
  238. for (y1 = 0; y1 < KTIMES; y1++) {
  239. yarrow_read(key, kl, &yarrow_prng);
  240. t_start();
  241. DO1(key);
  242. t1 = t_read();
  243. c1 = (t1 > c1) ? c1 : t1;
  244. }
  245. t1 = c1 - skew;
  246. results[no_results].spd1 = results[no_results].avg = t1;
  247. results[no_results++].id = x;
  248. fprintf(stderr, "."); fflush(stdout);
  249. #undef DO1
  250. }
  251. tally_results(0);
  252. return 0;
  253. }
  254. int time_cipher(void)
  255. {
  256. unsigned long x, y1;
  257. ulong64 t1, t2, c1, c2, a1, a2;
  258. symmetric_ECB ecb;
  259. unsigned char key[MAXBLOCKSIZE], pt[4096];
  260. int err;
  261. fprintf(stderr, "\n\nECB Time Trials for the Symmetric Ciphers:\n");
  262. no_results = 0;
  263. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  264. ecb_start(x, key, cipher_descriptor[x].min_key_length, 0, &ecb);
  265. /* sanity check on cipher */
  266. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  267. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  268. exit(EXIT_FAILURE);
  269. }
  270. #define DO1 ecb_encrypt(pt, pt, sizeof(pt), &ecb);
  271. #define DO2 DO1 DO1
  272. c1 = c2 = (ulong64)-1;
  273. for (y1 = 0; y1 < 100; y1++) {
  274. t_start();
  275. DO1;
  276. t1 = t_read();
  277. DO2;
  278. t2 = t_read();
  279. t2 -= t1;
  280. c1 = (t1 > c1 ? c1 : t1);
  281. c2 = (t2 > c2 ? c2 : t2);
  282. }
  283. a1 = c2 - c1 - skew;
  284. #undef DO1
  285. #undef DO2
  286. #define DO1 ecb_decrypt(pt, pt, sizeof(pt), &ecb);
  287. #define DO2 DO1 DO1
  288. c1 = c2 = (ulong64)-1;
  289. for (y1 = 0; y1 < 100; y1++) {
  290. t_start();
  291. DO1;
  292. t1 = t_read();
  293. DO2;
  294. t2 = t_read();
  295. t2 -= t1;
  296. c1 = (t1 > c1 ? c1 : t1);
  297. c2 = (t2 > c2 ? c2 : t2);
  298. }
  299. a2 = c2 - c1 - skew;
  300. results[no_results].id = x;
  301. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  302. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  303. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  304. ++no_results;
  305. fprintf(stderr, "."); fflush(stdout);
  306. #undef DO2
  307. #undef DO1
  308. }
  309. tally_results(1);
  310. return 0;
  311. }
  312. #ifdef CBC
  313. int time_cipher2(void)
  314. {
  315. unsigned long x, y1;
  316. ulong64 t1, t2, c1, c2, a1, a2;
  317. symmetric_CBC cbc;
  318. unsigned char key[MAXBLOCKSIZE], pt[4096];
  319. int err;
  320. fprintf(stderr, "\n\nCBC Time Trials for the Symmetric Ciphers:\n");
  321. no_results = 0;
  322. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  323. cbc_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, &cbc);
  324. /* sanity check on cipher */
  325. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  326. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  327. exit(EXIT_FAILURE);
  328. }
  329. #define DO1 cbc_encrypt(pt, pt, sizeof(pt), &cbc);
  330. #define DO2 DO1 DO1
  331. c1 = c2 = (ulong64)-1;
  332. for (y1 = 0; y1 < 100; y1++) {
  333. t_start();
  334. DO1;
  335. t1 = t_read();
  336. DO2;
  337. t2 = t_read();
  338. t2 -= t1;
  339. c1 = (t1 > c1 ? c1 : t1);
  340. c2 = (t2 > c2 ? c2 : t2);
  341. }
  342. a1 = c2 - c1 - skew;
  343. #undef DO1
  344. #undef DO2
  345. #define DO1 cbc_decrypt(pt, pt, sizeof(pt), &cbc);
  346. #define DO2 DO1 DO1
  347. c1 = c2 = (ulong64)-1;
  348. for (y1 = 0; y1 < 100; y1++) {
  349. t_start();
  350. DO1;
  351. t1 = t_read();
  352. DO2;
  353. t2 = t_read();
  354. t2 -= t1;
  355. c1 = (t1 > c1 ? c1 : t1);
  356. c2 = (t2 > c2 ? c2 : t2);
  357. }
  358. a2 = c2 - c1 - skew;
  359. results[no_results].id = x;
  360. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  361. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  362. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  363. ++no_results;
  364. fprintf(stderr, "."); fflush(stdout);
  365. #undef DO2
  366. #undef DO1
  367. }
  368. tally_results(1);
  369. return 0;
  370. }
  371. #else
  372. int time_cipher2(void) { fprintf(stderr, "NO CBC\n"); return 0; }
  373. #endif
  374. #ifdef CTR
  375. int time_cipher3(void)
  376. {
  377. unsigned long x, y1;
  378. ulong64 t1, t2, c1, c2, a1, a2;
  379. symmetric_CTR ctr;
  380. unsigned char key[MAXBLOCKSIZE], pt[4096];
  381. int err;
  382. fprintf(stderr, "\n\nCTR Time Trials for the Symmetric Ciphers:\n");
  383. no_results = 0;
  384. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  385. ctr_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr);
  386. /* sanity check on cipher */
  387. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  388. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  389. exit(EXIT_FAILURE);
  390. }
  391. #define DO1 ctr_encrypt(pt, pt, sizeof(pt), &ctr);
  392. #define DO2 DO1 DO1
  393. c1 = c2 = (ulong64)-1;
  394. for (y1 = 0; y1 < 100; y1++) {
  395. t_start();
  396. DO1;
  397. t1 = t_read();
  398. DO2;
  399. t2 = t_read();
  400. t2 -= t1;
  401. c1 = (t1 > c1 ? c1 : t1);
  402. c2 = (t2 > c2 ? c2 : t2);
  403. }
  404. a1 = c2 - c1 - skew;
  405. #undef DO1
  406. #undef DO2
  407. #define DO1 ctr_decrypt(pt, pt, sizeof(pt), &ctr);
  408. #define DO2 DO1 DO1
  409. c1 = c2 = (ulong64)-1;
  410. for (y1 = 0; y1 < 100; y1++) {
  411. t_start();
  412. DO1;
  413. t1 = t_read();
  414. DO2;
  415. t2 = t_read();
  416. t2 -= t1;
  417. c1 = (t1 > c1 ? c1 : t1);
  418. c2 = (t2 > c2 ? c2 : t2);
  419. }
  420. a2 = c2 - c1 - skew;
  421. results[no_results].id = x;
  422. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  423. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  424. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  425. ++no_results;
  426. fprintf(stderr, "."); fflush(stdout);
  427. #undef DO2
  428. #undef DO1
  429. }
  430. tally_results(1);
  431. return 0;
  432. }
  433. #else
  434. int time_cipher3(void) { fprintf(stderr, "NO CTR\n"); return 0; }
  435. #endif
  436. int time_hash(void)
  437. {
  438. unsigned long x, y1, len;
  439. ulong64 t1, t2, c1, c2;
  440. hash_state md;
  441. int (*func)(hash_state *, const unsigned char *, unsigned long), err;
  442. unsigned char pt[MAXBLOCKSIZE];
  443. fprintf(stderr, "\n\nHASH Time Trials for:\n");
  444. no_results = 0;
  445. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  446. /* sanity check on hash */
  447. if ((err = hash_descriptor[x].test()) != CRYPT_OK) {
  448. fprintf(stderr, "\n\nERROR: Hash %s failed self-test %s\n", hash_descriptor[x].name, error_to_string(err));
  449. exit(EXIT_FAILURE);
  450. }
  451. hash_descriptor[x].init(&md);
  452. #define DO1 func(&md,pt,len);
  453. #define DO2 DO1 DO1
  454. func = hash_descriptor[x].process;
  455. len = hash_descriptor[x].blocksize;
  456. c1 = c2 = (ulong64)-1;
  457. for (y1 = 0; y1 < TIMES; y1++) {
  458. t_start();
  459. DO1;
  460. t1 = t_read();
  461. DO2;
  462. t2 = t_read() - t1;
  463. c1 = (t1 > c1) ? c1 : t1;
  464. c2 = (t2 > c2) ? c2 : t2;
  465. }
  466. t1 = c2 - c1 - skew;
  467. t1 = ((t1 * CONST64(1000))) / ((ulong64)hash_descriptor[x].blocksize);
  468. results[no_results].id = x;
  469. results[no_results].spd1 = results[no_results].avg = t1;
  470. ++no_results;
  471. fprintf(stderr, "."); fflush(stdout);
  472. #undef DO2
  473. #undef DO1
  474. }
  475. tally_results(2);
  476. return 0;
  477. }
  478. #undef MPI
  479. //#warning you need an mp_rand!!!
  480. #ifdef MPI
  481. void time_mult(void)
  482. {
  483. ulong64 t1, t2;
  484. unsigned long x, y;
  485. void *a, *b, *c;
  486. fprintf(stderr, "Timing Multiplying:\n");
  487. mp_init_multi(&a,&b,&c,NULL);
  488. for (x = 128/DIGIT_BIT; x <= 1536/DIGIT_BIT; x += 128/DIGIT_BIT) {
  489. mp_rand(&a, x);
  490. mp_rand(&b, x);
  491. #define DO1 mp_mul(&a, &b, &c);
  492. #define DO2 DO1; DO1;
  493. t2 = -1;
  494. for (y = 0; y < TIMES; y++) {
  495. t_start();
  496. t1 = t_read();
  497. DO2;
  498. t1 = (t_read() - t1)>>1;
  499. if (t1 < t2) t2 = t1;
  500. }
  501. fprintf(stderr, "%4lu bits: %9llu cycles\n", x*DIGIT_BIT, t2);
  502. }
  503. mp_clear_multi(&a,&b,&c,NULL);
  504. #undef DO1
  505. #undef DO2
  506. }
  507. void time_sqr(void)
  508. {
  509. ulong64 t1, t2;
  510. unsigned long x, y;
  511. mp_int a, b;
  512. fprintf(stderr, "Timing Squaring:\n");
  513. mp_init_multi(&a,&b,NULL);
  514. for (x = 128/DIGIT_BIT; x <= 1536/DIGIT_BIT; x += 128/DIGIT_BIT) {
  515. mp_rand(&a, x);
  516. #define DO1 mp_sqr(&a, &b);
  517. #define DO2 DO1; DO1;
  518. t2 = -1;
  519. for (y = 0; y < TIMES; y++) {
  520. t_start();
  521. t1 = t_read();
  522. DO2;
  523. t1 = (t_read() - t1)>>1;
  524. if (t1 < t2) t2 = t1;
  525. }
  526. fprintf(stderr, "%4lu bits: %9llu cycles\n", x*DIGIT_BIT, t2);
  527. }
  528. mp_clear_multi(&a,&b,NULL);
  529. #undef DO1
  530. #undef DO2
  531. }
  532. #else
  533. void time_mult(void) { fprintf(stderr, "NO MULT\n"); }
  534. void time_sqr(void) { fprintf(stderr, "NO SQR\n"); }
  535. #endif
  536. void time_prng(void)
  537. {
  538. ulong64 t1, t2;
  539. unsigned char buf[4096];
  540. prng_state tprng;
  541. unsigned long x, y;
  542. int err;
  543. fprintf(stderr, "Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n");
  544. for (x = 0; prng_descriptor[x].name != NULL; x++) {
  545. /* sanity check on prng */
  546. if ((err = prng_descriptor[x].test()) != CRYPT_OK) {
  547. fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor[x].name, error_to_string(err));
  548. exit(EXIT_FAILURE);
  549. }
  550. prng_descriptor[x].start(&tprng);
  551. zeromem(buf, 256);
  552. prng_descriptor[x].add_entropy(buf, 256, &tprng);
  553. prng_descriptor[x].ready(&tprng);
  554. t2 = -1;
  555. #define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { fprintf(stderr, "\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
  556. #define DO2 DO1 DO1
  557. for (y = 0; y < 10000; y++) {
  558. t_start();
  559. t1 = t_read();
  560. DO2;
  561. t1 = (t_read() - t1)>>1;
  562. if (t1 < t2) t2 = t1;
  563. }
  564. fprintf(stderr, "%20s: %5llu ", prng_descriptor[x].name, t2>>12);
  565. #undef DO2
  566. #undef DO1
  567. #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);
  568. #define DO2 DO1 DO1
  569. for (y = 0; y < 10000; y++) {
  570. t_start();
  571. t1 = t_read();
  572. DO2;
  573. t1 = (t_read() - t1)>>1;
  574. if (t1 < t2) t2 = t1;
  575. }
  576. fprintf(stderr, "%5llu\n", t2);
  577. #undef DO2
  578. #undef DO1
  579. }
  580. }
  581. #ifdef MRSA
  582. /* time various RSA operations */
  583. void time_rsa(void)
  584. {
  585. rsa_key key;
  586. ulong64 t1, t2;
  587. unsigned char buf[2][4096];
  588. unsigned long x, y, z, zzz;
  589. int err, zz;
  590. for (x = 1024; x <= 2048; x += 256) {
  591. t2 = 0;
  592. for (y = 0; y < 4; y++) {
  593. t_start();
  594. t1 = t_read();
  595. if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) {
  596. 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));
  597. exit(EXIT_FAILURE);
  598. }
  599. t1 = t_read() - t1;
  600. t2 += t1;
  601. if (y < 3) {
  602. rsa_free(&key);
  603. }
  604. }
  605. t2 >>= 2;
  606. fprintf(stderr, "RSA-%lu make_key took %15llu cycles\n", x, t2);
  607. t2 = 0;
  608. for (y = 0; y < 16; y++) {
  609. t_start();
  610. t1 = t_read();
  611. z = sizeof(buf[1]);
  612. if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, "testprog", 8, &yarrow_prng,
  613. find_prng("yarrow"), find_hash("sha1"),
  614. &key)) != CRYPT_OK) {
  615. 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));
  616. exit(EXIT_FAILURE);
  617. }
  618. t1 = t_read() - t1;
  619. t2 += t1;
  620. }
  621. t2 >>= 4;
  622. fprintf(stderr, "RSA-%lu encrypt_key took %15llu cycles\n", x, t2);
  623. t2 = 0;
  624. for (y = 0; y < 2048; y++) {
  625. t_start();
  626. t1 = t_read();
  627. zzz = sizeof(buf[0]);
  628. if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, "testprog", 8, find_hash("sha1"),
  629. &zz, &key)) != CRYPT_OK) {
  630. 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));
  631. exit(EXIT_FAILURE);
  632. }
  633. t1 = t_read() - t1;
  634. t2 += t1;
  635. }
  636. t2 >>= 11;
  637. fprintf(stderr, "RSA-%lu decrypt_key took %15llu cycles\n", x, t2);
  638. rsa_free(&key);
  639. }
  640. }
  641. #else
  642. void time_rsa(void) { fprintf(stderr, "NO RSA\n"); }
  643. #endif
  644. #ifdef MKAT
  645. /* time various KAT operations */
  646. void time_katja(void)
  647. {
  648. katja_key key;
  649. ulong64 t1, t2;
  650. unsigned char buf[2][4096];
  651. unsigned long x, y, z, zzz;
  652. int err, zz;
  653. for (x = 1024; x <= 2048; x += 256) {
  654. t2 = 0;
  655. for (y = 0; y < 4; y++) {
  656. t_start();
  657. t1 = t_read();
  658. if ((err = katja_make_key(&yarrow_prng, find_prng("yarrow"), x/8, &key)) != CRYPT_OK) {
  659. 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));
  660. exit(EXIT_FAILURE);
  661. }
  662. t1 = t_read() - t1;
  663. t2 += t1;
  664. if (y < 3) {
  665. rsa_free(&key);
  666. }
  667. }
  668. t2 >>= 2;
  669. fprintf(stderr, "Katja-%lu make_key took %15llu cycles\n", x, t2);
  670. t2 = 0;
  671. for (y = 0; y < 16; y++) {
  672. t_start();
  673. t1 = t_read();
  674. z = sizeof(buf[1]);
  675. if ((err = katja_encrypt_key(buf[0], 32, buf[1], &z, "testprog", 8, &yarrow_prng,
  676. find_prng("yarrow"), find_hash("sha1"),
  677. &key)) != CRYPT_OK) {
  678. 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));
  679. exit(EXIT_FAILURE);
  680. }
  681. t1 = t_read() - t1;
  682. t2 += t1;
  683. }
  684. t2 >>= 4;
  685. fprintf(stderr, "Katja-%lu encrypt_key took %15llu cycles\n", x, t2);
  686. t2 = 0;
  687. for (y = 0; y < 2048; y++) {
  688. t_start();
  689. t1 = t_read();
  690. zzz = sizeof(buf[0]);
  691. if ((err = katja_decrypt_key(buf[1], z, buf[0], &zzz, "testprog", 8, find_hash("sha1"),
  692. &zz, &key)) != CRYPT_OK) {
  693. 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));
  694. exit(EXIT_FAILURE);
  695. }
  696. t1 = t_read() - t1;
  697. t2 += t1;
  698. }
  699. t2 >>= 11;
  700. fprintf(stderr, "Katja-%lu decrypt_key took %15llu cycles\n", x, t2);
  701. katja_free(&key);
  702. }
  703. }
  704. #else
  705. void time_katja(void) { fprintf(stderr, "NO Katja\n"); }
  706. #endif
  707. #ifdef MECC
  708. /* time various ECC operations */
  709. void time_ecc(void)
  710. {
  711. ecc_key key;
  712. ulong64 t1, t2;
  713. unsigned char buf[2][4096];
  714. unsigned long i, x, y, z;
  715. int err;
  716. static unsigned long sizes[] = {
  717. #ifdef ECC192
  718. 192/8,
  719. #endif
  720. #ifdef ECC224
  721. 224/8,
  722. #endif
  723. #ifdef ECC256
  724. 256/8,
  725. #endif
  726. #ifdef ECC384
  727. 384/8,
  728. #endif
  729. #ifdef ECC521
  730. 521/8,
  731. #endif
  732. 100000};
  733. for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
  734. t2 = 0;
  735. for (y = 0; y < 64; y++) {
  736. t_start();
  737. t1 = t_read();
  738. if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
  739. 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));
  740. exit(EXIT_FAILURE);
  741. }
  742. t1 = t_read() - t1;
  743. t2 += t1;
  744. if (y < 63) {
  745. ecc_free(&key);
  746. }
  747. }
  748. t2 >>= 6;
  749. fprintf(stderr, "ECC-%lu make_key took %15llu cycles\n", x*8, t2);
  750. t2 = 0;
  751. for (y = 0; y < 16; y++) {
  752. t_start();
  753. t1 = t_read();
  754. z = sizeof(buf[1]);
  755. if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
  756. &key)) != CRYPT_OK) {
  757. 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));
  758. exit(EXIT_FAILURE);
  759. }
  760. t1 = t_read() - t1;
  761. t2 += t1;
  762. }
  763. t2 >>= 4;
  764. fprintf(stderr, "ECC-%lu encrypt_key took %15llu cycles\n", x*8, t2);
  765. ecc_free(&key);
  766. }
  767. }
  768. #else
  769. void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
  770. #endif
  771. void time_macs_(unsigned long MAC_SIZE)
  772. {
  773. unsigned char *buf, key[16], tag[16];
  774. ulong64 t1, t2;
  775. unsigned long x, z;
  776. int err, cipher_idx, hash_idx;
  777. fprintf(stderr, "\nMAC Timings (cycles/byte on %luKB blocks):\n", MAC_SIZE);
  778. buf = XMALLOC(MAC_SIZE*1024);
  779. if (buf == NULL) {
  780. fprintf(stderr, "\n\nout of heap yo\n\n");
  781. exit(EXIT_FAILURE);
  782. }
  783. cipher_idx = find_cipher("aes");
  784. hash_idx = find_hash("sha1");
  785. if (cipher_idx == -1 || hash_idx == -1) {
  786. fprintf(stderr, "Warning the MAC tests requires AES and SHA1 to operate... so sorry\n");
  787. return;
  788. }
  789. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  790. yarrow_read(key, 16, &yarrow_prng);
  791. #ifdef OMAC
  792. t2 = -1;
  793. for (x = 0; x < 10000; x++) {
  794. t_start();
  795. t1 = t_read();
  796. z = 16;
  797. if ((err = omac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  798. fprintf(stderr, "\n\nomac error... %s\n", error_to_string(err));
  799. exit(EXIT_FAILURE);
  800. }
  801. t1 = t_read() - t1;
  802. if (t1 < t2) t2 = t1;
  803. }
  804. fprintf(stderr, "OMAC-%s\t\t%9llu\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  805. #endif
  806. #ifdef PMAC
  807. t2 = -1;
  808. for (x = 0; x < 10000; x++) {
  809. t_start();
  810. t1 = t_read();
  811. z = 16;
  812. if ((err = pmac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  813. fprintf(stderr, "\n\npmac error... %s\n", error_to_string(err));
  814. exit(EXIT_FAILURE);
  815. }
  816. t1 = t_read() - t1;
  817. if (t1 < t2) t2 = t1;
  818. }
  819. fprintf(stderr, "PMAC-AES\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  820. #endif
  821. #ifdef PELICAN
  822. t2 = -1;
  823. for (x = 0; x < 10000; x++) {
  824. t_start();
  825. t1 = t_read();
  826. z = 16;
  827. if ((err = pelican_memory(key, 16, buf, MAC_SIZE*1024, tag)) != CRYPT_OK) {
  828. fprintf(stderr, "\n\npelican 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, "PELICAN \t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  835. #endif
  836. #ifdef HMAC
  837. t2 = -1;
  838. for (x = 0; x < 10000; x++) {
  839. t_start();
  840. t1 = t_read();
  841. z = 16;
  842. if ((err = hmac_memory(hash_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  843. fprintf(stderr, "\n\nhmac 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, "HMAC-%s\t\t%9llu\n", hash_descriptor[hash_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  850. #endif
  851. XFREE(buf);
  852. }
  853. void time_macs(void)
  854. {
  855. time_macs_(1);
  856. time_macs_(4);
  857. time_macs_(32);
  858. }
  859. void time_encmacs_(unsigned long MAC_SIZE)
  860. {
  861. unsigned char *buf, IV[16], key[16], tag[16];
  862. ulong64 t1, t2;
  863. unsigned long x, z;
  864. int err, cipher_idx;
  865. symmetric_key skey;
  866. fprintf(stderr, "\nENC+MAC Timings (zero byte AAD, 16 byte IV, cycles/byte on %luKB blocks):\n", MAC_SIZE);
  867. buf = XMALLOC(MAC_SIZE*1024);
  868. if (buf == NULL) {
  869. fprintf(stderr, "\n\nout of heap yo\n\n");
  870. exit(EXIT_FAILURE);
  871. }
  872. cipher_idx = find_cipher("aes");
  873. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  874. yarrow_read(key, 16, &yarrow_prng);
  875. yarrow_read(IV, 16, &yarrow_prng);
  876. #ifdef EAX_MODE
  877. t2 = -1;
  878. for (x = 0; x < 10000; x++) {
  879. t_start();
  880. t1 = t_read();
  881. z = 16;
  882. if ((err = eax_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  883. fprintf(stderr, "\nEAX error... %s\n", error_to_string(err));
  884. exit(EXIT_FAILURE);
  885. }
  886. t1 = t_read() - t1;
  887. if (t1 < t2) t2 = t1;
  888. }
  889. fprintf(stderr, "EAX \t\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  890. #endif
  891. #ifdef OCB_MODE
  892. t2 = -1;
  893. for (x = 0; x < 10000; x++) {
  894. t_start();
  895. t1 = t_read();
  896. z = 16;
  897. if ((err = ocb_encrypt_authenticate_memory(cipher_idx, key, 16, IV, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  898. fprintf(stderr, "\nOCB error... %s\n", error_to_string(err));
  899. exit(EXIT_FAILURE);
  900. }
  901. t1 = t_read() - t1;
  902. if (t1 < t2) t2 = t1;
  903. }
  904. fprintf(stderr, "OCB \t\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  905. #endif
  906. #ifdef CCM_MODE
  907. t2 = -1;
  908. for (x = 0; x < 10000; x++) {
  909. t_start();
  910. t1 = t_read();
  911. z = 16;
  912. if ((err = ccm_memory(cipher_idx, key, 16, NULL, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  913. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  914. exit(EXIT_FAILURE);
  915. }
  916. t1 = t_read() - t1;
  917. if (t1 < t2) t2 = t1;
  918. }
  919. fprintf(stderr, "CCM (no-precomp) \t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  920. cipher_descriptor[cipher_idx].setup(key, 16, 0, &skey);
  921. t2 = -1;
  922. for (x = 0; x < 10000; x++) {
  923. t_start();
  924. t1 = t_read();
  925. z = 16;
  926. if ((err = ccm_memory(cipher_idx, key, 16, &skey, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  927. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  928. exit(EXIT_FAILURE);
  929. }
  930. t1 = t_read() - t1;
  931. if (t1 < t2) t2 = t1;
  932. }
  933. fprintf(stderr, "CCM (precomp) \t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  934. cipher_descriptor[cipher_idx].done(&skey);
  935. #endif
  936. #ifdef GCM_MODE
  937. t2 = -1;
  938. for (x = 0; x < 100; x++) {
  939. t_start();
  940. t1 = t_read();
  941. z = 16;
  942. if ((err = gcm_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, GCM_ENCRYPT)) != CRYPT_OK) {
  943. fprintf(stderr, "\nGCM error... %s\n", error_to_string(err));
  944. exit(EXIT_FAILURE);
  945. }
  946. t1 = t_read() - t1;
  947. if (t1 < t2) t2 = t1;
  948. }
  949. fprintf(stderr, "GCM (no-precomp)\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  950. {
  951. gcm_state gcm;
  952. if ((err = gcm_init(&gcm, cipher_idx, key, 16)) != CRYPT_OK) { fprintf(stderr, "gcm_init: %s\n", error_to_string(err)); exit(EXIT_FAILURE); }
  953. t2 = -1;
  954. for (x = 0; x < 10000; x++) {
  955. t_start();
  956. t1 = t_read();
  957. z = 16;
  958. if ((err = gcm_reset(&gcm)) != CRYPT_OK) {
  959. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  960. exit(EXIT_FAILURE);
  961. }
  962. if ((err = gcm_add_iv(&gcm, IV, 16)) != CRYPT_OK) {
  963. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  964. exit(EXIT_FAILURE);
  965. }
  966. if ((err = gcm_add_aad(&gcm, NULL, 0)) != CRYPT_OK) {
  967. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  968. exit(EXIT_FAILURE);
  969. }
  970. if ((err = gcm_process(&gcm, buf, MAC_SIZE*1024, buf, GCM_ENCRYPT)) != CRYPT_OK) {
  971. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  972. exit(EXIT_FAILURE);
  973. }
  974. if ((err = gcm_done(&gcm, tag, &z)) != CRYPT_OK) {
  975. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  976. exit(EXIT_FAILURE);
  977. }
  978. t1 = t_read() - t1;
  979. if (t1 < t2) t2 = t1;
  980. }
  981. fprintf(stderr, "GCM (precomp)\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  982. }
  983. #endif
  984. }
  985. void time_encmacs(void)
  986. {
  987. time_encmacs_(1);
  988. time_encmacs_(4);
  989. time_encmacs_(32);
  990. }
  991. /* $Source$ */
  992. /* $Revision$ */
  993. /* $Date$ */