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