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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::enclave::EnclaveRange;
use crate::error;
use core::cmp;
use core::fmt;
use core::mem;
use sgx_types::error::{SgxResult, SgxStatus};
pub fn rand(buf: &mut [u8]) -> SgxResult {
ensure!(
buf.is_enclave_range() || buf.is_host_range(),
SgxStatus::InvalidParameter
);
let mut left_len = buf.len();
let mut offset = 0_usize;
while left_len > 0 {
let rand_num = rand32()?.to_ne_bytes();
let copy_len = cmp::min(left_len, mem::size_of::<u32>());
buf[offset..offset + copy_len].copy_from_slice(&rand_num[..copy_len]);
left_len -= copy_len;
offset += copy_len;
}
Ok(())
}
#[cfg(feature = "sim")]
#[inline]
fn rand32() -> SgxResult<u32> {
if is_x86_feature_detected!("rdrand") {
rdrand()
} else {
Ok(rand_lcg())
}
}
#[cfg(not(feature = "sim"))]
#[inline]
fn rand32() -> SgxResult<u32> {
rdrand()
}
#[inline]
fn rdrand() -> SgxResult<u32> {
const RDRAND_RETRY_TIMES: usize = 10;
unsafe {
#[cfg(target_arch = "x86")]
use core::arch::x86::_rdrand32_step;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::_rdrand32_step;
let mut ret = 0_u32;
for _in in 0..RDRAND_RETRY_TIMES {
if _rdrand32_step(&mut ret) == 1 {
return Ok(ret);
}
}
Err(SgxStatus::Unexpected)
}
}
#[cfg(feature = "sim")]
fn rand_lcg() -> u32 {
use crate::inst::GlobalSim;
use crate::sync::SpinMutex;
static LOCK: SpinMutex<()> = SpinMutex::new(());
let _guard = LOCK.lock();
let seed = &mut GlobalSim::get_mut().seed;
let rand = unsafe {
6364136223846793005_u64
.unchecked_mul(*seed)
.unchecked_add(1)
};
*seed = rand;
(rand >> 32) as u32
}
#[inline]
pub fn getrandom(buf: &mut [u8]) {
if getrandom_fill_bytes(buf).is_err() {
error::abort();
}
fn getrandom_fill_bytes(buf: &mut [u8]) -> SgxResult {
rand(buf)
}
}
pub struct Rng;
impl Rng {
pub fn new() -> Rng {
Rng
}
pub fn next_u32(&mut self) -> u32 {
next_u32(&mut getrandom)
}
pub fn next_u64(&mut self) -> u64 {
next_u64(&mut getrandom)
}
pub fn next_usize(&mut self) -> usize {
next_usize(&mut getrandom)
}
pub fn fill_bytes(&mut self, buf: &mut [u8]) {
getrandom(buf)
}
}
impl fmt::Debug for Rng {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Rng {{}}")
}
}
impl Default for Rng {
fn default() -> Rng {
Rng::new()
}
}
fn next_u32(fill_buf: &mut dyn FnMut(&mut [u8])) -> u32 {
let mut buf = [0_u8; 4];
fill_buf(&mut buf);
unsafe { mem::transmute::<[u8; 4], u32>(buf) }
}
fn next_u64(fill_buf: &mut dyn FnMut(&mut [u8])) -> u64 {
let mut buf = [0_u8; 8];
fill_buf(&mut buf);
unsafe { mem::transmute::<[u8; 8], u64>(buf) }
}
fn next_usize(fill_buf: &mut dyn FnMut(&mut [u8])) -> usize {
let mut buf = [0_u8; mem::size_of::<usize>()];
fill_buf(&mut buf);
unsafe { mem::transmute::<[u8; mem::size_of::<usize>()], usize>(buf) }
}