quantum_shield/
error.rs

1//! Error types for Quantum Shield
2
3use std::fmt;
4
5/// Result type alias for Quantum Shield operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur during cryptographic operations
9#[derive(Debug)]
10pub enum Error {
11    /// Invalid key format or size
12    InvalidKey(String),
13    
14    /// Encryption operation failed
15    EncryptionFailed(String),
16    
17    /// Decryption operation failed  
18    DecryptionFailed(String),
19    
20    /// Signature generation failed
21    SigningFailed(String),
22    
23    /// Signature verification failed
24    VerificationFailed(String),
25    
26    /// Key generation failed
27    KeyGenerationFailed(String),
28    
29    /// Serialization/deserialization error
30    SerializationError(String),
31    
32    /// Invalid ciphertext format
33    InvalidCiphertext(String),
34    
35    /// Invalid signature format
36    InvalidSignature(String),
37    
38    /// IO error
39    IoError(String),
40    
41    /// Unsupported version
42    UnsupportedVersion(u32),
43    
44    /// Generic error
45    Other(String),
46}
47
48impl fmt::Display for Error {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            Error::InvalidKey(msg) => write!(f, "Invalid key: {}", msg),
52            Error::EncryptionFailed(msg) => write!(f, "Encryption failed: {}", msg),
53            Error::DecryptionFailed(msg) => write!(f, "Decryption failed: {}", msg),
54            Error::SigningFailed(msg) => write!(f, "Signing failed: {}", msg),
55            Error::VerificationFailed(msg) => write!(f, "Verification failed: {}", msg),
56            Error::KeyGenerationFailed(msg) => write!(f, "Key generation failed: {}", msg),
57            Error::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
58            Error::InvalidCiphertext(msg) => write!(f, "Invalid ciphertext: {}", msg),
59            Error::InvalidSignature(msg) => write!(f, "Invalid signature: {}", msg),
60            Error::IoError(msg) => write!(f, "IO error: {}", msg),
61            Error::UnsupportedVersion(v) => write!(f, "Unsupported version: {}", v),
62            Error::Other(msg) => write!(f, "{}", msg),
63        }
64    }
65}
66
67impl std::error::Error for Error {}
68
69impl From<std::io::Error> for Error {
70    fn from(err: std::io::Error) -> Self {
71        Error::IoError(err.to_string())
72    }
73}
74
75impl From<serde_json::Error> for Error {
76    fn from(err: serde_json::Error) -> Self {
77        Error::SerializationError(err.to_string())
78    }
79}
80
81impl From<base64::DecodeError> for Error {
82    fn from(err: base64::DecodeError) -> Self {
83        Error::SerializationError(format!("Base64 decode error: {}", err))
84    }
85}
86
87impl From<rsa::Error> for Error {
88    fn from(err: rsa::Error) -> Self {
89        Error::Other(format!("RSA error: {}", err))
90    }
91}
92
93impl From<rsa::pkcs8::Error> for Error {
94    fn from(err: rsa::pkcs8::Error) -> Self {
95        Error::InvalidKey(format!("PKCS8 error: {}", err))
96    }
97}
98
99impl From<aes_gcm::Error> for Error {
100    fn from(err: aes_gcm::Error) -> Self {
101        Error::EncryptionFailed(format!("AES-GCM error: {:?}", err))
102    }
103}
104