argon2/
error.rs

1// Copyright (c) 2017 Martijn Rijkeboer <mrr@sru-systems.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use std::{error, fmt};
10
11#[cfg(feature = "serde")]
12use serde::{Deserialize, Serialize};
13
14/// Error type for Argon2 errors.
15#[derive(Clone, Debug, PartialEq)]
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
18pub enum Error {
19    /// The output (hash) is too short (minimum is 4).
20    OutputTooShort,
21
22    /// The output (hash) is too long (maximum is 2^32 - 1).
23    OutputTooLong,
24
25    /// The password is too short (minimum is 0).
26    PwdTooShort,
27
28    /// The password is too long (maximum is 2^32 - 1).
29    PwdTooLong,
30
31    /// The salt is too short (minimum is 8).
32    SaltTooShort,
33
34    /// The salt is too long (maximum is 2^32 - 1).
35    SaltTooLong,
36
37    /// The associated data is too short (minimum is 0).
38    AdTooShort,
39
40    /// The associated data is too long (maximum is 2^32 - 1).
41    AdTooLong,
42
43    /// The secret value is too short (minimum is 0).
44    SecretTooShort,
45
46    /// The secret value is too long (maximum is 2^32 - 1).
47    SecretTooLong,
48
49    /// The time cost (passes) is too small (minimum is 1).
50    TimeTooSmall,
51
52    /// The time cost (passes) is too large (maximum is 2^32 - 1).
53    TimeTooLarge,
54
55    /// The memory cost is too small (minimum is 8 x parallelism).
56    MemoryTooLittle,
57
58    /// The memory cost is too large (maximum 2GiB on 32-bit or 4TiB on 64-bit).
59    MemoryTooMuch,
60
61    /// The number of lanes (parallelism) is too small (minimum is 1).
62    LanesTooFew,
63
64    /// The number of lanes (parallelism) is too large (maximum is 2^24 - 1).
65    LanesTooMany,
66
67    /// Incorrect Argon2 variant.
68    IncorrectType,
69
70    /// Incorrect Argon2 version.
71    IncorrectVersion,
72
73    /// The decoding of the encoded data has failed.
74    DecodingFail,
75}
76
77impl Error {
78    fn msg(&self) -> &str {
79        match *self {
80            Error::OutputTooShort => "Output is too short",
81            Error::OutputTooLong => "Output is too long",
82            Error::PwdTooShort => "Password is too short",
83            Error::PwdTooLong => "Password is too long",
84            Error::SaltTooShort => "Salt is too short",
85            Error::SaltTooLong => "Salt is too long",
86            Error::AdTooShort => "Associated data is too short",
87            Error::AdTooLong => "Associated data is too long",
88            Error::SecretTooShort => "Secret is too short",
89            Error::SecretTooLong => "Secret is too long",
90            Error::TimeTooSmall => "Time cost is too small",
91            Error::TimeTooLarge => "Time cost is too large",
92            Error::MemoryTooLittle => "Memory cost is too small",
93            Error::MemoryTooMuch => "Memory cost is too large",
94            Error::LanesTooFew => "Too few lanes",
95            Error::LanesTooMany => "Too many lanes",
96            Error::IncorrectType => "There is no such type of Argon2",
97            Error::IncorrectVersion => "There is no such version of Argon2",
98            Error::DecodingFail => "Decoding failed",
99        }
100    }
101}
102
103impl fmt::Display for Error {
104    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105        write!(f, "{}", self.msg())
106    }
107}
108
109impl error::Error for Error {
110    fn description(&self) -> &str {
111        self.msg()
112    }
113
114    fn cause(&self) -> Option<&dyn error::Error> {
115        None
116    }
117}
118
119impl From<base64::DecodeError> for Error {
120    fn from(_: base64::DecodeError) -> Self {
121        Error::DecodingFail
122    }
123}