x86_prof.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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 KSEED
  163. register_cipher (&kseed_desc);
  164. #endif
  165. #ifdef LTC_KASUMI
  166. register_cipher (&kasumi_desc);
  167. #endif
  168. #ifdef TIGER
  169. register_hash (&tiger_desc);
  170. #endif
  171. #ifdef MD2
  172. register_hash (&md2_desc);
  173. #endif
  174. #ifdef MD4
  175. register_hash (&md4_desc);
  176. #endif
  177. #ifdef MD5
  178. register_hash (&md5_desc);
  179. #endif
  180. #ifdef SHA1
  181. register_hash (&sha1_desc);
  182. #endif
  183. #ifdef SHA224
  184. register_hash (&sha224_desc);
  185. #endif
  186. #ifdef SHA256
  187. register_hash (&sha256_desc);
  188. #endif
  189. #ifdef SHA384
  190. register_hash (&sha384_desc);
  191. #endif
  192. #ifdef SHA512
  193. register_hash (&sha512_desc);
  194. #endif
  195. #ifdef RIPEMD128
  196. register_hash (&rmd128_desc);
  197. #endif
  198. #ifdef RIPEMD160
  199. register_hash (&rmd160_desc);
  200. #endif
  201. #ifdef RIPEMD256
  202. register_hash (&rmd256_desc);
  203. #endif
  204. #ifdef RIPEMD320
  205. register_hash (&rmd320_desc);
  206. #endif
  207. #ifdef WHIRLPOOL
  208. register_hash (&whirlpool_desc);
  209. #endif
  210. #ifdef CHC_HASH
  211. register_hash(&chc_desc);
  212. if ((err = chc_register(register_cipher(&aes_desc))) != CRYPT_OK) {
  213. fprintf(stderr, "chc_register error: %s\n", error_to_string(err));
  214. exit(EXIT_FAILURE);
  215. }
  216. #endif
  217. #ifndef YARROW
  218. #error This demo requires Yarrow.
  219. #endif
  220. register_prng(&yarrow_desc);
  221. #ifdef FORTUNA
  222. register_prng(&fortuna_desc);
  223. #endif
  224. #ifdef RC4
  225. register_prng(&rc4_desc);
  226. #endif
  227. #ifdef SOBER128
  228. register_prng(&sober128_desc);
  229. #endif
  230. if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
  231. fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
  232. exit(EXIT_FAILURE);
  233. }
  234. }
  235. int time_keysched(void)
  236. {
  237. unsigned long x, y1;
  238. ulong64 t1, c1;
  239. symmetric_key skey;
  240. int kl;
  241. int (*func) (const unsigned char *, int , int , symmetric_key *);
  242. unsigned char key[MAXBLOCKSIZE];
  243. fprintf(stderr, "\n\nKey Schedule Time Trials for the Symmetric Ciphers:\n(Times are cycles per key)\n");
  244. no_results = 0;
  245. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  246. #define DO1(k) func(k, kl, 0, &skey);
  247. func = cipher_descriptor[x].setup;
  248. kl = cipher_descriptor[x].min_key_length;
  249. c1 = (ulong64)-1;
  250. for (y1 = 0; y1 < KTIMES; y1++) {
  251. yarrow_read(key, kl, &yarrow_prng);
  252. t_start();
  253. DO1(key);
  254. t1 = t_read();
  255. c1 = (t1 > c1) ? c1 : t1;
  256. }
  257. t1 = c1 - skew;
  258. results[no_results].spd1 = results[no_results].avg = t1;
  259. results[no_results++].id = x;
  260. fprintf(stderr, "."); fflush(stdout);
  261. #undef DO1
  262. }
  263. tally_results(0);
  264. return 0;
  265. }
  266. int time_cipher(void)
  267. {
  268. unsigned long x, y1;
  269. ulong64 t1, t2, c1, c2, a1, a2;
  270. symmetric_ECB ecb;
  271. unsigned char key[MAXBLOCKSIZE], pt[4096];
  272. int err;
  273. fprintf(stderr, "\n\nECB Time Trials for the Symmetric Ciphers:\n");
  274. no_results = 0;
  275. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  276. ecb_start(x, key, cipher_descriptor[x].min_key_length, 0, &ecb);
  277. /* sanity check on cipher */
  278. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  279. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  280. exit(EXIT_FAILURE);
  281. }
  282. #define DO1 ecb_encrypt(pt, pt, sizeof(pt), &ecb);
  283. #define DO2 DO1 DO1
  284. c1 = c2 = (ulong64)-1;
  285. for (y1 = 0; y1 < 100; y1++) {
  286. t_start();
  287. DO1;
  288. t1 = t_read();
  289. DO2;
  290. t2 = t_read();
  291. t2 -= t1;
  292. c1 = (t1 > c1 ? c1 : t1);
  293. c2 = (t2 > c2 ? c2 : t2);
  294. }
  295. a1 = c2 - c1 - skew;
  296. #undef DO1
  297. #undef DO2
  298. #define DO1 ecb_decrypt(pt, pt, sizeof(pt), &ecb);
  299. #define DO2 DO1 DO1
  300. c1 = c2 = (ulong64)-1;
  301. for (y1 = 0; y1 < 100; y1++) {
  302. t_start();
  303. DO1;
  304. t1 = t_read();
  305. DO2;
  306. t2 = t_read();
  307. t2 -= t1;
  308. c1 = (t1 > c1 ? c1 : t1);
  309. c2 = (t2 > c2 ? c2 : t2);
  310. }
  311. a2 = c2 - c1 - skew;
  312. ecb_done(&ecb);
  313. results[no_results].id = x;
  314. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  315. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  316. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  317. ++no_results;
  318. fprintf(stderr, "."); fflush(stdout);
  319. #undef DO2
  320. #undef DO1
  321. }
  322. tally_results(1);
  323. return 0;
  324. }
  325. #ifdef LTC_CBC_MODE
  326. int time_cipher2(void)
  327. {
  328. unsigned long x, y1;
  329. ulong64 t1, t2, c1, c2, a1, a2;
  330. symmetric_CBC cbc;
  331. unsigned char key[MAXBLOCKSIZE], pt[4096];
  332. int err;
  333. fprintf(stderr, "\n\nCBC Time Trials for the Symmetric Ciphers:\n");
  334. no_results = 0;
  335. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  336. cbc_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, &cbc);
  337. /* sanity check on cipher */
  338. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  339. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  340. exit(EXIT_FAILURE);
  341. }
  342. #define DO1 cbc_encrypt(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. a1 = c2 - c1 - skew;
  356. #undef DO1
  357. #undef DO2
  358. #define DO1 cbc_decrypt(pt, pt, sizeof(pt), &cbc);
  359. #define DO2 DO1 DO1
  360. c1 = c2 = (ulong64)-1;
  361. for (y1 = 0; y1 < 100; y1++) {
  362. t_start();
  363. DO1;
  364. t1 = t_read();
  365. DO2;
  366. t2 = t_read();
  367. t2 -= t1;
  368. c1 = (t1 > c1 ? c1 : t1);
  369. c2 = (t2 > c2 ? c2 : t2);
  370. }
  371. a2 = c2 - c1 - skew;
  372. cbc_done(&cbc);
  373. results[no_results].id = x;
  374. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  375. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  376. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  377. ++no_results;
  378. fprintf(stderr, "."); fflush(stdout);
  379. #undef DO2
  380. #undef DO1
  381. }
  382. tally_results(1);
  383. return 0;
  384. }
  385. #else
  386. int time_cipher2(void) { fprintf(stderr, "NO CBC\n"); return 0; }
  387. #endif
  388. #ifdef LTC_CTR_MODE
  389. int time_cipher3(void)
  390. {
  391. unsigned long x, y1;
  392. ulong64 t1, t2, c1, c2, a1, a2;
  393. symmetric_CTR ctr;
  394. unsigned char key[MAXBLOCKSIZE], pt[4096];
  395. int err;
  396. fprintf(stderr, "\n\nCTR Time Trials for the Symmetric Ciphers:\n");
  397. no_results = 0;
  398. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  399. ctr_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr);
  400. /* sanity check on cipher */
  401. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  402. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  403. exit(EXIT_FAILURE);
  404. }
  405. #define DO1 ctr_encrypt(pt, pt, sizeof(pt), &ctr);
  406. #define DO2 DO1 DO1
  407. c1 = c2 = (ulong64)-1;
  408. for (y1 = 0; y1 < 100; y1++) {
  409. t_start();
  410. DO1;
  411. t1 = t_read();
  412. DO2;
  413. t2 = t_read();
  414. t2 -= t1;
  415. c1 = (t1 > c1 ? c1 : t1);
  416. c2 = (t2 > c2 ? c2 : t2);
  417. }
  418. a1 = c2 - c1 - skew;
  419. #undef DO1
  420. #undef DO2
  421. #define DO1 ctr_decrypt(pt, pt, sizeof(pt), &ctr);
  422. #define DO2 DO1 DO1
  423. c1 = c2 = (ulong64)-1;
  424. for (y1 = 0; y1 < 100; y1++) {
  425. t_start();
  426. DO1;
  427. t1 = t_read();
  428. DO2;
  429. t2 = t_read();
  430. t2 -= t1;
  431. c1 = (t1 > c1 ? c1 : t1);
  432. c2 = (t2 > c2 ? c2 : t2);
  433. }
  434. a2 = c2 - c1 - skew;
  435. ctr_done(&ctr);
  436. results[no_results].id = x;
  437. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  438. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  439. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  440. ++no_results;
  441. fprintf(stderr, "."); fflush(stdout);
  442. #undef DO2
  443. #undef DO1
  444. }
  445. tally_results(1);
  446. return 0;
  447. }
  448. #else
  449. int time_cipher3(void) { fprintf(stderr, "NO CTR\n"); return 0; }
  450. #endif
  451. #ifdef LTC_LRW_MODE
  452. int time_cipher4(void)
  453. {
  454. unsigned long x, y1;
  455. ulong64 t1, t2, c1, c2, a1, a2;
  456. symmetric_LRW lrw;
  457. unsigned char key[MAXBLOCKSIZE], pt[4096];
  458. int err;
  459. fprintf(stderr, "\n\nLRW Time Trials for the Symmetric Ciphers:\n");
  460. no_results = 0;
  461. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  462. if (cipher_descriptor[x].block_length != 16) continue;
  463. lrw_start(x, pt, key, cipher_descriptor[x].min_key_length, key, 0, &lrw);
  464. /* sanity check on cipher */
  465. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  466. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  467. exit(EXIT_FAILURE);
  468. }
  469. #define DO1 lrw_encrypt(pt, pt, sizeof(pt), &lrw);
  470. #define DO2 DO1 DO1
  471. c1 = c2 = (ulong64)-1;
  472. for (y1 = 0; y1 < 100; y1++) {
  473. t_start();
  474. DO1;
  475. t1 = t_read();
  476. DO2;
  477. t2 = t_read();
  478. t2 -= t1;
  479. c1 = (t1 > c1 ? c1 : t1);
  480. c2 = (t2 > c2 ? c2 : t2);
  481. }
  482. a1 = c2 - c1 - skew;
  483. #undef DO1
  484. #undef DO2
  485. #define DO1 lrw_decrypt(pt, pt, sizeof(pt), &lrw);
  486. #define DO2 DO1 DO1
  487. c1 = c2 = (ulong64)-1;
  488. for (y1 = 0; y1 < 100; y1++) {
  489. t_start();
  490. DO1;
  491. t1 = t_read();
  492. DO2;
  493. t2 = t_read();
  494. t2 -= t1;
  495. c1 = (t1 > c1 ? c1 : t1);
  496. c2 = (t2 > c2 ? c2 : t2);
  497. }
  498. a2 = c2 - c1 - skew;
  499. lrw_done(&lrw);
  500. results[no_results].id = x;
  501. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  502. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  503. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  504. ++no_results;
  505. fprintf(stderr, "."); fflush(stdout);
  506. #undef DO2
  507. #undef DO1
  508. }
  509. tally_results(1);
  510. return 0;
  511. }
  512. #else
  513. int time_cipher4(void) { fprintf(stderr, "NO LRW\n"); return 0; }
  514. #endif
  515. int time_hash(void)
  516. {
  517. unsigned long x, y1, len;
  518. ulong64 t1, t2, c1, c2;
  519. hash_state md;
  520. int (*func)(hash_state *, const unsigned char *, unsigned long), err;
  521. unsigned char pt[MAXBLOCKSIZE];
  522. fprintf(stderr, "\n\nHASH Time Trials for:\n");
  523. no_results = 0;
  524. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  525. /* sanity check on hash */
  526. if ((err = hash_descriptor[x].test()) != CRYPT_OK) {
  527. fprintf(stderr, "\n\nERROR: Hash %s failed self-test %s\n", hash_descriptor[x].name, error_to_string(err));
  528. exit(EXIT_FAILURE);
  529. }
  530. hash_descriptor[x].init(&md);
  531. #define DO1 func(&md,pt,len);
  532. #define DO2 DO1 DO1
  533. func = hash_descriptor[x].process;
  534. len = hash_descriptor[x].blocksize;
  535. c1 = c2 = (ulong64)-1;
  536. for (y1 = 0; y1 < TIMES; y1++) {
  537. t_start();
  538. DO1;
  539. t1 = t_read();
  540. DO2;
  541. t2 = t_read() - t1;
  542. c1 = (t1 > c1) ? c1 : t1;
  543. c2 = (t2 > c2) ? c2 : t2;
  544. }
  545. t1 = c2 - c1 - skew;
  546. t1 = ((t1 * CONST64(1000))) / ((ulong64)hash_descriptor[x].blocksize);
  547. results[no_results].id = x;
  548. results[no_results].spd1 = results[no_results].avg = t1;
  549. ++no_results;
  550. fprintf(stderr, "."); fflush(stdout);
  551. #undef DO2
  552. #undef DO1
  553. }
  554. tally_results(2);
  555. return 0;
  556. }
  557. #undef MPI
  558. /*#warning you need an mp_rand!!!*/
  559. #ifdef MPI
  560. void time_mult(void)
  561. {
  562. ulong64 t1, t2;
  563. unsigned long x, y;
  564. void *a, *b, *c;
  565. fprintf(stderr, "Timing Multiplying:\n");
  566. mp_init_multi(&a,&b,&c,NULL);
  567. for (x = 128/DIGIT_BIT; x <= 1536/DIGIT_BIT; x += 128/DIGIT_BIT) {
  568. mp_rand(&a, x);
  569. mp_rand(&b, x);
  570. #define DO1 mp_mul(&a, &b, &c);
  571. #define DO2 DO1; DO1;
  572. t2 = -1;
  573. for (y = 0; y < TIMES; y++) {
  574. t_start();
  575. t1 = t_read();
  576. DO2;
  577. t1 = (t_read() - t1)>>1;
  578. if (t1 < t2) t2 = t1;
  579. }
  580. fprintf(stderr, "%4lu bits: %9llu cycles\n", x*DIGIT_BIT, t2);
  581. }
  582. mp_clear_multi(&a,&b,&c,NULL);
  583. #undef DO1
  584. #undef DO2
  585. }
  586. void time_sqr(void)
  587. {
  588. ulong64 t1, t2;
  589. unsigned long x, y;
  590. mp_int a, b;
  591. fprintf(stderr, "Timing Squaring:\n");
  592. mp_init_multi(&a,&b,NULL);
  593. for (x = 128/DIGIT_BIT; x <= 1536/DIGIT_BIT; x += 128/DIGIT_BIT) {
  594. mp_rand(&a, x);
  595. #define DO1 mp_sqr(&a, &b);
  596. #define DO2 DO1; DO1;
  597. t2 = -1;
  598. for (y = 0; y < TIMES; y++) {
  599. t_start();
  600. t1 = t_read();
  601. DO2;
  602. t1 = (t_read() - t1)>>1;
  603. if (t1 < t2) t2 = t1;
  604. }
  605. fprintf(stderr, "%4lu bits: %9llu cycles\n", x*DIGIT_BIT, t2);
  606. }
  607. mp_clear_multi(&a,&b,NULL);
  608. #undef DO1
  609. #undef DO2
  610. }
  611. #else
  612. void time_mult(void) { fprintf(stderr, "NO MULT\n"); }
  613. void time_sqr(void) { fprintf(stderr, "NO SQR\n"); }
  614. #endif
  615. void time_prng(void)
  616. {
  617. ulong64 t1, t2;
  618. unsigned char buf[4096];
  619. prng_state tprng;
  620. unsigned long x, y;
  621. int err;
  622. fprintf(stderr, "Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n");
  623. for (x = 0; prng_descriptor[x].name != NULL; x++) {
  624. /* sanity check on prng */
  625. if ((err = prng_descriptor[x].test()) != CRYPT_OK) {
  626. fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor[x].name, error_to_string(err));
  627. exit(EXIT_FAILURE);
  628. }
  629. prng_descriptor[x].start(&tprng);
  630. zeromem(buf, 256);
  631. prng_descriptor[x].add_entropy(buf, 256, &tprng);
  632. prng_descriptor[x].ready(&tprng);
  633. t2 = -1;
  634. #define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { fprintf(stderr, "\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
  635. #define DO2 DO1 DO1
  636. for (y = 0; y < 10000; y++) {
  637. t_start();
  638. t1 = t_read();
  639. DO2;
  640. t1 = (t_read() - t1)>>1;
  641. if (t1 < t2) t2 = t1;
  642. }
  643. fprintf(stderr, "%20s: %5llu ", prng_descriptor[x].name, t2>>12);
  644. #undef DO2
  645. #undef DO1
  646. #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);
  647. #define DO2 DO1 DO1
  648. for (y = 0; y < 10000; y++) {
  649. t_start();
  650. t1 = t_read();
  651. DO2;
  652. t1 = (t_read() - t1)>>1;
  653. if (t1 < t2) t2 = t1;
  654. }
  655. fprintf(stderr, "%5llu\n", t2);
  656. #undef DO2
  657. #undef DO1
  658. }
  659. }
  660. #ifdef MDSA
  661. /* time various DSA operations */
  662. void time_dsa(void)
  663. {
  664. dsa_key key;
  665. ulong64 t1, t2;
  666. unsigned long x, y;
  667. int err;
  668. static const struct {
  669. int group, modulus;
  670. } groups[] = {
  671. { 20, 96 },
  672. { 20, 128 },
  673. { 24, 192 },
  674. { 28, 256 },
  675. { 32, 512 }
  676. };
  677. for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
  678. t2 = 0;
  679. for (y = 0; y < 4; y++) {
  680. t_start();
  681. t1 = t_read();
  682. if ((err = dsa_make_key(&yarrow_prng, find_prng("yarrow"), groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) {
  683. fprintf(stderr, "\n\ndsa_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
  684. exit(EXIT_FAILURE);
  685. }
  686. t1 = t_read() - t1;
  687. t2 += t1;
  688. #ifdef LTC_PROFILE
  689. t2 <<= 2;
  690. break;
  691. #endif
  692. if (y < 3) {
  693. dsa_free(&key);
  694. }
  695. }
  696. t2 >>= 2;
  697. fprintf(stderr, "DSA-(%lu, %lu) make_key took %15llu cycles\n", (unsigned long)groups[x].group*8, (unsigned long)groups[x].modulus*8, t2);
  698. }
  699. }
  700. #endif
  701. #ifdef MRSA
  702. /* time various RSA operations */
  703. void time_rsa(void)
  704. {
  705. rsa_key key;
  706. ulong64 t1, t2;
  707. unsigned char buf[2][2048];
  708. unsigned long x, y, z, zzz;
  709. int err, zz, stat;
  710. for (x = 1024; x <= 2048; x += 256) {
  711. t2 = 0;
  712. for (y = 0; y < 4; y++) {
  713. t_start();
  714. t1 = t_read();
  715. if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) {
  716. 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));
  717. exit(EXIT_FAILURE);
  718. }
  719. t1 = t_read() - t1;
  720. t2 += t1;
  721. #ifdef LTC_PROFILE
  722. t2 <<= 2;
  723. break;
  724. #endif
  725. if (y < 3) {
  726. rsa_free(&key);
  727. }
  728. }
  729. t2 >>= 2;
  730. fprintf(stderr, "RSA-%lu make_key took %15llu cycles\n", x, t2);
  731. t2 = 0;
  732. for (y = 0; y < 16; y++) {
  733. t_start();
  734. t1 = t_read();
  735. z = sizeof(buf[1]);
  736. if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, (const unsigned char *)"testprog", 8, &yarrow_prng,
  737. find_prng("yarrow"), find_hash("sha1"),
  738. &key)) != CRYPT_OK) {
  739. 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));
  740. exit(EXIT_FAILURE);
  741. }
  742. t1 = t_read() - t1;
  743. t2 += t1;
  744. #ifdef LTC_PROFILE
  745. t2 <<= 4;
  746. break;
  747. #endif
  748. }
  749. t2 >>= 4;
  750. fprintf(stderr, "RSA-%lu encrypt_key took %15llu cycles\n", x, t2);
  751. t2 = 0;
  752. for (y = 0; y < 2048; y++) {
  753. t_start();
  754. t1 = t_read();
  755. zzz = sizeof(buf[0]);
  756. if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, (const unsigned char *)"testprog", 8, find_hash("sha1"),
  757. &zz, &key)) != CRYPT_OK) {
  758. 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));
  759. exit(EXIT_FAILURE);
  760. }
  761. t1 = t_read() - t1;
  762. t2 += t1;
  763. #ifdef LTC_PROFILE
  764. t2 <<= 11;
  765. break;
  766. #endif
  767. }
  768. t2 >>= 11;
  769. fprintf(stderr, "RSA-%lu decrypt_key took %15llu cycles\n", x, t2);
  770. t2 = 0;
  771. for (y = 0; y < 256; y++) {
  772. t_start();
  773. t1 = t_read();
  774. z = sizeof(buf[1]);
  775. if ((err = rsa_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
  776. find_prng("yarrow"), find_hash("sha1"), 8, &key)) != CRYPT_OK) {
  777. fprintf(stderr, "\n\nrsa_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
  778. exit(EXIT_FAILURE);
  779. }
  780. t1 = t_read() - t1;
  781. t2 += t1;
  782. #ifdef LTC_PROFILE
  783. t2 <<= 8;
  784. break;
  785. #endif
  786. }
  787. t2 >>= 8;
  788. fprintf(stderr, "RSA-%lu sign_hash took %15llu cycles\n", x, t2);
  789. t2 = 0;
  790. for (y = 0; y < 2048; y++) {
  791. t_start();
  792. t1 = t_read();
  793. if ((err = rsa_verify_hash(buf[1], z, buf[0], 20, find_hash("sha1"), 8, &stat, &key)) != CRYPT_OK) {
  794. fprintf(stderr, "\n\nrsa_verify_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
  795. exit(EXIT_FAILURE);
  796. }
  797. if (stat == 0) {
  798. fprintf(stderr, "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n", x, y);
  799. exit(EXIT_FAILURE);
  800. }
  801. t1 = t_read() - t1;
  802. t2 += t1;
  803. #ifdef LTC_PROFILE
  804. t2 <<= 11;
  805. break;
  806. #endif
  807. }
  808. t2 >>= 11;
  809. fprintf(stderr, "RSA-%lu verify_hash took %15llu cycles\n", x, t2);
  810. fprintf(stderr, "\n\n");
  811. rsa_free(&key);
  812. }
  813. }
  814. #else
  815. void time_rsa(void) { fprintf(stderr, "NO RSA\n"); }
  816. #endif
  817. #ifdef MKAT
  818. /* time various KAT operations */
  819. void time_katja(void)
  820. {
  821. katja_key key;
  822. ulong64 t1, t2;
  823. unsigned char buf[2][4096];
  824. unsigned long x, y, z, zzz;
  825. int err, zz;
  826. for (x = 1024; x <= 2048; x += 256) {
  827. t2 = 0;
  828. for (y = 0; y < 4; y++) {
  829. t_start();
  830. t1 = t_read();
  831. if ((err = katja_make_key(&yarrow_prng, find_prng("yarrow"), x/8, &key)) != CRYPT_OK) {
  832. 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));
  833. exit(EXIT_FAILURE);
  834. }
  835. t1 = t_read() - t1;
  836. t2 += t1;
  837. if (y < 3) {
  838. katja_free(&key);
  839. }
  840. }
  841. t2 >>= 2;
  842. fprintf(stderr, "Katja-%lu make_key took %15llu cycles\n", x, t2);
  843. t2 = 0;
  844. for (y = 0; y < 16; y++) {
  845. t_start();
  846. t1 = t_read();
  847. z = sizeof(buf[1]);
  848. if ((err = katja_encrypt_key(buf[0], 32, buf[1], &z, "testprog", 8, &yarrow_prng,
  849. find_prng("yarrow"), find_hash("sha1"),
  850. &key)) != CRYPT_OK) {
  851. 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));
  852. exit(EXIT_FAILURE);
  853. }
  854. t1 = t_read() - t1;
  855. t2 += t1;
  856. }
  857. t2 >>= 4;
  858. fprintf(stderr, "Katja-%lu encrypt_key took %15llu cycles\n", x, t2);
  859. t2 = 0;
  860. for (y = 0; y < 2048; y++) {
  861. t_start();
  862. t1 = t_read();
  863. zzz = sizeof(buf[0]);
  864. if ((err = katja_decrypt_key(buf[1], z, buf[0], &zzz, "testprog", 8, find_hash("sha1"),
  865. &zz, &key)) != CRYPT_OK) {
  866. 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));
  867. exit(EXIT_FAILURE);
  868. }
  869. t1 = t_read() - t1;
  870. t2 += t1;
  871. }
  872. t2 >>= 11;
  873. fprintf(stderr, "Katja-%lu decrypt_key took %15llu cycles\n", x, t2);
  874. katja_free(&key);
  875. }
  876. }
  877. #else
  878. void time_katja(void) { fprintf(stderr, "NO Katja\n"); }
  879. #endif
  880. #ifdef MECC
  881. /* time various ECC operations */
  882. void time_ecc(void)
  883. {
  884. ecc_key key;
  885. ulong64 t1, t2;
  886. unsigned char buf[2][256];
  887. unsigned long i, w, x, y, z;
  888. int err, stat;
  889. static unsigned long sizes[] = {
  890. #ifdef ECC112
  891. 112/8,
  892. #endif
  893. #ifdef ECC128
  894. 128/8,
  895. #endif
  896. #ifdef ECC160
  897. 160/8,
  898. #endif
  899. #ifdef ECC192
  900. 192/8,
  901. #endif
  902. #ifdef ECC224
  903. 224/8,
  904. #endif
  905. #ifdef ECC256
  906. 256/8,
  907. #endif
  908. #ifdef ECC384
  909. 384/8,
  910. #endif
  911. #ifdef ECC521
  912. 521/8,
  913. #endif
  914. 100000};
  915. for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
  916. t2 = 0;
  917. for (y = 0; y < 256; y++) {
  918. t_start();
  919. t1 = t_read();
  920. if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
  921. 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));
  922. exit(EXIT_FAILURE);
  923. }
  924. t1 = t_read() - t1;
  925. t2 += t1;
  926. #ifdef LTC_PROFILE
  927. t2 <<= 8;
  928. break;
  929. #endif
  930. if (y < 255) {
  931. ecc_free(&key);
  932. }
  933. }
  934. t2 >>= 8;
  935. fprintf(stderr, "ECC-%lu make_key took %15llu cycles\n", x*8, t2);
  936. t2 = 0;
  937. for (y = 0; y < 256; y++) {
  938. t_start();
  939. t1 = t_read();
  940. z = sizeof(buf[1]);
  941. if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
  942. &key)) != CRYPT_OK) {
  943. 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));
  944. exit(EXIT_FAILURE);
  945. }
  946. t1 = t_read() - t1;
  947. t2 += t1;
  948. #ifdef LTC_PROFILE
  949. t2 <<= 8;
  950. break;
  951. #endif
  952. }
  953. t2 >>= 8;
  954. fprintf(stderr, "ECC-%lu encrypt_key took %15llu cycles\n", x*8, t2);
  955. t2 = 0;
  956. for (y = 0; y < 256; y++) {
  957. t_start();
  958. t1 = t_read();
  959. w = 20;
  960. if ((err = ecc_decrypt_key(buf[1], z, buf[0], &w, &key)) != CRYPT_OK) {
  961. fprintf(stderr, "\n\necc_decrypt_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
  962. exit(EXIT_FAILURE);
  963. }
  964. t1 = t_read() - t1;
  965. t2 += t1;
  966. #ifdef LTC_PROFILE
  967. t2 <<= 8;
  968. break;
  969. #endif
  970. }
  971. t2 >>= 8;
  972. fprintf(stderr, "ECC-%lu decrypt_key took %15llu cycles\n", x*8, t2);
  973. t2 = 0;
  974. for (y = 0; y < 256; y++) {
  975. t_start();
  976. t1 = t_read();
  977. z = sizeof(buf[1]);
  978. if ((err = ecc_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
  979. find_prng("yarrow"), &key)) != CRYPT_OK) {
  980. fprintf(stderr, "\n\necc_sign_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
  981. exit(EXIT_FAILURE);
  982. }
  983. t1 = t_read() - t1;
  984. t2 += t1;
  985. #ifdef LTC_PROFILE
  986. t2 <<= 8;
  987. break;
  988. #endif
  989. }
  990. t2 >>= 8;
  991. fprintf(stderr, "ECC-%lu sign_hash took %15llu cycles\n", x*8, t2);
  992. t2 = 0;
  993. for (y = 0; y < 256; y++) {
  994. t_start();
  995. t1 = t_read();
  996. if ((err = ecc_verify_hash(buf[1], z, buf[0], 20, &stat, &key)) != CRYPT_OK) {
  997. fprintf(stderr, "\n\necc_verify_hash says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
  998. exit(EXIT_FAILURE);
  999. }
  1000. if (stat == 0) {
  1001. fprintf(stderr, "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n", x*8, y);
  1002. exit(EXIT_FAILURE);
  1003. }
  1004. t1 = t_read() - t1;
  1005. t2 += t1;
  1006. #ifdef LTC_PROFILE
  1007. t2 <<= 8;
  1008. break;
  1009. #endif
  1010. }
  1011. t2 >>= 8;
  1012. fprintf(stderr, "ECC-%lu verify_hash took %15llu cycles\n", x*8, t2);
  1013. fprintf(stderr, "\n\n");
  1014. ecc_free(&key);
  1015. }
  1016. }
  1017. #else
  1018. void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
  1019. #endif
  1020. void time_macs_(unsigned long MAC_SIZE)
  1021. {
  1022. unsigned char *buf, key[16], tag[16];
  1023. ulong64 t1, t2;
  1024. unsigned long x, z;
  1025. int err, cipher_idx, hash_idx;
  1026. fprintf(stderr, "\nMAC Timings (cycles/byte on %luKB blocks):\n", MAC_SIZE);
  1027. buf = XMALLOC(MAC_SIZE*1024);
  1028. if (buf == NULL) {
  1029. fprintf(stderr, "\n\nout of heap yo\n\n");
  1030. exit(EXIT_FAILURE);
  1031. }
  1032. cipher_idx = find_cipher("aes");
  1033. hash_idx = find_hash("sha1");
  1034. if (cipher_idx == -1 || hash_idx == -1) {
  1035. fprintf(stderr, "Warning the MAC tests requires AES and SHA1 to operate... so sorry\n");
  1036. return;
  1037. }
  1038. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  1039. yarrow_read(key, 16, &yarrow_prng);
  1040. #ifdef LTC_OMAC
  1041. t2 = -1;
  1042. for (x = 0; x < 10000; x++) {
  1043. t_start();
  1044. t1 = t_read();
  1045. z = 16;
  1046. if ((err = omac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1047. fprintf(stderr, "\n\nomac error... %s\n", error_to_string(err));
  1048. exit(EXIT_FAILURE);
  1049. }
  1050. t1 = t_read() - t1;
  1051. if (t1 < t2) t2 = t1;
  1052. }
  1053. fprintf(stderr, "OMAC-%s\t\t%9llu\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1054. #endif
  1055. #ifdef LTC_XCBC
  1056. t2 = -1;
  1057. for (x = 0; x < 10000; x++) {
  1058. t_start();
  1059. t1 = t_read();
  1060. z = 16;
  1061. if ((err = xcbc_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1062. fprintf(stderr, "\n\nxcbc error... %s\n", error_to_string(err));
  1063. exit(EXIT_FAILURE);
  1064. }
  1065. t1 = t_read() - t1;
  1066. if (t1 < t2) t2 = t1;
  1067. }
  1068. fprintf(stderr, "XCBC-%s\t\t%9llu\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1069. #endif
  1070. #ifdef LTC_F9_MODE
  1071. t2 = -1;
  1072. for (x = 0; x < 10000; x++) {
  1073. t_start();
  1074. t1 = t_read();
  1075. z = 16;
  1076. if ((err = f9_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1077. fprintf(stderr, "\n\nF9 error... %s\n", error_to_string(err));
  1078. exit(EXIT_FAILURE);
  1079. }
  1080. t1 = t_read() - t1;
  1081. if (t1 < t2) t2 = t1;
  1082. }
  1083. fprintf(stderr, "F9-%s\t\t\t%9llu\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1084. #endif
  1085. #ifdef LTC_PMAC
  1086. t2 = -1;
  1087. for (x = 0; x < 10000; x++) {
  1088. t_start();
  1089. t1 = t_read();
  1090. z = 16;
  1091. if ((err = pmac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1092. fprintf(stderr, "\n\npmac error... %s\n", error_to_string(err));
  1093. exit(EXIT_FAILURE);
  1094. }
  1095. t1 = t_read() - t1;
  1096. if (t1 < t2) t2 = t1;
  1097. }
  1098. fprintf(stderr, "PMAC-AES\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  1099. #endif
  1100. #ifdef PELICAN
  1101. t2 = -1;
  1102. for (x = 0; x < 10000; x++) {
  1103. t_start();
  1104. t1 = t_read();
  1105. z = 16;
  1106. if ((err = pelican_memory(key, 16, buf, MAC_SIZE*1024, tag)) != CRYPT_OK) {
  1107. fprintf(stderr, "\n\npelican error... %s\n", error_to_string(err));
  1108. exit(EXIT_FAILURE);
  1109. }
  1110. t1 = t_read() - t1;
  1111. if (t1 < t2) t2 = t1;
  1112. }
  1113. fprintf(stderr, "PELICAN \t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  1114. #endif
  1115. #ifdef LTC_HMAC
  1116. t2 = -1;
  1117. for (x = 0; x < 10000; x++) {
  1118. t_start();
  1119. t1 = t_read();
  1120. z = 16;
  1121. if ((err = hmac_memory(hash_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1122. fprintf(stderr, "\n\nhmac error... %s\n", error_to_string(err));
  1123. exit(EXIT_FAILURE);
  1124. }
  1125. t1 = t_read() - t1;
  1126. if (t1 < t2) t2 = t1;
  1127. }
  1128. fprintf(stderr, "HMAC-%s\t\t%9llu\n", hash_descriptor[hash_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1129. #endif
  1130. XFREE(buf);
  1131. }
  1132. void time_macs(void)
  1133. {
  1134. time_macs_(1);
  1135. time_macs_(4);
  1136. time_macs_(32);
  1137. }
  1138. void time_encmacs_(unsigned long MAC_SIZE)
  1139. {
  1140. unsigned char *buf, IV[16], key[16], tag[16];
  1141. ulong64 t1, t2;
  1142. unsigned long x, z;
  1143. int err, cipher_idx;
  1144. symmetric_key skey;
  1145. fprintf(stderr, "\nENC+MAC Timings (zero byte AAD, 16 byte IV, cycles/byte on %luKB blocks):\n", MAC_SIZE);
  1146. buf = XMALLOC(MAC_SIZE*1024);
  1147. if (buf == NULL) {
  1148. fprintf(stderr, "\n\nout of heap yo\n\n");
  1149. exit(EXIT_FAILURE);
  1150. }
  1151. cipher_idx = find_cipher("aes");
  1152. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  1153. yarrow_read(key, 16, &yarrow_prng);
  1154. yarrow_read(IV, 16, &yarrow_prng);
  1155. #ifdef EAX_MODE
  1156. t2 = -1;
  1157. for (x = 0; x < 10000; x++) {
  1158. t_start();
  1159. t1 = t_read();
  1160. z = 16;
  1161. if ((err = eax_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1162. fprintf(stderr, "\nEAX error... %s\n", error_to_string(err));
  1163. exit(EXIT_FAILURE);
  1164. }
  1165. t1 = t_read() - t1;
  1166. if (t1 < t2) t2 = t1;
  1167. }
  1168. fprintf(stderr, "EAX \t\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  1169. #endif
  1170. #ifdef OCB_MODE
  1171. t2 = -1;
  1172. for (x = 0; x < 10000; x++) {
  1173. t_start();
  1174. t1 = t_read();
  1175. z = 16;
  1176. if ((err = ocb_encrypt_authenticate_memory(cipher_idx, key, 16, IV, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1177. fprintf(stderr, "\nOCB error... %s\n", error_to_string(err));
  1178. exit(EXIT_FAILURE);
  1179. }
  1180. t1 = t_read() - t1;
  1181. if (t1 < t2) t2 = t1;
  1182. }
  1183. fprintf(stderr, "OCB \t\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  1184. #endif
  1185. #ifdef CCM_MODE
  1186. t2 = -1;
  1187. for (x = 0; x < 10000; x++) {
  1188. t_start();
  1189. t1 = t_read();
  1190. z = 16;
  1191. if ((err = ccm_memory(cipher_idx, key, 16, NULL, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  1192. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  1193. exit(EXIT_FAILURE);
  1194. }
  1195. t1 = t_read() - t1;
  1196. if (t1 < t2) t2 = t1;
  1197. }
  1198. fprintf(stderr, "CCM (no-precomp) \t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  1199. cipher_descriptor[cipher_idx].setup(key, 16, 0, &skey);
  1200. t2 = -1;
  1201. for (x = 0; x < 10000; x++) {
  1202. t_start();
  1203. t1 = t_read();
  1204. z = 16;
  1205. if ((err = ccm_memory(cipher_idx, key, 16, &skey, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  1206. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  1207. exit(EXIT_FAILURE);
  1208. }
  1209. t1 = t_read() - t1;
  1210. if (t1 < t2) t2 = t1;
  1211. }
  1212. fprintf(stderr, "CCM (precomp) \t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  1213. cipher_descriptor[cipher_idx].done(&skey);
  1214. #endif
  1215. #ifdef GCM_MODE
  1216. t2 = -1;
  1217. for (x = 0; x < 100; x++) {
  1218. t_start();
  1219. t1 = t_read();
  1220. z = 16;
  1221. if ((err = gcm_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, GCM_ENCRYPT)) != CRYPT_OK) {
  1222. fprintf(stderr, "\nGCM error... %s\n", error_to_string(err));
  1223. exit(EXIT_FAILURE);
  1224. }
  1225. t1 = t_read() - t1;
  1226. if (t1 < t2) t2 = t1;
  1227. }
  1228. fprintf(stderr, "GCM (no-precomp)\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  1229. {
  1230. gcm_state gcm
  1231. #ifdef GCM_TABLES_SSE2
  1232. __attribute__ ((aligned (16)))
  1233. #endif
  1234. ;
  1235. if ((err = gcm_init(&gcm, cipher_idx, key, 16)) != CRYPT_OK) { fprintf(stderr, "gcm_init: %s\n", error_to_string(err)); exit(EXIT_FAILURE); }
  1236. t2 = -1;
  1237. for (x = 0; x < 10000; x++) {
  1238. t_start();
  1239. t1 = t_read();
  1240. z = 16;
  1241. if ((err = gcm_reset(&gcm)) != CRYPT_OK) {
  1242. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1243. exit(EXIT_FAILURE);
  1244. }
  1245. if ((err = gcm_add_iv(&gcm, IV, 16)) != CRYPT_OK) {
  1246. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1247. exit(EXIT_FAILURE);
  1248. }
  1249. if ((err = gcm_add_aad(&gcm, NULL, 0)) != CRYPT_OK) {
  1250. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1251. exit(EXIT_FAILURE);
  1252. }
  1253. if ((err = gcm_process(&gcm, buf, MAC_SIZE*1024, buf, GCM_ENCRYPT)) != CRYPT_OK) {
  1254. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1255. exit(EXIT_FAILURE);
  1256. }
  1257. if ((err = gcm_done(&gcm, tag, &z)) != CRYPT_OK) {
  1258. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1259. exit(EXIT_FAILURE);
  1260. }
  1261. t1 = t_read() - t1;
  1262. if (t1 < t2) t2 = t1;
  1263. }
  1264. fprintf(stderr, "GCM (precomp)\t\t%9llu\n", t2/(ulong64)(MAC_SIZE*1024));
  1265. }
  1266. #endif
  1267. }
  1268. void time_encmacs(void)
  1269. {
  1270. time_encmacs_(1);
  1271. time_encmacs_(4);
  1272. time_encmacs_(32);
  1273. }
  1274. /* $Source: /cvs/libtom/libtomcrypt/testprof/x86_prof.c,v $ */
  1275. /* $Revision: 1.51 $ */
  1276. /* $Date: 2006/11/21 00:10:18 $ */