Skip to content

Commit

Permalink
Back to raw-dylib
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Richey <[email protected]>
  • Loading branch information
josephlr committed May 1, 2024
1 parent 08f2741 commit 1d62b41
Showing 1 changed file with 49 additions and 33 deletions.
82 changes: 49 additions & 33 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -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<c_void> = AtomicPtr::new(ptr::null_mut());
// static PROCESS_PRNG_PTR: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());

fn get_process_prng() -> Result<ProcessPrng, Error> {
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<ProcessPrng, Error> {
// 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<u8>]) -> 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),
}
Expand Down

0 comments on commit 1d62b41

Please sign in to comment.