FAQ - Help and questions

How do I choose Argon2 parameters, and upgrade them later?

#security#passwords#argon2published 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Set memory, passes and parallelism explicitly with Params, starting from the OWASP minimum of 19 MiB, 2 iterations and 1 lane for Argon2id, then tune upward for your hardware. Because the PHC string records the settings, you can detect old hashes at login and replace them.

Checked with rustc 1.93.0, argon2 0.5.3, Windows 11 (release build)

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Argon2 has three cost settings: memory (m, in KiB), number of passes (t) and parallel lanes (p). The OWASP Password Storage Cheat Sheet, read for this thread, lists Argon2id with a minimum of 19 MiB of memory, 2 iterations and 1 degree of parallelism, and advises benchmarking on your own servers and tuning upward while logins stay fast enough. The argon2 crate's default parameters, as printed by the previous thread's program, are the same (m=19456,t=2,p=1).

The code below states the policy explicitly and notices a hash that is weaker than the policy, so it can be replaced the next time that user signs in (you have the plain password only at that moment).

src/main.rsrust
use argon2::{
    password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, SaltString},
    Algorithm, Argon2, Params, Version,
};

// Our current policy: Argon2id, 19 MiB of memory, 2 passes, 1 lane.
fn current() -> Argon2<'static> {
    let params = Params::new(19 * 1024, 2, 1, None).expect("valid parameters");
    Argon2::new(Algorithm::Argon2id, Version::V0x13, params)
}

/// True when a stored hash was made with weaker settings than the policy above.
fn needs_rehash(stored: &str) -> bool {
    let Ok(parsed) = PasswordHash::new(stored) else { return true };
    let Ok(p) = Params::try_from(&parsed) else { return true };
    parsed.algorithm.as_str() != "argon2id" || p.m_cost() < 19 * 1024 || p.t_cost() < 2
}

fn main() {
    let salt = SaltString::generate(&mut OsRng);
    let new = current().hash_password(b"pw", &salt).unwrap().to_string();

    let weak = Params::new(8 * 1024, 1, 1, None).unwrap();
    let old = Argon2::new(Algorithm::Argon2id, Version::V0x13, weak)
        .hash_password(b"pw", &salt).unwrap().to_string();

    println!("new hash starts: {}", &new[..31]);
    println!("old hash starts: {}", &old[..30]);
    println!("new needs rehash: {}", needs_rehash(&new));
    println!("old needs rehash: {}", needs_rehash(&old));
}
text
new hash starts: $argon2id$v=19$m=19456,t=2,p=1$
old hash starts: $argon2id$v=19$m=8192,t=1,p=1$
new needs rehash: false
old needs rehash: true

How to do it#

  1. Write your policy in one function (current() above), so it is changed in one place.
  2. Benchmark on the real server, in a release build, with the number of simultaneous logins you expect. Pick the strongest setting that keeps a login quick and memory use safe under load.
  3. At each successful login, call needs_rehash; if it is true, hash the password again with the current policy and save the new PHC string.
  4. Review the parameters now and then; hardware improves.

Common mistakes#

  • Setting very high memory without thinking about concurrency: 100 logins at the same moment need 100 times the memory.
  • Rehashing without verifying first. Only replace a hash after the password has been checked.
  • Comparing only the memory setting. A hash can also differ in algorithm, so the check above looks at that too.

Why Argon2id and not Argon2i or Argon2d?#

Argon2id is the hybrid variant that the OWASP sheet and RFC 9106 recommend for password hashing.

Can I change the salt length or output length?#

The defaults are fine for passwords. Leave them unless a specification you must follow says otherwise.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet