From 078bf89f358ae77354dc87883db35f4139571524 Mon Sep 17 00:00:00 2001 From: Arthur Gautier Date: Wed, 5 Nov 2025 18:40:25 -0800 Subject: [PATCH] chacha20: adopt the new Generator API from rand_core The new API force the output to be a straight array, but there is a custom drop implementation where we can put the Zeroize of the output. --- Cargo.lock | 3 +-- Cargo.toml | 3 +++ chacha20/src/rng.rs | 51 ++++++++++++--------------------------------- 3 files changed, 17 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25779110..0c15ee56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -288,8 +288,7 @@ dependencies = [ [[package]] name = "rand_core" version = "0.10.0-rc-2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "104a23e4e8b77312a823b6b5613edbac78397e2f34320bc7ac4277013ec4478e" +source = "git+https://github.com/rust-random/rand_core.git#77770a4eee8a4a1726fc264eb0366ca0e680bc29" [[package]] name = "rand_xorshift" diff --git a/Cargo.toml b/Cargo.toml index 26910c8c..6116d468 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,6 @@ members = [ [profile.dev] opt-level = 2 + +[patch.crates-io] +rand_core = { git = "https://github.com/rust-random/rand_core.git" } diff --git a/chacha20/src/rng.rs b/chacha20/src/rng.rs index 4c483442..eade9265 100644 --- a/chacha20/src/rng.rs +++ b/chacha20/src/rng.rs @@ -10,7 +10,7 @@ use core::fmt::Debug; use rand_core::{ CryptoRng, RngCore, SeedableRng, - block::{BlockRng, BlockRngCore, CryptoBlockRng}, + block::{BlockRng, CryptoGenerator, Generator}, }; #[cfg(feature = "zeroize")] @@ -146,35 +146,6 @@ pub type StreamId = U32x2; /// The arrays should be in little endian order. pub type BlockPos = U32x2; -/// The results buffer that zeroizes on drop when the `zeroize` feature is enabled. -#[derive(Clone)] -pub struct BlockRngResults([u32; BUFFER_SIZE]); - -impl AsRef<[u32]> for BlockRngResults { - fn as_ref(&self) -> &[u32] { - &self.0 - } -} - -impl AsMut<[u32]> for BlockRngResults { - fn as_mut(&mut self) -> &mut [u32] { - &mut self.0 - } -} - -impl Default for BlockRngResults { - fn default() -> Self { - Self([0u32; BUFFER_SIZE]) - } -} - -#[cfg(feature = "zeroize")] -impl Drop for BlockRngResults { - fn drop(&mut self) { - self.0.zeroize(); - } -} - const BUFFER_SIZE: usize = 64; // NB. this must remain consistent with some currently hard-coded numbers in this module @@ -323,18 +294,18 @@ macro_rules! impl_chacha_rng { impl RngCore for $ChaChaXRng { #[inline] fn next_u32(&mut self) -> u32 { - self.core.next_u32() + self.core.next_word() } #[inline] fn next_u64(&mut self) -> u64 { - self.core.next_u64() + self.core.next_u64_from_u32() } #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { self.core.fill_bytes(dest) } } - impl CryptoBlockRng for $ChaChaXCore {} + impl CryptoGenerator for $ChaChaXCore {} impl CryptoRng for $ChaChaXRng {} #[cfg(feature = "zeroize")] @@ -535,13 +506,17 @@ macro_rules! impl_chacha_rng { } } - impl BlockRngCore for $ChaChaXCore { - type Item = u32; - type Results = BlockRngResults; + impl Generator for $ChaChaXCore { + type Output = [u32; BUFFER_SIZE]; #[inline] - fn generate(&mut self, r: &mut Self::Results) { - self.0.generate(&mut r.0); + fn generate(&mut self, r: &mut Self::Output) { + self.0.generate(r); + } + + #[cfg(feature = "zeroize")] + fn drop(&mut self, output: &mut Self::Output) { + output.zeroize(); } } };