Skip to content

Commit

Permalink
Require OCaml 4.13 and remove local overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesm committed Jun 9, 2024
1 parent 9650c3f commit 6dc789a
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ocaml-version: ["4.14.2", "4.13.1", "4.12.1"]
ocaml-version: ["4.14.2", "4.13.1"]
operating-system: [macos-latest, ubuntu-latest]

runs-on: ${{ matrix.operating-system }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ocaml-version: ["4.14.2", "4.13.1", "4.12.1"]
ocaml-version: ["4.14.2", "4.13.1"]
operating-system: [windows-latest]

runs-on: ${{ matrix.operating-system }}
Expand Down
16 changes: 6 additions & 10 deletions ec/mirage_crypto_ec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ let rev_string buf =

exception Message_too_long

let string_get_uint8 buf idx =
(* TODO: use String.get_uint8 when mirage-crypto-ec requires OCaml >= 4.13 *)
Bytes.get_uint8 (Bytes.unsafe_of_string buf) idx

let bit_at buf i =
let byte_num = i / 8 in
let bit_num = i mod 8 in
let byte = string_get_uint8 buf byte_num in
let byte = String.get_uint8 buf byte_num in
byte land (1 lsl bit_num) <> 0

module type Dh = sig
Expand Down Expand Up @@ -320,7 +316,7 @@ module Make_point (P : Parameters) (F : Foreign) : Point = struct
if compress then
let out = Bytes.create (P.byte_length + 1) in
let ident =
2 + (string_get_uint8 buf ((P.byte_length * 2) - 1)) land 1
2 + (String.get_uint8 buf ((P.byte_length * 2) - 1)) land 1
in
Bytes.unsafe_blit_string buf 1 out 1 P.byte_length;
Bytes.set_uint8 out 0 ident;
Expand Down Expand Up @@ -389,9 +385,9 @@ module Make_point (P : Parameters) (F : Foreign) : Point = struct
let y' = Fe.from_montgomery y' in
let y_struct2 = Fe.to_octets y' in (* number must not be in montgomery domain*)
let y_struct2 = rev_string y_struct2 in
let ident = string_get_uint8 pk 0 in
let ident = String.get_uint8 pk 0 in
let signY =
2 + (string_get_uint8 y_struct (P.byte_length - 2)) land 1
2 + (String.get_uint8 y_struct (P.byte_length - 2)) land 1
in
let res = if Int.equal signY ident then y_struct else y_struct2 in
let out = Bytes.create ((P.byte_length * 2) + 1) in
Expand All @@ -410,7 +406,7 @@ module Make_point (P : Parameters) (F : Foreign) : Point = struct
let y = String.sub buf (1 + len) len in
validate_finite_point ~x ~y
in
match string_get_uint8 buf 0 with
match String.get_uint8 buf 0 with
| 0x00 when String.length buf = 1 ->
Ok (at_infinity ())
| 0x02 | 0x03 when String.length P.pident > 0 ->
Expand Down Expand Up @@ -614,7 +610,7 @@ module Make_dsa (Param : Parameters) (F : Fn) (P : Point) (S : Scalar) (H : Dige
let first_byte_ok () =
match Param.first_byte_bits with
| None -> true
| Some m -> (string_get_uint8 msg 0) land (0xFF land (lnot m)) = 0
| Some m -> (String.get_uint8 msg 0) land (0xFF land (lnot m)) = 0
in
if l > bl || (l = bl && not (first_byte_ok ())) then
raise Message_too_long
Expand Down
2 changes: 1 addition & 1 deletion mirage-crypto-ec.opam
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ doc: "https://mirage.github.io/mirage-crypto/doc"
bug-reports: "https://github.com/mirage/mirage-crypto/issues"
depends: [
"dune" {>= "2.7"}
"ocaml" {>= "4.08.0"}
"ocaml" {>= "4.13.0"}
"dune-configurator"
"eqaf" {>= "0.7"}
"mirage-crypto-rng" {=version}
Expand Down
2 changes: 1 addition & 1 deletion mirage-crypto-pk.opam
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ build: [ ["dune" "subst"] {dev}

depends: [
"conf-gmp-powm-sec" {build}
"ocaml" {>= "4.08.0"}
"ocaml" {>= "4.13.0"}
"dune" {>= "2.7"}
"ounit2" {with-test}
"randomconv" {with-test & >= "0.2.0"}
Expand Down
2 changes: 1 addition & 1 deletion mirage-crypto-rng-async.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build: [ ["dune" "subst"] {dev}
["dune" "runtest" "-p" name "-j" jobs] {with-test} ]

depends: [
"ocaml" {>= "4.08.0"}
"ocaml" {>= "4.13.0"}
"dune" {>= "2.7"}
"dune-configurator" {>= "2.0.0"}
"async" {>= "v0.14"}
Expand Down
2 changes: 1 addition & 1 deletion mirage-crypto-rng-lwt.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build: [ ["dune" "subst"] {dev}
["dune" "runtest" "-p" name "-j" jobs] {with-test} ]

depends: [
"ocaml" {>= "4.08.0"}
"ocaml" {>= "4.13.0"}
"dune" {>= "2.7"}
"duration"
"logs"
Expand Down
2 changes: 1 addition & 1 deletion mirage-crypto-rng-mirage.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build: [ ["dune" "subst"] {dev}
["dune" "runtest" "-p" name "-j" jobs] {with-test} ]

depends: [
"ocaml" {>= "4.08.0"}
"ocaml" {>= "4.13.0"}
"dune" {>= "2.7"}
"mirage-crypto-rng" {=version}
"duration"
Expand Down
2 changes: 1 addition & 1 deletion mirage-crypto-rng.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build: [ ["dune" "subst"] {dev}
["dune" "runtest" "-p" name "-j" jobs] {with-test} ]

depends: [
"ocaml" {>= "4.12.0"}
"ocaml" {>= "4.13.0"}
"dune" {>= "2.7"}
"dune-configurator" {>= "2.0.0"}
"duration"
Expand Down
2 changes: 1 addition & 1 deletion mirage-crypto.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build: [ ["dune" "subst"] {dev}
["dune" "runtest" "-p" name "-j" jobs] {with-test} ]

depends: [
"ocaml" {>= "4.09.0"}
"ocaml" {>= "4.13.0"}
"dune" {>= "2.7"}
"dune-configurator" {>= "2.0.0"}
"ounit2" {with-test}
Expand Down
29 changes: 10 additions & 19 deletions pk/rsa.ml
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,6 @@ let encrypt ~key = reformat (pub_bits key) (encrypt_z ~key)
let decrypt ?(crt_hardening=false) ?(mask=`Yes) ~key =
reformat (priv_bits key) (decrypt_z ~crt_hardening ~mask ~key)

(* OCaml 4.13 *)
let string_get_uint8 buf idx =
Bytes.get_uint8 (Bytes.unsafe_of_string buf) idx

let bx00, bx01 = "\x00", "\x01"

module PKCS1 = struct
Expand All @@ -214,7 +210,7 @@ module PKCS1 = struct
let rec go nonce i j =
if i = n then Bytes.unsafe_to_string buf else
if j = k then go Mirage_crypto_rng.(generate ?g k) i 0 else
match string_get_uint8 nonce j with
match String.get_uint8 nonce j with
| b when f b -> Bytes.set_uint8 buf i b ; go nonce (succ i) (succ j)
| _ -> go nonce i (succ j) in
go Mirage_crypto_rng.(generate ?g k) 0 0
Expand All @@ -226,9 +222,9 @@ module PKCS1 = struct
let unpad ~mark ~is_pad buf =
let f = not &. is_pad in
let i = ct_find_uint8 ~default:2 ~off:2 ~f buf in
let c1 = string_get_uint8 buf 0 = 0x00
and c2 = string_get_uint8 buf 1 = mark
and c3 = string_get_uint8 buf i = 0x00
let c1 = String.get_uint8 buf 0 = 0x00
and c2 = String.get_uint8 buf 1 = mark
and c3 = String.get_uint8 buf i = 0x00
and c4 = min_pad <= i - 2 in
if c1 && c2 && c3 && c4 then
Some (String.sub buf (i + 1) (String.length buf - i - 1))
Expand Down Expand Up @@ -264,11 +260,6 @@ module PKCS1 = struct
let decrypt ?(crt_hardening = false) ?mask ~key msg =
unpadded unpad_02 (decrypt ~crt_hardening ?mask ~key) (priv_bits key) msg

(* OCaml 4.13 contains starts_with *)
let is_prefix asn msg =
String.length msg >= String.length asn &&
String.equal asn (String.sub msg 0 (String.length asn))

let asn_of_hash, detect =
let map = [
`MD5, "\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10" ;
Expand All @@ -280,7 +271,7 @@ module PKCS1 = struct
]
in
(fun h -> List.assoc h map),
(fun buf -> List.find_opt (fun (_, d) -> is_prefix d buf) map)
(fun buf -> List.find_opt (fun (_, d) -> String.starts_with ~prefix:d buf) map)

let sign ?(crt_hardening = true) ?mask ~hash ~key msg =
let module H = (val Digestif.module_of_hash' (hash :> Digestif.hash')) in
Expand Down Expand Up @@ -353,8 +344,8 @@ module OAEP (H : Digestif.S) = struct
let db = Bytes.unsafe_to_string (MGF.mask ~seed:(Bytes.unsafe_to_string (MGF.mask ~seed:mdb ms)) mdb) in
let i = ct_find_uint8 ~default:0 ~off:hlen ~f:((<>) 0x00) db in
let c1 = Eqaf.equal (String.sub db 0 hlen) H.(digest_string label |> to_raw_string)
and c2 = string_get_uint8 b0 0 = 0x00
and c3 = string_get_uint8 db i = 0x01 in
and c2 = String.get_uint8 b0 0 = 0x00
and c3 = String.get_uint8 db i = 0x01 in
if c1 && c2 && c3 then Some (String.sub db (i + 1) (String.length db - i - 1)) else None

let encrypt ?g ?label ~key msg =
Expand Down Expand Up @@ -402,17 +393,17 @@ module PSS (H: Digestif.S) = struct
let emsa_pss_verify slen emlen em msg =
let mdb = String.sub em 0 (String.length em - hlen - 1)
and h = String.sub em (String.length em - hlen - 1) hlen
and bxx = string_get_uint8 em (String.length em - 1)
and bxx = String.get_uint8 em (String.length em - 1)
in
let db = MGF.mask ~seed:h mdb in
Bytes.set_uint8 db 0 (Bytes.get_uint8 db 0 land b0mask emlen) ;
let db = Bytes.unsafe_to_string db in
let salt = String.sub db (String.length db - slen) slen in
let h' = digest ~salt:salt msg
and i = ct_find_uint8 ~default:0 ~f:((<>) 0x00) db in
let c1 = lnot (b0mask emlen) land string_get_uint8 mdb 0 = 0x00
let c1 = lnot (b0mask emlen) land String.get_uint8 mdb 0 = 0x00
and c2 = i = String.length em - hlen - slen - 2
and c3 = string_get_uint8 db i = 0x01
and c3 = String.get_uint8 db i = 0x01
and c4 = bxx = 0xbc
and c5 = Eqaf.equal h h' in
c1 && c2 && c3 && c4 && c5
Expand Down
23 changes: 5 additions & 18 deletions pk/z_extra.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,24 @@ open Mirage_crypto.Uncommon

let bit_bound z = Z.size z * 64

(* revise once OCaml 4.13 is the lower bound *)
let string_get_int64_be buf idx =
Bytes.get_int64_be (Bytes.unsafe_of_string buf) idx

let string_get_int32_be buf idx =
Bytes.get_int32_be (Bytes.unsafe_of_string buf) idx

let string_get_uint16_be buf idx =
Bytes.get_uint16_be (Bytes.unsafe_of_string buf) idx

let string_get_uint8 buf idx =
Bytes.get_uint8 (Bytes.unsafe_of_string buf) idx

let of_octets_be ?bits buf =
let rec loop acc i = function
| b when b >= 64 ->
let x = string_get_int64_be buf i in
let x = String.get_int64_be buf i in
let x = Z.of_int64_unsigned Int64.(shift_right_logical x 8) in
loop Z.(x + acc lsl 56) (i + 7) (b - 56)
| b when b >= 32 ->
let x = string_get_int32_be buf i in
let x = String.get_int32_be buf i in
let x = Z.of_int32_unsigned Int32.(shift_right_logical x 8) in
loop Z.(x + acc lsl 24) (i + 3) (b - 24)
| b when b >= 16 ->
let x = Z.of_int (string_get_uint16_be buf i) in
let x = Z.of_int (String.get_uint16_be buf i) in
loop Z.(x + acc lsl 16) (i + 2) (b - 16)
| b when b >= 8 ->
let x = Z.of_int (string_get_uint8 buf i) in
let x = Z.of_int (String.get_uint8 buf i) in
loop Z.(x + acc lsl 8 ) (i + 1) (b - 8 )
| b when b > 0 ->
let x = string_get_uint8 buf i and b' = 8 - b in
let x = String.get_uint8 buf i and b' = 8 - b in
Z.(of_int x asr b' + acc lsl b)
| _ -> acc in
loop Z.zero 0 @@ match bits with
Expand Down
11 changes: 2 additions & 9 deletions src/cipher_block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ module Counters = struct
module C64be = struct
type ctr = int64
let size = 8
(* Until OCaml 4.13 is lower bound*)
let of_octets cs = Bytes.get_int64_be (Bytes.unsafe_of_string cs) 0
let of_octets cs = String.get_int64_be cs 0
let add = Int64.add
let unsafe_count_into t buf ~blocks =
let tmp = Bytes.create 8 in
Expand Down Expand Up @@ -277,16 +276,10 @@ module Modes = struct
Bytes.set_int64_be cs 8 b;
Bytes.unsafe_to_string cs

(* OCaml 4.13 *)
let string_get_int64 s idx =
Bytes.get_int64_be (Bytes.unsafe_of_string s) idx
let string_get_int32 s idx =
Bytes.get_int32_be (Bytes.unsafe_of_string s) idx

let counter ~hkey nonce = match String.length nonce with
| 0 -> invalid_arg "GCM: invalid nonce of length 0"
| 12 ->
let (w1, w2) = string_get_int64 nonce 0, string_get_int32 nonce 8 in
let (w1, w2) = String.get_int64_be nonce 0, String.get_int32_be nonce 8 in
(w1, Int64.(shift_left (of_int32 w2) 32 |> add 1L))
| _ ->
CTR.ctr_of_octets @@
Expand Down
4 changes: 2 additions & 2 deletions src/cipher_stream.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module ARC4 = struct
let rec loop j = function
| 256 -> ()
| i ->
let x = string_get_uint8 buf (i mod len) in
let x = String.get_uint8 buf (i mod len) in
let si = s.(i) in
let j = (j + si + x) land 0xff in
let sj = s.(j) in
Expand All @@ -43,7 +43,7 @@ module ARC4 = struct
let sj = s.(j) in
s.(i) <- sj ; s.(j) <- si ;
let k = s.((si + sj) land 0xff) in
Bytes.set_uint8 res n (k lxor string_get_uint8 buf n);
Bytes.set_uint8 res n (k lxor String.get_uint8 buf n);
mix i j (succ n)
in
let key' = mix i j 0 in
Expand Down
4 changes: 0 additions & 4 deletions src/uncommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,3 @@ let xor a b =
let b' = Bytes.of_string b in
xor_into a ~src_off:0 b' ~dst_off:0 (Bytes.length b');
Bytes.unsafe_to_string b'

(* revise once OCaml 4.13 is the lower bound *)
let string_get_uint8 buf idx =
Bytes.get_uint8 (Bytes.unsafe_of_string buf) idx
Loading

0 comments on commit 6dc789a

Please sign in to comment.