Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
+1
@@ -0,0 +1 @@
|
||||
{"$comment":"This file only protects against accidental modifications. It is not a security mechanism and does not protect against malicious changes.","files":{".cargo_vcs_info.json":"14b81d3c44670e34376d60d06a5e24a78548bb3ad32be81e6ecee66a36de096d","CHANGELOG.md":"c1cfcc3d23705b5f2c921c65534246baf66c55a4438cd7b937b0393305bace67","Cargo.lock":"743068b6375eb32c15b242739227028eb2f6b995d5e8a02feef8b9a9e52ea212","Cargo.toml":"4f107fe4ceacee71d0068cfbbc69554b3d8523a1fcbb99b17e9fa0d008961ff9","Cargo.toml.orig":"eebe7c6d086d3f40c2fa79f8784b00c36dc501e49b2757a1caa97e7fa4dbacf7","LICENSE-APACHE":"aaff376532ea30a0cd5330b9502ad4a4c8bf769c539c87ffe78819d188a18ebf","LICENSE-MIT":"523a42c25d245dde9c015f882cec7f4555aad883382a6cf19b4b7d9b2cd5419b","README.md":"700180aadb3c22383920a2c9dd2fa7fedaf2e5992a0924a061ae09aae19cc69e","SECURITY.md":"816ea79f8c7937888ab5a972a1efb270c4bada028b448953a195359fe11d526e","benches/buffer.rs":"cae45dc9e4fa0db8206f76a0a9b4b7a05f9cd0eda33d42f29887ca76f949522c","build.rs":"662faa06d7b2a9c5bea2c17a127a90e00b9f9b31105b0360da516e47cf816e95","src/backends.rs":"4eeedf166b675594c32254a2ee99bf93ff8bc4e7f37f5bdfc33fb56e666fc17d","src/backends/apple_other.rs":"f8fa784588f347cfd7c2b5f37a96b23d446f95c3157668069d7146e281fa783b","src/backends/custom.rs":"448b44a9800b3be95f91a85e35f1f313894bbd4e64e3751afaecf15b3f3532b3","src/backends/efi_rng.rs":"b22e505a5e85eadf1bc0bab8f7e96f226109a5cd3fb289dee06e7ed5a36ac1ef","src/backends/esp_idf.rs":"98d4997dcf7c0869883a617f8b5f341f9c97f4e21365c68de97db783215ae864","src/backends/extern_impl.rs":"0a13d8e059ee5203cf414101017a92d9f7289f86451a23d872c471b24279ece1","src/backends/fuchsia.rs":"4404823618bf1ac30f998a080cf46712596871de954fc0681cd8328b2e598e78","src/backends/getentropy.rs":"10f9486239aea0506eedcb13b553ed064addf27a902d8e19728a811a763dbcc7","src/backends/getrandom.rs":"da934695a424479f4eb80ba3a2aab97316f3bb6ed158523160eae1e3670e2b40","src/backends/hermit.rs":"f485c6431111c0867b1ebb76ce592a8502c4d88f07ea1a9c0650d235298518af","src/backends/linux_android_with_fallback.rs":"78a5c62a17d654cccb66bc0772c7017e57b0b6451e0d7135f834917456f343fb","src/backends/linux_raw.rs":"94f6a494edd6ab99eca1989294b1064b58ba502f401b642aedbc3b97e98c0bf7","src/backends/netbsd.rs":"a4fe488579d20976d4288a18d7059cc2d6cf662f55a95ab369e9866ef8ed8ce6","src/backends/rdrand.rs":"013c90b04f882ea82a73cc82b8d282227e2cfe9fe7d4f8eae9020a1b17aeb3b1","src/backends/rndr.rs":"edded389094deef0207fdf08acdf9c2e10d9ae8576cc64f7af21333cab0d0448","src/backends/solaris.rs":"8f3e51f90f56b64c4c1376cdd5399f1fcac5a19d3a88c7ec04f355c1b543bcaa","src/backends/solid.rs":"c89c809996aa656e2ef13b0753e7c7f742a047b9e72174806467d27946d88385","src/backends/unsupported.rs":"a531ab9883b7d7b584c6f37a5c13543ec63d25d3acc36608d57d5d82ef44cbff","src/backends/use_file.rs":"37dfc2ed2a454064196a2a268fc3bc90318ea1db11a9e24a77d53474c5001d70","src/backends/vxworks.rs":"2e10e1c1039e353353cf7bbeab8c9b2b33368ca28a0b1b1557e8a86e1b705471","src/backends/wasi_p1.rs":"5bf346a3f2e654243bddab1a323f7aa07dee2636d35308d5fac9038ac862bf17","src/backends/wasi_p2_3.rs":"48c183d7048db5cd36a9c3339a3380a09d85b8b74690ca3f69969f68b083e691","src/backends/wasm_js.rs":"4a9bf118575514709adf807cf6a4bd4e248b0e02f2a292bd32a01af2ad99e51c","src/backends/windows.rs":"814c83c3142e521b5d1d1f03743585328ffe385373b6cdcca58ee11b693b3fb8","src/backends/windows_legacy.rs":"d4619333768daf09c1abf28b08a972a9c6c1e2e57dca9c153821e27feb62b9ed","src/error.rs":"529e5d8e3b119bc8f1cb1cde844c667a321d7f2cda764e5a1b7459d75c7050a3","src/error_std_impls.rs":"32a3469b0a1291f2182d9bd98b9bdb6232ed259b7747b3665cbe8c3124483957","src/lib.rs":"68bd1f2daa0aec1b55da25c07744869728a8be25d7c7d127bc788cd50fd8687b","src/sys_rng.rs":"ff110b2095252c34270d1c88b600e261eb4c2e96cb79b6e6512b41c37529d7eb","src/util.rs":"b1f6bb46c35e1bc71faed96aaaac82be99df636b2e6bdcd676e898a669dee37f","src/utils/get_errno.rs":"d5afe0e5e41a32a85105fb5242156fc1c15096fd0ebb20363cb3368a5e63c7b2","src/utils/lazy_bool.rs":"573d5575c859c816262f44875f36c6aa8c58ecd5b2b1cf484d2415a6a41957d7","src/utils/lazy_ptr.rs":"5c0c29f1d636f303f65be626c998f0ef7f3e3329d6be1696479af07e53370920","src/utils/sanitizer.rs":"f9db2840b940d620efef7c5b6ab3a25ba24c46a15fdb5aecb37a22ed249d4bad","src/utils/sys_fill_exact.rs":"d18e26399844e96b0a8e6ff0e82a9d7d3ee315e97e31eb51498430b201cb768c","tests/mod.rs":"6f8ffe1b36d104906e5ed6f8f189c2c51e6b6135a564d218609d3b74a9ac6c67","tests/sys_rng.rs":"25aab3742e8c927a2f15a668d5d0808d4fd54cf69c07be9ea794d5bf30f66466"},"package":"300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"git": {
|
||||
"sha1": "5e7cd5733536844a9856dc7259bd4696bbe5e3ae"
|
||||
},
|
||||
"path_in_vcs": ""
|
||||
}
|
||||
Vendored
+741
@@ -0,0 +1,741 @@
|
||||
# Changelog
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [0.4.3] - 2026-06-17
|
||||
### Added
|
||||
- `wasm64-unknown-unknown` target support for `wasm_js` backend [#848]
|
||||
|
||||
### Changed
|
||||
- Drop `wasip2` and `wasip3` dependencies in favor of manual bindings [#830]
|
||||
|
||||
[0.4.3]: https://github.com/rust-random/getrandom/compare/v0.4.2...v0.4.3
|
||||
[#830]: https://github.com/rust-random/getrandom/pull/830
|
||||
[#848]: https://github.com/rust-random/getrandom/pull/848
|
||||
|
||||
## [0.4.2] - 2026-03-03
|
||||
|
||||
### Changed
|
||||
- Bump `r-efi` dependency to v6 [#814]
|
||||
|
||||
### Fixed
|
||||
- Read `errno` only when it is set [#810]
|
||||
- Check the return value of `ProcessPrng` on Windows [#811]
|
||||
|
||||
[0.4.2]: https://github.com/rust-random/getrandom/compare/v0.4.1...v0.4.2
|
||||
[#810]: https://github.com/rust-random/getrandom/pull/810
|
||||
[#811]: https://github.com/rust-random/getrandom/pull/811
|
||||
[#814]: https://github.com/rust-random/getrandom/pull/814
|
||||
|
||||
## [0.4.1] - 2026-02-03
|
||||
|
||||
### Fixed
|
||||
- Documentation build on docs.rs [#801]
|
||||
|
||||
[0.4.1]: https://github.com/rust-random/getrandom/compare/v0.4.0...v0.4.1
|
||||
[#801]: https://github.com/rust-random/getrandom/pull/801
|
||||
|
||||
## [0.4.0] - 2026-02-02
|
||||
|
||||
### Added
|
||||
- `RawOsError` type alias [#739]
|
||||
- `SysRng` behind new feature `sys_rng` [#751]
|
||||
- WASIp3 support [#779]
|
||||
- `extern_impl` opt-in backend [#786] [#794]
|
||||
- Motor OS support [#797]
|
||||
|
||||
### Changed
|
||||
- Use Edition 2024 and MSRV 1.85 [#749]
|
||||
|
||||
[0.4.0]: https://github.com/rust-random/getrandom/compare/v0.3.4...v0.4.0
|
||||
[#739]: https://github.com/rust-random/getrandom/pull/739
|
||||
[#749]: https://github.com/rust-random/getrandom/pull/749
|
||||
[#751]: https://github.com/rust-random/getrandom/pull/751
|
||||
[#779]: https://github.com/rust-random/getrandom/pull/779
|
||||
[#786]: https://github.com/rust-random/getrandom/pull/786
|
||||
[#794]: https://github.com/rust-random/getrandom/pull/794
|
||||
[#797]: https://github.com/rust-random/getrandom/pull/797
|
||||
|
||||
## [0.3.4] - 2025-10-14
|
||||
|
||||
### Major change to `wasm_js` backend
|
||||
|
||||
Now, when the `wasm_js` feature is enabled, the `wasm_js` backend will be used
|
||||
by default. Users of `wasm32-unknown-unknown` targeting JavaScript environments
|
||||
like the Web and Node.js will no longer need to specify:
|
||||
```
|
||||
--cfg getrandom_backend="wasm_js"
|
||||
```
|
||||
in `RUSTFLAGS` for the crate to compile. They can now simple enable a feature.
|
||||
|
||||
Note: this should not affect non-JS users of the `wasm32-unknown-unknown`
|
||||
target. Using `--cfg getrandom_backend` will still override the source of
|
||||
randomness _even if_ the `wasm_js` feature is enabled. This includes
|
||||
`--cfg getrandom_backend=custom` and `--cfg getrandom_backend=unsupported`.
|
||||
|
||||
For more information, see the discussions in [#671], [#675], and [#730].
|
||||
|
||||
### Added
|
||||
- `unsupported` opt-in backend [#667]
|
||||
- `windows_legacy` opt-in backend [#724]
|
||||
|
||||
### Changed
|
||||
- Implement Memory Sanitizer unpoisoning more precisely [#678]
|
||||
- Relax MSRV for the `linux_raw` opt-in backend on ARM targets [#688]
|
||||
- Use `getrandom` syscall on all RISC-V Linux targets [#699]
|
||||
- Replaced `wasi` dependency with `wasip2` [#721]
|
||||
- Enable `wasm_js` backend by default if the `wasm_js` feature is enabled [#730]
|
||||
|
||||
### Removed
|
||||
- Unstable `rustc-dep-of-std` crate feature [#694]
|
||||
|
||||
[0.3.4]: https://github.com/rust-random/getrandom/compare/v0.3.3...v0.3.4
|
||||
[#667]: https://github.com/rust-random/getrandom/pull/667
|
||||
[#671]: https://github.com/rust-random/getrandom/issues/671
|
||||
[#675]: https://github.com/rust-random/getrandom/pull/675
|
||||
[#678]: https://github.com/rust-random/getrandom/pull/678
|
||||
[#688]: https://github.com/rust-random/getrandom/pull/688
|
||||
[#694]: https://github.com/rust-random/getrandom/pull/694
|
||||
[#699]: https://github.com/rust-random/getrandom/pull/699
|
||||
[#721]: https://github.com/rust-random/getrandom/pull/721
|
||||
[#724]: https://github.com/rust-random/getrandom/pull/724
|
||||
[#730]: https://github.com/rust-random/getrandom/pull/730
|
||||
|
||||
## [0.3.3] - 2025-05-09
|
||||
|
||||
### Changed
|
||||
- Doc improvements [#632] [#634] [#635]
|
||||
- Add crate version to docs.rs links used in `compile_error!`s [#639]
|
||||
|
||||
### Fixed
|
||||
- Error handling in WASI p1 [#661]
|
||||
|
||||
[0.3.3]: https://github.com/rust-random/getrandom/compare/v0.3.2...v0.3.3
|
||||
[#632]: https://github.com/rust-random/getrandom/pull/632
|
||||
[#634]: https://github.com/rust-random/getrandom/pull/634
|
||||
[#635]: https://github.com/rust-random/getrandom/pull/635
|
||||
[#639]: https://github.com/rust-random/getrandom/pull/639
|
||||
[#661]: https://github.com/rust-random/getrandom/pull/661
|
||||
|
||||
## [0.3.2] - 2025-03-17
|
||||
|
||||
### Added
|
||||
- `efi_rng` opt-in backend [#570]
|
||||
- `linux_raw` opt-in backend [#572]
|
||||
- `.cargo/config.toml` example in the crate-level docs [#591]
|
||||
- `getrandom_test_linux_without_fallback` configuration flag to test that file fallback
|
||||
is not triggered in the `linux_android_with_fallback` backend [#605]
|
||||
- Built-in support for `*-linux-none` targets [#618]
|
||||
- Cygwin support [#626]
|
||||
|
||||
### Changed
|
||||
- Update `wasi` dependency to v0.14 [#594]
|
||||
- Add `#[inline]` attribute to the inner functions [#596]
|
||||
- Update WASI and Emscripten links in the crate-level docs [#597]
|
||||
- Do not use `dlsym` on MUSL targets in the `linux_android_with_fallback` backend [#602]
|
||||
- Remove `linux_android.rs` and use `getrandom.rs` instead [#603]
|
||||
- Always use `RtlGenRandom` on Windows targets when compiling with pre-1.78 Rust [#610]
|
||||
- Internal representation of the `Error` type [#614]
|
||||
- Remove `windows-targets` dependency and use [`raw-dylib`] directly [#627]
|
||||
|
||||
### Removed
|
||||
- `Error::INTERNAL_START` and `Error::CUSTOM_START` associated constants [#614]
|
||||
|
||||
[0.3.2]: https://github.com/rust-random/getrandom/compare/v0.3.1...v0.3.2
|
||||
[#570]: https://github.com/rust-random/getrandom/pull/570
|
||||
[#572]: https://github.com/rust-random/getrandom/pull/572
|
||||
[#591]: https://github.com/rust-random/getrandom/pull/591
|
||||
[#594]: https://github.com/rust-random/getrandom/pull/594
|
||||
[#596]: https://github.com/rust-random/getrandom/pull/596
|
||||
[#597]: https://github.com/rust-random/getrandom/pull/597
|
||||
[#602]: https://github.com/rust-random/getrandom/pull/602
|
||||
[#603]: https://github.com/rust-random/getrandom/pull/603
|
||||
[#605]: https://github.com/rust-random/getrandom/pull/605
|
||||
[#610]: https://github.com/rust-random/getrandom/pull/610
|
||||
[#614]: https://github.com/rust-random/getrandom/pull/614
|
||||
[#618]: https://github.com/rust-random/getrandom/pull/618
|
||||
[#626]: https://github.com/rust-random/getrandom/pull/626
|
||||
[#627]: https://github.com/rust-random/getrandom/pull/627
|
||||
[`raw-dylib`]: https://doc.rust-lang.org/reference/items/external-blocks.html?highlight=link#dylib-versus-raw-dylib
|
||||
|
||||
## [0.3.1] - 2025-01-28
|
||||
|
||||
### Fixed
|
||||
- Build error on Android [#588]
|
||||
|
||||
[0.3.1]: https://github.com/rust-random/getrandom/compare/v0.3.0...v0.3.1
|
||||
[#588]: https://github.com/rust-random/getrandom/pull/588
|
||||
|
||||
## [0.3.0] - 2025-01-25
|
||||
|
||||
### Breaking Changes
|
||||
|
||||
#### Changed
|
||||
- Bump MSRV to 1.63 [#542]
|
||||
- Rename `getrandom` and `getrandom_uninit` functions to `fill` and `fill_uninit` respectively [#532]
|
||||
|
||||
#### Removed
|
||||
- `wasm32-wasi` target support (use `wasm32-wasip1` or `wasm32-wasip2` instead) [#499]
|
||||
- `linux_disable_fallback`, `rdrand`, `js`, `test-in-browser`, and `custom` crate features
|
||||
in favor of configuration flags [#504]
|
||||
- `register_custom_getrandom!` macro [#504]
|
||||
- Implementation of `From<NonZeroU32>` for `Error` and `Error::code` method [#507]
|
||||
- Internet Explorer 11 support [#554]
|
||||
- Target-specific associated `Error` constants [#562]
|
||||
|
||||
### Changed
|
||||
- Use `ProcessPrng` on Windows 10 and up, and use `RtlGenRandom` on older Windows versions [#415]
|
||||
- Do not use locale-specific `strerror_r` for retrieving error code descriptions [#440]
|
||||
- Avoid assuming `usize` is the native word size in the `rdrand` backend [#442]
|
||||
- Do not read from `errno` when `libc` did not indicate error on Solaris [#448]
|
||||
- Switch from `libpthread`'s mutex to `futex` on Linux and to `nanosleep`-based wait loop
|
||||
on other targets in the `use_file` backend [#490]
|
||||
- Do not retry on `EAGAIN` while polling `/dev/random` on Linux [#522]
|
||||
- Remove separate codepath for Node.js in the `wasm_js` backend
|
||||
(bumps minimum supported Node.js version to v19) [#557]
|
||||
- Use `js_namespace` in the `wasm_js` backend [#559]
|
||||
|
||||
### Added
|
||||
- `wasm32-wasip1` and `wasm32-wasip2` support [#499]
|
||||
- `getrandom_backend` configuration flag for selection of opt-in backends [#504]
|
||||
- `Error::new_custom` method [#507]
|
||||
- `rndr` opt-in backend [#512]
|
||||
- Automatic MemorySanitizer support [#521] [#571]
|
||||
- `u32` and `u64` functions for generating random values of the respective type [#544]
|
||||
- `wasm32v1-none` support in the `wasm_js` backend [#560]
|
||||
- `wasm_js` crate feature which allows users to enable the `wasm_js` opt-in backend [#574]
|
||||
|
||||
### Fixed
|
||||
- NetBSD fallback code based on `KERN_ARND` [#555]
|
||||
|
||||
[0.3.0]: https://github.com/rust-random/getrandom/compare/v0.2.15...v0.3.0
|
||||
[#415]: https://github.com/rust-random/getrandom/pull/415
|
||||
[#440]: https://github.com/rust-random/getrandom/pull/440
|
||||
[#442]: https://github.com/rust-random/getrandom/pull/442
|
||||
[#448]: https://github.com/rust-random/getrandom/pull/448
|
||||
[#490]: https://github.com/rust-random/getrandom/pull/490
|
||||
[#499]: https://github.com/rust-random/getrandom/pull/499
|
||||
[#504]: https://github.com/rust-random/getrandom/pull/504
|
||||
[#507]: https://github.com/rust-random/getrandom/pull/507
|
||||
[#512]: https://github.com/rust-random/getrandom/pull/512
|
||||
[#521]: https://github.com/rust-random/getrandom/pull/521
|
||||
[#522]: https://github.com/rust-random/getrandom/pull/522
|
||||
[#532]: https://github.com/rust-random/getrandom/pull/532
|
||||
[#542]: https://github.com/rust-random/getrandom/pull/542
|
||||
[#544]: https://github.com/rust-random/getrandom/pull/544
|
||||
[#554]: https://github.com/rust-random/getrandom/pull/554
|
||||
[#555]: https://github.com/rust-random/getrandom/pull/555
|
||||
[#557]: https://github.com/rust-random/getrandom/pull/557
|
||||
[#559]: https://github.com/rust-random/getrandom/pull/559
|
||||
[#560]: https://github.com/rust-random/getrandom/pull/560
|
||||
[#562]: https://github.com/rust-random/getrandom/pull/562
|
||||
[#571]: https://github.com/rust-random/getrandom/pull/571
|
||||
[#574]: https://github.com/rust-random/getrandom/pull/574
|
||||
|
||||
## [0.2.17] - 2026-01-12
|
||||
### Fixed
|
||||
- Use `doc_cfg` instead of `doc_auto_cfg` (partial backport of [#732]) [#768]
|
||||
- `BCryptGenRandom` signature [#778]
|
||||
|
||||
[0.2.17]: https://github.com/rust-random/getrandom/compare/v0.2.16...v0.2.17
|
||||
[#732]: https://github.com/rust-random/getrandom/pull/732
|
||||
[#768]: https://github.com/rust-random/getrandom/pull/768
|
||||
[#778]: https://github.com/rust-random/getrandom/pull/778
|
||||
|
||||
## [0.2.16] - 2025-04-22
|
||||
### Added
|
||||
- Cygwin support (backport of [#626]) [#654]
|
||||
|
||||
[0.2.16]: https://github.com/rust-random/getrandom/compare/v0.2.15...v0.2.16
|
||||
[#654]: https://github.com/rust-random/getrandom/pull/654
|
||||
|
||||
## [0.2.15] - 2024-05-06
|
||||
### Added
|
||||
- Apple visionOS support [#410]
|
||||
|
||||
### Changed
|
||||
- Use `libc::getrandom` on DragonflyBSD, FreeBSD, illumos, and Solaris [#411] [#416] [#417] [#420]
|
||||
- Unify `libc::getentropy`-based implementations [#418]
|
||||
|
||||
[0.2.15]: https://github.com/rust-random/getrandom/compare/v0.2.14...v0.2.15
|
||||
[#410]: https://github.com/rust-random/getrandom/pull/410
|
||||
[#411]: https://github.com/rust-random/getrandom/pull/411
|
||||
[#416]: https://github.com/rust-random/getrandom/pull/416
|
||||
[#417]: https://github.com/rust-random/getrandom/pull/417
|
||||
[#418]: https://github.com/rust-random/getrandom/pull/418
|
||||
[#420]: https://github.com/rust-random/getrandom/pull/420
|
||||
|
||||
## [0.2.14] - 2024-04-08
|
||||
### Fixed
|
||||
- Enable `/dev/urandom` fallback for MUSL-based Linux targets [#408]
|
||||
|
||||
[0.2.14]: https://github.com/rust-random/getrandom/compare/v0.2.13...v0.2.14
|
||||
[#408]: https://github.com/rust-random/getrandom/pull/408
|
||||
|
||||
## [0.2.13] - 2024-04-06
|
||||
### Added
|
||||
- `linux_disable_fallback` crate feature to disable `/dev/urandom`-based fallback on Linux and
|
||||
Android targets. Enabling this feature bumps minimum supported Linux kernel version to 3.17 and
|
||||
Android API level to 23 (Marshmallow). [#396]
|
||||
|
||||
### Changed
|
||||
- Disable `/dev/urandom` fallback for Linux targets outside of the following `target_arch`es:
|
||||
`aarch64`, `arm`, `powerpc`, `powerpc64`, `s390x`, `x86`, `x86_64` [#396]
|
||||
- Do not catch `EPERM` error code on Android while checking availability of
|
||||
the `getrandom` syscall [#396]
|
||||
|
||||
[0.2.13]: https://github.com/rust-random/getrandom/compare/v0.2.12...v0.2.13
|
||||
[#396]: https://github.com/rust-random/getrandom/pull/396
|
||||
|
||||
## [0.2.12] - 2024-01-09
|
||||
### Fixed
|
||||
- Custom backend for targets without atomics [#385]
|
||||
|
||||
### Changed
|
||||
- Improve robustness of the Hermit backend and `sys_fill_exact` [#386]
|
||||
- Raise minimum supported Apple OS versions to macOS 10.12 and iOS 10 [#388]
|
||||
|
||||
### Added
|
||||
- Document platform support policy [#387]
|
||||
|
||||
[0.2.12]: https://github.com/rust-random/getrandom/compare/v0.2.11...v0.2.12
|
||||
[#385]: https://github.com/rust-random/getrandom/pull/385
|
||||
[#386]: https://github.com/rust-random/getrandom/pull/386
|
||||
[#387]: https://github.com/rust-random/getrandom/pull/387
|
||||
[#388]: https://github.com/rust-random/getrandom/pull/388
|
||||
|
||||
## [0.2.11] - 2023-11-08
|
||||
### Added
|
||||
- GNU/Hurd support [#370]
|
||||
|
||||
### Changed
|
||||
- Renamed `__getrandom_internal` to `__GETRANDOM_INTERNAL` [#369]
|
||||
- Updated link to Hermit docs [#374]
|
||||
|
||||
[0.2.11]: https://github.com/rust-random/getrandom/compare/v0.2.10...v0.2.11
|
||||
[#369]: https://github.com/rust-random/getrandom/pull/369
|
||||
[#370]: https://github.com/rust-random/getrandom/pull/370
|
||||
[#374]: https://github.com/rust-random/getrandom/pull/374
|
||||
|
||||
## [0.2.10] - 2023-06-06
|
||||
### Added
|
||||
- Support for PS Vita (`armv7-sony-vita-newlibeabihf`) [#359]
|
||||
|
||||
### Changed
|
||||
- Use getentropy from libc on Emscripten targets [#362]
|
||||
|
||||
[0.2.10]: https://github.com/rust-random/getrandom/compare/v0.2.9...v0.2.10
|
||||
[#359]: https://github.com/rust-random/getrandom/pull/359
|
||||
[#362]: https://github.com/rust-random/getrandom/pull/362
|
||||
|
||||
## [0.2.9] - 2023-04-06
|
||||
### Added
|
||||
- AIX support [#282]
|
||||
- `getrandom_uninit` function [#291]
|
||||
- `wasm64-unknown-unknown` support [#303]
|
||||
- tvOS and watchOS support [#317]
|
||||
- QNX/nto support [#325]
|
||||
- Support for `getrandom` syscall on NetBSD ≥ 10.0 [#331]
|
||||
- `RtlGenRandom` fallback for non-UWP Windows [#337]
|
||||
|
||||
### Breaking Changes
|
||||
- Update MSRV to 1.36 [#291]
|
||||
|
||||
### Fixed
|
||||
- Solaris/OpenBSD/Dragonfly build [#301]
|
||||
|
||||
### Changed
|
||||
- Update MSRV to 1.36 [#291]
|
||||
- Use getentropy on Emscripten [#307]
|
||||
- Solaris: consistently use `/dev/random` source [#310]
|
||||
- Move 3ds selection above rdrand/js/custom fallback [#312]
|
||||
- Remove buffer zeroing from Node.js implementation [#315]
|
||||
- Use `open` instead of `open64` [#326]
|
||||
- Remove #cfg from bsd_arandom.rs [#332]
|
||||
- Hermit: use `sys_read_entropy` syscall [#333]
|
||||
- Eliminate potential panic in sys_fill_exact [#334]
|
||||
- rdrand: Remove checking for 0 and !0 and instead check CPU family and do a self-test [#335]
|
||||
- Move `__getrandom_custom` definition into a const block [#344]
|
||||
- Switch the custom backend to Rust ABI [#347]
|
||||
|
||||
[0.2.9]: https://github.com/rust-random/getrandom/compare/v0.2.8...v0.2.9
|
||||
[#282]: https://github.com/rust-random/getrandom/pull/282
|
||||
[#291]: https://github.com/rust-random/getrandom/pull/291
|
||||
[#301]: https://github.com/rust-random/getrandom/pull/301
|
||||
[#303]: https://github.com/rust-random/getrandom/pull/303
|
||||
[#307]: https://github.com/rust-random/getrandom/pull/307
|
||||
[#310]: https://github.com/rust-random/getrandom/pull/310
|
||||
[#312]: https://github.com/rust-random/getrandom/pull/312
|
||||
[#315]: https://github.com/rust-random/getrandom/pull/315
|
||||
[#317]: https://github.com/rust-random/getrandom/pull/317
|
||||
[#325]: https://github.com/rust-random/getrandom/pull/325
|
||||
[#326]: https://github.com/rust-random/getrandom/pull/326
|
||||
[#331]: https://github.com/rust-random/getrandom/pull/331
|
||||
[#332]: https://github.com/rust-random/getrandom/pull/332
|
||||
[#333]: https://github.com/rust-random/getrandom/pull/333
|
||||
[#334]: https://github.com/rust-random/getrandom/pull/334
|
||||
[#335]: https://github.com/rust-random/getrandom/pull/335
|
||||
[#337]: https://github.com/rust-random/getrandom/pull/337
|
||||
[#344]: https://github.com/rust-random/getrandom/pull/344
|
||||
[#347]: https://github.com/rust-random/getrandom/pull/347
|
||||
|
||||
## [0.2.8] - 2022-10-20
|
||||
### Changed
|
||||
- The [Web Cryptography API] will now be preferred on `wasm32-unknown-unknown`
|
||||
when using the `"js"` feature, even on Node.js [#284] [#295]
|
||||
|
||||
### Added
|
||||
- Added benchmarks to track buffer initialization cost [#272]
|
||||
|
||||
### Fixed
|
||||
- Use `$crate` in `register_custom_getrandom!` [#270]
|
||||
|
||||
### Documentation
|
||||
- Add information about enabling `"js"` feature [#280]
|
||||
- Fix link to `wasm-bindgen` [#278]
|
||||
- Document the varied implementations for underlying randomness sources [#276]
|
||||
|
||||
[0.2.8]: https://github.com/rust-random/getrandom/compare/v0.2.7...v0.2.8
|
||||
[#284]: https://github.com/rust-random/getrandom/pull/284
|
||||
[#295]: https://github.com/rust-random/getrandom/pull/295
|
||||
[#272]: https://github.com/rust-random/getrandom/pull/272
|
||||
[#270]: https://github.com/rust-random/getrandom/pull/270
|
||||
[#280]: https://github.com/rust-random/getrandom/pull/280
|
||||
[#278]: https://github.com/rust-random/getrandom/pull/278
|
||||
[#276]: https://github.com/rust-random/getrandom/pull/276
|
||||
[Web Cryptography API]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API
|
||||
|
||||
## [0.2.7] - 2022-06-14
|
||||
### Changed
|
||||
- Update `wasi` dependency to `0.11` [#253]
|
||||
|
||||
### Fixed
|
||||
- Use `AtomicPtr` instead of `AtomicUsize` for Strict Provenance compatibility. [#263]
|
||||
|
||||
### Documentation
|
||||
- Add comments explaining use of fallback mechanisms [#257] [#260]
|
||||
|
||||
[0.2.7]: https://github.com/rust-random/getrandom/compare/v0.2.6...v0.2.7
|
||||
[#263]: https://github.com/rust-random/getrandom/pull/263
|
||||
[#260]: https://github.com/rust-random/getrandom/pull/260
|
||||
[#253]: https://github.com/rust-random/getrandom/pull/253
|
||||
[#257]: https://github.com/rust-random/getrandom/pull/257
|
||||
|
||||
## [0.2.6] - 2022-03-28
|
||||
### Added
|
||||
- Nintendo 3DS (`armv6k-nintendo-3ds`) support [#248]
|
||||
|
||||
### Changed
|
||||
- Retry `open` when interrupted [#252]
|
||||
|
||||
[0.2.6]: https://github.com/rust-random/getrandom/compare/v0.2.5...v0.2.6
|
||||
[#248]: https://github.com/rust-random/getrandom/pull/248
|
||||
[#252]: https://github.com/rust-random/getrandom/pull/252
|
||||
|
||||
## [0.2.5] - 2022-02-22
|
||||
### Added
|
||||
- ESP-IDF targets (`*‑espidf`) support [#245]
|
||||
|
||||
### Fixed
|
||||
- Webpack warning caused by dynamic require [#234]
|
||||
- Error checking on iOS for `SecRandomCopyBytes` [#244]
|
||||
|
||||
[0.2.5]: https://github.com/rust-random/getrandom/compare/v0.2.4...v0.2.5
|
||||
[#234]: https://github.com/rust-random/getrandom/pull/234
|
||||
[#244]: https://github.com/rust-random/getrandom/pull/244
|
||||
[#245]: https://github.com/rust-random/getrandom/pull/245
|
||||
|
||||
## [0.2.4] - 2021-12-13
|
||||
### Changed
|
||||
- Use explicit imports in the `js` backend [#220]
|
||||
- Use `/dev/urandom` on Redox instead of `rand:` [#222]
|
||||
- Use `NonZeroU32::new_unchecked` to convert wasi error [#233]
|
||||
|
||||
### Added
|
||||
- SOLID targets (`*-kmc-solid_*`) support [#235]
|
||||
- Limited Hermit (`x86_64-unknown-hermit`) support [#236]
|
||||
|
||||
[0.2.4]: https://github.com/rust-random/getrandom/compare/v0.2.3...v0.2.4
|
||||
[#220]: https://github.com/rust-random/getrandom/pull/220
|
||||
[#222]: https://github.com/rust-random/getrandom/pull/222
|
||||
[#233]: https://github.com/rust-random/getrandom/pull/233
|
||||
[#235]: https://github.com/rust-random/getrandom/pull/235
|
||||
[#236]: https://github.com/rust-random/getrandom/pull/236
|
||||
|
||||
## [0.2.3] - 2021-04-10
|
||||
### Changed
|
||||
- Replace build.rs with link attributes. [#205]
|
||||
- Add support for getrandom syscall on DragonFly BSD. [#210]
|
||||
- Improve Node.js detection. [#215]
|
||||
|
||||
[0.2.3]: https://github.com/rust-random/getrandom/compare/v0.2.2...v0.2.3
|
||||
[#205]: https://github.com/rust-random/getrandom/pull/205
|
||||
[#210]: https://github.com/rust-random/getrandom/pull/210
|
||||
[#215]: https://github.com/rust-random/getrandom/pull/215
|
||||
|
||||
## [0.2.2] - 2021-01-19
|
||||
### Changed
|
||||
- Forward `rustc-dep-of-std` to dependencies. [#198]
|
||||
- Highlight feature-dependent functionality in documentation using the `doc_cfg` feature. [#200]
|
||||
|
||||
[0.2.2]: https://github.com/rust-random/getrandom/compare/v0.2.1...v0.2.2
|
||||
[#198]: https://github.com/rust-random/getrandom/pull/198
|
||||
[#200]: https://github.com/rust-random/getrandom/pull/200
|
||||
|
||||
## [0.2.1] - 2021-01-03
|
||||
### Changed
|
||||
- Update `cfg-if` to v1.0. [#166]
|
||||
- Update `wasi` to v0.10. [#167]
|
||||
|
||||
### Fixed
|
||||
- Multithreaded WASM support. [#165]
|
||||
|
||||
### Removed
|
||||
- Windows XP support. [#177]
|
||||
- Direct `stdweb` support. [#178]
|
||||
- CloudABI support. [#184]
|
||||
|
||||
[0.2.1]: https://github.com/rust-random/getrandom/compare/v0.2.0...v0.2.1
|
||||
[#165]: https://github.com/rust-random/getrandom/pull/165
|
||||
[#166]: https://github.com/rust-random/getrandom/pull/166
|
||||
[#167]: https://github.com/rust-random/getrandom/pull/167
|
||||
[#177]: https://github.com/rust-random/getrandom/pull/177
|
||||
[#178]: https://github.com/rust-random/getrandom/pull/178
|
||||
[#184]: https://github.com/rust-random/getrandom/pull/184
|
||||
|
||||
## [0.2.0] - 2020-09-10
|
||||
### Features for using getrandom on unsupported targets
|
||||
|
||||
The following (off by default) Cargo features have been added:
|
||||
- `"rdrand"` - use the RDRAND instruction on `no_std` `x86`/`x86_64` targets [#133]
|
||||
- `"js"` - use JavaScript calls on `wasm32-unknown-unknown` [#149]
|
||||
- Replaces the `stdweb` and `wasm-bindgen` features (which are removed)
|
||||
- `"custom"` - allows a user to specify a custom implementation [#109]
|
||||
|
||||
### Breaking Changes
|
||||
- Unsupported targets no longer compile [#107]
|
||||
- Change/Add `Error` constants [#120]
|
||||
- Only impl `std` traits when the `"std"` Cargo feature is specified [#106]
|
||||
- Remove official support for Hermit, L4Re, and UEFI [#133]
|
||||
- Remove optional `"log"` dependency [#131]
|
||||
- Update minimum supported Linux kernel to 2.6.32 [#153]
|
||||
- Update MSRV to 1.34 [#159]
|
||||
|
||||
[0.2.0]: https://github.com/rust-random/getrandom/compare/v0.1.16...v0.2.0
|
||||
[#106]: https://github.com/rust-random/getrandom/pull/106
|
||||
[#107]: https://github.com/rust-random/getrandom/pull/107
|
||||
[#109]: https://github.com/rust-random/getrandom/pull/109
|
||||
[#120]: https://github.com/rust-random/getrandom/pull/120
|
||||
[#131]: https://github.com/rust-random/getrandom/pull/131
|
||||
[#133]: https://github.com/rust-random/getrandom/pull/133
|
||||
[#149]: https://github.com/rust-random/getrandom/pull/149
|
||||
[#153]: https://github.com/rust-random/getrandom/pull/153
|
||||
[#159]: https://github.com/rust-random/getrandom/pull/159
|
||||
|
||||
## [0.1.16] - 2020-12-31
|
||||
### Changed
|
||||
- Update `cfg-if` to v1.0. [#173]
|
||||
- Implement `std::error::Error` for the `Error` type on additional targets. [#169]
|
||||
|
||||
### Fixed
|
||||
- Multithreaded WASM support. [#171]
|
||||
|
||||
[0.1.16]: https://github.com/rust-random/getrandom/compare/v0.1.15...v0.1.16
|
||||
[#173]: https://github.com/rust-random/getrandom/pull/173
|
||||
[#171]: https://github.com/rust-random/getrandom/pull/171
|
||||
[#169]: https://github.com/rust-random/getrandom/pull/169
|
||||
|
||||
## [0.1.15] - 2020-09-10
|
||||
### Changed
|
||||
- Added support for Internet Explorer 11 [#139]
|
||||
- Fix Webpack require warning with `wasm-bindgen` [#137]
|
||||
|
||||
[0.1.15]: https://github.com/rust-random/getrandom/compare/v0.1.14...v0.1.15
|
||||
[#137]: https://github.com/rust-random/getrandom/pull/137
|
||||
[#139]: https://github.com/rust-random/getrandom/pull/139
|
||||
|
||||
## [0.1.14] - 2020-01-07
|
||||
### Changed
|
||||
- Remove use of spin-locks in the `use_file` module. [#125]
|
||||
- Update `wasi` to v0.9. [#126]
|
||||
- Do not read errno value on DragonFlyBSD to fix compilation failure. [#129]
|
||||
|
||||
[0.1.14]: https://github.com/rust-random/getrandom/compare/v0.1.13...v0.1.14
|
||||
[#125]: https://github.com/rust-random/getrandom/pull/125
|
||||
[#126]: https://github.com/rust-random/getrandom/pull/126
|
||||
[#129]: https://github.com/rust-random/getrandom/pull/129
|
||||
|
||||
## [0.1.13] - 2019-08-25
|
||||
### Added
|
||||
- VxWorks targets support. [#86]
|
||||
|
||||
### Changed
|
||||
- If zero-length slice is passed to the `getrandom` function, always return
|
||||
`Ok(())` immediately without doing any calls to the underlying operating
|
||||
system. [#104]
|
||||
- Use the `kern.arandom` sysctl on NetBSD. [#115]
|
||||
|
||||
### Fixed
|
||||
- Bump `cfg-if` minimum version from 0.1.0 to 0.1.2. [#112]
|
||||
- Typos and bad doc links. [#117]
|
||||
|
||||
[0.1.13]: https://github.com/rust-random/getrandom/compare/v0.1.12...v0.1.13
|
||||
[#86]: https://github.com/rust-random/getrandom/pull/86
|
||||
[#104]: https://github.com/rust-random/getrandom/pull/104
|
||||
[#112]: https://github.com/rust-random/getrandom/pull/112
|
||||
[#115]: https://github.com/rust-random/getrandom/pull/115
|
||||
[#117]: https://github.com/rust-random/getrandom/pull/117
|
||||
|
||||
## [0.1.12] - 2019-08-18
|
||||
### Changed
|
||||
- Update wasi dependency from v0.5 to v0.7. [#100]
|
||||
|
||||
[0.1.12]: https://github.com/rust-random/getrandom/compare/v0.1.11...v0.1.12
|
||||
[#100]: https://github.com/rust-random/getrandom/pull/100
|
||||
|
||||
## [0.1.11] - 2019-08-25
|
||||
### Fixed
|
||||
- Implement `std`-dependent traits for selected targets even if `std`
|
||||
feature is disabled. (backward compatibility with v0.1.8) [#96]
|
||||
|
||||
[0.1.11]: https://github.com/rust-random/getrandom/compare/v0.1.10...v0.1.11
|
||||
[#96]: https://github.com/rust-random/getrandom/pull/96
|
||||
|
||||
## [0.1.10] - 2019-08-18 [YANKED]
|
||||
### Changed
|
||||
- Use the dummy implementation on `wasm32-unknown-unknown` even with the
|
||||
disabled `dummy` feature. [#90]
|
||||
|
||||
### Fixed
|
||||
- Fix CSP error for `wasm-bindgen`. [#92]
|
||||
|
||||
[0.1.10]: https://github.com/rust-random/getrandom/compare/v0.1.9...v0.1.10
|
||||
[#90]: https://github.com/rust-random/getrandom/pull/90
|
||||
[#92]: https://github.com/rust-random/getrandom/pull/92
|
||||
|
||||
## [0.1.9] - 2019-08-14 [YANKED]
|
||||
### Changed
|
||||
- Remove `std` dependency for opening and reading files. [#58]
|
||||
- Use `wasi` instead of `libc` on WASI target. [#64]
|
||||
- By default emit a compile-time error when built for an unsupported target.
|
||||
This behaviour can be disabled by using the `dummy` feature. [#71]
|
||||
|
||||
### Added
|
||||
- Add support for UWP targets. [#69]
|
||||
- Add unstable `rustc-dep-of-std` feature. [#78]
|
||||
|
||||
[0.1.9]: https://github.com/rust-random/getrandom/compare/v0.1.8...v0.1.9
|
||||
[#58]: https://github.com/rust-random/getrandom/pull/58
|
||||
[#64]: https://github.com/rust-random/getrandom/pull/64
|
||||
[#69]: https://github.com/rust-random/getrandom/pull/69
|
||||
[#71]: https://github.com/rust-random/getrandom/pull/71
|
||||
[#78]: https://github.com/rust-random/getrandom/pull/78
|
||||
|
||||
## [0.1.8] - 2019-07-29
|
||||
### Changed
|
||||
- Explicitly specify types to arguments of 'libc::syscall'. [#74]
|
||||
|
||||
[0.1.8]: https://github.com/rust-random/getrandom/compare/v0.1.7...v0.1.8
|
||||
[#74]: https://github.com/rust-random/getrandom/pull/74
|
||||
|
||||
## [0.1.7] - 2019-07-29
|
||||
### Added
|
||||
- Support for hermit and l4re. [#61]
|
||||
- `Error::raw_os_error` method, `Error::INTERNAL_START` and
|
||||
`Error::CUSTOM_START` constants. Use `libc` for retrieving OS error descriptions. [#54]
|
||||
|
||||
### Changed
|
||||
- Remove `lazy_static` dependency and use custom structures for lock-free
|
||||
initialization. [#51] [#52]
|
||||
- Try `getrandom()` first on FreeBSD. [#57]
|
||||
|
||||
### Removed
|
||||
- Bitrig support. [#56]
|
||||
|
||||
### Deprecated
|
||||
- `Error::UNKNOWN`, `Error::UNAVAILABLE`. [#54]
|
||||
|
||||
[0.1.7]: https://github.com/rust-random/getrandom/compare/v0.1.6...v0.1.7
|
||||
[#51]: https://github.com/rust-random/getrandom/pull/51
|
||||
[#52]: https://github.com/rust-random/getrandom/pull/52
|
||||
[#54]: https://github.com/rust-random/getrandom/pull/54
|
||||
[#56]: https://github.com/rust-random/getrandom/pull/56
|
||||
[#57]: https://github.com/rust-random/getrandom/pull/57
|
||||
[#61]: https://github.com/rust-random/getrandom/pull/61
|
||||
|
||||
## [0.1.6] - 2019-06-30
|
||||
### Changed
|
||||
- Minor change of RDRAND AMD bug handling. [#48]
|
||||
|
||||
[0.1.6]: https://github.com/rust-random/getrandom/compare/v0.1.5...v0.1.6
|
||||
[#48]: https://github.com/rust-random/getrandom/pull/48
|
||||
|
||||
## [0.1.5] - 2019-06-29
|
||||
### Fixed
|
||||
- Use shared `File` instead of shared file descriptor. [#44]
|
||||
- Workaround for RDRAND hardware bug present on some AMD CPUs. [#43]
|
||||
|
||||
### Changed
|
||||
- Try `getentropy` and then fallback to `/dev/random` on macOS. [#38]
|
||||
|
||||
[0.1.5]: https://github.com/rust-random/getrandom/compare/v0.1.4...v0.1.5
|
||||
[#38]: https://github.com/rust-random/getrandom/issues/38
|
||||
[#43]: https://github.com/rust-random/getrandom/pull/43
|
||||
[#44]: https://github.com/rust-random/getrandom/issues/44
|
||||
|
||||
## [0.1.4] - 2019-06-28
|
||||
### Added
|
||||
- Add support for `x86_64-unknown-uefi` target by using RDRAND with CPUID
|
||||
feature detection. [#30]
|
||||
|
||||
### Fixed
|
||||
- Fix long buffer issues on Windows and Linux. [#31] [#32]
|
||||
- Check `EPERM` in addition to `ENOSYS` on Linux. [#37]
|
||||
|
||||
### Changed
|
||||
- Improve efficiency by sharing file descriptor across threads. [#13]
|
||||
- Remove `cloudabi`, `winapi`, and `fuchsia-cprng` dependencies. [#40]
|
||||
- Improve RDRAND implementation. [#24]
|
||||
- Don't block during syscall detection on Linux. [#26]
|
||||
- Increase consistency with libc implementation on FreeBSD. [#36]
|
||||
- Apply `rustfmt`. [#39]
|
||||
|
||||
[0.1.4]: https://github.com/rust-random/getrandom/compare/v0.1.3...v0.1.4
|
||||
[#30]: https://github.com/rust-random/getrandom/pull/30
|
||||
[#13]: https://github.com/rust-random/getrandom/issues/13
|
||||
[#40]: https://github.com/rust-random/getrandom/pull/40
|
||||
[#26]: https://github.com/rust-random/getrandom/pull/26
|
||||
[#24]: https://github.com/rust-random/getrandom/pull/24
|
||||
[#39]: https://github.com/rust-random/getrandom/pull/39
|
||||
[#36]: https://github.com/rust-random/getrandom/pull/36
|
||||
[#31]: https://github.com/rust-random/getrandom/issues/31
|
||||
[#32]: https://github.com/rust-random/getrandom/issues/32
|
||||
[#37]: https://github.com/rust-random/getrandom/issues/37
|
||||
|
||||
## [0.1.3] - 2019-05-15
|
||||
- Update for `wasm32-unknown-wasi` being renamed to `wasm32-wasi`, and for
|
||||
WASI being categorized as an OS.
|
||||
|
||||
[0.1.3]: https://github.com/rust-random/getrandom/compare/v0.1.2...v0.1.3
|
||||
|
||||
## [0.1.2] - 2019-04-06
|
||||
- Add support for `wasm32-unknown-wasi` target.
|
||||
|
||||
[0.1.2]: https://github.com/rust-random/getrandom/compare/v0.1.1...v0.1.2
|
||||
|
||||
## [0.1.1] - 2019-04-05
|
||||
- Enable std functionality for CloudABI by default.
|
||||
|
||||
[0.1.1]: https://github.com/rust-random/getrandom/compare/v0.1.0...v0.1.1
|
||||
|
||||
## [0.1.0] - 2019-03-23
|
||||
Publish initial implementation.
|
||||
|
||||
[0.1.0]: https://github.com/rust-random/getrandom/compare/v0.0.0...v0.1.0
|
||||
|
||||
## [0.0.0] - 2019-01-19
|
||||
Publish an empty template library.
|
||||
|
||||
[0.0.0]: https://github.com/rust-random/getrandom/releases/tag/v0.0.0
|
||||
+424
@@ -0,0 +1,424 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.20.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649"
|
||||
|
||||
[[package]]
|
||||
name = "cast"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.64"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dad887fd958be91b5098c0248def011f4523ab786cd411be668777e55063501f"
|
||||
dependencies = [
|
||||
"find-msvc-tools",
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "find-msvc-tools"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
|
||||
|
||||
[[package]]
|
||||
name = "futures-core"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d"
|
||||
|
||||
[[package]]
|
||||
name = "futures-task"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393"
|
||||
|
||||
[[package]]
|
||||
name = "futures-util"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
"pin-project-lite",
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.4.3"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
"libc",
|
||||
"r-efi",
|
||||
"rand_core",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-test",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.102"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "03d04c30968dffe80775bd4d7fb676131cd04a1fb46d2686dbffbaec2d9dfd31"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"futures-util",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.186"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
|
||||
|
||||
[[package]]
|
||||
name = "libm"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "88904434abc2901f197fe8cc55f0445e7ded921dba5911dad2e2b39b48e663c4"
|
||||
|
||||
[[package]]
|
||||
name = "minicov"
|
||||
version = "0.3.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4869b6a491569605d66d3952bcdf03df789e5b536e5f0cf7758a7f08a55ae24d"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.50.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"libm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
||||
|
||||
[[package]]
|
||||
name = "oorandom"
|
||||
version = "11.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "r-efi"
|
||||
version = "6.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.150"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.118"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
|
||||
dependencies = [
|
||||
"same-file",
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.125"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ddb3f79143bced6de84270411622a2699cee572fc0875aeaf1e7867cf9fca1a"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
"rustversion",
|
||||
"wasm-bindgen-macro",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-futures"
|
||||
version = "0.4.75"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "503b14d284f2c8dac03b819967e155ea753f573586193b2b2c95990cb5d69280"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.125"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e21a184b13fb19e157296e2c46056aec9092264fab83e4ba59e68c61b323c3d"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.125"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fecefd9c35bd935a20fc3fc344b5f29138961e4f47fb03297d88f2587afb5ebd"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.125"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23939e44bb9a5d7576fa2b563dc2e136628f1224e88a8deed09e04858b77871f"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-test"
|
||||
version = "0.3.75"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d16931d57bcdd6a0dcc11254bdd1388827690c1b6807f0d2836f59a4f9eb30ac"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"cast",
|
||||
"js-sys",
|
||||
"libm",
|
||||
"minicov",
|
||||
"nu-ansi-term",
|
||||
"num-traits",
|
||||
"oorandom",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
"wasm-bindgen-test-macro",
|
||||
"wasm-bindgen-test-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-test-macro"
|
||||
version = "0.3.75"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fccaddcb2cd722baa7453b4c3d2cb2a3071597bb91f7a4373e8775bdb151778c"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-test-shared"
|
||||
version = "0.2.125"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3746ad029217960123cf2ebf939dda51eb931ba9f8f09c117e39c820b96aa794"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
|
||||
Vendored
+165
@@ -0,0 +1,165 @@
|
||||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2024"
|
||||
rust-version = "1.85"
|
||||
name = "getrandom"
|
||||
version = "0.4.3"
|
||||
authors = ["The Rand Project Developers"]
|
||||
build = "build.rs"
|
||||
exclude = [".*"]
|
||||
autolib = false
|
||||
autobins = false
|
||||
autoexamples = false
|
||||
autotests = false
|
||||
autobenches = false
|
||||
description = "A small cross-platform library for retrieving random data from system source"
|
||||
documentation = "https://docs.rs/getrandom"
|
||||
readme = "README.md"
|
||||
categories = [
|
||||
"os",
|
||||
"no-std",
|
||||
]
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/rust-random/getrandom"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = [
|
||||
"std",
|
||||
"sys_rng",
|
||||
]
|
||||
rustdoc-args = [
|
||||
"--cfg",
|
||||
'getrandom_backend="extern_impl"',
|
||||
]
|
||||
|
||||
[package.metadata.cross.target.x86_64-unknown-netbsd]
|
||||
pre-build = [
|
||||
"mkdir -p /tmp/netbsd",
|
||||
"curl -fO https://cdn.netbsd.org/pub/NetBSD/NetBSD-9.3/amd64/binary/sets/base.tar.xz",
|
||||
"tar -C /tmp/netbsd -xJf base.tar.xz",
|
||||
"cp /tmp/netbsd/usr/lib/libexecinfo.so /usr/local/x86_64-unknown-netbsd/lib",
|
||||
"rm base.tar.xz",
|
||||
"rm -rf /tmp/netbsd",
|
||||
]
|
||||
|
||||
[features]
|
||||
std = []
|
||||
sys_rng = ["dep:rand_core"]
|
||||
wasm_js = [
|
||||
"dep:wasm-bindgen",
|
||||
"dep:js-sys",
|
||||
]
|
||||
|
||||
[lib]
|
||||
name = "getrandom"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[test]]
|
||||
name = "mod"
|
||||
path = "tests/mod.rs"
|
||||
|
||||
[[test]]
|
||||
name = "sys_rng"
|
||||
path = "tests/sys_rng.rs"
|
||||
|
||||
[[bench]]
|
||||
name = "buffer"
|
||||
path = "benches/buffer.rs"
|
||||
|
||||
[dependencies.cfg-if]
|
||||
version = "1"
|
||||
|
||||
[dependencies.rand_core]
|
||||
version = "0.10.0"
|
||||
optional = true
|
||||
|
||||
[target.'cfg(all(any(target_os = "linux", target_os = "android"), not(any(all(target_os = "linux", target_env = ""), getrandom_backend = "custom", getrandom_backend = "linux_raw", getrandom_backend = "rdrand", getrandom_backend = "rndr"))))'.dependencies.libc]
|
||||
version = "0.2.154"
|
||||
default-features = false
|
||||
|
||||
[target.'cfg(all(target_family = "wasm", any(target_os = "unknown", target_os = "none")))'.dependencies.wasm-bindgen]
|
||||
version = "0.2.98"
|
||||
optional = true
|
||||
default-features = false
|
||||
|
||||
[target.'cfg(all(target_family = "wasm", any(target_os = "unknown", target_os = "none")))'.dev-dependencies.wasm-bindgen-test]
|
||||
version = "0.3"
|
||||
|
||||
[target.'cfg(all(target_family = "wasm", any(target_os = "unknown", target_os = "none"), target_feature = "atomics"))'.dependencies.js-sys]
|
||||
version = "0.3.77"
|
||||
optional = true
|
||||
default-features = false
|
||||
|
||||
[target.'cfg(all(target_os = "uefi", getrandom_backend = "efi_rng"))'.dependencies.r-efi]
|
||||
version = "6"
|
||||
default-features = false
|
||||
|
||||
[target.'cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "hurd", target_os = "illumos", target_os = "cygwin", all(target_os = "horizon", target_arch = "arm")))'.dependencies.libc]
|
||||
version = "0.2.154"
|
||||
default-features = false
|
||||
|
||||
[target.'cfg(any(target_os = "haiku", target_os = "redox", target_os = "nto", target_os = "aix"))'.dependencies.libc]
|
||||
version = "0.2.154"
|
||||
default-features = false
|
||||
|
||||
[target.'cfg(any(target_os = "ios", target_os = "visionos", target_os = "watchos", target_os = "tvos"))'.dependencies.libc]
|
||||
version = "0.2.154"
|
||||
default-features = false
|
||||
|
||||
[target.'cfg(any(target_os = "macos", target_os = "openbsd", target_os = "vita", target_os = "emscripten"))'.dependencies.libc]
|
||||
version = "0.2.154"
|
||||
default-features = false
|
||||
|
||||
[target.'cfg(target_os = "netbsd")'.dependencies.libc]
|
||||
version = "0.2.154"
|
||||
default-features = false
|
||||
|
||||
[target.'cfg(target_os = "solaris")'.dependencies.libc]
|
||||
version = "0.2.154"
|
||||
default-features = false
|
||||
|
||||
[target.'cfg(target_os = "vxworks")'.dependencies.libc]
|
||||
version = "0.2.154"
|
||||
default-features = false
|
||||
|
||||
[lints.clippy]
|
||||
cast_lossless = "warn"
|
||||
cast_possible_truncation = "warn"
|
||||
cast_possible_wrap = "warn"
|
||||
cast_precision_loss = "warn"
|
||||
cast_ptr_alignment = "warn"
|
||||
cast_sign_loss = "warn"
|
||||
char_lit_as_u8 = "warn"
|
||||
checked_conversions = "warn"
|
||||
fn_to_numeric_cast = "warn"
|
||||
fn_to_numeric_cast_with_truncation = "warn"
|
||||
ptr_as_ptr = "warn"
|
||||
unnecessary_cast = "warn"
|
||||
useless_conversion = "warn"
|
||||
|
||||
[lints.rust]
|
||||
missing_docs = "warn"
|
||||
unused_lifetimes = "warn"
|
||||
|
||||
[lints.rust.unexpected_cfgs]
|
||||
level = "warn"
|
||||
priority = 0
|
||||
check-cfg = [
|
||||
'cfg(getrandom_backend, values("custom", "efi_rng", "rdrand", "rndr", "linux_getrandom", "linux_raw", "windows_legacy", "unsupported", "extern_impl"))',
|
||||
"cfg(getrandom_msan)",
|
||||
"cfg(getrandom_test_linux_fallback)",
|
||||
"cfg(getrandom_test_linux_without_fallback)",
|
||||
"cfg(getrandom_test_netbsd_fallback)",
|
||||
'cfg(target_os, values("cygwin"))',
|
||||
'cfg(target_os, values("motor"))',
|
||||
]
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
[package]
|
||||
name = "getrandom"
|
||||
version = "0.4.3"
|
||||
edition = "2024"
|
||||
rust-version = "1.85" # Sync tests.yml and README.md.
|
||||
authors = ["The Rand Project Developers"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "A small cross-platform library for retrieving random data from system source"
|
||||
documentation = "https://docs.rs/getrandom"
|
||||
repository = "https://github.com/rust-random/getrandom"
|
||||
categories = ["os", "no-std"]
|
||||
exclude = [".*"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
features = ["std", "sys_rng"]
|
||||
rustdoc-args = ["--cfg", "getrandom_backend=\"extern_impl\""]
|
||||
|
||||
[features]
|
||||
# Implement From<getrandom::Error> for std::io::Error and
|
||||
# use std to retrieve OS error descriptions
|
||||
std = []
|
||||
|
||||
# Optional backend: wasm_js
|
||||
#
|
||||
# This flag enables the wasm_js backend and uses it by default on wasm32 where
|
||||
# the target_os is unknown. The getrandom_backend cfg may override this.
|
||||
#
|
||||
# WARNING: We strongly recommend against enabling this feature in libraries (except for tests)
|
||||
# since it is known to break non-Web WASM builds and further since the usage of `wasm-bindgen`
|
||||
# causes significant bloat to `Cargo.lock` (on all targets).
|
||||
#
|
||||
# The only exception to this rule: if your crate already unconditionally depends on `wasm-bindgen`
|
||||
# or `js-sys` on "unknown" WASM targets then it's acceptable to enable this feature unconditionally.
|
||||
wasm_js = ["dep:wasm-bindgen", "dep:js-sys"]
|
||||
|
||||
# Provide SysRng over rand_core
|
||||
sys_rng = ["dep:rand_core"]
|
||||
|
||||
[dependencies]
|
||||
cfg-if = "1"
|
||||
rand_core = { version = "0.10.0", optional = true }
|
||||
|
||||
# getrandom / linux_android_with_fallback
|
||||
[target.'cfg(all(any(target_os = "linux", target_os = "android"), not(any(all(target_os = "linux", target_env = ""), getrandom_backend = "custom", getrandom_backend = "linux_raw", getrandom_backend = "rdrand", getrandom_backend = "rndr"))))'.dependencies]
|
||||
libc = { version = "0.2.154", default-features = false }
|
||||
|
||||
# apple-other
|
||||
[target.'cfg(any(target_os = "ios", target_os = "visionos", target_os = "watchos", target_os = "tvos"))'.dependencies]
|
||||
libc = { version = "0.2.154", default-features = false }
|
||||
|
||||
# efi_rng
|
||||
[target.'cfg(all(target_os = "uefi", getrandom_backend = "efi_rng"))'.dependencies]
|
||||
r-efi = { version = "6", default-features = false }
|
||||
|
||||
# getentropy
|
||||
[target.'cfg(any(target_os = "macos", target_os = "openbsd", target_os = "vita", target_os = "emscripten"))'.dependencies]
|
||||
libc = { version = "0.2.154", default-features = false }
|
||||
|
||||
# getrandom
|
||||
[target.'cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "hurd", target_os = "illumos", target_os = "cygwin", all(target_os = "horizon", target_arch = "arm")))'.dependencies]
|
||||
libc = { version = "0.2.154", default-features = false }
|
||||
|
||||
# netbsd
|
||||
[target.'cfg(target_os = "netbsd")'.dependencies]
|
||||
libc = { version = "0.2.154", default-features = false }
|
||||
|
||||
# solaris
|
||||
[target.'cfg(target_os = "solaris")'.dependencies]
|
||||
libc = { version = "0.2.154", default-features = false }
|
||||
|
||||
# use_file
|
||||
[target.'cfg(any(target_os = "haiku", target_os = "redox", target_os = "nto", target_os = "aix"))'.dependencies]
|
||||
libc = { version = "0.2.154", default-features = false }
|
||||
|
||||
# vxworks
|
||||
[target.'cfg(target_os = "vxworks")'.dependencies]
|
||||
libc = { version = "0.2.154", default-features = false }
|
||||
|
||||
# wasm_js
|
||||
[target.'cfg(all(target_family = "wasm", any(target_os = "unknown", target_os = "none")))'.dependencies]
|
||||
wasm-bindgen = { version = "0.2.98", default-features = false, optional = true }
|
||||
[target.'cfg(all(target_family = "wasm", any(target_os = "unknown", target_os = "none"), target_feature = "atomics"))'.dependencies]
|
||||
js-sys = { version = "0.3.77", default-features = false, optional = true }
|
||||
[target.'cfg(all(target_family = "wasm", any(target_os = "unknown", target_os = "none")))'.dev-dependencies]
|
||||
wasm-bindgen-test = "0.3"
|
||||
|
||||
[lints.rust.unexpected_cfgs]
|
||||
level = "warn"
|
||||
check-cfg = [
|
||||
'cfg(getrandom_backend, values("custom", "efi_rng", "rdrand", "rndr", "linux_getrandom", "linux_raw", "windows_legacy", "unsupported", "extern_impl"))',
|
||||
'cfg(getrandom_msan)',
|
||||
'cfg(getrandom_test_linux_fallback)',
|
||||
'cfg(getrandom_test_linux_without_fallback)',
|
||||
'cfg(getrandom_test_netbsd_fallback)',
|
||||
'cfg(target_os, values("cygwin"))', # TODO(MSRV 1.86): Remove this.
|
||||
'cfg(target_os, values("motor"))',
|
||||
]
|
||||
|
||||
[lints.rust]
|
||||
unused_lifetimes = "warn"
|
||||
missing_docs = "warn"
|
||||
|
||||
[lints.clippy]
|
||||
cast_lossless = "warn"
|
||||
cast_possible_truncation = "warn"
|
||||
cast_possible_wrap = "warn"
|
||||
cast_precision_loss = "warn"
|
||||
cast_ptr_alignment = "warn"
|
||||
cast_sign_loss = "warn"
|
||||
char_lit_as_u8 = "warn"
|
||||
checked_conversions = "warn"
|
||||
fn_to_numeric_cast = "warn"
|
||||
fn_to_numeric_cast_with_truncation = "warn"
|
||||
ptr_as_ptr = "warn"
|
||||
unnecessary_cast = "warn"
|
||||
useless_conversion = "warn"
|
||||
|
||||
# workaround for https://github.com/cross-rs/cross/issues/1345
|
||||
[package.metadata.cross.target.x86_64-unknown-netbsd]
|
||||
pre-build = [
|
||||
"mkdir -p /tmp/netbsd",
|
||||
"curl -fO https://cdn.netbsd.org/pub/NetBSD/NetBSD-9.3/amd64/binary/sets/base.tar.xz",
|
||||
"tar -C /tmp/netbsd -xJf base.tar.xz",
|
||||
"cp /tmp/netbsd/usr/lib/libexecinfo.so /usr/local/x86_64-unknown-netbsd/lib",
|
||||
"rm base.tar.xz",
|
||||
"rm -rf /tmp/netbsd",
|
||||
]
|
||||
Vendored
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
https://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
https://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
Copyright (c) 2018-2026 The rust-random Project Developers
|
||||
Copyright (c) 2014 The Rust Project Developers
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
Vendored
+416
@@ -0,0 +1,416 @@
|
||||
# getrandom: system's random number generator
|
||||
|
||||
[![Build Status]][GitHub Actions]
|
||||
[![Crate]][crates.io]
|
||||
[![Documentation]][docs.rs]
|
||||
[![Dependency Status]][deps.rs]
|
||||
[![Downloads]][crates.io]
|
||||
[![License]][LICENSE-MIT]
|
||||
|
||||
`getrandom` is a Rust library for retrieving random data from (operating) system sources.
|
||||
|
||||
It is assumed that the system always provides high-quality, cryptographically secure random
|
||||
data, ideally backed by hardware entropy sources. This crate derives its name from
|
||||
the Linux `getrandom` syscall but is cross-platform, roughly supporting the same set
|
||||
of platforms as Rust's `std` library.
|
||||
|
||||
This is a low-level API. Most users should prefer using a higher-level random-number
|
||||
library like [`rand`].
|
||||
|
||||
[`rand`]: https://crates.io/crates/rand
|
||||
|
||||
## Examples
|
||||
|
||||
```rust
|
||||
fn get_random_u128() -> Result<u128, getrandom::Error> {
|
||||
let mut buf = [0u8; 16];
|
||||
getrandom::fill(&mut buf)?;
|
||||
Ok(u128::from_ne_bytes(buf))
|
||||
}
|
||||
```
|
||||
|
||||
## Supported targets
|
||||
|
||||
| Target | Target Triple | Implementation
|
||||
| ------------------ | ------------------ | --------------
|
||||
| Linux, Android | `*‑linux‑*` | [`getrandom`][1] system call if available, otherwise [`/dev/urandom`][2] after successfully polling `/dev/random`
|
||||
| Windows 10+ | `*‑windows‑*` | [`ProcessPrng`]
|
||||
| Windows 7, 8 | `*-win7‑windows‑*` | [`RtlGenRandom`]
|
||||
| macOS | `*‑apple‑darwin` | [`getentropy`][3]
|
||||
| iOS, tvOS, watchOS | `*‑apple‑{ios,tvos,watchos}` | [`CCRandomGenerateBytes`]
|
||||
| FreeBSD | `*‑freebsd` | [`getrandom`][5]
|
||||
| OpenBSD | `*‑openbsd` | [`getentropy`][7]
|
||||
| NetBSD | `*‑netbsd` | [`getrandom`][16] if available, otherwise [`kern.arandom`][8]
|
||||
| Dragonfly BSD | `*‑dragonfly` | [`getrandom`][9]
|
||||
| Solaris | `*‑solaris` | [`getrandom`][11] with `GRND_RANDOM`
|
||||
| illumos | `*‑illumos` | [`getrandom`][12]
|
||||
| Fuchsia OS | `*‑fuchsia` | [`cprng_draw`]
|
||||
| Redox | `*‑redox` | `/dev/urandom`
|
||||
| Haiku | `*‑haiku` | `/dev/urandom` (identical to `/dev/random`)
|
||||
| Hermit | `*-hermit` | [`sys_read_entropy`]
|
||||
| Hurd | `*-hurd-*` | [`getrandom`][17]
|
||||
| SGX | `x86_64‑*‑sgx` | [`RDRAND`]
|
||||
| VxWorks | `*‑wrs‑vxworks‑*` | `randABytes` after checking entropy pool initialization with `randSecure`
|
||||
| Emscripten | `*‑emscripten` | [`getentropy`][13]
|
||||
| WASI 0.1 | `wasm32‑wasip1` | [`random_get`]
|
||||
| WASI 0.2 | `wasm32‑wasip2` | [`get-random-u64`]
|
||||
| SOLID | `*-kmc-solid_*` | `SOLID_RNG_SampleRandomBytes`
|
||||
| Nintendo 3DS | `*-nintendo-3ds` | [`getrandom`][18]
|
||||
| ESP-IDF | `*‑espidf` | [`esp_fill_random`] WARNING: see "Early Boot" section below
|
||||
| PS Vita | `*-vita-*` | [`getentropy`][19]
|
||||
| QNX Neutrino | `*‑nto-qnx*` | [`/dev/urandom`][14] (identical to `/dev/random`)
|
||||
| AIX | `*-ibm-aix` | [`/dev/urandom`][15]
|
||||
| Cygwin | `*-cygwin` | [`getrandom`][20] (based on [`RtlGenRandom`])
|
||||
| Motor OS | `x86_64-unknown-motor` | [`RDRAND`]
|
||||
|
||||
Pull Requests that add support for new targets to `getrandom` are always welcome.
|
||||
|
||||
### WebAssembly support
|
||||
|
||||
This crate fully supports the [WASI] and [Emscripten] targets. However, the `wasm32-unknown-unknown`
|
||||
and `wasm64-unknown-unknown` targets (i.e. the target used by `wasm-pack`)
|
||||
are not automatically supported since, from the target name alone, we cannot deduce
|
||||
which JavaScript interface should be used (or if JavaScript is available at all).
|
||||
|
||||
We do not include support for these targets in the default configuration because our JS backend
|
||||
(supporting web browsers, web workers and Node.js v19 or later) requires [`wasm-bindgen`],
|
||||
**bloating `Cargo.lock`** and **potentially breaking builds** on non-web WASM platforms.
|
||||
|
||||
To enable `getrandom`'s functionality on `wasm32/64-unknown-unknown` using
|
||||
[`Crypto.getRandomValues`] via [`wasm-bindgen`], enable the `wasm_js` crate feature.
|
||||
|
||||
WARNING: We strongly recommend against enabling this feature in libraries (except for tests)
|
||||
since it is known to break non-Web WASM builds and further since the usage of `wasm-bindgen`
|
||||
causes significant bloat to `Cargo.lock` (on all targets).
|
||||
|
||||
The only exception to this rule: if your crate already unconditionally depends on `wasm-bindgen`
|
||||
or `js-sys` on "unknown" WASM targets then it's acceptable to enable this feature unconditionally.
|
||||
|
||||
### Opt-in backends
|
||||
|
||||
`getrandom` also provides optional (opt-in) backends, which allow users to customize the source
|
||||
of randomness based on their specific needs:
|
||||
|
||||
| Backend name | Target | Target Triple | Implementation
|
||||
| ------------------- | -------------------- | ------------------------ | --------------
|
||||
| `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).
|
||||
| `linux_raw` | Linux, Android | `*‑linux‑*` | Same as `linux_getrandom`, but uses raw `asm!`-based syscalls instead of `libc`.
|
||||
| `rdrand` | x86, x86-64 | `x86_64-*`, `i686-*` | [`RDRAND`] instruction
|
||||
| `rndr` | AArch64 | `aarch64-*` | [`RNDR`] register
|
||||
| `efi_rng` | UEFI | `*-unknown‑uefi` | [`EFI_RNG_PROTOCOL`] with `EFI_RNG_ALGORITHM_RAW` (requires `std` and Nightly compiler)
|
||||
| `windows_legacy` | Windows | `*-windows-*` | [`RtlGenRandom`]
|
||||
| `custom` | All targets | `*` | User-provided custom implementation (see [custom backend])
|
||||
| `unsupported` | All targets | `*` | Always returns `Err(Error::UNSUPPORTED)` (see [unsupported backend])
|
||||
| `extern_impl` | All targets | `*` | Externally-provided custom implementation (see [externally implemented interface])
|
||||
|
||||
Opt-in backends can be enabled using the `getrandom_backend` configuration flag.
|
||||
The flag can be set either by specifying the `rustflags` field in [`.cargo/config.toml`]:
|
||||
```toml
|
||||
# It's recommended to set the flag on a per-target basis:
|
||||
[target.'cfg(target_os = "linux")']
|
||||
rustflags = ['--cfg', 'getrandom_backend="linux_getrandom"']
|
||||
```
|
||||
|
||||
Or by using the `RUSTFLAGS` environment variable:
|
||||
|
||||
```sh
|
||||
RUSTFLAGS='--cfg getrandom_backend="linux_getrandom"' cargo build
|
||||
```
|
||||
|
||||
Enabling an opt-in backend will replace the backend used by default. Doing this for
|
||||
an incorrect target (e.g. using `linux_getrandom` while compiling for a Windows target)
|
||||
will result in a compilation error. Be extremely careful while using opt-in backends,
|
||||
as incorrect configuration may result in vulnerable applications or applications
|
||||
that always panic.
|
||||
|
||||
Note that using an opt-in backend in a library (e.g. for tests or benchmarks)
|
||||
WILL NOT have any effect on its downstream users.
|
||||
|
||||
[`.cargo/config.toml`]: https://doc.rust-lang.org/cargo/reference/config.html
|
||||
|
||||
### Raw Linux syscall support
|
||||
|
||||
Currently the `linux_raw` backend supports only targets with stabilized `asm!` macro,
|
||||
i.e. `arm`, `aarch64`, `loongarch64`, `riscv32`, `riscv64`, `s390x`, `x86`, and `x86_64`.
|
||||
|
||||
Note that the raw syscall backend may be slower than backends based on `libc::getrandom`,
|
||||
e.g. it does not implement vDSO optimizations and on `x86` it uses the infamously slow
|
||||
`int 0x80` instruction to perform syscall.
|
||||
|
||||
### Custom backend
|
||||
|
||||
If this crate does not support your target out of the box or you have to use
|
||||
a non-default entropy source, then you can provide a custom implementation.
|
||||
You need to enable the custom backend as described in the
|
||||
[opt-in backends][opt-in] section.
|
||||
|
||||
Next, you need to define an `extern` function with the following signature:
|
||||
|
||||
```rust
|
||||
use getrandom::Error;
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe extern "Rust" fn __getrandom_v03_custom(
|
||||
dest: *mut u8,
|
||||
len: usize,
|
||||
) -> Result<(), Error> {
|
||||
todo!()
|
||||
}
|
||||
```
|
||||
|
||||
This function should, ideally, be defined in the root crate of your project,
|
||||
e.g. in your `main.rs`. This function MUST be defined only once for your
|
||||
project, i.e. upstream library crates SHOULD NOT define it outside of
|
||||
tests and benchmarks. Improper configuration of this backend may result
|
||||
in linking errors.
|
||||
|
||||
The function accepts a pointer to a buffer that should be filled with random
|
||||
data and its length in bytes. Note that the buffer MAY be uninitialized.
|
||||
On success, the function should return `Ok(())` and fully fill the input buffer;
|
||||
otherwise, it should return an error value.
|
||||
|
||||
While wrapping functions which work with byte slices you should fully initialize
|
||||
the buffer before passing it to the function:
|
||||
```rust
|
||||
use getrandom::Error;
|
||||
|
||||
fn my_entropy_source(buf: &mut [u8]) -> Result<(), getrandom::Error> {
|
||||
// ...
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe extern "Rust" fn __getrandom_v03_custom(
|
||||
dest: *mut u8,
|
||||
len: usize,
|
||||
) -> Result<(), Error> {
|
||||
let buf = unsafe {
|
||||
// fill the buffer with zeros
|
||||
core::ptr::write_bytes(dest, 0, len);
|
||||
// create mutable byte slice
|
||||
core::slice::from_raw_parts_mut(dest, len)
|
||||
};
|
||||
my_entropy_source(buf)
|
||||
}
|
||||
```
|
||||
|
||||
### Externally Implemented Interface
|
||||
|
||||
Using the nightly-only feature [`extern_item_impls`] it is possible to provide
|
||||
a custom backend for `getrandom`, even to override an existing first-party implementation.
|
||||
First, enable the `extern_impl` opt-in backend to allow usage of this nightly feature.
|
||||
Then, you may provide implementations for `fill_uninit`, `u32`, and/or `u64`
|
||||
with an attribute macro from the `implementation` module.
|
||||
|
||||
[`extern_item_impls`]: https://github.com/rust-lang/rust/issues/125418
|
||||
|
||||
```rust
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
#[cfg(getrandom_backend = "extern_impl")]
|
||||
#[getrandom::implementation::fill_uninit]
|
||||
fn my_fill_uninit_implementation(
|
||||
dest: &mut [MaybeUninit<u8>]
|
||||
) -> Result<(), getrandom::Error> {
|
||||
// ...
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
For further details on what a suitable implementation for `fill_uninit` may look
|
||||
like, see [custom backend].
|
||||
|
||||
`getrandom` will provide a default implementation for `u32` and `u64`, but does
|
||||
not currently provide a default for `fill_uninit`, even if one is normally
|
||||
available for the current target. If no implementation is available,
|
||||
a compilation error will be raised with instructions for how to provide
|
||||
an implementation.
|
||||
|
||||
### Unsupported backend
|
||||
|
||||
In some rare scenarios, you might be compiling this crate for an unsupported
|
||||
target (e.g. `wasm32-unknown-unknown`), but this crate's functionality
|
||||
is not actually used by your code. If you are confident that `getrandom` is
|
||||
not used in your project, but it gets pulled nevertheless by one of your
|
||||
dependencies, then you can enable the `unsupported` backend, which always
|
||||
returns `Err(Error::UNSUPPORTED)`.
|
||||
|
||||
### Platform Support
|
||||
|
||||
This crate generally supports the same operating system and platform versions
|
||||
that the Rust standard library does. Additional targets may be supported using
|
||||
the opt-in custom backend.
|
||||
|
||||
This means that as Rust drops support for old versions of operating systems
|
||||
(such as old Linux kernel versions, Android API levels, etc.) in stable releases,
|
||||
`getrandom` may create new patch releases that remove support for
|
||||
outdated platform versions.
|
||||
|
||||
### `/dev/urandom` fallback on Linux and Android
|
||||
|
||||
On Linux targets, the `/dev/urandom` fallback is present only if either `target_env`
|
||||
is `musl`, or `target_arch` is one of the following: `aarch64`, `arm`, `powerpc`,
|
||||
`powerpc64`, `s390x`, `x86`, `x86_64`. Other supported targets [require][platform-support]
|
||||
kernel versions that support the `getrandom` system call, so the fallback is not needed.
|
||||
|
||||
On Android targets the fallback is present only for the following `target_arch`es:
|
||||
`aarch64`, `arm`, `x86`, `x86_64`. Other `target_arch`es (e.g. RISC-V) require
|
||||
sufficiently high API levels.
|
||||
|
||||
The fallback can be disabled by enabling the `linux_getrandom` opt-in backend.
|
||||
Note that doing so will bump minimum supported Linux kernel version to 3.17
|
||||
and Android API level to 23 (Marshmallow).
|
||||
|
||||
### Early boot
|
||||
|
||||
Sometimes, early in the boot process, the OS has not collected enough
|
||||
entropy to securely seed its RNG. This is especially common on virtual
|
||||
machines, where standard "random" events are hard to come by.
|
||||
|
||||
Some operating system interfaces always block until the RNG is securely
|
||||
seeded. This can take anywhere from a few seconds to more than a minute.
|
||||
A few (Linux, NetBSD and Solaris) offer a choice between blocking and
|
||||
getting an error; in these cases, we always choose to block.
|
||||
|
||||
On Linux (when the `getrandom` system call is not available), reading from
|
||||
`/dev/urandom` never blocks, even when the OS hasn't collected enough
|
||||
entropy yet. To avoid returning low-entropy bytes, we first poll
|
||||
`/dev/random` and only switch to `/dev/urandom` once this has succeeded.
|
||||
|
||||
On OpenBSD, this kind of entropy accounting isn't available, and on
|
||||
NetBSD, blocking on it is discouraged. On these platforms, nonblocking
|
||||
interfaces are used, even when reliable entropy may not be available.
|
||||
On the platforms where it is used, the reliability of entropy accounting
|
||||
itself isn't free from controversy. This library provides randomness
|
||||
sourced according to the platform's best practices, but each platform has
|
||||
its own limits on the grade of randomness it can promise in environments
|
||||
with few sources of entropy.
|
||||
|
||||
On ESP-IDF, if `esp_fill_random` is used before enabling WiFi, BT, or the
|
||||
voltage noise entropy source (SAR ADC), the Hardware RNG will only be seeded
|
||||
via RC_FAST_CLK. This can occur during early boot unless
|
||||
`bootloader_random_enable()` is called. For more information see the
|
||||
[ESP-IDF RNG Docs][esp-idf-rng] or the
|
||||
[RNG section of the ESP32 Technical Reference Manual][esp-trng-docs].
|
||||
|
||||
## Error handling
|
||||
|
||||
We always prioritize failure over returning known insecure "random" bytes.
|
||||
Generally, on supported platforms, failure is highly unlikely, though not
|
||||
impossible. If an error does occur, it is likely that it will occur
|
||||
on every call to `getrandom`. Therefore, after the first successful call,
|
||||
one can be reasonably confident that no errors will occur.
|
||||
|
||||
## Panic handling
|
||||
|
||||
We strive to eliminate all potential panics from our backend implementations.
|
||||
In other words, when compiled with optimizations enabled, the generated
|
||||
binary code for `getrandom` functions should not contain any panic branches.
|
||||
Even if the platform misbehaves and returns an unexpected result,
|
||||
our code should correctly handle it and return an error, e.g.
|
||||
[`Error::UNEXPECTED`].
|
||||
|
||||
## Sanitizer support
|
||||
|
||||
If your code uses [`fill_uninit`] and you enable
|
||||
[MemorySanitizer](https://doc.rust-lang.org/beta/unstable-book/compiler-flags/sanitizer.html#memorysanitizer)
|
||||
(i.e. `-Zsanitizer=memory`), we will automatically handle unpoisoning
|
||||
of the destination buffer filled by `fill_uninit`.
|
||||
|
||||
You can run sanitizer tests for your crate dependent on `getrandom` like this:
|
||||
```sh
|
||||
RUSTFLAGS="-Zsanitizer=memory" cargo test -Zbuild-std --target=x86_64-unknown-linux-gnu
|
||||
```
|
||||
|
||||
## Minimum Supported Rust Version
|
||||
|
||||
This crate requires Rust 1.85 or later.
|
||||
|
||||
The most reliable way to get an MSRV-respecting behavior is to set
|
||||
`CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS=fallback` when doing
|
||||
`cargo update -p getrandom`, like so:
|
||||
```sh
|
||||
CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS=fallback cargo update -p getrandom
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
The `getrandom` library is distributed under either of
|
||||
|
||||
* [Apache License, Version 2.0][LICENSE-APACHE]
|
||||
* [MIT license][LICENSE-MIT]
|
||||
|
||||
at your option.
|
||||
|
||||
### Contribution
|
||||
|
||||
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
|
||||
dual licensed as above, without any additional terms or conditions.
|
||||
|
||||
[//]: # (badges)
|
||||
|
||||
[GitHub Actions]: https://github.com/rust-random/getrandom/actions?query=branch:master
|
||||
[Build Status]: https://github.com/rust-random/getrandom/actions/workflows/tests.yml/badge.svg?branch=master
|
||||
[crates.io]: https://crates.io/crates/getrandom
|
||||
[Crate]: https://img.shields.io/crates/v/getrandom
|
||||
[docs.rs]: https://docs.rs/getrandom
|
||||
[Documentation]: https://docs.rs/getrandom/badge.svg
|
||||
[deps.rs]: https://deps.rs/repo/github/rust-random/getrandom
|
||||
[Dependency Status]: https://deps.rs/repo/github/rust-random/getrandom/status.svg
|
||||
[Downloads]: https://img.shields.io/crates/d/getrandom
|
||||
[License]: https://img.shields.io/crates/l/getrandom
|
||||
|
||||
[//]: # (supported targets)
|
||||
|
||||
[1]: https://manned.org/getrandom.2
|
||||
[2]: https://manned.org/urandom.4
|
||||
[3]: https://www.unix.com/man-page/mojave/2/getentropy/
|
||||
[4]: https://www.unix.com/man-page/mojave/4/urandom/
|
||||
[5]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable
|
||||
[7]: https://man.openbsd.org/getentropy.2
|
||||
[8]: https://man.netbsd.org/sysctl.7
|
||||
[9]: https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom
|
||||
[11]: https://docs.oracle.com/cd/E88353_01/html/E37841/getrandom-2.html
|
||||
[12]: https://illumos.org/man/2/getrandom
|
||||
[13]: https://github.com/emscripten-core/emscripten/pull/12240
|
||||
[14]: https://www.qnx.com/developers/docs/7.1/index.html#com.qnx.doc.neutrino.utilities/topic/r/random.html
|
||||
[15]: https://www.ibm.com/docs/en/aix/7.3?topic=files-random-urandom-devices
|
||||
[16]: https://man.netbsd.org/getrandom.2
|
||||
[17]: https://www.gnu.org/software/libc/manual/html_mono/libc.html#index-getrandom
|
||||
[18]: https://github.com/rust3ds/shim-3ds/commit/b01d2568836dea2a65d05d662f8e5f805c64389d
|
||||
[19]: https://github.com/vitasdk/newlib/blob/2d869fe47aaf02b8e52d04e9a2b79d5b210fd016/newlib/libc/sys/vita/getentropy.c
|
||||
[20]: https://github.com/cygwin/cygwin/blob/main/winsup/cygwin/libc/getentropy.cc
|
||||
|
||||
[`ProcessPrng`]: https://learn.microsoft.com/en-us/windows/win32/seccng/processprng
|
||||
[`RtlGenRandom`]: https://learn.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom
|
||||
[`Crypto.getRandomValues`]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues
|
||||
[`RDRAND`]: https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide
|
||||
[`RNDR`]: https://developer.arm.com/documentation/ddi0601/2024-06/AArch64-Registers/RNDR--Random-Number
|
||||
[`CCRandomGenerateBytes`]: https://opensource.apple.com/source/CommonCrypto/CommonCrypto-60074/include/CommonRandom.h.auto.html
|
||||
[`cprng_draw`]: https://fuchsia.dev/fuchsia-src/zircon/syscalls/cprng_draw
|
||||
[`esp_fill_random`]: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/random.html#functions
|
||||
[esp-idf-rng]: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/random.html
|
||||
[esp-trng-docs]: https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf#rng
|
||||
[`EFI_RNG_PROTOCOL`]: https://uefi.org/specs/UEFI/2.10/37_Secure_Technologies.html#efi-rng-protocol
|
||||
[`random_get`]: https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-random_getbuf-pointeru8-buf_len-size---errno
|
||||
[`get-random-u64`]: https://github.com/WebAssembly/WASI/blob/v0.2.1/wasip2/random/random.wit#L23-L28
|
||||
[configuration flags]: #configuration-flags
|
||||
[custom backend]: #custom-backend
|
||||
[unsupported backend]: #unsupported-backend
|
||||
[`wasm-bindgen`]: https://github.com/rustwasm/wasm-bindgen
|
||||
[`module`]: https://rustwasm.github.io/wasm-bindgen/reference/attributes/on-js-imports/module.html
|
||||
[`sys_read_entropy`]: https://github.com/hermit-os/kernel/blob/315f58ff5efc81d9bf0618af85a59963ff55f8b1/src/syscalls/entropy.rs#L47-L55
|
||||
[platform-support]: https://doc.rust-lang.org/stable/rustc/platform-support.html
|
||||
[WASI]: https://github.com/WebAssembly/WASI
|
||||
[Emscripten]: https://emscripten.org
|
||||
[opt-in]: #opt-in-backends
|
||||
[externally implemented interface]: #externally-implemented-interface
|
||||
|
||||
[//]: # (licenses)
|
||||
|
||||
[LICENSE-APACHE]: https://github.com/rust-random/getrandom/blob/master/LICENSE-APACHE
|
||||
[LICENSE-MIT]: https://github.com/rust-random/getrandom/blob/master/LICENSE-MIT
|
||||
|
||||
[`Error::UNEXPECTED`]: https://docs.rs/getrandom/latest/getrandom/struct.Error.html#associatedconstant.UNEXPECTED
|
||||
[`fill_uninit`]: https://docs.rs/getrandom/latest/getrandom/fn.fill_uninit.html
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported Versions
|
||||
|
||||
Security updates are applied only to the latest release.
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
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.
|
||||
|
||||
Please disclose it at [security advisory](https://github.com/rust-random/getrandom/security/advisories/new).
|
||||
|
||||
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.
|
||||
Vendored
+123
@@ -0,0 +1,123 @@
|
||||
//! Basic benchmarks
|
||||
#![feature(test)]
|
||||
extern crate test;
|
||||
|
||||
use std::{
|
||||
mem::{MaybeUninit, size_of},
|
||||
slice,
|
||||
};
|
||||
|
||||
// Call getrandom on a zero-initialized stack buffer
|
||||
#[inline(always)]
|
||||
fn bench_fill<const N: usize>() {
|
||||
let mut buf = [0u8; N];
|
||||
getrandom::fill(&mut buf).unwrap();
|
||||
test::black_box(&buf[..]);
|
||||
}
|
||||
|
||||
// Call fill_uninit on an uninitialized stack buffer
|
||||
#[inline(always)]
|
||||
fn bench_fill_uninit<const N: usize>() {
|
||||
let mut uninit = [MaybeUninit::uninit(); N];
|
||||
let buf: &[u8] = getrandom::fill_uninit(&mut uninit).unwrap();
|
||||
test::black_box(buf);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_u32(b: &mut test::Bencher) {
|
||||
#[inline(never)]
|
||||
fn inner() -> u32 {
|
||||
getrandom::u32().unwrap()
|
||||
}
|
||||
b.bytes = 4;
|
||||
b.iter(inner);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_u32_via_fill(b: &mut test::Bencher) {
|
||||
#[inline(never)]
|
||||
fn inner() -> u32 {
|
||||
let mut res = MaybeUninit::<u32>::uninit();
|
||||
let dst: &mut [MaybeUninit<u8>] =
|
||||
unsafe { slice::from_raw_parts_mut(res.as_mut_ptr().cast(), size_of::<u32>()) };
|
||||
getrandom::fill_uninit(dst).unwrap();
|
||||
unsafe { res.assume_init() }
|
||||
}
|
||||
b.bytes = 4;
|
||||
b.iter(inner);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_u64(b: &mut test::Bencher) {
|
||||
#[inline(never)]
|
||||
fn inner() -> u64 {
|
||||
getrandom::u64().unwrap()
|
||||
}
|
||||
b.bytes = 8;
|
||||
b.iter(inner);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_u64_via_fill(b: &mut test::Bencher) {
|
||||
#[inline(never)]
|
||||
fn inner() -> u64 {
|
||||
let mut res = MaybeUninit::<u64>::uninit();
|
||||
let dst: &mut [MaybeUninit<u8>] =
|
||||
unsafe { slice::from_raw_parts_mut(res.as_mut_ptr().cast(), size_of::<u64>()) };
|
||||
getrandom::fill_uninit(dst).unwrap();
|
||||
unsafe { res.assume_init() }
|
||||
}
|
||||
b.bytes = 8;
|
||||
b.iter(inner);
|
||||
}
|
||||
|
||||
// We benchmark using #[inline(never)] "inner" functions for two reasons:
|
||||
// - Avoiding inlining reduces a source of variance when running benchmarks.
|
||||
// - It is _much_ easier to get the assembly or IR for the inner loop.
|
||||
//
|
||||
// For example, using cargo-show-asm (https://github.com/pacak/cargo-show-asm),
|
||||
// we can get the assembly for a particular benchmark's inner loop by running:
|
||||
// cargo asm --bench buffer --release buffer::p384::bench_getrandom::inner
|
||||
macro_rules! bench {
|
||||
( $name:ident, $size:expr ) => {
|
||||
mod $name {
|
||||
#[bench]
|
||||
fn bench_fill(b: &mut test::Bencher) {
|
||||
#[inline(never)]
|
||||
fn inner() {
|
||||
super::bench_fill::<{ $size }>()
|
||||
}
|
||||
|
||||
b.bytes = $size as u64;
|
||||
b.iter(inner);
|
||||
}
|
||||
#[bench]
|
||||
fn bench_fill_uninit(b: &mut test::Bencher) {
|
||||
#[inline(never)]
|
||||
fn inner() {
|
||||
super::bench_fill_uninit::<{ $size }>()
|
||||
}
|
||||
|
||||
b.bytes = $size as u64;
|
||||
b.iter(inner);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// 16 bytes (128 bits) is the size of an 128-bit AES key/nonce.
|
||||
bench!(aes128, 128 / 8);
|
||||
|
||||
// 32 bytes (256 bits) is the seed sized used for rand::thread_rng
|
||||
// and the `random` value in a ClientHello/ServerHello for TLS.
|
||||
// This is also the size of a 256-bit AES/HMAC/P-256/Curve25519 key
|
||||
// and/or nonce.
|
||||
bench!(p256, 256 / 8);
|
||||
|
||||
// A P-384/HMAC-384 key and/or nonce.
|
||||
bench!(p384, 384 / 8);
|
||||
|
||||
// Initializing larger buffers is not the primary use case of this library, as
|
||||
// this should normally be done by a userspace CSPRNG. However, we have a test
|
||||
// here to see the effects of a lower (amortized) syscall overhead.
|
||||
bench!(page, 4096);
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
//! Build script for memory sanitization support
|
||||
|
||||
fn main() {
|
||||
// Automatically detect cfg(sanitize = "memory") even if cfg(sanitize) isn't
|
||||
// supported. Build scripts get cfg() info, even if the cfg is unstable.
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
let sanitizers = std::env::var("CARGO_CFG_SANITIZE").unwrap_or_default();
|
||||
if sanitizers.contains("memory") {
|
||||
println!("cargo:rustc-cfg=getrandom_msan");
|
||||
}
|
||||
}
|
||||
Vendored
+190
@@ -0,0 +1,190 @@
|
||||
//! System-specific implementations.
|
||||
//!
|
||||
//! This module should provide `fill_inner` with the signature
|
||||
//! `fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>`.
|
||||
//! The function MUST fully initialize `dest` when `Ok(())` is returned;
|
||||
//! the function may need to use `sanitizer::unpoison` as well.
|
||||
//! The function MUST NOT ever write uninitialized bytes into `dest`,
|
||||
//! regardless of what value it returns.
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(getrandom_backend = "custom")] {
|
||||
mod custom;
|
||||
pub use custom::*;
|
||||
} else if #[cfg(getrandom_backend = "linux_getrandom")] {
|
||||
mod getrandom;
|
||||
pub use getrandom::*;
|
||||
} else if #[cfg(getrandom_backend = "linux_raw")] {
|
||||
mod linux_raw;
|
||||
pub use linux_raw::*;
|
||||
} else if #[cfg(getrandom_backend = "rdrand")] {
|
||||
mod rdrand;
|
||||
pub use rdrand::*;
|
||||
} else if #[cfg(getrandom_backend = "rndr")] {
|
||||
mod rndr;
|
||||
pub use rndr::*;
|
||||
} else if #[cfg(getrandom_backend = "efi_rng")] {
|
||||
mod efi_rng;
|
||||
pub use efi_rng::*;
|
||||
} else if #[cfg(getrandom_backend = "windows_legacy")] {
|
||||
mod windows_legacy;
|
||||
pub use windows_legacy::*;
|
||||
} else if #[cfg(getrandom_backend = "unsupported")] {
|
||||
mod unsupported;
|
||||
pub use unsupported::*;
|
||||
} else if #[cfg(getrandom_backend = "extern_impl")] {
|
||||
pub(crate) mod extern_impl;
|
||||
pub use extern_impl::*;
|
||||
} else if #[cfg(all(target_os = "linux", target_env = ""))] {
|
||||
mod linux_raw;
|
||||
pub use linux_raw::*;
|
||||
} else if #[cfg(target_os = "espidf")] {
|
||||
mod esp_idf;
|
||||
pub use esp_idf::*;
|
||||
} else if #[cfg(any(
|
||||
target_os = "haiku",
|
||||
target_os = "redox",
|
||||
target_os = "nto",
|
||||
target_os = "aix",
|
||||
))] {
|
||||
mod use_file;
|
||||
pub use use_file::*;
|
||||
} else if #[cfg(any(
|
||||
target_os = "macos",
|
||||
target_os = "openbsd",
|
||||
target_os = "vita",
|
||||
target_os = "emscripten",
|
||||
))] {
|
||||
mod getentropy;
|
||||
pub use getentropy::*;
|
||||
} else if #[cfg(any(
|
||||
// Rust supports Android API level 19 (KitKat) [0] and the next upgrade targets
|
||||
// level 21 (Lollipop) [1], while `getrandom(2)` was added only in
|
||||
// level 23 (Marshmallow). Note that it applies only to the "old" `target_arch`es,
|
||||
// RISC-V Android targets sufficiently new API level, same will apply for potential
|
||||
// new Android `target_arch`es.
|
||||
// [0]: https://blog.rust-lang.org/2023/01/09/android-ndk-update-r25.html
|
||||
// [1]: https://github.com/rust-lang/rust/pull/120593
|
||||
all(
|
||||
target_os = "android",
|
||||
any(
|
||||
target_arch = "aarch64",
|
||||
target_arch = "arm",
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64",
|
||||
),
|
||||
),
|
||||
// Only on these `target_arch`es Rust supports Linux kernel versions (3.2+)
|
||||
// that precede the version (3.17) in which `getrandom(2)` was added:
|
||||
// https://doc.rust-lang.org/stable/rustc/platform-support.html
|
||||
all(
|
||||
target_os = "linux",
|
||||
any(
|
||||
target_arch = "aarch64",
|
||||
target_arch = "arm",
|
||||
target_arch = "powerpc",
|
||||
target_arch = "powerpc64",
|
||||
target_arch = "s390x",
|
||||
target_arch = "x86",
|
||||
target_arch = "x86_64",
|
||||
// Minimum supported Linux kernel version for MUSL targets
|
||||
// is not specified explicitly (as of Rust 1.77) and they
|
||||
// are used in practice to target pre-3.17 kernels.
|
||||
all(
|
||||
target_env = "musl",
|
||||
not(
|
||||
any(
|
||||
target_arch = "riscv64",
|
||||
target_arch = "riscv32",
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
))] {
|
||||
mod use_file;
|
||||
mod linux_android_with_fallback;
|
||||
pub use linux_android_with_fallback::*;
|
||||
} else if #[cfg(any(
|
||||
target_os = "android",
|
||||
target_os = "linux",
|
||||
target_os = "dragonfly",
|
||||
target_os = "freebsd",
|
||||
target_os = "hurd",
|
||||
target_os = "illumos",
|
||||
target_os = "cygwin",
|
||||
// Check for target_arch = "arm" to only include the 3DS. Does not
|
||||
// include the Nintendo Switch (which is target_arch = "aarch64").
|
||||
all(target_os = "horizon", target_arch = "arm"),
|
||||
))] {
|
||||
mod getrandom;
|
||||
pub use getrandom::*;
|
||||
} else if #[cfg(target_os = "solaris")] {
|
||||
mod solaris;
|
||||
pub use solaris::*;
|
||||
} else if #[cfg(target_os = "netbsd")] {
|
||||
mod netbsd;
|
||||
pub use netbsd::*;
|
||||
} else if #[cfg(target_os = "fuchsia")] {
|
||||
mod fuchsia;
|
||||
pub use fuchsia::*;
|
||||
} else if #[cfg(any(
|
||||
target_os = "ios",
|
||||
target_os = "visionos",
|
||||
target_os = "watchos",
|
||||
target_os = "tvos",
|
||||
))] {
|
||||
mod apple_other;
|
||||
pub use apple_other::*;
|
||||
} else if #[cfg(all(target_arch = "wasm32", target_os = "wasi"))] {
|
||||
cfg_if! {
|
||||
if #[cfg(target_env = "p1")] {
|
||||
mod wasi_p1;
|
||||
pub use wasi_p1::*;
|
||||
} else {
|
||||
mod wasi_p2_3;
|
||||
pub use wasi_p2_3::*;
|
||||
}
|
||||
}
|
||||
} else if #[cfg(target_os = "hermit")] {
|
||||
mod hermit;
|
||||
pub use hermit::*;
|
||||
} else if #[cfg(all(target_arch = "x86_64", target_os = "motor"))] {
|
||||
mod rdrand;
|
||||
pub use rdrand::*;
|
||||
} else if #[cfg(target_os = "vxworks")] {
|
||||
mod vxworks;
|
||||
pub use vxworks::*;
|
||||
} else if #[cfg(target_os = "solid_asp3")] {
|
||||
mod solid;
|
||||
pub use solid::*;
|
||||
} else if #[cfg(all(windows, target_vendor = "win7"))] {
|
||||
mod windows_legacy;
|
||||
pub use windows_legacy::*;
|
||||
} else if #[cfg(windows)] {
|
||||
mod windows;
|
||||
pub use windows::*;
|
||||
} else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] {
|
||||
mod rdrand;
|
||||
pub use rdrand::*;
|
||||
} else if #[cfg(all(target_family = "wasm", any(target_os = "unknown", target_os = "none")))] {
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "wasm_js")] {
|
||||
mod wasm_js;
|
||||
pub use wasm_js::*;
|
||||
} else {
|
||||
compile_error!(concat!(
|
||||
"The wasm32/64-unknown-unknown are not supported by default; \
|
||||
you may need to enable the \"wasm_js\" crate feature. \
|
||||
For more information see: \
|
||||
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support"
|
||||
));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
compile_error!(concat!(
|
||||
"target is not supported. You may need to define a custom backend see: \
|
||||
https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#custom-backend"
|
||||
));
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
//! Implementation for iOS, tvOS, and watchOS where `getentropy` is unavailable.
|
||||
use crate::Error;
|
||||
use core::{ffi::c_void, mem::MaybeUninit};
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
let dst_ptr = dest.as_mut_ptr().cast::<c_void>();
|
||||
let ret = unsafe { libc::CCRandomGenerateBytes(dst_ptr, dest.len()) };
|
||||
if ret == libc::kCCSuccess {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::IOS_RANDOM_GEN)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Call to `CCRandomGenerateBytes` failed.
|
||||
pub(crate) const IOS_RANDOM_GEN: Error = Self::new_internal(10);
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
//! An implementation which calls out to an externally defined function.
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
unsafe extern "Rust" {
|
||||
fn __getrandom_v03_custom(dest: *mut u8, len: usize) -> Result<(), Error>;
|
||||
}
|
||||
unsafe { __getrandom_v03_custom(dest.as_mut_ptr().cast(), dest.len()) }
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
//! Implementation for UEFI using EFI_RNG_PROTOCOL
|
||||
use crate::Error;
|
||||
use core::{
|
||||
mem::MaybeUninit,
|
||||
ptr::{self, NonNull},
|
||||
};
|
||||
use r_efi::{
|
||||
efi::{BootServices, Handle},
|
||||
protocols::rng,
|
||||
};
|
||||
|
||||
extern crate std;
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
#[cfg(not(target_os = "uefi"))]
|
||||
compile_error!("`efi_rng` backend can be enabled only for UEFI targets!");
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn init() -> Result<NonNull<rng::Protocol>, Error> {
|
||||
const HANDLE_SIZE: usize = size_of::<Handle>();
|
||||
|
||||
let boot_services = std::os::uefi::env::boot_services()
|
||||
.ok_or(Error::BOOT_SERVICES_UNAVAILABLE)?
|
||||
.cast::<BootServices>();
|
||||
|
||||
let mut handles = [ptr::null_mut(); 16];
|
||||
// `locate_handle` operates with length in bytes
|
||||
let mut buf_size = handles.len() * HANDLE_SIZE;
|
||||
let mut guid = rng::PROTOCOL_GUID;
|
||||
let ret = unsafe {
|
||||
((*boot_services.as_ptr()).locate_handle)(
|
||||
r_efi::efi::BY_PROTOCOL,
|
||||
&mut guid,
|
||||
ptr::null_mut(),
|
||||
&mut buf_size,
|
||||
handles.as_mut_ptr(),
|
||||
)
|
||||
};
|
||||
|
||||
if ret.is_error() {
|
||||
return Err(Error::from_uefi_code(ret.as_usize()));
|
||||
}
|
||||
|
||||
let handles_len = buf_size / HANDLE_SIZE;
|
||||
let handles = handles.get(..handles_len).ok_or(Error::UNEXPECTED)?;
|
||||
|
||||
let system_handle = std::os::uefi::env::image_handle();
|
||||
for &handle in handles {
|
||||
let mut protocol: MaybeUninit<*mut rng::Protocol> = MaybeUninit::uninit();
|
||||
|
||||
let mut protocol_guid = rng::PROTOCOL_GUID;
|
||||
let ret = unsafe {
|
||||
((*boot_services.as_ptr()).open_protocol)(
|
||||
handle,
|
||||
&mut protocol_guid,
|
||||
protocol.as_mut_ptr().cast(),
|
||||
system_handle.as_ptr(),
|
||||
ptr::null_mut(),
|
||||
r_efi::system::OPEN_PROTOCOL_GET_PROTOCOL,
|
||||
)
|
||||
};
|
||||
|
||||
let protocol = if ret.is_error() {
|
||||
continue;
|
||||
} else {
|
||||
let protocol = unsafe { protocol.assume_init() };
|
||||
NonNull::new(protocol).ok_or(Error::UNEXPECTED)?
|
||||
};
|
||||
|
||||
// Try to use the acquired protocol handle
|
||||
let mut buf = [0u8; 8];
|
||||
let mut alg_guid = rng::ALGORITHM_RAW;
|
||||
let ret = unsafe {
|
||||
((*protocol.as_ptr()).get_rng)(
|
||||
protocol.as_ptr(),
|
||||
&mut alg_guid,
|
||||
buf.len(),
|
||||
buf.as_mut_ptr(),
|
||||
)
|
||||
};
|
||||
|
||||
if ret.is_error() {
|
||||
continue;
|
||||
}
|
||||
|
||||
return Ok(protocol);
|
||||
}
|
||||
Err(Error::NO_RNG_HANDLE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
#[path = "../utils/lazy_ptr.rs"]
|
||||
mod lazy;
|
||||
|
||||
static RNG_PROTOCOL: lazy::LazyPtr<rng::Protocol> = lazy::LazyPtr::new();
|
||||
|
||||
let protocol = RNG_PROTOCOL.try_unsync_init(init)?;
|
||||
|
||||
let mut alg_guid = rng::ALGORITHM_RAW;
|
||||
let ret = unsafe {
|
||||
((*protocol.as_ptr()).get_rng)(
|
||||
protocol.as_ptr(),
|
||||
&mut alg_guid,
|
||||
dest.len(),
|
||||
dest.as_mut_ptr().cast::<u8>(),
|
||||
)
|
||||
};
|
||||
|
||||
if ret.is_error() {
|
||||
Err(Error::from_uefi_code(ret.as_usize()))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub(crate) const BOOT_SERVICES_UNAVAILABLE: Error = Self::new_internal(10);
|
||||
pub(crate) const NO_RNG_HANDLE: Error = Self::new_internal(11);
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
//! Implementation for ESP-IDF
|
||||
use crate::Error;
|
||||
use core::{ffi::c_void, mem::MaybeUninit};
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
unsafe extern "C" {
|
||||
fn esp_fill_random(buf: *mut c_void, len: usize) -> u32;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
// Not that NOT enabling WiFi, BT, or the voltage noise entropy source (via `bootloader_random_enable`)
|
||||
// will cause ESP-IDF to return pseudo-random numbers based on the voltage noise entropy, after the initial boot process:
|
||||
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/random.html
|
||||
//
|
||||
// However tracking if some of these entropy sources is enabled is way too difficult to implement here
|
||||
unsafe { esp_fill_random(dest.as_mut_ptr().cast(), dest.len()) };
|
||||
|
||||
Ok(())
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
//! An implementation which calls out to an externally defined function.
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
/// Declares this function as an external implementation of [`fill_uninit`](crate::fill_uninit).
|
||||
#[eii(fill_uninit)]
|
||||
pub(crate) fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>;
|
||||
|
||||
/// Declares this function as an external implementation of [`u32`](crate::u32).
|
||||
#[eii(u32)]
|
||||
pub(crate) fn inner_u32() -> Result<u32, crate::Error> {
|
||||
crate::util::inner_u32()
|
||||
}
|
||||
|
||||
/// Declares this function as an external implementation of [`u64`](crate::u64).
|
||||
#[eii(u64)]
|
||||
pub(crate) fn inner_u64() -> Result<u64, crate::Error> {
|
||||
crate::util::inner_u64()
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
//! Implementation for Fuchsia Zircon
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
#[link(name = "zircon")]
|
||||
unsafe extern "C" {
|
||||
fn zx_cprng_draw(buffer: *mut u8, length: usize);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
unsafe { zx_cprng_draw(dest.as_mut_ptr().cast::<u8>(), dest.len()) }
|
||||
Ok(())
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
//! Implementation using getentropy(2)
|
||||
//!
|
||||
//! When porting to a new target, ensure that its implementation follows the
|
||||
//! POSIX conventions from
|
||||
//! <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getentropy.html>.
|
||||
//!
|
||||
//! Available since:
|
||||
//! - macOS 10.12
|
||||
//! - OpenBSD 5.6
|
||||
//! - Emscripten 2.0.5
|
||||
//! - vita newlib since Dec 2021
|
||||
//!
|
||||
//! For these targets, we use getentropy(2) because getrandom(2) doesn't exist.
|
||||
use crate::Error;
|
||||
use core::{ffi::c_void, mem::MaybeUninit};
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
#[path = "../utils/get_errno.rs"]
|
||||
mod utils;
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/limits.h.html
|
||||
// says `GETENTROPY_MAX` is at least 256.
|
||||
const GETENTROPY_MAX: usize = 256;
|
||||
|
||||
for chunk in dest.chunks_mut(GETENTROPY_MAX) {
|
||||
let ret = unsafe { libc::getentropy(chunk.as_mut_ptr().cast::<c_void>(), chunk.len()) };
|
||||
match ret {
|
||||
0 => continue,
|
||||
-1 => return Err(Error::from_errno(utils::get_errno())),
|
||||
_ => return Err(Error::UNEXPECTED),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
//! Implementation using getrandom(2).
|
||||
//!
|
||||
//! Available since:
|
||||
//! - Linux Kernel 3.17, Glibc 2.25, Musl 1.1.20
|
||||
//! - Android API level 23 (Marshmallow)
|
||||
//! - NetBSD 10.0
|
||||
//! - FreeBSD 12.0
|
||||
//! - illumos since Dec 2018
|
||||
//! - DragonFly 5.7
|
||||
//! - Hurd Glibc 2.31
|
||||
//! - shim-3ds since Feb 2022
|
||||
//!
|
||||
//! For these platforms, we always use the default pool and never set the
|
||||
//! GRND_RANDOM flag to use the /dev/random pool. On Linux/Android/Hurd, using
|
||||
//! GRND_RANDOM is not recommended. On NetBSD/FreeBSD/Dragonfly/3ds, it does
|
||||
//! nothing. On illumos, the default pool is used to implement getentropy(2),
|
||||
//! so we assume it is acceptable here.
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
#[path = "../utils/sys_fill_exact.rs"]
|
||||
mod utils;
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
utils::sys_fill_exact(dest, |buf| unsafe {
|
||||
libc::getrandom(buf.as_mut_ptr().cast(), buf.len(), 0)
|
||||
})
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
//! Implementation for Hermit
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
unsafe extern "C" {
|
||||
fn sys_read_entropy(buffer: *mut u8, length: usize, flags: u32) -> isize;
|
||||
// Note that `sys_secure_rand32/64` are implemented using `sys_read_entropy`:
|
||||
// https://github.com/hermit-os/kernel/blob/430da84/src/syscalls/entropy.rs#L62-L104
|
||||
// But this may change in future and can depend on compilation target,
|
||||
// so to future-proof we use these "syscalls".
|
||||
fn sys_secure_rand32(value: *mut u32) -> i32;
|
||||
fn sys_secure_rand64(value: *mut u64) -> i32;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn inner_u32() -> Result<u32, Error> {
|
||||
let mut res = MaybeUninit::uninit();
|
||||
let ret = unsafe { sys_secure_rand32(res.as_mut_ptr()) };
|
||||
match ret {
|
||||
0 => Ok(unsafe { res.assume_init() }),
|
||||
-1 => Err(Error::UNSUPPORTED),
|
||||
_ => Err(Error::UNEXPECTED),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn inner_u64() -> Result<u64, Error> {
|
||||
let mut res = MaybeUninit::uninit();
|
||||
let ret = unsafe { sys_secure_rand64(res.as_mut_ptr()) };
|
||||
match ret {
|
||||
0 => Ok(unsafe { res.assume_init() }),
|
||||
-1 => Err(Error::UNSUPPORTED),
|
||||
_ => Err(Error::UNEXPECTED),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
while !dest.is_empty() {
|
||||
let res = unsafe { sys_read_entropy(dest.as_mut_ptr().cast::<u8>(), dest.len(), 0) };
|
||||
match res {
|
||||
res if res > 0 => {
|
||||
let len = usize::try_from(res).map_err(|_| Error::UNEXPECTED)?;
|
||||
dest = dest.get_mut(len..).ok_or(Error::UNEXPECTED)?;
|
||||
}
|
||||
code => {
|
||||
let code = i32::try_from(code).map_err(|_| Error::UNEXPECTED)?;
|
||||
return Err(Error::from_neg_error_code(code));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
//! Implementation for Linux / Android with `/dev/urandom` fallback
|
||||
use super::use_file;
|
||||
use crate::Error;
|
||||
use core::{
|
||||
ffi::c_void,
|
||||
mem::{MaybeUninit, transmute},
|
||||
ptr::{self, NonNull},
|
||||
};
|
||||
use use_file::utils;
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
type GetRandomFn = unsafe extern "C" fn(*mut c_void, libc::size_t, libc::c_uint) -> libc::ssize_t;
|
||||
|
||||
/// Sentinel value which indicates that `libc::getrandom` either not available,
|
||||
/// or not supported by kernel.
|
||||
const NOT_AVAILABLE: NonNull<c_void> = unsafe { NonNull::new_unchecked(usize::MAX as *mut c_void) };
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn init() -> NonNull<c_void> {
|
||||
// Use static linking to `libc::getrandom` on MUSL targets and `dlsym` everywhere else
|
||||
#[cfg(not(target_env = "musl"))]
|
||||
let raw_ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, c"getrandom".as_ptr()) };
|
||||
#[cfg(target_env = "musl")]
|
||||
let raw_ptr = {
|
||||
let fptr: GetRandomFn = libc::getrandom;
|
||||
unsafe { transmute::<GetRandomFn, *mut c_void>(fptr) }
|
||||
};
|
||||
|
||||
let res_ptr = match NonNull::new(raw_ptr) {
|
||||
Some(fptr) => {
|
||||
let getrandom_fn = unsafe { transmute::<*mut c_void, GetRandomFn>(fptr.as_ptr()) };
|
||||
// Check that `getrandom` syscall is supported by kernel
|
||||
let res = unsafe { getrandom_fn(ptr::dangling_mut(), 0, 0) };
|
||||
if cfg!(getrandom_test_linux_fallback) {
|
||||
NOT_AVAILABLE
|
||||
} else if res.is_negative() {
|
||||
match utils::get_errno() {
|
||||
libc::ENOSYS => NOT_AVAILABLE, // No kernel support
|
||||
// The fallback on EPERM is intentionally not done on Android since this workaround
|
||||
// seems to be needed only for specific Linux-based products that aren't based
|
||||
// on Android. See https://github.com/rust-random/getrandom/issues/229.
|
||||
#[cfg(target_os = "linux")]
|
||||
libc::EPERM => NOT_AVAILABLE, // Blocked by seccomp
|
||||
_ => fptr,
|
||||
}
|
||||
} else {
|
||||
fptr
|
||||
}
|
||||
}
|
||||
None => NOT_AVAILABLE,
|
||||
};
|
||||
|
||||
#[cfg(getrandom_test_linux_without_fallback)]
|
||||
if res_ptr == NOT_AVAILABLE {
|
||||
panic!("Fallback is triggered with enabled `getrandom_test_linux_without_fallback`")
|
||||
}
|
||||
|
||||
res_ptr
|
||||
}
|
||||
|
||||
// Prevent inlining of the fallback implementation
|
||||
#[inline(never)]
|
||||
fn use_file_fallback(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
use_file::fill_inner(dest)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
#[path = "../utils/lazy_ptr.rs"]
|
||||
mod lazy;
|
||||
|
||||
static GETRANDOM_FN: lazy::LazyPtr<c_void> = lazy::LazyPtr::new();
|
||||
let fptr = GETRANDOM_FN.unsync_init(init);
|
||||
|
||||
if fptr == NOT_AVAILABLE {
|
||||
use_file_fallback(dest)
|
||||
} else {
|
||||
// note: `transmute` is currently the only way to convert a pointer into a function reference
|
||||
let getrandom_fn = unsafe { transmute::<*mut c_void, GetRandomFn>(fptr.as_ptr()) };
|
||||
utils::sys_fill_exact(dest, |buf| unsafe {
|
||||
getrandom_fn(buf.as_mut_ptr().cast(), buf.len(), 0)
|
||||
})
|
||||
}
|
||||
}
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
//! Implementation for Linux / Android using `asm!`-based syscalls.
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
use crate::{Error, MaybeUninit};
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "linux")))]
|
||||
compile_error!("`linux_raw` backend can be enabled only for Linux/Android targets!");
|
||||
|
||||
#[path = "../utils/sanitizer.rs"]
|
||||
mod utils;
|
||||
|
||||
#[allow(non_upper_case_globals)]
|
||||
unsafe fn getrandom_syscall(buf: *mut u8, buflen: usize, flags: u32) -> isize {
|
||||
let r0;
|
||||
|
||||
// Based on `rustix` and `linux-raw-sys` code.
|
||||
cfg_if! {
|
||||
if #[cfg(all(
|
||||
target_arch = "arm",
|
||||
any(target_abi = "eabi", target_abi = "eabihf"),
|
||||
))] {
|
||||
const __NR_getrandom: u32 = 384;
|
||||
// In thumb-mode, r7 is the frame pointer and is not permitted to be used in
|
||||
// an inline asm operand, so we have to use a different register and copy it
|
||||
// into r7 inside the inline asm.
|
||||
// Theoretically, we could detect thumb mode in the build script, but several
|
||||
// register moves are cheap enough compared to the syscall cost, so we do not
|
||||
// bother with it.
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"mov {tmp}, r7",
|
||||
"mov r7, {nr}",
|
||||
"svc 0",
|
||||
"mov r7, {tmp}",
|
||||
tmp = out(reg) _,
|
||||
nr = const __NR_getrandom,
|
||||
inlateout("r0") buf => r0,
|
||||
in("r1") buflen,
|
||||
in("r2") flags,
|
||||
options(nostack, preserves_flags)
|
||||
);
|
||||
}
|
||||
} else if #[cfg(all(
|
||||
target_arch = "aarch64",
|
||||
any(target_abi = "", target_abi = "ilp32"),
|
||||
))] {
|
||||
// According to the ILP32 patch for the kernel that hasn't yet
|
||||
// been merged into the mainline, "AARCH64/ILP32 ABI uses standard
|
||||
// syscall table [...] with the exceptions listed below," where
|
||||
// getrandom is not mentioned as an exception.
|
||||
const __NR_getrandom: u32 = 278;
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"svc 0",
|
||||
in("x8") __NR_getrandom,
|
||||
inlateout("x0") buf => r0,
|
||||
in("x1") buflen,
|
||||
in("x2") flags,
|
||||
options(nostack, preserves_flags)
|
||||
);
|
||||
}
|
||||
} else if #[cfg(all(
|
||||
target_arch = "loongarch64",
|
||||
any(target_abi = "", target_abi = "ilp32"),
|
||||
))] {
|
||||
const __NR_getrandom: u32 = 278;
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"syscall 0",
|
||||
in("$a7") __NR_getrandom,
|
||||
inlateout("$a0") buf => r0,
|
||||
in("$a1") buflen,
|
||||
in("$a2") flags,
|
||||
options(nostack, preserves_flags)
|
||||
);
|
||||
}
|
||||
} else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] {
|
||||
const __NR_getrandom: u32 = 278;
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"ecall",
|
||||
in("a7") __NR_getrandom,
|
||||
inlateout("a0") buf => r0,
|
||||
in("a1") buflen,
|
||||
in("a2") flags,
|
||||
options(nostack, preserves_flags)
|
||||
);
|
||||
}
|
||||
} else if #[cfg(target_arch = "s390x")] {
|
||||
const __NR_getrandom: u32 = 349;
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"svc 0",
|
||||
in("r1") __NR_getrandom,
|
||||
inlateout("r2") buf => r0,
|
||||
in("r3") buflen,
|
||||
in("r4") flags,
|
||||
options(nostack, preserves_flags)
|
||||
);
|
||||
}
|
||||
} else if #[cfg(target_arch = "x86")] {
|
||||
const __NR_getrandom: u32 = 355;
|
||||
// `int 0x80` is famously slow, but implementing vDSO is too complex
|
||||
// and `sysenter`/`syscall` have their own portability issues,
|
||||
// so we use the simple "legacy" way of doing syscalls.
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"int $$0x80",
|
||||
in("eax") __NR_getrandom,
|
||||
in("ebx") buf,
|
||||
in("ecx") buflen,
|
||||
in("edx") flags,
|
||||
lateout("eax") r0,
|
||||
options(nostack, preserves_flags)
|
||||
);
|
||||
}
|
||||
} else if #[cfg(all(
|
||||
target_arch = "x86_64",
|
||||
any(target_abi = "", target_abi = "x32"),
|
||||
))] {
|
||||
const __X32_SYSCALL_BIT: u32 = 0x40000000;
|
||||
const OFFSET: u32 = if cfg!(target_pointer_width = "32") { __X32_SYSCALL_BIT } else { 0 };
|
||||
const __NR_getrandom: u32 = OFFSET + 318;
|
||||
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"syscall",
|
||||
in("rax") __NR_getrandom,
|
||||
in("rdi") buf,
|
||||
in("rsi") buflen,
|
||||
in("rdx") flags,
|
||||
lateout("rax") r0,
|
||||
lateout("rcx") _,
|
||||
lateout("r11") _,
|
||||
options(nostack, preserves_flags)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
compile_error!("`linux_raw` backend does not support this target arch");
|
||||
}
|
||||
}
|
||||
|
||||
r0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
// Value of this error code is stable across all target arches.
|
||||
const EINTR: isize = -4;
|
||||
|
||||
loop {
|
||||
let ret = unsafe { getrandom_syscall(dest.as_mut_ptr().cast(), dest.len(), 0) };
|
||||
match usize::try_from(ret) {
|
||||
Ok(0) => return Err(Error::UNEXPECTED),
|
||||
Ok(len) => {
|
||||
let (l, r) = dest.split_at_mut_checked(len).ok_or(Error::UNEXPECTED)?;
|
||||
unsafe { utils::unpoison(l) };
|
||||
dest = r;
|
||||
if dest.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Err(_) if ret == EINTR => continue,
|
||||
Err(_) => {
|
||||
let code = i32::try_from(ret).map_err(|_| Error::UNEXPECTED)?;
|
||||
return Err(Error::from_neg_error_code(code));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
//! Implementation for NetBSD
|
||||
//!
|
||||
//! `getrandom(2)` was introduced in NetBSD 10. To support older versions we
|
||||
//! implement our own weak linkage to it, and provide a fallback based on the
|
||||
//! KERN_ARND sysctl.
|
||||
use crate::Error;
|
||||
use core::{
|
||||
cmp,
|
||||
ffi::c_void,
|
||||
mem::{self, MaybeUninit},
|
||||
ptr::{self, NonNull},
|
||||
};
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
#[path = "../utils/sys_fill_exact.rs"]
|
||||
mod utils;
|
||||
|
||||
unsafe extern "C" fn polyfill_using_kern_arand(
|
||||
buf: *mut c_void,
|
||||
buflen: libc::size_t,
|
||||
flags: libc::c_uint,
|
||||
) -> libc::ssize_t {
|
||||
debug_assert_eq!(flags, 0);
|
||||
|
||||
const MIB_LEN: libc::c_uint = 2;
|
||||
static MIB: [libc::c_int; MIB_LEN as usize] = [libc::CTL_KERN, libc::KERN_ARND];
|
||||
|
||||
// NetBSD will only return up to 256 bytes at a time, and
|
||||
// older NetBSD kernels will fail on longer buffers.
|
||||
let mut len = cmp::min(buflen, 256);
|
||||
let ret = unsafe { libc::sysctl(MIB.as_ptr(), MIB_LEN, buf, &mut len, ptr::null(), 0) };
|
||||
|
||||
match ret {
|
||||
0 if len <= 256 => libc::ssize_t::try_from(len).expect("len is in the range of 0..=256"),
|
||||
-1 => -1,
|
||||
// Zero return result will be converted into `Error::UNEXPECTED` by `sys_fill_exact`
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
type GetRandomFn = unsafe extern "C" fn(*mut c_void, libc::size_t, libc::c_uint) -> libc::ssize_t;
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn init() -> NonNull<c_void> {
|
||||
let ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, c"getrandom".as_ptr()) };
|
||||
if !cfg!(getrandom_test_netbsd_fallback) {
|
||||
if let Some(ptr) = NonNull::new(ptr) {
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
// Verify `polyfill_using_kern_arand` has the right signature.
|
||||
const POLYFILL: GetRandomFn = polyfill_using_kern_arand;
|
||||
unsafe { NonNull::new_unchecked(POLYFILL as *mut c_void) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
#[path = "../utils/lazy_ptr.rs"]
|
||||
mod lazy;
|
||||
|
||||
static GETRANDOM_FN: lazy::LazyPtr<c_void> = lazy::LazyPtr::new();
|
||||
|
||||
let fptr = GETRANDOM_FN.unsync_init(init);
|
||||
let fptr = unsafe { mem::transmute::<*mut c_void, GetRandomFn>(fptr.as_ptr()) };
|
||||
utils::sys_fill_exact(dest, |buf| unsafe {
|
||||
fptr(buf.as_mut_ptr().cast::<c_void>(), buf.len(), 0)
|
||||
})
|
||||
}
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
//! RDRAND backend for x86(-64) targets
|
||||
use crate::{Error, util::slice_as_uninit};
|
||||
use core::mem::{MaybeUninit, size_of};
|
||||
|
||||
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
|
||||
compile_error!("`rdrand` backend can be enabled only for x86 and x86-64 targets!");
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(target_arch = "x86_64")] {
|
||||
use core::arch::x86_64 as arch;
|
||||
use arch::_rdrand64_step as rdrand_step;
|
||||
type Word = u64;
|
||||
} else if #[cfg(target_arch = "x86")] {
|
||||
use core::arch::x86 as arch;
|
||||
use arch::_rdrand32_step as rdrand_step;
|
||||
type Word = u32;
|
||||
}
|
||||
}
|
||||
|
||||
// Recommendation from "Intel® Digital Random Number Generator (DRNG) Software
|
||||
// Implementation Guide" - Section 5.2.1 and "Intel® 64 and IA-32 Architectures
|
||||
// Software Developer’s Manual" - Volume 1 - Section 7.3.17.1.
|
||||
const RETRY_LIMIT: usize = 10;
|
||||
|
||||
#[target_feature(enable = "rdrand")]
|
||||
fn rdrand() -> Option<Word> {
|
||||
for _ in 0..RETRY_LIMIT {
|
||||
let mut val = 0;
|
||||
// SAFETY: this function is safe to call from a `[target_feature(enable
|
||||
// = "rdrand")]` context (it itself is annotated with
|
||||
// `target_feature(enable = "rdrand")`) but was marked unsafe until
|
||||
// https://github.com/rust-lang/stdarch/commit/59864cd which was pulled
|
||||
// in via https://github.com/rust-lang/rust/commit/f2eb88b which is
|
||||
// expected to be included in 1.93.0. Since our MSRV is 1.85, we need to
|
||||
// use unsafe here and suppress the lint.
|
||||
//
|
||||
// TODO(MSRV 1.93): remove allow(unused_unsafe) and the unsafe block.
|
||||
#[allow(unused_unsafe)]
|
||||
if unsafe { rdrand_step(&mut val) } == 1 {
|
||||
return Some(val);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
// "rdrand" target feature requires "+rdrand" flag, see https://github.com/rust-lang/rust/issues/49653.
|
||||
#[cfg(all(target_env = "sgx", not(target_feature = "rdrand")))]
|
||||
compile_error!(
|
||||
"SGX targets require 'rdrand' target feature. Enable by using -C target-feature=+rdrand."
|
||||
);
|
||||
|
||||
// Run a small self-test to make sure we aren't repeating values
|
||||
// Adapted from Linux's test in arch/x86/kernel/cpu/rdrand.c
|
||||
// Fails with probability < 2^(-90) on 32-bit systems
|
||||
#[target_feature(enable = "rdrand")]
|
||||
fn self_test() -> bool {
|
||||
// On AMD, RDRAND returns 0xFF...FF on failure, count it as a collision.
|
||||
let mut prev = Word::MAX;
|
||||
let mut fails = 0;
|
||||
for _ in 0..8 {
|
||||
match rdrand() {
|
||||
Some(val) if val == prev => fails += 1,
|
||||
Some(val) => prev = val,
|
||||
None => return false,
|
||||
};
|
||||
}
|
||||
fails <= 2
|
||||
}
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn init() -> bool {
|
||||
#[cfg(not(target_feature = "rdrand"))]
|
||||
{
|
||||
// SAFETY: All Rust x86 targets are new enough to have CPUID, and we
|
||||
// check that leaf 1 is supported before using it.
|
||||
//
|
||||
// TODO(MSRV 1.94): remove allow(unused_unsafe) and the unsafe blocks for `__cpuid`.
|
||||
#[allow(unused_unsafe)]
|
||||
let cpuid0 = unsafe { arch::__cpuid(0) };
|
||||
if cpuid0.eax < 1 {
|
||||
return false;
|
||||
}
|
||||
#[allow(unused_unsafe)]
|
||||
let cpuid1 = unsafe { arch::__cpuid(1) };
|
||||
|
||||
let vendor_id = [
|
||||
cpuid0.ebx.to_le_bytes(),
|
||||
cpuid0.edx.to_le_bytes(),
|
||||
cpuid0.ecx.to_le_bytes(),
|
||||
];
|
||||
if vendor_id == [*b"Auth", *b"enti", *b"cAMD"] {
|
||||
let mut family = (cpuid1.eax >> 8) & 0xF;
|
||||
if family == 0xF {
|
||||
family += (cpuid1.eax >> 20) & 0xFF;
|
||||
}
|
||||
// AMD CPUs families before 17h (Zen) sometimes fail to set CF when
|
||||
// RDRAND fails after suspend. Don't use RDRAND on those families.
|
||||
// See https://bugzilla.redhat.com/show_bug.cgi?id=1150286
|
||||
if family < 0x17 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const RDRAND_FLAG: u32 = 1 << 30;
|
||||
if cpuid1.ecx & RDRAND_FLAG == 0 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// SAFETY: We have already checked that rdrand is available.
|
||||
unsafe { self_test() }
|
||||
}
|
||||
|
||||
fn is_rdrand_good() -> bool {
|
||||
#[path = "../utils/lazy_bool.rs"]
|
||||
mod lazy;
|
||||
|
||||
static RDRAND_GOOD: lazy::LazyBool = lazy::LazyBool::new();
|
||||
|
||||
RDRAND_GOOD.unsync_init(init)
|
||||
}
|
||||
|
||||
#[target_feature(enable = "rdrand")]
|
||||
fn rdrand_exact(dest: &mut [MaybeUninit<u8>]) -> Option<()> {
|
||||
// We use chunks_exact_mut instead of chunks_mut as it allows almost all
|
||||
// calls to memcpy to be elided by the compiler.
|
||||
let mut chunks = dest.chunks_exact_mut(size_of::<Word>());
|
||||
for chunk in chunks.by_ref() {
|
||||
let src = rdrand()?.to_ne_bytes();
|
||||
chunk.copy_from_slice(slice_as_uninit(&src));
|
||||
}
|
||||
|
||||
let tail = chunks.into_remainder();
|
||||
let n = tail.len();
|
||||
if n > 0 {
|
||||
let src = rdrand()?.to_ne_bytes();
|
||||
tail.copy_from_slice(slice_as_uninit(&src[..n]));
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
#[target_feature(enable = "rdrand")]
|
||||
fn rdrand_u32() -> Option<u32> {
|
||||
rdrand().map(crate::util::truncate)
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
#[target_feature(enable = "rdrand")]
|
||||
fn rdrand_u64() -> Option<u64> {
|
||||
rdrand()
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86")]
|
||||
#[target_feature(enable = "rdrand")]
|
||||
fn rdrand_u32() -> Option<u32> {
|
||||
rdrand()
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86")]
|
||||
#[target_feature(enable = "rdrand")]
|
||||
fn rdrand_u64() -> Option<u64> {
|
||||
let a = rdrand()?;
|
||||
let b = rdrand()?;
|
||||
Some((u64::from(a) << 32) | u64::from(b))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn inner_u32() -> Result<u32, Error> {
|
||||
if !is_rdrand_good() {
|
||||
return Err(Error::NO_RDRAND);
|
||||
}
|
||||
// SAFETY: After this point, we know rdrand is supported.
|
||||
unsafe { rdrand_u32() }.ok_or(Error::FAILED_RDRAND)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn inner_u64() -> Result<u64, Error> {
|
||||
if !is_rdrand_good() {
|
||||
return Err(Error::NO_RDRAND);
|
||||
}
|
||||
// SAFETY: After this point, we know rdrand is supported.
|
||||
unsafe { rdrand_u64() }.ok_or(Error::FAILED_RDRAND)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
if !is_rdrand_good() {
|
||||
return Err(Error::NO_RDRAND);
|
||||
}
|
||||
// SAFETY: After this point, we know rdrand is supported.
|
||||
unsafe { rdrand_exact(dest) }.ok_or(Error::FAILED_RDRAND)
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// RDRAND instruction failed due to a hardware issue.
|
||||
pub(crate) const FAILED_RDRAND: Error = Self::new_internal(10);
|
||||
/// RDRAND instruction unsupported on this target.
|
||||
pub(crate) const NO_RDRAND: Error = Self::new_internal(11);
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
//! RNDR register backend for aarch64 targets
|
||||
//!
|
||||
//! Arm Architecture Reference Manual for A-profile architecture:
|
||||
//! ARM DDI 0487K.a, ID032224, D23.2.147 RNDR, Random Number
|
||||
use crate::{
|
||||
Error,
|
||||
util::{slice_as_uninit, truncate},
|
||||
};
|
||||
use core::arch::asm;
|
||||
use core::mem::{MaybeUninit, size_of};
|
||||
|
||||
#[cfg(not(target_arch = "aarch64"))]
|
||||
compile_error!("the `rndr` backend can be enabled only for AArch64 targets!");
|
||||
|
||||
const RETRY_LIMIT: usize = 5;
|
||||
|
||||
/// Read a random number from the aarch64 RNDR register
|
||||
///
|
||||
/// Callers must ensure that FEAT_RNG is available on the system
|
||||
/// The function assumes that the RNDR register is available
|
||||
/// If it fails to read a random number, it will retry up to 5 times
|
||||
/// After 5 failed reads the function will return `None`
|
||||
#[target_feature(enable = "rand")]
|
||||
unsafe fn rndr() -> Option<u64> {
|
||||
for _ in 0..RETRY_LIMIT {
|
||||
let mut x: u64;
|
||||
let mut nzcv: u64;
|
||||
|
||||
// AArch64 RNDR register is accessible by s3_3_c2_c4_0
|
||||
unsafe {
|
||||
asm!(
|
||||
"mrs {x}, RNDR",
|
||||
"mrs {nzcv}, NZCV",
|
||||
x = out(reg) x,
|
||||
nzcv = out(reg) nzcv,
|
||||
);
|
||||
}
|
||||
|
||||
// If the hardware returns a genuine random number, PSTATE.NZCV is set to 0b0000
|
||||
if nzcv == 0 {
|
||||
return Some(x);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[target_feature(enable = "rand")]
|
||||
unsafe fn rndr_fill(dest: &mut [MaybeUninit<u8>]) -> Option<()> {
|
||||
let mut chunks = dest.chunks_exact_mut(size_of::<u64>());
|
||||
for chunk in chunks.by_ref() {
|
||||
let src = unsafe { rndr() }?.to_ne_bytes();
|
||||
chunk.copy_from_slice(slice_as_uninit(&src));
|
||||
}
|
||||
|
||||
let tail = chunks.into_remainder();
|
||||
let n = tail.len();
|
||||
if n > 0 {
|
||||
let src = unsafe { rndr() }?.to_ne_bytes();
|
||||
tail.copy_from_slice(slice_as_uninit(&src[..n]));
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
#[cfg(target_feature = "rand")]
|
||||
fn is_rndr_available() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(not(target_feature = "rand"))]
|
||||
fn is_rndr_available() -> bool {
|
||||
#[path = "../utils/lazy_bool.rs"]
|
||||
mod lazy;
|
||||
|
||||
static RNDR_GOOD: lazy::LazyBool = lazy::LazyBool::new();
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "std")] {
|
||||
extern crate std;
|
||||
RNDR_GOOD.unsync_init(|| std::arch::is_aarch64_feature_detected!("rand"))
|
||||
} else if #[cfg(target_os = "linux")] {
|
||||
/// Check whether FEAT_RNG is available on the system
|
||||
///
|
||||
/// Requires the caller either be running in EL1 or be on a system supporting MRS
|
||||
/// emulation. Due to the above, the implementation is currently restricted to Linux.
|
||||
///
|
||||
/// Relying on runtime detection bumps minimum supported Linux kernel version to 4.11.
|
||||
fn mrs_check() -> bool {
|
||||
let mut id_aa64isar0: u64;
|
||||
|
||||
// If FEAT_RNG is implemented, ID_AA64ISAR0_EL1.RNDR (bits 60-63) are 0b0001
|
||||
// This is okay to do from EL0 in Linux because Linux will emulate MRS as per
|
||||
// https://docs.kernel.org/arch/arm64/cpu-feature-registers.html
|
||||
unsafe {
|
||||
asm!(
|
||||
"mrs {id}, ID_AA64ISAR0_EL1",
|
||||
id = out(reg) id_aa64isar0,
|
||||
);
|
||||
}
|
||||
|
||||
(id_aa64isar0 >> 60) & 0xf >= 1
|
||||
}
|
||||
|
||||
RNDR_GOOD.unsync_init(mrs_check)
|
||||
} else {
|
||||
compile_error!(
|
||||
"RNDR `no_std` runtime detection is currently supported only on Linux targets. \
|
||||
Either enable the `std` crate feature, or `rand` target feature at compile time."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn inner_u32() -> Result<u32, Error> {
|
||||
if !is_rndr_available() {
|
||||
return Err(Error::RNDR_NOT_AVAILABLE);
|
||||
}
|
||||
// SAFETY: after this point, we know the `rand` target feature is enabled
|
||||
let res = unsafe { rndr() };
|
||||
res.map(truncate).ok_or(Error::RNDR_FAILURE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn inner_u64() -> Result<u64, Error> {
|
||||
if !is_rndr_available() {
|
||||
return Err(Error::RNDR_NOT_AVAILABLE);
|
||||
}
|
||||
// SAFETY: after this point, we know the `rand` target feature is enabled
|
||||
let res = unsafe { rndr() };
|
||||
res.ok_or(Error::RNDR_FAILURE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
if !is_rndr_available() {
|
||||
return Err(Error::RNDR_NOT_AVAILABLE);
|
||||
}
|
||||
// SAFETY: after this point, we know the `rand` target feature is enabled
|
||||
unsafe { rndr_fill(dest).ok_or(Error::RNDR_FAILURE) }
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// RNDR register read failed due to a hardware issue.
|
||||
pub(crate) const RNDR_FAILURE: Error = Self::new_internal(10);
|
||||
/// RNDR register is not supported on this target.
|
||||
pub(crate) const RNDR_NOT_AVAILABLE: Error = Self::new_internal(11);
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
//! Solaris implementation using getrandom(2).
|
||||
//!
|
||||
//! While getrandom(2) has been available since Solaris 11.3, it has a few
|
||||
//! quirks not present on other OSes. First, on Solaris 11.3, calls will always
|
||||
//! fail if bufsz > 1024. Second, it will always either fail or completely fill
|
||||
//! the buffer (returning bufsz). Third, error is indicated by returning 0,
|
||||
//! rather than by returning -1. Finally, "if GRND_RANDOM is not specified
|
||||
//! then getrandom(2) is always a non blocking call". This _might_ imply that
|
||||
//! in early-boot scenarios with low entropy, getrandom(2) will not properly
|
||||
//! block. To be safe, we set GRND_RANDOM, mirroring the man page examples.
|
||||
//!
|
||||
//! For more information, see the man page linked in lib.rs and this blog post:
|
||||
//! https://blogs.oracle.com/solaris/post/solaris-new-system-calls-getentropy2-and-getrandom2
|
||||
//! which also explains why this crate should not use getentropy(2).
|
||||
use crate::Error;
|
||||
use core::{ffi::c_void, mem::MaybeUninit};
|
||||
use libc::___errno as errno_location;
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
const MAX_BYTES: usize = 1024;
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
for chunk in dest.chunks_mut(MAX_BYTES) {
|
||||
let ptr = chunk.as_mut_ptr().cast::<c_void>();
|
||||
let ret = unsafe { libc::getrandom(ptr, chunk.len(), libc::GRND_RANDOM) };
|
||||
// In case the man page has a typo, we also check for negative ret.
|
||||
// If getrandom(2) succeeds, it should have completely filled chunk.
|
||||
match usize::try_from(ret) {
|
||||
// Good. Keep going.
|
||||
Ok(ret) if ret == chunk.len() => {}
|
||||
// The syscall failed.
|
||||
Ok(0) => {
|
||||
let errno = unsafe { core::ptr::read(errno_location()) };
|
||||
return Err(Error::from_errno(errno));
|
||||
}
|
||||
// All other cases should be impossible.
|
||||
_ => return Err(Error::UNEXPECTED),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
//! Implementation for SOLID
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
unsafe extern "C" {
|
||||
pub fn SOLID_RNG_SampleRandomBytes(buffer: *mut u8, length: usize) -> i32;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
let ret = unsafe { SOLID_RNG_SampleRandomBytes(dest.as_mut_ptr().cast::<u8>(), dest.len()) };
|
||||
if ret >= 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::from_neg_error_code(ret))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//! Implementation that errors at runtime.
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
pub fn fill_inner(_dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
Err(Error::UNSUPPORTED)
|
||||
}
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
//! Implementations that just need to read from a file
|
||||
use crate::Error;
|
||||
use core::{
|
||||
ffi::{CStr, c_void},
|
||||
mem::MaybeUninit,
|
||||
sync::atomic::{AtomicI32, Ordering},
|
||||
};
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "linux")))]
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
#[path = "../utils/sys_fill_exact.rs"]
|
||||
pub(super) mod utils;
|
||||
|
||||
/// For all platforms, we use `/dev/urandom` rather than `/dev/random`.
|
||||
/// For more information see the linked man pages in lib.rs.
|
||||
/// - On Linux, "/dev/urandom is preferred and sufficient in all use cases".
|
||||
/// - On Redox, only /dev/urandom is provided.
|
||||
/// - On AIX, /dev/urandom will "provide cryptographically secure output".
|
||||
/// - On Haiku and QNX Neutrino they are identical.
|
||||
const FILE_PATH: &CStr = c"/dev/urandom";
|
||||
|
||||
// File descriptor is a "nonnegative integer", so we can safely use negative sentinel values.
|
||||
const FD_UNINIT: libc::c_int = -1;
|
||||
const FD_ONGOING_INIT: libc::c_int = -2;
|
||||
|
||||
// In theory `libc::c_int` could be something other than `i32`, but for the
|
||||
// targets we currently support that use `use_file`, it is always `i32`.
|
||||
// If/when we add support for a target where that isn't the case, we may
|
||||
// need to use a different atomic type or make other accommodations. The
|
||||
// compiler will let us know if/when that is the case, because the
|
||||
// `FD.store(fd)` would fail to compile.
|
||||
//
|
||||
// The opening of the file, by libc/libstd/etc. may write some unknown
|
||||
// state into in-process memory. (Such state may include some sanitizer
|
||||
// bookkeeping, or we might be operating in a unikernal-like environment
|
||||
// where all the "kernel" file descriptor bookkeeping is done in our
|
||||
// process.) `get_fd_locked` stores into FD using `Ordering::Release` to
|
||||
// ensure any such state is synchronized. `get_fd` loads from `FD` with
|
||||
// `Ordering::Acquire` to synchronize with it.
|
||||
static FD: AtomicI32 = AtomicI32::new(FD_UNINIT);
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
let mut fd = FD.load(Ordering::Acquire);
|
||||
if fd == FD_UNINIT || fd == FD_ONGOING_INIT {
|
||||
fd = open_or_wait()?;
|
||||
}
|
||||
utils::sys_fill_exact(dest, |buf| unsafe {
|
||||
libc::read(fd, buf.as_mut_ptr().cast::<c_void>(), buf.len())
|
||||
})
|
||||
}
|
||||
|
||||
/// Open a file in read-only mode.
|
||||
fn open_readonly(path: &CStr) -> Result<libc::c_int, Error> {
|
||||
loop {
|
||||
let fd = unsafe { libc::open(path.as_ptr(), libc::O_RDONLY | libc::O_CLOEXEC) };
|
||||
if fd >= 0 {
|
||||
return Ok(fd);
|
||||
}
|
||||
let errno = utils::get_errno();
|
||||
// We should try again if open() was interrupted.
|
||||
if errno != libc::EINTR {
|
||||
return Err(Error::from_errno(errno));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
fn open_or_wait() -> Result<libc::c_int, Error> {
|
||||
loop {
|
||||
match FD.load(Ordering::Acquire) {
|
||||
FD_UNINIT => {
|
||||
let res = FD.compare_exchange_weak(
|
||||
FD_UNINIT,
|
||||
FD_ONGOING_INIT,
|
||||
Ordering::AcqRel,
|
||||
Ordering::Relaxed,
|
||||
);
|
||||
if res.is_ok() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
FD_ONGOING_INIT => sync::wait(),
|
||||
fd => return Ok(fd),
|
||||
}
|
||||
}
|
||||
|
||||
let res = open_fd();
|
||||
let val = match res {
|
||||
Ok(fd) => fd,
|
||||
Err(_) => FD_UNINIT,
|
||||
};
|
||||
FD.store(val, Ordering::Release);
|
||||
|
||||
// On non-Linux targets `wait` is just 1 ms sleep,
|
||||
// so we don't need any explicit wake up in addition
|
||||
// to updating value of `FD`.
|
||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||
sync::wake();
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
fn open_fd() -> Result<libc::c_int, Error> {
|
||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||
sync::wait_until_rng_ready()?;
|
||||
let fd = open_readonly(FILE_PATH)?;
|
||||
debug_assert!(fd >= 0);
|
||||
Ok(fd)
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "linux")))]
|
||||
mod sync {
|
||||
/// Sleep 1 ms before checking `FD` again.
|
||||
///
|
||||
/// On non-Linux targets the critical section only opens file,
|
||||
/// which should not block, so in the unlikely contended case,
|
||||
/// we can sleep-wait for the opening operation to finish.
|
||||
pub(super) fn wait() {
|
||||
let rqtp = libc::timespec {
|
||||
tv_sec: 0,
|
||||
tv_nsec: 1_000_000,
|
||||
};
|
||||
let mut rmtp = libc::timespec {
|
||||
tv_sec: 0,
|
||||
tv_nsec: 0,
|
||||
};
|
||||
// We do not care if sleep gets interrupted, so the return value is ignored
|
||||
unsafe {
|
||||
libc::nanosleep(&rqtp, &mut rmtp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||
mod sync {
|
||||
use super::{Error, FD, FD_ONGOING_INIT, open_readonly, utils};
|
||||
|
||||
/// Wait for atomic `FD` to change value from `FD_ONGOING_INIT` to something else.
|
||||
///
|
||||
/// Futex syscall with `FUTEX_WAIT` op puts the current thread to sleep
|
||||
/// until futex syscall with `FUTEX_WAKE` op gets executed for `FD`.
|
||||
///
|
||||
/// For more information read: https://www.man7.org/linux/man-pages/man2/futex.2.html
|
||||
pub(super) fn wait() {
|
||||
let op = libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG;
|
||||
let timeout_ptr = core::ptr::null::<libc::timespec>();
|
||||
let ret = unsafe { libc::syscall(libc::SYS_futex, &FD, op, FD_ONGOING_INIT, timeout_ptr) };
|
||||
// FUTEX_WAIT should return either 0 or EAGAIN error
|
||||
debug_assert!({
|
||||
match ret {
|
||||
0 => true,
|
||||
-1 => utils::get_errno() == libc::EAGAIN,
|
||||
_ => false,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Wake up all threads which wait for value of atomic `FD` to change.
|
||||
pub(super) fn wake() {
|
||||
let op = libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG;
|
||||
let ret = unsafe { libc::syscall(libc::SYS_futex, &FD, op, libc::INT_MAX) };
|
||||
debug_assert!(ret >= 0);
|
||||
}
|
||||
|
||||
// Polls /dev/random to make sure it is ok to read from /dev/urandom.
|
||||
//
|
||||
// Polling avoids draining the estimated entropy from /dev/random;
|
||||
// short-lived processes reading even a single byte from /dev/random could
|
||||
// be problematic if they are being executed faster than entropy is being
|
||||
// collected.
|
||||
//
|
||||
// OTOH, reading a byte instead of polling is more compatible with
|
||||
// sandboxes that disallow `poll()` but which allow reading /dev/random,
|
||||
// e.g. sandboxes that assume that `poll()` is for network I/O. This way,
|
||||
// fewer applications will have to insert pre-sandbox-initialization logic.
|
||||
// Often (blocking) file I/O is not allowed in such early phases of an
|
||||
// application for performance and/or security reasons.
|
||||
//
|
||||
// It is hard to write a sandbox policy to support `libc::poll()` because
|
||||
// it may invoke the `poll`, `ppoll`, `ppoll_time64` (since Linux 5.1, with
|
||||
// newer versions of glibc), and/or (rarely, and probably only on ancient
|
||||
// systems) `select`. depending on the libc implementation (e.g. glibc vs
|
||||
// musl), libc version, potentially the kernel version at runtime, and/or
|
||||
// the target architecture.
|
||||
//
|
||||
// BoringSSL and libstd don't try to protect against insecure output from
|
||||
// `/dev/urandom'; they don't open `/dev/random` at all.
|
||||
//
|
||||
// OpenSSL uses `libc::select()` unless the `dev/random` file descriptor
|
||||
// is too large; if it is too large then it does what we do here.
|
||||
//
|
||||
// libsodium uses `libc::poll` similarly to this.
|
||||
pub(super) fn wait_until_rng_ready() -> Result<(), Error> {
|
||||
let fd = open_readonly(c"/dev/random")?;
|
||||
let mut pfd = libc::pollfd {
|
||||
fd,
|
||||
events: libc::POLLIN,
|
||||
revents: 0,
|
||||
};
|
||||
|
||||
let res = loop {
|
||||
// A negative timeout means an infinite timeout.
|
||||
let res = unsafe { libc::poll(&mut pfd, 1, -1) };
|
||||
if res >= 0 {
|
||||
// We only used one fd, and cannot timeout.
|
||||
debug_assert_eq!(res, 1);
|
||||
break Ok(());
|
||||
}
|
||||
let errno = utils::get_errno();
|
||||
// Assuming that `poll` is called correctly,
|
||||
// on Linux it can return only EINTR and ENOMEM errors.
|
||||
match errno {
|
||||
libc::EINTR => continue,
|
||||
_ => break Err(Error::from_errno(errno)),
|
||||
}
|
||||
};
|
||||
unsafe { libc::close(fd) };
|
||||
res
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
//! Implementation for VxWorks
|
||||
use crate::Error;
|
||||
use core::{
|
||||
cmp::Ordering::{Equal, Greater, Less},
|
||||
mem::MaybeUninit,
|
||||
sync::atomic::{AtomicBool, Ordering::Relaxed},
|
||||
};
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
static RNG_INIT: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
#[cold]
|
||||
fn init() -> Result<(), Error> {
|
||||
let ret = unsafe { libc::randSecure() };
|
||||
match ret.cmp(&0) {
|
||||
Greater => RNG_INIT.store(true, Relaxed),
|
||||
Equal => unsafe {
|
||||
libc::usleep(10);
|
||||
},
|
||||
Less => return Err(Error::VXWORKS_RAND_SECURE),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
while !RNG_INIT.load(Relaxed) {
|
||||
init()?;
|
||||
}
|
||||
|
||||
// Prevent overflow of i32
|
||||
let chunk_size = usize::try_from(i32::MAX).expect("VxWorks does not support 16-bit targets");
|
||||
for chunk in dest.chunks_mut(chunk_size) {
|
||||
let chunk_len: libc::c_int = chunk
|
||||
.len()
|
||||
.try_into()
|
||||
.expect("chunk size is bounded by i32::MAX");
|
||||
let p: *mut libc::c_uchar = chunk.as_mut_ptr().cast();
|
||||
let ret = unsafe { libc::randABytes(p, chunk_len) };
|
||||
match ret {
|
||||
0 => continue,
|
||||
-1 => {
|
||||
let errno = unsafe { libc::errnoGet() };
|
||||
return Err(Error::from_errno(errno));
|
||||
}
|
||||
_ => return Err(Error::UNEXPECTED),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// On VxWorks, call to `randSecure` failed (random number generator is not yet initialized).
|
||||
pub(crate) const VXWORKS_RAND_SECURE: Error = Self::new_internal(10);
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
//! Implementation for WASI Preview 1
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
// This linking is vendored from the wasi crate:
|
||||
// https://docs.rs/wasi/0.11.0+wasi-snapshot-preview1/src/wasi/lib_generated.rs.html#2344-2350
|
||||
#[link(wasm_import_module = "wasi_snapshot_preview1")]
|
||||
unsafe extern "C" {
|
||||
fn random_get(arg0: i32, arg1: i32) -> i32;
|
||||
}
|
||||
|
||||
/// WASI p1 uses `u16` for error codes in its witx definitions:
|
||||
/// https://github.com/WebAssembly/WASI/blob/38454e9e/legacy/preview1/witx/typenames.witx#L34-L39
|
||||
const MAX_ERROR_CODE: i32 = u16::MAX as i32;
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
// Based on the wasi code:
|
||||
// https://docs.rs/wasi/0.11.0+wasi-snapshot-preview1/src/wasi/lib_generated.rs.html#2046-2062
|
||||
// Note that size of an allocated object can not be bigger than isize::MAX bytes.
|
||||
// WASI 0.1 supports only 32-bit WASM, so casting length to `i32` is safe.
|
||||
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
|
||||
let ret = unsafe { random_get(dest.as_mut_ptr() as i32, dest.len() as i32) };
|
||||
match ret {
|
||||
0 => Ok(()),
|
||||
// WASI functions should return positive error codes which are smaller than `MAX_ERROR_CODE`
|
||||
code if code <= MAX_ERROR_CODE => Err(Error::from_neg_error_code(-code)),
|
||||
_ => Err(Error::UNEXPECTED),
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
//! Implementation for WASIp2 and WASIp3.
|
||||
use crate::Error;
|
||||
use core::{mem::MaybeUninit, ptr::copy_nonoverlapping};
|
||||
|
||||
#[cfg(not(target_env = "p2"))]
|
||||
#[cfg(not(target_env = "p3"))]
|
||||
compile_error!("Unknown version of WASI (only previews 1, 2 and 3 are supported)");
|
||||
|
||||
// Use manual bindings instead of `wasip2/3` crates to prevent Cargo.lock bloat.
|
||||
//
|
||||
// Technically, such manual bindings may not work since we also have to provide
|
||||
// type information which is expected by `wasm-component-ld`, but we assume that
|
||||
// in practice the relevant type information will be provided by linking to `std`
|
||||
// and/or `wasip2/3` crate.
|
||||
//
|
||||
// See https://github.com/rust-random/getrandom/pull/828 for more information.
|
||||
#[cfg_attr(
|
||||
target_env = "p2",
|
||||
link(wasm_import_module = "wasi:random/random@0.2.0")
|
||||
)]
|
||||
// TODO(MSRV-1.92): Use `target_env = "p3"`
|
||||
#[cfg_attr(
|
||||
not(target_env = "p2"),
|
||||
link(wasm_import_module = "wasi:random/random@0.3.0")
|
||||
)]
|
||||
unsafe extern "C" {
|
||||
#[link_name = "get-random-u64"]
|
||||
safe fn get_random_u64() -> u64;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn inner_u32() -> Result<u32, Error> {
|
||||
let val = get_random_u64();
|
||||
Ok(crate::util::truncate(val))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn inner_u64() -> Result<u64, Error> {
|
||||
Ok(get_random_u64())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
let (prefix, chunks, suffix) = unsafe { dest.align_to_mut::<MaybeUninit<u64>>() };
|
||||
|
||||
// We use `get_random_u64` instead of `get_random_bytes` because the latter creates
|
||||
// an allocation due to the Wit IDL [restrictions][0]. This should be fine since
|
||||
// the main use case of `getrandom` is seed generation.
|
||||
//
|
||||
// [0]: https://github.com/WebAssembly/wasi-random/issues/27
|
||||
if !prefix.is_empty() {
|
||||
let val = get_random_u64();
|
||||
let src = (&val as *const u64).cast();
|
||||
unsafe {
|
||||
copy_nonoverlapping(src, prefix.as_mut_ptr(), prefix.len());
|
||||
}
|
||||
}
|
||||
|
||||
for dst in chunks {
|
||||
dst.write(get_random_u64());
|
||||
}
|
||||
|
||||
if !suffix.is_empty() {
|
||||
let val = get_random_u64();
|
||||
let src = (&val as *const u64).cast();
|
||||
unsafe {
|
||||
copy_nonoverlapping(src, suffix.as_mut_ptr(), suffix.len());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
//! Implementation for WASM based on Web and Node.js
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
#[cfg(not(all(target_family = "wasm", any(target_os = "unknown", target_os = "none"),)))]
|
||||
compile_error!("`wasm_js` backend can be enabled only for OS-less WASM targets!");
|
||||
|
||||
use wasm_bindgen::{JsValue, prelude::wasm_bindgen};
|
||||
|
||||
// Maximum buffer size allowed in `Crypto.getRandomValuesSize` is 65536 bytes.
|
||||
// See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
|
||||
const MAX_BUFFER_SIZE: usize = 65536;
|
||||
|
||||
#[cfg(not(target_feature = "atomics"))]
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
for chunk in dest.chunks_mut(MAX_BUFFER_SIZE) {
|
||||
if get_random_values(chunk).is_err() {
|
||||
return Err(Error::WEB_CRYPTO);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(target_feature = "atomics")]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
// getRandomValues does not work with all types of WASM memory,
|
||||
// so we initially write to browser memory to avoid exceptions.
|
||||
let buf_len = usize::min(dest.len(), MAX_BUFFER_SIZE);
|
||||
let buf_len_u32 = buf_len
|
||||
.try_into()
|
||||
.expect("buffer length is bounded by MAX_BUFFER_SIZE");
|
||||
let buf = js_sys::Uint8Array::new_with_length(buf_len_u32);
|
||||
for chunk in dest.chunks_mut(buf_len) {
|
||||
let chunk_len = chunk
|
||||
.len()
|
||||
.try_into()
|
||||
.expect("chunk length is bounded by MAX_BUFFER_SIZE");
|
||||
// The chunk can be smaller than buf's length, so we call to
|
||||
// JS to create a smaller view of buf without allocation.
|
||||
let sub_buf = if chunk_len == buf_len_u32 {
|
||||
&buf
|
||||
} else {
|
||||
&buf.subarray(0, chunk_len)
|
||||
};
|
||||
|
||||
if get_random_values(sub_buf).is_err() {
|
||||
return Err(Error::WEB_CRYPTO);
|
||||
}
|
||||
|
||||
sub_buf.copy_to_uninit(chunk);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
unsafe extern "C" {
|
||||
// Crypto.getRandomValues()
|
||||
#[cfg(not(target_feature = "atomics"))]
|
||||
#[wasm_bindgen(js_namespace = ["globalThis", "crypto"], js_name = getRandomValues, catch)]
|
||||
fn get_random_values(buf: &mut [MaybeUninit<u8>]) -> Result<(), JsValue>;
|
||||
#[cfg(target_feature = "atomics")]
|
||||
#[wasm_bindgen(js_namespace = ["globalThis", "crypto"], js_name = getRandomValues, catch)]
|
||||
fn get_random_values(buf: &js_sys::Uint8Array) -> Result<(), JsValue>;
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// The environment does not support the Web Crypto API.
|
||||
pub(crate) const WEB_CRYPTO: Error = Self::new_internal(10);
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
//! Implementation for Windows 10 and later
|
||||
//!
|
||||
//! On Windows 10 and later, ProcessPrng "is the primary interface to the
|
||||
//! user-mode per-processor PRNGs" and only requires bcryptprimitives.dll,
|
||||
//! making it a better option than the other Windows RNG APIs:
|
||||
//! - BCryptGenRandom: https://learn.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
|
||||
//! - Requires bcrypt.dll (which loads bcryptprimitives.dll anyway)
|
||||
//! - Can cause crashes/hangs as BCrypt accesses the Windows Registry:
|
||||
//! https://github.com/rust-lang/rust/issues/99341
|
||||
//! - Causes issues inside sandboxed code:
|
||||
//! https://issues.chromium.org/issues/40277768
|
||||
//! - CryptGenRandom: https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-cryptgenrandom
|
||||
//! - Deprecated and not available on UWP targets
|
||||
//! - Requires advapi32.lib/advapi32.dll (in addition to bcryptprimitives.dll)
|
||||
//! - Thin wrapper around ProcessPrng
|
||||
//! - RtlGenRandom: https://learn.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom
|
||||
//! - Deprecated and not available on UWP targets
|
||||
//! - Requires advapi32.dll (in addition to bcryptprimitives.dll)
|
||||
//! - Requires using name "SystemFunction036"
|
||||
//! - Thin wrapper around ProcessPrng
|
||||
//!
|
||||
//! For more information see the Windows RNG Whitepaper: https://aka.ms/win10rng
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
// Binding to the Windows.Win32.Security.Cryptography.ProcessPrng API. As
|
||||
// bcryptprimitives.dll lacks an import library, we use "raw-dylib".
|
||||
#[cfg_attr(
|
||||
target_arch = "x86",
|
||||
link(
|
||||
name = "bcryptprimitives",
|
||||
kind = "raw-dylib",
|
||||
import_name_type = "undecorated"
|
||||
)
|
||||
)]
|
||||
#[cfg_attr(
|
||||
not(target_arch = "x86"),
|
||||
link(name = "bcryptprimitives", kind = "raw-dylib")
|
||||
)]
|
||||
unsafe extern "system" {
|
||||
fn ProcessPrng(pbdata: *mut u8, cbdata: usize) -> BOOL;
|
||||
}
|
||||
#[expect(clippy::upper_case_acronyms)]
|
||||
type BOOL = core::ffi::c_int;
|
||||
const TRUE: BOOL = 1;
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
let result = unsafe { ProcessPrng(dest.as_mut_ptr().cast::<u8>(), dest.len()) };
|
||||
// On Windows 10 and later, `ProcessPrng` is documented to always return
|
||||
// TRUE. All potential errors are handled during loading of
|
||||
// `BCryptPrimitive.dll`. See the "Process base PRNG" section in the
|
||||
// aforementioned Windows RNG whitepaper for more information.
|
||||
//
|
||||
// The Zig project found that Windows 8 implements `ProcessPrng` in a way
|
||||
// that may fail and return a value other than `TRUE`. Although recent
|
||||
// versions of the Rust toolchain do not support Windows 8, we cannot rule
|
||||
// out this backend being used in an executable that will run on Windows 8
|
||||
// (e.g. a fork of this crate backported to have an MSRV lower than 1.76,
|
||||
// or a fork of the Rust toolchain to support older Windows versions, or
|
||||
// other build hacks).
|
||||
//
|
||||
// Further, Wine's implementation of `ProcessPrng` CAN fail, in every
|
||||
// version through Wine 11.2, and this may be the case for any other Windows
|
||||
// emulation layers.
|
||||
if result == TRUE {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error::UNEXPECTED)
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
//! Legacy implementation for Windows XP and later
|
||||
//!
|
||||
//! For targets where we cannot use ProcessPrng (added in Windows 10), we use
|
||||
//! RtlGenRandom. See windows.rs for a more detailed discussion of the Windows
|
||||
//! RNG APIs (and why we don't use BCryptGenRandom). On versions prior to
|
||||
//! Windows 10, this implementation is secure. On Windows 10 and later, this
|
||||
//! implementation behaves identically to the windows.rs implementation, except
|
||||
//! that it forces the loading of an additional DLL (advapi32.dll).
|
||||
//!
|
||||
//! This implementation will not work on UWP targets (which lack advapi32.dll),
|
||||
//! but such targets require Windows 10, so can use the standard implementation.
|
||||
use crate::Error;
|
||||
use core::{ffi::c_void, mem::MaybeUninit};
|
||||
|
||||
pub use crate::util::{inner_u32, inner_u64};
|
||||
|
||||
#[cfg(not(windows))]
|
||||
compile_error!("`windows_legacy` backend can be enabled only for Windows targets!");
|
||||
|
||||
// Binding to the Windows.Win32.Security.Authentication.Identity.RtlGenRandom
|
||||
// API. Don't use windows-targets as it doesn't support Windows 7 targets.
|
||||
#[link(name = "advapi32")]
|
||||
unsafe extern "system" {
|
||||
#[link_name = "SystemFunction036"]
|
||||
fn RtlGenRandom(randombuffer: *mut c_void, randombufferlength: u32) -> BOOLEAN;
|
||||
}
|
||||
#[allow(clippy::upper_case_acronyms)]
|
||||
type BOOLEAN = u8;
|
||||
const TRUE: BOOLEAN = 1u8;
|
||||
|
||||
#[inline]
|
||||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
|
||||
// Prevent overflow of u32
|
||||
let chunk_size = usize::try_from(i32::MAX).expect("Windows does not support 16-bit targets");
|
||||
for chunk in dest.chunks_mut(chunk_size) {
|
||||
let chunk_len = u32::try_from(chunk.len()).expect("chunk size is bounded by i32::MAX");
|
||||
let ret = unsafe { RtlGenRandom(chunk.as_mut_ptr().cast::<c_void>(), chunk_len) };
|
||||
if ret != TRUE {
|
||||
return Err(Error::WINDOWS_RTL_GEN_RANDOM);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Call to Windows [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom) failed.
|
||||
pub(crate) const WINDOWS_RTL_GEN_RANDOM: Error = Self::new_internal(10);
|
||||
}
|
||||
Vendored
+245
@@ -0,0 +1,245 @@
|
||||
#[cfg(feature = "std")]
|
||||
extern crate std;
|
||||
|
||||
use core::fmt;
|
||||
|
||||
cfg_if::cfg_if!(
|
||||
if #[cfg(target_os = "uefi")] {
|
||||
// See the UEFI spec for more information:
|
||||
// https://uefi.org/specs/UEFI/2.10/Apx_D_Status_Codes.html
|
||||
|
||||
/// Raw error code.
|
||||
///
|
||||
/// This alias mirrors unstable [`std::io::RawOsError`].
|
||||
///
|
||||
/// [`std::io::RawOsError`]: https://doc.rust-lang.org/std/io/type.RawOsError.html
|
||||
pub type RawOsError = usize;
|
||||
type NonZeroRawOsError = core::num::NonZeroUsize;
|
||||
const UEFI_ERROR_FLAG: RawOsError = 1 << (RawOsError::BITS - 1);
|
||||
} else {
|
||||
/// Raw error code.
|
||||
///
|
||||
/// This alias mirrors unstable [`std::io::RawOsError`].
|
||||
///
|
||||
/// [`std::io::RawOsError`]: https://doc.rust-lang.org/std/io/type.RawOsError.html
|
||||
pub type RawOsError = i32;
|
||||
type NonZeroRawOsError = core::num::NonZeroI32;
|
||||
}
|
||||
);
|
||||
|
||||
/// A small and `no_std` compatible error type
|
||||
///
|
||||
/// The [`Error::raw_os_error()`] will indicate if the error is from the OS, and
|
||||
/// if so, which error code the OS gave the application. If such an error is
|
||||
/// encountered, please consult with your system documentation.
|
||||
///
|
||||
/// *If this crate's `"std"` Cargo feature is enabled*, then:
|
||||
/// - [`getrandom::Error`][Error] implements
|
||||
/// [`std::error::Error`](https://doc.rust-lang.org/std/error/trait.Error.html)
|
||||
/// - [`std::io::Error`](https://doc.rust-lang.org/std/io/struct.Error.html) implements
|
||||
/// [`From<getrandom::Error>`](https://doc.rust-lang.org/std/convert/trait.From.html).
|
||||
|
||||
// note: on non-UEFI targets OS errors are represented as negative integers,
|
||||
// while on UEFI targets OS errors have the highest bit set to 1.
|
||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||
pub struct Error(NonZeroRawOsError);
|
||||
|
||||
impl Error {
|
||||
/// This target/platform is not supported by `getrandom`.
|
||||
pub const UNSUPPORTED: Error = Self::new_internal(0);
|
||||
/// The platform-specific `errno` returned a non-positive value.
|
||||
pub const ERRNO_NOT_POSITIVE: Error = Self::new_internal(1);
|
||||
/// Encountered an unexpected situation which should not happen in practice.
|
||||
pub const UNEXPECTED: Error = Self::new_internal(2);
|
||||
|
||||
/// Internal errors can be in the range of 2^16..2^17
|
||||
const INTERNAL_START: RawOsError = 1 << 16;
|
||||
/// Custom errors can be in the range of 2^17..(2^17 + 2^16)
|
||||
const CUSTOM_START: RawOsError = 1 << 17;
|
||||
|
||||
/// Creates a new `Error` instance from a positive error code.
|
||||
///
|
||||
/// Returns [`Error::ERRNO_NOT_POSITIVE`] for zero and negative error codes.
|
||||
#[cfg(not(target_os = "uefi"))]
|
||||
#[allow(dead_code)]
|
||||
pub(super) fn from_errno(errno: i32) -> Self {
|
||||
if errno > 0 {
|
||||
let code = errno
|
||||
.checked_neg()
|
||||
.expect("Positive number can be always negated");
|
||||
Error::from_neg_error_code(code)
|
||||
} else {
|
||||
Error::ERRNO_NOT_POSITIVE
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new `Error` instance from a negative error code.
|
||||
///
|
||||
/// Returns [`Error::UNEXPECTED`] for zero and positive error codes.
|
||||
#[cfg(not(target_os = "uefi"))]
|
||||
#[allow(dead_code)]
|
||||
pub(super) fn from_neg_error_code(code: RawOsError) -> Self {
|
||||
if code < 0 {
|
||||
let code = NonZeroRawOsError::new(code).expect("`code` is negative");
|
||||
Self(code)
|
||||
} else {
|
||||
Error::UNEXPECTED
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new instance of an `Error` from an UEFI error code.
|
||||
#[cfg(target_os = "uefi")]
|
||||
#[allow(dead_code)]
|
||||
pub(super) fn from_uefi_code(code: RawOsError) -> Self {
|
||||
if code & UEFI_ERROR_FLAG != 0 {
|
||||
let code = NonZeroRawOsError::new(code).expect("The highest bit of `code` is set to 1");
|
||||
Self(code)
|
||||
} else {
|
||||
Self::UNEXPECTED
|
||||
}
|
||||
}
|
||||
|
||||
/// Extract the raw OS error code (if this error came from the OS)
|
||||
///
|
||||
/// This method is identical to [`std::io::Error::raw_os_error()`][1], except
|
||||
/// that it works in `no_std` contexts. On most targets this method returns
|
||||
/// `Option<i32>`, but some platforms (e.g. UEFI) may use a different primitive
|
||||
/// type like `usize`. Consult with the [`RawOsError`] docs for more information.
|
||||
///
|
||||
/// If this method returns `None`, the error value can still be formatted via
|
||||
/// the `Display` implementation.
|
||||
///
|
||||
/// [1]: https://doc.rust-lang.org/std/io/struct.Error.html#method.raw_os_error
|
||||
/// [`RawOsError`]: https://doc.rust-lang.org/std/io/type.RawOsError.html
|
||||
#[inline]
|
||||
pub fn raw_os_error(self) -> Option<RawOsError> {
|
||||
let code = self.0.get();
|
||||
|
||||
// note: in this method we need to cover only backends which rely on
|
||||
// `Error::{from_error_code, from_errno, from_uefi_code}` methods,
|
||||
// on all other backends this method always returns `None`.
|
||||
|
||||
#[cfg(target_os = "uefi")]
|
||||
{
|
||||
if code & UEFI_ERROR_FLAG != 0 {
|
||||
Some(code)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "uefi"))]
|
||||
{
|
||||
// On most targets `std` expects positive error codes while retrieving error strings:
|
||||
// - `libc`-based targets use `strerror_r` which expects positive error codes.
|
||||
// - Hermit relies on the `hermit-abi` crate, which expects positive error codes:
|
||||
// https://docs.rs/hermit-abi/0.4.0/src/hermit_abi/errno.rs.html#400-532
|
||||
// - WASIp1 uses the same conventions as `libc`:
|
||||
// https://github.com/rust-lang/rust/blob/1.85.0/library/std/src/sys/pal/wasi/os.rs#L57-L67
|
||||
//
|
||||
// The only exception is Solid, `std` expects negative system error codes, see:
|
||||
// https://github.com/rust-lang/rust/blob/1.85.0/library/std/src/sys/pal/solid/error.rs#L5-L31
|
||||
if code >= 0 {
|
||||
None
|
||||
} else if cfg!(not(target_os = "solid_asp3")) {
|
||||
code.checked_neg()
|
||||
} else {
|
||||
Some(code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new instance of an `Error` from a particular custom error code.
|
||||
pub const fn new_custom(n: u16) -> Error {
|
||||
// SAFETY: code > 0 as CUSTOM_START > 0 and adding `n` won't overflow `RawOsError`.
|
||||
let code = Error::CUSTOM_START + (n as RawOsError);
|
||||
Error(unsafe { NonZeroRawOsError::new_unchecked(code) })
|
||||
}
|
||||
|
||||
/// Creates a new instance of an `Error` from a particular internal error code.
|
||||
pub(crate) const fn new_internal(n: u16) -> Error {
|
||||
// SAFETY: code > 0 as INTERNAL_START > 0 and adding `n` won't overflow `RawOsError`.
|
||||
let code = Error::INTERNAL_START + (n as RawOsError);
|
||||
Error(unsafe { NonZeroRawOsError::new_unchecked(code) })
|
||||
}
|
||||
|
||||
fn internal_desc(&self) -> Option<&'static str> {
|
||||
let desc = match *self {
|
||||
Error::UNSUPPORTED => "getrandom: this target is not supported",
|
||||
Error::ERRNO_NOT_POSITIVE => "errno: did not return a positive value",
|
||||
Error::UNEXPECTED => "unexpected situation",
|
||||
#[cfg(any(
|
||||
target_os = "ios",
|
||||
target_os = "visionos",
|
||||
target_os = "watchos",
|
||||
target_os = "tvos",
|
||||
))]
|
||||
Error::IOS_RANDOM_GEN => "SecRandomCopyBytes: iOS Security framework failure",
|
||||
#[cfg(all(windows, target_vendor = "win7"))]
|
||||
Error::WINDOWS_RTL_GEN_RANDOM => "RtlGenRandom: Windows system function failure",
|
||||
#[cfg(all(
|
||||
feature = "wasm_js",
|
||||
target_arch = "wasm32",
|
||||
any(target_os = "unknown", target_os = "none")
|
||||
))]
|
||||
Error::WEB_CRYPTO => "Web Crypto API is unavailable",
|
||||
#[cfg(target_os = "vxworks")]
|
||||
Error::VXWORKS_RAND_SECURE => "randSecure: VxWorks RNG module is not initialized",
|
||||
|
||||
#[cfg(any(
|
||||
getrandom_backend = "rdrand",
|
||||
all(target_arch = "x86_64", target_env = "sgx")
|
||||
))]
|
||||
Error::FAILED_RDRAND => "RDRAND: failed multiple times: CPU issue likely",
|
||||
#[cfg(any(
|
||||
getrandom_backend = "rdrand",
|
||||
all(target_arch = "x86_64", target_env = "sgx")
|
||||
))]
|
||||
Error::NO_RDRAND => "RDRAND: instruction not supported",
|
||||
|
||||
#[cfg(getrandom_backend = "rndr")]
|
||||
Error::RNDR_FAILURE => "RNDR: Could not generate a random number",
|
||||
#[cfg(getrandom_backend = "rndr")]
|
||||
Error::RNDR_NOT_AVAILABLE => "RNDR: Register not supported",
|
||||
_ => return None,
|
||||
};
|
||||
Some(desc)
|
||||
}
|
||||
}
|
||||
|
||||
impl core::error::Error for Error {}
|
||||
|
||||
impl fmt::Debug for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut dbg = f.debug_struct("Error");
|
||||
if let Some(errno) = self.raw_os_error() {
|
||||
dbg.field("os_error", &errno);
|
||||
#[cfg(feature = "std")]
|
||||
dbg.field("description", &std::io::Error::from_raw_os_error(errno));
|
||||
} else if let Some(desc) = self.internal_desc() {
|
||||
dbg.field("internal_code", &self.0.get());
|
||||
dbg.field("description", &desc);
|
||||
} else {
|
||||
dbg.field("unknown_code", &self.0.get());
|
||||
}
|
||||
dbg.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if let Some(errno) = self.raw_os_error() {
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "std")] {
|
||||
std::io::Error::from_raw_os_error(errno).fmt(f)
|
||||
} else {
|
||||
write!(f, "OS Error: {errno}")
|
||||
}
|
||||
}
|
||||
} else if let Some(desc) = self.internal_desc() {
|
||||
f.write_str(desc)
|
||||
} else {
|
||||
write!(f, "Unknown Error: {}", self.0.get())
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
extern crate std;
|
||||
|
||||
use crate::Error;
|
||||
use std::io;
|
||||
|
||||
impl From<Error> for io::Error {
|
||||
fn from(err: Error) -> Self {
|
||||
match err.raw_os_error() {
|
||||
Some(errno) => io::Error::from_raw_os_error(errno),
|
||||
None => io::Error::other(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+159
@@ -0,0 +1,159 @@
|
||||
// Overwrite links to crate items with intra-crate links
|
||||
//! [`Error::UNEXPECTED`]: Error::UNEXPECTED
|
||||
//! [`fill_uninit`]: fill_uninit
|
||||
|
||||
#![no_std]
|
||||
#![doc(
|
||||
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
|
||||
html_favicon_url = "https://www.rust-lang.org/favicon.ico"
|
||||
)]
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![cfg_attr(getrandom_backend = "efi_rng", feature(uefi_std))]
|
||||
#![cfg_attr(getrandom_backend = "extern_impl", feature(extern_item_impls))]
|
||||
|
||||
#[macro_use]
|
||||
extern crate cfg_if;
|
||||
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
mod backends;
|
||||
mod error;
|
||||
mod util;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod error_std_impls;
|
||||
|
||||
/// `rand_core` adapter
|
||||
#[cfg(feature = "sys_rng")]
|
||||
mod sys_rng;
|
||||
|
||||
#[cfg(feature = "sys_rng")]
|
||||
pub use rand_core;
|
||||
#[cfg(feature = "sys_rng")]
|
||||
pub use sys_rng::SysRng;
|
||||
|
||||
pub use crate::error::{Error, RawOsError};
|
||||
|
||||
/// Attribute macros for overwriting the core functionality of this crate.
|
||||
///
|
||||
/// This allows `getrandom` to provide a default implementation and a common interface
|
||||
/// for all crates to use, while giving users a safe way to override that default where required.
|
||||
///
|
||||
/// Must be enabled via the `extern_impl` opt-in backend, as this functionality
|
||||
/// is currently limited to nightly.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// # use core::mem::MaybeUninit;
|
||||
/// # #[cfg(getrandom_backend = "extern_impl")]
|
||||
/// #[getrandom::implementation::fill_uninit]
|
||||
/// fn my_fill_uninit_implementation(
|
||||
/// dest: &mut [MaybeUninit<u8>]
|
||||
/// ) -> Result<(), getrandom::Error> {
|
||||
/// // ...
|
||||
/// # let _ = dest;
|
||||
/// # Err(Error::UNSUPPORTED)
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg(getrandom_backend = "extern_impl")]
|
||||
pub mod implementation {
|
||||
pub use crate::backends::extern_impl::{fill_uninit, u32, u64};
|
||||
}
|
||||
|
||||
/// Fill `dest` with random bytes from the system's preferred random number source.
|
||||
///
|
||||
/// This function returns an error on any failure, including partial reads. We
|
||||
/// make no guarantees regarding the contents of `dest` on error. If `dest` is
|
||||
/// empty, `getrandom` immediately returns success, making no calls to the
|
||||
/// underlying operating system.
|
||||
///
|
||||
/// Blocking is possible, at least during early boot; see module documentation.
|
||||
///
|
||||
/// In general, `getrandom` will be fast enough for interactive usage, though
|
||||
/// significantly slower than a user-space CSPRNG; for the latter consider
|
||||
/// [`rand::thread_rng`](https://docs.rs/rand/*/rand/fn.thread_rng.html).
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), getrandom::Error> {
|
||||
/// let mut buf = [0u8; 32];
|
||||
/// getrandom::fill(&mut buf)?;
|
||||
/// # Ok(()) }
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn fill(dest: &mut [u8]) -> Result<(), Error> {
|
||||
// SAFETY: The `&mut MaybeUninit<_>` reference doesn't escape,
|
||||
// and `fill_uninit` guarantees it will never de-initialize
|
||||
// any part of `dest`.
|
||||
fill_uninit(unsafe { util::slice_as_uninit_mut(dest) })?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Fill potentially uninitialized buffer `dest` with random bytes from
|
||||
/// the system's preferred random number source and return a mutable
|
||||
/// reference to those bytes.
|
||||
///
|
||||
/// On successful completion this function is guaranteed to return a slice
|
||||
/// which points to the same memory as `dest` and has the same length.
|
||||
/// In other words, it's safe to assume that `dest` is initialized after
|
||||
/// this function has returned `Ok`.
|
||||
///
|
||||
/// No part of `dest` will ever be de-initialized at any point, regardless
|
||||
/// of what is returned.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```ignore
|
||||
/// # // We ignore this test since `uninit_array` is unstable.
|
||||
/// #![feature(maybe_uninit_uninit_array)]
|
||||
/// # fn main() -> Result<(), getrandom::Error> {
|
||||
/// let mut buf = core::mem::MaybeUninit::uninit_array::<1024>();
|
||||
/// let buf: &mut [u8] = getrandom::fill_uninit(&mut buf)?;
|
||||
/// # Ok(()) }
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn fill_uninit(dest: &mut [MaybeUninit<u8>]) -> Result<&mut [u8], Error> {
|
||||
if !dest.is_empty() {
|
||||
backends::fill_inner(dest)?;
|
||||
}
|
||||
|
||||
#[cfg(getrandom_msan)]
|
||||
unsafe extern "C" {
|
||||
fn __msan_unpoison(a: *mut core::ffi::c_void, size: usize);
|
||||
}
|
||||
|
||||
// SAFETY: `dest` has been fully initialized by `imp::fill_inner`
|
||||
// since it returned `Ok`.
|
||||
Ok(unsafe { util::slice_assume_init_mut(dest) })
|
||||
}
|
||||
|
||||
/// Get random `u32` from the system's preferred random number source.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), getrandom::Error> {
|
||||
/// let rng_seed = getrandom::u32()?;
|
||||
/// # Ok(()) }
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn u32() -> Result<u32, Error> {
|
||||
backends::inner_u32()
|
||||
}
|
||||
|
||||
/// Get random `u64` from the system's preferred random number source.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), getrandom::Error> {
|
||||
/// let rng_seed = getrandom::u64()?;
|
||||
/// # Ok(()) }
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn u64() -> Result<u64, Error> {
|
||||
backends::inner_u64()
|
||||
}
|
||||
Vendored
+55
@@ -0,0 +1,55 @@
|
||||
use crate::Error;
|
||||
use rand_core::{TryCryptoRng, TryRng};
|
||||
|
||||
/// A [`TryRng`] interface over the system's preferred random number source
|
||||
///
|
||||
/// This is a zero-sized struct. It can be freely constructed with just `SysRng`.
|
||||
///
|
||||
/// This struct is also available as [`rand::rngs::SysRng`] when using [rand].
|
||||
///
|
||||
/// # Usage example
|
||||
///
|
||||
/// `SysRng` implements [`TryRng`]:
|
||||
/// ```
|
||||
/// use getrandom::{rand_core::TryRng, SysRng};
|
||||
///
|
||||
/// let mut key = [0u8; 32];
|
||||
/// SysRng.try_fill_bytes(&mut key).unwrap();
|
||||
/// ```
|
||||
///
|
||||
/// Using it as an [`Rng`] is possible using [`UnwrapErr`]:
|
||||
/// ```
|
||||
/// use getrandom::rand_core::{Rng, UnwrapErr};
|
||||
/// use getrandom::SysRng;
|
||||
///
|
||||
/// let mut rng = UnwrapErr(SysRng);
|
||||
/// let random_u64 = rng.next_u64();
|
||||
/// ```
|
||||
///
|
||||
/// [rand]: https://crates.io/crates/rand
|
||||
/// [`rand::rngs::SysRng`]: https://docs.rs/rand/latest/rand/rngs/struct.SysRng.html
|
||||
/// [`Rng`]: rand_core::Rng
|
||||
/// [`UnwrapErr`]: rand_core::UnwrapErr
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct SysRng;
|
||||
|
||||
impl TryRng for SysRng {
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn try_next_u32(&mut self) -> Result<u32, Error> {
|
||||
crate::u32()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_next_u64(&mut self) -> Result<u64, Error> {
|
||||
crate::u64()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
|
||||
crate::fill(dest)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryCryptoRng for SysRng {}
|
||||
Vendored
+72
@@ -0,0 +1,72 @@
|
||||
#![allow(dead_code)]
|
||||
use crate::Error;
|
||||
use core::{mem::MaybeUninit, ptr, slice};
|
||||
|
||||
/// Polyfill for `maybe_uninit_slice` feature's
|
||||
/// `MaybeUninit::slice_assume_init_mut`. Every element of `slice` must have
|
||||
/// been initialized.
|
||||
#[inline(always)]
|
||||
pub unsafe fn slice_assume_init_mut<T>(slice: &mut [MaybeUninit<T>]) -> &mut [T] {
|
||||
let ptr = ptr::from_mut(slice) as *mut [T];
|
||||
// SAFETY: `MaybeUninit<T>` is guaranteed to be layout-compatible with `T`.
|
||||
unsafe { &mut *ptr }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn uninit_slice_fill_zero(slice: &mut [MaybeUninit<u8>]) -> &mut [u8] {
|
||||
unsafe { ptr::write_bytes(slice.as_mut_ptr(), 0, slice.len()) };
|
||||
unsafe { slice_assume_init_mut(slice) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn slice_as_uninit<T>(slice: &[T]) -> &[MaybeUninit<T>] {
|
||||
let ptr = ptr::from_ref(slice) as *const [MaybeUninit<T>];
|
||||
// SAFETY: `MaybeUninit<T>` is guaranteed to be layout-compatible with `T`.
|
||||
unsafe { &*ptr }
|
||||
}
|
||||
|
||||
/// View an mutable initialized array as potentially-uninitialized.
|
||||
///
|
||||
/// This is unsafe because it allows assigning uninitialized values into
|
||||
/// `slice`, which would be undefined behavior.
|
||||
#[inline(always)]
|
||||
pub unsafe fn slice_as_uninit_mut<T>(slice: &mut [T]) -> &mut [MaybeUninit<T>] {
|
||||
let ptr = ptr::from_mut(slice) as *mut [MaybeUninit<T>];
|
||||
// SAFETY: `MaybeUninit<T>` is guaranteed to be layout-compatible with `T`.
|
||||
unsafe { &mut *ptr }
|
||||
}
|
||||
|
||||
/// Default implementation of `inner_u32` on top of `fill_uninit`
|
||||
#[inline]
|
||||
pub fn inner_u32() -> Result<u32, Error> {
|
||||
let mut res = MaybeUninit::<u32>::uninit();
|
||||
// SAFETY: the created slice has the same size as `res`
|
||||
let dst = unsafe {
|
||||
let p: *mut MaybeUninit<u8> = res.as_mut_ptr().cast();
|
||||
slice::from_raw_parts_mut(p, core::mem::size_of::<u32>())
|
||||
};
|
||||
crate::fill_uninit(dst)?;
|
||||
// SAFETY: `dst` has been fully initialized by `imp::fill_inner`
|
||||
// since it returned `Ok`.
|
||||
Ok(unsafe { res.assume_init() })
|
||||
}
|
||||
|
||||
/// Default implementation of `inner_u64` on top of `fill_uninit`
|
||||
#[inline]
|
||||
pub fn inner_u64() -> Result<u64, Error> {
|
||||
let mut res = MaybeUninit::<u64>::uninit();
|
||||
// SAFETY: the created slice has the same size as `res`
|
||||
let dst = unsafe {
|
||||
let p: *mut MaybeUninit<u8> = res.as_mut_ptr().cast();
|
||||
slice::from_raw_parts_mut(p, core::mem::size_of::<u64>())
|
||||
};
|
||||
crate::fill_uninit(dst)?;
|
||||
// SAFETY: `dst` has been fully initialized by `imp::fill_inner`
|
||||
// since it returned `Ok`.
|
||||
Ok(unsafe { res.assume_init() })
|
||||
}
|
||||
|
||||
/// Truncates `u64` and returns the lower 32 bits as `u32`
|
||||
pub(crate) fn truncate(val: u64) -> u32 {
|
||||
u32::try_from(val & u64::from(u32::MAX)).expect("The higher 32 bits are masked")
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
cfg_if! {
|
||||
if #[cfg(any(target_os = "netbsd", target_os = "openbsd", target_os = "android", target_os = "cygwin"))] {
|
||||
use libc::__errno as errno_location;
|
||||
} else if #[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "hurd", target_os = "redox", target_os = "dragonfly"))] {
|
||||
use libc::__errno_location as errno_location;
|
||||
} else if #[cfg(target_os = "illumos")] {
|
||||
use libc::___errno as errno_location;
|
||||
} else if #[cfg(any(target_os = "macos", target_os = "freebsd"))] {
|
||||
use libc::__error as errno_location;
|
||||
} else if #[cfg(target_os = "haiku")] {
|
||||
use libc::_errnop as errno_location;
|
||||
} else if #[cfg(target_os = "nto")] {
|
||||
use libc::__get_errno_ptr as errno_location;
|
||||
} else if #[cfg(any(all(target_os = "horizon", target_arch = "arm"), target_os = "vita"))] {
|
||||
unsafe extern "C" {
|
||||
// Not provided by libc: https://github.com/rust-lang/libc/issues/1995
|
||||
fn __errno() -> *mut libc::c_int;
|
||||
}
|
||||
use __errno as errno_location;
|
||||
} else if #[cfg(target_os = "aix")] {
|
||||
use libc::_Errno as errno_location;
|
||||
} else {
|
||||
compile_error!("errno_location is not provided for the target");
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_errno() -> libc::c_int {
|
||||
unsafe { core::ptr::read(errno_location()) }
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
use core::sync::atomic::{AtomicU8, Ordering::Relaxed};
|
||||
|
||||
/// Lazily caches a `bool` in an `AtomicU8`.
|
||||
///
|
||||
/// Initialization is intentionally unsynchronized: concurrent callers may race
|
||||
/// and run `init` more than once. Once a value is produced, it is cached and
|
||||
/// reused by subsequent calls.
|
||||
///
|
||||
/// Uses `Relaxed` ordering because this helper only publishes the cached
|
||||
/// value itself.
|
||||
pub(crate) struct LazyBool(AtomicU8);
|
||||
|
||||
impl LazyBool {
|
||||
const UNINIT: u8 = u8::MAX;
|
||||
|
||||
/// Create new `LazyBool`.
|
||||
pub const fn new() -> Self {
|
||||
Self(AtomicU8::new(Self::UNINIT))
|
||||
}
|
||||
|
||||
/// Call the `init` closure and return the result after caching it.
|
||||
#[cold]
|
||||
fn cold_init(&self, init: impl FnOnce() -> bool) -> bool {
|
||||
let val = u8::from(init());
|
||||
self.0.store(val, Relaxed);
|
||||
val != 0
|
||||
}
|
||||
|
||||
/// Retrieve the cached value if it was already initialized or call the `init` closure
|
||||
/// and return the result after caching it.
|
||||
#[inline]
|
||||
pub fn unsync_init(&self, init: impl FnOnce() -> bool) -> bool {
|
||||
let val = self.0.load(Relaxed);
|
||||
if val == Self::UNINIT {
|
||||
return self.cold_init(init);
|
||||
}
|
||||
val != 0
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
use core::{
|
||||
convert::Infallible,
|
||||
ptr::{self, NonNull},
|
||||
sync::atomic::{AtomicPtr, Ordering::Relaxed},
|
||||
};
|
||||
|
||||
/// Lazily caches a non-null pointer in an `AtomicPtr`.
|
||||
///
|
||||
/// Initialization is intentionally unsynchronized: concurrent callers may race
|
||||
/// and run `init` more than once. Once a value is produced, it is cached and
|
||||
/// reused by subsequent calls.
|
||||
///
|
||||
/// For fallible initialization (`try_unsync_init`), only successful values are
|
||||
/// cached; errors are returned to the caller and are not cached.
|
||||
///
|
||||
/// Uses `Ordering::Relaxed` because this helper only publishes the cached
|
||||
/// pointer value. Callers must not rely on this mechanism to synchronize
|
||||
/// unrelated memory side effects performed by `init`.
|
||||
pub(crate) struct LazyPtr<T>(AtomicPtr<T>);
|
||||
|
||||
impl<T> LazyPtr<T> {
|
||||
/// Create new `LazyPtr`.
|
||||
pub const fn new() -> Self {
|
||||
Self(AtomicPtr::new(ptr::null_mut()))
|
||||
}
|
||||
|
||||
/// Call the `init` closure and return the result after caching it in the case of success.
|
||||
#[cold]
|
||||
fn cold_init<E>(&self, init: impl FnOnce() -> Result<NonNull<T>, E>) -> Result<NonNull<T>, E> {
|
||||
let val = init()?;
|
||||
self.0.store(val.as_ptr(), Relaxed);
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
/// Retrieve the cached value if it was already initialized or call the potentially fallible
|
||||
/// `init` closure and return the result after caching it in the case of success.
|
||||
#[inline]
|
||||
pub fn try_unsync_init<E>(
|
||||
&self,
|
||||
init: impl FnOnce() -> Result<NonNull<T>, E>,
|
||||
) -> Result<NonNull<T>, E> {
|
||||
let p = self.0.load(Relaxed);
|
||||
match NonNull::new(p) {
|
||||
Some(val) => Ok(val),
|
||||
None => self.cold_init(init),
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieve the cached value if it was already initialized or call the `init` closure
|
||||
/// and return the result after caching it.
|
||||
#[inline]
|
||||
#[allow(dead_code, reason = "Some modules use only `try_unsync_init`")]
|
||||
pub fn unsync_init(&self, init: impl FnOnce() -> NonNull<T>) -> NonNull<T> {
|
||||
let Ok(p): Result<_, Infallible> = self.try_unsync_init(|| Ok(init()));
|
||||
p
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
/// Unpoisons `buf` if MSAN support is enabled.
|
||||
///
|
||||
/// Most backends do not need to unpoison their output. Rust language- and
|
||||
/// library- provided functionality unpoisons automatically. Similarly, libc
|
||||
/// either natively supports MSAN and/or MSAN hooks libc-provided functions
|
||||
/// to unpoison outputs on success. Only when all of these things are
|
||||
/// bypassed do we need to do it ourselves.
|
||||
///
|
||||
/// The call to unpoison should be done as close to the write as possible.
|
||||
/// For example, if the backend partially fills the output buffer in chunks,
|
||||
/// each chunk should be unpoisoned individually. This way, the correctness of
|
||||
/// the chunking logic can be validated (in part) using MSAN.
|
||||
pub unsafe fn unpoison(buf: &mut [MaybeUninit<u8>]) {
|
||||
cfg_if! {
|
||||
if #[cfg(getrandom_msan)] {
|
||||
unsafe extern "C" {
|
||||
fn __msan_unpoison(a: *mut core::ffi::c_void, size: usize);
|
||||
}
|
||||
let a = buf.as_mut_ptr().cast();
|
||||
let size = buf.len();
|
||||
unsafe { __msan_unpoison(a, size) };
|
||||
} else {
|
||||
let _ = buf;
|
||||
}
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
use crate::Error;
|
||||
use core::mem::MaybeUninit;
|
||||
|
||||
mod get_errno;
|
||||
mod sanitizer;
|
||||
|
||||
pub(crate) use get_errno::get_errno;
|
||||
|
||||
/// Fill a buffer by repeatedly invoking `sys_fill`.
|
||||
///
|
||||
/// The `sys_fill` function:
|
||||
/// - should return -1 and set errno on failure
|
||||
/// - should return the number of bytes written on success
|
||||
pub(crate) fn sys_fill_exact(
|
||||
mut buf: &mut [MaybeUninit<u8>],
|
||||
sys_fill: impl Fn(&mut [MaybeUninit<u8>]) -> libc::ssize_t,
|
||||
) -> Result<(), Error> {
|
||||
while !buf.is_empty() {
|
||||
let res = sys_fill(buf);
|
||||
match res {
|
||||
res if res > 0 => {
|
||||
let len = usize::try_from(res).map_err(|_| Error::UNEXPECTED)?;
|
||||
let (l, r) = buf.split_at_mut_checked(len).ok_or(Error::UNEXPECTED)?;
|
||||
unsafe { sanitizer::unpoison(l) };
|
||||
buf = r;
|
||||
}
|
||||
-1 => {
|
||||
let errno = get_errno();
|
||||
// We should try again if the call was interrupted.
|
||||
if errno != libc::EINTR {
|
||||
return Err(Error::from_errno(errno));
|
||||
}
|
||||
}
|
||||
// Negative return codes not equal to -1 should be impossible.
|
||||
// EOF (ret = 0) should be impossible, as the data we are reading
|
||||
// should be an infinite stream of random bytes.
|
||||
_ => return Err(Error::UNEXPECTED),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Vendored
+213
@@ -0,0 +1,213 @@
|
||||
//! Main `getrandom` tests
|
||||
use core::mem::MaybeUninit;
|
||||
use getrandom::{fill, fill_uninit};
|
||||
|
||||
#[cfg(all(
|
||||
feature = "wasm_js",
|
||||
target_family = "wasm",
|
||||
any(target_os = "unknown", target_os = "none"),
|
||||
))]
|
||||
use wasm_bindgen_test::wasm_bindgen_test as test;
|
||||
|
||||
#[test]
|
||||
fn test_zero() {
|
||||
// Test that APIs are happy with zero-length requests
|
||||
fill(&mut [0u8; 0]).unwrap();
|
||||
let res = fill_uninit(&mut []).unwrap();
|
||||
assert!(res.is_empty());
|
||||
}
|
||||
|
||||
trait DiffBits: Sized {
|
||||
fn diff_bits(ab: (&Self, &Self)) -> usize;
|
||||
}
|
||||
|
||||
impl DiffBits for u8 {
|
||||
fn diff_bits((a, b): (&Self, &Self)) -> usize {
|
||||
(a ^ b).count_ones() as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl DiffBits for u32 {
|
||||
fn diff_bits((a, b): (&Self, &Self)) -> usize {
|
||||
(a ^ b).count_ones() as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl DiffBits for u64 {
|
||||
fn diff_bits((a, b): (&Self, &Self)) -> usize {
|
||||
(a ^ b).count_ones() as usize
|
||||
}
|
||||
}
|
||||
|
||||
// Return the number of bits in which s1 and s2 differ
|
||||
fn num_diff_bits<T: DiffBits>(s1: &[T], s2: &[T]) -> usize {
|
||||
assert_eq!(s1.len(), s2.len());
|
||||
s1.iter().zip(s2.iter()).map(T::diff_bits).sum()
|
||||
}
|
||||
|
||||
// Tests the quality of calling getrandom on two large buffers
|
||||
#[test]
|
||||
fn test_diff() {
|
||||
const N: usize = 1000;
|
||||
let mut v1 = [0u8; N];
|
||||
let mut v2 = [0u8; N];
|
||||
fill(&mut v1).unwrap();
|
||||
fill(&mut v2).unwrap();
|
||||
|
||||
let mut t1 = [MaybeUninit::uninit(); N];
|
||||
let mut t2 = [MaybeUninit::uninit(); N];
|
||||
let r1 = fill_uninit(&mut t1).unwrap();
|
||||
let r2 = fill_uninit(&mut t2).unwrap();
|
||||
assert_eq!(r1.len(), N);
|
||||
assert_eq!(r2.len(), N);
|
||||
|
||||
// Between 3.5 and 4.5 bits per byte should differ. Probability of failure:
|
||||
// ~ 2^(-94) = 2 * CDF[BinomialDistribution[8000, 0.5], 3500]
|
||||
let d1 = num_diff_bits(&v1, &v2);
|
||||
assert!(d1 > 3500);
|
||||
assert!(d1 < 4500);
|
||||
let d2 = num_diff_bits(r1, r2);
|
||||
assert!(d2 > 3500);
|
||||
assert!(d2 < 4500);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_diff_u32() {
|
||||
const N: usize = 1000 / 4;
|
||||
let mut v1 = [0u32; N];
|
||||
let mut v2 = [0u32; N];
|
||||
for v in v1.iter_mut() {
|
||||
*v = getrandom::u32().unwrap();
|
||||
}
|
||||
for v in v2.iter_mut() {
|
||||
*v = getrandom::u32().unwrap();
|
||||
}
|
||||
|
||||
// Between 3.5 and 4.5 bits per byte should differ. Probability of failure:
|
||||
// ~ 2^(-94) = 2 * CDF[BinomialDistribution[8000, 0.5], 3500]
|
||||
let d1 = num_diff_bits(&v1, &v2);
|
||||
assert!(d1 > 3500);
|
||||
assert!(d1 < 4500);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_diff_u64() {
|
||||
const N: usize = 1000 / 8;
|
||||
let mut v1 = [0u64; N];
|
||||
let mut v2 = [0u64; N];
|
||||
for v in v1.iter_mut() {
|
||||
*v = getrandom::u64().unwrap();
|
||||
}
|
||||
for v in v2.iter_mut() {
|
||||
*v = getrandom::u64().unwrap();
|
||||
}
|
||||
|
||||
// Between 3.5 and 4.5 bits per byte should differ. Probability of failure:
|
||||
// ~ 2^(-94) = 2 * CDF[BinomialDistribution[8000, 0.5], 3500]
|
||||
let d1 = num_diff_bits(&v1, &v2);
|
||||
assert!(d1 > 3500);
|
||||
assert!(d1 < 4500);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_small() {
|
||||
const N: usize = 64;
|
||||
// For each buffer size, get at least 256 bytes and check that between
|
||||
// 3 and 5 bits per byte differ. Probability of failure:
|
||||
// ~ 2^(-91) = 64 * 2 * CDF[BinomialDistribution[8*256, 0.5], 3*256]
|
||||
for size in 1..=N {
|
||||
let mut num_bytes = 0;
|
||||
let mut diff_bits = 0;
|
||||
while num_bytes < 256 {
|
||||
let mut buf1 = [0u8; N];
|
||||
let mut buf2 = [0u8; N];
|
||||
|
||||
let s1 = &mut buf1[..size];
|
||||
let s2 = &mut buf2[..size];
|
||||
|
||||
fill(s1).unwrap();
|
||||
fill(s2).unwrap();
|
||||
|
||||
num_bytes += size;
|
||||
diff_bits += num_diff_bits(s1, s2);
|
||||
}
|
||||
assert!(diff_bits > 3 * num_bytes);
|
||||
assert!(diff_bits < 5 * num_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
// Tests the quality of calling getrandom repeatedly on small buffers
|
||||
#[test]
|
||||
fn test_small_uninit() {
|
||||
const N: usize = 64;
|
||||
// For each buffer size, get at least 256 bytes and check that between
|
||||
// 3 and 5 bits per byte differ. Probability of failure:
|
||||
// ~ 2^(-91) = 64 * 2 * CDF[BinomialDistribution[8*256, 0.5], 3*256]
|
||||
for size in 1..=N {
|
||||
let mut num_bytes = 0;
|
||||
let mut diff_bits = 0;
|
||||
while num_bytes < 256 {
|
||||
let mut buf1 = [MaybeUninit::uninit(); N];
|
||||
let mut buf2 = [MaybeUninit::uninit(); N];
|
||||
|
||||
let s1 = &mut buf1[..size];
|
||||
let s2 = &mut buf2[..size];
|
||||
|
||||
let r1 = fill_uninit(s1).unwrap();
|
||||
let r2 = fill_uninit(s2).unwrap();
|
||||
assert_eq!(r1.len(), size);
|
||||
assert_eq!(r2.len(), size);
|
||||
|
||||
num_bytes += size;
|
||||
diff_bits += num_diff_bits(r1, r2);
|
||||
}
|
||||
assert!(diff_bits > 3 * num_bytes);
|
||||
assert!(diff_bits < 5 * num_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_huge() {
|
||||
let mut huge = [0u8; 100_000];
|
||||
fill(&mut huge).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_huge_uninit() {
|
||||
const N: usize = 100_000;
|
||||
let mut huge = [MaybeUninit::uninit(); N];
|
||||
let res = fill_uninit(&mut huge).unwrap();
|
||||
assert_eq!(res.len(), N);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(
|
||||
target_arch = "wasm32",
|
||||
ignore = "The thread API always fails/panics on WASM"
|
||||
)]
|
||||
fn test_multithreading() {
|
||||
extern crate std;
|
||||
use std::{sync::mpsc::channel, thread, vec};
|
||||
|
||||
let mut txs = vec![];
|
||||
for _ in 0..20 {
|
||||
let (tx, rx) = channel();
|
||||
txs.push(tx);
|
||||
|
||||
thread::spawn(move || {
|
||||
// wait until all the tasks are ready to go.
|
||||
rx.recv().unwrap();
|
||||
let mut v = [0u8; 1000];
|
||||
|
||||
for _ in 0..100 {
|
||||
fill(&mut v).unwrap();
|
||||
thread::yield_now();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// start all the tasks
|
||||
for tx in txs.iter() {
|
||||
tx.send(()).unwrap();
|
||||
}
|
||||
}
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
//! Tests for `SysRng`
|
||||
#![cfg(feature = "sys_rng")]
|
||||
|
||||
use getrandom::SysRng;
|
||||
use getrandom::rand_core::TryRng;
|
||||
|
||||
#[test]
|
||||
fn test_sys_rng() {
|
||||
let x = SysRng.try_next_u64().unwrap();
|
||||
let y = SysRng.try_next_u64().unwrap();
|
||||
assert!(x != 0);
|
||||
assert!(x != y);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_construction() {
|
||||
assert!(SysRng.try_next_u64().unwrap() != 0);
|
||||
}
|
||||
Reference in New Issue
Block a user