@@ -19,7 +19,7 @@ library like [`rand`].
19
19
20
20
[ `rand` ] : https://crates.io/crates/rand
21
21
22
- ## Usage
22
+ ## Examples
23
23
24
24
Add the ` getrandom ` dependency to your ` Cargo.toml ` file:
25
25
@@ -38,6 +38,16 @@ fn get_random_u128() -> Result<u128, getrandom::Error> {
38
38
}
39
39
```
40
40
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
+
41
51
## Supported targets
42
52
43
53
| Target | Target Triple | Implementation
@@ -106,6 +116,25 @@ WILL NOT have any effect on its downstream users.
106
116
107
117
[ `.cargo/config.toml` ] : https://doc.rust-lang.org/cargo/reference/config.html
108
118
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
+
109
138
### WebAssembly support
110
139
111
140
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.
210
239
Note that doing so will bump minimum supported Linux kernel version to 3.17
211
240
and Android API level to 23 (Marshmallow).
212
241
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
-
238
242
## Error handling
239
243
240
244
We always prioritize failure over returning known insecure "random" bytes.
@@ -346,4 +350,11 @@ dual licensed as above, without any additional terms or conditions.
346
350
[ LICENSE-MIT ] : https://github.com/rust-random/getrandom/blob/master/LICENSE-MIT
347
351
348
352
[ `Error::UNEXPECTED` ] : https://docs.rs/getrandom/latest/getrandom/struct.Error.html#associatedconstant.UNEXPECTED
353
+ [ `fill` ] : https://docs.rs/getrandom/latest/getrandom/fn.fill.html
349
354
[ `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
0 commit comments