···28282929exception Message_too_long
30303131-let string_get_uint8 buf idx =
3232- (* TODO: use String.get_uint8 when mirage-crypto-ec requires OCaml >= 4.13 *)
3333- Bytes.get_uint8 (Bytes.unsafe_of_string buf) idx
3434-3531let bit_at buf i =
3632 let byte_num = i / 8 in
3733 let bit_num = i mod 8 in
3838- let byte = string_get_uint8 buf byte_num in
3434+ let byte = String.get_uint8 buf byte_num in
3935 byte land (1 lsl bit_num) <> 0
40364137module type Dh = sig
···320316 if compress then
321317 let out = Bytes.create (P.byte_length + 1) in
322318 let ident =
323323- 2 + (string_get_uint8 buf ((P.byte_length * 2) - 1)) land 1
319319+ 2 + (String.get_uint8 buf ((P.byte_length * 2) - 1)) land 1
324320 in
325321 Bytes.unsafe_blit_string buf 1 out 1 P.byte_length;
326322 Bytes.set_uint8 out 0 ident;
···389385 let y' = Fe.from_montgomery y' in
390386 let y_struct2 = Fe.to_octets y' in (* number must not be in montgomery domain*)
391387 let y_struct2 = rev_string y_struct2 in
392392- let ident = string_get_uint8 pk 0 in
388388+ let ident = String.get_uint8 pk 0 in
393389 let signY =
394394- 2 + (string_get_uint8 y_struct (P.byte_length - 2)) land 1
390390+ 2 + (String.get_uint8 y_struct (P.byte_length - 2)) land 1
395391 in
396392 let res = if Int.equal signY ident then y_struct else y_struct2 in
397393 let out = Bytes.create ((P.byte_length * 2) + 1) in
···410406 let y = String.sub buf (1 + len) len in
411407 validate_finite_point ~x ~y
412408 in
413413- match string_get_uint8 buf 0 with
409409+ match String.get_uint8 buf 0 with
414410 | 0x00 when String.length buf = 1 ->
415411 Ok (at_infinity ())
416412 | 0x02 | 0x03 when String.length P.pident > 0 ->
···614610 let first_byte_ok () =
615611 match Param.first_byte_bits with
616612 | None -> true
617617- | Some m -> (string_get_uint8 msg 0) land (0xFF land (lnot m)) = 0
613613+ | Some m -> (String.get_uint8 msg 0) land (0xFF land (lnot m)) = 0
618614 in
619615 if l > bl || (l = bl && not (first_byte_ok ())) then
620616 raise Message_too_long
···197197let decrypt ?(crt_hardening=false) ?(mask=`Yes) ~key =
198198 reformat (priv_bits key) (decrypt_z ~crt_hardening ~mask ~key)
199199200200-(* OCaml 4.13 *)
201201-let string_get_uint8 buf idx =
202202- Bytes.get_uint8 (Bytes.unsafe_of_string buf) idx
203203-204200let bx00, bx01 = "\x00", "\x01"
205201206202module PKCS1 = struct
···214210 let rec go nonce i j =
215211 if i = n then Bytes.unsafe_to_string buf else
216212 if j = k then go Mirage_crypto_rng.(generate ?g k) i 0 else
217217- match string_get_uint8 nonce j with
213213+ match String.get_uint8 nonce j with
218214 | b when f b -> Bytes.set_uint8 buf i b ; go nonce (succ i) (succ j)
219215 | _ -> go nonce i (succ j) in
220216 go Mirage_crypto_rng.(generate ?g k) 0 0
···226222 let unpad ~mark ~is_pad buf =
227223 let f = not &. is_pad in
228224 let i = ct_find_uint8 ~default:2 ~off:2 ~f buf in
229229- let c1 = string_get_uint8 buf 0 = 0x00
230230- and c2 = string_get_uint8 buf 1 = mark
231231- and c3 = string_get_uint8 buf i = 0x00
225225+ let c1 = String.get_uint8 buf 0 = 0x00
226226+ and c2 = String.get_uint8 buf 1 = mark
227227+ and c3 = String.get_uint8 buf i = 0x00
232228 and c4 = min_pad <= i - 2 in
233229 if c1 && c2 && c3 && c4 then
234230 Some (String.sub buf (i + 1) (String.length buf - i - 1))
···264260 let decrypt ?(crt_hardening = false) ?mask ~key msg =
265261 unpadded unpad_02 (decrypt ~crt_hardening ?mask ~key) (priv_bits key) msg
266262267267- (* OCaml 4.13 contains starts_with *)
268268- let is_prefix asn msg =
269269- String.length msg >= String.length asn &&
270270- String.equal asn (String.sub msg 0 (String.length asn))
271271-272263 let asn_of_hash, detect =
273264 let map = [
274265 `MD5, "\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10" ;
···280271 ]
281272 in
282273 (fun h -> List.assoc h map),
283283- (fun buf -> List.find_opt (fun (_, d) -> is_prefix d buf) map)
274274+ (fun buf -> List.find_opt (fun (_, d) -> String.starts_with ~prefix:d buf) map)
284275285276 let sign ?(crt_hardening = true) ?mask ~hash ~key msg =
286277 let module H = (val Digestif.module_of_hash' (hash :> Digestif.hash')) in
···353344 let db = Bytes.unsafe_to_string (MGF.mask ~seed:(Bytes.unsafe_to_string (MGF.mask ~seed:mdb ms)) mdb) in
354345 let i = ct_find_uint8 ~default:0 ~off:hlen ~f:((<>) 0x00) db in
355346 let c1 = Eqaf.equal (String.sub db 0 hlen) H.(digest_string label |> to_raw_string)
356356- and c2 = string_get_uint8 b0 0 = 0x00
357357- and c3 = string_get_uint8 db i = 0x01 in
347347+ and c2 = String.get_uint8 b0 0 = 0x00
348348+ and c3 = String.get_uint8 db i = 0x01 in
358349 if c1 && c2 && c3 then Some (String.sub db (i + 1) (String.length db - i - 1)) else None
359350360351 let encrypt ?g ?label ~key msg =
···402393 let emsa_pss_verify slen emlen em msg =
403394 let mdb = String.sub em 0 (String.length em - hlen - 1)
404395 and h = String.sub em (String.length em - hlen - 1) hlen
405405- and bxx = string_get_uint8 em (String.length em - 1)
396396+ and bxx = String.get_uint8 em (String.length em - 1)
406397 in
407398 let db = MGF.mask ~seed:h mdb in
408399 Bytes.set_uint8 db 0 (Bytes.get_uint8 db 0 land b0mask emlen) ;
···410401 let salt = String.sub db (String.length db - slen) slen in
411402 let h' = digest ~salt:salt msg
412403 and i = ct_find_uint8 ~default:0 ~f:((<>) 0x00) db in
413413- let c1 = lnot (b0mask emlen) land string_get_uint8 mdb 0 = 0x00
404404+ let c1 = lnot (b0mask emlen) land String.get_uint8 mdb 0 = 0x00
414405 and c2 = i = String.length em - hlen - slen - 2
415415- and c3 = string_get_uint8 db i = 0x01
406406+ and c3 = String.get_uint8 db i = 0x01
416407 and c4 = bxx = 0xbc
417408 and c5 = Eqaf.equal h h' in
418409 c1 && c2 && c3 && c4 && c5
+5-18
pk/z_extra.ml
···2233let bit_bound z = Z.size z * 64
4455-(* revise once OCaml 4.13 is the lower bound *)
66-let string_get_int64_be buf idx =
77- Bytes.get_int64_be (Bytes.unsafe_of_string buf) idx
88-99-let string_get_int32_be buf idx =
1010- Bytes.get_int32_be (Bytes.unsafe_of_string buf) idx
1111-1212-let string_get_uint16_be buf idx =
1313- Bytes.get_uint16_be (Bytes.unsafe_of_string buf) idx
1414-1515-let string_get_uint8 buf idx =
1616- Bytes.get_uint8 (Bytes.unsafe_of_string buf) idx
1717-185let of_octets_be ?bits buf =
196 let rec loop acc i = function
207 | b when b >= 64 ->
2121- let x = string_get_int64_be buf i in
88+ let x = String.get_int64_be buf i in
229 let x = Z.of_int64_unsigned Int64.(shift_right_logical x 8) in
2310 loop Z.(x + acc lsl 56) (i + 7) (b - 56)
2411 | b when b >= 32 ->
2525- let x = string_get_int32_be buf i in
1212+ let x = String.get_int32_be buf i in
2613 let x = Z.of_int32_unsigned Int32.(shift_right_logical x 8) in
2714 loop Z.(x + acc lsl 24) (i + 3) (b - 24)
2815 | b when b >= 16 ->
2929- let x = Z.of_int (string_get_uint16_be buf i) in
1616+ let x = Z.of_int (String.get_uint16_be buf i) in
3017 loop Z.(x + acc lsl 16) (i + 2) (b - 16)
3118 | b when b >= 8 ->
3232- let x = Z.of_int (string_get_uint8 buf i) in
1919+ let x = Z.of_int (String.get_uint8 buf i) in
3320 loop Z.(x + acc lsl 8 ) (i + 1) (b - 8 )
3421 | b when b > 0 ->
3535- let x = string_get_uint8 buf i and b' = 8 - b in
2222+ let x = String.get_uint8 buf i and b' = 8 - b in
3623 Z.(of_int x asr b' + acc lsl b)
3724 | _ -> acc in
3825 loop Z.zero 0 @@ match bits with
+2-9
src/cipher_block.ml
···8989 module C64be = struct
9090 type ctr = int64
9191 let size = 8
9292- (* Until OCaml 4.13 is lower bound*)
9393- let of_octets cs = Bytes.get_int64_be (Bytes.unsafe_of_string cs) 0
9292+ let of_octets cs = String.get_int64_be cs 0
9493 let add = Int64.add
9594 let unsafe_count_into t buf ~blocks =
9695 let tmp = Bytes.create 8 in
···277276 Bytes.set_int64_be cs 8 b;
278277 Bytes.unsafe_to_string cs
279278280280- (* OCaml 4.13 *)
281281- let string_get_int64 s idx =
282282- Bytes.get_int64_be (Bytes.unsafe_of_string s) idx
283283- let string_get_int32 s idx =
284284- Bytes.get_int32_be (Bytes.unsafe_of_string s) idx
285285-286279 let counter ~hkey nonce = match String.length nonce with
287280 | 0 -> invalid_arg "GCM: invalid nonce of length 0"
288281 | 12 ->
289289- let (w1, w2) = string_get_int64 nonce 0, string_get_int32 nonce 8 in
282282+ let (w1, w2) = String.get_int64_be nonce 0, String.get_int32_be nonce 8 in
290283 (w1, Int64.(shift_left (of_int32 w2) 32 |> add 1L))
291284 | _ ->
292285 CTR.ctr_of_octets @@
+2-2
src/cipher_stream.ml
···2121 let rec loop j = function
2222 | 256 -> ()
2323 | i ->
2424- let x = string_get_uint8 buf (i mod len) in
2424+ let x = String.get_uint8 buf (i mod len) in
2525 let si = s.(i) in
2626 let j = (j + si + x) land 0xff in
2727 let sj = s.(j) in
···4343 let sj = s.(j) in
4444 s.(i) <- sj ; s.(j) <- si ;
4545 let k = s.((si + sj) land 0xff) in
4646- Bytes.set_uint8 res n (k lxor string_get_uint8 buf n);
4646+ Bytes.set_uint8 res n (k lxor String.get_uint8 buf n);
4747 mix i j (succ n)
4848 in
4949 let key' = mix i j 0 in
-4
src/uncommon.ml
···2525 let b' = Bytes.of_string b in
2626 xor_into a ~src_off:0 b' ~dst_off:0 (Bytes.length b');
2727 Bytes.unsafe_to_string b'
2828-2929-(* revise once OCaml 4.13 is the lower bound *)
3030-let string_get_uint8 buf idx =
3131- Bytes.get_uint8 (Bytes.unsafe_of_string buf) idx
+5-13
tests/test_ec_wycheproof.ml
···4455let ( let* ) = Result.bind
6677-let concat_map f l =
88- (* adapt once OCaml 4.10 is lower bound *)
99- List.map f l |> List.concat
1010-1111-let string_get_uint8 d off =
1212- (* adapt once OCaml 4.13 is lower bound *)
1313- Bytes.get_uint8 (Bytes.unsafe_of_string d) off
1414-157let hex = Alcotest.testable Wycheproof.pp_hex Wycheproof.equal_hex
168179module Asn = struct
···155147 let groups : ecdh_test_group list =
156148 List.map ecdh_test_group_exn data.testGroups
157149 in
158158- concat_map (fun (group : ecdh_test_group) ->
159159- concat_map (to_ecdh_tests group.curve) group.tests)
150150+ List.concat_map (fun (group : ecdh_test_group) ->
151151+ List.concat_map (to_ecdh_tests group.curve) group.tests)
160152 groups
161153162154let make_ecdsa_test curve key hash (tst : dsa_test) =
···219211 let groups : ecdsa_test_group list =
220212 List.map ecdsa_test_group_exn data.testGroups
221213 in
222222- concat_map to_ecdsa_tests groups
214214+ List.concat_map to_ecdsa_tests groups
223215224216let to_x25519_test (x : ecdh_test) =
225217 let name = Printf.sprintf "%d - %s" x.tcId x.comment
···262254 let groups : ecdh_test_group list =
263255 List.map ecdh_test_group_exn data.testGroups
264256 in
265265- concat_map (fun (group : ecdh_test_group) ->
257257+ List.concat_map (fun (group : ecdh_test_group) ->
266258 List.map to_x25519_test group.tests)
267259 groups
268260···297289 let groups : eddsa_test_group list =
298290 List.map eddsa_test_group_exn data.testGroups
299291 in
300300- concat_map (fun (group : eddsa_test_group) ->
292292+ List.concat_map (fun (group : eddsa_test_group) ->
301293 let keys = to_ed25519_keys group.key in
302294 List.map (to_ed25519_test keys) group.tests)
303295 groups