x86_prof.c 38 KB

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