pub struct RandCtx { /* private fields */ }Expand description
A seeded DRBG context (EVP_RAND_CTX*).
Has up_ref so Clone is implemented; wrapping in Arc<RandCtx> is safe.
Implementations§
Source§impl RandCtx
impl RandCtx
Sourcepub fn new(alg: &RandAlg, parent: Option<&RandCtx>) -> Result<Self, ErrorStack>
pub fn new(alg: &RandAlg, parent: Option<&RandCtx>) -> Result<Self, ErrorStack>
Create a new uninstantiated DRBG context from an algorithm descriptor.
Provide parent = None to seed from the global PRNG.
§Errors
Sourcepub fn instantiate(
&mut self,
strength: u32,
prediction_resistance: bool,
params: Option<&Params<'_>>,
) -> Result<(), ErrorStack>
pub fn instantiate( &mut self, strength: u32, prediction_resistance: bool, params: Option<&Params<'_>>, ) -> Result<(), ErrorStack>
Instantiate (seed) the DRBG at the requested security strength.
Must be called before generate if the context was not auto-seeded.
When parent = None was used in new, pass strength ≤ 128 to stay
within the system seed-source’s entropy ceiling.
§Errors
Sourcepub fn generate(
&mut self,
out: &mut [u8],
req: &GenerateRequest<'_>,
) -> Result<(), ErrorStack>
pub fn generate( &mut self, out: &mut [u8], req: &GenerateRequest<'_>, ) -> Result<(), ErrorStack>
Sourcepub fn fill(&mut self, out: &mut [u8]) -> Result<(), ErrorStack>
pub fn fill(&mut self, out: &mut [u8]) -> Result<(), ErrorStack>
Generate random bytes with default parameters (strength=256, no prediction resistance, no additional input).
Equivalent to calling generate with GenerateRequest::default.
Call Self::instantiate before the first fill.
§Errors
Sourcepub fn state(&self) -> RandState
pub fn state(&self) -> RandState
Current lifecycle state of this DRBG context.
Returns RandState::Ready after a successful Self::instantiate.
Sourcepub fn set_params(&mut self, params: &Params<'_>) -> Result<(), ErrorStack>
pub fn set_params(&mut self, params: &Params<'_>) -> Result<(), ErrorStack>
Set DRBG parameters (e.g. reseed interval, additional input length).
§Errors
Returns Err if EVP_RAND_CTX_set_params fails.
Sourcepub fn public() -> Result<GlobalRandCtx, ErrorStack>
pub fn public() -> Result<GlobalRandCtx, ErrorStack>
Return a reference to the process-wide public DRBG.
The returned GlobalRandCtx does not free the pointer when dropped —
the global DRBG is owned by the OpenSSL runtime.
Pass Some(global.as_rand_ctx()) as the parent argument to
RandCtx::new to chain a child DRBG off the global instance.
Requires OpenSSL 3.2+.
§Errors
Returns Err if the global DRBG is not yet initialised.
Sourcepub fn private_global() -> Result<GlobalRandCtx, ErrorStack>
pub fn private_global() -> Result<GlobalRandCtx, ErrorStack>
Return a reference to the process-wide private DRBG.
Same semantics as RandCtx::public.
Requires OpenSSL 3.2+.
§Errors
Sourcepub fn primary() -> Result<GlobalRandCtx, ErrorStack>
pub fn primary() -> Result<GlobalRandCtx, ErrorStack>
Return a reference to the process-wide primary DRBG.
The primary DRBG is the root of the global DRBG hierarchy — the public and private DRBGs are seeded from it. It is useful for diagnostics and for configuring the root of a custom DRBG chain.
Same ownership semantics as RandCtx::public: the returned
GlobalRandCtx does not free the pointer on drop.
Requires OpenSSL 3.2+.
§Errors
Returns Err if the primary DRBG is not yet initialised.