1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use rand_core::{CryptoRng, RngCore};
pub trait Random: CryptoRng + RngCore + Send + Sync {}
#[cfg(feature = "kem")]
pub trait Kem: Send + Sync {
/// The string that the Noise spec defines for the primitive.
fn name(&self) -> &'static str;
/// The length in bytes of a public key for this primitive.
fn pub_len(&self) -> usize;
/// The length in bytes the Kem cipherthext for this primitive.
fn ciphertext_len(&self) -> usize;
/// Shared secret length in bytes that this Kem encapsulates.
fn shared_secret_len(&self) -> usize;
/// Generate a new private key.
fn generate(&mut self, rng: &mut dyn Random);
/// Get the public key
fn pubkey(&self) -> &[u8];
/// Generate a shared secret and encapsulate it using this Kem.
#[must_use]
fn encapsulate(
&self,
pubkey: &[u8],
shared_secret_out: &mut [u8],
ciphertext_out: &mut [u8],
rng: &mut dyn Random
) -> Result<(usize, usize), ()>;
/// Decapsulate a ciphertext producing a shared secret.
#[must_use]
fn decapsulate(&self, ciphertext: &[u8], shared_secret_out: &mut [u8]) -> Result<usize, ()>;
}