|
67 | 67 | //! # Error handling |
68 | 68 | //! |
69 | 69 | //! We always choose failure over returning insecure "random" bytes. In general, |
70 | | -//! on supported platforms, failure is unlikely, though not impossible. If an |
71 | | -//! error does occur, then it is likely that it will occur on every call to |
| 70 | +//! on supported platforms, failure is highly unlikely, though not impossible. |
| 71 | +//! If an error does occur, then it is likely that it will occur on every call to |
72 | 72 | //! `getrandom`, hence after the first successful call one can be reasonably |
73 | 73 | //! confident that no errors will occur. |
74 | | -//! |
75 | | -//! On unsupported platforms, `getrandom` always fails. |
| 74 | +//! |
| 75 | +//! On unsupported platforms, `getrandom` always fails with [`Error::UNAVAILABLE`]. |
| 76 | +//! |
| 77 | +//! ## Error codes |
| 78 | +//! The crate uses the following custom error codes: |
| 79 | +//! - `0x57f4c500` (dec: 1475659008) - an unknown error. Constant: |
| 80 | +//! [`Error::UNKNOWN`] |
| 81 | +//! - `0x57f4c501` (dec: 1475659009) - no generator is available. Constant: |
| 82 | +//! [`Error::UNAVAILABLE`] |
| 83 | +//! - `0x57f4c580` (dec: 1475659136) - `self.crypto` is undefined, |
| 84 | +//! `wasm-bindgen` specific error. |
| 85 | +//! - `0x57f4c581` (dec: 1475659137) - `crypto.getRandomValues` is undefined, |
| 86 | +//! `wasm-bindgen` specific error. |
| 87 | +//! |
| 88 | +//! These codes are provided for reference only and should not be matched upon |
| 89 | +//! (but you can match on `Error` constants). The codes may change in future and |
| 90 | +//! such change will not be considered a breaking one. |
| 91 | +//! |
| 92 | +//! Other error codes will originate from an underlying system. In case if such |
| 93 | +//! error is encountered, please consult with your system documentation. |
76 | 94 | //! |
77 | 95 | //! [1]: http://man7.org/linux/man-pages/man2/getrandom.2.html |
78 | 96 | //! [2]: http://man7.org/linux/man-pages/man4/urandom.4.html |
@@ -128,10 +146,10 @@ extern crate wasm_bindgen; |
128 | 146 | ))] |
129 | 147 | mod utils; |
130 | 148 | mod error; |
131 | | -pub use error::{Error, ERROR_UNKNOWN, ERROR_UNAVAILABLE}; |
| 149 | +pub use error::Error; |
132 | 150 |
|
133 | 151 | // System-specific implementations. |
134 | | -// |
| 152 | +// |
135 | 153 | // These should all provide getrandom_inner with the same signature as getrandom. |
136 | 154 |
|
137 | 155 | macro_rules! mod_use { |
@@ -215,12 +233,12 @@ mod_use!( |
215 | 233 |
|
216 | 234 | /// Fill `dest` with random bytes from the system's preferred random number |
217 | 235 | /// source. |
218 | | -/// |
| 236 | +/// |
219 | 237 | /// This function returns an error on any failure, including partial reads. We |
220 | 238 | /// make no guarantees regarding the contents of `dest` on error. |
221 | | -/// |
| 239 | +/// |
222 | 240 | /// Blocking is possible, at least during early boot; see module documentation. |
223 | | -/// |
| 241 | +/// |
224 | 242 | /// In general, `getrandom` will be fast enough for interactive usage, though |
225 | 243 | /// significantly slower than a user-space CSPRNG; for the latter consider |
226 | 244 | /// [`rand::thread_rng`](https://docs.rs/rand/*/rand/fn.thread_rng.html). |
|
0 commit comments