timing.c 43 KB

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