1use thiserror::Error as ThisError;
2
3#[derive(ThisError, Debug)]
4pub enum Error {
5 #[error("Argon2 error: {0}")]
6 Argon2(#[from] Argon2Error),
7}
8
9#[derive(ThisError, Debug, Copy, Clone, Eq, PartialEq)]
10pub enum Argon2Error {
11 #[error("Output pointer is null")]
12 OutputPtrNull,
13 #[error("Output is too short")]
14 OutputTooShort,
15 #[error("Output is too long")]
16 OutputTooLong,
17 #[error("Password is too short")]
18 PasswordTooShort,
19 #[error("Password is too long")]
20 PasswordTooLong,
21 #[error("Salt is too short")]
22 SaltTooShort,
23 #[error("Salt is too long")]
24 SaltTooLong,
25 #[error("Associated data is too short")]
26 AdTooShort,
27 #[error("Associated data is too long")]
28 AdTooLong,
29 #[error("Secret is too short")]
30 SecretTooShort,
31 #[error("Secret is too long")]
32 SecretTooLong,
33 #[error("Time cost is too small")]
34 TimeTooSmall,
35 #[error("Time cost is too large")]
36 TimeTooLarge,
37 #[error("Memory cost is too little")]
38 MemoryTooLittle,
39 #[error("Memory cost is too much")]
40 MemoryTooMuch,
41 #[error("Number of lanes is too few")]
42 LanesTooFew,
43 #[error("Number of lanes is too many")]
44 LanesTooMany,
45 #[error("Password pointer mismatch")]
46 PwdPtrMismatch,
47 #[error("Salt pointer mismatch")]
48 SaltPtrMismatch,
49 #[error("Secret pointer mismatch")]
50 SecretPtrMismatch,
51 #[error("Associated data pointer mismatch")]
52 AdPtrMismatch,
53 #[error("Memory allocation error")]
54 MemoryAllocationError,
55 #[error("Free memory callback is null")]
56 FreeMemoryCbkNull,
57 #[error("Allocate memory callback is null")]
58 AllocateMemoryCbkNull,
59 #[error("Incorrect parameter")]
60 IncorrectParameter,
61 #[error("Incorrect Argon2 type")]
62 IncorrectType,
63 #[error("Output pointer mismatch")]
64 OutPtrMismatch,
65 #[error("Number of threads is too few")]
66 ThreadsTooFew,
67 #[error("Number of threads is too many")]
68 ThreadsTooMany,
69 #[error("Missing arguments")]
70 MissingArgs,
71 #[error("Encoding failed")]
72 EncodingFail,
73 #[error("Decoding failed")]
74 DecodingFail,
75 #[error("Thread failed")]
76 ThreadFail,
77 #[error("Decoding length failed")]
78 DecodingLengthFail,
79 #[error("Verification mismatch")]
80 VerifyMismatch,
81 #[error("Unknown argon2 error with code: {0}")]
82 Unknown(i32),
83}
84
85pub(crate) fn map_argon2_error(code: i32) -> Argon2Error {
86 match code {
87 -1 => Argon2Error::OutputPtrNull,
88 -2 => Argon2Error::OutputTooShort,
89 -3 => Argon2Error::OutputTooLong,
90 -4 => Argon2Error::PasswordTooShort,
91 -5 => Argon2Error::PasswordTooLong,
92 -6 => Argon2Error::SaltTooShort,
93 -7 => Argon2Error::SaltTooLong,
94 -8 => Argon2Error::AdTooShort,
95 -9 => Argon2Error::AdTooLong,
96 -10 => Argon2Error::SecretTooShort,
97 -11 => Argon2Error::SecretTooLong,
98 -12 => Argon2Error::TimeTooSmall,
99 -13 => Argon2Error::TimeTooLarge,
100 -14 => Argon2Error::MemoryTooLittle,
101 -15 => Argon2Error::MemoryTooMuch,
102 -16 => Argon2Error::LanesTooFew,
103 -17 => Argon2Error::LanesTooMany,
104 -18 => Argon2Error::PwdPtrMismatch,
105 -19 => Argon2Error::SaltPtrMismatch,
106 -20 => Argon2Error::SecretPtrMismatch,
107 -21 => Argon2Error::AdPtrMismatch,
108 -22 => Argon2Error::MemoryAllocationError,
109 -23 => Argon2Error::FreeMemoryCbkNull,
110 -24 => Argon2Error::AllocateMemoryCbkNull,
111 -25 => Argon2Error::IncorrectParameter,
112 -26 => Argon2Error::IncorrectType,
113 -27 => Argon2Error::OutPtrMismatch,
114 -28 => Argon2Error::ThreadsTooFew,
115 -29 => Argon2Error::ThreadsTooMany,
116 -30 => Argon2Error::MissingArgs,
117 -31 => Argon2Error::EncodingFail,
118 -32 => Argon2Error::DecodingFail,
119 -33 => Argon2Error::ThreadFail,
120 -34 => Argon2Error::DecodingLengthFail,
121 -35 => Argon2Error::VerifyMismatch,
122 _ => Argon2Error::Unknown(code),
123 }
124}