Skip to content

Commit

Permalink
Unify getentropy-based implementations (#418)
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Richey <joerichey@google.com>
  • Loading branch information
josephlr committed May 2, 2024
1 parent dca4961 commit 20c2213
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 61 deletions.
13 changes: 0 additions & 13 deletions src/emscripten.rs

This file was deleted.

11 changes: 8 additions & 3 deletions src/openbsd.rs → 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<u8>]) -> 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());
}
}
Expand Down
24 changes: 10 additions & 14 deletions src/lib.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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")] {
Expand All @@ -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;
Expand Down
18 changes: 0 additions & 18 deletions src/macos.rs

This file was deleted.

13 changes: 0 additions & 13 deletions src/vita.rs

This file was deleted.

0 comments on commit 20c2213

Please sign in to comment.