Skip to content

Commit 83e5337

Browse files
authored
readme: minor text improvements (#535)
1 parent 01ccda5 commit 83e5337

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

README.md

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
`getrandom` is a Rust library for retrieving random data from (operating) system sources.
1111

12-
It is assumed that the system always provides high-quality cryptographically secure random
13-
data, ideally backed by hardware entropy sources. This crate derives its name
14-
from Linux's `getrandom` function, but is cross-platform, roughly supporting
15-
the same set of platforms as Rust's `std` lib.
12+
It is assumed that the system always provides high-quality, cryptographically secure random
13+
data, ideally backed by hardware entropy sources. This crate derives its name from
14+
the Linux `getrandom` syscall but is cross-platform, roughly supporting the same set
15+
of platforms as Rust's `std` library.
1616

17-
This is a low-level API. Most users should prefer using high-level random-number
17+
This is a low-level API. Most users should prefer using a higher-level random-number
1818
library like [`rand`].
1919

2020
[`rand`]: https://crates.io/crates/rand
@@ -73,31 +73,33 @@ Pull Requests that add support for new targets to `getrandom` are always welcome
7373

7474
### Opt-in backends
7575

76-
`getrandom` also provides optional backends which can be enabled using `getrandom_backend`
77-
configuration flag:
76+
`getrandom` also provides optional (opt-in) backends, which allow users to customize the source
77+
of randomness based on their specific needs:
7878

7979
| Backend name | Target | Target Triple | Implementation
8080
| ----------------- | -------------------- | -------------------- | --------------
8181
| `linux_getrandom` | Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call (without `/dev/urandom` fallback). Bumps minimum supported Linux kernel version to 3.17 and Android API level to 23 (Marshmallow).
8282
| `linux_rustix` | Linux, Android | `*‑linux‑*` | Same as `linux_getrandom`, but uses [`rustix`] instead of `libc`.
8383
| `rdrand` | x86, x86-64 | `x86_64-*`, `i686-*` | [`RDRAND`] instruction
8484
| `rndr` | AArch64 | `aarch64-*` | [`RNDR`] register
85-
| `esp_idf` | ESP-IDF | `*‑espidf` | [`esp_fill_random`]. WARNING: can return low quality entropy without proper hardware configuration!
85+
| `esp_idf` | ESP-IDF | `*‑espidf` | [`esp_fill_random`]. WARNING: can return low-quality entropy without proper hardware configuration!
8686
| `wasm_js` | Web Browser, Node.js | `wasm*‑*‑unknown` | [`Crypto.getRandomValues`] if available, then [`crypto.randomFillSync`] if on Node.js (see [WebAssembly support])
8787
| `custom` | All targets | `*` | User-provided custom implementation (see [custom backend])
8888

89-
The configuration flag can be enabled either by specifying the `rustflags` field in
89+
Opt-in backends can be enabled using the `getrandom_backend` configuration flag.
90+
The flag can be set either by specifying the `rustflags` field in
9091
[`.cargo/config.toml`] (note that it can be done on a per-target basis), or by using
91-
`RUSTFLAGS` environment variable:
92+
the `RUSTFLAGS` environment variable:
9293

9394
```sh
9495
RUSTFLAGS='--cfg getrandom_backend="linux_getrandom"' cargo build
9596
```
9697

97-
Enabling an opt-in backend will replace backend used by default. Doing it for a wrong target
98-
(e.g. using `linux_getrandom` while compiling for a Windows target) will result
99-
in a compilation error. Be extremely carefull while using opt-in backends, since incorrect
100-
configuration may result in vulnerable or in always panicking applications.
98+
Enabling an opt-in backend will replace the backend used by default. Doing this for
99+
an incorrect target (e.g. using `linux_getrandom` while compiling for a Windows target)
100+
will result in a compilation error. Be extremely careful while using opt-in backends,
101+
as incorrect configuration may result in vulnerable applications or applications
102+
that always panic.
101103

102104
Note that using an opt-in backend in a library (e.g. for tests or benchmarks)
103105
WILL NOT have any effect on its downstream users.
@@ -138,7 +140,7 @@ This crate will then use the provided `webcrypto` implementation.
138140

139141
### Custom backend
140142

141-
If this crate does not support your target out of box or you have to use
143+
If this crate does not support your target out of the box or you have to use
142144
a non-default entropy source, then you can provide a custom implementation.
143145
You need to enable the custom backend as described in the [configuration flags]
144146
section. Next, you need to define an `extern` function with the following
@@ -156,20 +158,20 @@ unsafe extern "Rust" fn __getrandom_v03_custom(
156158
}
157159
```
158160

159-
This function ideally should be defined in the root crate of your project,
161+
This function should, ideally, be defined in the root crate of your project,
160162
e.g. in your `main.rs`. This function MUST be defined only once for your
161163
project, i.e. upstream library crates SHOULD NOT define it outside of
162164
tests and benchmarks. Improper configuration of this backend may result
163165
in linking errors.
164166

165-
The function accepts pointer to buffer which should be filled with random
166-
data and length in bytes. Note that the buffer MAY be uninitialized.
167-
On success the function should return 0 and fully fill the input buffer,
168-
every other return result will be interpreted as an error code.
167+
The function accepts a pointer to a buffer that should be filled with random
168+
data and its length in bytes. Note that the buffer MAY be uninitialized.
169+
On success, the function should return `Ok(())` and fully fill the input buffer;
170+
otherwise, it should return an error value.
169171

170172
If you are confident that `getrandom` is not used in your project, but
171173
it gets pulled nevertheless by one of your dependencies, then you can
172-
use the following custom backend which always returns "unsupported" error:
174+
use the following custom backend, which always returns the "unsupported" error:
173175
```rust
174176
use getrandom::Error;
175177

@@ -183,29 +185,30 @@ unsafe extern "Rust" fn __getrandom_v03_custom(
183185
```
184186

185187
### Platform Support
188+
186189
This crate generally supports the same operating system and platform versions
187190
that the Rust standard library does. Additional targets may be supported using
188-
pluggable custom implementations.
191+
the opt-in custom backend.
189192

190193
This means that as Rust drops support for old versions of operating systems
191-
(such as old Linux kernel versions, Android API levels, etc) in stable releases,
192-
`getrandom` may create new patch releases (`0.N.x`) that remove support for
194+
(such as old Linux kernel versions, Android API levels, etc.) in stable releases,
195+
`getrandom` may create new patch releases that remove support for
193196
outdated platform versions.
194197

195198
### `/dev/urandom` fallback on Linux and Android
196199

197-
On Linux targets the fallback is present only if either `target_env` is `musl`,
198-
or `target_arch` is one of the following: `aarch64`, `arm`, `powerpc`, `powerpc64`,
199-
`s390x`, `x86`, `x86_64`. Other supported targets [require][platform-support]
200-
kernel versions which support `getrandom` system call, so fallback is not needed.
200+
On Linux targets, the `/dev/urandom` fallback is present only if either `target_env`
201+
is `musl`, or `target_arch` is one of the following: `aarch64`, `arm`, `powerpc`,
202+
`powerpc64`, `s390x`, `x86`, `x86_64`. Other supported targets [require][platform-support]
203+
kernel versions that support the `getrandom` system call, so the fallback is not needed.
201204

202205
On Android targets the fallback is present only for the following `target_arch`es:
203206
`aarch64`, `arm`, `x86`, `x86_64`. Other `target_arch`es (e.g. RISC-V) require
204207
sufficiently high API levels.
205208

206209
The fallback can be disabled by enabling the `linux_getrandom` opt-in backend.
207-
Note that doing so will bump minimum supported Linux kernel version to 3.17 and
208-
Android API level to 23 (Marshmallow).
210+
Note that doing so will bump minimum supported Linux kernel version to 3.17
211+
and Android API level to 23 (Marshmallow).
209212

210213
### Early boot
211214

@@ -234,28 +237,29 @@ with few sources of entropy.
234237

235238
## Error handling
236239

237-
We always choose failure over returning known insecure "random" bytes. In
238-
general, on supported platforms, failure is highly unlikely, though not
239-
impossible. If an error does occur, then it is likely that it will occur
240-
on every call to `getrandom`, hence after the first successful call one
241-
can be reasonably confident that no errors will occur.
240+
We always prioritize failure over returning known insecure "random" bytes.
241+
Generally, on supported platforms, failure is highly unlikely, though not
242+
impossible. If an error does occur, it is likely that it will occur
243+
on every call to `getrandom`. Therefore, after the first successful call,
244+
one can be reasonably confident that no errors will occur.
242245

243246
## Panic handling
244247

245-
We strive to eliminate all potential panics from our implementation.
246-
In other words, when compiled with enabled optimizations, generated
247-
binary code for `getrandom` functions should not contain any panic
248-
branches. Even if platform misbiheaves and returns an unexpected
249-
result, our code should correctly handle it and return an error like
248+
We strive to eliminate all potential panics from our backend implementations.
249+
In other words, when compiled with optimizations enabled, the generated
250+
binary code for `getrandom` functions should not contain any panic branches.
251+
Even if the platform misbehaves and returns an unexpected result,
252+
our code should correctly handle it and return an error, e.g.
250253
[`Error::UNEXPECTED`].
251254

252255
## Sanitizer support
253256

254-
If your code uses [`fill_uninit`] and you use memory sanitizer
255-
(i.e. `-Zsanitizer=memory`), then you need to pass `getrandom_sanitize`
256-
configuration flag for `fill_uninit` to unpoison destination buffer.
257+
If your code uses [`fill_uninit`] and you enable memory sanitization
258+
(i.e. `-Zsanitizer=memory`), you need to pass the `getrandom_sanitize`
259+
configuration flag to enable unpoisoning of the destination buffer
260+
filled by `fill_uninit`.
257261

258-
For example, it can be done like this (requires Nightly compiler):
262+
For example, this can be done as follows (requires a Nightly compiler):
259263
```sh
260264
RUSTFLAGS="-Zsanitizer=memory --cfg getrandom_sanitize" \
261265
cargo test -Zbuild-std --target=x86_64-unknown-linux-gnu

0 commit comments

Comments
 (0)