2014-12-12 23:39:27 +00:00
|
|
|
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
|
2013-09-22 10:51:57 +00: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.
|
|
|
|
|
2014-05-30 04:37:31 +00:00
|
|
|
pub use self::imp::OsRng;
|
2013-09-22 10:51:57 +00:00
|
|
|
|
2016-07-29 07:48:52 +00:00
|
|
|
use mem;
|
|
|
|
|
2017-08-01 12:03:03 +00:00
|
|
|
fn next_u32(fill_buf: &mut FnMut(&mut [u8])) -> u32 {
|
2016-07-29 07:48:52 +00:00
|
|
|
let mut buf: [u8; 4] = [0; 4];
|
|
|
|
fill_buf(&mut buf);
|
|
|
|
unsafe { mem::transmute::<[u8; 4], u32>(buf) }
|
|
|
|
}
|
|
|
|
|
2017-08-01 12:03:03 +00:00
|
|
|
fn next_u64(fill_buf: &mut FnMut(&mut [u8])) -> u64 {
|
2016-07-29 07:48:52 +00:00
|
|
|
let mut buf: [u8; 8] = [0; 8];
|
|
|
|
fill_buf(&mut buf);
|
|
|
|
unsafe { mem::transmute::<[u8; 8], u64>(buf) }
|
|
|
|
}
|
|
|
|
|
2016-08-14 08:27:11 +00:00
|
|
|
#[cfg(all(unix,
|
|
|
|
not(target_os = "ios"),
|
|
|
|
not(target_os = "openbsd"),
|
2016-10-24 23:42:57 +00:00
|
|
|
not(target_os = "freebsd"),
|
|
|
|
not(target_os = "fuchsia")))]
|
2014-03-20 00:53:57 +00:00
|
|
|
mod imp {
|
2014-11-06 08:05:53 +00:00
|
|
|
use self::OsRngInner::*;
|
2016-07-29 07:48:52 +00:00
|
|
|
use super::{next_u32, next_u64};
|
2014-11-06 08:05:53 +00:00
|
|
|
|
2015-04-10 00:42:22 +00:00
|
|
|
use fs::File;
|
|
|
|
use io;
|
2015-03-30 18:00:05 +00:00
|
|
|
use libc;
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 08:39:37 +00:00
|
|
|
use rand::Rng;
|
|
|
|
use rand::reader::ReaderRng;
|
2015-03-30 18:00:05 +00:00
|
|
|
use sys::os::errno;
|
2014-11-05 18:53:27 +00:00
|
|
|
|
|
|
|
#[cfg(all(target_os = "linux",
|
2014-12-12 23:39:27 +00:00
|
|
|
any(target_arch = "x86_64",
|
|
|
|
target_arch = "x86",
|
|
|
|
target_arch = "arm",
|
2015-01-10 04:20:15 +00:00
|
|
|
target_arch = "aarch64",
|
2015-12-28 21:09:06 +00:00
|
|
|
target_arch = "powerpc",
|
2016-09-09 21:00:23 +00:00
|
|
|
target_arch = "powerpc64",
|
|
|
|
target_arch = "s390x")))]
|
2014-11-05 18:53:27 +00:00
|
|
|
fn getrandom(buf: &mut [u8]) -> libc::c_long {
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
const NR_GETRANDOM: libc::c_long = 318;
|
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
const NR_GETRANDOM: libc::c_long = 355;
|
2016-01-08 06:42:51 +00:00
|
|
|
#[cfg(target_arch = "arm")]
|
2015-01-10 04:20:15 +00:00
|
|
|
const NR_GETRANDOM: libc::c_long = 384;
|
2016-09-09 21:00:23 +00:00
|
|
|
#[cfg(target_arch = "s390x")]
|
|
|
|
const NR_GETRANDOM: libc::c_long = 349;
|
2016-01-30 21:27:00 +00:00
|
|
|
#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))]
|
2016-01-08 06:42:51 +00:00
|
|
|
const NR_GETRANDOM: libc::c_long = 359;
|
2015-12-17 05:36:31 +00:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
2015-07-26 10:19:35 +00:00
|
|
|
const NR_GETRANDOM: libc::c_long = 278;
|
2014-11-05 18:53:27 +00:00
|
|
|
|
2016-04-16 20:25:56 +00:00
|
|
|
const GRND_NONBLOCK: libc::c_uint = 0x0001;
|
|
|
|
|
2014-11-05 18:53:27 +00:00
|
|
|
unsafe {
|
2016-04-16 20:25:56 +00:00
|
|
|
libc::syscall(NR_GETRANDOM, buf.as_mut_ptr(), buf.len(), GRND_NONBLOCK)
|
2014-11-05 18:53:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(all(target_os = "linux",
|
2014-12-12 23:39:27 +00:00
|
|
|
any(target_arch = "x86_64",
|
|
|
|
target_arch = "x86",
|
|
|
|
target_arch = "arm",
|
2015-01-10 04:20:15 +00:00
|
|
|
target_arch = "aarch64",
|
2015-12-28 21:09:06 +00:00
|
|
|
target_arch = "powerpc",
|
2016-09-09 21:00:23 +00:00
|
|
|
target_arch = "powerpc64",
|
|
|
|
target_arch = "s390x"))))]
|
2014-11-05 18:53:27 +00:00
|
|
|
fn getrandom(_buf: &mut [u8]) -> libc::c_long { -1 }
|
|
|
|
|
|
|
|
fn getrandom_fill_bytes(v: &mut [u8]) {
|
|
|
|
let mut read = 0;
|
2015-12-17 05:36:31 +00:00
|
|
|
while read < v.len() {
|
2015-01-18 00:15:52 +00:00
|
|
|
let result = getrandom(&mut v[read..]);
|
2014-11-05 18:53:27 +00:00
|
|
|
if result == -1 {
|
|
|
|
let err = errno() as libc::c_int;
|
|
|
|
if err == libc::EINTR {
|
|
|
|
continue;
|
2016-04-16 20:25:56 +00:00
|
|
|
} else if err == libc::EAGAIN {
|
2016-04-20 22:29:33 +00:00
|
|
|
// if getrandom() returns EAGAIN 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
|
2016-04-16 20:25:56 +00:00
|
|
|
let reader = File::open("/dev/urandom").expect("Unable to open /dev/urandom");
|
|
|
|
let mut reader_rng = ReaderRng::new(reader);
|
2016-10-08 13:48:28 +00:00
|
|
|
reader_rng.fill_bytes(&mut v[read..]);
|
|
|
|
read += v.len();
|
2014-11-05 18:53:27 +00:00
|
|
|
} else {
|
|
|
|
panic!("unexpected getrandom error: {}", err);
|
|
|
|
}
|
|
|
|
} else {
|
2015-01-27 00:48:29 +00:00
|
|
|
read += result as usize;
|
2014-11-05 18:53:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(target_os = "linux",
|
2014-12-12 23:39:27 +00:00
|
|
|
any(target_arch = "x86_64",
|
|
|
|
target_arch = "x86",
|
|
|
|
target_arch = "arm",
|
2015-01-10 04:20:15 +00:00
|
|
|
target_arch = "aarch64",
|
2015-12-28 21:09:06 +00:00
|
|
|
target_arch = "powerpc",
|
2016-09-09 21:00:23 +00:00
|
|
|
target_arch = "powerpc64",
|
|
|
|
target_arch = "s390x")))]
|
2014-11-05 18:53:27 +00:00
|
|
|
fn is_getrandom_available() -> bool {
|
2015-05-27 08:18:36 +00:00
|
|
|
use sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use sync::Once;
|
2014-11-05 18:53:27 +00:00
|
|
|
|
2015-05-27 08:18:36 +00:00
|
|
|
static CHECKER: Once = Once::new();
|
|
|
|
static AVAILABLE: AtomicBool = AtomicBool::new(false);
|
2014-11-05 18:53:27 +00:00
|
|
|
|
2015-04-17 12:17:33 +00:00
|
|
|
CHECKER.call_once(|| {
|
2014-12-30 08:19:41 +00:00
|
|
|
let mut buf: [u8; 0] = [];
|
2014-11-05 18:53:27 +00:00
|
|
|
let result = getrandom(&mut buf);
|
|
|
|
let available = if result == -1 {
|
2015-04-17 12:17:33 +00:00
|
|
|
let err = io::Error::last_os_error().raw_os_error();
|
|
|
|
err != Some(libc::ENOSYS)
|
2014-11-05 18:53:27 +00:00
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
2015-04-17 12:17:33 +00:00
|
|
|
AVAILABLE.store(available, Ordering::Relaxed);
|
|
|
|
});
|
|
|
|
|
|
|
|
AVAILABLE.load(Ordering::Relaxed)
|
2014-11-05 18:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(all(target_os = "linux",
|
2014-12-12 23:39:27 +00:00
|
|
|
any(target_arch = "x86_64",
|
|
|
|
target_arch = "x86",
|
|
|
|
target_arch = "arm",
|
2015-01-10 04:20:15 +00:00
|
|
|
target_arch = "aarch64",
|
2015-12-28 21:09:06 +00:00
|
|
|
target_arch = "powerpc",
|
2016-09-09 21:00:23 +00:00
|
|
|
target_arch = "powerpc64",
|
|
|
|
target_arch = "s390x"))))]
|
2014-11-05 18:53:27 +00:00
|
|
|
fn is_getrandom_available() -> bool { false }
|
2014-03-20 00:53:57 +00:00
|
|
|
|
2014-05-30 04:37:31 +00:00
|
|
|
pub struct OsRng {
|
2014-11-05 18:53:27 +00:00
|
|
|
inner: OsRngInner,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum OsRngInner {
|
|
|
|
OsGetrandomRng,
|
|
|
|
OsReaderRng(ReaderRng<File>),
|
2013-09-22 10:51:57 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 04:37:31 +00:00
|
|
|
impl OsRng {
|
|
|
|
/// Create a new `OsRng`.
|
2015-04-10 00:42:22 +00:00
|
|
|
pub fn new() -> io::Result<OsRng> {
|
2014-11-05 18:53:27 +00:00
|
|
|
if is_getrandom_available() {
|
|
|
|
return Ok(OsRng { inner: OsGetrandomRng });
|
|
|
|
}
|
|
|
|
|
2016-03-23 03:01:37 +00:00
|
|
|
let reader = File::open("/dev/urandom")?;
|
2014-03-20 00:53:57 +00:00
|
|
|
let reader_rng = ReaderRng::new(reader);
|
2013-09-22 10:51:57 +00:00
|
|
|
|
2014-11-05 18:53:27 +00:00
|
|
|
Ok(OsRng { inner: OsReaderRng(reader_rng) })
|
2014-03-20 00:53:57 +00:00
|
|
|
}
|
2013-09-22 10:51:57 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 04:37:31 +00:00
|
|
|
impl Rng for OsRng {
|
2014-03-20 00:53:57 +00:00
|
|
|
fn next_u32(&mut self) -> u32 {
|
2014-11-05 18:53:27 +00:00
|
|
|
match self.inner {
|
2016-07-29 07:48:52 +00:00
|
|
|
OsGetrandomRng => next_u32(&mut getrandom_fill_bytes),
|
2014-11-05 18:53:27 +00:00
|
|
|
OsReaderRng(ref mut rng) => rng.next_u32(),
|
|
|
|
}
|
2014-03-20 00:53:57 +00:00
|
|
|
}
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
2014-11-05 18:53:27 +00:00
|
|
|
match self.inner {
|
2016-07-29 07:48:52 +00:00
|
|
|
OsGetrandomRng => next_u64(&mut getrandom_fill_bytes),
|
2014-11-05 18:53:27 +00:00
|
|
|
OsReaderRng(ref mut rng) => rng.next_u64(),
|
|
|
|
}
|
2014-03-20 00:53:57 +00:00
|
|
|
}
|
|
|
|
fn fill_bytes(&mut self, v: &mut [u8]) {
|
2014-11-05 18:53:27 +00:00
|
|
|
match self.inner {
|
|
|
|
OsGetrandomRng => getrandom_fill_bytes(v),
|
|
|
|
OsReaderRng(ref mut rng) => rng.fill_bytes(v)
|
|
|
|
}
|
2014-03-20 00:53:57 +00:00
|
|
|
}
|
2013-09-22 10:51:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 02:54:16 +00:00
|
|
|
#[cfg(target_os = "openbsd")]
|
|
|
|
mod imp {
|
2016-07-29 07:48:52 +00:00
|
|
|
use super::{next_u32, next_u64};
|
|
|
|
|
2015-12-17 02:54:16 +00:00
|
|
|
use io;
|
2015-12-21 09:12:48 +00:00
|
|
|
use libc;
|
2015-12-17 02:54:16 +00:00
|
|
|
use sys::os::errno;
|
|
|
|
use rand::Rng;
|
|
|
|
|
|
|
|
pub struct OsRng {
|
|
|
|
// dummy field to ensure that this struct cannot be constructed outside
|
|
|
|
// of this module
|
|
|
|
_dummy: (),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OsRng {
|
|
|
|
/// Create a new `OsRng`.
|
|
|
|
pub fn new() -> io::Result<OsRng> {
|
|
|
|
Ok(OsRng { _dummy: () })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rng for OsRng {
|
|
|
|
fn next_u32(&mut self) -> u32 {
|
2016-07-29 07:48:52 +00:00
|
|
|
next_u32(&mut |v| self.fill_bytes(v))
|
2015-12-17 02:54:16 +00:00
|
|
|
}
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
2016-07-29 07:48:52 +00:00
|
|
|
next_u64(&mut |v| self.fill_bytes(v))
|
2015-12-17 02:54:16 +00:00
|
|
|
}
|
|
|
|
fn fill_bytes(&mut self, v: &mut [u8]) {
|
|
|
|
// getentropy(2) permits a maximum buffer size of 256 bytes
|
|
|
|
for s in v.chunks_mut(256) {
|
2015-12-17 05:52:18 +00:00
|
|
|
let ret = unsafe {
|
2015-12-23 10:24:11 +00:00
|
|
|
libc::getentropy(s.as_mut_ptr() as *mut libc::c_void, s.len())
|
2015-12-17 05:52:18 +00:00
|
|
|
};
|
2015-12-17 02:54:16 +00:00
|
|
|
if ret == -1 {
|
2015-12-17 05:17:31 +00:00
|
|
|
panic!("unexpected getentropy error: {}", errno());
|
2015-12-17 02:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 07:07:49 +00:00
|
|
|
#[cfg(target_os = "ios")]
|
|
|
|
mod imp {
|
2016-07-29 07:48:52 +00:00
|
|
|
use super::{next_u32, next_u64};
|
|
|
|
|
2015-04-01 17:38:58 +00:00
|
|
|
use io;
|
2015-09-03 06:49:50 +00:00
|
|
|
use ptr;
|
2014-05-05 07:07:49 +00:00
|
|
|
use rand::Rng;
|
2015-08-11 19:52:22 +00:00
|
|
|
use libc::{c_int, size_t};
|
2014-05-05 07:07:49 +00:00
|
|
|
|
|
|
|
pub struct OsRng {
|
2015-04-10 00:42:22 +00:00
|
|
|
// dummy field to ensure that this struct cannot be constructed outside
|
|
|
|
// of this module
|
2014-12-15 20:35:34 +00:00
|
|
|
_dummy: (),
|
2014-05-05 07:07:49 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 21:50:18 +00:00
|
|
|
enum SecRandom {}
|
2014-05-05 07:07:49 +00:00
|
|
|
|
2014-10-27 22:37:07 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2015-09-03 06:49:50 +00:00
|
|
|
const kSecRandomDefault: *const SecRandom = ptr::null();
|
2014-05-05 07:07:49 +00:00
|
|
|
|
2016-01-21 23:36:25 +00:00
|
|
|
extern {
|
2014-06-25 19:47:34 +00:00
|
|
|
fn SecRandomCopyBytes(rnd: *const SecRandom,
|
|
|
|
count: size_t, bytes: *mut u8) -> c_int;
|
2014-05-05 07:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OsRng {
|
|
|
|
/// Create a new `OsRng`.
|
2015-04-10 00:42:22 +00:00
|
|
|
pub fn new() -> io::Result<OsRng> {
|
2014-12-15 20:35:34 +00:00
|
|
|
Ok(OsRng { _dummy: () })
|
2014-05-05 07:07:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rng for OsRng {
|
|
|
|
fn next_u32(&mut self) -> u32 {
|
2016-07-29 07:48:52 +00:00
|
|
|
next_u32(&mut |v| self.fill_bytes(v))
|
2014-05-05 07:07:49 +00:00
|
|
|
}
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
2016-07-29 07:48:52 +00:00
|
|
|
next_u64(&mut |v| self.fill_bytes(v))
|
2014-05-05 07:07:49 +00:00
|
|
|
}
|
|
|
|
fn fill_bytes(&mut self, v: &mut [u8]) {
|
|
|
|
let ret = unsafe {
|
2016-10-08 13:48:28 +00:00
|
|
|
SecRandomCopyBytes(kSecRandomDefault, v.len(),
|
2015-04-10 00:42:22 +00:00
|
|
|
v.as_mut_ptr())
|
2014-05-05 07:07:49 +00:00
|
|
|
};
|
|
|
|
if ret == -1 {
|
2015-04-10 00:42:22 +00:00
|
|
|
panic!("couldn't generate random bytes: {}",
|
|
|
|
io::Error::last_os_error());
|
2014-05-05 07:07:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 08:27:11 +00:00
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
mod imp {
|
|
|
|
use super::{next_u32, next_u64};
|
|
|
|
|
|
|
|
use io;
|
|
|
|
use libc;
|
|
|
|
use rand::Rng;
|
|
|
|
use ptr;
|
|
|
|
|
|
|
|
pub struct OsRng {
|
|
|
|
// dummy field to ensure that this struct cannot be constructed outside
|
|
|
|
// of this module
|
|
|
|
_dummy: (),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OsRng {
|
|
|
|
/// Create a new `OsRng`.
|
|
|
|
pub fn new() -> io::Result<OsRng> {
|
|
|
|
Ok(OsRng { _dummy: () })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rng for OsRng {
|
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
next_u32(&mut |v| self.fill_bytes(v))
|
|
|
|
}
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
|
|
|
next_u64(&mut |v| self.fill_bytes(v))
|
|
|
|
}
|
|
|
|
fn fill_bytes(&mut self, 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-10-24 23:42:57 +00:00
|
|
|
|
|
|
|
#[cfg(target_os = "fuchsia")]
|
|
|
|
mod imp {
|
|
|
|
use super::{next_u32, next_u64};
|
|
|
|
|
|
|
|
use io;
|
|
|
|
use rand::Rng;
|
|
|
|
|
2017-09-15 19:38:08 +00:00
|
|
|
#[link(name = "zircon")]
|
2016-10-24 23:42:57 +00:00
|
|
|
extern {
|
2017-09-15 19:38:08 +00:00
|
|
|
fn zx_cprng_draw(buffer: *mut u8, len: usize, actual: *mut usize) -> i32;
|
2016-10-24 23:42:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 20:01:15 +00:00
|
|
|
fn getrandom(buf: &mut [u8]) -> Result<usize, i32> {
|
|
|
|
unsafe {
|
|
|
|
let mut actual = 0;
|
2017-09-15 19:38:08 +00:00
|
|
|
let status = zx_cprng_draw(buf.as_mut_ptr(), buf.len(), &mut actual);
|
2016-11-04 20:01:15 +00:00
|
|
|
if status == 0 {
|
|
|
|
Ok(actual)
|
|
|
|
} else {
|
|
|
|
Err(status)
|
|
|
|
}
|
|
|
|
}
|
2016-10-24 23:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct OsRng {
|
|
|
|
// dummy field to ensure that this struct cannot be constructed outside
|
|
|
|
// of this module
|
|
|
|
_dummy: (),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OsRng {
|
|
|
|
/// Create a new `OsRng`.
|
|
|
|
pub fn new() -> io::Result<OsRng> {
|
|
|
|
Ok(OsRng { _dummy: () })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Rng for OsRng {
|
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
next_u32(&mut |v| self.fill_bytes(v))
|
|
|
|
}
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
|
|
|
next_u64(&mut |v| self.fill_bytes(v))
|
|
|
|
}
|
|
|
|
fn fill_bytes(&mut self, v: &mut [u8]) {
|
|
|
|
let mut buf = v;
|
|
|
|
while !buf.is_empty() {
|
|
|
|
let ret = getrandom(buf);
|
2016-11-04 20:01:15 +00:00
|
|
|
match ret {
|
|
|
|
Err(err) => {
|
2017-09-15 19:38:08 +00:00
|
|
|
panic!("kernel zx_cprng_draw call failed! (returned {}, buf.len() {})",
|
2016-11-04 20:01:15 +00:00
|
|
|
err, buf.len())
|
|
|
|
}
|
|
|
|
Ok(actual) => {
|
|
|
|
let move_buf = buf;
|
|
|
|
buf = &mut move_buf[(actual as usize)..];
|
|
|
|
}
|
2016-10-24 23:42:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|