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 Jan 1, 2019
1 parent 158c3e9 commit 727c70f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -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 }
3 changes: 3 additions & 0 deletions rand_os/Cargo.toml
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions rand_os/src/lib.rs
Expand Up @@ -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]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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");
38 changes: 38 additions & 0 deletions rand_os/src/sgx.rs
@@ -0,0 +1,38 @@
// Copyright 2018 Developers of the Rand project.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, 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<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()
}
}

0 comments on commit 727c70f

Please sign in to comment.