@@ -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.)
@@ -95,8 +97,7 @@ impl sealed::Sealed for SystemRandom {}
95
97
96
98
#[ cfg( all(
97
99
feature = "use_heap" ,
98
- not( any( target_os = "linux" , target_os = "macos" , target_os = "ios" , windows) )
99
- ) ) ]
100
+ not( any( target_os = "linux" , target_os = "macos" , target_os = "ios" , windows, target_env = "sgx" ) ) ) ) ]
100
101
use self :: urandom:: fill as fill_impl;
101
102
102
103
#[ cfg( any(
@@ -110,6 +111,10 @@ use self::sysrand_or_urandom::fill as fill_impl;
110
111
111
112
#[ cfg( any( target_os = "macos" , target_os = "ios" ) ) ]
112
113
use self :: darwin:: fill as fill_impl;
114
+
115
+ #[ cfg( target_env = "sgx" ) ]
116
+ use self :: rdrandom:: fill as fill_impl;
117
+
113
118
use crate :: sealed;
114
119
115
120
#[ cfg( target_os = "linux" ) ]
@@ -279,6 +284,19 @@ mod darwin {
279
284
}
280
285
}
281
286
287
+ #[ cfg( target_env = "sgx" ) ]
288
+ mod rdrandom {
289
+ use crate :: error;
290
+ use rdrand:: RdRand ;
291
+ use rand_core:: RngCore ;
292
+
293
+ pub fn fill ( dest : & mut [ u8 ] ) -> Result < ( ) , error:: Unspecified > {
294
+ let mut rng = RdRand :: new ( ) . map_err ( |_| error:: Unspecified ) ?;
295
+ rng. try_fill_bytes ( dest) . map_err ( |_| error:: Unspecified ) ?;
296
+ Ok ( ( ) )
297
+ }
298
+ }
299
+
282
300
#[ cfg( test) ]
283
301
mod tests {
284
302
use crate :: rand:: { self , SecureRandom } ;
0 commit comments