relay_crypto.c 10 KB

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