Skip to content

Commit 2d8b82a

Browse files
committed
custom: Add support for Custom RNGs
1 parent b6839f6 commit 2d8b82a

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

.travis.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ matrix:
9292
# Check that setting various features does not break the build
9393
- cargo build --features=std
9494
- cargo build --features=log
95+
- cargo build --features=custom
9596
# remove cached documentation, otherwise files from previous PRs can get included
9697
- rm -rf target/doc
97-
- cargo doc --no-deps --features=std
98+
- cargo doc --no-deps --features=std,custom
9899
- cargo deadlinks --dir target/doc
99100
# also test minimum dependency versions are usable
100101
- cargo generate-lockfile -Z minimal-versions
101-
- cargo test --features=std,log
102+
- cargo test --features=std,log,custom
102103

103104
- <<: *nightly_and_docs
104105
name: "OSX, nightly, docs"

Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ wasm-bindgen-test = "0.2"
3737

3838
[features]
3939
std = []
40+
# Feature to enable custom RNG implementations
41+
custom = []
4042
# Unstable feature to support being a libstd dependency
4143
rustc-dep-of-std = ["compiler_builtins", "core"]
4244
# Unstable feature for testing
4345
test-in-browser = ["wasm-bindgen"]
4446

4547
[package.metadata.docs.rs]
46-
features = ["std"]
48+
features = ["std", "custom"]

src/custom.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
9+
//! An implementation which calls out to an externally defined function.
10+
use crate::Error;
11+
use core::num::NonZeroU32;
12+
13+
/// Register a function to be invoked by `getrandom` on custom targets.
14+
///
15+
/// This function will only be invoked on targets not supported by `getrandom`.
16+
/// This prevents crate dependencies from either inadvertently or maliciously
17+
/// overriding the secure RNG implementations in `getrandom`.
18+
///
19+
/// *This API requires the following crate features to be activated: `custom`*
20+
#[macro_export]
21+
macro_rules! register_custom_getrandom {
22+
($path:path) => {
23+
// We use an extern "C" function to get the guarantees of a stable ABI.
24+
#[no_mangle]
25+
extern "C" fn __getrandom_custom(dest: *mut u8, len: usize) -> u32 {
26+
let slice = unsafe { ::std::slice::from_raw_parts_mut(dest, len) };
27+
match $path(slice) {
28+
Ok(()) => 0,
29+
Err(e) => e.code().get(),
30+
}
31+
}
32+
};
33+
}
34+
35+
#[allow(dead_code)]
36+
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
37+
extern "C" {
38+
fn __getrandom_custom(dest: *mut u8, len: usize) -> u32;
39+
}
40+
let ret = unsafe { __getrandom_custom(dest.as_mut_ptr(), dest.len()) };
41+
match NonZeroU32::new(ret) {
42+
None => Ok(()),
43+
Some(code) => Err(Error::from(code)),
44+
}
45+
}

src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ cfg_if! {
155155

156156
mod error;
157157
mod util;
158-
158+
// To prevent a breaking change when targets are added, we always export the
159+
// register_custom_getrandom macro, so old Custom RNG crates continue to build.
160+
#[cfg(feature = "custom")]
161+
mod custom;
159162
#[cfg(feature = "std")]
160163
mod error_impls;
161164

@@ -222,6 +225,8 @@ cfg_if! {
222225
");
223226
}
224227
}
228+
} else if #[cfg(feature = "custom")] {
229+
use custom as imp;
225230
} else {
226231
compile_error!("\
227232
target is not supported, for more information see: \

0 commit comments

Comments
 (0)