bench.c 21 KB

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