timing.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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. ltc_mp_init_multi(&a,&b,&c,NULL);
  454. for (x = 128/LTC_MP_DIGIT_BIT; x <= (unsigned long)1536/LTC_MP_DIGIT_BIT; x += 128/LTC_MP_DIGIT_BIT) {
  455. ltc_mp_rand(a, x);
  456. ltc_mp_rand(b, x);
  457. #define DO1 ltc_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*LTC_MP_DIGIT_BIT, t2);
  468. }
  469. ltc_mp_deinit_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. ltc_mp_init_multi(&a,&b,NULL);
  481. for (x = 128/LTC_MP_DIGIT_BIT; x <= (unsigned long)1536/LTC_MP_DIGIT_BIT; x += 128/LTC_MP_DIGIT_BIT) {
  482. ltc_mp_rand(a, x);
  483. #define DO1 ltc_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*LTC_MP_DIGIT_BIT, t2);
  494. }
  495. ltc_mp_deinit_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 < LTC_ARRAY_SIZE(groups); 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. const 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. ltc_ecc_sig_opts sig_opts = {
  788. .type = LTC_ECCSIG_RFC7518,
  789. .prng = &yarrow_prng,
  790. .wprng = find_prng ("yarrow")
  791. };
  792. if (ltc_mp.name == NULL) return;
  793. for (x = sizes[i=0]; x < 100000; x = sizes[++i]) {
  794. t2 = 0;
  795. for (y = 0; y < 256; y++) {
  796. t_start();
  797. t1 = t_read();
  798. if ((err = ecc_make_key(&yarrow_prng, find_prng("yarrow"), x, &key)) != CRYPT_OK) {
  799. 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));
  800. exit(EXIT_FAILURE);
  801. }
  802. t1 = t_read() - t1;
  803. t2 += t1;
  804. #ifdef LTC_PROFILE
  805. t2 <<= 8;
  806. break;
  807. #endif
  808. if (y < 255) {
  809. ecc_free(&key);
  810. }
  811. }
  812. t2 >>= 8;
  813. fprintf(stderr, "ECC-%lu make_key took %15"PRI64"u cycles\n", x*8, t2);
  814. t2 = 0;
  815. for (y = 0; y < 256; y++) {
  816. t_start();
  817. t1 = t_read();
  818. z = sizeof(buf[1]);
  819. if ((err = ecc_encrypt_key(buf[0], 20, buf[1], &z, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"),
  820. &key)) != CRYPT_OK) {
  821. 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));
  822. exit(EXIT_FAILURE);
  823. }
  824. t1 = t_read() - t1;
  825. t2 += t1;
  826. #ifdef LTC_PROFILE
  827. t2 <<= 8;
  828. break;
  829. #endif
  830. }
  831. t2 >>= 8;
  832. fprintf(stderr, "ECC-%lu encrypt_key took %15"PRI64"u cycles\n", x*8, t2);
  833. t2 = 0;
  834. for (y = 0; y < 256; y++) {
  835. t_start();
  836. t1 = t_read();
  837. w = 20;
  838. if ((err = ecc_decrypt_key(buf[1], z, buf[0], &w, &key)) != CRYPT_OK) {
  839. 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));
  840. exit(EXIT_FAILURE);
  841. }
  842. t1 = t_read() - t1;
  843. t2 += t1;
  844. #ifdef LTC_PROFILE
  845. t2 <<= 8;
  846. break;
  847. #endif
  848. }
  849. t2 >>= 8;
  850. fprintf(stderr, "ECC-%lu decrypt_key took %15"PRI64"u cycles\n", x*8, t2);
  851. t2 = 0;
  852. for (y = 0; y < 256; y++) {
  853. t_start();
  854. t1 = t_read();
  855. z = sizeof(buf[1]);
  856. if ((err = ecc_sign_hash_v2(buf[0], 20, buf[1], &z, &sig_opts, &key)) != CRYPT_OK) {
  857. 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));
  858. exit(EXIT_FAILURE);
  859. }
  860. t1 = t_read() - t1;
  861. t2 += t1;
  862. #ifdef LTC_PROFILE
  863. t2 <<= 8;
  864. break;
  865. #endif
  866. }
  867. t2 >>= 8;
  868. fprintf(stderr, "ECC-%lu sign_hash took %15"PRI64"u cycles\n", x*8, t2);
  869. t2 = 0;
  870. for (y = 0; y < 256; y++) {
  871. t_start();
  872. t1 = t_read();
  873. if ((err = ecc_verify_hash_v2(buf[1], z, buf[0], 20, &sig_opts, &stat, &key)) != CRYPT_OK) {
  874. 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));
  875. exit(EXIT_FAILURE);
  876. }
  877. if (stat == 0) {
  878. fprintf(stderr, "\n\necc_verify_hash for ECC-%lu failed to verify signature(%lu)\n", x*8, y);
  879. exit(EXIT_FAILURE);
  880. }
  881. t1 = t_read() - t1;
  882. t2 += t1;
  883. #ifdef LTC_PROFILE
  884. t2 <<= 8;
  885. break;
  886. #endif
  887. }
  888. t2 >>= 8;
  889. fprintf(stderr, "ECC-%lu verify_hash took %15"PRI64"u cycles\n", x*8, t2);
  890. fprintf(stderr, "\n\n");
  891. ecc_free(&key);
  892. }
  893. }
  894. #else
  895. static void time_ecc(void) { fprintf(stderr, "NO ECC\n"); }
  896. #endif
  897. static void time_macs_(unsigned long MAC_SIZE)
  898. {
  899. #if defined(LTC_OMAC) || defined(LTC_XCBC) || defined(LTC_F9_MODE) || defined(LTC_PMAC) || defined(LTC_PELICAN) || defined(LTC_HMAC)
  900. unsigned char *buf, key[16], tag[16];
  901. ulong64 t1, t2;
  902. unsigned long x, z;
  903. int err, cipher_idx, hash_idx;
  904. fprintf(stderr, "\nMAC Timings (cycles/byte on %luKB blocks):\n", MAC_SIZE);
  905. buf = XMALLOC(MAC_SIZE*1024);
  906. if (buf == NULL) {
  907. fprintf(stderr, "\n\nout of heap yo\n\n");
  908. exit(EXIT_FAILURE);
  909. }
  910. cipher_idx = find_cipher("aes");
  911. hash_idx = find_hash("sha1");
  912. if (cipher_idx == -1 || hash_idx == -1) {
  913. fprintf(stderr, "Warning the MAC tests requires AES and SHA1 to operate... so sorry\n");
  914. exit(EXIT_FAILURE);
  915. }
  916. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  917. yarrow_read(key, 16, &yarrow_prng);
  918. #ifdef LTC_OMAC
  919. t2 = -1;
  920. for (x = 0; x < 10000; x++) {
  921. t_start();
  922. t1 = t_read();
  923. z = 16;
  924. if ((err = omac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  925. fprintf(stderr, "\n\nomac-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  926. exit(EXIT_FAILURE);
  927. }
  928. t1 = t_read() - t1;
  929. if (t1 < t2) t2 = t1;
  930. }
  931. fprintf(stderr, "OMAC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  932. #endif
  933. #ifdef LTC_XCBC
  934. t2 = -1;
  935. for (x = 0; x < 10000; x++) {
  936. t_start();
  937. t1 = t_read();
  938. z = 16;
  939. if ((err = xcbc_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  940. fprintf(stderr, "\n\nxcbc-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  941. exit(EXIT_FAILURE);
  942. }
  943. t1 = t_read() - t1;
  944. if (t1 < t2) t2 = t1;
  945. }
  946. fprintf(stderr, "XCBC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  947. #endif
  948. #ifdef LTC_F9_MODE
  949. t2 = -1;
  950. for (x = 0; x < 10000; x++) {
  951. t_start();
  952. t1 = t_read();
  953. z = 16;
  954. if ((err = f9_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  955. fprintf(stderr, "\n\nF9-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  956. exit(EXIT_FAILURE);
  957. }
  958. t1 = t_read() - t1;
  959. if (t1 < t2) t2 = t1;
  960. }
  961. fprintf(stderr, "F9-%s\t\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  962. #endif
  963. #ifdef LTC_PMAC
  964. t2 = -1;
  965. for (x = 0; x < 10000; x++) {
  966. t_start();
  967. t1 = t_read();
  968. z = 16;
  969. if ((err = pmac_memory(cipher_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  970. fprintf(stderr, "\n\npmac-%s error... %s\n", cipher_descriptor[cipher_idx].name, error_to_string(err));
  971. exit(EXIT_FAILURE);
  972. }
  973. t1 = t_read() - t1;
  974. if (t1 < t2) t2 = t1;
  975. }
  976. fprintf(stderr, "PMAC-%s\t\t%9"PRI64"u\n", cipher_descriptor[cipher_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  977. #endif
  978. #ifdef LTC_PELICAN
  979. t2 = -1;
  980. for (x = 0; x < 10000; x++) {
  981. t_start();
  982. t1 = t_read();
  983. z = 16;
  984. if ((err = pelican_memory(key, 16, buf, MAC_SIZE*1024, tag)) != CRYPT_OK) {
  985. fprintf(stderr, "\n\npelican error... %s\n", error_to_string(err));
  986. exit(EXIT_FAILURE);
  987. }
  988. t1 = t_read() - t1;
  989. if (t1 < t2) t2 = t1;
  990. }
  991. fprintf(stderr, "PELICAN \t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  992. #endif
  993. #ifdef LTC_HMAC
  994. t2 = -1;
  995. for (x = 0; x < 10000; x++) {
  996. t_start();
  997. t1 = t_read();
  998. z = 16;
  999. if ((err = hmac_memory(hash_idx, key, 16, buf, MAC_SIZE*1024, tag, &z)) != CRYPT_OK) {
  1000. fprintf(stderr, "\n\nhmac-%s error... %s\n", hash_descriptor[hash_idx].name, error_to_string(err));
  1001. exit(EXIT_FAILURE);
  1002. }
  1003. t1 = t_read() - t1;
  1004. if (t1 < t2) t2 = t1;
  1005. }
  1006. fprintf(stderr, "HMAC-%s\t\t%9"PRI64"u\n", hash_descriptor[hash_idx].name, t2/(ulong64)(MAC_SIZE*1024));
  1007. #endif
  1008. XFREE(buf);
  1009. #else
  1010. LTC_UNUSED_PARAM(MAC_SIZE);
  1011. fprintf(stderr, "NO MACs\n");
  1012. #endif
  1013. }
  1014. static void time_macs(void)
  1015. {
  1016. time_macs_(1);
  1017. time_macs_(4);
  1018. time_macs_(32);
  1019. }
  1020. static void time_encmacs_(unsigned long MAC_SIZE)
  1021. {
  1022. #if defined(LTC_EAX_MODE) || defined(LTC_OCB_MODE) || defined(LTC_OCB3_MODE) || \
  1023. defined(LTC_CCM_MODE) || defined(LTC_GCM_MODE) || defined(LTC_SIV_MODE)
  1024. #if defined(LTC_SIV_MODE)
  1025. unsigned char *aad[4];
  1026. unsigned long buflen;
  1027. #endif
  1028. unsigned char *buf, IV[16], key[32], tag[16];
  1029. ulong64 t1, t2;
  1030. unsigned long x, z;
  1031. int err, cipher_idx;
  1032. symmetric_ECB skey;
  1033. fprintf(stderr, "\nENC+MAC Timings (zero byte AAD, 16 byte IV, cycles/byte on %luKB blocks):\n", MAC_SIZE);
  1034. buf = XMALLOC(MAC_SIZE*1024);
  1035. if (buf == NULL) {
  1036. fprintf(stderr, "\n\nout of heap yo\n\n");
  1037. exit(EXIT_FAILURE);
  1038. }
  1039. cipher_idx = find_cipher("aes");
  1040. yarrow_read(buf, MAC_SIZE*1024, &yarrow_prng);
  1041. yarrow_read(key, sizeof(key), &yarrow_prng);
  1042. yarrow_read(IV, sizeof(IV), &yarrow_prng);
  1043. #ifdef LTC_EAX_MODE
  1044. t2 = -1;
  1045. for (x = 0; x < 10000; x++) {
  1046. t_start();
  1047. t1 = t_read();
  1048. z = 16;
  1049. if ((err = eax_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1050. fprintf(stderr, "\nEAX error... %s\n", error_to_string(err));
  1051. exit(EXIT_FAILURE);
  1052. }
  1053. t1 = t_read() - t1;
  1054. if (t1 < t2) t2 = t1;
  1055. }
  1056. fprintf(stderr, "EAX \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1057. #endif
  1058. #ifdef LTC_OCB_MODE
  1059. t2 = -1;
  1060. for (x = 0; x < 10000; x++) {
  1061. t_start();
  1062. t1 = t_read();
  1063. z = 16;
  1064. if ((err = ocb_encrypt_authenticate_memory(cipher_idx, key, 16, IV, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1065. fprintf(stderr, "\nOCB error... %s\n", error_to_string(err));
  1066. exit(EXIT_FAILURE);
  1067. }
  1068. t1 = t_read() - t1;
  1069. if (t1 < t2) t2 = t1;
  1070. }
  1071. fprintf(stderr, "OCB \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1072. #endif
  1073. #ifdef LTC_OCB3_MODE
  1074. t2 = -1;
  1075. for (x = 0; x < 10000; x++) {
  1076. t_start();
  1077. t1 = t_read();
  1078. z = 16;
  1079. if ((err = ocb3_encrypt_authenticate_memory(cipher_idx, key, 16, IV, 15, (unsigned char*)"", 0, buf, MAC_SIZE*1024, buf, tag, &z)) != CRYPT_OK) {
  1080. fprintf(stderr, "\nOCB3 error... %s\n", error_to_string(err));
  1081. exit(EXIT_FAILURE);
  1082. }
  1083. t1 = t_read() - t1;
  1084. if (t1 < t2) t2 = t1;
  1085. }
  1086. fprintf(stderr, "OCB3 \t\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1087. #endif
  1088. #ifdef LTC_CCM_MODE
  1089. t2 = -1;
  1090. for (x = 0; x < 10000; x++) {
  1091. t_start();
  1092. t1 = t_read();
  1093. z = 16;
  1094. if ((err = ccm_memory(cipher_idx, key, 16, NULL, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  1095. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  1096. exit(EXIT_FAILURE);
  1097. }
  1098. t1 = t_read() - t1;
  1099. if (t1 < t2) t2 = t1;
  1100. }
  1101. fprintf(stderr, "CCM (no-precomp) \t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1102. ecb_start(cipher_idx, key, 16, 0, &skey);
  1103. t2 = -1;
  1104. for (x = 0; x < 10000; x++) {
  1105. t_start();
  1106. t1 = t_read();
  1107. z = 16;
  1108. if ((err = ccm_memory(cipher_idx, key, 16, &skey, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, CCM_ENCRYPT)) != CRYPT_OK) {
  1109. fprintf(stderr, "\nCCM error... %s\n", error_to_string(err));
  1110. exit(EXIT_FAILURE);
  1111. }
  1112. t1 = t_read() - t1;
  1113. if (t1 < t2) t2 = t1;
  1114. }
  1115. fprintf(stderr, "CCM (precomp) \t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1116. ecb_done(&skey);
  1117. #endif
  1118. #ifdef LTC_GCM_MODE
  1119. t2 = -1;
  1120. for (x = 0; x < 100; x++) {
  1121. t_start();
  1122. t1 = t_read();
  1123. z = 16;
  1124. if ((err = gcm_memory(cipher_idx, key, 16, IV, 16, NULL, 0, buf, MAC_SIZE*1024, buf, tag, &z, GCM_ENCRYPT)) != CRYPT_OK) {
  1125. fprintf(stderr, "\nGCM error... %s\n", error_to_string(err));
  1126. exit(EXIT_FAILURE);
  1127. }
  1128. t1 = t_read() - t1;
  1129. if (t1 < t2) t2 = t1;
  1130. }
  1131. fprintf(stderr, "GCM (no-precomp)\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1132. {
  1133. gcm_state gcm
  1134. #ifdef LTC_GCM_TABLES_SSE2
  1135. __attribute__ ((aligned (16)))
  1136. #endif
  1137. ;
  1138. if ((err = gcm_init(&gcm, cipher_idx, key, 16)) != CRYPT_OK) { fprintf(stderr, "gcm_init: %s\n", error_to_string(err)); exit(EXIT_FAILURE); }
  1139. t2 = -1;
  1140. for (x = 0; x < 10000; x++) {
  1141. t_start();
  1142. t1 = t_read();
  1143. z = 16;
  1144. if ((err = gcm_reset(&gcm)) != CRYPT_OK) {
  1145. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1146. exit(EXIT_FAILURE);
  1147. }
  1148. if ((err = gcm_add_iv(&gcm, IV, 16)) != CRYPT_OK) {
  1149. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1150. exit(EXIT_FAILURE);
  1151. }
  1152. if ((err = gcm_add_aad(&gcm, NULL, 0)) != CRYPT_OK) {
  1153. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1154. exit(EXIT_FAILURE);
  1155. }
  1156. if ((err = gcm_process(&gcm, buf, MAC_SIZE*1024, buf, GCM_ENCRYPT)) != CRYPT_OK) {
  1157. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1158. exit(EXIT_FAILURE);
  1159. }
  1160. if ((err = gcm_done(&gcm, tag, &z)) != CRYPT_OK) {
  1161. fprintf(stderr, "\nGCM error[%d]... %s\n", __LINE__, error_to_string(err));
  1162. exit(EXIT_FAILURE);
  1163. }
  1164. t1 = t_read() - t1;
  1165. if (t1 < t2) t2 = t1;
  1166. }
  1167. fprintf(stderr, "GCM (precomp)\t\t%9"PRI64"u\n", t2/(ulong64)(MAC_SIZE*1024));
  1168. }
  1169. #endif
  1170. #ifdef LTC_SIV_MODE
  1171. for(z = 0; z < 4; z++) {
  1172. aad[z] = IV + z * 4;
  1173. }
  1174. for(z = 0; z < 4; z++) {
  1175. t2 = -1;
  1176. for (x = 0; x < 10000; x++) {
  1177. buflen = MAC_SIZE*1024;
  1178. t_start();
  1179. t1 = t_read();
  1180. if ((err = siv_memory(cipher_idx, LTC_ENCRYPT,
  1181. key, 32,
  1182. buf, MAC_SIZE*1024 - 16,
  1183. buf, &buflen,
  1184. aad[0], 16,
  1185. aad[1], 12,
  1186. aad[2], 8,
  1187. aad[3], 4,
  1188. NULL)) != CRYPT_OK) {
  1189. fprintf(stderr, "\nSIV error... %s\n", error_to_string(err));
  1190. exit(EXIT_FAILURE);
  1191. }
  1192. t1 = t_read() - t1;
  1193. if (t1 < t2) t2 = t1;
  1194. }
  1195. aad[3-z] = NULL;
  1196. fprintf(stderr, "SIV (%lu x AAD)\t\t%9"PRI64"u\n", 4-z, t2/(ulong64)(MAC_SIZE*1024));
  1197. }
  1198. #endif
  1199. XFREE(buf);
  1200. #else
  1201. LTC_UNUSED_PARAM(MAC_SIZE);
  1202. fprintf(stderr, "NO ENCMACs\n");
  1203. #endif
  1204. }
  1205. static void time_encmacs(void)
  1206. {
  1207. time_encmacs_(1);
  1208. time_encmacs_(4);
  1209. time_encmacs_(32);
  1210. }
  1211. static void LTC_NORETURN die(int status)
  1212. {
  1213. FILE* o = status == EXIT_SUCCESS ? stdout : stderr;
  1214. fprintf(o,
  1215. "Usage: timing [<-h|-l|alg>] [mpi]\n\n"
  1216. "Run timing tests of all built-in algorithms, or only the one given in <alg>.\n\n"
  1217. "\talg\tThe algorithm to test. Use the '-l' option to check for valid values.\n"
  1218. "\tmpi\tThe MPI provider to use.\n"
  1219. "\t-l\tList all built-in algorithms that can be timed.\n"
  1220. "\t-h\tThe help you're looking at.\n"
  1221. );
  1222. exit(status);
  1223. }
  1224. #define LTC_TEST_FN(f) { time_ ## f, #f }
  1225. int main(int argc, char **argv)
  1226. {
  1227. int err;
  1228. const struct
  1229. {
  1230. void (*fn)(void);
  1231. const char* name;
  1232. } test_functions[] = {
  1233. LTC_TEST_FN(keysched),
  1234. LTC_TEST_FN(cipher_ecb),
  1235. LTC_TEST_FN(cipher_cbc),
  1236. LTC_TEST_FN(cipher_ctr),
  1237. LTC_TEST_FN(cipher_lrw),
  1238. LTC_TEST_FN(hash),
  1239. LTC_TEST_FN(macs),
  1240. LTC_TEST_FN(encmacs),
  1241. LTC_TEST_FN(prng),
  1242. LTC_TEST_FN(mult),
  1243. LTC_TEST_FN(sqr),
  1244. LTC_TEST_FN(rsa),
  1245. LTC_TEST_FN(dsa),
  1246. LTC_TEST_FN(ecc),
  1247. LTC_TEST_FN(dh),
  1248. };
  1249. char *single_test = NULL;
  1250. unsigned int i;
  1251. const char* mpi_provider = NULL;
  1252. if (argc > 1) {
  1253. if (strstr(argv[1], "-h")) {
  1254. die(EXIT_SUCCESS);
  1255. } else if (strstr(argv[1], "-l")) {
  1256. for (i = 0; i < LTC_ARRAY_SIZE(test_functions); ++i) {
  1257. printf("%s\n", test_functions[i].name);
  1258. }
  1259. exit(0);
  1260. }
  1261. }
  1262. init_timer();
  1263. register_all_ciphers();
  1264. register_all_hashes();
  1265. register_all_prngs();
  1266. #ifdef USE_LTM
  1267. mpi_provider = "ltm";
  1268. #elif defined(USE_TFM)
  1269. mpi_provider = "tfm";
  1270. #elif defined(USE_GMP)
  1271. mpi_provider = "gmp";
  1272. #elif defined(EXT_MATH_LIB)
  1273. mpi_provider = "ext";
  1274. #endif
  1275. if (argc > 2) {
  1276. mpi_provider = argv[2];
  1277. }
  1278. if (crypt_mp_init(mpi_provider) != CRYPT_OK) {
  1279. fprintf(stderr, "Init of MPI provider \"%s\" failed\n", mpi_provider ? mpi_provider : "(null)");
  1280. }
  1281. if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) {
  1282. fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err));
  1283. exit(EXIT_FAILURE);
  1284. }
  1285. /* single test name from commandline */
  1286. if (argc > 1) single_test = argv[1];
  1287. for (i = 0; i < LTC_ARRAY_SIZE(test_functions); ++i) {
  1288. if (single_test && strstr(test_functions[i].name, single_test) == NULL) {
  1289. continue;
  1290. }
  1291. test_functions[i].fn();
  1292. }
  1293. return EXIT_SUCCESS;
  1294. }