Skip to content

Commit 448bcac

Browse files
josephlrnewpavlov
authored andcommitted
custom: Add support for Custom RNGs
1 parent d6b75d1 commit 448bcac

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ matrix:
9191
- cargo test --benches
9292
# Check that setting various features does not break the build
9393
- cargo build --features=std
94+
- cargo build --features=custom
9495
# remove cached documentation, otherwise files from previous PRs can get included
9596
- rm -rf target/doc
96-
- cargo doc --no-deps --features=std
97+
- cargo doc --no-deps --features=std,custom
9798
- cargo deadlinks --dir target/doc
9899
# also test minimum dependency versions are usable
99100
- cargo generate-lockfile -Z minimal-versions
100-
- cargo test --features=std
101+
- cargo test --features=std,custom
101102

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

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ wasm-bindgen-test = "0.2"
3636

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

4446
[package.metadata.docs.rs]
45-
features = ["std"]
47+
features = ["std", "custom"]

src/custom.rs

Lines changed: 45 additions & 0 deletions
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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ extern crate cfg_if;
134134

135135
mod error;
136136
mod util;
137-
137+
// To prevent a breaking change when targets are added, we always export the
138+
// register_custom_getrandom macro, so old Custom RNG crates continue to build.
139+
#[cfg(feature = "custom")]
140+
mod custom;
138141
#[cfg(feature = "std")]
139142
mod error_impls;
140143

@@ -201,6 +204,8 @@ cfg_if! {
201204
");
202205
}
203206
}
207+
} else if #[cfg(feature = "custom")] {
208+
use custom as imp;
204209
} else {
205210
compile_error!("\
206211
target is not supported, for more information see: \

0 commit comments

Comments
 (0)