#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
)]
mod expander;
pub use expander::{Expander, InvalidLength};
pub use kem::{self, Decapsulator, Encapsulate, Generate, Kem, TryDecapsulate};
#[cfg(feature = "ecdh")]
mod ecdh_kem;
#[cfg(feature = "x25519")]
mod x25519_kem;
#[cfg(feature = "ecdh")]
pub use ecdh_kem::{EcdhDecapsulationKey, EcdhEncapsulationKey, EcdhKem};
#[cfg(feature = "x25519")]
pub use x25519_kem::{X25519DecapsulationKey, X25519EncapsulationKey, X25519Kem};
#[cfg(feature = "ecdh")]
use elliptic_curve::{
CurveArithmetic, PublicKey, bigint,
sec1::{self, FromSec1Point, ToSec1Point},
};
#[cfg(feature = "zeroize")]
use zeroize::{Zeroize, ZeroizeOnDrop};
#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Default)]
pub struct DecapsulationKey<DK, EK> {
dk: DK,
ek: EncapsulationKey<EK>,
}
impl<DK, EK> DecapsulationKey<DK, EK> {
pub fn into_inner(self) -> DK {
self.dk
}
}
impl<DK, EK> AsRef<EncapsulationKey<EK>> for DecapsulationKey<DK, EK> {
fn as_ref(&self) -> &EncapsulationKey<EK> {
&self.ek
}
}
impl<DK, EK> From<DK> for DecapsulationKey<DK, EK>
where
EK: for<'a> From<&'a DK>,
{
fn from(dk: DK) -> Self {
let ek = EncapsulationKey(EK::from(&dk));
Self { dk, ek }
}
}
#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Default)]
pub struct EncapsulationKey<EK>(EK);
impl<EK> EncapsulationKey<EK> {
pub fn into_inner(self) -> EK {
self.0
}
}
impl<EK> From<EK> for EncapsulationKey<EK> {
fn from(inner: EK) -> Self {
Self(inner)
}
}
#[cfg(feature = "ecdh")]
impl<C> FromSec1Point<C> for EcdhEncapsulationKey<C>
where
C: CurveArithmetic,
C::FieldBytesSize: sec1::ModulusSize,
PublicKey<C>: FromSec1Point<C>,
{
fn from_sec1_point(point: &sec1::Sec1Point<C>) -> bigint::CtOption<Self> {
PublicKey::<C>::from_sec1_point(point).map(Into::into)
}
}
#[cfg(feature = "ecdh")]
impl<C> ToSec1Point<C> for EcdhEncapsulationKey<C>
where
C: CurveArithmetic,
C::FieldBytesSize: sec1::ModulusSize,
PublicKey<C>: ToSec1Point<C>,
{
fn to_sec1_point(&self, compress: bool) -> sec1::Sec1Point<C> {
self.0.to_sec1_point(compress)
}
}
#[cfg(feature = "zeroize")]
impl<DK: Zeroize, EK> Zeroize for DecapsulationKey<DK, EK> {
fn zeroize(&mut self) {
self.dk.zeroize();
}
}
#[cfg(feature = "zeroize")]
impl<DK: ZeroizeOnDrop, EK> ZeroizeOnDrop for DecapsulationKey<DK, EK> {}
#[cfg(feature = "p256")]
pub type NistP256Kem = EcdhKem<p256::NistP256>;
#[cfg(feature = "p256")]
pub type NistP256DecapsulationKey = EcdhDecapsulationKey<p256::NistP256>;
#[cfg(feature = "p256")]
pub type NistP256EncapsulationKey = EcdhEncapsulationKey<p256::NistP256>;
#[cfg(feature = "p384")]
pub type NistP384Kem = EcdhKem<p384::NistP384>;
#[cfg(feature = "p384")]
pub type NistP384DecapsulationKey = EcdhDecapsulationKey<p384::NistP384>;
#[cfg(feature = "p384")]
pub type NistP384EncapsulationKey = EcdhEncapsulationKey<p384::NistP384>;
#[cfg(feature = "p521")]
pub type NistP521Kem = EcdhKem<p521::NistP521>;
#[cfg(feature = "p521")]
pub type NistP521DecapsulationKey = EcdhDecapsulationKey<p521::NistP521>;
#[cfg(feature = "p521")]
pub type NistP521EncapsulationKey = EcdhEncapsulationKey<p521::NistP521>;
#[cfg(feature = "p521")]
pub type Secp256k1Kem = EcdhKem<k256::Secp256k1>;
#[cfg(feature = "k256")]
pub type Secp256k1DecapsulationKey = EcdhDecapsulationKey<k256::Secp256k1>;
#[cfg(feature = "k256")]
pub type Secp256k1EncapsulationKey = EcdhEncapsulationKey<k256::Secp256k1>;