package sodium-fmt

  1. Overview
  2. Docs

Source file sodium_fmt.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
(* 
 * This file is part of sodium-fmt (https://git.sr.ht/~schube/sodium-fmt).
 * Copyright © 2022 Victor Schubert.
 *
 * sodium-fmt is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * sodium-fmt is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * sodium-fmt. If not, see <https://www.gnu.org/licenses/>. 
 *)

open Fmt

let base64 : bytes Fmt.t =
  let base64' bytes =
    bytes |> Bytes.unsafe_to_string
    |> Base64.(encode_exn ~alphabet:uri_safe_alphabet)
  in
  using base64' string

let parens_pair left right = parens @@ hvbox @@ pair ~sep:sp left right

let prefix prefix fmt formatter value =
  (parens_pair string fmt) formatter (prefix, value)

let of_typed prefix_fmt fmt conv formatter value =
  match conv value with
  | value -> fmt formatter value
  | exception Sodium.Size_mismatch _ ->
      (prefix_fmt @@ styled `Bold
      @@ styled (`Fg (`Hi `Red))
      @@ const string "<size mismatch>")
        formatter ()

module Box = struct
  open Sodium.Box.Bytes

  let secret_key_prefix fmt = prefix "Box.secret_key" fmt
  let secret_key = secret_key_prefix @@ using of_secret_key base64
  let public_key_prefix fmt = prefix "Box.public_key" fmt
  let public_key = public_key_prefix @@ using of_public_key base64
  let channel_key_prefix fmt = prefix "Box.channel_key" fmt
  let channel_key = channel_key_prefix @@ using of_channel_key base64
  let nonce_prefix fmt = prefix "Box.nonce" fmt
  let nonce = nonce_prefix @@ using of_nonce base64
  let keypair = parens_pair secret_key public_key

  module type S = sig
    type storage

    val secret_key : storage Fmt.t
    val public_key : storage Fmt.t
    val channel_key : storage Fmt.t
    val nonce : storage Fmt.t
  end

  module type Storage = sig
    open Sodium.Box

    type storage

    val to_secret_key : storage -> secret_key
    val to_public_key : storage -> public_key
    val to_channel_key : storage -> channel_key
    val to_nonce : storage -> nonce
  end

  module Make (Storage : Storage) = struct
    type storage = Storage.storage

    open Storage

    let secret_key = of_typed secret_key_prefix secret_key to_secret_key
    let public_key = of_typed public_key_prefix public_key to_public_key
    let channel_key = of_typed channel_key_prefix channel_key to_channel_key
    let nonce = of_typed nonce_prefix nonce to_nonce
  end

  module Bytes = Make (Sodium.Box.Bytes)
  module Bigbytes = Make (Sodium.Box.Bigbytes)
end

module Scalar_mult = struct
  open Sodium.Scalar_mult.Bytes

  let group_elt_prefix fmt = prefix "Scalar_mult.group_elt" fmt
  let group_elt = group_elt_prefix @@ using of_group_elt base64
  let integer_prefix fmt = prefix "Scalar_mult.integer" fmt
  let integer = integer_prefix @@ using of_integer base64

  module type S = sig
    type storage

    val group_elt : storage Fmt.t
    val integer : storage Fmt.t
  end

  module type Storage = sig
    open Sodium.Scalar_mult

    type storage

    val to_group_elt : storage -> group_elt
    val to_integer : storage -> integer
  end

  module Make (Storage : Storage) = struct
    type storage = Storage.storage

    open Storage

    let group_elt = of_typed group_elt_prefix group_elt to_group_elt
    let integer = of_typed integer_prefix integer to_integer
  end

  module Bytes = Make (Sodium.Scalar_mult.Bytes)
  module Bigbytes = Make (Sodium.Scalar_mult.Bigbytes)
end

module Sign = struct
  open Sodium.Sign.Bytes

  let secret_key_prefix fmt = prefix "Sign.secret_key" fmt
  let secret_key = secret_key_prefix @@ using of_secret_key base64
  let public_key_prefix fmt = prefix "Sign.public_key" fmt
  let public_key = public_key_prefix @@ using of_public_key base64
  let keypair = parens_pair secret_key public_key
  let signature_prefix fmt = prefix "Sign.signature" fmt
  let signature = signature_prefix @@ using of_signature base64
  let seed_prefix fmt = prefix "Sign.seed" fmt
  let seed = seed_prefix @@ using of_seed base64

  module type S = sig
    type storage

    val secret_key : storage Fmt.t
    val public_key : storage Fmt.t
    val signature : storage Fmt.t
    val seed : storage Fmt.t
  end

  module type Storage = sig
    open Sodium.Sign

    type storage

    val to_secret_key : storage -> secret_key
    val to_public_key : storage -> public_key
    val to_signature : storage -> signature
    val to_seed : storage -> seed
  end

  module Make (Storage : Storage) = struct
    type storage = Storage.storage

    open Storage

    let secret_key = of_typed secret_key_prefix secret_key to_secret_key
    let public_key = of_typed public_key_prefix public_key to_public_key
    let signature = of_typed signature_prefix signature to_signature
    let seed = of_typed seed_prefix seed to_seed
  end

  module Bytes = Make (Sodium.Sign.Bytes)
  module Bigbytes = Make (Sodium.Sign.Bigbytes)
end

module Password_hash = struct
  open Sodium.Password_hash.Bytes

  let salt_prefix fmt = prefix "Password_hash.salt" fmt
  let salt = salt_prefix @@ using of_salt base64

  module type S = sig
    type storage

    val salt : storage Fmt.t
  end

  module type Storage = sig
    open Sodium.Password_hash

    type storage

    val to_salt : storage -> salt
  end

  module Make (Storage : Storage) = struct
    type storage = Storage.storage

    open Storage

    let salt = of_typed salt_prefix salt to_salt
  end

  module Bytes = Make (Sodium.Password_hash.Bytes)
  module Bigbytes = Make (Sodium.Password_hash.Bigbytes)
end

module Secret_box = struct
  open Sodium.Secret_box.Bytes

  let secret_key_prefix fmt = prefix "Secret_box.secret_key" fmt
  let secret_key = secret_key_prefix @@ using of_key base64
  let nonce_prefix fmt = prefix "Secret_box.nonce" fmt
  let nonce = nonce_prefix @@ using of_nonce base64

  module type S = sig
    type storage

    val secret_key : storage Fmt.t
    val nonce : storage Fmt.t
  end

  module type Storage = sig
    open Sodium.Secret_box

    type storage

    val to_key : storage -> secret_key
    val to_nonce : storage -> nonce
  end

  module Make (Storage : Storage) = struct
    type storage = Storage.storage

    open Storage

    let secret_key = of_typed secret_key_prefix secret_key to_key
    let nonce = of_typed nonce_prefix nonce to_nonce
  end

  module Bytes = Make (Sodium.Secret_box.Bytes)
  module Bigbytes = Make (Sodium.Secret_box.Bigbytes)
end

module Stream = struct
  open Sodium.Stream.Bytes

  let secret_key_prefix fmt = prefix "Stream.secret_key" fmt
  let secret_key = secret_key_prefix @@ using of_key base64
  let nonce_prefix fmt = prefix "Stream.nonce" fmt
  let nonce = nonce_prefix @@ using of_nonce base64

  module type S = sig
    type storage

    val secret_key : storage Fmt.t
    val nonce : storage Fmt.t
  end

  module type Storage = sig
    open Sodium.Stream

    type storage

    val to_key : storage -> secret_key
    val to_nonce : storage -> nonce
  end

  module Make (Storage : Storage) = struct
    type storage = Storage.storage

    open Storage

    let secret_key = of_typed secret_key_prefix secret_key to_key
    let nonce = of_typed nonce_prefix nonce to_nonce
  end

  module Bytes = Make (Sodium.Stream.Bytes)
  module Bigbytes = Make (Sodium.Stream.Bigbytes)
end

module Auth = struct
  open Sodium.Auth.Bytes

  let secret_key_prefix fmt = prefix "Auth.secret_key" fmt
  let secret_key = secret_key_prefix @@ using of_key base64

  module type S = sig
    type storage

    val secret_key : storage Fmt.t
  end

  module type Storage = sig
    open Sodium.Auth

    type storage

    val to_key : storage -> secret_key
  end

  module Make (Storage : Storage) = struct
    type storage = Storage.storage

    open Storage

    let secret_key = of_typed secret_key_prefix secret_key to_key
  end

  module Bytes = Make (Sodium.Auth.Bytes)
  module Bigbytes = Make (Sodium.Auth.Bigbytes)
end

module One_time_auth = struct
  open Sodium.One_time_auth.Bytes

  let secret_key_prefix fmt = prefix "One_time_auth.secret_key" fmt
  let secret_key = secret_key_prefix @@ using of_key base64

  module type S = sig
    type storage

    val secret_key : storage Fmt.t
  end

  module type Storage = sig
    open Sodium.One_time_auth

    type storage

    val to_key : storage -> secret_key
  end

  module Make (Storage : Storage) = struct
    type storage = Storage.storage

    open Storage

    let secret_key = of_typed secret_key_prefix secret_key to_key
  end

  module Bytes = Make (Sodium.One_time_auth.Bytes)
  module Bigbytes = Make (Sodium.One_time_auth.Bigbytes)
end

module Hash = struct
  open Sodium.Hash.Bytes

  let hash_prefix fmt = prefix "Hash.hash" fmt
  let hash = hash_prefix @@ using of_hash base64

  module type S = sig
    type storage

    val hash : storage Fmt.t
  end

  module type Storage = sig
    open Sodium.Hash

    type storage

    val to_hash : storage -> hash
  end

  module Make (Storage : Storage) = struct
    type storage = Storage.storage

    open Storage

    let hash = of_typed hash_prefix hash to_hash
  end

  module Bytes = Make (Sodium.Hash.Bytes)
  module Bigbytes = Make (Sodium.Hash.Bigbytes)
end

module Generichash = struct
  open Sodium.Generichash.Bytes

  let hash_prefix fmt = prefix "Generichash.hash" fmt
  let hash = hash_prefix @@ using of_hash base64
  let secret_key_prefix fmt = prefix "Generichash.secret_key" fmt
  let secret_key = secret_key_prefix @@ using of_key base64

  module type S = sig
    type storage

    val hash : storage Fmt.t
    val secret_key : storage Fmt.t
  end

  module type Storage = sig
    open Sodium.Generichash

    type storage

    val to_hash : storage -> hash
    val to_key : storage -> secret_key
  end

  module Make (Storage : Storage) = struct
    type storage = Storage.storage

    open Storage

    let hash = of_typed hash_prefix hash to_hash
    let secret_key = of_typed secret_key_prefix secret_key to_key
  end

  module Bytes = Make (Sodium.Generichash.Bytes)
  module Bigbytes = Make (Sodium.Generichash.Bigbytes)
end
OCaml

Innovation. Community. Security.