Skip to content

Commit

Permalink
chacha20: remove Clone impls on RNGs (#220)
Browse files Browse the repository at this point in the history
Removes `derive(Clone)` on `ChaCha*Rng*`.

They date back to the original PR which added RNG support (#63).

Allowing `Clone` on an RNG is problematic because the cloned RNG will
have the same internal state, duplicating outputs which can be
catastrophic in a cryptographic context.

Instead, the `SeedableRng::from_rng` method can be used to "fork" one
RNG from another, seeing a new RNG with an output from another:

https://docs.rs/rand_core/0.6.2/rand_core/trait.SeedableRng.html#method.from_rng
  • Loading branch information
tarcieri authored Mar 16, 2021
1 parent 2376701 commit ba7d695
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions chacha20/src/rng.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Block RNG based on rand_core::BlockRng
use rand_core::block::{BlockRng, BlockRngCore};
use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
use rand_core::{
block::{BlockRng, BlockRngCore},
CryptoRng, Error, RngCore, SeedableRng,
};

use crate::{
backend::{Core, BUFFER_SIZE},
Expand All @@ -13,7 +15,6 @@ use core::convert::TryInto;
macro_rules! impl_chacha_rng {
($name:ident, $core:ident, $rounds:ident, $doc:expr) => {
#[doc = $doc]
#[derive(Clone)]
#[cfg_attr(docsrs, doc(cfg(feature = "rng")))]
pub struct $name(BlockRng<$core>);

Expand Down Expand Up @@ -52,7 +53,6 @@ macro_rules! impl_chacha_rng {
impl CryptoRng for $name {}

#[doc = "Core random number generator, for use with [`rand_core::block::BlockRng`]"]
#[derive(Clone)]
#[cfg_attr(docsrs, doc(cfg(feature = "rng")))]
pub struct $core {
block: Core<$rounds>,
Expand Down

0 comments on commit ba7d695

Please sign in to comment.