serai/crypto/transcript/src/lib.rs

117 lines
3.6 KiB
Rust
Raw Normal View History

2022-09-29 08:47:55 +00:00
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![no_std]
#[cfg(feature = "merlin")]
mod merlin;
#[cfg(feature = "merlin")]
pub use crate::merlin::MerlinTranscript;
2023-03-02 15:57:22 +00:00
use digest::{
typenum::{
consts::U32, marker_traits::NonZero, type_operators::IsGreaterOrEqual, operator_aliases::GrEq,
},
Digest, Output, HashMarker,
};
pub trait Transcript {
type Challenge: Clone + Send + Sync + AsRef<[u8]>;
2022-09-29 09:25:29 +00:00
/// Create a new transcript with the specified name.
fn new(name: &'static [u8]) -> Self;
2022-09-29 09:25:29 +00:00
/// Apply a domain separator to the transcript.
fn domain_separate(&mut self, label: &'static [u8]);
2022-07-09 04:37:39 +00:00
2022-09-29 09:25:29 +00:00
/// Append a message to the transcript.
fn append_message<M: AsRef<[u8]>>(&mut self, label: &'static [u8], message: M);
2022-07-09 04:37:39 +00:00
/// Produce a challenge. This MUST update the transcript as it does so, preventing the same
2022-09-29 09:25:29 +00:00
/// challenge from being generated multiple times.
fn challenge(&mut self, label: &'static [u8]) -> Self::Challenge;
2022-07-09 04:37:39 +00:00
/// Produce a RNG seed. Helper function for parties needing to generate random data from an
/// agreed upon state. Internally calls the challenge function for the needed bytes, converting
2022-09-29 09:25:29 +00:00
/// them to the seed format rand_core expects.
fn rng_seed(&mut self, label: &'static [u8]) -> [u8; 32];
}
enum DigestTranscriptMember {
Name,
Domain,
Label,
Value,
2022-07-15 05:26:07 +00:00
Challenge,
}
impl DigestTranscriptMember {
fn as_u8(&self) -> u8 {
match self {
DigestTranscriptMember::Name => 0,
DigestTranscriptMember::Domain => 1,
DigestTranscriptMember::Label => 2,
DigestTranscriptMember::Value => 3,
2022-07-15 05:26:07 +00:00
DigestTranscriptMember::Challenge => 4,
}
}
}
2022-09-29 09:25:29 +00:00
/// A trait defining cryptographic Digests with at least a 256-byte output size, assuming at least
/// a 128-bit level of security accordingly.
pub trait SecureDigest: Digest + HashMarker {}
2023-03-02 15:57:22 +00:00
impl<D: Digest + HashMarker> SecureDigest for D
where
// This just lets us perform the comparison
D::OutputSize: IsGreaterOrEqual<U32>,
// Perform the comparison and make sure it's true (not zero), meaning D::OutputSize is >= U32
// This should be U32 as it's length in bytes, not bits
GrEq<D::OutputSize, U32>: NonZero,
{
}
2022-09-29 09:25:29 +00:00
/// A simple transcript format constructed around the specified hash algorithm.
#[derive(Clone, Debug)]
pub struct DigestTranscript<D: Clone + SecureDigest>(D);
impl<D: Clone + SecureDigest> DigestTranscript<D> {
fn append(&mut self, kind: DigestTranscriptMember, value: &[u8]) {
2022-09-05 01:23:38 +00:00
self.0.update([kind.as_u8()]);
// Assumes messages don't exceed 16 exabytes
self.0.update(u64::try_from(value.len()).unwrap().to_le_bytes());
self.0.update(value);
}
}
impl<D: Clone + SecureDigest> Transcript for DigestTranscript<D> {
type Challenge = Output<D>;
fn new(name: &'static [u8]) -> Self {
let mut res = DigestTranscript(D::new());
res.append(DigestTranscriptMember::Name, name);
res
2022-05-06 11:33:08 +00:00
}
fn domain_separate(&mut self, label: &'static [u8]) {
self.append(DigestTranscriptMember::Domain, label);
}
fn append_message<M: AsRef<[u8]>>(&mut self, label: &'static [u8], message: M) {
self.append(DigestTranscriptMember::Label, label);
self.append(DigestTranscriptMember::Value, message.as_ref());
}
fn challenge(&mut self, label: &'static [u8]) -> Self::Challenge {
self.append(DigestTranscriptMember::Challenge, label);
self.0.clone().finalize()
}
fn rng_seed(&mut self, label: &'static [u8]) -> [u8; 32] {
let mut seed = [0; 32];
seed.copy_from_slice(&self.challenge(label)[.. 32]);
2022-05-06 11:33:08 +00:00
seed
}
}
/// The recommended transcript, secure against length-extension attacks.
#[cfg(feature = "recommended")]
pub type RecommendedTranscript = DigestTranscript<blake2::Blake2b512>;