Skip to content

Commit 20c2213

Browse files
authored
Unify getentropy-based implementations (#418)
Signed-off-by: Joe Richey <[email protected]>
1 parent dca4961 commit 20c2213

File tree

5 files changed

+18
-61
lines changed

5 files changed

+18
-61
lines changed

src/emscripten.rs

-13
This file was deleted.

src/openbsd.rs src/getentropy.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
//! Implementation for OpenBSD
1+
//! Implementation using libc::getentropy
2+
//!
3+
//! Available since:
4+
//! - macOS 10.12
5+
//! - OpenBSD 5.6
6+
//! - Emscripten 2.0.5
7+
//! - vita newlib since Dec 2021
28
use crate::{util_libc::last_os_error, Error};
39
use core::mem::MaybeUninit;
410

511
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
6-
// getentropy(2) was added in OpenBSD 5.6, so we can use it unconditionally.
712
for chunk in dest.chunks_mut(256) {
813
let ret = unsafe { libc::getentropy(chunk.as_mut_ptr() as *mut libc::c_void, chunk.len()) };
9-
if ret == -1 {
14+
if ret != 0 {
1015
return Err(last_os_error());
1116
}
1217
}

src/lib.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
//! | Web Browser and Node.js | `wasm*‑*‑unknown` | [`Crypto.getRandomValues`] if available, then [`crypto.randomFillSync`] if on Node.js, see [WebAssembly support]
2727
//! | SOLID | `*-kmc-solid_*` | `SOLID_RNG_SampleRandomBytes`
2828
//! | Nintendo 3DS | `armv6k-nintendo-3ds` | [`getrandom`][1]
29-
//! | PS Vita | `armv7-sony-vita-newlibeabihf` | [`getentropy`][13]
30-
//! | QNX Neutrino | `*‑nto-qnx*` | [`/dev/urandom`][14] (identical to `/dev/random`)
29+
//! | PS Vita | `*-vita-*` | [`getentropy`][13]
30+
//! | QNX Neutrino | `*‑nto-qnx*` | [`/dev/urandom`][14] (identical to `/dev/random`)
3131
//! | AIX | `*-ibm-aix` | [`/dev/urandom`][15]
3232
//!
3333
//! There is no blanket implementation on `unix` targets that reads from
@@ -237,6 +237,14 @@ cfg_if! {
237237
if #[cfg(any(target_os = "haiku", target_os = "redox", target_os = "nto", target_os = "aix"))] {
238238
mod util_libc;
239239
#[path = "use_file.rs"] mod imp;
240+
} else if #[cfg(any(
241+
target_os = "macos",
242+
target_os = "openbsd",
243+
target_os = "vita",
244+
target_os = "emscripten",
245+
))] {
246+
mod util_libc;
247+
#[path = "getentropy.rs"] mod imp;
240248
} else if #[cfg(any(
241249
target_os = "dragonfly",
242250
target_os = "freebsd",
@@ -305,12 +313,6 @@ cfg_if! {
305313
#[path = "fuchsia.rs"] mod imp;
306314
} else if #[cfg(any(target_os = "ios", target_os = "visionos", target_os = "watchos", target_os = "tvos"))] {
307315
#[path = "apple-other.rs"] mod imp;
308-
} else if #[cfg(target_os = "macos")] {
309-
mod util_libc;
310-
#[path = "macos.rs"] mod imp;
311-
} else if #[cfg(target_os = "openbsd")] {
312-
mod util_libc;
313-
#[path = "openbsd.rs"] mod imp;
314316
} else if #[cfg(all(target_arch = "wasm32", target_os = "wasi"))] {
315317
#[path = "wasi.rs"] mod imp;
316318
} else if #[cfg(target_os = "hermit")] {
@@ -324,12 +326,6 @@ cfg_if! {
324326
#[path = "espidf.rs"] mod imp;
325327
} else if #[cfg(windows)] {
326328
#[path = "windows.rs"] mod imp;
327-
} else if #[cfg(target_os = "vita")] {
328-
mod util_libc;
329-
#[path = "vita.rs"] mod imp;
330-
} else if #[cfg(target_os = "emscripten")] {
331-
mod util_libc;
332-
#[path = "emscripten.rs"] mod imp;
333329
} else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] {
334330
mod lazy;
335331
#[path = "rdrand.rs"] mod imp;

src/macos.rs

-18
This file was deleted.

src/vita.rs

-13
This file was deleted.

0 commit comments

Comments
 (0)