-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unconditionally use libc::getrandom on Illumos and Solaris
Also removes the use of `GRND_RANDOM`, which appears to be based on outdated staements about the RNGs. For Solaris, see [this blog post](https://blogs.oracle.com/solaris/post/solaris-new-system-calls-getentropy2-and-getrandom2). For Illumos, the algorithms are less clear, but I don't see a clear reason to continue using `GRND_RANDOM`. I updated the documentation in `getrandom.rs` to full document this decision and to have a common place listing when `getrandom(2)` became avalible on each platform. I also updated the main lib.rs docs to point to the correct man pages. Note that Solaris 11.3 has a maximum buffer length of 1024 bytes, while Illumos doesn't have this sort of issue. Signed-off-by: Joe Richey <[email protected]>
- Loading branch information
Showing
4 changed files
with
45 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,43 @@ | ||
//! Implementation using libc::getrandom | ||
//! | ||
//! Available since: | ||
//! - Linux Kernel 3.17, Glibc 2.25, Musl 1.1.20 | ||
//! - Android API level 23 (Marshmallow) | ||
//! - NetBSD 10.0 | ||
//! - FreeBSD 12.0 | ||
//! - Solaris 11.3 | ||
//! - Illumos since Dec 2018 | ||
//! - DragonFly 5.7 | ||
//! - Hurd Glibc 2.31 | ||
//! - shim-3ds since Feb 2022 | ||
//! | ||
//! For all platforms, we use the default randomness source (the one used | ||
//! by /dev/urandom) rather than the /dev/random (GRND_RANDOM) source. For | ||
//! more information see the linked man pages in lib.rs. | ||
//! - On Linux, "/dev/urandom is preferred and sufficient in all use cases". | ||
//! - On NetBSD, "there is no reason to ever use" GRND_RANDOM. | ||
//! - On Illumos, the default source is used for getentropy() and the like: | ||
//! https://github.com/illumos/illumos-gate/blob/89cf0c2ce8a47dcf555bb1596f9034f07b9467fa/usr/src/lib/libc/port/gen/getentropy.c#L33 | ||
//! - On Solaris, both sources use FIPS 140-2 / NIST SP-900-90A DRBGs, see: | ||
//! https://blogs.oracle.com/solaris/post/solaris-new-system-calls-getentropy2-and-getrandom2 | ||
//! - On Redox, only /dev/urandom is provided. | ||
//! - On AIX, /dev/urandom will "provide cryptographically secure output". | ||
//! - On Haiku, QNX Neutrino, DragonFly, and FreeBSD, they are identical. | ||
use crate::{util_libc::sys_fill_exact, Error}; | ||
use core::mem::MaybeUninit; | ||
|
||
// On Solaris 11.3, getrandom() will fail if bufsz > 1024 (bufsz > 133120 on Solaris 11.4). | ||
// This issue is not present in Illumos's implementation of getrandom(). | ||
#[cfg(target_os = "solaris")] | ||
const MAX_BYTES: usize = 1024; | ||
#[cfg(not(target_os = "solaris"))] | ||
const MAX_BYTES: usize = usize::MAX; | ||
|
||
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
sys_fill_exact(dest, |buf| unsafe { | ||
libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0) | ||
}) | ||
for chunk in dest.chunks_mut(MAX_BYTES) { | ||
sys_fill_exact(chunk, |buf| unsafe { | ||
libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0) | ||
})?; | ||
} | ||
Ok(()) | ||
} |
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 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