timing.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  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"); return 0; }
  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. #if !defined(USE_LTM) && !defined(USE_TFM) && !defined(USE_GMP) && !defined(EXT_MATH_LIB)
  449. #undef LTC_MPI
  450. #undef LTC_TEST_MPI
  451. #else
  452. #define LTC_TEST_MPI
  453. #endif
  454. #ifdef LTC_MPI
  455. static void time_mult(void)
  456. {
  457. ulong64 t1, t2;
  458. unsigned long x, y;
  459. void *a, *b, *c;
  460. fprintf(stderr, "Timing Multiplying:\n");
  461. mp_init_multi(&a,&b,&c,NULL);
  462. for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
  463. mp_rand(a, x);
  464. mp_rand(b, x);
  465. #define DO1 mp_mul(a, b, c);
  466. #define DO2 DO1; DO1;
  467. t2 = -1;
  468. for (y = 0; y < TIMES; y++) {
  469. t_start();
  470. t1 = t_read();
  471. DO2;
  472. t1 = (t_read() - t1)>>1;
  473. if (t1 < t2) t2 = t1;
  474. }
  475. fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*MP_DIGIT_BIT, t2);
  476. }
  477. mp_clear_multi(a,b,c,NULL);
  478. #undef DO1
  479. #undef DO2
  480. }
  481. static void time_sqr(void)
  482. {
  483. ulong64 t1, t2;
  484. unsigned long x, y;
  485. void *a, *b;
  486. fprintf(stderr, "Timing Squaring:\n");
  487. mp_init_multi(&a,&b,NULL);
  488. for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) {
  489. mp_rand(a, x);
  490. #define DO1 mp_sqr(a, b);
  491. #define DO2 DO1; DO1;
  492. t2 = -1;
  493. for (y = 0; y < TIMES; y++) {
  494. t_start();
  495. t1 = t_read();
  496. DO2;
  497. t1 = (t_read() - t1)>>1;
  498. if (t1 < t2) t2 = t1;
  499. }
  500. fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*MP_DIGIT_BIT, t2);
  501. }
  502. mp_clear_multi(a,b,NULL);
  503. #undef DO1
  504. #undef DO2
  505. }
  506. #else
  507. static void time_mult(void) { fprintf(stderr, "NO MULT\n"); }
  508. static void time_sqr(void) { fprintf(stderr, "NO SQR\n"); }
  509. #endif
  510. static void time_prng(void)
  511. {
  512. ulong64 t1, t2;
  513. unsigned char buf[4096];
  514. prng_state tprng;
  515. unsigned long x, y;
  516. int err;
  517. fprintf(stderr, "Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n");
  518. for (x = 0; prng_descriptor[x].name != NULL; x++) {
  519. /* sanity check on prng */
  520. if ((err = prng_descriptor[x].test()) != CRYPT_OK) {
  521. fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor[x].name, error_to_string(err));
  522. exit(EXIT_FAILURE);
  523. }
  524. prng_descriptor[x].start(&tprng);
  525. zeromem(buf, 256);
  526. prng_descriptor[x].add_entropy(buf, 256, &tprng);
  527. prng_descriptor[x].ready(&tprng);
  528. t2 = -1;
  529. #define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { fprintf(stderr, "\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
  530. #define DO2 DO1 DO1
  531. for (y = 0; y < 10000; y++) {
  532. t_start();
  533. t1 = t_read();
  534. DO2;
  535. t1 = (t_read() - t1)>>1;
  536. if (t1 < t2) t2 = t1;
  537. }
  538. fprintf(stderr, "%20s: %5"PRI64"u ", prng_descriptor[x].name, t2>>12);
  539. #undef DO2
  540. #undef DO1
  541. #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);
  542. #define DO2 DO1 DO1
  543. for (y = 0; y < 10000; y++) {
  544. t_start();
  545. t1 = t_read();
  546. DO2;
  547. t1 = (t_read() - t1)>>1;
  548. if (t1 < t2) t2 = t1;
  549. }
  550. fprintf(stderr, "%5"PRI64"u\n", t2);
  551. #undef DO2
  552. #undef DO1
  553. }
  554. }
  555. #if defined(LTC_MDSA) && defined(LTC_TEST_MPI)
  556. /* time various DSA operations */
  557. static void time_dsa(void)
  558. {
  559. dsa_key key;
  560. ulong64 t1, t2;
  561. unsigned long x, y;
  562. int err;
  563. static const struct {
  564. int group, modulus;
  565. } groups[] = {
  566. { 20, 96 },
  567. { 20, 128 },
  568. { 24, 192 },
  569. { 28, 256 },
  570. #ifndef TFM_DESC
  571. { 32, 512 },
  572. #endif
  573. };
  574. for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
  575. t2 = 0;
  576. for (y = 0; y < 4; y++) {
  577. t_start();
  578. t1 = t_read();
  579. if ((err = dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) {
  580. 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));
  581. exit(EXIT_FAILURE);
  582. }
  583. if ((err = dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
  584. 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));
  585. exit(EXIT_FAILURE);
  586. }
  587. t1 = t_read() - t1;
  588. t2 += t1;
  589. #ifdef LTC_PROFILE
  590. t2 <<= 2;
  591. break;
  592. #endif
  593. if (y < 3) {
  594. dsa_free(&key);
  595. }
  596. }
  597. t2 >>= 2;
  598. 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);
  599. dsa_free(&key);
  600. }
  601. fprintf(stderr, "\n\n");
  602. }
  603. #else
  604. static void time_dsa(void) { fprintf(stderr, "NO DSA\n"); }
  605. #endif
  606. #if defined(LTC_MRSA) && defined(LTC_TEST_MPI)
  607. /* time various RSA operations */
  608. static void time_rsa(void)
  609. {
  610. rsa_key key;
  611. ulong64 t1, t2;
  612. unsigned char buf[2][2048] = { 0 };
  613. unsigned long x, y, z, zzz;
  614. int err, zz, stat;
  615. for (x = 1024; x <= 2048; x += 256) {
  616. t2 = 0;
  617. for (y = 0; y < 4; y++) {
  618. t_start();
  619. t1 = t_read();
  620. if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) {
  621. 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));
  622. exit(EXIT_FAILURE);
  623. }
  624. t1 = t_read() - t1;
  625. t2 += t1;
  626. #ifdef LTC_PROFILE
  627. t2 <<= 2;
  628. break;
  629. #endif
  630. if (y < 3) {
  631. rsa_free(&key);
  632. }
  633. }
  634. t2 >>= 2;
  635. fprintf(stderr, "RSA-%lu make_key took %15"PRI64"u cycles\n", x, t2);
  636. t2 = 0;
  637. for (y = 0; y < 16; y++) {
  638. t_start();
  639. t1 = t_read();
  640. z = sizeof(buf[1]);
  641. if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, (const unsigned char *)"testprog", 8, &yarrow_prng,
  642. find_prng("yarrow"), find_hash("sha1"),
  643. &key)) != CRYPT_OK) {
  644. 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));
  645. exit(EXIT_FAILURE);
  646. }
  647. t1 = t_read() - t1;
  648. t2 += t1;
  649. #ifdef LTC_PROFILE
  650. t2 <<= 4;
  651. break;
  652. #endif
  653. }
  654. t2 >>= 4;
  655. fprintf(stderr, "RSA-%lu encrypt_key took %15"PRI64"u cycles\n", x, t2);
  656. t2 = 0;
  657. for (y = 0; y < 2048; y++) {
  658. t_start();
  659. t1 = t_read();
  660. zzz = sizeof(buf[0]);
  661. if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, (const unsigned char *)"testprog", 8, find_hash("sha1"),
  662. &zz, &key)) != CRYPT_OK) {
  663. 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));
  664. exit(EXIT_FAILURE);
  665. }
  666. t1 = t_read() - t1;
  667. t2 += t1;
  668. #ifdef LTC_PROFILE
  669. t2 <<= 11;
  670. break;
  671. #endif
  672. }
  673. t2 >>= 11;
  674. fprintf(stderr, "RSA-%lu decrypt_key took %15"PRI64"u cycles\n", x, t2);
  675. t2 = 0;
  676. for (y = 0; y < 256; y++) {
  677. t_start();
  678. t1 = t_read();
  679. z = sizeof(buf[1]);
  680. if ((err = rsa_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
  681. find_prng("yarrow"), find_hash("sha1"), 8, &key)) != CRYPT_OK) {
  682. 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));
  683. exit(EXIT_FAILURE);
  684. }
  685. t1 = t_read() - t1;
  686. t2 += t1;
  687. #ifdef LTC_PROFILE
  688. t2 <<= 8;
  689. break;
  690. #endif
  691. }
  692. t2 >>= 8;
  693. fprintf(stderr, "RSA-%lu sign_hash took %15"PRI64"u cycles\n", x, t2);
  694. t2 = 0;
  695. for (y = 0; y < 2048; y++) {
  696. t_start();
  697. t1 = t_read();
  698. if ((err = rsa_verify_hash(buf[1], z, buf[0], 20, find_hash("sha1"), 8, &stat, &key)) != CRYPT_OK) {
  699. 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));
  700. exit(EXIT_FAILURE);
  701. }
  702. if (stat == 0) {
  703. fprintf(stderr, "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n", x, y);
  704. exit(EXIT_FAILURE);
  705. }
  706. t1 = t_read() - t1;
  707. t2 += t1;
  708. #ifdef LTC_PROFILE
  709. t2 <<= 11;
  710. break;
  711. #endif
  712. }
  713. t2 >>= 11;
  714. fprintf(stderr, "RSA-%lu verify_hash took %15"PRI64"u cycles\n", x, t2);
  715. fprintf(stderr, "\n\n");
  716. rsa_free(&key);
  717. }
  718. }
  719. #else
  720. static void time_rsa(void) { fprintf(stderr, "NO RSA\n"); }
  721. #endif
  722. #if defined(LTC_MKAT) && defined(LTC_TEST_MPI)
  723. /* time various KAT operations */
  724. static void time_katja(void)
  725. {
  726. katja_key key;
  727. ulong64 t1, t2;
  728. unsigned char buf[2][4096];
  729. unsigned long x, y, z, zzz;
  730. int err, zz;
  731. for (x = 1024; x <= 2048; x += 256) {
  732. t2 = 0;
  733. for (y = 0; y < 4; y++) {
  734. t_start();
  735. t1 = t_read();
  736. if ((err = katja_make_key(&yarrow_prng, find_prng("yarrow"), x/8, &key)) != CRYPT_OK) {
  737. 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));
  738. exit(EXIT_FAILURE);
  739. }
  740. t1 = t_read() - t1;
  741. t2 += t1;
  742. if (y < 3) {
  743. katja_free(&key);
  744. }
  745. }
  746. t2 >>= 2;
  747. fprintf(stderr, "Katja-%lu make_key took %15"PRI64"u cycles\n", x, t2);
  748. t2 = 0;
  749. for (y = 0; y < 16; y++) {
  750. t_start();
  751. t1 = t_read();
  752. z = sizeof(buf[1]);
  753. if ((err = katja_encrypt_key(buf[0], 32, buf[1], &z, "testprog", 8, &yarrow_prng,
  754. find_prng("yarrow"), find_hash("sha1"),
  755. &key)) != CRYPT_OK) {
  756. 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));
  757. exit(EXIT_FAILURE);
  758. }
  759. t1 = t_read() - t1;
  760. t2 += t1;
  761. }
  762. t2 >>= 4;
  763. fprintf(stderr, "Katja-%lu encrypt_key took %15"PRI64"u cycles\n", x, t2);
  764. t2 = 0;
  765. for (y = 0; y < 2048; y++) {
  766. t_start();
  767. t1 = t_read();
  768. zzz = sizeof(buf[0]);
  769. if ((err = katja_decrypt_key(buf[1], z, buf[0], &zzz, "testprog", 8, find_hash("sha1"),
  770. &zz, &key)) != CRYPT_OK) {
  771. 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));
  772. exit(EXIT_FAILURE);
  773. }
  774. t1 = t_read() - t1;
  775. t2 += t1;
  776. }
  777. t2 >>= 11;
  778. fprintf(stderr, "Katja-%lu decrypt_key took %15"PRI64"u cycles\n", x, t2);
  779. katja_free(&key);
  780. }
  781. }
  782. #else
  783. static void time_katja(void) { fprintf(stderr, "NO Katja\n"); }
  784. #endif
  785. #if defined(LTC_MDH) && defined(LTC_TEST_MPI)
  786. /* time various DH operations */
  787. static void time_dh(void)
  788. {
  789. dh_key key;
  790. ulong64 t1, t2;
  791. unsigned long i, x, y;
  792. int err;
  793. static unsigned long sizes[] = {768/8, 1024/8, 1536/8, 2048/8, 3072/8, 4096/8, 6144/8, 8192/8, 100000};
  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) && defined(LTC_TEST_MPI)
  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. for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
  854. t2 = 0;
  855. for (y = 0; y < 256; y++) {
  856. t_start();
  857. t1 = t_read();
  858. if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
  859. 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));
  860. exit(EXIT_FAILURE);
  861. }
  862. t1 = t_read() - t1;
  863. t2 += t1;
  864. #ifdef LTC_PROFILE
  865. t2 <<= 8;
  866. break;
  867. #endif
  868. if (y < 255) {
  869. ecc_free(&key);
  870. }
  871. }
  872. t2 >>= 8;
  873. fprintf(stderr, "ECC-%lu make_key took %15"PRI64"u cycles\n", x*8, t2);
  874. t2 = 0;
  875. for (y = 0; y < 256; y++) {
  876. t_start();
  877. t1 = t_read();
  878. z = sizeof(buf[1]);
  879. if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
  880. &key)) != CRYPT_OK) {
  881. 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));
  882. exit(EXIT_FAILURE);
  883. }
  884. t1 = t_read() - t1;
  885. t2 += t1;
  886. #ifdef LTC_PROFILE
  887. t2 <<= 8;
  888. break;
  889. #endif
  890. }
  891. t2 >>= 8;
  892. fprintf(stderr, "ECC-%lu encrypt_key took %15"PRI64"u cycles\n", x*8, t2);
  893. t2 = 0;
  894. for (y = 0; y < 256; y++) {
  895. t_start();
  896. t1 = t_read();
  897. w = 20;
  898. if ((err = ecc_decrypt_key(buf[1], z, buf[0], &w, &key)) != CRYPT_OK) {
  899. 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));
  900. exit(EXIT_FAILURE);
  901. }
  902. t1 = t_read() - t1;
  903. t2 += t1;
  904. #ifdef LTC_PROFILE
  905. t2 <<= 8;
  906. break;
  907. #endif
  908. }
  909. t2 >>= 8;
  910. fprintf(stderr, "ECC-%lu decrypt_key took %15"PRI64"u cycles\n", x*8, t2);
  911. t2 = 0;
  912. for (y = 0; y < 256; y++) {
  913. t_start();
  914. t1 = t_read();
  915. z = sizeof(buf[1]);
  916. if ((err = ecc_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
  917. find_prng("yarrow"), &key)) != CRYPT_OK) {
  918. 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));
  919. exit(EXIT_FAILURE);
  920. }
  921. t1 = t_read() - t1;
  922. t2 += t1;
  923. #ifdef LTC_PROFILE
  924. t2 <<= 8;
  925. break;
  926. #endif
  927. }
  928. t2 >>= 8;
  929. fprintf(stderr, "ECC-%lu sign_hash took %15"PRI64"u cycles\n", x*8, t2);
  930. t2 = 0;
  931. for (y = 0; y < 256; y++) {
  932. t_start();
  933. t1 = t_read();
  934. if ((err = ecc_verify_hash(buf[1], z, buf[0], 20, &stat, &key)) != CRYPT_OK) {
  935. 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));
  936. exit(EXIT_FAILURE);
  937. }
  938. if (stat == 0) {
  939. fprintf(stderr, "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n", x*8, y);
  940. exit(EXIT_FAILURE);
  941. }
  942. t1 = t_read() - t1;
  943. t2 += t1;
  944. #ifdef LTC_PROFILE
  945. t2 <<= 8;
  946. break;
  947. #endif
  948. }
  949. t2 >>= 8;
  950. fprintf(stderr, "ECC-%lu verify_hash took %15"PRI64"u cycles\n", x*8, t2);
  951. fprintf(stderr, "\n\n");
  952. ecc_free(&key);
  953. }
  954. }
  955. #else
  956. static void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
  957. #endif
  958. static void time_macs_(unsigned long MAC_SIZE)
  959. {
  960. #if defined(LTC_OMAC) || defined(LTC_XCBC) || defined(LTC_F9_MODE) || defined(LTC_PMAC) || defined(LTC_PELICAN) || defined(LTC_HMAC)
  961. unsigned char *buf, key[16], tag[16];
  962. ulong64 t1, t2;
  963. unsigned long x, z;
  964. int err, cipher_idx, hash_idx;
  965. fprintf(stderr, "\nMAC Timings (cycles/byte on %luKB blocks):\n", MAC_SIZE);
  966. buf = XMALLOC(MAC_SIZE*1024);
  967. if (buf == NULL) {
  968. fprintf(stderr, "\n\nout of heap yo\n\n");
  969. exit(EXIT_FAILURE);
  970. }
  971. cipher_idx = find_cipher("aes");
  972. hash_idx = find_hash("sha1");
  973. if (cipher_idx == -1 || hash_idx == -1) {
  974. fprintf(stderr, "Warning the MAC tests requires AES and SHA1 to operate... so sorry\n");
  975. exit(EXIT_FAILURE);
  976. }
  977. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  978. yarrow_read(key, 16, &yarrow_prng);
  979. #ifdef LTC_OMAC
  980. t2 = -1;
  981. for (x = 0; x < 10000; x++) {
  982. t_start();
  983. t1 = t_read();
  984. z = 16;
  985. if ((err = omac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  986. fprintf(stderr, "\n\nomac-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  987. exit(EXIT_FAILURE);
  988. }
  989. t1 = t_read() - t1;
  990. if (t1 < t2) t2 = t1;
  991. }
  992. fprintf(stderr, "OMAC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  993. #endif
  994. #ifdef LTC_XCBC
  995. t2 = -1;
  996. for (x = 0; x < 10000; x++) {
  997. t_start();
  998. t1 = t_read();
  999. z = 16;
  1000. if ((err = xcbc_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1001. fprintf(stderr, "\n\nxcbc-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  1002. exit(EXIT_FAILURE);
  1003. }
  1004. t1 = t_read() - t1;
  1005. if (t1 < t2) t2 = t1;
  1006. }
  1007. fprintf(stderr, "XCBC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1008. #endif
  1009. #ifdef LTC_F9_MODE
  1010. t2 = -1;
  1011. for (x = 0; x < 10000; x++) {
  1012. t_start();
  1013. t1 = t_read();
  1014. z = 16;
  1015. if ((err = f9_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1016. fprintf(stderr, "\n\nF9-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  1017. exit(EXIT_FAILURE);
  1018. }
  1019. t1 = t_read() - t1;
  1020. if (t1 < t2) t2 = t1;
  1021. }
  1022. fprintf(stderr, "F9-%s\t\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1023. #endif
  1024. #ifdef LTC_PMAC
  1025. t2 = -1;
  1026. for (x = 0; x < 10000; x++) {
  1027. t_start();
  1028. t1 = t_read();
  1029. z = 16;
  1030. if ((err = pmac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1031. fprintf(stderr, "\n\npmac-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  1032. exit(EXIT_FAILURE);
  1033. }
  1034. t1 = t_read() - t1;
  1035. if (t1 < t2) t2 = t1;
  1036. }
  1037. fprintf(stderr, "PMAC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1038. #endif
  1039. #ifdef LTC_PELICAN
  1040. t2 = -1;
  1041. for (x = 0; x < 10000; x++) {
  1042. t_start();
  1043. t1 = t_read();
  1044. z = 16;
  1045. if ((err = pelican_memory(key, 16, buf, MAC_SIZE*1024, tag)) != CRYPT_OK) {
  1046. fprintf(stderr, "\n\npelican error... %s\n", error_to_string(err));
  1047. exit(EXIT_FAILURE);
  1048. }
  1049. t1 = t_read() - t1;
  1050. if (t1 < t2) t2 = t1;
  1051. }
  1052. fprintf(stderr, "PELICAN \t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1053. #endif
  1054. #ifdef LTC_HMAC
  1055. t2 = -1;
  1056. for (x = 0; x < 10000; x++) {
  1057. t_start();
  1058. t1 = t_read();
  1059. z = 16;
  1060. if ((err = hmac_memory(hash_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1061. fprintf(stderr, "\n\nhmac-%s error... %s\n", hash_descriptor[hash_idx].name, error_to_string(err));
  1062. exit(EXIT_FAILURE);
  1063. }
  1064. t1 = t_read() - t1;
  1065. if (t1 < t2) t2 = t1;
  1066. }
  1067. fprintf(stderr, "HMAC-%s\t\t%9"PRI64"u\n", hash_descriptor[hash_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1068. #endif
  1069. XFREE(buf);
  1070. #else
  1071. LTC_UNUSED_PARAM(MAC_SIZE);
  1072. fprintf(stderr, "NO MACs\n");
  1073. #endif
  1074. }
  1075. static void time_macs(void)
  1076. {
  1077. time_macs_(1);
  1078. time_macs_(4);
  1079. time_macs_(32);
  1080. }
  1081. static void time_encmacs_(unsigned long MAC_SIZE)
  1082. {
  1083. #if defined(LTC_EAX_MODE) || defined(LTC_OCB_MODE) || defined(LTC_OCB3_MODE) || defined(LTC_CCM_MODE) || defined(LTC_GCM_MODE)
  1084. unsigned char *buf, IV[16], key[16], tag[16];
  1085. ulong64 t1, t2;
  1086. unsigned long x, z;
  1087. int err, cipher_idx;
  1088. symmetric_key skey;
  1089. fprintf(stderr, "\nENC+MAC Timings (zero byte AAD, 16 byte IV, cycles/byte on %luKB blocks):\n", MAC_SIZE);
  1090. buf = XMALLOC(MAC_SIZE*1024);
  1091. if (buf == NULL) {
  1092. fprintf(stderr, "\n\nout of heap yo\n\n");
  1093. exit(EXIT_FAILURE);
  1094. }
  1095. cipher_idx = find_cipher("aes");
  1096. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  1097. yarrow_read(key, 16, &yarrow_prng);
  1098. yarrow_read(IV, 16, &yarrow_prng);
  1099. #ifdef LTC_EAX_MODE
  1100. t2 = -1;
  1101. for (x = 0; x < 10000; x++) {
  1102. t_start();
  1103. t1 = t_read();
  1104. z = 16;
  1105. if ((err = eax_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1106. fprintf(stderr, "\nEAX error... %s\n", error_to_string(err));
  1107. exit(EXIT_FAILURE);
  1108. }
  1109. t1 = t_read() - t1;
  1110. if (t1 < t2) t2 = t1;
  1111. }
  1112. fprintf(stderr, "EAX \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1113. #endif
  1114. #ifdef LTC_OCB_MODE
  1115. t2 = -1;
  1116. for (x = 0; x < 10000; x++) {
  1117. t_start();
  1118. t1 = t_read();
  1119. z = 16;
  1120. if ((err = ocb_encrypt_authenticate_memory(cipher_idx, key, 16, IV, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1121. fprintf(stderr, "\nOCB error... %s\n", error_to_string(err));
  1122. exit(EXIT_FAILURE);
  1123. }
  1124. t1 = t_read() - t1;
  1125. if (t1 < t2) t2 = t1;
  1126. }
  1127. fprintf(stderr, "OCB \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1128. #endif
  1129. #ifdef LTC_OCB3_MODE
  1130. t2 = -1;
  1131. for (x = 0; x < 10000; x++) {
  1132. t_start();
  1133. t1 = t_read();
  1134. z = 16;
  1135. if ((err = ocb3_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 15, (unsigned char*)"", 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1136. fprintf(stderr, "\nOCB3 error... %s\n", error_to_string(err));
  1137. exit(EXIT_FAILURE);
  1138. }
  1139. t1 = t_read() - t1;
  1140. if (t1 < t2) t2 = t1;
  1141. }
  1142. fprintf(stderr, "OCB3 \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1143. #endif
  1144. #ifdef LTC_CCM_MODE
  1145. t2 = -1;
  1146. for (x = 0; x < 10000; x++) {
  1147. t_start();
  1148. t1 = t_read();
  1149. z = 16;
  1150. if ((err = ccm_memory(cipher_idx, key, 16, NULL, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  1151. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  1152. exit(EXIT_FAILURE);
  1153. }
  1154. t1 = t_read() - t1;
  1155. if (t1 < t2) t2 = t1;
  1156. }
  1157. fprintf(stderr, "CCM (no-precomp) \t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1158. cipher_descriptor[cipher_idx].setup(key, 16, 0, &skey);
  1159. t2 = -1;
  1160. for (x = 0; x < 10000; x++) {
  1161. t_start();
  1162. t1 = t_read();
  1163. z = 16;
  1164. if ((err = ccm_memory(cipher_idx, key, 16, &skey, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  1165. fprintf(stderr, "\nCCM 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, "CCM (precomp) \t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1172. cipher_descriptor[cipher_idx].done(&skey);
  1173. #endif
  1174. #ifdef LTC_GCM_MODE
  1175. t2 = -1;
  1176. for (x = 0; x < 100; x++) {
  1177. t_start();
  1178. t1 = t_read();
  1179. z = 16;
  1180. if ((err = gcm_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, GCM_ENCRYPT)) != CRYPT_OK) {
  1181. fprintf(stderr, "\nGCM error... %s\n", error_to_string(err));
  1182. exit(EXIT_FAILURE);
  1183. }
  1184. t1 = t_read() - t1;
  1185. if (t1 < t2) t2 = t1;
  1186. }
  1187. fprintf(stderr, "GCM (no-precomp)\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1188. {
  1189. gcm_state gcm
  1190. #ifdef LTC_GCM_TABLES_SSE2
  1191. __attribute__ ((aligned (16)))
  1192. #endif
  1193. ;
  1194. if ((err = gcm_init(&gcm, cipher_idx, key, 16)) != CRYPT_OK) { fprintf(stderr, "gcm_init: %s\n", error_to_string(err)); exit(EXIT_FAILURE); }
  1195. t2 = -1;
  1196. for (x = 0; x < 10000; x++) {
  1197. t_start();
  1198. t1 = t_read();
  1199. z = 16;
  1200. if ((err = gcm_reset(&gcm)) != CRYPT_OK) {
  1201. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1202. exit(EXIT_FAILURE);
  1203. }
  1204. if ((err = gcm_add_iv(&gcm, IV, 16)) != CRYPT_OK) {
  1205. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1206. exit(EXIT_FAILURE);
  1207. }
  1208. if ((err = gcm_add_aad(&gcm, NULL, 0)) != CRYPT_OK) {
  1209. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1210. exit(EXIT_FAILURE);
  1211. }
  1212. if ((err = gcm_process(&gcm, buf, MAC_SIZE*1024, buf, GCM_ENCRYPT)) != CRYPT_OK) {
  1213. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1214. exit(EXIT_FAILURE);
  1215. }
  1216. if ((err = gcm_done(&gcm, tag, &z)) != CRYPT_OK) {
  1217. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1218. exit(EXIT_FAILURE);
  1219. }
  1220. t1 = t_read() - t1;
  1221. if (t1 < t2) t2 = t1;
  1222. }
  1223. fprintf(stderr, "GCM (precomp)\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1224. }
  1225. #endif
  1226. XFREE(buf);
  1227. #else
  1228. LTC_UNUSED_PARAM(MAC_SIZE);
  1229. fprintf(stderr, "NO ENCMACs\n");
  1230. #endif
  1231. }
  1232. static void time_encmacs(void)
  1233. {
  1234. time_encmacs_(1);
  1235. time_encmacs_(4);
  1236. time_encmacs_(32);
  1237. }
  1238. #define LTC_TEST_FN(f) { f, #f }
  1239. int main(int argc, char **argv)
  1240. {
  1241. int err;
  1242. const struct
  1243. {
  1244. void (*fn)(void);
  1245. const char* name;
  1246. } test_functions[] = {
  1247. LTC_TEST_FN(time_keysched),
  1248. LTC_TEST_FN(time_cipher_ecb),
  1249. LTC_TEST_FN(time_cipher_cbc),
  1250. LTC_TEST_FN(time_cipher_ctr),
  1251. LTC_TEST_FN(time_cipher_lrw),
  1252. LTC_TEST_FN(time_hash),
  1253. LTC_TEST_FN(time_macs),
  1254. LTC_TEST_FN(time_encmacs),
  1255. LTC_TEST_FN(time_prng),
  1256. LTC_TEST_FN(time_mult),
  1257. LTC_TEST_FN(time_sqr),
  1258. LTC_TEST_FN(time_rsa),
  1259. LTC_TEST_FN(time_dsa),
  1260. LTC_TEST_FN(time_ecc),
  1261. LTC_TEST_FN(time_dh),
  1262. LTC_TEST_FN(time_katja)
  1263. };
  1264. char *single_test = NULL;
  1265. unsigned int i;
  1266. init_timer();
  1267. register_all_ciphers();
  1268. register_all_hashes();
  1269. register_all_prngs();
  1270. #ifdef USE_LTM
  1271. ltc_mp = ltm_desc;
  1272. #elif defined(USE_TFM)
  1273. ltc_mp = tfm_desc;
  1274. #elif defined(USE_GMP)
  1275. ltc_mp = gmp_desc;
  1276. #elif defined(EXT_MATH_LIB)
  1277. {
  1278. extern ltc_math_descriptor EXT_MATH_LIB;
  1279. ltc_mp = EXT_MATH_LIB;
  1280. }
  1281. #endif
  1282. if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
  1283. fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
  1284. exit(EXIT_FAILURE);
  1285. }
  1286. /* single test name from commandline */
  1287. if (argc > 1) single_test = argv[1];
  1288. for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
  1289. if (single_test && strstr(test_functions[i].name, single_test) == NULL) {
  1290. continue;
  1291. }
  1292. test_functions[i].fn();
  1293. }
  1294. return EXIT_SUCCESS;
  1295. }
  1296. /* ref: $Format:%D$ */
  1297. /* git commit: $Format:%H$ */
  1298. /* commit time: $Format:%ai$ */