|
1 | | -use crate::mem; |
2 | | -use crate::ptr; |
3 | 1 | use crate::sys::c; |
| 2 | +use core::mem; |
| 3 | +use core::ptr; |
4 | 4 |
|
| 5 | +#[cfg(not(target_vendor = "win7"))] |
| 6 | +#[inline] |
5 | 7 | pub fn hashmap_random_keys() -> (u64, u64) { |
6 | 8 | let mut v = (0, 0); |
7 | | - let ret = unsafe { |
8 | | - c::BCryptGenRandom( |
9 | | - ptr::null_mut(), |
10 | | - core::ptr::addr_of_mut!(v) as *mut u8, |
11 | | - mem::size_of_val(&v) as c::ULONG, |
12 | | - c::BCRYPT_USE_SYSTEM_PREFERRED_RNG, |
13 | | - ) |
14 | | - }; |
15 | | - if c::nt_success(ret) { v } else { fallback_rng() } |
| 9 | + let ret = unsafe { c::ProcessPrng(ptr::addr_of_mut!(v).cast::<u8>(), mem::size_of_val(&v)) }; |
| 10 | + // ProcessPrng is documented as always returning `TRUE`. |
| 11 | + // https://learn.microsoft.com/en-us/windows/win32/seccng/processprng#return-value |
| 12 | + debug_assert_eq!(ret, c::TRUE); |
| 13 | + v |
16 | 14 | } |
17 | 15 |
|
18 | | -/// Generate random numbers using the fallback RNG function (RtlGenRandom) |
19 | | -/// |
20 | | -/// This is necessary because of a failure to load the SysWOW64 variant of the |
21 | | -/// bcryptprimitives.dll library from code that lives in bcrypt.dll |
22 | | -/// See <https://bugzilla.mozilla.org/show_bug.cgi?id=1788004#c9> |
23 | | -#[cfg(not(target_vendor = "uwp"))] |
24 | | -#[inline(never)] |
25 | | -fn fallback_rng() -> (u64, u64) { |
| 16 | +#[cfg(target_vendor = "win7")] |
| 17 | +pub fn hashmap_random_keys() -> (u64, u64) { |
26 | 18 | use crate::ffi::c_void; |
27 | 19 | use crate::io; |
28 | 20 |
|
29 | 21 | let mut v = (0, 0); |
30 | 22 | let ret = unsafe { |
31 | | - c::RtlGenRandom(core::ptr::addr_of_mut!(v) as *mut c_void, mem::size_of_val(&v) as c::ULONG) |
| 23 | + c::RtlGenRandom(ptr::addr_of_mut!(v).cast::<c_void>(), mem::size_of_val(&v) as c::ULONG) |
32 | 24 | }; |
33 | 25 |
|
34 | | - if ret != 0 { v } else { panic!("fallback RNG broken: {}", io::Error::last_os_error()) } |
35 | | -} |
36 | | - |
37 | | -/// We can't use RtlGenRandom with UWP, so there is no fallback |
38 | | -#[cfg(target_vendor = "uwp")] |
39 | | -#[inline(never)] |
40 | | -fn fallback_rng() -> (u64, u64) { |
41 | | - panic!("fallback RNG broken: RtlGenRandom() not supported on UWP"); |
| 26 | + if ret != 0 { v } else { panic!("RNG broken: {}", io::Error::last_os_error()) } |
42 | 27 | } |
0 commit comments