dcrypt_sign/
error.rs

1//! Error types for the signature crate
2
3use core::fmt;
4
5/// Errors that can occur during signature operations
6#[derive(Debug, Clone)]
7pub enum Error {
8    /// Algorithm not supported
9    Algorithm(String),
10
11    /// Invalid key size
12    InvalidKeySize { expected: usize, actual: usize },
13
14    /// Invalid signature size
15    InvalidSignatureSize { expected: usize, actual: usize },
16
17    /// Invalid parameter
18    InvalidParameter(String),
19
20    /// Invalid key
21    InvalidKey(String),
22
23    /// Key generation failed
24    KeyGeneration {
25        algorithm: &'static str,
26        details: String,
27    },
28
29    /// Signature generation failed
30    SignatureGeneration {
31        algorithm: &'static str,
32        details: String,
33    },
34
35    /// Verification failed
36    Verification {
37        algorithm: &'static str,
38        details: String,
39    },
40
41    /// Encoding error
42    Encoding(String),
43
44    /// Deserialization error
45    Deserialization(String),
46
47    /// Serialization error
48    Serialization(String),
49
50    /// Nonce error
51    Nonce(String),
52
53    /// Hashing error
54    Hashing(String),
55
56    /// RNG error
57    Rng(String),
58
59    /// Sampling error
60    Sampling(String),
61
62    /// Internal error
63    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
105// Helper method to convert from algorithms error
106impl Error {
107    pub fn from_algo(err: dcrypt_algorithms::error::Error) -> Self {
108        Self::Internal(format!("Algorithm error: {:?}", err))
109    }
110}
111
112// Convert from algorithms::error::Error
113impl 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
129// Convert to api::Error
130impl From<Error> for dcrypt_api::Error {
131    fn from(err: Error) -> Self {
132        match err {
133            // Map Algorithm error to InvalidParameter with context
134            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            // Map KeyGeneration to InvalidKey (key generation failures produce invalid keys)
160            Error::KeyGeneration { algorithm, details } => dcrypt_api::Error::InvalidKey {
161                context: algorithm,
162                message: format!("Key generation failed: {}", details),
163            },
164            // Map SignatureGeneration to InvalidSignature
165            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            // Map internal errors (Hashing, Rng, Sampling, Internal) to InvalidParameter
192            // This isn't ideal but without an Internal variant in api::Error, this is the best mapping
193            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>;