2014-12-12 23:39:27 +00:00
|
|
|
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
|
2013-09-22 20:51:57 +10:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2016-07-29 07:48:52 +00:00
|
|
|
use mem;
|
2017-11-01 12:32:13 -07:00
|
|
|
use slice;
|
|
|
|
|
|
|
|
|
|
pub fn hashmap_random_keys() -> (u64, u64) {
|
|
|
|
|
let mut v = (0, 0);
|
|
|
|
|
unsafe {
|
|
|
|
|
let view = slice::from_raw_parts_mut(&mut v as *mut _ as *mut u8,
|
|
|
|
|
mem::size_of_val(&v));
|
|
|
|
|
imp::fill_bytes(view);
|
|
|
|
|
}
|
|
|
|
|
return v
|
2016-07-29 07:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-14 08:27:11 +00:00
|
|
|
#[cfg(all(unix,
|
|
|
|
|
not(target_os = "ios"),
|
|
|
|
|
not(target_os = "openbsd"),
|
2016-10-24 16:42:57 -07:00
|
|
|
not(target_os = "freebsd"),
|
|
|
|
|
not(target_os = "fuchsia")))]
|
2014-03-19 17:53:57 -07:00
|
|
|
mod imp {
|
2015-04-09 17:42:22 -07:00
|
|
|
use fs::File;
|
2017-11-01 12:32:13 -07:00
|
|
|
use io::Read;
|
2015-03-30 11:00:05 -07:00
|
|
|
use libc;
|
|
|
|
|
use sys::os::errno;
|
2014-11-06 03:53:27 +09:00
|
|
|
|
2017-11-09 18:49:29 -02:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
2014-11-06 03:53:27 +09:00
|
|
|
fn getrandom(buf: &mut [u8]) -> libc::c_long {
|
|
|
|
|
unsafe {
|
2017-11-09 18:49:29 -02:00
|
|
|
libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr(), buf.len(), libc::GRND_NONBLOCK)
|
2014-11-06 03:53:27 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 18:49:29 -02:00
|
|
|
#[cfg(not(any(target_os = "linux", target_os = "android")))]
|
2014-11-06 03:53:27 +09:00
|
|
|
fn getrandom(_buf: &mut [u8]) -> libc::c_long { -1 }
|
|
|
|
|
|
2017-11-01 12:32:13 -07:00
|
|
|
fn getrandom_fill_bytes(v: &mut [u8]) -> bool {
|
2014-11-06 03:53:27 +09:00
|
|
|
let mut read = 0;
|
2015-12-17 00:36:31 -05:00
|
|
|
while read < v.len() {
|
2015-01-17 16:15:52 -08:00
|
|
|
let result = getrandom(&mut v[read..]);
|
2014-11-06 03:53:27 +09:00
|
|
|
if result == -1 {
|
|
|
|
|
let err = errno() as libc::c_int;
|
|
|
|
|
if err == libc::EINTR {
|
|
|
|
|
continue;
|
2016-04-16 15:25:56 -05:00
|
|
|
} else if err == libc::EAGAIN {
|
2017-11-01 12:32:13 -07:00
|
|
|
return false
|
2014-11-06 03:53:27 +09:00
|
|
|
} else {
|
|
|
|
|
panic!("unexpected getrandom error: {}", err);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2015-01-26 19:48:29 -05:00
|
|
|
read += result as usize;
|
2014-11-06 03:53:27 +09:00
|
|
|
}
|
|
|
|
|
}
|
2017-11-01 12:32:13 -07:00
|
|
|
|
|
|
|
|
return true
|
2014-11-06 03:53:27 +09:00
|
|
|
}
|
|
|
|
|
|
2017-11-09 18:49:29 -02:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
2014-11-06 03:53:27 +09:00
|
|
|
fn is_getrandom_available() -> bool {
|
2017-11-01 12:32:13 -07:00
|
|
|
use io;
|
2015-05-27 11:18:36 +03:00
|
|
|
use sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
use sync::Once;
|
2014-11-06 03:53:27 +09:00
|
|
|
|
2015-05-27 11:18:36 +03:00
|
|
|
static CHECKER: Once = Once::new();
|
|
|
|
|
static AVAILABLE: AtomicBool = AtomicBool::new(false);
|
2014-11-06 03:53:27 +09:00
|
|
|
|
2015-04-17 21:17:33 +09:00
|
|
|
CHECKER.call_once(|| {
|
2014-12-30 21:19:41 +13:00
|
|
|
let mut buf: [u8; 0] = [];
|
2014-11-06 03:53:27 +09:00
|
|
|
let result = getrandom(&mut buf);
|
|
|
|
|
let available = if result == -1 {
|
2015-04-17 21:17:33 +09:00
|
|
|
let err = io::Error::last_os_error().raw_os_error();
|
|
|
|
|
err != Some(libc::ENOSYS)
|
2014-11-06 03:53:27 +09:00
|
|
|
} else {
|
|
|
|
|
true
|
|
|
|
|
};
|
2015-04-17 21:17:33 +09:00
|
|
|
AVAILABLE.store(available, Ordering::Relaxed);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AVAILABLE.load(Ordering::Relaxed)
|
2014-11-06 03:53:27 +09:00
|
|
|
}
|
|
|
|
|
|
2017-11-09 18:49:29 -02:00
|
|
|
#[cfg(not(any(target_os = "linux", target_os = "android")))]
|
2014-11-06 03:53:27 +09:00
|
|
|
fn is_getrandom_available() -> bool { false }
|
2014-03-19 17:53:57 -07:00
|
|
|
|
2017-11-01 12:32:13 -07:00
|
|
|
pub fn fill_bytes(v: &mut [u8]) {
|
|
|
|
|
// getrandom_fill_bytes here can fail if getrandom() returns EAGAIN,
|
|
|
|
|
// meaning it would have blocked because the non-blocking pool (urandom)
|
|
|
|
|
// has not initialized in the kernel yet due to a lack of entropy the
|
|
|
|
|
// fallback we do here is to avoid blocking applications which could
|
|
|
|
|
// depend on this call without ever knowing they do and don't have a
|
|
|
|
|
// work around. The PRNG of /dev/urandom will still be used but not
|
|
|
|
|
// over a completely full entropy pool
|
|
|
|
|
if is_getrandom_available() && getrandom_fill_bytes(v) {
|
|
|
|
|
return
|
2014-03-19 17:53:57 -07:00
|
|
|
}
|
2013-09-22 20:51:57 +10:00
|
|
|
|
2017-11-01 12:32:13 -07:00
|
|
|
let mut file = File::open("/dev/urandom")
|
|
|
|
|
.expect("failed to open /dev/urandom");
|
|
|
|
|
file.read_exact(v).expect("failed to read /dev/urandom");
|
2013-09-22 20:51:57 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-16 21:54:16 -05:00
|
|
|
#[cfg(target_os = "openbsd")]
|
|
|
|
|
mod imp {
|
2015-12-21 10:12:48 +01:00
|
|
|
use libc;
|
2015-12-16 21:54:16 -05:00
|
|
|
use sys::os::errno;
|
|
|
|
|
|
2017-11-01 12:32:13 -07:00
|
|
|
pub fn fill_bytes(v: &mut [u8]) {
|
|
|
|
|
// getentropy(2) permits a maximum buffer size of 256 bytes
|
|
|
|
|
for s in v.chunks_mut(256) {
|
|
|
|
|
let ret = unsafe {
|
|
|
|
|
libc::getentropy(s.as_mut_ptr() as *mut libc::c_void, s.len())
|
|
|
|
|
};
|
|
|
|
|
if ret == -1 {
|
|
|
|
|
panic!("unexpected getentropy error: {}", errno());
|
2015-12-16 21:54:16 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-05 10:07:49 +03:00
|
|
|
#[cfg(target_os = "ios")]
|
|
|
|
|
mod imp {
|
2015-04-01 20:38:58 +03:00
|
|
|
use io;
|
2015-08-11 14:52:22 -05:00
|
|
|
use libc::{c_int, size_t};
|
2017-11-01 12:32:13 -07:00
|
|
|
use ptr;
|
2014-05-05 10:07:49 +03:00
|
|
|
|
2015-08-05 17:50:18 -04:00
|
|
|
enum SecRandom {}
|
2014-05-05 10:07:49 +03:00
|
|
|
|
2014-10-27 15:37:07 -07:00
|
|
|
#[allow(non_upper_case_globals)]
|
2015-09-03 09:49:50 +03:00
|
|
|
const kSecRandomDefault: *const SecRandom = ptr::null();
|
2014-05-05 10:07:49 +03:00
|
|
|
|
2016-01-21 15:36:25 -08:00
|
|
|
extern {
|
2014-06-25 12:47:34 -07:00
|
|
|
fn SecRandomCopyBytes(rnd: *const SecRandom,
|
2017-11-01 12:32:13 -07:00
|
|
|
count: size_t,
|
|
|
|
|
bytes: *mut u8) -> c_int;
|
2014-05-05 10:07:49 +03:00
|
|
|
}
|
|
|
|
|
|
2017-11-01 12:32:13 -07:00
|
|
|
pub fn fill_bytes(v: &mut [u8]) {
|
|
|
|
|
let ret = unsafe {
|
|
|
|
|
SecRandomCopyBytes(kSecRandomDefault,
|
|
|
|
|
v.len(),
|
|
|
|
|
v.as_mut_ptr())
|
|
|
|
|
};
|
|
|
|
|
if ret == -1 {
|
|
|
|
|
panic!("couldn't generate random bytes: {}",
|
|
|
|
|
io::Error::last_os_error());
|
2014-05-05 10:07:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-14 08:27:11 +00:00
|
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
|
mod imp {
|
|
|
|
|
use libc;
|
|
|
|
|
use ptr;
|
|
|
|
|
|
2017-11-01 12:32:13 -07:00
|
|
|
pub fn fill_bytes(v: &mut [u8]) {
|
|
|
|
|
let mib = [libc::CTL_KERN, libc::KERN_ARND];
|
|
|
|
|
// kern.arandom permits a maximum buffer size of 256 bytes
|
|
|
|
|
for s in v.chunks_mut(256) {
|
|
|
|
|
let mut s_len = s.len();
|
|
|
|
|
let ret = unsafe {
|
|
|
|
|
libc::sysctl(mib.as_ptr(), mib.len() as libc::c_uint,
|
|
|
|
|
s.as_mut_ptr() as *mut _, &mut s_len,
|
|
|
|
|
ptr::null(), 0)
|
|
|
|
|
};
|
|
|
|
|
if ret == -1 || s_len != s.len() {
|
|
|
|
|
panic!("kern.arandom sysctl failed! (returned {}, s.len() {}, oldlenp {})",
|
|
|
|
|
ret, s.len(), s_len);
|
2016-08-14 08:27:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-24 16:42:57 -07:00
|
|
|
|
|
|
|
|
#[cfg(target_os = "fuchsia")]
|
|
|
|
|
mod imp {
|
2017-09-15 12:38:08 -07:00
|
|
|
#[link(name = "zircon")]
|
2016-10-24 16:42:57 -07:00
|
|
|
extern {
|
2018-06-27 08:56:19 -07:00
|
|
|
fn zx_cprng_draw(buffer: *mut u8, len: usize);
|
2016-10-24 16:42:57 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-01 12:32:13 -07:00
|
|
|
pub fn fill_bytes(v: &mut [u8]) {
|
2018-06-27 08:56:19 -07:00
|
|
|
unsafe { zx_cprng_draw(v.as_mut_ptr(), v.len()) }
|
2016-10-24 16:42:57 -07:00
|
|
|
}
|
|
|
|
|
}
|