timing.c 42 KB

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