bench.c 18 KB

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