Skip to content

Commit

Permalink
Rollup merge of rust-lang#70207 - hatoo:macos-getentropy, r=dtolnay
Browse files Browse the repository at this point in the history
Use getentropy(2) on macos

resolves rust-lang#70179
  • Loading branch information
Centril committed Mar 23, 2020
2 parents 1229c0a + 61ef72f commit c43941a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/libstd/sys/unix/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn hashmap_random_keys() -> (u64, u64) {

#[cfg(all(
unix,
not(target_os = "macos"),
not(target_os = "ios"),
not(target_os = "openbsd"),
not(target_os = "freebsd"),
Expand Down Expand Up @@ -92,6 +93,42 @@ mod imp {
}
}

#[cfg(target_os = "macos")]
mod imp {
use crate::fs::File;
use crate::io::Read;
use crate::sys::os::errno;
use libc::{c_int, c_void, size_t};

fn getentropy_fill_bytes(v: &mut [u8]) -> bool {
weak!(fn getentropy(*mut c_void, size_t) -> c_int);

getentropy
.get()
.map(|f| {
// getentropy(2) permits a maximum buffer size of 256 bytes
for s in v.chunks_mut(256) {
let ret = unsafe { f(s.as_mut_ptr() as *mut c_void, s.len()) };
if ret == -1 {
panic!("unexpected getentropy error: {}", errno());
}
}
true
})
.unwrap_or(false)
}

pub fn fill_bytes(v: &mut [u8]) {
if getentropy_fill_bytes(v) {
return;
}

// for older macos which doesn't support getentropy
let mut file = File::open("/dev/urandom").expect("failed to open /dev/urandom");
file.read_exact(v).expect("failed to read /dev/urandom")
}
}

#[cfg(target_os = "openbsd")]
mod imp {
use crate::sys::os::errno;
Expand Down

0 comments on commit c43941a

Please sign in to comment.