upstream: https://github.com/mirage/mirage-crypto
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

new test_entropy: testing bootstrap function and timer to not produce the same output on two subsequent calls

rename test_entropy to test_entropy_collection

+66 -37
+2 -2
rng/mirage_crypto_rng.mli
··· 96 96 (** {1 Timer source} *) 97 97 98 98 val interrupt_hook : unit -> unit -> Cstruct.t 99 - (** [interrupt_hook ()] collects the lower 4 bytes from [rdtsc], to be 99 + (** [interrupt_hook ()] collects lower bytes from the cycle counter, to be 100 100 used for entropy collection in the event loop. *) 101 101 102 102 val timer_accumulator : g option -> unit -> unit 103 - (** [timer_accumulator g] is the accumulator for the [`Timer] source, 103 + (** [timer_accumulator g] is the accumulator for the timer source, 104 104 applying {!interrupt_hook} on each call. *) 105 105 106 106 (** {1 Periodic pulled sources} *)
+8 -2
tests/dune
··· 28 28 (modules test_numeric test_dh test_dsa test_rsa test_pk_runner)) 29 29 30 30 (test 31 - (name test_entropy) 32 - (modules test_entropy) 31 + (name test_entropy_collection) 32 + (modules test_entropy_collection) 33 33 (package mirage-crypto-rng-mirage) 34 34 (libraries mirage-crypto-rng-mirage mirage-unix mirage-time-unix mirage-clock-unix)) 35 + 36 + (test 37 + (name test_entropy) 38 + (modules test_entropy) 39 + (package mirage-crypto-rng) 40 + (libraries mirage-crypto-rng))
+18 -33
tests/test_entropy.ml
··· 1 - open Lwt.Infix 2 1 3 - module Printing_rng = struct 4 - type g = unit 2 + let data = ref Cstruct.empty 5 3 6 - let block = 16 7 - 8 - let create ?time:_ () = () 9 - 10 - let generate ~g:_ _n = assert false 4 + let bootstrap_check () = 5 + for _i = 0 to 10 do 6 + let data' = Mirage_crypto_rng.Entropy.bootstrap 1 in 7 + if Cstruct.equal !data data' then failwith "same data from bootstrap" ; 8 + data := data' 9 + done 11 10 12 - let reseed ~g:_ data = 13 - Format.printf "reseeding: %a@.%!" Cstruct.hexdump_pp data 14 - 15 - let accumulate ~g:_ source = 16 - let print data = 17 - Format.printf "accumulate: (src: %a) %a@.%!" 18 - Mirage_crypto_rng.Entropy.pp_source source Cstruct.hexdump_pp data 19 - in 20 - `Acc print 21 - 22 - let seeded ~g:_ = true 23 - let pools = 1 24 - end 25 - 26 - module E = Mirage_crypto_rng_mirage.Make(Time)(Mclock) 27 - 28 - let with_entropy act = 29 - E.initialize (module Printing_rng) >>= fun () -> 30 - Format.printf "entropy sources: %a@,%!" 31 - (fun ppf -> List.iter (fun x -> 32 - Mirage_crypto_rng.Entropy.pp_source ppf x; 33 - Format.pp_print_space ppf ())) 34 - (Mirage_crypto_rng.Entropy.sources ()); 35 - act () 11 + let timer_check () = 12 + let data' = Mirage_crypto_rng.Entropy.interrupt_hook () () in 13 + data := Cstruct.create (Cstruct.len data'); 14 + for _i = 0 to 10 do 15 + let data' = Mirage_crypto_rng.Entropy.interrupt_hook () () in 16 + if Cstruct.equal !data data' then failwith "same data from timer" ; 17 + Cstruct.blit data' 0 !data 0 (Cstruct.len data') 18 + done 36 19 37 20 let () = 38 - OS.(Main.run (with_entropy (fun () -> Time.sleep_ns 1_000L))) 21 + timer_check (); 22 + bootstrap_check (); 23 + print_endline "test entropy OK"
+38
tests/test_entropy_collection.ml
··· 1 + open Lwt.Infix 2 + 3 + module Printing_rng = struct 4 + type g = unit 5 + 6 + let block = 16 7 + 8 + let create ?time:_ () = () 9 + 10 + let generate ~g:_ _n = assert false 11 + 12 + let reseed ~g:_ data = 13 + Format.printf "reseeding: %a@.%!" Cstruct.hexdump_pp data 14 + 15 + let accumulate ~g:_ source = 16 + let print data = 17 + Format.printf "accumulate: (src: %a) %a@.%!" 18 + Mirage_crypto_rng.Entropy.pp_source source Cstruct.hexdump_pp data 19 + in 20 + `Acc print 21 + 22 + let seeded ~g:_ = true 23 + let pools = 1 24 + end 25 + 26 + module E = Mirage_crypto_rng_mirage.Make(Time)(Mclock) 27 + 28 + let with_entropy act = 29 + E.initialize (module Printing_rng) >>= fun () -> 30 + Format.printf "entropy sources: %a@,%!" 31 + (fun ppf -> List.iter (fun x -> 32 + Mirage_crypto_rng.Entropy.pp_source ppf x; 33 + Format.pp_print_space ppf ())) 34 + (Mirage_crypto_rng.Entropy.sources ()); 35 + act () 36 + 37 + let () = 38 + OS.(Main.run (with_entropy (fun () -> Time.sleep_ns 1_000L)))