Skip to content

Commit e535bc8

Browse files
committed
Switch Solaris to getentropy
1 parent 495fefd commit e535bc8

File tree

3 files changed

+13
-35
lines changed

3 files changed

+13
-35
lines changed

src/getrandom.rs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Implementation using libc::getrandom
1+
//! Implementation using `libc::getrandom`.
22
//!
33
//! Available since:
44
//! - Linux Kernel 3.17, Glibc 2.25, Musl 1.1.20
@@ -10,34 +10,11 @@
1010
//! - DragonFly 5.7
1111
//! - Hurd Glibc 2.31
1212
//! - shim-3ds since Feb 2022
13-
//!
14-
//! For all platforms, we use the default randomness source (the one used
15-
//! by /dev/urandom) rather than the /dev/random (GRND_RANDOM) source. For
16-
//! more information see the linked man pages in lib.rs.
17-
//! - On Linux, "/dev/urandom is preferred and sufficient in all use cases".
18-
//! - On NetBSD, "there is no reason to ever use" GRND_RANDOM.
19-
//! - On Illumos, the default source is used for getentropy() and the like:
20-
//! https://github.com/illumos/illumos-gate/blob/89cf0c2ce8a47dcf555bb1596f9034f07b9467fa/usr/src/lib/libc/port/gen/getentropy.c#L33
21-
//! - On Solaris, both sources use FIPS 140-2 / NIST SP-900-90A DRBGs, see:
22-
//! https://blogs.oracle.com/solaris/post/solaris-new-system-calls-getentropy2-and-getrandom2
23-
//! - On Redox, only /dev/urandom is provided.
24-
//! - On AIX, /dev/urandom will "provide cryptographically secure output".
25-
//! - On Haiku, QNX Neutrino, DragonFly, and FreeBSD, they are identical.
2613
use crate::{util_libc::sys_fill_exact, Error};
2714
use core::mem::MaybeUninit;
2815

29-
// On Solaris 11.3, getrandom() will fail if bufsz > 1024 (bufsz > 133120 on Solaris 11.4).
30-
// This issue is not present in Illumos's implementation of getrandom().
31-
#[cfg(target_os = "solaris")]
32-
const MAX_BYTES: usize = 1024;
33-
#[cfg(not(target_os = "solaris"))]
34-
const MAX_BYTES: usize = usize::MAX;
35-
3616
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
37-
for chunk in dest.chunks_mut(MAX_BYTES) {
38-
sys_fill_exact(chunk, |buf| unsafe {
39-
libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0)
40-
})?;
41-
}
42-
Ok(())
17+
sys_fill_exact(dest, |buf| unsafe {
18+
libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0)
19+
})
4320
}

src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! | OpenBSD | `*‑openbsd` | [`getentropy`][7]
1313
//! | NetBSD | `*‑netbsd` | [`getrandom`][16] if available, otherwise [`kern.arandom`][8]
1414
//! | Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9]
15-
//! | Solaris | `*‑solaris` | [`getrandom`][11]
15+
//! | Solaris | `*‑solaris` | [`getentropy`][11]
1616
//! | Illumos | `*‑illumos` | [`getrandom`][12]
1717
//! | Fuchsia OS | `*‑fuchsia` | [`cprng_draw`]
1818
//! | Redox | `*‑redox` | `/dev/urandom`
@@ -31,10 +31,6 @@
3131
//! | QNX Neutrino | `*‑nto-qnx*` | [`/dev/urandom`][14] (identical to `/dev/random`)
3232
//! | AIX | `*-ibm-aix` | [`/dev/urandom`][15]
3333
//!
34-
//! There is no blanket implementation on `unix` targets that reads from
35-
//! `/dev/urandom`. This ensures all supported targets are using the recommended
36-
//! interface and respect maximum buffer sizes.
37-
//!
3834
//! Pull Requests that add support for new targets to `getrandom` are always welcome.
3935
//!
4036
//! ## Unsupported targets
@@ -177,7 +173,7 @@
177173
//! [7]: https://man.openbsd.org/getentropy.2
178174
//! [8]: https://man.netbsd.org/sysctl.7
179175
//! [9]: https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom
180-
//! [11]: https://docs.oracle.com/cd/E88353_01/html/E37841/getrandom-2.html
176+
//! [11]: https://docs.oracle.com/cd/E88353_01/html/E37841/getentropy-2.html
181177
//! [12]: https://illumos.org/man/2/getrandom
182178
//! [13]: https://github.com/emscripten-core/emscripten/pull/12240
183179
//! [14]: https://www.qnx.com/developers/docs/7.1/index.html#com.qnx.doc.neutrino.utilities/topic/r/random.html
@@ -242,6 +238,7 @@ cfg_if! {
242238
} else if #[cfg(any(
243239
target_os = "macos",
244240
target_os = "openbsd",
241+
target_os = "solaris",
245242
target_os = "vita",
246243
target_os = "emscripten",
247244
))] {
@@ -252,7 +249,6 @@ cfg_if! {
252249
target_os = "freebsd",
253250
target_os = "hurd",
254251
target_os = "illumos",
255-
target_os = "solaris",
256252
// Check for target_arch = "arm" to only include the 3DS. Does not
257253
// include the Nintendo Switch (which is target_arch = "aarch64").
258254
all(target_os = "horizon", target_arch = "arm"),

src/use_file.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ use core::{
99
sync::atomic::{AtomicUsize, Ordering::Relaxed},
1010
};
1111

12-
// We always use /dev/urandom, see the comment in getrandom.rs.
12+
/// For all platforms, we use `/dev/urandom` rather than `/dev/random`.
13+
/// For more information see the linked man pages in lib.rs.
14+
/// - On Linux, "/dev/urandom is preferred and sufficient in all use cases".
15+
/// - On Redox, only /dev/urandom is provided.
16+
/// - On AIX, /dev/urandom will "provide cryptographically secure output".
17+
/// - On Haiku and QNX Neutrino they are identical.
1318
const FILE_PATH: &str = "/dev/urandom\0";
1419
const FD_UNINIT: usize = usize::max_value();
1520

0 commit comments

Comments
 (0)