relay_crypto.c 10 KB

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