Skip to main content

miden_crypto/ecdh/
x25519.rs

1//! X25519 (Elliptic Curve Diffie-Hellman) key agreement implementation using
2//! Curve25519.
3//!
4//! Note that the intended use is in the context of a one-way, sender initiated key agreement
5//! scenario. Namely, when the sender knows the (static) public key of the receiver and it
6//! uses that, together with an ephemeral secret key that it generates, to derive a shared
7//! secret.
8//!
9//! This shared secret will then be used to encrypt some message (using for example a key
10//! derivation function).
11//!
12//! The public key associated with the ephemeral secret key will be sent alongside the encrypted
13//! message.
14
15use alloc::vec::Vec;
16
17use hkdf::{Hkdf, hmac::SimpleHmac};
18use k256::sha2::Sha256;
19use rand::{CryptoRng, RngCore};
20use subtle::ConstantTimeEq;
21
22use crate::{
23    dsa::eddsa_25519_sha512::{KeyExchangeKey, PublicKey},
24    ecdh::KeyAgreementScheme,
25    utils::{
26        ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
27        zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing},
28    },
29};
30// SHARED SECRETE
31// ================================================================================================
32
33/// A shared secret computed using the X25519 (Elliptic Curve Diffie-Hellman) key agreement.
34///
35/// This type implements `ZeroizeOnDrop` because the inner `x25519_dalek::SharedSecret`
36/// implements it, ensuring the shared secret is securely wiped from memory when dropped.
37pub struct SharedSecret {
38    pub(crate) inner: x25519_dalek::SharedSecret,
39}
40impl SharedSecret {
41    pub(crate) fn new(inner: x25519_dalek::SharedSecret) -> SharedSecret {
42        Self { inner }
43    }
44
45    /// Returns a HKDF that can be used to derive uniform keys from the shared secret.
46    pub fn extract(&self, salt: Option<&[u8]>) -> Hkdf<Sha256, SimpleHmac<Sha256>> {
47        Hkdf::new(salt, self.inner.as_bytes())
48    }
49}
50
51impl Zeroize for SharedSecret {
52    /// Securely clears the shared secret from memory.
53    ///
54    /// # Security
55    ///
56    /// This implementation follows the same security methodology as the `zeroize` crate to ensure
57    /// that sensitive cryptographic material is reliably cleared from memory:
58    ///
59    /// - **Volatile writes**: Uses `ptr::write_volatile` to prevent dead store elimination and
60    ///   other compiler optimizations that might remove the zeroing operation.
61    /// - **Memory ordering**: Includes a sequentially consistent compiler fence (`SeqCst`) to
62    ///   prevent instruction reordering that could expose the secret data after this function
63    ///   returns.
64    fn zeroize(&mut self) {
65        let bytes = self.inner.as_bytes();
66        for byte in
67            unsafe { core::slice::from_raw_parts_mut(bytes.as_ptr() as *mut u8, bytes.len()) }
68        {
69            unsafe {
70                core::ptr::write_volatile(byte, 0u8);
71            }
72        }
73        core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
74    }
75}
76
77// Safe to derive ZeroizeOnDrop because we implement Zeroize above
78impl ZeroizeOnDrop for SharedSecret {}
79
80impl AsRef<[u8]> for SharedSecret {
81    fn as_ref(&self) -> &[u8] {
82        self.inner.as_bytes()
83    }
84}
85
86// EPHEMERAL SECRET KEY
87// ================================================================================================
88
89/// Ephemeral secret key for X25519 key agreement.
90///
91/// This type implements `ZeroizeOnDrop` because the inner `x25519_dalek::EphemeralSecret`
92/// implements it, ensuring the secret key material is securely wiped from memory when dropped.
93pub struct EphemeralSecretKey {
94    inner: x25519_dalek::EphemeralSecret,
95}
96
97impl ZeroizeOnDrop for EphemeralSecretKey {}
98
99impl EphemeralSecretKey {
100    /// Generates a new random ephemeral secret key using the OS random number generator.
101    #[cfg(feature = "std")]
102    #[allow(clippy::new_without_default)]
103    pub fn new() -> Self {
104        let mut rng = rand::rng();
105
106        Self::with_rng(&mut rng)
107    }
108
109    /// Generates a new random ephemeral secret key using the provided RNG.
110    pub fn with_rng<R: CryptoRng + RngCore>(rng: &mut R) -> Self {
111        // we use a seedable CSPRNG and seed it with `rng`
112        // this is a work around the fact that the version of the `rand` dependency in our crate
113        // is different than the one used in the `x25519_dalek` one. This solution will no longer be
114        // needed once `x25519_dalek` gets a new release with a version of the `rand`
115        // dependency matching ours
116        use k256::elliptic_curve::rand_core::SeedableRng;
117        let mut seed = Zeroizing::new([0_u8; 32]);
118        RngCore::fill_bytes(rng, &mut *seed);
119        let rng = rand_hc::Hc128Rng::from_seed(*seed);
120
121        let sk = x25519_dalek::EphemeralSecret::random_from_rng(rng);
122        Self { inner: sk }
123    }
124
125    /// Returns the corresponding ephemeral public key.
126    pub fn public_key(&self) -> EphemeralPublicKey {
127        EphemeralPublicKey {
128            inner: x25519_dalek::PublicKey::from(&self.inner),
129        }
130    }
131
132    /// Computes a Diffie-Hellman shared secret from this ephemeral secret key and the other party's
133    /// static public key.
134    pub fn diffie_hellman(self, pk_other: &PublicKey) -> SharedSecret {
135        let shared = self.inner.diffie_hellman(&pk_other.to_x25519());
136        SharedSecret::new(shared)
137    }
138}
139
140// EPHEMERAL PUBLIC KEY
141// ================================================================================================
142
143/// Ephemeral public key for X25519 agreement.
144#[derive(Debug, Clone, PartialEq, Eq)]
145pub struct EphemeralPublicKey {
146    pub(crate) inner: x25519_dalek::PublicKey,
147}
148
149impl Serializable for EphemeralPublicKey {
150    fn write_into<W: ByteWriter>(&self, target: &mut W) {
151        target.write_bytes(self.inner.as_bytes());
152    }
153}
154
155impl Deserializable for EphemeralPublicKey {
156    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
157        let bytes: [u8; 32] = source.read_array()?;
158        // Reject twist points and low-order points. We intentionally avoid the more expensive
159        // torsion-free check; small-order rejection mitigates the most dangerous malleability
160        // issues, even though it does not guarantee torsion-freeness.
161        let mont = curve25519_dalek::montgomery::MontgomeryPoint(bytes);
162        let edwards = mont.to_edwards(0).ok_or_else(|| {
163            DeserializationError::InvalidValue("Invalid X25519 public key".into())
164        })?;
165        if edwards.is_small_order() {
166            return Err(DeserializationError::InvalidValue("Invalid X25519 public key".into()));
167        }
168
169        Ok(Self {
170            inner: x25519_dalek::PublicKey::from(bytes),
171        })
172    }
173}
174
175// KEY AGREEMENT TRAIT IMPLEMENTATION
176// ================================================================================================
177
178pub struct X25519;
179
180impl KeyAgreementScheme for X25519 {
181    type EphemeralSecretKey = EphemeralSecretKey;
182    type EphemeralPublicKey = EphemeralPublicKey;
183
184    type SecretKey = KeyExchangeKey;
185    type PublicKey = PublicKey;
186
187    type SharedSecret = SharedSecret;
188
189    fn generate_ephemeral_keypair<R: CryptoRng + RngCore>(
190        rng: &mut R,
191    ) -> (Self::EphemeralSecretKey, Self::EphemeralPublicKey) {
192        let sk = EphemeralSecretKey::with_rng(rng);
193        let pk = sk.public_key();
194
195        (sk, pk)
196    }
197
198    fn exchange_ephemeral_static(
199        ephemeral_sk: Self::EphemeralSecretKey,
200        static_pk: &Self::PublicKey,
201    ) -> Result<Self::SharedSecret, super::KeyAgreementError> {
202        let shared = ephemeral_sk.diffie_hellman(static_pk);
203        if is_all_zero(shared.as_ref()) {
204            return Err(super::KeyAgreementError::InvalidSharedSecret);
205        }
206        Ok(shared)
207    }
208
209    fn exchange_static_ephemeral(
210        static_sk: &Self::SecretKey,
211        ephemeral_pk: &Self::EphemeralPublicKey,
212    ) -> Result<Self::SharedSecret, super::KeyAgreementError> {
213        let shared = static_sk.get_shared_secret(ephemeral_pk.clone());
214        if is_all_zero(shared.as_ref()) {
215            return Err(super::KeyAgreementError::InvalidSharedSecret);
216        }
217        Ok(shared)
218    }
219
220    fn extract_key_material(
221        shared_secret: &Self::SharedSecret,
222        length: usize,
223        info: &[u8],
224    ) -> Result<Vec<u8>, super::KeyAgreementError> {
225        let hkdf = shared_secret.extract(None);
226        let mut buf = vec![0_u8; length];
227        hkdf.expand(info, &mut buf)
228            .map_err(|_| super::KeyAgreementError::HkdfExpansionFailed)?;
229        Ok(buf)
230    }
231}
232
233fn is_all_zero(bytes: &[u8]) -> bool {
234    // Empty input is treated as invalid caller input rather than "all zero".
235    if bytes.is_empty() {
236        return false;
237    }
238    let acc = bytes.iter().fold(0u8, |acc, &byte| acc | byte);
239    acc.ct_eq(&0u8).into()
240}
241
242// TESTS
243// ================================================================================================
244
245#[cfg(test)]
246mod tests {
247    use curve25519_dalek::{constants::EIGHT_TORSION, montgomery::MontgomeryPoint};
248
249    use super::*;
250    use crate::{
251        dsa::eddsa_25519_sha512::KeyExchangeKey, ecdh::KeyAgreementError,
252        rand::test_utils::seeded_rng, utils::Deserializable,
253    };
254
255    #[test]
256    fn key_agreement() {
257        let mut rng = seeded_rng([0u8; 32]);
258
259        // 1. Generate the static key-pair for Alice
260        let sk = KeyExchangeKey::with_rng(&mut rng);
261        let pk = sk.public_key();
262
263        // 2. Generate the ephemeral key-pair for Bob
264        let sk_e = EphemeralSecretKey::with_rng(&mut rng);
265        let pk_e = sk_e.public_key();
266
267        // 3. Bob computes the shared secret key (Bob will send pk_e with the encrypted note to
268        //    Alice)
269        let shared_secret_key_1 = sk_e.diffie_hellman(&pk);
270
271        // 4. Alice uses its secret key and the ephemeral public key sent with the encrypted note by
272        //    Bob in order to create the shared secret key. This shared secret key will be used to
273        //    decrypt the encrypted note
274        let shared_secret_key_2 = sk.get_shared_secret(pk_e);
275
276        // Check that the computed shared secret keys are equal
277        assert_eq!(shared_secret_key_1.inner.to_bytes(), shared_secret_key_2.inner.to_bytes());
278    }
279
280    #[test]
281    fn ephemeral_public_key_rejects_small_order() {
282        let bytes = EIGHT_TORSION[1].to_montgomery().to_bytes();
283        let result = EphemeralPublicKey::read_from_bytes(&bytes);
284        assert!(result.is_err());
285    }
286
287    #[test]
288    fn ephemeral_public_key_rejects_twist_point() {
289        let bytes = find_twist_point_bytes();
290        let result = EphemeralPublicKey::read_from_bytes(&bytes);
291        assert!(result.is_err());
292    }
293
294    #[test]
295    fn exchange_static_ephemeral_rejects_zero_shared_secret() {
296        let mut rng = seeded_rng([0u8; 32]);
297        let static_sk = KeyExchangeKey::with_rng(&mut rng);
298
299        let low_order_bytes = EIGHT_TORSION[0].to_montgomery().to_bytes();
300        let low_order_pk = EphemeralPublicKey {
301            inner: x25519_dalek::PublicKey::from(low_order_bytes),
302        };
303
304        let result = X25519::exchange_static_ephemeral(&static_sk, &low_order_pk);
305        assert!(matches!(result, Err(KeyAgreementError::InvalidSharedSecret)));
306    }
307
308    #[test]
309    fn is_all_zero_accepts_arbitrary_lengths() {
310        assert!(!is_all_zero(&[]));
311        assert!(is_all_zero(&[0u8; 16]));
312        assert!(!is_all_zero(&[0u8, 1u8, 0u8, 0u8]));
313    }
314
315    fn find_twist_point_bytes() -> [u8; 32] {
316        let mut bytes = [0u8; 32];
317        for i in 0u16..=u16::MAX {
318            bytes[0] = (i & 0xff) as u8;
319            bytes[1] = (i >> 8) as u8;
320            if MontgomeryPoint(bytes).to_edwards(0).is_none() {
321                return bytes;
322            }
323        }
324        panic!("no twist point found in 16-bit search space");
325    }
326}