relay_crypto.c 12 KB

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