]>
git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/rand.rs
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")))]
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(_buf
: &mut [u8]) -> libc
::c_long { -1 }
45 fn getrandom_fill_bytes(v
: &mut [u8]) -> bool
{
47 while read
< v
.len() {
48 let result
= getrandom(&mut v
[read
..]);
50 let err
= errno() as libc
::c_int
;
51 if err
== libc
::EINTR
{
53 } else if err
== libc
::EAGAIN
{
56 panic
!("unexpected getrandom error: {}", err
);
59 read
+= result
as usize;
66 #[cfg(any(target_os = "linux", target_os = "android"))]
67 fn is_getrandom_available() -> bool
{
69 use sync
::atomic
::{AtomicBool, Ordering}
;
72 static CHECKER
: Once
= Once
::new();
73 static AVAILABLE
: AtomicBool
= AtomicBool
::new(false);
75 CHECKER
.call_once(|| {
76 let mut buf
: [u8; 0] = [];
77 let result
= getrandom(&mut buf
);
78 let available
= if result
== -1 {
79 let err
= io
::Error
::last_os_error().raw_os_error();
80 err
!= Some(libc
::ENOSYS
)
84 AVAILABLE
.store(available
, Ordering
::Relaxed
);
87 AVAILABLE
.load(Ordering
::Relaxed
)
90 #[cfg(not(any(target_os = "linux", target_os = "android")))]
91 fn is_getrandom_available() -> bool { false }
93 pub fn fill_bytes(v
: &mut [u8]) {
94 // getrandom_fill_bytes here can fail if getrandom() returns EAGAIN,
95 // meaning it would have blocked because the non-blocking pool (urandom)
96 // has not initialized in the kernel yet due to a lack of entropy the
97 // fallback we do here is to avoid blocking applications which could
98 // depend on this call without ever knowing they do and don't have a
99 // work around. The PRNG of /dev/urandom will still be used but not
100 // over a completely full entropy pool
101 if is_getrandom_available() && getrandom_fill_bytes(v
) {
105 let mut file
= File
::open("/dev/urandom")
106 .expect("failed to open /dev/urandom");
107 file
.read_exact(v
).expect("failed to read /dev/urandom");
111 #[cfg(target_os = "openbsd")]
116 pub fn fill_bytes(v
: &mut [u8]) {
117 // getentropy(2) permits a maximum buffer size of 256 bytes
118 for s
in v
.chunks_mut(256) {
120 libc
::getentropy(s
.as_mut_ptr() as *mut libc
::c_void
, s
.len())
123 panic
!("unexpected getentropy error: {}", errno());
129 #[cfg(target_os = "ios")]
132 use libc
::{c_int, size_t}
;
137 #[allow(non_upper_case_globals)]
138 const kSecRandomDefault
: *const SecRandom
= ptr
::null();
141 fn SecRandomCopyBytes(rnd
: *const SecRandom
,
143 bytes
: *mut u8) -> c_int
;
146 pub fn fill_bytes(v
: &mut [u8]) {
148 SecRandomCopyBytes(kSecRandomDefault
,
153 panic
!("couldn't generate random bytes: {}",
154 io
::Error
::last_os_error());
159 #[cfg(target_os = "freebsd")]
164 pub fn fill_bytes(v
: &mut [u8]) {
165 let mib
= [libc
::CTL_KERN
, libc
::KERN_ARND
];
166 // kern.arandom permits a maximum buffer size of 256 bytes
167 for s
in v
.chunks_mut(256) {
168 let mut s_len
= s
.len();
170 libc
::sysctl(mib
.as_ptr(), mib
.len() as libc
::c_uint
,
171 s
.as_mut_ptr() as *mut _
, &mut s_len
,
174 if ret
== -1 || s_len
!= s
.len() {
175 panic
!("kern.arandom sysctl failed! (returned {}, s.len() {}, oldlenp {})",
176 ret
, s
.len(), s_len
);
182 #[cfg(target_os = "fuchsia")]
184 #[link(name = "zircon")]
186 fn zx_cprng_draw(buffer
: *mut u8, len
: usize, actual
: *mut usize) -> i32;
189 fn getrandom(buf
: &mut [u8]) -> Result
<usize, i32> {
192 let status
= zx_cprng_draw(buf
.as_mut_ptr(), buf
.len(), &mut actual
);
201 pub fn fill_bytes(v
: &mut [u8]) {
203 while !buf
.is_empty() {
204 let ret
= getrandom(buf
);
207 panic
!("kernel zx_cprng_draw call failed! (returned {}, buf.len() {})",
212 buf
= &mut move_buf
[(actual
as usize)..];