bench.c 20 KB

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