-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathkx.rs
More file actions
169 lines (145 loc) · 5.37 KB
/
Copy pathkx.rs
File metadata and controls
169 lines (145 loc) · 5.37 KB
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
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
use crypto::{SharedSecret, SupportedKxGroup};
use crypto_common::Generate;
use getrandom::rand_core::UnwrapErr;
use paste::paste;
use rustls::crypto;
mod hybrid;
mod mlkem;
pub use hybrid::{SecP256r1MLKEM768, SecP384r1MLKEM1024, X25519MLKEM768};
#[derive(Debug)]
pub struct X25519;
impl crypto::SupportedKxGroup for X25519 {
fn name(&self) -> rustls::NamedGroup {
rustls::NamedGroup::X25519
}
fn start(&self) -> Result<Box<dyn crypto::ActiveKeyExchange>, rustls::Error> {
let mut rng = UnwrapErr(getrandom::SysRng);
let priv_key = x25519_dalek::EphemeralSecret::random_from_rng(&mut rng);
let pub_key = (&priv_key).into();
Ok(Box::new(X25519KeyExchange { priv_key, pub_key }))
}
}
pub struct X25519KeyExchange {
priv_key: x25519_dalek::EphemeralSecret,
pub_key: x25519_dalek::PublicKey,
}
impl crypto::ActiveKeyExchange for X25519KeyExchange {
fn complete(self: Box<X25519KeyExchange>, peer: &[u8]) -> Result<SharedSecret, rustls::Error> {
let peer_array: [u8; 32] = peer
.try_into()
.map_err(|_| rustls::Error::from(rustls::PeerMisbehaved::InvalidKeyShare))?;
Ok(self
.priv_key
.diffie_hellman(&peer_array.into())
.as_ref()
.into())
}
fn pub_key(&self) -> &[u8] {
self.pub_key.as_bytes()
}
fn group(&self) -> rustls::NamedGroup {
X25519.name()
}
}
macro_rules! impl_kx {
($name:ident, $kx_name:ty, $secret:ty, $public_key:ty) => {
paste! {
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct $name;
impl crypto::SupportedKxGroup for $name {
fn name(&self) -> rustls::NamedGroup {
$kx_name
}
fn start(&self) -> Result<Box<dyn crypto::ActiveKeyExchange>, rustls::Error> {
let mut rng = UnwrapErr(getrandom::SysRng);
let priv_key = <$secret>::try_generate_from_rng(&mut rng).map_err(|_| rustls::Error::from(rustls::PeerMisbehaved::InvalidKeyShare))?;
let pub_key: $public_key = (&priv_key).into();
Ok(Box::new([<$name KeyExchange>] {
priv_key,
pub_key: pub_key.to_sec1_bytes(),
}))
}
}
#[allow(non_camel_case_types)]
pub struct [<$name KeyExchange>] {
priv_key: $secret,
pub_key: Box<[u8]>,
}
impl crypto::ActiveKeyExchange for [<$name KeyExchange>] {
fn complete(
self: Box<[<$name KeyExchange>]>,
peer: &[u8],
) -> Result<SharedSecret, rustls::Error> {
let their_pub = $public_key::from_sec1_bytes(peer)
.map_err(|_| rustls::Error::from(rustls::PeerMisbehaved::InvalidKeyShare))?;
Ok(self
.priv_key
.diffie_hellman(&their_pub)
.raw_secret_bytes()
.as_slice()
.into())
}
fn pub_key(&self) -> &[u8] {
&self.pub_key
}
fn group(&self) -> rustls::NamedGroup {
$name.name()
}
}
}
};
}
impl_kx! {SecP256R1, rustls::NamedGroup::secp256r1, p256::ecdh::EphemeralSecret, p256::PublicKey}
impl_kx! {SecP384R1, rustls::NamedGroup::secp384r1, p384::ecdh::EphemeralSecret, p384::PublicKey}
pub const ALL_KX_GROUPS: &[&dyn SupportedKxGroup] = &[
&X25519,
&SecP256R1,
&SecP384R1,
&X25519MLKEM768,
&SecP256r1MLKEM768,
&SecP384r1MLKEM1024,
];
#[cfg(test)]
mod test {
// Make sure every key exchange algorithm can round-trip with itself.
#[test]
fn kx_roundtrip() -> Result<(), rustls::Error> {
for kx in super::ALL_KX_GROUPS {
let client_state = kx.start()?;
let server_output = kx.start_and_complete(client_state.pub_key())?;
let client_output = client_state.complete(&server_output.pub_key)?;
assert_eq!(server_output.group, kx.name());
assert_eq!(
server_output.secret.secret_bytes(),
client_output.secret_bytes()
);
}
Ok(())
}
// Make sure that the hybrid optimization works for each key
// exchange that provides it.
#[test]
fn kx_hybrid_optimization() -> Result<(), rustls::Error> {
for kx in super::ALL_KX_GROUPS {
let client_state = kx.start()?;
if let Some((grp, client_pubkey)) = client_state.hybrid_component() {
let server_kx = super::ALL_KX_GROUPS
.iter()
.find(|g| g.name() == grp)
.unwrap();
let server_output = server_kx.start_and_complete(client_pubkey)?;
let client_output =
client_state.complete_hybrid_component(&server_output.pub_key)?;
assert_eq!(server_output.group, grp);
assert_eq!(
server_output.secret.secret_bytes(),
client_output.secret_bytes()
);
}
}
Ok(())
}
}