Skip to content

Commit 8e2c423

Browse files
committed
custom: Add RNG for CPU-based randomness
Signed-off-by: Joe Richey <[email protected]>
1 parent 8365277 commit 8e2c423

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ appveyor = { repository = "rust-random/getrandom" }
1616

1717
[workspace]
1818
members = [
19+
"custom/cpurand",
1920
"custom/stdweb",
2021
"custom/wasm-bindgen",
2122
]

custom/cpurand/Cargo.toml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "cpurand-getrandom"
3+
version = "0.1.0"
4+
edition = "2018"
5+
authors = ["The Rand Project Developers"]
6+
license = "MIT OR Apache-2.0"
7+
description = "Custom shim for using getrandom with CPU RNG instructions"
8+
documentation = "https://docs.rs/cpurand-getrandom"
9+
repository = "https://github.com/rust-random/getrandom"
10+
categories = ["hardware-support", "no-std"]
11+
12+
[dependencies]
13+
cfg-if = "0.1.2"
14+
getrandom = { path = "../..", version = "0.2", features = ["custom"] }
15+
16+
[[test]]
17+
name = "common"
18+
path = "../../tests/common.rs"
19+
20+
[package.metadata.docs.rs]
21+
default-target = "x86_64-unknown-linux"

custom/cpurand/src/lib.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 Developers of the Rand project.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
#![no_std]
9+
10+
#[macro_use]
11+
extern crate cfg_if;
12+
13+
use getrandom::{register_custom_getrandom, Error};
14+
15+
cfg_if! {
16+
if #[cfg(target_arch = "x86_64")] {
17+
#[path = "../../../src/util.rs"] mod util;
18+
#[path = "../../../src/rdrand.rs"] mod imp;
19+
} else {
20+
compile_error!("CPU-based randomness is not supported for this arch");
21+
}
22+
}
23+
24+
register_custom_getrandom!(imp::getrandom_inner);

src/custom.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ macro_rules! register_custom_getrandom {
2323
// We use an extern "C" function to get the guarantees of a stable ABI.
2424
#[no_mangle]
2525
extern "C" fn __getrandom_custom(dest: *mut u8, len: usize) -> u32 {
26-
let slice = unsafe { ::std::slice::from_raw_parts_mut(dest, len) };
26+
let slice = unsafe { ::core::slice::from_raw_parts_mut(dest, len) };
2727
match $path(slice) {
2828
Ok(()) => 0,
2929
Err(e) => e.code().get(),

src/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,7 @@ cfg_if! {
183183
#[path = "windows_uwp.rs"] mod imp;
184184
} else if #[cfg(windows)] {
185185
#[path = "windows.rs"] mod imp;
186-
} else if #[cfg(all(target_arch = "x86_64", any(
187-
target_os = "hermit",
188-
target_os = "l4re",
189-
target_os = "uefi",
190-
target_env = "sgx",
191-
)))] {
186+
} else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] {
192187
#[path = "rdrand.rs"] mod imp;
193188
} else if #[cfg(feature = "custom")] {
194189
use custom as imp;

0 commit comments

Comments
 (0)