diff --git a/Cargo.toml b/Cargo.toml index 7b18500770c..1aea45fe7cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,3 +75,6 @@ autocfg = "0.1" [package.metadata.docs.rs] all-features = true + +[patch.crates-io] +rand_core = { path = "rand_core", version = "0.3", default-features = false } diff --git a/rand_os/Cargo.toml b/rand_os/Cargo.toml index 393230ec2a8..092b086ea1c 100644 --- a/rand_os/Cargo.toml +++ b/rand_os/Cargo.toml @@ -34,3 +34,6 @@ fuchsia-zircon = "0.3.2" [target.wasm32-unknown-unknown.dependencies] wasm-bindgen = { version = "0.2.12", optional = true } stdweb = { version = "0.4", optional = true } + +[target.'cfg(target_env = "sgx")'.dependencies] +rdrand = "0.4.0" \ No newline at end of file diff --git a/rand_os/src/lib.rs b/rand_os/src/lib.rs index 736fb5c6d46..1c7ccea71ef 100644 --- a/rand_os/src/lib.rs +++ b/rand_os/src/lib.rs @@ -142,6 +142,8 @@ extern crate wasm_bindgen; feature = "stdweb"))] #[macro_use] extern crate stdweb; +#[cfg(target_env = "sgx")] +extern crate rdrand; #[cfg(not(feature = "log"))] #[macro_use] @@ -310,6 +312,7 @@ mod_use!(cfg(target_os = "openbsd"), openbsd_bitrig); mod_use!(cfg(target_os = "redox"), redox); mod_use!(cfg(target_os = "solaris"), solaris); mod_use!(cfg(windows), windows); +mod_use!(cfg(target_env = "sgx"), sgx); mod_use!( cfg(all( @@ -356,5 +359,6 @@ compile_error!("enable either wasm_bindgen or stdweb feature"); target_os = "solaris", windows, target_arch = "wasm32", + target_env = "sgx" )))] compile_error!("OS RNG support is not available for this platform"); diff --git a/rand_os/src/sgx.rs b/rand_os/src/sgx.rs new file mode 100644 index 00000000000..58da5b03837 --- /dev/null +++ b/rand_os/src/sgx.rs @@ -0,0 +1,30 @@ +use super::OsRngImpl; +use Error; +use rdrand::RdRand; +use rand_core::RngCore; +use std::fmt::{Debug, Formatter, Result as FmtResult}; + +#[derive(Clone)] +pub struct OsRng{ + gen: RdRand +} + +impl OsRngImpl for OsRng { + fn new() -> Result { + let rng = RdRand::new()?; + Ok(OsRng{ gen: rng }) + } + + fn fill_chunk(&mut self, dest: &mut [u8]) -> Result<(), Error> { + self.gen.try_fill_bytes(dest) + } + + fn method_str(&self) -> &'static str { "RDRAND" } +} + +impl Debug for OsRng { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + f.debug_struct("OsRng") + .finish() + } +}