DRBG

Bindings for the Deterministic Random Bit Generator algorithm of HACL*.

class pyhacl.drbg.DRBGRandom
>>> DRBGRandom(
>>>     digestmod: SpecHashDefinitions,
>>>     entropy: bytes,
>>>     nonce: bytes,
>>>     personalization_string: bytes,
>>> )

A deterministric CSPRNG built upon HMAC_DRBG as described in NIST SP 800-90A.

It takes a (short) raw entropy and stretches it in (long) random bytes that are evenly distributed and are not predictable to anyone who lacks the initial raw entropy.

Parameters:
  • digestmod (SpecHashDefinitions) – The hash for the underlying HMAC.

  • entropy (bytes) – Random bytes coming from another CSPRNG such as os.urandom(), or collected from hardware noise. Each digestmod comes with a minimum required entropy, see min_length().

  • nonce (bytes) – Non-secret bytes used to allow initializing a new different DRBGRandom with a same entropy, or Can be empty.

  • personalization_string (bytes) – A human-readable bytes used to identify this instanciation. Its purpose it to allow reusing a same entropy but in various settings. Similar to the label in RFC 5869 (HKDF). Can be empty.

The entropy, nonce and personalization string form together the initial state over which random bytes are generated.

Raises:

ValueError – When not enough entropy is provided.

See also os.urandom() and secrets, both found in the python standard library. They use the operating system random number generator which is suited for cryptography, is much faster, but is not deterministic. They can be used in situations where a deterministic algorithm is not necessary.

generate(length: int, additional_input: bytes = b'') bytes

Generate output.

Parameters:
  • length – How many bytes to generate.

  • additional_input – Optional entropy that’ll be consumed at next reseed().

Raises:

HACLError – When the entropy has been exhausted and that it is unsafe to generate new bytes. Call reseed() to feed more entropy.

Binding for Hacl_HMAC_DRBG_generate.

static min_length(digestmod: SpecHashDefinitions) int

Get the minimum entropy required for a given hash algorithm.

All SHA2 algorithms requires 32 bytes of entropy.

Binding for Hacl_HMAC_DRBG_free.

reseed(entropy: bytes, additional_input: bytes = b'') None

Feed new entropy inside the internal state.

Parameters:

entropy – Random bytes coming from another CSPRNG such as os.urandom(), or collected from hardware noise.

Binding for Hacl_HMAC_DRBG_reseed.

class pyhacl.drbg.SpecHashDefinitions(*values)

Available hash algorithms.

SHA2_256 = 1
SHA2_384 = 2
SHA2_512 = 3
pyhacl.drbg.get_reseed_interval() int

Get the global call count limit of DRBGRandom.generate() before new bytes must be seeded.

pyhacl.drbg.set_reseed_interval(value: int) None

Set the global call count limit of DRBGRandom.generate() before new bytes must be seeded.