Skip to content

Commit 078bf89

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 078bf89

File tree

3 files changed

+17
-40
lines changed

3 files changed

+17
-40
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: 13 additions & 38 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
@@ -323,18 +294,18 @@ macro_rules! impl_chacha_rng {
323294
impl RngCore for $ChaChaXRng {
324295
#[inline]
325296
fn next_u32(&mut self) -> u32 {
326-
self.core.next_u32()
297+
self.core.next_word()
327298
}
328299
#[inline]
329300
fn next_u64(&mut self) -> u64 {
330-
self.core.next_u64()
301+
self.core.next_u64_from_u32()
331302
}
332303
#[inline]
333304
fn fill_bytes(&mut self, dest: &mut [u8]) {
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,17 @@ 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);
515+
}
516+
517+
#[cfg(feature = "zeroize")]
518+
fn drop(&mut self, output: &mut Self::Output) {
519+
output.zeroize();
545520
}
546521
}
547522
};

0 commit comments

Comments
 (0)