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
use super::SecretKey32;
use p256::{ecdh::EphemeralSecret, PublicKey};
use rand::rngs::OsRng;
use sha2::Sha256;
use std::{convert::TryFrom, io};
mod pkb;
pub use pkb::PublicKeyBytes;
mod salt;
pub use salt::Salt;
pub struct KeyExchange {
secret: EphemeralSecret,
salt: Salt,
}
impl Default for KeyExchange {
fn default() -> Self {
let secret = EphemeralSecret::random(&mut OsRng);
let salt = Salt::random();
Self { secret, salt }
}
}
impl KeyExchange {
pub fn pk_bytes(&self) -> PublicKeyBytes {
PublicKeyBytes::from(self.secret.public_key())
}
pub fn salt(&self) -> &Salt {
&self.salt
}
pub fn derive_shared_secret(
&self,
public_key: PublicKeyBytes,
salt: Salt,
) -> io::Result<SecretKey32> {
let decoded_public_key = PublicKey::try_from(public_key)?;
let shared_salt = self.salt ^ salt;
let shared_secret = self.secret.diffie_hellman(&decoded_public_key);
let hkdf = shared_secret.extract::<Sha256>(Some(shared_salt.as_ref()));
let mut shared_key = [0u8; 32];
match hkdf.expand(&[], &mut shared_key) {
Ok(_) => Ok(SecretKey32::from(shared_key)),
Err(x) => Err(io::Error::new(io::ErrorKind::InvalidData, x.to_string())),
}
}
}