aboutsummaryrefslogtreecommitdiff
path: root/src/windows.rs
diff options
context:
space:
mode:
authorJeff Vander Stoep <jeffv@google.com>2024-02-02 10:38:02 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2024-02-02 10:38:02 +0000
commitde0ba12639dc1a10a5e339d20e9845afd28d6157 (patch)
tree1c4feccd1f48b299a4c2ff42904596640a8cb629 /src/windows.rs
parent60d419ebd502ebc5fe0e73cba13d09daaa5e3ed4 (diff)
parent1872b3e1228910bdcbfd12744c5c6039dbcc9765 (diff)
downloadgetrandom-de0ba12639dc1a10a5e339d20e9845afd28d6157.tar.gz
Upgrade getrandom to 0.2.12 am: 1872b3e122emu-34-2-dev
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/getrandom/+/2944786 Change-Id: I315dfe2e9f343a483e0a525f8f29e2a53fc9e8f7 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
Diffstat (limited to 'src/windows.rs')
-rw-r--r--src/windows.rs32
1 files changed, 21 insertions, 11 deletions
diff --git a/src/windows.rs b/src/windows.rs
index 41dc37a..2d1c483 100644
--- a/src/windows.rs
+++ b/src/windows.rs
@@ -1,13 +1,6 @@
-// Copyright 2018 Developers of the Rand project.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
+//! Implementation for Windows
use crate::Error;
-use core::{ffi::c_void, num::NonZeroU32, ptr};
+use core::{ffi::c_void, mem::MaybeUninit, num::NonZeroU32, ptr};
const BCRYPT_USE_SYSTEM_PREFERRED_RNG: u32 = 0x00000002;
@@ -21,20 +14,37 @@ extern "system" {
) -> u32;
}
-pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
+// Forbidden when targetting UWP
+#[cfg(not(target_vendor = "uwp"))]
+#[link(name = "advapi32")]
+extern "system" {
+ #[link_name = "SystemFunction036"]
+ fn RtlGenRandom(RandomBuffer: *mut c_void, RandomBufferLength: u32) -> u8;
+}
+
+pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
// Prevent overflow of u32
for chunk in dest.chunks_mut(u32::max_value() as usize) {
// BCryptGenRandom was introduced in Windows Vista
let ret = unsafe {
BCryptGenRandom(
ptr::null_mut(),
- chunk.as_mut_ptr(),
+ chunk.as_mut_ptr() as *mut u8,
chunk.len() as u32,
BCRYPT_USE_SYSTEM_PREFERRED_RNG,
)
};
// NTSTATUS codes use the two highest bits for severity status.
if ret >> 30 == 0b11 {
+ // Failed. Try RtlGenRandom as a fallback.
+ #[cfg(not(target_vendor = "uwp"))]
+ {
+ let ret =
+ unsafe { RtlGenRandom(chunk.as_mut_ptr() as *mut c_void, chunk.len() as u32) };
+ if ret != 0 {
+ continue;
+ }
+ }
// We zeroize the highest bit, so the error code will reside
// inside the range designated for OS codes.
let code = ret ^ (1 << 31);