bench.c 19 KB

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