timing.c 38 KB

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