Skip to content

Commit

Permalink
Merge pull request #670 from akash-fortanix/sgx-target
Browse files Browse the repository at this point in the history
Add support for x86_64-fortanix-unknown-sgx target
  • Loading branch information
dhardy authored Jan 8, 2019
2 parents 613e177 + 727c70f commit afc9d9a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 afc9d9a

Please sign in to comment.