I2C toy code
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

timing.c 41 KiB

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