timing.c 41 KB

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