ytls_rustcrypto/
dh.rs

1//! yTLS RustCrypto Hashers
2
3use x25519_dalek::EphemeralSecret;
4use x25519_dalek::PublicKey;
5use ytls_traits::CryptoX25519Processor;
6
7use rand_core::CryptoRng;
8
9/// RustCrypto Sha384Hasher
10pub struct X25519 {
11    ep: EphemeralSecret,
12}
13
14impl X25519 {
15    pub fn x25519_init<R: CryptoRng>(rng: &mut R) -> Self {
16        let ep = EphemeralSecret::random_from_rng(rng);
17        Self { ep }
18    }
19}
20
21impl CryptoX25519Processor for X25519 {
22    fn x25519_public_key(&self) -> [u8; 32] {
23        PublicKey::from(&self.ep).to_bytes()
24    }
25    fn x25519_shared_secret(self, pub_key: &[u8; 32]) -> [u8; 32] {
26        self.ep.diffie_hellman(&(*pub_key).into()).to_bytes()
27    }
28}