x86_prof.c 39 KB

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