From 727c70f4f1d45c8038f7ea2ac974facde71927fb Mon Sep 17 00:00:00 2001 From: akashfortanix Date: Mon, 24 Dec 2018 13:35:11 +0530 Subject: [PATCH] support sgx-target --- Cargo.toml | 3 +++ rand_os/Cargo.toml | 3 +++ rand_os/src/lib.rs | 4 ++++ rand_os/src/sgx.rs | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 rand_os/src/sgx.rs 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..6a328917bde 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" 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..43ae0ef79f1 --- /dev/null +++ b/rand_os/src/sgx.rs @@ -0,0 +1,38 @@ +// Copyright 2018 Developers of the Rand project. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +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() + } +}