1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use crate::{bindings, error::code::*, Error, Result};
pub fn getrandom(dest: &mut [u8]) -> Result {
let res = unsafe { bindings::wait_for_random_bytes() };
if res != 0 {
return Err(Error::from_kernel_errno(res));
}
unsafe {
bindings::get_random_bytes(dest.as_mut_ptr() as *mut core::ffi::c_void, dest.len());
}
Ok(())
}
pub fn getrandom_nonblock(dest: &mut [u8]) -> Result {
if !unsafe { bindings::rng_is_initialized() } {
return Err(EAGAIN);
}
getrandom(dest)
}
pub fn add_randomness(data: &[u8]) {
unsafe {
bindings::add_device_randomness(data.as_ptr() as *const core::ffi::c_void, data.len());
}
}