Skip to main content

quantacipher_core/
error.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum QuantaCipherError {
5    KeygenFailed,
6    EncapsulationFailed,
7    DecapsulationFailed,
8    EncryptionFailed,
9    DecryptionFailed,
10    InvalidPublicKeyLength,
11    InvalidPrivateKeyLength,
12    InvalidCiphertextLength,
13    InvalidPayloadFormat,
14    DecodeError(base64::DecodeError),
15    Utf8Error(std::string::FromUtf8Error),
16}
17
18impl fmt::Display for QuantaCipherError {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            Self::KeygenFailed => write!(f, "Kyber keygen failed"),
22            Self::EncapsulationFailed => write!(f, "Kyber encapsulation failed"),
23            Self::DecapsulationFailed => write!(f, "Kyber decapsulation failed (wrong key?)"),
24            Self::EncryptionFailed => write!(f, "AES-GCM encryption failed"),
25            Self::DecryptionFailed => write!(f, "AES-GCM decryption failed (tampered data or wrong key?)"),
26            Self::InvalidPublicKeyLength => write!(f, "Invalid public key length"),
27            Self::InvalidPrivateKeyLength => write!(f, "Invalid private key length"),
28            Self::InvalidCiphertextLength => write!(f, "Invalid Kyber ciphertext length"),
29            Self::InvalidPayloadFormat => write!(f, "Invalid payload format"),
30            Self::DecodeError(e) => write!(f, "Base64 decode error: {}", e),
31            Self::Utf8Error(e) => write!(f, "UTF-8 decode error: {}", e),
32        }
33    }
34}
35
36impl std::error::Error for QuantaCipherError {}
37
38impl From<base64::DecodeError> for QuantaCipherError {
39    fn from(err: base64::DecodeError) -> Self {
40        QuantaCipherError::DecodeError(err)
41    }
42}
43
44impl From<std::string::FromUtf8Error> for QuantaCipherError {
45    fn from(err: std::string::FromUtf8Error) -> Self {
46        QuantaCipherError::Utf8Error(err)
47    }
48}