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
#[derive(Debug)]
pub enum CryptoError {
UnsupportedVersion(u8),
OldVersion(u8),
DecryptFailed,
BadLength {
step: &'static str,
actual: usize,
expected: usize,
},
BadKey,
BadFormat(&'static str),
ObjectMismatch(&'static str),
SignatureFailed,
NotSupportedByVault,
}
impl CryptoError {
pub fn serde_err(&self) -> String {
match *self {
CryptoError::UnsupportedVersion(version) => {
format!("crypto version ({}) not supported.", version)
}
CryptoError::OldVersion(version) => {
format!("crypto version ({}) old and deemed unsafe", version)
}
CryptoError::DecryptFailed => "could not decrypt with key".to_string(),
CryptoError::BadLength {
step,
actual,
expected,
} => format!(
"expected data length {}, but got {} on step [{}]",
expected, actual, step
),
CryptoError::BadKey => "crypto key is weak or invalid".to_string(),
CryptoError::BadFormat(s) => format!("format of data does not match spec: {}", s),
CryptoError::ObjectMismatch(s) => format!("object mismatch: {}", s),
CryptoError::SignatureFailed => "signature verification failed".to_string(),
CryptoError::NotSupportedByVault => "vault doesn't support this operation".to_string(),
}
}
}
use std::fmt;
impl fmt::Display for CryptoError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CryptoError::UnsupportedVersion(version) => {
write!(f, "Chosen crypto version ({}) not supported.", version)
}
CryptoError::OldVersion(version) => {
write!(f, "Crypto version ({}) is old and deemed unsafe.", version)
}
CryptoError::DecryptFailed => write!(f, "Could not decrypt with key"),
CryptoError::BadLength {
step,
actual,
expected,
} => write!(
f,
"Expected data length {}, but got {} on step [{}]",
expected, actual, step
),
CryptoError::BadKey => write!(f, "Crypto key is weak or invalid"),
CryptoError::BadFormat(s) => write!(f, "Format of data does not match spec: {}", s),
CryptoError::ObjectMismatch(s) => write!(f, "Object mismatch: {}", s),
CryptoError::SignatureFailed => write!(f, "Signature verification failed"),
CryptoError::NotSupportedByVault => write!(f, "Vault doesn't support this operation"),
}
}
}
impl std::error::Error for CryptoError {}