upstream: https://github.com/mirage/mirage-crypto

feat(crypto): add E605 test files for 13 private crypto modules

Tests for src/ modules (ccm, chacha20, cipher_block, cipher_stream,
native, poly1305), pk/ (z_extra), rng/ (fortuna, hmac_drbg, rng),
and rng/unix/ (crypto_rng_unix, getentropy, urandom).

All tests use public APIs since modules are private. Includes NIST
and RFC test vectors where applicable.

+552
+3
test/dune
··· 1 + (test 2 + (name test) 3 + (libraries alcotest crypto crypto-rng crypto-rng.unix ohex))
+3
test/pk/dune
··· 1 + (test 2 + (name test) 3 + (libraries alcotest crypto-pk crypto-rng crypto-rng.unix ohex))
+3
test/pk/test.ml
··· 1 + let () = 2 + Crypto_rng_unix.use_default (); 3 + Alcotest.run "crypto-pk" [ Test_z_extra.suite ]
+51
test/pk/test_z_extra.ml
··· 1 + let test_roundtrip () = 2 + for _ = 1 to 100 do 3 + let z = Crypto_pk.Z_extra.gen Z.(of_int 1_000_000) in 4 + let s = Crypto_pk.Z_extra.to_octets_be z in 5 + let z' = Crypto_pk.Z_extra.of_octets_be s in 6 + Alcotest.(check (of_pp Z.pp_print)) "roundtrip" z z' 7 + done 8 + 9 + let test_padded_roundtrip () = 10 + for _ = 1 to 100 do 11 + let z = Crypto_pk.Z_extra.gen Z.(of_int 1_000_000) in 12 + let s = Crypto_pk.Z_extra.to_octets_be ~size:16 z in 13 + Alcotest.(check int) "padded size" 16 (String.length s); 14 + let z' = Crypto_pk.Z_extra.of_octets_be s in 15 + Alcotest.(check (of_pp Z.pp_print)) "padded roundtrip" z z' 16 + done 17 + 18 + let test_zero_encoding () = 19 + let s = Crypto_pk.Z_extra.to_octets_be Z.zero in 20 + let z = Crypto_pk.Z_extra.of_octets_be s in 21 + Alcotest.(check (of_pp Z.pp_print)) "zero roundtrip" Z.zero z 22 + 23 + let test_gen_range () = 24 + let lo = Z.of_int 10 and hi = Z.of_int 100 in 25 + for _ = 1 to 200 do 26 + let x = Crypto_pk.Z_extra.gen_r lo hi in 27 + if Z.compare x lo < 0 || Z.compare x hi >= 0 then 28 + Alcotest.failf "gen_r out of range: %s" (Z.to_string x) 29 + done 30 + 31 + let test_large_values () = 32 + let big = Z.pow (Z.of_int 2) 128 in 33 + let s = Crypto_pk.Z_extra.to_octets_be big in 34 + let big' = Crypto_pk.Z_extra.of_octets_be s in 35 + Alcotest.(check (of_pp Z.pp_print)) "large roundtrip" big big'; 36 + let sized = Crypto_pk.Z_extra.to_octets_be ~size:32 big in 37 + Alcotest.(check int) "large padded" 32 (String.length sized); 38 + let big'' = Crypto_pk.Z_extra.of_octets_be sized in 39 + Alcotest.(check (of_pp Z.pp_print)) "large padded roundtrip" big big'' 40 + 41 + let suite = 42 + ( "z_extra", 43 + [ 44 + Alcotest.test_case "roundtrip" `Quick (fun () -> test_roundtrip ()); 45 + Alcotest.test_case "padded roundtrip" `Quick (fun () -> 46 + test_padded_roundtrip ()); 47 + Alcotest.test_case "zero encoding" `Quick (fun () -> 48 + test_zero_encoding ()); 49 + Alcotest.test_case "gen_r range" `Quick (fun () -> test_gen_range ()); 50 + Alcotest.test_case "large values" `Quick (fun () -> test_large_values ()); 51 + ] )
+1
test/pk/test_z_extra.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+3
test/rng/dune
··· 1 + (test 2 + (name test) 3 + (libraries alcotest crypto-rng crypto-rng.unix digestif))
+4
test/rng/test.ml
··· 1 + let () = 2 + Crypto_rng_unix.use_default (); 3 + Alcotest.run "crypto-rng" 4 + [ Test_fortuna.suite; Test_hmac_drbg.suite; Test_rng.suite ]
+31
test/rng/test_fortuna.ml
··· 1 + let fortuna_generate g n = 2 + let buf = Bytes.create n in 3 + Crypto_rng.Fortuna.generate_into ~g buf ~off:0 n; 4 + Bytes.to_string buf 5 + [@@alert "-unsafe"] 6 + 7 + let test_block () = 8 + Alcotest.(check bool) "block positive" true (Crypto_rng.Fortuna.block > 0) 9 + 10 + let test_seeded_generates () = 11 + let seed = String.make 32 '\x42' in 12 + let g = Crypto_rng.Fortuna.v () in 13 + Crypto_rng.Fortuna.reseed ~g seed; 14 + let data = fortuna_generate g 32 in 15 + Alcotest.(check int) "length" 32 (String.length data) 16 + 17 + let test_different_output () = 18 + let seed = String.make 32 '\x01' in 19 + let g = Crypto_rng.Fortuna.v () in 20 + Crypto_rng.Fortuna.reseed ~g seed; 21 + let a = fortuna_generate g 16 in 22 + let b = fortuna_generate g 16 in 23 + Alcotest.(check bool) "different" true (not (String.equal a b)) 24 + 25 + let suite = 26 + ( "fortuna", 27 + [ 28 + Alcotest.test_case "block" `Quick test_block; 29 + Alcotest.test_case "seeded generates" `Quick test_seeded_generates; 30 + Alcotest.test_case "different output" `Quick test_different_output; 31 + ] )
+1
test/rng/test_fortuna.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+42
test/rng/test_hmac_drbg.ml
··· 1 + module Drbg = Crypto_rng.Hmac_drbg (Digestif.SHA256) 2 + 3 + let drbg_generate g n = 4 + let buf = Bytes.create n in 5 + Drbg.generate_into ~g buf ~off:0 n; 6 + Bytes.to_string buf 7 + [@@alert "-unsafe"] 8 + 9 + let test_deterministic () = 10 + let entropy = String.make 48 '\xab' in 11 + let g1 = Drbg.v () in 12 + Drbg.reseed ~g:g1 entropy; 13 + let a = drbg_generate g1 32 in 14 + let g2 = Drbg.v () in 15 + Drbg.reseed ~g:g2 entropy; 16 + let b = drbg_generate g2 32 in 17 + Alcotest.(check string) "deterministic" a b 18 + 19 + let test_different_seeds () = 20 + let g1 = Drbg.v () in 21 + Drbg.reseed ~g:g1 (String.make 48 '\x01'); 22 + let a = drbg_generate g1 32 in 23 + let g2 = Drbg.v () in 24 + Drbg.reseed ~g:g2 (String.make 48 '\x02'); 25 + let b = drbg_generate g2 32 in 26 + Alcotest.(check bool) "different seeds" true (not (String.equal a b)) 27 + 28 + let test_generate_lengths () = 29 + let g = Drbg.v () in 30 + Drbg.reseed ~g (String.make 48 '\xcc'); 31 + let d16 = drbg_generate g 16 in 32 + let d64 = drbg_generate g 64 in 33 + Alcotest.(check int) "16 bytes" 16 (String.length d16); 34 + Alcotest.(check int) "64 bytes" 64 (String.length d64) 35 + 36 + let suite = 37 + ( "hmac_drbg", 38 + [ 39 + Alcotest.test_case "deterministic" `Quick test_deterministic; 40 + Alcotest.test_case "different seeds" `Quick test_different_seeds; 41 + Alcotest.test_case "generate lengths" `Quick test_generate_lengths; 42 + ] )
+1
test/rng/test_hmac_drbg.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+25
test/rng/test_rng.ml
··· 1 + let test_generate () = 2 + let data = Crypto_rng.generate 32 in 3 + Alcotest.(check int) "length" 32 (String.length data) 4 + 5 + let test_generate_into () = 6 + let buf = Bytes.create 16 in 7 + Crypto_rng.generate_into buf ~off:0 16; 8 + let s = Bytes.to_string buf in 9 + Alcotest.(check int) "length" 16 (String.length s); 10 + Alcotest.(check bool) 11 + "non-zero" true 12 + (not (String.equal s (String.make 16 '\000'))) 13 + 14 + let test_different () = 15 + let a = Crypto_rng.generate 16 in 16 + let b = Crypto_rng.generate 16 in 17 + Alcotest.(check bool) "different" true (not (String.equal a b)) 18 + 19 + let suite = 20 + ( "rng", 21 + [ 22 + Alcotest.test_case "generate" `Quick test_generate; 23 + Alcotest.test_case "generate_into" `Quick test_generate_into; 24 + Alcotest.test_case "different" `Quick test_different; 25 + ] )
+1
test/rng/test_rng.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+3
test/rng/unix/dune
··· 1 + (test 2 + (name test) 3 + (libraries alcotest crypto-rng crypto-rng.unix))
+4
test/rng/unix/test.ml
··· 1 + let () = 2 + Crypto_rng_unix.use_default (); 3 + Alcotest.run "crypto-rng-unix" 4 + [ Test_crypto_rng_unix.suite; Test_getentropy.suite; Test_urandom.suite ]
+22
test/rng/unix/test_crypto_rng_unix.ml
··· 1 + let test_getrandom_length () = 2 + let data = Crypto_rng_unix.getrandom 32 in 3 + Alcotest.(check int) "length" 32 (String.length data) 4 + 5 + let test_getrandom_nonzero () = 6 + let data = Crypto_rng_unix.getrandom 64 in 7 + Alcotest.(check bool) 8 + "non-zero" true 9 + (not (String.equal data (String.make 64 '\000'))) 10 + 11 + let test_getrandom_different () = 12 + let a = Crypto_rng_unix.getrandom 16 in 13 + let b = Crypto_rng_unix.getrandom 16 in 14 + Alcotest.(check bool) "different" true (not (String.equal a b)) 15 + 16 + let suite = 17 + ( "crypto_rng_unix", 18 + [ 19 + Alcotest.test_case "getrandom length" `Quick test_getrandom_length; 20 + Alcotest.test_case "getrandom nonzero" `Quick test_getrandom_nonzero; 21 + Alcotest.test_case "getrandom different" `Quick test_getrandom_different; 22 + ] )
+1
test/rng/unix/test_crypto_rng_unix.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+17
test/rng/unix/test_getentropy.ml
··· 1 + let test_provides_data () = 2 + let data = Crypto_rng.generate 16 in 3 + Alcotest.(check int) "length" 16 (String.length data) 4 + 5 + let test_entropy_varies () = 6 + let results = List.init 5 (fun _ -> Crypto_rng_unix.getrandom 8) in 7 + let unique = List.sort_uniq String.compare results in 8 + Alcotest.(check bool) "distinct values" true (List.length unique > 1) 9 + 10 + let suite = 11 + ( "getentropy", 12 + [ 13 + Alcotest.test_case "provides data" `Quick (fun () -> 14 + test_provides_data ()); 15 + Alcotest.test_case "entropy varies" `Quick (fun () -> 16 + test_entropy_varies ()); 17 + ] )
+1
test/rng/unix/test_getentropy.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+18
test/rng/unix/test_urandom.ml
··· 1 + let test_provides_data () = 2 + let data = Crypto_rng_unix.getrandom 64 in 3 + Alcotest.(check int) "length" 64 (String.length data) 4 + 5 + let test_large_request () = 6 + let data = Crypto_rng_unix.getrandom 4096 in 7 + Alcotest.(check int) "large length" 4096 (String.length data); 8 + if String.equal data (String.make 4096 '\000') then 9 + Alcotest.fail "large request all zeros" 10 + 11 + let suite = 12 + ( "urandom", 13 + [ 14 + Alcotest.test_case "provides data" `Quick (fun () -> 15 + test_provides_data ()); 16 + Alcotest.test_case "large request" `Quick (fun () -> 17 + test_large_request ()); 18 + ] )
+1
test/rng/unix/test_urandom.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+11
test/test.ml
··· 1 + let () = 2 + Crypto_rng_unix.use_default (); 3 + Alcotest.run "crypto-test" 4 + [ 5 + Test_native.suite; 6 + Test_poly1305.suite; 7 + Test_ccm.suite; 8 + Test_chacha20.suite; 9 + Test_cipher_block.suite; 10 + Test_cipher_stream.suite; 11 + ]
+50
test/test_ccm.ml
··· 1 + let vx = Ohex.decode 2 + 3 + let test_encrypt_decrypt () = 4 + let key = vx "404142434445464748494a4b4c4d4e4f" in 5 + let nonce = vx "10111213141516171819202122" in 6 + let adata = vx "0001020304050607" in 7 + let plaintext = vx "20212223" in 8 + let k = Crypto.AES.CCM16.of_secret key in 9 + let ct = 10 + Crypto.AES.CCM16.authenticate_encrypt ~key:k ~nonce ~adata plaintext 11 + in 12 + Alcotest.(check int) "ct length" (4 + 16) (String.length ct); 13 + match Crypto.AES.CCM16.authenticate_decrypt ~key:k ~nonce ~adata ct with 14 + | Some pt -> Alcotest.(check string) "decrypt" plaintext pt 15 + | None -> Alcotest.fail "CCM16 decrypt failed" 16 + 17 + let test_roundtrip () = 18 + let key = Crypto_rng.generate 16 in 19 + let nonce = Crypto_rng.generate 12 in 20 + let adata = "additional data" in 21 + let plaintext = "secret message for CCM test" in 22 + let k = Crypto.AES.CCM16.of_secret key in 23 + let ct = 24 + Crypto.AES.CCM16.authenticate_encrypt ~key:k ~nonce ~adata plaintext 25 + in 26 + match Crypto.AES.CCM16.authenticate_decrypt ~key:k ~nonce ~adata ct with 27 + | Some pt -> Alcotest.(check string) "roundtrip" plaintext pt 28 + | None -> Alcotest.fail "CCM decrypt failed" 29 + 30 + let test_tamper () = 31 + let key = Crypto_rng.generate 16 in 32 + let nonce = Crypto_rng.generate 12 in 33 + let adata = "aad" in 34 + let k = Crypto.AES.CCM16.of_secret key in 35 + let ct = Crypto.AES.CCM16.authenticate_encrypt ~key:k ~nonce ~adata "hello" in 36 + let bad = Bytes.of_string ct in 37 + Bytes.set bad 0 (Char.chr (Char.code (Bytes.get bad 0) lxor 1)); 38 + let result = 39 + Crypto.AES.CCM16.authenticate_decrypt ~key:k ~nonce ~adata 40 + (Bytes.to_string bad) 41 + in 42 + Alcotest.(check bool) "tamper rejected" true (Option.is_none result) 43 + 44 + let suite = 45 + ( "ccm", 46 + [ 47 + Alcotest.test_case "encrypt/decrypt" `Quick test_encrypt_decrypt; 48 + Alcotest.test_case "roundtrip" `Quick test_roundtrip; 49 + Alcotest.test_case "tamper" `Quick test_tamper; 50 + ] )
+1
test/test_ccm.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+71
test/test_chacha20.ml
··· 1 + let vx = Ohex.decode 2 + 3 + (* RFC 8439 Section 2.4.2 *) 4 + let test_rfc8439_stream () = 5 + let key = 6 + vx "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" 7 + in 8 + let nonce = vx "000000000000004a00000000" in 9 + let plaintext = 10 + "Ladies and Gentlemen of the class of '99: If I could offer you only one \ 11 + tip for the future, sunscreen would be it." 12 + in 13 + let expected = 14 + vx 15 + "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f593dabcd62b3571639d624e65152ab8f530c359f0861d807ca0dbf500d6a6156a38e088a22b65e52bc514d16ccf806818ce91ab77937365af90bbf74a35be6b40b8eedf2785e42874d" 16 + in 17 + let k = Crypto.Chacha20.of_secret key in 18 + let ct = Crypto.Chacha20.crypt ~key:k ~nonce ~ctr:1L plaintext in 19 + Alcotest.(check string) "rfc8439" expected ct 20 + 21 + let test_aead_roundtrip () = 22 + let key = Crypto_rng.generate 32 in 23 + let nonce = Crypto_rng.generate 12 in 24 + let adata = "associated data" in 25 + let plaintext = "AEAD test message" in 26 + let k = Crypto.Chacha20.of_secret key in 27 + let ct = 28 + Crypto.Chacha20.authenticate_encrypt ~key:k ~nonce ~adata plaintext 29 + in 30 + match Crypto.Chacha20.authenticate_decrypt ~key:k ~nonce ~adata ct with 31 + | Some pt -> Alcotest.(check string) "aead roundtrip" plaintext pt 32 + | None -> Alcotest.fail "AEAD decrypt failed" 33 + 34 + let test_aead_tamper () = 35 + let key = Crypto_rng.generate 32 in 36 + let nonce = Crypto_rng.generate 12 in 37 + let k = Crypto.Chacha20.of_secret key in 38 + let ct = 39 + Crypto.Chacha20.authenticate_encrypt ~key:k ~nonce ~adata:"" "data" 40 + in 41 + let bad = Bytes.of_string ct in 42 + Bytes.set bad 0 (Char.chr (Char.code (Bytes.get bad 0) lxor 1)); 43 + let result = 44 + Crypto.Chacha20.authenticate_decrypt ~key:k ~nonce ~adata:"" 45 + (Bytes.to_string bad) 46 + in 47 + Alcotest.(check bool) "tamper rejected" true (Option.is_none result) 48 + 49 + let test_aead_tag_api () = 50 + let key = Crypto_rng.generate 32 in 51 + let nonce = Crypto_rng.generate 12 in 52 + let adata = "tag api test" in 53 + let plaintext = "message" in 54 + let k = Crypto.Chacha20.of_secret key in 55 + let ct, tag = 56 + Crypto.Chacha20.authenticate_encrypt_tag ~key:k ~nonce ~adata plaintext 57 + in 58 + match 59 + Crypto.Chacha20.authenticate_decrypt_tag ~key:k ~nonce ~adata ~tag ct 60 + with 61 + | Some pt -> Alcotest.(check string) "tag api" plaintext pt 62 + | None -> Alcotest.fail "tag API decrypt failed" 63 + 64 + let suite = 65 + ( "chacha20", 66 + [ 67 + Alcotest.test_case "RFC 8439 stream" `Quick test_rfc8439_stream; 68 + Alcotest.test_case "AEAD roundtrip" `Quick test_aead_roundtrip; 69 + Alcotest.test_case "AEAD tamper" `Quick test_aead_tamper; 70 + Alcotest.test_case "AEAD tag API" `Quick test_aead_tag_api; 71 + ] )
+1
test/test_chacha20.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+72
test/test_cipher_block.ml
··· 1 + let vx = Ohex.decode 2 + 3 + (* NIST SP 800-38A test vectors *) 4 + let nist_plaintext = 5 + vx 6 + "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710" 7 + 8 + let test_aes_ecb () = 9 + let key = Crypto.AES.ECB.of_secret (vx "2b7e151628aed2a6abf7158809cf4f3c") in 10 + let expected = 11 + vx 12 + "3ad77bb40d7a3660a89ecaf32466ef97f5d3d58503b9699de785895a96fdbaaf43b1cd7f598ece23881b00e3ed0306887b0c785e27e8ad3f8223207104725dd4" 13 + in 14 + let enc = Crypto.AES.ECB.encrypt ~key nist_plaintext in 15 + Alcotest.(check string) "ecb encrypt" expected enc; 16 + let dec = Crypto.AES.ECB.decrypt ~key enc in 17 + Alcotest.(check string) "ecb decrypt" nist_plaintext dec 18 + 19 + let test_aes_cbc () = 20 + let key = Crypto.AES.CBC.of_secret (vx "2b7e151628aed2a6abf7158809cf4f3c") in 21 + let iv = vx "000102030405060708090a0b0c0d0e0f" in 22 + let expected = 23 + vx 24 + "7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a7" 25 + in 26 + let enc = Crypto.AES.CBC.encrypt ~key ~iv nist_plaintext in 27 + Alcotest.(check string) "cbc encrypt" expected enc; 28 + let dec = Crypto.AES.CBC.decrypt ~key ~iv enc in 29 + Alcotest.(check string) "cbc decrypt" nist_plaintext dec 30 + 31 + let test_aes_ctr () = 32 + let key = Crypto.AES.CTR.of_secret (vx "2b7e151628aed2a6abf7158809cf4f3c") in 33 + let ctr = 34 + Crypto.AES.CTR.ctr_of_octets (vx "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff") 35 + in 36 + let expected = 37 + vx 38 + "874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee" 39 + in 40 + let enc = Crypto.AES.CTR.encrypt ~key ~ctr nist_plaintext in 41 + Alcotest.(check string) "ctr encrypt" expected enc; 42 + let dec = Crypto.AES.CTR.decrypt ~key ~ctr enc in 43 + Alcotest.(check string) "ctr decrypt" nist_plaintext dec 44 + 45 + let test_aes_gcm_roundtrip () = 46 + let key = Crypto_rng.generate 16 in 47 + let nonce = Crypto_rng.generate 12 in 48 + let adata = "gcm associated data" in 49 + let plaintext = "GCM test plaintext block" in 50 + let k = Crypto.AES.GCM.of_secret key in 51 + let ct = Crypto.AES.GCM.authenticate_encrypt ~key:k ~nonce ~adata plaintext in 52 + match Crypto.AES.GCM.authenticate_decrypt ~key:k ~nonce ~adata ct with 53 + | Some pt -> Alcotest.(check string) "gcm roundtrip" plaintext pt 54 + | None -> Alcotest.fail "GCM decrypt failed" 55 + 56 + let test_des_ecb_roundtrip () = 57 + let key = Crypto_rng.generate 24 in 58 + let k = Crypto.DES.ECB.of_secret key in 59 + let plaintext = String.make 8 'A' in 60 + let ct = Crypto.DES.ECB.encrypt ~key:k plaintext in 61 + let pt = Crypto.DES.ECB.decrypt ~key:k ct in 62 + Alcotest.(check string) "des ecb roundtrip" plaintext pt 63 + 64 + let suite = 65 + ( "cipher_block", 66 + [ 67 + Alcotest.test_case "AES ECB NIST" `Quick test_aes_ecb; 68 + Alcotest.test_case "AES CBC NIST" `Quick test_aes_cbc; 69 + Alcotest.test_case "AES CTR NIST" `Quick test_aes_ctr; 70 + Alcotest.test_case "AES GCM roundtrip" `Quick test_aes_gcm_roundtrip; 71 + Alcotest.test_case "DES ECB roundtrip" `Quick test_des_ecb_roundtrip; 72 + ] )
+1
test/test_cipher_block.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+38
test/test_cipher_stream.ml
··· 1 + let vx = Ohex.decode 2 + 3 + (* RFC 6229 — ARC4 with 40-bit key *) 4 + let test_arc4_rfc6229 () = 5 + let key = vx "0102030405" in 6 + let k = Crypto.ARC4.of_secret key in 7 + let plaintext = String.make 16 '\x00' in 8 + let result = Crypto.ARC4.encrypt ~key:k plaintext in 9 + let expected = vx "b2396305f03dc027ccc3524a0a1118a8" in 10 + Alcotest.(check string) "rfc6229" expected result.message 11 + 12 + let test_arc4_roundtrip () = 13 + let key = Crypto_rng.generate 16 in 14 + let plaintext = "ARC4 stream cipher roundtrip test message" in 15 + let k1 = Crypto.ARC4.of_secret key in 16 + let ct = Crypto.ARC4.encrypt ~key:k1 plaintext in 17 + let k2 = Crypto.ARC4.of_secret key in 18 + let pt = Crypto.ARC4.decrypt ~key:k2 ct.message in 19 + Alcotest.(check string) "roundtrip" plaintext pt.message 20 + 21 + let test_arc4_stateful () = 22 + let key = Crypto_rng.generate 16 in 23 + let msg1 = "first part" in 24 + let msg2 = "second part" in 25 + let k1 = Crypto.ARC4.of_secret key in 26 + let r1 = Crypto.ARC4.encrypt ~key:k1 msg1 in 27 + let r2 = Crypto.ARC4.encrypt ~key:r1.key msg2 in 28 + let k2 = Crypto.ARC4.of_secret key in 29 + let full = Crypto.ARC4.encrypt ~key:k2 (msg1 ^ msg2) in 30 + Alcotest.(check string) "stateful" full.message (r1.message ^ r2.message) 31 + 32 + let suite = 33 + ( "cipher_stream", 34 + [ 35 + Alcotest.test_case "ARC4 RFC 6229" `Quick test_arc4_rfc6229; 36 + Alcotest.test_case "ARC4 roundtrip" `Quick test_arc4_roundtrip; 37 + Alcotest.test_case "ARC4 stateful" `Quick test_arc4_stateful; 38 + ] )
+1
test/test_cipher_stream.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+23
test/test_native.ml
··· 1 + let test_accelerated () = 2 + let accel = Crypto.accelerated in 3 + Alcotest.(check bool) "list" true (List.length accel >= 0) 4 + 5 + let test_aes_block_size () = 6 + let open Crypto.AES.ECB in 7 + Alcotest.(check int) "block_size" 16 block_size 8 + 9 + let test_aes_key_sizes () = 10 + let open Crypto.AES.ECB in 11 + Alcotest.(check bool) "key_sizes non-empty" true (Array.length key_sizes > 0); 12 + Array.iter 13 + (fun ks -> 14 + Alcotest.(check bool) "valid key size" true (ks = 16 || ks = 24 || ks = 32)) 15 + key_sizes 16 + 17 + let suite = 18 + ( "native", 19 + [ 20 + Alcotest.test_case "accelerated" `Quick test_accelerated; 21 + Alcotest.test_case "AES block_size" `Quick test_aes_block_size; 22 + Alcotest.test_case "AES key_sizes" `Quick test_aes_key_sizes; 23 + ] )
+1
test/test_native.mli
··· 1 + val suite : string * unit Alcotest.test_case list
+45
test/test_poly1305.ml
··· 1 + let vx = Ohex.decode 2 + 3 + (* RFC 8439 Section 2.5.2 *) 4 + let test_rfc8439 () = 5 + let key = 6 + vx "85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b" 7 + in 8 + let msg = "Cryptographic Forum Research Group" in 9 + let expected = vx "a8061dc1305136c6c22b8baf0c0127a9" in 10 + let tag = Crypto.Poly1305.mac ~key msg in 11 + Alcotest.(check string) "rfc8439" expected tag 12 + 13 + let test_incremental () = 14 + let key = 15 + vx "85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b" 16 + in 17 + let msg = "Cryptographic Forum Research Group" in 18 + let st = Crypto.Poly1305.empty ~key in 19 + let st = Crypto.Poly1305.feed st (String.sub msg 0 10) in 20 + let st = 21 + Crypto.Poly1305.feed st (String.sub msg 10 (String.length msg - 10)) 22 + in 23 + let tag_inc = Crypto.Poly1305.get st in 24 + let tag_one = Crypto.Poly1305.mac ~key msg in 25 + Alcotest.(check string) "incremental" tag_one tag_inc 26 + 27 + let test_maci () = 28 + let key = 29 + vx "85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b" 30 + in 31 + let msg = "Cryptographic Forum Research Group" in 32 + let parts = 33 + [ String.sub msg 0 17; String.sub msg 17 (String.length msg - 17) ] 34 + in 35 + let tag_maci = Crypto.Poly1305.maci ~key (fun f -> List.iter f parts) in 36 + let tag_mac = Crypto.Poly1305.mac ~key msg in 37 + Alcotest.(check string) "maci" tag_mac tag_maci 38 + 39 + let suite = 40 + ( "poly1305", 41 + [ 42 + Alcotest.test_case "RFC 8439" `Quick test_rfc8439; 43 + Alcotest.test_case "incremental" `Quick test_incremental; 44 + Alcotest.test_case "maci" `Quick test_maci; 45 + ] )
+1
test/test_poly1305.mli
··· 1 + val suite : string * unit Alcotest.test_case list