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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
use sodiumoxide::crypto::{auth, hash::sha256, scalarmult::curve25519, secretbox, sign::ed25519};
use thiserror::Error;

/// Helper type used to define the kind of private key in a scalar multiplication.  Used to define
/// scalar multiplication errors.
#[derive(Debug)]
pub enum ScalarMultSk {
    Ephemeral,
    LongTerm,
}

/// Helper type used to define the kind of public key in a scalar multiplication.  Used to define
/// scalar multiplication errors.
#[derive(Debug)]
pub enum ScalarMultPk {
    ClientEphemeral,
    ClientLongTerm,
    ServerEphemeral,
    ServerLongTerm,
}

/// The error type for handshake operations.
#[derive(Error, Debug)]
pub enum Error {
    #[error("failed verifing server hello")]
    RecvServerHelloAuth,
    #[error("failed opening server secret box")]
    RecvServerAcceptSecretbox,
    #[error("failed verifing server signature")]
    RecvServerAcceptEd25519,
    #[error("failed verifing client hello")]
    RecvClientHelloAuth,
    #[error("failed opening client secret box")]
    RecvClientAuthSecretbox,
    #[error("failed verifying client signature")]
    RecvClientAuthEd25519,
    #[error("scalar math failed (sendclientauth)")]
    SendClientAuthScalarmult(ScalarMultSk, ScalarMultPk),
    #[error("scalar math failed (recvclienthelo)")]
    RecvClientHelloScalarmult(ScalarMultSk, ScalarMultPk),
    #[error("scalar math failed (recvclientauth)")]
    RecvClientAuthScalarmult(ScalarMultSk, ScalarMultPk),
}

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

#[derive(Debug)]
#[allow(non_snake_case)]
pub struct SharedSecretPartial {
    ab: curve25519::GroupElement,
    aB: curve25519::GroupElement,
}

/// The shared secret obtained after a successful handshake.
#[derive(Debug, Clone, PartialEq)]
#[allow(non_snake_case)]
pub struct SharedSecret {
    pub ab: curve25519::GroupElement,
    pub aB: curve25519::GroupElement,
    pub Ab: curve25519::GroupElement,
}

/// The initialization data of a handshake that exists in every state of the handshake.
#[derive(Debug)]
pub struct HandshakeBase {
    net_id: auth::Key,
    pk: ed25519::PublicKey,
    sk: ed25519::SecretKey,
    ephemeral_pk: curve25519::GroupElement,
    ephemeral_sk: curve25519::Scalar,
}

/// The `Handshake` type maintains the different states that happen in each step of the handshake,
/// allowing it to advance to completion.
///
/// The `Handshake` follows the [typestate pattern](http://cliffle.com/blog/rust-typestate/).
#[derive(Debug)]
pub struct Handshake<S: State> {
    pub base: HandshakeBase,
    pub state: S,
}

// Client States

/// The client state that can send the client hello.
#[derive(Debug)]
pub struct SendClientHello;

/// The client state that can receive the server hello.
#[derive(Debug)]
pub struct RecvServerHello;

/// The client state that can send the client auth.
#[derive(Debug)]
pub struct SendClientAuth {
    server_ephemeral_pk: curve25519::GroupElement,
}

/// The client state that can receive the server accept.
#[derive(Debug)]
pub struct RecvServerAccept {
    server_pk: ed25519::PublicKey,
    server_ephemeral_pk: curve25519::GroupElement,
    shared_secret: SharedSecret,
    sig: ed25519::Signature,
}

// Server States

/// The server state that can receive the client hello.
#[derive(Debug)]
pub struct RecvClientHello;

/// The server state that can send the server hello.
#[derive(Debug)]
pub struct SendServerHello {
    client_ephemeral_pk: curve25519::GroupElement,
    shared_secret_partial: SharedSecretPartial,
}

/// The server state that can receive the client auth.
#[derive(Debug)]
pub struct RecvClientAuth {
    client_ephemeral_pk: curve25519::GroupElement,
    shared_secret_partial: SharedSecretPartial,
}

/// The server state that can send the server accept.
#[derive(Debug)]
#[allow(non_snake_case)]
pub struct SendServerAccept {
    client_pk: ed25519::PublicKey,
    client_ephemeral_pk: curve25519::GroupElement,
    shared_secret: SharedSecret,
    client_sig: ed25519::Signature,
}

/// The client/server state reached when a handshake has been performed.
#[derive(Debug)]
pub struct Complete {
    pub peer_pk: ed25519::PublicKey,
    pub peer_ephemeral_pk: curve25519::GroupElement,
    pub shared_secret: SharedSecret,
}

/// The `State` trait is used to implement the typestate pattern for the `Handshake`.
///
/// The state machine is as follows:
///
/// Client:
/// - [`SendClientHello`] - `send_client_hello()` -> [`RecvServerHello]
/// - [`RecvServerHello`] - `recv_server_hello()` -> [`SendClientAuth]
/// - [`SendClientAuth`] - `send_client_auth()` -> [`RecvServerAccept]
/// - [`RecvServerAccept`] - `recv_server_accept()` -> [`Complete]
///
/// Server:
/// - [`RecvClientHello`] - `recv_client_hello()` -> [`SendServerHello`]
/// - [`SendServerHello`] - `send_server_hello()` -> [`RecvClientAuth`]
/// - [`RecvClientAuth`] - `recv_client_auth()` -> [`SendServerAccept`]
/// - [`SendServerAccept`] - `send_server_accept()` -> [`Complete`]
pub trait State {}
impl State for SendClientHello {}
impl State for RecvServerHello {}
impl State for SendClientAuth {}
impl State for RecvServerAccept {}

impl State for RecvClientHello {}
impl State for SendServerHello {}
impl State for RecvClientAuth {}
impl State for SendServerAccept {}

impl State for Complete {}

// Client
impl Handshake<SendClientHello> {
    /// Create a new handshake client that can send the client hello.
    pub fn new_client(
        net_id: auth::Key,
        pk: ed25519::PublicKey,
        sk: ed25519::SecretKey,
    ) -> Handshake<SendClientHello> {
        let (ephemeral_ed_pk, ephemeral_ed_sk) = ed25519::gen_keypair();
        let ephemeral_pk = ephemeral_ed_pk.to_curve25519();
        let ephemeral_sk = ephemeral_ed_sk.to_curve25519();
        let state = SendClientHello;
        let base = HandshakeBase {
            net_id,
            pk,
            sk,
            ephemeral_pk,
            ephemeral_sk,
        };
        Handshake { base, state }
    }
}

/// Size of the client hello message.
pub const CLIENT_HELLO_BYTES: usize = 64;

impl Handshake<SendClientHello> {
    /// Send a client hello and advance to the next client state.
    pub fn send_client_hello(self, send_buf: &mut [u8]) -> Handshake<RecvServerHello> {
        concat_into!(
            send_buf,
            auth::authenticate(self.base.ephemeral_pk.as_ref(), &self.base.net_id).as_ref(),
            self.base.ephemeral_pk.as_ref()
        );
        let state = RecvServerHello;
        Handshake {
            base: self.base,
            state,
        }
    }
    /// Number of bytes that will be written to the `send_buf` in this state.
    pub const fn send_bytes(&self) -> usize {
        CLIENT_HELLO_BYTES
    }
}

impl Handshake<RecvServerHello> {
    /// Receive a server hello and advance to the next client state.
    pub fn recv_server_hello(self, recv_buf: &[u8]) -> Result<Handshake<SendClientAuth>> {
        let server_hmac = auth::Tag::from_slice(&recv_buf[..32]).unwrap();
        let server_ephemeral_pk = curve25519::GroupElement::from_slice(&recv_buf[32..]).unwrap();
        if !auth::verify(
            &server_hmac,
            server_ephemeral_pk.as_ref(),
            &self.base.net_id,
        ) {
            return Err(Error::RecvServerHelloAuth);
        }
        Ok(Handshake {
            base: self.base,
            state: SendClientAuth {
                server_ephemeral_pk,
            },
        })
    }
    /// Number of bytes that will be read from the `recv_buf` in this state.
    pub const fn recv_bytes(&self) -> usize {
        SERVER_HELLO_BYTES
    }
}

/// Size of the client auth message.
pub const CLIENT_AUTH_BYTES: usize = 112;

impl Handshake<SendClientAuth> {
    /// Send a client auth and advance to the next client state.
    pub fn send_client_auth(
        self,
        send_buf: &mut [u8],
        server_pk: ed25519::PublicKey,
    ) -> Result<Handshake<RecvServerAccept>> {
        let fn_error = |a, b| Err(Error::SendClientAuthScalarmult(a, b));
        let shared_secret = SharedSecret {
            ab: curve25519::scalarmult(&self.base.ephemeral_sk, &self.state.server_ephemeral_pk)
                .or_else(|_| fn_error(ScalarMultSk::Ephemeral, ScalarMultPk::ServerEphemeral))?,
            aB: curve25519::scalarmult(&self.base.ephemeral_sk, &server_pk.to_curve25519())
                .or_else(|_| fn_error(ScalarMultSk::Ephemeral, ScalarMultPk::ServerLongTerm))?,
            Ab: curve25519::scalarmult(
                &self.base.sk.to_curve25519(),
                &self.state.server_ephemeral_pk,
            )
            .or_else(|_| fn_error(ScalarMultSk::LongTerm, ScalarMultPk::ServerEphemeral))?,
        };

        let sig = ed25519::sign_detached(
            &concat!(
                auth::KEYBYTES + ed25519::PUBLICKEYBYTES + sha256::DIGESTBYTES,
                self.base.net_id.as_ref(),
                server_pk.as_ref(),
                sha256::hash(shared_secret.ab.as_ref()).as_ref()
            ),
            &self.base.sk,
        );

        let tag = secretbox::seal_detached(
            concat_into!(
                &mut send_buf[secretbox::MACBYTES..],
                sig.as_ref(),
                self.base.pk.as_ref()
            ),
            &secretbox::Nonce([0; 24]),
            &secretbox::Key(
                sha256::hash(&concat!(
                    auth::KEYBYTES + curve25519::GROUPELEMENTBYTES * 2,
                    self.base.net_id.as_ref(),
                    shared_secret.ab.as_ref(),
                    shared_secret.aB.as_ref()
                ))
                .0,
            ),
        );
        send_buf[..secretbox::MACBYTES].copy_from_slice(tag.as_ref());

        Ok(Handshake {
            base: self.base,
            state: RecvServerAccept {
                server_pk,
                server_ephemeral_pk: self.state.server_ephemeral_pk,
                shared_secret,
                sig,
            },
        })
    }
    /// Number of bytes that will be written to the `send_buf` in this state.
    pub const fn send_bytes(&self) -> usize {
        CLIENT_AUTH_BYTES
    }
}

impl Handshake<RecvServerAccept> {
    pub fn recv_server_accept(self, recv_buf: &mut [u8]) -> Result<Handshake<Complete>> {
        let (tag_buf, mut enc_buf) = recv_buf.split_at_mut(secretbox::MACBYTES);
        secretbox::open_detached(
            &mut enc_buf,
            &secretbox::Tag::from_slice(tag_buf).unwrap(),
            &secretbox::Nonce([0; 24]),
            &secretbox::Key(
                sha256::hash(
                    &[
                        self.base.net_id.as_ref(),
                        self.state.shared_secret.ab.as_ref(),
                        self.state.shared_secret.aB.as_ref(),
                        self.state.shared_secret.Ab.as_ref(),
                    ]
                    .concat(),
                )
                .0,
            ),
        )
        .or(Err(Error::RecvServerAcceptSecretbox))?;
        let dec_buf = enc_buf;
        let sig = ed25519::Signature::from_slice(dec_buf).unwrap();
        if !ed25519::verify_detached(
            &sig,
            &[
                self.base.net_id.as_ref(),
                self.state.sig.as_ref(),
                self.base.pk.as_ref(),
                sha256::hash(self.state.shared_secret.ab.as_ref()).as_ref(),
            ]
            .concat(),
            &self.state.server_pk,
        ) {
            return Err(Error::RecvServerAcceptEd25519);
        }
        Ok(Handshake {
            base: self.base,
            state: Complete {
                peer_pk: self.state.server_pk,
                peer_ephemeral_pk: self.state.server_ephemeral_pk,
                shared_secret: self.state.shared_secret,
            },
        })
    }
    /// Number of bytes that will be read from the `recv_buf` in this state.
    pub const fn recv_bytes(&self) -> usize {
        SERVER_ACCEPT_BYTES
    }
}

// Server
impl Handshake<RecvClientHello> {
    /// Create a new handshake server that can receive the client hello.
    pub fn new_server(
        net_id: auth::Key,
        pk: ed25519::PublicKey,
        sk: ed25519::SecretKey,
    ) -> Handshake<RecvClientHello> {
        let (ephemeral_ed_pk, ephemeral_ed_sk) = ed25519::gen_keypair();
        let ephemeral_pk = ephemeral_ed_pk.to_curve25519();
        let ephemeral_sk = ephemeral_ed_sk.to_curve25519();
        Handshake {
            base: HandshakeBase {
                net_id,
                pk,
                sk,
                ephemeral_pk,
                ephemeral_sk,
            },
            state: RecvClientHello,
        }
    }
}

impl Handshake<RecvClientHello> {
    /// Receive a client hello and advance to the next server state.
    pub fn recv_client_hello(self, recv_buf: &[u8]) -> Result<Handshake<SendServerHello>> {
        let client_hmac = auth::Tag::from_slice(&recv_buf[..32]).unwrap();
        let client_ephemeral_pk = curve25519::GroupElement::from_slice(&recv_buf[32..]).unwrap();
        if !auth::verify(
            &client_hmac,
            client_ephemeral_pk.as_ref(),
            &self.base.net_id,
        ) {
            return Err(Error::RecvClientHelloAuth);
        }
        let fn_error = |a, b| Err(Error::RecvClientHelloScalarmult(a, b));
        let shared_secret_partial = SharedSecretPartial {
            ab: curve25519::scalarmult(&self.base.ephemeral_sk, &client_ephemeral_pk)
                .or_else(|_| fn_error(ScalarMultSk::Ephemeral, ScalarMultPk::ClientEphemeral))?,
            aB: curve25519::scalarmult(&self.base.sk.to_curve25519(), &client_ephemeral_pk)
                .or_else(|_| fn_error(ScalarMultSk::LongTerm, ScalarMultPk::ClientEphemeral))?,
        };
        Ok(Handshake {
            base: self.base,
            state: SendServerHello {
                client_ephemeral_pk,
                shared_secret_partial,
            },
        })
    }
    /// Number of bytes that will be read from the `recv_buf` in this state.
    pub const fn recv_bytes(&self) -> usize {
        CLIENT_HELLO_BYTES
    }
}

/// Size of the server hello message.
pub const SERVER_HELLO_BYTES: usize = 64;

impl Handshake<SendServerHello> {
    /// Send a server hello and advance to the next server state.
    pub fn send_server_hello(self, send_buf: &mut [u8]) -> Handshake<RecvClientAuth> {
        concat_into!(
            send_buf,
            auth::authenticate(self.base.ephemeral_pk.as_ref(), &self.base.net_id).as_ref(),
            self.base.ephemeral_pk.as_ref()
        );
        Handshake {
            base: self.base,
            state: RecvClientAuth {
                client_ephemeral_pk: self.state.client_ephemeral_pk,
                shared_secret_partial: self.state.shared_secret_partial,
            },
        }
    }
    /// Number of bytes that will be written to the `send_buf` in this state.
    pub const fn send_bytes(&self) -> usize {
        SERVER_HELLO_BYTES
    }
}

impl Handshake<RecvClientAuth> {
    /// Receive a client auth and advance to the next server state.
    pub fn recv_client_auth(self, recv_buf: &mut [u8]) -> Result<Handshake<SendServerAccept>> {
        let (tag_buf, mut enc_buf) = recv_buf.split_at_mut(secretbox::MACBYTES);
        secretbox::open_detached(
            &mut enc_buf,
            &secretbox::Tag::from_slice(tag_buf).unwrap(),
            &secretbox::Nonce([0; 24]),
            &secretbox::Key(
                sha256::hash(
                    &[
                        self.base.net_id.as_ref(),
                        self.state.shared_secret_partial.ab.as_ref(),
                        self.state.shared_secret_partial.aB.as_ref(),
                    ]
                    .concat(),
                )
                .0,
            ),
        )
        .or(Err(Error::RecvClientAuthSecretbox))?;
        let dec_buf = enc_buf;
        let client_sig = ed25519::Signature::from_slice(&dec_buf[..64]).unwrap();
        let client_pk = ed25519::PublicKey::from_slice(&dec_buf[64..]).unwrap();
        if !ed25519::verify_detached(
            &client_sig,
            &[
                self.base.net_id.as_ref(),
                self.base.pk.as_ref(),
                sha256::hash(self.state.shared_secret_partial.ab.as_ref()).as_ref(),
            ]
            .concat(),
            &client_pk,
        ) {
            return Err(Error::RecvClientAuthEd25519);
        }
        let fn_error = |a, b| Err(Error::RecvClientHelloScalarmult(a, b));
        let shared_secret = SharedSecret {
            ab: self.state.shared_secret_partial.ab,
            aB: self.state.shared_secret_partial.aB,
            Ab: curve25519::scalarmult(&self.base.ephemeral_sk, &client_pk.to_curve25519())
                .or_else(|_| fn_error(ScalarMultSk::Ephemeral, ScalarMultPk::ClientLongTerm))?,
        };
        Ok(Handshake {
            base: self.base,
            state: SendServerAccept {
                client_pk,
                client_ephemeral_pk: self.state.client_ephemeral_pk,
                shared_secret,
                client_sig,
            },
        })
    }
    /// Number of bytes that will be read from the `recv_buf` in this state.
    pub const fn recv_bytes(&self) -> usize {
        CLIENT_AUTH_BYTES
    }
}

/// Size of the server accept message.
pub const SERVER_ACCEPT_BYTES: usize = 80;

impl Handshake<SendServerAccept> {
    /// Send a server accept and advance to the next server state.
    pub fn send_server_accept(self, send_buf: &mut [u8]) -> Handshake<Complete> {
        let sig = ed25519::sign_detached(
            &concat!(
                auth::KEYBYTES
                    + ed25519::SIGNATUREBYTES
                    + ed25519::PUBLICKEYBYTES
                    + sha256::DIGESTBYTES,
                self.base.net_id.as_ref(),
                self.state.client_sig.as_ref(),
                self.state.client_pk.as_ref(),
                sha256::hash(self.state.shared_secret.ab.as_ref()).as_ref()
            ),
            &self.base.sk,
        );
        send_buf[secretbox::MACBYTES..].copy_from_slice(sig.as_ref());
        let tag = secretbox::seal_detached(
            &mut send_buf[secretbox::MACBYTES..],
            &secretbox::Nonce([0; 24]),
            &secretbox::Key(
                sha256::hash(&concat!(
                    auth::KEYBYTES + curve25519::GROUPELEMENTBYTES * 3,
                    self.base.net_id.as_ref(),
                    self.state.shared_secret.ab.as_ref(),
                    self.state.shared_secret.aB.as_ref(),
                    self.state.shared_secret.Ab.as_ref()
                ))
                .0,
            ),
        );
        send_buf[..secretbox::MACBYTES].copy_from_slice(tag.as_ref());

        Handshake {
            base: self.base,
            state: Complete {
                peer_pk: self.state.client_pk,
                peer_ephemeral_pk: self.state.client_ephemeral_pk,
                shared_secret: self.state.shared_secret,
            },
        }
    }
    /// Number of bytes that will be written to the `send_buf` in this state.
    pub const fn send_bytes(&self) -> usize {
        SERVER_ACCEPT_BYTES
    }
}

/// Type used to group all the values obtained during a successful handshake that can be used to
/// stablish a secure authenticated channel.
#[derive(Debug)]
pub struct HandshakeComplete {
    pub net_id: auth::Key,
    pub pk: ed25519::PublicKey,
    pub ephemeral_pk: curve25519::GroupElement,
    pub peer_pk: ed25519::PublicKey,
    pub peer_ephemeral_pk: curve25519::GroupElement,
    pub shared_secret: SharedSecret,
}

impl Handshake<Complete> {
    /// Create a `HandshakeComplete` out of a `Handshake` in the `Complete` state.
    pub fn complete(&self) -> HandshakeComplete {
        HandshakeComplete {
            net_id: self.base.net_id.clone(),
            pk: self.base.pk,
            ephemeral_pk: self.base.ephemeral_pk.clone(),
            peer_pk: self.state.peer_pk,
            peer_ephemeral_pk: self.state.peer_ephemeral_pk.clone(),
            shared_secret: self.state.shared_secret.clone(),
        }
    }
}

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

    #[test]
    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::new_client(net_id.clone(), client_pk, client_sk);
        let hs_server = 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 mut server_buf = &mut buf[..hs_server.recv_bytes()];
            let hs_server = hs_server.recv_client_hello(&mut server_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 mut client_buf = &mut buf[..hs_client.recv_bytes()];
            let hs_client = hs_client.recv_server_hello(&mut client_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 mut server_buf = &mut buf[..hs_server.recv_bytes()];
            let hs_server = hs_server.recv_client_auth(&mut server_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 mut client_buf = &mut buf[..hs_client.recv_bytes()];
            let hs_client = hs_client.recv_server_accept(&mut client_buf).unwrap();
            (hs_client, hs_server)
        };

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

        assert_eq!(complete_client.net_id, complete_server.net_id);
        assert_eq!(complete_client.shared_secret, complete_server.shared_secret);
        assert_eq!(complete_client.pk, complete_server.peer_pk);
        assert_eq!(
            complete_client.ephemeral_pk,
            complete_server.peer_ephemeral_pk
        );
        assert_eq!(complete_client.peer_pk, complete_server.pk);
        assert_eq!(
            complete_client.peer_ephemeral_pk,
            complete_server.ephemeral_pk
        );
    }
}