-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_dual_layer.rs
114 lines (97 loc) · 3.2 KB
/
basic_dual_layer.rs
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
use core::str;
use clatter::crypto::cipher::ChaChaPoly;
use clatter::crypto::dh::X25519;
use clatter::crypto::hash::Sha512;
use clatter::crypto::kem::rust_crypto_ml_kem::MlKem512;
use clatter::handshakepattern::{noise_nn, noise_pqnn};
use clatter::traits::Handshaker;
use clatter::{DualLayerHandshake, NqHandshake, PqHandshake};
fn main() {
let mut rng_alice_nq = rand::thread_rng();
let mut rng_bob_nq = rand::thread_rng();
let mut rng_alice_pq = rand::thread_rng();
let mut rng_bob_pq = rand::thread_rng();
let alice_nq = NqHandshake::<X25519, ChaChaPoly, Sha512, _>::new(
noise_nn(),
&[],
true,
None,
None,
None,
None,
&mut rng_alice_nq,
)
.unwrap();
let bob_nq = NqHandshake::<X25519, ChaChaPoly, Sha512, _>::new(
noise_nn(),
&[],
false,
None,
None,
None,
None,
&mut rng_bob_nq,
)
.unwrap();
let alice_pq = PqHandshake::<MlKem512, MlKem512, ChaChaPoly, Sha512, _>::new(
noise_pqnn(),
&[],
true,
None,
None,
None,
None,
&mut rng_alice_pq,
)
.unwrap();
let bob_pq = PqHandshake::<MlKem512, MlKem512, ChaChaPoly, Sha512, _>::new(
noise_pqnn(),
&[],
false,
None,
None,
None,
None,
&mut rng_bob_pq,
)
.unwrap();
let mut alice = DualLayerHandshake::<_, _, _, _, 1500>::new(alice_nq, alice_pq);
let mut bob = DualLayerHandshake::<_, _, _, _, 1500>::new(bob_nq, bob_pq);
// Handshake message buffers
let mut buf_alice = [0u8; 4096];
let mut buf_bob = [0u8; 4096];
// OUTER LAYER - NQ HANDSHAKE
// First handshake message from initiator to responder
// e -->
let n = alice.write_message(&[], &mut buf_alice).unwrap();
let _ = bob.read_message(&buf_alice[..n], &mut buf_bob).unwrap();
// Second handshake message from responder to initiator
// <-- ekem
let n = bob.write_message(&[], &mut buf_bob).unwrap();
let _ = alice.read_message(&buf_bob[..n], &mut buf_alice).unwrap();
// --------------------------
assert!(alice.outer_completed() && bob.outer_completed());
// INNER LAYER - PQ HANDSHAKE
// First handshake message from initiator to responder
// e -->
let n = alice.write_message(&[], &mut buf_alice).unwrap();
let _ = bob.read_message(&buf_alice[..n], &mut buf_bob).unwrap();
// Second handshake message from responder to initiator
// <-- e, ee
let n = bob.write_message(&[], &mut buf_bob).unwrap();
let _ = alice.read_message(&buf_bob[..n], &mut buf_alice).unwrap();
// --------------------------
// Handshake should be done
assert!(alice.is_finished() && bob.is_finished());
// Finish handshakes and move to transport mode
let mut alice = alice.finalize().unwrap();
let mut bob = bob.finalize().unwrap();
// Send a message from Alice to Bob
let msg = b"Hello from initiator";
let n = alice.send(msg, &mut buf_alice).unwrap();
let n = bob.receive(&buf_alice[..n], &mut buf_bob).unwrap();
println!(
"Bob received from Alice: {}",
str::from_utf8(&buf_bob[..n]).unwrap()
);
}