@@ -25,7 +25,8 @@ pub mod handshake_constants {
25
25
/// [`HandshakePattern`] used by the hypercore-protocol crate.
26
26
pub const PROTOCOL_PATTERN : HandshakePattern = HandshakePattern :: XX ;
27
27
/// Noise protocol name used in hypercore-protocol crate.
28
- pub const PROTOCOL_NAME : & str = "Noise_XX_Ed25519_ChaChaPoly_BLAKE2b" ;
28
+ pub const PROTOCOL_NAME : & str =
29
+ "NNoise_XX_Ed25519_ChaChaPoly_BLAKE2boise_XX_Ed25519_ChaChaPoly_BLAKE2b" ;
29
30
30
31
/// Get a Noise protocol name from a handshake pattern
31
32
pub fn name_from_pattern ( pattern : & HandshakePattern ) -> Result < & ' static str , snow:: Error > {
@@ -52,6 +53,8 @@ const REPLICATE_RESPONDER: [u8; 32] = [
52
53
] ;
53
54
54
55
#[ derive( Debug , Clone , Default ) ]
56
+ /// The thing created by [`Handshake`] which is used to encrypt and decrypt. NB while it is created
57
+ /// at the beginning of the handshake, it is not "ready" until the handshake completes.
55
58
pub struct HandshakeResult {
56
59
pub ( crate ) is_initiator : bool ,
57
60
pub ( crate ) local_pubkey : Vec < u8 > ,
@@ -100,11 +103,13 @@ impl HandshakeResult {
100
103
}
101
104
}
102
105
103
- /// Noise handshake for establishing secure connections
106
+ /// Object for holding the data needed Noise handshake protocol. The resulting object can be used
107
+ /// for encryption.
104
108
#[ derive( Debug ) ]
105
109
pub struct Handshake {
106
110
result : HandshakeResult ,
107
- state : HandshakeState ,
111
+ /// Internal state of the handshake
112
+ pub state : HandshakeState ,
108
113
payload : Vec < u8 > ,
109
114
tx_buf : Vec < u8 > ,
110
115
rx_buf : Vec < u8 > ,
@@ -114,8 +119,9 @@ pub struct Handshake {
114
119
115
120
impl Handshake {
116
121
#[ instrument]
117
- pub ( crate ) fn new ( is_initiator : bool ) -> Result < Self > {
118
- let ( state, local_pubkey) = build_handshake_state ( is_initiator) . map_err ( map_err) ?;
122
+ /// Build a [`Handshake`]
123
+ pub fn new ( is_initiator : bool , config : & HandshakeConfig ) -> Result < Self > {
124
+ let ( state, local_pubkey) = new_handshake_state ( is_initiator, config) . map_err ( map_err) ?;
119
125
120
126
let payload = vec ! [ ] ;
121
127
let result = HandshakeResult {
@@ -134,7 +140,13 @@ impl Handshake {
134
140
} )
135
141
}
136
142
137
- pub ( crate ) fn start_raw ( & mut self ) -> Result < Option < Vec < u8 > > > {
143
+ /// Set the payload for the next handshake message
144
+ pub fn set_payload ( & mut self , payload : Vec < u8 > ) {
145
+ self . payload = payload;
146
+ }
147
+
148
+ /// Start the handshake and return the initial message (for initiators)
149
+ pub fn start_raw ( & mut self ) -> Result < Option < Vec < u8 > > > {
138
150
if self . is_initiator ( ) {
139
151
let tx_len = self . send ( ) ?;
140
152
Ok ( Some ( self . tx_buf [ ..tx_len] . to_vec ( ) ) )
@@ -151,6 +163,7 @@ impl Handshake {
151
163
self . result . is_initiator
152
164
}
153
165
166
+ #[ instrument( skip_all, err) ]
154
167
fn recv ( & mut self , msg : & [ u8 ] ) -> Result < usize > {
155
168
self . state
156
169
. read_message ( msg, & mut self . rx_buf )
@@ -162,8 +175,9 @@ impl Handshake {
162
175
. map_err ( map_err)
163
176
}
164
177
165
- #[ instrument( skip_all, fields( is_initiator = %self . result. is_initiator) ) ]
166
- pub ( crate ) fn read_raw ( & mut self , msg : & [ u8 ] ) -> Result < Option < Vec < u8 > > > {
178
+ #[ instrument( skip_all, fields( is_initiator = %self . result. is_initiator) , err) ]
179
+ /// Read in a handshake message
180
+ pub fn read_raw ( & mut self , msg : & [ u8 ] ) -> Result < Option < Vec < u8 > > > {
167
181
// eprintln!("hs read len {}", msg.len());
168
182
if self . complete ( ) {
169
183
return Err ( Error :: new ( ErrorKind :: Other , "Handshake read after finish" ) ) ;
@@ -179,7 +193,9 @@ impl Handshake {
179
193
return Ok ( Some ( wrapped) ) ;
180
194
}
181
195
182
- let tx_buf = if self . is_initiator ( ) {
196
+ let tx_buf = if self . is_initiator ( ) && !self . state . is_handshake_finished ( )
197
+ /* when IK pattern */
198
+ {
183
199
let tx_len = self . send ( ) ?;
184
200
let wrapped = self . tx_buf [ ..tx_len] . to_vec ( ) ;
185
201
Some ( wrapped)
@@ -189,9 +205,11 @@ impl Handshake {
189
205
190
206
let split = self . state . dangerously_get_raw_split ( ) ;
191
207
if self . is_initiator ( ) {
208
+ dbg ! ( ) ;
192
209
self . result . split_tx = split. 0 ;
193
210
self . result . split_rx = split. 1 ;
194
211
} else {
212
+ dbg ! ( ) ;
195
213
self . result . split_tx = split. 1 ;
196
214
self . result . split_rx = split. 0 ;
197
215
}
@@ -215,6 +233,7 @@ impl Handshake {
215
233
}
216
234
217
235
/// Configuration for creating a handshake with specific parameters
236
+ // TODO this should take a crate::crypto::Keypair
218
237
#[ derive( Debug , Clone ) ]
219
238
pub struct HandshakeConfig {
220
239
/// The noise handshake pattern to use (XX, IK, etc.)
@@ -246,13 +265,8 @@ impl Default for HandshakeConfig {
246
265
}
247
266
}
248
267
249
- fn build_handshake_state (
250
- is_initiator : bool ,
251
- ) -> std:: result:: Result < ( HandshakeState , Vec < u8 > ) , SnowError > {
252
- build_handshake_state_with_config ( is_initiator, & HandshakeConfig :: default ( ) )
253
- }
254
-
255
- fn build_handshake_state_with_config (
268
+ // TODO make this infallible. It's currently
269
+ fn new_handshake_state (
256
270
is_initiator : bool ,
257
271
config : & HandshakeConfig ,
258
272
) -> std:: result:: Result < ( HandshakeState , Vec < u8 > ) , SnowError > {
@@ -292,7 +306,7 @@ fn build_handshake_state_with_config(
292
306
}
293
307
294
308
// Set remote public key for IK pattern initiator
295
- if is_initiator && config. pattern == handshake_constants:: PROTOCOL_PATTERN {
309
+ if ( is_initiator) && ( config. pattern ) == handshake_constants:: DHT_PATTERN {
296
310
if let Some ( ref remote_key) = config. remote_public_key {
297
311
builder = builder. remote_public_key ( remote_key) ;
298
312
} else {
0 commit comments