diff --git a/src/emscripten.rs b/src/emscripten.rs deleted file mode 100644 index 30221c6e..00000000 --- a/src/emscripten.rs +++ /dev/null @@ -1,13 +0,0 @@ -//! Implementation for Emscripten -use crate::{util_libc::last_os_error, Error}; -use core::mem::MaybeUninit; - -pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { - // Emscripten 2.0.5 added getentropy, so we can use it unconditionally. - // Unlike other getentropy implementations, there is no max buffer length. - let ret = unsafe { libc::getentropy(dest.as_mut_ptr() as *mut libc::c_void, dest.len()) }; - if ret < 0 { - return Err(last_os_error()); - } - Ok(()) -} diff --git a/src/openbsd.rs b/src/getentropy.rs similarity index 65% rename from src/openbsd.rs rename to src/getentropy.rs index f4d64daf..1d35b572 100644 --- a/src/openbsd.rs +++ b/src/getentropy.rs @@ -1,12 +1,17 @@ -//! Implementation for OpenBSD +//! Implementation using libc::getentropy +//! +//! Available since: +//! - macOS 10.12 +//! - OpenBSD 5.6 +//! - Emscripten 2.0.5 +//! - vita newlib since Dec 2021 use crate::{util_libc::last_os_error, Error}; use core::mem::MaybeUninit; pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { - // getentropy(2) was added in OpenBSD 5.6, so we can use it unconditionally. for chunk in dest.chunks_mut(256) { let ret = unsafe { libc::getentropy(chunk.as_mut_ptr() as *mut libc::c_void, chunk.len()) }; - if ret == -1 { + if ret != 0 { return Err(last_os_error()); } } diff --git a/src/lib.rs b/src/lib.rs index d44d87f0..6823041f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,8 +26,8 @@ //! | Web Browser and Node.js | `wasm*‑*‑unknown` | [`Crypto.getRandomValues`] if available, then [`crypto.randomFillSync`] if on Node.js, see [WebAssembly support] //! | SOLID | `*-kmc-solid_*` | `SOLID_RNG_SampleRandomBytes` //! | Nintendo 3DS | `armv6k-nintendo-3ds` | [`getrandom`][1] -//! | PS Vita | `armv7-sony-vita-newlibeabihf` | [`getentropy`][13] -//! | QNX Neutrino | `*‑nto-qnx*` | [`/dev/urandom`][14] (identical to `/dev/random`) +//! | PS Vita | `*-vita-*` | [`getentropy`][13] +//! | QNX Neutrino | `*‑nto-qnx*` | [`/dev/urandom`][14] (identical to `/dev/random`) //! | AIX | `*-ibm-aix` | [`/dev/urandom`][15] //! //! There is no blanket implementation on `unix` targets that reads from @@ -237,6 +237,14 @@ cfg_if! { if #[cfg(any(target_os = "haiku", target_os = "redox", target_os = "nto", target_os = "aix"))] { mod util_libc; #[path = "use_file.rs"] mod imp; + } else if #[cfg(any( + target_os = "macos", + target_os = "openbsd", + target_os = "vita", + target_os = "emscripten", + ))] { + mod util_libc; + #[path = "getentropy.rs"] mod imp; } else if #[cfg(any( target_os = "dragonfly", target_os = "freebsd", @@ -305,12 +313,6 @@ cfg_if! { #[path = "fuchsia.rs"] mod imp; } else if #[cfg(any(target_os = "ios", target_os = "visionos", target_os = "watchos", target_os = "tvos"))] { #[path = "apple-other.rs"] mod imp; - } else if #[cfg(target_os = "macos")] { - mod util_libc; - #[path = "macos.rs"] mod imp; - } else if #[cfg(target_os = "openbsd")] { - mod util_libc; - #[path = "openbsd.rs"] mod imp; } else if #[cfg(all(target_arch = "wasm32", target_os = "wasi"))] { #[path = "wasi.rs"] mod imp; } else if #[cfg(target_os = "hermit")] { @@ -324,12 +326,6 @@ cfg_if! { #[path = "espidf.rs"] mod imp; } else if #[cfg(windows)] { #[path = "windows.rs"] mod imp; - } else if #[cfg(target_os = "vita")] { - mod util_libc; - #[path = "vita.rs"] mod imp; - } else if #[cfg(target_os = "emscripten")] { - mod util_libc; - #[path = "emscripten.rs"] mod imp; } else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] { mod lazy; #[path = "rdrand.rs"] mod imp; diff --git a/src/macos.rs b/src/macos.rs deleted file mode 100644 index 44af76b0..00000000 --- a/src/macos.rs +++ /dev/null @@ -1,18 +0,0 @@ -//! Implementation for macOS -use crate::{util_libc::last_os_error, Error}; -use core::mem::MaybeUninit; - -extern "C" { - // Supported as of macOS 10.12+. - fn getentropy(buf: *mut u8, size: libc::size_t) -> libc::c_int; -} - -pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { - for chunk in dest.chunks_mut(256) { - let ret = unsafe { getentropy(chunk.as_mut_ptr() as *mut u8, chunk.len()) }; - if ret != 0 { - return Err(last_os_error()); - } - } - Ok(()) -} diff --git a/src/vita.rs b/src/vita.rs deleted file mode 100644 index 20a98782..00000000 --- a/src/vita.rs +++ /dev/null @@ -1,13 +0,0 @@ -//! Implementation for PS Vita -use crate::{util_libc::last_os_error, Error}; -use core::mem::MaybeUninit; - -pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { - for chunk in dest.chunks_mut(256) { - let ret = unsafe { libc::getentropy(chunk.as_mut_ptr() as *mut libc::c_void, chunk.len()) }; - if ret == -1 { - return Err(last_os_error()); - } - } - Ok(()) -}