Skip to content

Commit 19c39fb

Browse files
authored
Minor updates (#12)
1 parent 170cbc1 commit 19c39fb

File tree

8 files changed

+85
-62
lines changed

8 files changed

+85
-62
lines changed

Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ name = "getrandom"
33
version = "0.1.0"
44
authors = ["The Rand Project Developers"]
55
license = "MIT OR Apache-2.0"
6-
description = "A small cross-platform library to securely get random data (entropy)"
6+
description = "A small cross-platform library for retrieving random data from system source"
7+
documentation = "https://docs.rs/getrandom"
8+
repository = "https://github.com/rust-random/getrandom"
9+
categories = ["os", "no-std"]
10+
exclude = ["utils/*", ".*", "appveyor.yml"]
711

812
[badges]
913
travis-ci = { repository = "rust-random/getrandom" }
@@ -18,8 +22,7 @@ members = [
1822
log = { version = "0.4", optional = true }
1923

2024
[target.'cfg(unix)'.dependencies]
21-
# In general, we need at least 0.2.27. On Solaris, we need some unknown newer version.
22-
libc = "0.2.50"
25+
libc = "0.2.29"
2326

2427
[target.'cfg(windows)'.dependencies]
2528
winapi = { version = "0.3.6", features = ["minwindef", "ntsecapi", "winnt"] }

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
# Rand
1+
# getrandom
22

33
[![Build Status](https://travis-ci.org/rust-random/getrandom.svg?branch=master)](https://travis-ci.org/rust-random/getrandom)
44
[![Build Status](https://ci.appveyor.com/api/projects/status/github/rust-random/getrandom?svg=true)](https://ci.appveyor.com/project/rust-random/getrandom)
55
[![Crate](https://img.shields.io/crates/v/getrandom.svg)](https://crates.io/crates/getrandom)
6-
[![API](https://docs.rs/getrandom/badge.svg)](https://docs.rs/getrandom)
6+
[![Documentation](https://docs.rs/getrandom/badge.svg)](https://docs.rs/getrandom)
7+
[![Dependency status](https://deps.rs/repo/github/rust-random/getrandom/status.svg)](https://deps.rs/repo/github/rust-random/getrandom)
78

8-
A Rust library to securely get random entropy. This crate derives its name from
9-
Linux's `getrandom` function, but is cross platform, roughly supporting the same
10-
set of platforms as Rust's `std` lib.
119

12-
This is a low-level API. Most users should prefer a high-level random-number
13-
library like [Rand] or a cryptography library.
10+
A Rust library for retrieving random data from (operating) system source. It is
11+
assumed that system always provides high-quality cryptographically secure random
12+
data, ideally backed by hardware entropy sources. This crate derives its name
13+
from Linux's `getrandom` function, but is cross platform, roughly supporting
14+
the same set of platforms as Rust's `std` lib.
15+
16+
This is a low-level API. Most users should prefer using high-level random-number
17+
library like [`rand`].
1418

1519
[Rand]: https://crates.io/crates/rand
1620

@@ -36,7 +40,7 @@ fn get_random_buf() -> Result<[u8; 32], getrandom::Error> {
3640

3741
## Features
3842

39-
This library is `no_std` compatible on SGX but requires `std` on most platforms.
43+
This library is `no_std` compatible, but uses `std` on most platforms.
4044

4145
The `log` library is supported as an optional dependency. If enabled, error
4246
reporting will be improved on some platforms.
@@ -47,15 +51,17 @@ one of the following features must be enabled:
4751
- [`wasm-bindgen`](https://crates.io/crates/wasm_bindgen)
4852
- [`stdweb`](https://crates.io/crates/stdweb)
4953

50-
## Versions
54+
## Minimum Supported Rust Version
5155

5256
This crate requires Rustc version 1.28.0 or later due to usage of `NonZeroU32`.
5357

5458

5559
# License
5660

57-
The `getrandom` library is distributed under the terms of both the MIT license
58-
and the Apache License (Version 2.0).
61+
The `getrandom` library is distributed under either of
62+
63+
* [Apache License, Version 2.0](LICENSE-APACHE)
64+
* [MIT license](LICENSE-MIT)
65+
66+
at your option.
5967

60-
See [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT), and
61-
[COPYRIGHT](COPYRIGHT) for details.

src/dummy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
// except according to those terms.
88

99
//! A dummy implementation for unsupported targets which always returns
10-
//! `Err(ERROR_UNAVAILABLE)`
10+
//! `Err(Error::UNAVAILABLE)`
1111
use std::num::NonZeroU32;
12-
use {Error, ERROR_UNAVAILABLE};
12+
use Error;
1313

1414
pub fn getrandom_inner(_: &mut [u8]) -> Result<(), Error> {
1515
error!("no support for this platform");
16-
Err(ERROR_UNAVAILABLE)
16+
Err(Error::UNAVAILABLE)
1717
}
1818

1919
#[inline(always)]

src/error.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,45 @@ use core::fmt;
1212
#[cfg(not(target_env = "sgx"))]
1313
use std::{io, error};
1414

15-
// A randomly-chosen 16-bit prefix for our codes
16-
pub(crate) const CODE_PREFIX: u32 = 0x57f40000;
17-
const CODE_UNKNOWN: u32 = CODE_PREFIX | 0;
18-
const CODE_UNAVAILABLE: u32 = CODE_PREFIX | 1;
19-
20-
/// An unknown error.
21-
///
22-
/// This is the following constant: 57F40000 (hex) / 1475608576 (decimal).
23-
pub const ERROR_UNKNOWN: Error = Error(unsafe {
24-
NonZeroU32::new_unchecked(CODE_UNKNOWN)
25-
});
26-
27-
/// No generator is available.
28-
///
29-
/// This is the following constant: 57F40001 (hex) / 1475608577 (decimal).
30-
pub const ERROR_UNAVAILABLE: Error = Error(unsafe {
31-
NonZeroU32::new_unchecked(CODE_UNAVAILABLE)
32-
});
15+
// A randomly-chosen 24-bit prefix for our codes
16+
pub(crate) const CODE_PREFIX: u32 = 0x57f4c500;
17+
const CODE_UNKNOWN: u32 = CODE_PREFIX | 0x00;
18+
const CODE_UNAVAILABLE: u32 = CODE_PREFIX | 0x01;
3319

3420
/// The error type.
35-
///
21+
///
3622
/// This type is small and no-std compatible.
3723
#[derive(Copy, Clone, Eq, PartialEq)]
3824
pub struct Error(NonZeroU32);
3925

4026
impl Error {
27+
/// An unknown error.
28+
pub const UNKNOWN: Error = Error(unsafe {
29+
NonZeroU32::new_unchecked(CODE_UNKNOWN)
30+
});
31+
32+
/// No generator is available.
33+
pub const UNAVAILABLE: Error = Error(unsafe {
34+
NonZeroU32::new_unchecked(CODE_UNAVAILABLE)
35+
});
36+
4137
/// Extract the error code.
42-
///
38+
///
4339
/// This may equal one of the codes defined in this library or may be a
4440
/// system error code.
45-
///
41+
///
4642
/// One may attempt to format this error via the `Display` implementation.
4743
pub fn code(&self) -> NonZeroU32 {
4844
self.0
4945
}
50-
46+
5147
fn msg(&self) -> Option<&'static str> {
5248
if let Some(msg) = super::error_msg_inner(self.0) {
5349
Some(msg)
5450
} else {
5551
match *self {
56-
ERROR_UNKNOWN => Some("getrandom: unknown error"),
57-
ERROR_UNAVAILABLE => Some("getrandom: unavailable"),
52+
Error::UNKNOWN => Some("getrandom: unknown error"),
53+
Error::UNAVAILABLE => Some("getrandom: unavailable"),
5854
_ => None
5955
}
6056
}
@@ -92,7 +88,7 @@ impl From<io::Error> for Error {
9288
.and_then(|code| NonZeroU32::new(code as u32))
9389
.map(|code| Error(code))
9490
// in practice this should never happen
95-
.unwrap_or(ERROR_UNKNOWN)
91+
.unwrap_or(Error::UNKNOWN)
9692
}
9793
}
9894

@@ -113,7 +109,7 @@ impl error::Error for Error { }
113109
mod tests {
114110
use std::mem::size_of;
115111
use super::Error;
116-
112+
117113
#[test]
118114
fn test_size() {
119115
assert_eq!(size_of::<Error>(), 4);

src/lib.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,30 @@
6767
//! # Error handling
6868
//!
6969
//! We always choose failure over returning insecure "random" bytes. In general,
70-
//! on supported platforms, failure is unlikely, though not impossible. If an
71-
//! error does occur, then it is likely that it will occur on every call to
70+
//! on supported platforms, failure is highly unlikely, though not impossible.
71+
//! If an error does occur, then it is likely that it will occur on every call to
7272
//! `getrandom`, hence after the first successful call one can be reasonably
7373
//! confident that no errors will occur.
74-
//!
75-
//! On unsupported platforms, `getrandom` always fails.
74+
//!
75+
//! On unsupported platforms, `getrandom` always fails with [`Error::UNAVAILABLE`].
76+
//!
77+
//! ## Error codes
78+
//! The crate uses the following custom error codes:
79+
//! - `0x57f4c500` (dec: 1475659008) - an unknown error. Constant:
80+
//! [`Error::UNKNOWN`]
81+
//! - `0x57f4c501` (dec: 1475659009) - no generator is available. Constant:
82+
//! [`Error::UNAVAILABLE`]
83+
//! - `0x57f4c580` (dec: 1475659136) - `self.crypto` is undefined,
84+
//! `wasm-bindgen` specific error.
85+
//! - `0x57f4c581` (dec: 1475659137) - `crypto.getRandomValues` is undefined,
86+
//! `wasm-bindgen` specific error.
87+
//!
88+
//! These codes are provided for reference only and should not be matched upon
89+
//! (but you can match on `Error` constants). The codes may change in future and
90+
//! such change will not be considered a breaking one.
91+
//!
92+
//! Other error codes will originate from an underlying system. In case if such
93+
//! error is encountered, please consult with your system documentation.
7694
//!
7795
//! [1]: http://man7.org/linux/man-pages/man2/getrandom.2.html
7896
//! [2]: http://man7.org/linux/man-pages/man4/urandom.4.html
@@ -128,10 +146,10 @@ extern crate wasm_bindgen;
128146
))]
129147
mod utils;
130148
mod error;
131-
pub use error::{Error, ERROR_UNKNOWN, ERROR_UNAVAILABLE};
149+
pub use error::Error;
132150

133151
// System-specific implementations.
134-
//
152+
//
135153
// These should all provide getrandom_inner with the same signature as getrandom.
136154

137155
macro_rules! mod_use {
@@ -215,12 +233,12 @@ mod_use!(
215233

216234
/// Fill `dest` with random bytes from the system's preferred random number
217235
/// source.
218-
///
236+
///
219237
/// This function returns an error on any failure, including partial reads. We
220238
/// make no guarantees regarding the contents of `dest` on error.
221-
///
239+
///
222240
/// Blocking is possible, at least during early boot; see module documentation.
223-
///
241+
///
224242
/// In general, `getrandom` will be fast enough for interactive usage, though
225243
/// significantly slower than a user-space CSPRNG; for the latter consider
226244
/// [`rand::thread_rng`](https://docs.rs/rand/*/rand/fn.thread_rng.html).

src/sgx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// except according to those terms.
88

99
//! Implementation for SGX using RDRAND instruction
10-
use {Error, ERROR_UNKNOWN};
10+
use Error;
1111

1212
use core::{mem, ptr};
1313
use core::arch::x86_64::_rdrand64_step;
@@ -27,7 +27,7 @@ fn get_rand_u64() -> Result<u64, Error> {
2727
}
2828
};
2929
}
30-
Err(ERROR_UNKNOWN)
30+
Err(Error::UNKNOWN)
3131
}
3232

3333
pub fn getrandom_inner(mut dest: &mut [u8]) -> Result<(), Error> {

src/wasm32_bindgen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ use wasm_bindgen::prelude::*;
1616

1717
use __wbg_shims::*;
1818
use Error;
19+
use error::CODE_PREFIX;
1920
use utils::use_init;
2021

21-
const CODE_PREFIX: u32 = ::error::CODE_PREFIX | 0x8e00;
22-
const CODE_CRYPTO_UNDEF: u32 = CODE_PREFIX | 1;
23-
const CODE_GRV_UNDEF: u32 = CODE_PREFIX | 2;
22+
const CODE_CRYPTO_UNDEF: u32 = CODE_PREFIX | 0x80;
23+
const CODE_GRV_UNDEF: u32 = CODE_PREFIX | 0x81;
2424

2525
#[derive(Clone, Debug)]
2626
pub enum RngSource {

src/wasm32_stdweb.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::num::NonZeroU32;
1515
use stdweb::unstable::TryInto;
1616
use stdweb::web::error::Error as WebError;
1717

18-
use {Error, ERROR_UNAVAILABLE, ERROR_UNKNOWN};
18+
use Error;
1919
use utils::use_init;
2020

2121
#[derive(Clone, Debug)]
@@ -67,7 +67,7 @@ fn getrandom_init() -> Result<RngSource, Error> {
6767
} else {
6868
let err: WebError = js!{ return @{ result }.error }.try_into().unwrap();
6969
error!("getrandom unavailable: {}", err);
70-
Err(ERROR_UNAVAILABLE)
70+
Err(Error::UNAVAILABLE)
7171
}
7272
}
7373

@@ -103,7 +103,7 @@ fn getrandom_fill(source: &mut RngSource, dest: &mut [u8]) -> Result<(), Error>
103103
if js!{ return @{ result.as_ref() }.success } != true {
104104
let err: WebError = js!{ return @{ result }.error }.try_into().unwrap();
105105
error!("getrandom failed: {}", err);
106-
return Err(ERROR_UNKNOWN)
106+
return Err(Error::UNKNOWN)
107107
}
108108
}
109109
Ok(())

0 commit comments

Comments
 (0)