onion_fast.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /**
  7. * \file onion_fast.c
  8. * \brief Functions implement the CREATE_FAST circuit handshake.
  9. *
  10. * The "CREATE_FAST" handshake is an unauthenticated, non-forward-secure
  11. * key derivation mechanism based on SHA1. We used to use it for the
  12. * first hop of each circuit, since the TAP handshake provided no
  13. * additional security beyond the security already provided by the TLS
  14. * handshake [*].
  15. *
  16. * When we switched to ntor, we deprecated CREATE_FAST, since ntor is
  17. * stronger than our TLS handshake was, and fast enough to not be worrisome.
  18. *
  19. * This handshake, like the other circuit-extension handshakes, is
  20. * invoked from onion.c.
  21. *
  22. * [*]Actually, it's possible that TAP _was_ a little better than TLS with
  23. * RSA1024 certificates and EDH1024 for forward secrecy, if you
  24. * hypothesize an adversary who can compute discrete logarithms on a
  25. * small number of targeted DH1024 fields, but who can't break all that
  26. * many RSA1024 keys.
  27. **/
  28. #include "core/or/or.h"
  29. #include "core/crypto/onion_fast.h"
  30. #include "lib/crypt_ops/crypto_hkdf.h"
  31. #include "lib/crypt_ops/crypto_rand.h"
  32. #include "lib/crypt_ops/crypto_util.h"
  33. /** Release all state held in <b>victim</b>. */
  34. void
  35. fast_handshake_state_free_(fast_handshake_state_t *victim)
  36. {
  37. if (! victim)
  38. return;
  39. memwipe(victim, 0, sizeof(fast_handshake_state_t));
  40. tor_free(victim);
  41. }
  42. /** Create the state needed to perform a CREATE_FAST handshake. Return 0
  43. * on success, -1 on failure. */
  44. int
  45. fast_onionskin_create(fast_handshake_state_t **handshake_state_out,
  46. uint8_t *handshake_out)
  47. {
  48. fast_handshake_state_t *s;
  49. *handshake_state_out = s = tor_malloc(sizeof(fast_handshake_state_t));
  50. crypto_rand((char*)s->state, sizeof(s->state));
  51. memcpy(handshake_out, s->state, DIGEST_LEN);
  52. return 0;
  53. }
  54. /** Implement the server side of the CREATE_FAST abbreviated handshake. The
  55. * client has provided DIGEST_LEN key bytes in <b>key_in</b> ("x"). We
  56. * generate a reply of DIGEST_LEN*2 bytes in <b>key_out</b>, consisting of a
  57. * new random "y", followed by H(x|y) to check for correctness. We set
  58. * <b>key_out_len</b> bytes of key material in <b>key_out</b>.
  59. * Return 0 on success, &lt;0 on failure.
  60. **/
  61. int
  62. fast_server_handshake(const uint8_t *key_in, /* DIGEST_LEN bytes */
  63. uint8_t *handshake_reply_out, /* DIGEST_LEN*2 bytes */
  64. uint8_t *key_out,
  65. size_t key_out_len)
  66. {
  67. uint8_t tmp[DIGEST_LEN+DIGEST_LEN];
  68. uint8_t *out = NULL;
  69. size_t out_len;
  70. int r = -1;
  71. crypto_rand((char*)handshake_reply_out, DIGEST_LEN);
  72. memcpy(tmp, key_in, DIGEST_LEN);
  73. memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
  74. out_len = key_out_len+DIGEST_LEN;
  75. out = tor_malloc(out_len);
  76. if (BUG(crypto_expand_key_material_TAP(tmp, sizeof(tmp), out, out_len))) {
  77. goto done; // LCOV_EXCL_LINE
  78. }
  79. memcpy(handshake_reply_out+DIGEST_LEN, out, DIGEST_LEN);
  80. memcpy(key_out, out+DIGEST_LEN, key_out_len);
  81. r = 0;
  82. done:
  83. memwipe(tmp, 0, sizeof(tmp));
  84. memwipe(out, 0, out_len);
  85. tor_free(out);
  86. return r;
  87. }
  88. /** Implement the second half of the client side of the CREATE_FAST handshake.
  89. * We sent the server <b>handshake_state</b> ("x") already, and the server
  90. * told us <b>handshake_reply_out</b> (y|H(x|y)). Make sure that the hash is
  91. * correct, and generate key material in <b>key_out</b>. Return 0 on success,
  92. * true on failure.
  93. *
  94. * NOTE: The "CREATE_FAST" handshake path is distinguishable from regular
  95. * "onionskin" handshakes, and is not secure if an adversary can see or modify
  96. * the messages. Therefore, it should only be used by clients, and only as
  97. * the first hop of a circuit (since the first hop is already authenticated
  98. * and protected by TLS).
  99. */
  100. int
  101. fast_client_handshake(const fast_handshake_state_t *handshake_state,
  102. const uint8_t *handshake_reply_out,/*DIGEST_LEN*2 bytes*/
  103. uint8_t *key_out,
  104. size_t key_out_len,
  105. const char **msg_out)
  106. {
  107. uint8_t tmp[DIGEST_LEN+DIGEST_LEN];
  108. uint8_t *out;
  109. size_t out_len;
  110. int r = -1;
  111. memcpy(tmp, handshake_state->state, DIGEST_LEN);
  112. memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
  113. out_len = key_out_len+DIGEST_LEN;
  114. out = tor_malloc(out_len);
  115. if (BUG(crypto_expand_key_material_TAP(tmp, sizeof(tmp), out, out_len))) {
  116. /* LCOV_EXCL_START */
  117. if (msg_out)
  118. *msg_out = "Failed to expand key material";
  119. goto done;
  120. /* LCOV_EXCL_STOP */
  121. }
  122. if (tor_memneq(out, handshake_reply_out+DIGEST_LEN, DIGEST_LEN)) {
  123. /* H(K) does *not* match. Something fishy. */
  124. if (msg_out)
  125. *msg_out = "Digest DOES NOT MATCH on fast handshake. Bug or attack.";
  126. goto done;
  127. }
  128. memcpy(key_out, out+DIGEST_LEN, key_out_len);
  129. r = 0;
  130. done:
  131. memwipe(tmp, 0, sizeof(tmp));
  132. memwipe(out, 0, out_len);
  133. tor_free(out);
  134. return r;
  135. }