@@ -23,11 +23,6 @@ module Digest_or (H : Digestif.S) = struct
23
23
invalid_arg " (`Digest _): %d bytes, expecting %d" n m
24
24
end
25
25
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
-
31
26
exception Insufficient_key
32
27
33
28
type pub = { e : Z .t ; n : Z .t }
@@ -274,69 +269,23 @@ module PKCS1 = struct
274
269
String. length msg > = String. length asn &&
275
270
String. equal asn (String. sub msg 0 (String. length asn))
276
271
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
-
307
272
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
+ ]
314
281
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)
337
284
338
285
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
340
289
sig_encode ~crt_hardening ?mask ~key msg'
341
290
342
291
let verify ~hashp ~key ~signature msg =
@@ -346,11 +295,14 @@ module PKCS1 = struct
346
295
Option. value
347
296
(sig_decode ~key signature >> = fun buf ->
348
297
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)
350
301
~default: false
351
302
352
303
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
354
306
end
355
307
356
308
module MGF1 (H : Digestif.S ) = struct
@@ -364,8 +316,11 @@ module MGF1 (H : Digestif.S) = struct
364
316
let mgf ~seed len =
365
317
let rec go acc c = function
366
318
| 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
369
324
go [] 0l (len // H. digest_size)
370
325
371
326
let mask ~seed buf =
0 commit comments