Uh oh!
There was an error while loading. Please reload this page.
Seed weak_rng with the current time if OsRng fails - #181
Conversation
dhardy
left a comment
There was a problem hiding this comment.
The main thing I want to say here is that Xorshift is a terrible PRNG to use to seed other PRNGs with. If you want a fallback source of entropy to use when seeding stuff, I'd recommend still using a better PRNG internally (e.g. Isaac: StdRng).
Xorshift is really so bad for seeding stuff that if you seed one XorshiftRng from another, they essentially end up being clones. The refactor in progress will very likely remove this algorithm.
Possibly my recommendation here would be to adapt thread_rng instead, and make weak_rng seed from thread_rng. This does make thread_rng weaker, but it's not supposed to be used for crypto anyway. Not 100% sure if this is agreeable; alternatively you could add a new function.
| Err(e) => panic!("weak_rng: failed to create seeded RNG: {:?}", e) | ||
| Err(_) => { | ||
| // Set a bit because XorShiftRng will panic if the seed is | ||
| // entirely zero. |
There was a problem hiding this comment.
Are you sure you don't want this to panic if precise_time_ns is so broken it returns zero? You've got zero entropy in this case... for some uses that doesn't matter but not necessarily all.
dhardy
commented
Oct 23, 2017
See also dhardy#21 which makes a similar change on my branch of rand |
Thanks. I see that fixing |
030c961 to
b008e1dComparecpeterso
commented
Oct 24, 2017
This PR moves the I construct the weak fallback seed from the current system time using std::time, as suggested in dhardy#21 (comment), instead of adding a new crate dependency just for |
| let unix_time = now.duration_since(time::UNIX_EPOCH).unwrap(); | ||
| let secs = unix_time.as_secs() as usize; | ||
| let nanos = unix_time.subsec_nanos() as usize; | ||
| [(secs << 32) | nanos] |
There was a problem hiding this comment.
1s = 10^9 ns, not 2^32 ns
It doesn't matter precisely, but if you want a fast approximation use << 30 to avoid leaving 2 empty bits
There was a problem hiding this comment.
If usize is 32 bits, then shifting secs << 32 (or 30) will lose data. Instead of shifting any bits, I could return them all like [secs, nanos] and let StdRng use the bits as it sees fit. A comment in isaac.rs says reseed() will "make the seed into [seed[0], seed[1], ..., seed[seed.len() - 1], 0, 0, ...], to fill rng.rsl."
b008e1d to
0812927Compare0812927 to
6d297d5Comparealexcrichton
commented
Oct 27, 2017
Thanks @cpeterso! |
Seed weak_rng with the current time if OsRng fails
And add a test for
weak_rng.