Skip to content

Commit 0bd71e4

Browse files
committed
1 parent ff479b1 commit 0bd71e4

File tree

2 files changed

+5
-38
lines changed

2 files changed

+5
-38
lines changed

library/std/src/sys/windows/c.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ pub fn nt_success(status: NTSTATUS) -> bool {
285285
status >= 0
286286
}
287287

288-
pub const BCRYPT_RNG_ALG_HANDLE: usize = 0x81;
288+
pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD = 0x00000002;
289289

290290
#[repr(C)]
291291
pub struct UNICODE_STRING {

library/std/src/sys/windows/rand.rs

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,16 @@
1-
//! # Random key generation
2-
//!
3-
//! This module wraps the RNG provided by the OS. There are a few different
4-
//! ways to interface with the OS RNG so it's worth exploring each of the options.
5-
//! Note that at the time of writing these all go through the (undocumented)
6-
//! `bcryptPrimitives.dll` but they use different route to get there.
7-
//!
8-
//! Originally we were using [`RtlGenRandom`], however that function is
9-
//! deprecated and warns it "may be altered or unavailable in subsequent versions".
10-
//!
11-
//! So we switched to [`BCryptGenRandom`] with the `BCRYPT_USE_SYSTEM_PREFERRED_RNG`
12-
//! flag to query and find the system configured RNG. However, this change caused a small
13-
//! but significant number of users to experience panics caused by a failure of
14-
//! this function. See [#94098].
15-
//!
16-
//! The current version changes this to use the `BCRYPT_RNG_ALG_HANDLE`
17-
//! [Pseudo-handle], which gets the default RNG algorithm without querying the
18-
//! system preference thus hopefully avoiding the previous issue.
19-
//! This is only supported on Windows 10+ so a fallback is used for older versions.
20-
//!
21-
//! [#94098]: https://github.com/rust-lang/rust/issues/94098
22-
//! [`RtlGenRandom`]: https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom
23-
//! [`BCryptGenRandom`]: https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
24-
//! [Pseudo-handle]: https://docs.microsoft.com/en-us/windows/win32/seccng/cng-algorithm-pseudo-handles
251
use crate::io;
262
use crate::mem;
273
use crate::ptr;
284
use crate::sys::c;
295

30-
/// Generates high quality secure random keys for use by [`HashMap`].
31-
///
32-
/// This is used to seed the default [`RandomState`].
33-
///
34-
/// [`HashMap`]: crate::collections::HashMap
35-
/// [`RandomState`]: crate::collections::hash_map::RandomState
366
pub fn hashmap_random_keys() -> (u64, u64) {
377
let mut v = (0, 0);
388
let ret = unsafe {
39-
let size = mem::size_of_val(&v).try_into().unwrap();
409
c::BCryptGenRandom(
41-
// BCRYPT_RNG_ALG_HANDLE is only supported in Windows 10+.
42-
// So for Windows 8.1 and Windows 7 we'll need a fallback when this fails.
43-
ptr::invalid_mut(c::BCRYPT_RNG_ALG_HANDLE),
44-
ptr::addr_of_mut!(v).cast(),
45-
size,
46-
0,
10+
ptr::null_mut(),
11+
&mut v as *mut _ as *mut u8,
12+
mem::size_of_val(&v) as c::ULONG,
13+
c::BCRYPT_USE_SYSTEM_PREFERRED_RNG,
4714
)
4815
};
4916
if ret != 0 { fallback_rng() } else { v }

0 commit comments

Comments
 (0)