From 1d62b414b7fc7686a441a894e55d2d5f96ad4d9d Mon Sep 17 00:00:00 2001 From: Joe Richey Date: Tue, 30 Apr 2024 20:28:58 -0700 Subject: [PATCH] Back to raw-dylib Signed-off-by: Joe Richey --- src/windows.rs | 82 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 33 deletions(-) diff --git a/src/windows.rs b/src/windows.rs index 03c1aadb..9eb8a8b6 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,48 +1,64 @@ //! Implementation for Windows use crate::Error; -use core::{ - ffi::c_void, - mem, - mem::MaybeUninit, - ptr, - sync::atomic::{fence, AtomicPtr, Ordering}, -}; +use core::mem::MaybeUninit; +// use core::{ +// ffi::c_void, +// mem, +// ptr, +// sync::atomic::{fence, AtomicPtr, Ordering}, +// }; -type HMODULE = isize; type BOOL = i32; const TRUE: BOOL = 1; +// type HMODULE = isize; -#[link(name = "kernel32")] -extern "system" { - fn LoadLibraryA(libfilename: *const u8) -> HMODULE; - fn GetProcAddress(hmodule: HMODULE, procname: *const u8) -> *mut c_void; -} +// #[link(name = "kernel32")] +// extern "system" { +// fn LoadLibraryA(libfilename: *const u8) -> HMODULE; +// fn GetProcAddress(hmodule: HMODULE, procname: *const u8) -> *mut c_void; +// } -type ProcessPrng = unsafe extern "system" fn(*mut u8, usize) -> BOOL; +// type ProcessPrng = unsafe extern "system" fn(*mut u8, usize) -> BOOL; -static PROCESS_PRNG_PTR: AtomicPtr = AtomicPtr::new(ptr::null_mut()); +// 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); - } - Ok(unsafe { mem::transmute(p) }) +// 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); +// } +// Ok(unsafe { mem::transmute(p) }) +// } + +#[cfg_attr( + target_arch = "x86", + link( + name = "bcryptprimitives", + kind = "raw-dylib", + import_name_type = "undecorated" + ) +)] +#[cfg_attr( + not(target_arch = "x86"), + link(name = "bcryptprimitives", kind = "raw-dylib") +)] +extern "system" { + fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> BOOL; } 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()) } { + // let process_prng = get_process_prng()?; + match unsafe { ProcessPrng(dest.as_mut_ptr() as *mut u8, dest.len()) } { TRUE => Ok(()), _ => Err(Error::WINDOWS_PROCESS_PRNG), }