Skip to content

Commit b073ac6

Browse files
Remove error branch of ProcessPrng
associated cleanup and documentation
1 parent 9fb4a9a commit b073ac6

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/backends/windows.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Implementation for Windows 10 and later
22
//!
33
//! 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,
55
//! making it a better option than the other Windows RNG APIs:
66
//! - BCryptGenRandom: https://learn.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
77
//! - Requires bcrypt.dll (which loads bcryptprimitives.dll anyway)
@@ -27,21 +27,21 @@ pub use crate::util::{inner_u32, inner_u64};
2727

2828
// Binding to the Windows.Win32.Security.Cryptography.ProcessPrng API. As
2929
// 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);
4238
}
4339

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(())
4747
}

src/error.rs

-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ impl Error {
114114
target_os = "tvos",
115115
))]
116116
Error::IOS_RANDOM_GEN => "SecRandomCopyBytes: iOS Security framework failure",
117-
#[cfg(all(windows, not(target_vendor = "win7")))]
118-
Error::WINDOWS_PROCESS_PRNG => "ProcessPrng: Windows system function failure",
119117
#[cfg(all(windows, target_vendor = "win7"))]
120118
Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure",
121119
#[cfg(getrandom_backend = "wasm_js")]

0 commit comments

Comments
 (0)