Skip to main content

devolutions_crypto/
error.rs

1//! Possible errors in the library.
2use cbc::cipher::block_padding::Error as UnpadError;
3
4#[cfg(feature = "wbindgen")]
5use wasm_bindgen::JsValue;
6
7use strum::IntoStaticStr;
8
9use hmac::digest::MacError;
10
11pub type Result<T> = std::result::Result<T, Error>;
12
13/// This crate's error type.
14#[derive(Debug, IntoStaticStr, thiserror::Error)]
15pub enum Error {
16    /// The provided data has an invalid length. Error code: -1
17    #[error("The provided data has an invalid length")]
18    InvalidLength,
19    /// The key length is invalid. Error code: -2
20    #[error("The key length is invalid.")]
21    InvalidKeyLength,
22    /// The length of the FFI output buffer is invalid. Error code: -3
23    #[error("The length of the FFI output buffer is invalid.")]
24    InvalidOutputLength,
25    /// The signature of the data blob does not match 0x0d0c. Error code: -11
26    #[error("The signature of the data blob does not match 0x0d0c.")]
27    InvalidSignature,
28    /// The MAC is invalid. Error code: -12
29    #[error("The MAC is invalid.")]
30    InvalidMac,
31    /// The operation cannot be done with this type. Error code: -13
32    #[error("The operation cannot be done with this type.")]
33    InvalidDataType,
34    /// The data type is unknown. Error code: -21
35    #[error("The data type is unknown.")]
36    UnknownType,
37    /// The data subtype is unknown. Error code: -22
38    #[error("The data subtype is unknown.")]
39    UnknownSubtype,
40    /// The data type version is unknown. Error code: -23
41    #[error("The data type version is unknown.")]
42    UnknownVersion,
43    /// The data is invalid. Error code: -24
44    #[error("The data is invalid.")]
45    InvalidData,
46    /// A null pointer has been passed to the FFI interface. Error code: -31
47    #[error("A null pointer has been passed to the FFI interface.")]
48    NullPointer,
49    /// A cryptographic error occurred. Error code: -32
50    #[error("A cryptographic error occurred.")]
51    CryptoError,
52    /// An error with the Random Number Generator occurred. Error code: -33
53    #[error("An error with the Random Number Generator occurred.")]
54    RandomError,
55    /// A generic IO error has occurred. Error code: -34
56    #[error("Generic IO error: {0}")]
57    IoError(#[from] std::io::Error),
58    /// There is not enough shares to regenerate a secret: -41
59    #[error("There wasn't enough share to regenerate the secret.")]
60    NotEnoughShares,
61    /// The version of the multiple data is inconsistent: -42
62    #[error("The version is not the same for all the data.")]
63    InconsistentVersion,
64    /// The length of the data to encrypt/decrypt during online encryption is not the same as the chunk size: -43
65    #[error(
66        "The length of the data to encrypt/decrypt during online encryption is not the same as the chunk size"
67    )]
68    InvalidChunkLength,
69    /// The mutex is poisoned and cannot be locked: -44
70    #[error("The mutex is poisoned and cannot be locked")]
71    PoisonedMutex,
72}
73
74impl Error {
75    /// Returns the error code associated with the error.
76    /// This is useful for passing the exception type across a language boundary.
77    pub fn error_code(&self) -> i64 {
78        match *self {
79            Error::InvalidLength => -1,
80            Error::InvalidKeyLength => -2,
81            Error::InvalidOutputLength => -3,
82            Error::InvalidSignature => -11,
83            Error::InvalidMac => -12,
84            Error::InvalidDataType => -13,
85            Error::UnknownType => -21,
86            Error::UnknownSubtype => -22,
87            Error::UnknownVersion => -23,
88            Error::InvalidData => -24,
89            Error::NullPointer => -31,
90            Error::CryptoError => -32,
91            Error::RandomError => -33,
92            Error::IoError(_) => -34,
93            Error::NotEnoughShares => -41,
94            Error::InconsistentVersion => -42,
95            Error::InvalidChunkLength => -43,
96            Error::PoisonedMutex => -44,
97        }
98    }
99}
100
101impl From<hmac::digest::InvalidLength> for Error {
102    fn from(_error: hmac::digest::InvalidLength) -> Error {
103        Error::InvalidKeyLength
104    }
105}
106
107impl From<MacError> for Error {
108    fn from(_error: MacError) -> Error {
109        Error::InvalidMac
110    }
111}
112
113impl From<UnpadError> for Error {
114    fn from(_error: UnpadError) -> Error {
115        Error::CryptoError
116    }
117}
118
119impl From<chacha20poly1305::aead::Error> for Error {
120    fn from(_error: chacha20poly1305::aead::Error) -> Error {
121        Error::InvalidMac
122    }
123}
124
125impl From<argon2::Error> for Error {
126    fn from(_error: argon2::Error) -> Self {
127        Error::CryptoError
128    }
129}
130
131#[cfg(feature = "wbindgen")]
132impl From<Error> for JsValue {
133    fn from(error: Error) -> JsValue {
134        let js_error = js_sys::Error::new(&error.to_string());
135
136        js_error.set_name(error.into());
137        js_error.into()
138    }
139}