1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use error::{SnowError, StateProblem};
use handshakestate::HandshakeState;
#[cfg(feature = "nightly")] use std::convert::{TryFrom, TryInto};
#[cfg(not(feature = "nightly"))] use utils::{TryFrom, TryInto};
use transportstate::TransportState;
use stateless_transportstate::StatelessTransportState;
/// A state machine for the entire Noise session.
///
/// Enums provide a convenient interface as it's how Rust implements union structs, meaning this is
/// a sized object.
// TODO: check up on memory usage, since this clippy warning seems like a legit perf issue.
#[cfg_attr(feature = "cargo-clippy", allow(large_enum_variant))]
#[derive(Debug)]
pub enum Session {
/// A session in the handshake stage (the starting state).
Handshake(HandshakeState),
/// A session after having completed a handshake, in general-purpose trasport mode.
Transport(TransportState),
/// A session after having completed a handshake, in explicit-nonce trasport mode.
StatelessTransport(StatelessTransportState),
}
impl Session {
/// This method will return `true` if the *previous* write payload was encrypted.
///
/// See [Payload Security Properties](http://noiseprotocol.org/noise.html#payload-security-properties)
/// for more information on the specific properties of your chosen handshake pattern.
///
/// # Examples
///
/// ```rust,ignore
/// let mut session = Builder::new("Noise_NN_25519_AESGCM_SHA256".parse()?)
/// .build_initiator()?;
///
/// // write message...
///
/// assert!(session.was_write_payload_encrypted());
/// ```
pub fn was_write_payload_encrypted(&self) -> bool {
match *self {
Session::Handshake(ref state) => state.was_write_payload_encrypted(),
Session::Transport(_) => true,
Session::StatelessTransport(_) => true,
}
}
/// True if the handshake is finished and the Session state machine is ready to be transitioned
/// to transport mode. This function also returns a vacuous true if already in transport mode.
///
/// # Examples
///
/// ```rust,ignore
/// let mut session = Builder::new("Noise_NN_25519_AESGCM_SHA256".parse()?)
/// .build_initiator()?;
///
/// if (session.is_handshake_finished()) {
/// session = session.into_transport_mode()?;
/// }
/// ```
pub fn is_handshake_finished(&self) -> bool {
match *self {
Session::Handshake(ref state) => state.is_finished(),
Session::Transport(_) => true,
Session::StatelessTransport(_) => true,
}
}
/// Will report if the session has the initiator role (i.e. was built with [`Builder.build_initiator()`]).
///
/// [`Builder.build_initiator()`]: struct.Builder.html#method.build_initiator
pub fn is_initiator(&self) -> bool {
match *self {
Session::Handshake(ref state) => state.is_initiator(),
Session::Transport(ref state) => state.is_initiator(),
Session::StatelessTransport(ref state) => state.is_initiator(),
}
}
/// Construct a message from `payload` (and pending handshake tokens if in handshake state),
/// and writes it to the `output` buffer.
///
/// Returns the size of the written payload.
///
/// # Errors
///
/// Will result in `SnowError::Input` if the size of the output exceeds the max message
/// length in the Noise Protocol (65535 bytes).
#[must_use]
pub fn write_message(&mut self, payload: &[u8], output: &mut [u8]) -> Result<usize, SnowError> {
match *self {
Session::Handshake(ref mut state) => state.write_handshake_message(payload, output),
Session::Transport(ref mut state) => state.write_transport_message(payload, output),
Session::StatelessTransport(_) => bail!(StateProblem::StatelessTransportMode),
}
}
/// Construct a message from `payload` with an explicitly provided nonce and write it to the
/// `output` buffer.
///
/// Returns the size of the written payload.
///
/// # Errors
///
/// Will result in `SnowError::Input` if the size of the output exceeds the max message
/// length in the Noise Protocol (65535 bytes).
///
/// Will result in `SnowError::StateProblem` if not in stateless transport mode.
#[must_use]
pub fn write_message_with_nonce(&self, nonce: u64, payload: &[u8], output: &mut [u8]) -> Result<usize, SnowError> {
match *self {
Session::StatelessTransport(ref state) => state.write_transport_message(nonce, payload, output),
_ => bail!(StateProblem::StatelessTransportMode),
}
}
/// Reads a noise message from `input`
///
/// Returns the size of the payload written to `payload`.
///
/// # Errors
///
/// Will result in `SnowError::Decrypt` if the contents couldn't be decrypted and/or the
/// authentication tag didn't verify.
///
/// # Panics
///
/// This function will panic if there is no key, or if there is a nonce overflow.
#[must_use]
pub fn read_message(&mut self, input: &[u8], payload: &mut [u8]) -> Result<usize, SnowError> {
match *self {
Session::Handshake(ref mut state) => state.read_handshake_message(input, payload),
Session::Transport(ref mut state) => state.read_transport_message(input, payload),
Session::StatelessTransport(_) => bail!(StateProblem::StatelessTransportMode),
}
}
/// Construct a message from `payload` (and pending handshake tokens if in handshake state),
/// and writes it to the `output` buffer.
///
/// Returns the size of the written payload.
///
/// # Errors
///
/// Will result in `SnowError::Input` if the size of the output exceeds the max message
/// length in the Noise Protocol (65535 bytes).
///
/// Will result in `SnowError::StateProblem` if not in stateless transport mode.
#[must_use]
pub fn read_message_with_nonce(&self, nonce: u64, input: &[u8], payload: &mut [u8]) -> Result<usize, SnowError> {
match *self {
Session::StatelessTransport(ref state) => state.read_transport_message(nonce, input, payload),
_ => bail!(StateProblem::StatelessTransportMode),
}
}
/// Generates a new key for the egress symmetric cipher according to Section 4.2
/// of the Noise Specification. Synchronizing timing of rekey between initiator and
/// responder is the responsibility of the application, as described in Section 11.3
/// of the Noise Specification.
///
/// # Errors
///
/// Will result in `SnowError::State` if not in transport mode.
#[must_use]
pub fn rekey_outgoing(&mut self) -> Result<(), SnowError> {
match *self {
Session::Handshake(_) => bail!(StateProblem::HandshakeNotFinished),
Session::Transport(ref mut state) => {
state.rekey_outgoing();
Ok(())
},
Session::StatelessTransport(ref mut state) => {
state.rekey_outgoing();
Ok(())
},
}
}
/// Generates a new key for the ingress symmetric cipher according to Section 4.2
/// of the Noise Specification. Synchronizing timing of rekey between initiator and
/// responder is the responsibility of the application, as described in Section 11.3
/// of the Noise Specification.
///
/// # Errors
///
/// Will result in `SnowError::State` if not in transport mode.
#[must_use]
pub fn rekey_incoming(&mut self) -> Result<(), SnowError> {
match *self {
Session::Handshake(_) => bail!(StateProblem::HandshakeNotFinished),
Session::Transport(ref mut state) => {
state.rekey_incoming();
Ok(())
},
Session::StatelessTransport(ref mut state) => {
state.rekey_incoming();
Ok(())
},
}
}
/// Set a new key for the one or both of the initiator-egress and responder-egress symmetric ciphers.
///
/// # Errors
///
/// Will result in `SnowError::State` if not in transport mode.
#[must_use]
pub fn rekey_manually(&mut self, initiator: Option<&[u8]>, responder: Option<&[u8]>) -> Result<(), SnowError> {
match *self {
Session::Handshake(_) => bail!(StateProblem::HandshakeNotFinished),
Session::Transport(ref mut state) => {
if let Some(key) = initiator {
state.rekey_initiator_manually(key);
}
if let Some(key) = responder {
state.rekey_responder_manually(key);
}
Ok(())
},
Session::StatelessTransport(ref mut state) => {
if let Some(key) = initiator {
state.rekey_initiator_manually(key);
}
if let Some(key) = responder {
state.rekey_responder_manually(key);
}
Ok(())
},
}
}
/// Get the forthcoming inbound nonce value.
///
/// # Errors
///
/// Will result in `SnowError::State` if not in transport mode.
pub fn receiving_nonce(&self) -> Result<u64, SnowError> {
match *self {
Session::Handshake(_) => bail!(StateProblem::HandshakeNotFinished),
Session::Transport(ref state) => Ok(state.receiving_nonce()),
Session::StatelessTransport(_) => bail!(StateProblem::StatelessTransportMode),
}
}
/// Get the forthcoming outbound nonce value.
///
/// # Errors
///
/// Will result in `SnowError::State` if not in transport mode.
pub fn sending_nonce(&self) -> Result<u64, SnowError> {
match *self {
Session::Handshake(_) => bail!(StateProblem::HandshakeNotFinished),
Session::Transport(ref state) => Ok(state.sending_nonce()),
Session::StatelessTransport(_) => bail!(StateProblem::StatelessTransportMode),
}
}
/// Get the remote static key that was possibly encrypted in the first payload.
///
/// Returns a slice of length `Dh.pub_len()` (i.e. DHLEN for the chosen DH function).
pub fn get_remote_static(&self) -> Option<&[u8]> {
match *self {
Session::Handshake(ref state) => state.get_remote_static(),
Session::Transport(ref state) => state.get_remote_static(),
Session::StatelessTransport(ref state) => state.get_remote_static(),
}
}
/// Get the handshake hash.
///
/// Returns a slice of length `Hasher.hash_len()` (i.e. HASHLEN for the chosen Hash function).
pub fn get_handshake_hash(&self) -> Result<&[u8], SnowError> {
match *self {
Session::Handshake(ref state) => Ok(state.get_handshake_hash()),
_ => bail!(StateProblem::HandshakeAlreadyFinished),
}
}
/// Set the preshared key at the specified location. It is up to the caller
/// to correctly set the location based on the specified handshake - Snow
/// won't stop you from placing a PSK in an unused slot.
///
/// # Errors
///
/// Will result in `SnowError::Input` if the PSK is not the right length or the location is out of bounds.
/// Will result in `SnowError::State` if in transport mode.
#[must_use]
pub fn set_psk(&mut self, location: usize, key: &[u8]) -> Result<(), SnowError> {
match *self {
Session::Handshake(ref mut state) => state.set_psk(location, key),
_ => bail!(StateProblem::HandshakeAlreadyFinished)
}
}
/// Transition the session into transport mode. This can only be done once the handshake
/// has finished.
///
/// Consumes the previous state, and returns the new transport state object, thereby freeing
/// any material only used during the handshake phase.
///
/// # Errors
///
/// Will result in `SnowError::State` if the handshake is not finished.
///
/// # Examples
///
/// ```rust,ignore
/// let mut session = Builder::new("Noise_NN_25519_AESGCM_SHA256".parse()?)
/// .build_initiator()?;
///
/// // ... complete handshake ...
///
/// session = session.into_transport_mode()?;
/// ```
///
pub fn into_transport_mode(self) -> Result<Self, SnowError> {
match self {
Session::Handshake(state) => {
if !state.is_finished() {
bail!(StateProblem::HandshakeNotFinished)
} else {
Ok(Session::Transport(state.try_into()?))
}
},
_ => Ok(self)
}
}
/// Transition the session into stateless (explicit nonce) transport mode.
/// This is useful when using Noise over lossy transports.
/// Like `into_transport_mode()`, this can only be done once the handshake has finished.
///
/// Consumes the previous state, and returns the new transport state object, thereby freeing
/// any material only used during the handshake phase.
///
/// # Errors
///
/// Will result in `SnowError::State` if the handshake is not finished.
///
/// # Examples
///
/// ```rust,ignore
/// let mut session = Builder::new("Noise_NN_25519_AESGCM_SHA256".parse()?)
/// .build_initiator()?;
///
/// // ... complete handshake ...
///
/// session = session.into_stateless_transport_mode()?;
/// ```
///
pub fn into_stateless_transport_mode(self) -> Result<Self, SnowError> {
match self {
Session::Handshake(state) => {
if !state.is_finished() {
bail!(StateProblem::HandshakeNotFinished)
} else {
Ok(Session::StatelessTransport(state.try_into()?))
}
},
_ => Ok(self)
}
}
}
impl Into<Session> for HandshakeState {
fn into(self) -> Session {
Session::Handshake(self)
}
}
impl TryFrom<HandshakeState> for TransportState {
type Error = SnowError;
fn try_from(old: HandshakeState) -> Result<Self, Self::Error> {
TransportState::new(old)
}
}
impl TryFrom<HandshakeState> for StatelessTransportState {
type Error = SnowError;
fn try_from(old: HandshakeState) -> Result<Self, Self::Error> {
StatelessTransportState::new(old)
}
}