package spoke

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

Source file flow.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
let src = Logs.Src.create "spoke.flow"

module Log = (val Logs.src_log src : Logs.LOG)

type ctx = {
  a_buffer : bytes;
  mutable a_pos : int;
  mutable a_max : int;
  b_buffer : bytes;
  mutable b_pos : int;
}

let ctx () =
  {
    a_buffer = Bytes.create 128;
    a_pos = 0;
    a_max = 0;
    b_buffer = Bytes.create 128;
    b_pos = 0;
  }

let remaining_bytes_of_ctx { a_pos; a_max; a_buffer; _ } =
  if a_pos >= a_max then None
  else Some (Bytes.sub_string a_buffer a_pos (a_max - a_pos))

type error = [ `Not_enough_space | `End_of_input | `Spoke of Spoke.error ]

let pp_error ppf = function
  | `Not_enough_space -> Fmt.pf ppf "Not enough space"
  | `End_of_input -> Fmt.pf ppf "End of input"
  | `Spoke err -> Spoke.pp_error ppf err

type 'a t =
  | Rd of { buf : bytes; off : int; len : int; k : 'a krd }
  | Wr of { str : string; off : int; len : int; k : 'a kwr }
  | Done of 'a
  | Fail of error

and 'a krd = [ `End | `Len of int ] -> 'a t
and 'a kwr = int -> 'a t

exception Leave of error

let leave_with _ctx error = raise (Leave error)
let safe k ctx = try k ctx with Leave err -> Fail err
let always x _ = x

module Send = struct
  let flush k0 ctx =
    if ctx.b_pos > 0 then
      let rec k1 n =
        if n < ctx.b_pos then
          Wr
            {
              str = Bytes.unsafe_to_string ctx.b_buffer;
              off = n;
              len = ctx.b_pos - n;
              k = (fun m -> k1 (n + m));
            }
        else (
          ctx.b_pos <- 0;
          k0 ctx)
      in
      k1 0
    else k0 ctx

  let write str ctx =
    let max = Bytes.length ctx.b_buffer in
    let go j l ctx =
      let rem = max - ctx.b_pos in
      let len = if l > rem then rem else l in
      Bytes.blit_string str j ctx.b_buffer ctx.b_pos len;
      ctx.b_pos <- ctx.b_pos + len;
      if len < l then leave_with ctx `Not_enough_space
    in
    go 0 (String.length str) ctx

  let send ctx str =
    safe
      (fun ctx ->
        write str ctx;
        flush (always (Done ())) ctx)
      ctx
end

module Recv = struct
  let prompt ~required k ctx =
    if ctx.a_pos > 0 then (
      let rest = ctx.a_max - ctx.a_pos in
      Bytes.blit ctx.a_buffer ctx.a_pos ctx.a_buffer 0 rest;
      ctx.a_max <- rest;
      ctx.a_pos <- 0);
    let rec go off =
      if off = Bytes.length ctx.a_buffer then Fail `Not_enough_space
      else if off - ctx.a_pos < required then
        let k = function
          | `Len len -> go (off + len)
          | `End -> Fail `End_of_input
        in
        Rd { buf = ctx.a_buffer; off; len = Bytes.length ctx.a_buffer - off; k }
      else (
        ctx.a_max <- off;
        safe k ctx)
    in
    go ctx.a_max

  let recv ctx ~len =
    let k ctx =
      let str = Bytes.sub_string ctx.a_buffer ctx.a_pos len in
      ctx.a_pos <- ctx.a_pos + len;
      Done str
    in
    prompt ~required:len k ctx
end

let ( let* ) =
  let rec go f m len =
    match m len with
    | Done v -> f v
    | Fail err -> Fail err
    | Rd { buf; off; len; k } -> Rd { buf; off; len; k = go f k }
    | Wr { str; off; len; k } ->
        let k0 = function `End -> k 0 | `Len len -> k len in
        let k1 = function 0 -> go f k0 `End | len -> go f k0 (`Len len) in
        Wr { str; off; len; k = k1 }
  in
  fun m f ->
    match m with
    | Done v -> f v
    | Fail err -> Fail err
    | Rd { buf; off; len; k } -> Rd { buf; off; len; k = go f k }
    | Wr { str; off; len; k } ->
        let k0 = function `End -> k 0 | `Len len -> k len in
        let k1 = function 0 -> go f k0 `End | len -> go f k0 (`Len len) in
        Wr { str; off; len; k = k1 }

let ( let+ ) x f = match x with Ok v -> f v | Error err -> Fail (`Spoke err)
let send = Send.send
let recv = Recv.recv
let return v = Done v

type cfg = Cfg : 'a Spoke.algorithm * 'a -> cfg

let handshake_client ctx ?g ~identity password =
  let* public = recv ctx ~len:34 in
  let+ ciphers = Spoke.ciphers_of_public public in
  let+ client, packet = Spoke.hello ?g ~public password in
  let* () = send ctx packet in
  let* packet = recv ctx ~len:96 in
  let+ shared_keys, packet =
    Spoke.client_compute ~client ~identity (String.sub packet 0 32)
      (String.sub packet 32 64)
  in
  let* () = send ctx packet in
  return (ciphers, shared_keys)

let handshake_server ctx ?g ~password ~identity (Cfg (algorithm, arguments)) =
  let ciphers = Spoke.(AEAD GCM, AEAD ChaCha20_Poly1305) in
  let secret, public =
    Spoke.generate ?g ~password ~ciphers ~algorithm arguments
  in
  let* () = send ctx (Spoke.public_to_string public) in
  let* packet = recv ctx ~len:32 in
  let+ server, (_Y, validator) =
    Spoke.server_compute ~secret ~identity packet
  in
  let* () = send ctx (_Y ^ validator) in
  let* packet = recv ctx ~len:64 in
  let+ shared_keys = Spoke.server_finalize ~server packet in
  return (ciphers, shared_keys)

type 'k cipher_block = (module Mirage_crypto.AEAD with type key = 'k)

let module_of : type k. k Spoke.aead -> k cipher_block = function
  | Spoke.GCM -> (module Mirage_crypto.Cipher_block.AES.GCM)
  | Spoke.CCM16 -> (module Mirage_crypto.Cipher_block.AES.CCM16)
  | Spoke.ChaCha20_Poly1305 -> (module Mirage_crypto.Chacha20)

module Make (Flow : Mirage_flow.S) = struct
  open Lwt.Infix

  let ( >>? ) = Lwt_result.bind
  let reword_error f = function Ok v -> Ok v | Error err -> Error (f err)

  type symmetric =
    | Symmetric : {
        key : 'k;
        nonce : Cstruct.t;
        impl : 'k cipher_block;
      }
        -> symmetric

  external xor_into :
    Bigstringaf.t ->
    src_off:int ->
    Bigstringaf.t ->
    dst_off:int ->
    len:int ->
    unit = "spoke_xor_into_generic_bigarray"

  let xor src dst =
    let len = min (Cstruct.length src) (Cstruct.length dst) in
    xor_into (Cstruct.to_bigarray src) ~src_off:0 (Cstruct.to_bigarray dst)
      ~dst_off:0 ~len

  let xor a b =
    let len = min (Cstruct.length a) (Cstruct.length b) in
    let res = Cstruct.of_string (Cstruct.to_string b ~off:0 ~len) in
    xor a res;
    res

  let make_nonce nonce seq =
    let seq =
      let len = Cstruct.length nonce in
      let seq =
        let buf = Cstruct.create 8 in
        Cstruct.BE.set_uint64 buf 0 seq;
        buf
      in
      let pad = Cstruct.create (len - 8) in
      Cstruct.append pad seq
    in
    xor nonce seq

  let make_adata len =
    let buf = Cstruct.create 4 in
    Cstruct.BE.set_uint16 buf 0 Spoke.version;
    Cstruct.BE.set_uint16 buf 2 len;
    buf

  let encrypt (Symmetric { key; nonce; impl = (module Cipher_block) }) sequence
      buf =
    let nonce = make_nonce nonce sequence in
    let adata = make_adata (Cstruct.length buf) in
    Cipher_block.authenticate_encrypt ~key ~adata ~nonce buf

  let decrypt (Symmetric { key; nonce; impl = (module Cipher_block) }) sequence
      buf =
    let nonce = make_nonce nonce sequence in
    let adata = make_adata (Cstruct.length buf - Cipher_block.tag_size) in
    Cipher_block.authenticate_decrypt ~key ~adata ~nonce buf

  let symmetric_of_key_nonce_and_cipher key_nonce (Spoke.AEAD aead) =
    let key_len =
      match aead with
      | Spoke.GCM -> 32
      | Spoke.CCM16 -> 32
      | Spoke.ChaCha20_Poly1305 -> 32
    in
    let nonce_len =
      match aead with
      | Spoke.GCM -> 12
      | Spoke.CCM16 -> 12
      | Spoke.ChaCha20_Poly1305 -> 12
    in
    let module Cipher_block = (val module_of aead) in
    let key = Cstruct.of_string ~off:0 ~len:key_len key_nonce in
    Log.debug (fun m ->
        m "Private key: %s" (Base64.encode_exn (String.sub key_nonce 0 key_len)));
    let key = Cipher_block.of_secret key in
    let nonce = Cstruct.of_string ~off:key_len ~len:nonce_len key_nonce in
    Symmetric { key; nonce; impl = (module Cipher_block) }

  type flow = {
    flow : Flow.flow;
    recv : symmetric;
    send : symmetric;
    recv_record : Cstruct.t;
    send_record : Cstruct.t;
    mutable recv_seq : int64;
    mutable send_seq : int64;
    recv_queue : (char, Bigarray.int8_unsigned_elt) Ke.Rke.t;
    send_queue : (char, Bigarray.int8_unsigned_elt) Ke.Rke.t;
  }

  let blit0 src src_off dst dst_off len =
    let dst = Cstruct.of_bigarray dst ~off:dst_off ~len in
    Cstruct.blit src src_off dst 0 len

  let blit1 src src_off dst dst_off len =
    let src = Cstruct.of_bigarray src ~off:src_off ~len in
    Cstruct.blit_to_bytes src 0 dst dst_off len

  let run queue flow fiber =
    let cs_wr = Cstruct.create 128 in
    let allocator len = Cstruct.sub cs_wr 0 len in
    let rec go = function
      | Done v -> Lwt.return_ok v
      | Fail (#error as err) -> Lwt.return_error err
      | Rd { buf; off; len; k } as fiber ->
          if Ke.Rke.is_empty queue then (
            Flow.read flow >|= reword_error (fun err -> `Flow err) >>? function
            | `Eof -> go (k `End)
            | `Data cs ->
                Ke.Rke.N.push queue ~blit:blit0 ~length:Cstruct.length cs;
                go fiber)
          else
            let len = min len (Ke.Rke.length queue) in
            Ke.Rke.N.keep_exn queue ~blit:blit1 ~length:Bytes.length ~off ~len
              buf;
            Ke.Rke.N.shift_exn queue len;
            go (k (`Len len))
      | Wr { str; off; len; k } ->
          let cs = Cstruct.of_string ~allocator ~off ~len str in
          Flow.write flow cs >|= reword_error (fun err -> `Flow_write err)
          >>? fun () -> go (k len)
    in
    go fiber

  let max_record = 0xFFFF

  let client_of_flow ?g ~identity ~password flow =
    let ctx = ctx () in
    let queue = Ke.Rke.create ~capacity:128 Bigarray.char in
    run queue flow (handshake_client ctx ?g ~identity password)
    >>? fun ((cipher0, cipher1), (k0, k1)) ->
    let rem = remaining_bytes_of_ctx ctx in
    let rem = Option.value ~default:"" rem in
    let recv = symmetric_of_key_nonce_and_cipher k0 cipher0 in
    let send = symmetric_of_key_nonce_and_cipher k1 cipher1 in
    let recv_queue = Ke.Rke.create ~capacity:0x10000 Bigarray.char in
    let blit src src_off dst dst_off len =
      Bigstringaf.blit_from_string src ~src_off dst ~dst_off ~len
    in
    Ke.Rke.N.push recv_queue ~blit ~length:String.length rem;
    let send_queue = Ke.Rke.create ~capacity:0x10000 Bigarray.char in
    let recv_record =
      let (Symmetric { impl = (module Cipher_block); _ }) = recv in
      Cstruct.create (2 + max_record + Cipher_block.tag_size)
    in
    let send_record =
      let (Symmetric { impl = (module Cipher_block); _ }) = send in
      Cstruct.create (2 + max_record + Cipher_block.tag_size)
    in
    Lwt.return_ok
      {
        flow;
        recv;
        send;
        recv_record;
        send_record;
        recv_seq = 0L;
        send_seq = 0L;
        recv_queue;
        send_queue;
      }

  let server_of_flow ?g ~cfg ~identity ~password flow =
    let ctx = ctx () in
    let queue = Ke.Rke.create ~capacity:128 Bigarray.char in
    run queue flow (handshake_server ctx ?g ~identity ~password cfg)
    >>? fun ((cipher0, cipher1), (k0, k1)) ->
    let rem = remaining_bytes_of_ctx ctx in
    let rem = Option.value ~default:"" rem in
    Log.debug (fun m ->
        m "Remains %d byte(s) from the client." (String.length rem));
    let recv = symmetric_of_key_nonce_and_cipher k1 cipher1 in
    let send = symmetric_of_key_nonce_and_cipher k0 cipher0 in
    let recv_queue = Ke.Rke.create ~capacity:0x10000 Bigarray.char in
    let blit src src_off dst dst_off len =
      Bigstringaf.blit_from_string src ~src_off dst ~dst_off ~len
    in
    Ke.Rke.N.push recv_queue ~blit ~length:String.length rem;
    let send_queue = Ke.Rke.create ~capacity:0x10000 Bigarray.char in
    let recv_record =
      let (Symmetric { impl = (module Cipher_block); _ }) = recv in
      Cstruct.create (2 + max_record + Cipher_block.tag_size)
    in
    let send_record =
      let (Symmetric { impl = (module Cipher_block); _ }) = send in
      Cstruct.create (2 + max_record + Cipher_block.tag_size)
    in
    Lwt.return_ok
      {
        flow;
        recv;
        send;
        recv_record;
        send_record;
        recv_seq = 0L;
        send_seq = 0L;
        recv_queue;
        send_queue;
      }

  type write_error =
    [ `Closed | `Flow of Flow.error | `Flow_write of Flow.write_error | error ]

  let pp_write_error ppf = function
    | `Closed -> Flow.pp_write_error ppf `Closed
    | `Flow err -> Flow.pp_error ppf err
    | `Flow_write err -> Flow.pp_write_error ppf err
    | #error as err -> pp_error ppf err

  type error = [ `Flow of Flow.error | `Corrupted ]

  let pp_error ppf = function
    | `Flow err -> Flow.pp_error ppf err
    | `Corrupted -> Fmt.pf ppf "Communication corrupted"

  let get_record record queue symmetric =
    let (Symmetric { impl = (module Cipher_block); _ }) = symmetric in
    match Ke.Rke.length queue with
    | 0 -> `Await_hdr
    | 1 -> `Await_rec 1
    | 2 | _ ->
        let blit src src_off dst dst_off len =
          let src = Cstruct.of_bigarray src ~off:src_off ~len in
          Cstruct.blit src 0 dst dst_off len
        in
        Ke.Rke.N.keep_exn queue ~blit ~length:Cstruct.length record ~len:2;
        let len = Cstruct.BE.get_uint16 record 0 in
        if Ke.Rke.length queue >= len then (
          Ke.Rke.N.keep_exn queue ~blit ~length:Cstruct.length record ~len;
          Ke.Rke.N.shift_exn queue len;
          `Record (Cstruct.sub record 2 (len - 2)))
        else `Await_rec (len - Ke.Rke.length queue)

  let rec read flow =
    match get_record flow.recv_record flow.recv_queue flow.recv with
    | `Record buf -> (
        match decrypt flow.recv flow.recv_seq buf with
        | Some buf (* copy *) ->
            flow.recv_seq <- Int64.succ flow.recv_seq;
            Lwt.return_ok (`Data buf)
        | None -> Lwt.return_error `Corrupted)
    | (`Await_hdr | `Await_rec _) as await -> (
        Flow.read flow.flow >>= function
        | Error err -> Lwt.return_error (`Flow err)
        | Ok `Eof ->
            if await = `Await_hdr then Lwt.return_ok `Eof
            else Lwt.return_error `Corrupted
        | Ok (`Data buf) ->
            let blit src src_off dst dst_off len =
              let dst = Cstruct.of_bigarray dst ~off:dst_off ~len in
              Cstruct.blit src src_off dst 0 len
            in
            Ke.Rke.N.push flow.recv_queue ~blit ~length:Cstruct.length buf;
            read flow)

  let record ~dst ~sequence queue symmetric =
    let len = min max_record (Ke.Rke.length queue) in
    let blit src src_off dst dst_off len =
      let src = Cstruct.of_bigarray src ~off:src_off ~len in
      Cstruct.blit src 0 dst dst_off len
    in
    Ke.Rke.N.keep_exn queue ~length:Cstruct.length ~blit ~off:2 ~len dst;
    let buf (* copy *) = encrypt symmetric sequence (Cstruct.sub dst 2 len) in
    Ke.Rke.N.shift_exn queue len;
    let len = 2 + Cstruct.length buf in
    Cstruct.BE.set_uint16 dst 0 len;
    Cstruct.blit buf 0 dst 2 (Cstruct.length buf);
    Cstruct.sub dst 0 len

  let rec flush flow =
    if not (Ke.Rke.is_empty flow.send_queue) then (
      let record =
        record ~dst:flow.send_record ~sequence:flow.send_seq flow.send_queue
          flow.send
      in
      flow.send_seq <- Int64.succ flow.send_seq;
      Flow.write flow.flow record >>? fun () ->
      (* XXX(dinosaure): reset [send_record]? *)
      flush flow)
    else Lwt.return_ok ()

  let write flow data =
    Ke.Rke.N.push flow.send_queue ~blit:blit0 ~length:Cstruct.length data;
    flush flow >>= function
    | Ok () -> Lwt.return_ok ()
    | Error err -> Lwt.return_error (`Flow_write err)

  let read flow = read flow
  let write flow data = write flow data

  let writev flow css =
    let rec go = function
      | [] -> Lwt.return_ok ()
      | cs :: css -> (
          write flow cs >>= function
          | Ok () -> go css
          | Error err -> Lwt.return_error err)
    in
    go css

  let close { flow; _ } = Flow.close flow
  let shutdown { flow; _ } value = Flow.shutdown flow value
end
OCaml

Innovation. Community. Security.