test_foo_bar_traits/kem.rs
1//! This module provides a common interface trait for key
2//! encapsulation mechanisms (KEMs).
3
4pub mod arrayref;
5pub mod owned;
6pub mod slice;
7
8#[cfg(feature = "generic-tests")]
9pub mod tests;
10
11use rand::CryptoRng;
12
13/// A KEM keypair.
14pub type KeyPair<DK, EK> = (DK, EK);
15
16/// Errors during KEM operations.
17#[derive(Debug)]
18pub enum KEMError {
19 /// An error that occurred during key generation.
20 KeyGeneration,
21 /// An error that occurred during encapsulation.
22 Encapsulation,
23 /// An error that occurred during decapsulation.
24 Decapsulation,
25}
26
27/// This trait captures the required interface of a key encapsulation
28/// mechanism (KEM).
29pub trait KEM {
30 /// The KEM's ciphertext.
31 type Ciphertext;
32 /// The KEM's shared secret.
33 type SharedSecret;
34 /// The KEM's encapsulation key.
35 type EncapsulationKey;
36 /// The KEM's decapsulation key.
37 type DecapsulationKey;
38
39 /// Generate a pair of encapsulation and decapsulation keys.
40 fn generate_key_pair(
41 rng: &mut impl CryptoRng,
42 ) -> Result<KeyPair<Self::DecapsulationKey, Self::EncapsulationKey>, KEMError>;
43
44 /// Encapsulate a shared secret towards a given encapsulation key.
45 fn encapsulate(
46 ek: &Self::EncapsulationKey,
47 rng: &mut impl CryptoRng,
48 ) -> Result<(Self::SharedSecret, Self::Ciphertext), KEMError>;
49
50 /// Decapsulate a shared secret.
51 fn decapsulate(
52 dk: &Self::DecapsulationKey,
53 ctxt: &Self::Ciphertext,
54 ) -> Result<Self::SharedSecret, KEMError>;
55}