From 868e8f9c92cf6dd80bc031358f0389c35c86ef4f Mon Sep 17 00:00:00 2001 From: SimonIT Date: Sun, 14 Sep 2025 16:56:28 +0200 Subject: [PATCH 1/3] feat: Add support for Ariel OS --- .github/workflows/workspace.yml | 4 + Cargo.lock | 184 ++++++++++++++++++++++++++++++++ Cargo.toml | 7 +- README.md | 19 ++-- src/backends.rs | 3 + src/backends/ariel_os.rs | 16 +++ 6 files changed, 223 insertions(+), 10 deletions(-) create mode 100644 src/backends/ariel_os.rs diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index d6ba854a..834cf4cd 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -37,6 +37,10 @@ jobs: env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="esp_idf" run: cargo clippy -Zbuild-std=core --target riscv32imc-esp-espidf + - name: Ariel OS (ariel_os.rs) + env: + RUSTFLAGS: -Dwarnings --cfg getrandom_backend="ariel-os" + run: cargo clippy -Zbuild-std=core --target riscv32imac-unknown-none-elf - name: Fuchsia (fuchsia.rs) run: cargo clippy -Zbuild-std=core --target x86_64-unknown-fuchsia - name: OpenBSD (getentropy.rs) diff --git a/Cargo.lock b/Cargo.lock index da027edd..27bd4b54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,29 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ariel-os-random" +version = "0.2.0" +source = "git+https://github.com/ariel-os/ariel-os.git?tag=v0.2.1#1d9062d957168d8ac0a3c9536123480114186163" +dependencies = [ + "embassy-sync", + "rand_chacha", + "rand_core", + "rand_pcg", +] + [[package]] name = "bumpalo" version = "3.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "cc" version = "1.2.36" @@ -24,25 +41,111 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9" +[[package]] +name = "critical-section" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" + +[[package]] +name = "embassy-sync" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d2c8cdff05a7a51ba0087489ea44b0b1d97a296ca6b1d6d1a33ea7423d34049" +dependencies = [ + "cfg-if", + "critical-section", + "embedded-io-async", + "futures-sink", + "futures-util", + "heapless", +] + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + +[[package]] +name = "embedded-io-async" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff09972d4073aa8c299395be75161d582e7629cd663171d62af73c8d50dba3f" +dependencies = [ + "embedded-io", +] + [[package]] name = "find-msvc-tools" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fd99930f64d146689264c637b5af2f0233a933bef0d8570e2526bf9e083192d" +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-sink" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + [[package]] name = "getrandom" version = "0.3.3" dependencies = [ + "ariel-os-random", "cfg-if", "js-sys", "libc", "r-efi", + "rand", "wasi", "wasm-bindgen", "wasm-bindgen-test", ] +[[package]] +name = "hash32" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" +dependencies = [ + "byteorder", +] + +[[package]] +name = "heapless" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" +dependencies = [ + "hash32", + "stable_deref_trait", +] + [[package]] name = "js-sys" version = "0.3.78" @@ -81,6 +184,27 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + [[package]] name = "proc-macro2" version = "1.0.101" @@ -105,6 +229,40 @@ version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + +[[package]] +name = "rand_pcg" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59cad018caf63deb318e5a4586d99a24424a364f40f1e5778c29aca23f4fc73e" +dependencies = [ + "rand_core", +] + [[package]] name = "same-file" version = "1.0.6" @@ -120,6 +278,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + [[package]] name = "syn" version = "2.0.106" @@ -290,3 +454,23 @@ name = "wit-bindgen" version = "0.45.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c573471f125075647d03df72e026074b7203790d41351cd6edc96f46bcccd36" + +[[package]] +name = "zerocopy" +version = "0.8.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0894878a5fa3edfd6da3f88c4805f4c8558e2b996227a3d864f47fe11e38282c" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index 03b188be..d10ee9d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,10 +74,15 @@ js-sys = { version = "0.3.77", default-features = false, optional = true } [target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dev-dependencies] wasm-bindgen-test = "0.3" +# ariel-os +[target.'cfg(getrandom_backend = "ariel-os")'.dependencies] +ariel-os-random = { git = "https://github.com/ariel-os/ariel-os.git", tag = "v0.2.1", features = ["csprng"] } +rand = { version = "0.8.5", default-features = false } + [lints.rust.unexpected_cfgs] level = "warn" check-cfg = [ - 'cfg(getrandom_backend, values("custom", "efi_rng", "rdrand", "rndr", "linux_getrandom", "linux_raw", "wasm_js", "unsupported"))', + 'cfg(getrandom_backend, values("custom", "efi_rng", "rdrand", "rndr", "linux_getrandom", "linux_raw", "wasm_js", "ariel-os", "unsupported"))', 'cfg(getrandom_msan)', 'cfg(getrandom_windows_legacy)', 'cfg(getrandom_test_linux_fallback)', diff --git a/README.md b/README.md index 17e84860..d3e175b8 100644 --- a/README.md +++ b/README.md @@ -78,16 +78,17 @@ Pull Requests that add support for new targets to `getrandom` are always welcome `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 +| 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 | `wasm_js` | Web Browser, Node.js | `wasm32‑unknown‑unknown`, `wasm32v1-none` | [`Crypto.getRandomValues`]. Requires feature `wasm_js` ([see below](#webassembly-support)). -| `efi_rng` | UEFI | `*-unknown‑uefi` | [`EFI_RNG_PROTOCOL`] with `EFI_RNG_ALGORITHM_RAW` (requires `std` and Nightly compiler) -| `custom` | All targets | `*` | User-provided custom implementation (see [custom backend]) -| `unsupported` | All targets | `*` | Always returns `Err(Error::UNSUPPORTED)` (see [unsupported backend]) +| `efi_rng` | UEFI | `*-unknown‑uefi` | [`EFI_RNG_PROTOCOL`] with `EFI_RNG_ALGORITHM_RAW` (requires `std` and Nightly compiler) +| `ariel-os` | Ariel OS | | Ariel OS's built-in entropy source (see [Ariel OS documentation](https://ariel-os.org/docs/development/getting-random-numbers/)) +| `custom` | All targets | `*` | User-provided custom implementation (see [custom backend]) +| `unsupported` | All targets | `*` | Always returns `Err(Error::UNSUPPORTED)` (see [unsupported backend]) 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`]: diff --git a/src/backends.rs b/src/backends.rs index d7b73cec..777005da 100644 --- a/src/backends.rs +++ b/src/backends.rs @@ -190,6 +190,9 @@ cfg_if! { For more information see: \ https://docs.rs/getrandom/", env!("CARGO_PKG_VERSION"), "/#webassembly-support" )); + } else if #[cfg(getrandom_backend = "ariel-os")] { + mod ariel_os; + pub use ariel_os::*; } else { compile_error!(concat!( "target is not supported. You may need to define a custom backend see: \ diff --git a/src/backends/ariel_os.rs b/src/backends/ariel_os.rs new file mode 100644 index 00000000..79ea1c7c --- /dev/null +++ b/src/backends/ariel_os.rs @@ -0,0 +1,16 @@ +/// Implementation for Ariel OS +use ariel_os_random; +use core::ffi::c_void; +use crate::Error; +use core::mem::MaybeUninit; +use rand::RngCore; + +pub use crate::util::{inner_u32, inner_u64}; + +pub fn fill_inner(_dest: &mut [MaybeUninit]) -> Result<(), Error> { + let mut rng = ariel_os_random::crypto_rng(); + let buf: &mut [u8] = + unsafe { core::slice::from_raw_parts_mut(_dest.as_mut_ptr().cast::(), _dest.len()) }; + rng.try_fill_bytes(buf) + .map_err(|e| Error::from_neg_error_code(e.raw_os_error().unwrap())) +} From 7c7811a6fdfc2228aec417ef549b5f93d1138eb4 Mon Sep 17 00:00:00 2001 From: SimonIT Date: Sun, 14 Sep 2025 17:01:49 +0200 Subject: [PATCH 2/3] fix: fmt --- src/backends/ariel_os.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backends/ariel_os.rs b/src/backends/ariel_os.rs index 79ea1c7c..e1cac100 100644 --- a/src/backends/ariel_os.rs +++ b/src/backends/ariel_os.rs @@ -1,7 +1,7 @@ -/// Implementation for Ariel OS +///! Implementation for Ariel OS +use crate::Error; use ariel_os_random; use core::ffi::c_void; -use crate::Error; use core::mem::MaybeUninit; use rand::RngCore; From f93a3f87a7ff8331e95091c879c858cadd2875bb Mon Sep 17 00:00:00 2001 From: SimonIT Date: Sun, 14 Sep 2025 17:12:03 +0200 Subject: [PATCH 3/3] fix: clippy --- src/backends/ariel_os.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/backends/ariel_os.rs b/src/backends/ariel_os.rs index e1cac100..cbfd1c79 100644 --- a/src/backends/ariel_os.rs +++ b/src/backends/ariel_os.rs @@ -1,7 +1,5 @@ -///! Implementation for Ariel OS +//! Implementation for Ariel OS use crate::Error; -use ariel_os_random; -use core::ffi::c_void; use core::mem::MaybeUninit; use rand::RngCore;