Hacl: refactoring

This commit is contained in:
Vincent Bernardoff 2018-07-02 10:00:56 +02:00 committed by Grégoire Henry
parent db740cee41
commit 1a93df75ae
No known key found for this signature in database
GPG Key ID: 50D984F20BD445D2
2 changed files with 47 additions and 43 deletions

View File

@ -83,7 +83,8 @@ module Hash = struct
finish st finish st
end end
module SHA256 = Make(struct module SHA256 = struct
module H = Make(struct
(* state -> unit *) (* state -> unit *)
external init : Bigstring.t -> unit = external init : Bigstring.t -> unit =
"ml_Hacl_SHA2_256_init" [@@noalloc] "ml_Hacl_SHA2_256_init" [@@noalloc]
@ -104,6 +105,26 @@ module Hash = struct
let blockbytes = 64 let blockbytes = 64
let statebytes = 137 * 4 let statebytes = 137 * 4
end) end)
include H
module HMAC = struct
(* mac -> key -> data *)
external hmac :
Bigstring.t -> Bigstring.t -> Bigstring.t -> unit =
"ml_Hacl_HMAC_SHA2_256_hmac" [@@noalloc]
let write_hmac ~key ~msg buf =
if Bigstring.length buf < 32 then
invalid_arg (Printf.sprintf "Hash.write_hmac_sha156: invalid len \
%d" 32) ;
hmac buf key msg
let hmac ~key ~msg =
let buf = Bigstring.create 32 in
write_hmac ~key ~msg buf ;
buf
end
end
module SHA512 = Make(struct module SHA512 = Make(struct
(* state -> unit *) (* state -> unit *)
@ -127,23 +148,6 @@ module Hash = struct
let statebytes = 169 * 8 let statebytes = 169 * 8
end) end)
module HMAC_SHA256 = struct
(* mac -> key -> data *)
external hmac :
Bigstring.t -> Bigstring.t -> Bigstring.t -> unit =
"ml_Hacl_HMAC_SHA2_256_hmac" [@@noalloc]
let write_hmac ~key ~msg buf =
if Bigstring.length buf < 32 then
invalid_arg (Printf.sprintf "Hash.write_hmac_sha156: invalid len \
%d" 32) ;
hmac buf key msg
let hmac ~key ~msg =
let buf = Bigstring.create 32 in
write_hmac ~key ~msg buf ;
buf
end
end end
module Nonce = struct module Nonce = struct

View File

@ -40,6 +40,15 @@ module Hash : sig
(** Direct Interface *) (** Direct Interface *)
val digest : Bigstring.t -> Bigstring.t val digest : Bigstring.t -> Bigstring.t
module HMAC : sig
val write_hmac :
key:Bigstring.t -> msg:Bigstring.t -> Bigstring.t -> unit
(** @raise [Invalid_argument] if argument is less than 32 bytes long *)
val hmac :
key:Bigstring.t -> msg:Bigstring.t -> Bigstring.t
end
end end
module SHA512 : sig module SHA512 : sig
@ -55,15 +64,6 @@ module Hash : sig
val digest : Bigstring.t -> Bigstring.t val digest : Bigstring.t -> Bigstring.t
end end
module HMAC_SHA256 : sig
val write_hmac :
key:Bigstring.t -> msg:Bigstring.t -> Bigstring.t -> unit
(** @raise Invalid_argument if argument is less than 32 bytes long *)
val hmac :
key:Bigstring.t -> msg:Bigstring.t -> Bigstring.t
end
end end
module Nonce : sig module Nonce : sig