use core::convert::Infallible;
use core::error::Error;
use core::fmt;
use core::fmt::Display;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AeadInitError<E = Infallible> {
InvalidKeyLength {
actual: usize,
},
InvalidBlockSize {
actual: usize,
required: usize,
},
InvalidNonceLength {
actual: usize,
},
InvalidInitialAadLength {
actual: usize,
},
InvalidMacSize {
actual: usize,
},
InvalidCounterSize {
actual: usize,
},
NonceReuse,
InternalFailure,
Cipher(E),
}
impl<E> Display for AeadInitError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidKeyLength { actual } => {
write!(f, "invalid AEAD key length: {actual} bytes")
}
Self::InvalidBlockSize { actual, required } => {
write!(
f,
"invalid AEAD block cipher size: requires {required} bytes, got {actual}"
)
}
Self::InvalidNonceLength { actual } => {
write!(f, "invalid AEAD nonce length: {actual} bytes")
}
Self::InvalidInitialAadLength { actual } => {
write!(
f,
"invalid AEAD initial associated data length: {actual} bytes"
)
}
Self::InvalidMacSize { actual } => {
write!(f, "invalid AEAD authentication-tag size: {actual} bytes")
}
Self::InvalidCounterSize { actual } => {
write!(f, "invalid AEAD counter size: {actual} bytes")
}
Self::NonceReuse => f.write_str("key and nonce cannot be reused for AEAD encryption"),
Self::InternalFailure => f.write_str("internal AEAD primitive failure"),
Self::Cipher(_) => f.write_str("underlying cipher initialization failed"),
}
}
}
impl<E: Error + 'static> Error for AeadInitError<E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Cipher(error) => Some(error),
_ => None,
}
}
}