pub use encoding::digest;
pub use rand_core;
#[cfg(feature = "mlkem768x25519-sha256")]
pub mod mlkem768x25519;
#[cfg(feature = "mlkem768x25519-sha256")]
pub use mlkem768x25519::MlKem768X25519;
use core::fmt;
use digest::{Digest, Output};
use encoding::{Decode, DigestWriter, Encode};
use rand_core::CryptoRng;
pub trait Kex: Sized {
const NAME: &'static str;
type Hash: Digest;
type Init: Decode<Error = encoding::Error> + Encode;
type Reply: Decode<Error = encoding::Error> + Encode;
type Secret: AsRef<[u8]> + Encode;
type Error: core::error::Error;
fn start<R: CryptoRng + ?Sized>(rng: &mut R) -> Result<(Self, Self::Init), Self::Error>;
fn respond<R: CryptoRng + ?Sized>(
rng: &mut R,
init: &Self::Init,
) -> Result<(Self::Reply, Self::Secret), Self::Error>;
fn finish(self, reply: &Self::Reply) -> Result<Self::Secret, Self::Error>;
}
pub struct Transcript<K: Kex>(K::Hash);
impl<K: Kex> Transcript<K> {
pub fn new(
client_id: &[u8],
server_id: &[u8],
client_kexinit: &[u8],
server_kexinit: &[u8],
) -> encoding::Result<Self> {
let mut hash = K::Hash::new();
let mut writer = DigestWriter(&mut hash);
for field in [client_id, server_id, client_kexinit, server_kexinit] {
field.encode(&mut writer)?;
}
Ok(Self(hash))
}
pub fn finish(
mut self,
server_host_key: &[u8],
init: &K::Init,
reply: &K::Reply,
secret: &K::Secret,
) -> encoding::Result<Output<K::Hash>> {
let mut writer = DigestWriter(&mut self.0);
server_host_key.encode(&mut writer)?;
init.encode(&mut writer)?;
reply.encode(&mut writer)?;
secret.encode(&mut writer)?;
Ok(self.0.finalize())
}
}
impl<K: Kex> fmt::Debug for Transcript<K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Transcript")
.field("method", &K::NAME)
.finish_non_exhaustive()
}
}