forked from model-checking/verify-rust-std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
std: switch to faster random sources on macOS and most BSDs
- Loading branch information
Showing
9 changed files
with
90 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! Random data generation with `arc4random_buf`. | ||
//! | ||
//! Contrary to its name, `arc4random` doesn't actually use the horribly-broken | ||
//! RC4 cypher anymore, at least not on modern systems, but rather something | ||
//! like ChaCha20 with continual reseeding from the OS. That makes it an ideal | ||
//! source of large quantities of cryptographically secure data, which is exactly | ||
//! what we need for `DefaultRandomSource`. Unfortunately, it's not available | ||
//! on all UNIX systems, most notably Linux (until recently, but it's just a | ||
//! wrapper for `getrandom`. Since we need to hook into `getrandom` directly | ||
//! for `HashMap` keys anyway, we just keep our version). | ||
|
||
#[cfg(not(any( | ||
target_os = "haiku", | ||
target_os = "illumos", | ||
target_os = "solaris", | ||
target_os = "vita", | ||
)))] | ||
use libc::arc4random_buf; | ||
|
||
// FIXME: move these to libc (except Haiku, that one needs to link to libbsd.so). | ||
#[cfg(any( | ||
target_os = "haiku", // See https://git.haiku-os.org/haiku/tree/headers/compatibility/bsd/stdlib.h | ||
target_os = "illumos", // See https://www.illumos.org/man/3C/arc4random | ||
target_os = "solaris", // See https://docs.oracle.com/cd/E88353_01/html/E37843/arc4random-3c.html | ||
target_os = "vita", // See https://github.com/vitasdk/newlib/blob/b89e5bc183b516945f9ee07eef483ecb916e45ff/newlib/libc/include/stdlib.h#L74 | ||
))] | ||
#[cfg_attr(target_os = "haiku", link(name = "bsd"))] | ||
extern "C" { | ||
fn arc4random_buf(buf: *mut core::ffi::c_void, nbytes: libc::size_t); | ||
} | ||
|
||
pub fn fill_bytes(bytes: &mut [u8]) { | ||
unsafe { arc4random_buf(bytes.as_mut_ptr().cast(), bytes.len()) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//! Random data generation through `getentropy`. | ||
//! | ||
//! Since issue 8 (2024), the POSIX specification mandates the existence of the | ||
//! `getentropy` function, which fills a slice of up to `GETENTROPY_MAX` bytes | ||
//! (256 on all known platforms) with random data. Unfortunately, it's only | ||
//! meant to be used to seed other CPRNGs, which we don't have, so we only use | ||
//! it where `arc4random_buf` and friends aren't available or secure (currently | ||
//! that's only the case on Emscripten). | ||
|
||
pub fn fill_bytes(bytes: &mut [u8]) { | ||
// GETENTROPY_MAX isn't defined yet on most platforms, but it's mandated | ||
// to be at least 256, so just use that as limit. | ||
for chunk in bytes.chunks_mut(256) { | ||
let r = unsafe { libc::getentropy(chunk.as_mut_ptr().cast(), chunk.len()) }; | ||
assert_ne!(r, -1, "failed to generate random data"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters