Skip to content

Add support for x86_64-fortanix-unknown-sgx target #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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