Skip to main content

bouncycastle_core/
errors.rs

1//! Core error types to be used in Result return types.
2//! Errors defined in this module are typically one-to-one with traits defined in [`crate::traits`].
3//!
4//! Most errors are self-explanatory, but additional description is available on some.
5
6///
7#[derive(Debug)]
8pub enum HashError {
9    ///
10    GenericError(&'static str),
11    ///
12    InvalidLength(&'static str),
13    ///
14    InvalidState(&'static str),
15    ///
16    InvalidInput(&'static str),
17    ///
18    KeyMaterialError(KeyMaterialError),
19}
20
21///
22#[derive(Debug)]
23pub enum KeyMaterialError {
24    ///
25    ActingOnZeroizedKey,
26    ///
27    GenericError(&'static str),
28    ///
29    HazardousOperationNotPermitted,
30    ///
31    InputDataLongerThanKeyCapacity,
32    ///
33    InvalidKeyType(&'static str),
34    ///
35    InvalidLength,
36    ///
37    SecurityStrength(&'static str),
38}
39
40///
41#[derive(Debug)]
42pub enum KDFError {
43    ///
44    GenericError(&'static str),
45    ///
46    HashError(HashError),
47    ///
48    InvalidLength(&'static str),
49    ///
50    KeyMaterialError(KeyMaterialError),
51    ///
52    MACError(MACError),
53}
54
55///
56#[derive(Debug)]
57pub enum KEMError {
58    ///
59    GenericError(&'static str),
60    ///
61    ConsistencyCheckFailed(&'static str),
62    ///
63    EncodingError(&'static str),
64    ///
65    DecapsulationFailed,
66    ///
67    DecodingError(&'static str),
68    ///
69    KeyGenError(&'static str),
70    ///
71    KeyMaterialError(KeyMaterialError),
72    ///
73    LengthError(&'static str),
74    ///
75    RNGError(RNGError),
76}
77
78///
79#[derive(Debug)]
80pub enum MACError {
81    ///
82    GenericError(&'static str),
83    ///
84    HashError(HashError),
85    ///
86    InvalidLength(&'static str),
87    ///
88    InvalidState(&'static str),
89    ///
90    KeyMaterialError(KeyMaterialError),
91}
92
93///
94#[derive(Debug)]
95pub enum RNGError {
96    ///
97    GenericError(&'static str),
98
99    /// Attempting to extract output before the RNG has been seeded.
100    Uninitialized,
101
102    /// The RNG has been seeded, but not sufficiently to support the requested generation operation.
103    /// This includes uses in SP 800-90A mode where more output is requested than the security strength
104    /// to which the RNG has been initialized.
105    InsufficientSeedEntropy,
106
107    /// Indicates that the RNG cannot produce any more output until it has been reseeded with fresh entropy.
108    ReseedRequired,
109
110    /// Thrown my algorithms attempting to use an RNG instance, for example for key generation or
111    /// other randomness required by the algorithm, but the provided RNG is at a lower security strength
112    /// than the algorithm requires.
113    SecurityStrengthInsufficientForAlgorithm,
114
115    ///
116    KeyMaterialError(KeyMaterialError),
117}
118
119///
120#[derive(Debug)]
121pub enum SuspendableError {
122    /// The serialized state was produced by a library version incompatible with this one.
123    IncompatibleVersion,
124    /// The serialized state is malformed or corrupt.
125    InvalidData,
126}
127
128///
129#[derive(Debug)]
130pub enum SignatureError {
131    ///
132    GenericError(&'static str),
133    ///
134    ConsistencyCheckFailed(),
135    ///
136    EncodingError(&'static str),
137    ///
138    DecodingError(&'static str),
139    ///
140    KeyGenError(&'static str),
141    ///
142    KeyMaterialError(KeyMaterialError),
143    ///
144    LengthError(&'static str),
145    ///
146    SignatureVerificationFailed,
147    ///
148    RNGError(RNGError),
149}
150
151///
152#[derive(Debug)]
153pub enum SymmetricCipherError {
154    ///
155    GenericError(&'static str),
156    ///
157    AEADTagCheckFailed,
158    ///
159    DecryptionFailed,
160    /// Indicates that the output buffer is not large enough to hold the requested output.
161    /// The usize represents the required buffer length.
162    IncorrectOutputBufferLength(&'static str, usize),
163    ///
164    KeyMaterialError(KeyMaterialError),
165    ///
166    RNGError(RNGError),
167    ///
168    StateError(&'static str),
169}
170
171/*** Promotion functions ***/
172impl From<KeyMaterialError> for SymmetricCipherError {
173    fn from(e: KeyMaterialError) -> SymmetricCipherError {
174        Self::KeyMaterialError(e)
175    }
176}
177
178impl From<RNGError> for SymmetricCipherError {
179    fn from(e: RNGError) -> SymmetricCipherError {
180        Self::RNGError(e)
181    }
182}
183
184impl From<KeyMaterialError> for HashError {
185    fn from(e: KeyMaterialError) -> HashError {
186        Self::KeyMaterialError(e)
187    }
188}
189
190impl From<HashError> for KDFError {
191    fn from(e: HashError) -> KDFError {
192        Self::HashError(e)
193    }
194}
195
196impl From<MACError> for KDFError {
197    fn from(e: MACError) -> KDFError {
198        Self::MACError(e)
199    }
200}
201
202impl From<KeyMaterialError> for KDFError {
203    fn from(e: KeyMaterialError) -> KDFError {
204        Self::KeyMaterialError(e)
205    }
206}
207
208impl From<KeyMaterialError> for KEMError {
209    fn from(e: KeyMaterialError) -> KEMError {
210        Self::KeyMaterialError(e)
211    }
212}
213
214impl From<RNGError> for KEMError {
215    fn from(e: RNGError) -> KEMError {
216        Self::RNGError(e)
217    }
218}
219
220impl From<KeyMaterialError> for MACError {
221    fn from(e: KeyMaterialError) -> MACError {
222        Self::KeyMaterialError(e)
223    }
224}
225
226impl From<HashError> for MACError {
227    fn from(e: HashError) -> MACError {
228        Self::HashError(e)
229    }
230}
231
232impl From<KeyMaterialError> for RNGError {
233    fn from(e: KeyMaterialError) -> RNGError {
234        Self::KeyMaterialError(e)
235    }
236}
237
238impl From<KeyMaterialError> for SignatureError {
239    fn from(e: KeyMaterialError) -> SignatureError {
240        Self::KeyMaterialError(e)
241    }
242}
243
244impl From<RNGError> for SignatureError {
245    fn from(e: RNGError) -> SignatureError {
246        Self::RNGError(e)
247    }
248}