Skip to content

Commit a01a825

Browse files
committed
Additional logging for OsRng and JitterRng
1 parent 4102ec0 commit a01a825

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

src/jitter.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ impl JitterRng {
142142
// This allows the timer test to run multiple times; we don't care.
143143
rounds = ec.test_timer()?;
144144
JITTER_ROUNDS.store(rounds as usize, Ordering::Relaxed);
145+
info!("JitterRng: using {} rounds per u64 output", rounds);
145146
}
146147
ec.set_rounds(rounds);
147148
Ok(ec)
@@ -422,6 +423,8 @@ impl JitterRng {
422423
}
423424

424425
fn gen_entropy(&mut self) -> u64 {
426+
trace!("JitterRng: collecting entropy");
427+
425428
// Prime `self.prev_time`, and run the noice sources to make sure the
426429
// first loop round collects the expected entropy.
427430
let _ = self.measure_jitter();
@@ -444,6 +447,7 @@ impl JitterRng {
444447
/// to collect 64 bits of entropy. Otherwise a `TimerError` with the cause
445448
/// of the failure will be returned.
446449
pub fn test_timer(&mut self) -> Result<u32, TimerError> {
450+
debug!("JitterRng: testing timer ...");
447451
// We could add a check for system capabilities such as `clock_getres`
448452
// or check for `CONFIG_X86_TSC`, but it does not make much sense as the
449453
// following sanity checks verify that we have a high-resolution timer.

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@
256256

257257
#[cfg(feature = "log")] #[macro_use] extern crate log;
258258
#[cfg(not(feature = "log"))] macro_rules! trace { ($($x:tt)*) => () }
259+
#[cfg(not(feature = "log"))] macro_rules! debug { ($($x:tt)*) => () }
260+
#[cfg(all(feature="std", not(feature = "log")))] macro_rules! info { ($($x:tt)*) => () }
259261
#[cfg(all(feature="std", not(feature = "log")))] macro_rules! warn { ($($x:tt)*) => () }
260262

261263

src/os.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ impl ReadRng {
142142
let mutex = unsafe{ READ_RNG_FILE.as_ref().unwrap() };
143143
let mut guard = mutex.lock().unwrap();
144144
if (*guard).is_none() {
145+
info!("OsRng: opening random device {}", path.as_ref().display());
145146
let file = File::open(path).map_err(|err| Error::with_cause(
146147
ErrorKind::Unavailable,
147148
"error opening random device",
@@ -154,6 +155,7 @@ impl ReadRng {
154155
}
155156

156157
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
158+
trace!("OsRng: reading {} bytes from random device", dest.len());
157159
if dest.len() == 0 { return Ok(()); }
158160

159161
// Since we have an instance of Self, we can assume that our memory was
@@ -223,6 +225,7 @@ mod imp {
223225
fn getrandom(_buf: &mut [u8]) -> libc::c_long { -1 }
224226

225227
fn getrandom_try_fill(v: &mut [u8]) -> Result<(), Error> {
228+
trace!("OsRng: reading {} bytes via getrandom", v.len());
226229
let mut read = 0;
227230
let len = v.len();
228231
while read < len {
@@ -265,6 +268,7 @@ mod imp {
265268
static AVAILABLE: AtomicBool = ATOMIC_BOOL_INIT;
266269

267270
CHECKER.call_once(|| {
271+
debug!("OsRng: testing getrandom");
268272
let mut buf: [u8; 0] = [];
269273
let result = getrandom(&mut buf);
270274
let available = if result == -1 {
@@ -274,6 +278,7 @@ mod imp {
274278
true
275279
};
276280
AVAILABLE.store(available, Ordering::Relaxed);
281+
info!("OsRng: using {}", if available { "getrandom" } else { "/dev/urandom" });
277282
});
278283

279284
AVAILABLE.load(Ordering::Relaxed)
@@ -332,6 +337,7 @@ mod imp {
332337
}
333338

334339
pub fn try_fill_bytes(&mut self, v: &mut [u8]) -> Result<(), Error> {
340+
trace!("OsRng: reading {} bytes via cloadabi::random_get", v.len());
335341
if unsafe { cloudabi::random_get(v) } == cloudabi::errno::SUCCESS {
336342
Ok(())
337343
} else {
@@ -372,6 +378,7 @@ mod imp {
372378
Ok(OsRng)
373379
}
374380
pub fn try_fill_bytes(&mut self, v: &mut [u8]) -> Result<(), Error> {
381+
trace!("OsRng: reading {} bytes via SecRandomCopyBytes", v.len());
375382
let ret = unsafe {
376383
SecRandomCopyBytes(kSecRandomDefault, v.len() as size_t, v.as_mut_ptr())
377384
};
@@ -407,6 +414,7 @@ mod imp {
407414
// kern.arandom permits a maximum buffer size of 256 bytes
408415
for s in v.chunks_mut(256) {
409416
let mut s_len = s.len();
417+
trace!("OsRng: reading {} bytes via kern.arandom", v.len());
410418
let ret = unsafe {
411419
libc::sysctl(mib.as_ptr(), mib.len() as libc::c_uint,
412420
s.as_mut_ptr() as *mut _, &mut s_len,
@@ -441,6 +449,7 @@ mod imp {
441449
pub fn try_fill_bytes(&mut self, v: &mut [u8]) -> Result<(), Error> {
442450
// getentropy(2) permits a maximum buffer size of 256 bytes
443451
for s in v.chunks_mut(256) {
452+
trace!("OsRng: reading {} bytes via getentropy", s.len());
444453
let ret = unsafe {
445454
libc::getentropy(s.as_mut_ptr() as *mut libc::c_void, s.len())
446455
};
@@ -494,6 +503,7 @@ mod imp {
494503
}
495504
pub fn try_fill_bytes(&mut self, v: &mut [u8]) -> Result<(), Error> {
496505
for s in v.chunks_mut(fuchsia_zircon::sys::ZX_CPRNG_DRAW_MAX_LEN) {
506+
trace!("OsRng: reading {} bytes via cprng_draw", s.len());
497507
let mut filled = 0;
498508
while filled < s.len() {
499509
match fuchsia_zircon::cprng_draw(&mut s[filled..]) {
@@ -535,6 +545,7 @@ mod imp {
535545
// RtlGenRandom takes an ULONG (u32) for the length so we need to
536546
// split up the buffer.
537547
for slice in v.chunks_mut(<ULONG>::max_value() as usize) {
548+
trace!("OsRng: reading {} bytes via RtlGenRandom", slice.len());
538549
let ret = unsafe {
539550
RtlGenRandom(slice.as_mut_ptr() as PVOID, slice.len() as ULONG)
540551
};

src/reseeding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<R: Rng, Rsdr: Reseeder<R>> ReseedingRng<R, Rsdr> {
4747
/// generated exceed the threshold.
4848
pub fn reseed_if_necessary(&mut self) {
4949
if self.bytes_generated >= self.generation_threshold {
50-
trace!("Reseeding RNG");
50+
trace!("Reseeding RNG after {} bytes", self.bytes_generated);
5151
self.reseeder.reseed(&mut self.rng).unwrap();
5252
self.bytes_generated = 0;
5353
}

0 commit comments

Comments
 (0)