Skip to content

Commit aa99d95

Browse files
committed
Test windows random shims
1 parent 99df708 commit aa99d95

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
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+
typeNTSTATUS = i32;
8+
typeBOOLEAN = u8;
9+
constBCRYPT_USE_SYSTEM_PREFERRED_RNG:u32 = 0x00000002;
10+
constBCRYPT_RNG_ALG_HANDLE:*mutc_void = 0x81as*mutc_void;
11+
#[link(name = "bcrypt")]
12+
extern"system"{
13+
fnBCryptGenRandom(
14+
halgorithm:*mutc_void,
15+
pbbuffer:*mutu8,
16+
cbbuffer:u32,
17+
dwflags:u32,
18+
) -> NTSTATUS;
19+
}
20+
#[link(name = "advapi32")]
21+
extern"system"{
22+
#[link_name = "SystemFunction036"]
23+
fnRtlGenRandom(RandomBuffer:*mutu8,RandomBufferLength:u32) -> BOOLEAN;
24+
}
25+
26+
fnmain(){
27+
letmut 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)