timing.c 41 KB

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