Skip to content

Commit

Permalink
Merge pull request #680 from akash-fortanix/sgx-target-for-0.4
Browse files Browse the repository at this point in the history
Add support for x86_64-fortanix-unknown-sgx target
  • Loading branch information
dhardy committed Jan 8, 2019
2 parents 5301750 + 0c2e923 commit 87634af
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
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)
Expand Down
6 changes: 5 additions & 1 deletion 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"
Expand Down Expand Up @@ -33,3 +33,7 @@ members = ["rand-derive"]

[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"
6 changes: 6 additions & 0 deletions src/lib.rs
Expand Up @@ -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;
Expand Down
49 changes: 48 additions & 1 deletion src/os.rs
Expand Up @@ -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
Expand Down Expand Up @@ -53,12 +57,14 @@ impl fmt::Debug for OsRng {
}
}

#[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);
Expand Down Expand Up @@ -562,6 +568,47 @@ mod imp {
}
}

#[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;
Expand Down

0 comments on commit 87634af

Please sign in to comment.