pub struct HmacDrbgSha256 { /* private fields */ }Expand description
NIST SP 800-90A Rev. 1 HMAC_DRBG(SHA-256) state (K, V, counters, lifecycle flag).
All internal secret state (key, val, counters) is automatically zeroized on drop.
For FIPS module boundaries, prefer Self::uninitialized combined with
Self::instantiate_in_place or Self::instantiate_rbg2_in_place so (K, V) are
initialized directly in their final resting place without by-value stack moves.
Implementations§
Source§impl HmacDrbgSha256
impl HmacDrbgSha256
Sourcepub const SECURITY_STRENGTH_BITS: usize = 256
pub const SECURITY_STRENGTH_BITS: usize = 256
Maximum supported DRBG security strength in bits (256, SP 800-90A Rev. 1 Table 2).
Sourcepub const OUT_LEN: usize = HmacSha256::TAG_LEN
pub const OUT_LEN: usize = HmacSha256::TAG_LEN
Underlying HMAC-SHA-256 output block size in bytes (32).
Sourcepub const MIN_ENTROPY_LEN: usize = 32
pub const MIN_ENTROPY_LEN: usize = 32
Minimum entropy input length in bytes (32 bytes / 256 bits, SP 800-90A Rev. 1 Table 2).
Sourcepub const MIN_NONCE_LEN: usize = 16
pub const MIN_NONCE_LEN: usize = 16
Minimum nonce length in bytes (16 bytes / 128 bits, SP 800-90A Rev. 1 Section 8.6.7).
Sourcepub const RBG2_INIT_MIN_ENTROPY_LEN: usize = 48
pub const RBG2_INIT_MIN_ENTROPY_LEN: usize = 48
Minimum initialization min-entropy in bytes for 256-bit RBG2 (48 bytes / 384 bits,
SP 800-90C Section 5.2.3).
Sourcepub const MAX_BYTES_PER_REQUEST: usize = 65_536
pub const MAX_BYTES_PER_REQUEST: usize = 65_536
Maximum output length per single generate request (65,536 bytes = 2^19 bits,
SP 800-90A Rev. 1 Table 2).
Sourcepub const MAX_RESEED_INTERVAL: u64
pub const MAX_RESEED_INTERVAL: u64
Maximum reseed_counter before a reseed is mandatory (2^48, SP 800-90A Rev. 1 Table 2).
Sourcepub const SP800_90C_RESEED_BYTES: u64 = 16_384
pub const SP800_90C_RESEED_BYTES: u64 = 16_384
SP 800-90C Sections 5.2.3–5.3 output threshold (16,384 bytes = 2^17 bits): once total
output since instantiation or last reseed reaches this threshold, the next generate call
requires a reseed first.
Sourcepub const MAX_INPUT_LEN: usize
pub const MAX_INPUT_LEN: usize
Maximum length in bytes accepted for any single input (entropy, nonce,
personalization, or additional_input), safely below the SP 800-90A 2^35-bit ceiling.
Sourcepub const fn uninitialized() -> Self
pub const fn uninitialized() -> Self
Creates an uninitialized DRBG slot (initialized == false).
All generate, generate_with_prediction_resistance, and reseed calls fail with
[Error::DrbgInvalidRequest] until Self::instantiate_in_place or
Self::instantiate_rbg2_in_place succeeds.
Sourcepub fn instantiate_in_place(
&mut self,
entropy: &[u8],
nonce: &[u8],
personalization: &[u8],
prediction_resistance: bool,
) -> Result<()>
pub fn instantiate_in_place( &mut self, entropy: &[u8], nonce: &[u8], personalization: &[u8], prediction_resistance: bool, ) -> Result<()>
Instantiates self in place (SP 800-90A Rev. 1, Section 10.1.2.3), avoiding
by-value stack moves of (K, V).
Enforces:
entropy.len() >= 32(256 bits)nonce.len() >= 16(128 bits; caller MUST guarantee SP 800-90A §8.6.7 nonce uniqueness or>= 128bits of min-entropy so the nonce does not repeat across instantiations)- each input
<= MAX_INPUT_LEN
On error, self is left zeroized and uninitialized.
Sourcepub fn instantiate_rbg2_in_place(
&mut self,
entropy: &[u8],
personalization: &[u8],
prediction_resistance: bool,
) -> Result<()>
pub fn instantiate_rbg2_in_place( &mut self, entropy: &[u8], personalization: &[u8], prediction_resistance: bool, ) -> Result<()>
Instantiates self in place for a 256-bit SP 800-90C RBG2 construction.
Requires at least 384 bits (48 bytes) of min-entropy in entropy (combining the
256-bit security strength and 128-bit nonce entropy per SP 800-90C §5.2.3).
On error, self is left zeroized and uninitialized.
Sourcepub fn instantiate(
entropy: &[u8],
nonce: &[u8],
personalization: &[u8],
prediction_resistance: bool,
) -> Result<Self>
pub fn instantiate( entropy: &[u8], nonce: &[u8], personalization: &[u8], prediction_resistance: bool, ) -> Result<Self>
Convenience constructor delegating to Self::instantiate_in_place.
Prefer Self::uninitialized + Self::instantiate_in_place in FIPS boundary code to
initialize (K, V) directly in their final storage without a Result<Self> stack move.
Sourcepub fn instantiate_rbg2(
entropy: &[u8],
personalization: &[u8],
prediction_resistance: bool,
) -> Result<Self>
pub fn instantiate_rbg2( entropy: &[u8], personalization: &[u8], prediction_resistance: bool, ) -> Result<Self>
Convenience constructor delegating to Self::instantiate_rbg2_in_place.
Prefer Self::uninitialized + Self::instantiate_rbg2_in_place in FIPS boundary code.
Sourcepub fn reseed(&mut self, entropy: &[u8], additional_input: &[u8]) -> Result<()>
pub fn reseed(&mut self, entropy: &[u8], additional_input: &[u8]) -> Result<()>
Reseeds an initialized DRBG with at least 256 bits (32 bytes) of fresh entropy
(SP 800-90A Rev. 1, Section 10.1.2.4).
Returns [Error::DrbgInvalidRequest] if the DRBG is uninitialized/zeroized or inputs
violate length bounds.
Sourcepub fn generate(
&mut self,
out: &mut [u8],
additional_input: &[u8],
) -> Result<()>
pub fn generate( &mut self, out: &mut [u8], additional_input: &[u8], ) -> Result<()>
Generates pseudorandom bytes into out (SP 800-90A Rev. 1, Section 10.1.2.5).
On any error, out is left unmodified:
- [
Error::DrbgInvalidRequest] if uninitialized/zeroized orout.len() > MAX_BYTES_PER_REQUESToradditional_input.len() > MAX_INPUT_LEN. - [
Error::DrbgReseedRequired] if configured withprediction_resistance == true,reseed_counter > MAX_RESEED_INTERVAL, orbytes_since_reseed >= SP800_90C_RESEED_BYTES.
Note: a 0-byte out slice still executes SP 800-90A §10.1.2.5 state update and increments
reseed_counter without emitting bytes.
Sourcepub fn generate_with_prediction_resistance(
&mut self,
out: &mut [u8],
entropy: &[u8],
additional_input: &[u8],
) -> Result<()>
pub fn generate_with_prediction_resistance( &mut self, out: &mut [u8], entropy: &[u8], additional_input: &[u8], ) -> Result<()>
Reseeds an initialized DRBG with fresh entropy and generates pseudorandom bytes into
out (SP 800-90A Rev. 1, Section 9.3.1 with prediction resistance).
Per SP 800-90A Rev. 1 Section 9.3.1 Step 5, returns [Error::DrbgInvalidRequest] without
modifying state or out if prediction_resistance == false on this instance, if the DRBG
is uninitialized/zeroized, or if length bounds are violated. Note: a 0-byte out slice
still reseeds and advances internal state per SP 800-90A §9.3.1.
Sourcepub const fn is_initialized(&self) -> bool
pub const fn is_initialized(&self) -> bool
Returns true if this DRBG is currently instantiated and usable.
Sourcepub const fn reseed_counter(&self) -> u64
pub const fn reseed_counter(&self) -> u64
Returns the current SP 800-90A reseed_counter.
Sourcepub const fn bytes_since_reseed(&self) -> u64
pub const fn bytes_since_reseed(&self) -> u64
Returns the cumulative number of bytes generated since instantiation or the last reseed.
Sourcepub const fn needs_reseed(&self) -> bool
pub const fn needs_reseed(&self) -> bool
Returns true if the DRBG is uninitialized or must be reseeded before calling
Self::generate.
Sourcepub const fn prediction_resistance(&self) -> bool
pub const fn prediction_resistance(&self) -> bool
Returns whether this instance enforces prediction resistance on every generate call.
Sourcepub fn self_test() -> Result<()>
pub fn self_test() -> Result<()>
Executes the FIPS 140-3 / SP 800-90A Rev. 1 Section 11.3 DRBG self-test (CAST + health check).
Uses independently computed fixed test inputs (verified against Python’s hmac/hashlib
SP 800-90A reference) to exercise all operational paths in constant time:
instantiate_in_place→reseed→generate→generateKAT.instantiate_rbg2_in_place(384-bit RBG2 path) →generate→reseed→generateKAT.generate_with_prediction_resistanceKAT and prediction-resistance enforcement.- SP 800-90A §11.3 error-handling checks (short entropy, short RBG2 entropy, and post- zeroization uninitialized rejection).