Coverage Report

Created: 2026-06-30 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/password-hash-0.5.0/src/output.rs
Line
Count
Source
1
//! Outputs from password hashing functions.
2
3
use crate::{Encoding, Error, Result};
4
use core::{
5
    cmp::{Ordering, PartialEq},
6
    fmt,
7
    str::FromStr,
8
};
9
use subtle::{Choice, ConstantTimeEq};
10
11
/// Output from password hashing functions, i.e. the "hash" or "digest"
12
/// as raw bytes.
13
///
14
/// The [`Output`] type implements the RECOMMENDED best practices described in
15
/// the [PHC string format specification][1], namely:
16
///
17
/// > The hash output, for a verification, must be long enough to make preimage
18
/// > attacks at least as hard as password guessing. To promote wide acceptance,
19
/// > a default output size of 256 bits (32 bytes, encoded as 43 characters) is
20
/// > recommended. Function implementations SHOULD NOT allow outputs of less
21
/// > than 80 bits to be used for password verification.
22
///
23
/// # Recommended length
24
/// Per the description above, the recommended default length for an [`Output`]
25
/// of a password hashing function is **32-bytes** (256-bits).
26
///
27
/// # Constraints
28
/// The above guidelines are interpreted into the following constraints:
29
///
30
/// - Minimum length: **10**-bytes (80-bits)
31
/// - Maximum length: **64**-bytes (512-bits)
32
///
33
/// The specific recommendation of a 64-byte maximum length is taken as a best
34
/// practice from the hash output guidelines for [Argon2 Encoding][2] given in
35
/// the same document:
36
///
37
/// > The hash output...length shall be between 12 and 64 bytes (16 and 86
38
/// > characters, respectively). The default output length is 32 bytes
39
/// > (43 characters).
40
///
41
/// Based on this guidance, this type enforces an upper bound of 64-bytes
42
/// as a reasonable maximum, and recommends using 32-bytes.
43
///
44
/// # Constant-time comparisons
45
/// The [`Output`] type impls the [`ConstantTimeEq`] trait from the [`subtle`]
46
/// crate and uses it to perform constant-time comparisons.
47
///
48
/// Additionally the [`PartialEq`] and [`Eq`] trait impls for [`Output`] use
49
/// [`ConstantTimeEq`] when performing comparisons.
50
///
51
/// ## Attacks on non-constant-time password hash comparisons
52
/// Comparing password hashes in constant-time is known to mitigate at least
53
/// one [poorly understood attack][3] involving an adversary with the following
54
/// knowledge/capabilities:
55
///
56
/// - full knowledge of what password hashing algorithm is being used
57
///   including any relevant configurable parameters
58
/// - knowledge of the salt for a particular victim
59
/// - ability to accurately measure a timing side-channel on comparisons
60
///   of the password hash over the network
61
///
62
/// An attacker with the above is able to perform an offline computation of
63
/// the hash for any chosen password in such a way that it will match the
64
/// hash computed by the server.
65
///
66
/// As noted above, they also measure timing variability in the server's
67
/// comparison of the hash it computes for a given password and a target hash
68
/// the attacker is trying to learn.
69
///
70
/// When the attacker observes a hash comparison that takes longer than their
71
/// previous attempts, they learn that they guessed another byte in the
72
/// password hash correctly. They can leverage repeated measurements and
73
/// observations with different candidate passwords to learn the password
74
/// hash a byte-at-a-time in a manner similar to other such timing side-channel
75
/// attacks.
76
///
77
/// The attack may seem somewhat counterintuitive since learning prefixes of a
78
/// password hash does not reveal any additional information about the password
79
/// itself. However, the above can be combined with an offline dictionary
80
/// attack where the attacker is able to determine candidate passwords to send
81
/// to the server by performing a brute force search offline and selecting
82
/// candidate passwords whose hashes match the portion of the prefix they have
83
/// learned so far.
84
///
85
/// As the attacker learns a longer and longer prefix of the password hash,
86
/// they are able to more effectively eliminate candidate passwords offline as
87
/// part of a dictionary attack, until they eventually guess the correct
88
/// password or exhaust their set of candidate passwords.
89
///
90
/// ## Mitigations
91
/// While we have taken care to ensure password hashes are compared in constant
92
/// time, we would also suggest preventing such attacks by using randomly
93
/// generated salts and keeping those salts secret.
94
///
95
/// The [`SaltString::generate`][`crate::SaltString::generate`] function can be
96
/// used to generate random high-entropy salt values.
97
///
98
/// [1]: https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md#function-duties
99
/// [2]: https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md#argon2-encoding
100
/// [3]: https://web.archive.org/web/20130208100210/http://security-assessment.com/files/documents/presentations/TimingAttackPresentation2012.pdf
101
#[derive(Copy, Clone, Eq)]
102
pub struct Output {
103
    /// Byte array containing a password hashing function output.
104
    bytes: [u8; Self::MAX_LENGTH],
105
106
    /// Length of the password hashing function output in bytes.
107
    length: u8,
108
109
    /// Encoding which output should be serialized with.
110
    encoding: Encoding,
111
}
112
113
#[allow(clippy::len_without_is_empty)]
114
impl Output {
115
    /// Minimum length of a [`Output`] string: 10-bytes.
116
    pub const MIN_LENGTH: usize = 10;
117
118
    /// Maximum length of [`Output`] string: 64-bytes.
119
    ///
120
    /// See type-level documentation about [`Output`] for more information.
121
    pub const MAX_LENGTH: usize = 64;
122
123
    /// Maximum length of [`Output`] when encoded as B64 string: 86-bytes
124
    /// (i.e. 86 ASCII characters)
125
    pub const B64_MAX_LENGTH: usize = ((Self::MAX_LENGTH * 4) / 3) + 1;
126
127
    /// Create a [`Output`] from the given byte slice, validating it according
128
    /// to [`Output::MIN_LENGTH`] and [`Output::MAX_LENGTH`] restrictions.
129
0
    pub fn new(input: &[u8]) -> Result<Self> {
130
0
        Self::init_with(input.len(), |bytes| {
131
0
            bytes.copy_from_slice(input);
132
0
            Ok(())
133
0
        })
134
0
    }
135
136
    /// Create a [`Output`] from the given byte slice and [`Encoding`],
137
    /// validating it according to [`Output::MIN_LENGTH`] and
138
    /// [`Output::MAX_LENGTH`] restrictions.
139
0
    pub fn new_with_encoding(input: &[u8], encoding: Encoding) -> Result<Self> {
140
0
        let mut result = Self::new(input)?;
141
0
        result.encoding = encoding;
142
0
        Ok(result)
143
0
    }
144
145
    /// Initialize an [`Output`] using the provided method, which is given
146
    /// a mutable byte slice into which it should write the output.
147
    ///
148
    /// The `output_size` (in bytes) must be known in advance, as well as at
149
    /// least [`Output::MIN_LENGTH`] bytes and at most [`Output::MAX_LENGTH`]
150
    /// bytes.
151
0
    pub fn init_with<F>(output_size: usize, f: F) -> Result<Self>
152
0
    where
153
0
        F: FnOnce(&mut [u8]) -> Result<()>,
154
    {
155
0
        if output_size < Self::MIN_LENGTH {
156
0
            return Err(Error::OutputSize {
157
0
                provided: Ordering::Less,
158
0
                expected: Self::MIN_LENGTH,
159
0
            });
160
0
        }
161
162
0
        if output_size > Self::MAX_LENGTH {
163
0
            return Err(Error::OutputSize {
164
0
                provided: Ordering::Greater,
165
0
                expected: Self::MAX_LENGTH,
166
0
            });
167
0
        }
168
169
0
        let mut bytes = [0u8; Self::MAX_LENGTH];
170
0
        f(&mut bytes[..output_size])?;
171
172
0
        Ok(Self {
173
0
            bytes,
174
0
            length: output_size as u8,
175
0
            encoding: Encoding::default(),
176
0
        })
177
0
    }
Unexecuted instantiation: <password_hash::output::Output>::init_with::<<scrypt::simple::Scrypt as password_hash::traits::PasswordHasher>::hash_password_customized<password_hash::salt::Salt>::{closure#0}>
Unexecuted instantiation: <password_hash::output::Output>::init_with::<<scrypt::simple::Scrypt as password_hash::traits::PasswordHasher>::hash_password_customized<&password_hash::salt::SaltString>::{closure#0}>
Unexecuted instantiation: <password_hash::output::Output>::init_with::<<pbkdf2::simple::Pbkdf2 as password_hash::traits::PasswordHasher>::hash_password_customized<password_hash::salt::Salt>::{closure#0}>
Unexecuted instantiation: <password_hash::output::Output>::init_with::<<pbkdf2::simple::Pbkdf2 as password_hash::traits::PasswordHasher>::hash_password_customized<&password_hash::salt::SaltString>::{closure#0}>
Unexecuted instantiation: <password_hash::output::Output>::init_with::<<argon2::Argon2 as password_hash::traits::PasswordHasher>::hash_password<password_hash::salt::Salt>::{closure#0}>
Unexecuted instantiation: <password_hash::output::Output>::init_with::<<argon2::Argon2 as password_hash::traits::PasswordHasher>::hash_password<&password_hash::salt::SaltString>::{closure#0}>
Unexecuted instantiation: <password_hash::output::Output>::init_with::<<password_hash::output::Output>::new::{closure#0}>
178
179
    /// Borrow the output value as a byte slice.
180
0
    pub fn as_bytes(&self) -> &[u8] {
181
0
        &self.bytes[..self.len()]
182
0
    }
183
184
    /// Get the [`Encoding`] that this [`Output`] is serialized with.
185
0
    pub fn encoding(&self) -> Encoding {
186
0
        self.encoding
187
0
    }
188
189
    /// Get the length of the output value as a byte slice.
190
0
    pub fn len(&self) -> usize {
191
0
        usize::from(self.length)
192
0
    }
193
194
    /// Parse B64-encoded [`Output`], i.e. using the PHC string
195
    /// specification's restricted interpretation of Base64.
196
0
    pub fn b64_decode(input: &str) -> Result<Self> {
197
0
        Self::decode(input, Encoding::B64)
198
0
    }
199
200
    /// Write B64-encoded [`Output`] to the provided buffer, returning
201
    /// a sub-slice containing the encoded data.
202
    ///
203
    /// Returns an error if the buffer is too short to contain the output.
204
0
    pub fn b64_encode<'a>(&self, out: &'a mut [u8]) -> Result<&'a str> {
205
0
        self.encode(out, Encoding::B64)
206
0
    }
207
208
    /// Decode the given input string using the specified [`Encoding`].
209
0
    pub fn decode(input: &str, encoding: Encoding) -> Result<Self> {
210
0
        let mut bytes = [0u8; Self::MAX_LENGTH];
211
0
        encoding
212
0
            .decode(input, &mut bytes)
213
0
            .map_err(Into::into)
214
0
            .and_then(|decoded| Self::new_with_encoding(decoded, encoding))
215
0
    }
216
217
    /// Encode this [`Output`] using the specified [`Encoding`].
218
0
    pub fn encode<'a>(&self, out: &'a mut [u8], encoding: Encoding) -> Result<&'a str> {
219
0
        Ok(encoding.encode(self.as_ref(), out)?)
220
0
    }
221
222
    /// Get the length of this [`Output`] when encoded as B64.
223
0
    pub fn b64_len(&self) -> usize {
224
0
        Encoding::B64.encoded_len(self.as_ref())
225
0
    }
226
}
227
228
impl AsRef<[u8]> for Output {
229
0
    fn as_ref(&self) -> &[u8] {
230
0
        self.as_bytes()
231
0
    }
232
}
233
234
impl ConstantTimeEq for Output {
235
0
    fn ct_eq(&self, other: &Self) -> Choice {
236
0
        self.as_ref().ct_eq(other.as_ref())
237
0
    }
238
}
239
240
impl FromStr for Output {
241
    type Err = Error;
242
243
0
    fn from_str(s: &str) -> Result<Self> {
244
0
        Self::b64_decode(s)
245
0
    }
246
}
247
248
impl PartialEq for Output {
249
0
    fn eq(&self, other: &Self) -> bool {
250
0
        self.ct_eq(other).into()
251
0
    }
252
}
253
254
impl TryFrom<&[u8]> for Output {
255
    type Error = Error;
256
257
0
    fn try_from(input: &[u8]) -> Result<Output> {
258
0
        Self::new(input)
259
0
    }
260
}
261
262
impl fmt::Display for Output {
263
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
264
0
        let mut buffer = [0u8; Self::B64_MAX_LENGTH];
265
0
        self.encode(&mut buffer, self.encoding)
266
0
            .map_err(|_| fmt::Error)
267
0
            .and_then(|encoded| f.write_str(encoded))
268
0
    }
269
}
270
271
impl fmt::Debug for Output {
272
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
273
0
        write!(f, "Output(\"{}\")", self)
274
0
    }
275
}
276
277
#[cfg(test)]
278
mod tests {
279
    use super::{Error, Ordering, Output};
280
281
    #[test]
282
    fn new_with_valid_min_length_input() {
283
        let bytes = [10u8; 10];
284
        let output = Output::new(&bytes).unwrap();
285
        assert_eq!(output.as_ref(), &bytes);
286
    }
287
288
    #[test]
289
    fn new_with_valid_max_length_input() {
290
        let bytes = [64u8; 64];
291
        let output = Output::new(&bytes).unwrap();
292
        assert_eq!(output.as_ref(), &bytes);
293
    }
294
295
    #[test]
296
    fn reject_new_too_short() {
297
        let bytes = [9u8; 9];
298
        let err = Output::new(&bytes).err().unwrap();
299
        assert_eq!(
300
            err,
301
            Error::OutputSize {
302
                provided: Ordering::Less,
303
                expected: Output::MIN_LENGTH
304
            }
305
        );
306
    }
307
308
    #[test]
309
    fn reject_new_too_long() {
310
        let bytes = [65u8; 65];
311
        let err = Output::new(&bytes).err().unwrap();
312
        assert_eq!(
313
            err,
314
            Error::OutputSize {
315
                provided: Ordering::Greater,
316
                expected: Output::MAX_LENGTH
317
            }
318
        );
319
    }
320
321
    #[test]
322
    fn partialeq_true() {
323
        let a = Output::new(&[1u8; 32]).unwrap();
324
        let b = Output::new(&[1u8; 32]).unwrap();
325
        assert_eq!(a, b);
326
    }
327
328
    #[test]
329
    fn partialeq_false() {
330
        let a = Output::new(&[1u8; 32]).unwrap();
331
        let b = Output::new(&[2u8; 32]).unwrap();
332
        assert_ne!(a, b);
333
    }
334
}