kem/
lib.rs

1#![doc = include_str!("../README.md")]
2#![no_std]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4#![doc(
5    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
6    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
7)]
8#![forbid(unsafe_code)]
9#![warn(missing_docs, unused_qualifications, missing_debug_implementations)]
10
11pub use crypto_common::{KeyInit, KeySizeUser, typenum::consts};
12
13use core::fmt::Debug;
14use rand_core::TryCryptoRng;
15
16/// A value that can be encapsulated to. Often, this will just be a public key. However, it can
17/// also be a bundle of public keys, or it can include a sender's private key for authenticated
18/// encapsulation.
19pub trait Encapsulate<EK, SS> {
20    /// Encapsulation error
21    type Error: Debug;
22
23    /// Encapsulates a fresh shared secret
24    fn encapsulate<R: TryCryptoRng + ?Sized>(&self, rng: &mut R) -> Result<(EK, SS), Self::Error>;
25}
26
27/// A value that can be used to decapsulate an encapsulated key.
28///
29/// Often, this will just be a secret key. But, as with [`Encapsulate`], it can be a bundle
30/// of secret keys, or it can include a sender's private key for authenticated encapsulation.
31pub trait Decapsulate<EK, SS> {
32    /// Encapsulator which corresponds to this decapsulator.
33    type Encapsulator: Encapsulate<EK, SS>;
34
35    /// Decapsulation error
36    type Error: Debug;
37
38    /// Decapsulates the given encapsulated key
39    fn decapsulate(&self, encapsulated_key: &EK) -> Result<SS, Self::Error>;
40
41    /// Retrieve the encapsulator associated with this decapsulator.
42    fn encapsulator(&self) -> Self::Encapsulator;
43}