onion_ntor.c 12 KB

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