diff options
Diffstat (limited to 'vendor/getrandom/src/use_file.rs')
-rw-r--r-- | vendor/getrandom/src/use_file.rs | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/vendor/getrandom/src/use_file.rs b/vendor/getrandom/src/use_file.rs index 16c0216b6..a6ef0d235 100644 --- a/vendor/getrandom/src/use_file.rs +++ b/vendor/getrandom/src/use_file.rs @@ -14,34 +14,33 @@ use crate::{ }; use core::{ cell::UnsafeCell, + mem::MaybeUninit, sync::atomic::{AtomicUsize, Ordering::Relaxed}, }; +// We prefer using /dev/urandom and only use /dev/random if the OS +// documentation indicates that /dev/urandom is insecure. +// On Solaris/Illumos, see src/solaris_illumos.rs +// On Dragonfly, Haiku, macOS, and QNX Neutrino the devices are identical. +#[cfg(any(target_os = "solaris", target_os = "illumos"))] +const FILE_PATH: &str = "/dev/random\0"; #[cfg(any( + target_os = "aix", + target_os = "android", + target_os = "linux", + target_os = "redox", target_os = "dragonfly", - target_os = "emscripten", target_os = "haiku", target_os = "macos", - target_os = "solaris", - target_os = "illumos" + target_os = "nto", ))] -const FILE_PATH: &str = "/dev/random\0"; -#[cfg(any(target_os = "android", target_os = "linux", target_os = "redox"))] const FILE_PATH: &str = "/dev/urandom\0"; -pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { let fd = get_rng_fd()?; - let read = |buf: &mut [u8]| unsafe { libc::read(fd, buf.as_mut_ptr() as *mut _, buf.len()) }; - - if cfg!(target_os = "emscripten") { - // `Crypto.getRandomValues` documents `dest` should be at most 65536 bytes. - for chunk in dest.chunks_mut(65536) { - sys_fill_exact(chunk, read)?; - } - } else { - sys_fill_exact(dest, read)?; - } - Ok(()) + sys_fill_exact(dest, |buf| unsafe { + libc::read(fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len()) + }) } // Returns the file descriptor for the device file used to retrieve random |