1#![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#[derive(Debug)]
11pub enum Error {
12 Primitive(PrimitiveError),
14
15 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 InvalidKey {
33 key_type: &'static str,
34 reason: &'static str,
35 },
36
37 InvalidCiphertext {
39 algorithm: &'static str,
40 reason: &'static str,
41 },
42
43 Serialization {
45 context: &'static str,
46 details: &'static str,
47 },
48
49 #[cfg(feature = "std")]
51 Io(std::io::Error),
52}
53
54impl 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
79pub 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#[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
122impl From<PrimitiveError> for Error {
124 fn from(err: PrimitiveError) -> Self {
125 Error::Primitive(err)
126 }
127}
128
129#[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
137impl 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
181pub mod validate;
183
184pub use dcrypt_api::error::{ResultExt, SecureErrorHandling};