rand: Rewrite OsRng in Rust for windows
This removes even more rust_builtin.c code, and allows us to more gracefully handle errors (not a process panic, but a task failure).
This commit is contained in:
parent
ab1dd09d73
commit
d2e99a0b33
2 changed files with 116 additions and 159 deletions
|
@ -11,124 +11,140 @@
|
||||||
//! Interfaces to the operating system provided random number
|
//! Interfaces to the operating system provided random number
|
||||||
//! generators.
|
//! generators.
|
||||||
|
|
||||||
use Rng;
|
pub use self::imp::OSRng;
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use reader::ReaderRng;
|
mod imp {
|
||||||
#[cfg(unix)]
|
use Rng;
|
||||||
use std::io::File;
|
use reader::ReaderRng;
|
||||||
|
use std::io::File;
|
||||||
|
|
||||||
#[cfg(windows)]
|
/// A random number generator that retrieves randomness straight from
|
||||||
use std::cast;
|
/// the operating system. Platform sources:
|
||||||
#[cfg(windows)]
|
///
|
||||||
use std::libc::{c_long, DWORD, BYTE};
|
/// - Unix-like systems (Linux, Android, Mac OSX): read directly from
|
||||||
#[cfg(windows)]
|
/// `/dev/urandom`.
|
||||||
type HCRYPTPROV = c_long;
|
/// - Windows: calls `CryptGenRandom`, using the default cryptographic
|
||||||
// the extern functions imported from the runtime on Windows are
|
/// service provider with the `PROV_RSA_FULL` type.
|
||||||
// implemented so that they either succeed or abort(), so we can just
|
///
|
||||||
// assume they work when we call them.
|
/// This does not block.
|
||||||
|
|
||||||
/// A random number generator that retrieves randomness straight from
|
|
||||||
/// 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.
|
|
||||||
///
|
|
||||||
/// This does not block.
|
|
||||||
#[cfg(unix)]
|
|
||||||
pub struct OSRng {
|
|
||||||
priv inner: ReaderRng<File>
|
|
||||||
}
|
|
||||||
/// A random number generator that retrieves randomness straight from
|
|
||||||
/// 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.
|
|
||||||
///
|
|
||||||
/// This does not block.
|
|
||||||
#[cfg(windows)]
|
|
||||||
pub struct OSRng {
|
|
||||||
priv hcryptprov: HCRYPTPROV
|
|
||||||
}
|
|
||||||
|
|
||||||
impl OSRng {
|
|
||||||
/// Create a new `OSRng`.
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub fn new() -> OSRng {
|
pub struct OSRng {
|
||||||
let reader = File::open(&Path::new("/dev/urandom"));
|
priv inner: ReaderRng<File>
|
||||||
let reader = reader.ok().expect("Error opening /dev/urandom");
|
|
||||||
let reader_rng = ReaderRng::new(reader);
|
|
||||||
|
|
||||||
OSRng { inner: reader_rng }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new `OSRng`.
|
impl OSRng {
|
||||||
#[cfg(windows)]
|
/// Create a new `OSRng`.
|
||||||
pub fn new() -> OSRng {
|
pub fn new() -> OSRng {
|
||||||
extern { fn rust_win32_rand_acquire(phProv: *mut HCRYPTPROV); }
|
let reader = File::open(&Path::new("/dev/urandom"));
|
||||||
|
let reader = reader.ok().expect("Error opening /dev/urandom");
|
||||||
|
let reader_rng = ReaderRng::new(reader);
|
||||||
|
|
||||||
let mut hcp = 0;
|
OSRng { inner: reader_rng }
|
||||||
unsafe {rust_win32_rand_acquire(&mut hcp)};
|
|
||||||
|
|
||||||
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]) {
|
|
||||||
extern {
|
|
||||||
fn rust_win32_rand_gen(hProv: HCRYPTPROV, dwLen: DWORD,
|
|
||||||
pbBuffer: *mut BYTE);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unsafe {rust_win32_rand_gen(self.hcryptprov, v.len() as DWORD, v.as_mut_ptr())}
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for OSRng {
|
#[cfg(windows)]
|
||||||
#[cfg(unix)]
|
mod imp {
|
||||||
fn drop(&mut self) {
|
use Rng;
|
||||||
// ensure that OSRng is not implicitly copyable on all
|
use std::cast;
|
||||||
// platforms, for consistency.
|
use std::libc::{c_ulong, DWORD, BYTE, LPCSTR, BOOL};
|
||||||
|
use std::os;
|
||||||
|
|
||||||
|
type HCRYPTPROV = c_ulong;
|
||||||
|
|
||||||
|
/// A random number generator that retrieves randomness straight from
|
||||||
|
/// 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.
|
||||||
|
///
|
||||||
|
/// This does not block.
|
||||||
|
pub struct OSRng {
|
||||||
|
priv hcryptprov: HCRYPTPROV
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(windows)]
|
static PROV_RSA_FULL: DWORD = 1;
|
||||||
fn drop(&mut self) {
|
static CRYPT_SILENT: DWORD = 64;
|
||||||
extern { fn rust_win32_rand_release(hProv: HCRYPTPROV); }
|
static CRYPT_VERIFYCONTEXT: DWORD = 0xF0000000;
|
||||||
|
|
||||||
unsafe {rust_win32_rand_release(self.hcryptprov)}
|
extern "system" {
|
||||||
|
fn CryptAcquireContextA(phProv: *mut HCRYPTPROV,
|
||||||
|
pszContainer: LPCSTR,
|
||||||
|
pszProvider: LPCSTR,
|
||||||
|
dwProvType: DWORD,
|
||||||
|
dwFlags: DWORD) -> BOOL;
|
||||||
|
fn CryptGenRandom(hProv: HCRYPTPROV,
|
||||||
|
dwLen: DWORD,
|
||||||
|
pbBuffer: *mut BYTE) -> BOOL;
|
||||||
|
fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlags: DWORD) -> BOOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OSRng {
|
||||||
|
/// Create a new `OSRng`.
|
||||||
|
pub fn new() -> OSRng {
|
||||||
|
let mut hcp = 0;
|
||||||
|
let ret = unsafe {
|
||||||
|
CryptAcquireContextA(&mut hcp, 0 as LPCSTR, 0 as LPCSTR,
|
||||||
|
PROV_RSA_FULL,
|
||||||
|
CRYPT_VERIFYCONTEXT | CRYPT_SILENT)
|
||||||
|
};
|
||||||
|
if ret == 0 {
|
||||||
|
fail!("couldn't create context: {}", os::last_os_error());
|
||||||
|
}
|
||||||
|
OSRng { hcryptprov: hcp }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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]) {
|
||||||
|
let ret = unsafe {
|
||||||
|
CryptGenRandom(self.hcryptprov, v.len() as DWORD,
|
||||||
|
v.as_mut_ptr())
|
||||||
|
};
|
||||||
|
if ret == 0 {
|
||||||
|
fail!("couldn't generate random bytes: {}", os::last_os_error());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for OSRng {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let ret = unsafe {
|
||||||
|
CryptReleaseContext(self.hcryptprov, 0)
|
||||||
|
};
|
||||||
|
if ret == 0 {
|
||||||
|
fail!("couldn't release context: {}", os::last_os_error());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::OSRng;
|
use super::OSRng;
|
||||||
|
|
|
@ -387,65 +387,6 @@ rust_unset_sigprocmask() {
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__WIN32__)
|
|
||||||
void
|
|
||||||
win32_require(LPCTSTR fn, BOOL ok) {
|
|
||||||
if (!ok) {
|
|
||||||
LPTSTR buf;
|
|
||||||
DWORD err = GetLastError();
|
|
||||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
||||||
NULL, err,
|
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
||||||
(LPTSTR) &buf, 0, NULL );
|
|
||||||
fprintf(stderr, "%s failed with error %ld: %s", fn, err, buf);
|
|
||||||
LocalFree((HLOCAL)buf);
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
rust_win32_rand_acquire(HCRYPTPROV* phProv) {
|
|
||||||
win32_require
|
|
||||||
(_T("CryptAcquireContext"),
|
|
||||||
// changes to the parameters here should be reflected in the docs of
|
|
||||||
// rand::os::OSRng
|
|
||||||
CryptAcquireContext(phProv, NULL, NULL, PROV_RSA_FULL,
|
|
||||||
CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
|
|
||||||
|
|
||||||
}
|
|
||||||
void
|
|
||||||
rust_win32_rand_gen(HCRYPTPROV hProv, DWORD dwLen, BYTE* pbBuffer) {
|
|
||||||
win32_require
|
|
||||||
(_T("CryptGenRandom"), CryptGenRandom(hProv, dwLen, pbBuffer));
|
|
||||||
}
|
|
||||||
void
|
|
||||||
rust_win32_rand_release(HCRYPTPROV hProv) {
|
|
||||||
win32_require
|
|
||||||
(_T("CryptReleaseContext"), CryptReleaseContext(hProv, 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
// these symbols are listed in rustrt.def.in, so they need to exist; but they
|
|
||||||
// should never be called.
|
|
||||||
|
|
||||||
void
|
|
||||||
rust_win32_rand_acquire() {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
void
|
|
||||||
rust_win32_rand_gen() {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
void
|
|
||||||
rust_win32_rand_release() {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
// mode: C++
|
// mode: C++
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue