Skip to content

Commit

Permalink
Fortuna.add: don't allocate a 2 byte cstruct on each call
Browse files Browse the repository at this point in the history
Instead, use a temporary buffer. Contradicts mirage#186
  • Loading branch information
hannesm committed Jan 24, 2024
1 parent 46e71a9 commit b939bff
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions rng/fortuna.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,15 @@ let generate ~g bytes =
chunk (generate_rekey ~g n' :: acc) (n - n') in
Cstruct.concat @@ chunk [] bytes

let add ~g (source, _) ~pool data =
let pool = pool land (pools - 1)
and source = source land 0xff in
let header = Cs.of_bytes [ source ; Cstruct.length data ] in
g.pools.(pool) <- SHAd256.feedi g.pools.(pool) (iter2 header data);
if pool = 0 then g.pool0_size <- g.pool0_size + Cstruct.length data
let add ~g (source, _) ~pool =
let buf = Cstruct.create_unsafe 2 in
fun data ->
let pool = pool land (pools - 1)
and source = source land 0xff in
Cstruct.set_uint8 buf 0 source;
Cstruct.set_uint8 buf 1 (Cstruct.length data);
g.pools.(pool) <- SHAd256.feedi g.pools.(pool) (iter2 buf data);
if pool = 0 then g.pool0_size <- g.pool0_size + Cstruct.length data

(* XXX
* Schneier recommends against using generator-imposed pool-seeding schedule
Expand Down

0 comments on commit b939bff

Please sign in to comment.