Skip to content

Commit ce59098

Browse files
support sgx-target
1 parent 2b5eac5 commit ce59098

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ fuchsia-zircon = { version = "0.3.2", optional = true }
6565
stdweb = { version = "0.4", optional = true }
6666
wasm-bindgen = { version = "0.2.12", optional = true }
6767

68+
[target.'cfg(target_env = "sgx")'.dependencies]
69+
rdrand = "0.4.0"
70+
6871
[dev-dependencies]
6972
# This has a histogram implementation used for testing uniformity.
7073
average = "0.9.2"
@@ -74,3 +77,6 @@ autocfg = "0.1"
7477

7578
[package.metadata.docs.rs]
7679
all-features = true
80+
81+
[patch.crates-io]
82+
rand_core = { path = "rand_core", version = "0.3", default-features = false }

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ extern crate stdweb;
7272
#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
7373
extern crate wasm_bindgen;
7474

75+
#[cfg(target_env = "sgx")]
76+
extern crate rdrand;
77+
7578
extern crate rand_core;
7679
extern crate rand_isaac; // only for deprecations
7780
extern crate rand_chacha; // only for deprecations

src/rngs/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ pub use self::std::StdRng;
194194
windows,
195195
all(target_arch = "wasm32", feature = "stdweb"),
196196
all(target_arch = "wasm32", feature = "wasm-bindgen"),
197+
target_env = "sgx",
197198
)))]
198199
mod os;
199200

src/rngs/os.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,39 @@ mod imp {
11911191
}
11921192
}
11931193

1194+
#[cfg(target_env = "sgx")]
1195+
mod imp {
1196+
use super::OsRngImpl;
1197+
use Error;
1198+
use rdrand::RdRand;
1199+
use rand_core::RngCore;
1200+
use std::fmt::{Debug, Formatter, Result as FmtResult};
1201+
1202+
#[derive(Clone)]
1203+
pub struct OsRng{
1204+
gen: RdRand
1205+
}
1206+
1207+
impl OsRngImpl for OsRng {
1208+
fn new() -> Result<OsRng, Error> {
1209+
let rng = RdRand::new()?;
1210+
Ok(OsRng{ gen: rng })
1211+
}
1212+
1213+
fn fill_chunk(&mut self, dest: &mut [u8]) -> Result<(), Error> {
1214+
self.gen.try_fill_bytes(dest)
1215+
}
1216+
1217+
fn method_str(&self) -> &'static str { "RDRAND" }
1218+
}
1219+
1220+
impl Debug for OsRng {
1221+
fn fmt(&self, f: &mut Formatter) -> FmtResult {
1222+
f.debug_struct("OsRng")
1223+
.finish()
1224+
}
1225+
}
1226+
}
11941227

11951228
#[cfg(test)]
11961229
mod test {

0 commit comments

Comments
 (0)