pub trait Kem {
type PublicKey: Clone + Serialize;
type SecretKey: Zeroize + Clone + SerializeSecret;
type SharedSecret: Zeroize + Clone + SerializeSecret;
type Ciphertext: Clone + Serialize;
type KeyPair: Clone;
// Required methods
fn name() -> &'static str;
fn keypair<R: CryptoRng + RngCore>(rng: &mut R) -> Result<Self::KeyPair>;
fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey;
fn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey;
fn encapsulate<R: CryptoRng + RngCore>(
rng: &mut R,
public_key: &Self::PublicKey,
) -> Result<(Self::Ciphertext, Self::SharedSecret)>;
fn decapsulate(
secret_key: &Self::SecretKey,
ciphertext: &Self::Ciphertext,
) -> Result<Self::SharedSecret>;
}Expand description
Trait for Key Encapsulation Mechanism (KEM) with domain-specific types.
§Security Design
This trait defines distinct types and explicit serialization contracts for KEM inputs and outputs. Implementations remain responsible for validating encodings and meeting their algorithm-specific security requirements.
Required Associated Types§
Sourcetype PublicKey: Clone + Serialize
type PublicKey: Clone + Serialize
Public key type with appropriate constraints.
§Security Note
Implements Serialize for the public encoding boundary.
Sourcetype SecretKey: Zeroize + Clone + SerializeSecret
type SecretKey: Zeroize + Clone + SerializeSecret
Secret key type with an explicit clearing and serialization contract.
§Security Note
- Implements
Zeroizefor best-effort clearing of owned initialized storage. - Implements
SerializeSecretfor exact-size protected exports.
Shared-secret type with an explicit clearing and serialization contract.
§Security Note
- Implements
Zeroizefor best-effort clearing of owned initialized storage. - Implements
SerializeSecretfor exact-size protected exports. - Should be converted to application keys immediately after generation.
Sourcetype Ciphertext: Clone + Serialize
type Ciphertext: Clone + Serialize
Ciphertext type for the encapsulated key.
§Security Note
Implements Serialize for the public encoding boundary.
Required Methods§
Sourcefn keypair<R: CryptoRng + RngCore>(rng: &mut R) -> Result<Self::KeyPair>
fn keypair<R: CryptoRng + RngCore>(rng: &mut R) -> Result<Self::KeyPair>
Generate a new keypair.
§Security Requirements
- Must use the provided CSPRNG for all randomness.
- Keys must be generated according to the algorithm specification.
Sourcefn public_key(keypair: &Self::KeyPair) -> Self::PublicKey
fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey
Extract public key from keypair.
Sourcefn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey
fn secret_key(keypair: &Self::KeyPair) -> Self::SecretKey
Extract secret key from keypair.
§Security Note
The returned secret key should be protected and explicitly cleared after use.
Sourcefn encapsulate<R: CryptoRng + RngCore>(
rng: &mut R,
public_key: &Self::PublicKey,
) -> Result<(Self::Ciphertext, Self::SharedSecret)>
fn encapsulate<R: CryptoRng + RngCore>( rng: &mut R, public_key: &Self::PublicKey, ) -> Result<(Self::Ciphertext, Self::SharedSecret)>
Encapsulate a shared secret using the recipient’s public key.
§Security Requirements
- Must validate the public key internally.
- Must use fresh randomness from the provided RNG.
- Must not intentionally branch or index memory on secret material. Concrete compiler/target behavior requires separate validation.
Sourcefn decapsulate(
secret_key: &Self::SecretKey,
ciphertext: &Self::Ciphertext,
) -> Result<Self::SharedSecret>
fn decapsulate( secret_key: &Self::SecretKey, ciphertext: &Self::Ciphertext, ) -> Result<Self::SharedSecret>
Decapsulate a shared secret using the private key.
§Security Requirements
- Must not intentionally branch or index memory on secret material. Concrete compiler/target behavior requires separate validation.
- Should use implicit rejection for IND-CCA2 security where applicable.
- Must not leak information about the secret key.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".