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
use aes_gcm::aead;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum EncryptionError {
    #[error("failed to encrypt payload")]
    AesEncryption,
    #[error("failed to generate data key")]
    KeyGeneration(#[from] KeyGenerationError),
    #[error("failed to generate random bytes")]
    RngGeneration(#[from] rand::Error),
    #[error("an unknown encryption error ocurred")]
    Unknown,
}

impl Default for EncryptionError {
    fn default() -> Self {
        Self::Unknown
    }
}

impl From<aead::Error> for EncryptionError {
    fn from(_: aead::Error) -> Self {
        Self::AesEncryption
    }
}

#[derive(Error, Debug)]
pub enum DecryptionError {
    #[error("failed to decrypt payload")]
    AesDecryption,
    #[error("failed to decrypt data key")]
    KeyDecryption(#[from] KeyDecryptionError),
    #[error("an unknown decryption error ocurred")]
    Unknown,
}

impl Default for DecryptionError {
    fn default() -> Self {
        Self::Unknown
    }
}

impl From<aead::Error> for DecryptionError {
    fn from(_: aead::Error) -> Self {
        Self::AesDecryption
    }
}

#[derive(Debug, Error)]
pub enum KeyGenerationError {
    #[error("failed to generate random bytes")]
    RngGeneration(#[from] rand::Error),
    #[error("failed to encrypt key payload")]
    AesEncryption,
    #[error("{0}")]
    Other(String),
    #[error("an unknown key generation error ocurred")]
    Unknown,
}

impl From<aead::Error> for KeyGenerationError {
    fn from(_: aead::Error) -> Self {
        Self::AesEncryption
    }
}

impl Default for KeyGenerationError {
    fn default() -> Self {
        Self::Unknown
    }
}

#[derive(Error, Debug)]
pub enum KeyDecryptionError {
    #[error("failed to decrypt key")]
    AesDecryption,
    #[error("{0}")]
    Other(String),
    #[error("an unknown key decryption error ocurred")]
    Unknown,
}

impl From<aead::Error> for KeyDecryptionError {
    fn from(_: aead::Error) -> Self {
        Self::AesDecryption
    }
}

impl Default for KeyDecryptionError {
    fn default() -> Self {
        Self::Unknown
    }
}