Skip to content

Commit

Permalink
Custom Debug implementation for ChaCha and Xorshift
Browse files Browse the repository at this point in the history
So the internal state is never exposed (may be security-sensitive)

[Cherry-picked from e513aaa]
  • Loading branch information
pitdicker authored and dhardy committed Dec 15, 2017
1 parent 675e6f3 commit e154d22
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/prng/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! The ChaCha random number generator.
use core::num::Wrapping as w;
use core::fmt;
use {Rng, SeedableRng, Rand};
use impls;

Expand All @@ -30,13 +31,20 @@ const CHACHA_ROUNDS: u32 = 20; // Cryptographically secure from 8 upwards as of
///
/// [1]: D. J. Bernstein, [*ChaCha, a variant of
/// Salsa20*](http://cr.yp.to/chacha.html)
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct ChaChaRng {
buffer: [w32; STATE_WORDS], // Internal buffer of output
state: [w32; STATE_WORDS], // Initial state
index: usize, // Index into state
}

// Custom Debug implementation that does not expose the internal state
impl fmt::Debug for ChaChaRng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ChaChaRng {{}}")
}
}

macro_rules! quarter_round{
($a: expr, $b: expr, $c: expr, $d: expr) => {{
$a = $a + $b; $d = $d ^ $a; $d = w($d.0.rotate_left(16));
Expand Down
1 change: 1 addition & 0 deletions src/prng/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl Clone for IsaacRng {
}
}

// Custom Debug implementation that does not expose the internal state
impl fmt::Debug for IsaacRng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "IsaacRng {{}}")
Expand Down
1 change: 1 addition & 0 deletions src/prng/isaac64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl Clone for Isaac64Rng {
}
}

// Custom Debug implementation that does not expose the internal state
impl fmt::Debug for Isaac64Rng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Isaac64Rng {{}}")
Expand Down
11 changes: 9 additions & 2 deletions src/prng/xorshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! Xorshift generators
use core::num::Wrapping as w;
use core::fmt;
use {Rng, SeedableRng, Rand};
use impls;

Expand All @@ -24,15 +25,21 @@ use impls;
/// [1]: Marsaglia, George (July 2003). ["Xorshift
/// RNGs"](http://www.jstatsoft.org/v08/i14/paper). *Journal of
/// Statistical Software*. Vol. 8 (Issue 14).
#[allow(missing_copy_implementations)]
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct XorShiftRng {
x: w<u32>,
y: w<u32>,
z: w<u32>,
w: w<u32>,
}

// Custom Debug implementation that does not expose the internal state
impl fmt::Debug for XorShiftRng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "XorShiftRng {{}}")
}
}

impl XorShiftRng {
/// Creates a new XorShiftRng instance which is not seeded.
///
Expand Down

0 comments on commit e154d22

Please sign in to comment.