timing.c 39 KB

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