Skip to content

Commit e513aaa

Browse files
committed
Custom Debug implementation for ChaCha and Xorshift
So the internal state is never exposed (may be security-sensitive)
1 parent 6712a3a commit e513aaa

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

src/prng/chacha.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! The ChaCha random number generator.
1212
1313
use core::num::Wrapping as w;
14+
use core::fmt;
1415
use {Rng, CryptoRng, SeedFromRng, SeedableRng, Error};
1516

1617
#[allow(bad_style)]
@@ -29,13 +30,20 @@ const CHACHA_ROUNDS: u32 = 20; // Cryptographically secure from 8 upwards as of
2930
///
3031
/// [1]: D. J. Bernstein, [*ChaCha, a variant of
3132
/// Salsa20*](http://cr.yp.to/chacha.html)
32-
#[derive(Clone, Debug)]
33+
#[derive(Clone)]
3334
pub struct ChaChaRng {
3435
buffer: [w32; STATE_WORDS], // Internal buffer of output
3536
state: [w32; STATE_WORDS], // Initial state
3637
index: usize, // Index into state
3738
}
3839

40+
// Custom Debug implementation that does not expose the internal state
41+
impl fmt::Debug for ChaChaRng {
42+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43+
write!(f, "ChaChaRng {{}}")
44+
}
45+
}
46+
3947
macro_rules! quarter_round{
4048
($a: expr, $b: expr, $c: expr, $d: expr) => {{
4149
$a = $a + $b; $d = $d ^ $a; $d = w($d.0.rotate_left(16));

src/prng/isaac.rs

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl Clone for IsaacRng {
110110
}
111111
}
112112

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

src/prng/isaac64.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ impl Clone for Isaac64Rng {
9494
}
9595
}
9696

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

src/prng/xorshift.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Xorshift generators
1212
1313
use core::num::Wrapping as w;
14+
use core::fmt;
1415
use {Rng, SeedFromRng, SeedableRng, Error};
1516

1617
/// An Xorshift[1] random number
@@ -23,15 +24,21 @@ use {Rng, SeedFromRng, SeedableRng, Error};
2324
/// [1]: Marsaglia, George (July 2003). ["Xorshift
2425
/// RNGs"](http://www.jstatsoft.org/v08/i14/paper). *Journal of
2526
/// Statistical Software*. Vol. 8 (Issue 14).
26-
#[allow(missing_copy_implementations)]
27-
#[derive(Clone, Debug)]
27+
#[derive(Clone)]
2828
pub struct XorShiftRng {
2929
x: w<u32>,
3030
y: w<u32>,
3131
z: w<u32>,
3232
w: w<u32>,
3333
}
3434

35+
// Custom Debug implementation that does not expose the internal state
36+
impl fmt::Debug for XorShiftRng {
37+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38+
write!(f, "XorShiftRng {{}}")
39+
}
40+
}
41+
3542
impl XorShiftRng {
3643
/// Creates a new XorShiftRng instance which is not seeded.
3744
///

0 commit comments

Comments
 (0)