onion_fast.c 3.8 KB

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