timing.c 39 KB

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