-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide access to the system's random number generator #159
Comments
I'm not sure about this. Given that we'll never be a better choice for user code than using (I also have some concerns about providing this via a |
This is already true for most of |
It's worse than the status quo though. |
That is true for any addition to std (that can't live in alloc / core) though, as they all can't work on |
I understand what you mean, that issue also applies to the Sidenote: Sometimes I wonder whether it would be worth it to add a |
An additional issue is the use of the CC @BlackHoleFox, who I know has been trying to come up with a design for std randomness off and on for ages. |
Yes, that could be an issue. We can however always change Using the |
Don't want to further block this ACP from getting merged, but think it's worth elaborating here how this point is meant to be addressed:
Since currently, the attached PR has an |
We definitely do need the insecure flag for internal usage, but I don't think it should be part of a MVP public API which I tried to propose here. Adding it in the future seems very reasonable though. |
Note there's #393 which has been accepted. |
Ah yes, that makes this ACP unnecessary. |
On second thought, I have some issues with the accepted ACP. I'll reopen this so that I have an alternative to link to. |
Nevermind, I'm happy with how everything turned out. |
Proposal
(an alternative to #393)
Motivation
Nearly all targets that
std
supports (with the exception of WASM) provide access to a cryptographically secure random number generator. Although this functionality is more widespread than even filesystem access,std
does not currently have an abstraction for it, leading users to rely on an external crate likegetrandom
. While that crate is of high quality, this still leads to the duplication of functionality thatstd
already implements, asHashMap
is initialized with seeds generated from the system's RNG by default. Also, this complicates the addition of new targets to the ecosystem, as more libraries need to be changed to support this very common use case.Unfortunately, the current lack of a common abstraction means there is a widespread pattern of users using
File::open("/dev/urandom")
for easy access to random data (GitHub search). This is not very portable and can be problematic, as "/dev/urandom" returns low-quality randomness on Linux if the entropy pool is not yet initialized (this is unlikely to be a problem for most usecases, but especially on embedded devices, security will suffer).Solution sketches
A
Read
able handle type similar to theStdin
/Stdout
handles is added tostd::io
along with its matching constructor:Random bytes are generated by using the
Read
trait's methods:While this represents a departure from
getrandom
's API, using theRead
trait is more idiomatic, enables better interplay with otherio
functions such ascopy
and allows for optimizations like using an uninitialized buffer withread_buf
. Additionally, it enables the configuration of behaviour like blocking until there is enough entropy available1 through methods on thestruct
, mirroring API likeTcpStream::set_nonblocking
.Links and related work
std
std
's random number generation in favour ofgetrandom
Footnotes
which should IMHO be the default, but users may want to depart from this behaviour ↩
The text was updated successfully, but these errors were encountered: