tor  0.4.1.0-alpha-dev
onion_fast.c
Go to the documentation of this file.
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 
30 #include "core/or/or.h"
31 #include "core/crypto/onion_fast.h"
35 
37 void
39 {
40  if (! victim)
41  return;
42  memwipe(victim, 0, sizeof(fast_handshake_state_t));
43  tor_free(victim);
44 }
45 
48 int
50  uint8_t *handshake_out)
51 {
53  *handshake_state_out = s = tor_malloc(sizeof(fast_handshake_state_t));
54  crypto_rand((char*)s->state, sizeof(s->state));
55  memcpy(handshake_out, s->state, DIGEST_LEN);
56  return 0;
57 }
58 
66 int
67 fast_server_handshake(const uint8_t *key_in, /* DIGEST_LEN bytes */
68  uint8_t *handshake_reply_out, /* DIGEST_LEN*2 bytes */
69  uint8_t *key_out,
70  size_t key_out_len)
71 {
72  uint8_t tmp[DIGEST_LEN+DIGEST_LEN];
73  uint8_t *out = NULL;
74  size_t out_len;
75  int r = -1;
76 
77  crypto_rand((char*)handshake_reply_out, DIGEST_LEN);
78 
79  memcpy(tmp, key_in, DIGEST_LEN);
80  memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
81  out_len = key_out_len+DIGEST_LEN;
82  out = tor_malloc(out_len);
83  if (BUG(crypto_expand_key_material_TAP(tmp, sizeof(tmp), out, out_len))) {
84  goto done; // LCOV_EXCL_LINE
85  }
86  memcpy(handshake_reply_out+DIGEST_LEN, out, DIGEST_LEN);
87  memcpy(key_out, out+DIGEST_LEN, key_out_len);
88  r = 0;
89  done:
90  memwipe(tmp, 0, sizeof(tmp));
91  memwipe(out, 0, out_len);
92  tor_free(out);
93  return r;
94 }
95 
108 int
110  const uint8_t *handshake_reply_out,/*DIGEST_LEN*2 bytes*/
111  uint8_t *key_out,
112  size_t key_out_len,
113  const char **msg_out)
114 {
115  uint8_t tmp[DIGEST_LEN+DIGEST_LEN];
116  uint8_t *out;
117  size_t out_len;
118  int r = -1;
119 
120  memcpy(tmp, handshake_state->state, DIGEST_LEN);
121  memcpy(tmp+DIGEST_LEN, handshake_reply_out, DIGEST_LEN);
122  out_len = key_out_len+DIGEST_LEN;
123  out = tor_malloc(out_len);
124  if (BUG(crypto_expand_key_material_TAP(tmp, sizeof(tmp), out, out_len))) {
125  /* LCOV_EXCL_START */
126  if (msg_out)
127  *msg_out = "Failed to expand key material";
128  goto done;
129  /* LCOV_EXCL_STOP */
130  }
131  if (tor_memneq(out, handshake_reply_out+DIGEST_LEN, DIGEST_LEN)) {
132  /* H(K) does *not* match. Something fishy. */
133  if (msg_out)
134  *msg_out = "Digest DOES NOT MATCH on fast handshake. Bug or attack.";
135  goto done;
136  }
137  memcpy(key_out, out+DIGEST_LEN, key_out_len);
138  r = 0;
139  done:
140  memwipe(tmp, 0, sizeof(tmp));
141  memwipe(out, 0, out_len);
142  tor_free(out);
143  return r;
144 }
Common functions for using (pseudo-)random number generators.
int crypto_expand_key_material_TAP(const uint8_t *key_in, size_t key_in_len, uint8_t *key_out, size_t key_out_len)
Definition: crypto_hkdf.c:43
int fast_onionskin_create(fast_handshake_state_t **handshake_state_out, uint8_t *handshake_out)
Definition: onion_fast.c:49
int fast_server_handshake(const uint8_t *key_in, uint8_t *handshake_reply_out, uint8_t *key_out, size_t key_out_len)
Definition: onion_fast.c:67
#define tor_free(p)
Definition: malloc.h:52
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:57
Headers for crypto_hkdf.h.
Common functions for cryptographic routines.
#define DIGEST_LEN
Definition: digest_sizes.h:20
Master header file for Tor-specific functionality.
Header file for onion_fast.c.
int fast_client_handshake(const fast_handshake_state_t *handshake_state, const uint8_t *handshake_reply_out, uint8_t *key_out, size_t key_out_len, const char **msg_out)
Definition: onion_fast.c:109
void fast_handshake_state_free_(fast_handshake_state_t *victim)
Definition: onion_fast.c:38