-
Notifications
You must be signed in to change notification settings - Fork 210
Crate implementation #6
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
71a3cb8
first commit
newpavlov 9420df9
remove fuchsia loop
newpavlov 99a1310
use fuchsia-cprng
newpavlov d8c356b
remove wasm-bindgen shims
newpavlov 8f6edad
add benchmarks
newpavlov 1049ce8
review updates
newpavlov 39ba5d2
remove redundant extern crate
newpavlov 9855b52
change test cfg
newpavlov 5ebbaf7
remove unsafe and newlines, add notice
newpavlov f860bde
use atomic bool to check RNG initialization
newpavlov 1e2a5aa
new error
newpavlov 9871d55
remove unsafe
newpavlov af5035f
remove unused feature
newpavlov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
[package] | ||
name = "getrandom" | ||
version = "0.0.0" | ||
version = "0.1.0" | ||
authors = ["The Rand Project Developers"] | ||
license = "MIT/Apache-2.0" | ||
edition = "2015" | ||
license = "MIT OR Apache-2.0" | ||
description = "A small cross-platform library to securely get random data (entropy)" | ||
|
||
[dependencies] | ||
[badges] | ||
travis-ci = { repository = "rust-random/getrandom" } | ||
appveyor = { repository = "rust-random/getrandom" } | ||
|
||
[target.'cfg(unix)'.dependencies] | ||
libc = "0.2" | ||
|
||
[target.'cfg(windows)'.dependencies] | ||
winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "winnt"] } | ||
|
||
[target.wasm32-unknown-unknown.dependencies] | ||
wasm-bindgen = { version = "0.2.12", optional = true } | ||
stdweb = { version = "0.4", optional = true } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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. | ||
use error::Error; | ||
|
||
extern "C" { | ||
fn cloudabi_sys_random_get(buf: *mut u8, len: usize) -> u16; | ||
} | ||
|
||
pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { | ||
let errno = unsafe { cloudabi_sys_random_get(dest.as_ptr(), dest.len()) }; | ||
if errno == 0 { | ||
Ok(()) | ||
} else { | ||
Err(Error::Unknown) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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. | ||
|
||
//! Implementation for DragonFly / Haiku | ||
use super::Error; | ||
use std::fs::File; | ||
use std::io::Read; | ||
use std::cell::RefCell; | ||
use std::ops::DerefMut; | ||
|
||
thread_local!(static RNG_FILE: RefCell<Option<File>> = RefCell::new(None)); | ||
|
||
pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { | ||
RNG_FILE.with(|f| { | ||
let mut f = f.borrow_mut(); | ||
let f: &mut Option<File> = f.deref_mut(); | ||
if let Some(f) = f { | ||
f.read_exact(dest) | ||
} else { | ||
let mut rng_file = File::open("/dev/random")?; | ||
rng_file.read_exact(dest)?; | ||
newpavlov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*f = Some(rng_file); | ||
Ok(()) | ||
} | ||
}).map_err(|_| Error::Unknown) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 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. | ||
|
||
//! A dummy implementation for unsupported targets which always returns | ||
//! `Err(Error::Unavailable)` | ||
use super::Error; | ||
|
||
pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { | ||
Err(Error::Unavailable) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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. | ||
|
||
//! Implementation for DragonFly / Haiku / Emscripten | ||
newpavlov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use super::Error; | ||
use std::fs::File; | ||
use std::io::Read; | ||
use std::cell::RefCell; | ||
use std::ops::DerefMut; | ||
|
||
thread_local!(static RNG_FILE: RefCell<Option<File>> = RefCell::new(None)); | ||
|
||
pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { | ||
// `Crypto.getRandomValues` documents `dest` should be at most 65536 | ||
// bytes. `crypto.randomBytes` documents: "To minimize threadpool | ||
// task length variation, partition large randomBytes requests when | ||
// doing so as part of fulfilling a client request. | ||
for chunk in dest.chunks_mut(65536) { | ||
RNG_FILE.with(|f| { | ||
let mut f = f.borrow_mut(); | ||
let f: &mut Option<File> = f.deref_mut(); | ||
if let Some(f) = f { | ||
f.read_exact(chunk) | ||
} else { | ||
let mut rng_file = File::open("/dev/random")?; | ||
rng_file.read_exact(chunk)?; | ||
*f = Some(rng_file); | ||
Ok(()) | ||
} | ||
}).map_err(|_| Error::Unknown)?; | ||
} | ||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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. | ||
|
||
#[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
pub enum Error { | ||
/// Call was interrupted. | ||
/// | ||
/// Typically it can be retried. | ||
Interrupted, | ||
/// RNG source is unavailable on a given system. | ||
Unavailable, | ||
/// Unknown error. | ||
Unknown, | ||
#[doc(hidden)] | ||
__Nonexhaustive, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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. | ||
|
||
//! Implementation for FreeBSD | ||
extern crate libc; | ||
|
||
use super::Error; | ||
use std::ptr; | ||
newpavlov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
pub fn getrandom(dest: &mut [u8]) -> Result<(), Error> { | ||
let mib = [libc::CTL_KERN, libc::KERN_ARND]; | ||
// kern.arandom permits a maximum buffer size of 256 bytes | ||
for chunk in dest.chunks_mut(256) { | ||
let mut len = chunk.len(); | ||
let ret = unsafe { | ||
libc::sysctl( | ||
mib.as_ptr(), mib.len() as libc::c_uint, | ||
chunk.as_mut_ptr() as *mut _, &mut len, ptr::null(), 0, | ||
) | ||
}; | ||
if ret == -1 || len != chunk.len() { | ||
return Err(Error::Unknown); | ||
} | ||
} | ||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// 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. | ||
|
||
//! Implementation for Fuchsia Zircon | ||
use super::Error; | ||
|
||
#[link(name = "zircon")] | ||
extern { | ||
fn zx_cprng_draw(buffer: *mut u8, len: usize); | ||
} | ||
|
||
pub fn getrandom(&mut self, dest: &mut [u8]) -> Result<(), Error> { | ||
for chunk in dest.chunks(256) { | ||
newpavlov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unsafe { zx_cprng_draw(chunk.as_mut_ptr(), chunk.len()) }; | ||
} | ||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.