bench.c 20 KB

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