timing.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. #include <tomcrypt.h>
  10. #if defined(_WIN32)
  11. #define PRI64 "I64d"
  12. #else
  13. #define PRI64 "ll"
  14. #endif
  15. static prng_state yarrow_prng;
  16. /* timing */
  17. #define KTIMES 25
  18. #define TIMES 100000
  19. static struct list {
  20. int id;
  21. ulong64 spd1, spd2, avg;
  22. } results[100];
  23. static int no_results;
  24. static int sorter(const void *a, const void *b)
  25. {
  26. const struct list *A, *B;
  27. A = a;
  28. B = b;
  29. if (A->avg < B->avg) return -1;
  30. if (A->avg > B->avg) return 1;
  31. return 0;
  32. }
  33. static void tally_results(int type)
  34. {
  35. int x;
  36. /* qsort the results */
  37. qsort(results, no_results, sizeof(struct list), &sorter);
  38. fprintf(stderr, "\n");
  39. if (type == 0) {
  40. for (x = 0; x < no_results; x++) {
  41. fprintf(stderr, "%-20s: Schedule at %6lu\n", cipher_descriptor[results[x].id].name, (unsigned long)results[x].spd1);
  42. }
  43. } else if (type == 1) {
  44. for (x = 0; x < no_results; x++) {
  45. printf
  46. ("%-20s[%3d]: Encrypt at %5"PRI64"u, Decrypt at %5"PRI64"u\n", cipher_descriptor[results[x].id].name, cipher_descriptor[results[x].id].ID, results[x].spd1, results[x].spd2);
  47. }
  48. } else {
  49. for (x = 0; x < no_results; x++) {
  50. printf
  51. ("%-20s: Process at %5"PRI64"u\n", hash_descriptor[results[x].id].name, results[x].spd1 / 1000);
  52. }
  53. }
  54. }
  55. /* RDTSC from Scott Duplichan */
  56. static ulong64 rdtsc (void)
  57. {
  58. #if defined __GNUC__ && !defined(LTC_NO_ASM)
  59. #if defined(__i386__) || defined(__x86_64__)
  60. /* version from http://www.mcs.anl.gov/~kazutomo/rdtsc.html
  61. * the old code always got a warning issued by gcc, clang did not complain...
  62. */
  63. unsigned hi, lo;
  64. __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
  65. return ((ulong64)lo)|( ((ulong64)hi)<<32);
  66. #elif defined(LTC_PPC32) || defined(TFM_PPC32)
  67. unsigned long a, b;
  68. __asm__ __volatile__ ("mftbu %1 \nmftb %0\n":"=r"(a), "=r"(b));
  69. return (((ulong64)b) << 32ULL) | ((ulong64)a);
  70. #elif defined(__ia64__) /* gcc-IA64 version */
  71. unsigned long result;
  72. __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
  73. while (__builtin_expect ((int) result == -1, 0))
  74. __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
  75. return result;
  76. #elif defined(__sparc__)
  77. #if defined(__arch64__)
  78. ulong64 a;
  79. asm volatile("rd %%tick,%0" : "=r" (a));
  80. return a;
  81. #else
  82. register unsigned long x, y;
  83. __asm__ __volatile__ ("rd %%tick, %0; clruw %0, %1; srlx %0, 32, %0" : "=r" (x), "=r" (y) : "0" (x), "1" (y));
  84. return ((unsigned long long) x << 32) | y;
  85. #endif
  86. #else
  87. return XCLOCK();
  88. #endif
  89. /* Microsoft and Intel Windows compilers */
  90. #elif defined _M_IX86 && !defined(LTC_NO_ASM)
  91. __asm rdtsc
  92. #elif defined _M_AMD64 && !defined(LTC_NO_ASM)
  93. return __rdtsc ();
  94. #elif defined _M_IA64 && !defined(LTC_NO_ASM)
  95. #if defined __INTEL_COMPILER
  96. #include <ia64intrin.h>
  97. #endif
  98. return __getReg (3116);
  99. #else
  100. return XCLOCK();
  101. #endif
  102. }
  103. static ulong64 timer, skew = 0;
  104. static void t_start(void)
  105. {
  106. timer = rdtsc();
  107. }
  108. static ulong64 t_read(void)
  109. {
  110. return rdtsc() - timer;
  111. }
  112. static void init_timer(void)
  113. {
  114. ulong64 c1, c2, t1, t2;
  115. unsigned long y1;
  116. c1 = c2 = (ulong64)-1;
  117. for (y1 = 0; y1 < TIMES*100; y1++) {
  118. t_start();
  119. t1 = t_read();
  120. t2 = (t_read() - t1)>>1;
  121. c1 = (t1 > c1) ? t1 : c1;
  122. c2 = (t2 > c2) ? t2 : c2;
  123. }
  124. skew = c2 - c1;
  125. fprintf(stderr, "Clock Skew: %lu\n", (unsigned long)skew);
  126. }
  127. static void time_keysched(void)
  128. {
  129. unsigned long x, y1;
  130. ulong64 t1, c1;
  131. symmetric_key skey;
  132. int kl;
  133. int (*func) (const unsigned char *, int , int , symmetric_key *);
  134. unsigned char key[MAXBLOCKSIZE];
  135. fprintf(stderr, "\n\nKey Schedule Time Trials for the Symmetric Ciphers:\n(Times are cycles per key)\n");
  136. no_results = 0;
  137. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  138. #define DO1(k) func(k, kl, 0, &skey);
  139. func = cipher_descriptor[x].setup;
  140. kl = cipher_descriptor[x].min_key_length;
  141. c1 = (ulong64)-1;
  142. for (y1 = 0; y1 < KTIMES; y1++) {
  143. yarrow_read(key, kl, &yarrow_prng);
  144. t_start();
  145. DO1(key);
  146. t1 = t_read();
  147. c1 = (t1 > c1) ? c1 : t1;
  148. }
  149. t1 = c1 - skew;
  150. results[no_results].spd1 = results[no_results].avg = t1;
  151. results[no_results++].id = x;
  152. fprintf(stderr, "."); fflush(stdout);
  153. #undef DO1
  154. }
  155. tally_results(0);
  156. }
  157. #ifdef LTC_ECB_MODE
  158. static void time_cipher_ecb(void)
  159. {
  160. unsigned long x, y1;
  161. ulong64 t1, t2, c1, c2, a1, a2;
  162. symmetric_ECB ecb;
  163. unsigned char key[MAXBLOCKSIZE] = { 0 }, pt[4096] = { 0 };
  164. int err;
  165. fprintf(stderr, "\n\nECB Time Trials for the Symmetric Ciphers:\n");
  166. no_results = 0;
  167. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  168. ecb_start(x, key, cipher_descriptor[x].min_key_length, 0, &ecb);
  169. /* sanity check on cipher */
  170. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  171. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  172. exit(EXIT_FAILURE);
  173. }
  174. #define DO1 ecb_encrypt(pt, pt, sizeof(pt), &ecb);
  175. #define DO2 DO1 DO1
  176. c1 = c2 = (ulong64)-1;
  177. for (y1 = 0; y1 < 100; y1++) {
  178. t_start();
  179. DO1;
  180. t1 = t_read();
  181. DO2;
  182. t2 = t_read();
  183. t2 -= t1;
  184. c1 = (t1 > c1 ? c1 : t1);
  185. c2 = (t2 > c2 ? c2 : t2);
  186. }
  187. a1 = c2 - c1 - skew;
  188. #undef DO1
  189. #undef DO2
  190. #define DO1 ecb_decrypt(pt, pt, sizeof(pt), &ecb);
  191. #define DO2 DO1 DO1
  192. c1 = c2 = (ulong64)-1;
  193. for (y1 = 0; y1 < 100; y1++) {
  194. t_start();
  195. DO1;
  196. t1 = t_read();
  197. DO2;
  198. t2 = t_read();
  199. t2 -= t1;
  200. c1 = (t1 > c1 ? c1 : t1);
  201. c2 = (t2 > c2 ? c2 : t2);
  202. }
  203. a2 = c2 - c1 - skew;
  204. ecb_done(&ecb);
  205. results[no_results].id = x;
  206. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  207. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  208. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  209. ++no_results;
  210. fprintf(stderr, "."); fflush(stdout);
  211. #undef DO2
  212. #undef DO1
  213. }
  214. tally_results(1);
  215. }
  216. #else
  217. static void time_cipher_ecb(void) { fprintf(stderr, "NO ECB\n"); return 0; }
  218. #endif
  219. #ifdef LTC_CBC_MODE
  220. static void time_cipher_cbc(void)
  221. {
  222. unsigned long x, y1;
  223. ulong64 t1, t2, c1, c2, a1, a2;
  224. symmetric_CBC cbc;
  225. unsigned char key[MAXBLOCKSIZE] = { 0 }, pt[4096] = { 0 };
  226. int err;
  227. fprintf(stderr, "\n\nCBC Time Trials for the Symmetric Ciphers:\n");
  228. no_results = 0;
  229. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  230. cbc_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, &cbc);
  231. /* sanity check on cipher */
  232. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  233. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  234. exit(EXIT_FAILURE);
  235. }
  236. #define DO1 cbc_encrypt(pt, pt, sizeof(pt), &cbc);
  237. #define DO2 DO1 DO1
  238. c1 = c2 = (ulong64)-1;
  239. for (y1 = 0; y1 < 100; y1++) {
  240. t_start();
  241. DO1;
  242. t1 = t_read();
  243. DO2;
  244. t2 = t_read();
  245. t2 -= t1;
  246. c1 = (t1 > c1 ? c1 : t1);
  247. c2 = (t2 > c2 ? c2 : t2);
  248. }
  249. a1 = c2 - c1 - skew;
  250. #undef DO1
  251. #undef DO2
  252. #define DO1 cbc_decrypt(pt, pt, sizeof(pt), &cbc);
  253. #define DO2 DO1 DO1
  254. c1 = c2 = (ulong64)-1;
  255. for (y1 = 0; y1 < 100; y1++) {
  256. t_start();
  257. DO1;
  258. t1 = t_read();
  259. DO2;
  260. t2 = t_read();
  261. t2 -= t1;
  262. c1 = (t1 > c1 ? c1 : t1);
  263. c2 = (t2 > c2 ? c2 : t2);
  264. }
  265. a2 = c2 - c1 - skew;
  266. cbc_done(&cbc);
  267. results[no_results].id = x;
  268. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  269. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  270. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  271. ++no_results;
  272. fprintf(stderr, "."); fflush(stdout);
  273. #undef DO2
  274. #undef DO1
  275. }
  276. tally_results(1);
  277. }
  278. #else
  279. static void time_cipher_cbc(void) { fprintf(stderr, "NO CBC\n"); return 0; }
  280. #endif
  281. #ifdef LTC_CTR_MODE
  282. static void time_cipher_ctr(void)
  283. {
  284. unsigned long x, y1;
  285. ulong64 t1, t2, c1, c2, a1, a2;
  286. symmetric_CTR ctr;
  287. unsigned char key[MAXBLOCKSIZE] = { 0 }, pt[4096] = { 0 };
  288. int err;
  289. fprintf(stderr, "\n\nCTR Time Trials for the Symmetric Ciphers:\n");
  290. no_results = 0;
  291. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  292. ctr_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr);
  293. /* sanity check on cipher */
  294. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  295. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  296. exit(EXIT_FAILURE);
  297. }
  298. #define DO1 ctr_encrypt(pt, pt, sizeof(pt), &ctr);
  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. a1 = c2 - c1 - skew;
  312. #undef DO1
  313. #undef DO2
  314. #define DO1 ctr_decrypt(pt, pt, sizeof(pt), &ctr);
  315. #define DO2 DO1 DO1
  316. c1 = c2 = (ulong64)-1;
  317. for (y1 = 0; y1 < 100; y1++) {
  318. t_start();
  319. DO1;
  320. t1 = t_read();
  321. DO2;
  322. t2 = t_read();
  323. t2 -= t1;
  324. c1 = (t1 > c1 ? c1 : t1);
  325. c2 = (t2 > c2 ? c2 : t2);
  326. }
  327. a2 = c2 - c1 - skew;
  328. ctr_done(&ctr);
  329. results[no_results].id = x;
  330. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  331. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  332. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  333. ++no_results;
  334. fprintf(stderr, "."); fflush(stdout);
  335. #undef DO2
  336. #undef DO1
  337. }
  338. tally_results(1);
  339. }
  340. #else
  341. static void time_cipher_ctr(void) { fprintf(stderr, "NO CTR\n"); return 0; }
  342. #endif
  343. #ifdef LTC_LRW_MODE
  344. static void time_cipher_lrw(void)
  345. {
  346. unsigned long x, y1;
  347. ulong64 t1, t2, c1, c2, a1, a2;
  348. symmetric_LRW lrw;
  349. unsigned char key[MAXBLOCKSIZE] = { 0 }, pt[4096] = { 0 };
  350. int err;
  351. fprintf(stderr, "\n\nLRW Time Trials for the Symmetric Ciphers:\n");
  352. no_results = 0;
  353. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  354. if (cipher_descriptor[x].block_length != 16) continue;
  355. lrw_start(x, pt, key, cipher_descriptor[x].min_key_length, key, 0, &lrw);
  356. /* sanity check on cipher */
  357. if ((err = cipher_descriptor[x].test()) != CRYPT_OK) {
  358. fprintf(stderr, "\n\nERROR: Cipher %s failed self-test %s\n", cipher_descriptor[x].name, error_to_string(err));
  359. exit(EXIT_FAILURE);
  360. }
  361. #define DO1 lrw_encrypt(pt, pt, sizeof(pt), &lrw);
  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. a1 = c2 - c1 - skew;
  375. #undef DO1
  376. #undef DO2
  377. #define DO1 lrw_decrypt(pt, pt, sizeof(pt), &lrw);
  378. #define DO2 DO1 DO1
  379. c1 = c2 = (ulong64)-1;
  380. for (y1 = 0; y1 < 100; y1++) {
  381. t_start();
  382. DO1;
  383. t1 = t_read();
  384. DO2;
  385. t2 = t_read();
  386. t2 -= t1;
  387. c1 = (t1 > c1 ? c1 : t1);
  388. c2 = (t2 > c2 ? c2 : t2);
  389. }
  390. a2 = c2 - c1 - skew;
  391. lrw_done(&lrw);
  392. results[no_results].id = x;
  393. results[no_results].spd1 = a1/(sizeof(pt)/cipher_descriptor[x].block_length);
  394. results[no_results].spd2 = a2/(sizeof(pt)/cipher_descriptor[x].block_length);
  395. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  396. ++no_results;
  397. fprintf(stderr, "."); fflush(stdout);
  398. #undef DO2
  399. #undef DO1
  400. }
  401. tally_results(1);
  402. }
  403. #else
  404. static void time_cipher_lrw(void) { fprintf(stderr, "NO LRW\n"); }
  405. #endif
  406. static void time_hash(void)
  407. {
  408. unsigned long x, y1, len;
  409. ulong64 t1, t2, c1, c2;
  410. hash_state md;
  411. int (*func)(hash_state *, const unsigned char *, unsigned long), err;
  412. unsigned char pt[MAXBLOCKSIZE] = { 0 };
  413. fprintf(stderr, "\n\nHASH Time Trials for:\n");
  414. no_results = 0;
  415. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  416. /* sanity check on hash */
  417. if ((err = hash_descriptor[x].test()) != CRYPT_OK) {
  418. fprintf(stderr, "\n\nERROR: Hash %s failed self-test %s\n", hash_descriptor[x].name, error_to_string(err));
  419. exit(EXIT_FAILURE);
  420. }
  421. hash_descriptor[x].init(&md);
  422. #define DO1 func(&md,pt,len);
  423. #define DO2 DO1 DO1
  424. func = hash_descriptor[x].process;
  425. len = hash_descriptor[x].blocksize;
  426. c1 = c2 = (ulong64)-1;
  427. for (y1 = 0; y1 < TIMES; y1++) {
  428. t_start();
  429. DO1;
  430. t1 = t_read();
  431. DO2;
  432. t2 = t_read() - t1;
  433. c1 = (t1 > c1) ? c1 : t1;
  434. c2 = (t2 > c2) ? c2 : t2;
  435. }
  436. t1 = c2 - c1 - skew;
  437. t1 = ((t1 * CONST64(1000))) / ((ulong64)hash_descriptor[x].blocksize);
  438. results[no_results].id = x;
  439. results[no_results].spd1 = results[no_results].avg = t1;
  440. ++no_results;
  441. fprintf(stderr, "."); fflush(stdout);
  442. #undef DO2
  443. #undef DO1
  444. }
  445. tally_results(2);
  446. }
  447. /*#warning you need an mp_rand!!!*/
  448. static void time_mult(void)
  449. {
  450. ulong64 t1, t2;
  451. unsigned long x, y;
  452. void *a, *b, *c;
  453. if (ltc_mp.name == NULL) return;
  454. fprintf(stderr, "Timing Multiplying:\n");
  455. mp_init_multi(&a,&b,&c,NULL);
  456. for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
  457. mp_rand(a, x);
  458. mp_rand(b, x);
  459. #define DO1 mp_mul(a, b, c);
  460. #define DO2 DO1; DO1;
  461. t2 = -1;
  462. for (y = 0; y < TIMES; y++) {
  463. t_start();
  464. t1 = t_read();
  465. DO2;
  466. t1 = (t_read() - t1)>>1;
  467. if (t1 < t2) t2 = t1;
  468. }
  469. fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*MP_DIGIT_BIT, t2);
  470. }
  471. mp_clear_multi(a,b,c,NULL);
  472. #undef DO1
  473. #undef DO2
  474. }
  475. static void time_sqr(void)
  476. {
  477. ulong64 t1, t2;
  478. unsigned long x, y;
  479. void *a, *b;
  480. if (ltc_mp.name == NULL) return;
  481. fprintf(stderr, "Timing Squaring:\n");
  482. mp_init_multi(&a,&b,NULL);
  483. for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
  484. mp_rand(a, x);
  485. #define DO1 mp_sqr(a, b);
  486. #define DO2 DO1; DO1;
  487. t2 = -1;
  488. for (y = 0; y < TIMES; y++) {
  489. t_start();
  490. t1 = t_read();
  491. DO2;
  492. t1 = (t_read() - t1)>>1;
  493. if (t1 < t2) t2 = t1;
  494. }
  495. fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*MP_DIGIT_BIT, t2);
  496. }
  497. mp_clear_multi(a,b,NULL);
  498. #undef DO1
  499. #undef DO2
  500. }
  501. static void time_prng(void)
  502. {
  503. ulong64 t1, t2;
  504. unsigned char buf[4096];
  505. prng_state tprng;
  506. unsigned long x, y;
  507. int err;
  508. fprintf(stderr, "Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n");
  509. for (x = 0; prng_descriptor[x].name != NULL; x++) {
  510. /* sanity check on prng */
  511. if ((err = prng_descriptor[x].test()) != CRYPT_OK) {
  512. fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor[x].name, error_to_string(err));
  513. exit(EXIT_FAILURE);
  514. }
  515. prng_descriptor[x].start(&tprng);
  516. zeromem(buf, 256);
  517. prng_descriptor[x].add_entropy(buf, 256, &tprng);
  518. prng_descriptor[x].ready(&tprng);
  519. t2 = -1;
  520. #define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { fprintf(stderr, "\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
  521. #define DO2 DO1 DO1
  522. for (y = 0; y < 10000; y++) {
  523. t_start();
  524. t1 = t_read();
  525. DO2;
  526. t1 = (t_read() - t1)>>1;
  527. if (t1 < t2) t2 = t1;
  528. }
  529. fprintf(stderr, "%20s: %5"PRI64"u ", prng_descriptor[x].name, t2>>12);
  530. #undef DO2
  531. #undef DO1
  532. #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);
  533. #define DO2 DO1 DO1
  534. for (y = 0; y < 10000; y++) {
  535. t_start();
  536. t1 = t_read();
  537. DO2;
  538. t1 = (t_read() - t1)>>1;
  539. if (t1 < t2) t2 = t1;
  540. }
  541. fprintf(stderr, "%5"PRI64"u\n", t2);
  542. #undef DO2
  543. #undef DO1
  544. }
  545. }
  546. #if defined(LTC_MDSA)
  547. /* time various DSA operations */
  548. static void time_dsa(void)
  549. {
  550. dsa_key key;
  551. ulong64 t1, t2;
  552. unsigned long x, y;
  553. int err;
  554. static const struct {
  555. int group, modulus;
  556. } groups[] = {
  557. { 20, 96 },
  558. { 20, 128 },
  559. { 24, 192 },
  560. { 28, 256 },
  561. #ifndef TFM_DESC
  562. { 32, 512 },
  563. #endif
  564. };
  565. if (ltc_mp.name == NULL) return;
  566. for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
  567. t2 = 0;
  568. for (y = 0; y < 4; y++) {
  569. t_start();
  570. t1 = t_read();
  571. if ((err = dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) {
  572. fprintf(stderr, "\n\ndsa_generate_pqg says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
  573. exit(EXIT_FAILURE);
  574. }
  575. if ((err = dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
  576. 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));
  577. exit(EXIT_FAILURE);
  578. }
  579. t1 = t_read() - t1;
  580. t2 += t1;
  581. #ifdef LTC_PROFILE
  582. t2 <<= 2;
  583. break;
  584. #endif
  585. if (y < 3) {
  586. dsa_free(&key);
  587. }
  588. }
  589. t2 >>= 2;
  590. fprintf(stderr, "DSA-(%lu, %lu) make_key took %15"PRI64"u cycles\n", (unsigned long)groups[x].group*8, (unsigned long)groups[x].modulus*8, t2);
  591. dsa_free(&key);
  592. }
  593. fprintf(stderr, "\n\n");
  594. }
  595. #else
  596. static void time_dsa(void) { fprintf(stderr, "NO DSA\n"); }
  597. #endif
  598. #if defined(LTC_MRSA)
  599. /* time various RSA operations */
  600. static void time_rsa(void)
  601. {
  602. rsa_key key;
  603. ulong64 t1, t2;
  604. unsigned char buf[2][2048] = { 0 };
  605. unsigned long x, y, z, zzz;
  606. int err, zz, stat;
  607. if (ltc_mp.name == NULL) return;
  608. for (x = 1024; x <= 2048; x += 256) {
  609. t2 = 0;
  610. for (y = 0; y < 4; y++) {
  611. t_start();
  612. t1 = t_read();
  613. if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) {
  614. 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));
  615. exit(EXIT_FAILURE);
  616. }
  617. t1 = t_read() - t1;
  618. t2 += t1;
  619. #ifdef LTC_PROFILE
  620. t2 <<= 2;
  621. break;
  622. #endif
  623. if (y < 3) {
  624. rsa_free(&key);
  625. }
  626. }
  627. t2 >>= 2;
  628. fprintf(stderr, "RSA-%lu make_key took %15"PRI64"u cycles\n", x, t2);
  629. t2 = 0;
  630. for (y = 0; y < 16; y++) {
  631. t_start();
  632. t1 = t_read();
  633. z = sizeof(buf[1]);
  634. if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, (const unsigned char *)"testprog", 8, &yarrow_prng,
  635. find_prng("yarrow"), find_hash("sha1"),
  636. &key)) != CRYPT_OK) {
  637. 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));
  638. exit(EXIT_FAILURE);
  639. }
  640. t1 = t_read() - t1;
  641. t2 += t1;
  642. #ifdef LTC_PROFILE
  643. t2 <<= 4;
  644. break;
  645. #endif
  646. }
  647. t2 >>= 4;
  648. fprintf(stderr, "RSA-%lu encrypt_key took %15"PRI64"u cycles\n", x, t2);
  649. t2 = 0;
  650. for (y = 0; y < 2048; y++) {
  651. t_start();
  652. t1 = t_read();
  653. zzz = sizeof(buf[0]);
  654. if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, (const unsigned char *)"testprog", 8, find_hash("sha1"),
  655. &zz, &key)) != CRYPT_OK) {
  656. 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));
  657. exit(EXIT_FAILURE);
  658. }
  659. t1 = t_read() - t1;
  660. t2 += t1;
  661. #ifdef LTC_PROFILE
  662. t2 <<= 11;
  663. break;
  664. #endif
  665. }
  666. t2 >>= 11;
  667. fprintf(stderr, "RSA-%lu decrypt_key took %15"PRI64"u cycles\n", x, t2);
  668. t2 = 0;
  669. for (y = 0; y < 256; y++) {
  670. t_start();
  671. t1 = t_read();
  672. z = sizeof(buf[1]);
  673. if ((err = rsa_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
  674. find_prng("yarrow"), find_hash("sha1"), 8, &key)) != CRYPT_OK) {
  675. 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));
  676. exit(EXIT_FAILURE);
  677. }
  678. t1 = t_read() - t1;
  679. t2 += t1;
  680. #ifdef LTC_PROFILE
  681. t2 <<= 8;
  682. break;
  683. #endif
  684. }
  685. t2 >>= 8;
  686. fprintf(stderr, "RSA-%lu sign_hash took %15"PRI64"u cycles\n", x, t2);
  687. t2 = 0;
  688. for (y = 0; y < 2048; y++) {
  689. t_start();
  690. t1 = t_read();
  691. if ((err = rsa_verify_hash(buf[1], z, buf[0], 20, find_hash("sha1"), 8, &stat, &key)) != CRYPT_OK) {
  692. 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));
  693. exit(EXIT_FAILURE);
  694. }
  695. if (stat == 0) {
  696. fprintf(stderr, "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n", x, y);
  697. exit(EXIT_FAILURE);
  698. }
  699. t1 = t_read() - t1;
  700. t2 += t1;
  701. #ifdef LTC_PROFILE
  702. t2 <<= 11;
  703. break;
  704. #endif
  705. }
  706. t2 >>= 11;
  707. fprintf(stderr, "RSA-%lu verify_hash took %15"PRI64"u cycles\n", x, t2);
  708. fprintf(stderr, "\n\n");
  709. rsa_free(&key);
  710. }
  711. }
  712. #else
  713. static void time_rsa(void) { fprintf(stderr, "NO RSA\n"); }
  714. #endif
  715. #if defined(LTC_MKAT)
  716. /* time various KAT operations */
  717. static void time_katja(void)
  718. {
  719. katja_key key;
  720. ulong64 t1, t2;
  721. unsigned char buf[2][4096];
  722. unsigned long x, y, z, zzz;
  723. int err, zz;
  724. if (ltc_mp.name == NULL) return;
  725. for (x = 1024; x <= 2048; x += 256) {
  726. t2 = 0;
  727. for (y = 0; y < 4; y++) {
  728. t_start();
  729. t1 = t_read();
  730. if ((err = katja_make_key(&yarrow_prng, find_prng("yarrow"), x/8, &key)) != CRYPT_OK) {
  731. 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));
  732. exit(EXIT_FAILURE);
  733. }
  734. t1 = t_read() - t1;
  735. t2 += t1;
  736. if (y < 3) {
  737. katja_free(&key);
  738. }
  739. }
  740. t2 >>= 2;
  741. fprintf(stderr, "Katja-%lu make_key took %15"PRI64"u cycles\n", x, t2);
  742. t2 = 0;
  743. for (y = 0; y < 16; y++) {
  744. t_start();
  745. t1 = t_read();
  746. z = sizeof(buf[1]);
  747. if ((err = katja_encrypt_key(buf[0], 32, buf[1], &z, "testprog", 8, &yarrow_prng,
  748. find_prng("yarrow"), find_hash("sha1"),
  749. &key)) != CRYPT_OK) {
  750. 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));
  751. exit(EXIT_FAILURE);
  752. }
  753. t1 = t_read() - t1;
  754. t2 += t1;
  755. }
  756. t2 >>= 4;
  757. fprintf(stderr, "Katja-%lu encrypt_key took %15"PRI64"u cycles\n", x, t2);
  758. t2 = 0;
  759. for (y = 0; y < 2048; y++) {
  760. t_start();
  761. t1 = t_read();
  762. zzz = sizeof(buf[0]);
  763. if ((err = katja_decrypt_key(buf[1], z, buf[0], &zzz, "testprog", 8, find_hash("sha1"),
  764. &zz, &key)) != CRYPT_OK) {
  765. 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));
  766. exit(EXIT_FAILURE);
  767. }
  768. t1 = t_read() - t1;
  769. t2 += t1;
  770. }
  771. t2 >>= 11;
  772. fprintf(stderr, "Katja-%lu decrypt_key took %15"PRI64"u cycles\n", x, t2);
  773. katja_free(&key);
  774. }
  775. }
  776. #else
  777. static void time_katja(void) { fprintf(stderr, "NO Katja\n"); }
  778. #endif
  779. #if defined(LTC_MDH)
  780. /* time various DH operations */
  781. static void time_dh(void)
  782. {
  783. dh_key key;
  784. ulong64 t1, t2;
  785. unsigned long i, x, y;
  786. int err;
  787. static unsigned long sizes[] = {768/8, 1024/8, 1536/8, 2048/8,
  788. #ifndef TFM_DESC
  789. 3072/8, 4096/8, 6144/8, 8192/8,
  790. #endif
  791. 100000
  792. };
  793. if (ltc_mp.name == NULL) return;
  794. for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
  795. t2 = 0;
  796. for (y = 0; y < 16; y++) {
  797. if((err = dh_set_pg_groupsize(x, &key)) != CRYPT_OK) {
  798. fprintf(stderr, "\n\ndh_set_pg_groupsize says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
  799. exit(EXIT_FAILURE);
  800. }
  801. t_start();
  802. t1 = t_read();
  803. if ((err = dh_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
  804. fprintf(stderr, "\n\ndh_make_key says %s, wait...no it should say %s...damn you!\n", error_to_string(err), error_to_string(CRYPT_OK));
  805. exit(EXIT_FAILURE);
  806. }
  807. t1 = t_read() - t1;
  808. t2 += t1;
  809. dh_free(&key);
  810. }
  811. t2 >>= 4;
  812. fprintf(stderr, "DH-%4lu make_key took %15"PRI64"u cycles\n", x*8, t2);
  813. }
  814. }
  815. #else
  816. static void time_dh(void) { fprintf(stderr, "NO DH\n"); }
  817. #endif
  818. #if defined(LTC_MECC)
  819. /* time various ECC operations */
  820. static void time_ecc(void)
  821. {
  822. ecc_key key;
  823. ulong64 t1, t2;
  824. unsigned char buf[2][256] = { 0 };
  825. unsigned long i, w, x, y, z;
  826. int err, stat;
  827. static unsigned long sizes[] = {
  828. #ifdef LTC_ECC112
  829. 112/8,
  830. #endif
  831. #ifdef LTC_ECC128
  832. 128/8,
  833. #endif
  834. #ifdef LTC_ECC160
  835. 160/8,
  836. #endif
  837. #ifdef LTC_ECC192
  838. 192/8,
  839. #endif
  840. #ifdef LTC_ECC224
  841. 224/8,
  842. #endif
  843. #ifdef LTC_ECC256
  844. 256/8,
  845. #endif
  846. #ifdef LTC_ECC384
  847. 384/8,
  848. #endif
  849. #ifdef LTC_ECC521
  850. 521/8,
  851. #endif
  852. 100000};
  853. if (ltc_mp.name == NULL) return;
  854. for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
  855. t2 = 0;
  856. for (y = 0; y < 256; y++) {
  857. t_start();
  858. t1 = t_read();
  859. if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
  860. 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));
  861. exit(EXIT_FAILURE);
  862. }
  863. t1 = t_read() - t1;
  864. t2 += t1;
  865. #ifdef LTC_PROFILE
  866. t2 <<= 8;
  867. break;
  868. #endif
  869. if (y < 255) {
  870. ecc_free(&key);
  871. }
  872. }
  873. t2 >>= 8;
  874. fprintf(stderr, "ECC-%lu make_key took %15"PRI64"u cycles\n", x*8, t2);
  875. t2 = 0;
  876. for (y = 0; y < 256; y++) {
  877. t_start();
  878. t1 = t_read();
  879. z = sizeof(buf[1]);
  880. if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
  881. &key)) != CRYPT_OK) {
  882. 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));
  883. exit(EXIT_FAILURE);
  884. }
  885. t1 = t_read() - t1;
  886. t2 += t1;
  887. #ifdef LTC_PROFILE
  888. t2 <<= 8;
  889. break;
  890. #endif
  891. }
  892. t2 >>= 8;
  893. fprintf(stderr, "ECC-%lu encrypt_key took %15"PRI64"u cycles\n", x*8, t2);
  894. t2 = 0;
  895. for (y = 0; y < 256; y++) {
  896. t_start();
  897. t1 = t_read();
  898. w = 20;
  899. if ((err = ecc_decrypt_key(buf[1], z, buf[0], &w, &key)) != CRYPT_OK) {
  900. 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));
  901. exit(EXIT_FAILURE);
  902. }
  903. t1 = t_read() - t1;
  904. t2 += t1;
  905. #ifdef LTC_PROFILE
  906. t2 <<= 8;
  907. break;
  908. #endif
  909. }
  910. t2 >>= 8;
  911. fprintf(stderr, "ECC-%lu decrypt_key took %15"PRI64"u cycles\n", x*8, t2);
  912. t2 = 0;
  913. for (y = 0; y < 256; y++) {
  914. t_start();
  915. t1 = t_read();
  916. z = sizeof(buf[1]);
  917. if ((err = ecc_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
  918. find_prng("yarrow"), &key)) != CRYPT_OK) {
  919. 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));
  920. exit(EXIT_FAILURE);
  921. }
  922. t1 = t_read() - t1;
  923. t2 += t1;
  924. #ifdef LTC_PROFILE
  925. t2 <<= 8;
  926. break;
  927. #endif
  928. }
  929. t2 >>= 8;
  930. fprintf(stderr, "ECC-%lu sign_hash took %15"PRI64"u cycles\n", x*8, t2);
  931. t2 = 0;
  932. for (y = 0; y < 256; y++) {
  933. t_start();
  934. t1 = t_read();
  935. if ((err = ecc_verify_hash(buf[1], z, buf[0], 20, &stat, &key)) != CRYPT_OK) {
  936. 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));
  937. exit(EXIT_FAILURE);
  938. }
  939. if (stat == 0) {
  940. fprintf(stderr, "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n", x*8, y);
  941. exit(EXIT_FAILURE);
  942. }
  943. t1 = t_read() - t1;
  944. t2 += t1;
  945. #ifdef LTC_PROFILE
  946. t2 <<= 8;
  947. break;
  948. #endif
  949. }
  950. t2 >>= 8;
  951. fprintf(stderr, "ECC-%lu verify_hash took %15"PRI64"u cycles\n", x*8, t2);
  952. fprintf(stderr, "\n\n");
  953. ecc_free(&key);
  954. }
  955. }
  956. #else
  957. static void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
  958. #endif
  959. static void time_macs_(unsigned long MAC_SIZE)
  960. {
  961. #if defined(LTC_OMAC) || defined(LTC_XCBC) || defined(LTC_F9_MODE) || defined(LTC_PMAC) || defined(LTC_PELICAN) || defined(LTC_HMAC)
  962. unsigned char *buf, key[16], tag[16];
  963. ulong64 t1, t2;
  964. unsigned long x, z;
  965. int err, cipher_idx, hash_idx;
  966. fprintf(stderr, "\nMAC Timings (cycles/byte on %luKB blocks):\n", MAC_SIZE);
  967. buf = XMALLOC(MAC_SIZE*1024);
  968. if (buf == NULL) {
  969. fprintf(stderr, "\n\nout of heap yo\n\n");
  970. exit(EXIT_FAILURE);
  971. }
  972. cipher_idx = find_cipher("aes");
  973. hash_idx = find_hash("sha1");
  974. if (cipher_idx == -1 || hash_idx == -1) {
  975. fprintf(stderr, "Warning the MAC tests requires AES and SHA1 to operate... so sorry\n");
  976. exit(EXIT_FAILURE);
  977. }
  978. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  979. yarrow_read(key, 16, &yarrow_prng);
  980. #ifdef LTC_OMAC
  981. t2 = -1;
  982. for (x = 0; x < 10000; x++) {
  983. t_start();
  984. t1 = t_read();
  985. z = 16;
  986. if ((err = omac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  987. fprintf(stderr, "\n\nomac-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  988. exit(EXIT_FAILURE);
  989. }
  990. t1 = t_read() - t1;
  991. if (t1 < t2) t2 = t1;
  992. }
  993. fprintf(stderr, "OMAC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  994. #endif
  995. #ifdef LTC_XCBC
  996. t2 = -1;
  997. for (x = 0; x < 10000; x++) {
  998. t_start();
  999. t1 = t_read();
  1000. z = 16;
  1001. if ((err = xcbc_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1002. fprintf(stderr, "\n\nxcbc-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  1003. exit(EXIT_FAILURE);
  1004. }
  1005. t1 = t_read() - t1;
  1006. if (t1 < t2) t2 = t1;
  1007. }
  1008. fprintf(stderr, "XCBC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1009. #endif
  1010. #ifdef LTC_F9_MODE
  1011. t2 = -1;
  1012. for (x = 0; x < 10000; x++) {
  1013. t_start();
  1014. t1 = t_read();
  1015. z = 16;
  1016. if ((err = f9_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1017. fprintf(stderr, "\n\nF9-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  1018. exit(EXIT_FAILURE);
  1019. }
  1020. t1 = t_read() - t1;
  1021. if (t1 < t2) t2 = t1;
  1022. }
  1023. fprintf(stderr, "F9-%s\t\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1024. #endif
  1025. #ifdef LTC_PMAC
  1026. t2 = -1;
  1027. for (x = 0; x < 10000; x++) {
  1028. t_start();
  1029. t1 = t_read();
  1030. z = 16;
  1031. if ((err = pmac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1032. fprintf(stderr, "\n\npmac-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  1033. exit(EXIT_FAILURE);
  1034. }
  1035. t1 = t_read() - t1;
  1036. if (t1 < t2) t2 = t1;
  1037. }
  1038. fprintf(stderr, "PMAC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1039. #endif
  1040. #ifdef LTC_PELICAN
  1041. t2 = -1;
  1042. for (x = 0; x < 10000; x++) {
  1043. t_start();
  1044. t1 = t_read();
  1045. z = 16;
  1046. if ((err = pelican_memory(key, 16, buf, MAC_SIZE*1024, tag)) != CRYPT_OK) {
  1047. fprintf(stderr, "\n\npelican 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, "PELICAN \t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1054. #endif
  1055. #ifdef LTC_HMAC
  1056. t2 = -1;
  1057. for (x = 0; x < 10000; x++) {
  1058. t_start();
  1059. t1 = t_read();
  1060. z = 16;
  1061. if ((err = hmac_memory(hash_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1062. fprintf(stderr, "\n\nhmac-%s error... %s\n", hash_descriptor[hash_idx].name, error_to_string(err));
  1063. exit(EXIT_FAILURE);
  1064. }
  1065. t1 = t_read() - t1;
  1066. if (t1 < t2) t2 = t1;
  1067. }
  1068. fprintf(stderr, "HMAC-%s\t\t%9"PRI64"u\n", hash_descriptor[hash_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1069. #endif
  1070. XFREE(buf);
  1071. #else
  1072. LTC_UNUSED_PARAM(MAC_SIZE);
  1073. fprintf(stderr, "NO MACs\n");
  1074. #endif
  1075. }
  1076. static void time_macs(void)
  1077. {
  1078. time_macs_(1);
  1079. time_macs_(4);
  1080. time_macs_(32);
  1081. }
  1082. static void time_encmacs_(unsigned long MAC_SIZE)
  1083. {
  1084. #if defined(LTC_EAX_MODE) || defined(LTC_OCB_MODE) || defined(LTC_OCB3_MODE) || defined(LTC_CCM_MODE) || defined(LTC_GCM_MODE)
  1085. unsigned char *buf, IV[16], key[16], tag[16];
  1086. ulong64 t1, t2;
  1087. unsigned long x, z;
  1088. int err, cipher_idx;
  1089. symmetric_key skey;
  1090. fprintf(stderr, "\nENC+MAC Timings (zero byte AAD, 16 byte IV, cycles/byte on %luKB blocks):\n", MAC_SIZE);
  1091. buf = XMALLOC(MAC_SIZE*1024);
  1092. if (buf == NULL) {
  1093. fprintf(stderr, "\n\nout of heap yo\n\n");
  1094. exit(EXIT_FAILURE);
  1095. }
  1096. cipher_idx = find_cipher("aes");
  1097. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  1098. yarrow_read(key, 16, &yarrow_prng);
  1099. yarrow_read(IV, 16, &yarrow_prng);
  1100. #ifdef LTC_EAX_MODE
  1101. t2 = -1;
  1102. for (x = 0; x < 10000; x++) {
  1103. t_start();
  1104. t1 = t_read();
  1105. z = 16;
  1106. if ((err = eax_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1107. fprintf(stderr, "\nEAX 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, "EAX \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1114. #endif
  1115. #ifdef LTC_OCB_MODE
  1116. t2 = -1;
  1117. for (x = 0; x < 10000; x++) {
  1118. t_start();
  1119. t1 = t_read();
  1120. z = 16;
  1121. if ((err = ocb_encrypt_authenticate_memory(cipher_idx, key, 16, IV, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1122. fprintf(stderr, "\nOCB 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, "OCB \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1129. #endif
  1130. #ifdef LTC_OCB3_MODE
  1131. t2 = -1;
  1132. for (x = 0; x < 10000; x++) {
  1133. t_start();
  1134. t1 = t_read();
  1135. z = 16;
  1136. if ((err = ocb3_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 15, (unsigned char*)"", 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1137. fprintf(stderr, "\nOCB3 error... %s\n", error_to_string(err));
  1138. exit(EXIT_FAILURE);
  1139. }
  1140. t1 = t_read() - t1;
  1141. if (t1 < t2) t2 = t1;
  1142. }
  1143. fprintf(stderr, "OCB3 \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1144. #endif
  1145. #ifdef LTC_CCM_MODE
  1146. t2 = -1;
  1147. for (x = 0; x < 10000; x++) {
  1148. t_start();
  1149. t1 = t_read();
  1150. z = 16;
  1151. if ((err = ccm_memory(cipher_idx, key, 16, NULL, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  1152. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  1153. exit(EXIT_FAILURE);
  1154. }
  1155. t1 = t_read() - t1;
  1156. if (t1 < t2) t2 = t1;
  1157. }
  1158. fprintf(stderr, "CCM (no-precomp) \t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1159. cipher_descriptor[cipher_idx].setup(key, 16, 0, &skey);
  1160. t2 = -1;
  1161. for (x = 0; x < 10000; x++) {
  1162. t_start();
  1163. t1 = t_read();
  1164. z = 16;
  1165. if ((err = ccm_memory(cipher_idx, key, 16, &skey, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  1166. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  1167. exit(EXIT_FAILURE);
  1168. }
  1169. t1 = t_read() - t1;
  1170. if (t1 < t2) t2 = t1;
  1171. }
  1172. fprintf(stderr, "CCM (precomp) \t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1173. cipher_descriptor[cipher_idx].done(&skey);
  1174. #endif
  1175. #ifdef LTC_GCM_MODE
  1176. t2 = -1;
  1177. for (x = 0; x < 100; x++) {
  1178. t_start();
  1179. t1 = t_read();
  1180. z = 16;
  1181. if ((err = gcm_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, GCM_ENCRYPT)) != CRYPT_OK) {
  1182. fprintf(stderr, "\nGCM error... %s\n", error_to_string(err));
  1183. exit(EXIT_FAILURE);
  1184. }
  1185. t1 = t_read() - t1;
  1186. if (t1 < t2) t2 = t1;
  1187. }
  1188. fprintf(stderr, "GCM (no-precomp)\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1189. {
  1190. gcm_state gcm
  1191. #ifdef LTC_GCM_TABLES_SSE2
  1192. __attribute__ ((aligned (16)))
  1193. #endif
  1194. ;
  1195. if ((err = gcm_init(&gcm, cipher_idx, key, 16)) != CRYPT_OK) { fprintf(stderr, "gcm_init: %s\n", error_to_string(err)); exit(EXIT_FAILURE); }
  1196. t2 = -1;
  1197. for (x = 0; x < 10000; x++) {
  1198. t_start();
  1199. t1 = t_read();
  1200. z = 16;
  1201. if ((err = gcm_reset(&gcm)) != CRYPT_OK) {
  1202. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1203. exit(EXIT_FAILURE);
  1204. }
  1205. if ((err = gcm_add_iv(&gcm, IV, 16)) != CRYPT_OK) {
  1206. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1207. exit(EXIT_FAILURE);
  1208. }
  1209. if ((err = gcm_add_aad(&gcm, NULL, 0)) != CRYPT_OK) {
  1210. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1211. exit(EXIT_FAILURE);
  1212. }
  1213. if ((err = gcm_process(&gcm, buf, MAC_SIZE*1024, buf, GCM_ENCRYPT)) != CRYPT_OK) {
  1214. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1215. exit(EXIT_FAILURE);
  1216. }
  1217. if ((err = gcm_done(&gcm, tag, &z)) != CRYPT_OK) {
  1218. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1219. exit(EXIT_FAILURE);
  1220. }
  1221. t1 = t_read() - t1;
  1222. if (t1 < t2) t2 = t1;
  1223. }
  1224. fprintf(stderr, "GCM (precomp)\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1225. }
  1226. #endif
  1227. XFREE(buf);
  1228. #else
  1229. LTC_UNUSED_PARAM(MAC_SIZE);
  1230. fprintf(stderr, "NO ENCMACs\n");
  1231. #endif
  1232. }
  1233. static void time_encmacs(void)
  1234. {
  1235. time_encmacs_(1);
  1236. time_encmacs_(4);
  1237. time_encmacs_(32);
  1238. }
  1239. #define LTC_TEST_FN(f) { f, #f }
  1240. int main(int argc, char **argv)
  1241. {
  1242. int err;
  1243. const struct
  1244. {
  1245. void (*fn)(void);
  1246. const char* name;
  1247. } test_functions[] = {
  1248. LTC_TEST_FN(time_keysched),
  1249. LTC_TEST_FN(time_cipher_ecb),
  1250. LTC_TEST_FN(time_cipher_cbc),
  1251. LTC_TEST_FN(time_cipher_ctr),
  1252. LTC_TEST_FN(time_cipher_lrw),
  1253. LTC_TEST_FN(time_hash),
  1254. LTC_TEST_FN(time_macs),
  1255. LTC_TEST_FN(time_encmacs),
  1256. LTC_TEST_FN(time_prng),
  1257. LTC_TEST_FN(time_mult),
  1258. LTC_TEST_FN(time_sqr),
  1259. LTC_TEST_FN(time_rsa),
  1260. LTC_TEST_FN(time_dsa),
  1261. LTC_TEST_FN(time_ecc),
  1262. LTC_TEST_FN(time_dh),
  1263. LTC_TEST_FN(time_katja)
  1264. };
  1265. char *single_test = NULL;
  1266. unsigned int i;
  1267. const char* mpi_provider = NULL;
  1268. init_timer();
  1269. register_all_ciphers();
  1270. register_all_hashes();
  1271. register_all_prngs();
  1272. #ifdef USE_LTM
  1273. mpi_provider = "ltm";
  1274. #elif defined(USE_TFM)
  1275. mpi_provider = "tfm";
  1276. #elif defined(USE_GMP)
  1277. mpi_provider = "gmp";
  1278. #elif defined(EXT_MATH_LIB)
  1279. mpi_provider = "ext";
  1280. #endif
  1281. if (argc > 2) {
  1282. mpi_provider = argv[2];
  1283. }
  1284. crypt_mp_init(mpi_provider);
  1285. if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
  1286. fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
  1287. exit(EXIT_FAILURE);
  1288. }
  1289. /* single test name from commandline */
  1290. if (argc > 1) single_test = argv[1];
  1291. for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
  1292. if (single_test && strstr(test_functions[i].name, single_test) == NULL) {
  1293. continue;
  1294. }
  1295. test_functions[i].fn();
  1296. }
  1297. return EXIT_SUCCESS;
  1298. }
  1299. /* ref: $Format:%D$ */
  1300. /* git commit: $Format:%H$ */
  1301. /* commit time: $Format:%ai$ */