onion_fast.c 4.0 KB

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