@@ -48,12 +48,9 @@ impl<Step> SecStream<Step> {
48
48
}
49
49
}
50
50
51
- /// Initial initiator state
51
+ /// Initiator
52
52
#[ 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 > {
57
54
_res_step : PhantomData < Step > ,
58
55
}
59
56
@@ -63,11 +60,17 @@ pub struct InitiatorInitialSent<Step> {
63
60
/// emitting the next message. This distinction is necessary so we can handle the received payload
64
61
/// and send a new one
65
62
#[ derive( Debug ) ]
66
- pub struct Responder < RespStep > {
67
- _res_step : PhantomData < RespStep > ,
63
+ pub struct Responder < Step > {
64
+ _res_step : PhantomData < Step > ,
68
65
}
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 ;
71
74
72
75
/// No decryptor yet
73
76
pub struct EncryptorReady {
@@ -99,7 +102,7 @@ impl Debug for Ready {
99
102
}
100
103
}
101
104
102
- impl SecStream < InitiatorInitial > {
105
+ impl SecStream < Initiator < Start > > {
103
106
/// Create an initiator of a secret stream
104
107
pub fn new_initiator ( config : InitiatorConfig ) -> Result < Self , Error > {
105
108
let params: NoiseParams = PARAMS . parse ( ) . expect ( "known to work" ) ;
@@ -113,15 +116,17 @@ impl SecStream<InitiatorInitial> {
113
116
is_initiator : true ,
114
117
state,
115
118
msg_buf : [ 0 ; 1024 ] ,
116
- step : InitiatorInitial ,
119
+ step : Initiator {
120
+ _res_step : PhantomData ,
121
+ } ,
117
122
} )
118
123
}
119
124
120
125
/// Create the first message the initiator sends to the responder
121
- pub fn make_first_msg (
126
+ pub fn write_msg (
122
127
mut self ,
123
128
prologue : & [ u8 ] ,
124
- ) -> Result < ( SecStream < InitiatorInitialSent < One > > , Vec < u8 > ) , Error > {
129
+ ) -> Result < ( SecStream < Initiator < HsMsgSent > > , Vec < u8 > ) , Error > {
125
130
let len = self . state . write_message ( prologue, & mut self . msg_buf ) ?;
126
131
let msg = self . msg_buf [ ..len] . to_vec ( ) ;
127
132
let Self {
@@ -135,7 +140,7 @@ impl SecStream<InitiatorInitial> {
135
140
is_initiator,
136
141
state,
137
142
msg_buf,
138
- step : InitiatorInitialSent {
143
+ step : Initiator {
139
144
_res_step : PhantomData ,
140
145
} ,
141
146
} ,
@@ -144,7 +149,7 @@ impl SecStream<InitiatorInitial> {
144
149
}
145
150
}
146
151
147
- impl SecStream < Responder < One > > {
152
+ impl SecStream < Responder < Start > > {
148
153
/// Create a responder of a secret stream
149
154
pub fn new_responder ( private : & [ u8 ] ) -> Result < Self , Error > {
150
155
let params: NoiseParams = PARAMS . parse ( ) . expect ( "known to work" ) ;
@@ -162,10 +167,10 @@ impl SecStream<Responder<One>> {
162
167
}
163
168
164
169
/// Read msg and return it's payload
165
- pub fn read_first_msg_get_payload (
170
+ pub fn read_msg (
166
171
mut self ,
167
172
msg : & [ u8 ] ,
168
- ) -> Result < ( SecStream < Responder < Two > > , Vec < u8 > ) , Error > {
173
+ ) -> Result < ( SecStream < Responder < HsDone > > , Vec < u8 > ) , Error > {
169
174
let len = self . state . read_message ( msg, & mut self . msg_buf ) ?;
170
175
let payload = & self . msg_buf [ ..len] ;
171
176
let Self {
@@ -187,19 +192,19 @@ impl SecStream<Responder<One>> {
187
192
) )
188
193
}
189
194
/// 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 (
191
196
self ,
192
197
msg : & [ u8 ] ,
193
198
) -> 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 ( & [ ] )
196
201
}
197
202
}
198
203
199
- impl SecStream < Responder < Two > > {
204
+ impl SecStream < Responder < HsDone > > {
200
205
/// Make second message with the given payload. Returns two messages, the first completes the
201
206
/// 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 (
203
208
mut self ,
204
209
payload : & [ u8 ] ,
205
210
) -> Result < ( SecStream < EncryptorReady > , [ Vec < u8 > ; 2 ] ) , Error > {
@@ -244,12 +249,12 @@ impl SecStream<Responder<Two>> {
244
249
}
245
250
}
246
251
247
- impl SecStream < InitiatorInitialSent < One > > {
252
+ impl SecStream < Initiator < HsMsgSent > > {
248
253
/// Recieve the last message to complet the handsake
249
- pub fn read_first_msg_get_payload (
254
+ pub fn read_msg (
250
255
mut self ,
251
256
msg : & [ u8 ] ,
252
- ) -> Result < ( SecStream < InitiatorInitialSent < Two > > , Vec < u8 > ) , Error > {
257
+ ) -> Result < ( SecStream < Initiator < HsDone > > , Vec < u8 > ) , Error > {
253
258
let len = self . state . read_message ( msg, & mut self . msg_buf ) ?;
254
259
let payload = & self . msg_buf [ ..len] ;
255
260
let Self {
@@ -263,25 +268,27 @@ impl SecStream<InitiatorInitialSent<One>> {
263
268
is_initiator,
264
269
state,
265
270
msg_buf,
266
- step : InitiatorInitialSent {
271
+ step : Initiator {
267
272
_res_step : PhantomData ,
268
273
} ,
269
274
} ,
270
275
payload. to_vec ( ) ,
271
276
) )
272
277
}
273
278
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 ,
276
283
msg : & [ u8 ] ,
277
284
) -> 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 ( )
280
287
}
281
288
}
282
289
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 > {
285
292
let ( tx, rx) = self . split_handshake ( ) ;
286
293
let key: [ u8 ; SNOW_CIPHERKEYLEN ] = tx[ ..SNOW_CIPHERKEYLEN ]
287
294
. try_into ( )
@@ -324,7 +331,7 @@ impl SecStream<InitiatorInitialSent<Two>> {
324
331
325
332
impl SecStream < EncryptorReady > {
326
333
/// 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 > {
328
335
let Self {
329
336
is_initiator,
330
337
step :
@@ -377,31 +384,36 @@ mod test {
377
384
378
385
#[ tokio:: test]
379
386
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
381
388
let params: NoiseParams = PARAMS . parse ( ) . expect ( "known to work" ) ;
382
389
let kp = Builder :: new ( params. clone ( ) ) . generate_keypair ( ) ?;
383
390
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 ) ?;
386
394
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" ) ?;
389
398
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) ?;
392
401
assert_eq ! ( payload, b"hello" ) ;
393
402
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
395
405
let ( resp, [ msg1, msg2] ) : ( SecStream < EncryptorReady > , [ Vec < u8 > ; 2 ] ) =
396
- resp. make_second_msg ( payload2 ) ?;
406
+ resp. write_msg ( b"goodbye" ) ?;
397
407
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) ?;
399
410
assert_eq ! ( payload_recv, b"goodbye" ) ;
400
411
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 ( ) ?;
402
414
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) ?;
405
417
406
418
let hello = b"hello" . to_vec ( ) ;
407
419
let mut msg = hello. clone ( ) ;
0 commit comments