Skip to content

Commit

Permalink
Defunctorise mirage-crypto-rng-mirage, use mirage-sleep and mirage-mt…
Browse files Browse the repository at this point in the history
…ime instead
  • Loading branch information
hannesm committed Feb 5, 2025
1 parent 198f6db commit dc64a36
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 251 deletions.
6 changes: 2 additions & 4 deletions mirage-crypto-rng-mirage.opam
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ depends: [
"logs"
"lwt" {>= "4.0.0"}
"mirage-runtime" {>= "3.8.0"}
"mirage-time" {>= "2.0.0"}
"mirage-clock" {>= "3.0.0"}
"mirage-sleep" {>= "4.0.0"}
"mirage-mtime" {>= "4.0.0"}
"mirage-unix" {with-test & >= "5.0.0"}
"mirage-time-unix" {with-test & >= "2.0.0"}
"mirage-clock-unix" {with-test & >= "3.0.0"}
"ohex" {with-test & >= "0.2.0"}
]
description: """
Expand Down
31 changes: 0 additions & 31 deletions mirage-crypto-rng-mirage2.opam

This file was deleted.

2 changes: 1 addition & 1 deletion rng/mirage/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(library
(name mirage_crypto_rng_mirage)
(public_name mirage-crypto-rng-mirage)
(libraries lwt mirage-runtime mirage-crypto-rng mirage-time mirage-clock
(libraries lwt mirage-runtime mirage-crypto-rng mirage-sleep mirage-mtime
duration logs))
93 changes: 37 additions & 56 deletions rng/mirage/mirage_crypto_rng_mirage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,66 +27,47 @@
* 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)

module Make (T : Mirage_time.S) (M : Mirage_clock.MCLOCK) = struct
include Mirage_crypto_rng
open Mirage_crypto_rng

let rdrand_task delta =
match Entropy.cpu_rng with
| Error `Not_supported -> ()
| Ok cpu_rng ->
let open Lwt.Infix in
let rdrand = cpu_rng None in
Lwt.async (fun () ->
let rec one () =
rdrand ();
T.sleep_ns delta >>=
one
in
one ())
let rdrand_task delta =
match Entropy.cpu_rng with
| Error `Not_supported -> ()
| Ok cpu_rng ->
let open Lwt.Infix in
let rdrand = cpu_rng None in
Lwt.async (fun () ->
let rec one () =
rdrand ();
Mirage_sleep.ns delta >>=
one
in
one ())

let bootstrap_functions () =
[ Entropy.bootstrap ; Entropy.bootstrap ;
Entropy.whirlwind_bootstrap ; Entropy.bootstrap ]
let bootstrap_functions () =
Entropy.[ bootstrap ; bootstrap ; whirlwind_bootstrap ; bootstrap ]

let running = ref false
let running = ref false

let initialize (type a) ?g ?(sleep = Duration.of_sec 1) (rng : a generator) =
if !running then
Lwt.fail_with "entropy collection already running"
else begin
(try
let _ = default_generator () in
Log.warn (fun m -> m "Mirage_crypto_rng.default_generator has already \
been set, check that this call is intentional");
with
No_default_generator -> ());
running := true;
let seed =
List.mapi (fun i f -> f i) (bootstrap_functions ()) |> String.concat ""
in
let rng = create ?g ~seed ~time:M.elapsed_ns rng in
set_default_generator rng;
rdrand_task sleep;
Mirage_runtime.at_enter_iter (Entropy.timer_accumulator None);
Lwt.return_unit
end
end
let initialize (type a) ?g ?(sleep = Duration.of_sec 1) (rng : a generator) =
if !running then
Lwt.fail_with "entropy collection already running"
else begin
(try
let _ = default_generator () in
Log.warn (fun m -> m "Mirage_crypto_rng.default_generator has already \
been set, check that this call is intentional");
with
No_default_generator -> ());
running := true;
let seed =
List.mapi (fun i f -> f i) (bootstrap_functions ()) |> String.concat ""
in
let rng = create ?g ~seed ~time:Mirage_mtime.elapsed_ns rng in
set_default_generator rng;
rdrand_task sleep;
Mirage_runtime.at_enter_iter (Entropy.timer_accumulator None);
Lwt.return_unit
end
53 changes: 7 additions & 46 deletions rng/mirage/mirage_crypto_rng_mirage.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,10 @@
* 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. *)
end
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. *)
5 changes: 0 additions & 5 deletions rng/mirage2/dune

This file was deleted.

73 changes: 0 additions & 73 deletions rng/mirage2/mirage_crypto_rng_mirage.ml

This file was deleted.

35 changes: 0 additions & 35 deletions rng/mirage2/mirage_crypto_rng_mirage.mli

This file was deleted.

0 comments on commit dc64a36

Please sign in to comment.