Skip to content

Commit

Permalink
mirage-crypto-rng-mirage: provide a module type S
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hannesm committed Jun 9, 2024
1 parent 448ad35 commit 83b3bee
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mirage/unikernel.ml
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 16 additions & 0 deletions rng/mirage/mirage_crypto_rng_mirage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
39 changes: 37 additions & 2 deletions rng/mirage/mirage_crypto_rng_mirage.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,49 @@
* 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
[generator] and sets up periodic entropy feeding for that rng. This
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

0 comments on commit 83b3bee

Please sign in to comment.