Skip to content

Commit 20eb746

Browse files
committed
std: rand: Use BCrypt on UWP
As Rtl* functions are not allowed there
1 parent 98f9bba commit 20eb746

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-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: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#![cfg_attr(test, allow(dead_code))]
55
#![unstable(issue = "0", feature = "windows_c")]
66

7+
macro_rules! ifdef {
8+
($($t:tt)*) => ($($t)*)
9+
}
10+
711
use crate::os::raw::{c_int, c_uint, c_ulong, c_long, c_longlong, c_ushort, c_char};
812
use crate::ptr;
913

@@ -655,6 +659,27 @@ pub struct timeval {
655659
pub tv_usec: c_long,
656660
}
657661

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

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

956979
pub fn GetProcessHeap() -> HANDLE;
957980
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)