-
Notifications
You must be signed in to change notification settings - Fork 22
Description
Proposal
Problem statement
Provide a simple to use way to fill a fixed-sized buffer with unpredictable data using whatever platform provided API is most appropriate for the target.
Also the current tracking issues for secure random number generation (rust-lang/rust#130703) mixes together both secure generation and generic RNG trait(s), leading to a somewhat chaotic discussion. I.e. the currently proposed random::Distribution
trait is in no way specific to secure random number generation and could be (arguable more) useful for other types of RNGs.
Motivating examples or use cases
Often it is useful to completely fill a small fixed-size buffer with cryptographically secure random data. Example use cases:
- cryptographic key material
- to use as a token
- creating a nonce/IV
Solution sketch
Tracking issues
Divide the tracking issues differently:
- A general purpose rand-like traits and possibly non-secure implementation(s)
- Implementation of guaranteed secure random generation from the system
This will hopefully focus the discussion more effectively.
fill_bytes
A standalone, infallible, fill_bytes
function that returns system provided cryptographically secure random data.
// In std::random
fn fill_bytes(dest: &mut [u8]);
This is independent of any random traits that may (or may not) be added to the standard library. So I propose splitting them from one another to keep discussion focused.
The name is of course amenable to bikeshedding. E.g. getrandom
, system_random
, cprng
, etc.
Alternatives
- Fully separate the OS CRNG from the other rand implementations and traits to make it super clear that these are special. E.g.
std::crng::fill_bytes
orstd::secure_random::fill_bytes
. - Or have a
SecureRandom
type in thestd::random
module which would be a bit clearer thanDefaultRandomSource
. - Instead of doing any of this, encourage people to use traits only so that the CRNG can be overridden by injecting an alternative CRNG.
Future possibilities
There could be types that wrap this interface to implement std traits. But this should exist independently of those.
fill_bytes
requires the bytes to be pre-initialised. While I don't think this make much practical impact (at least judging from the RustCrypto experience and the lack of use of getrandom::uninit
),, even that initialisation can be optimised out by LLVM if we can tell it that the bytes are writeonly
. Rust doesn't currently have a way to do this but it could. Even an unstable attribute could be used purely for optimisation purposes internally by std.
This ACP does not rule out having some variation on a fallible/uninit function in the future (e.g. try_fill_bytes
). But that is explicitly not proposed here. Even with that, the simpler function should be available.
Links and related work
- Tracking Issue for secure random data generation in
std
rust#130703 - Tracking Issue for deterministic random number generation rust#131606
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.