relay_crypto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "core/or/or.h"
  7. #include "core/or/circuitlist.h"
  8. #include "app/config/config.h"
  9. #include "lib/crypt_ops/crypto_cipher.h"
  10. #include "lib/crypt_ops/crypto_util.h"
  11. #include "core/crypto/hs_ntor.h" // for HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN
  12. #include "core/or/relay.h"
  13. #include "core/crypto/relay_crypto.h"
  14. #include "core/or/cell_st.h"
  15. #include "core/or/or_circuit_st.h"
  16. #include "core/or/origin_circuit_st.h"
  17. /** Update digest from the payload of cell. Assign integrity part to
  18. * cell.
  19. */
  20. static void
  21. relay_set_digest(crypto_digest_t *digest, cell_t *cell)
  22. {
  23. char integrity[4];
  24. relay_header_t rh;
  25. crypto_digest_add_bytes(digest, (char*)cell->payload, CELL_PAYLOAD_SIZE);
  26. crypto_digest_get_digest(digest, integrity, 4);
  27. // log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
  28. // integrity[0], integrity[1], integrity[2], integrity[3]);
  29. relay_header_unpack(&rh, cell->payload);
  30. memcpy(rh.integrity, integrity, 4);
  31. relay_header_pack(cell->payload, &rh);
  32. }
  33. /** Does the digest for this circuit indicate that this cell is for us?
  34. *
  35. * Update digest from the payload of cell (with the integrity part set
  36. * to 0). If the integrity part is valid, return 1, else restore digest
  37. * and cell to their original state and return 0.
  38. */
  39. static int
  40. relay_digest_matches(crypto_digest_t *digest, cell_t *cell)
  41. {
  42. uint32_t received_integrity, calculated_integrity;
  43. relay_header_t rh;
  44. crypto_digest_checkpoint_t backup_digest;
  45. crypto_digest_checkpoint(&backup_digest, digest);
  46. relay_header_unpack(&rh, cell->payload);
  47. memcpy(&received_integrity, rh.integrity, 4);
  48. memset(rh.integrity, 0, 4);
  49. relay_header_pack(cell->payload, &rh);
  50. // log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
  51. // received_integrity[0], received_integrity[1],
  52. // received_integrity[2], received_integrity[3]);
  53. crypto_digest_add_bytes(digest, (char*) cell->payload, CELL_PAYLOAD_SIZE);
  54. crypto_digest_get_digest(digest, (char*) &calculated_integrity, 4);
  55. int rv = 1;
  56. if (calculated_integrity != received_integrity) {
  57. // log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
  58. // (%d vs %d).", received_integrity, calculated_integrity);
  59. /* restore digest to its old form */
  60. crypto_digest_restore(digest, &backup_digest);
  61. /* restore the relay header */
  62. memcpy(rh.integrity, &received_integrity, 4);
  63. relay_header_pack(cell->payload, &rh);
  64. rv = 0;
  65. }
  66. memwipe(&backup_digest, 0, sizeof(backup_digest));
  67. return rv;
  68. }
  69. /** Apply <b>cipher</b> to CELL_PAYLOAD_SIZE bytes of <b>in</b>
  70. * (in place).
  71. *
  72. * Note that we use the same operation for encrypting and for decrypting.
  73. */
  74. static void
  75. relay_crypt_one_payload(crypto_cipher_t *cipher, uint8_t *in)
  76. {
  77. crypto_cipher_crypt_inplace(cipher, (char*) in, CELL_PAYLOAD_SIZE);
  78. }
  79. /** Do the appropriate en/decryptions for <b>cell</b> arriving on
  80. * <b>circ</b> in direction <b>cell_direction</b>.
  81. *
  82. * If cell_direction == CELL_DIRECTION_IN:
  83. * - If we're at the origin (we're the OP), for hops 1..N,
  84. * decrypt cell. If recognized, stop.
  85. * - Else (we're not the OP), encrypt one hop. Cell is not recognized.
  86. *
  87. * If cell_direction == CELL_DIRECTION_OUT:
  88. * - decrypt one hop. Check if recognized.
  89. *
  90. * If cell is recognized, set *recognized to 1, and set
  91. * *layer_hint to the hop that recognized it.
  92. *
  93. * Return -1 to indicate that we should mark the circuit for close,
  94. * else return 0.
  95. */
  96. int
  97. relay_decrypt_cell(circuit_t *circ, cell_t *cell,
  98. cell_direction_t cell_direction,
  99. crypt_path_t **layer_hint, char *recognized)
  100. {
  101. relay_header_t rh;
  102. tor_assert(circ);
  103. tor_assert(cell);
  104. tor_assert(recognized);
  105. tor_assert(cell_direction == CELL_DIRECTION_IN ||
  106. cell_direction == CELL_DIRECTION_OUT);
  107. if (cell_direction == CELL_DIRECTION_IN) {
  108. if (CIRCUIT_IS_ORIGIN(circ)) { /* We're at the beginning of the circuit.
  109. * We'll want to do layered decrypts. */
  110. crypt_path_t *thishop, *cpath = TO_ORIGIN_CIRCUIT(circ)->cpath;
  111. thishop = cpath;
  112. if (thishop->state != CPATH_STATE_OPEN) {
  113. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  114. "Relay cell before first created cell? Closing.");
  115. return -1;
  116. }
  117. do { /* Remember: cpath is in forward order, that is, first hop first. */
  118. tor_assert(thishop);
  119. /* decrypt one layer */
  120. relay_crypt_one_payload(thishop->crypto.b_crypto, cell->payload);
  121. relay_header_unpack(&rh, cell->payload);
  122. if (rh.recognized == 0) {
  123. /* it's possibly recognized. have to check digest to be sure. */
  124. if (relay_digest_matches(thishop->crypto.b_digest, cell)) {
  125. *recognized = 1;
  126. *layer_hint = thishop;
  127. return 0;
  128. }
  129. }
  130. thishop = thishop->next;
  131. } while (thishop != cpath && thishop->state == CPATH_STATE_OPEN);
  132. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  133. "Incoming cell at client not recognized. Closing.");
  134. return -1;
  135. } else {
  136. relay_crypto_t *crypto = &TO_OR_CIRCUIT(circ)->crypto;
  137. /* We're in the middle. Encrypt one layer. */
  138. relay_crypt_one_payload(crypto->b_crypto, cell->payload);
  139. }
  140. } else /* cell_direction == CELL_DIRECTION_OUT */ {
  141. /* We're in the middle. Decrypt one layer. */
  142. relay_crypto_t *crypto = &TO_OR_CIRCUIT(circ)->crypto;
  143. relay_crypt_one_payload(crypto->f_crypto, cell->payload);
  144. relay_header_unpack(&rh, cell->payload);
  145. if (rh.recognized == 0) {
  146. /* it's possibly recognized. have to check digest to be sure. */
  147. if (relay_digest_matches(crypto->f_digest, cell)) {
  148. *recognized = 1;
  149. return 0;
  150. }
  151. }
  152. }
  153. return 0;
  154. }
  155. /**
  156. * Encrypt a cell <b>cell</b> that we are creating, and sending outbound on
  157. * <b>circ</b> until the hop corresponding to <b>layer_hint</b>.
  158. *
  159. * The integrity field and recognized field of <b>cell</b>'s relay headers
  160. * must be set to zero.
  161. */
  162. void
  163. relay_encrypt_cell_outbound(cell_t *cell,
  164. origin_circuit_t *circ,
  165. crypt_path_t *layer_hint)
  166. {
  167. crypt_path_t *thishop; /* counter for repeated crypts */
  168. relay_set_digest(layer_hint->crypto.f_digest, cell);
  169. thishop = layer_hint;
  170. /* moving from farthest to nearest hop */
  171. do {
  172. tor_assert(thishop);
  173. log_debug(LD_OR,"encrypting a layer of the relay cell.");
  174. relay_crypt_one_payload(thishop->crypto.f_crypto, cell->payload);
  175. thishop = thishop->prev;
  176. } while (thishop != circ->cpath->prev);
  177. }
  178. /**
  179. * Encrypt a cell <b>cell</b> that we are creating, and sending on
  180. * <b>circuit</b> to the origin.
  181. *
  182. * The integrity field and recognized field of <b>cell</b>'s relay headers
  183. * must be set to zero.
  184. */
  185. void
  186. relay_encrypt_cell_inbound(cell_t *cell,
  187. or_circuit_t *or_circ)
  188. {
  189. relay_set_digest(or_circ->crypto.b_digest, cell);
  190. /* encrypt one layer */
  191. relay_crypt_one_payload(or_circ->crypto.b_crypto, cell->payload);
  192. }
  193. /**
  194. * Release all storage held inside <b>crypto</b>, but do not free
  195. * <b>crypto</b> itself: it lives inside another object.
  196. */
  197. void
  198. relay_crypto_clear(relay_crypto_t *crypto)
  199. {
  200. if (BUG(!crypto))
  201. return;
  202. crypto_cipher_free(crypto->f_crypto);
  203. crypto_cipher_free(crypto->b_crypto);
  204. crypto_digest_free(crypto->f_digest);
  205. crypto_digest_free(crypto->b_digest);
  206. }
  207. /** Initialize <b>crypto</b> from the key material in key_data.
  208. *
  209. * If <b>is_hs_v3</b> is set, this cpath will be used for next gen hidden
  210. * service circuits and <b>key_data</b> must be at least
  211. * HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN bytes in length.
  212. *
  213. * If <b>is_hs_v3</b> is not set, key_data must contain CPATH_KEY_MATERIAL_LEN
  214. * bytes, which are used as follows:
  215. * - 20 to initialize f_digest
  216. * - 20 to initialize b_digest
  217. * - 16 to key f_crypto
  218. * - 16 to key b_crypto
  219. *
  220. * (If 'reverse' is true, then f_XX and b_XX are swapped.)
  221. *
  222. * Return 0 if init was successful, else -1 if it failed.
  223. */
  224. int
  225. relay_crypto_init(relay_crypto_t *crypto,
  226. const char *key_data, size_t key_data_len,
  227. int reverse, int is_hs_v3)
  228. {
  229. crypto_digest_t *tmp_digest;
  230. crypto_cipher_t *tmp_crypto;
  231. size_t digest_len = 0;
  232. size_t cipher_key_len = 0;
  233. tor_assert(crypto);
  234. tor_assert(key_data);
  235. tor_assert(!(crypto->f_crypto || crypto->b_crypto ||
  236. crypto->f_digest || crypto->b_digest));
  237. /* Basic key size validation */
  238. if (is_hs_v3 && BUG(key_data_len != HS_NTOR_KEY_EXPANSION_KDF_OUT_LEN)) {
  239. goto err;
  240. } else if (!is_hs_v3 && BUG(key_data_len != CPATH_KEY_MATERIAL_LEN)) {
  241. goto err;
  242. }
  243. /* If we are using this crypto for next gen onion services use SHA3-256,
  244. otherwise use good ol' SHA1 */
  245. if (is_hs_v3) {
  246. digest_len = DIGEST256_LEN;
  247. cipher_key_len = CIPHER256_KEY_LEN;
  248. crypto->f_digest = crypto_digest256_new(DIGEST_SHA3_256);
  249. crypto->b_digest = crypto_digest256_new(DIGEST_SHA3_256);
  250. } else {
  251. digest_len = DIGEST_LEN;
  252. cipher_key_len = CIPHER_KEY_LEN;
  253. crypto->f_digest = crypto_digest_new();
  254. crypto->b_digest = crypto_digest_new();
  255. }
  256. tor_assert(digest_len != 0);
  257. tor_assert(cipher_key_len != 0);
  258. const int cipher_key_bits = (int) cipher_key_len * 8;
  259. crypto_digest_add_bytes(crypto->f_digest, key_data, digest_len);
  260. crypto_digest_add_bytes(crypto->b_digest, key_data+digest_len, digest_len);
  261. crypto->f_crypto = crypto_cipher_new_with_bits(key_data+(2*digest_len),
  262. cipher_key_bits);
  263. if (!crypto->f_crypto) {
  264. log_warn(LD_BUG,"Forward cipher initialization failed.");
  265. goto err;
  266. }
  267. crypto->b_crypto = crypto_cipher_new_with_bits(
  268. key_data+(2*digest_len)+cipher_key_len,
  269. cipher_key_bits);
  270. if (!crypto->b_crypto) {
  271. log_warn(LD_BUG,"Backward cipher initialization failed.");
  272. goto err;
  273. }
  274. if (reverse) {
  275. tmp_digest = crypto->f_digest;
  276. crypto->f_digest = crypto->b_digest;
  277. crypto->b_digest = tmp_digest;
  278. tmp_crypto = crypto->f_crypto;
  279. crypto->f_crypto = crypto->b_crypto;
  280. crypto->b_crypto = tmp_crypto;
  281. }
  282. return 0;
  283. err:
  284. relay_crypto_clear(crypto);
  285. return -1;
  286. }
  287. /** Assert that <b>crypto</b> is valid and set. */
  288. void
  289. relay_crypto_assert_ok(const relay_crypto_t *crypto)
  290. {
  291. tor_assert(crypto->f_crypto);
  292. tor_assert(crypto->b_crypto);
  293. tor_assert(crypto->f_digest);
  294. tor_assert(crypto->b_digest);
  295. }