Module Mirage_crypto_rng

Randomness

Usage notes

Interface

type bits = int
type g

A generator (PRNG) with its state.

exception Unseeded_generator

Thrown when using an uninitialized generator.

exception No_default_generator

Thrown when set_generator has not been called.

module Entropy : sig ... end

Entropy sources and collection

module type Generator = sig ... end

A single PRNG algorithm.

type 'a generator = (module Generator with type g = 'a)

Fortuna, a CSPRNG proposed by Schneier.

module Hmac_drbg : functor (H : Mirage_crypto.Hash.S) -> Generator

HMAC_DRBG: A NIST-specified RNG based on HMAC construction over the provided hash.

val create : ?⁠g:'a -> ?⁠seed:Cstruct.t -> ?⁠strict:bool -> ?⁠time:(unit -> int64) -> (module Generator with type g = 'a) -> g

create ~g ~seed ~strict ~time module uses a module conforming to the Generator signature to instantiate the generic generator g.

g is the state to use, otherwise a fresh one is created.

seed can be provided to immediately reseed the generator with.

strict puts the generator into a more standards-conformant, but slighty slower mode. Useful if the outputs need to match published test-vectors.

time is used to limit the amount of reseedings. Fortuna uses at most once every second.

val default_generator : unit -> g

default_generator () is the default generator. Functions in this module use this generator when not explicitly supplied one.

raises No_default_generator

if set_generator has not been called.

val set_default_generator : g -> unit

set_default_generator g sets the default generator to g. This function must be called once.

val generate : ?⁠g:g -> int -> Cstruct.t

Invoke generate on g or default generator.

val block : g option -> int

Block size of g or default generator.

Examples