Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rust-random/getrandom
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: b7e5df8c92e8443b86e9eb76fbebb8c2101cb727
Choose a base ref
..
head repository: rust-random/getrandom
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ad621319725179dcbfc9cc9cc746dbe4a38e0098
Choose a head ref
Showing with 61 additions and 57 deletions.
  1. +3 −0 .travis.yml
  2. +1 −1 Cargo.toml
  3. +1 −1 src/lib.rs
  4. +1 −4 src/linux_android.rs
  5. +55 −0 src/rdrand.rs
  6. +0 −51 src/sgx.rs
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -105,13 +105,15 @@ matrix:
#- rustup target add x86_64-unknown-fuchsia
- rustup target add x86_64-unknown-netbsd
- rustup target add x86_64-unknown-redox
- rustup target add x86_64-fortanix-unknown-sgx
script:
- cargo build --target=x86_64-sun-solaris --all-features
- cargo build --target=x86_64-unknown-cloudabi --all-features
- cargo build --target=x86_64-unknown-freebsd --all-features
#- cargo build --target=x86_64-unknown-fuchsia --all-features
- cargo build --target=x86_64-unknown-netbsd --all-features
- cargo build --target=x86_64-unknown-redox --all-features
- cargo build --target=x86_64-fortanix-unknown-sgx --all-features
# also test minimum dependency versions are usable
- cargo generate-lockfile -Z minimal-versions
- cargo build --target=x86_64-sun-solaris --all-features
@@ -120,6 +122,7 @@ matrix:
#- cargo build --target=x86_64-unknown-fuchsia --all-features
- cargo build --target=x86_64-unknown-netbsd --all-features
- cargo build --target=x86_64-unknown-redox --all-features
- cargo build --target=x86_64-fortanix-unknown-sgx --all-features

# Trust cross-built/emulated targets. We must repeat all non-default values.
- rust: stable
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ members = ["tests/wasm_bindgen"]
log = { version = "0.4", optional = true }

[target.'cfg(unix)'.dependencies]
libc = "0.2.29"
libc = "0.2.34"
lazy_static = "1.3.0"

[target.'cfg(windows)'.dependencies]
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -183,7 +183,7 @@ mod_use!(cfg(target_os = "openbsd"), openbsd_bitrig);
mod_use!(cfg(target_os = "redox"), use_file);
mod_use!(cfg(target_os = "solaris"), solaris_illumos);
mod_use!(cfg(windows), windows);
mod_use!(cfg(target_env = "sgx"), sgx);
mod_use!(cfg(target_env = "sgx"), rdrand);
mod_use!(cfg(target_os = "wasi"), wasi);

mod_use!(
5 changes: 1 addition & 4 deletions src/linux_android.rs
Original file line number Diff line number Diff line change
@@ -14,11 +14,8 @@ use core::num::NonZeroU32;
use lazy_static::lazy_static;
use std::io;

// This flag tells getrandom() to return EAGAIN instead of blocking.
const GRND_NONBLOCK: libc::c_uint = 0x0001;

fn syscall_getrandom(dest: &mut [u8], block: bool) -> Result<(), io::Error> {
let flags = if block { 0 } else { GRND_NONBLOCK };
let flags = if block { 0 } else { libc::GRND_NONBLOCK };
let ret = unsafe {
libc::syscall(libc::SYS_getrandom, dest.as_mut_ptr(), dest.len(), flags)
};
55 changes: 55 additions & 0 deletions src/rdrand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2018 Developers of the Rand project.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Implementation for SGX using RDRAND instruction
use crate::Error;
use core::mem;
use core::arch::x86_64::_rdrand64_step;
use core::num::NonZeroU32;

#[cfg(not(target_feature = "rdrand"))]
compile_error!("enable rdrand target feature!");

// Recommendation from "Intel® Digital Random Number Generator (DRNG) Software
// Implementation Guide" - Section 5.2.1 and "Intel® 64 and IA-32 Architectures
// Software Developer’s Manual" - Volume 1 - Section 7.3.17.1.
const RETRY_LIMIT: usize = 10;
const WORD_SIZE: usize = mem::size_of::<u64>();

fn rdrand() -> Result<[u8; WORD_SIZE], Error> {
for _ in 0..RETRY_LIMIT {
unsafe {
// SAFETY: we've checked RDRAND support, and u64 can have any value.
let mut el = mem::uninitialized();
if _rdrand64_step(&mut el) == 1 {
return Ok(el.to_ne_bytes());
}
};
}
error!("RDRAND failed, CPU issue likely");
Err(Error::UNKNOWN)
}

pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
// We use chunks_exact_mut instead of chunks_mut as it allows almost all
// calls to memcpy to be elided by the compiler.
let mut chunks = dest.chunks_exact_mut(WORD_SIZE);
for chunk in chunks.by_ref() {
chunk.copy_from_slice(&rdrand()?);
}

let tail = chunks.into_remainder();
let n = tail.len();
if n > 0 {
tail.copy_from_slice(&rdrand()?[..n]);
}
Ok(())
}

#[inline(always)]
pub fn error_msg_inner(_: NonZeroU32) -> Option<&'static str> { None }
51 changes: 0 additions & 51 deletions src/sgx.rs

This file was deleted.