ncrypt_me/
error.rs

1use thiserror::Error as ThisError;
2
3#[derive(ThisError, Debug)]
4pub enum Error {
5   #[error("Hash Length cannot be less than 32 bytes")]
6   HashLength,
7
8   #[error("Header not found, data corrupted?")]
9   InvalidFileFormat,
10
11   #[error("Could not parse EncryptedInfo length")]
12   EncryptedInfo,
13
14   #[error("Invalid Credentials {0}")]
15   InvalidCredentials(String),
16
17   #[error("EncrytedInfo Encoding Failed {0}")]
18   EncodingFailed(String),
19
20   #[error("Encryption Failed {0}")]
21   EncryptionFailed(String),
22
23   #[error("EncryptedInfo Decoding Failed {0}")]
24   DecodingFailed(String),
25
26   #[error("Decryption Failed {0}")]
27   DecryptionFailed(String),
28
29   #[error("Failed to read file {0}")]
30   FileReadFailed(String),
31
32   #[error("Argon2 error: {0}")]
33   Argon2(#[from] Argon2Error),
34
35   #[error("{0}")]
36   Credentials(#[from] CredentialsError),
37
38   #[error("{0}")]
39   Custom(String),
40}
41
42#[derive(ThisError, Debug, Copy, Clone, Eq, PartialEq)]
43pub enum Argon2Error {
44    #[error("Output pointer is null")]
45    OutputPtrNull,
46    #[error("Output is too short")]
47    OutputTooShort,
48    #[error("Output is too long")]
49    OutputTooLong,
50    #[error("Password is too short")]
51    PasswordTooShort,
52    #[error("Password is too long")]
53    PasswordTooLong,
54    #[error("Salt is too short")]
55    SaltTooShort,
56    #[error("Salt is too long")]
57    SaltTooLong,
58    #[error("Associated data is too short")]
59    AdTooShort,
60    #[error("Associated data is too long")]
61    AdTooLong,
62    #[error("Secret is too short")]
63    SecretTooShort,
64    #[error("Secret is too long")]
65    SecretTooLong,
66    #[error("Time cost is too small")]
67    TimeTooSmall,
68    #[error("Time cost is too large")]
69    TimeTooLarge,
70    #[error("Memory cost is too little")]
71    MemoryTooLittle,
72    #[error("Memory cost is too much")]
73    MemoryTooMuch,
74    #[error("Number of lanes is too few")]
75    LanesTooFew,
76    #[error("Number of lanes is too many")]
77    LanesTooMany,
78    #[error("Password pointer mismatch")]
79    PwdPtrMismatch,
80    #[error("Salt pointer mismatch")]
81    SaltPtrMismatch,
82    #[error("Secret pointer mismatch")]
83    SecretPtrMismatch,
84    #[error("Associated data pointer mismatch")]
85    AdPtrMismatch,
86    #[error("Memory allocation error")]
87    MemoryAllocationError,
88    #[error("Free memory callback is null")]
89    FreeMemoryCbkNull,
90    #[error("Allocate memory callback is null")]
91    AllocateMemoryCbkNull,
92    #[error("Incorrect parameter")]
93    IncorrectParameter,
94    #[error("Incorrect Argon2 type")]
95    IncorrectType,
96    #[error("Output pointer mismatch")]
97    OutPtrMismatch,
98    #[error("Number of threads is too few")]
99    ThreadsTooFew,
100    #[error("Number of threads is too many")]
101    ThreadsTooMany,
102    #[error("Missing arguments")]
103    MissingArgs,
104    #[error("Encoding failed")]
105    EncodingFail,
106    #[error("Decoding failed")]
107    DecodingFail,
108    #[error("Thread failed")]
109    ThreadFail,
110    #[error("Decoding length failed")]
111    DecodingLengthFail,
112    #[error("Verification mismatch")]
113    VerifyMismatch,
114    #[error("Unknown argon2 error with code: {0}")]
115    Unknown(i32)
116}
117
118#[derive(ThisError, Debug)]
119pub enum CredentialsError {
120   #[error("Username is empty")]
121   UsernameEmpty,
122
123   #[error("Password is empty")]
124   PasswordEmpty,
125
126   #[error("Confirm password is empty")]
127   ConfirmPasswordEmpty,
128
129   #[error("Passwords do not match")]
130   PasswordsDoNotMatch,
131
132   #[error("{0}")]
133   Custom(String),
134}
135
136
137pub(crate) fn map_argon2_error(code: i32) -> Argon2Error {
138    match code {
139        -1 => Argon2Error::OutputPtrNull,
140        -2 => Argon2Error::OutputTooShort,
141        -3 => Argon2Error::OutputTooLong,
142        -4 => Argon2Error::PasswordTooShort,
143        -5 => Argon2Error::PasswordTooLong,
144        -6 => Argon2Error::SaltTooShort,
145        -7 => Argon2Error::SaltTooLong,
146        -8 => Argon2Error::AdTooShort,
147        -9 => Argon2Error::AdTooLong,
148        -10 => Argon2Error::SecretTooShort,
149        -11 => Argon2Error::SecretTooLong,
150        -12 => Argon2Error::TimeTooSmall,
151        -13 => Argon2Error::TimeTooLarge,
152        -14 => Argon2Error::MemoryTooLittle,
153        -15 => Argon2Error::MemoryTooMuch,
154        -16 => Argon2Error::LanesTooFew,
155        -17 => Argon2Error::LanesTooMany,
156        -18 => Argon2Error::PwdPtrMismatch,
157        -19 => Argon2Error::SaltPtrMismatch,
158        -20 => Argon2Error::SecretPtrMismatch,
159        -21 => Argon2Error::AdPtrMismatch,
160        -22 => Argon2Error::MemoryAllocationError,
161        -23 => Argon2Error::FreeMemoryCbkNull,
162        -24 => Argon2Error::AllocateMemoryCbkNull,
163        -25 => Argon2Error::IncorrectParameter,
164        -26 => Argon2Error::IncorrectType,
165        -27 => Argon2Error::OutPtrMismatch,
166        -28 => Argon2Error::ThreadsTooFew,
167        -29 => Argon2Error::ThreadsTooMany,
168        -30 => Argon2Error::MissingArgs,
169        -31 => Argon2Error::EncodingFail,
170        -32 => Argon2Error::DecodingFail,
171        -33 => Argon2Error::ThreadFail,
172        -34 => Argon2Error::DecodingLengthFail,
173        -35 => Argon2Error::VerifyMismatch,
174        _ => Argon2Error::Unknown(code),
175    }
176}