Skip to content
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

RtlGenRandom on Windows uses the wrong ABI #52

Open
cakarsubasi opened this issue Sep 3, 2024 · 0 comments
Open

RtlGenRandom on Windows uses the wrong ABI #52

cakarsubasi opened this issue Sep 3, 2024 · 0 comments

Comments

@cakarsubasi
Copy link

I ran into this issue while while running rdst with miri. The RtlGenRandom function has the following definition:

BOOLEAN RtlGenRandom(
  [out] PVOID RandomBuffer,
  [in]  ULONG RandomBufferLength
);

Which was translated as follows:

fn RtlGenRandom(pBuffer: *mut u8, cbBuffer: usize) -> u32;

However, an ULONG on Windows is 4 bytes long, not pointer sized, and a BOOLEAN (not to be confused with a BOOL) is 1 byte long.

So, the updated signature plus the entropy function in the file should be like this:

use core::ffi::{c_void, c_ulong, c_char};

extern "system" {
	#[link_name = "SystemFunction036"]
	fn RtlGenRandom(pBuffer: *mut c_void, cbBuffer: c_ulong) -> c_char;
}

/// Obtain a random 64-bit number using WinAPI's `RtlGenRandom` function.
pub fn entropy(out: &mut [u8]) -> bool {
	unsafe { RtlGenRandom(out.as_mut_ptr() as *mut c_void, out.len() as u32) == 0 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant