From 1ed123828c97d9f7e031a9caee02e340295ef21e Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 11 Apr 2021 14:55:47 +0200 Subject: [PATCH] Use BCryptGenRandom instead of RtlGenRandom on Windows. BCryptGenRandom isn't available on XP, but we dropped XP support a while ago. --- library/std/src/sys/windows/c.rs | 29 ++++++++++++++--------------- library/std/src/sys/windows/rand.rs | 12 ------------ 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/library/std/src/sys/windows/c.rs b/library/std/src/sys/windows/c.rs index 6fb850d1828..e5c550802a7 100644 --- a/library/std/src/sys/windows/c.rs +++ b/library/std/src/sys/windows/c.rs @@ -262,6 +262,8 @@ pub struct ipv6_mreq { 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 { @@ -678,10 +680,6 @@ pub struct BY_HANDLE_FILE_INFORMATION { #[link(name = "advapi32")] extern "system" { - // Forbidden when targeting UWP - #[link_name = "SystemFunction036"] - pub fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: ULONG) -> BOOLEAN; - // Allowed but unused by UWP pub fn OpenProcessToken( ProcessHandle: HANDLE, @@ -743,8 +741,6 @@ pub fn CreateHardLinkW( // 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, @@ -754,15 +750,6 @@ pub struct FILE_STANDARD_INFO { pub Directory: BOOLEAN, } - #[link(name = "bcrypt")] - extern "system" { - pub fn BCryptGenRandom( - hAlgorithm: LPVOID, - pBuffer: *mut u8, - cbBuffer: ULONG, - dwFlags: ULONG, - ) -> LONG; - } #[link(name = "kernel32")] extern "system" { pub fn GetFileInformationByHandleEx( @@ -1085,6 +1072,18 @@ pub fn select( ) -> c_int; } +#[link(name = "bcrypt")] +extern "system" { + // >= 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, // but we still use them and just provide some form of a fallback implementation. compat_fn! { diff --git a/library/std/src/sys/windows/rand.rs b/library/std/src/sys/windows/rand.rs index 87ea416bf67..de73e9154b4 100644 --- a/library/std/src/sys/windows/rand.rs +++ b/library/std/src/sys/windows/rand.rs @@ -2,18 +2,6 @@ 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; -- 2.44.0