-
Notifications
You must be signed in to change notification settings - Fork 237
Use ProcessPrng on Windows #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6766018
Add Window7-specific implementation
josephlr 9fa83b5
Add Windows 7 test
josephlr 22635a8
Add ProcessPrng implementation
josephlr 0ba324e
Increase MSRV to 1.56 and update docs
josephlr c231147
Update documentation and don't use newtype for BOOL/BOOLEAN
josephlr 796321e
Pin Nightly for Win7
josephlr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,60 +1,38 @@ | ||
| //! Implementation for Windows | ||
| //! Implementation for Windows 10 and later | ||
| //! | ||
| //! On Windows 10 and later, ProcessPrng "is the primary interface to the | ||
| //! user-mode per-processer PRNGs" and only requires bcryptprimitives.dll, | ||
| //! making it a better option than the other Windows RNG APIs: | ||
| //! - BCryptGenRandom: https://learn.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom | ||
| //! - Requires bcrypt.dll (which loads bcryptprimitives.dll anyway) | ||
| //! - Can cause crashes/hangs as BCrypt accesses the Windows Registry: | ||
| //! https://github.com/rust-lang/rust/issues/99341 | ||
| //! - Causes issues inside sandboxed code: | ||
| //! https://issues.chromium.org/issues/40277768 | ||
| //! - CryptGenRandom: https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptgenrandom | ||
| //! - Deprecated and not available on UWP targets | ||
| //! - Requires advapi32.lib/advapi32.dll (in addition to bcryptprimitives.dll) | ||
| //! - Thin wrapper around ProcessPrng | ||
| //! - RtlGenRandom: https://learn.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom | ||
| //! - Deprecated and not available on UWP targets | ||
| //! - Requires advapi32.dll (in addition to bcryptprimitives.dll) | ||
| //! - Requires using name "SystemFunction036" | ||
| //! - Thin wrapper around ProcessPrng | ||
| //! For more information see the Windows RNG Whitepaper: https://aka.ms/win10rng | ||
| use crate::Error; | ||
| use core::{ffi::c_void, mem::MaybeUninit, num::NonZeroU32, ptr}; | ||
| use core::mem::MaybeUninit; | ||
|
|
||
| const BCRYPT_USE_SYSTEM_PREFERRED_RNG: u32 = 0x00000002; | ||
|
|
||
| #[link(name = "bcrypt")] | ||
| extern "system" { | ||
| fn BCryptGenRandom( | ||
| hAlgorithm: *mut c_void, | ||
| pBuffer: *mut u8, | ||
| cbBuffer: u32, | ||
| dwFlags: u32, | ||
| ) -> u32; | ||
| } | ||
|
|
||
| // 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; | ||
| } | ||
| // Binding to the Windows.Win32.Security.Cryptography.ProcessPrng API. As | ||
| // bcryptprimitives.dll lacks an import library, we use the windows-targets | ||
| // crate to link to it. | ||
| windows_targets::link!("bcryptprimitives.dll" "system" fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> BOOL); | ||
| pub type BOOL = i32; | ||
| pub const TRUE: BOOL = 1i32; | ||
|
|
||
| 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().cast::<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().cast::<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); | ||
| // SAFETY: the second highest bit is always equal to one, | ||
| // so it's impossible to get zero. Unfortunately the type | ||
| // system does not have a way to express this yet. | ||
| let code = unsafe { NonZeroU32::new_unchecked(code) }; | ||
| return Err(Error::from(code)); | ||
| } | ||
| // ProcessPrng should always return TRUE, but we check just in case. | ||
| match unsafe { ProcessPrng(dest.as_mut_ptr().cast::<u8>(), dest.len()) } { | ||
| TRUE => Ok(()), | ||
| _ => Err(Error::WINDOWS_PROCESS_PRNG), | ||
josephlr marked this conversation as resolved.
Show resolved
Hide resolved
josephlr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| //! Legacy implementation for Windows XP and later | ||
| //! | ||
| //! For targets where we cannot use ProcessPrng (added in Windows 10), we use | ||
| //! RtlGenRandom. See windows.rs for a more detailed discussion of the Windows | ||
| //! RNG APIs (and why we don't use BCryptGenRandom). On versions prior to | ||
| //! Windows 10, this implementation is secure. On Windows 10 and later, this | ||
| //! implementation behaves identically to the windows.rs implementation, except | ||
| //! that it forces the loading of an additonal DLL (advapi32.dll). | ||
| //! | ||
| //! This implementation will not work on UWP targets (which lack advapi32.dll), | ||
| //! but such targets require Windows 10, so can use the standard implementation. | ||
| use crate::Error; | ||
| use core::{ffi::c_void, mem::MaybeUninit}; | ||
|
|
||
| // Binding to the Windows.Win32.Security.Authentication.Identity.RtlGenRandom | ||
| // API. Don't use windows-targets as it doesn't support Windows 7 targets. | ||
| #[link(name = "advapi32")] | ||
| extern "system" { | ||
| #[link_name = "SystemFunction036"] | ||
| fn RtlGenRandom(randombuffer: *mut c_void, randombufferlength: u32) -> BOOLEAN; | ||
| } | ||
| type BOOLEAN = u8; | ||
| const TRUE: BOOLEAN = 1u8; | ||
|
|
||
| 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) { | ||
| let ret = unsafe { RtlGenRandom(chunk.as_mut_ptr().cast::<c_void>(), chunk.len() as u32) }; | ||
| if ret != TRUE { | ||
| return Err(Error::WINDOWS_RTL_GEN_RANDOM); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.