1 //! Implementation for the Solaris family
2 //!
3 //! `/dev/random` uses the Hash_DRBG with SHA512 algorithm from NIST SP 800-90A.
4 //! `/dev/urandom` uses the FIPS 186-2 algorithm, which is considered less
5 //! secure. We choose to read from `/dev/random` (and use GRND_RANDOM).
6 //!
7 //! Solaris 11.3 and late-2018 illumos added the getrandom(2) libc function.
8 //! To make sure we can compile on both Solaris and its derivatives, as well as
9 //! function, we check for the existence of getrandom(2) in libc by calling
10 //! libc::dlsym.
11 use crate::{
12 use_file,
13 util_libc::{sys_fill_exact, Weak},
14 Error,
15 };
16 use core::mem::{self, MaybeUninit};
17
18 static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") };
19 type GetRandomFn =
20 unsafe extern "C" fn(*mut libc::c_void, libc::size_t, libc::c_uint) -> libc::ssize_t;
21
getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>22 pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
23 if let Some(fptr) = GETRANDOM.ptr() {
24 let func: GetRandomFn = unsafe { mem::transmute(fptr) };
25 // 256 bytes is the lowest common denominator across all the Solaris
26 // derived platforms for atomically obtaining random data.
27 for chunk in dest.chunks_mut(256) {
28 sys_fill_exact(chunk, |buf| unsafe {
29 // A cast is needed for the flags as libc uses the wrong type.
30 func(
31 buf.as_mut_ptr() as *mut libc::c_void,
32 buf.len(),
33 libc::GRND_RANDOM as libc::c_uint,
34 )
35 })?
36 }
37 Ok(())
38 } else {
39 use_file::getrandom_inner(dest)
40 }
41 }
42