@@ -3,6 +3,7 @@ use blake2::{
3
3
digest:: { typenum:: U32 , FixedOutput , Update } ,
4
4
Blake2bMac ,
5
5
} ;
6
+ use handshake_patterns:: name_from_pattern;
6
7
use snow:: {
7
8
params:: HandshakePattern ,
8
9
resolvers:: { DefaultResolver , FallbackResolver } ,
@@ -16,6 +17,7 @@ const CIPHERKEYLEN: usize = 32;
16
17
/// The [`HandshakePattern`]s we support for connections
17
18
pub mod handshake_patterns {
18
19
use snow:: params:: HandshakePattern ;
20
+ use tracing:: error;
19
21
/// [`HandshakePattern`] used by the hyperdht crate.
20
22
pub const DHT : HandshakePattern = HandshakePattern :: IK ;
21
23
/// Noise protocol name used in hyperdht crate.
@@ -24,6 +26,18 @@ pub mod handshake_patterns {
24
26
pub const PROTOCOL : HandshakePattern = HandshakePattern :: XX ;
25
27
/// Noise protocol name used in hypercore-protocol crate.
26
28
pub const PROTOCOL_NAME : & str = "Noise_XX_Ed25519_ChaChaPoly_BLAKE2b" ;
29
+
30
+ /// Get a Noise protocol name from a handshake pattern
31
+ pub fn name_from_pattern ( pattern : & HandshakePattern ) -> Result < & ' static str , snow:: Error > {
32
+ Ok ( match * pattern {
33
+ DHT => DHT_NAME ,
34
+ PROTOCOL => PROTOCOL_NAME ,
35
+ unsupported_pattern => {
36
+ error ! ( "Got an unsupported handshake pattern {unsupported_pattern:?}" ) ;
37
+ return Err ( snow:: Error :: Input ) ;
38
+ }
39
+ } )
40
+ }
27
41
}
28
42
29
43
// These the output of, see `hash_namespace` test below for how they are produced
@@ -200,6 +214,38 @@ impl Handshake {
200
214
}
201
215
}
202
216
217
+ /// Configuration for creating a handshake with specific parameters
218
+ #[ derive( Debug , Clone ) ]
219
+ pub struct HandshakeConfig {
220
+ /// The noise handshake pattern to use (XX, IK, etc.)
221
+ pub pattern : HandshakePattern ,
222
+ /// Optional prologue data to include in the handshake
223
+ pub prologue : Option < Vec < u8 > > ,
224
+ /// Remote public key (required for IK pattern initiator)
225
+ // TODO replace with an actual key type
226
+ pub remote_public_key : Option < [ u8 ; 32 ] > ,
227
+ }
228
+
229
+ impl HandshakeConfig {
230
+ fn new (
231
+ pattern : HandshakePattern ,
232
+ prologue : Option < Vec < u8 > > ,
233
+ remote_public_key : Option < [ u8 ; 32 ] > ,
234
+ ) -> Self {
235
+ Self {
236
+ pattern,
237
+ prologue,
238
+ remote_public_key,
239
+ }
240
+ }
241
+ }
242
+
243
+ impl Default for HandshakeConfig {
244
+ fn default ( ) -> Self {
245
+ Self :: new ( HandshakePattern :: XX , None , None )
246
+ }
247
+ }
248
+
203
249
fn build_handshake_state (
204
250
is_initiator : bool ,
205
251
) -> std:: result:: Result < ( HandshakeState , Vec < u8 > ) , SnowError > {
@@ -215,14 +261,10 @@ fn build_handshake_state_with_config(
215
261
NoiseParams ,
216
262
} ;
217
263
218
- let pattern_str = match config. pattern {
219
- HandshakePattern :: XX => "Noise_XX_Ed25519_ChaChaPoly_BLAKE2b" ,
220
- HandshakePattern :: IK => "Noise_IK_Ed25519_ChaChaPoly_BLAKE2b" ,
221
- _ => return Err ( SnowError :: Input ) ,
222
- } ;
264
+ let hs_name = name_from_pattern ( & config. pattern ) ?;
223
265
224
266
let noise_params = NoiseParams :: new (
225
- pattern_str . to_string ( ) ,
267
+ hs_name . to_string ( ) ,
226
268
BaseChoice :: Noise ,
227
269
HandshakeChoice {
228
270
pattern : config. pattern ,
0 commit comments