1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
pub use anyhow::Error;
use std::num::TryFromIntError;

#[derive(thiserror::Error, Debug)]
pub enum CryptoError {
    #[error("Success")]
    Success,
    #[error("Guest error")]
    GuestError(#[from] Error),
    #[error("Not implemented")]
    NotImplemented,
    #[error("Unsupported feature")]
    UnsupportedFeature,
    #[error("Prohibited by local policy")]
    ProhibitedOperation,
    #[error("Unsupported encoding")]
    UnsupportedEncoding,
    #[error("Unsupported algorithm")]
    UnsupportedAlgorithm,
    #[error("Unsupported option")]
    UnsupportedOption,
    #[error("Invalid key")]
    InvalidKey,
    #[error("Verification failed")]
    InvalidLength,
    #[error("Invalid length")]
    VerificationFailed,
    #[error("RNG error")]
    RNGError,
    #[error("Operation failed")]
    AlgorithmFailure,
    #[error("Invalid signature")]
    InvalidSignature,
    #[error("Handle already closed")]
    Closed,
    #[error("Invalid handle")]
    InvalidHandle,
    #[error("Overflow")]
    Overflow,
    #[error("Internal error")]
    InternalError,
    #[error("Too many open handles")]
    TooManyHandles,
    #[error("Selected algorithm doesn't support a key")]
    KeyNotSupported,
    #[error("Selected algorithm requires a key")]
    KeyRequired,
    #[error("Authentication tag did not verify")]
    InvalidTag,
    #[error("Operation invalid for the selected algorithm")]
    InvalidOperation,
    #[error("Nonce required")]
    NonceRequired,
    #[error("Nonce doesn't have a correct size")]
    InvalidNonce,
    #[error("Option not set")]
    OptionNotSet,
    #[error("Key not found")]
    NotFound,
    #[error("Parameters missing")]
    ParametersMissing,
    #[error("Incompatible keys")]
    IncompatibleKeys,
    #[error("Expired secret")]
    Expired,
}

impl From<TryFromIntError> for CryptoError {
    fn from(_: TryFromIntError) -> Self {
        CryptoError::Overflow
    }
}

#[macro_export]
macro_rules! ensure {
    ($cond:expr, $err:expr $(,)?) => {
        if !$cond {
            return Err($err);
        }
    };
    ($cond:expr, $fmt:expr, $($arg:tt)*) => {
        if !$cond {
            return Err($fmt, $($arg)*);
        }
    };
}

#[macro_export]
macro_rules! bail {
    ($err:expr $(,)?) => {
        return Err($err)
    };
    ($fmt:expr, $($arg:tt)*) => {
        return Err($fmt, $($arg)*)
    };
}

pub use {bail, ensure};