use core::error::Error;
use core::fmt;
use core::fmt::Display;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum KeyWrapError<E> {
NotInitialized,
NotForWrapping,
NotForUnwrapping,
InvalidWrapLength,
InvalidUnwrapLength,
UnsupportedBlockSize {
actual: usize,
required: usize,
},
BlockSizeTooShort {
actual: usize,
minimum: usize,
},
OutputTooShort {
required: usize,
available: usize,
},
IntegrityCheckFailed,
Cipher(E),
}
impl<E: Error> Display for KeyWrapError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotInitialized => f.write_str("key wrapper not initialized"),
Self::NotForWrapping => f.write_str("key wrapper not set for wrapping"),
Self::NotForUnwrapping => f.write_str("key wrapper not set for unwrapping"),
Self::InvalidWrapLength => f.write_str("invalid key-wrap input length"),
Self::InvalidUnwrapLength => f.write_str("invalid key-unwrap input length"),
Self::UnsupportedBlockSize { actual, required } => write!(
f,
"unsupported block size: {actual} bytes; key wrapper requires {required} bytes"
),
Self::BlockSizeTooShort { actual, minimum } => write!(
f,
"block size {actual} is too short; key wrapper requires at least {minimum} bytes"
),
Self::OutputTooShort {
required,
available,
} => write!(
f,
"output buffer is too short: requires {required} bytes, has {available}"
),
Self::IntegrityCheckFailed => f.write_str("key-wrap integrity check failed"),
Self::Cipher(error) => write!(f, "underlying cipher error: {error}"),
}
}
}
impl<E: Error> Error for KeyWrapError<E> {}