Skip to content

Commit

Permalink
mirage-crypto: CCM bugfix 32 bit with long adata (#207)
Browse files Browse the repository at this point in the history
* Add 32 bit ccm test case
* Fix CCM on 32 bit architecture
  • Loading branch information
reynir authored Feb 28, 2024
1 parent 3ebc0e3 commit 701e983
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ccm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ let gen_adata a =
let buf = Cstruct.create 2 in
Cstruct.BE.set_uint16 buf 0 x ;
buf
| x when x < (1 lsl 32) ->
| x when Sys.int_size < 32 || x < (1 lsl 32) ->
let buf = Cstruct.create 4 in
Cstruct.BE.set_uint32 buf 0 (Int32.of_int x) ;
Cs.of_bytes [0xff ; 0xfe] <+> buf
| x ->
| x ->
let buf = Cstruct.create 8 in
Cstruct.BE.set_uint64 buf 0 (Int64.of_int x) ;
Cs.of_bytes [0xff ; 0xff] <+> buf
Expand Down
11 changes: 11 additions & 0 deletions tests/test_cipher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ let ccm_regressions =
match authenticate_decrypt ~key ~nonce ~adata cipher with
| Some x -> assert_cs_equal ~msg:"CCM decrypt of empty message" p x
| None -> assert_failure "decryption broken"
and long_adata _ =
let key = of_secret (vx "000102030405060708090a0b0c0d0e0f")
and nonce = vx "0001020304050607"
and plaintext = Cstruct.of_string "hello"
(* [adata] is greater than [1 lsl 16 - 1 lsl 8] *)
and adata = Cstruct.create 65280
and expected = vx "6592169e946f98973bc06d080f7c9dbb493a536f8a"
in
let cipher = authenticate_encrypt ~adata ~key ~nonce plaintext in
assert_cs_equal ~msg:"CCM encrypt of >=65280 adata" expected cipher
in
[
test_case no_vs_empty_ad ;
Expand All @@ -389,6 +399,7 @@ let ccm_regressions =
test_case short_nonce_enc3 ;
test_case long_nonce_enc ;
test_case enc_dec_empty_message ;
test_case long_adata ;
]

let gcm_regressions =
Expand Down

0 comments on commit 701e983

Please sign in to comment.