1use std::num::TryFromIntError;
2
3pub use anyhow::Error;
4
5#[derive(thiserror::Error, Debug)]
6pub enum CryptoError {
7 #[error("Success")]
8 Success,
9 #[error("Guest error")]
10 GuestError(#[from] Error),
11 #[error("Not implemented")]
12 NotImplemented,
13 #[error("Unsupported feature")]
14 UnsupportedFeature,
15 #[error("Prohibited by local policy")]
16 ProhibitedOperation,
17 #[error("Unsupported encoding")]
18 UnsupportedEncoding,
19 #[error("Unsupported algorithm")]
20 UnsupportedAlgorithm,
21 #[error("Unsupported option")]
22 UnsupportedOption,
23 #[error("Invalid key")]
24 InvalidKey,
25 #[error("Invalid length")]
26 InvalidLength,
27 #[error("Verification failed")]
28 VerificationFailed,
29 #[error("RNG error")]
30 RNGError,
31 #[error("Operation failed")]
32 AlgorithmFailure,
33 #[error("Invalid signature")]
34 InvalidSignature,
35 #[error("Handle already closed")]
36 Closed,
37 #[error("Invalid handle")]
38 InvalidHandle,
39 #[error("Overflow")]
40 Overflow,
41 #[error("Internal error")]
42 InternalError,
43 #[error("Too many open handles")]
44 TooManyHandles,
45 #[error("Selected algorithm doesn't support a key")]
46 KeyNotSupported,
47 #[error("Selected algorithm requires a key")]
48 KeyRequired,
49 #[error("Authentication tag did not verify")]
50 InvalidTag,
51 #[error("Operation invalid for the selected algorithm")]
52 InvalidOperation,
53 #[error("Nonce required")]
54 NonceRequired,
55 #[error("Nonce doesn't have a correct size")]
56 InvalidNonce,
57 #[error("Option not set")]
58 OptionNotSet,
59 #[error("Key not found")]
60 NotFound,
61 #[error("Parameters missing")]
62 ParametersMissing,
63 #[error("Incompatible keys")]
64 IncompatibleKeys,
65 #[error("Expired secret")]
66 Expired,
67}
68
69impl From<TryFromIntError> for CryptoError {
70 fn from(_: TryFromIntError) -> Self {
71 CryptoError::Overflow
72 }
73}
74
75#[macro_export]
76macro_rules! ensure {
77 ($cond:expr, $err:expr $(,)?) => {
78 if !$cond {
79 return Err($err);
80 }
81 };
82 ($cond:expr, $fmt:expr, $($arg:tt)*) => {
83 if !$cond {
84 return Err($fmt, $($arg)*);
85 }
86 };
87}
88
89#[macro_export]
90macro_rules! bail {
91 ($err:expr $(,)?) => {
92 return Err($err)
93 };
94 ($fmt:expr, $($arg:tt)*) => {
95 return Err($fmt, $($arg)*)
96 };
97}
98
99pub use {bail, ensure};