Skip to content

Commit 727c70f

Browse files
support sgx-target
1 parent 158c3e9 commit 727c70f

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,6 @@ autocfg = "0.1"
7575

7676
[package.metadata.docs.rs]
7777
all-features = true
78+
79+
[patch.crates-io]
80+
rand_core = { path = "rand_core", version = "0.3", default-features = false }

rand_os/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ fuchsia-zircon = "0.3.2"
3434
[target.wasm32-unknown-unknown.dependencies]
3535
wasm-bindgen = { version = "0.2.12", optional = true }
3636
stdweb = { version = "0.4", optional = true }
37+
38+
[target.'cfg(target_env = "sgx")'.dependencies]
39+
rdrand = "0.4.0"

rand_os/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ extern crate wasm_bindgen;
142142
feature = "stdweb"))]
143143
#[macro_use] extern crate stdweb;
144144

145+
#[cfg(target_env = "sgx")]
146+
extern crate rdrand;
145147

146148
#[cfg(not(feature = "log"))]
147149
#[macro_use]
@@ -310,6 +312,7 @@ mod_use!(cfg(target_os = "openbsd"), openbsd_bitrig);
310312
mod_use!(cfg(target_os = "redox"), redox);
311313
mod_use!(cfg(target_os = "solaris"), solaris);
312314
mod_use!(cfg(windows), windows);
315+
mod_use!(cfg(target_env = "sgx"), sgx);
313316

314317
mod_use!(
315318
cfg(all(
@@ -356,5 +359,6 @@ compile_error!("enable either wasm_bindgen or stdweb feature");
356359
target_os = "solaris",
357360
windows,
358361
target_arch = "wasm32",
362+
target_env = "sgx"
359363
)))]
360364
compile_error!("OS RNG support is not available for this platform");

rand_os/src/sgx.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2018 Developers of the Rand project.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
use super::OsRngImpl;
10+
use Error;
11+
use rdrand::RdRand;
12+
use rand_core::RngCore;
13+
use std::fmt::{Debug, Formatter, Result as FmtResult};
14+
15+
#[derive(Clone)]
16+
pub struct OsRng{
17+
gen: RdRand
18+
}
19+
20+
impl OsRngImpl for OsRng {
21+
fn new() -> Result<OsRng, Error> {
22+
let rng = RdRand::new()?;
23+
Ok(OsRng{ gen: rng })
24+
}
25+
26+
fn fill_chunk(&mut self, dest: &mut [u8]) -> Result<(), Error> {
27+
self.gen.try_fill_bytes(dest)
28+
}
29+
30+
fn method_str(&self) -> &'static str { "RDRAND" }
31+
}
32+
33+
impl Debug for OsRng {
34+
fn fmt(&self, f: &mut Formatter) -> FmtResult {
35+
f.debug_struct("OsRng")
36+
.finish()
37+
}
38+
}

0 commit comments

Comments
 (0)