devolutions_crypto/
error.rs1use 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#[derive(Debug, IntoStaticStr, thiserror::Error)]
15pub enum Error {
16 #[error("The provided data has an invalid length")]
18 InvalidLength,
19 #[error("The key length is invalid.")]
21 InvalidKeyLength,
22 #[error("The length of the FFI output buffer is invalid.")]
24 InvalidOutputLength,
25 #[error("The signature of the data blob does not match 0x0d0c.")]
27 InvalidSignature,
28 #[error("The MAC is invalid.")]
30 InvalidMac,
31 #[error("The operation cannot be done with this type.")]
33 InvalidDataType,
34 #[error("The data type is unknown.")]
36 UnknownType,
37 #[error("The data subtype is unknown.")]
39 UnknownSubtype,
40 #[error("The data type version is unknown.")]
42 UnknownVersion,
43 #[error("The data is invalid.")]
45 InvalidData,
46 #[error("A null pointer has been passed to the FFI interface.")]
48 NullPointer,
49 #[error("A cryptographic error occurred.")]
51 CryptoError,
52 #[error("An error with the Random Number Generator occurred.")]
54 RandomError,
55 #[error("Generic IO error: {0}")]
57 IoError(#[from] std::io::Error),
58 #[error("There wasn't enough share to regenerate the secret.")]
60 NotEnoughShares,
61 #[error("The version is not the same for all the data.")]
63 InconsistentVersion,
64 #[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 #[error("The mutex is poisoned and cannot be locked")]
71 PoisonedMutex,
72}
73
74impl Error {
75 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}