rust: Add support for feeding entropy to randomness pool
PATCH v2 1/1 rust: Add support for feeding entropy to randomness pool
Adds just enough support to allow device drivers to feed entropy to the central pool.
Signed-off-by Matthew Maurer <[email protected]>
---
Changes in v2:
- Added more details in the docs about the API, specifically about it
not crediting entropy and when it ought to be used.
- Link to v1: https://lore.kernel.org/r/[email protected]
--- rust/kernel/lib.rs | 1 + rust/kernel/rand.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+)
rust/kernel/lib.rs +1 -0
@@ -130,6 +130,7 @@pub mod ptr;#[cfg( CONFIG_RUST_PWM_ABSTRACTIONS )] pub mod pwm; +pub mod rand;pub mod rbtree;pub mod regulator;pub mod revocable;
rust/kernel/rand.rs new file +19 -0
@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Randomness. +//! +//! C header: [`include/linux/random.h`](../../../../include/linux/random.h) + +use crate ::bindings; +use crate ::ffi::c_void; + +/// Adds the given buffer to the entropy pool, but does not credit any entropy. +/// +/// This is intended for use mixing in data that is likely to differ between devices or boots, but +/// may otherwise be predictable. Examples include MAC addresses or RTC values. This slightly +/// improves randomness in entropy-constrained environments (especially common for embedded +/// devices). +pub fn add_device_randomness (buf: &[u8 ]) { +// SAFETY: We just need the pointer to be valid for the length, which a slice provides. +unsafe { bindings::add_device_randomness (buf.as_ptr ().cast ::<c_void >(), buf.len ()) }; +}
---
base-commit: 008d3547aae5bc86fac3eda317489169c3fda112
change-id: 20251029-add-entropy-f57e12ebe110
Best regards,
Signature · 1 line
Matthew Maurer <[email protected]>
Quoted 5 lines · Matthew Maurer
Adds just enough support to allow device drivers to feed entropy to the central pool.
Signed-off-by Matthew Maurer <[email protected]>
Some nits below. With those fixed:
Reviewed-by Alice Ryhl <[email protected]>
rust/kernel/rand.rs line 5
Quoted 3 lines
rust/kernel/rand.rs new file +3
-0+//! Randomness. +//! +//! C header: [`include/linux/random.h`](../../../../include/linux/random.h)
Needs to use srctree instead of ../
rust/kernel/rand.rs line 8
Quoted 2 lines
rust/kernel/rand.rs new file +2
-0+use crate ::bindings; +use crate ::ffi::c_void;
Please import the prelude here.
Quoted 10 lines
rust/kernel/rand.rs new file +10
-0+/// Adds the given buffer to the entropy pool, but does not credit any entropy. +/// +/// This is intended for use mixing in data that is likely to differ between devices or boots, but +/// may otherwise be predictable. Examples include MAC addresses or RTC values. This slightly +/// improves randomness in entropy-constrained environments (especially common for embedded +/// devices). +pub fn add_device_randomness (buf: &[u8 ]) { +// SAFETY: We just need the pointer to be valid for the length, which a slice provides. +unsafe { bindings::add_device_randomness (buf.as_ptr ().cast ::<c_void >(), buf.len ()) }; +}