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 24, 2018
1 parent 9859d62 commit 2065c83
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ libc = { version = "0.2.45" }
[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]
rand_core = "0.3.0"
rdrand = "0.4.0"

# Keep this in sync with `[dependencies]` in pregenerate_asm/Cargo.toml.
[build-dependencies]
cc = "1.0.26"
Expand Down
21 changes: 20 additions & 1 deletion src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub trait SecureRandom: sealed::Sealed {
/// On Windows, `fill` is implemented using the platform's API for secure
/// random number generation.
///
/// On SGX, `fill()` is implemented using the `RDRAND` instruction.
///
/// Otherwise, `fill()` is implemented by reading from `/dev/urandom`. (This is
/// something that should be improved for any platform that adds something
/// better.)
Expand Down Expand Up @@ -93,7 +95,7 @@ impl SecureRandom for SystemRandom {

impl sealed::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 +109,10 @@ 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::sealed;

#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -275,6 +281,19 @@ mod darwin {
}
}

#[cfg(target_env = "sgx")]
mod rdrandom {
use crate::error;
use rdrand::RdRand;
use rand_core::RngCore;

pub fn fill(dest: &mut [u8]) -> Result<(), error::Unspecified> {
let mut rng = RdRand::new().map_err(|_| error::Unspecified)?;
rng.try_fill_bytes(dest).map_err(|_| error::Unspecified)?;
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::rand::{self, SecureRandom};
Expand Down

0 comments on commit 2065c83

Please sign in to comment.