dev(ciphers): make the libcrux implementation of chachapoly return an error instead of panicking when decryption fails. This makes tests decryptions possible.

This commit is contained in:
David Niehues
2025-03-14 10:47:07 +01:00
parent 2d2d109246
commit 6d25c13fd1

View File

@@ -1,3 +1,4 @@
use std::fmt::format;
use rosenpass_to::ops::copy_slice;
use rosenpass_to::To;
@@ -101,12 +102,18 @@ pub fn decrypt(
let (ciphertext, mac) = ciphertext.split_at(ciphertext.len() - TAG_LEN);
use libcrux::aead as C;
let crux_key = C::Key::Chacha20Poly1305(C::Chacha20Key(key.try_into().unwrap()));
let crux_iv = C::Iv(nonce.try_into().unwrap());
let crux_tag = C::Tag::from_slice(mac).unwrap();
let crux_key = C::Key::Chacha20Poly1305(C::Chacha20Key(key.try_into()?));
let crux_iv = C::Iv(nonce.try_into()?);
let crux_tag = match C::Tag::from_slice(mac) {
Ok(tag) => tag,
Err(err) => return Err(anyhow::anyhow!(format!("{:?}", err))),
};
copy_slice(ciphertext).to(plaintext);
libcrux::aead::decrypt(&crux_key, plaintext, crux_iv, ad, &crux_tag).unwrap();
let dec_res = libcrux::aead::decrypt(&crux_key, plaintext, crux_iv, ad, &crux_tag);
if dec_res.is_err() {
return Err(anyhow::anyhow!("Decryption failed {:?}", dec_res.err()));
}
match crux_key {
C::Key::Chacha20Poly1305(mut k) => k.0.zeroize(),