timing.c 39 KB

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