bench.c 20 KB

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