bench.c 23 KB

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