|
1 | | -//! Implementation using libc::getrandom |
| 1 | +//! Implementation using `libc::getrandom`. |
2 | 2 | //! |
3 | 3 | //! Available since: |
4 | 4 | //! - Linux Kernel 3.17, Glibc 2.25, Musl 1.1.20 |
|
10 | 10 | //! - DragonFly 5.7 |
11 | 11 | //! - Hurd Glibc 2.31 |
12 | 12 | //! - 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. |
26 | 13 | use crate::{util_libc::sys_fill_exact, Error}; |
27 | 14 | use core::mem::MaybeUninit; |
28 | 15 |
|
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 | | - |
36 | 16 | 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 | + }) |
43 | 20 | } |
0 commit comments