Skip to main content

Kem

Trait Kem 

Source
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§

Source

type PublicKey: Clone + Serialize

Public key type with appropriate constraints.

§Security Note

Implements Serialize to guarantee safe from_bytes and to_bytes methods.

Source

type SecretKey: Zeroize + Clone + SerializeSecret

Secret key type with security guarantees.

§Security Note
  • Implements Zeroize for secure memory cleanup.
  • Implements SerializeSecret to guarantee safe from_bytes and to_bytes_zeroizing methods.
Source

type SharedSecret: Zeroize + Clone + SerializeSecret

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.
Source

type Ciphertext: Clone + Serialize

Ciphertext type for the encapsulated key.

§Security Note

Implements Serialize for safe from_bytes and to_bytes methods.

Source

type KeyPair: Clone

Keypair type for efficient storage of related keys. It is an intermediate type and does not require a serialization contract itself.

Required Methods§

Source

fn name() -> &'static str

Returns the KEM algorithm name.

Source

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.
Source

fn public_key(keypair: &Self::KeyPair) -> Self::PublicKey

Extract public key from keypair.

Source

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.

Source

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.
Source

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".

Implementors§