support sgx-target
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1811b45..9241ad1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,10 @@
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
+## [0.4.4] - 2018-01-06
+### Added
+- SGX support
+
## [0.4.3] - 2018-08-16
### Fixed
- Use correct syscall number for PowerPC (#589)
diff --git a/Cargo.toml b/Cargo.toml
index c21f53e..6ffd4a1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "rand"
-version = "0.4.3"
+version = "0.4.4"
authors = ["The Rust Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
@@ -33,3 +33,7 @@
[target.'cfg(target_os = "fuchsia")'.dependencies]
fuchsia-zircon = "0.3.2"
+
+[target.'cfg(target_env = "sgx")'.dependencies]
+rdrand = "0.4.0"
+rand_core = "0.3.0"
diff --git a/src/lib.rs b/src/lib.rs
index 7b22dd4..696ff6f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -250,6 +250,12 @@
#[cfg(feature="std")] extern crate std as core;
#[cfg(all(feature = "alloc", not(feature="std")))] extern crate alloc;
+#[cfg(target_env = "sgx")]
+extern crate rdrand;
+
+#[cfg(target_env = "sgx")]
+extern crate rand_core;
+
use core::marker;
use core::mem;
#[cfg(feature="std")] use std::cell::RefCell;
diff --git a/src/os.rs b/src/os.rs
index 10022fb..0be8713 100644
--- a/src/os.rs
+++ b/src/os.rs
@@ -11,7 +11,11 @@
//! Interfaces to the operating system provided random number
//! generators.
-use std::{io, mem, fmt};
+use std::{io, fmt};
+
+#[cfg(not(target_env = "sgx"))]
+use std::mem;
+
use Rng;
/// A random number generator that retrieves randomness straight from
@@ -53,12 +57,14 @@
}
}
+#[cfg(not(target_env = "sgx"))]
fn next_u32(fill_buf: &mut FnMut(&mut [u8])) -> u32 {
let mut buf: [u8; 4] = [0; 4];
fill_buf(&mut buf);
unsafe { mem::transmute::<[u8; 4], u32>(buf) }
}
+#[cfg(not(target_env = "sgx"))]
fn next_u64(fill_buf: &mut FnMut(&mut [u8])) -> u64 {
let mut buf: [u8; 8] = [0; 8];
fill_buf(&mut buf);
@@ -562,6 +568,47 @@
}
}
+#[cfg(target_env = "sgx")]
+mod imp {
+ use rdrand::RdRand;
+ use std::io;
+ use rand_core::RngCore;
+
+ pub struct OsRng{
+ gen: RdRand
+ }
+
+ impl OsRng {
+ pub fn new() -> io::Result<OsRng> {
+ match RdRand::new() {
+ Ok(rng) => Ok(OsRng { gen: rng }),
+ Err(_) => Err(io::Error::new(io::ErrorKind::Other, "Not supported"))
+ }
+ }
+
+ pub(crate) fn next_u32(&mut self) -> u32 {
+ match self.gen.try_next_u32() {
+ Some(n) => n,
+ None => panic!("Non-recoverable hardware failure has occured")
+ }
+ }
+
+ pub(crate) fn next_u64(&mut self) -> u64 {
+ match self.gen.try_next_u64() {
+ Some(n) => n,
+ None => panic!("Non-recoverable hardware failure has occured")
+ }
+ }
+
+ pub(crate) fn fill_bytes(&mut self, v: &mut [u8]) {
+ match self.gen.try_fill_bytes(v) {
+ Ok(_) => {},
+ Err(_) => panic!("Non-recoverable hardware failure has occured")
+ }
+ }
+ }
+}
+
#[cfg(test)]
mod test {
use std::sync::mpsc::channel;