Repository navigation
Description
Roughly, rand could be modularised as follows:
| component | dependencies |
|---|---|
rand_core |
|
| PRNG crates | core |
rand_os |
core + std |
JitterRng |
core + std |
EntropyRng |
core + os + jitter + std |
ThreadRng |
core + os + jitter + entropy + std + hc |
seq |
core |
distributions |
core |
Rng |
core + seq + distributions |
Roughly 30% of the current code comes under non-linear distributions, which is its own story (#290). For the rest I don't think there's sufficient utility for more crates.
I regret accepting #643 as-is; it "promises" to support all std platforms yet fails on WASM with a compile error (#674, #678) and on any unsupported platforms. The reasons that is an issue are convoluted (probably involving users depending on rand with default features yet not needing them all), but the conclusion of #678 is that Rand should always build for wasm32-unknown-unknown, failing if necessary at run-time.
We used to use run-time failure within EntropyRng but disable OsRng at compile time where unneeded. Practical solutions are:
- Restore the old behavior:
rand_osmust compile but without theOsRngstruct on unsupported platforms.EntropyRngis currently available on all platforms but with run-time failure when no source is available, however it requires a complexcfgpattern in order to disable the missingOsRngdependency when not available; it would therefore make sense to have this in the same crate. - Enable
OsRngon WASM, and use run-time failure on missing implementation. - Enable
OsRngon all platforms, and use run-time failure on missing implementation.
I think the cleanest solution would be (1), but @tarcieri @naftulikay I understand there would be resistance to moving JitterRng into rand_os. Is this really justified? I guess in part this depends on whether the RFC proposed in #648 gets accepted, though this will be a while coming.
Otherwise we can do (2) or (3); the difference is that (2) would cause build failures on any platform where Rand is used with std/default features but OsRng is unavailable. I'm not sure whether this is a good idea?
I suppose (2) is most sensible now.
Activity
tarcieri commentedon Jan 6, 2019
I do not consider
JitterRngas meeting the contract of anOsRng. It is bespoke Rust code, as opposed to a system-provided CSRNG. Personally I would like something where if an OS / platform-specific RNG is not available, it will refuse to compile, as opposed to falling back toJitterRng.dhardy commentedon Jan 6, 2019
The current
compile_error!is not an option due to the required cfgs being over-complex, but (1) is close enough.I am not proposing to adjust
OsRngin any other way; simply to addEntropyRngandJitterRngto the same crate.newpavlov commentedon Jan 6, 2019
I disagree with it. I believe that
rand_osshould fail at compile time forwasm32-unknown-unknownif none ofstdweborwasm_bindgenfeatures are enabled, or if they are enabled both.The breakage caused by splitting code into
rand_osis indeed unfortunate, but I think we better do the following:rand_os v0.1.1which will usestdwebas a "default"rand_os v0.1.0rand_os v0.2.0which will use compile error if both or none of the features are enabledCryZe commentedon Jan 6, 2019
You can't have target specific features with cargo atm, therefore it is impossible to correctly implement failing to compile at the moment. I have indirect dependencies on rand through criterion and parking_lot. You can't realistically disable the rand_os dependency (via rand's std feature as far as I understand) for both of these crates. Therefore my use case will always (or at least until cargo can support target specific features) compile in rand_os without using stdweb or wasm_bindgen. So failing to compile is not a realistic option until then.
What is needed is either a split between the -unknown and a potential -web target and / or fixing cargo.
dhardy commentedon Jan 6, 2019
Noted, but the alternative is to keep telling those who complain about broken CI that they need more complex set-up to add the required feature on WASM... I guess this is possible, but it's a support nightmare.
@CryZe you can probably do this — force your CI tests to depend on WASM + stdweb (or -bindgen). However if you don't use Rand directly, you will need to add it (as a dev-dependency or via another crate only used for testing).
Well, I disagree with that, because there is a plan to eventually make the two compatible.
Agreed; however for now we have to support
wasm32-unknown-unknown, and for that I think we're justified in deviating a little from how we normally do things.CryZe commentedon Jan 6, 2019
It's not my tests that are broken, it's the actual compile (the tests work just fine). For my actual compile, I can't activate either stdweb or wasm-bindgen feature, because I'm not using them (I'm using a different binding generator, so this is kind of the truly "unknown" use case where I'm fine with rand not having a notion of an OS RNG). Also my crate does not exercise any code paths in the wasm compiled code that uses rand_os. However it gets compiled regardless because criterion is a dev-dependency, so cargo not separating features across different dependency kinds or targets is a big problem.
dhardy commentedon Jan 6, 2019
But you could work around this by adding one of the features in your build?
tarcieri commentedon Jan 6, 2019
I'm confused why this can't be done with combinations of
cfg(all(...))/cfg(all(not(...), ...)),feature,target, andcompile_error!. It seems possible to implement correctly to me, albeit a bit onerous (and also seems like something to this effect is already happening).CryZe commentedon Jan 6, 2019
The stdweb feature doesn't work as I'm not using cargo web (this wouldn't make sense for my unknown use case):
However I just tried the wasm-bindgen feature too and it does seem like it does not conflict with anything the unknown use case needs. So I'm actually fine with that.
Cargo propagates features of any cfg combination in the Cargo.toml to all dev-dependencies / (normal-)dependencies and all cfg combinations. They are all in a shared feature space, the cfg'ing or dev-dependency-ing doesn't do anything. This is a bug which is not fixed. Therefore within your actual code your cfg will be against the wrong feature flags, that are actually coming from a different target of a different dependency kind. So here it is the dev-dependency criterion compiled for the host that needs OsRng, but the actual wasm32-unknown-unknown target that your code is targeting doesn't need or want OsRng, but criterion's rand/std feature gets propagated to rand regardless and thus OsRng gets compiled in, even though it's only needed in the dev-dependency on the host (x86, not wasm).
burdges commentedon Jan 7, 2019
I'd prefer if
ThreadRngremained cryptographic, so I'm now more curious about insecurities in the fallback toJitterRng. I supposeThreadRng: CryptoRngcould become platform dependent though?I've no opinion on
rand_osbeing a separate crate, but reverting that decision sounds doable. I love combining all this mess into one crate regardless, so I'd say throwJitterRng,EntropyRng, etc. all intorand_oseven if not required.OsRngwould never callJitterRngof course, butJitterRngexists due to OS shortcomings, sorand_osmakes sense. Anyone writing an OS PRNG could reuse this code inside their OS by providing a different build target for the kernel itself.I think runtime failures sound perfectly acceptable here too.
impl RngCore for OsRngcould just calldebug_assert_eq!(true,false,"OsRng unavailable!");everywhere. It triggers in debug builds and CI only if you useOsRng. I'm curious though ifpub type OsRng = !;orpub enum OsRng {}provides a compile time solution, preferably omittingOsRng: RngCoreorOsRng: CryptoRngin those cases.newpavlov commentedon Jan 7, 2019
IIRC jitter fallback is only needed for Windows. One option will be to incorporate it into Windows OS RNG implementation inside
rand_os(feature gated maybe?). This way we can removeEntropyRngaltogether and useOsRngdirectly.In my understanding JitterRng should be cryptographically secure, the main issue with it is its performance. Citing
JitterRngdocs:dhardy commentedon Jan 7, 2019
Interesting point about
JitterRngonly being needed for Windows. We added it because of one observed problem, though don't know in general how widely useful it is — certainly, most of the time it will not be used.We could put it behind a feature gate which is disabled by default — most people won't notice the difference, and those that do can consider enabling it.
It will — at the very least we will avoid known security problems and only use things we have reason to believe are secure. Whether or not this meets your standards of security is another matter; currently we don't have formal standards and I don't really want to go there.
dhardy commentedon Jan 8, 2019
Before we get too carried away (#685) we should discuss what we actually want. One possible option, compatible with @vks's "rand is just a shim" goal, is:
rand_osfor nowrand_entropycontainingJitterRng,EntropyRngandFromEntropyOsRngcrate? (or optional PRNG dependencies) #648, we can deprecate therand_oscrate and addOsRng(as a simple wrapper around thestdfunction) intorand_entropyRationale:
OsRngand no more; this lets them keep thatrand_entropyour "get your entropy from here!" crate for users wanting seeding/random bytes without the rest ofrandJitterRngis really just a backup option which may not always be compiled in anyway; it doesn't really need to be promoted to its own crateLimitation:
JitterRngbut with a custom nanosecond timer, and use that inEntropyRng, it might be more difficult — though this isn't necessarily a problem (we need some configuration to enableno_stdsources anyway)newpavlov commentedon Jan 8, 2019
What is a rationale for having
EntropyRngassumingrand_oswill incorporate feature-gatedJitterRngfallback for windows? Why can't we seed RNGs directly fromOsRng?If we'll get
getrandom-like funciton intostd, we will still needrand_osto implementrand_coretraits, I highly doubt we will getRngCoreinto std. Thoughrand_oswill become much-much thinner, as most of the complexity will move intostd.I disagree.
JitterRngis quite self-contained and fits into a separate crate really nicely, someone may want to use it independently, e.g. for bare-metal targets. Also in my opinion having it as a optional dependency will be nicer and more explicit compared to a feature gated module.dhardy commentedon Jan 8, 2019
But why do we want to roll the fall-back into the same RNG? It's not simply a wrapper around the OS RNG any more. Same goes for embedded device-specific RNGs and
RDRAND— which implies we might want to change how the recent SGX implementation works.As I said already, I'd prefer to keep
JitterRnggenerally-applicable but put it behind a feature-gate if not everyone wants it.This is already possible. The entire reason
EntropyRngexists is to allow a flexible cross-platform entropy source, even where there is no OS (andOsRng). We're not there yet, but it should be possible to achieve that by plugging in a high-resolution timer or an external RNG, or even with a local entropy collector/pool. But that is #579.However, I guess it still works nicely as a separate crate, though with the number of crates we're collecting I'm wondering again about using multiple repos (core / entropy stuff / rngs / sampling).
19 remaining items
newpavlov commentedon Jan 16, 2019
I personally like
getrandomname. The crate will have to be mostlystd-dependable (except SGXrdrand-based back-end and always erroring fallback). I am not sure how exactly it should be incorporated into std, so let's leave it for RFC discussion.No, I think it certainly shouldn't be a part of
getrandom(or whatever it will be named) crate. The idea is that in future users will be able to overwritegetrandomfunction definition at application level if needed, e.g. by using function with Jitter fallback.Overall I don't quite understand the reluctance toward splitting Jitter RNG into its own crate. It's a self-contained algorithm, and however we will use it in future, a separate crate will not result in any substantial amount of harm, while being easier to add, move or remove.
I believe we will still need
RngCore::try_fill_bytesandCryptoRngeven after introduction ofgetrandom(as a separate crate or in the std). Don't forget that crypto crates can be generic over RNGs. For example some may useThreadRngto speed-up key generation, or use a PRNG for deterministic testing. So I am mostly happy with the currentrand_core. (well, except the block part)I hope in future we will use a single
ChaChaimplementation for both PRNG and stream cipher. Sorand_core::blockcan be moved into RustCrypto/utils.dhardy commentedon Jan 16, 2019
You're now saying you don't think the jitter fallback should be used on Windows by default, but somehow applications which might need it should enable the fallback, and do so by overwriting the
secure_randomfunction they want to use as their default random source?My reluctance is that I am not convinced we will want to keep
JitterRngas an RNG; if it is used to implementsecure_randomthen only a byte-filling-function is required.Additionally, I'm not sure whether we should make applications provide a timer to
JitterRngthen provide that to implementsecure_randomon certainno_stdplatforms, or include the jitter code and simply make the application specify the timer.You point me to an example using only
fill_bytes, which could use a simpler trait, and I wonder if this should example should panic on error? However, having only a singleRngCoretrait to implement for RNGs certainly makes it easier to share CSPRNG code.tarcieri commentedon Jan 16, 2019
I'd be all for a
getrandomcrate, particularly if it were free ofJitterRngfallback. Perhapsrand_oscould wrapgetrandomand handle theJitterRngfallback?Concretely I'd only want the existing OS random functions (
getrandomon Linux,RtlGenRandomorBCryptGenRandomon Windows, "safe"arc4randometc), and RDRAND on SGX targets, with no fallbacks likeJitterRng.Speaking of RDRAND, it seems like that would be preferable to falling back to
JitterRngif available, unless there are performance concerns.tarcieri commentedon Jan 16, 2019
Also am I the only one who finds the idea of
JitterRngscary in the age of microarchitectural covert channels?dhardy commentedon Jan 16, 2019
Have you any idea how slow
JitterRngis?@tarcieri I am not well versed in side-channel attacks, and don't know where to start answering such a question, aside from the obvious: some type of local hardware or software bug needs to be present, and if that can already read your memory then everything is vulnerable. How practical it is to guess the output of a generator based on nano-second jitter I don't know, but you are right that we shouldn't fully trust it, and RDRAND is likely preferable (that may also not be fully trusted, but if you don't trust your CPU then secure entropy sources aren't your only problem).
We now have a slight architectural problem though because @nagisa's RDRAND implementation is dependent on
rand_corebut we are now discussing using it from a lower-levelgetrandomcrate not dependentrand_core.tarcieri commentedon Jan 16, 2019
@dhardy an example of a potential attack vector is attacker-controlled JavaScript running cotenant on the same CPU as a Rust program.
As we saw last year, covert channels are exploitable from web browsers.
So the question for
JitterRngto truly be aCryptoRng, in my opinion, is: can an attacker-controlled cotenant program, such as an attacker-controlled JavaScript running in the same browser, either learn anything about or influenceJitterRng's output?newpavlov commentedon Jan 16, 2019
@tarcieri
I think that it's highly unlikely that such attack is practical, as there is just too much stuff happens on a machine constantly at the same time which influence timings in unpredictable ways, and if you'll add an additional load timings will just become less predictable. Though I have vague concerns regarding simplicity of the whitening scheme used in
JitterRng, to be safe I would prefer to see something like k12 to consume timings and emit entropy, it could even help with the performance issues a bit if we'll take a less conservative stance on the amount of required "true" entropy.@dhardy
We can make
rand_corean optional dependency forJitterRng, i.e.RngCoreimplementation will be feature gated (but probably enabled by default). The same goes forrdrand.In the linked case it will panic, which seems is not a problem for dalek developers. But some of the crypto crate developers (e.g. @briansmith and @ctz IIUC) prefer to expose every potential error even if it's highly unlikely. And I don't think that
try_fill_bytestakes too much space, considering that it does not get exposed in higher-level code.tarcieri commentedon Jan 16, 2019
I know next to nothing about the current
JitterRngimplementation, apologies. I can learn, but my goal is to avoid it.Perhaps it should be moved to
getrandomas well?I'd prefer it be a guarantee as opposed to a best guess. A more concrete question to ask is: is the current TRNG algorithm designed to be sidechannel resistant?
A quick search for papers on this topic pulls up:
It seems provable sidechannel resistance for TRNGs is a thing.
dhardy commentedon Jan 17, 2019
@tarcieri
JitterRnguses the high-resolution part of nano-second timers. As far as I can tell, JavaScript cannot even access nanosecond timers in the browser. And as @newpavlov says, if it adds extra delays to "manipulate" the output, it will not make the output any easier to predict.Your first article on GAROs sounds like it is observational rather than a definitive proof? I didn't read the whole article.
@newpavlov probably we should have an issue on
JitterRngimprovements; this isn't the first suggestion I've seen (the other relating to sources of timing delta entropy).Summary:
JitterRngis very likely secure vs JavaScript side-channel attacks. It could have some improvements (at least regarding performance) and ought to have a security review.But as to what to do, it might still be best not to use a jitter-based-rng in our
getrandomcrate, but instead use RDRAND as a backup option. Should this be enabled by default on all supported targets?This prevents us from making
rand_coredepend (optionally) ongetrandom, which would be nice: we could move thefrom_entropymethod intoSeedableRng.tarcieri commentedon Jan 17, 2019
I made a separate issue for
JitterRngif you'd like to continue the discussion there: #699dhardy commentedon Jan 17, 2019
@newpavlov I think this means we should either delete
JitterRngor move it to a new repository, however we may as well migrate away from using it before removing the code fromrand. That means closing #685, though if you like I can make a new repo for that code (jitter_rngorrand_jitter).Ideally I'd like another backup source in place before removing it however.
newpavlov commentedon Jan 17, 2019
I don't mind moving
rand_jitterinto a separate repository, but do you plan to publishrand v0.7? BecauseJitterRngis a part of public API. I highly doubt anyone has used it directly, but still strictly speaking removing it for v0.6 will be breakage of compatibility promise. So I think it will be better to merge #685 and decide its fate later in #699. I don't think we need two versions ofJitterRng, one in the separate repo, and one as part ofrand.dhardy commentedon Jan 17, 2019
Agreed, we shouldn't remove it before 0.7, and even then we might want a deprecation warning.
Fair point, the crate name doesn't need to change, so there's no reason we can't merge #685 now.
dhardy commentedon Mar 23, 2019
I think the time has come to close this thread. It has several progeny:
rand_jitter(Split JitterRng into a separate crate #685)The continuation of the remainder of this discussion can move to #760.