Skip to content

Commit

Permalink
Add support for x86_64-fortanix-unknown-sgx target
Browse files Browse the repository at this point in the history
  • Loading branch information
akash-fortanix committed Dec 12, 2018
1 parent aa562a1 commit abd3819
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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")]
Expand Down Expand Up @@ -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};
Expand Down

0 comments on commit abd3819

Please sign in to comment.