From b51d7c7982dac2db810c7942a21eed1f32d17be6 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Thu, 9 Jan 2020 03:10:24 -0800 Subject: [PATCH 1/3] log: Remove optional log dependancy This feature isn't enabled by rand/rand_core and provides very little error information that isn't already conveyed through our Error values. This also simplifies the supported configuration space for getrandom. Signed-off-by: Joe Richey --- Cargo.toml | 1 - src/bsd_arandom.rs | 1 - src/cloudabi.rs | 1 - src/lib.rs | 21 --------------------- src/macos.rs | 4 +--- src/openbsd.rs | 4 +--- src/rdrand.rs | 1 - src/wasm32_stdweb.rs | 6 ------ src/windows_uwp.rs | 30 ++++++++++-------------------- 9 files changed, 12 insertions(+), 57 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3999d3112..88dd1f325 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ travis-ci = { repository = "rust-random/getrandom" } appveyor = { repository = "rust-random/getrandom" } [dependencies] -log = { version = "0.4", optional = true } cfg-if = "0.1.2" # When built as part of libstd diff --git a/src/bsd_arandom.rs b/src/bsd_arandom.rs index eb564ffff..450dc0c6b 100644 --- a/src/bsd_arandom.rs +++ b/src/bsd_arandom.rs @@ -25,7 +25,6 @@ fn kern_arnd(buf: &mut [u8]) -> libc::ssize_t { ) }; if ret == -1 { - error!("sysctl kern.arandom: syscall failed"); -1 } else { len as libc::ssize_t diff --git a/src/cloudabi.rs b/src/cloudabi.rs index d3d09289b..5c0ae4110 100644 --- a/src/cloudabi.rs +++ b/src/cloudabi.rs @@ -17,7 +17,6 @@ extern "C" { pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { let errno = unsafe { cloudabi_sys_random_get(dest.as_mut_ptr(), dest.len()) }; if let Some(code) = NonZeroU32::new(errno as u32) { - error!("cloudabi_sys_random_get: failed with {}", errno); Err(Error::from(code)) } else { Ok(()) // Zero means success for CloudABI diff --git a/src/lib.rs b/src/lib.rs index b11795d5d..1ce321ec6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -132,27 +132,6 @@ #[macro_use] extern crate cfg_if; -cfg_if! { - if #[cfg(feature = "log")] { - #[allow(unused)] - #[macro_use] - extern crate log; - } else { - #[allow(unused)] - macro_rules! error { - ($($x:tt)*) => {}; - } - #[allow(unused)] - macro_rules! warn { - ($($x:tt)*) => {}; - } - #[allow(unused)] - macro_rules! info { - ($($x:tt)*) => {}; - } - } -} - mod error; mod util; diff --git a/src/macos.rs b/src/macos.rs index c3bc53341..9201ec5e3 100644 --- a/src/macos.rs +++ b/src/macos.rs @@ -20,9 +20,7 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { for chunk in dest.chunks_mut(256) { let ret = unsafe { func(chunk.as_mut_ptr(), chunk.len()) }; if ret != 0 { - let err = last_os_error(); - error!("getentropy syscall failed"); - return Err(err); + return Err(last_os_error()); } } Ok(()) diff --git a/src/openbsd.rs b/src/openbsd.rs index e1ac179f2..6e2cf9aec 100644 --- a/src/openbsd.rs +++ b/src/openbsd.rs @@ -14,9 +14,7 @@ pub fn getrandom_inner(dest: &mut [u8]) -> 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 { - let err = last_os_error(); - error!("libc::getentropy call failed"); - return Err(err); + return Err(last_os_error()); } } Ok(()) diff --git a/src/rdrand.rs b/src/rdrand.rs index c38ac59da..d8dfc7f2d 100644 --- a/src/rdrand.rs +++ b/src/rdrand.rs @@ -32,7 +32,6 @@ unsafe fn rdrand() -> Result<[u8; WORD_SIZE], Error> { if el != 0 && el != !0 { return Ok(el.to_ne_bytes()); } - error!("RDRAND returned {:X}, CPU RNG may be broken", el); // Keep looping in case this was a false positive. } } diff --git a/src/wasm32_stdweb.rs b/src/wasm32_stdweb.rs index 4e79363c0..4e28d78ee 100644 --- a/src/wasm32_stdweb.rs +++ b/src/wasm32_stdweb.rs @@ -13,8 +13,6 @@ use core::mem; use std::sync::Once; use stdweb::js; -use stdweb::unstable::TryInto; -use stdweb::web::error::Error as WebError; use crate::Error; @@ -68,8 +66,6 @@ fn getrandom_init() -> Result { unreachable!() } } else { - let _err: WebError = js! { return @{ result }.error }.try_into().unwrap(); - error!("getrandom unavailable: {}", _err); Err(Error::STDWEB_NO_RNG) } } @@ -104,8 +100,6 @@ fn getrandom_fill(source: RngSource, dest: &mut [u8]) -> Result<(), Error> { }; if js! { return @{ result.as_ref() }.success } != true { - let _err: WebError = js! { return @{ result }.error }.try_into().unwrap(); - error!("getrandom failed: {}", _err); return Err(Error::STDWEB_RNG_FAILED); } } diff --git a/src/windows_uwp.rs b/src/windows_uwp.rs index 586c6f61c..6ba3a5e06 100644 --- a/src/windows_uwp.rs +++ b/src/windows_uwp.rs @@ -33,26 +33,16 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { BCRYPT_USE_SYSTEM_PREFERRED_RNG, ) }; - // NTSTATUS codes use two highest bits for severity status - match ret >> 30 { - 0b01 => { - info!("BCryptGenRandom: information code 0x{:08X}", ret); - } - 0b10 => { - warn!("BCryptGenRandom: warning code 0x{:08X}", ret); - } - 0b11 => { - error!("BCryptGenRandom: failed with 0x{:08X}", ret); - // We zeroize the highest bit, so the error code will reside - // inside the range of designated for OS codes. - let code = ret ^ (1 << 31); - // SAFETY: the second highest bit is always equal to one, - // so it's impossible to get zero. Unfortunately compiler - // is not smart enough to figure out it yet. - let code = unsafe { NonZeroU32::new_unchecked(code) }; - return Err(Error::from(code)); - } - _ => (), + // NTSTATUS codes use the two highest bits for severity status. + if ret >> 30 == 0b11 { + // We zeroize the highest bit, so the error code will reside + // inside the range designated for OS codes. + let code = ret ^ (1 << 31); + // SAFETY: the second highest bit is always equal to one, + // so it's impossible to get zero. Unfortunately the type + // system does not have a way to express this yet. + let code = unsafe { NonZeroU32::new_unchecked(code) }; + return Err(Error::from(code)); } } Ok(()) From 272b0538f956d69ee4e84ca6b3231a18665cf4f3 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Thu, 9 Jan 2020 03:18:17 -0800 Subject: [PATCH 2/3] README: We are at 0.2 and no longer have a 'log' feature --- README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/README.md b/README.md index e2a3b6818..9003fddf7 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Add this to your `Cargo.toml`: ```toml [dependencies] -getrandom = "0.1" +getrandom = "0.2" ``` Then invoke the `getrandom` function: @@ -44,9 +44,6 @@ usually requires calling some external system API. This means most platforms will require linking against system libraries (i.e. `libc` for Unix, `Advapi32.dll` for Windows, Security framework on iOS, etc...). -The `log` library is supported as an optional dependency. If enabled, error -reporting will be improved on some platforms. - For the `wasm32-unknown-unknown` target, one of the following features should be enabled: From 56c77c704f93fe87ceb98757b837da426596b090 Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Thu, 9 Jan 2020 03:36:04 -0800 Subject: [PATCH 3/3] travis: Update CI to not use log feature --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2ec4afaf4..0ac4a213d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -91,14 +91,13 @@ matrix: - cargo test --benches # Check that setting various features does not break the build - cargo build --features=std - - cargo build --features=log # remove cached documentation, otherwise files from previous PRs can get included - rm -rf target/doc - cargo doc --no-deps --features=std - cargo deadlinks --dir target/doc # also test minimum dependency versions are usable - cargo generate-lockfile -Z minimal-versions - - cargo test --features=std,log + - cargo test --features=std - <<: *nightly_and_docs name: "OSX, nightly, docs"