Source file digestif.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
module Bi = Digestif_bigstring
module By = Digestif_bytes
module Pp = Digestif_pp
module Xor = Baijiu_xor
module type S = Digestif_sig.S
module type T = Digestif_sig.T
module type Desc =
sig
val digest_size : int
val block_size : int
end
module type BUFFER = Baijiu_buffer.S
module type Convenience =
sig
type t
val compare : t -> t -> int
val eq : t -> t -> bool
val neq : t -> t -> bool
end
module type Hash =
functor (Buffer : BUFFER) -> sig
type ctx
type buffer
val init : unit -> ctx
val feed : ctx -> buffer -> int -> int -> unit
val feed_bytes : ctx -> By.t -> int -> int -> unit
val feed_bigstring : ctx -> Bi.t -> int -> int -> unit
val get : ctx -> buffer
end with type buffer = Buffer.buffer
module Core (Hash : Hash) (D : Desc) =
struct
let digest_size = D.digest_size
let block_size = D.block_size
module Bigstring =
struct
include (Bi : Convenience with type t = Bi.t)
include Hash (struct include Bi type buffer = t end)
include Pp.Make (Bi) (D)
let feed ctx buf =
feed ctx buf 0 (Bi.length buf)
let feed_bytes ctx buf =
feed_bytes ctx buf 0 (By.length buf)
let feed_bigstring ctx buf =
feed_bigstring ctx buf 0 (Bi.length buf)
let digest buf =
let t = init () in ( feed t buf; get t)
let digestv bufs =
let t = init () in ( List.iter (feed t) bufs; get t )
end
module Bytes =
struct
include (By : Convenience with type t = By.t)
include Hash (struct include By type buffer = t end)
include Pp.Make (By) (D)
let feed ctx buf =
feed ctx buf 0 (By.length buf)
let feed_bytes ctx buf =
feed_bytes ctx buf 0 (By.length buf)
let feed_bigstring ctx buf =
feed_bigstring ctx buf 0 (Bi.length buf)
let digest buf =
let t = init () in ( feed t buf; get t)
let digestv bufs =
let t = init () in ( List.iter (feed t) bufs; get t )
end
end
module Make (H : Hash) (D : Desc) =
struct
module C = Core (H) (D)
let block_size = C.block_size
and digest_size = C.digest_size
and ctx_size = 0
module Bytes =
struct
module Xor = Xor.Make (struct include By type buffer = t end)
include C.Bytes
let opad = By.init C.block_size (fun _ -> '\x5c')
let ipad = By.init C.block_size (fun _ -> '\x36')
let rec norm key =
match Pervasives.compare (By.length key) C.block_size with
| 1 -> norm (C.Bytes.digest key)
| -1 -> By.rpad key C.block_size '\000'
| _ -> key
let hmacv ~key msg =
let key = norm key in
let outer = Xor.xor key opad in
let inner = Xor.xor key ipad in
C.Bytes.digestv [ outer; C.Bytes.digestv (inner :: msg) ]
let hmac ~key msg = hmacv ~key [ msg ]
end
module Bigstring =
struct
module Xor = Xor.Make (struct include Bi type buffer = t end)
include C.Bigstring
let opad = Bi.init C.block_size (fun _ -> '\x5c')
let ipad = Bi.init C.block_size (fun _ -> '\x36')
let rec norm key =
match Pervasives.compare (Bi.length key) C.block_size with
| 1 -> norm (C.Bigstring.digest key)
| -1 -> Bi.rpad key C.block_size '\000'
| _ -> key
let hmacv ~key msg =
let key = norm key in
let outer = Xor.xor key opad in
let inner = Xor.xor key ipad in
C.Bigstring.digestv [ outer; C.Bigstring.digestv (inner :: msg) ]
let hmac ~key msg = hmacv ~key [ msg ]
end
end
module NI (B : BUFFER) =
struct
let not_implemented () = raise (Failure "Not implemented")
type t = B.buffer
type buffer = B.buffer
type ctx = unit
let init = not_implemented
let feed ctx buf = not_implemented ()
let feed_bytes ctx buf = not_implemented ()
let feed_bigstring ctx buf = not_implemented ()
let get ctx = not_implemented ()
end
module type Hash' =
functor (Buffer : BUFFER) -> sig
type ctx
type buffer
val init : unit -> ctx
val init': buffer -> int -> int -> ctx
val feed : ctx -> buffer -> int -> int -> unit
val feed_bytes : ctx -> By.t -> int -> int -> unit
val feed_bigstring : ctx -> Bi.t -> int -> int -> unit
val get : ctx -> buffer
end with type buffer = Buffer.buffer
module Make_BLAKE2B (Hash : Hash') (D : Desc) : S =
struct
let digest_size = D.digest_size
module Bytes =
struct
include (By : Convenience with type t = By.t)
include Hash (struct include By type buffer = t end)
include Pp.Make (By) (D)
let feed ctx buf =
feed ctx buf 0 (By.length buf)
let feed_bytes ctx buf =
feed_bytes ctx buf 0 (By.length buf)
let feed_bigstring ctx buf =
feed_bigstring ctx buf 0 (Bi.length buf)
let digest buf =
let t = init () in ( feed t buf; get t)
let digestv bufs =
let t = init () in ( List.iter (feed t) bufs; get t )
let hmacv ~key msg =
let ctx = init' key 0 (By.length key) in
List.iter (fun x -> feed ctx x) msg;
get ctx
let hmac ~key msg =
hmacv ~key [ msg ]
end
module Bigstring =
struct
include (Bi : Convenience with type t = Bi.t)
include Hash (struct include Bi type buffer = t end)
include Pp.Make (Bi) (D)
let feed ctx buf =
feed ctx buf 0 (Bi.length buf)
let feed_bytes ctx buf =
feed_bytes ctx buf 0 (By.length buf)
let feed_bigstring ctx buf =
feed_bigstring ctx buf 0 (Bi.length buf)
let digest buf =
let t = init () in ( feed t buf; get t)
let digestv bufs =
let t = init () in ( List.iter (feed t) bufs; get t )
let hmacv ~key msg =
let ctx = init' key 0 (Bi.length key) in
List.iter (fun x -> feed ctx x) msg;
get ctx
let hmac ~key msg =
hmacv ~key [ msg ]
end
end
module DI =
struct
let digest_size = 0
let block_size = 0
end
module MD5 : S = Make (Baijiu_md5.Make) (struct let (digest_size, block_size) = (16, 64) end)
module SHA1 : S = Make (Baijiu_sha1.Make) (struct let (digest_size, block_size) = (20, 64) end)
module SHA224 : S = Make (Baijiu_sha224.Make) (struct let (digest_size, block_size) = (28, 64) end)
module SHA256 : S = Make (Baijiu_sha256.Make) (struct let (digest_size, block_size) = (32, 64) end)
module SHA384 : S = Make (Baijiu_sha384.Make) (struct let (digest_size, block_size) = (48, 128) end)
module SHA512 : S = Make (Baijiu_sha512.Make) (struct let (digest_size, block_size) = (64, 128) end)
module BLAKE2B = Make_BLAKE2B(Baijiu_blake2b.Make) (struct let (digest_size, block_size) = (64, 128) end)
module BLAKE2S = Make_BLAKE2B(Baijiu_blake2s.Make) (struct let (digest_size, block_size) = (32, 64) end)
module RMD160 : S = Make (Baijiu_rmd160.Make) (struct let (digest_size, block_size) = (20, 64) end)
type hash =
[ `MD5
| `SHA1
| `SHA224
| `SHA256
| `SHA384
| `SHA512
| `BLAKE2B
| `BLAKE2S
| `RMD160 ]
let module_of = function
| `MD5 -> (module MD5 : S)
| `SHA1 -> (module SHA1 : S)
| `SHA224 -> (module SHA224 : S)
| `SHA256 -> (module SHA256 : S)
| `SHA384 -> (module SHA384 : S)
| `SHA512 -> (module SHA512 : S)
| `BLAKE2B -> (module BLAKE2B : S)
| `BLAKE2S -> (module BLAKE2S : S)
| `RMD160 -> (module RMD160 : S)
module Bytes =
struct
type t = Bytes.t
type buffer = Bytes.t
let digest hash =
let module H = (val (module_of hash)) in
H.Bytes.digest
let digestv hash =
let module H = (val (module_of hash)) in
H.Bytes.digestv
let mac hash =
let module H = (val (module_of hash)) in
H.Bytes.hmac
let macv hash =
let module H = (val (module_of hash)) in
H.Bytes.hmacv
let of_hex hash =
let module H = (val (module_of hash)) in
H.Bytes.of_hex
let to_hex hash =
let module H = (val (module_of hash)) in
H.Bytes.to_hex
let pp hash =
let module H = (val (module_of hash)) in
H.Bytes.pp
end
module Bigstring =
struct
type t = Bi.t
type buffer = Bi.t
let digest hash =
let module H = (val (module_of hash)) in
H.Bigstring.digest
let digestv hash =
let module H = (val (module_of hash)) in
H.Bigstring.digestv
let mac hash =
let module H = (val (module_of hash)) in
H.Bigstring.hmac
let macv hash =
let module H = (val (module_of hash)) in
H.Bigstring.hmacv
let of_hex hash =
let module H = (val (module_of hash)) in
H.Bigstring.of_hex
let to_hex hash =
let module H = (val (module_of hash)) in
H.Bigstring.to_hex
let pp hash =
let module H = (val (module_of hash)) in
H.Bigstring.pp
end
let digest_size hash =
let module H = (val (module_of hash)) in
H.digest_size