Skip to main content

confium_api/plugin/
rng.rs

1//! `RngPlugin` trait — the Rust-side counterpart of the RNG v0 wire
2//! protocol.
3//!
4//! Plugin authors implement this trait on their RNG state type, then
5//! apply `#[plugin_interface(name = "rng", version = 0)]` to the impl
6//! block.
7//!
8//! Method → symbol mapping (see `crates/confium-core/src/ffi/rng.rs`
9//! for the loader-side wire types):
10//!
11//! | trait method | FFI symbol | purpose |
12//! |--------------|------------|---------|
13//! | [`RngPlugin::create`]      | `cfmp_rng_create`       | construct a new instance |
14//! | [`RngPlugin::reseed`]      | `cfmp_rng_reseed`       | reseed from entropy |
15//! | [`RngPlugin::add_entropy`] | `cfmp_rng_add_entropy`  | add entropy |
16//! | [`RngPlugin::generate`]    | `cfmp_rng_generate`     | generate random bytes |
17//! | `Drop`                     | `cfmp_rng_destroy`      | reclaim the boxed state |
18
19use crate::error::PluginResult;
20use crate::options::OptionView;
21
22/// Trait implemented by RNG plugins.
23pub trait RngPlugin: Sized {
24    /// Construct a new RNG instance for the named algorithm.
25    fn create(algorithm: &str, opts: Option<OptionView<'_>>) -> PluginResult<Self>;
26
27    /// Reseed the generator from the provided entropy.
28    fn reseed(&mut self, data: &[u8]) -> PluginResult<()>;
29
30    /// Add entropy without a full reseed.
31    fn add_entropy(&mut self, data: &[u8]) -> PluginResult<()>;
32
33    /// Fill `out` with random bytes.
34    fn generate(&mut self, out: &mut [u8]) -> PluginResult<()>;
35}