Skip to content

Commit 56a5238

Browse files
authored
Merge pull request #333 from pitdicker/osrng_doc
Osrng doc
2 parents cdad51e + 54f9413 commit 56a5238

File tree

3 files changed

+31
-47
lines changed

3 files changed

+31
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ You may also find the [Update Guide](UPDATING.md) useful.
6060
- All PRNGs are now portable across big- and little-endian architectures. (#209)
6161
- `Isaac64Rng::next_u32` no longer throws away half the results. (#209)
6262
- Add `IsaacRng::new_from_u64` and `Isaac64Rng::new_from_u64`. (#209)
63-
- Remove `IsaacWordRng` wrapper. (#277)
6463
- Add the HC-128 CSPRNG `Hc128Rng`. (#210)
6564
- Add `ChaChaRng::set_rounds` method. (#243)
6665
- Changes to `JitterRng` to get its size down from 2112 to 24 bytes. (#251)
@@ -71,6 +70,7 @@ You may also find the [Update Guide](UPDATING.md) useful.
7170
- Remove support for NaCl. (#225)
7271
- WASM support for `OsRng` via stdweb, behind the `stdweb` feature. (#272, #336)
7372
- Use `getrandom` on more platforms for Linux, and on Android. (#338)
73+
- Use the `SecRandomCopyBytes` interface on macOS. (#322)
7474
- On systems that do not have a syscall interface, only keep a single file descriptor open for `OsRng`. (#239)
7575
- On Unix, first try a single read from `/dev/random`, then `/dev/urandom`. (#338)
7676
- Better error handling and reporting in `OsRng` (using new error type). (#225)
@@ -90,7 +90,6 @@ You may also find the [Update Guide](UPDATING.md) useful.
9090
- Use widening multiply method for much faster integer range reduction. (#274)
9191
- `Uniform` distributions for `bool` uses `Range`. (#274)
9292
- `Uniform` distributions for `bool` uses sign test. (#274)
93-
- Add `HighPrecision01` distribution. (#320)
9493

9594

9695
## [0.4.2] - 2018-01-06

UPDATING.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,14 @@ distribution).
226226

227227
The `Open01` and `Closed01` wrappers have been removed. `Rng::gen()` (via
228228
`Uniform`) now yields samples from `(0, 1)` for floats; i.e. the same as the old
229-
`Open01`. This is considered sufficient for most uses. If you require more
230-
precision, use the `HighPrecision01` distribution.
229+
`Open01`. This is considered sufficient for most uses.
231230

232231
#### Uniform distributions
233232

234-
Three new distributions are available:
233+
Two new distributions are available:
235234

236235
- `Uniform` produces uniformly-distributed samples for many different types,
237236
and acts as a replacement for `Rand`
238-
- `HighPrecision01` generates floating-point numbers in the range `[0, 1)`
239-
(similar to `Uniform`) but with as much precision as the floating point
240-
format can represent
241237
- `Alphanumeric` samples `char`s from the ranges `a-z A-Z 0-9`
242238

243239
##### Ranges

src/os.rs

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,44 @@
1414
use std::fmt;
1515
use rand_core::{RngCore, Error, impls};
1616

17-
/// A random number generator that retrieves randomness straight from
18-
/// the operating system.
17+
/// A random number generator that retrieves randomness straight from the
18+
/// operating system. This is the preferred external source of entropy for most
19+
/// applications. Commonly it is used to initialize a user-space RNG, which can
20+
/// then be used to generate random values with much less overhead than `OsRng`.
1921
///
20-
/// Platform sources:
22+
/// You may prefer to use [`EntropyRng`] instead of `OsRng`. Is is unlikely, but
23+
/// not entirely theoretical, for `OsRng` to fail. In such cases `EntropyRng`
24+
/// falls back on a good alternative entropy source.
2125
///
22-
/// - Linux, Android: read from `getrandom(2)` system call if available,
23-
/// otherwise from` /dev/urandom`.
24-
/// - MacOS, iOS: calls SecRandomCopyBytes.
25-
/// - Windows: calls `RtlGenRandom`, exported from `advapi32.dll` as
26-
/// `SystemFunction036`.
26+
/// `OsRng` usually does not block. On some systems, and notably virtual
27+
/// machines, it may block very early in the init process, when the OS CSPRNG
28+
/// has not yet been seeded.
29+
///
30+
/// `OsRng::new()` is guaranteed to be very cheap (after first call), and will
31+
/// never consume more than one file handle per process.
32+
///
33+
/// ## Platform sources:
34+
///
35+
/// - Linux, Android: reads from the `getrandom(2)` system call if available,
36+
/// otherwise from `/dev/urandom`.
37+
/// - macOS, iOS: calls `SecRandomCopyBytes`.
38+
/// - Windows: calls `RtlGenRandom`.
2739
/// - WASM: calls `window.crypto.getRandomValues` in browsers,
28-
/// `require("crypto").randomBytes` in Node.js.
40+
/// and in Node.js `require("crypto").randomBytes`.
2941
/// - OpenBSD: calls `getentropy(2)`.
3042
/// - FreeBSD: uses the `kern.arandom` `sysctl(2)` mib.
3143
/// - Fuchsia: calls `cprng_draw`.
3244
/// - Redox: reads from `rand:` device.
3345
/// - CloudABI: calls `random_get`.
34-
/// - Other Unix-like systems: read directly from `/dev/urandom`.
35-
///
36-
/// This usually does not block. On some systems (e.g. FreeBSD, OpenBSD,
37-
/// Max OS X, and modern Linux) this may block very early in the init
38-
/// process, if the CSPRNG has not been seeded yet.[1]
39-
///
40-
/// *Note*: many Unix systems provide `/dev/random` as well as `/dev/urandom`.
41-
/// This module uses `getrandom` if available, otherwise `/dev/urandom`, for
42-
/// the following reasons:
46+
/// - Other Unix-like systems: reads directly from `/dev/urandom`.
47+
/// Note: many Unix systems provide `/dev/random` as well as `/dev/urandom`.
48+
/// On all modern systems these two interfaces offer identical quality, with
49+
/// the difference that on some systems `/dev/random` may block. This is a
50+
/// dated design, and `/dev/urandom` is preferred by cryptography experts. [1]
4351
///
44-
/// - On Linux, `/dev/random` may block if entropy pool is empty;
45-
/// `/dev/urandom` will not block. This does not mean that `/dev/random`
46-
/// provides better output than `/dev/urandom`; the kernel internally runs a
47-
/// cryptographically secure pseudorandom number generator (CSPRNG) based on
48-
/// entropy pool for random number generation, so the "quality" of
49-
/// `/dev/random` is not better than `/dev/urandom` in most cases. However,
50-
/// this means that `/dev/urandom` can yield somewhat predictable randomness
51-
/// if the entropy pool is very small, such as immediately after first
52-
/// booting. Linux 3.17 added the `getrandom(2)` system call which solves
53-
/// the issue: it blocks if entropy pool is not initialized yet, but it does
54-
/// not block once initialized. `OsRng` tries to use `getrandom(2)` if
55-
/// available, and use `/dev/urandom` fallback if not. If an application
56-
/// does not have `getrandom` and likely to be run soon after first booting,
57-
/// or on a system with very few entropy sources, one should consider using
58-
/// `/dev/random` via `ReadRng`.
59-
/// - On some systems (e.g. FreeBSD, OpenBSD and Mac OS X) there is no
60-
/// difference between the two sources. (Also note that, on some systems
61-
/// e.g. FreeBSD, both `/dev/random` and `/dev/urandom` may block once if
62-
/// the CSPRNG has not seeded yet.)
52+
/// [1] See [Myths about urandom](https://www.2uo.de/myths-about-urandom/).
6353
///
64-
/// [1] See <https://www.python.org/dev/peps/pep-0524/> for a more
65-
/// in-depth discussion.
54+
/// [`EntropyRng`]: struct.EntropyRng.html
6655
6756
#[allow(unused)] // not used by all targets
6857
pub struct OsRng(imp::OsRng);

0 commit comments

Comments
 (0)