Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make fallback to /dev/random optional when getrandom(2) is available #127

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ stdweb = { version = "0.4.18", optional = true }
wasm-bindgen-test = "0.2"

[features]
default = ["file-fallback"]
std = []
# Enables falling back to file-based implementations
file-fallback = []
# Enables dummy implementation for unsupported targets
dummy = []
# Unstable feature to support being a libstd dependency
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ build: false
test_script:
- set RUSTFLAGS=-D warnings
- cargo test
- cargo test --no-default-features
- cargo test --examples

branches:
Expand Down
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,15 @@ cfg_if! {
}

// These targets read from a file as a fallback method.
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "macos",
target_os = "solaris",
target_os = "illumos",
#[cfg(all(
feature = "file-fallback",
any(
target_os = "android",
target_os = "linux",
target_os = "macos",
target_os = "solaris",
target_os = "illumos",
)
))]
mod use_file;

Expand Down
14 changes: 12 additions & 2 deletions src/linux_android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
// except according to those terms.

//! Implementation for Linux / Android
#[cfg(not(feature = "file-fallback"))]
use crate::error;
#[cfg(feature = "file-fallback")]
use crate::use_file;
use crate::util::LazyBool;
use crate::util_libc::{last_os_error, sys_fill_exact};
use crate::{use_file, Error};
use crate::Error;

pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
static HAS_GETRANDOM: LazyBool = LazyBool::new();
Expand All @@ -18,7 +22,13 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0)
})
} else {
use_file::getrandom_inner(dest)
cfg_if! {
if #[cfg(feature = "file-fallback")] {
use_file::getrandom_inner(dest)
} else {
Err(error::UNSUPPORTED)
}
}
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
// except according to those terms.

//! Implementation for macOS
#[cfg(not(feature = "file-fallback"))]
use crate::error;
#[cfg(feature = "file-fallback")]
use crate::use_file;
use crate::util_libc::{last_os_error, Weak};
use crate::{use_file, Error};
use crate::Error;
use core::mem;

type GetEntropyFn = unsafe extern "C" fn(*mut u8, libc::size_t) -> libc::c_int;
Expand All @@ -27,8 +31,12 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
}
Ok(())
} else {
// We fallback to reading from /dev/random instead of SecRandomCopyBytes
// to avoid high startup costs and linking the Security framework.
use_file::getrandom_inner(dest)
cfg_if! {
if #[cfg(feature = "file-fallback")] {
use_file::getrandom_inner(dest)
} else {
Err(error::UNSUPPORTED)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a macOS specific error constant (with a good description)

}
}
}
}
14 changes: 12 additions & 2 deletions src/solaris_illumos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
//! To make sure we can compile on both Solaris and its derivatives, as well as
//! function, we check for the existence of getrandom(2) in libc by calling
//! libc::dlsym.
#[cfg(not(feature = "file-fallback"))]
use crate::error;
#[cfg(feature = "file-fallback")]
use crate::use_file;
use crate::util_libc::{sys_fill_exact, Weak};
use crate::{use_file, Error};
use crate::Error;
use core::mem;

#[cfg(target_os = "illumos")]
Expand All @@ -39,6 +43,12 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
}
Ok(())
} else {
use_file::getrandom_inner(dest)
cfg_if! {
if #[cfg(feature = "file-fallback")] {
use_file::getrandom_inner(dest)
} else {
Err(error::UNSUPPORTED)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a Solaris specific error constant (with a good description)

}
}
}
}