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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
extern crate log;

use crate::handshake::HandshakeComplete;

use core::{cmp, mem};
use sodiumoxide::crypto::{auth, hash::sha256, scalarmult::curve25519, secretbox};
use std::{convert, io};

use thiserror::Error;

/// Max length of encrypted body (with MAC detached)
pub const MSG_BODY_MAX_LEN: usize = 4096;
/// Length of decrypted header (body_len || enc_body_mac)
pub const MSG_HEADER_DEC_LEN: usize = 18;
/// Length of encrypted header (with MAC prefixed)
pub const MSG_HEADER_LEN: usize = MSG_HEADER_DEC_LEN + secretbox::MACBYTES;

/// The error type for boxstream operations.  Errors originate from decryption errors.
#[derive(Error, Debug, PartialEq)]
pub enum Error {
    #[error("secretbox::open failed in header decryption")]
    DecryptHeaderSecretbox,
    #[error("secretbox::open failed in body decryption")]
    DecryptBodySecretbox,
    #[error("received goodbye message")]
    GoodbyeReceived,
    #[error("sent goodbye message")]
    GoodbyeSent,
}

impl convert::From<Error> for io::Error {
    fn from(error: Error) -> Self {
        match error {
            Error::DecryptHeaderSecretbox => Self::new(io::ErrorKind::InvalidData, error),
            Error::DecryptBodySecretbox => Self::new(io::ErrorKind::InvalidData, error),
            Error::GoodbyeReceived => Self::new(io::ErrorKind::Other, error),
            Error::GoodbyeSent => Self::new(io::ErrorKind::Other, error),
        }
    }
}

/// The result type for boxstream operations.
pub type Result<T> = std::result::Result<T, Error>;

/// A pair of key and nonce used for encryption/decryption of every message in the boxstream.
#[derive(Debug, PartialEq)]
pub struct KeyNonce {
    key: secretbox::Key,
    nonce: secretbox::Nonce,
}

impl KeyNonce {
    /// Creates a new `KeyNonce` from a key and a nonce.
    pub fn new(key: secretbox::Key, nonce: secretbox::Nonce) -> Self {
        Self { key, nonce }
    }
    /// Derives the sender and receiver `KeyNonce` from a `HandshakeComplete`.  Returns
    /// `(key_nonce_send, key_nonce_recv)`.
    pub fn from_handshake(handshake_complete: HandshakeComplete) -> (Self, Self) {
        let HandshakeComplete {
            net_id,
            pk,
            ephemeral_pk,
            peer_pk,
            peer_ephemeral_pk,
            shared_secret,
        } = handshake_complete;
        let shared_secret_0 = sha256::hash(&concat!(
            auth::KEYBYTES + curve25519::GROUPELEMENTBYTES * 3,
            net_id.as_ref(),
            shared_secret.ab.as_ref(),
            shared_secret.aB.as_ref(),
            shared_secret.Ab.as_ref()
        ));
        let shared_secret_1 = sha256::hash(shared_secret_0.as_ref());
        let send_hmac_nonce = auth::authenticate(peer_ephemeral_pk.as_ref(), &net_id);
        let key_nonce_send = KeyNonce {
            key: secretbox::Key(
                sha256::hash(&[shared_secret_1.as_ref(), peer_pk.as_ref()].concat()).0,
            ),
            nonce: secretbox::Nonce::from_slice(&send_hmac_nonce.as_ref()[..secretbox::NONCEBYTES])
                .unwrap(),
        };
        let recv_hmac_nonce = auth::authenticate(ephemeral_pk.as_ref(), &net_id);
        let key_nonce_recv = KeyNonce {
            key: secretbox::Key(sha256::hash(&[shared_secret_1.as_ref(), pk.as_ref()].concat()).0),
            nonce: secretbox::Nonce::from_slice(&recv_hmac_nonce.as_ref()[..secretbox::NONCEBYTES])
                .unwrap(),
        };
        (key_nonce_send, key_nonce_recv)
    }

    /// Increments the nonce value by one.  The nonce value is encoded/decoded in big endian,
    /// following the boxstream spec.
    pub fn increment_nonce_be_inplace(&mut self) {
        for n in (0..self.nonce.0.len()).rev() {
            let (inc, _) = self.nonce.0[n].overflowing_add(1);
            self.nonce.0[n] = inc;
            if self.nonce.0[n] != 0 {
                break;
            }
        }
    }
}

/// The header of a message in the boxstream.
#[derive(Debug, PartialEq)]
pub struct Header {
    body_len: usize,
    body_mac: secretbox::Tag,
}

impl Header {
    /// Decodes `MSG_HEADER_DEC_LEN` length byte array to build a `Header`.
    pub fn from_bytes(buf: &[u8; MSG_HEADER_DEC_LEN]) -> Self {
        Self {
            body_len: u16::from_be_bytes([buf[0], buf[1]]) as usize,
            body_mac: secretbox::Tag::from_slice(&buf[2..]).unwrap(),
        }
    }

    /// Decodes `MSG_HEADER_DEC_LEN` bytes from a slice to build a `Header`.
    pub fn from_slice(buf: &[u8]) -> Option<Self> {
        if buf.len() != MSG_HEADER_DEC_LEN {
            return None;
        }
        Some(Self {
            body_len: u16::from_be_bytes([buf[0], buf[1]]) as usize,
            body_mac: secretbox::Tag::from_slice(&buf[2..MSG_HEADER_DEC_LEN]).unwrap(),
        })
    }

    /// Encodes the `Header` to a byte array.
    pub fn to_bytes(&self) -> [u8; MSG_HEADER_DEC_LEN] {
        concat!(
            MSG_HEADER_DEC_LEN,
            (self.body_len as u16).to_be_bytes().as_ref(),
            self.body_mac.as_ref()
        )
    }
}

/// Encrypt the final goodbye message into `enc`.  Returns the number of bytes written into `enc`.
///
/// Note that the nonce is not incremented since this **must** be the last nonce used.
fn encrypt_box_stream_goodbye(key_nonce: &mut KeyNonce, enc: &mut [u8]) -> usize {
    let (goodbye_tag_buf, mut goodbye_header_buf) =
        enc[..MSG_HEADER_LEN].split_at_mut(secretbox::MACBYTES);
    goodbye_header_buf.iter_mut().for_each(|x| *x = 0);

    let goodbye_tag =
        secretbox::seal_detached(&mut goodbye_header_buf, &key_nonce.nonce, &key_nonce.key);
    goodbye_tag_buf.copy_from_slice(goodbye_tag.as_ref());
    MSG_HEADER_LEN
}

/// Encrypt a single message from `buf` into `enc`.  Return the number of bytes read
/// from `buf` and the number of bytes written into `enc`.
fn encrypt_box_stream_msg(key_nonce: &mut KeyNonce, buf: &[u8], enc: &mut [u8]) -> (usize, usize) {
    let body = &buf[..cmp::min(buf.len(), MSG_BODY_MAX_LEN)];

    let header_nonce = key_nonce.nonce;
    key_nonce.increment_nonce_be_inplace();
    let body_nonce = key_nonce.nonce;
    key_nonce.increment_nonce_be_inplace();

    let (header_buf, mut body_buf) =
        enc[..MSG_HEADER_LEN + body.len()].split_at_mut(MSG_HEADER_LEN);
    let (header_tag_buf, mut header_body_buf) = header_buf.split_at_mut(secretbox::MACBYTES);
    body_buf.copy_from_slice(body);
    let body_tag = secretbox::seal_detached(&mut body_buf, &body_nonce, &key_nonce.key);
    let header = Header {
        body_len: body.len(),
        body_mac: body_tag,
    };
    header_body_buf.copy_from_slice(&header.to_bytes());
    let header_tag = secretbox::seal_detached(&mut header_body_buf, &header_nonce, &key_nonce.key);
    header_tag_buf.copy_from_slice(header_tag.as_ref());
    (body.len(), MSG_HEADER_LEN + body.len())
}

/// The transport agnostic boxstream sending side.
pub struct BoxStreamSend {
    key_nonce: KeyNonce,
    goodbye: bool,
}

impl BoxStreamSend {
    /// Create a new `BoxStreamSend` from the `key_nonce`.
    pub fn new(key_nonce: KeyNonce) -> Self {
        Self {
            key_nonce,
            goodbye: false,
        }
    }
    /// Encrypt a single boxstream message by taking bytes from `buf` and encrypting them into
    /// `enc`.  Returns the number of bytes read from `buf` and the number of bytes written into
    /// `enc`.
    pub fn encrypt(&mut self, buf: &[u8], mut enc: &mut [u8]) -> Result<(usize, usize)> {
        if self.goodbye {
            return Err(Error::GoodbyeSent);
        }
        if buf.is_empty() {
            Ok((0, 0))
        } else {
            Ok(encrypt_box_stream_msg(&mut self.key_nonce, buf, &mut enc))
        }
    }
    /// Encrypt a goodbye message into `enc`.  Returns the number of bytes written into `enc`.
    pub fn encrypt_goodbye(&mut self, enc: &mut [u8]) -> Result<usize> {
        if self.goodbye {
            return Err(Error::GoodbyeSent);
        }
        self.goodbye = true;
        Ok(encrypt_box_stream_goodbye(&mut self.key_nonce, enc))
    }

    /// Returns whether the goodbye message has been sent or not.
    pub fn goodbye_sent(&self) -> bool {
        self.goodbye
    }
}

/// Result of a successful decryption within a boxstream.
#[derive(Debug, PartialEq)]
pub enum Decrypted<T> {
    Some(T),
    Goodbye,
}

impl<T> Decrypted<T> {
    /// Moves the value v out of the Decrypted<T> if it is Some(v).  Panics if Decrypted<T> is
    /// Goodbye.
    pub fn unwrap(self) -> T {
        match self {
            Decrypted::Some(val) => val,
            Decrypted::Goodbye => panic!("called `Decrypted::unwrap()` on a `Goodbye` value"),
        }
    }
}

/// Decrypt and decode a boxstream `Header` from `buf`.
fn decrypt_box_stream_header(
    key_nonce: &mut KeyNonce,
    buf: &mut [u8],
) -> Result<Decrypted<Header>> {
    let (header_tag_buf, mut header_body_buf) =
        buf[..MSG_HEADER_LEN].split_at_mut(secretbox::MACBYTES);
    match secretbox::open_detached(
        &mut header_body_buf,
        &secretbox::Tag::from_slice(header_tag_buf).unwrap(),
        &key_nonce.nonce,
        &key_nonce.key,
    ) {
        Ok(()) => {
            key_nonce.increment_nonce_be_inplace();
            if header_body_buf.iter().all(|&b| b == 0) {
                Ok(Decrypted::Goodbye)
            } else {
                Ok(Decrypted::Some(
                    Header::from_slice(header_body_buf).unwrap(),
                ))
            }
        }
        Err(()) => Err(Error::DecryptHeaderSecretbox),
    }
}

/// Decrypt the body of a boxstream message from `buf` inplace.  Returns the number of bytes of the
/// decrypted body in `buf`.
fn decrypt_box_stream_body(
    header: &Header,
    key_nonce: &mut KeyNonce,
    buf: &mut [u8],
) -> Result<usize> {
    let mut body_buf = &mut buf[..header.body_len];
    match secretbox::open_detached(
        &mut body_buf,
        &header.body_mac,
        &key_nonce.nonce,
        &key_nonce.key,
    ) {
        Ok(()) => {
            key_nonce.increment_nonce_be_inplace();
            Ok(header.body_len)
        }
        Err(()) => Err(Error::DecryptBodySecretbox),
    }
}

/// The state of a boxstream receiver.  It can be expecting a header or the body of a previously
/// received header.
#[derive(Debug)]
enum RecvState {
    ExpectHeader,
    ExpectBody(Header),
}

/// The transport agnostic boxstream receiving side.
pub struct BoxStreamRecv {
    key_nonce: KeyNonce,
    state: RecvState,
    goodbye: bool,
}

impl BoxStreamRecv {
    /// Create a new `BoxStreamRecv` from the `key_nonce`.
    pub fn new(key_nonce: KeyNonce) -> Self {
        Self {
            key_nonce,
            state: RecvState::ExpectHeader,
            goodbye: false,
        }
    }

    /// Decrypt a single boxstream message every two calls (one to decrypt and parse the header, the
    /// other do decrypt the body) by decrypting from `buf` and writting the plaintex into `dec`.
    /// Returns the number of bytes read from `buf` and the number of bytes written into `dec`.  If
    /// a goodbye message is received, an Err(Error::Goodbye) will be returned and the following
    /// calls will return Ok((0, 0)).
    pub fn decrypt(&mut self, buf: &[u8], dec: &mut [u8]) -> Result<Decrypted<(usize, usize)>> {
        if self.goodbye {
            return Err(Error::GoodbyeReceived);
        }
        let n = self.recv_bytes();
        let mut state = RecvState::ExpectHeader;
        mem::swap(&mut state, &mut self.state);
        match state {
            RecvState::ExpectHeader => {
                dec[..n].copy_from_slice(&buf[..n]);
                let decrypted = decrypt_box_stream_header(&mut self.key_nonce, &mut dec[..n])?;
                let header = match decrypted {
                    Decrypted::Goodbye => {
                        self.goodbye = true;
                        return Ok(Decrypted::Goodbye);
                    }
                    Decrypted::Some(header) => header,
                };
                self.state = RecvState::ExpectBody(header);
                Ok(Decrypted::Some((n, 0)))
            }
            RecvState::ExpectBody(header) => {
                dec[..n].copy_from_slice(&buf[..n]);
                assert_eq!(
                    n,
                    decrypt_box_stream_body(&header, &mut self.key_nonce, &mut dec[..n],)?
                );
                self.state = RecvState::ExpectHeader;
                Ok(Decrypted::Some((n, n)))
            }
        }
    }

    /// Returns the number of received bytes needed for the next `decrypt` call.
    pub fn recv_bytes(&self) -> usize {
        if !self.goodbye {
            match &self.state {
                RecvState::ExpectHeader => MSG_HEADER_LEN,
                RecvState::ExpectBody(header) => header.body_len,
            }
        } else {
            0
        }
    }

    /// Returns whether the goodbye message has been received or not.
    pub fn goodbye_recvd(&self) -> bool {
        self.goodbye
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const KEY_A_HEX: &str = "8198e2d3456f022b2020f36ce874ad8b337a1c2da13f69f6458fd63415a51943";
    const NONCE_A_HEX: &str = "a20fa8fe59a80f5f07c80265e5e7664582f0f553f36cd6ce";
    const KEY_B_HEX: &str = "9bf1ec7af3f80934474e5ff73e27f2f5070f4fe4d80511923b7acb686463bfcc";
    const NONCE_B_HEX: &str = "799762378d9e1d0a8a510a249dc4e76788d6ff9993efc5df";

    #[test]
    fn test_header() {
        const HEADER_HEX: &str = "123400000000000000000000000000000000";
        let header_slice = &hex::decode(HEADER_HEX).unwrap();
        let mut header_array = [0; MSG_HEADER_DEC_LEN];
        header_array[..].copy_from_slice(header_slice);

        let header_from_slice = Header::from_slice(header_slice).unwrap();
        let header_from_array = Header::from_bytes(&header_array);

        assert_eq!(header_from_slice, header_from_array);
        assert_eq!(header_from_slice.body_len, 0x1234);
    }

    #[test]
    fn test_boxstream_error() {
        let test_error = |error| {
            format!("{}", error);
            io::Error::from(error);
        };
        test_error(Error::DecryptBodySecretbox);
        test_error(Error::DecryptHeaderSecretbox);
    }

    #[test]
    fn test_keynonce() {
        const KEY_HEX: &str = "8198e2d3456f022b2020f36ce874ad8b337a1c2da13f69f6458fd63415a51943";
        const NONCE0_HEX: &str = "00000000000000000000000000000000000000000a00ffff";
        const NONCE1_HEX: &str = "00000000000000000000000000000000000000000a010000";
        let key = secretbox::Key::from_slice(&hex::decode(KEY_HEX).unwrap()).unwrap();
        let nonce0 = secretbox::Nonce::from_slice(&hex::decode(NONCE0_HEX).unwrap()).unwrap();
        let nonce1 = secretbox::Nonce::from_slice(&hex::decode(NONCE1_HEX).unwrap()).unwrap();
        let mut key_nonce = KeyNonce { key, nonce: nonce0 };
        key_nonce.increment_nonce_be_inplace();
        assert_eq!(key_nonce.nonce, nonce1);
    }

    struct Peer {
        key_nonce_send: KeyNonce,
        key_nonce_recv: KeyNonce,
    }

    fn load_peers() -> (Peer, Peer) {
        let key_a = secretbox::Key::from_slice(&hex::decode(KEY_A_HEX).unwrap()).unwrap();
        let nonce_a = secretbox::Nonce::from_slice(&hex::decode(NONCE_A_HEX).unwrap()).unwrap();
        let key_b = secretbox::Key::from_slice(&hex::decode(KEY_B_HEX).unwrap()).unwrap();
        let nonce_b = secretbox::Nonce::from_slice(&hex::decode(NONCE_B_HEX).unwrap()).unwrap();

        let peer_a = Peer {
            key_nonce_send: KeyNonce {
                key: key_a.clone(),
                nonce: nonce_a,
            },
            key_nonce_recv: KeyNonce {
                key: key_b.clone(),
                nonce: nonce_b,
            },
        };
        let peer_b = Peer {
            key_nonce_send: KeyNonce {
                key: key_b.clone(),
                nonce: nonce_b,
            },
            key_nonce_recv: KeyNonce {
                key: key_a.clone(),
                nonce: nonce_a,
            },
        };
        (peer_a, peer_b)
    }

    #[test]
    fn test_boxstream() {
        let (mut peer_a, mut peer_b) = load_peers();

        let mut buf_a = [0; 4096];
        let mut buf_b = [0; 4096];

        let msg_a0: Vec<u8> = (0..=255).collect();
        let msg_a1: Vec<u8> = (0..=255).rev().collect();
        let msg_a2: Vec<u8> = (0..=255).collect();

        // Send two messages from A to B
        for msg_a in &[msg_a0, msg_a1, msg_a2] {
            // A
            let send_buf_a = {
                let (n_read, n_write) =
                    encrypt_box_stream_msg(&mut peer_a.key_nonce_send, &msg_a, &mut buf_a);
                // Assert that 256 bytes have been encrypted from msg_a
                assert_eq!(n_read, 256);
                &buf_a[..n_write]
            };

            // B
            let mut recv_buf_b = &mut buf_b[..send_buf_a.len()];
            recv_buf_b.copy_from_slice(send_buf_a);
            let dec_msg_a = {
                let header = decrypt_box_stream_header(&mut peer_b.key_nonce_recv, &mut recv_buf_b)
                    .unwrap()
                    .unwrap();
                // Assert that the body is 256 bytes
                assert_eq!(header.body_len, 256);
                let mut enc_body = &mut recv_buf_b[MSG_HEADER_LEN..];
                let n = decrypt_box_stream_body(&header, &mut peer_b.key_nonce_recv, &mut enc_body)
                    .unwrap();
                // Assert that the decrypted bytes are all the received bytes
                assert_eq!(n, enc_body.len());
                &enc_body[..n]
            };
            // Assert that the decrypted message is the message that was encrypted
            assert_eq!(&dec_msg_a[..], &msg_a[..]);
        }
    }

    #[test]
    fn test_boxstream_send_recv() {
        let (peer_a, peer_b) = load_peers();

        let mut sender = BoxStreamSend::new(peer_a.key_nonce_send);
        let mut receiver = BoxStreamRecv::new(peer_b.key_nonce_recv);

        let mut buf_a = [0; 4096];
        let mut buf_b = [0; 4096];

        let msg_a0: Vec<u8> = (0..=255).collect();
        let msg_a1: Vec<u8> = (0..=255).rev().collect();
        let msg_a2: Vec<u8> = (0..=255).collect();

        // Send two messages from A to B
        for msg_a in &[msg_a0, msg_a1, msg_a2] {
            // A
            let send_buf_a = {
                let (n_read, n_write) = sender.encrypt(&msg_a, &mut buf_a).unwrap();
                // Assert that 256 bytes have been encrypted from msg_a
                assert_eq!(n_read, 256);
                assert_eq!(n_write, MSG_HEADER_LEN + 256);
                &buf_a[..n_write]
            };

            let mut recv_buf_a = send_buf_a;

            // B
            let dec_msg_a = {
                // Decrypt header
                let (n_read, n_write) = receiver.decrypt(&recv_buf_a, &mut buf_b).unwrap().unwrap();
                assert_eq!(n_read, MSG_HEADER_LEN);
                assert_eq!(n_write, 0);
                recv_buf_a = &recv_buf_a[n_read..];
                // Decrypt body
                let (n_read, n_write) = receiver.decrypt(&recv_buf_a, &mut buf_b).unwrap().unwrap();
                assert_eq!(n_read, 256);
                assert_eq!(n_write, 256);
                &buf_b[..n_write]
            };
            // Assert that the decrypted message is the message that was encrypted
            assert_eq!(&dec_msg_a[..], &msg_a[..]);
        }

        // Send and receive goodbye
        sender.encrypt_goodbye(&mut buf_a).unwrap();
        let recv_buf_a = buf_a;
        assert_eq!(
            Ok(Decrypted::Goodbye),
            receiver.decrypt(&recv_buf_a, &mut buf_b)
        );

        // Boxstream is over
        assert_eq!(true, sender.goodbye_sent());
        assert_eq!(true, receiver.goodbye_recvd());
        assert_eq!(
            Err(Error::GoodbyeSent),
            sender.encrypt(&[0, 1, 2, 3], &mut buf_a)
        );
        assert_eq!(
            Err(Error::GoodbyeReceived),
            receiver.decrypt(&recv_buf_a, &mut buf_b)
        );
    }

    use crate::handshake;
    use sodiumoxide::crypto::{auth, sign::ed25519};

    #[test]
    fn test_key_nonce() {
        // Copied from `src/handshake.rs`:`fn test_handshake()`
        let net_id_hex = "d4a1cb88a66f02f8db635ce26441cc5dac1b08420ceaac230839b755845a9ffb";
        let net_id = auth::Key::from_slice(&hex::decode(net_id_hex).unwrap()).unwrap();

        let client_seed_hex = "0000000000000000000000000000000000000000000000000000000000000000";
        let (client_pk, client_sk) = ed25519::keypair_from_seed(
            &ed25519::Seed::from_slice(&hex::decode(client_seed_hex).unwrap()).unwrap(),
        );

        let server_seed_hex = "0000000000000000000000000000000000000000000000000000000000000001";
        let (server_pk, server_sk) = ed25519::keypair_from_seed(
            &ed25519::Seed::from_slice(&hex::decode(server_seed_hex).unwrap()).unwrap(),
        );

        let hs_client = handshake::Handshake::new_client(net_id.clone(), client_pk, client_sk);
        let hs_server = handshake::Handshake::new_server(net_id, server_pk, server_sk);

        let mut buf = [0; 5000];

        let (hs_client, hs_server) = {
            let mut client_buf = &mut buf[..hs_client.send_bytes()];
            let hs_client = hs_client.send_client_hello(&mut client_buf);
            let hs_server = hs_server.recv_client_hello(&mut client_buf).unwrap();
            (hs_client, hs_server)
        };
        let (hs_client, hs_server) = {
            let mut server_buf = &mut buf[..hs_server.send_bytes()];
            let hs_server = hs_server.send_server_hello(&mut server_buf);
            let hs_client = hs_client.recv_server_hello(&mut server_buf).unwrap();
            (hs_client, hs_server)
        };
        let (hs_client, hs_server) = {
            let mut client_buf = &mut buf[..hs_client.send_bytes()];
            let hs_client = hs_client
                .send_client_auth(&mut client_buf, server_pk)
                .unwrap();
            let hs_server = hs_server.recv_client_auth(&mut client_buf).unwrap();
            (hs_client, hs_server)
        };
        let (hs_client, hs_server) = {
            let mut server_buf = &mut buf[..hs_server.send_bytes()];
            let hs_server = hs_server.send_server_accept(&mut server_buf);
            let hs_client = hs_client.recv_server_accept(&mut server_buf).unwrap();
            (hs_client, hs_server)
        };

        let complete_client = hs_client.complete();
        let complete_server = hs_server.complete();

        let (client_send, client_recv) = KeyNonce::from_handshake(complete_client);
        let (server_send, server_recv) = KeyNonce::from_handshake(complete_server);
        assert_eq!(client_send, server_recv);
        assert_eq!(client_recv, server_send);
    }
}