Repository navigation
Description
Perhaps this can stay open for a while as a discussion item. I'd especially like feedback from lib users who have looked at this trait.
This trait has requirements on the PRNG algorithm, but not the seeding method (as comes up in #537):
/// A marker trait used to indicate that an [`RngCore`] or [`BlockRngCore`]
/// implementation is supposed to be cryptographically secure.
///
/// *Cryptographically secure generators*, also known as *CSPRNGs*, should
/// satisfy an additional properties over other generators: given the first
/// *k* bits of an algorithm's output
/// sequence, it should not be possible using polynomial-time algorithms to
/// predict the next bit with probability significantly greater than 50%.
///
/// Some generators may satisfy an additional property, however this is not
/// required by this trait: if the CSPRNG's state is revealed, it should not be
/// computationally-feasible to reconstruct output prior to this. Some other
/// generators allow backwards-computation and are consided *reversible*.
///
/// Note that this trait is provided for guidance only and cannot guarantee
/// suitability for cryptographic applications. In general it should only be
/// implemented for well-reviewed code implementing well-regarded algorithms.
///
/// Note also that use of a `CryptoRng` does not protect against other
/// weaknesses such as seeding from a weak entropy source or leaking state.
///
/// [`RngCore`]: trait.RngCore.html
/// [`BlockRngCore`]: ../rand_core/block/trait.BlockRngCore.html
pub trait CryptoRng {}The trait can guarantee absolutely nothing about the quality of the seed used on the PRNG. For example, it would be quite easy to write:
let key = [0u8; 32];
// TODO: randomise the key
let rng = ChaChaRng::from_seed(key);and forget to actually add any entropy to the system.
Or, in some ways worse (because it is harder to spot), the key could be written using a non-crypto RNG (stretching an insecure key).
There is no easy way to guard against insecure seeding (and even if there were, there are other ways to go wrong in cryptography). Even if we added a function like this it wouldn't really help:
pub trait CryptoRng {
/// Implementations must estimate the number of bits of entropy in their seed
fn estimate_entropy(&self) -> u32;
}It would be simple enough to detect seeds with too many 0s or 1s, but still extra complexity to implement. Detecting that the seed was set by a weak PRNG however would be much much harder (computationally very expensive and very complex) — this is not viable.
Summary: security is a property of the complete implementation, not just the type.
So my conclusion: CryptoRng appears to be making a promise it cannot guarantee when implemented on PRNG algorithms.
Options:
- Keep the status quo; rely on documentation
- Remove implementation of
CryptoRngfrom PRNG algorithms and keep only onOsRng,JitterRng,EntropyRngandThreadRng(which all manage their own seeding or directly get entropy from external sources) - Remove the
CryptoRngtrait altogether
Activity
dhardy commentedon Jul 7, 2018
Since the rationale of
CryptoRngis type-level documentation, the second option seems a good choice to me. It would improve the guarantee provided by something like this:And it would still allow user-defined types to implement the traits, though of course we recommend only implementing on types which manage seeding internally. For example:
vks commentedon Jul 7, 2018
I agree that
SeedableRngcan be problematic for CSPRNG. If we want to be conservative, we could removeSeedableRngfromStdRng(Option 4), although this would not allow some use cases:StdRngfor its statistical quality only.Changing the definition of
CryptoRngto not apply to seedable RNGs would help, but even then I'm not sure it would be a helpful trait. If I wanted to make sure my randomness source is secure, I would probably just pick an implementation and not make the code generic. This is what all the Rust crypto libraries are doing, they essentially use their own implementation ofOsRng.In summary, I think generic code involving
CryptoRngis probably a bad idea, so we might as well remove the trait.burdges commentedon Jul 8, 2018
We could do runtime checking by providing a function like
for which that
OsRngsimply returnstrue, but any CSPRNGs copy a flag from their seed:We should not even implement
CryptoRngfor completely non-cryptographic PRNGs, so numerical users might require seeding non-cryptographic PRNGs from non-cryptographic PRNGs. We could support this without special methods though:dhardy commentedon Jul 8, 2018
@burdges I already mentioned runtime checking in the first post. I don't like it, because e.g.
from_seedcould be called with something that's not random (but maybe still looking random).burdges commentedon Jul 8, 2018
Is this doable entirely at the type level once the
constfeature set improves? AlaWe could give CSPRNGs a type level parameter like:
but I fear this will become annoying in practice.
There are interesting type level work around with wrapper types:
We could also split
SeedableRnginto an insecure traitSeedableRngand a secure traitFromRng:We'd simply explain that
FromRngshould be secure but code that usedSeedableRngrequires stricter auditing. meh..burdges commentedon Jul 8, 2018
I dislike all these answers myself actually. :) In fact I'll add several concerns that count against
CryptoRng:Tests or benchmarks in cryptographic crates might employ non-cryptographic PRNGs, like Xorshift, maybe so that benchmarks more accurately represent the crate's actual work, or maybe because the standard is written that way.
It's common curve libraries, etc. have functions to build an element with an
Rngbut frequently you actually want to construct this element with PRF output, so you doMyCurveScalar::generate( ChaChaRng::from_seed(my_prf_output) ), which such tricks cannot recognize as secure.There are various cryptographic properties one might consider here, including the security level.
dhardy commentedon Jul 8, 2018
I see @quininer has used
CryptoRngin several places, e.g. oake, sarkara, keyber and @tendermint has in yubihsm.All these examples are part of a generic API: allowing some kind of secret to be seeded from a user-provided secure RNG.
For these usages I think it makes sense to keep
CryptoRng, but as mentioned above, recommendingCryptoRngs do not implementSeedableRngmakes sense. This doesn't provide a uniform way of constructing such RNGs however, so how about we add a method toCryptoRng?We could then implement
FromEntropyforCryptoRngs as well asSeedableRngs.In practice: this means that CSPRNGs would have two "guises": the seedable variant (impls
RngCoreandSeedableRng) and the secured variant (wraps the other; derivesRngCoreand implsCryptoRngviaSeedableRng). Limitation: this doesn't really work for reproducible and secure CSPRNGs.burdges commentedon Jul 8, 2018
Isn't that going to break
OsRng: CryptoRng? You could add a method toFromEntropyalaBut..
I disagree strongly with not implementing
SeedableRngfor CSPRNGs because PRFs are ubiquitous, as I said above. In fact, I'll argue that all CSPRNGs use cases requirefrom_seed, except for those already handled entirely insiderand, alaThreadRngorEntropyRngor whatever, which makes theFromEntropysolution useless. In fact, even the English phrase CSPRNG refers to algorithmic assumptions/properties, not input to the algorithm, so that's all one could mean byCryptoRng.There are ways to consider the CSPRNG inputs when doing formal verification, but this requires languages with much stronger dependent types, like F*, and even languages that understand game-hopping proofs, like CryptoVerif/ProtoVerif. There is a tentative project to compile Rust to F* for these purposes, btw.
Also, we should not imho go with runtime checking but my initial runtime musings actually did handle
from_seedcorrectly. You could even require justification strings everywhere:But
CryptoRngitself still means "the algorithm is a CSPRNG".dhardy commentedon Jul 9, 2018
Good argument @burdges. Then I guess we should do nothing here.
I brought this up because in #537 @vks keeps saying that
CryptoRngs should not implementseed_from_u64(because that's not appropriate for cryptography), but I don't see why the algorithms shouldn't still implement it for non-crypto uses.vks commentedon Jul 9, 2018
It's a tradeoff. On the one hand,
seed_from_u64is never the correct way to initialize a CSPRNG that is actually required to be cryptographically secure, on the other hand, not having it makes them more annoying to initialize for non-crypto purposes.burdges commentedon Jul 10, 2018
If
seed_from_u64becomes aSeedableRngmethod, then you could either ask that it panic allCryptoRngs, or else provide a wrapper type that it panics. I think either sounds fine since if you were usingChaChaRngas just a better numerical PRNG then you'd also likely be okay callingfrom_seed, no?dhardy commentedon Jul 11, 2018
No, because either the type
CryptoRngprotects against bad seeding or it does not, and as we have discussed here protecting against bad seeding does not appear to be viable.I don't see some halfway-house solution (deliberately breaking
seed_from_u64on crypto PRNGs) as useful.vks commentedon Jul 11, 2018
What's the decision here? Keep the status quo?
dhardy commentedon Jul 11, 2018
Yes. @burdges is right that we can't really just make
SeedableRngincompatible withCryptoRngand I don't see another good option.Runtime checking would be a partial fix, but then really we'd want to modify
from_seed:(or wrap the boolean with a struct / enum, but same effect)
The status quo is poor but
foo<R: Rng + CryptoRng>(rng: &mut R)is basically documentation.burdges commentedon Jul 11, 2018
Yes, I think a
boolorOption<&'static str>argument works fine, if you want (debug) runtime checking. I'd like runtime checking more if I didn't know about plans to compile Rust to F* for much stronger validation ;)