bench.c 19 KB

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