#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum CryptoError {
InvalidKey,
InvalidNonce,
InvalidTag,
BufferTooSmall,
BadInput,
Internal(&'static str),
Kex,
Sign,
Rng,
Encoding,
UnsupportedAlgorithm,
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for CryptoError {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(serde::Deserialize)]
#[serde(rename = "CryptoError")]
enum Repr {
InvalidKey,
InvalidNonce,
InvalidTag,
BufferTooSmall,
BadInput,
Internal(#[allow(dead_code)] alloc::string::String),
Kex,
Sign,
Rng,
Encoding,
UnsupportedAlgorithm,
}
let repr = Repr::deserialize(deserializer)?;
Ok(match repr {
Repr::InvalidKey => CryptoError::InvalidKey,
Repr::InvalidNonce => CryptoError::InvalidNonce,
Repr::InvalidTag => CryptoError::InvalidTag,
Repr::BufferTooSmall => CryptoError::BufferTooSmall,
Repr::BadInput => CryptoError::BadInput,
Repr::Internal(_) => CryptoError::Internal(""),
Repr::Kex => CryptoError::Kex,
Repr::Sign => CryptoError::Sign,
Repr::Rng => CryptoError::Rng,
Repr::Encoding => CryptoError::Encoding,
Repr::UnsupportedAlgorithm => CryptoError::UnsupportedAlgorithm,
})
}
}
impl core::fmt::Display for CryptoError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
CryptoError::InvalidKey => write!(f, "invalid key"),
CryptoError::InvalidNonce => write!(f, "invalid nonce"),
CryptoError::InvalidTag => write!(f, "invalid authentication tag"),
CryptoError::BufferTooSmall => write!(f, "output buffer too small"),
CryptoError::BadInput => write!(f, "bad input"),
CryptoError::Internal(msg) => write!(f, "internal error: {msg}"),
CryptoError::Kex => write!(f, "key exchange or encapsulation failure"),
CryptoError::Sign => write!(f, "signature generation or verification failure"),
CryptoError::Rng => write!(f, "random number generator failure"),
CryptoError::Encoding => write!(f, "encoding or decoding failure"),
CryptoError::UnsupportedAlgorithm => write!(f, "unsupported algorithm"),
}
}
}
impl core::error::Error for CryptoError {}
#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "std")]
impl From<CryptoError> for std::io::Error {
fn from(e: CryptoError) -> Self {
std::io::Error::other(alloc::format!("{e}"))
}
}