Skip to content

Commit c102653

Browse files
committed
Auto merge of #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 265fef4 + acee39e commit c102653

File tree

3 files changed

+16
-29
lines changed

3 files changed

+16
-29
lines changed

Diff for: library/std/src/sys/windows/c.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ pub const STACK_SIZE_PARAM_IS_A_RESERVATION: DWORD = 0x00010000;
262262

263263
pub const STATUS_SUCCESS: NTSTATUS = 0x00000000;
264264

265+
pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD = 0x00000002;
266+
265267
#[repr(C)]
266268
#[cfg(not(target_pointer_width = "64"))]
267269
pub struct WSADATA {
@@ -678,10 +680,6 @@ if #[cfg(not(target_vendor = "uwp"))] {
678680

679681
#[link(name = "advapi32")]
680682
extern "system" {
681-
// Forbidden when targeting UWP
682-
#[link_name = "SystemFunction036"]
683-
pub fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: ULONG) -> BOOLEAN;
684-
685683
// Allowed but unused by UWP
686684
pub fn OpenProcessToken(
687685
ProcessHandle: HANDLE,
@@ -743,8 +741,6 @@ if #[cfg(not(target_vendor = "uwp"))] {
743741
// UWP specific functions & types
744742
cfg_if::cfg_if! {
745743
if #[cfg(target_vendor = "uwp")] {
746-
pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD = 0x00000002;
747-
748744
#[repr(C)]
749745
pub struct FILE_STANDARD_INFO {
750746
pub AllocationSize: LARGE_INTEGER,
@@ -754,15 +750,6 @@ if #[cfg(target_vendor = "uwp")] {
754750
pub Directory: BOOLEAN,
755751
}
756752

757-
#[link(name = "bcrypt")]
758-
extern "system" {
759-
pub fn BCryptGenRandom(
760-
hAlgorithm: LPVOID,
761-
pBuffer: *mut u8,
762-
cbBuffer: ULONG,
763-
dwFlags: ULONG,
764-
) -> LONG;
765-
}
766753
#[link(name = "kernel32")]
767754
extern "system" {
768755
pub fn GetFileInformationByHandleEx(
@@ -1085,6 +1072,18 @@ extern "system" {
10851072
) -> c_int;
10861073
}
10871074

1075+
#[link(name = "bcrypt")]
1076+
extern "system" {
1077+
// >= Vista / Server 2008
1078+
// https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
1079+
pub fn BCryptGenRandom(
1080+
hAlgorithm: LPVOID,
1081+
pBuffer: *mut u8,
1082+
cbBuffer: ULONG,
1083+
dwFlags: ULONG,
1084+
) -> NTSTATUS;
1085+
}
1086+
10881087
// Functions that aren't available on every version of Windows that we support,
10891088
// but we still use them and just provide some form of a fallback implementation.
10901089
compat_fn! {

Diff for: 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

Diff for: src/test/run-make-fulldeps/tools.mk

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ endif
101101
# Extra flags needed to compile a working executable with the standard library
102102
ifdef IS_WINDOWS
103103
ifdef IS_MSVC
104-
EXTRACFLAGS := ws2_32.lib userenv.lib advapi32.lib
104+
EXTRACFLAGS := ws2_32.lib userenv.lib advapi32.lib bcrypt.lib
105105
else
106-
EXTRACFLAGS := -lws2_32 -luserenv
106+
EXTRACFLAGS := -lws2_32 -luserenv -lbcrypt
107107
EXTRACXXFLAGS := -lstdc++
108108
# So this is a bit hacky: we can't use the DLL version of libstdc++ because
109109
# it pulls in the DLL version of libgcc, which means that we end up with 2

0 commit comments

Comments
 (0)