onion_fast.c 4.9 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-2018, 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 "or/or.h"
  29. #include "or/onion_fast.h"
  30. #include "lib/crypt_ops/crypto_rand.h"
  31. #include "lib/crypt_ops/crypto_util.h"
  32. /** Release all state held in <b>victim</b>. */
  33. void
  34. fast_handshake_state_free_(fast_handshake_state_t *victim)
  35. {
  36. if (! victim)
  37. return;
  38. memwipe(victim, 0, sizeof(fast_handshake_state_t));
  39. tor_free(victim);
  40. }
  41. /** Create the state needed to perform a CREATE_FAST handshake. Return 0
  42. * on success, -1 on failure. */
  43. int
  44. fast_onionskin_create(fast_handshake_state_t **handshake_state_out,
  45. uint8_t *handshake_out)
  46. {
  47. fast_handshake_state_t *s;
  48. *handshake_state_out = s = tor_malloc(sizeof(fast_handshake_state_t));
  49. crypto_rand((char*)s->state, sizeof(s->state));
  50. memcpy(handshake_out, s->state, DIGEST_LEN);
  51. return 0;
  52. }
  53. /** Implement the server side of the CREATE_FAST abbreviated handshake. The
  54. * client has provided DIGEST_LEN key bytes in <b>key_in</b> ("x"). We
  55. * generate a reply of DIGEST_LEN*2 bytes in <b>key_out</b>, consisting of a
  56. * new random "y", followed by H(x|y) to check for correctness. We set
  57. * <b>key_out_len</b> bytes of key material in <b>key_out</b>.
  58. * Return 0 on success, &lt;0 on failure.
  59. **/
  60. int
  61. fast_server_handshake(const uint8_t *key_in, /* DIGEST_LEN bytes */
  62. uint8_t *handshake_reply_out, /* DIGEST_LEN*2 bytes */
  63. uint8_t *key_out,
  64. size_t key_out_len)
  65. {
  66. uint8_t tmp[DIGEST_LEN+DIGEST_LEN];
  67. uint8_t *out = NULL;
  68. size_t out_len;
  69. int r = -1;
  70. crypto_rand((char*)handshake_reply_out, DIGEST_LEN);
  71. memcpy(tmp, key_in, DIGEST_LEN);
  72. memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
  73. out_len = key_out_len+DIGEST_LEN;
  74. out = tor_malloc(out_len);
  75. if (BUG(crypto_expand_key_material_TAP(tmp, sizeof(tmp), out, out_len))) {
  76. goto done; // LCOV_EXCL_LINE
  77. }
  78. memcpy(handshake_reply_out+DIGEST_LEN, out, DIGEST_LEN);
  79. memcpy(key_out, out+DIGEST_LEN, key_out_len);
  80. r = 0;
  81. done:
  82. memwipe(tmp, 0, sizeof(tmp));
  83. memwipe(out, 0, out_len);
  84. tor_free(out);
  85. return r;
  86. }
  87. /** Implement the second half of the client side of the CREATE_FAST handshake.
  88. * We sent the server <b>handshake_state</b> ("x") already, and the server
  89. * told us <b>handshake_reply_out</b> (y|H(x|y)). Make sure that the hash is
  90. * correct, and generate key material in <b>key_out</b>. Return 0 on success,
  91. * true on failure.
  92. *
  93. * NOTE: The "CREATE_FAST" handshake path is distinguishable from regular
  94. * "onionskin" handshakes, and is not secure if an adversary can see or modify
  95. * the messages. Therefore, it should only be used by clients, and only as
  96. * the first hop of a circuit (since the first hop is already authenticated
  97. * and protected by TLS).
  98. */
  99. int
  100. fast_client_handshake(const fast_handshake_state_t *handshake_state,
  101. const uint8_t *handshake_reply_out,/*DIGEST_LEN*2 bytes*/
  102. uint8_t *key_out,
  103. size_t key_out_len,
  104. const char **msg_out)
  105. {
  106. uint8_t tmp[DIGEST_LEN+DIGEST_LEN];
  107. uint8_t *out;
  108. size_t out_len;
  109. int r = -1;
  110. memcpy(tmp, handshake_state->state, DIGEST_LEN);
  111. memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
  112. out_len = key_out_len+DIGEST_LEN;
  113. out = tor_malloc(out_len);
  114. if (BUG(crypto_expand_key_material_TAP(tmp, sizeof(tmp), out, out_len))) {
  115. /* LCOV_EXCL_START */
  116. if (msg_out)
  117. *msg_out = "Failed to expand key material";
  118. goto done;
  119. /* LCOV_EXCL_STOP */
  120. }
  121. if (tor_memneq(out, handshake_reply_out+DIGEST_LEN, DIGEST_LEN)) {
  122. /* H(K) does *not* match. Something fishy. */
  123. if (msg_out)
  124. *msg_out = "Digest DOES NOT MATCH on fast handshake. Bug or attack.";
  125. goto done;
  126. }
  127. memcpy(key_out, out+DIGEST_LEN, key_out_len);
  128. r = 0;
  129. done:
  130. memwipe(tmp, 0, sizeof(tmp));
  131. memwipe(out, 0, out_len);
  132. tor_free(out);
  133. return r;
  134. }