Skip to content

Commit 9407ed7

Browse files
committed
std: rand: Use BCrypt on UWP
As Rtl* functions are not allowed there
1 parent 642f8cd commit 9407ed7

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/libstd/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ fn main() {
4141
println!("cargo:rustc-link-lib=resolv");
4242
} else if target.contains("uwp") {
4343
println!("cargo:rustc-link-lib=ws2_32");
44+
// For BCryptGenRandom
45+
println!("cargo:rustc-link-lib=bcrypt");
4446
} else if target.contains("windows") {
4547
println!("cargo:rustc-link-lib=advapi32");
4648
println!("cargo:rustc-link-lib=ws2_32");

src/libstd/sys/windows/c.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,29 @@ pub struct timeval {
655655
pub tv_usec: c_long,
656656
}
657657

658+
// Functions forbidden when targeting UWP
659+
cfg_if::cfg_if! {
660+
if #[cfg(not(target_vendor = "uwp"))] {
661+
extern "system" {
662+
#[link_name = "SystemFunction036"]
663+
pub fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: ULONG) -> BOOLEAN;
664+
}
665+
}
666+
}
667+
668+
// UWP specific functions & types
669+
cfg_if::cfg_if! {
670+
if #[cfg(target_vendor = "uwp")] {
671+
pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD = 0x00000002;
672+
673+
extern "system" {
674+
pub fn BCryptGenRandom(hAlgorithm: LPVOID, pBuffer: *mut u8,
675+
cbBuffer: ULONG, dwFlags: ULONG) -> LONG;
676+
}
677+
}
678+
}
679+
680+
// Shared between Desktop & UWP
658681
extern "system" {
659682
pub fn WSAStartup(wVersionRequested: WORD,
660683
lpWSAData: LPWSADATA) -> c_int;
@@ -950,8 +973,6 @@ extern "system" {
950973
exceptfds: *mut fd_set,
951974
timeout: *const timeval) -> c_int;
952975

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

956977
pub fn GetProcessHeap() -> HANDLE;
957978
pub fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;

src/libstd/sys/windows/rand.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::io;
22
use crate::mem;
33
use crate::sys::c;
44

5+
#[cfg(not(target_vendor = "uwp"))]
56
pub fn hashmap_random_keys() -> (u64, u64) {
67
let mut v = (0, 0);
78
let ret = unsafe {
@@ -14,3 +15,20 @@ pub fn hashmap_random_keys() -> (u64, u64) {
1415
}
1516
return v
1617
}
18+
19+
#[cfg(target_vendor = "uwp")]
20+
pub fn hashmap_random_keys() -> (u64, u64) {
21+
use crate::ptr;
22+
23+
let mut v = (0, 0);
24+
let ret = unsafe {
25+
c::BCryptGenRandom(ptr::null_mut(), &mut v as *mut _ as *mut u8,
26+
mem::size_of_val(&v) as c::ULONG,
27+
c::BCRYPT_USE_SYSTEM_PREFERRED_RNG)
28+
};
29+
if ret != 0 {
30+
panic!("couldn't generate random bytes: {}",
31+
io::Error::last_os_error());
32+
}
33+
return v
34+
}

0 commit comments

Comments
 (0)