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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use super::*;
cfg_if::cfg_if! {
if #[cfg(feature = "getrandom")] {
/// Provides cryptographically secure entropy.
///
/// # Panics
///
/// If unable to provide secure entropy this method will panic.
///
/// # Implementation notes
///
/// The implementation is provided by the [`getrandom`](https://crates.io/crates/getrandom) crate.
#[inline]
pub fn getentropy<T: dataview::Pod>(buf: &mut [T]) -> &mut [T] {
let buf: &mut [MaybeUninit<T>] = unsafe { mem::transmute(buf) };
getentropy_uninit(buf)
}
/// Provides cryptographically secure entropy.
///
/// # Panics
///
/// If unable to provide secure entropy this method will panic.
///
/// # Implementation notes
///
/// The implementation is provided by the [`getrandom`](https://crates.io/crates/getrandom) crate.
#[inline]
pub fn getentropy_uninit<T: dataview::Pod>(buf: &mut [MaybeUninit<T>]) -> &mut [T] {
let dest = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr() as *mut MaybeUninit<u8>, mem::size_of_val(buf)) };
match getrandom::getrandom_uninit(dest) {
Ok(_) => unsafe { mem::transmute(buf) },
Err(_) => getentropy_not_ready(),
}
}
}
else {
extern "C" {
fn getentropy_raw(ptr: *mut u8, len: usize) -> bool;
}
/// Provides cryptographically secure entropy.
///
/// # Panics
///
/// If unable to provide secure entropy this method will panic.
///
/// # Implementation notes
///
/// The implementation is provided by linking against an extern function.
/// If `false` is returned then this function panics.
///
/// ```
/// extern "C" {
/// fn getentropy_raw(buffer_ptr: *mut u8, buffer_len: usize) -> bool;
/// }
/// ```
#[inline]
pub fn getentropy<T: dataview::Pod>(buf: &mut [T]) -> &mut [T] {
let buf: &mut [MaybeUninit<T>] = unsafe { mem::transmute(buf) };
getentropy_uninit(buf)
}
/// Provides cryptographically secure entropy.
///
/// # Panics
///
/// If unable to provide secure entropy this method will panic.
///
/// # Implementation notes
///
/// The implementation is provided by linking against an extern function.
/// If `false` is returned then this function panics.
///
/// ```
/// extern "C" {
/// fn getentropy_raw(buffer_ptr: *mut u8, buffer_len: usize) -> bool;
/// }
/// ```
#[inline]
pub fn getentropy_uninit<T: dataview::Pod>(buf: &mut [MaybeUninit<T>]) -> &mut [T] {
if buf.len() > 0 {
if !unsafe { getentropy_raw(buf.as_mut_ptr() as *mut u8, mem::size_of_val(buf)) } {
getentropy_not_ready()
}
}
unsafe { mem::transmute(buf) }
}
}
}
#[cold]
fn getentropy_not_ready() -> ! {
panic!("getentropy not ready")
}