Verified Commit 1a93df75 authored by Vincent Bernardoff's avatar Vincent Bernardoff Committed by Grégoire Henry

Hacl: refactoring

parent db740cee
......@@ -83,27 +83,48 @@ module Hash = struct
finish st
end
module SHA256 = Make(struct
(* state -> unit *)
external init : Bigstring.t -> unit =
"ml_Hacl_SHA2_256_init" [@@noalloc]
(* state -> data -> unit *)
external update : Bigstring.t -> Bigstring.t -> unit =
"ml_Hacl_SHA2_256_update" [@@noalloc]
(* state -> data -> datalen -> unit *)
external update_last : Bigstring.t -> Bigstring.t -> int -> unit =
"ml_Hacl_SHA2_256_update_last" [@@noalloc]
(* state -> hash *)
external finish : Bigstring.t -> Bigstring.t -> unit =
"ml_Hacl_SHA2_256_finish" [@@noalloc]
let bytes = 32
let blockbytes = 64
let statebytes = 137 * 4
end)
module SHA256 = struct
module H = Make(struct
(* state -> unit *)
external init : Bigstring.t -> unit =
"ml_Hacl_SHA2_256_init" [@@noalloc]
(* state -> data -> unit *)
external update : Bigstring.t -> Bigstring.t -> unit =
"ml_Hacl_SHA2_256_update" [@@noalloc]
(* state -> data -> datalen -> unit *)
external update_last : Bigstring.t -> Bigstring.t -> int -> unit =
"ml_Hacl_SHA2_256_update_last" [@@noalloc]
(* state -> hash *)
external finish : Bigstring.t -> Bigstring.t -> unit =
"ml_Hacl_SHA2_256_finish" [@@noalloc]
let bytes = 32
let blockbytes = 64
let statebytes = 137 * 4
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
(* state -> unit *)
......@@ -127,23 +148,6 @@ module Hash = struct
let statebytes = 169 * 8
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
module Nonce = struct
......
......@@ -40,6 +40,15 @@ module Hash : sig
(** Direct Interface *)
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
module SHA512 : sig
......@@ -55,15 +64,6 @@ module Hash : sig
val digest : Bigstring.t -> Bigstring.t
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
module Nonce : sig
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment