bench.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /* Ordinarily defined in tor_main.c; this bit is just here to provide one
  6. * since we're not linking to tor_main.c */
  7. const char tor_git_revision[] = "";
  8. /**
  9. * \file bench.c
  10. * \brief Benchmarks for lower level Tor modules.
  11. **/
  12. #include "orconfig.h"
  13. #include "or.h"
  14. #include "onion_tap.h"
  15. #include "relay.h"
  16. #include <openssl/opensslv.h>
  17. #include <openssl/evp.h>
  18. #ifndef OPENSSL_NO_EC
  19. #include <openssl/ec.h>
  20. #include <openssl/ecdh.h>
  21. #include <openssl/obj_mac.h>
  22. #endif
  23. #include "config.h"
  24. #ifdef CURVE25519_ENABLED
  25. #include "crypto_curve25519.h"
  26. #include "onion_ntor.h"
  27. #endif
  28. #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
  29. static uint64_t nanostart;
  30. static inline uint64_t
  31. timespec_to_nsec(const struct timespec *ts)
  32. {
  33. return ((uint64_t)ts->tv_sec)*1000000000 + ts->tv_nsec;
  34. }
  35. static void
  36. reset_perftime(void)
  37. {
  38. struct timespec ts;
  39. int r;
  40. r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  41. tor_assert(r == 0);
  42. nanostart = timespec_to_nsec(&ts);
  43. }
  44. static uint64_t
  45. 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. return timespec_to_nsec(&ts) - nanostart;
  52. }
  53. #else
  54. static struct timeval tv_start = { 0, 0 };
  55. static void
  56. reset_perftime(void)
  57. {
  58. tor_gettimeofday(&tv_start);
  59. }
  60. static uint64_t
  61. perftime(void)
  62. {
  63. struct timeval now, out;
  64. tor_gettimeofday(&now);
  65. timersub(&now, &tv_start, &out);
  66. return ((uint64_t)out.tv_sec)*1000000000 + out.tv_usec*1000;
  67. }
  68. #endif
  69. #define NANOCOUNT(start,end,iters) \
  70. ( ((double)((end)-(start))) / (iters) )
  71. /** Run AES performance benchmarks. */
  72. static void
  73. bench_aes(void)
  74. {
  75. int len, i;
  76. char *b1, *b2;
  77. crypto_cipher_t *c;
  78. uint64_t start, end;
  79. const int bytes_per_iter = (1<<24);
  80. reset_perftime();
  81. c = crypto_cipher_new(NULL);
  82. for (len = 1; len <= 8192; len *= 2) {
  83. int iters = bytes_per_iter / len;
  84. b1 = tor_malloc_zero(len);
  85. b2 = tor_malloc_zero(len);
  86. start = perftime();
  87. for (i = 0; i < iters; ++i) {
  88. crypto_cipher_encrypt(c, b1, b2, len);
  89. }
  90. end = perftime();
  91. tor_free(b1);
  92. tor_free(b2);
  93. printf("%d bytes: %.2f nsec per byte\n", len,
  94. NANOCOUNT(start, end, iters*len));
  95. }
  96. crypto_cipher_free(c);
  97. }
  98. static void
  99. bench_onion_TAP(void)
  100. {
  101. const int iters = 1<<9;
  102. int i;
  103. crypto_pk_t *key, *key2;
  104. uint64_t start, end;
  105. char os[TAP_ONIONSKIN_CHALLENGE_LEN];
  106. char or[TAP_ONIONSKIN_REPLY_LEN];
  107. crypto_dh_t *dh_out;
  108. key = crypto_pk_new();
  109. key2 = crypto_pk_new();
  110. if (crypto_pk_generate_key_with_bits(key, 1024) < 0)
  111. goto done;
  112. if (crypto_pk_generate_key_with_bits(key2, 1024) < 0)
  113. goto done;
  114. reset_perftime();
  115. start = perftime();
  116. for (i = 0; i < iters; ++i) {
  117. onion_skin_TAP_create(key, &dh_out, os);
  118. crypto_dh_free(dh_out);
  119. }
  120. end = perftime();
  121. printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3);
  122. onion_skin_TAP_create(key, &dh_out, os);
  123. start = perftime();
  124. for (i = 0; i < iters; ++i) {
  125. char key_out[CPATH_KEY_MATERIAL_LEN];
  126. onion_skin_TAP_server_handshake(os, key, NULL, or,
  127. key_out, sizeof(key_out));
  128. }
  129. end = perftime();
  130. printf("Server-side, key guessed right: %f usec\n",
  131. NANOCOUNT(start, end, iters)/1e3);
  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, key2, key, or,
  136. key_out, sizeof(key_out));
  137. }
  138. end = perftime();
  139. printf("Server-side, key guessed wrong: %f usec.\n",
  140. NANOCOUNT(start, end, iters)/1e3);
  141. start = perftime();
  142. for (i = 0; i < iters; ++i) {
  143. crypto_dh_t *dh;
  144. char key_out[CPATH_KEY_MATERIAL_LEN];
  145. int s;
  146. dh = crypto_dh_dup(dh_out);
  147. s = onion_skin_TAP_client_handshake(dh, or, key_out, sizeof(key_out),
  148. NULL);
  149. crypto_dh_free(dh);
  150. tor_assert(s == 0);
  151. }
  152. end = perftime();
  153. printf("Client-side, part 2: %f usec.\n",
  154. NANOCOUNT(start, end, iters)/1e3);
  155. done:
  156. crypto_pk_free(key);
  157. crypto_pk_free(key2);
  158. }
  159. #ifdef CURVE25519_ENABLED
  160. static void
  161. bench_onion_ntor(void)
  162. {
  163. const int iters = 1<<10;
  164. int i;
  165. curve25519_keypair_t keypair1, keypair2;
  166. uint64_t start, end;
  167. uint8_t os[NTOR_ONIONSKIN_LEN];
  168. uint8_t or[NTOR_REPLY_LEN];
  169. ntor_handshake_state_t *state = NULL;
  170. uint8_t nodeid[DIGEST_LEN];
  171. di_digest256_map_t *keymap = NULL;
  172. curve25519_secret_key_generate(&keypair1.seckey, 0);
  173. curve25519_public_key_generate(&keypair1.pubkey, &keypair1.seckey);
  174. curve25519_secret_key_generate(&keypair2.seckey, 0);
  175. curve25519_public_key_generate(&keypair2.pubkey, &keypair2.seckey);
  176. dimap_add_entry(&keymap, keypair1.pubkey.public_key, &keypair1);
  177. dimap_add_entry(&keymap, keypair2.pubkey.public_key, &keypair2);
  178. reset_perftime();
  179. start = perftime();
  180. for (i = 0; i < iters; ++i) {
  181. onion_skin_ntor_create(nodeid, &keypair1.pubkey, &state, os);
  182. ntor_handshake_state_free(state);
  183. state = NULL;
  184. }
  185. end = perftime();
  186. printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3);
  187. state = NULL;
  188. onion_skin_ntor_create(nodeid, &keypair1.pubkey, &state, os);
  189. start = perftime();
  190. for (i = 0; i < iters; ++i) {
  191. uint8_t key_out[CPATH_KEY_MATERIAL_LEN];
  192. onion_skin_ntor_server_handshake(os, keymap, NULL, nodeid, or,
  193. key_out, sizeof(key_out));
  194. }
  195. end = perftime();
  196. printf("Server-side: %f usec\n",
  197. NANOCOUNT(start, end, iters)/1e3);
  198. start = perftime();
  199. for (i = 0; i < iters; ++i) {
  200. uint8_t key_out[CPATH_KEY_MATERIAL_LEN];
  201. int s;
  202. s = onion_skin_ntor_client_handshake(state, or, key_out, sizeof(key_out),
  203. NULL);
  204. tor_assert(s == 0);
  205. }
  206. end = perftime();
  207. printf("Client-side, part 2: %f usec.\n",
  208. NANOCOUNT(start, end, iters)/1e3);
  209. ntor_handshake_state_free(state);
  210. dimap_free(keymap, NULL);
  211. }
  212. #endif
  213. static void
  214. bench_cell_aes(void)
  215. {
  216. uint64_t start, end;
  217. const int len = 509;
  218. const int iters = (1<<16);
  219. const int max_misalign = 15;
  220. char *b = tor_malloc(len+max_misalign);
  221. crypto_cipher_t *c;
  222. int i, misalign;
  223. c = crypto_cipher_new(NULL);
  224. reset_perftime();
  225. for (misalign = 0; misalign <= max_misalign; ++misalign) {
  226. start = perftime();
  227. for (i = 0; i < iters; ++i) {
  228. crypto_cipher_crypt_inplace(c, b+misalign, len);
  229. }
  230. end = perftime();
  231. printf("%d bytes, misaligned by %d: %.2f nsec per byte\n", len, misalign,
  232. NANOCOUNT(start, end, iters*len));
  233. }
  234. crypto_cipher_free(c);
  235. tor_free(b);
  236. }
  237. /** Run digestmap_t performance benchmarks. */
  238. static void
  239. bench_dmap(void)
  240. {
  241. smartlist_t *sl = smartlist_new();
  242. smartlist_t *sl2 = smartlist_new();
  243. uint64_t start, end, pt2, pt3, pt4;
  244. int iters = 8192;
  245. const int elts = 4000;
  246. const int fpostests = 100000;
  247. char d[20];
  248. int i,n=0, fp = 0;
  249. digestmap_t *dm = digestmap_new();
  250. digestset_t *ds = digestset_new(elts);
  251. for (i = 0; i < elts; ++i) {
  252. crypto_rand(d, 20);
  253. smartlist_add(sl, tor_memdup(d, 20));
  254. }
  255. for (i = 0; i < elts; ++i) {
  256. crypto_rand(d, 20);
  257. smartlist_add(sl2, tor_memdup(d, 20));
  258. }
  259. printf("nbits=%d\n", ds->mask+1);
  260. reset_perftime();
  261. start = perftime();
  262. for (i = 0; i < iters; ++i) {
  263. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_set(dm, cp, (void*)1));
  264. }
  265. pt2 = perftime();
  266. printf("digestmap_set: %.2f ns per element\n",
  267. NANOCOUNT(start, pt2, iters*elts));
  268. for (i = 0; i < iters; ++i) {
  269. SMARTLIST_FOREACH(sl, const char *, cp, digestmap_get(dm, cp));
  270. SMARTLIST_FOREACH(sl2, const char *, cp, digestmap_get(dm, cp));
  271. }
  272. pt3 = perftime();
  273. printf("digestmap_get: %.2f ns per element\n",
  274. NANOCOUNT(pt2, pt3, iters*elts*2));
  275. for (i = 0; i < iters; ++i) {
  276. SMARTLIST_FOREACH(sl, const char *, cp, digestset_add(ds, cp));
  277. }
  278. pt4 = perftime();
  279. printf("digestset_add: %.2f ns per element\n",
  280. NANOCOUNT(pt3, pt4, iters*elts));
  281. for (i = 0; i < iters; ++i) {
  282. SMARTLIST_FOREACH(sl, const char *, cp, n += digestset_contains(ds, cp));
  283. SMARTLIST_FOREACH(sl2, const char *, cp, n += digestset_contains(ds, cp));
  284. }
  285. end = perftime();
  286. printf("digestset_contains: %.2f ns per element.\n",
  287. NANOCOUNT(pt4, end, iters*elts*2));
  288. /* We need to use this, or else the whole loop gets optimized out. */
  289. printf("Hits == %d\n", n);
  290. for (i = 0; i < fpostests; ++i) {
  291. crypto_rand(d, 20);
  292. if (digestset_contains(ds, d)) ++fp;
  293. }
  294. printf("False positive rate on digestset: %.2f%%\n",
  295. (fp/(double)fpostests)*100);
  296. digestmap_free(dm, NULL);
  297. digestset_free(ds);
  298. SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
  299. SMARTLIST_FOREACH(sl2, char *, cp, tor_free(cp));
  300. smartlist_free(sl);
  301. smartlist_free(sl2);
  302. }
  303. static void
  304. bench_siphash(void)
  305. {
  306. char buf[128];
  307. int lens[] = { 7, 8, 15, 16, 20, 32, 111, 128, -1 };
  308. int i, j;
  309. uint64_t start, end;
  310. const int N = 300000;
  311. crypto_rand(buf, sizeof(buf));
  312. for (i = 0; lens[i] > 0; ++i) {
  313. reset_perftime();
  314. start = perftime();
  315. for (j = 0; j < N; ++j) {
  316. siphash24g(buf, lens[i]);
  317. }
  318. end = perftime();
  319. printf("siphash24g(%d): %.2f ns per call\n",
  320. lens[i], NANOCOUNT(start,end,N));
  321. }
  322. }
  323. static void
  324. bench_cell_ops(void)
  325. {
  326. const int iters = 1<<16;
  327. int i;
  328. /* benchmarks for cell ops at relay. */
  329. or_circuit_t *or_circ = tor_malloc_zero(sizeof(or_circuit_t));
  330. cell_t *cell = tor_malloc(sizeof(cell_t));
  331. int outbound;
  332. uint64_t start, end;
  333. crypto_rand((char*)cell->payload, sizeof(cell->payload));
  334. /* Mock-up or_circuit_t */
  335. or_circ->base_.magic = OR_CIRCUIT_MAGIC;
  336. or_circ->base_.purpose = CIRCUIT_PURPOSE_OR;
  337. /* Initialize crypto */
  338. or_circ->p_crypto = crypto_cipher_new(NULL);
  339. or_circ->n_crypto = crypto_cipher_new(NULL);
  340. or_circ->p_digest = crypto_digest_new();
  341. or_circ->n_digest = crypto_digest_new();
  342. reset_perftime();
  343. for (outbound = 0; outbound <= 1; ++outbound) {
  344. cell_direction_t d = outbound ? CELL_DIRECTION_OUT : CELL_DIRECTION_IN;
  345. start = perftime();
  346. for (i = 0; i < iters; ++i) {
  347. char recognized = 0;
  348. crypt_path_t *layer_hint = NULL;
  349. relay_crypt(TO_CIRCUIT(or_circ), cell, d, &layer_hint, &recognized);
  350. }
  351. end = perftime();
  352. printf("%sbound cells: %.2f ns per cell. (%.2f ns per byte of payload)\n",
  353. outbound?"Out":" In",
  354. NANOCOUNT(start,end,iters),
  355. NANOCOUNT(start,end,iters*CELL_PAYLOAD_SIZE));
  356. }
  357. crypto_digest_free(or_circ->p_digest);
  358. crypto_digest_free(or_circ->n_digest);
  359. crypto_cipher_free(or_circ->p_crypto);
  360. crypto_cipher_free(or_circ->n_crypto);
  361. tor_free(or_circ);
  362. tor_free(cell);
  363. }
  364. static void
  365. bench_dh(void)
  366. {
  367. const int iters = 1<<10;
  368. int i;
  369. uint64_t start, end;
  370. reset_perftime();
  371. start = perftime();
  372. for (i = 0; i < iters; ++i) {
  373. char dh_pubkey_a[DH_BYTES], dh_pubkey_b[DH_BYTES];
  374. char secret_a[DH_BYTES], secret_b[DH_BYTES];
  375. ssize_t slen_a, slen_b;
  376. crypto_dh_t *dh_a = crypto_dh_new(DH_TYPE_TLS);
  377. crypto_dh_t *dh_b = crypto_dh_new(DH_TYPE_TLS);
  378. crypto_dh_generate_public(dh_a);
  379. crypto_dh_generate_public(dh_b);
  380. crypto_dh_get_public(dh_a, dh_pubkey_a, sizeof(dh_pubkey_a));
  381. crypto_dh_get_public(dh_b, dh_pubkey_b, sizeof(dh_pubkey_b));
  382. slen_a = crypto_dh_compute_secret(LOG_NOTICE,
  383. dh_a, dh_pubkey_b, sizeof(dh_pubkey_b),
  384. secret_a, sizeof(secret_a));
  385. slen_b = crypto_dh_compute_secret(LOG_NOTICE,
  386. dh_b, dh_pubkey_a, sizeof(dh_pubkey_a),
  387. secret_b, sizeof(secret_b));
  388. tor_assert(slen_a == slen_b);
  389. tor_assert(!memcmp(secret_a, secret_b, slen_a));
  390. crypto_dh_free(dh_a);
  391. crypto_dh_free(dh_b);
  392. }
  393. end = perftime();
  394. printf("Complete DH handshakes (1024 bit, public and private ops):\n"
  395. " %f millisec each.\n", NANOCOUNT(start, end, iters)/1e6);
  396. }
  397. #if (!defined(OPENSSL_NO_EC) \
  398. && OPENSSL_VERSION_NUMBER >= OPENSSL_V_SERIES(1,0,0))
  399. #define HAVE_EC_BENCHMARKS
  400. static void
  401. bench_ecdh_impl(int nid, const char *name)
  402. {
  403. const int iters = 1<<10;
  404. int i;
  405. uint64_t start, end;
  406. reset_perftime();
  407. start = perftime();
  408. for (i = 0; i < iters; ++i) {
  409. char secret_a[DH_BYTES], secret_b[DH_BYTES];
  410. ssize_t slen_a, slen_b;
  411. EC_KEY *dh_a = EC_KEY_new_by_curve_name(nid);
  412. EC_KEY *dh_b = EC_KEY_new_by_curve_name(nid);
  413. if (!dh_a || !dh_b) {
  414. puts("Skipping. (No implementation?)");
  415. return;
  416. }
  417. EC_KEY_generate_key(dh_a);
  418. EC_KEY_generate_key(dh_b);
  419. slen_a = ECDH_compute_key(secret_a, DH_BYTES,
  420. EC_KEY_get0_public_key(dh_b), dh_a,
  421. NULL);
  422. slen_b = ECDH_compute_key(secret_b, DH_BYTES,
  423. EC_KEY_get0_public_key(dh_a), dh_b,
  424. NULL);
  425. tor_assert(slen_a == slen_b);
  426. tor_assert(!memcmp(secret_a, secret_b, slen_a));
  427. EC_KEY_free(dh_a);
  428. EC_KEY_free(dh_b);
  429. }
  430. end = perftime();
  431. printf("Complete ECDH %s handshakes (2 public and 2 private ops):\n"
  432. " %f millisec each.\n", name, NANOCOUNT(start, end, iters)/1e6);
  433. }
  434. static void
  435. bench_ecdh_p256(void)
  436. {
  437. bench_ecdh_impl(NID_X9_62_prime256v1, "P-256");
  438. }
  439. static void
  440. bench_ecdh_p224(void)
  441. {
  442. bench_ecdh_impl(NID_secp224r1, "P-224");
  443. }
  444. #endif
  445. typedef void (*bench_fn)(void);
  446. typedef struct benchmark_t {
  447. const char *name;
  448. bench_fn fn;
  449. int enabled;
  450. } benchmark_t;
  451. #define ENT(s) { #s , bench_##s, 0 }
  452. static struct benchmark_t benchmarks[] = {
  453. ENT(dmap),
  454. ENT(siphash),
  455. ENT(aes),
  456. ENT(onion_TAP),
  457. #ifdef CURVE25519_ENABLED
  458. ENT(onion_ntor),
  459. #endif
  460. ENT(cell_aes),
  461. ENT(cell_ops),
  462. ENT(dh),
  463. #ifdef HAVE_EC_BENCHMARKS
  464. ENT(ecdh_p256),
  465. ENT(ecdh_p224),
  466. #endif
  467. {NULL,NULL,0}
  468. };
  469. static benchmark_t *
  470. find_benchmark(const char *name)
  471. {
  472. benchmark_t *b;
  473. for (b = benchmarks; b->name; ++b) {
  474. if (!strcmp(name, b->name)) {
  475. return b;
  476. }
  477. }
  478. return NULL;
  479. }
  480. /** Main entry point for benchmark code: parse the command line, and run
  481. * some benchmarks. */
  482. int
  483. main(int argc, const char **argv)
  484. {
  485. int i;
  486. int list=0, n_enabled=0;
  487. benchmark_t *b;
  488. char *errmsg;
  489. or_options_t *options;
  490. tor_threads_init();
  491. for (i = 1; i < argc; ++i) {
  492. if (!strcmp(argv[i], "--list")) {
  493. list = 1;
  494. } else {
  495. benchmark_t *b = find_benchmark(argv[i]);
  496. ++n_enabled;
  497. if (b) {
  498. b->enabled = 1;
  499. } else {
  500. printf("No such benchmark as %s\n", argv[i]);
  501. }
  502. }
  503. }
  504. reset_perftime();
  505. crypto_seed_rng(1);
  506. crypto_init_siphash_key();
  507. options = options_new();
  508. init_logging();
  509. options->command = CMD_RUN_UNITTESTS;
  510. options->DataDirectory = tor_strdup("");
  511. options_init(options);
  512. if (set_options(options, &errmsg) < 0) {
  513. printf("Failed to set initial options: %s\n", errmsg);
  514. tor_free(errmsg);
  515. return 1;
  516. }
  517. for (b = benchmarks; b->name; ++b) {
  518. if (b->enabled || n_enabled == 0) {
  519. printf("===== %s =====\n", b->name);
  520. if (!list)
  521. b->fn();
  522. }
  523. }
  524. return 0;
  525. }