| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | use argon2::password_hash::rand_core::OsRng; |
| 14 | use argon2::password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString}; |
| 15 | use argon2::{Algorithm, Argon2, Params, Version}; |
| 16 | |
| 17 | use crate::AuthError; |
| 18 | |
| 19 | |
| 20 | |
| 21 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 22 | pub struct HashParams { |
| 23 | |
| 24 | pub m_cost: u32, |
| 25 | |
| 26 | pub t_cost: u32, |
| 27 | |
| 28 | pub p_cost: u32, |
| 29 | } |
| 30 | |
| 31 | impl Default for HashParams { |
| 32 | |
| 33 | fn default() -> Self { |
| 34 | Self { |
| 35 | m_cost: 19_456, |
| 36 | t_cost: 2, |
| 37 | p_cost: 1, |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl HashParams { |
| 43 | |
| 44 | fn hasher(self) -> Result<Argon2<'static>, AuthError> { |
| 45 | let params = Params::new(self.m_cost, self.t_cost, self.p_cost, None) |
| 46 | .map_err(|e| AuthError::Hash(e.to_string()))?; |
| 47 | Ok(Argon2::new(Algorithm::Argon2id, Version::V0x13, params)) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | pub fn hash_password(password: &str, params: HashParams) -> Result<String, AuthError> { |
| 61 | let salt = SaltString::generate(&mut OsRng); |
| 62 | let hash = params |
| 63 | .hasher()? |
| 64 | .hash_password(password.as_bytes(), &salt) |
| 65 | .map_err(|e| AuthError::Hash(e.to_string()))?; |
| 66 | Ok(hash.to_string()) |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | #[must_use] |
| 79 | pub fn verify_password(password: &str, stored: Option<&str>) -> bool { |
| 80 | let Some(hash) = stored else { |
| 81 | dummy_verify(password); |
| 82 | return false; |
| 83 | }; |
| 84 | let Ok(parsed) = PasswordHash::new(hash) else { |
| 85 | dummy_verify(password); |
| 86 | return false; |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | Argon2::default() |
| 91 | .verify_password(password.as_bytes(), &parsed) |
| 92 | .is_ok() |
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
| 97 | fn dummy_verify(password: &str) { |
| 98 | let _ = hash_password(password, HashParams::default()); |
| 99 | } |
| 100 | |
| 101 | #[cfg(test)] |
| 102 | mod tests { |
| 103 | #![allow(clippy::unwrap_used)] |
| 104 | |
| 105 | use super::*; |
| 106 | |
| 107 | |
| 108 | |
| 109 | const FAST: HashParams = HashParams { |
| 110 | m_cost: 8, |
| 111 | t_cost: 1, |
| 112 | p_cost: 1, |
| 113 | }; |
| 114 | |
| 115 | #[test] |
| 116 | fn hash_is_phc_argon2id_and_verifies() { |
| 117 | let hash = hash_password("correct horse", FAST).unwrap(); |
| 118 | assert!(hash.starts_with("$argon2id$"), "PHC prefix, got {hash}"); |
| 119 | assert!(verify_password("correct horse", Some(&hash))); |
| 120 | assert!(!verify_password("wrong horse", Some(&hash))); |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn hashing_is_salted() { |
| 125 | let a = hash_password("same", FAST).unwrap(); |
| 126 | let b = hash_password("same", FAST).unwrap(); |
| 127 | assert_ne!(a, b, "distinct salts should yield distinct hashes"); |
| 128 | assert!(verify_password("same", Some(&a))); |
| 129 | assert!(verify_password("same", Some(&b))); |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn absent_hash_never_verifies() { |
| 134 | assert!(!verify_password("anything", None)); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn malformed_hash_never_verifies() { |
| 139 | assert!(!verify_password("anything", Some("not-a-phc-string"))); |
| 140 | assert!(!verify_password("anything", Some("$argon2id$garbage"))); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn params_round_trip_through_the_hash() { |
| 145 | |
| 146 | |
| 147 | let hash = hash_password("pw", FAST).unwrap(); |
| 148 | assert!(hash.contains("m=8"), "params embedded, got {hash}"); |
| 149 | assert!(verify_password("pw", Some(&hash))); |
| 150 | } |
| 151 | } |