|
1 |
| -//! Implementation for Windows |
| 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-processer 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 | +//! For more information see the Windows RNG Whitepaper: https://aka.ms/win10rng |
2 | 22 | use crate::Error;
|
3 |
| -use core::{ffi::c_void, mem::MaybeUninit, num::NonZeroU32, ptr}; |
| 23 | +use core::mem::MaybeUninit; |
4 | 24 |
|
5 |
| -const BCRYPT_USE_SYSTEM_PREFERRED_RNG: u32 = 0x00000002; |
6 |
| - |
7 |
| -#[link(name = "bcrypt")] |
8 |
| -extern "system" { |
9 |
| - fn BCryptGenRandom( |
10 |
| - hAlgorithm: *mut c_void, |
11 |
| - pBuffer: *mut u8, |
12 |
| - cbBuffer: u32, |
13 |
| - dwFlags: u32, |
14 |
| - ) -> u32; |
15 |
| -} |
16 |
| - |
17 |
| -// Forbidden when targetting UWP |
18 |
| -#[cfg(not(target_vendor = "uwp"))] |
19 |
| -#[link(name = "advapi32")] |
20 |
| -extern "system" { |
21 |
| - #[link_name = "SystemFunction036"] |
22 |
| - fn RtlGenRandom(RandomBuffer: *mut c_void, RandomBufferLength: u32) -> u8; |
23 |
| -} |
| 25 | +// Binding to the Windows.Win32.Security.Cryptography.ProcessPrng API. As |
| 26 | +// bcryptprimitives.dll lacks an import library, we use the windows-targets |
| 27 | +// crate to link to it. |
| 28 | +windows_targets::link!("bcryptprimitives.dll" "system" fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> BOOL); |
| 29 | +pub type BOOL = i32; |
| 30 | +pub const TRUE: BOOL = 1i32; |
24 | 31 |
|
25 | 32 | pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
26 |
| - // Prevent overflow of u32 |
27 |
| - for chunk in dest.chunks_mut(u32::max_value() as usize) { |
28 |
| - // BCryptGenRandom was introduced in Windows Vista |
29 |
| - let ret = unsafe { |
30 |
| - BCryptGenRandom( |
31 |
| - ptr::null_mut(), |
32 |
| - chunk.as_mut_ptr().cast::<u8>(), |
33 |
| - chunk.len() as u32, |
34 |
| - BCRYPT_USE_SYSTEM_PREFERRED_RNG, |
35 |
| - ) |
36 |
| - }; |
37 |
| - // NTSTATUS codes use the two highest bits for severity status. |
38 |
| - if ret >> 30 == 0b11 { |
39 |
| - // Failed. Try RtlGenRandom as a fallback. |
40 |
| - #[cfg(not(target_vendor = "uwp"))] |
41 |
| - { |
42 |
| - let ret = unsafe { |
43 |
| - RtlGenRandom(chunk.as_mut_ptr().cast::<c_void>(), chunk.len() as u32) |
44 |
| - }; |
45 |
| - if ret != 0 { |
46 |
| - continue; |
47 |
| - } |
48 |
| - } |
49 |
| - // We zeroize the highest bit, so the error code will reside |
50 |
| - // inside the range designated for OS codes. |
51 |
| - let code = ret ^ (1 << 31); |
52 |
| - // SAFETY: the second highest bit is always equal to one, |
53 |
| - // so it's impossible to get zero. Unfortunately the type |
54 |
| - // system does not have a way to express this yet. |
55 |
| - let code = unsafe { NonZeroU32::new_unchecked(code) }; |
56 |
| - return Err(Error::from(code)); |
57 |
| - } |
| 33 | + // ProcessPrng should always return TRUE, but we check just in case. |
| 34 | + match unsafe { ProcessPrng(dest.as_mut_ptr().cast::<u8>(), dest.len()) } { |
| 35 | + TRUE => Ok(()), |
| 36 | + _ => Err(Error::WINDOWS_PROCESS_PRNG), |
58 | 37 | }
|
59 |
| - Ok(()) |
60 | 38 | }
|
0 commit comments