use libcrux_secrets::U8;
pub trait Aead<const KEY_LEN: usize, const TAG_LEN: usize, const NONCE_LEN: usize> {
fn keygen(key: &mut [U8; KEY_LEN], rand: &[U8; KEY_LEN]) -> Result<(), KeyGenError>;
fn encrypt(
ciphertext: &mut [u8],
tag: &mut [U8; TAG_LEN],
key: &[U8; KEY_LEN],
nonce: &[U8; NONCE_LEN],
aad: &[u8],
plaintext: &[U8],
) -> Result<(), EncryptError>;
fn decrypt(
plaintext: &mut [U8],
key: &[U8; KEY_LEN],
nonce: &[U8; NONCE_LEN],
aad: &[u8],
ciphertext: &[u8],
tag: &[U8; TAG_LEN],
) -> Result<(), DecryptError>;
}
pub struct KeyGenError;
#[derive(Debug, PartialEq, Eq)]
pub enum EncryptError {
WrongCiphertextLength,
PlaintextTooLong,
AadTooLong,
Unknown,
}
#[derive(Debug, PartialEq, Eq)]
pub enum DecryptError {
InvalidTag,
WrongPlaintextLength,
PlaintextTooLong,
AadTooLong,
Unknown,
}
impl core::fmt::Display for EncryptError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let text = match self {
EncryptError::WrongCiphertextLength => "ciphertext buffer has wrong length",
EncryptError::PlaintextTooLong => {
"plaintext is too long for algorithm or implementation"
}
EncryptError::AadTooLong => "aad is too long for algorithm or implementation",
EncryptError::Unknown => "an unknown error occurred",
};
f.write_str(text)
}
}
impl core::fmt::Display for DecryptError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let text = match self {
DecryptError::InvalidTag => "invalid authentication tag",
DecryptError::WrongPlaintextLength => "plaintext buffer has wrong length",
DecryptError::PlaintextTooLong => {
"plaintext is too long for algorithm or implementation"
}
DecryptError::AadTooLong => "aad is too long for algorithm or implementation",
DecryptError::Unknown => "an unknown error occurred",
};
f.write_str(text)
}
}
#[cfg(feature = "error-in-core")]
mod error_in_core {
impl core::error::Error for super::EncryptError {}
impl core::error::Error for super::DecryptError {}
}