From 83b3bee7e359238b73c74d614f5862f486bc28bd Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Sun, 9 Jun 2024 18:14:15 +0200 Subject: [PATCH] mirage-crypto-rng-mirage: provide a module type S The goal is to remove the mirage-random opam package from the chain. It is a fine moment to do so, since we need to touch all the users thereof anyways due to the fact that generate now produces string. --- mirage/unikernel.ml | 2 +- rng/mirage/mirage_crypto_rng_mirage.ml | 16 ++++++++++ rng/mirage/mirage_crypto_rng_mirage.mli | 39 +++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/mirage/unikernel.ml b/mirage/unikernel.ml index 27e3ab59..cd5cfac3 100644 --- a/mirage/unikernel.ml +++ b/mirage/unikernel.ml @@ -1,4 +1,4 @@ -module Main (R : Mirage_random.S) = struct +module Main (R : Mirage_crypto_rng_mirage.S) = struct let start _r = Logs.info (fun m -> m "using Fortuna, entropy sources: %a" Fmt.(list ~sep:(any ", ") Mirage_crypto_rng.Entropy.pp_source) diff --git a/rng/mirage/mirage_crypto_rng_mirage.ml b/rng/mirage/mirage_crypto_rng_mirage.ml index 92a2414b..bc846a38 100644 --- a/rng/mirage/mirage_crypto_rng_mirage.ml +++ b/rng/mirage/mirage_crypto_rng_mirage.ml @@ -27,6 +27,22 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *) +module type S = sig + type g = Mirage_crypto_rng.g + module Entropy : + sig + type source = Mirage_crypto_rng.Entropy.source + val sources : unit -> source list + val pp_source : Format.formatter -> source -> unit + val register_source : string -> source + end + + val generate_into : ?g:g -> bytes -> ?off:int -> int -> unit + val generate : ?g:g -> int -> string + + val accumulate : g option -> Entropy.source -> [`Acc of string -> unit] +end + let src = Logs.Src.create "mirage-crypto-rng-mirage" ~doc:"Mirage crypto RNG mirage" module Log = (val Logs.src_log src : Logs.LOG) diff --git a/rng/mirage/mirage_crypto_rng_mirage.mli b/rng/mirage/mirage_crypto_rng_mirage.mli index 7bc83b8f..d085b367 100644 --- a/rng/mirage/mirage_crypto_rng_mirage.mli +++ b/rng/mirage/mirage_crypto_rng_mirage.mli @@ -26,7 +26,44 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *) +module type S = sig + type g = Mirage_crypto_rng.g + (** A generator (PRNG) with its state. *) + + (** Entropy sources and collection *) + module Entropy : + sig + (** Entropy sources. *) + type source = Mirage_crypto_rng.Entropy.source + + val sources : unit -> source list + (** [sources ()] returns the list of available sources. *) + + val pp_source : Format.formatter -> source -> unit + (** [pp_source ppf source] pretty-prints the entropy [source] on [ppf]. *) + + val register_source : string -> source + (** [register_source name] registers [name] as entropy source. *) + end + + val generate_into : ?g:g -> bytes -> ?off:int -> int -> unit + (** [generate_into ~g buf ~off len] invokes + {{!Generator.generate_into}generate_into} on [g] or + {{!generator}default generator}. The random data is put into [buf] starting + at [off] (defaults to 0) with [len] bytes. *) + + val generate : ?g:g -> int -> string + (** Invoke {!generate_into} on [g] or {{!generator}default generator} and a + freshly allocated string. *) + + val accumulate : g option -> Entropy.source -> [`Acc of string -> unit] + (** [accumulate g source] is a function [data -> unit] to feed entropy to the + RNG. This is useful if your system has a special entropy source. *) +end + module Make (T : Mirage_time.S) (M : Mirage_clock.MCLOCK) : sig + include S + val initialize : ?g:'a -> ?sleep:int64 -> 'a Mirage_crypto_rng.generator -> unit Lwt.t (** [initialize ~g ~sleep generator] sets the default generator to the @@ -34,6 +71,4 @@ module Make (T : Mirage_time.S) (M : Mirage_clock.MCLOCK) : sig function fails ([Lwt.fail]) if it is called a second time. The argument [~sleep] is measured in ns, and used as sleep between cpu assisted random number collection. It defaults to one second. *) - - include module type of Mirage_crypto_rng end