1 // Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 pub fn hashmap_random_keys() -> (u64, u64) {
17 let view = slice::from_raw_parts_mut(&mut v as *mut _ as *mut u8,
18 mem::size_of_val(&v));
19 imp::fill_bytes(view);
25 not(target_os = "ios"),
26 not(target_os = "openbsd"),
27 not(target_os = "freebsd"),
28 not(target_os = "fuchsia")))]
32 #[cfg(any(target_os = "linux", target_os = "android"))]
35 #[cfg(any(target_os = "linux", target_os = "android"))]
36 fn getrandom(buf: &mut [u8]) -> libc::c_long {
38 libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr(), buf.len(), libc::GRND_NONBLOCK)
42 #[cfg(not(any(target_os = "linux", target_os = "android")))]
43 fn getrandom_fill_bytes(_buf: &mut [u8]) -> bool { false }
45 #[cfg(any(target_os = "linux", target_os = "android"))]
46 fn getrandom_fill_bytes(v: &mut [u8]) -> bool {
47 use sync::atomic::{AtomicBool, Ordering};
50 static GETRANDOM_UNAVAILABLE: AtomicBool = AtomicBool::new(false);
51 if GETRANDOM_UNAVAILABLE.load(Ordering::Relaxed) {
56 while read < v.len() {
57 let result = getrandom(&mut v[read..]);
59 let err = errno() as libc::c_int;
60 if err == libc::EINTR {
62 } else if err == libc::ENOSYS {
63 GETRANDOM_UNAVAILABLE.store(true, Ordering::Relaxed);
65 } else if err == libc::EAGAIN {
68 panic!("unexpected getrandom error: {}", err);
71 read += result as usize;
77 pub fn fill_bytes(v: &mut [u8]) {
78 // getrandom_fill_bytes here can fail if getrandom() returns EAGAIN,
79 // meaning it would have blocked because the non-blocking pool (urandom)
80 // has not initialized in the kernel yet due to a lack of entropy. The
81 // fallback we do here is to avoid blocking applications which could
82 // depend on this call without ever knowing they do and don't have a
83 // work around. The PRNG of /dev/urandom will still be used but over a
84 // possibly predictable entropy pool.
85 if getrandom_fill_bytes(v) {
89 // getrandom failed because it is permanently or temporarily (because
90 // of missing entropy) unavailable. Open /dev/urandom, read from it,
91 // and close it again.
92 let mut file = File::open("/dev/urandom").expect("failed to open /dev/urandom");
93 file.read_exact(v).expect("failed to read /dev/urandom")
97 #[cfg(target_os = "openbsd")]
102 pub fn fill_bytes(v: &mut [u8]) {
103 // getentropy(2) permits a maximum buffer size of 256 bytes
104 for s in v.chunks_mut(256) {
106 libc::getentropy(s.as_mut_ptr() as *mut libc::c_void, s.len())
109 panic!("unexpected getentropy error: {}", errno());
115 #[cfg(target_os = "ios")]
118 use libc::{c_int, size_t};
123 #[allow(non_upper_case_globals)]
124 const kSecRandomDefault: *const SecRandom = ptr::null();
127 fn SecRandomCopyBytes(rnd: *const SecRandom,
129 bytes: *mut u8) -> c_int;
132 pub fn fill_bytes(v: &mut [u8]) {
134 SecRandomCopyBytes(kSecRandomDefault,
139 panic!("couldn't generate random bytes: {}",
140 io::Error::last_os_error());
145 #[cfg(target_os = "freebsd")]
150 pub fn fill_bytes(v: &mut [u8]) {
151 let mib = [libc::CTL_KERN, libc::KERN_ARND];
152 // kern.arandom permits a maximum buffer size of 256 bytes
153 for s in v.chunks_mut(256) {
154 let mut s_len = s.len();
156 libc::sysctl(mib.as_ptr(), mib.len() as libc::c_uint,
157 s.as_mut_ptr() as *mut _, &mut s_len,
160 if ret == -1 || s_len != s.len() {
161 panic!("kern.arandom sysctl failed! (returned {}, s.len() {}, oldlenp {})",
162 ret, s.len(), s_len);
168 #[cfg(target_os = "fuchsia")]
170 #[link(name = "zircon")]
172 fn zx_cprng_draw(buffer: *mut u8, len: usize);
175 pub fn fill_bytes(v: &mut [u8]) {
176 unsafe { zx_cprng_draw(v.as_mut_ptr(), v.len()) }