From 0f371be445d2c4ea7f378ccd425ae56a0ca08c01 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 18 Feb 2019 14:38:53 +0000 Subject: [PATCH] =?UTF-8?q?Rename=20getrandom=5Fos=20=E2=86=92=20getrandom?= =?UTF-8?q?=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 b296cd7..fc47f2e 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 ee93c05..f30e064 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 1d6a0b7..751266b 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 6763fcd..96acb91 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 37a1ae1..dd98985 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 e59076e..9756d1b 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 82382b7..39741eb 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 471ce02..f60b8ae 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 4775fe0..13dfb3b 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 baafaaf..5cb541e 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 e76ec02..6a59509 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 675c2ca..abdea21 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 87dfaf9..2302666 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 e4e7ba5..7517d1f 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 20305ad..742578a 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 55c9e3b..1063bdd 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) };