x86_prof.c 38 KB

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