onion_fast.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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-2012, 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. /** Implement the server side of the CREATE_FAST abbreviated handshake. The
  13. * client has provided DIGEST_LEN key bytes in <b>key_in</b> ("x"). We
  14. * generate a reply of DIGEST_LEN*2 bytes in <b>key_out</b>, consisting of a
  15. * new random "y", followed by H(x|y) to check for correctness. We set
  16. * <b>key_out_len</b> bytes of key material in <b>key_out</b>.
  17. * Return 0 on success, &lt;0 on failure.
  18. **/
  19. int
  20. fast_server_handshake(const uint8_t *key_in, /* DIGEST_LEN bytes */
  21. uint8_t *handshake_reply_out, /* DIGEST_LEN*2 bytes */
  22. uint8_t *key_out,
  23. size_t key_out_len)
  24. {
  25. uint8_t tmp[DIGEST_LEN+DIGEST_LEN];
  26. uint8_t *out = NULL;
  27. size_t out_len;
  28. int r = -1;
  29. if (crypto_rand((char*)handshake_reply_out, DIGEST_LEN)<0)
  30. return -1;
  31. memcpy(tmp, key_in, DIGEST_LEN);
  32. memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
  33. out_len = key_out_len+DIGEST_LEN;
  34. out = tor_malloc(out_len);
  35. if (crypto_expand_key_material_TAP(tmp, sizeof(tmp), out, out_len)) {
  36. goto done;
  37. }
  38. memcpy(handshake_reply_out+DIGEST_LEN, out, DIGEST_LEN);
  39. memcpy(key_out, out+DIGEST_LEN, key_out_len);
  40. r = 0;
  41. done:
  42. memwipe(tmp, 0, sizeof(tmp));
  43. memwipe(out, 0, out_len);
  44. tor_free(out);
  45. return r;
  46. }
  47. /** Implement the second half of the client side of the CREATE_FAST handshake.
  48. * We sent the server <b>handshake_state</b> ("x") already, and the server
  49. * told us <b>handshake_reply_out</b> (y|H(x|y)). Make sure that the hash is
  50. * correct, and generate key material in <b>key_out</b>. Return 0 on success,
  51. * true on failure.
  52. *
  53. * NOTE: The "CREATE_FAST" handshake path is distinguishable from regular
  54. * "onionskin" handshakes, and is not secure if an adversary can see or modify
  55. * the messages. Therefore, it should only be used by clients, and only as
  56. * the first hop of a circuit (since the first hop is already authenticated
  57. * and protected by TLS).
  58. */
  59. int
  60. fast_client_handshake(const uint8_t *handshake_state,/*DIGEST_LEN bytes*/
  61. const uint8_t *handshake_reply_out,/*DIGEST_LEN*2 bytes*/
  62. uint8_t *key_out,
  63. size_t key_out_len)
  64. {
  65. uint8_t tmp[DIGEST_LEN+DIGEST_LEN];
  66. uint8_t *out;
  67. size_t out_len;
  68. int r = -1;
  69. memcpy(tmp, handshake_state, DIGEST_LEN);
  70. memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
  71. out_len = key_out_len+DIGEST_LEN;
  72. out = tor_malloc(out_len);
  73. if (crypto_expand_key_material_TAP(tmp, sizeof(tmp), out, out_len)) {
  74. goto done;
  75. }
  76. if (tor_memneq(out, handshake_reply_out+DIGEST_LEN, DIGEST_LEN)) {
  77. /* H(K) does *not* match. Something fishy. */
  78. log_warn(LD_PROTOCOL,"Digest DOES NOT MATCH on fast handshake. "
  79. "Bug or attack.");
  80. goto done;
  81. }
  82. memcpy(key_out, out+DIGEST_LEN, key_out_len);
  83. r = 0;
  84. done:
  85. memwipe(tmp, 0, sizeof(tmp));
  86. memwipe(out, 0, out_len);
  87. tor_free(out);
  88. return r;
  89. }