1use core::fmt;
4
5#[derive(Debug, Clone)]
7pub enum Error {
8 Algorithm(String),
10
11 InvalidKeySize { expected: usize, actual: usize },
13
14 InvalidSignatureSize { expected: usize, actual: usize },
16
17 InvalidParameter(String),
19
20 InvalidKey(String),
22
23 KeyGeneration {
25 algorithm: &'static str,
26 details: String,
27 },
28
29 SignatureGeneration {
31 algorithm: &'static str,
32 details: String,
33 },
34
35 Verification {
37 algorithm: &'static str,
38 details: String,
39 },
40
41 Encoding(String),
43
44 Deserialization(String),
46
47 Serialization(String),
49
50 Nonce(String),
52
53 Hashing(String),
55
56 Rng(String),
58
59 Sampling(String),
61
62 Internal(String),
64}
65
66impl fmt::Display for Error {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 match self {
69 Error::Algorithm(alg) => write!(f, "Unsupported algorithm: {}", alg),
70 Error::InvalidKeySize { expected, actual } => {
71 write!(f, "Invalid key size: expected {}, got {}", expected, actual)
72 }
73 Error::InvalidSignatureSize { expected, actual } => {
74 write!(
75 f,
76 "Invalid signature size: expected {}, got {}",
77 expected, actual
78 )
79 }
80 Error::InvalidParameter(msg) => write!(f, "Invalid parameter: {}", msg),
81 Error::InvalidKey(msg) => write!(f, "Invalid key: {}", msg),
82 Error::KeyGeneration { algorithm, details } => {
83 write!(f, "{} key generation failed: {}", algorithm, details)
84 }
85 Error::SignatureGeneration { algorithm, details } => {
86 write!(f, "{} signature generation failed: {}", algorithm, details)
87 }
88 Error::Verification { algorithm, details } => {
89 write!(f, "{} verification failed: {}", algorithm, details)
90 }
91 Error::Encoding(msg) => write!(f, "Encoding error: {}", msg),
92 Error::Deserialization(msg) => write!(f, "Deserialization error: {}", msg),
93 Error::Serialization(msg) => write!(f, "Serialization error: {}", msg),
94 Error::Nonce(msg) => write!(f, "Nonce error: {}", msg),
95 Error::Hashing(msg) => write!(f, "Hashing error: {}", msg),
96 Error::Rng(msg) => write!(f, "RNG error: {}", msg),
97 Error::Sampling(msg) => write!(f, "Sampling error: {}", msg),
98 Error::Internal(msg) => write!(f, "Internal error: {}", msg),
99 }
100 }
101}
102
103impl std::error::Error for Error {}
104
105impl Error {
107 pub fn from_algo(err: dcrypt_algorithms::error::Error) -> Self {
108 Self::Internal(format!("Algorithm error: {:?}", err))
109 }
110}
111
112impl From<dcrypt_algorithms::error::Error> for Error {
114 fn from(err: dcrypt_algorithms::error::Error) -> Self {
115 use dcrypt_algorithms::error::Error as AlgoError;
116
117 match err {
118 AlgoError::Parameter { name, reason } => {
119 Error::InvalidParameter(format!("{}: {}", name, reason))
120 }
121 AlgoError::NotImplemented { feature } => {
122 Error::Internal(format!("Feature not implemented: {}", feature))
123 }
124 _ => Error::Internal(format!("Algorithm error: {:?}", err)),
125 }
126 }
127}
128
129impl From<Error> for dcrypt_api::Error {
131 fn from(err: Error) -> Self {
132 match err {
133 Error::Algorithm(alg) => dcrypt_api::Error::InvalidParameter {
135 context: "algorithm",
136 message: format!("Unsupported algorithm: {}", alg),
137 },
138 Error::InvalidKeySize { expected, actual } => dcrypt_api::Error::InvalidKey {
139 context: "sign",
140 message: format!("Invalid key size: expected {}, got {}", expected, actual),
141 },
142 Error::InvalidSignatureSize { expected, actual } => {
143 dcrypt_api::Error::InvalidSignature {
144 context: "sign",
145 message: format!(
146 "Invalid signature size: expected {}, got {}",
147 expected, actual
148 ),
149 }
150 }
151 Error::InvalidParameter(msg) => dcrypt_api::Error::InvalidParameter {
152 context: "sign",
153 message: msg,
154 },
155 Error::InvalidKey(msg) => dcrypt_api::Error::InvalidKey {
156 context: "sign",
157 message: msg,
158 },
159 Error::KeyGeneration { algorithm, details } => dcrypt_api::Error::InvalidKey {
161 context: algorithm,
162 message: format!("Key generation failed: {}", details),
163 },
164 Error::SignatureGeneration { algorithm, details } => {
166 dcrypt_api::Error::InvalidSignature {
167 context: algorithm,
168 message: format!("Signature generation failed: {}", details),
169 }
170 }
171 Error::Verification { algorithm, details } => dcrypt_api::Error::InvalidSignature {
172 context: algorithm,
173 message: details,
174 },
175 Error::Encoding(s) => dcrypt_api::Error::InvalidParameter {
176 context: "encoding",
177 message: s,
178 },
179 Error::Deserialization(s) => dcrypt_api::Error::InvalidParameter {
180 context: "deserialization",
181 message: s,
182 },
183 Error::Serialization(s) => dcrypt_api::Error::InvalidParameter {
184 context: "serialization",
185 message: s,
186 },
187 Error::Nonce(s) => dcrypt_api::Error::InvalidParameter {
188 context: "nonce",
189 message: s,
190 },
191 Error::Hashing(s) => dcrypt_api::Error::InvalidParameter {
194 context: "hashing",
195 message: s,
196 },
197 Error::Rng(s) => dcrypt_api::Error::InvalidParameter {
198 context: "rng",
199 message: s,
200 },
201 Error::Sampling(s) => dcrypt_api::Error::InvalidParameter {
202 context: "sampling",
203 message: s,
204 },
205 Error::Internal(s) => dcrypt_api::Error::InvalidParameter {
206 context: "internal",
207 message: s,
208 },
209 }
210 }
211}
212
213pub type Result<T> = core::result::Result<T, Error>;