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. #include "or.h"
  15. #include "onion_tap.h"
  16. #include "relay.h"
  17. #include <openssl/opensslv.h>
  18. #include <openssl/evp.h>
  19. #ifndef OPENSSL_NO_EC
  20. #include <openssl/ec.h>
  21. #include <openssl/ecdh.h>
  22. #include <openssl/obj_mac.h>
  23. #endif
  24. #include "config.h"
  25. #ifdef CURVE25519_ENABLED
  26. #include "crypto_curve25519.h"
  27. #include "onion_ntor.h"
  28. #endif
  29. #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
  30. static uint64_t nanostart;
  31. static inline uint64_t
  32. timespec_to_nsec(const struct timespec *ts)
  33. {
  34. return ((uint64_t)ts->tv_sec)*1000000000 + ts->tv_nsec;
  35. }
  36. static void
  37. reset_perftime(void)
  38. {
  39. struct timespec ts;
  40. int r;
  41. r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  42. tor_assert(r == 0);
  43. nanostart = timespec_to_nsec(&ts);
  44. }
  45. static uint64_t
  46. perftime(void)
  47. {
  48. struct timespec ts;
  49. int r;
  50. r = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  51. tor_assert(r == 0);
  52. return timespec_to_nsec(&ts) - nanostart;
  53. }
  54. #else
  55. static struct timeval tv_start = { 0, 0 };
  56. static void
  57. reset_perftime(void)
  58. {
  59. tor_gettimeofday(&tv_start);
  60. }
  61. static uint64_t
  62. perftime(void)
  63. {
  64. struct timeval now, out;
  65. tor_gettimeofday(&now);
  66. timersub(&now, &tv_start, &out);
  67. return ((uint64_t)out.tv_sec)*1000000000 + out.tv_usec*1000;
  68. }
  69. #endif
  70. #define NANOCOUNT(start,end,iters) \
  71. ( ((double)((end)-(start))) / (iters) )
  72. /** Run AES performance benchmarks. */
  73. static void
  74. bench_aes(void)
  75. {
  76. int len, i;
  77. char *b1, *b2;
  78. crypto_cipher_t *c;
  79. uint64_t start, end;
  80. const int bytes_per_iter = (1<<24);
  81. reset_perftime();
  82. c = crypto_cipher_new(NULL);
  83. for (len = 1; len <= 8192; len *= 2) {
  84. int iters = bytes_per_iter / len;
  85. b1 = tor_malloc_zero(len);
  86. b2 = tor_malloc_zero(len);
  87. start = perftime();
  88. for (i = 0; i < iters; ++i) {
  89. crypto_cipher_encrypt(c, b1, b2, len);
  90. }
  91. end = perftime();
  92. tor_free(b1);
  93. tor_free(b2);
  94. printf("%d bytes: %.2f nsec per byte\n", len,
  95. NANOCOUNT(start, end, iters*len));
  96. }
  97. crypto_cipher_free(c);
  98. }
  99. static void
  100. bench_onion_TAP(void)
  101. {
  102. const int iters = 1<<9;
  103. int i;
  104. crypto_pk_t *key, *key2;
  105. uint64_t start, end;
  106. char os[TAP_ONIONSKIN_CHALLENGE_LEN];
  107. char or[TAP_ONIONSKIN_REPLY_LEN];
  108. crypto_dh_t *dh_out;
  109. key = crypto_pk_new();
  110. key2 = crypto_pk_new();
  111. if (crypto_pk_generate_key_with_bits(key, 1024) < 0)
  112. goto done;
  113. if (crypto_pk_generate_key_with_bits(key2, 1024) < 0)
  114. goto done;
  115. reset_perftime();
  116. start = perftime();
  117. for (i = 0; i < iters; ++i) {
  118. onion_skin_TAP_create(key, &dh_out, os);
  119. crypto_dh_free(dh_out);
  120. }
  121. end = perftime();
  122. printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3);
  123. onion_skin_TAP_create(key, &dh_out, os);
  124. start = perftime();
  125. for (i = 0; i < iters; ++i) {
  126. char key_out[CPATH_KEY_MATERIAL_LEN];
  127. onion_skin_TAP_server_handshake(os, key, NULL, or,
  128. key_out, sizeof(key_out));
  129. }
  130. end = perftime();
  131. printf("Server-side, key guessed right: %f usec\n",
  132. NANOCOUNT(start, end, iters)/1e3);
  133. start = perftime();
  134. for (i = 0; i < iters; ++i) {
  135. char key_out[CPATH_KEY_MATERIAL_LEN];
  136. onion_skin_TAP_server_handshake(os, key2, key, or,
  137. key_out, sizeof(key_out));
  138. }
  139. end = perftime();
  140. printf("Server-side, key guessed wrong: %f usec.\n",
  141. NANOCOUNT(start, end, iters)/1e3);
  142. start = perftime();
  143. for (i = 0; i < iters; ++i) {
  144. crypto_dh_t *dh;
  145. char key_out[CPATH_KEY_MATERIAL_LEN];
  146. int s;
  147. dh = crypto_dh_dup(dh_out);
  148. s = onion_skin_TAP_client_handshake(dh, or, key_out, sizeof(key_out));
  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. 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. }