onion_ntor.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* Copyright (c) 2012-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file onion_ntor.c
  5. *
  6. * \brief Implementation for the ntor handshake.
  7. *
  8. * The ntor circuit-extension handshake was developed as a replacement
  9. * for the old TAP handshake. It uses Elliptic-curve Diffie-Hellman and
  10. * a hash function in order to perform a one-way authenticated key
  11. * exchange. The ntor handshake is meant to replace the old "TAP"
  12. * handshake.
  13. *
  14. * We instantiate ntor with curve25519, HMAC-SHA256, and HKDF.
  15. *
  16. * This handshake, like the other circuit-extension handshakes, is
  17. * invoked from onion.c.
  18. */
  19. #include "orconfig.h"
  20. #define ONION_NTOR_PRIVATE
  21. #include "lib/crypt_ops/crypto.h"
  22. #include "lib/crypt_ops/crypto_hkdf.h"
  23. #include "lib/crypt_ops/crypto_digest.h"
  24. #include "lib/crypt_ops/crypto_util.h"
  25. #include "or/onion_ntor.h"
  26. #include "lib/log/torlog.h"
  27. #include "lib/ctime/di_ops.h"
  28. #include "common/util.h"
  29. /** Free storage held in an ntor handshake state. */
  30. void
  31. ntor_handshake_state_free_(ntor_handshake_state_t *state)
  32. {
  33. if (!state)
  34. return;
  35. memwipe(state, 0, sizeof(*state));
  36. tor_free(state);
  37. }
  38. /** Convenience function to represent HMAC_SHA256 as our instantiation of
  39. * ntor's "tweaked hash'. Hash the <b>inp_len</b> bytes at <b>inp</b> into
  40. * a DIGEST256_LEN-byte digest at <b>out</b>, with the hash changing
  41. * depending on the value of <b>tweak</b>. */
  42. static void
  43. h_tweak(uint8_t *out,
  44. const uint8_t *inp, size_t inp_len,
  45. const char *tweak)
  46. {
  47. size_t tweak_len = strlen(tweak);
  48. crypto_hmac_sha256((char*)out, tweak, tweak_len, (const char*)inp, inp_len);
  49. }
  50. /** Wrapper around a set of tweak-values for use with the ntor handshake. */
  51. typedef struct tweakset_t {
  52. const char *t_mac;
  53. const char *t_key;
  54. const char *t_verify;
  55. const char *m_expand;
  56. } tweakset_t;
  57. /** The tweaks to be used with our handshake. */
  58. static const tweakset_t proto1_tweaks = {
  59. #define PROTOID "ntor-curve25519-sha256-1"
  60. #define PROTOID_LEN 24
  61. PROTOID ":mac",
  62. PROTOID ":key_extract",
  63. PROTOID ":verify",
  64. PROTOID ":key_expand"
  65. };
  66. /** Convenience macro: copy <b>len</b> bytes from <b>inp</b> to <b>ptr</b>,
  67. * and advance <b>ptr</b> by the number of bytes copied. */
  68. #define APPEND(ptr, inp, len) \
  69. STMT_BEGIN { \
  70. memcpy(ptr, (inp), (len)); \
  71. ptr += len; \
  72. } STMT_END
  73. /**
  74. * Compute the first client-side step of the ntor handshake for communicating
  75. * with a server whose DIGEST_LEN-byte server identity is <b>router_id</b>,
  76. * and whose onion key is <b>router_key</b>. Store the NTOR_ONIONSKIN_LEN-byte
  77. * message in <b>onion_skin_out</b>, and store the handshake state in
  78. * *<b>handshake_state_out</b>. Return 0 on success, -1 on failure.
  79. */
  80. int
  81. onion_skin_ntor_create(const uint8_t *router_id,
  82. const curve25519_public_key_t *router_key,
  83. ntor_handshake_state_t **handshake_state_out,
  84. uint8_t *onion_skin_out)
  85. {
  86. ntor_handshake_state_t *state;
  87. uint8_t *op;
  88. state = tor_malloc_zero(sizeof(ntor_handshake_state_t));
  89. memcpy(state->router_id, router_id, DIGEST_LEN);
  90. memcpy(&state->pubkey_B, router_key, sizeof(curve25519_public_key_t));
  91. if (curve25519_secret_key_generate(&state->seckey_x, 0) < 0) {
  92. /* LCOV_EXCL_START
  93. * Secret key generation should be unable to fail when the key isn't
  94. * marked as "extra-strong" */
  95. tor_assert_nonfatal_unreached();
  96. tor_free(state);
  97. return -1;
  98. /* LCOV_EXCL_STOP */
  99. }
  100. curve25519_public_key_generate(&state->pubkey_X, &state->seckey_x);
  101. op = onion_skin_out;
  102. APPEND(op, router_id, DIGEST_LEN);
  103. APPEND(op, router_key->public_key, CURVE25519_PUBKEY_LEN);
  104. APPEND(op, state->pubkey_X.public_key, CURVE25519_PUBKEY_LEN);
  105. tor_assert(op == onion_skin_out + NTOR_ONIONSKIN_LEN);
  106. *handshake_state_out = state;
  107. return 0;
  108. }
  109. #define SERVER_STR "Server"
  110. #define SERVER_STR_LEN 6
  111. #define SECRET_INPUT_LEN (CURVE25519_PUBKEY_LEN * 3 + \
  112. CURVE25519_OUTPUT_LEN * 2 + \
  113. DIGEST_LEN + PROTOID_LEN)
  114. #define AUTH_INPUT_LEN (DIGEST256_LEN + DIGEST_LEN + \
  115. CURVE25519_PUBKEY_LEN*3 + \
  116. PROTOID_LEN + SERVER_STR_LEN)
  117. /**
  118. * Perform the server side of an ntor handshake. Given an
  119. * NTOR_ONIONSKIN_LEN-byte message in <b>onion_skin</b>, our own identity
  120. * fingerprint as <b>my_node_id</b>, and an associative array mapping public
  121. * onion keys to curve25519_keypair_t in <b>private_keys</b>, attempt to
  122. * perform the handshake. Use <b>junk_keys</b> if present if the handshake
  123. * indicates an unrecognized public key. Write an NTOR_REPLY_LEN-byte
  124. * message to send back to the client into <b>handshake_reply_out</b>, and
  125. * generate <b>key_out_len</b> bytes of key material in <b>key_out</b>. Return
  126. * 0 on success, -1 on failure.
  127. */
  128. int
  129. onion_skin_ntor_server_handshake(const uint8_t *onion_skin,
  130. const di_digest256_map_t *private_keys,
  131. const curve25519_keypair_t *junk_keys,
  132. const uint8_t *my_node_id,
  133. uint8_t *handshake_reply_out,
  134. uint8_t *key_out,
  135. size_t key_out_len)
  136. {
  137. const tweakset_t *T = &proto1_tweaks;
  138. /* Sensitive stack-allocated material. Kept in an anonymous struct to make
  139. * it easy to wipe. */
  140. struct {
  141. uint8_t secret_input[SECRET_INPUT_LEN];
  142. uint8_t auth_input[AUTH_INPUT_LEN];
  143. curve25519_public_key_t pubkey_X;
  144. curve25519_secret_key_t seckey_y;
  145. curve25519_public_key_t pubkey_Y;
  146. uint8_t verify[DIGEST256_LEN];
  147. } s;
  148. uint8_t *si = s.secret_input, *ai = s.auth_input;
  149. const curve25519_keypair_t *keypair_bB;
  150. int bad;
  151. /* Decode the onion skin */
  152. /* XXXX Does this possible early-return business threaten our security? */
  153. if (tor_memneq(onion_skin, my_node_id, DIGEST_LEN))
  154. return -1;
  155. /* Note that on key-not-found, we go through with this operation anyway,
  156. * using "junk_keys". This will result in failed authentication, but won't
  157. * leak whether we recognized the key. */
  158. keypair_bB = dimap_search(private_keys, onion_skin + DIGEST_LEN,
  159. (void*)junk_keys);
  160. if (!keypair_bB)
  161. return -1;
  162. memcpy(s.pubkey_X.public_key, onion_skin+DIGEST_LEN+DIGEST256_LEN,
  163. CURVE25519_PUBKEY_LEN);
  164. /* Make y, Y */
  165. curve25519_secret_key_generate(&s.seckey_y, 0);
  166. curve25519_public_key_generate(&s.pubkey_Y, &s.seckey_y);
  167. /* NOTE: If we ever use a group other than curve25519, or a different
  168. * representation for its points, we may need to perform different or
  169. * additional checks on X here and on Y in the client handshake, or lose our
  170. * security properties. What checks we need would depend on the properties
  171. * of the group and its representation.
  172. *
  173. * In short: if you use anything other than curve25519, this aspect of the
  174. * code will need to be reconsidered carefully. */
  175. /* build secret_input */
  176. curve25519_handshake(si, &s.seckey_y, &s.pubkey_X);
  177. bad = safe_mem_is_zero(si, CURVE25519_OUTPUT_LEN);
  178. si += CURVE25519_OUTPUT_LEN;
  179. curve25519_handshake(si, &keypair_bB->seckey, &s.pubkey_X);
  180. bad |= safe_mem_is_zero(si, CURVE25519_OUTPUT_LEN);
  181. si += CURVE25519_OUTPUT_LEN;
  182. APPEND(si, my_node_id, DIGEST_LEN);
  183. APPEND(si, keypair_bB->pubkey.public_key, CURVE25519_PUBKEY_LEN);
  184. APPEND(si, s.pubkey_X.public_key, CURVE25519_PUBKEY_LEN);
  185. APPEND(si, s.pubkey_Y.public_key, CURVE25519_PUBKEY_LEN);
  186. APPEND(si, PROTOID, PROTOID_LEN);
  187. tor_assert(si == s.secret_input + sizeof(s.secret_input));
  188. /* Compute hashes of secret_input */
  189. h_tweak(s.verify, s.secret_input, sizeof(s.secret_input), T->t_verify);
  190. /* Compute auth_input */
  191. APPEND(ai, s.verify, DIGEST256_LEN);
  192. APPEND(ai, my_node_id, DIGEST_LEN);
  193. APPEND(ai, keypair_bB->pubkey.public_key, CURVE25519_PUBKEY_LEN);
  194. APPEND(ai, s.pubkey_Y.public_key, CURVE25519_PUBKEY_LEN);
  195. APPEND(ai, s.pubkey_X.public_key, CURVE25519_PUBKEY_LEN);
  196. APPEND(ai, PROTOID, PROTOID_LEN);
  197. APPEND(ai, SERVER_STR, SERVER_STR_LEN);
  198. tor_assert(ai == s.auth_input + sizeof(s.auth_input));
  199. /* Build the reply */
  200. memcpy(handshake_reply_out, s.pubkey_Y.public_key, CURVE25519_PUBKEY_LEN);
  201. h_tweak(handshake_reply_out+CURVE25519_PUBKEY_LEN,
  202. s.auth_input, sizeof(s.auth_input),
  203. T->t_mac);
  204. /* Generate the key material */
  205. crypto_expand_key_material_rfc5869_sha256(
  206. s.secret_input, sizeof(s.secret_input),
  207. (const uint8_t*)T->t_key, strlen(T->t_key),
  208. (const uint8_t*)T->m_expand, strlen(T->m_expand),
  209. key_out, key_out_len);
  210. /* Wipe all of our local state */
  211. memwipe(&s, 0, sizeof(s));
  212. return bad ? -1 : 0;
  213. }
  214. /**
  215. * Perform the final client side of the ntor handshake, using the state in
  216. * <b>handshake_state</b> and the server's NTOR_REPLY_LEN-byte reply in
  217. * <b>handshake_reply</b>. Generate <b>key_out_len</b> bytes of key material
  218. * in <b>key_out</b>. Return 0 on success, -1 on failure.
  219. */
  220. int
  221. onion_skin_ntor_client_handshake(
  222. const ntor_handshake_state_t *handshake_state,
  223. const uint8_t *handshake_reply,
  224. uint8_t *key_out,
  225. size_t key_out_len,
  226. const char **msg_out)
  227. {
  228. const tweakset_t *T = &proto1_tweaks;
  229. /* Sensitive stack-allocated material. Kept in an anonymous struct to make
  230. * it easy to wipe. */
  231. struct {
  232. curve25519_public_key_t pubkey_Y;
  233. uint8_t secret_input[SECRET_INPUT_LEN];
  234. uint8_t verify[DIGEST256_LEN];
  235. uint8_t auth_input[AUTH_INPUT_LEN];
  236. uint8_t auth[DIGEST256_LEN];
  237. } s;
  238. uint8_t *ai = s.auth_input, *si = s.secret_input;
  239. const uint8_t *auth_candidate;
  240. int bad;
  241. /* Decode input */
  242. memcpy(s.pubkey_Y.public_key, handshake_reply, CURVE25519_PUBKEY_LEN);
  243. auth_candidate = handshake_reply + CURVE25519_PUBKEY_LEN;
  244. /* See note in server_handshake above about checking points. The
  245. * circumstances under which we'd need to check Y for membership are
  246. * different than those under which we'd be checking X. */
  247. /* Compute secret_input */
  248. curve25519_handshake(si, &handshake_state->seckey_x, &s.pubkey_Y);
  249. bad = safe_mem_is_zero(si, CURVE25519_OUTPUT_LEN);
  250. si += CURVE25519_OUTPUT_LEN;
  251. curve25519_handshake(si, &handshake_state->seckey_x,
  252. &handshake_state->pubkey_B);
  253. bad |= (safe_mem_is_zero(si, CURVE25519_OUTPUT_LEN) << 1);
  254. si += CURVE25519_OUTPUT_LEN;
  255. APPEND(si, handshake_state->router_id, DIGEST_LEN);
  256. APPEND(si, handshake_state->pubkey_B.public_key, CURVE25519_PUBKEY_LEN);
  257. APPEND(si, handshake_state->pubkey_X.public_key, CURVE25519_PUBKEY_LEN);
  258. APPEND(si, s.pubkey_Y.public_key, CURVE25519_PUBKEY_LEN);
  259. APPEND(si, PROTOID, PROTOID_LEN);
  260. tor_assert(si == s.secret_input + sizeof(s.secret_input));
  261. /* Compute verify from secret_input */
  262. h_tweak(s.verify, s.secret_input, sizeof(s.secret_input), T->t_verify);
  263. /* Compute auth_input */
  264. APPEND(ai, s.verify, DIGEST256_LEN);
  265. APPEND(ai, handshake_state->router_id, DIGEST_LEN);
  266. APPEND(ai, handshake_state->pubkey_B.public_key, CURVE25519_PUBKEY_LEN);
  267. APPEND(ai, s.pubkey_Y.public_key, CURVE25519_PUBKEY_LEN);
  268. APPEND(ai, handshake_state->pubkey_X.public_key, CURVE25519_PUBKEY_LEN);
  269. APPEND(ai, PROTOID, PROTOID_LEN);
  270. APPEND(ai, SERVER_STR, SERVER_STR_LEN);
  271. tor_assert(ai == s.auth_input + sizeof(s.auth_input));
  272. /* Compute auth */
  273. h_tweak(s.auth, s.auth_input, sizeof(s.auth_input), T->t_mac);
  274. bad |= (tor_memneq(s.auth, auth_candidate, DIGEST256_LEN) << 2);
  275. crypto_expand_key_material_rfc5869_sha256(
  276. s.secret_input, sizeof(s.secret_input),
  277. (const uint8_t*)T->t_key, strlen(T->t_key),
  278. (const uint8_t*)T->m_expand, strlen(T->m_expand),
  279. key_out, key_out_len);
  280. memwipe(&s, 0, sizeof(s));
  281. if (bad) {
  282. if (bad & 4) {
  283. if (msg_out)
  284. *msg_out = NULL; /* Don't report this one; we probably just had the
  285. * wrong onion key.*/
  286. log_fn(LOG_INFO, LD_PROTOCOL,
  287. "Invalid result from curve25519 handshake: %d", bad);
  288. }
  289. if (bad & 3) {
  290. if (msg_out)
  291. *msg_out = "Zero output from curve25519 handshake";
  292. log_fn(LOG_WARN, LD_PROTOCOL,
  293. "Invalid result from curve25519 handshake: %d", bad);
  294. }
  295. }
  296. return bad ? -1 : 0;
  297. }