package blake2

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file blake2.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(*---------------------------------------------------------------------------
   Copyright (c) 2018 Vincent Bernardoff. All rights reserved.
   Distributed under the ISC license, see terms at the end of the file.
  ---------------------------------------------------------------------------*)

module Blake2b = struct
  type t = Bytes.t

  external sizeof_state : unit -> int =
    "sizeof_blake2b_state" [@@noalloc]

  let bytes = sizeof_state ()

  external init : Bytes.t -> int -> int =
    "ml_blake2b_init" [@@noalloc]

  external outlen : Bytes.t -> int =
    "blake2b_state_outlen" [@@noalloc]

  let outlen t = outlen t

  external init_key : Bytes.t -> int -> Bytes.t -> int =
    "ml_blake2b_init_key" [@@noalloc]

  external update : Bytes.t -> Bytes.t -> int =
    "ml_blake2b_update" [@@noalloc]

  external final : Bytes.t -> Bytes.t -> int =
    "ml_blake2b_final" [@@noalloc]

  external direct :
    Bytes.t -> Bytes.t -> Bytes.t -> int =
    "ml_blake2b" [@@noalloc]

  let or_fail ~msg f =
    match f () with
    | 0 -> ()
    | _ -> failwith msg

  let init ?key size =
    if size < 1 || size > 64 then
      invalid_arg "Blake2b.init: size must be between 1 and 64" ;
    let t = Bytes.create bytes in
    begin match key with
      | Some key ->
          or_fail ~msg:"Blake2b.init"
            (fun () -> init_key t size key)
      | None ->
          or_fail ~msg:"Blake2b.init"
            (fun () -> init t size)
    end ;
    t

  let update t buf =
    or_fail ~msg:"Blake2b.update" (fun () -> update t buf)

  type hash = Hash of Bytes.t

  let final t =
    let len = outlen t in
    let buf = Bytes.create len in
    or_fail ~msg:"Blake2b.final" (fun () -> final t buf) ;
    Hash buf

  let direct ?(key=Bytes.create 0) inbuf len =
    if len < 1 || len > 64 then
      invalid_arg "Blake2b.direct: size must be between 1 and 64" ;
    let outbuf = Bytes.create len in
    or_fail ~msg:"Blake2b.direct" (fun () -> direct outbuf inbuf key) ;
    Hash outbuf
end

(*---------------------------------------------------------------------------
   Copyright (c) 2018 Vincent Bernardoff

   Permission to use, copy, modify, and/or distribute this software for any
   purpose with or without fee is hereby granted, provided that the above
   copyright notice and this permission notice appear in all copies.

   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  ---------------------------------------------------------------------------*)