@@ -48,12 +48,9 @@ impl<Step> SecStream<Step> {
4848 }
4949}
5050
51- /// Initial initiator state
51+ /// Initiator
5252#[ derive( Debug ) ]
53- pub struct InitiatorInitial ;
54- /// Initiator has sent the first message
55- #[ derive( Debug ) ]
56- pub struct InitiatorInitialSent < Step > {
53+ pub struct Initiator < Step > {
5754 _res_step : PhantomData < Step > ,
5855}
5956
@@ -63,11 +60,17 @@ pub struct InitiatorInitialSent<Step> {
6360/// emitting the next message. This distinction is necessary so we can handle the received payload
6461/// and send a new one
6562#[ derive( Debug ) ]
66- pub struct Responder < RespStep > {
67- _res_step : PhantomData < RespStep > ,
63+ pub struct Responder < Step > {
64+ _res_step : PhantomData < Step > ,
6865}
69- struct One ;
70- struct Two ;
66+ /// The first step. We must send or receive a handshake message to proceed.
67+ struct Start ;
68+ /// The handshake message has been sent. We must receive a handshake message to proceed to
69+ /// [`HsDone`]. Only on [`Initiator`].
70+ struct HsMsgSent ;
71+ /// [`snow::HandshakeState::is_handshake_finished`] is `true`.
72+ /// We are ready create a [`PushStream`] and proeed to [`EncryptorReady`].
73+ struct HsDone ;
7174
7275/// No decryptor yet
7376pub struct EncryptorReady {
@@ -99,7 +102,7 @@ impl Debug for Ready {
99102 }
100103}
101104
102- impl SecStream < InitiatorInitial > {
105+ impl SecStream < Initiator < Start > > {
103106 /// Create an initiator of a secret stream
104107 pub fn new_initiator ( config : InitiatorConfig ) -> Result < Self , Error > {
105108 let params: NoiseParams = PARAMS . parse ( ) . expect ( "known to work" ) ;
@@ -113,15 +116,17 @@ impl SecStream<InitiatorInitial> {
113116 is_initiator : true ,
114117 state,
115118 msg_buf : [ 0 ; 1024 ] ,
116- step : InitiatorInitial ,
119+ step : Initiator {
120+ _res_step : PhantomData ,
121+ } ,
117122 } )
118123 }
119124
120125 /// Create the first message the initiator sends to the responder
121- pub fn make_first_msg (
126+ pub fn write_msg (
122127 mut self ,
123128 prologue : & [ u8 ] ,
124- ) -> Result < ( SecStream < InitiatorInitialSent < One > > , Vec < u8 > ) , Error > {
129+ ) -> Result < ( SecStream < Initiator < HsMsgSent > > , Vec < u8 > ) , Error > {
125130 let len = self . state . write_message ( prologue, & mut self . msg_buf ) ?;
126131 let msg = self . msg_buf [ ..len] . to_vec ( ) ;
127132 let Self {
@@ -135,7 +140,7 @@ impl SecStream<InitiatorInitial> {
135140 is_initiator,
136141 state,
137142 msg_buf,
138- step : InitiatorInitialSent {
143+ step : Initiator {
139144 _res_step : PhantomData ,
140145 } ,
141146 } ,
@@ -144,7 +149,7 @@ impl SecStream<InitiatorInitial> {
144149 }
145150}
146151
147- impl SecStream < Responder < One > > {
152+ impl SecStream < Responder < Start > > {
148153 /// Create a responder of a secret stream
149154 pub fn new_responder ( private : & [ u8 ] ) -> Result < Self , Error > {
150155 let params: NoiseParams = PARAMS . parse ( ) . expect ( "known to work" ) ;
@@ -162,10 +167,10 @@ impl SecStream<Responder<One>> {
162167 }
163168
164169 /// Read msg and return it's payload
165- pub fn read_first_msg_get_payload (
170+ pub fn read_msg (
166171 mut self ,
167172 msg : & [ u8 ] ,
168- ) -> Result < ( SecStream < Responder < Two > > , Vec < u8 > ) , Error > {
173+ ) -> Result < ( SecStream < Responder < HsDone > > , Vec < u8 > ) , Error > {
169174 let len = self . state . read_message ( msg, & mut self . msg_buf ) ?;
170175 let payload = & self . msg_buf [ ..len] ;
171176 let Self {
@@ -187,19 +192,19 @@ impl SecStream<Responder<One>> {
187192 ) )
188193 }
189194 /// Read the first message of the protocol, create the next two messages to send to the initiator.
190- pub fn read_first_msg (
195+ pub fn read_and_write_msg (
191196 self ,
192197 msg : & [ u8 ] ,
193198 ) -> Result < ( SecStream < EncryptorReady > , [ Vec < u8 > ; 2 ] ) , Error > {
194- let ( self2, _rx_payload) = self . read_first_msg_get_payload ( msg) ?;
195- self2. make_second_msg ( & [ ] )
199+ let ( self2, _rx_payload) = self . read_msg ( msg) ?;
200+ self2. write_msg ( & [ ] )
196201 }
197202}
198203
199- impl SecStream < Responder < Two > > {
204+ impl SecStream < Responder < HsDone > > {
200205 /// Make second message with the given payload. Returns two messages, the first completes the
201206 /// Noise handshake. The second has the shared key for the remote to set up a Decryptor.
202- pub fn make_second_msg (
207+ pub fn write_msg (
203208 mut self ,
204209 payload : & [ u8 ] ,
205210 ) -> Result < ( SecStream < EncryptorReady > , [ Vec < u8 > ; 2 ] ) , Error > {
@@ -244,12 +249,12 @@ impl SecStream<Responder<Two>> {
244249 }
245250}
246251
247- impl SecStream < InitiatorInitialSent < One > > {
252+ impl SecStream < Initiator < HsMsgSent > > {
248253 /// Recieve the last message to complet the handsake
249- pub fn read_first_msg_get_payload (
254+ pub fn read_msg (
250255 mut self ,
251256 msg : & [ u8 ] ,
252- ) -> Result < ( SecStream < InitiatorInitialSent < Two > > , Vec < u8 > ) , Error > {
257+ ) -> Result < ( SecStream < Initiator < HsDone > > , Vec < u8 > ) , Error > {
253258 let len = self . state . read_message ( msg, & mut self . msg_buf ) ?;
254259 let payload = & self . msg_buf [ ..len] ;
255260 let Self {
@@ -263,25 +268,27 @@ impl SecStream<InitiatorInitialSent<One>> {
263268 is_initiator,
264269 state,
265270 msg_buf,
266- step : InitiatorInitialSent {
271+ step : Initiator {
267272 _res_step : PhantomData ,
268273 } ,
269274 } ,
270275 payload. to_vec ( ) ,
271276 ) )
272277 }
273278
274- pub fn receive_msg (
275- mut self ,
279+ /// read in a message, and write the next message. Any payload in the recieved message is
280+ /// dropped.
281+ pub fn read_and_write_msg (
282+ self ,
276283 msg : & [ u8 ] ,
277284 ) -> Result < ( SecStream < EncryptorReady > , Vec < u8 > ) , Error > {
278- let ( mut self2, _payload) = self . read_first_msg_get_payload ( msg) ?;
279- self2. make_msg ( )
285+ let ( self2, _payload) = self . read_msg ( msg) ?;
286+ self2. write_msg ( )
280287 }
281288}
282289
283- impl SecStream < InitiatorInitialSent < Two > > {
284- fn make_msg ( mut self ) -> Result < ( SecStream < EncryptorReady > , Vec < u8 > ) , Error > {
290+ impl SecStream < Initiator < HsDone > > {
291+ fn write_msg ( mut self ) -> Result < ( SecStream < EncryptorReady > , Vec < u8 > ) , Error > {
285292 let ( tx, rx) = self . split_handshake ( ) ;
286293 let key: [ u8 ; SNOW_CIPHERKEYLEN ] = tx[ ..SNOW_CIPHERKEYLEN ]
287294 . try_into ( )
@@ -324,7 +331,7 @@ impl SecStream<InitiatorInitialSent<Two>> {
324331
325332impl SecStream < EncryptorReady > {
326333 /// Recieve message the last message, used to set up the decryption stream
327- pub fn receive_msg ( self , msg : & [ u8 ] ) -> Result < SecStream < Ready > , Error > {
334+ pub fn read_msg ( self , msg : & [ u8 ] ) -> Result < SecStream < Ready > , Error > {
328335 let Self {
329336 is_initiator,
330337 step :
@@ -377,31 +384,36 @@ mod test {
377384
378385 #[ tokio:: test]
379386 async fn set_up_secret_steram ( ) -> std:: result:: Result < ( ) , Box < dyn std:: error:: Error > > {
380- /// Excessive typing to demonstrate flow through typestates
387+ // Excessive typing to demonstrate flow through typestates
381388 let params: NoiseParams = PARAMS . parse ( ) . expect ( "known to work" ) ;
382389 let kp = Builder :: new ( params. clone ( ) ) . generate_keypair ( ) ?;
383390 let config = InitiatorConfig :: new ( kp. public . try_into ( ) . unwrap ( ) ) ;
384- let init: SecStream < InitiatorInitial > = SecStream :: new_initiator ( config) ?;
385- let resp: SecStream < Responder < One > > = SecStream :: new_responder ( & kp. private ) ?;
391+ // Create an initiator and responder
392+ let init: SecStream < Initiator < Start > > = SecStream :: new_initiator ( config) ?;
393+ let resp: SecStream < Responder < Start > > = SecStream :: new_responder ( & kp. private ) ?;
386394
387- let ( init, msg) : ( SecStream < InitiatorInitialSent < One > > , Vec < u8 > ) =
388- init. make_first_msg ( b"hello" ) ?;
395+ // initiator sends the first handshake message, a payload can be included to send extra data to the
396+ // responder.
397+ let ( init, msg) : ( SecStream < Initiator < HsMsgSent > > , Vec < u8 > ) = init. write_msg ( b"hello" ) ?;
389398
390- let ( resp , payload ) : ( SecStream < Responder < Two > > , Vec < u8 > ) =
391- resp. read_first_msg_get_payload ( & msg) ?;
399+ // responder receives the hs message, extracts the payload
400+ let ( resp , payload ) : ( SecStream < Responder < HsDone > > , Vec < u8 > ) = resp. read_msg ( & msg) ?;
392401 assert_eq ! ( payload, b"hello" ) ;
393402
394- let payload2 = b"goodbye" ;
403+ // responder sends a handshake message, which can include a payload. As well as a second
404+ // message which contains the symmetric key needed to set up the decryptor
395405 let ( resp, [ msg1, msg2] ) : ( SecStream < EncryptorReady > , [ Vec < u8 > ; 2 ] ) =
396- resp. make_second_msg ( payload2 ) ?;
406+ resp. write_msg ( b"goodbye" ) ?;
397407
398- let ( init, payload_recv) = init. read_first_msg_get_payload ( & msg1) ?;
408+ // Initiator receives last handshake message, use handshake to create the extract payload.
409+ let ( init, payload_recv) : ( SecStream < Initiator < HsDone > > , Vec < u8 > ) = init. read_msg ( & msg1) ?;
399410 assert_eq ! ( payload_recv, b"goodbye" ) ;
400411
401- let ( init, to_resp) : ( SecStream < EncryptorReady > , Vec < u8 > ) = init. make_msg ( ) ?;
412+ // receive decryptor keey
413+ let ( init, to_resp) : ( SecStream < EncryptorReady > , Vec < u8 > ) = init. write_msg ( ) ?;
402414
403- let mut init: SecStream < Ready > = init. receive_msg ( & msg2) ?;
404- let mut resp: SecStream < Ready > = resp. receive_msg ( & to_resp) ?;
415+ let mut init: SecStream < Ready > = init. read_msg ( & msg2) ?;
416+ let mut resp: SecStream < Ready > = resp. read_msg ( & to_resp) ?;
405417
406418 let hello = b"hello" . to_vec ( ) ;
407419 let mut msg = hello. clone ( ) ;
0 commit comments