Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding zeroize for BlockRng and BlockRng64 #1374

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rand_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ serde1 = ["serde"] # enables serde for BlockRng wrapper
serde = { version = "1", features = ["derive"], optional = true }
getrandom = { version = "0.2", optional = true }
zerocopy = { version = "0.7.20", default-features = false }
zeroize = { version = "1.7", optional = true }
22 changes: 22 additions & 0 deletions rand_core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ use core::convert::AsRef;
use core::fmt;
#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "zeroize")]
use zeroize::{DefaultIsZeroes, Zeroize};

/// A trait for RNGs which do not generate random numbers individually, but in
/// blocks (typically `[u32; N]`). This technique is commonly used by
Expand All @@ -67,7 +69,11 @@ use serde::{Deserialize, Serialize};
/// See the [module][crate::block] documentation for details.
pub trait BlockRngCore {
/// Results element type, e.g. `u32`.
#[cfg(not(feature = "zeroize"))]
type Item;
/// Results element type, e.g. `u32`.
#[cfg(feature = "zeroize")]
type Item: DefaultIsZeroes;
Comment on lines 71 to +76
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has potential to cause unexpected broken builds.


/// Results type. This is the 'block' an RNG implementing `BlockRngCore`
/// generates, which will usually be an array like `[u32; 16]`.
Expand Down Expand Up @@ -262,6 +268,14 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
}
}

#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl<R: BlockRngCore> Zeroize for BlockRng<R> {
fn zeroize(&mut self) {
self.results.as_mut().zeroize()
}
}

impl<R: CryptoBlockRng + BlockRngCore<Item = u32>> CryptoRng for BlockRng<R> {}

/// A wrapper type implementing [`RngCore`] for some type implementing
Expand Down Expand Up @@ -430,6 +444,14 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {
}
}

#[cfg(feature = "zeroize")]
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))]
impl<R: BlockRngCore> Zeroize for BlockRng64<R> {
fn zeroize(&mut self) {
self.results.as_mut().zeroize()
}
}

impl<R: CryptoBlockRng + BlockRngCore<Item = u64>> CryptoRng for BlockRng64<R> {}

#[cfg(test)]
Expand Down
Loading