1
1
//! Implementation for Windows 10 and later
2
2
//!
3
3
//! On Windows 10 and later, ProcessPrng "is the primary interface to the
4
- //! user-mode per-processer PRNGs" and only requires bcryptprimitives.dll,
4
+ //! user-mode per-processor PRNGs" and only requires bcryptprimitives.dll,
5
5
//! making it a better option than the other Windows RNG APIs:
6
6
//! - BCryptGenRandom: https://learn.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
7
7
//! - Requires bcrypt.dll (which loads bcryptprimitives.dll anyway)
@@ -27,21 +27,21 @@ pub use crate::util::{inner_u32, inner_u64};
27
27
28
28
// Binding to the Windows.Win32.Security.Cryptography.ProcessPrng API. As
29
29
// bcryptprimitives.dll lacks an import library, we use the windows-targets
30
- // crate to link to it.
31
- windows_targets:: link!( "bcryptprimitives.dll" "system" fn ProcessPrng ( pbdata: * mut u8 , cbdata: usize ) -> BOOL ) ;
32
- #[ allow( clippy:: upper_case_acronyms) ]
33
- pub type BOOL = i32 ;
34
- pub const TRUE : BOOL = 1i32 ;
35
-
36
- pub fn fill_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
37
- // ProcessPrng should always return TRUE, but we check just in case.
38
- match unsafe { ProcessPrng ( dest. as_mut_ptr ( ) . cast :: < u8 > ( ) , dest. len ( ) ) } {
39
- TRUE => Ok ( ( ) ) ,
40
- _ => Err ( Error :: WINDOWS_PROCESS_PRNG ) ,
41
- }
30
+ // crate to link to it. The link! macro always declares external functions
31
+ // as pub, so we wrap it in a private module to keep it local.
32
+ //
33
+ // TODO(MSRV 1.71): Migrate to linking as raw-dylib directly.
34
+ // https://github.com/joboet/rust/blob/5c1c72572479afe98734d5f78fa862abe662c41a/library/std/src/sys/pal/windows/c.rs#L119
35
+ // https://github.com/microsoft/windows-rs/blob/0.60.0/crates/libs/targets/src/lib.rs
36
+ mod internal {
37
+ windows_targets:: link!( "bcryptprimitives.dll" "system" fn ProcessPrng ( pbdata: * mut u8 , cbdata: usize ) -> i32 ) ;
42
38
}
43
39
44
- impl Error {
45
- /// Calling Windows ProcessPrng failed.
46
- pub ( crate ) const WINDOWS_PROCESS_PRNG : Error = Self :: new_internal ( 10 ) ;
40
+ pub fn fill_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
41
+ // Since Windows 10, calls to the user-mode RNG are guaranteed
42
+ // to never fail during runtime (rare windows W).
43
+ // See the bottom of page 6 of the aforementioned Windows RNG
44
+ // whitepaper for more information.
45
+ unsafe { internal:: ProcessPrng ( dest. as_mut_ptr ( ) . cast :: < u8 > ( ) , dest. len ( ) ) } ;
46
+ Ok ( ( ) )
47
47
}
0 commit comments