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,27 +83,48 @@ module Hash = struct
finish st finish st
end end
module SHA256 = Make(struct module SHA256 = struct
(* state -> unit *) module H = Make(struct
external init : Bigstring.t -> unit = (* state -> unit *)
"ml_Hacl_SHA2_256_init" [@@noalloc] external init : Bigstring.t -> unit =
"ml_Hacl_SHA2_256_init" [@@noalloc]
(* state -> data -> unit *) (* state -> data -> unit *)
external update : Bigstring.t -> Bigstring.t -> unit = external update : Bigstring.t -> Bigstring.t -> unit =
"ml_Hacl_SHA2_256_update" [@@noalloc] "ml_Hacl_SHA2_256_update" [@@noalloc]
(* state -> data -> datalen -> unit *) (* state -> data -> datalen -> unit *)
external update_last : Bigstring.t -> Bigstring.t -> int -> unit = external update_last : Bigstring.t -> Bigstring.t -> int -> unit =
"ml_Hacl_SHA2_256_update_last" [@@noalloc] "ml_Hacl_SHA2_256_update_last" [@@noalloc]
(* state -> hash *) (* state -> hash *)
external finish : Bigstring.t -> Bigstring.t -> unit = external finish : Bigstring.t -> Bigstring.t -> unit =
"ml_Hacl_SHA2_256_finish" [@@noalloc] "ml_Hacl_SHA2_256_finish" [@@noalloc]
let bytes = 32 let bytes = 32
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