use std::sync::{Arc, Mutex};
use crate::{
ActiveKeyExchange, AeadAlgorithm, AeadCipher, BlockCipherAlgorithm, CbcAlgorithm, CbcCipher,
CryptoAlgorithm, CryptoError, HashAlgorithm, HmacAlgorithm, KeyExchangeAlgorithm, Mac,
PublicKey, RTCCrypto, RTCCryptoProvider, RTCRandom, SecretVec, SignatureScheme, SigningKey,
StreamCipher, StreamCipherAlgorithm,
};
struct SeededRandom(Mutex<u64>);
impl RTCRandom for SeededRandom {
fn fill(&self, output: &mut [u8]) -> Result<(), CryptoError> {
let mut state = self.0.lock().map_err(|_| CryptoError::RandomnessFailed)?;
for chunk in output.chunks_mut(8) {
*state = state.wrapping_add(0x9e37_79b9_7f4a_7c15);
let mut z = *state;
z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
z ^= z >> 31;
chunk.copy_from_slice(&z.to_le_bytes()[..chunk.len()]);
}
Ok(())
}
}
struct FixedKeyExchange {
algorithm: KeyExchangeAlgorithm,
public_key: Vec<u8>,
}
impl FixedKeyExchange {
fn new(algorithm: KeyExchangeAlgorithm) -> Self {
let public_key = match algorithm {
KeyExchangeAlgorithm::X25519 => vec![0x09; 32],
KeyExchangeAlgorithm::P256 => [&[0x04][..], &[0x11; 64][..]].concat(),
KeyExchangeAlgorithm::P384 => [&[0x04][..], &[0x22; 96][..]].concat(),
KeyExchangeAlgorithm::X25519MlKem768 => vec![0x33; 1216],
};
Self {
algorithm,
public_key,
}
}
}
impl ActiveKeyExchange for FixedKeyExchange {
fn algorithm(&self) -> KeyExchangeAlgorithm {
self.algorithm
}
fn public_key(&self) -> &[u8] {
&self.public_key
}
fn complete(self: Box<Self>, peer_public_key: &[u8]) -> Result<SecretVec, CryptoError> {
if peer_public_key.is_empty() {
return Err(CryptoError::InvalidPublicKey);
}
let len = match self.algorithm {
KeyExchangeAlgorithm::P384 => 48,
KeyExchangeAlgorithm::X25519MlKem768 => 64,
_ => 32,
};
Ok(SecretVec::new(vec![0x5a; len]))
}
}
fn fixed_signature(inner: &dyn RTCCrypto, message: &[u8]) -> Result<Vec<u8>, CryptoError> {
let digest = inner.hash(HashAlgorithm::Sha256, message)?;
Ok([digest.as_slice(), digest.as_slice()].concat())
}
struct FixedSigningKey {
inner: Arc<dyn SigningKey>,
provider: Arc<dyn RTCCryptoProvider>,
}
impl SigningKey for FixedSigningKey {
fn supports(&self, scheme: SignatureScheme) -> bool {
self.inner.supports(scheme)
}
fn public_key(&self) -> PublicKey<'_> {
self.inner.public_key()
}
fn sign(&self, _scheme: SignatureScheme, message: &[u8]) -> Result<Vec<u8>, CryptoError> {
fixed_signature(self.provider.crypto(), message)
}
fn to_pkcs8_der(&self) -> Result<Option<SecretVec>, CryptoError> {
self.inner.to_pkcs8_der()
}
}
struct DeterministicCrypto {
inner: Arc<dyn RTCCryptoProvider>,
fixed: bool,
}
impl RTCCrypto for DeterministicCrypto {
fn supports(&self, algorithm: CryptoAlgorithm) -> bool {
self.inner.crypto().supports(algorithm)
}
fn hash(&self, algorithm: HashAlgorithm, data: &[u8]) -> Result<Vec<u8>, CryptoError> {
self.inner.crypto().hash(algorithm, data)
}
fn block_encrypt(
&self,
algorithm: BlockCipherAlgorithm,
key: &[u8],
block: &mut [u8],
) -> Result<(), CryptoError> {
self.inner.crypto().block_encrypt(algorithm, key, block)
}
fn new_hmac(&self, algorithm: HmacAlgorithm, key: &[u8]) -> Result<Box<dyn Mac>, CryptoError> {
self.inner.crypto().new_hmac(algorithm, key)
}
fn new_stream_cipher(
&self,
algorithm: StreamCipherAlgorithm,
key: &[u8],
) -> Result<Box<dyn StreamCipher>, CryptoError> {
self.inner.crypto().new_stream_cipher(algorithm, key)
}
fn new_aead(
&self,
algorithm: AeadAlgorithm,
key: &[u8],
) -> Result<Box<dyn AeadCipher>, CryptoError> {
self.inner.crypto().new_aead(algorithm, key)
}
fn new_cbc(
&self,
algorithm: CbcAlgorithm,
key: &[u8],
) -> Result<Box<dyn CbcCipher>, CryptoError> {
self.inner.crypto().new_cbc(algorithm, key)
}
fn new_header_mask(
&self,
algorithm: crate::HeaderMaskAlgorithm,
key: &[u8],
) -> Result<Box<dyn crate::HeaderMask>, CryptoError> {
self.inner.crypto().new_header_mask(algorithm, key)
}
fn start_key_exchange(
&self,
algorithm: KeyExchangeAlgorithm,
) -> Result<Box<dyn ActiveKeyExchange>, CryptoError> {
if self.fixed {
Ok(Box::new(FixedKeyExchange::new(algorithm)))
} else {
self.inner.crypto().start_key_exchange(algorithm)
}
}
fn generate_signing_key(
&self,
scheme: SignatureScheme,
) -> Result<Arc<dyn SigningKey>, CryptoError> {
self.inner.crypto().generate_signing_key(scheme)
}
fn import_signing_key(
&self,
scheme: SignatureScheme,
pkcs8_der: &[u8],
) -> Result<Arc<dyn SigningKey>, CryptoError> {
let key = self.inner.crypto().import_signing_key(scheme, pkcs8_der)?;
if self.fixed {
Ok(Arc::new(FixedSigningKey {
inner: key,
provider: self.inner.clone(),
}))
} else {
Ok(key)
}
}
fn verify_signature(
&self,
scheme: SignatureScheme,
public_key: PublicKey<'_>,
message: &[u8],
signature: &[u8],
) -> Result<(), CryptoError> {
if !self.fixed {
return self
.inner
.crypto()
.verify_signature(scheme, public_key, message, signature);
}
if fixed_signature(self.inner.crypto(), message)? == signature {
Ok(())
} else {
Err(CryptoError::InvalidSignature)
}
}
}
pub struct DeterministicProvider {
crypto: DeterministicCrypto,
random: SeededRandom,
}
impl DeterministicProvider {
pub fn new(inner: Arc<dyn RTCCryptoProvider>, seed: u64, fixed: bool) -> Self {
Self {
crypto: DeterministicCrypto { inner, fixed },
random: SeededRandom(Mutex::new(seed)),
}
}
}
impl RTCCryptoProvider for DeterministicProvider {
fn name(&self) -> &'static str {
"deterministic-fuzzing"
}
fn crypto(&self) -> &dyn RTCCrypto {
&self.crypto
}
fn random(&self) -> &dyn RTCRandom {
&self.random
}
}
pub fn deterministic_provider(
seed: u64,
fixed: bool,
) -> Result<Arc<dyn RTCCryptoProvider>, CryptoError> {
Ok(Arc::new(DeterministicProvider::new(
crate::default_provider()?,
seed,
fixed,
)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn same_seed_same_bytes() {
let a = deterministic_provider(7, false).unwrap();
let b = deterministic_provider(7, false).unwrap();
let (mut x, mut y) = ([0u8; 37], [0u8; 37]);
a.random().fill(&mut x).unwrap();
b.random().fill(&mut y).unwrap();
assert_eq!(x, y);
assert_ne!(x, [0u8; 37]);
}
#[test]
fn fixed_key_exchange_agrees() {
let p = deterministic_provider(1, true).unwrap();
let a = p
.crypto()
.start_key_exchange(KeyExchangeAlgorithm::X25519)
.unwrap();
let b = p
.crypto()
.start_key_exchange(KeyExchangeAlgorithm::X25519)
.unwrap();
let (pa, pb) = (a.public_key().to_vec(), b.public_key().to_vec());
assert_eq!(
a.complete(&pb).unwrap().as_ref(),
b.complete(&pa).unwrap().as_ref()
);
}
}