From 4374fd7783be893d3d88e6d65c7dd4a9264fee0a Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 18 Feb 2019 12:09:23 +0000 Subject: [PATCH 1/4] Add getrandom wrapper func and documentation --- README.md | 15 +++++- src/cloudabi.rs | 2 +- src/dragonfly_haiku.rs | 2 +- src/dummy.rs | 2 +- src/emscripten.rs | 2 +- src/freebsd.rs | 2 +- src/fuchsia.rs | 2 +- src/lib.rs | 108 ++++++++++++++++++++++++++++++++++++++++- src/linux_android.rs | 2 +- src/macos.rs | 2 +- src/netbsd.rs | 2 +- src/openbsd_bitrig.rs | 2 +- src/redox.rs | 2 +- src/sgx.rs | 2 +- src/solaris.rs | 2 +- src/windows.rs | 2 +- 16 files changed, 135 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a9c8fb087..b296cd7a7 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,11 @@ A Rust library to securely get random entropy. This crate derives its name from Linux's `getrandom` function, but is cross platform, roughly supporting the same set of platforms as Rust's `std` lib. +This is a low-level API. Most users should prefer a high-level random-number +library like [Rand] or a cryptography library. + +[Rand]: https://crates.io/crates/rand + ## Usage @@ -19,7 +24,15 @@ Add this to your `Cargo.toml`: getrandom = "0.1" ``` -TODO +Then invoke the `getrandom` function: + +```rust +fn get_random_buf() -> Result<[u8; 32], getrandom::Error> { + let mut buf = [0u8; 32]; + getrandom::getrandom(&mut buf)?; + buf +} +``` # License diff --git a/src/cloudabi.rs b/src/cloudabi.rs index 775150502..ee93c052f 100644 --- a/src/cloudabi.rs +++ b/src/cloudabi.rs @@ -11,7 +11,7 @@ extern "C" { fn cloudabi_sys_random_get(buf: *mut u8, len: usize) -> u16; } -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { let errno = unsafe { cloudabi_sys_random_get(dest.as_ptr(), dest.len()) }; if errno == 0 { Ok(()) diff --git a/src/dragonfly_haiku.rs b/src/dragonfly_haiku.rs index 7691616ed..1d6a0b753 100644 --- a/src/dragonfly_haiku.rs +++ b/src/dragonfly_haiku.rs @@ -15,7 +15,7 @@ use std::cell::RefCell; thread_local!(static RNG_FILE: RefCell> = RefCell::new(None)); -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { RNG_FILE.with(|f| { use_init(f, || File::open("/dev/random").map_err(From::from), diff --git a/src/dummy.rs b/src/dummy.rs index 41fa39fd3..6763fcdc4 100644 --- a/src/dummy.rs +++ b/src/dummy.rs @@ -10,6 +10,6 @@ //! `Err(UNAVAILABLE_ERROR)` use super::UNAVAILABLE_ERROR; -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { Err(UNAVAILABLE_ERROR) } diff --git a/src/emscripten.rs b/src/emscripten.rs index 446e46345..37a1ae11d 100644 --- a/src/emscripten.rs +++ b/src/emscripten.rs @@ -15,7 +15,7 @@ use super::utils::use_init; thread_local!(static RNG_FILE: RefCell> = RefCell::new(None)); -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { // `Crypto.getRandomValues` documents `dest` should be at most 65536 // bytes. `crypto.randomBytes` documents: "To minimize threadpool // task length variation, partition large randomBytes requests when diff --git a/src/freebsd.rs b/src/freebsd.rs index ac990f3f4..e59076ed2 100644 --- a/src/freebsd.rs +++ b/src/freebsd.rs @@ -13,7 +13,7 @@ use super::Error; use core::ptr; use std::io; -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { let mib = [libc::CTL_KERN, libc::KERN_ARND]; // kern.arandom permits a maximum buffer size of 256 bytes for chunk in dest.chunks_mut(256) { diff --git a/src/fuchsia.rs b/src/fuchsia.rs index b152cde15..82382b7b2 100644 --- a/src/fuchsia.rs +++ b/src/fuchsia.rs @@ -11,7 +11,7 @@ extern crate fuchsia_cprng; use super::Error; -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { fuchsia_cprng::cprng_draw(dest); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 104b58844..471ce0289 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,91 @@ // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. + +//! Interface to the random number generator of the operating system. +//! +//! # Platform sources +//! +//! | OS | interface +//! |------------------|--------------------------------------------------------- +//! | Linux, Android | [`getrandom`][1] system call if available, otherwise [`/dev/urandom`][2] after reading from `/dev/random` once +//! | Windows | [`RtlGenRandom`][3] +//! | macOS, iOS | [`SecRandomCopyBytes`][4] +//! | FreeBSD | [`kern.arandom`][5] +//! | OpenBSD, Bitrig | [`getentropy`][6] +//! | NetBSD | [`/dev/urandom`][7] after reading from `/dev/random` once +//! | Dragonfly BSD | [`/dev/random`][8] +//! | Solaris, illumos | [`getrandom`][9] system call if available, otherwise [`/dev/random`][10] +//! | Fuchsia OS | [`cprng_draw`][11] +//! | Redox | [`rand:`][12] +//! | CloudABI | [`random_get`][13] +//! | Haiku | `/dev/random` (identical to `/dev/urandom`) +//! | SGX | RDRAND +//! | Web browsers | [`Crypto.getRandomValues`][14] (see [Support for WebAssembly and ams.js][14]) +//! | Node.js | [`crypto.randomBytes`][15] (see [Support for WebAssembly and ams.js][16]) +//! +//! Getrandom doesn't have a blanket implementation for all Unix-like operating +//! systems that reads from `/dev/urandom`. This ensures all supported operating +//! systems are using the recommended interface and respect maximum buffer +//! sizes. +//! +//! ## Support for WebAssembly and ams.js +//! +//! The three Emscripten targets `asmjs-unknown-emscripten`, +//! `wasm32-unknown-emscripten` and `wasm32-experimental-emscripten` use +//! Emscripten's emulation of `/dev/random` on web browsers and Node.js. +//! +//! The bare WASM target `wasm32-unknown-unknown` tries to call the javascript +//! methods directly, using either `stdweb` or `wasm-bindgen` depending on what +//! features are activated for this crate. Note that if both features are +//! enabled `wasm-bindgen` will be used. +//! +//! ## Early boot +//! +//! It is possible that early in the boot process the OS hasn't had enough time +//! yet to collect entropy to securely seed its RNG, especially on virtual +//! machines. +//! +//! Some operating systems always block the thread until the RNG is securely +//! seeded. This can take anywhere from a few seconds to more than a minute. +//! Others make a best effort to use a seed from before the shutdown and don't +//! document much. +//! +//! A few, Linux, NetBSD and Solaris, offer a choice between blocking and +//! getting an error; in these cases we always choose to block. +//! +//! On Linux (when the `genrandom` system call is not available) and on NetBSD +//! reading from `/dev/urandom` never blocks, even when the OS hasn't collected +//! enough entropy yet. To avoid returning low-entropy bytes, we first read from +//! `/dev/random` and only switch to `/dev/urandom` once this has succeeded. +//! +//! # Error handling +//! +//! We always choose failure over returning insecure "random" bytes. In general, +//! on supported platforms, failure is unlikely, though not impossible. If an +//! error does occur, then it is likely that it will occur on every call to +//! `getrandom`, hence after the first successful call one can be reasonably +//! confident that no errors will occur. +//! +//! On unsupported platforms, `getrandom` always fails. +//! +//! [1]: http://man7.org/linux/man-pages/man2/getrandom.2.html +//! [2]: http://man7.org/linux/man-pages/man4/urandom.4.html +//! [3]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx +//! [4]: https://developer.apple.com/documentation/security/1399291-secrandomcopybytes?language=objc +//! [5]: https://www.freebsd.org/cgi/man.cgi?query=random&sektion=4 +//! [6]: https://man.openbsd.org/getentropy.2 +//! [7]: http://netbsd.gw.com/cgi-bin/man-cgi?random+4+NetBSD-current +//! [8]: https://leaf.dragonflybsd.org/cgi/web-man?command=random§ion=4 +//! [9]: https://docs.oracle.com/cd/E88353_01/html/E37841/getrandom-2.html +//! [10]: https://docs.oracle.com/cd/E86824_01/html/E54777/random-7d.html +//! [11]: https://fuchsia.googlesource.com/zircon/+/HEAD/docs/syscalls/cprng_draw.md +//! [12]: https://github.com/redox-os/randd/blob/master/src/main.rs +//! [13]: https://github.com/NuxiNL/cloudabi/blob/v0.20/cloudabi.txt#L1826 +//! [14]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues +//! [15]: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback +//! [16]: #support-for-webassembly-and-amsjs + #![no_std] #[cfg(not(target_env = "sgx"))] @@ -24,12 +109,17 @@ mod utils; mod error; pub use error::{Error, UNKNOWN_ERROR, UNAVAILABLE_ERROR}; + +// System-specific implementations. +// +// These should all provide getrandom_os with the same signature as getrandom. + macro_rules! mod_use { ($cond:meta, $module:ident) => { #[$cond] mod $module; #[$cond] - pub use $module::getrandom; + use $module::getrandom_os; } } @@ -100,3 +190,19 @@ mod_use!( ))), dummy ); + + +/// Fill `dest` with random bytes from the system's preferred random number +/// source. +/// +/// This function returns an error on any failure, including partial reads. We +/// make no guarantees regarding the contents of `dest` on error. +/// +/// Blocking is possible, at least during early boot; see module documentation. +/// +/// In general, `getrandom` will be fast enough for interactive usage, though +/// significantly slower than a user-space CSPRNG; for the latter consider +/// [`rand::thread_rng`](https://docs.rs/rand/*/rand/fn.thread_rng.html). +pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { + getrandom_os(dest) +} diff --git a/src/linux_android.rs b/src/linux_android.rs index db6c6e481..4775fe0c0 100644 --- a/src/linux_android.rs +++ b/src/linux_android.rs @@ -38,7 +38,7 @@ fn syscall_getrandom(dest: &mut [u8]) -> Result<(), io::Error> { Ok(()) } -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { RNG_SOURCE.with(|f| { use_init(f, || { diff --git a/src/macos.rs b/src/macos.rs index 3855f1ac6..baafaaf64 100644 --- a/src/macos.rs +++ b/src/macos.rs @@ -21,7 +21,7 @@ extern { ) -> c_int; } -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { let ret = unsafe { SecRandomCopyBytes( kSecRandomDefault, diff --git a/src/netbsd.rs b/src/netbsd.rs index f2aa68ca4..e76ec025b 100644 --- a/src/netbsd.rs +++ b/src/netbsd.rs @@ -19,7 +19,7 @@ static RNG_INIT: AtomicBool = ATOMIC_BOOL_INIT; thread_local!(static RNG_FILE: RefCell> = RefCell::new(None)); -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { RNG_FILE.with(|f| { use_init(f, || { // read one byte from "/dev/random" to ensure that diff --git a/src/openbsd_bitrig.rs b/src/openbsd_bitrig.rs index f8096fa29..675c2ca6c 100644 --- a/src/openbsd_bitrig.rs +++ b/src/openbsd_bitrig.rs @@ -12,7 +12,7 @@ extern crate libc; use super::Error; use std::io; -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { for chunk in dest.chunks_mut(256) { let ret = unsafe { libc::getentropy( diff --git a/src/redox.rs b/src/redox.rs index 0c7e85786..87dfaf9eb 100644 --- a/src/redox.rs +++ b/src/redox.rs @@ -15,7 +15,7 @@ use std::cell::RefCell; thread_local!(static RNG_FILE: RefCell> = RefCell::new(None)); -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { RNG_FILE.with(|f| { use_init(f, || File::open("rand:").map_err(From::from), diff --git a/src/sgx.rs b/src/sgx.rs index a98f55f11..e4e7ba55d 100644 --- a/src/sgx.rs +++ b/src/sgx.rs @@ -29,7 +29,7 @@ fn get_rand_u64() -> Result { Err(UNKNOWN_ERROR) } -pub fn getrandom(mut dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(mut dest: &mut [u8]) -> Result<(), Error> { while dest.len() >= 8 { let (chunk, left) = {dest}.split_at_mut(8); dest = left; diff --git a/src/solaris.rs b/src/solaris.rs index e27e4bc0d..f081eb764 100644 --- a/src/solaris.rs +++ b/src/solaris.rs @@ -53,7 +53,7 @@ fn syscall_getrandom(dest: &mut [u8]) -> Result<(), Error> { Ok(()) } -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { // The documentation says 1024 is the maximum for getrandom // and 1040 for /dev/random. RNG_SOURCE.with(|f| { diff --git a/src/windows.rs b/src/windows.rs index 6507b6cdd..55c9e3b7d 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -15,7 +15,7 @@ use self::winapi::um::winnt::PVOID; use std::io; use super::Error; -pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { let ret = unsafe { RtlGenRandom(dest.as_mut_ptr() as PVOID, dest.len() as ULONG) }; From 9ba1d379520b038b2ce2f72f6c9e0b46ed039f68 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 18 Feb 2019 12:23:11 +0000 Subject: [PATCH 2/4] Impl From not Into --- src/error.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/error.rs b/src/error.rs index a6dd3d7e3..eb6b5e3b3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -51,9 +51,9 @@ impl From for Error { } #[cfg(not(target_env = "sgx"))] -impl Into for Error { - fn into(self) -> io::Error { - match self { +impl From for io::Error { + fn from(err: Error) -> Self { + match err { UNKNOWN_ERROR => io::Error::new(io::ErrorKind::Other, "getrandom error: unknown"), UNAVAILABLE_ERROR => io::Error::new(io::ErrorKind::Other, From 7ff3aff0765ef387cc65e44d5e4863d2a57b0ffb Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 18 Feb 2019 12:50:37 +0000 Subject: [PATCH 3/4] Error doc; fix for Solaris --- src/error.rs | 11 +++++++++++ src/solaris.rs | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/error.rs b/src/error.rs index eb6b5e3b3..b3fca9d8d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,18 +12,29 @@ use core::fmt; #[cfg(not(target_env = "sgx"))] use std::{io, error}; +/// An unknown error. pub const UNKNOWN_ERROR: Error = Error(unsafe { NonZeroU32::new_unchecked(0x756e6b6e) // "unkn" }); +/// No generator is available. pub const UNAVAILABLE_ERROR: Error = Error(unsafe { NonZeroU32::new_unchecked(0x4e416e61) // "NAna" }); +/// The error type. +/// +/// This type is small and no-std compatible. #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct Error(NonZeroU32); impl Error { + /// Extract the error code. + /// + /// This may equal one of the codes defined in this library or may be a + /// system error code. + /// + /// One may attempt to format this error via the `Display` implementation. pub fn code(&self) -> NonZeroU32 { self.0 } diff --git a/src/solaris.rs b/src/solaris.rs index f081eb764..20305ad24 100644 --- a/src/solaris.rs +++ b/src/solaris.rs @@ -48,7 +48,7 @@ fn syscall_getrandom(dest: &mut [u8]) -> Result<(), Error> { syscall(SYS_GETRANDOM, dest.as_mut_ptr(), dest.len(), 0) }; if ret == -1 || ret != dest.len() as i64 { - return Err(io::Error::last_os_error().from()); + return Err(io::Error::last_os_error().into()); } Ok(()) } @@ -75,7 +75,7 @@ pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { }, } }) - }).map_err(|_| Error::Unknown) + }) } fn is_getrandom_available() -> bool { From 0f371be445d2c4ea7f378ccd425ae56a0ca08c01 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 18 Feb 2019 14:38:53 +0000 Subject: [PATCH 4/4] =?UTF-8?q?Rename=20getrandom=5Fos=20=E2=86=92=20getra?= =?UTF-8?q?ndom=5Finner;=20more=20WASM=20doc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++++++++ src/cloudabi.rs | 2 +- src/dragonfly_haiku.rs | 2 +- src/dummy.rs | 2 +- src/emscripten.rs | 2 +- src/freebsd.rs | 2 +- src/fuchsia.rs | 2 +- src/lib.rs | 9 +++++---- src/linux_android.rs | 2 +- src/macos.rs | 2 +- src/netbsd.rs | 2 +- src/openbsd_bitrig.rs | 2 +- src/redox.rs | 2 +- src/sgx.rs | 2 +- src/solaris.rs | 2 +- src/windows.rs | 2 +- 16 files changed, 29 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b296cd7a7..fc47f2e28 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,16 @@ fn get_random_buf() -> Result<[u8; 32], getrandom::Error> { } ``` +## Features + +This library is `no_std` compatible on SGX but requires `std` on most platforms. + +For WebAssembly (`wasm32`), Enscripten targets are supported directly; otherwise +one of the following features must be enabled: + +- [`wasm-bindgen`](https://crates.io/crates/wasm_bindgen) +- [`stdweb`](https://crates.io/crates/stdweb) + # License diff --git a/src/cloudabi.rs b/src/cloudabi.rs index ee93c052f..f30e064a8 100644 --- a/src/cloudabi.rs +++ b/src/cloudabi.rs @@ -11,7 +11,7 @@ extern "C" { fn cloudabi_sys_random_get(buf: *mut u8, len: usize) -> u16; } -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { let errno = unsafe { cloudabi_sys_random_get(dest.as_ptr(), dest.len()) }; if errno == 0 { Ok(()) diff --git a/src/dragonfly_haiku.rs b/src/dragonfly_haiku.rs index 1d6a0b753..751266be3 100644 --- a/src/dragonfly_haiku.rs +++ b/src/dragonfly_haiku.rs @@ -15,7 +15,7 @@ use std::cell::RefCell; thread_local!(static RNG_FILE: RefCell> = RefCell::new(None)); -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { RNG_FILE.with(|f| { use_init(f, || File::open("/dev/random").map_err(From::from), diff --git a/src/dummy.rs b/src/dummy.rs index 6763fcdc4..96acb910c 100644 --- a/src/dummy.rs +++ b/src/dummy.rs @@ -10,6 +10,6 @@ //! `Err(UNAVAILABLE_ERROR)` use super::UNAVAILABLE_ERROR; -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { Err(UNAVAILABLE_ERROR) } diff --git a/src/emscripten.rs b/src/emscripten.rs index 37a1ae11d..dd98985ab 100644 --- a/src/emscripten.rs +++ b/src/emscripten.rs @@ -15,7 +15,7 @@ use super::utils::use_init; thread_local!(static RNG_FILE: RefCell> = RefCell::new(None)); -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { // `Crypto.getRandomValues` documents `dest` should be at most 65536 // bytes. `crypto.randomBytes` documents: "To minimize threadpool // task length variation, partition large randomBytes requests when diff --git a/src/freebsd.rs b/src/freebsd.rs index e59076ed2..9756d1ba8 100644 --- a/src/freebsd.rs +++ b/src/freebsd.rs @@ -13,7 +13,7 @@ use super::Error; use core::ptr; use std::io; -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { let mib = [libc::CTL_KERN, libc::KERN_ARND]; // kern.arandom permits a maximum buffer size of 256 bytes for chunk in dest.chunks_mut(256) { diff --git a/src/fuchsia.rs b/src/fuchsia.rs index 82382b7b2..39741eb40 100644 --- a/src/fuchsia.rs +++ b/src/fuchsia.rs @@ -11,7 +11,7 @@ extern crate fuchsia_cprng; use super::Error; -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { fuchsia_cprng::cprng_draw(dest); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 471ce0289..f60b8ae64 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,8 @@ //! The bare WASM target `wasm32-unknown-unknown` tries to call the javascript //! methods directly, using either `stdweb` or `wasm-bindgen` depending on what //! features are activated for this crate. Note that if both features are -//! enabled `wasm-bindgen` will be used. +//! enabled `wasm-bindgen` will be used. If neither feature is enabled, +//! `getrandom` will always fail. //! //! ## Early boot //! @@ -112,14 +113,14 @@ pub use error::{Error, UNKNOWN_ERROR, UNAVAILABLE_ERROR}; // System-specific implementations. // -// These should all provide getrandom_os with the same signature as getrandom. +// These should all provide getrandom_inner with the same signature as getrandom. macro_rules! mod_use { ($cond:meta, $module:ident) => { #[$cond] mod $module; #[$cond] - use $module::getrandom_os; + use $module::getrandom_inner; } } @@ -204,5 +205,5 @@ mod_use!( /// significantly slower than a user-space CSPRNG; for the latter consider /// [`rand::thread_rng`](https://docs.rs/rand/*/rand/fn.thread_rng.html). pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { - getrandom_os(dest) + getrandom_inner(dest) } diff --git a/src/linux_android.rs b/src/linux_android.rs index 4775fe0c0..13dfb3b7c 100644 --- a/src/linux_android.rs +++ b/src/linux_android.rs @@ -38,7 +38,7 @@ fn syscall_getrandom(dest: &mut [u8]) -> Result<(), io::Error> { Ok(()) } -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { RNG_SOURCE.with(|f| { use_init(f, || { diff --git a/src/macos.rs b/src/macos.rs index baafaaf64..5cb541e39 100644 --- a/src/macos.rs +++ b/src/macos.rs @@ -21,7 +21,7 @@ extern { ) -> c_int; } -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { let ret = unsafe { SecRandomCopyBytes( kSecRandomDefault, diff --git a/src/netbsd.rs b/src/netbsd.rs index e76ec025b..6a59509b4 100644 --- a/src/netbsd.rs +++ b/src/netbsd.rs @@ -19,7 +19,7 @@ static RNG_INIT: AtomicBool = ATOMIC_BOOL_INIT; thread_local!(static RNG_FILE: RefCell> = RefCell::new(None)); -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { RNG_FILE.with(|f| { use_init(f, || { // read one byte from "/dev/random" to ensure that diff --git a/src/openbsd_bitrig.rs b/src/openbsd_bitrig.rs index 675c2ca6c..abdea21ce 100644 --- a/src/openbsd_bitrig.rs +++ b/src/openbsd_bitrig.rs @@ -12,7 +12,7 @@ extern crate libc; use super::Error; use std::io; -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { for chunk in dest.chunks_mut(256) { let ret = unsafe { libc::getentropy( diff --git a/src/redox.rs b/src/redox.rs index 87dfaf9eb..23026664a 100644 --- a/src/redox.rs +++ b/src/redox.rs @@ -15,7 +15,7 @@ use std::cell::RefCell; thread_local!(static RNG_FILE: RefCell> = RefCell::new(None)); -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { RNG_FILE.with(|f| { use_init(f, || File::open("rand:").map_err(From::from), diff --git a/src/sgx.rs b/src/sgx.rs index e4e7ba55d..7517d1f44 100644 --- a/src/sgx.rs +++ b/src/sgx.rs @@ -29,7 +29,7 @@ fn get_rand_u64() -> Result { Err(UNKNOWN_ERROR) } -pub fn getrandom_os(mut dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(mut dest: &mut [u8]) -> Result<(), Error> { while dest.len() >= 8 { let (chunk, left) = {dest}.split_at_mut(8); dest = left; diff --git a/src/solaris.rs b/src/solaris.rs index 20305ad24..742578a48 100644 --- a/src/solaris.rs +++ b/src/solaris.rs @@ -53,7 +53,7 @@ fn syscall_getrandom(dest: &mut [u8]) -> Result<(), Error> { Ok(()) } -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { // The documentation says 1024 is the maximum for getrandom // and 1040 for /dev/random. RNG_SOURCE.with(|f| { diff --git a/src/windows.rs b/src/windows.rs index 55c9e3b7d..1063bdd80 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -15,7 +15,7 @@ use self::winapi::um::winnt::PVOID; use std::io; use super::Error; -pub fn getrandom_os(dest: &mut [u8]) -> Result<(), Error> { +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { let ret = unsafe { RtlGenRandom(dest.as_mut_ptr() as PVOID, dest.len() as ULONG) };