diff --git a/rand_core/Cargo.toml b/rand_core/Cargo.toml index 8c9d902a70..633b49bc18 100644 --- a/rand_core/Cargo.toml +++ b/rand_core/Cargo.toml @@ -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 } \ No newline at end of file diff --git a/rand_core/src/block.rs b/rand_core/src/block.rs index a8cefc8e40..be1ab512bd 100644 --- a/rand_core/src/block.rs +++ b/rand_core/src/block.rs @@ -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 @@ -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; /// Results type. This is the 'block' an RNG implementing `BlockRngCore` /// generates, which will usually be an array like `[u32; 16]`. @@ -262,6 +268,14 @@ impl SeedableRng for BlockRng { } } +#[cfg(feature = "zeroize")] +#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))] +impl Zeroize for BlockRng { + fn zeroize(&mut self) { + self.results.as_mut().zeroize() + } +} + impl> CryptoRng for BlockRng {} /// A wrapper type implementing [`RngCore`] for some type implementing @@ -430,6 +444,14 @@ impl SeedableRng for BlockRng64 { } } +#[cfg(feature = "zeroize")] +#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))] +impl Zeroize for BlockRng64 { + fn zeroize(&mut self) { + self.results.as_mut().zeroize() + } +} + impl> CryptoRng for BlockRng64 {} #[cfg(test)]