Skip to content

Commit 18d6c13

Browse files
committed
Tweak docs
1 parent c79a8bd commit 18d6c13

File tree

2 files changed

+52
-26
lines changed

2 files changed

+52
-26
lines changed

README.md

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ library like [`rand`].
1919

2020
[`rand`]: https://crates.io/crates/rand
2121

22-
## Usage
22+
## Examples
2323

2424
Add the `getrandom` dependency to your `Cargo.toml` file:
2525

@@ -38,6 +38,16 @@ fn get_random_u128() -> Result<u128, getrandom::Error> {
3838
}
3939
```
4040

41+
The crate also support direct generation of random `u32` and `u64` values:
42+
43+
```rust
44+
fn get_rand_u32_u64() -> Result<(u32, u64), getrandom::Error> {
45+
let a = getrandom::u32()?;
46+
let b = getrandom::u64()?;
47+
Ok((a, b))
48+
}
49+
```
50+
4151
## Supported targets
4252

4353
| Target | Target Triple | Implementation
@@ -106,6 +116,25 @@ WILL NOT have any effect on its downstream users.
106116

107117
[`.cargo/config.toml`]: https://doc.rust-lang.org/cargo/reference/config.html
108118

119+
### "Insecure" functions
120+
121+
Sometimes, early in the boot process, the OS has not collected enough
122+
entropy to securely seed its RNG. This is especially common on virtual
123+
machines, where standard "random" events are hard to come by.
124+
125+
Some operating system interfaces always block until the RNG is securely
126+
seeded, which can take anywhere from a few seconds to more than a minute.
127+
Some platforms offer a choice between blocking and getting potentially less
128+
secure randomness, i.e. generated data may be less difficult for an attacker
129+
to predict.
130+
131+
We expose this functionality through two sets of functions. The "secure"
132+
functions ([`fill`], [`fill_uninit`], [`u32`], and [`u64`]) may block but
133+
always return "secure" randomness suitable for cryptographic needs.
134+
Meanwhile, their "insecure" counterparts ([`insecure_fill`],
135+
[`insecure_fill_uninit`], [`insecure_u32`], and [`insecure_u64`]) may return
136+
randomness of lesser quality but are less likely to block in entropy-starved scenarios.
137+
109138
### WebAssembly support
110139

111140
This crate fully supports the [WASI] and [Emscripten] targets. However,
@@ -210,31 +239,6 @@ The fallback can be disabled by enabling the `linux_getrandom` opt-in backend.
210239
Note that doing so will bump minimum supported Linux kernel version to 3.17
211240
and Android API level to 23 (Marshmallow).
212241

213-
### Early boot
214-
215-
Sometimes, early in the boot process, the OS has not collected enough
216-
entropy to securely seed its RNG. This is especially common on virtual
217-
machines, where standard "random" events are hard to come by.
218-
219-
Some operating system interfaces always block until the RNG is securely
220-
seeded. This can take anywhere from a few seconds to more than a minute.
221-
A few (Linux, NetBSD and Solaris) offer a choice between blocking and
222-
getting an error; in these cases, we always choose to block.
223-
224-
On Linux (when the `getrandom` system call is not available), reading from
225-
`/dev/urandom` never blocks, even when the OS hasn't collected enough
226-
entropy yet. To avoid returning low-entropy bytes, we first poll
227-
`/dev/random` and only switch to `/dev/urandom` once this has succeeded.
228-
229-
On OpenBSD, this kind of entropy accounting isn't available, and on
230-
NetBSD, blocking on it is discouraged. On these platforms, nonblocking
231-
interfaces are used, even when reliable entropy may not be available.
232-
On the platforms where it is used, the reliability of entropy accounting
233-
itself isn't free from controversy. This library provides randomness
234-
sourced according to the platform's best practices, but each platform has
235-
its own limits on the grade of randomness it can promise in environments
236-
with few sources of entropy.
237-
238242
## Error handling
239243

240244
We always prioritize failure over returning known insecure "random" bytes.
@@ -346,4 +350,11 @@ dual licensed as above, without any additional terms or conditions.
346350
[LICENSE-MIT]: https://github.com/rust-random/getrandom/blob/master/LICENSE-MIT
347351

348352
[`Error::UNEXPECTED`]: https://docs.rs/getrandom/latest/getrandom/struct.Error.html#associatedconstant.UNEXPECTED
353+
[`fill`]: https://docs.rs/getrandom/latest/getrandom/fn.fill.html
349354
[`fill_uninit`]: https://docs.rs/getrandom/latest/getrandom/fn.fill_uninit.html
355+
[`u32`]: https://docs.rs/getrandom/latest/getrandom/fn.u32.html
356+
[`u64`]: https://docs.rs/getrandom/latest/getrandom/fn.u64.html
357+
[`insecure_fill`]: https://docs.rs/getrandom/latest/getrandom/fn.insecure_fill.html
358+
[`insecure_fill_uninit`]: https://docs.rs/getrandom/latest/getrandom/fn.insecure_fill_uninit.html
359+
[`insecure_u32`]: https://docs.rs/getrandom/latest/getrandom/fn.insecure_u32.html
360+
[`insecure_u64`]: https://docs.rs/getrandom/latest/getrandom/fn.insecure_u64.html

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
// Overwrite links to crate items with intra-crate links
22
//! [`Error::UNEXPECTED`]: Error::UNEXPECTED
3+
//! [`fill`]: fill
34
//! [`fill_uninit`]: fill_uninit
5+
//! [`u32`]: u32()
6+
//! [`u64`]: u64()
7+
//! [`insecure_fill`]: insecure_fill
8+
//! [`insecure_fill_uninit`]: insecure_fill_uninit
9+
//! [`insecure_u32`]: insecure_u32
10+
//! [`insecure_u64`]: insecure_u64
411
512
#![no_std]
613
#![doc(
@@ -74,6 +81,8 @@ pub fn fill(dst: &mut [u8]) -> Result<(), Error> {
7481

7582
/// Fill `dst` with **potentially insecure** random bytes from the system's entropy source.
7683
///
84+
/// See the ["insecure" functions][crate#insecure-functions] section for more information.
85+
///
7786
/// # Examples
7887
/// ```
7988
/// # fn main() -> Result<(), getrandom::Error> {
@@ -132,6 +141,8 @@ pub fn fill_uninit(dst: &mut [MaybeUninit<u8>]) -> Result<&mut [u8], Error> {
132141
/// No part of `dst` will ever be de-initialized at any point, regardless
133142
/// of what is returned.
134143
///
144+
/// See the ["insecure" functions][crate#insecure-functions] section for more information.
145+
///
135146
/// # Examples
136147
/// ```ignore
137148
/// # // We ignore this test since `uninit_array` is unstable.
@@ -167,6 +178,8 @@ pub fn u32() -> Result<u32, Error> {
167178

168179
/// Get **potentially insecure** random `u32` from the system's entropy source.
169180
///
181+
/// See the ["insecure" functions][crate#insecure-functions] section for more information.
182+
///
170183
/// # Examples
171184
/// ```
172185
/// # fn main() -> Result<(), getrandom::Error> {
@@ -193,6 +206,8 @@ pub fn u64() -> Result<u64, Error> {
193206

194207
/// Get **potentially insecure** random `u64` from the system's entropy source.
195208
///
209+
/// See the ["insecure" functions][crate#insecure-functions] section for more information.
210+
///
196211
/// # Examples
197212
/// ```
198213
/// # fn main() -> Result<(), getrandom::Error> {

0 commit comments

Comments
 (0)