Skip to content

Commit 90c2ee2

Browse files
committed
Add support for Custom RNGs
1 parent afe9d9d commit 90c2ee2

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/custom.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
12+
/// Reister a function to be invoked by getrandom on custom targets. This macro
13+
/// is only avalible on custom targets to prevent registering a function that
14+
/// will not be called.
15+
#[macro_export]
16+
macro_rules! register_custom_getrandom {
17+
($path:path) => {
18+
#[no_mangle]
19+
fn __getrandom_custom(dest: &mut [u8]) -> Result<(), Error> {
20+
$path(dest)
21+
}
22+
};
23+
}
24+
25+
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
26+
extern "Rust" {
27+
#[allow(improper_ctypes)] // See rust-lang/rust#64593
28+
fn __getrandom_custom(dest: &mut [u8]) -> Result<(), Error>;
29+
}
30+
unsafe { __getrandom_custom(dest) }
31+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ cfg_if! {
230230
target_env = "sgx",
231231
)))] {
232232
#[path = "rdrand.rs"] mod imp;
233+
} else if #[cfg(feature = "custom")] {
234+
#[path = "custom.rs"] mod imp;
233235
} else {
234236
compile_error!("\
235237
target is not supported, for more information see: \

0 commit comments

Comments
 (0)