Skip to content

Commit f57179d

Browse files
committed
chacha20: adopt the new Generator API from rand_core
The new API force the output to be a straight array and does not allow the generator output to be ZerorizedOnDrop.
1 parent 0713373 commit f57179d

File tree

3 files changed

+10
-38
lines changed

3 files changed

+10
-38
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: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use core::fmt::Debug;
1010

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

1616
#[cfg(feature = "zeroize")]
@@ -146,35 +146,6 @@ pub type StreamId = U32x2;
146146
/// The arrays should be in little endian order.
147147
pub type BlockPos = U32x2;
148148

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-
178149
const BUFFER_SIZE: usize = 64;
179150

180151
// NB. this must remain consistent with some currently hard-coded numbers in this module
@@ -334,7 +305,7 @@ macro_rules! impl_chacha_rng {
334305
self.core.fill_bytes(dest)
335306
}
336307
}
337-
impl CryptoBlockRng for $ChaChaXCore {}
308+
impl CryptoGenerator for $ChaChaXCore {}
338309
impl CryptoRng for $ChaChaXRng {}
339310

340311
#[cfg(feature = "zeroize")]
@@ -535,13 +506,12 @@ macro_rules! impl_chacha_rng {
535506
}
536507
}
537508

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

542512
#[inline]
543-
fn generate(&mut self, r: &mut Self::Results) {
544-
self.0.generate(&mut r.0);
513+
fn generate(&mut self, r: &mut Self::Output) {
514+
self.0.generate(r);
545515
}
546516
}
547517
};

0 commit comments

Comments
 (0)