mirleft / ocaml-nocrypto

OCaml cryptographic library
ISC License
111 stars 53 forks source link

CCM mode: decrypting an empty ciphertext fails #168

Open emillon opened 5 years ago

emillon commented 5 years ago

Hi,

It looks like CCM decryption does not correctly handle the case when the ciphertext is empty. This happens when there is AAD and when there is not.

In the following example, I would expect Some Cstruct.empty to be returned:

let () =
  let key =
    Cstruct.of_hex "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f"
    |> Nocrypto.Cipher_block.AES.CCM.of_secret ~maclen:16
  in
  let nonce = Cstruct.of_hex "00 01 02 03 04 05 06 07" in
  let adata = Cstruct.of_string "hello" in
  let recovered =
    Cstruct.empty
    |> Nocrypto.Cipher_block.AES.CCM.encrypt ~key ~nonce ~adata
    |> Nocrypto.Cipher_block.AES.CCM.decrypt ~key ~nonce ~adata
  in
  match recovered with
  | Some cs -> Cstruct.hexdump cs
  | None -> print_endline "None"

Output:

None

Thanks!