From abd38194dcdbd22fb94da3591873d5ebe3988b5d Mon Sep 17 00:00:00 2001 From: akashfortanix Date: Wed, 12 Dec 2018 14:07:20 +0530 Subject: [PATCH] Add support for x86_64-fortanix-unknown-sgx target --- Cargo.toml | 3 +++ src/rand.rs | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d192eb236a..5e50bb8966 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -286,6 +286,9 @@ libc = { version = "0.2.34" } [target.'cfg(any(target_os = "redox", all(unix, not(any(target_os = "macos", target_os = "ios")))))'.dependencies] lazy_static = "1.2" +[target.'cfg(target_env = "sgx")'.dependencies] +rdrand = "0.3.0" + # Keep this in sync with `[dependencies]` in pregenerate_asm/Cargo.toml. [build-dependencies] # we do not use the gcc parallel feature because we do the diff --git a/src/rand.rs b/src/rand.rs index 3cf6aece8a..2bcd34a401 100644 --- a/src/rand.rs +++ b/src/rand.rs @@ -93,7 +93,7 @@ impl SecureRandom for SystemRandom { impl private::Sealed for SystemRandom {} -#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "ios", windows)))] +#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "ios", windows, target_env = "sgx")))] use self::urandom::fill as fill_impl; #[cfg(any( @@ -107,6 +107,9 @@ use self::sysrand_or_urandom::fill as fill_impl; #[cfg(any(target_os = "macos", target_os = "ios"))] use self::darwin::fill as fill_impl; + +#[cfg(target_env = "sgx")] +use self::rdrandom::fill as fill_impl; use crate::private; #[cfg(target_os = "linux")] @@ -275,6 +278,24 @@ mod darwin { } } +#[cfg(target_env = "sgx")] +mod rdrandom { + use rdrand::RdRand; + use crate::error; + use crate::endian::LittleEndian; + + pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> { + let rng = RdRand::new().map_err(|_| error::Unspecified)?; + for dst_chunk in dest.chunks_mut(8) { + let src_num = rng.try_next_u64().ok_or(error::Unspecified)?; + let temp = LittleEndian::from(src_num); + let src_chunk = temp.as_ref(); + dst_chunk.copy_from_slice(&src_chunk[..dst_chunk.len()]); + } + Ok(()) + } +} + #[cfg(test)] mod tests { use crate::rand::{self, SecureRandom};