Skip to content

Commit 2677c49

Browse files
authored
Revise "not a crypto library" policy and SECURITY.md (#1565)
Attempt to slightly improve the wording left by #1514.
1 parent bfd1826 commit 2677c49

File tree

3 files changed

+94
-80
lines changed

3 files changed

+94
-80
lines changed

README.md

+11-15
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ Rand **is not**:
3939
not simplicity. If you prefer a small-and-simple library, there are
4040
alternatives including [fastrand](https://crates.io/crates/fastrand)
4141
and [oorandom](https://crates.io/crates/oorandom).
42-
- A cryptography library. Rand provides functionality for generating
43-
unpredictable random data (potentially applicable depending on requirements)
44-
but does not provide high-level cryptography functionality.
45-
46-
Rand is a community project and cannot provide legally-binding guarantees of
47-
security.
42+
- Primarily a cryptographic library. `rand` does provide some generators which
43+
aim to support unpredictable value generation under certain constraints;
44+
see [SECURITY.md](SECURITY.md) for details.
45+
Users are expected to determine for themselves
46+
whether `rand`'s functionality meets their own security requirements.
4847

4948
Documentation:
5049

@@ -97,16 +96,13 @@ Many (but not all) algorithms are intended to have reproducible output. Read mor
9796

9897
The Rand library supports a variety of CPU architectures. Platform integration is outsourced to [getrandom].
9998

100-
### WASM support
99+
### WebAssembly support
101100

102-
Seeding entropy from OS on WASM target `wasm32-unknown-unknown` is not
103-
*automatically* supported by `rand` or `getrandom`. If you are fine with
104-
seeding the generator manually, you can disable the `os_rng` feature
105-
and use the methods on the `SeedableRng` trait. To enable seeding from OS,
106-
either use a different target such as `wasm32-wasi` or add a direct
107-
dependency on [getrandom] with the `js` feature (if the target supports
108-
JavaScript). See
109-
[getrandom#WebAssembly support](https://docs.rs/getrandom/latest/getrandom/#webassembly-support).
101+
The [WASI](https://github.com/WebAssembly/WASI/tree/main) and Emscripten
102+
targets are directly supported. The `wasm32-unknown-unknown` target is not
103+
*automatically* supported. To enable support for this target, refer to the
104+
[`getrandom` documentation for WebAssembly](https://docs.rs/getrandom/latest/getrandom/#webassembly-support).
105+
Alternatively, the `os_rng` feature may be disabled.
110106

111107
# License
112108

SECURITY.md

+47-39
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,24 @@ security.
1010
### Marker traits
1111

1212
Rand provides the marker traits `CryptoRng`, `TryCryptoRng` and
13-
`CryptoBlockRng`. Generators implementing one of these traits and used in a way
14-
which meets the following additional constraints:
15-
16-
- Instances of seedable RNGs (those implementing `SeedableRng`) are
17-
constructed with cryptographically secure seed values
18-
- The state (memory) of the RNG and its seed value are not exposed
13+
`CryptoBlockRng`. Generators (RNGs) implementing one of these traits which are
14+
used according to these additional constraints:
15+
16+
- The generator may be constructed using `std::default::Default` where the
17+
generator supports this trait. Note that generators should *only* support
18+
`Default` where the `default()` instance is appropriately seeded: for
19+
example `OsRng` has no state and thus has a trivial `default()` instance
20+
while `ThreadRng::default()` returns a handle to a thread-local instance
21+
seeded using `OsRng`.
22+
- The generator may be constructed using `rand_core::SeedableRng` in any of
23+
the following ways where the generator supports this trait:
24+
25+
- Via `SeedableRng::from_seed` using a cryptographically secure seed value
26+
- Via `SeedableRng::from_rng` or `try_from_rng` using a cryptographically
27+
secure source `rng`
28+
- Via `SeedableRng::from_os_rng` or `try_from_os_rng`
29+
- The state (memory) of the generator and its seed value (or source `rng`) are
30+
not exposed
1931

2032
are expected to provide the following:
2133

@@ -34,48 +46,44 @@ are expected to provide the following:
3446
`OsRng` is a stateless "generator" implemented via [getrandom]. As such, it has
3547
no possible state to leak and cannot be improperly seeded.
3648

37-
`ThreadRng` will periodically reseed itself, thus placing an upper bound on the
38-
number of bits of output from an instance before any advantage an attacker may
39-
have gained through state-compromising side-channel attacks is lost.
49+
`StdRng` is a `CryptoRng` and `SeedableRng` using a pseudo-random algorithm
50+
selected for good security and performance qualities. Since it does not offer
51+
reproducibility of output, its algorithm may be changed in any release version.
52+
53+
`ChaCha12Rng` and `ChaCha20Rng` are selected pseudo-random generators
54+
distributed by the `rand` project which meet the requirements of the `CryptoRng`
55+
trait and implement `SeedableRng` with a commitment to reproducibility of
56+
results.
57+
58+
`ThreadRng` is a conveniently-packaged generator over `StdRng` offering
59+
automatic seeding from `OsRng`, periodic reseeding and thread locality.
60+
This random source is intended to offer a good compromise between cryptographic
61+
security, fast generation with reasonably low memory and initialization cost
62+
overheads, and robustness against misuse.
4063

4164
[getrandom]: https://crates.io/crates/getrandom
4265

4366
### Distributions
4467

45-
Additionally, derivations from such an RNG (including the `Rng` trait,
46-
implementations of the `Distribution` trait, and `seq` algorithms) should not
47-
introduce significant bias other than that expected from the operation in
48-
question (e.g. bias from a weighted distribution).
68+
Methods of the `Rng` trait, functionality of the `rand::seq` module and
69+
implementators of the `Distribution` trait are expected, while using a
70+
cryptographically secure `CryptoRng` instance meeting the above constraints,
71+
to not introduce significant bias to their operation beyond what would be
72+
expected of the operation. Note that the usage of 'significant' here permits
73+
some bias, as noted for example in the documentation of the `Uniform`
74+
distribution.
4975

5076
## Supported Versions
5177

52-
We will attempt to uphold these premises in the following crate versions,
53-
provided that only the latest patch version is used, and with potential
54-
exceptions for theoretical issues without a known exploit:
55-
56-
| Crate | Versions | Exceptions |
57-
| ----- | -------- | ---------- |
58-
| `rand` | 0.8 | |
59-
| `rand` | 0.7 | |
60-
| `rand` | 0.5, 0.6 | Jitter |
61-
| `rand` | 0.4 | Jitter, ISAAC |
62-
| `rand_core` | 0.2 - 0.6 | |
63-
| `rand_chacha` | 0.1 - 0.3 | |
78+
We aim to provide security fixes in the form of a new patch version for the
79+
latest release version of `rand` and its dependencies `rand_core` and
80+
`rand_chacha`, as well as for prior major and minor releases which were, at some
81+
time during the previous 12 months, the latest release version.
6482

65-
Explanation of exceptions:
66-
67-
- Jitter: `JitterRng` is used as an entropy source when the primary source
68-
fails; this source may not be secure against side-channel attacks, see #699.
69-
- ISAAC: the [ISAAC](https://burtleburtle.net/bob/rand/isaacafa.html) RNG used
70-
to implement `ThreadRng` is difficult to analyse and thus cannot provide
71-
strong assertions of security.
72-
73-
## Known issues
83+
## Reporting a Vulnerability
7484

75-
In `rand` version 0.3 (0.3.18 and later), if `OsRng` fails, `ThreadRng` is
76-
seeded from the system time in an insecure manner.
85+
If you have discovered a security vulnerability in this project, please report it privately. **Do not disclose it as a public issue.** This gives us time to work with you to fix the issue before public exposure, reducing the chance that the exploit will be used before a patch is released.
7786

78-
## Reporting a Vulnerability
87+
Please disclose it at [security advisory](https://github.com/rust-random/rand/security/advisories/new).
7988

80-
To report a vulnerability, [open a new issue](https://github.com/rust-random/rand/issues/new).
81-
Once the issue is resolved, the vulnerability should be [reported to RustSec](https://github.com/RustSec/advisory-db/blob/master/CONTRIBUTING.md).
89+
This project is maintained by a team of volunteers on a reasonable-effort basis. As such, please give us at least 90 days to work on a fix before public exposure.

rand_core/src/lib.rs

+36-26
Original file line numberDiff line numberDiff line change
@@ -175,32 +175,32 @@ where
175175
}
176176
}
177177

178-
/// A marker trait used to indicate that an [`RngCore`] implementation is
179-
/// supposed to be cryptographically secure.
180-
///
181-
/// *Cryptographically secure generators*, also known as *CSPRNGs*, should
182-
/// satisfy an additional properties over other generators: given the first
183-
/// *k* bits of an algorithm's output
178+
/// A marker trait over [`RngCore`] for securely unpredictable RNGs
179+
///
180+
/// This marker trait indicates that the implementing generator is intended,
181+
/// when correctly seeded and protected from side-channel attacks such as a
182+
/// leaking of state, to be a cryptographically secure generator. This trait is
183+
/// provided as a tool to aid review of cryptographic code, but does not by
184+
/// itself guarantee suitability for cryptographic applications.
185+
///
186+
/// Implementors of `CryptoRng` automatically implement the [`TryCryptoRng`]
187+
/// trait.
188+
///
189+
/// Implementors of `CryptoRng` should only implement [`Default`] if the
190+
/// `default()` instances are themselves secure generators: for example if the
191+
/// implementing type is a stateless interface over a secure external generator
192+
/// (like [`OsRng`]) or if the `default()` instance uses a strong, fresh seed.
193+
///
194+
/// Formally, a CSPRNG (Cryptographically Secure Pseudo-Random Number Generator)
195+
/// should satisfy an additional property over other generators: assuming that
196+
/// the generator has been appropriately seeded and has unknown state, then
197+
/// given the first *k* bits of an algorithm's output
184198
/// sequence, it should not be possible using polynomial-time algorithms to
185199
/// predict the next bit with probability significantly greater than 50%.
186200
///
187-
/// Some generators may satisfy an additional property, however this is not
188-
/// required by this trait: if the CSPRNG's state is revealed, it should not be
189-
/// computationally-feasible to reconstruct output prior to this. Some other
190-
/// generators allow backwards-computation and are considered *reversible*.
191-
///
192-
/// Note that this trait is provided for guidance only and cannot guarantee
193-
/// suitability for cryptographic applications. In general it should only be
194-
/// implemented for well-reviewed code implementing well-regarded algorithms.
195-
///
196-
/// Note also that use of a `CryptoRng` does not protect against other
197-
/// weaknesses such as seeding from a weak entropy source or leaking state.
198-
///
199-
/// Note that implementors of [`CryptoRng`] also automatically implement
200-
/// the [`TryCryptoRng`] trait.
201-
///
202-
/// [`BlockRngCore`]: block::BlockRngCore
203-
/// [`Infallible`]: core::convert::Infallible
201+
/// An optional property of CSPRNGs is backtracking resistance: if the CSPRNG's
202+
/// state is revealed, it will not be computationally-feasible to reconstruct
203+
/// prior output values. This property is not required by `CryptoRng`.
204204
pub trait CryptoRng: RngCore {}
205205

206206
impl<T: DerefMut> CryptoRng for T where T::Target: CryptoRng {}
@@ -269,10 +269,20 @@ impl<R: RngCore> TryRngCore for R {
269269
}
270270
}
271271

272-
/// A marker trait used to indicate that a [`TryRngCore`] implementation is
273-
/// supposed to be cryptographically secure.
272+
/// A marker trait over [`TryRngCore`] for securely unpredictable RNGs
273+
///
274+
/// This trait is like [`CryptoRng`] but for the trait [`TryRngCore`].
275+
///
276+
/// This marker trait indicates that the implementing generator is intended,
277+
/// when correctly seeded and protected from side-channel attacks such as a
278+
/// leaking of state, to be a cryptographically secure generator. This trait is
279+
/// provided as a tool to aid review of cryptographic code, but does not by
280+
/// itself guarantee suitability for cryptographic applications.
274281
///
275-
/// See [`CryptoRng`] docs for more information about cryptographically secure generators.
282+
/// Implementors of `TryCryptoRng` should only implement [`Default`] if the
283+
/// `default()` instances are themselves secure generators: for example if the
284+
/// implementing type is a stateless interface over a secure external generator
285+
/// (like [`OsRng`]) or if the `default()` instance uses a strong, fresh seed.
276286
pub trait TryCryptoRng: TryRngCore {}
277287

278288
impl<R: CryptoRng> TryCryptoRng for R {}

0 commit comments

Comments
 (0)