hs_ntor.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /* Copyright (c) 2017-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /** \file hs_ntor.c
  4. * \brief Implements the ntor variant used in Tor hidden services.
  5. *
  6. * \details
  7. * This module handles the variant of the ntor handshake that is documented in
  8. * section [NTOR-WITH-EXTRA-DATA] of rend-spec-ng.txt .
  9. *
  10. * The functions in this file provide an API that should be used when sending
  11. * or receiving INTRODUCE1/RENDEZVOUS1 cells to generate the various key
  12. * material required to create and handle those cells.
  13. *
  14. * In the case of INTRODUCE1 it provides encryption and MAC keys to
  15. * encode/decode the encrypted blob (see hs_ntor_intro_cell_keys_t). The
  16. * relevant pub functions are hs_ntor_{client,service}_get_introduce1_keys().
  17. *
  18. * In the case of RENDEZVOUS1 it calculates the MAC required to authenticate
  19. * the cell, and also provides the key seed that is used to derive the crypto
  20. * material for rendezvous encryption (see hs_ntor_rend_cell_keys_t). The
  21. * relevant pub functions are hs_ntor_{client,service}_get_rendezvous1_keys().
  22. * It also provides a function (hs_ntor_circuit_key_expansion()) that does the
  23. * rendezvous key expansion to setup end-to-end rend circuit keys.
  24. */
  25. #include "or/or.h"
  26. #include "lib/crypt_ops/crypto_util.h"
  27. #include "or/hs_ntor.h"
  28. /* String constants used by the ntor HS protocol */
  29. #define PROTOID "tor-hs-ntor-curve25519-sha3-256-1"
  30. #define PROTOID_LEN (sizeof(PROTOID) - 1)
  31. #define SERVER_STR "Server"
  32. #define SERVER_STR_LEN (sizeof(SERVER_STR) - 1)
  33. /* Protocol-specific tweaks to our crypto inputs */
  34. #define T_HSENC PROTOID ":hs_key_extract"
  35. #define T_HSENC_LEN (sizeof(T_HSENC) - 1)
  36. #define T_HSVERIFY PROTOID ":hs_verify"
  37. #define T_HSMAC PROTOID ":hs_mac"
  38. #define M_HSEXPAND PROTOID ":hs_key_expand"
  39. #define M_HSEXPAND_LEN (sizeof(M_HSEXPAND) - 1)
  40. /************************* Helper functions: *******************************/
  41. /** Helper macro: copy <b>len</b> bytes from <b>inp</b> to <b>ptr</b> and
  42. *advance <b>ptr</b> by the number of bytes copied. Stolen from onion_ntor.c */
  43. #define APPEND(ptr, inp, len) \
  44. STMT_BEGIN { \
  45. memcpy(ptr, (inp), (len)); \
  46. ptr += len; \
  47. } STMT_END
  48. /* Length of EXP(X,y) | EXP(X,b) | AUTH_KEY | B | X | Y | PROTOID */
  49. #define REND_SECRET_HS_INPUT_LEN (CURVE25519_OUTPUT_LEN * 2 + \
  50. ED25519_PUBKEY_LEN + CURVE25519_PUBKEY_LEN * 3 + PROTOID_LEN)
  51. /* Length of auth_input = verify | AUTH_KEY | B | Y | X | PROTOID | "Server" */
  52. #define REND_AUTH_INPUT_LEN (DIGEST256_LEN + ED25519_PUBKEY_LEN + \
  53. CURVE25519_PUBKEY_LEN * 3 + PROTOID_LEN + SERVER_STR_LEN)
  54. /** Helper function: Compute the last part of the HS ntor handshake which
  55. * derives key material necessary to create and handle RENDEZVOUS1
  56. * cells. Function used by both client and service. The actual calculations is
  57. * as follows:
  58. *
  59. * NTOR_KEY_SEED = MAC(rend_secret_hs_input, t_hsenc)
  60. * verify = MAC(rend_secret_hs_input, t_hsverify)
  61. * auth_input = verify | AUTH_KEY | B | Y | X | PROTOID | "Server"
  62. * auth_input_mac = MAC(auth_input, t_hsmac)
  63. *
  64. * where in the above, AUTH_KEY is <b>intro_auth_pubkey</b>, B is
  65. * <b>intro_enc_pubkey</b>, Y is <b>service_ephemeral_rend_pubkey</b>, and X
  66. * is <b>client_ephemeral_enc_pubkey</b>. The provided
  67. * <b>rend_secret_hs_input</b> is of size REND_SECRET_HS_INPUT_LEN.
  68. *
  69. * The final results of NTOR_KEY_SEED and auth_input_mac are placed in
  70. * <b>hs_ntor_rend_cell_keys_out</b>. Return 0 if everything went fine. */
  71. static int
  72. get_rendezvous1_key_material(const uint8_t *rend_secret_hs_input,
  73. const ed25519_public_key_t *intro_auth_pubkey,
  74. const curve25519_public_key_t *intro_enc_pubkey,
  75. const curve25519_public_key_t *service_ephemeral_rend_pubkey,
  76. const curve25519_public_key_t *client_ephemeral_enc_pubkey,
  77. hs_ntor_rend_cell_keys_t *hs_ntor_rend_cell_keys_out)
  78. {
  79. int bad = 0;
  80. uint8_t ntor_key_seed[DIGEST256_LEN];
  81. uint8_t ntor_verify[DIGEST256_LEN];
  82. uint8_t rend_auth_input[REND_AUTH_INPUT_LEN];
  83. uint8_t rend_cell_auth[DIGEST256_LEN];
  84. uint8_t *ptr;
  85. /* Let's build NTOR_KEY_SEED */
  86. crypto_mac_sha3_256(ntor_key_seed, sizeof(ntor_key_seed),
  87. rend_secret_hs_input, REND_SECRET_HS_INPUT_LEN,
  88. (const uint8_t *)T_HSENC, strlen(T_HSENC));
  89. bad |= safe_mem_is_zero(ntor_key_seed, DIGEST256_LEN);
  90. /* Let's build ntor_verify */
  91. crypto_mac_sha3_256(ntor_verify, sizeof(ntor_verify),
  92. rend_secret_hs_input, REND_SECRET_HS_INPUT_LEN,
  93. (const uint8_t *)T_HSVERIFY, strlen(T_HSVERIFY));
  94. bad |= safe_mem_is_zero(ntor_verify, DIGEST256_LEN);
  95. /* Let's build auth_input: */
  96. ptr = rend_auth_input;
  97. /* Append ntor_verify */
  98. APPEND(ptr, ntor_verify, sizeof(ntor_verify));
  99. /* Append AUTH_KEY */
  100. APPEND(ptr, intro_auth_pubkey->pubkey, ED25519_PUBKEY_LEN);
  101. /* Append B */
  102. APPEND(ptr, intro_enc_pubkey->public_key, CURVE25519_PUBKEY_LEN);
  103. /* Append Y */
  104. APPEND(ptr,
  105. service_ephemeral_rend_pubkey->public_key, CURVE25519_PUBKEY_LEN);
  106. /* Append X */
  107. APPEND(ptr,
  108. client_ephemeral_enc_pubkey->public_key, CURVE25519_PUBKEY_LEN);
  109. /* Append PROTOID */
  110. APPEND(ptr, PROTOID, strlen(PROTOID));
  111. /* Append "Server" */
  112. APPEND(ptr, SERVER_STR, strlen(SERVER_STR));
  113. tor_assert(ptr == rend_auth_input + sizeof(rend_auth_input));
  114. /* Let's build auth_input_mac that goes in RENDEZVOUS1 cell */
  115. crypto_mac_sha3_256(rend_cell_auth, sizeof(rend_cell_auth),
  116. rend_auth_input, sizeof(rend_auth_input),
  117. (const uint8_t *)T_HSMAC, strlen(T_HSMAC));
  118. bad |= safe_mem_is_zero(ntor_verify, DIGEST256_LEN);
  119. { /* Get the computed RENDEZVOUS1 material! */
  120. memcpy(&hs_ntor_rend_cell_keys_out->rend_cell_auth_mac,
  121. rend_cell_auth, DIGEST256_LEN);
  122. memcpy(&hs_ntor_rend_cell_keys_out->ntor_key_seed,
  123. ntor_key_seed, DIGEST256_LEN);
  124. }
  125. memwipe(rend_cell_auth, 0, sizeof(rend_cell_auth));
  126. memwipe(rend_auth_input, 0, sizeof(rend_auth_input));
  127. memwipe(ntor_key_seed, 0, sizeof(ntor_key_seed));
  128. return bad;
  129. }
  130. /** Length of secret_input = EXP(B,x) | AUTH_KEY | X | B | PROTOID */
  131. #define INTRO_SECRET_HS_INPUT_LEN (CURVE25519_OUTPUT_LEN +ED25519_PUBKEY_LEN +\
  132. CURVE25519_PUBKEY_LEN + CURVE25519_PUBKEY_LEN + PROTOID_LEN)
  133. /* Length of info = m_hsexpand | subcredential */
  134. #define INFO_BLOB_LEN (M_HSEXPAND_LEN + DIGEST256_LEN)
  135. /* Length of KDF input = intro_secret_hs_input | t_hsenc | info */
  136. #define KDF_INPUT_LEN (INTRO_SECRET_HS_INPUT_LEN + T_HSENC_LEN + INFO_BLOB_LEN)
  137. /** Helper function: Compute the part of the HS ntor handshake that generates
  138. * key material for creating and handling INTRODUCE1 cells. Function used
  139. * by both client and service. Specifically, calculate the following:
  140. *
  141. * info = m_hsexpand | subcredential
  142. * hs_keys = KDF(intro_secret_hs_input | t_hsenc | info, S_KEY_LEN+MAC_LEN)
  143. * ENC_KEY = hs_keys[0:S_KEY_LEN]
  144. * MAC_KEY = hs_keys[S_KEY_LEN:S_KEY_LEN+MAC_KEY_LEN]
  145. *
  146. * where intro_secret_hs_input is <b>secret_input</b> (of size
  147. * INTRO_SECRET_HS_INPUT_LEN), and <b>subcredential</b> is of size
  148. * DIGEST256_LEN.
  149. *
  150. * If everything went well, fill <b>hs_ntor_intro_cell_keys_out</b> with the
  151. * necessary key material, and return 0. */
  152. static void
  153. get_introduce1_key_material(const uint8_t *secret_input,
  154. const uint8_t *subcredential,
  155. hs_ntor_intro_cell_keys_t *hs_ntor_intro_cell_keys_out)
  156. {
  157. uint8_t keystream[CIPHER256_KEY_LEN + DIGEST256_LEN];
  158. uint8_t info_blob[INFO_BLOB_LEN];
  159. uint8_t kdf_input[KDF_INPUT_LEN];
  160. crypto_xof_t *xof;
  161. uint8_t *ptr;
  162. /* Let's build info */
  163. ptr = info_blob;
  164. APPEND(ptr, M_HSEXPAND, strlen(M_HSEXPAND));
  165. APPEND(ptr, subcredential, DIGEST256_LEN);
  166. tor_assert(ptr == info_blob + sizeof(info_blob));
  167. /* Let's build the input to the KDF */
  168. ptr = kdf_input;
  169. APPEND(ptr, secret_input, INTRO_SECRET_HS_INPUT_LEN);
  170. APPEND(ptr, T_HSENC, strlen(T_HSENC));
  171. APPEND(ptr, info_blob, sizeof(info_blob));
  172. tor_assert(ptr == kdf_input + sizeof(kdf_input));
  173. /* Now we need to run kdf_input over SHAKE-256 */
  174. xof = crypto_xof_new();
  175. crypto_xof_add_bytes(xof, kdf_input, sizeof(kdf_input));
  176. crypto_xof_squeeze_bytes(xof, keystream, sizeof(keystream)) ;
  177. crypto_xof_free(xof);
  178. { /* Get the keys */
  179. memcpy(&hs_ntor_intro_cell_keys_out->enc_key, keystream,CIPHER256_KEY_LEN);
  180. memcpy(&hs_ntor_intro_cell_keys_out->mac_key,
  181. keystream+CIPHER256_KEY_LEN, DIGEST256_LEN);
  182. }
  183. memwipe(keystream, 0, sizeof(keystream));
  184. memwipe(kdf_input, 0, sizeof(kdf_input));
  185. }
  186. /** Helper function: Calculate the 'intro_secret_hs_input' element used by the
  187. * HS ntor handshake and place it in <b>secret_input_out</b>. This function is
  188. * used by both client and service code.
  189. *
  190. * For the client-side it looks like this:
  191. *
  192. * intro_secret_hs_input = EXP(B,x) | AUTH_KEY | X | B | PROTOID
  193. *
  194. * whereas for the service-side it looks like this:
  195. *
  196. * intro_secret_hs_input = EXP(X,b) | AUTH_KEY | X | B | PROTOID
  197. *
  198. * In this function, <b>dh_result</b> carries the EXP() result (and has size
  199. * CURVE25519_OUTPUT_LEN) <b>intro_auth_pubkey</b> is AUTH_KEY,
  200. * <b>client_ephemeral_enc_pubkey</b> is X, and <b>intro_enc_pubkey</b> is B.
  201. */
  202. static void
  203. get_intro_secret_hs_input(const uint8_t *dh_result,
  204. const ed25519_public_key_t *intro_auth_pubkey,
  205. const curve25519_public_key_t *client_ephemeral_enc_pubkey,
  206. const curve25519_public_key_t *intro_enc_pubkey,
  207. uint8_t *secret_input_out)
  208. {
  209. uint8_t *ptr;
  210. /* Append EXP() */
  211. ptr = secret_input_out;
  212. APPEND(ptr, dh_result, CURVE25519_OUTPUT_LEN);
  213. /* Append AUTH_KEY */
  214. APPEND(ptr, intro_auth_pubkey->pubkey, ED25519_PUBKEY_LEN);
  215. /* Append X */
  216. APPEND(ptr, client_ephemeral_enc_pubkey->public_key, CURVE25519_PUBKEY_LEN);
  217. /* Append B */
  218. APPEND(ptr, intro_enc_pubkey->public_key, CURVE25519_PUBKEY_LEN);
  219. /* Append PROTOID */
  220. APPEND(ptr, PROTOID, strlen(PROTOID));
  221. tor_assert(ptr == secret_input_out + INTRO_SECRET_HS_INPUT_LEN);
  222. }
  223. /** Calculate the 'rend_secret_hs_input' element used by the HS ntor handshake
  224. * and place it in <b>rend_secret_hs_input_out</b>. This function is used by
  225. * both client and service code.
  226. *
  227. * The computation on the client side is:
  228. * rend_secret_hs_input = EXP(X,y) | EXP(X,b) | AUTH_KEY | B | X | Y | PROTOID
  229. * whereas on the service side it is:
  230. * rend_secret_hs_input = EXP(Y,x) | EXP(B,x) | AUTH_KEY | B | X | Y | PROTOID
  231. *
  232. * where:
  233. * <b>dh_result1</b> and <b>dh_result2</b> carry the two EXP() results (of size
  234. * CURVE25519_OUTPUT_LEN)
  235. * <b>intro_auth_pubkey</b> is AUTH_KEY,
  236. * <b>intro_enc_pubkey</b> is B,
  237. * <b>client_ephemeral_enc_pubkey</b> is X, and
  238. * <b>service_ephemeral_rend_pubkey</b> is Y.
  239. */
  240. static void
  241. get_rend_secret_hs_input(const uint8_t *dh_result1, const uint8_t *dh_result2,
  242. const ed25519_public_key_t *intro_auth_pubkey,
  243. const curve25519_public_key_t *intro_enc_pubkey,
  244. const curve25519_public_key_t *client_ephemeral_enc_pubkey,
  245. const curve25519_public_key_t *service_ephemeral_rend_pubkey,
  246. uint8_t *rend_secret_hs_input_out)
  247. {
  248. uint8_t *ptr;
  249. ptr = rend_secret_hs_input_out;
  250. /* Append the first EXP() */
  251. APPEND(ptr, dh_result1, CURVE25519_OUTPUT_LEN);
  252. /* Append the other EXP() */
  253. APPEND(ptr, dh_result2, CURVE25519_OUTPUT_LEN);
  254. /* Append AUTH_KEY */
  255. APPEND(ptr, intro_auth_pubkey->pubkey, ED25519_PUBKEY_LEN);
  256. /* Append B */
  257. APPEND(ptr, intro_enc_pubkey->public_key, CURVE25519_PUBKEY_LEN);
  258. /* Append X */
  259. APPEND(ptr,
  260. client_ephemeral_enc_pubkey->public_key, CURVE25519_PUBKEY_LEN);
  261. /* Append Y */
  262. APPEND(ptr,
  263. service_ephemeral_rend_pubkey->public_key, CURVE25519_PUBKEY_LEN);
  264. /* Append PROTOID */
  265. APPEND(ptr, PROTOID, strlen(PROTOID));
  266. tor_assert(ptr == rend_secret_hs_input_out + REND_SECRET_HS_INPUT_LEN);
  267. }
  268. /************************* Public functions: *******************************/
  269. /* Public function: Do the appropriate ntor calculations and derive the keys
  270. * needed to encrypt and authenticate INTRODUCE1 cells. Return 0 and place the
  271. * final key material in <b>hs_ntor_intro_cell_keys_out</b> if everything went
  272. * well, otherwise return -1;
  273. *
  274. * The relevant calculations are as follows:
  275. *
  276. * intro_secret_hs_input = EXP(B,x) | AUTH_KEY | X | B | PROTOID
  277. * info = m_hsexpand | subcredential
  278. * hs_keys = KDF(intro_secret_hs_input | t_hsenc | info, S_KEY_LEN+MAC_LEN)
  279. * ENC_KEY = hs_keys[0:S_KEY_LEN]
  280. * MAC_KEY = hs_keys[S_KEY_LEN:S_KEY_LEN+MAC_KEY_LEN]
  281. *
  282. * where:
  283. * <b>intro_auth_pubkey</b> is AUTH_KEY (found in HS descriptor),
  284. * <b>intro_enc_pubkey</b> is B (also found in HS descriptor),
  285. * <b>client_ephemeral_enc_keypair</b> is freshly generated keypair (x,X)
  286. * <b>subcredential</b> is the hidden service subcredential (of size
  287. * DIGEST256_LEN). */
  288. int
  289. hs_ntor_client_get_introduce1_keys(
  290. const ed25519_public_key_t *intro_auth_pubkey,
  291. const curve25519_public_key_t *intro_enc_pubkey,
  292. const curve25519_keypair_t *client_ephemeral_enc_keypair,
  293. const uint8_t *subcredential,
  294. hs_ntor_intro_cell_keys_t *hs_ntor_intro_cell_keys_out)
  295. {
  296. int bad = 0;
  297. uint8_t secret_input[INTRO_SECRET_HS_INPUT_LEN];
  298. uint8_t dh_result[CURVE25519_OUTPUT_LEN];
  299. tor_assert(intro_auth_pubkey);
  300. tor_assert(intro_enc_pubkey);
  301. tor_assert(client_ephemeral_enc_keypair);
  302. tor_assert(subcredential);
  303. tor_assert(hs_ntor_intro_cell_keys_out);
  304. /* Calculate EXP(B,x) */
  305. curve25519_handshake(dh_result,
  306. &client_ephemeral_enc_keypair->seckey,
  307. intro_enc_pubkey);
  308. bad |= safe_mem_is_zero(dh_result, CURVE25519_OUTPUT_LEN);
  309. /* Get intro_secret_hs_input */
  310. get_intro_secret_hs_input(dh_result, intro_auth_pubkey,
  311. &client_ephemeral_enc_keypair->pubkey,
  312. intro_enc_pubkey, secret_input);
  313. bad |= safe_mem_is_zero(secret_input, CURVE25519_OUTPUT_LEN);
  314. /* Get ENC_KEY and MAC_KEY! */
  315. get_introduce1_key_material(secret_input, subcredential,
  316. hs_ntor_intro_cell_keys_out);
  317. /* Cleanup */
  318. memwipe(secret_input, 0, sizeof(secret_input));
  319. if (bad) {
  320. memwipe(hs_ntor_intro_cell_keys_out, 0, sizeof(hs_ntor_intro_cell_keys_t));
  321. }
  322. return bad ? -1 : 0;
  323. }
  324. /* Public function: Do the appropriate ntor calculations and derive the keys
  325. * needed to verify RENDEZVOUS1 cells and encrypt further rendezvous
  326. * traffic. Return 0 and place the final key material in
  327. * <b>hs_ntor_rend_cell_keys_out</b> if everything went well, else return -1.
  328. *
  329. * The relevant calculations are as follows:
  330. *
  331. * rend_secret_hs_input = EXP(Y,x) | EXP(B,x) | AUTH_KEY | B | X | Y | PROTOID
  332. * NTOR_KEY_SEED = MAC(rend_secret_hs_input, t_hsenc)
  333. * verify = MAC(rend_secret_hs_input, t_hsverify)
  334. * auth_input = verify | AUTH_KEY | B | Y | X | PROTOID | "Server"
  335. * auth_input_mac = MAC(auth_input, t_hsmac)
  336. *
  337. * where:
  338. * <b>intro_auth_pubkey</b> is AUTH_KEY (found in HS descriptor),
  339. * <b>client_ephemeral_enc_keypair</b> is freshly generated keypair (x,X)
  340. * <b>intro_enc_pubkey</b> is B (also found in HS descriptor),
  341. * <b>service_ephemeral_rend_pubkey</b> is Y (SERVER_PK in RENDEZVOUS1 cell) */
  342. int
  343. hs_ntor_client_get_rendezvous1_keys(
  344. const ed25519_public_key_t *intro_auth_pubkey,
  345. const curve25519_keypair_t *client_ephemeral_enc_keypair,
  346. const curve25519_public_key_t *intro_enc_pubkey,
  347. const curve25519_public_key_t *service_ephemeral_rend_pubkey,
  348. hs_ntor_rend_cell_keys_t *hs_ntor_rend_cell_keys_out)
  349. {
  350. int bad = 0;
  351. uint8_t rend_secret_hs_input[REND_SECRET_HS_INPUT_LEN];
  352. uint8_t dh_result1[CURVE25519_OUTPUT_LEN];
  353. uint8_t dh_result2[CURVE25519_OUTPUT_LEN];
  354. tor_assert(intro_auth_pubkey);
  355. tor_assert(client_ephemeral_enc_keypair);
  356. tor_assert(intro_enc_pubkey);
  357. tor_assert(service_ephemeral_rend_pubkey);
  358. tor_assert(hs_ntor_rend_cell_keys_out);
  359. /* Compute EXP(Y, x) */
  360. curve25519_handshake(dh_result1,
  361. &client_ephemeral_enc_keypair->seckey,
  362. service_ephemeral_rend_pubkey);
  363. bad |= safe_mem_is_zero(dh_result1, CURVE25519_OUTPUT_LEN);
  364. /* Compute EXP(B, x) */
  365. curve25519_handshake(dh_result2,
  366. &client_ephemeral_enc_keypair->seckey,
  367. intro_enc_pubkey);
  368. bad |= safe_mem_is_zero(dh_result2, CURVE25519_OUTPUT_LEN);
  369. /* Get rend_secret_hs_input */
  370. get_rend_secret_hs_input(dh_result1, dh_result2,
  371. intro_auth_pubkey, intro_enc_pubkey,
  372. &client_ephemeral_enc_keypair->pubkey,
  373. service_ephemeral_rend_pubkey,
  374. rend_secret_hs_input);
  375. /* Get NTOR_KEY_SEED and the auth_input MAC */
  376. bad |= get_rendezvous1_key_material(rend_secret_hs_input,
  377. intro_auth_pubkey,
  378. intro_enc_pubkey,
  379. service_ephemeral_rend_pubkey,
  380. &client_ephemeral_enc_keypair->pubkey,
  381. hs_ntor_rend_cell_keys_out);
  382. memwipe(rend_secret_hs_input, 0, sizeof(rend_secret_hs_input));
  383. if (bad) {
  384. memwipe(hs_ntor_rend_cell_keys_out, 0, sizeof(hs_ntor_rend_cell_keys_t));
  385. }
  386. return bad ? -1 : 0;
  387. }
  388. /* Public function: Do the appropriate ntor calculations and derive the keys
  389. * needed to decrypt and verify INTRODUCE1 cells. Return 0 and place the final
  390. * key material in <b>hs_ntor_intro_cell_keys_out</b> if everything went well,
  391. * otherwise return -1;
  392. *
  393. * The relevant calculations are as follows:
  394. *
  395. * intro_secret_hs_input = EXP(X,b) | AUTH_KEY | X | B | PROTOID
  396. * info = m_hsexpand | subcredential
  397. * hs_keys = KDF(intro_secret_hs_input | t_hsenc | info, S_KEY_LEN+MAC_LEN)
  398. * HS_DEC_KEY = hs_keys[0:S_KEY_LEN]
  399. * HS_MAC_KEY = hs_keys[S_KEY_LEN:S_KEY_LEN+MAC_KEY_LEN]
  400. *
  401. * where:
  402. * <b>intro_auth_pubkey</b> is AUTH_KEY (introduction point auth key),
  403. * <b>intro_enc_keypair</b> is (b,B) (introduction point encryption keypair),
  404. * <b>client_ephemeral_enc_pubkey</b> is X (CLIENT_PK in INTRODUCE2 cell),
  405. * <b>subcredential</b> is the HS subcredential (of size DIGEST256_LEN) */
  406. int
  407. hs_ntor_service_get_introduce1_keys(
  408. const ed25519_public_key_t *intro_auth_pubkey,
  409. const curve25519_keypair_t *intro_enc_keypair,
  410. const curve25519_public_key_t *client_ephemeral_enc_pubkey,
  411. const uint8_t *subcredential,
  412. hs_ntor_intro_cell_keys_t *hs_ntor_intro_cell_keys_out)
  413. {
  414. int bad = 0;
  415. uint8_t secret_input[INTRO_SECRET_HS_INPUT_LEN];
  416. uint8_t dh_result[CURVE25519_OUTPUT_LEN];
  417. tor_assert(intro_auth_pubkey);
  418. tor_assert(intro_enc_keypair);
  419. tor_assert(client_ephemeral_enc_pubkey);
  420. tor_assert(subcredential);
  421. tor_assert(hs_ntor_intro_cell_keys_out);
  422. /* Compute EXP(X, b) */
  423. curve25519_handshake(dh_result,
  424. &intro_enc_keypair->seckey,
  425. client_ephemeral_enc_pubkey);
  426. bad |= safe_mem_is_zero(dh_result, CURVE25519_OUTPUT_LEN);
  427. /* Get intro_secret_hs_input */
  428. get_intro_secret_hs_input(dh_result, intro_auth_pubkey,
  429. client_ephemeral_enc_pubkey,
  430. &intro_enc_keypair->pubkey,
  431. secret_input);
  432. bad |= safe_mem_is_zero(secret_input, CURVE25519_OUTPUT_LEN);
  433. /* Get ENC_KEY and MAC_KEY! */
  434. get_introduce1_key_material(secret_input, subcredential,
  435. hs_ntor_intro_cell_keys_out);
  436. memwipe(secret_input, 0, sizeof(secret_input));
  437. if (bad) {
  438. memwipe(hs_ntor_intro_cell_keys_out, 0, sizeof(hs_ntor_intro_cell_keys_t));
  439. }
  440. return bad ? -1 : 0;
  441. }
  442. /* Public function: Do the appropriate ntor calculations and derive the keys
  443. * needed to create and authenticate RENDEZVOUS1 cells. Return 0 and place the
  444. * final key material in <b>hs_ntor_rend_cell_keys_out</b> if all went fine,
  445. * return -1 if error happened.
  446. *
  447. * The relevant calculations are as follows:
  448. *
  449. * rend_secret_hs_input = EXP(X,y) | EXP(X,b) | AUTH_KEY | B | X | Y | PROTOID
  450. * NTOR_KEY_SEED = MAC(rend_secret_hs_input, t_hsenc)
  451. * verify = MAC(rend_secret_hs_input, t_hsverify)
  452. * auth_input = verify | AUTH_KEY | B | Y | X | PROTOID | "Server"
  453. * auth_input_mac = MAC(auth_input, t_hsmac)
  454. *
  455. * where:
  456. * <b>intro_auth_pubkey</b> is AUTH_KEY (intro point auth key),
  457. * <b>intro_enc_keypair</b> is (b,B) (intro point enc keypair)
  458. * <b>service_ephemeral_rend_keypair</b> is a fresh (y,Y) keypair
  459. * <b>client_ephemeral_enc_pubkey</b> is X (CLIENT_PK in INTRODUCE2 cell) */
  460. int
  461. hs_ntor_service_get_rendezvous1_keys(
  462. const ed25519_public_key_t *intro_auth_pubkey,
  463. const curve25519_keypair_t *intro_enc_keypair,
  464. const curve25519_keypair_t *service_ephemeral_rend_keypair,
  465. const curve25519_public_key_t *client_ephemeral_enc_pubkey,
  466. hs_ntor_rend_cell_keys_t *hs_ntor_rend_cell_keys_out)
  467. {
  468. int bad = 0;
  469. uint8_t rend_secret_hs_input[REND_SECRET_HS_INPUT_LEN];
  470. uint8_t dh_result1[CURVE25519_OUTPUT_LEN];
  471. uint8_t dh_result2[CURVE25519_OUTPUT_LEN];
  472. tor_assert(intro_auth_pubkey);
  473. tor_assert(intro_enc_keypair);
  474. tor_assert(service_ephemeral_rend_keypair);
  475. tor_assert(client_ephemeral_enc_pubkey);
  476. tor_assert(hs_ntor_rend_cell_keys_out);
  477. /* Compute EXP(X, y) */
  478. curve25519_handshake(dh_result1,
  479. &service_ephemeral_rend_keypair->seckey,
  480. client_ephemeral_enc_pubkey);
  481. bad |= safe_mem_is_zero(dh_result1, CURVE25519_OUTPUT_LEN);
  482. /* Compute EXP(X, b) */
  483. curve25519_handshake(dh_result2,
  484. &intro_enc_keypair->seckey,
  485. client_ephemeral_enc_pubkey);
  486. bad |= safe_mem_is_zero(dh_result2, CURVE25519_OUTPUT_LEN);
  487. /* Get rend_secret_hs_input */
  488. get_rend_secret_hs_input(dh_result1, dh_result2,
  489. intro_auth_pubkey,
  490. &intro_enc_keypair->pubkey,
  491. client_ephemeral_enc_pubkey,
  492. &service_ephemeral_rend_keypair->pubkey,
  493. rend_secret_hs_input);
  494. /* Get NTOR_KEY_SEED and AUTH_INPUT_MAC! */
  495. bad |= get_rendezvous1_key_material(rend_secret_hs_input,
  496. intro_auth_pubkey,
  497. &intro_enc_keypair->pubkey,
  498. &service_ephemeral_rend_keypair->pubkey,
  499. client_ephemeral_enc_pubkey,
  500. hs_ntor_rend_cell_keys_out);
  501. memwipe(rend_secret_hs_input, 0, sizeof(rend_secret_hs_input));
  502. if (bad) {
  503. memwipe(hs_ntor_rend_cell_keys_out, 0, sizeof(hs_ntor_rend_cell_keys_t));
  504. }
  505. return bad ? -1 : 0;
  506. }
  507. /** Given a received RENDEZVOUS2 MAC in <b>mac</b> (of length DIGEST256_LEN),
  508. * and the RENDEZVOUS1 key material in <b>hs_ntor_rend_cell_keys</b>, return 1
  509. * if the MAC is good, otherwise return 0. */
  510. int
  511. hs_ntor_client_rendezvous2_mac_is_good(
  512. const hs_ntor_rend_cell_keys_t *hs_ntor_rend_cell_keys,
  513. const uint8_t *rcvd_mac)
  514. {
  515. tor_assert(rcvd_mac);
  516. tor_assert(hs_ntor_rend_cell_keys);
  517. return tor_memeq(hs_ntor_rend_cell_keys->rend_cell_auth_mac,
  518. rcvd_mac, DIGEST256_LEN);
  519. }
  520. /* Input length to KDF for key expansion */
  521. #define NTOR_KEY_EXPANSION_KDF_INPUT_LEN (DIGEST256_LEN + M_HSEXPAND_LEN)
  522. /** Given the rendezvous key seed in <b>ntor_key_seed</b> (of size
  523. * DIGEST256_LEN), do the circuit key expansion as specified by section
  524. * '4.2.1. Key expansion' and place the keys in <b>keys_out</b> (which must be
  525. * of size HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN).
  526. *
  527. * Return 0 if things went well, else return -1. */
  528. int
  529. hs_ntor_circuit_key_expansion(const uint8_t *ntor_key_seed, size_t seed_len,
  530. uint8_t *keys_out, size_t keys_out_len)
  531. {
  532. uint8_t *ptr;
  533. uint8_t kdf_input[NTOR_KEY_EXPANSION_KDF_INPUT_LEN];
  534. crypto_xof_t *xof;
  535. /* Sanity checks on lengths to make sure we are good */
  536. if (BUG(seed_len != DIGEST256_LEN)) {
  537. return -1;
  538. }
  539. if (BUG(keys_out_len != HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN)) {
  540. return -1;
  541. }
  542. /* Let's build the input to the KDF */
  543. ptr = kdf_input;
  544. APPEND(ptr, ntor_key_seed, DIGEST256_LEN);
  545. APPEND(ptr, M_HSEXPAND, strlen(M_HSEXPAND));
  546. tor_assert(ptr == kdf_input + sizeof(kdf_input));
  547. /* Generate the keys */
  548. xof = crypto_xof_new();
  549. crypto_xof_add_bytes(xof, kdf_input, sizeof(kdf_input));
  550. crypto_xof_squeeze_bytes(xof, keys_out, HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN);
  551. crypto_xof_free(xof);
  552. return 0;
  553. }