Skip to content

Commit 4b757b1

Browse files
hannesmreynir
andauthored
use digestif 1.2.0 API (#215)
* use digestif 1.2.0 API Co-authored-by: Reynir Björnsson <reynir@reynir.dk>
1 parent fff44e5 commit 4b757b1

5 files changed

+43
-89
lines changed

ec/mirage_crypto_ec.ml

+13-11
Original file line numberDiff line numberDiff line change
@@ -967,15 +967,22 @@ module Ed25519 = struct
967967

968968
type priv = string
969969

970+
let sha512 datas =
971+
let open Digestif.SHA512 in
972+
let buf = Bytes.create digest_size in
973+
let ctx = List.fold_left (feed_string ?off:None ?len:None) empty datas in
974+
get_into_bytes ctx buf;
975+
buf
976+
970977
(* RFC 8032 *)
971978
let public secret =
972979
(* section 5.1.5 *)
973980
(* step 1 *)
974-
let h = Digestif.SHA512.(digest_string secret |> to_raw_string) in
981+
let h = sha512 [ secret ] in
975982
(* step 2 *)
976983
let s, rest =
977-
Bytes.unsafe_of_string (String.sub h 0 key_len),
978-
String.sub h key_len (String.length h - key_len)
984+
Bytes.sub h 0 key_len,
985+
Bytes.unsafe_to_string (Bytes.sub h key_len (Bytes.length h - key_len))
979986
in
980987
Bytes.set_uint8 s 0 ((Bytes.get_uint8 s 0) land 248);
981988
Bytes.set_uint8 s 31 (((Bytes.get_uint8 s 31) land 127) lor 64);
@@ -1009,13 +1016,11 @@ module Ed25519 = struct
10091016
let sign ~key msg =
10101017
(* section 5.1.6 *)
10111018
let pub, (s, prefix) = public key in
1012-
let r = Digestif.SHA512.(digest_string (String.concat "" [ prefix; msg ]) |> to_raw_string) in
1013-
let r = Bytes.unsafe_of_string r in
1019+
let r = sha512 [ prefix; msg ] in
10141020
reduce_l r;
10151021
let r = Bytes.unsafe_to_string r in
10161022
let r_big = scalar_mult_base_to_bytes r in
1017-
let k = Digestif.SHA512.(digest_string (String.concat "" [ r_big; pub; msg]) |> to_raw_string) in
1018-
let k = Bytes.unsafe_of_string k in
1023+
let k = sha512 [ r_big; pub; msg] in
10191024
reduce_l k;
10201025
let k = Bytes.unsafe_to_string k in
10211026
let s_out = muladd k s r in
@@ -1041,10 +1046,7 @@ module Ed25519 = struct
10411046
String.equal s'' s'
10421047
in
10431048
if s_smaller_l then begin
1044-
let k =
1045-
Digestif.SHA512.(digest_string (String.concat "" [ r ; key ; msg ]) |> to_raw_string)
1046-
in
1047-
let k = Bytes.unsafe_of_string k in
1049+
let k = sha512 [ r ; key ; msg ] in
10481050
reduce_l k;
10491051
let k = Bytes.unsafe_to_string k in
10501052
let success, r' = double_scalar_mult k key s in

mirage-crypto-ec.opam

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ depends: [
3030
"dune-configurator"
3131
"eqaf" {>= "0.7"}
3232
"mirage-crypto-rng" {=version}
33-
"digestif" {>= "1.1.4"}
33+
"digestif" {>= "1.2.0"}
3434
"hex" {with-test}
3535
"alcotest" {with-test & >= "0.8.1"}
3636
"ppx_deriving_yojson" {with-test}

mirage-crypto-pk.opam

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ depends: [
2020
"randomconv" {with-test & >= "0.2.0"}
2121
"mirage-crypto" {=version}
2222
"mirage-crypto-rng" {=version}
23-
"digestif" {>= "1.1.4"}
23+
"digestif" {>= "1.2.0"}
2424
"zarith" {>= "1.13"}
2525
"eqaf" {>= "0.8"}
2626
]

pk/mirage_crypto_pk.mli

+5-8
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,12 @@ module Rsa : sig
187187
was produced with the given [key] as per {{!sig_encode}sig_encode}, or
188188
[None] *)
189189

190-
type hash = [ `MD5 | `SHA1 | `SHA224 | `SHA256 | `SHA384 | `SHA512 ]
191-
(** The type of supported hash algorithms. *)
192-
193-
val min_key : hash -> bits
190+
val min_key : [< Digestif.hash' > `MD5 `SHA1 `SHA224 `SHA256 `SHA384 `SHA512 ] -> bits
194191
(** [min_key hash] is the minimum key size required by {{!sign}[sign]}. *)
195192

196193
val sign : ?crt_hardening:bool -> ?mask:mask ->
197-
hash:hash -> key:priv -> string or_digest ->
198-
string
194+
hash:[< Digestif.hash' > `MD5 `SHA1 `SHA224 `SHA256 `SHA384 `SHA512 ] ->
195+
key:priv -> string or_digest -> string
199196
(** [sign ~crt_hardening ~mask ~hash ~key message] is the PKCS 1.5
200197
signature of [message], signed by the [key], using the hash function
201198
[hash]. This is the full signature, with the ASN-encoded message digest
@@ -208,8 +205,8 @@ module Rsa : sig
208205
209206
@raise Invalid_argument if message is a [`Digest] of the wrong size. *)
210207

211-
val verify : hashp:(hash -> bool) -> key:pub ->
212-
signature:string -> string or_digest -> bool
208+
val verify : hashp:([< Digestif.hash' > `MD5 `SHA1 `SHA224 `SHA256 `SHA384 `SHA512 ] -> bool) ->
209+
key:pub -> signature:string -> string or_digest -> bool
213210
(** [verify ~hashp ~key ~signature message] checks that [signature] is the
214211
PKCS 1.5 signature of the [message] under the given [key].
215212

pk/rsa.ml

+23-68
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ module Digest_or (H : Digestif.S) = struct
2323
invalid_arg "(`Digest _): %d bytes, expecting %d" n m
2424
end
2525

26-
let digest_or (type a) ~(hash : a Digestif.hash) =
27-
let module H = (val Digestif.module_of hash) in
28-
let module D = Digest_or (H) in
29-
D.digest_or
30-
3126
exception Insufficient_key
3227

3328
type pub = { e : Z.t ; n : Z.t }
@@ -274,69 +269,23 @@ module PKCS1 = struct
274269
String.length msg >= String.length asn &&
275270
String.equal asn (String.sub msg 0 (String.length asn))
276271

277-
type hash = [ `MD5 | `SHA1 | `SHA224 | `SHA256 | `SHA384 | `SHA512 ]
278-
279-
let digestif_or = function
280-
| `MD5 -> digest_or ~hash:Digestif.md5
281-
| `SHA1 -> digest_or ~hash:Digestif.sha1
282-
| `SHA224 -> digest_or ~hash:Digestif.sha224
283-
| `SHA256 -> digest_or ~hash:Digestif.sha256
284-
| `SHA384 -> digest_or ~hash:Digestif.sha384
285-
| `SHA512 -> digest_or ~hash:Digestif.sha512
286-
287-
let digestif_size = function
288-
| `MD5 ->
289-
let module H = (val Digestif.module_of Digestif.md5) in
290-
H.digest_size
291-
| `SHA1 ->
292-
let module H = (val Digestif.module_of Digestif.sha1) in
293-
H.digest_size
294-
| `SHA224 ->
295-
let module H = (val Digestif.module_of Digestif.sha224) in
296-
H.digest_size
297-
| `SHA256 ->
298-
let module H = (val Digestif.module_of Digestif.sha256) in
299-
H.digest_size
300-
| `SHA384 ->
301-
let module H = (val Digestif.module_of Digestif.sha384) in
302-
H.digest_size
303-
| `SHA512 ->
304-
let module H = (val Digestif.module_of Digestif.sha512) in
305-
H.digest_size
306-
307272
let asn_of_hash, detect =
308-
let md5 = "\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10"
309-
and sha1 = "\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14"
310-
and sha224 = "\x30\x2d\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04\x05\x00\x04\x1c"
311-
and sha256 = "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20"
312-
and sha384 = "\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02\x05\x00\x04\x30"
313-
and sha512 = "\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40"
273+
let map = [
274+
`MD5, "\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10" ;
275+
`SHA1, "\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14" ;
276+
`SHA224, "\x30\x2d\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04\x05\x00\x04\x1c" ;
277+
`SHA256, "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20" ;
278+
`SHA384, "\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02\x05\x00\x04\x30" ;
279+
`SHA512, "\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40"
280+
]
314281
in
315-
(function
316-
| `MD5 -> md5
317-
| `SHA1 -> sha1
318-
| `SHA224 -> sha224
319-
| `SHA256 -> sha256
320-
| `SHA384 -> sha384
321-
| `SHA512 -> sha512),
322-
(fun buf ->
323-
if is_prefix md5 buf then
324-
Some (`MD5, md5)
325-
else if is_prefix sha1 buf then
326-
Some (`SHA1, sha1)
327-
else if is_prefix sha224 buf then
328-
Some (`SHA224, sha224)
329-
else if is_prefix sha256 buf then
330-
Some (`SHA256, sha256)
331-
else if is_prefix sha384 buf then
332-
Some (`SHA384, sha384)
333-
else if is_prefix sha512 buf then
334-
Some (`SHA512, sha512)
335-
else
336-
None)
282+
(fun h -> List.assoc h map),
283+
(fun buf -> List.find_opt (fun (_, d) -> is_prefix d buf) map)
337284

338285
let sign ?(crt_hardening = true) ?mask ~hash ~key msg =
339-
let msg' = asn_of_hash hash ^ digestif_or hash msg in
286+
let module H = (val Digestif.module_of_hash' (hash :> Digestif.hash')) in
287+
let module D = Digest_or(H) in
288+
let msg' = asn_of_hash hash ^ D.digest_or msg in
340289
sig_encode ~crt_hardening ?mask ~key msg'
341290

342291
let verify ~hashp ~key ~signature msg =
@@ -346,11 +295,14 @@ module PKCS1 = struct
346295
Option.value
347296
(sig_decode ~key signature >>= fun buf ->
348297
detect buf >>| fun (hash, asn) ->
349-
hashp hash && Eqaf.equal (asn ^ digestif_or hash msg) buf)
298+
let module H = (val Digestif.module_of_hash' (hash :> Digestif.hash')) in
299+
let module D = Digest_or(H) in
300+
hashp hash && Eqaf.equal (asn ^ D.digest_or msg) buf)
350301
~default:false
351302

352303
let min_key hash =
353-
(String.length (asn_of_hash hash) + digestif_size hash + min_pad + 2) * 8 + 1
304+
let module H = (val Digestif.module_of_hash' (hash :> Digestif.hash')) in
305+
(String.length (asn_of_hash hash) + H.digest_size + min_pad + 2) * 8 + 1
354306
end
355307

356308
module MGF1 (H : Digestif.S) = struct
@@ -364,8 +316,11 @@ module MGF1 (H : Digestif.S) = struct
364316
let mgf ~seed len =
365317
let rec go acc c = function
366318
| 0 -> Bytes.sub (Bytes.concat Bytes.empty (List.rev acc)) 0 len
367-
| n -> let h = Bytes.unsafe_of_string H.(digesti_string (iter2 seed (repr c)) |> to_raw_string) in
368-
go (h :: acc) Int32.(succ c) (pred n) in
319+
| n ->
320+
let h = Bytes.create H.digest_size in
321+
H.get_into_bytes (H.feedi_string H.empty (iter2 seed (repr c))) h;
322+
go (h :: acc) Int32.(succ c) (pred n)
323+
in
369324
go [] 0l (len // H.digest_size)
370325

371326
let mask ~seed buf =

0 commit comments

Comments
 (0)