Skip to content

Commit 865a213

Browse files
authored
Rollup merge of rust-lang#84096 - m-ou-se:windows-bcrypt-random, r=dtolnay
Use BCryptGenRandom instead of RtlGenRandom on Windows. This removes usage of RtlGenRandom on Windows, in favour of BCryptGenRandom. BCryptGenRandom isn't available on XP, but we dropped XP support a while ago.
2 parents 35d0d04 + b8a9112 commit 865a213

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ pub const STACK_SIZE_PARAM_IS_A_RESERVATION: DWORD = 0x00010000;
287287

288288
pub const STATUS_SUCCESS: NTSTATUS = 0x00000000;
289289

290+
pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD = 0x00000002;
291+
290292
#[repr(C)]
291293
#[cfg(not(target_pointer_width = "64"))]
292294
pub struct WSADATA {
@@ -687,9 +689,6 @@ if #[cfg(not(target_vendor = "uwp"))] {
687689
pub const TOKEN_READ: DWORD = 0x20008;
688690

689691
extern "system" {
690-
#[link_name = "SystemFunction036"]
691-
pub fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: ULONG) -> BOOLEAN;
692-
693692
pub fn ReadConsoleW(hConsoleInput: HANDLE,
694693
lpBuffer: LPVOID,
695694
nNumberOfCharsToRead: DWORD,
@@ -731,8 +730,6 @@ if #[cfg(not(target_vendor = "uwp"))] {
731730
// UWP specific functions & types
732731
cfg_if::cfg_if! {
733732
if #[cfg(target_vendor = "uwp")] {
734-
pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD = 0x00000002;
735-
736733
#[repr(C)]
737734
pub struct FILE_STANDARD_INFO {
738735
pub AllocationSize: LARGE_INTEGER,
@@ -747,8 +744,6 @@ if #[cfg(target_vendor = "uwp")] {
747744
fileInfoClass: FILE_INFO_BY_HANDLE_CLASS,
748745
lpFileInformation: LPVOID,
749746
dwBufferSize: DWORD) -> BOOL;
750-
pub fn BCryptGenRandom(hAlgorithm: LPVOID, pBuffer: *mut u8,
751-
cbBuffer: ULONG, dwFlags: ULONG) -> LONG;
752747
}
753748
}
754749
}
@@ -1068,6 +1063,15 @@ extern "system" {
10681063
pub fn ReleaseSRWLockShared(SRWLock: PSRWLOCK);
10691064
pub fn TryAcquireSRWLockExclusive(SRWLock: PSRWLOCK) -> BOOLEAN;
10701065
pub fn TryAcquireSRWLockShared(SRWLock: PSRWLOCK) -> BOOLEAN;
1066+
1067+
// >= Vista / Server 2008
1068+
// https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
1069+
pub fn BCryptGenRandom(
1070+
hAlgorithm: LPVOID,
1071+
pBuffer: *mut u8,
1072+
cbBuffer: ULONG,
1073+
dwFlags: ULONG,
1074+
) -> NTSTATUS;
10711075
}
10721076

10731077
// Functions that aren't available on every version of Windows that we support,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,13 @@ pub fn abort_internal() -> ! {
276276
cfg_if::cfg_if! {
277277
if #[cfg(target_vendor = "uwp")] {
278278
#[link(name = "ws2_32")]
279-
// For BCryptGenRandom
280-
#[link(name = "bcrypt")]
279+
#[link(name = "bcrypt")] // For BCryptGenRandom
281280
extern "C" {}
282281
} else {
283282
#[link(name = "advapi32")]
284283
#[link(name = "ws2_32")]
285284
#[link(name = "userenv")]
285+
#[link(name = "bcrypt")] // For BCryptGenRandom
286286
extern "C" {}
287287
}
288288
}

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

-12
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@ use crate::io;
22
use crate::mem;
33
use crate::sys::c;
44

5-
#[cfg(not(target_vendor = "uwp"))]
6-
pub fn hashmap_random_keys() -> (u64, u64) {
7-
let mut v = (0, 0);
8-
let ret =
9-
unsafe { c::RtlGenRandom(&mut v as *mut _ as *mut u8, mem::size_of_val(&v) as c::ULONG) };
10-
if ret == 0 {
11-
panic!("couldn't generate random bytes: {}", io::Error::last_os_error());
12-
}
13-
v
14-
}
15-
16-
#[cfg(target_vendor = "uwp")]
175
pub fn hashmap_random_keys() -> (u64, u64) {
186
use crate::ptr;
197

0 commit comments

Comments
 (0)