quantaegis_core/kem.rs
1//! Key Encapsulation Mechanism (KEM) trait abstraction.
2//!
3//! Designed to support both pure post-quantum KEMs (e.g., FIPS 203 ML-KEM) and hybrid KEMs
4//! (e.g., `X25519MLKEM768`) behind a uniform, crypto-agile interface.
5
6use crate::error::Result;
7use crate::rng::CryptoRng;
8
9/// Uniform interface for Key Encapsulation Mechanisms (KEMs).
10///
11/// # Secret-Output Staging Note
12/// Returning secret keys or shared secrets by value (`-> Result<...>`) in safe Rust can leave an
13/// unzeroized copy in a callee's return staging slot when moved into the caller's return place.
14/// Zeroization-critical callers (including `quantaegis-fips`) should prefer concrete in-place
15/// `keygen_into`, `encaps_into`, and `decaps_into` methods where provided by the parameter set.
16pub trait Kem {
17 /// Public encapsulation key type.
18 type EncapsulationKey;
19 /// Private decapsulation key type (must zeroize on drop).
20 type DecapsulationKey;
21 /// Encapsulated ciphertext type.
22 type Ciphertext;
23 /// Established shared secret type (must zeroize on drop).
24 type SharedSecret;
25
26 /// Generates a fresh `(EncapsulationKey, DecapsulationKey)` key pair using `rng`.
27 ///
28 /// For zeroization-critical callers avoiding safe-Rust return-slot staging copies of the
29 /// decapsulation key, prefer the concrete parameter set's `keygen_into` method.
30 fn keygen<R: CryptoRng + ?Sized>(
31 rng: &mut R,
32 ) -> Result<(Self::EncapsulationKey, Self::DecapsulationKey)>;
33
34 /// Encapsulates a fresh shared secret to `ek` using randomness from `rng`.
35 ///
36 /// For zeroization-critical callers avoiding safe-Rust return-slot staging copies of the
37 /// shared secret, prefer the concrete parameter set's `encaps_into` method.
38 fn encaps<R: CryptoRng + ?Sized>(
39 ek: &Self::EncapsulationKey,
40 rng: &mut R,
41 ) -> Result<(Self::Ciphertext, Self::SharedSecret)>;
42
43 /// Decapsulates `ct` using `dk`.
44 ///
45 /// For IND-CCA2 KEMs such as ML-KEM, decapsulation of a valid-length but tampered ciphertext
46 /// succeeds via constant-time implicit rejection rather than returning an error.
47 ///
48 /// For zeroization-critical callers avoiding safe-Rust return-slot staging copies of the
49 /// shared secret, prefer the concrete parameter set's `decaps_into` method.
50 fn decaps(dk: &Self::DecapsulationKey, ct: &Self::Ciphertext) -> Result<Self::SharedSecret>;
51}