Skip to content

Commit

Permalink
support sgx-target
Browse files Browse the repository at this point in the history
  • Loading branch information
akash-fortanix committed Dec 26, 2018
1 parent 2b5eac5 commit ce59098
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Expand Up @@ -65,6 +65,9 @@ fuchsia-zircon = { version = "0.3.2", optional = true }
stdweb = { version = "0.4", optional = true }
wasm-bindgen = { version = "0.2.12", optional = true }

[target.'cfg(target_env = "sgx")'.dependencies]
rdrand = "0.4.0"

[dev-dependencies]
# This has a histogram implementation used for testing uniformity.
average = "0.9.2"
Expand All @@ -74,3 +77,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 }
3 changes: 3 additions & 0 deletions src/lib.rs
Expand Up @@ -72,6 +72,9 @@ extern crate stdweb;
#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
extern crate wasm_bindgen;

#[cfg(target_env = "sgx")]
extern crate rdrand;

extern crate rand_core;
extern crate rand_isaac; // only for deprecations
extern crate rand_chacha; // only for deprecations
Expand Down
1 change: 1 addition & 0 deletions src/rngs/mod.rs
Expand Up @@ -194,6 +194,7 @@ pub use self::std::StdRng;
windows,
all(target_arch = "wasm32", feature = "stdweb"),
all(target_arch = "wasm32", feature = "wasm-bindgen"),
target_env = "sgx",
)))]
mod os;

Expand Down
33 changes: 33 additions & 0 deletions src/rngs/os.rs
Expand Up @@ -1191,6 +1191,39 @@ mod imp {
}
}

#[cfg(target_env = "sgx")]
mod imp {
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<OsRng, Error> {
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()
}
}
}

#[cfg(test)]
mod test {
Expand Down

0 comments on commit ce59098

Please sign in to comment.