Skip to content

Commit e050963

Browse files
committed
add UWP support
1 parent d93954a commit e050963

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

build.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![deny(warnings)]
2+
3+
use std::env;
4+
5+
fn main() {
6+
let target = env::var("TARGET").expect("TARGET was not set");
7+
if target.contains("uwp") {
8+
// for BCryptGenRandom
9+
println!("cargo:rustc-link-lib=bcrypt");
10+
} else if target.contains("windows") {
11+
// for RtlGenRandom (aka SystemFunction036)
12+
println!("cargo:rustc-link-lib=advapi32");
13+
} else if target.contains("apple-ios") {
14+
// for SecRandomCopyBytes and kSecRandomDefault
15+
println!("cargo:rustc-link-lib=framework=Security");
16+
}
17+
}

src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ cfg_if! {
126126
macro_rules! error {
127127
($($x:tt)*) => {};
128128
}
129+
#[allow(unused)]
130+
macro_rules! warn {
131+
($($x:tt)*) => {};
132+
}
133+
#[allow(unused)]
134+
macro_rules! info {
135+
($($x:tt)*) => {};
136+
}
129137
}
130138
}
131139

@@ -216,6 +224,15 @@ cfg_if! {
216224
#[path = "solaris_illumos.rs"] mod imp;
217225
} else if #[cfg(target_os = "wasi")] {
218226
#[path = "wasi.rs"] mod imp;
227+
} else if #[cfg(any(
228+
// target_vendor was stabilized only in Rust 1.33
229+
target = "i686-uwp-windows-gnu",
230+
target = "x86_64-uwp-windows-gnu",
231+
target = "aarch64-uwp-windows-msvc",
232+
target = "x86_64-uwp-windows-msvc",
233+
target = "i686-uwp-windows-msvc",
234+
))] {
235+
#[path = "windows_uwp.rs"] mod imp;
219236
} else if #[cfg(windows)] {
220237
#[path = "windows.rs"] mod imp;
221238
} else if #[cfg(all(target_arch = "x86_64", any(

src/windows_uwp.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2018 Developers of the Rand project.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Implementation for Windows UWP targets. After deprecation of Windows XP
10+
//! and Vista, this can superseed the `RtlGenRandom`-based implementation.
11+
use crate::Error;
12+
use core::{ffi::c_void, num::NonZeroU32, ptr, u32};
13+
14+
const BCRYPT_USE_SYSTEM_PREFERRED_RNG: u32 = 0x00000002;
15+
16+
extern "system" {
17+
fn BCryptGenRandom(
18+
hAlgorithm: *mut c_void,
19+
pBuffer: *mut u8,
20+
cbBuffer: u32,
21+
dwFlags: u32,
22+
) -> u32;
23+
}
24+
25+
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
26+
// Prevent overflow of u32
27+
for chunk in dest.chunks_mut(u32::max_value() as usize) {
28+
let ret = unsafe {
29+
BCryptGenRandom(
30+
ptr::null_mut(),
31+
chunk.as_mut_ptr(),
32+
chunk.len() as u32,
33+
BCRYPT_USE_SYSTEM_PREFERRED_RNG,
34+
)
35+
};
36+
// NTSTATUS codes use two highest bits for severity codes
37+
match ret >> 30 {
38+
0b01 => info!("BCryptGenRandom: information code 0x{:08X}", ret),
39+
0b10 => warn!("BCryptGenRandom: warning code 0x{:08X}", ret),
40+
0b11 => {
41+
error!("BCryptGenRandom: failed with 0x{:08X}", ret);
42+
// We zeroize the highest bit, so the error code will reside
43+
// inside the range designated for OS codes.
44+
let code = ret & (u32::MAX >> 1);
45+
// SAFETY: the second highest bit is always equal to one,
46+
// so it's impossible to get zero. Unfortunately compiler
47+
// is not smart enough to figure out it yet.
48+
let code = unsafe { NonZeroU32::new_unchecked(code) };
49+
return Err(Error::from(code));
50+
}
51+
_ => (),
52+
}
53+
}
54+
Ok(())
55+
}

0 commit comments

Comments
 (0)