Skip to content

Commit be7b6dc

Browse files
committed
libstd: windows: Use BCryptGenRandom on UWP
Since RtlGenRandom isn't available
1 parent c491116 commit be7b6dc

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/libstd/build.rs

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ fn main() {
4646
println!("cargo:rustc-link-lib=userenv");
4747
} else if target.contains("uwp") {
4848
println!("cargo:rustc-link-lib=ws2_32");
49+
// For BCryptGenRandom
50+
println!("cargo:rustc-link-lib=bcrypt");
4951
} else if target.contains("fuchsia") {
5052
println!("cargo:rustc-link-lib=zircon");
5153
println!("cargo:rustc-link-lib=fdio");

src/libstd/sys/windows/c.rs

+8
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ pub const STACK_SIZE_PARAM_IS_A_RESERVATION: DWORD = 0x00010000;
321321

322322
pub const HEAP_ZERO_MEMORY: DWORD = 0x00000008;
323323

324+
#[cfg(target_os = "uwp")]
325+
pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD = 0x00000002;
326+
324327
#[repr(C)]
325328
#[cfg(not(target_pointer_width = "64"))]
326329
pub struct WSADATA {
@@ -1323,8 +1326,13 @@ extern "system" {
13231326
timeout: *const timeval) -> c_int;
13241327

13251328
#[link_name = "SystemFunction036"]
1329+
#[cfg(not(target_os = "uwp"))]
13261330
pub fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: ULONG) -> BOOLEAN;
13271331

1332+
#[cfg(target_os = "uwp")]
1333+
pub fn BCryptGenRandom(hAlgorithm: LPVOID, pBuffer: *mut u8,
1334+
cbBuffer: ULONG, dwFlags: ULONG) -> LONG;
1335+
13281336
pub fn GetProcessHeap() -> HANDLE;
13291337
pub fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
13301338
pub fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;

src/libstd/sys/windows/rand.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::io;
22
use crate::mem;
3+
#[cfg(target_os = "uwp")]
4+
use crate::ptr;
35
use crate::sys::c;
46

7+
#[cfg(not(target_os = "uwp"))]
58
pub fn hashmap_random_keys() -> (u64, u64) {
69
let mut v = (0, 0);
710
let ret = unsafe {
@@ -14,3 +17,18 @@ pub fn hashmap_random_keys() -> (u64, u64) {
1417
}
1518
return v
1619
}
20+
21+
#[cfg(target_os = "uwp")]
22+
pub fn hashmap_random_keys() -> (u64, u64) {
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)