Skip to content

Commit

Permalink
pbkdf2: adopt OWASP recommendations (#389)
Browse files Browse the repository at this point in the history
Adopts the recommended settings from:

https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html

> use PBKDF2 with a work factor of 600,000 or more and set with an
> internal hash function of HMAC-SHA-256.
  • Loading branch information
tarcieri committed Mar 5, 2023
1 parent 093cd25 commit fbfcda5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pbkdf2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ where
/// use sha2::Sha256;
///
/// let mut buf = [0u8; 20];
/// pbkdf2::<Hmac<Sha256>>(b"password", b"salt", 4096, &mut buf)
/// pbkdf2::<Hmac<Sha256>>(b"password", b"salt", 600_000, &mut buf)
/// .expect("HMAC can be initialized with any key length");
/// assert_eq!(buf, hex!("c5e478d59288c841aa530db6845c4c8d962893a0"));
/// assert_eq!(buf, hex!("669cfe52482116fda1aa2cbe409b2f56c8e45637"));
/// ```
#[inline]
pub fn pbkdf2<PRF>(
Expand Down
25 changes: 24 additions & 1 deletion pbkdf2/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl PasswordHasher for Pbkdf2 {
params: Params,
salt: impl Into<Salt<'a>>,
) -> Result<PasswordHash<'a>> {
let algorithm = Algorithm::try_from(alg_id.unwrap_or(Algorithm::PBKDF2_SHA256_IDENT))?;
let algorithm = Algorithm::try_from(alg_id.unwrap_or(Algorithm::default().ident()))?;

// Versions unsupported
if version.is_some() {
Expand Down Expand Up @@ -79,6 +79,18 @@ pub enum Algorithm {
Pbkdf2Sha512,
}

impl Default for Algorithm {
/// Default suggested by the [OWASP cheat sheet]:
///
/// > Use PBKDF2 with a work factor of 600,000 or more and set with an
/// > internal hash function of HMAC-SHA-256.
///
/// [OWASP cheat sheet]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
fn default() -> Self {
Self::Pbkdf2Sha256
}
}

impl Algorithm {
/// PBKDF2 (SHA-1) algorithm identifier
#[cfg(feature = "sha1")]
Expand Down Expand Up @@ -162,6 +174,17 @@ pub struct Params {
pub output_length: usize,
}

impl Params {
/// Recommended number of PBKDF2 rounds (used by default).
///
/// This number is adopted from the [OWASP cheat sheet]:
///
/// > Use PBKDF2 with a work factor of 600,000 or more
///
/// [OWASP cheat sheet]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
pub const RECOMMENDED_ROUNDS: usize = 600_000;
}

impl Default for Params {
fn default() -> Params {
Params {
Expand Down

0 comments on commit fbfcda5

Please sign in to comment.