Skip to content

Commit 8dd5dfd

Browse files
committed
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.
1 parent 0713373 commit 8dd5dfd

File tree

3 files changed

+20
-42
lines changed

3 files changed

+20
-42
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ members = [
1010

1111
[profile.dev]
1212
opt-level = 2
13+
14+
[patch.crates-io]
15+
rand_core = { git = "https://github.com/rust-random/rand_core.git" }

chacha20/src/rng.rs

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
use core::fmt::Debug;
1010

1111
use rand_core::{
12+
block::{BlockRng, CryptoGenerator, Generator},
1213
CryptoRng, RngCore, SeedableRng,
13-
block::{BlockRng, BlockRngCore, CryptoBlockRng},
1414
};
1515

1616
#[cfg(feature = "zeroize")]
1717
use zeroize::{Zeroize, ZeroizeOnDrop};
1818

1919
use crate::{
20-
ChaChaCore, R8, R12, R20, Rounds, backends,
20+
backends,
2121
variants::{Legacy, Variant},
22+
ChaChaCore, Rounds, R12, R20, R8,
2223
};
2324

2425
use cfg_if::cfg_if;
@@ -146,35 +147,6 @@ pub type StreamId = U32x2;
146147
/// The arrays should be in little endian order.
147148
pub type BlockPos = U32x2;
148149

149-
/// The results buffer that zeroizes on drop when the `zeroize` feature is enabled.
150-
#[derive(Clone)]
151-
pub struct BlockRngResults([u32; BUFFER_SIZE]);
152-
153-
impl AsRef<[u32]> for BlockRngResults {
154-
fn as_ref(&self) -> &[u32] {
155-
&self.0
156-
}
157-
}
158-
159-
impl AsMut<[u32]> for BlockRngResults {
160-
fn as_mut(&mut self) -> &mut [u32] {
161-
&mut self.0
162-
}
163-
}
164-
165-
impl Default for BlockRngResults {
166-
fn default() -> Self {
167-
Self([0u32; BUFFER_SIZE])
168-
}
169-
}
170-
171-
#[cfg(feature = "zeroize")]
172-
impl Drop for BlockRngResults {
173-
fn drop(&mut self) {
174-
self.0.zeroize();
175-
}
176-
}
177-
178150
const BUFFER_SIZE: usize = 64;
179151

180152
// NB. this must remain consistent with some currently hard-coded numbers in this module
@@ -323,18 +295,18 @@ macro_rules! impl_chacha_rng {
323295
impl RngCore for $ChaChaXRng {
324296
#[inline]
325297
fn next_u32(&mut self) -> u32 {
326-
self.core.next_u32()
298+
self.core.next_word()
327299
}
328300
#[inline]
329301
fn next_u64(&mut self) -> u64 {
330-
self.core.next_u64()
302+
self.core.next_u64_from_u32()
331303
}
332304
#[inline]
333305
fn fill_bytes(&mut self, dest: &mut [u8]) {
334306
self.core.fill_bytes(dest)
335307
}
336308
}
337-
impl CryptoBlockRng for $ChaChaXCore {}
309+
impl CryptoGenerator for $ChaChaXCore {}
338310
impl CryptoRng for $ChaChaXRng {}
339311

340312
#[cfg(feature = "zeroize")]
@@ -535,13 +507,17 @@ macro_rules! impl_chacha_rng {
535507
}
536508
}
537509

538-
impl BlockRngCore for $ChaChaXCore {
539-
type Item = u32;
540-
type Results = BlockRngResults;
510+
impl Generator for $ChaChaXCore {
511+
type Output = [u32; BUFFER_SIZE];
541512

542513
#[inline]
543-
fn generate(&mut self, r: &mut Self::Results) {
544-
self.0.generate(&mut r.0);
514+
fn generate(&mut self, r: &mut Self::Output) {
515+
self.0.generate(r);
516+
}
517+
518+
#[cfg(feature = "zeroize")]
519+
fn drop(&mut self, output: &mut Self::Output) {
520+
output.zeroize();
545521
}
546522
}
547523
};
@@ -936,8 +912,8 @@ pub(crate) mod tests {
936912
/// Because this test uses `rand_chacha v0.3.1` which uses a 64-bit counter, these
937913
/// test results should be accurate up to `block_pos = 2^32 - 1`.
938914
fn test_fill_bytes_v2() {
939-
use rand_chacha::ChaCha20Rng as TesterRng;
940915
use rand_chacha::rand_core::{RngCore, SeedableRng};
916+
use rand_chacha::ChaCha20Rng as TesterRng;
941917

942918
let mut rng = ChaChaRng::from_seed([0u8; 32]);
943919
let mut tester_rng = TesterRng::from_seed([0u8; 32]);

0 commit comments

Comments
 (0)