@@ -9,8 +9,8 @@ use bitcoin_hashes::sha256::Hash as Sha256;
9
9
use secp256k1:: { PublicKey , SecretKey } ;
10
10
11
11
use ln:: peers:: { chacha, hkdf} ;
12
- use ln:: peers:: conduit:: Conduit ;
13
- use ln:: peers:: handshake:: acts:: { ActOne , ActThree , ActTwo } ;
12
+ use ln:: peers:: conduit:: { Conduit , SymmetricKey } ;
13
+ use ln:: peers:: handshake:: acts:: { ActOne , ActThree , ActTwo , ACT_ONE_LENGTH , ACT_TWO_LENGTH , ACT_THREE_LENGTH } ;
14
14
use ln:: peers:: handshake:: hash:: HandshakeHash ;
15
15
use ln:: peers:: handshake:: states:: { ActOneExpectation , ActThreeExpectation , ActTwoExpectation , HandshakeState } ;
16
16
@@ -33,7 +33,7 @@ impl PeerHandshake {
33
33
/// Instantiate a new handshake with a node identity secret key and an ephemeral private key
34
34
pub fn new ( private_key : & SecretKey , ephemeral_private_key : & SecretKey ) -> Self {
35
35
Self {
36
- state : Some ( HandshakeState :: Blank ) ,
36
+ state : Some ( HandshakeState :: Uninitiated ) ,
37
37
private_key : ( * private_key) . clone ( ) ,
38
38
ephemeral_private_key : ( * ephemeral_private_key) . clone ( ) ,
39
39
read_buffer : Vec :: new ( ) ,
@@ -68,7 +68,15 @@ impl PeerHandshake {
68
68
}
69
69
70
70
/// Process act dynamically
71
- /// The role must be set before this method can be called
71
+ /// # Arguments
72
+ /// `input`: Byte slice received from peer as part of the handshake protocol
73
+ /// `remote_public_key`: If outbound, the peer's static identity public key needs is required for the handshake initiation
74
+ ///
75
+ /// # Return values
76
+ /// Returns a tuple with the following components:
77
+ /// `.0`: Byte vector containing the next act to send back to the peer per the handshake protocol
78
+ /// `.1`: Conduit option if the handshake was just processed to completion and messages can now be encrypted and decrypted
79
+ /// `.2`: Public key option if the handshake was inbound and the peer's static identity pubkey was just learned
72
80
pub fn process_act ( & mut self , input : & [ u8 ] , remote_public_key : Option < & PublicKey > ) -> Result < ( Vec < u8 > , Option < Conduit > , Option < PublicKey > ) , String > {
73
81
let mut response: Vec < u8 > = Vec :: new ( ) ;
74
82
let mut connected_peer = None ;
@@ -78,7 +86,7 @@ impl PeerHandshake {
78
86
let read_buffer_length = self . read_buffer . len ( ) ;
79
87
80
88
match & self . state {
81
- & Some ( HandshakeState :: Blank ) => {
89
+ & Some ( HandshakeState :: Uninitiated ) => {
82
90
let remote_public_key = remote_public_key. ok_or ( "remote_public_key must be Some for outbound connections" ) ?;
83
91
let act_one = self . initiate ( & remote_public_key) ?;
84
92
response = act_one. 0 . to_vec ( ) ;
@@ -89,22 +97,21 @@ impl PeerHandshake {
89
97
return Err ( "need at least 50 bytes" . to_string ( ) ) ;
90
98
}
91
99
92
- let mut act_one_buffer = [ 0u8 ; 50 ] ;
93
- act_one_buffer. copy_from_slice ( & self . read_buffer [ ..act_length ] ) ;
94
- self . read_buffer . drain ( ..act_length ) ;
100
+ let mut act_one_buffer = [ 0u8 ; ACT_ONE_LENGTH ] ;
101
+ act_one_buffer. copy_from_slice ( & self . read_buffer [ ..ACT_ONE_LENGTH ] ) ;
102
+ self . read_buffer . drain ( ..ACT_ONE_LENGTH ) ;
95
103
96
104
let act_two = self . process_act_one ( ActOne ( act_one_buffer) ) ?;
97
105
response = act_two. 0 . to_vec ( ) ;
98
106
}
99
107
& Some ( HandshakeState :: AwaitingActTwo ( _) ) => {
100
- let act_length = 50 ;
101
- if read_buffer_length < act_length {
108
+ if read_buffer_length < ACT_TWO_LENGTH {
102
109
return Err ( "need at least 50 bytes" . to_string ( ) ) ;
103
110
}
104
111
105
- let mut act_two_buffer = [ 0u8 ; 50 ] ;
106
- act_two_buffer. copy_from_slice ( & self . read_buffer [ ..act_length ] ) ;
107
- self . read_buffer . drain ( ..act_length ) ;
112
+ let mut act_two_buffer = [ 0u8 ; ACT_TWO_LENGTH ] ;
113
+ act_two_buffer. copy_from_slice ( & self . read_buffer [ ..ACT_TWO_LENGTH ] ) ;
114
+ self . read_buffer . drain ( ..ACT_TWO_LENGTH ) ;
108
115
109
116
let ( act_three, mut conduit) = self . process_act_two ( ActTwo ( act_two_buffer) ) ?;
110
117
@@ -117,14 +124,13 @@ impl PeerHandshake {
117
124
connected_peer = Some ( conduit) ;
118
125
}
119
126
& Some ( HandshakeState :: AwaitingActThree ( _) ) => {
120
- let act_length = 66 ;
121
- if read_buffer_length < act_length {
122
- return Err ( "need at least 50 bytes" . to_string ( ) ) ;
127
+ if read_buffer_length < ACT_THREE_LENGTH {
128
+ return Err ( "need at least 66 bytes" . to_string ( ) ) ;
123
129
}
124
130
125
- let mut act_three_buffer = [ 0u8 ; 66 ] ;
126
- act_three_buffer. copy_from_slice ( & self . read_buffer [ ..act_length ] ) ;
127
- self . read_buffer . drain ( ..act_length ) ;
131
+ let mut act_three_buffer = [ 0u8 ; ACT_THREE_LENGTH ] ;
132
+ act_three_buffer. copy_from_slice ( & self . read_buffer [ ..ACT_THREE_LENGTH ] ) ;
133
+ self . read_buffer . drain ( ..ACT_THREE_LENGTH ) ;
128
134
129
135
let ( public_key, mut conduit) = self . process_act_three ( ActThree ( act_three_buffer) ) ?;
130
136
@@ -145,7 +151,7 @@ impl PeerHandshake {
145
151
146
152
/// Initiate the handshake with a peer and return the first act
147
153
pub fn initiate ( & mut self , remote_public_key : & PublicKey ) -> Result < ActOne , String > {
148
- if let & Some ( HandshakeState :: Blank ) = & self . state { } else {
154
+ if let & Some ( HandshakeState :: Uninitiated ) = & self . state { } else {
149
155
return Err ( "incorrect state" . to_string ( ) ) ;
150
156
}
151
157
@@ -174,9 +180,8 @@ impl PeerHandshake {
174
180
let state = self . state . take ( ) ;
175
181
let act_one_expectation = match state {
176
182
Some ( HandshakeState :: AwaitingActOne ( act_state) ) => act_state,
177
- Some ( HandshakeState :: Blank ) => {
183
+ Some ( HandshakeState :: Uninitiated ) => {
178
184
// this can also be initiated from a blank state
179
- // public key
180
185
let public_key = Self :: private_key_to_public_key ( & self . private_key ) ;
181
186
let ( hash, chaining_key) = Self :: initialize_state ( & public_key) ;
182
187
ActOneExpectation {
@@ -315,7 +320,7 @@ impl PeerHandshake {
315
320
Ok ( ( remote_pubkey, connected_peer) )
316
321
}
317
322
318
- fn calculate_act_message ( local_private_key : & SecretKey , remote_public_key : & PublicKey , chaining_key : [ u8 ; 32 ] , hash : & mut HandshakeHash ) -> ( [ u8 ; 50 ] , [ u8 ; 32 ] , [ u8 ; 32 ] ) {
323
+ fn calculate_act_message ( local_private_key : & SecretKey , remote_public_key : & PublicKey , chaining_key : [ u8 ; 32 ] , hash : & mut HandshakeHash ) -> ( [ u8 ; 50 ] , SymmetricKey , SymmetricKey ) {
319
324
let local_public_key = Self :: private_key_to_public_key ( local_private_key) ;
320
325
321
326
hash. update ( & local_public_key. serialize ( ) ) ;
@@ -333,7 +338,7 @@ impl PeerHandshake {
333
338
( act, chaining_key, temporary_key)
334
339
}
335
340
336
- fn process_act_message ( act_bytes : [ u8 ; 50 ] , local_private_key : & SecretKey , chaining_key : [ u8 ; 32 ] , hash : & mut HandshakeHash ) -> Result < ( PublicKey , [ u8 ; 32 ] , [ u8 ; 32 ] ) , String > {
341
+ fn process_act_message ( act_bytes : [ u8 ; 50 ] , local_private_key : & SecretKey , chaining_key : [ u8 ; 32 ] , hash : & mut HandshakeHash ) -> Result < ( PublicKey , SymmetricKey , SymmetricKey ) , String > {
337
342
let version = act_bytes[ 0 ] ;
338
343
if version != 0 {
339
344
return Err ( "unexpected version" . to_string ( ) ) ;
@@ -358,7 +363,7 @@ impl PeerHandshake {
358
363
// HKDF(chaining key, ECDH) -> chaining key' + next temporary key
359
364
let ( chaining_key, temporary_key) = hkdf:: derive ( & chaining_key, & ecdh) ;
360
365
361
- // Validate chacha tag (temporary key, 0, self. hash, chacha_tag)
366
+ // Validate chacha tag (temporary key, 0, hash, chacha_tag)
362
367
let _tag_check = chacha:: decrypt ( & temporary_key, 0 , & hash. value , & chacha_tag) ?;
363
368
364
369
hash. update ( & chacha_tag) ;
@@ -372,7 +377,7 @@ impl PeerHandshake {
372
377
pk_object
373
378
}
374
379
375
- fn ecdh ( private_key : & SecretKey , public_key : & PublicKey ) -> [ u8 ; 32 ] {
380
+ fn ecdh ( private_key : & SecretKey , public_key : & PublicKey ) -> SymmetricKey {
376
381
let curve = secp256k1:: Secp256k1 :: new ( ) ;
377
382
let mut pk_object = public_key. clone ( ) ;
378
383
pk_object. mul_assign ( & curve, & private_key[ ..] ) . expect ( "invalid multiplication" ) ;
0 commit comments