timing.c 41 KB

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