timing.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. fprintf(stderr, "Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n");
  507. for (x = 0; prng_descriptor[x].name != NULL; x++) {
  508. /* sanity check on prng */
  509. if ((err = prng_descriptor[x].test()) != CRYPT_OK) {
  510. fprintf(stderr, "\n\nERROR: PRNG %s failed self-test %s\n", prng_descriptor[x].name, error_to_string(err));
  511. exit(EXIT_FAILURE);
  512. }
  513. prng_descriptor[x].start(&tprng);
  514. zeromem(buf, 256);
  515. prng_descriptor[x].add_entropy(buf, 256, &tprng);
  516. prng_descriptor[x].ready(&tprng);
  517. t2 = -1;
  518. #define DO1 if (prng_descriptor[x].read(buf, 4096, &tprng) != 4096) { fprintf(stderr, "\n\nERROR READ != 4096\n\n"); exit(EXIT_FAILURE); }
  519. #define DO2 DO1 DO1
  520. for (y = 0; y < 10000; y++) {
  521. t_start();
  522. t1 = t_read();
  523. DO2;
  524. t1 = (t_read() - t1)>>1;
  525. if (t1 < t2) t2 = t1;
  526. }
  527. fprintf(stderr, "%20s: %5"PRI64"u ", prng_descriptor[x].name, t2>>12);
  528. #undef DO2
  529. #undef DO1
  530. #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);
  531. #define DO2 DO1 DO1
  532. for (y = 0; y < 10000; y++) {
  533. t_start();
  534. t1 = t_read();
  535. DO2;
  536. t1 = (t_read() - t1)>>1;
  537. if (t1 < t2) t2 = t1;
  538. }
  539. fprintf(stderr, "%5"PRI64"u\n", t2);
  540. #undef DO2
  541. #undef DO1
  542. }
  543. }
  544. #if defined(LTC_MDSA)
  545. /* time various DSA operations */
  546. static void time_dsa(void)
  547. {
  548. dsa_key key;
  549. ulong64 t1, t2;
  550. unsigned long x, y;
  551. int err;
  552. static const struct {
  553. int group, modulus;
  554. } groups[] = {
  555. { 20, 96 },
  556. { 20, 128 },
  557. { 24, 192 },
  558. { 28, 256 },
  559. #ifndef TFM_DESC
  560. { 32, 512 },
  561. #endif
  562. };
  563. if (ltc_mp.name == NULL) return;
  564. for (x = 0; x < (sizeof(groups)/sizeof(groups[0])); x++) {
  565. t2 = 0;
  566. for (y = 0; y < 4; y++) {
  567. t_start();
  568. t1 = t_read();
  569. if ((err = dsa_generate_pqg(&yarrow_prng, find_prng("yarrow"), groups[x].group, groups[x].modulus, &key)) != CRYPT_OK) {
  570. 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));
  571. exit(EXIT_FAILURE);
  572. }
  573. if ((err = dsa_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
  574. 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));
  575. exit(EXIT_FAILURE);
  576. }
  577. t1 = t_read() - t1;
  578. t2 += t1;
  579. #ifdef LTC_PROFILE
  580. t2 <<= 2;
  581. break;
  582. #endif
  583. if (y < 3) {
  584. dsa_free(&key);
  585. }
  586. }
  587. t2 >>= 2;
  588. 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);
  589. dsa_free(&key);
  590. }
  591. fprintf(stderr, "\n\n");
  592. }
  593. #else
  594. static void time_dsa(void) { fprintf(stderr, "NO DSA\n"); }
  595. #endif
  596. #if defined(LTC_MRSA)
  597. /* time various RSA operations */
  598. static void time_rsa(void)
  599. {
  600. rsa_key key;
  601. ulong64 t1, t2;
  602. unsigned char buf[2][2048] = { 0 };
  603. unsigned long x, y, z, zzz;
  604. int err, zz, stat;
  605. if (ltc_mp.name == NULL) return;
  606. for (x = 2048; x <= 8192; x <<= 1) {
  607. t2 = 0;
  608. for (y = 0; y < 4; y++) {
  609. t_start();
  610. t1 = t_read();
  611. if ((err = rsa_make_key(&yarrow_prng, find_prng("yarrow"), x/8, 65537, &key)) != CRYPT_OK) {
  612. 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));
  613. exit(EXIT_FAILURE);
  614. }
  615. t1 = t_read() - t1;
  616. t2 += t1;
  617. #ifdef LTC_PROFILE
  618. t2 <<= 2;
  619. break;
  620. #endif
  621. if (y < 3) {
  622. rsa_free(&key);
  623. }
  624. }
  625. t2 >>= 2;
  626. fprintf(stderr, "RSA-%lu make_key took %15"PRI64"u cycles\n", x, t2);
  627. t2 = 0;
  628. for (y = 0; y < 16; y++) {
  629. t_start();
  630. t1 = t_read();
  631. z = sizeof(buf[1]);
  632. if ((err = rsa_encrypt_key(buf[0], 32, buf[1], &z, (const unsigned char *)"testprog", 8, &yarrow_prng,
  633. find_prng("yarrow"), find_hash("sha1"),
  634. &key)) != CRYPT_OK) {
  635. 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));
  636. exit(EXIT_FAILURE);
  637. }
  638. t1 = t_read() - t1;
  639. t2 += t1;
  640. #ifdef LTC_PROFILE
  641. t2 <<= 4;
  642. break;
  643. #endif
  644. }
  645. t2 >>= 4;
  646. fprintf(stderr, "RSA-%lu encrypt_key took %15"PRI64"u cycles\n", x, t2);
  647. t2 = 0;
  648. for (y = 0; y < 2048; y++) {
  649. t_start();
  650. t1 = t_read();
  651. zzz = sizeof(buf[0]);
  652. if ((err = rsa_decrypt_key(buf[1], z, buf[0], &zzz, (const unsigned char *)"testprog", 8, find_hash("sha1"),
  653. &zz, &key)) != CRYPT_OK) {
  654. 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));
  655. exit(EXIT_FAILURE);
  656. }
  657. t1 = t_read() - t1;
  658. t2 += t1;
  659. #ifdef LTC_PROFILE
  660. t2 <<= 11;
  661. break;
  662. #endif
  663. }
  664. t2 >>= 11;
  665. fprintf(stderr, "RSA-%lu decrypt_key took %15"PRI64"u cycles\n", x, t2);
  666. t2 = 0;
  667. for (y = 0; y < 256; y++) {
  668. t_start();
  669. t1 = t_read();
  670. z = sizeof(buf[1]);
  671. if ((err = rsa_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
  672. find_prng("yarrow"), find_hash("sha1"), 8, &key)) != CRYPT_OK) {
  673. 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));
  674. exit(EXIT_FAILURE);
  675. }
  676. t1 = t_read() - t1;
  677. t2 += t1;
  678. #ifdef LTC_PROFILE
  679. t2 <<= 8;
  680. break;
  681. #endif
  682. }
  683. t2 >>= 8;
  684. fprintf(stderr, "RSA-%lu sign_hash took %15"PRI64"u cycles\n", x, t2);
  685. t2 = 0;
  686. for (y = 0; y < 2048; y++) {
  687. t_start();
  688. t1 = t_read();
  689. if ((err = rsa_verify_hash(buf[1], z, buf[0], 20, find_hash("sha1"), 8, &stat, &key)) != CRYPT_OK) {
  690. 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));
  691. exit(EXIT_FAILURE);
  692. }
  693. if (stat == 0) {
  694. fprintf(stderr, "\n\nrsa_verify_hash for RSA-%lu failed to verify signature(%lu)\n", x, y);
  695. exit(EXIT_FAILURE);
  696. }
  697. t1 = t_read() - t1;
  698. t2 += t1;
  699. #ifdef LTC_PROFILE
  700. t2 <<= 11;
  701. break;
  702. #endif
  703. }
  704. t2 >>= 11;
  705. fprintf(stderr, "RSA-%lu verify_hash took %15"PRI64"u cycles\n", x, t2);
  706. fprintf(stderr, "\n\n");
  707. rsa_free(&key);
  708. }
  709. }
  710. #else
  711. static void time_rsa(void) { fprintf(stderr, "NO RSA\n"); }
  712. #endif
  713. #if defined(LTC_MDH)
  714. /* time various DH operations */
  715. static void time_dh(void)
  716. {
  717. dh_key key;
  718. ulong64 t1, t2;
  719. unsigned long i, x, y;
  720. int err;
  721. static unsigned long sizes[] = {768/8, 1024/8, 1536/8, 2048/8,
  722. #ifndef TFM_DESC
  723. 3072/8, 4096/8, 6144/8, 8192/8,
  724. #endif
  725. 100000
  726. };
  727. if (ltc_mp.name == NULL) return;
  728. for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
  729. t2 = 0;
  730. for (y = 0; y < 16; y++) {
  731. if((err = dh_set_pg_groupsize(x, &key)) != CRYPT_OK) {
  732. 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));
  733. exit(EXIT_FAILURE);
  734. }
  735. t_start();
  736. t1 = t_read();
  737. if ((err = dh_generate_key(&yarrow_prng, find_prng("yarrow"), &key)) != CRYPT_OK) {
  738. 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));
  739. exit(EXIT_FAILURE);
  740. }
  741. t1 = t_read() - t1;
  742. t2 += t1;
  743. dh_free(&key);
  744. }
  745. t2 >>= 4;
  746. fprintf(stderr, "DH-%4lu make_key took %15"PRI64"u cycles\n", x*8, t2);
  747. }
  748. }
  749. #else
  750. static void time_dh(void) { fprintf(stderr, "NO DH\n"); }
  751. #endif
  752. #if defined(LTC_MECC)
  753. /* time various ECC operations */
  754. static void time_ecc(void)
  755. {
  756. ecc_key key;
  757. ulong64 t1, t2;
  758. unsigned char buf[2][256] = { 0 };
  759. unsigned long i, w, x, y, z;
  760. int err, stat;
  761. static unsigned long sizes[] = {
  762. #ifdef LTC_ECC_SECP112R1
  763. 112/8,
  764. #endif
  765. #ifdef LTC_ECC_SECP128R1
  766. 128/8,
  767. #endif
  768. #ifdef LTC_ECC_SECP160R1
  769. 160/8,
  770. #endif
  771. #ifdef LTC_ECC_SECP192R1
  772. 192/8,
  773. #endif
  774. #ifdef LTC_ECC_SECP224R1
  775. 224/8,
  776. #endif
  777. #ifdef LTC_ECC_SECP256R1
  778. 256/8,
  779. #endif
  780. #ifdef LTC_ECC_SECP384R1
  781. 384/8,
  782. #endif
  783. #ifdef LTC_ECC_SECP512R1
  784. 521/8,
  785. #endif
  786. 100000};
  787. if (ltc_mp.name == NULL) return;
  788. for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
  789. t2 = 0;
  790. for (y = 0; y < 256; y++) {
  791. t_start();
  792. t1 = t_read();
  793. if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
  794. 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));
  795. exit(EXIT_FAILURE);
  796. }
  797. t1 = t_read() - t1;
  798. t2 += t1;
  799. #ifdef LTC_PROFILE
  800. t2 <<= 8;
  801. break;
  802. #endif
  803. if (y < 255) {
  804. ecc_free(&key);
  805. }
  806. }
  807. t2 >>= 8;
  808. fprintf(stderr, "ECC-%lu make_key took %15"PRI64"u cycles\n", x*8, t2);
  809. t2 = 0;
  810. for (y = 0; y < 256; y++) {
  811. t_start();
  812. t1 = t_read();
  813. z = sizeof(buf[1]);
  814. if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
  815. &key)) != CRYPT_OK) {
  816. 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));
  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. }
  826. t2 >>= 8;
  827. fprintf(stderr, "ECC-%lu encrypt_key took %15"PRI64"u cycles\n", x*8, t2);
  828. t2 = 0;
  829. for (y = 0; y < 256; y++) {
  830. t_start();
  831. t1 = t_read();
  832. w = 20;
  833. if ((err = ecc_decrypt_key(buf[1], z, buf[0], &w, &key)) != CRYPT_OK) {
  834. 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));
  835. exit(EXIT_FAILURE);
  836. }
  837. t1 = t_read() - t1;
  838. t2 += t1;
  839. #ifdef LTC_PROFILE
  840. t2 <<= 8;
  841. break;
  842. #endif
  843. }
  844. t2 >>= 8;
  845. fprintf(stderr, "ECC-%lu decrypt_key took %15"PRI64"u cycles\n", x*8, t2);
  846. t2 = 0;
  847. for (y = 0; y < 256; y++) {
  848. t_start();
  849. t1 = t_read();
  850. z = sizeof(buf[1]);
  851. if ((err = ecc_sign_hash(buf[0], 20, buf[1], &z, &yarrow_prng,
  852. find_prng("yarrow"), &key)) != CRYPT_OK) {
  853. 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));
  854. exit(EXIT_FAILURE);
  855. }
  856. t1 = t_read() - t1;
  857. t2 += t1;
  858. #ifdef LTC_PROFILE
  859. t2 <<= 8;
  860. break;
  861. #endif
  862. }
  863. t2 >>= 8;
  864. fprintf(stderr, "ECC-%lu sign_hash took %15"PRI64"u cycles\n", x*8, t2);
  865. t2 = 0;
  866. for (y = 0; y < 256; y++) {
  867. t_start();
  868. t1 = t_read();
  869. if ((err = ecc_verify_hash(buf[1], z, buf[0], 20, &stat, &key)) != CRYPT_OK) {
  870. 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));
  871. exit(EXIT_FAILURE);
  872. }
  873. if (stat == 0) {
  874. fprintf(stderr, "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n", x*8, y);
  875. exit(EXIT_FAILURE);
  876. }
  877. t1 = t_read() - t1;
  878. t2 += t1;
  879. #ifdef LTC_PROFILE
  880. t2 <<= 8;
  881. break;
  882. #endif
  883. }
  884. t2 >>= 8;
  885. fprintf(stderr, "ECC-%lu verify_hash took %15"PRI64"u cycles\n", x*8, t2);
  886. fprintf(stderr, "\n\n");
  887. ecc_free(&key);
  888. }
  889. }
  890. #else
  891. static void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
  892. #endif
  893. static void time_macs_(unsigned long MAC_SIZE)
  894. {
  895. #if defined(LTC_OMAC) || defined(LTC_XCBC) || defined(LTC_F9_MODE) || defined(LTC_PMAC) || defined(LTC_PELICAN) || defined(LTC_HMAC)
  896. unsigned char *buf, key[16], tag[16];
  897. ulong64 t1, t2;
  898. unsigned long x, z;
  899. int err, cipher_idx, hash_idx;
  900. fprintf(stderr, "\nMAC Timings (cycles/byte on %luKB blocks):\n", MAC_SIZE);
  901. buf = XMALLOC(MAC_SIZE*1024);
  902. if (buf == NULL) {
  903. fprintf(stderr, "\n\nout of heap yo\n\n");
  904. exit(EXIT_FAILURE);
  905. }
  906. cipher_idx = find_cipher("aes");
  907. hash_idx = find_hash("sha1");
  908. if (cipher_idx == -1 || hash_idx == -1) {
  909. fprintf(stderr, "Warning the MAC tests requires AES and SHA1 to operate... so sorry\n");
  910. exit(EXIT_FAILURE);
  911. }
  912. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  913. yarrow_read(key, 16, &yarrow_prng);
  914. #ifdef LTC_OMAC
  915. t2 = -1;
  916. for (x = 0; x < 10000; x++) {
  917. t_start();
  918. t1 = t_read();
  919. z = 16;
  920. if ((err = omac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  921. fprintf(stderr, "\n\nomac-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  922. exit(EXIT_FAILURE);
  923. }
  924. t1 = t_read() - t1;
  925. if (t1 < t2) t2 = t1;
  926. }
  927. fprintf(stderr, "OMAC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  928. #endif
  929. #ifdef LTC_XCBC
  930. t2 = -1;
  931. for (x = 0; x < 10000; x++) {
  932. t_start();
  933. t1 = t_read();
  934. z = 16;
  935. if ((err = xcbc_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  936. fprintf(stderr, "\n\nxcbc-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  937. exit(EXIT_FAILURE);
  938. }
  939. t1 = t_read() - t1;
  940. if (t1 < t2) t2 = t1;
  941. }
  942. fprintf(stderr, "XCBC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  943. #endif
  944. #ifdef LTC_F9_MODE
  945. t2 = -1;
  946. for (x = 0; x < 10000; x++) {
  947. t_start();
  948. t1 = t_read();
  949. z = 16;
  950. if ((err = f9_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  951. fprintf(stderr, "\n\nF9-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  952. exit(EXIT_FAILURE);
  953. }
  954. t1 = t_read() - t1;
  955. if (t1 < t2) t2 = t1;
  956. }
  957. fprintf(stderr, "F9-%s\t\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  958. #endif
  959. #ifdef LTC_PMAC
  960. t2 = -1;
  961. for (x = 0; x < 10000; x++) {
  962. t_start();
  963. t1 = t_read();
  964. z = 16;
  965. if ((err = pmac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  966. fprintf(stderr, "\n\npmac-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  967. exit(EXIT_FAILURE);
  968. }
  969. t1 = t_read() - t1;
  970. if (t1 < t2) t2 = t1;
  971. }
  972. fprintf(stderr, "PMAC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  973. #endif
  974. #ifdef LTC_PELICAN
  975. t2 = -1;
  976. for (x = 0; x < 10000; x++) {
  977. t_start();
  978. t1 = t_read();
  979. z = 16;
  980. if ((err = pelican_memory(key, 16, buf, MAC_SIZE*1024, tag)) != CRYPT_OK) {
  981. fprintf(stderr, "\n\npelican error... %s\n", error_to_string(err));
  982. exit(EXIT_FAILURE);
  983. }
  984. t1 = t_read() - t1;
  985. if (t1 < t2) t2 = t1;
  986. }
  987. fprintf(stderr, "PELICAN \t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  988. #endif
  989. #ifdef LTC_HMAC
  990. t2 = -1;
  991. for (x = 0; x < 10000; x++) {
  992. t_start();
  993. t1 = t_read();
  994. z = 16;
  995. if ((err = hmac_memory(hash_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  996. fprintf(stderr, "\n\nhmac-%s error... %s\n", hash_descriptor[hash_idx].name, error_to_string(err));
  997. exit(EXIT_FAILURE);
  998. }
  999. t1 = t_read() - t1;
  1000. if (t1 < t2) t2 = t1;
  1001. }
  1002. fprintf(stderr, "HMAC-%s\t\t%9"PRI64"u\n", hash_descriptor[hash_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1003. #endif
  1004. XFREE(buf);
  1005. #else
  1006. LTC_UNUSED_PARAM(MAC_SIZE);
  1007. fprintf(stderr, "NO MACs\n");
  1008. #endif
  1009. }
  1010. static void time_macs(void)
  1011. {
  1012. time_macs_(1);
  1013. time_macs_(4);
  1014. time_macs_(32);
  1015. }
  1016. static void time_encmacs_(unsigned long MAC_SIZE)
  1017. {
  1018. #if defined(LTC_EAX_MODE) || defined(LTC_OCB_MODE) || defined(LTC_OCB3_MODE) || defined(LTC_CCM_MODE) || defined(LTC_GCM_MODE)
  1019. unsigned char *buf, IV[16], key[16], tag[16];
  1020. ulong64 t1, t2;
  1021. unsigned long x, z;
  1022. int err, cipher_idx;
  1023. symmetric_key skey;
  1024. fprintf(stderr, "\nENC+MAC Timings (zero byte AAD, 16 byte IV, cycles/byte on %luKB blocks):\n", MAC_SIZE);
  1025. buf = XMALLOC(MAC_SIZE*1024);
  1026. if (buf == NULL) {
  1027. fprintf(stderr, "\n\nout of heap yo\n\n");
  1028. exit(EXIT_FAILURE);
  1029. }
  1030. cipher_idx = find_cipher("aes");
  1031. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  1032. yarrow_read(key, 16, &yarrow_prng);
  1033. yarrow_read(IV, 16, &yarrow_prng);
  1034. #ifdef LTC_EAX_MODE
  1035. t2 = -1;
  1036. for (x = 0; x < 10000; x++) {
  1037. t_start();
  1038. t1 = t_read();
  1039. z = 16;
  1040. if ((err = eax_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1041. fprintf(stderr, "\nEAX error... %s\n", error_to_string(err));
  1042. exit(EXIT_FAILURE);
  1043. }
  1044. t1 = t_read() - t1;
  1045. if (t1 < t2) t2 = t1;
  1046. }
  1047. fprintf(stderr, "EAX \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1048. #endif
  1049. #ifdef LTC_OCB_MODE
  1050. t2 = -1;
  1051. for (x = 0; x < 10000; x++) {
  1052. t_start();
  1053. t1 = t_read();
  1054. z = 16;
  1055. if ((err = ocb_encrypt_authenticate_memory(cipher_idx, key, 16, IV, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1056. fprintf(stderr, "\nOCB error... %s\n", error_to_string(err));
  1057. exit(EXIT_FAILURE);
  1058. }
  1059. t1 = t_read() - t1;
  1060. if (t1 < t2) t2 = t1;
  1061. }
  1062. fprintf(stderr, "OCB \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1063. #endif
  1064. #ifdef LTC_OCB3_MODE
  1065. t2 = -1;
  1066. for (x = 0; x < 10000; x++) {
  1067. t_start();
  1068. t1 = t_read();
  1069. z = 16;
  1070. if ((err = ocb3_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 15, (unsigned char*)"", 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1071. fprintf(stderr, "\nOCB3 error... %s\n", error_to_string(err));
  1072. exit(EXIT_FAILURE);
  1073. }
  1074. t1 = t_read() - t1;
  1075. if (t1 < t2) t2 = t1;
  1076. }
  1077. fprintf(stderr, "OCB3 \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1078. #endif
  1079. #ifdef LTC_CCM_MODE
  1080. t2 = -1;
  1081. for (x = 0; x < 10000; x++) {
  1082. t_start();
  1083. t1 = t_read();
  1084. z = 16;
  1085. if ((err = ccm_memory(cipher_idx, key, 16, NULL, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  1086. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  1087. exit(EXIT_FAILURE);
  1088. }
  1089. t1 = t_read() - t1;
  1090. if (t1 < t2) t2 = t1;
  1091. }
  1092. fprintf(stderr, "CCM (no-precomp) \t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1093. cipher_descriptor[cipher_idx].setup(key, 16, 0, &skey);
  1094. t2 = -1;
  1095. for (x = 0; x < 10000; x++) {
  1096. t_start();
  1097. t1 = t_read();
  1098. z = 16;
  1099. if ((err = ccm_memory(cipher_idx, key, 16, &skey, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  1100. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  1101. exit(EXIT_FAILURE);
  1102. }
  1103. t1 = t_read() - t1;
  1104. if (t1 < t2) t2 = t1;
  1105. }
  1106. fprintf(stderr, "CCM (precomp) \t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1107. cipher_descriptor[cipher_idx].done(&skey);
  1108. #endif
  1109. #ifdef LTC_GCM_MODE
  1110. t2 = -1;
  1111. for (x = 0; x < 100; x++) {
  1112. t_start();
  1113. t1 = t_read();
  1114. z = 16;
  1115. if ((err = gcm_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, GCM_ENCRYPT)) != CRYPT_OK) {
  1116. fprintf(stderr, "\nGCM error... %s\n", error_to_string(err));
  1117. exit(EXIT_FAILURE);
  1118. }
  1119. t1 = t_read() - t1;
  1120. if (t1 < t2) t2 = t1;
  1121. }
  1122. fprintf(stderr, "GCM (no-precomp)\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1123. {
  1124. gcm_state gcm
  1125. #ifdef LTC_GCM_TABLES_SSE2
  1126. __attribute__ ((aligned (16)))
  1127. #endif
  1128. ;
  1129. if ((err = gcm_init(&gcm, cipher_idx, key, 16)) != CRYPT_OK) { fprintf(stderr, "gcm_init: %s\n", error_to_string(err)); exit(EXIT_FAILURE); }
  1130. t2 = -1;
  1131. for (x = 0; x < 10000; x++) {
  1132. t_start();
  1133. t1 = t_read();
  1134. z = 16;
  1135. if ((err = gcm_reset(&gcm)) != CRYPT_OK) {
  1136. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1137. exit(EXIT_FAILURE);
  1138. }
  1139. if ((err = gcm_add_iv(&gcm, IV, 16)) != CRYPT_OK) {
  1140. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1141. exit(EXIT_FAILURE);
  1142. }
  1143. if ((err = gcm_add_aad(&gcm, NULL, 0)) != CRYPT_OK) {
  1144. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1145. exit(EXIT_FAILURE);
  1146. }
  1147. if ((err = gcm_process(&gcm, buf, MAC_SIZE*1024, buf, GCM_ENCRYPT)) != CRYPT_OK) {
  1148. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1149. exit(EXIT_FAILURE);
  1150. }
  1151. if ((err = gcm_done(&gcm, tag, &z)) != CRYPT_OK) {
  1152. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1153. exit(EXIT_FAILURE);
  1154. }
  1155. t1 = t_read() - t1;
  1156. if (t1 < t2) t2 = t1;
  1157. }
  1158. fprintf(stderr, "GCM (precomp)\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1159. }
  1160. #endif
  1161. XFREE(buf);
  1162. #else
  1163. LTC_UNUSED_PARAM(MAC_SIZE);
  1164. fprintf(stderr, "NO ENCMACs\n");
  1165. #endif
  1166. }
  1167. static void time_encmacs(void)
  1168. {
  1169. time_encmacs_(1);
  1170. time_encmacs_(4);
  1171. time_encmacs_(32);
  1172. }
  1173. static void LTC_NORETURN die(int status)
  1174. {
  1175. FILE* o = status == EXIT_SUCCESS ? stdout : stderr;
  1176. fprintf(o,
  1177. "Usage: timing [<-h|-l|alg>] [mpi]\n\n"
  1178. "Run timing tests of all built-in algorithms, or only the one given in <alg>.\n\n"
  1179. "\talg\tThe algorithm to test. Use the '-l' option to check for valid values.\n"
  1180. "\tmpi\tThe MPI provider to use.\n"
  1181. "\t-l\tList all built-in algorithms that can be timed.\n"
  1182. "\t-h\tThe help you're looking at.\n"
  1183. );
  1184. exit(status);
  1185. }
  1186. #define LTC_TEST_FN(f) { time_ ## f, #f }
  1187. int main(int argc, char **argv)
  1188. {
  1189. int err;
  1190. const struct
  1191. {
  1192. void (*fn)(void);
  1193. const char* name;
  1194. } test_functions[] = {
  1195. LTC_TEST_FN(keysched),
  1196. LTC_TEST_FN(cipher_ecb),
  1197. LTC_TEST_FN(cipher_cbc),
  1198. LTC_TEST_FN(cipher_ctr),
  1199. LTC_TEST_FN(cipher_lrw),
  1200. LTC_TEST_FN(hash),
  1201. LTC_TEST_FN(macs),
  1202. LTC_TEST_FN(encmacs),
  1203. LTC_TEST_FN(prng),
  1204. LTC_TEST_FN(mult),
  1205. LTC_TEST_FN(sqr),
  1206. LTC_TEST_FN(rsa),
  1207. LTC_TEST_FN(dsa),
  1208. LTC_TEST_FN(ecc),
  1209. LTC_TEST_FN(dh),
  1210. };
  1211. char *single_test = NULL;
  1212. unsigned int i;
  1213. const char* mpi_provider = NULL;
  1214. if (argc > 1) {
  1215. if (strstr(argv[1], "-h")) {
  1216. die(EXIT_SUCCESS);
  1217. } else if (strstr(argv[1], "-l")) {
  1218. for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
  1219. printf("%s\n", test_functions[i].name);
  1220. }
  1221. exit(0);
  1222. }
  1223. }
  1224. init_timer();
  1225. register_all_ciphers();
  1226. register_all_hashes();
  1227. register_all_prngs();
  1228. #ifdef USE_LTM
  1229. mpi_provider = "ltm";
  1230. #elif defined(USE_TFM)
  1231. mpi_provider = "tfm";
  1232. #elif defined(USE_GMP)
  1233. mpi_provider = "gmp";
  1234. #elif defined(EXT_MATH_LIB)
  1235. mpi_provider = "ext";
  1236. #endif
  1237. if (argc > 2) {
  1238. mpi_provider = argv[2];
  1239. }
  1240. if (crypt_mp_init(mpi_provider) != CRYPT_OK) {
  1241. fprintf(stderr, "Init of MPI provider \"%s\" failed\n", mpi_provider ? mpi_provider : "(null)");
  1242. }
  1243. if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
  1244. fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
  1245. exit(EXIT_FAILURE);
  1246. }
  1247. /* single test name from commandline */
  1248. if (argc > 1) single_test = argv[1];
  1249. for (i = 0; i < sizeof(test_functions)/sizeof(test_functions[0]); ++i) {
  1250. if (single_test && strstr(test_functions[i].name, single_test) == NULL) {
  1251. continue;
  1252. }
  1253. test_functions[i].fn();
  1254. }
  1255. return EXIT_SUCCESS;
  1256. }