timing.c 41 KB

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