bench.c 14 KB

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