onion_ntor.c 12 KB

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