bench.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file bench.c
  7. * \brief Benchmarks for lower level Tor modules.
  8. **/
  9. #include "orconfig.h"
  10. #include "or/or.h"
  11. #include "or/onion_tap.h"
  12. #include "or/relay_crypto.h"
  13. #include <openssl/opensslv.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/ec.h>
  16. #include <openssl/ecdh.h>
  17. #include <openssl/obj_mac.h>
  18. #include "or/config.h"
  19. #include "lib/crypt_ops/crypto_curve25519.h"
  20. #include "lib/crypt_ops/crypto_dh.h"
  21. #include "or/onion_ntor.h"
  22. #include "lib/crypt_ops/crypto_ed25519.h"
  23. #include "lib/crypt_ops/crypto_rand.h"
  24. #include "or/consdiff.h"
  25. #include "lib/compress/compress.h"
  26. #include "or/cell_st.h"
  27. #include "or/or_circuit_st.h"
  28. #include "lib/crypt_ops/digestset.h"
  29. #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
  30. static uint64_t nanostart;
  31. static inline uint64_t
  32. timespec_to_nsec(const struct timespec *ts)
  33. {
  34. return ((uint64_t)ts->tv_sec)*1000000000 + ts->tv_nsec;
  35. }
  36. static void
  37. reset_perftime(void)
  38. {
  39. struct timespec ts;
  40. int r;
  41. r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  42. tor_assert(r == 0);
  43. nanostart = timespec_to_nsec(&ts);
  44. }
  45. static uint64_t
  46. perftime(void)
  47. {
  48. struct timespec ts;
  49. int r;
  50. r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  51. tor_assert(r == 0);
  52. return timespec_to_nsec(&ts) - nanostart;
  53. }
  54. #else /* !(defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)) */
  55. static struct timeval tv_start = { 0, 0 };
  56. static void
  57. reset_perftime(void)
  58. {
  59. tor_gettimeofday(&tv_start);
  60. }
  61. static uint64_t
  62. perftime(void)
  63. {
  64. struct timeval now, out;
  65. tor_gettimeofday(&now);
  66. timersub(&now, &tv_start, &out);
  67. return ((uint64_t)out.tv_sec)*1000000000 + out.tv_usec*1000;
  68. }
  69. #endif /* defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID) */
  70. #define NANOCOUNT(start,end,iters) \
  71. ( ((double)((end)-(start))) / (iters) )
  72. #define MICROCOUNT(start,end,iters) \
  73. ( NANOCOUNT((start), (end), (iters)) / 1000.0 )
  74. /** Run AES performance benchmarks. */
  75. static void
  76. bench_aes(void)
  77. {
  78. int len, i;
  79. char *b1, *b2;
  80. crypto_cipher_t *c;
  81. uint64_t start, end;
  82. const int bytes_per_iter = (1<<24);
  83. reset_perftime();
  84. char key[CIPHER_KEY_LEN];
  85. crypto_rand(key, sizeof(key));
  86. c = crypto_cipher_new(key);
  87. for (len = 1; len <= 8192; len *= 2) {
  88. int iters = bytes_per_iter / len;
  89. b1 = tor_malloc_zero(len);
  90. b2 = tor_malloc_zero(len);
  91. start = perftime();
  92. for (i = 0; i < iters; ++i) {
  93. crypto_cipher_encrypt(c, b1, b2, len);
  94. }
  95. end = perftime();
  96. tor_free(b1);
  97. tor_free(b2);
  98. printf("%d bytes: %.2f nsec per byte\n", len,
  99. NANOCOUNT(start, end, iters*len));
  100. }
  101. crypto_cipher_free(c);
  102. }
  103. static void
  104. bench_onion_TAP(void)
  105. {
  106. const int iters = 1<<9;
  107. int i;
  108. crypto_pk_t *key, *key2;
  109. uint64_t start, end;
  110. char os[TAP_ONIONSKIN_CHALLENGE_LEN];
  111. char or[TAP_ONIONSKIN_REPLY_LEN];
  112. crypto_dh_t *dh_out = NULL;
  113. key = crypto_pk_new();
  114. key2 = crypto_pk_new();
  115. if (crypto_pk_generate_key_with_bits(key, 1024) < 0)
  116. goto done;
  117. if (crypto_pk_generate_key_with_bits(key2, 1024) < 0)
  118. goto done;
  119. reset_perftime();
  120. start = perftime();
  121. for (i = 0; i < iters; ++i) {
  122. onion_skin_TAP_create(key, &dh_out, os);
  123. crypto_dh_free(dh_out);
  124. }
  125. end = perftime();
  126. printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3);
  127. onion_skin_TAP_create(key, &dh_out, os);
  128. start = perftime();
  129. for (i = 0; i < iters; ++i) {
  130. char key_out[CPATH_KEY_MATERIAL_LEN];
  131. onion_skin_TAP_server_handshake(os, key, NULL, or,
  132. key_out, sizeof(key_out));
  133. }
  134. end = perftime();
  135. printf("Server-side, key guessed right: %f usec\n",
  136. NANOCOUNT(start, end, iters)/1e3);
  137. start = perftime();
  138. for (i = 0; i < iters; ++i) {
  139. char key_out[CPATH_KEY_MATERIAL_LEN];
  140. onion_skin_TAP_server_handshake(os, key2, key, or,
  141. key_out, sizeof(key_out));
  142. }
  143. end = perftime();
  144. printf("Server-side, key guessed wrong: %f usec.\n",
  145. NANOCOUNT(start, end, iters)/1e3);
  146. start = perftime();
  147. for (i = 0; i < iters; ++i) {
  148. crypto_dh_t *dh;
  149. char key_out[CPATH_KEY_MATERIAL_LEN];
  150. int s;
  151. dh = crypto_dh_dup(dh_out);
  152. s = onion_skin_TAP_client_handshake(dh, or, key_out, sizeof(key_out),
  153. NULL);
  154. crypto_dh_free(dh);
  155. tor_assert(s == 0);
  156. }
  157. end = perftime();
  158. printf("Client-side, part 2: %f usec.\n",
  159. NANOCOUNT(start, end, iters)/1e3);
  160. done:
  161. crypto_dh_free(dh_out);
  162. crypto_pk_free(key);
  163. crypto_pk_free(key2);
  164. }
  165. static void
  166. bench_onion_ntor_impl(void)
  167. {
  168. const int iters = 1<<10;
  169. int i;
  170. curve25519_keypair_t keypair1, keypair2;
  171. uint64_t start, end;
  172. uint8_t os[NTOR_ONIONSKIN_LEN];
  173. uint8_t or[NTOR_REPLY_LEN];
  174. ntor_handshake_state_t *state = NULL;
  175. uint8_t nodeid[DIGEST_LEN];
  176. di_digest256_map_t *keymap = NULL;
  177. curve25519_secret_key_generate(&keypair1.seckey, 0);
  178. curve25519_public_key_generate(&keypair1.pubkey, &keypair1.seckey);
  179. curve25519_secret_key_generate(&keypair2.seckey, 0);
  180. curve25519_public_key_generate(&keypair2.pubkey, &keypair2.seckey);
  181. dimap_add_entry(&keymap, keypair1.pubkey.public_key, &keypair1);
  182. dimap_add_entry(&keymap, keypair2.pubkey.public_key, &keypair2);
  183. crypto_rand((char *)nodeid, sizeof(nodeid));
  184. reset_perftime();
  185. start = perftime();
  186. for (i = 0; i < iters; ++i) {
  187. onion_skin_ntor_create(nodeid, &keypair1.pubkey, &state, os);
  188. ntor_handshake_state_free(state);
  189. state = NULL;
  190. }
  191. end = perftime();
  192. printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3);
  193. state = NULL;
  194. onion_skin_ntor_create(nodeid, &keypair1.pubkey, &state, os);
  195. start = perftime();
  196. for (i = 0; i < iters; ++i) {
  197. uint8_t key_out[CPATH_KEY_MATERIAL_LEN];
  198. onion_skin_ntor_server_handshake(os, keymap, NULL, nodeid, or,
  199. key_out, sizeof(key_out));
  200. }
  201. end = perftime();
  202. printf("Server-side: %f usec\n",
  203. NANOCOUNT(start, end, iters)/1e3);
  204. start = perftime();
  205. for (i = 0; i < iters; ++i) {
  206. uint8_t key_out[CPATH_KEY_MATERIAL_LEN];
  207. int s;
  208. s = onion_skin_ntor_client_handshake(state, or, key_out, sizeof(key_out),
  209. NULL);
  210. tor_assert(s == 0);
  211. }
  212. end = perftime();
  213. printf("Client-side, part 2: %f usec.\n",
  214. NANOCOUNT(start, end, iters)/1e3);
  215. ntor_handshake_state_free(state);
  216. dimap_free(keymap, NULL);
  217. }
  218. static void
  219. bench_onion_ntor(void)
  220. {
  221. int ed;
  222. for (ed = 0; ed <= 1; ++ed) {
  223. printf("Ed25519-based basepoint multiply = %s.\n",
  224. (ed == 0) ? "disabled" : "enabled");
  225. curve25519_set_impl_params(ed);
  226. bench_onion_ntor_impl();
  227. }
  228. }
  229. static void
  230. bench_ed25519_impl(void)
  231. {
  232. uint64_t start, end;
  233. const int iters = 1<<12;
  234. int i;
  235. const uint8_t msg[] = "but leaving, could not tell what they had heard";
  236. ed25519_signature_t sig;
  237. ed25519_keypair_t kp;
  238. curve25519_keypair_t curve_kp;
  239. ed25519_public_key_t pubkey_tmp;
  240. ed25519_secret_key_generate(&kp.seckey, 0);
  241. start = perftime();
  242. for (i = 0; i < iters; ++i) {
  243. ed25519_public_key_generate(&kp.pubkey, &kp.seckey);
  244. }
  245. end = perftime();
  246. printf("Generate public key: %.2f usec\n",
  247. MICROCOUNT(start, end, iters));
  248. start = perftime();
  249. for (i = 0; i < iters; ++i) {
  250. ed25519_sign(&sig, msg, sizeof(msg), &kp);
  251. }
  252. end = perftime();
  253. printf("Sign a short message: %.2f usec\n",
  254. MICROCOUNT(start, end, iters));
  255. start = perftime();
  256. for (i = 0; i < iters; ++i) {
  257. ed25519_checksig(&sig, msg, sizeof(msg), &kp.pubkey);
  258. }
  259. end = perftime();
  260. printf("Verify signature: %.2f usec\n",
  261. MICROCOUNT(start, end, iters));
  262. curve25519_keypair_generate(&curve_kp, 0);
  263. start = perftime();
  264. for (i = 0; i < iters; ++i) {
  265. ed25519_public_key_from_curve25519_public_key(&pubkey_tmp,
  266. &curve_kp.pubkey, 1);
  267. }
  268. end = perftime();
  269. printf("Convert public point from curve25519: %.2f usec\n",
  270. MICROCOUNT(start, end, iters));
  271. curve25519_keypair_generate(&curve_kp, 0);
  272. start = perftime();
  273. for (i = 0; i < iters; ++i) {
  274. ed25519_public_blind(&pubkey_tmp, &kp.pubkey, msg);
  275. }
  276. end = perftime();
  277. printf("Blind a public key: %.2f usec\n",
  278. MICROCOUNT(start, end, iters));
  279. }
  280. static void
  281. bench_ed25519(void)
  282. {
  283. int donna;
  284. for (donna = 0; donna <= 1; ++donna) {
  285. printf("Ed25519-donna = %s.\n",
  286. (donna == 0) ? "disabled" : "enabled");
  287. ed25519_set_impl_params(donna);
  288. bench_ed25519_impl();
  289. }
  290. }
  291. static void
  292. bench_cell_aes(void)
  293. {
  294. uint64_t start, end;
  295. const int len = 509;
  296. const int iters = (1<<16);
  297. const int max_misalign = 15;
  298. char *b = tor_malloc(len+max_misalign);
  299. crypto_cipher_t *c;
  300. int i, misalign;
  301. char key[CIPHER_KEY_LEN];
  302. crypto_rand(key, sizeof(key));
  303. c = crypto_cipher_new(key);
  304. reset_perftime();
  305. for (misalign = 0; misalign <= max_misalign; ++misalign) {
  306. start = perftime();
  307. for (i = 0; i < iters; ++i) {
  308. crypto_cipher_crypt_inplace(c, b+misalign, len);
  309. }
  310. end = perftime();
  311. printf("%d bytes, misaligned by %d: %.2f nsec per byte\n", len, misalign,
  312. NANOCOUNT(start, end, iters*len));
  313. }
  314. crypto_cipher_free(c);
  315. tor_free(b);
  316. }
  317. /** Run digestmap_t performance benchmarks. */
  318. static void
  319. bench_dmap(void)
  320. {
  321. smartlist_t *sl = smartlist_new();
  322. smartlist_t *sl2 = smartlist_new();
  323. uint64_t start, end, pt2, pt3, pt4;
  324. int iters = 8192;
  325. const int elts = 4000;
  326. const int fpostests = 100000;
  327. char d[20];
  328. int i,n=0, fp = 0;
  329. digestmap_t *dm = digestmap_new();
  330. digestset_t *ds = digestset_new(elts);
  331. for (i = 0; i < elts; ++i) {
  332. crypto_rand(d, 20);
  333. smartlist_add(sl, tor_memdup(d, 20));
  334. }
  335. for (i = 0; i < elts; ++i) {
  336. crypto_rand(d, 20);
  337. smartlist_add(sl2, tor_memdup(d, 20));
  338. }
  339. //printf("nbits=%d\n", ds->mask+1);
  340. reset_perftime();
  341. start = perftime();
  342. for (i = 0; i < iters; ++i) {
  343. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
  344. }
  345. pt2 = perftime();
  346. printf("digestmap_set: %.2f ns per element\n",
  347. NANOCOUNT(start, pt2, iters*elts));
  348. for (i = 0; i < iters; ++i) {
  349. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
  350. SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
  351. }
  352. pt3 = perftime();
  353. printf("digestmap_get: %.2f ns per element\n",
  354. NANOCOUNT(pt2, pt3, iters*elts*2));
  355. for (i = 0; i < iters; ++i) {
  356. SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
  357. }
  358. pt4 = perftime();
  359. printf("digestset_add: %.2f ns per element\n",
  360. NANOCOUNT(pt3, pt4, iters*elts));
  361. for (i = 0; i < iters; ++i) {
  362. SMARTLIST_FOREACH(sl, const char *, cp,
  363. n += digestset_probably_contains(ds, cp));
  364. SMARTLIST_FOREACH(sl2, const char *, cp,
  365. n += digestset_probably_contains(ds, cp));
  366. }
  367. end = perftime();
  368. printf("digestset_probably_contains: %.2f ns per element.\n",
  369. NANOCOUNT(pt4, end, iters*elts*2));
  370. /* We need to use this, or else the whole loop gets optimized out. */
  371. printf("Hits == %d\n", n);
  372. for (i = 0; i < fpostests; ++i) {
  373. crypto_rand(d, 20);
  374. if (digestset_probably_contains(ds, d)) ++fp;
  375. }
  376. printf("False positive rate on digestset: %.2f%%\n",
  377. (fp/(double)fpostests)*100);
  378. digestmap_free(dm, NULL);
  379. digestset_free(ds);
  380. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  381. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  382. smartlist_free(sl);
  383. smartlist_free(sl2);
  384. }
  385. static void
  386. bench_siphash(void)
  387. {
  388. char buf[128];
  389. int lens[] = { 7, 8, 15, 16, 20, 32, 111, 128, -1 };
  390. int i, j;
  391. uint64_t start, end;
  392. const int N = 300000;
  393. crypto_rand(buf, sizeof(buf));
  394. for (i = 0; lens[i] > 0; ++i) {
  395. reset_perftime();
  396. start = perftime();
  397. for (j = 0; j < N; ++j) {
  398. siphash24g(buf, lens[i]);
  399. }
  400. end = perftime();
  401. printf("siphash24g(%d): %.2f ns per call\n",
  402. lens[i], NANOCOUNT(start,end,N));
  403. }
  404. }
  405. static void
  406. bench_digest(void)
  407. {
  408. char buf[8192];
  409. char out[DIGEST512_LEN];
  410. const int lens[] = { 1, 16, 32, 64, 128, 512, 1024, 2048, -1 };
  411. const int N = 300000;
  412. uint64_t start, end;
  413. crypto_rand(buf, sizeof(buf));
  414. for (int alg = 0; alg < N_DIGEST_ALGORITHMS; alg++) {
  415. for (int i = 0; lens[i] > 0; ++i) {
  416. reset_perftime();
  417. start = perftime();
  418. for (int j = 0; j < N; ++j) {
  419. switch (alg) {
  420. case DIGEST_SHA1:
  421. crypto_digest(out, buf, lens[i]);
  422. break;
  423. case DIGEST_SHA256:
  424. case DIGEST_SHA3_256:
  425. crypto_digest256(out, buf, lens[i], alg);
  426. break;
  427. case DIGEST_SHA512:
  428. case DIGEST_SHA3_512:
  429. crypto_digest512(out, buf, lens[i], alg);
  430. break;
  431. default:
  432. tor_assert(0);
  433. }
  434. }
  435. end = perftime();
  436. printf("%s(%d): %.2f ns per call\n",
  437. crypto_digest_algorithm_get_name(alg),
  438. lens[i], NANOCOUNT(start,end,N));
  439. }
  440. }
  441. }
  442. static void
  443. bench_cell_ops(void)
  444. {
  445. const int iters = 1<<16;
  446. int i;
  447. /* benchmarks for cell ops at relay. */
  448. or_circuit_t *or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  449. cell_t *cell = tor_malloc(sizeof(cell_t));
  450. int outbound;
  451. uint64_t start, end;
  452. crypto_rand((char*)cell->payload, sizeof(cell->payload));
  453. /* Mock-up or_circuit_t */
  454. or_circ->base_.magic = OR_CIRCUIT_MAGIC;
  455. or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  456. /* Initialize crypto */
  457. char key1[CIPHER_KEY_LEN], key2[CIPHER_KEY_LEN];
  458. crypto_rand(key1, sizeof(key1));
  459. crypto_rand(key2, sizeof(key2));
  460. or_circ->crypto.f_crypto = crypto_cipher_new(key1);
  461. or_circ->crypto.b_crypto = crypto_cipher_new(key2);
  462. or_circ->crypto.f_digest = crypto_digest_new();
  463. or_circ->crypto.b_digest = crypto_digest_new();
  464. reset_perftime();
  465. for (outbound = 0; outbound <= 1; ++outbound) {
  466. cell_direction_t d = outbound ? CELL_DIRECTION_OUT : CELL_DIRECTION_IN;
  467. start = perftime();
  468. for (i = 0; i < iters; ++i) {
  469. char recognized = 0;
  470. crypt_path_t *layer_hint = NULL;
  471. relay_decrypt_cell(TO_CIRCUIT(or_circ), cell, d,
  472. &layer_hint, &recognized);
  473. }
  474. end = perftime();
  475. printf("%sbound cells: %.2f ns per cell. (%.2f ns per byte of payload)\n",
  476. outbound?"Out":" In",
  477. NANOCOUNT(start,end,iters),
  478. NANOCOUNT(start,end,iters*CELL_PAYLOAD_SIZE));
  479. }
  480. relay_crypto_clear(&or_circ->crypto);
  481. tor_free(or_circ);
  482. tor_free(cell);
  483. }
  484. static void
  485. bench_dh(void)
  486. {
  487. const int iters = 1<<10;
  488. int i;
  489. uint64_t start, end;
  490. reset_perftime();
  491. start = perftime();
  492. for (i = 0; i < iters; ++i) {
  493. char dh_pubkey_a[DH1024_KEY_LEN], dh_pubkey_b[DH1024_KEY_LEN];
  494. char secret_a[DH1024_KEY_LEN], secret_b[DH1024_KEY_LEN];
  495. ssize_t slen_a, slen_b;
  496. crypto_dh_t *dh_a = crypto_dh_new(DH_TYPE_TLS);
  497. crypto_dh_t *dh_b = crypto_dh_new(DH_TYPE_TLS);
  498. crypto_dh_generate_public(dh_a);
  499. crypto_dh_generate_public(dh_b);
  500. crypto_dh_get_public(dh_a, dh_pubkey_a, sizeof(dh_pubkey_a));
  501. crypto_dh_get_public(dh_b, dh_pubkey_b, sizeof(dh_pubkey_b));
  502. slen_a = crypto_dh_compute_secret(LOG_NOTICE,
  503. dh_a, dh_pubkey_b, sizeof(dh_pubkey_b),
  504. secret_a, sizeof(secret_a));
  505. slen_b = crypto_dh_compute_secret(LOG_NOTICE,
  506. dh_b, dh_pubkey_a, sizeof(dh_pubkey_a),
  507. secret_b, sizeof(secret_b));
  508. tor_assert(slen_a == slen_b);
  509. tor_assert(fast_memeq(secret_a, secret_b, slen_a));
  510. crypto_dh_free(dh_a);
  511. crypto_dh_free(dh_b);
  512. }
  513. end = perftime();
  514. printf("Complete DH handshakes (1024 bit, public and private ops):\n"
  515. " %f millisec each.\n", NANOCOUNT(start, end, iters)/1e6);
  516. }
  517. static void
  518. bench_ecdh_impl(int nid, const char *name)
  519. {
  520. const int iters = 1<<10;
  521. int i;
  522. uint64_t start, end;
  523. reset_perftime();
  524. start = perftime();
  525. for (i = 0; i < iters; ++i) {
  526. char secret_a[DH1024_KEY_LEN], secret_b[DH1024_KEY_LEN];
  527. ssize_t slen_a, slen_b;
  528. EC_KEY *dh_a = EC_KEY_new_by_curve_name(nid);
  529. EC_KEY *dh_b = EC_KEY_new_by_curve_name(nid);
  530. if (!dh_a || !dh_b) {
  531. puts("Skipping. (No implementation?)");
  532. return;
  533. }
  534. EC_KEY_generate_key(dh_a);
  535. EC_KEY_generate_key(dh_b);
  536. slen_a = ECDH_compute_key(secret_a, DH1024_KEY_LEN,
  537. EC_KEY_get0_public_key(dh_b), dh_a,
  538. NULL);
  539. slen_b = ECDH_compute_key(secret_b, DH1024_KEY_LEN,
  540. EC_KEY_get0_public_key(dh_a), dh_b,
  541. NULL);
  542. tor_assert(slen_a == slen_b);
  543. tor_assert(fast_memeq(secret_a, secret_b, slen_a));
  544. EC_KEY_free(dh_a);
  545. EC_KEY_free(dh_b);
  546. }
  547. end = perftime();
  548. printf("Complete ECDH %s handshakes (2 public and 2 private ops):\n"
  549. " %f millisec each.\n", name, NANOCOUNT(start, end, iters)/1e6);
  550. }
  551. static void
  552. bench_ecdh_p256(void)
  553. {
  554. bench_ecdh_impl(NID_X9_62_prime256v1, "P-256");
  555. }
  556. static void
  557. bench_ecdh_p224(void)
  558. {
  559. bench_ecdh_impl(NID_secp224r1, "P-224");
  560. }
  561. typedef void (*bench_fn)(void);
  562. typedef struct benchmark_t {
  563. const char *name;
  564. bench_fn fn;
  565. int enabled;
  566. } benchmark_t;
  567. #define ENT(s) { #s , bench_##s, 0 }
  568. static struct benchmark_t benchmarks[] = {
  569. ENT(dmap),
  570. ENT(siphash),
  571. ENT(digest),
  572. ENT(aes),
  573. ENT(onion_TAP),
  574. ENT(onion_ntor),
  575. ENT(ed25519),
  576. ENT(cell_aes),
  577. ENT(cell_ops),
  578. ENT(dh),
  579. ENT(ecdh_p256),
  580. ENT(ecdh_p224),
  581. {NULL,NULL,0}
  582. };
  583. static benchmark_t *
  584. find_benchmark(const char *name)
  585. {
  586. benchmark_t *b;
  587. for (b = benchmarks; b->name; ++b) {
  588. if (!strcmp(name, b->name)) {
  589. return b;
  590. }
  591. }
  592. return NULL;
  593. }
  594. /** Main entry point for benchmark code: parse the command line, and run
  595. * some benchmarks. */
  596. int
  597. main(int argc, const char **argv)
  598. {
  599. int i;
  600. int list=0, n_enabled=0;
  601. char *errmsg;
  602. or_options_t *options;
  603. tor_threads_init();
  604. tor_compress_init();
  605. if (argc == 4 && !strcmp(argv[1], "diff")) {
  606. init_logging(1);
  607. const int N = 200;
  608. char *f1 = read_file_to_str(argv[2], RFTS_BIN, NULL);
  609. char *f2 = read_file_to_str(argv[3], RFTS_BIN, NULL);
  610. if (! f1 || ! f2) {
  611. perror("X");
  612. return 1;
  613. }
  614. for (i = 0; i < N; ++i) {
  615. char *diff = consensus_diff_generate(f1, f2);
  616. tor_free(diff);
  617. }
  618. char *diff = consensus_diff_generate(f1, f2);
  619. printf("%s", diff);
  620. tor_free(f1);
  621. tor_free(f2);
  622. tor_free(diff);
  623. return 0;
  624. }
  625. for (i = 1; i < argc; ++i) {
  626. if (!strcmp(argv[i], "--list")) {
  627. list = 1;
  628. } else {
  629. benchmark_t *benchmark = find_benchmark(argv[i]);
  630. ++n_enabled;
  631. if (benchmark) {
  632. benchmark->enabled = 1;
  633. } else {
  634. printf("No such benchmark as %s\n", argv[i]);
  635. }
  636. }
  637. }
  638. reset_perftime();
  639. if (crypto_seed_rng() < 0) {
  640. printf("Couldn't seed RNG; exiting.\n");
  641. return 1;
  642. }
  643. init_protocol_warning_severity_level();
  644. crypto_init_siphash_key();
  645. options = options_new();
  646. init_logging(1);
  647. options->command = CMD_RUN_UNITTESTS;
  648. options->DataDirectory = tor_strdup("");
  649. options->KeyDirectory = tor_strdup("");
  650. options->CacheDirectory = tor_strdup("");
  651. options_init(options);
  652. if (set_options(options, &errmsg) < 0) {
  653. printf("Failed to set initial options: %s\n", errmsg);
  654. tor_free(errmsg);
  655. return 1;
  656. }
  657. for (benchmark_t *b = benchmarks; b->name; ++b) {
  658. if (b->enabled || n_enabled == 0) {
  659. printf("===== %s =====\n", b->name);
  660. if (!list)
  661. b->fn();
  662. }
  663. }
  664. return 0;
  665. }