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 enforces strong type safety and clear contracts for serialization, preventing common security vulnerabilities.
Required Associated Types§
Sourcetype PublicKey: Clone + Serialize
type PublicKey: Clone + Serialize
Public key type with appropriate constraints.
§Security Note
Implements Serialize
to guarantee safe from_bytes
and to_bytes
methods.
Sourcetype SecretKey: Zeroize + Clone + SerializeSecret
type SecretKey: Zeroize + Clone + SerializeSecret
Secret key type with security guarantees.
§Security Note
- Implements
Zeroize
for secure memory cleanup. - Implements
SerializeSecret
to guarantee safefrom_bytes
andto_bytes_zeroizing
methods.
Shared secret type with security guarantees.
§Security Note
- Implements
Zeroize
for secure memory cleanup. - Implements
SerializeSecret
for secure serialization. - 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 safe from_bytes
and to_bytes
methods.
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 zeroized 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 be resistant to side-channel attacks.
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 be constant-time.
- 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", so this trait is not object safe.