Skip to main content

saorsa_pqc/api/
errors.rs

1//! Error types for the PQC API
2
3use std::fmt;
4
5/// Result type for PQC operations
6pub type PqcResult<T> = Result<T, PqcError>;
7
8/// Comprehensive error type for all PQC operations
9#[derive(Debug, Clone)]
10pub enum PqcError {
11    /// Key generation failed
12    KeyGenerationFailed(String),
13
14    /// Encapsulation failed
15    EncapsulationFailed(String),
16
17    /// Decapsulation failed
18    DecapsulationFailed(String),
19
20    /// Signing failed
21    SigningFailed(String),
22
23    /// Verification failed (signature invalid)
24    VerificationFailed,
25
26    /// Serialization/deserialization error
27    SerializationError(String),
28
29    /// Invalid parameter or input
30    InvalidInput(String),
31
32    /// RNG error
33    RngError(String),
34
35    /// Unsupported algorithm variant
36    UnsupportedVariant(String),
37
38    /// Invalid key size
39    InvalidKeySize {
40        /// Expected key size in bytes
41        expected: usize,
42        /// Actual key size received
43        got: usize,
44    },
45
46    /// Invalid signature size
47    InvalidSignatureSize {
48        /// Expected signature size in bytes
49        expected: usize,
50        /// Actual signature size received
51        got: usize,
52    },
53
54    /// Invalid ciphertext size
55    InvalidCiphertextSize {
56        /// Expected ciphertext size in bytes
57        expected: usize,
58        /// Actual ciphertext size received
59        got: usize,
60    },
61
62    /// Context too long (for ML-DSA)
63    ContextTooLong {
64        /// Maximum allowed context length
65        max: usize,
66        /// Actual context length provided
67        got: usize,
68    },
69
70    /// Encryption failed
71    EncryptionFailed(String),
72
73    /// Decryption failed
74    DecryptionFailed(String),
75
76    /// Feature not available
77    FeatureNotAvailable,
78
79    /// Invalid nonce length
80    InvalidNonceLength,
81
82    /// Invalid key length
83    InvalidKeyLength,
84
85    /// Invalid signature (for HMAC/MAC verification)
86    InvalidSignature,
87
88    /// Generic encryption error (for AEAD)
89    EncryptionError,
90
91    /// Generic decryption error (for AEAD)
92    DecryptionError,
93}
94
95impl fmt::Display for PqcError {
96    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97        match self {
98            Self::KeyGenerationFailed(msg) => write!(f, "Key generation failed: {msg}"),
99            Self::EncapsulationFailed(msg) => write!(f, "Encapsulation failed: {msg}"),
100            Self::DecapsulationFailed(msg) => write!(f, "Decapsulation failed: {msg}"),
101            Self::SigningFailed(msg) => write!(f, "Signing failed: {msg}"),
102            Self::VerificationFailed => write!(f, "Signature verification failed"),
103            Self::SerializationError(msg) => write!(f, "Serialization error: {msg}"),
104            Self::InvalidInput(msg) => write!(f, "Invalid input: {msg}"),
105            Self::RngError(msg) => write!(f, "RNG error: {msg}"),
106            Self::UnsupportedVariant(msg) => write!(f, "Unsupported variant: {msg}"),
107            Self::InvalidKeySize { expected, got } => {
108                write!(f, "Invalid key size: expected {expected} bytes, got {got}")
109            }
110            Self::InvalidSignatureSize { expected, got } => {
111                write!(
112                    f,
113                    "Invalid signature size: expected {expected} bytes, got {got}"
114                )
115            }
116            Self::InvalidCiphertextSize { expected, got } => {
117                write!(
118                    f,
119                    "Invalid ciphertext size: expected {expected} bytes, got {got}"
120                )
121            }
122            Self::ContextTooLong { max, got } => {
123                write!(f, "Context too long: maximum {max} bytes, got {got}")
124            }
125            Self::EncryptionFailed(msg) => write!(f, "Encryption failed: {msg}"),
126            Self::DecryptionFailed(msg) => write!(f, "Decryption failed: {msg}"),
127            Self::FeatureNotAvailable => write!(f, "Feature not available"),
128            Self::InvalidNonceLength => write!(f, "Invalid nonce length"),
129            Self::InvalidKeyLength => write!(f, "Invalid key length"),
130            Self::InvalidSignature => write!(f, "Invalid signature or MAC"),
131            Self::EncryptionError => write!(f, "Encryption error"),
132            Self::DecryptionError => write!(f, "Decryption error"),
133        }
134    }
135}
136
137impl std::error::Error for PqcError {}
138
139// Note: rand_core errors are handled differently in newer versions
140
141#[cfg(test)]
142#[allow(clippy::indexing_slicing)]
143#[allow(clippy::unwrap_used, clippy::expect_used)]
144mod tests {
145    use super::*;
146
147    #[test]
148    fn test_error_display() {
149        let err = PqcError::KeyGenerationFailed("test".to_string());
150        assert_eq!(err.to_string(), "Key generation failed: test");
151
152        let err = PqcError::InvalidKeySize {
153            expected: 32,
154            got: 16,
155        };
156        assert_eq!(
157            err.to_string(),
158            "Invalid key size: expected 32 bytes, got 16"
159        );
160    }
161}