bench.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. int failures = 0;
  423. for (int j = 0; j < N; ++j) {
  424. switch (alg) {
  425. case DIGEST_SHA1:
  426. failures += crypto_digest(out, buf, lens[i]) < 0;
  427. break;
  428. case DIGEST_SHA256:
  429. case DIGEST_SHA3_256:
  430. failures += crypto_digest256(out, buf, lens[i], alg) < 0;
  431. break;
  432. case DIGEST_SHA512:
  433. case DIGEST_SHA3_512:
  434. failures += crypto_digest512(out, buf, lens[i], alg) < 0;
  435. break;
  436. default:
  437. tor_assert(0);
  438. }
  439. }
  440. end = perftime();
  441. printf("%s(%d): %.2f ns per call\n",
  442. crypto_digest_algorithm_get_name(alg),
  443. lens[i], NANOCOUNT(start,end,N));
  444. if (failures)
  445. printf("ERROR: crypto_digest failed %d times.\n", failures);
  446. }
  447. }
  448. }
  449. static void
  450. bench_cell_ops(void)
  451. {
  452. const int iters = 1<<16;
  453. int i;
  454. /* benchmarks for cell ops at relay. */
  455. or_circuit_t *or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  456. cell_t *cell = tor_malloc(sizeof(cell_t));
  457. int outbound;
  458. uint64_t start, end;
  459. crypto_rand((char*)cell->payload, sizeof(cell->payload));
  460. /* Mock-up or_circuit_t */
  461. or_circ->base_.magic = OR_CIRCUIT_MAGIC;
  462. or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  463. /* Initialize crypto */
  464. char key1[CIPHER_KEY_LEN], key2[CIPHER_KEY_LEN];
  465. crypto_rand(key1, sizeof(key1));
  466. crypto_rand(key2, sizeof(key2));
  467. or_circ->crypto.f_crypto = crypto_cipher_new(key1);
  468. or_circ->crypto.b_crypto = crypto_cipher_new(key2);
  469. or_circ->crypto.f_digest = crypto_digest_new();
  470. or_circ->crypto.b_digest = crypto_digest_new();
  471. reset_perftime();
  472. for (outbound = 0; outbound <= 1; ++outbound) {
  473. cell_direction_t d = outbound ? CELL_DIRECTION_OUT : CELL_DIRECTION_IN;
  474. start = perftime();
  475. for (i = 0; i < iters; ++i) {
  476. char recognized = 0;
  477. crypt_path_t *layer_hint = NULL;
  478. relay_decrypt_cell(TO_CIRCUIT(or_circ), cell, d,
  479. &layer_hint, &recognized);
  480. }
  481. end = perftime();
  482. printf("%sbound cells: %.2f ns per cell. (%.2f ns per byte of payload)\n",
  483. outbound?"Out":" In",
  484. NANOCOUNT(start,end,iters),
  485. NANOCOUNT(start,end,iters*CELL_PAYLOAD_SIZE));
  486. }
  487. relay_crypto_clear(&or_circ->crypto);
  488. tor_free(or_circ);
  489. tor_free(cell);
  490. }
  491. static void
  492. bench_dh(void)
  493. {
  494. const int iters = 1<<10;
  495. int i;
  496. uint64_t start, end;
  497. reset_perftime();
  498. start = perftime();
  499. for (i = 0; i < iters; ++i) {
  500. char dh_pubkey_a[DH1024_KEY_LEN], dh_pubkey_b[DH1024_KEY_LEN];
  501. char secret_a[DH1024_KEY_LEN], secret_b[DH1024_KEY_LEN];
  502. ssize_t slen_a, slen_b;
  503. crypto_dh_t *dh_a = crypto_dh_new(DH_TYPE_TLS);
  504. crypto_dh_t *dh_b = crypto_dh_new(DH_TYPE_TLS);
  505. crypto_dh_generate_public(dh_a);
  506. crypto_dh_generate_public(dh_b);
  507. crypto_dh_get_public(dh_a, dh_pubkey_a, sizeof(dh_pubkey_a));
  508. crypto_dh_get_public(dh_b, dh_pubkey_b, sizeof(dh_pubkey_b));
  509. slen_a = crypto_dh_compute_secret(LOG_NOTICE,
  510. dh_a, dh_pubkey_b, sizeof(dh_pubkey_b),
  511. secret_a, sizeof(secret_a));
  512. slen_b = crypto_dh_compute_secret(LOG_NOTICE,
  513. dh_b, dh_pubkey_a, sizeof(dh_pubkey_a),
  514. secret_b, sizeof(secret_b));
  515. tor_assert(slen_a == slen_b);
  516. tor_assert(fast_memeq(secret_a, secret_b, slen_a));
  517. crypto_dh_free(dh_a);
  518. crypto_dh_free(dh_b);
  519. }
  520. end = perftime();
  521. printf("Complete DH handshakes (1024 bit, public and private ops):\n"
  522. " %f millisec each.\n", NANOCOUNT(start, end, iters)/1e6);
  523. }
  524. #ifdef ENABLE_OPENSSL
  525. static void
  526. bench_ecdh_impl(int nid, const char *name)
  527. {
  528. const int iters = 1<<10;
  529. int i;
  530. uint64_t start, end;
  531. reset_perftime();
  532. start = perftime();
  533. for (i = 0; i < iters; ++i) {
  534. char secret_a[DH1024_KEY_LEN], secret_b[DH1024_KEY_LEN];
  535. ssize_t slen_a, slen_b;
  536. EC_KEY *dh_a = EC_KEY_new_by_curve_name(nid);
  537. EC_KEY *dh_b = EC_KEY_new_by_curve_name(nid);
  538. if (!dh_a || !dh_b) {
  539. puts("Skipping. (No implementation?)");
  540. return;
  541. }
  542. EC_KEY_generate_key(dh_a);
  543. EC_KEY_generate_key(dh_b);
  544. slen_a = ECDH_compute_key(secret_a, DH1024_KEY_LEN,
  545. EC_KEY_get0_public_key(dh_b), dh_a,
  546. NULL);
  547. slen_b = ECDH_compute_key(secret_b, DH1024_KEY_LEN,
  548. EC_KEY_get0_public_key(dh_a), dh_b,
  549. NULL);
  550. tor_assert(slen_a == slen_b);
  551. tor_assert(fast_memeq(secret_a, secret_b, slen_a));
  552. EC_KEY_free(dh_a);
  553. EC_KEY_free(dh_b);
  554. }
  555. end = perftime();
  556. printf("Complete ECDH %s handshakes (2 public and 2 private ops):\n"
  557. " %f millisec each.\n", name, NANOCOUNT(start, end, iters)/1e6);
  558. }
  559. static void
  560. bench_ecdh_p256(void)
  561. {
  562. bench_ecdh_impl(NID_X9_62_prime256v1, "P-256");
  563. }
  564. static void
  565. bench_ecdh_p224(void)
  566. {
  567. bench_ecdh_impl(NID_secp224r1, "P-224");
  568. }
  569. #endif
  570. typedef void (*bench_fn)(void);
  571. typedef struct benchmark_t {
  572. const char *name;
  573. bench_fn fn;
  574. int enabled;
  575. } benchmark_t;
  576. #define ENT(s) { #s , bench_##s, 0 }
  577. static struct benchmark_t benchmarks[] = {
  578. ENT(dmap),
  579. ENT(siphash),
  580. ENT(digest),
  581. ENT(aes),
  582. ENT(onion_TAP),
  583. ENT(onion_ntor),
  584. ENT(ed25519),
  585. ENT(cell_aes),
  586. ENT(cell_ops),
  587. ENT(dh),
  588. #ifdef ENABLE_OPENSSL
  589. ENT(ecdh_p256),
  590. ENT(ecdh_p224),
  591. #endif
  592. {NULL,NULL,0}
  593. };
  594. static benchmark_t *
  595. find_benchmark(const char *name)
  596. {
  597. benchmark_t *b;
  598. for (b = benchmarks; b->name; ++b) {
  599. if (!strcmp(name, b->name)) {
  600. return b;
  601. }
  602. }
  603. return NULL;
  604. }
  605. /** Main entry point for benchmark code: parse the command line, and run
  606. * some benchmarks. */
  607. int
  608. main(int argc, const char **argv)
  609. {
  610. int i;
  611. int list=0, n_enabled=0;
  612. char *errmsg;
  613. or_options_t *options;
  614. tor_threads_init();
  615. tor_compress_init();
  616. init_logging(1);
  617. if (argc == 4 && !strcmp(argv[1], "diff")) {
  618. const int N = 200;
  619. char *f1 = read_file_to_str(argv[2], RFTS_BIN, NULL);
  620. char *f2 = read_file_to_str(argv[3], RFTS_BIN, NULL);
  621. if (! f1 || ! f2) {
  622. perror("X");
  623. return 1;
  624. }
  625. for (i = 0; i < N; ++i) {
  626. char *diff = consensus_diff_generate(f1, f2);
  627. tor_free(diff);
  628. }
  629. char *diff = consensus_diff_generate(f1, f2);
  630. printf("%s", diff);
  631. tor_free(f1);
  632. tor_free(f2);
  633. tor_free(diff);
  634. return 0;
  635. }
  636. for (i = 1; i < argc; ++i) {
  637. if (!strcmp(argv[i], "--list")) {
  638. list = 1;
  639. } else {
  640. benchmark_t *benchmark = find_benchmark(argv[i]);
  641. ++n_enabled;
  642. if (benchmark) {
  643. benchmark->enabled = 1;
  644. } else {
  645. printf("No such benchmark as %s\n", argv[i]);
  646. }
  647. }
  648. }
  649. reset_perftime();
  650. if (crypto_global_init(0, NULL, NULL) < 0) {
  651. printf("Couldn't seed RNG; exiting.\n");
  652. return 1;
  653. }
  654. init_protocol_warning_severity_level();
  655. options = options_new();
  656. init_logging(1);
  657. options->command = CMD_RUN_UNITTESTS;
  658. options->DataDirectory = tor_strdup("");
  659. options->KeyDirectory = tor_strdup("");
  660. options->CacheDirectory = tor_strdup("");
  661. options_init(options);
  662. if (set_options(options, &errmsg) < 0) {
  663. printf("Failed to set initial options: %s\n", errmsg);
  664. tor_free(errmsg);
  665. return 1;
  666. }
  667. for (benchmark_t *b = benchmarks; b->name; ++b) {
  668. if (b->enabled || n_enabled == 0) {
  669. printf("===== %s =====\n", b->name);
  670. if (!list)
  671. b->fn();
  672. }
  673. }
  674. return 0;
  675. }