@@ -60,6 +60,8 @@ pub trait SecureRandom: sealed::Sealed {
60
60
/// On Windows, `fill` is implemented using the platform's API for secure
61
61
/// random number generation.
62
62
///
63
+ /// On SGX, `fill()` is implemented using the `RDRAND` instruction.
64
+ ///
63
65
/// Otherwise, `fill()` is implemented by reading from `/dev/urandom`. (This is
64
66
/// something that should be improved for any platform that adds something
65
67
/// better.)
@@ -93,7 +95,7 @@ impl SecureRandom for SystemRandom {
93
95
94
96
impl sealed:: Sealed for SystemRandom { }
95
97
96
- #[ cfg( not( any( target_os = "linux" , target_os = "macos" , target_os = "ios" , windows) ) ) ]
98
+ #[ cfg( not( any( target_os = "linux" , target_os = "macos" , target_os = "ios" , windows, target_env = "sgx" ) ) ) ]
97
99
use self :: urandom:: fill as fill_impl;
98
100
99
101
#[ cfg( any(
@@ -107,6 +109,10 @@ use self::sysrand_or_urandom::fill as fill_impl;
107
109
108
110
#[ cfg( any( target_os = "macos" , target_os = "ios" ) ) ]
109
111
use self :: darwin:: fill as fill_impl;
112
+
113
+ #[ cfg( target_env = "sgx" ) ]
114
+ use self :: rdrandom:: fill as fill_impl;
115
+
110
116
use crate :: sealed;
111
117
112
118
#[ cfg( target_os = "linux" ) ]
@@ -275,6 +281,19 @@ mod darwin {
275
281
}
276
282
}
277
283
284
+ #[ cfg( target_env = "sgx" ) ]
285
+ mod rdrandom {
286
+ use crate :: error;
287
+ use rdrand:: RdRand ;
288
+ use rand_core:: RngCore ;
289
+
290
+ pub fn fill ( dest : & mut [ u8 ] ) -> Result < ( ) , error:: Unspecified > {
291
+ let mut rng = RdRand :: new ( ) . map_err ( |_| error:: Unspecified ) ?;
292
+ rng. try_fill_bytes ( dest) . map_err ( |_| error:: Unspecified ) ?;
293
+ Ok ( ( ) )
294
+ }
295
+ }
296
+
278
297
#[ cfg( test) ]
279
298
mod tests {
280
299
use crate :: rand:: { self , SecureRandom } ;
0 commit comments