Skip to content

Commit 65cc287

Browse files
committed
Auto merge of #3309 - ChrisDenton:windows-rand, r=RalfJung
Test windows random shims This adds tests for `BCryptGenRandom` and `RtlGenRandom` (aka `SystemFunction036`). Note that `BCryptGenRandom` comes in two flavours: ```rust BCryptGenRandom(null_mut(), key.as_mut_ptr(), len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) BCryptGenRandom(BCRYPT_RNG_ALG_HANDLE, key.as_mut_ptr(), len, 0) ``` Fixes #3308
2 parents 0da272c + db7fb0c commit 65cc287

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/pass/shims/windows-rand.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//@only-target-windows: this directly tests windows only random functions
2+
use core::ffi::c_void;
3+
use core::mem::size_of_val;
4+
use core::ptr::null_mut;
5+
6+
// Windows API definitions.
7+
type NTSTATUS = i32;
8+
type BOOLEAN = u8;
9+
const BCRYPT_USE_SYSTEM_PREFERRED_RNG: u32 = 0x00000002;
10+
const BCRYPT_RNG_ALG_HANDLE: *mut c_void = 0x81 as *mut c_void;
11+
#[link(name = "bcrypt")]
12+
extern "system" {
13+
fn BCryptGenRandom(
14+
halgorithm: *mut c_void,
15+
pbbuffer: *mut u8,
16+
cbbuffer: u32,
17+
dwflags: u32,
18+
) -> NTSTATUS;
19+
}
20+
#[link(name = "advapi32")]
21+
extern "system" {
22+
#[link_name = "SystemFunction036"]
23+
fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: u32) -> BOOLEAN;
24+
}
25+
26+
fn main() {
27+
let mut key = [0u8; 24];
28+
let len: u32 = size_of_val(&key).try_into().unwrap();
29+
let ret = unsafe {
30+
BCryptGenRandom(null_mut(), key.as_mut_ptr(), len, BCRYPT_USE_SYSTEM_PREFERRED_RNG)
31+
};
32+
// NTSTATUS codes use the high bit to indicate an error
33+
assert!(ret >= 0);
34+
35+
let ret = unsafe { BCryptGenRandom(BCRYPT_RNG_ALG_HANDLE, key.as_mut_ptr(), len, 0) };
36+
assert!(ret >= 0);
37+
38+
let ret = unsafe { RtlGenRandom(key.as_mut_ptr(), len) };
39+
// RtlGenRandom returns a BOOLEAN where 0 indicates an error
40+
assert_ne!(ret, 0);
41+
}

0 commit comments

Comments
 (0)