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
//! Noise Protocol Framework API
//!
//! This module is a wrapper for noise functionality provided by snow to build
//! and use secure communication channels between revault infrastructure machines.
//!

use crate::error::NoiseError;

use std::convert::TryInto;

use snow::{resolvers::SodiumResolver, Builder, HandshakeState, TransportState};

/// The static public key used to enact Noise authenticated and encrypted channels
pub use sodiumoxide::crypto::box_::curve25519xsalsa20poly1305::PublicKey;
/// The static secret key used to enact Noise authenticated and encrypted channels
pub use sodiumoxide::crypto::box_::curve25519xsalsa20poly1305::SecretKey;

/// The size of a key, either public or private, on the Curve25519
pub const KEY_SIZE: usize = 32;
/// Size of the poly1305 MAC
pub const MAC_SIZE: usize = 16;
/// Max message size pecified by Noise Protocol Framework
pub const NOISE_MESSAGE_MAX_SIZE: usize = 65535;
/// Two bytes are used for the message length prefix
pub const LENGTH_PREFIX_SIZE: usize = 2;
/// Message header length plus its MAC
pub const NOISE_MESSAGE_HEADER_SIZE: usize = LENGTH_PREFIX_SIZE + MAC_SIZE;
/// Maximum size of a message before being encrypted; limited by Noise Protocol Framework
pub const NOISE_PLAINTEXT_MAX_SIZE: usize =
    NOISE_MESSAGE_MAX_SIZE - NOISE_MESSAGE_HEADER_SIZE - MAC_SIZE;
/// e, es, ss
pub const KK_MSG_1_SIZE: usize = KEY_SIZE + HANDSHAKE_MESSAGE.len() + MAC_SIZE;
/// e, ee, se
pub const KK_MSG_2_SIZE: usize = KEY_SIZE + MAC_SIZE;
/// Sent for versioning and identification during handshake
pub const HANDSHAKE_MESSAGE: &[u8] = b"practical_revault_0";

/// First round of the KK handshake
#[derive(Debug)]
pub struct KKHandshakeActOne {
    state: HandshakeState,
}

/// Message sent during the first round of the KK handshake (e, es, ss)
pub struct KKMessageActOne(pub(crate) [u8; KK_MSG_1_SIZE]);

impl KKHandshakeActOne {
    /// Start the first act of the handshake as an initiator (sharing e, es, ss)
    pub fn initiator(
        my_privkey: &SecretKey,
        their_pubkey: &PublicKey,
    ) -> Result<(KKHandshakeActOne, KKMessageActOne), NoiseError> {
        // Build the initial initiator state
        let builder = Builder::with_resolver(
            "Noise_KK_25519_ChaChaPoly_SHA256"
                .parse()
                .expect("Valid params"),
            Box::new(SodiumResolver::default()),
        );
        let mut state = builder
            .local_private_key(&my_privkey.0)
            .remote_public_key(&their_pubkey.0)
            .build_initiator()?;

        // Write the first message
        let mut msg = [0u8; KK_MSG_1_SIZE];
        state.write_message(HANDSHAKE_MESSAGE, &mut msg)?;

        Ok((KKHandshakeActOne { state }, KKMessageActOne(msg)))
    }

    /// Start the first act of the handshake as a responder (reading e, es, ss and doing wizardry with it)
    pub fn responder(
        my_privkey: &SecretKey,
        their_possible_pubkeys: &[PublicKey],
        message: &KKMessageActOne,
    ) -> Result<KKHandshakeActOne, NoiseError> {
        // TODO: estimate how inefficient it is.
        for their_pubkey in their_possible_pubkeys {
            // Build the initial responder state
            let builder = Builder::with_resolver(
                "Noise_KK_25519_ChaChaPoly_SHA256"
                    .parse()
                    .expect("Valid params"),
                Box::new(SodiumResolver::default()),
            );
            let mut state = builder
                .local_private_key(&my_privkey.0)
                .remote_public_key(&their_pubkey.0)
                .build_responder()?;

            let mut msg = [0u8; KK_MSG_1_SIZE];
            if state.read_message(&message.0, &mut msg).is_err() {
                continue;
            }
            if &msg[..HANDSHAKE_MESSAGE.len()] != HANDSHAKE_MESSAGE {
                return Err(NoiseError::BadHandshake);
            }

            return Ok(KKHandshakeActOne { state });
        }

        Err(NoiseError::MissingStaticKey)
    }
}

/// Final round of the KK handshake
#[derive(Debug)]
pub struct KKHandshakeActTwo {
    /// Inner snow Noise KK handshake state
    state: HandshakeState,
}

/// Content of the message from the final round of the KK handshake (e, ee, se)
pub struct KKMessageActTwo(pub(crate) [u8; KK_MSG_2_SIZE]);

impl KKHandshakeActTwo {
    /// Start the second act of the handshake as a responder (read e, ee, se)
    pub fn initiator(
        mut handshake: KKHandshakeActOne,
        message: &KKMessageActTwo,
    ) -> Result<KKHandshakeActTwo, NoiseError> {
        // In handshake mode we don't actually care about the message
        let mut _m = [0u8; KK_MSG_2_SIZE];
        handshake.state.read_message(&message.0, &mut _m)?;

        Ok(KKHandshakeActTwo {
            state: handshake.state,
        })
    }

    /// Start the second act of the handshake as a responder (write e, ee, se)
    pub fn responder(
        mut handshake: KKHandshakeActOne,
    ) -> Result<(KKHandshakeActTwo, KKMessageActTwo), NoiseError> {
        let mut msg = [0u8; KK_MSG_2_SIZE];
        handshake.state.write_message(&[], &mut msg)?;

        Ok((
            KKHandshakeActTwo {
                state: handshake.state,
            },
            KKMessageActTwo(msg),
        ))
    }
}

/// A cypertext encrypted with [encrypt_message] containing the length prefix of
/// a plaintext message.
#[derive(Debug, Clone, Copy)]
pub struct NoiseEncryptedHeader(pub [u8; LENGTH_PREFIX_SIZE + MAC_SIZE]);

/// A cyphertext encrypted with [encrypt_message] containing the body of a Noise
/// message.
#[derive(Debug)]
pub struct NoiseEncryptedMessage(pub Vec<u8>);

/// A wrapper over Snow's transport state for a KK Noise communication channel.
#[derive(Debug)]
pub struct KKChannel {
    transport_state: TransportState,
}

fn encrypted_msg_size(plaintext_size: usize) -> usize {
    // Length prefix + MAC    ||   Message + MAC
    NOISE_MESSAGE_HEADER_SIZE + plaintext_size + MAC_SIZE
}

impl KKChannel {
    /// Constructs the KK Noise channel from a final stage KK handshake
    pub fn from_handshake(state: KKHandshakeActTwo) -> Result<KKChannel, NoiseError> {
        let transport_state = state.state.into_transport_mode()?;

        Ok(KKChannel { transport_state })
    }

    /// Use the channel to encrypt a message shorter than [NOISE_PLAINTEXT_MAX_SIZE].
    /// Pre-fixes the message with a 2-bytes big-endian length field MAC'ed on its own to permit
    /// incremental reads.
    /// On success, returns the ciphertext.
    pub fn encrypt_message(&mut self, message: &[u8]) -> Result<NoiseEncryptedMessage, NoiseError> {
        if message.len() > NOISE_PLAINTEXT_MAX_SIZE {
            return Err(NoiseError::TooLargePlaintext(message.len()));
        }
        let mut output = vec![0u8; encrypted_msg_size(message.len())];

        let message_len: u16 = (MAC_SIZE + message.len())
            .try_into()
            .expect("We just checked it was < NOISE_PLAINTEXT_MAX_SIZE");
        let prefix = message_len.to_be_bytes().to_vec();
        self.transport_state
            .write_message(&prefix, &mut output[..NOISE_MESSAGE_HEADER_SIZE])?;

        self.transport_state
            .write_message(&message, &mut output[NOISE_MESSAGE_HEADER_SIZE..])?;

        Ok(NoiseEncryptedMessage(output))
    }

    /// Get the size of the message following this header
    pub fn decrypt_header(&mut self, header: &NoiseEncryptedHeader) -> Result<u16, NoiseError> {
        let mut buf = [0u8; NOISE_MESSAGE_HEADER_SIZE];
        self.transport_state.read_message(&header.0, &mut buf)?;

        let len_be: [u8; 2] = buf[..NOISE_MESSAGE_HEADER_SIZE - MAC_SIZE]
            .try_into()
            .expect("NOISE_MESSAGE_HEADER_SIZE - MAC_SIZE == LENGTH_PREFIX_SIZE");
        Ok(u16::from_be_bytes(len_be))
    }

    /// Get plaintext bytes from a Noise-encrypted message
    pub fn decrypt_message(
        &mut self,
        message: &NoiseEncryptedMessage,
    ) -> Result<Vec<u8>, NoiseError> {
        // TODO: could be in NoiseEncryptedMessage's constructor?
        if message.0.len() < MAC_SIZE || message.0.len() > NOISE_MESSAGE_MAX_SIZE {
            return Err(NoiseError::InvalidCiphertextSize(message.0.len()));
        }
        let mut plaintext = vec![0u8; message.0.len()];

        self.transport_state
            .read_message(&message.0, &mut plaintext)?;

        // We read the MAC, but caller doesn't care about it
        // FIXME: add a test for invalid MAC getting refused
        plaintext.truncate(plaintext.len() - MAC_SIZE);
        Ok(plaintext)
    }

    /// Get the static public key of the peer
    pub fn remote_static(&self) -> PublicKey {
        PublicKey(
            self.transport_state
                .get_remote_static()
                .expect(
                    "We could not have settled the KK channel without their key. \
                     And if we could, better to crash now!",
                )
                .try_into()
                .expect("Our keys aren't 32 bytes anymore?"),
        )
    }
}

#[cfg(test)]
pub mod tests {
    use crate::noise::{
        KKChannel, KKHandshakeActOne, KKHandshakeActTwo, KKMessageActOne, KKMessageActTwo,
        NoiseEncryptedHeader, NoiseEncryptedMessage, KK_MSG_1_SIZE, KK_MSG_2_SIZE, MAC_SIZE,
        NOISE_MESSAGE_HEADER_SIZE, NOISE_MESSAGE_MAX_SIZE, NOISE_PLAINTEXT_MAX_SIZE,
    };
    use sodiumoxide::crypto::box_::curve25519xsalsa20poly1305::gen_keypair;
    use std::convert::TryInto;

    #[test]
    fn test_bidirectional_roundtrip() {
        let (initiator_pubkey, initiator_privkey) = gen_keypair();
        let (responder_pubkey, responder_privkey) = gen_keypair();

        // client
        let (cli_act_1, msg_1) =
            KKHandshakeActOne::initiator(&initiator_privkey, &responder_pubkey).unwrap();

        // server
        let serv_act_1 =
            KKHandshakeActOne::responder(&responder_privkey, &[initiator_pubkey], &msg_1).unwrap();
        let (serv_act_2, msg_2) = KKHandshakeActTwo::responder(serv_act_1).unwrap();
        let mut server_channel = KKChannel::from_handshake(serv_act_2).unwrap();

        // client
        let cli_act_2 = KKHandshakeActTwo::initiator(cli_act_1, &msg_2).unwrap();
        let mut client_channel = KKChannel::from_handshake(cli_act_2).unwrap();

        // client encrypts message for server
        let msg = "Hello".as_bytes();
        let encrypted_msg = client_channel.encrypt_message(&msg).unwrap();
        assert_eq!(
            encrypted_msg.0.len(),
            msg.len() + NOISE_MESSAGE_HEADER_SIZE + MAC_SIZE
        );
        let (header, body) = (
            &encrypted_msg.0[..NOISE_MESSAGE_HEADER_SIZE],
            &encrypted_msg.0[NOISE_MESSAGE_HEADER_SIZE..],
        );
        eprintln!("{:x?}", header);
        let msg_len = server_channel
            .decrypt_header(&NoiseEncryptedHeader(header.try_into().unwrap()))
            .unwrap();
        assert_eq!(msg_len as usize, msg.len() + MAC_SIZE);
        let decrypted_msg = server_channel
            .decrypt_message(&NoiseEncryptedMessage(body.to_vec()))
            .unwrap();
        assert_eq!(msg.to_vec(), decrypted_msg);

        // server encrypts message for client
        let msg = "Goodbye".as_bytes();
        let encrypted_msg = server_channel.encrypt_message(&msg).unwrap();
        let (header, body) = (
            &encrypted_msg.0[..NOISE_MESSAGE_HEADER_SIZE],
            &encrypted_msg.0[NOISE_MESSAGE_HEADER_SIZE..],
        );
        let msg_len = client_channel
            .decrypt_header(&NoiseEncryptedHeader(header.try_into().unwrap()))
            .unwrap();
        assert_eq!(msg_len as usize, msg.len() + MAC_SIZE);
        let decrypted_msg = client_channel
            .decrypt_message(&NoiseEncryptedMessage(body.to_vec()))
            .unwrap();
        assert_eq!(msg.to_vec(), decrypted_msg);
    }

    #[test]
    fn test_message_size_limit() {
        let (initiator_pubkey, initiator_privkey) = gen_keypair();
        let (responder_pubkey, responder_privkey) = gen_keypair();

        // client
        let (_, msg_1) =
            KKHandshakeActOne::initiator(&initiator_privkey, &responder_pubkey).unwrap();

        // server
        let serv_act_1 =
            KKHandshakeActOne::responder(&responder_privkey, &[initiator_pubkey], &msg_1).unwrap();
        let (serv_act_2, _msg_2) = KKHandshakeActTwo::responder(serv_act_1).unwrap();
        let mut server_channel = KKChannel::from_handshake(serv_act_2).unwrap();

        // Hit the limit
        let msg = [0u8; NOISE_PLAINTEXT_MAX_SIZE];
        server_channel
            .encrypt_message(&msg)
            .expect("Maximum allowed");

        // Fail if msg too large
        let msg = [0u8; NOISE_MESSAGE_MAX_SIZE - NOISE_MESSAGE_HEADER_SIZE + 1];
        server_channel
            .encrypt_message(&msg)
            .expect_err("Limit exceeded");

        // We can encrypt an empty message
        let msg = b"";
        server_channel
            .encrypt_message(msg)
            .expect("Empty message is fine to encrypt");

        // We cannot decrypt an empty message
        server_channel
            .decrypt_message(&NoiseEncryptedMessage(msg.to_vec()))
            .expect_err("Encrypted message with no header");
    }

    #[test]
    fn test_bad_messages() {
        let (initiator_pubkey, initiator_privkey) = gen_keypair();
        let (responder_pubkey, responder_privkey) = gen_keypair();

        // KK handshake fails if messages are badly formed.
        // Without a valid cli_act_2 nor serv_act_2, no KKChannel can be constructed.
        let (cli_act_1, _) = KKHandshakeActOne::initiator(&initiator_privkey, &responder_pubkey)
            .expect("The first act is valid.");

        let bad_msg = KKMessageActOne([1u8; KK_MSG_1_SIZE]);
        KKHandshakeActOne::responder(&responder_privkey, &[initiator_pubkey], &bad_msg)
            .expect_err("This one is invalid as bad_msg cannot be decrypted.");

        let bad_msg = KKMessageActTwo([1u8; KK_MSG_2_SIZE]);
        KKHandshakeActTwo::initiator(cli_act_1, &bad_msg).expect_err("So is this one.");
    }
}