Module Bytesrw_sysrandom

Cryptographically secure pseudorandom byte streams and entropy.

This module provides cryptographically secure pseudorandom bytes and an entropy primitive using operating system sources. See the documentation of the primitives for details on the sources.

Cryptographically secure pseurorandom bytes

val reads : ?pos:Bytesrw.Bytes.Stream.pos -> ?slice_length:Bytesrw.Bytes.Slice.length -> ?length:int -> unit -> Bytesrw.Bytes.Reader.t

reads () is a stream of length, or unbounded if unspecified, cryptographically secure pseudorandom bytes in slice_length chunks (defaults to Bytesrw.Bytes.Slice.default_length).

Reads may raise Panic which is not turned into a stream error as it likely indicates a serious condition in the system, see the underlying call set_random.

val string : int -> string

string n are n cryptographically secure pseudorandom bytes. Raises Panic in case of problems, see the underlying call set_random.

val bytes : int -> bytes

bytes n are n cryptographically secure pseudorandom bytes. Raises Panic in case of problems, see the underlying call set_random.

Primitives

exception Panic of string

Exception raised by primitives in case of problem. If this happens do not try to handle the exception, log it at the toplevel of your program and abort the program. It likely indicates a serious condition in the system.

Cryptographically secure pseudorandom bytes

val set_random : Bytesrw.Bytes.Slice.t -> unit

set_random s writes the bytes in the slice range with cryptographically secure pseudorandom bytes. Theoretically this function should not block or error, except perhaps if you try to use it in early OS boot phase.

This uses:

Raises Panic in case of problem, not meant to be handled.

Entropy

val set_entropy : Bytesrw.Bytes.Slice.t -> unit

set_entropy s write the bytes in the slice range with entropy from your operating system. The function blocks until enough entropy is gathered. The Bytesrw.Bytes.Slice.length of s must be smaller or equal to 256 or Invalid_argument is raised.

This uses:

Raises Panic in case of problem, not meant to be handled.