bench.c 18 KB

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