bench.c 19 KB

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