relay_crypto.c 10 KB

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