dcrypt_kem/error/
mod.rs

1//! Error handling for KEM operations
2
3#![cfg_attr(not(feature = "std"), no_std)]
4
5use core::fmt;
6use dcrypt_algorithms::error::Error as PrimitiveError;
7use dcrypt_api::error::Error as CoreError;
8
9/// Error type for KEM operations
10#[derive(Debug)]
11pub enum Error {
12    /// Primitive error
13    Primitive(PrimitiveError),
14
15    /// KEM-specific errors
16    KeyGeneration {
17        algorithm: &'static str,
18        details: &'static str,
19    },
20
21    Encapsulation {
22        algorithm: &'static str,
23        details: &'static str,
24    },
25
26    Decapsulation {
27        algorithm: &'static str,
28        details: &'static str,
29    },
30
31    /// Invalid key format
32    InvalidKey {
33        key_type: &'static str,
34        reason: &'static str,
35    },
36
37    /// Invalid ciphertext format
38    InvalidCiphertext {
39        algorithm: &'static str,
40        reason: &'static str,
41    },
42
43    /// Serialization/deserialization errors
44    Serialization {
45        context: &'static str,
46        details: &'static str,
47    },
48
49    /// I/O error (only when std is available)
50    #[cfg(feature = "std")]
51    Io(std::io::Error),
52}
53
54// Implement Clone manually since std::io::Error doesn't implement Clone
55impl Clone for Error {
56    fn clone(&self) -> Self {
57        match self {
58            Error::Primitive(e) => Error::Primitive(e.clone()),
59            Error::KeyGeneration { algorithm, details } => {
60                Error::KeyGeneration { algorithm, details }
61            }
62            Error::Encapsulation { algorithm, details } => {
63                Error::Encapsulation { algorithm, details }
64            }
65            Error::Decapsulation { algorithm, details } => {
66                Error::Decapsulation { algorithm, details }
67            }
68            Error::InvalidKey { key_type, reason } => Error::InvalidKey { key_type, reason },
69            Error::InvalidCiphertext { algorithm, reason } => {
70                Error::InvalidCiphertext { algorithm, reason }
71            }
72            Error::Serialization { context, details } => Error::Serialization { context, details },
73            #[cfg(feature = "std")]
74            Error::Io(e) => Error::Io(std::io::Error::new(e.kind(), e.to_string())),
75        }
76    }
77}
78
79/// Result type for KEM operations
80pub type Result<T> = core::result::Result<T, Error>;
81
82impl fmt::Display for Error {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        match self {
85            Error::Primitive(e) => write!(f, "Primitive error: {}", e),
86            Error::KeyGeneration { algorithm, details } => {
87                write!(f, "Key generation error for {}: {}", algorithm, details)
88            }
89            Error::Encapsulation { algorithm, details } => {
90                write!(f, "Encapsulation error for {}: {}", algorithm, details)
91            }
92            Error::Decapsulation { algorithm, details } => {
93                write!(f, "Decapsulation error for {}: {}", algorithm, details)
94            }
95            Error::InvalidKey { key_type, reason } => {
96                write!(f, "Invalid {} key: {}", key_type, reason)
97            }
98            Error::InvalidCiphertext { algorithm, reason } => {
99                write!(f, "Invalid {} ciphertext: {}", algorithm, reason)
100            }
101            Error::Serialization { context, details } => {
102                write!(f, "Serialization error in {}: {}", context, details)
103            }
104            #[cfg(feature = "std")]
105            Error::Io(e) => write!(f, "I/O error: {}", e),
106        }
107    }
108}
109
110// Standard error trait
111#[cfg(feature = "std")]
112impl std::error::Error for Error {
113    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
114        match self {
115            Error::Primitive(e) => Some(e),
116            Error::Io(e) => Some(e),
117            _ => None,
118        }
119    }
120}
121
122// From PrimitiveError to Error
123impl From<PrimitiveError> for Error {
124    fn from(err: PrimitiveError) -> Self {
125        Error::Primitive(err)
126    }
127}
128
129// From std::io::Error to Error (when std is available)
130#[cfg(feature = "std")]
131impl From<std::io::Error> for Error {
132    fn from(err: std::io::Error) -> Self {
133        Error::Io(err)
134    }
135}
136
137// FIXED: From Error to CoreError - removed incorrect generic parameter
138impl From<Error> for CoreError {
139    fn from(err: Error) -> Self {
140        match err {
141            Error::Primitive(e) => e.into(),
142            Error::KeyGeneration { algorithm, details } => CoreError::Other {
143                context: algorithm,
144                #[cfg(feature = "std")]
145                message: format!("key generation failed: {}", details),
146            },
147            Error::Encapsulation { algorithm, details } => CoreError::Other {
148                context: algorithm,
149                #[cfg(feature = "std")]
150                message: format!("encapsulation failed: {}", details),
151            },
152            Error::Decapsulation { algorithm, details } => CoreError::DecryptionFailed {
153                context: algorithm,
154                #[cfg(feature = "std")]
155                message: format!("decapsulation failed: {}", details),
156            },
157            Error::InvalidKey { key_type, reason } => CoreError::InvalidKey {
158                context: key_type,
159                #[cfg(feature = "std")]
160                message: reason.to_string(),
161            },
162            Error::InvalidCiphertext { algorithm, reason } => CoreError::InvalidCiphertext {
163                context: algorithm,
164                #[cfg(feature = "std")]
165                message: reason.to_string(),
166            },
167            Error::Serialization { context, details } => CoreError::SerializationError {
168                context,
169                #[cfg(feature = "std")]
170                message: details.to_string(),
171            },
172            #[cfg(feature = "std")]
173            Error::Io(e) => CoreError::Other {
174                context: "I/O operation",
175                message: e.to_string(),
176            },
177        }
178    }
179}
180
181// Include validation submodule
182pub mod validate;
183
184// Re-export core error handling traits
185pub use dcrypt_api::error::{ResultExt, SecureErrorHandling};