Skip to main content

HmacDrbgSha256

Struct HmacDrbgSha256 

Source
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

Source

pub const SECURITY_STRENGTH_BITS: usize = 256

Maximum supported DRBG security strength in bits (256, SP 800-90A Rev. 1 Table 2).

Source

pub const OUT_LEN: usize = HmacSha256::TAG_LEN

Underlying HMAC-SHA-256 output block size in bytes (32).

Source

pub const MIN_ENTROPY_LEN: usize = 32

Minimum entropy input length in bytes (32 bytes / 256 bits, SP 800-90A Rev. 1 Table 2).

Source

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).

Source

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).

Source

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).

Source

pub const MAX_RESEED_INTERVAL: u64

Maximum reseed_counter before a reseed is mandatory (2^48, SP 800-90A Rev. 1 Table 2).

Source

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.

Source

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.

Source

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.

Source

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 >= 128 bits of min-entropy so the nonce does not repeat across instantiations)
  • each input <= MAX_INPUT_LEN

On error, self is left zeroized and uninitialized.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 or out.len() > MAX_BYTES_PER_REQUEST or additional_input.len() > MAX_INPUT_LEN.
  • [Error::DrbgReseedRequired] if configured with prediction_resistance == true, reseed_counter > MAX_RESEED_INTERVAL, or bytes_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.

Source

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.

Source

pub const fn is_initialized(&self) -> bool

Returns true if this DRBG is currently instantiated and usable.

Source

pub const fn reseed_counter(&self) -> u64

Returns the current SP 800-90A reseed_counter.

Source

pub const fn bytes_since_reseed(&self) -> u64

Returns the cumulative number of bytes generated since instantiation or the last reseed.

Source

pub const fn needs_reseed(&self) -> bool

Returns true if the DRBG is uninitialized or must be reseeded before calling Self::generate.

Source

pub const fn prediction_resistance(&self) -> bool

Returns whether this instance enforces prediction resistance on every generate call.

Source

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:

  1. instantiate_in_place → reseed → generate → generate KAT.
  2. instantiate_rbg2_in_place (384-bit RBG2 path) → generate → reseed → generate KAT.
  3. generate_with_prediction_resistance KAT and prediction-resistance enforcement.
  4. SP 800-90A §11.3 error-handling checks (short entropy, short RBG2 entropy, and post- zeroization uninitialized rejection).

Trait Implementations§

Source§

impl Debug for HmacDrbgSha256

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for HmacDrbgSha256

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Zeroize for HmacDrbgSha256

Source§

fn zeroize(&mut self)

Overwrites all sensitive memory owned by self with zeros.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.