1use std::fmt;
4
5pub type PqcResult<T> = Result<T, PqcError>;
7
8#[derive(Debug, Clone)]
10pub enum PqcError {
11 KeyGenerationFailed(String),
13
14 EncapsulationFailed(String),
16
17 DecapsulationFailed(String),
19
20 SigningFailed(String),
22
23 VerificationFailed,
25
26 SerializationError(String),
28
29 InvalidInput(String),
31
32 RngError(String),
34
35 UnsupportedVariant(String),
37
38 InvalidKeySize {
40 expected: usize,
42 got: usize,
44 },
45
46 InvalidSignatureSize {
48 expected: usize,
50 got: usize,
52 },
53
54 InvalidCiphertextSize {
56 expected: usize,
58 got: usize,
60 },
61
62 ContextTooLong {
64 max: usize,
66 got: usize,
68 },
69
70 EncryptionFailed(String),
72
73 DecryptionFailed(String),
75
76 FeatureNotAvailable,
78
79 InvalidNonceLength,
81
82 InvalidKeyLength,
84
85 InvalidSignature,
87
88 EncryptionError,
90
91 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#[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}