Skip to content

Commit

Permalink
FreeBSD, NetBSD, OpenBSD, Solaris: Use /dev/urandom.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jul 16, 2019
1 parent e32e46c commit 70e5d96
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ libc = { version = "0.2.48", default-features = false }
[target.'cfg(all(any(target_arch = "aarch64", target_arch = "arm", target_arch = "x86", target_arch = "x86_64"), not(target_os = "ios")))'.dependencies]
spin = { version = "0.5.0", default-features = false }

[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
[target.'cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux", target_os = "netbsd", target_os = "openbsd", target_os = "solaris"))'.dependencies]
lazy_static = { version = "1.3", default-features = false, optional = true }

[target.'cfg(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown", target_env = ""))'.dependencies]
Expand Down
29 changes: 25 additions & 4 deletions src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ use self::sysrand::fill as fill_impl;
))]
use self::sysrand_or_urandom::fill as fill_impl;

#[cfg(any(
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "solaris"
))]
use self::urandom::fill as fill_impl;

#[cfg(any(target_os = "macos", target_os = "ios"))]
use self::darwin::fill as fill_impl;

Expand Down Expand Up @@ -333,13 +341,26 @@ mod sysrand_or_urandom {

match *MECHANISM {
Mechanism::Sysrand => super::sysrand::fill(dest),
Mechanism::DevURandom => urandom_fallback(dest),
Mechanism::DevURandom => super::urandom::fill(dest),
}
}
}

#[cfg(any(
all(
any(target_os = "android", target_os = "linux"),
feature = "dev_urandom_fallback"
),
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "solaris"
))]
mod urandom {
use crate::error;

#[cold]
#[inline(never)]
fn urandom_fallback(dest: &mut [u8]) -> Result<(), error::Unspecified> {
#[cfg_attr(any(target_os = "android", target_os = "linux"), cold, inline(never))]
pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> {
use lazy_static::lazy_static;

lazy_static! {
Expand Down

0 comments on commit 70e5d96

Please sign in to comment.