@@ -25,7 +25,8 @@ pub mod handshake_constants {
2525 /// [`HandshakePattern`] used by the hypercore-protocol crate.
2626 pub const PROTOCOL_PATTERN : HandshakePattern = HandshakePattern :: XX ;
2727 /// 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" ;
2930
3031 /// Get a Noise protocol name from a handshake pattern
3132 pub fn name_from_pattern ( pattern : & HandshakePattern ) -> Result < & ' static str , snow:: Error > {
@@ -52,6 +53,8 @@ const REPLICATE_RESPONDER: [u8; 32] = [
5253] ;
5354
5455#[ 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.
5558pub struct HandshakeResult {
5659 pub ( crate ) is_initiator : bool ,
5760 pub ( crate ) local_pubkey : Vec < u8 > ,
@@ -100,11 +103,13 @@ impl HandshakeResult {
100103 }
101104}
102105
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.
104108#[ derive( Debug ) ]
105109pub struct Handshake {
106110 result : HandshakeResult ,
107- state : HandshakeState ,
111+ /// Internal state of the handshake
112+ pub state : HandshakeState ,
108113 payload : Vec < u8 > ,
109114 tx_buf : Vec < u8 > ,
110115 rx_buf : Vec < u8 > ,
@@ -114,8 +119,9 @@ pub struct Handshake {
114119
115120impl Handshake {
116121 #[ 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) ?;
119125
120126 let payload = vec ! [ ] ;
121127 let result = HandshakeResult {
@@ -134,7 +140,13 @@ impl Handshake {
134140 } )
135141 }
136142
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 > > > {
138150 if self . is_initiator ( ) {
139151 let tx_len = self . send ( ) ?;
140152 Ok ( Some ( self . tx_buf [ ..tx_len] . to_vec ( ) ) )
@@ -151,6 +163,7 @@ impl Handshake {
151163 self . result . is_initiator
152164 }
153165
166+ #[ instrument( skip_all, err) ]
154167 fn recv ( & mut self , msg : & [ u8 ] ) -> Result < usize > {
155168 self . state
156169 . read_message ( msg, & mut self . rx_buf )
@@ -162,8 +175,9 @@ impl Handshake {
162175 . map_err ( map_err)
163176 }
164177
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 > > > {
167181 // eprintln!("hs read len {}", msg.len());
168182 if self . complete ( ) {
169183 return Err ( Error :: new ( ErrorKind :: Other , "Handshake read after finish" ) ) ;
@@ -179,7 +193,9 @@ impl Handshake {
179193 return Ok ( Some ( wrapped) ) ;
180194 }
181195
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+ {
183199 let tx_len = self . send ( ) ?;
184200 let wrapped = self . tx_buf [ ..tx_len] . to_vec ( ) ;
185201 Some ( wrapped)
@@ -189,9 +205,11 @@ impl Handshake {
189205
190206 let split = self . state . dangerously_get_raw_split ( ) ;
191207 if self . is_initiator ( ) {
208+ dbg ! ( ) ;
192209 self . result . split_tx = split. 0 ;
193210 self . result . split_rx = split. 1 ;
194211 } else {
212+ dbg ! ( ) ;
195213 self . result . split_tx = split. 1 ;
196214 self . result . split_rx = split. 0 ;
197215 }
@@ -215,6 +233,7 @@ impl Handshake {
215233}
216234
217235/// Configuration for creating a handshake with specific parameters
236+ // TODO this should take a crate::crypto::Keypair
218237#[ derive( Debug , Clone ) ]
219238pub struct HandshakeConfig {
220239 /// The noise handshake pattern to use (XX, IK, etc.)
@@ -246,13 +265,8 @@ impl Default for HandshakeConfig {
246265 }
247266}
248267
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 (
256270 is_initiator : bool ,
257271 config : & HandshakeConfig ,
258272) -> std:: result:: Result < ( HandshakeState , Vec < u8 > ) , SnowError > {
@@ -292,7 +306,7 @@ fn build_handshake_state_with_config(
292306 }
293307
294308 // 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 {
296310 if let Some ( ref remote_key) = config. remote_public_key {
297311 builder = builder. remote_public_key ( remote_key) ;
298312 } else {
0 commit comments