diff --git a/src/windows.rs b/src/windows.rs index 072fe6e3..03c1aadb 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,33 +1,47 @@ //! Implementation for Windows use crate::Error; -use core::mem::MaybeUninit; - -extern crate std; -use std::println; +use core::{ + ffi::c_void, + mem, + mem::MaybeUninit, + ptr, + sync::atomic::{fence, AtomicPtr, Ordering}, +}; type HMODULE = isize; type BOOL = i32; const TRUE: BOOL = 1; -type ProcessPrng = unsafe extern "system" fn(*mut u8, usize) -> BOOL; - #[link(name = "kernel32")] extern "system" { fn LoadLibraryA(libfilename: *const u8) -> HMODULE; - fn GetProcAddress(hmodule: HMODULE, procname: *const u8) -> Option; + fn GetProcAddress(hmodule: HMODULE, procname: *const u8) -> *mut c_void; } -pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { - let dll = unsafe { LoadLibraryA(b"bcryptprimitives.dll\0".as_ptr()) }; - println!("Got DLL: {}", dll); - if dll == 0 { - return Err(Error::WINDOWS_LOAD_DLL); +type ProcessPrng = unsafe extern "system" fn(*mut u8, usize) -> BOOL; + +static PROCESS_PRNG_PTR: AtomicPtr = AtomicPtr::new(ptr::null_mut()); + +fn get_process_prng() -> Result { + let mut p = PROCESS_PRNG_PTR.load(Ordering::Relaxed); + if p.is_null() { + let dll = unsafe { LoadLibraryA(b"bcryptprimitives.dll\0".as_ptr()) }; + if dll == 0 { + return Err(Error::WINDOWS_LOAD_DLL); + } + p = unsafe { GetProcAddress(dll, b"ProcessPrng\0".as_ptr()) }; + if p.is_null() { + return Err(Error::WINDOWS_LOAD_DLL); + } + PROCESS_PRNG_PTR.store(p, Ordering::Release); + } else { + fence(Ordering::Acquire); } - let process_prng = match unsafe { GetProcAddress(dll, b"ProcessPrng\0".as_ptr()) } { - Some(p) => p, - None => return Err(Error::WINDOWS_LOAD_DLL), - }; - println!("Got ProcessPrng: {:p}", process_prng); + Ok(unsafe { mem::transmute(p) }) +} + +pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { + let process_prng = get_process_prng()?; match unsafe { process_prng(dest.as_mut_ptr() as *mut u8, dest.len()) } { TRUE => Ok(()), _ => Err(Error::WINDOWS_PROCESS_PRNG),