Skip to content

Commit

Permalink
mirage-crypto-rng-lwt: always feed the default generator
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesm committed May 18, 2020
1 parent c1e7b5c commit a8c7bbd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
8 changes: 5 additions & 3 deletions rng/entropy.ml
Expand Up @@ -132,14 +132,16 @@ let interrupt_hook () =
buf

let timer_accumulator g =
let `Acc handle = Rng.accumulate (Some g) ~source:(source_id `Timer) in
let g = match g with None -> Some (Rng.default_generator ()) | Some g -> Some g in
let `Acc handle = Rng.accumulate g ~source:(source_id `Timer) in
let hook = interrupt_hook () in
add_source `Timer;
(fun () -> handle (hook ()))

let feed_pools g source f =
let `Acc handle = Rng.accumulate (Some g) ~source:(source_id source) in
for _i = 0 to pred (Rng.pools (Some g)) do
let g = match g with None -> Some (Rng.default_generator ()) | Some g -> Some g in
let `Acc handle = Rng.accumulate g ~source:(source_id source) in
for _i = 0 to pred (Rng.pools g) do
let cs = f () in
handle cs
done
Expand Down
16 changes: 8 additions & 8 deletions rng/lwt/mirage_crypto_rng_lwt.ml
Expand Up @@ -8,22 +8,22 @@ let periodic f delta =
in
one ())

let getrandom_task g delta =
let getrandom_task delta =
let task () =
let per_pool = 8 in
let size = per_pool * pools (Some g) in
let size = per_pool * pools None in
let random = Mirage_crypto_rng_unix.getrandom size in
let idx = ref 0 in
let f () =
incr idx;
Cstruct.sub random (per_pool * (pred !idx)) per_pool
in
Entropy.feed_pools g `Getrandom f
Entropy.feed_pools None `Getrandom f
in
periodic task delta

let rdrand_task g delta =
let task () = Entropy.cpu_rng g in
let rdrand_task delta =
let task () = Entropy.cpu_rng None in
periodic task delta

let running = ref false
Expand Down Expand Up @@ -53,7 +53,7 @@ let initialize ?(sleep = Duration.of_sec 1) () =
Entropy.add_source `Getrandom;
let rng = create ~seed ~time:Mtime_clock.elapsed_ns (module Fortuna) in
set_default_generator rng;
rdrand_task rng sleep;
getrandom_task rng (Int64.mul sleep 10L);
let _ = Lwt_main.Enter_iter_hooks.add_first (Entropy.timer_accumulator rng) in
rdrand_task sleep;
getrandom_task (Int64.mul sleep 10L);
let _ = Lwt_main.Enter_iter_hooks.add_first (Entropy.timer_accumulator None) in
Lwt.return_unit
4 changes: 2 additions & 2 deletions rng/mirage/mirage_crypto_rng_mirage.ml
Expand Up @@ -61,8 +61,8 @@ module Make (T : Mirage_time.S) (M : Mirage_clock.MCLOCK) = struct
in
let rng = create ?g ~seed ~time:M.elapsed_ns rng in
set_default_generator rng;
rdrand_task rng sleep;
Mirage_runtime.at_enter_iter (Entropy.timer_accumulator rng);
rdrand_task (Some rng) sleep;
Mirage_runtime.at_enter_iter (Entropy.timer_accumulator (Some rng));
Lwt.return_unit
end
end
6 changes: 3 additions & 3 deletions rng/mirage_crypto_rng.mli
Expand Up @@ -218,17 +218,17 @@ module Entropy : sig
(** [interrupt_hook ()] collects the lower 4 bytes from [rdtsc], to be
used for entropy collection in the event loop. *)

val timer_accumulator : g -> unit -> unit
val timer_accumulator : g option -> unit -> unit
(** [timer_accumulator g] is the accumulator for the [`Timer] source,
applying {!interrupt_hook} on each call. *)

(** {1 Periodic pulled sources} *)

val feed_pools : g -> source -> (unit -> Cstruct.t) -> unit
val feed_pools : g option -> source -> (unit -> Cstruct.t) -> unit
(** [feed_pools g source f] feeds all pools of [g] using [source] by executing
[f] for each pool. *)

val cpu_rng : g -> unit
val cpu_rng : g option -> unit
(** [cpu_rng g] uses the CPU RNG (rdrand or rdseed) to feed all pools
of [g]. *)

Expand Down

0 comments on commit a8c7bbd

Please sign in to comment.