-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Windows: Use ProcessPrng for random keys
- Loading branch information
1 parent
43fdd49
commit 08caefb
Showing
4 changed files
with
28 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,27 @@ | ||
use crate::mem; | ||
use crate::ptr; | ||
use crate::sys::c; | ||
use core::mem; | ||
use core::ptr; | ||
|
||
#[cfg(not(target_vendor = "win7"))] | ||
#[inline] | ||
pub fn hashmap_random_keys() -> (u64, u64) { | ||
let mut v = (0, 0); | ||
let ret = unsafe { | ||
c::BCryptGenRandom( | ||
ptr::null_mut(), | ||
core::ptr::addr_of_mut!(v) as *mut u8, | ||
mem::size_of_val(&v) as c::ULONG, | ||
c::BCRYPT_USE_SYSTEM_PREFERRED_RNG, | ||
) | ||
}; | ||
if c::nt_success(ret) { v } else { fallback_rng() } | ||
let ret = unsafe { c::ProcessPrng(ptr::addr_of_mut!(v).cast::<u8>(), mem::size_of_val(&v)) }; | ||
// ProcessPrng is documented as always returning `TRUE`. | ||
// https://learn.microsoft.com/en-us/windows/win32/seccng/processprng#return-value | ||
debug_assert_eq!(ret, c::TRUE); | ||
v | ||
} | ||
|
||
/// Generate random numbers using the fallback RNG function (RtlGenRandom) | ||
/// | ||
/// This is necessary because of a failure to load the SysWOW64 variant of the | ||
/// bcryptprimitives.dll library from code that lives in bcrypt.dll | ||
/// See <https://bugzilla.mozilla.org/show_bug.cgi?id=1788004#c9> | ||
#[cfg(not(target_vendor = "uwp"))] | ||
#[inline(never)] | ||
fn fallback_rng() -> (u64, u64) { | ||
#[cfg(target_vendor = "win7")] | ||
pub fn hashmap_random_keys() -> (u64, u64) { | ||
use crate::ffi::c_void; | ||
use crate::io; | ||
|
||
let mut v = (0, 0); | ||
let ret = unsafe { | ||
c::RtlGenRandom(core::ptr::addr_of_mut!(v) as *mut c_void, mem::size_of_val(&v) as c::ULONG) | ||
c::RtlGenRandom(ptr::addr_of_mut!(v).cast::<c_void>(), mem::size_of_val(&v) as c::ULONG) | ||
}; | ||
|
||
if ret != 0 { v } else { panic!("fallback RNG broken: {}", io::Error::last_os_error()) } | ||
} | ||
|
||
/// We can't use RtlGenRandom with UWP, so there is no fallback | ||
#[cfg(target_vendor = "uwp")] | ||
#[inline(never)] | ||
fn fallback_rng() -> (u64, u64) { | ||
panic!("fallback RNG broken: RtlGenRandom() not supported on UWP"); | ||
if ret != 0 { v } else { panic!("RNG broken: {}", io::Error::last_os_error()) } | ||
} |