bench.c 14 KB

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