Skip to content

Commit 95d0fe8

Browse files
committed
remove Windows XP support
1 parent 2d89994 commit 95d0fe8

File tree

4 files changed

+32
-66
lines changed

4 files changed

+32
-66
lines changed

build.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ use std::env;
44

55
fn main() {
66
let target = env::var("TARGET").expect("TARGET was not set");
7-
if target.contains("-uwp-windows-") {
7+
if target.contains("windows") {
88
// for BCryptGenRandom
99
println!("cargo:rustc-link-lib=bcrypt");
10-
} else if target.contains("windows") {
11-
// for RtlGenRandom (aka SystemFunction036)
12-
println!("cargo:rustc-link-lib=advapi32");
1310
} else if target.contains("apple-ios") {
1411
// for SecRandomCopyBytes and kSecRandomDefault
1512
println!("cargo:rustc-link-lib=framework=Security");

src/lib.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
//! | Target | Target Triple | Implementation
1414
//! | ----------------- | ------------------ | --------------
1515
//! | Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call if available, otherwise [`/dev/urandom`][2] after successfully polling `/dev/random` |
16-
//! | Windows | `*‑pc‑windows‑*` | [`RtlGenRandom`][3] |
17-
//! | [Windows UWP][22] | `*‑uwp‑windows‑*` | [`BCryptGenRandom`][23] |
16+
//! | Windows | `*‑pc‑windows‑*` | [`BCryptGenRandom`][3] |
1817
//! | macOS | `*‑apple‑darwin` | [`getentropy()`][19] if available, otherwise [`/dev/random`][20] (identical to `/dev/urandom`)
1918
//! | iOS | `*‑apple‑ios` | [`SecRandomCopyBytes`][4]
2019
//! | FreeBSD | `*‑freebsd` | [`getrandom()`][21] if available, otherwise [`kern.arandom`][5]
@@ -123,7 +122,7 @@
123122
//!
124123
//! [1]: http://man7.org/linux/man-pages/man2/getrandom.2.html
125124
//! [2]: http://man7.org/linux/man-pages/man4/urandom.4.html
126-
//! [3]: https://docs.microsoft.com/en-us/windows/desktop/api/ntsecapi/nf-ntsecapi-rtlgenrandom
125+
//! [3]: https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
127126
//! [4]: https://developer.apple.com/documentation/security/1399291-secrandomcopybytes?language=objc
128127
//! [5]: https://www.freebsd.org/cgi/man.cgi?query=random&sektion=4
129128
//! [6]: https://man.openbsd.org/getentropy.2
@@ -142,8 +141,6 @@
142141
//! [19]: https://www.unix.com/man-page/mojave/2/getentropy/
143142
//! [20]: https://www.unix.com/man-page/mojave/4/random/
144143
//! [21]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable
145-
//! [22]: https://docs.microsoft.com/en-us/windows/uwp/
146-
//! [23]: https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
147144
148145
#![doc(
149146
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
@@ -204,8 +201,6 @@ cfg_if! {
204201
} else if #[cfg(target_os = "vxworks")] {
205202
mod util_libc;
206203
#[path = "vxworks.rs"] mod imp;
207-
} else if #[cfg(all(windows, target_vendor = "uwp"))] {
208-
#[path = "windows_uwp.rs"] mod imp;
209204
} else if #[cfg(windows)] {
210205
#[path = "windows.rs"] mod imp;
211206
} else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] {

src/windows.rs

+29-6
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,43 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
//! Implementation for Windows
9+
//! Implementation for Windows UWP targets. After deprecation of Windows XP
10+
//! and Vista, this can supersede the `RtlGenRandom`-based implementation.
1011
use crate::Error;
12+
use core::{ffi::c_void, num::NonZeroU32, ptr};
13+
14+
const BCRYPT_USE_SYSTEM_PREFERRED_RNG: u32 = 0x00000002;
1115

1216
extern "system" {
13-
#[link_name = "SystemFunction036"]
14-
fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: u32) -> u8;
17+
fn BCryptGenRandom(
18+
hAlgorithm: *mut c_void,
19+
pBuffer: *mut u8,
20+
cbBuffer: u32,
21+
dwFlags: u32,
22+
) -> u32;
1523
}
1624

1725
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
1826
// Prevent overflow of u32
1927
for chunk in dest.chunks_mut(u32::max_value() as usize) {
20-
let ret = unsafe { RtlGenRandom(chunk.as_mut_ptr(), chunk.len() as u32) };
21-
if ret == 0 {
22-
return Err(Error::WINDOWS_RTL_GEN_RANDOM);
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 the two highest bits for severity status.
37+
if ret >> 30 == 0b11 {
38+
// We zeroize the highest bit, so the error code will reside
39+
// inside the range designated for OS codes.
40+
let code = ret ^ (1 << 31);
41+
// SAFETY: the second highest bit is always equal to one,
42+
// so it's impossible to get zero. Unfortunately the type
43+
// system does not have a way to express this yet.
44+
let code = unsafe { NonZeroU32::new_unchecked(code) };
45+
return Err(Error::from(code));
2346
}
2447
}
2548
Ok(())

src/windows_uwp.rs

-49
This file was deleted.

0 commit comments

Comments
 (0)