bench.c 14 KB

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