···11+exception Unsupported of string
22+(** Raised by backend operations that are not available in the current
33+ implementation (e.g. DES, ChaCha20, Poly1305 in the pure OCaml backend). *)
44+15(** Simpler crypto
2637 Mirage-crypto is a cryptographic library.
+25-37
src/ocaml/native.ml
···2727 (* For GCM we only need encrypt direction. Store encryption keys. *)
2828 derive_e key rk rounds
29293030- (* Cache: avoid re-parsing round keys on every block encrypt *)
3131- let cached_rk = ref ""
3232- let cached_rka = ref [||]
3333-3434- let get_rka rk rounds =
3535- if rk == !cached_rk then !cached_rka
3636- else
3737- let nrk = (rounds + 1) * 4 in
3838- let rka =
3939- Array.init nrk (fun i ->
4040- let o = i * 4 in
4141- Int32.logor
4242- (Int32.logor
4343- (Int32.shift_left
4444- (Int32.of_int (Char.code (String.get rk o)))
4545- 24)
4646- (Int32.shift_left
4747- (Int32.of_int (Char.code (String.get rk (o + 1))))
4848- 16))
4949- (Int32.logor
5050- (Int32.shift_left
5151- (Int32.of_int (Char.code (String.get rk (o + 2))))
5252- 8)
5353- (Int32.of_int (Char.code (String.get rk (o + 3))))))
5454- in
5555- cached_rk := rk;
5656- cached_rka := rka;
5757- rka
3030+ let parse_rk rk rounds =
3131+ let nrk = (rounds + 1) * 4 in
3232+ Array.init nrk (fun i ->
3333+ let o = i * 4 in
3434+ Int32.logor
3535+ (Int32.logor
3636+ (Int32.shift_left (Int32.of_int (Char.code (String.get rk o))) 24)
3737+ (Int32.shift_left
3838+ (Int32.of_int (Char.code (String.get rk (o + 1))))
3939+ 16))
4040+ (Int32.logor
4141+ (Int32.shift_left
4242+ (Int32.of_int (Char.code (String.get rk (o + 2))))
4343+ 8)
4444+ (Int32.of_int (Char.code (String.get rk (o + 3))))))
58455946 let enc src soff dst doff rk rounds blocks =
6060- let rka = get_rka rk rounds in
4747+ let rka = parse_rk rk rounds in
6148 Aes_pure.encrypt_ecb rka rounds src soff dst doff blocks
62496350 let dec _src _soff _dst _doff _rk _rounds _blocks =
6464- failwith
6565- "AES decrypt not implemented in pure OCaml backend (GCM uses encrypt \
6666- only)"
5151+ raise
5252+ (Crypto.Unsupported
5353+ "AES decrypt not available in pure OCaml backend (GCM uses encrypt \
5454+ only)")
67556856 let mode () = 0 (* generic *)
6957end
···7260 let k_s () = 128
73617462 let des3key _key _mode _ks =
7575- failwith "DES not implemented in pure OCaml backend"
6363+ raise (Crypto.Unsupported "DES not available in pure OCaml backend")
76647765 let ddes _src _soff _dst _doff _blocks _ks =
7878- failwith "DES not implemented in pure OCaml backend"
6666+ raise (Crypto.Unsupported "DES not available in pure OCaml backend")
7967end
80688169module Chacha = struct
8270 let round _count _state _dst _off =
8383- failwith "ChaCha20 not implemented in pure OCaml backend"
7171+ raise (Crypto.Unsupported "ChaCha20 not available in pure OCaml backend")
8472end
85738674module Poly1305 = struct
···8977 let init _ctx _key = failwith "Poly1305 not implemented in pure OCaml backend"
90789179 let update _ctx _data _off _len =
9292- failwith "Poly1305 not implemented in pure OCaml backend"
8080+ raise (Crypto.Unsupported "Poly1305 not available in pure OCaml backend")
93819482 let finalize _ctx _mac _off =
9595- failwith "Poly1305 not implemented in pure OCaml backend"
8383+ raise (Crypto.Unsupported "Poly1305 not available in pure OCaml backend")
9684end
97859886module GHASH = struct