argon2/
error.rs

1//! Error type
2
3use core::fmt;
4
5#[cfg(feature = "password-hash")]
6use {crate::Params, core::cmp::Ordering, password_hash::errors::InvalidValue};
7
8/// Result with argon2's [`Error`] type.
9pub type Result<T> = core::result::Result<T, Error>;
10
11/// Error type.
12#[derive(Copy, Clone, Debug, Eq, PartialEq)]
13pub enum Error {
14    /// Associated data is too long.
15    AdTooLong,
16
17    /// Algorithm identifier invalid.
18    AlgorithmInvalid,
19
20    /// "B64" encoding is invalid.
21    B64Encoding(base64ct::Error),
22
23    /// Key ID is too long.
24    KeyIdTooLong,
25
26    /// Memory cost is too small.
27    MemoryTooLittle,
28
29    /// Memory cost is too large.
30    MemoryTooMuch,
31
32    /// Output is too short.
33    OutputTooShort,
34
35    /// Output is too long.
36    OutputTooLong,
37
38    /// Password is too long.
39    PwdTooLong,
40
41    /// Salt is too short.
42    SaltTooShort,
43
44    /// Salt is too long.
45    SaltTooLong,
46
47    /// Secret is too long.
48    SecretTooLong,
49
50    /// Not enough threads.
51    ThreadsTooFew,
52
53    /// Too many threads.
54    ThreadsTooMany,
55
56    /// Time cost is too small.
57    TimeTooSmall,
58
59    /// Invalid version
60    VersionInvalid,
61}
62
63impl fmt::Display for Error {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        f.write_str(match self {
66            Error::AdTooLong => "associated data is too long",
67            Error::AlgorithmInvalid => "algorithm identifier invalid",
68            Error::B64Encoding(inner) => return write!(f, "B64 encoding invalid: {inner}"),
69            Error::KeyIdTooLong => "key ID is too long",
70            Error::MemoryTooLittle => "memory cost is too small",
71            Error::MemoryTooMuch => "memory cost is too large",
72            Error::OutputTooShort => "output is too short",
73            Error::OutputTooLong => "output is too long",
74            Error::PwdTooLong => "password is too long",
75            Error::SaltTooShort => "salt is too short",
76            Error::SaltTooLong => "salt is too long",
77            Error::SecretTooLong => "secret is too long",
78            Error::ThreadsTooFew => "not enough threads",
79            Error::ThreadsTooMany => "too many threads",
80            Error::TimeTooSmall => "time cost is too small",
81            Error::VersionInvalid => "invalid version",
82        })
83    }
84}
85
86impl From<base64ct::Error> for Error {
87    fn from(err: base64ct::Error) -> Error {
88        Error::B64Encoding(err)
89    }
90}
91
92#[cfg(feature = "password-hash")]
93#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
94impl From<Error> for password_hash::Error {
95    fn from(err: Error) -> password_hash::Error {
96        match err {
97            Error::AdTooLong => InvalidValue::TooLong.param_error(),
98            Error::AlgorithmInvalid => password_hash::Error::Algorithm,
99            Error::B64Encoding(inner) => password_hash::Error::B64Encoding(inner),
100            Error::KeyIdTooLong => InvalidValue::TooLong.param_error(),
101            Error::MemoryTooLittle => InvalidValue::TooShort.param_error(),
102            Error::MemoryTooMuch => InvalidValue::TooLong.param_error(),
103            Error::PwdTooLong => password_hash::Error::Password,
104            Error::OutputTooShort => password_hash::Error::OutputSize {
105                provided: Ordering::Less,
106                expected: Params::MIN_OUTPUT_LEN,
107            },
108            Error::OutputTooLong => password_hash::Error::OutputSize {
109                provided: Ordering::Greater,
110                expected: Params::MAX_OUTPUT_LEN,
111            },
112            Error::SaltTooShort => InvalidValue::TooShort.salt_error(),
113            Error::SaltTooLong => InvalidValue::TooLong.salt_error(),
114            Error::SecretTooLong => InvalidValue::TooLong.param_error(),
115            Error::ThreadsTooFew => InvalidValue::TooShort.param_error(),
116            Error::ThreadsTooMany => InvalidValue::TooLong.param_error(),
117            Error::TimeTooSmall => InvalidValue::TooShort.param_error(),
118            Error::VersionInvalid => password_hash::Error::Version,
119        }
120    }
121}
122
123#[cfg(feature = "std")]
124impl std::error::Error for Error {
125    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
126        match self {
127            Self::B64Encoding(err) => Some(err),
128            _ => None,
129        }
130    }
131}