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 6b25b45 commit 80baf18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 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
39 changes: 30 additions & 9 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 @@ -311,15 +319,15 @@ mod sysrand {
mod sysrand_or_urandom {
use crate::error;

enum Mechanism {
Sysrand,
DevURandom,
}

#[inline]
pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> {
use lazy_static::lazy_static;

enum Mechanism {
Sysrand,
DevURandom,
}

lazy_static! {
static ref MECHANISM: Mechanism = {
let mut dummy = [0u8; 1];
Expand All @@ -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),
}
}
}

#[cold]
#[inline(never)]
fn urandom_fallback(dest: &mut [u8]) -> Result<(), error::Unspecified> {
#[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;

#[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 80baf18

Please sign in to comment.