2013-09-22 05:51:57 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! Interfaces to the operating system provided random number
|
|
|
|
//! generators.
|
|
|
|
|
2014-03-01 18:23:04 -06:00
|
|
|
use Rng;
|
2013-09-22 05:51:57 -05:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
2014-03-01 18:23:04 -06:00
|
|
|
use reader::ReaderRng;
|
2013-09-22 05:51:57 -05:00
|
|
|
#[cfg(unix)]
|
2014-03-01 18:23:04 -06:00
|
|
|
use std::io::File;
|
2013-09-22 05:51:57 -05:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
2014-03-01 18:23:04 -06:00
|
|
|
use std::cast;
|
2013-10-08 17:45:38 -05:00
|
|
|
#[cfg(windows)]
|
2014-03-01 18:23:04 -06:00
|
|
|
use std::libc::{c_long, DWORD, BYTE};
|
2013-10-08 17:45:38 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
type HCRYPTPROV = c_long;
|
|
|
|
// the extern functions imported from the runtime on Windows are
|
|
|
|
// implemented so that they either succeed or abort(), so we can just
|
|
|
|
// assume they work when we call them.
|
2013-09-22 05:51:57 -05:00
|
|
|
|
|
|
|
/// A random number generator that retrieves randomness straight from
|
2013-10-11 01:25:40 -05:00
|
|
|
/// the operating system. Platform sources:
|
|
|
|
///
|
|
|
|
/// - Unix-like systems (Linux, Android, Mac OSX): read directly from
|
|
|
|
/// `/dev/urandom`.
|
|
|
|
/// - Windows: calls `CryptGenRandom`, using the default cryptographic
|
|
|
|
/// service provider with the `PROV_RSA_FULL` type.
|
2013-09-22 05:51:57 -05:00
|
|
|
///
|
|
|
|
/// This does not block.
|
|
|
|
#[cfg(unix)]
|
|
|
|
pub struct OSRng {
|
2013-10-30 01:31:07 -05:00
|
|
|
priv inner: ReaderRng<File>
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
|
|
|
/// A random number generator that retrieves randomness straight from
|
2013-10-11 01:25:40 -05:00
|
|
|
/// the operating system. Platform sources:
|
|
|
|
///
|
|
|
|
/// - Unix-like systems (Linux, Android, Mac OSX): read directly from
|
|
|
|
/// `/dev/urandom`.
|
|
|
|
/// - Windows: calls `CryptGenRandom`, using the default cryptographic
|
|
|
|
/// service provider with the `PROV_RSA_FULL` type.
|
2013-09-22 05:51:57 -05:00
|
|
|
///
|
|
|
|
/// This does not block.
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub struct OSRng {
|
2013-10-08 17:45:38 -05:00
|
|
|
priv hcryptprov: HCRYPTPROV
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl OSRng {
|
|
|
|
/// Create a new `OSRng`.
|
|
|
|
#[cfg(unix)]
|
|
|
|
pub fn new() -> OSRng {
|
2013-12-03 21:15:12 -06:00
|
|
|
let reader = File::open(&Path::new("/dev/urandom"));
|
2014-01-29 18:33:57 -06:00
|
|
|
let reader = reader.ok().expect("Error opening /dev/urandom");
|
2013-09-22 05:51:57 -05:00
|
|
|
let reader_rng = ReaderRng::new(reader);
|
|
|
|
|
|
|
|
OSRng { inner: reader_rng }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new `OSRng`.
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn new() -> OSRng {
|
2013-11-06 17:16:04 -06:00
|
|
|
extern { fn rust_win32_rand_acquire(phProv: *mut HCRYPTPROV); }
|
2013-10-08 17:45:38 -05:00
|
|
|
|
2013-10-01 11:18:57 -05:00
|
|
|
let mut hcp = 0;
|
2013-10-08 17:45:38 -05:00
|
|
|
unsafe {rust_win32_rand_acquire(&mut hcp)};
|
2013-09-22 05:51:57 -05:00
|
|
|
|
|
|
|
OSRng { hcryptprov: hcp }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
impl Rng for OSRng {
|
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
self.inner.next_u32()
|
|
|
|
}
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
|
|
|
self.inner.next_u64()
|
|
|
|
}
|
|
|
|
fn fill_bytes(&mut self, v: &mut [u8]) {
|
|
|
|
self.inner.fill_bytes(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl Rng for OSRng {
|
|
|
|
fn next_u32(&mut self) -> u32 {
|
|
|
|
let mut v = [0u8, .. 4];
|
|
|
|
self.fill_bytes(v);
|
|
|
|
unsafe { cast::transmute(v) }
|
|
|
|
}
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
|
|
|
let mut v = [0u8, .. 8];
|
|
|
|
self.fill_bytes(v);
|
|
|
|
unsafe { cast::transmute(v) }
|
|
|
|
}
|
|
|
|
fn fill_bytes(&mut self, v: &mut [u8]) {
|
2013-11-06 17:16:04 -06:00
|
|
|
extern {
|
|
|
|
fn rust_win32_rand_gen(hProv: HCRYPTPROV, dwLen: DWORD,
|
|
|
|
pbBuffer: *mut BYTE);
|
|
|
|
}
|
2013-10-01 11:18:57 -05:00
|
|
|
|
2013-12-17 09:13:20 -06:00
|
|
|
unsafe {rust_win32_rand_gen(self.hcryptprov, v.len() as DWORD, v.as_mut_ptr())}
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for OSRng {
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// ensure that OSRng is not implicitly copyable on all
|
|
|
|
// platforms, for consistency.
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn drop(&mut self) {
|
2013-11-06 17:16:04 -06:00
|
|
|
extern { fn rust_win32_rand_release(hProv: HCRYPTPROV); }
|
2013-09-22 05:51:57 -05:00
|
|
|
|
2013-10-08 17:45:38 -05:00
|
|
|
unsafe {rust_win32_rand_release(self.hcryptprov)}
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-08 17:45:38 -05:00
|
|
|
|
2013-09-22 05:51:57 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2014-03-01 18:23:04 -06:00
|
|
|
use super::OSRng;
|
|
|
|
use Rng;
|
|
|
|
use std::task;
|
2013-09-22 05:51:57 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_os_rng() {
|
|
|
|
let mut r = OSRng::new();
|
|
|
|
|
|
|
|
r.next_u32();
|
|
|
|
r.next_u64();
|
|
|
|
|
|
|
|
let mut v = [0u8, .. 1000];
|
|
|
|
r.fill_bytes(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_os_rng_tasks() {
|
|
|
|
|
|
|
|
let mut chans = ~[];
|
|
|
|
for _ in range(0, 20) {
|
2013-12-05 20:19:06 -06:00
|
|
|
let (p, c) = Chan::new();
|
2013-09-22 05:51:57 -05:00
|
|
|
chans.push(c);
|
2014-01-26 21:42:26 -06:00
|
|
|
task::spawn(proc() {
|
2013-09-22 05:51:57 -05:00
|
|
|
// wait until all the tasks are ready to go.
|
|
|
|
p.recv();
|
|
|
|
|
|
|
|
// deschedule to attempt to interleave things as much
|
|
|
|
// as possible (XXX: is this a good test?)
|
|
|
|
let mut r = OSRng::new();
|
|
|
|
task::deschedule();
|
|
|
|
let mut v = [0u8, .. 1000];
|
|
|
|
|
|
|
|
for _ in range(0, 100) {
|
|
|
|
r.next_u32();
|
|
|
|
task::deschedule();
|
|
|
|
r.next_u64();
|
|
|
|
task::deschedule();
|
|
|
|
r.fill_bytes(v);
|
|
|
|
task::deschedule();
|
|
|
|
}
|
2014-01-26 21:42:26 -06:00
|
|
|
})
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// start all the tasks
|
|
|
|
for c in chans.iter() {
|
|
|
|
c.send(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|