-
Notifications
You must be signed in to change notification settings - Fork 213
Add support for Custom RNGs #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
name = "stdweb-getrandom" | ||
version = "0.1.0" | ||
edition = "2018" | ||
authors = ["The Rand Project Developers"] | ||
license = "MIT OR Apache-2.0" | ||
description = "Custom shim for using getrandom with stdweb" | ||
documentation = "https://docs.rs/stdweb-getrandom" | ||
repository = "https://github.com/rust-random/getrandom" | ||
categories = ["wasm"] | ||
|
||
[dependencies] | ||
getrandom = { path = "../..", version = "0.2", features = ["custom"] } | ||
stdweb = "0.4.18" | ||
|
||
# Test-only features allowing us to reuse most of the code in common.rs | ||
[features] | ||
default = ["test-stdweb"] | ||
test-stdweb = [] | ||
|
||
[[test]] | ||
name = "common" | ||
path = "../../tests/common.rs" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need it? IIUC we already test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do need this. When we move the wasm/stdweb functionality to Custom RNGs, they can no longer be tested by |
||
|
||
[package.metadata.docs.rs] | ||
default-target = "wasm32-unknown-unknown" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "wasm-bindgen-getrandom" | ||
version = "0.1.0" | ||
edition = "2018" | ||
authors = ["The Rand Project Developers"] | ||
license = "MIT OR Apache-2.0" | ||
description = "Custom shim for using getrandom with wasm-bindgen" | ||
documentation = "https://docs.rs/wasm-bindgen-getrandom" | ||
repository = "https://github.com/rust-random/getrandom" | ||
categories = ["wasm"] | ||
|
||
[dependencies] | ||
getrandom = { path = "../..", version = "0.2", features = ["custom"] } | ||
wasm-bindgen = "0.2.29" | ||
|
||
[dev-dependencies] | ||
wasm-bindgen-test = "0.2" | ||
|
||
# Test-only features allowing us to reuse most of the code in common.rs | ||
[features] | ||
default = ["test-bindgen"] | ||
test-bindgen = [] | ||
test-in-browser = ["test-bindgen"] | ||
|
||
[[test]] | ||
name = "common" | ||
path = "../../tests/common.rs" | ||
|
||
[package.metadata.docs.rs] | ||
default-target = "wasm32-unknown-unknown" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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. | ||
|
||
//! An implementation which calls out to an externally defined function. | ||
use crate::Error; | ||
use core::num::NonZeroU32; | ||
|
||
/// Register a function to be invoked by `getrandom` on custom targets. | ||
/// | ||
/// This function will only be invoked on targets not supported by `getrandom`. | ||
/// This prevents crate dependencies from either inadvertently or maliciously | ||
/// overriding the secure RNG implementations in `getrandom`. | ||
/// | ||
/// *This API requires the following crate features to be activated: `custom`* | ||
#[macro_export] | ||
macro_rules! register_custom_getrandom { | ||
($path:path) => { | ||
// We use an extern "C" function to get the guarantees of a stable ABI. | ||
#[no_mangle] | ||
extern "C" fn __getrandom_custom(dest: *mut u8, len: usize) -> u32 { | ||
let slice = unsafe { ::std::slice::from_raw_parts_mut(dest, len) }; | ||
match $path(slice) { | ||
Ok(()) => 0, | ||
Err(e) => e.code().get(), | ||
} | ||
} | ||
}; | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { | ||
extern "C" { | ||
fn __getrandom_custom(dest: *mut u8, len: usize) -> u32; | ||
} | ||
let ret = unsafe { __getrandom_custom(dest.as_mut_ptr(), dest.len()) }; | ||
match NonZeroU32::new(ret) { | ||
None => Ok(()), | ||
Some(code) => Err(Error::from(code)), | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.