Skip to content

Commit

Permalink
Rollup merge of rust-lang#84096 - m-ou-se:windows-bcrypt-random, r=dt…
Browse files Browse the repository at this point in the history
…olnay

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.
  • Loading branch information
m-ou-se authored Apr 21, 2021
2 parents 35d0d04 + b8a9112 commit 865a213
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
18 changes: 11 additions & 7 deletions library/std/src/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ pub const STACK_SIZE_PARAM_IS_A_RESERVATION: DWORD = 0x00010000;

pub const STATUS_SUCCESS: NTSTATUS = 0x00000000;

pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD = 0x00000002;

#[repr(C)]
#[cfg(not(target_pointer_width = "64"))]
pub struct WSADATA {
Expand Down Expand Up @@ -687,9 +689,6 @@ if #[cfg(not(target_vendor = "uwp"))] {
pub const TOKEN_READ: DWORD = 0x20008;

extern "system" {
#[link_name = "SystemFunction036"]
pub fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: ULONG) -> BOOLEAN;

pub fn ReadConsoleW(hConsoleInput: HANDLE,
lpBuffer: LPVOID,
nNumberOfCharsToRead: DWORD,
Expand Down Expand Up @@ -731,8 +730,6 @@ if #[cfg(not(target_vendor = "uwp"))] {
// UWP specific functions & types
cfg_if::cfg_if! {
if #[cfg(target_vendor = "uwp")] {
pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD = 0x00000002;

#[repr(C)]
pub struct FILE_STANDARD_INFO {
pub AllocationSize: LARGE_INTEGER,
Expand All @@ -747,8 +744,6 @@ if #[cfg(target_vendor = "uwp")] {
fileInfoClass: FILE_INFO_BY_HANDLE_CLASS,
lpFileInformation: LPVOID,
dwBufferSize: DWORD) -> BOOL;
pub fn BCryptGenRandom(hAlgorithm: LPVOID, pBuffer: *mut u8,
cbBuffer: ULONG, dwFlags: ULONG) -> LONG;
}
}
}
Expand Down Expand Up @@ -1068,6 +1063,15 @@ extern "system" {
pub fn ReleaseSRWLockShared(SRWLock: PSRWLOCK);
pub fn TryAcquireSRWLockExclusive(SRWLock: PSRWLOCK) -> BOOLEAN;
pub fn TryAcquireSRWLockShared(SRWLock: PSRWLOCK) -> BOOLEAN;

// >= Vista / Server 2008
// https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
pub fn BCryptGenRandom(
hAlgorithm: LPVOID,
pBuffer: *mut u8,
cbBuffer: ULONG,
dwFlags: ULONG,
) -> NTSTATUS;
}

// Functions that aren't available on every version of Windows that we support,
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,13 @@ pub fn abort_internal() -> ! {
cfg_if::cfg_if! {
if #[cfg(target_vendor = "uwp")] {
#[link(name = "ws2_32")]
// For BCryptGenRandom
#[link(name = "bcrypt")]
#[link(name = "bcrypt")] // For BCryptGenRandom
extern "C" {}
} else {
#[link(name = "advapi32")]
#[link(name = "ws2_32")]
#[link(name = "userenv")]
#[link(name = "bcrypt")] // For BCryptGenRandom
extern "C" {}
}
}
12 changes: 0 additions & 12 deletions library/std/src/sys/windows/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ use crate::io;
use crate::mem;
use crate::sys::c;

#[cfg(not(target_vendor = "uwp"))]
pub fn hashmap_random_keys() -> (u64, u64) {
let mut v = (0, 0);
let ret =
unsafe { c::RtlGenRandom(&mut v as *mut _ as *mut u8, mem::size_of_val(&v) as c::ULONG) };
if ret == 0 {
panic!("couldn't generate random bytes: {}", io::Error::last_os_error());
}
v
}

#[cfg(target_vendor = "uwp")]
pub fn hashmap_random_keys() -> (u64, u64) {
use crate::ptr;

Expand Down

0 comments on commit 865a213

Please sign in to comment.