| 1 |
//! Implementation for Windows 10 and later |
| 2 |
//! |
| 3 |
//! On Windows 10 and later, ProcessPrng "is the primary interface to the |
| 4 |
//! user-mode per-processor PRNGs" and only requires bcryptprimitives.dll, |
| 5 |
//! making it a better option than the other Windows RNG APIs: |
| 6 |
//! - BCryptGenRandom: https://learn.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom |
| 7 |
//! - Requires bcrypt.dll (which loads bcryptprimitives.dll anyway) |
| 8 |
//! - Can cause crashes/hangs as BCrypt accesses the Windows Registry: |
| 9 |
//! https://github.com/rust-lang/rust/issues/99341 |
| 10 |
//! - Causes issues inside sandboxed code: |
| 11 |
//! https://issues.chromium.org/issues/40277768 |
| 12 |
//! - CryptGenRandom: https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptgenrandom |
| 13 |
//! - Deprecated and not available on UWP targets |
| 14 |
//! - Requires advapi32.lib/advapi32.dll (in addition to bcryptprimitives.dll) |
| 15 |
//! - Thin wrapper around ProcessPrng |
| 16 |
//! - RtlGenRandom: https://learn.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom |
| 17 |
//! - Deprecated and not available on UWP targets |
| 18 |
//! - Requires advapi32.dll (in addition to bcryptprimitives.dll) |
| 19 |
//! - Requires using name "SystemFunction036" |
| 20 |
//! - Thin wrapper around ProcessPrng |
| 21 |
//! |
| 22 |
//! For more information see the Windows RNG Whitepaper: https://aka.ms/win10rng |
| 23 |
use crate::Error; |
| 24 |
use core::mem::MaybeUninit; |
| 25 |
|
| 26 |
pub use crate::util::{inner_u32, inner_u64}; |
| 27 |
|
| 28 |
// Binding to the Windows.Win32.Security.Cryptography.ProcessPrng API. As |
| 29 |
// bcryptprimitives.dll lacks an import library, we use "raw-dylib". |
| 30 |
#[cfg_attr( |
| 31 |
target_arch = "x86", |
| 32 |
link( |
| 33 |
name = "bcryptprimitives", |
| 34 |
kind = "raw-dylib", |
| 35 |
import_name_type = "undecorated" |
| 36 |
) |
| 37 |
)] |
| 38 |
#[cfg_attr( |
| 39 |
not(target_arch = "x86"), |
| 40 |
link(name = "bcryptprimitives", kind = "raw-dylib") |
| 41 |
)] |
| 42 |
unsafe extern "system" { |
| 43 |
fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> BOOL; |
| 44 |
} |
| 45 |
#[expect(clippy::upper_case_acronyms)] |
| 46 |
type BOOL = core::ffi::c_int; |
| 47 |
const TRUE: BOOL = 1; |
| 48 |
|
| 49 |
#[inline] |
| 50 |
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
| 51 |
let result = unsafe { ProcessPrng(dest.as_mut_ptr().cast::<u8>(), dest.len()) }; |
| 52 |
// On Windows 10 and later, `ProcessPrng` is documented to always return |
| 53 |
// TRUE. All potential errors are handled during loading of |
| 54 |
// `BCryptPrimitive.dll`. See the "Process base PRNG" section in the |
| 55 |
// aforementioned Windows RNG whitepaper for more information. |
| 56 |
// |
| 57 |
// The Zig project found that Windows 8 implements `ProcessPrng` in a way |
| 58 |
// that may fail and return a value other than `TRUE`. Although recent |
| 59 |
// versions of the Rust toolchain do not support Windows 8, we cannot rule |
| 60 |
// out this backend being used in an executable that will run on Windows 8 |
| 61 |
// (e.g. a fork of this crate backported to have an MSRV lower than 1.76, |
| 62 |
// or a fork of the Rust toolchain to support older Windows versions, or |
| 63 |
// other build hacks). |
| 64 |
// |
| 65 |
// Further, Wine's implementation of `ProcessPrng` CAN fail, in every |
| 66 |
// version through Wine 11.2, and this may be the case for any other Windows |
| 67 |
// emulation layers. |
| 68 |
if result == TRUE { |
| 69 |
Ok(()) |
| 70 |
} else { |
| 71 |
Err(Error::UNEXPECTED) |
| 72 |
} |
| 73 |
} |