alexedwards / argon2id

Argon2id password hashing and verification for Go
MIT License
452 stars 44 forks source link

Question about default parameters #6

Closed bojanz closed 4 years ago

bojanz commented 4 years ago

Thank you for this package, it's just what I needed.

Since forever I've relied on bcrypt's simplicity, where there's only one knob to turn, and the default value (10 in Go and PHP) seems sufficient. Argon2 introduces a whole set of knobs, and the defaults across languages vary significantly.

So, I was wondering how the DefaultParams were chosen, and whether the package should provide more guidance on expected defaults.

  1. Parallelism Would it be safe to recommend always setting this to at least uint8(runtime.NumCPU())?

  2. Iterations My confusion here was regarding the different defaults between this package, the underlying golang package (x/crypto/argon2), and libsodium.

This package defaults to t=3. libsodium defaults to 2 for the interactive preset, which is also used as the default for PHP's password_hash().

The /x/crypto/argon2 docs for argon2id say:

The draft RFC recommends[2] time=1, and memory=64*1024 is a sensible number. If using that amount of memory (64 MB) is not possible in some contexts then the time parameter can be increased to compensate.

The RFC says:

   The attack cost estimates from [AB16] imply that for Argon2i, 3 passes is 
   almost optimal for the most of reasonable memory sizes, and that for Argon2d
   and Argon2id, 1 pass maximizes the attack costs for the constant
   defender time.

   The Argon2id variant with t=1 and maximum available memory is
   RECOMMENDED as a default setting for all environments.  This setting
   is secure against side-channel attacks and maximizes adversarial
   costs on dedicated bruteforce hardware.

I see the same text in the most recent version: https://tools.ietf.org/html/draft-irtf-cfrg-argon2-10#section-8.3

So, the question here is whether it makes sense to follow the spec in defaulting to 1, and placing emphasis on increasing memory (as the primary hardening parameter)? Should we always aim to make the hashing process as slow as possible, or is there a point at which the result is good enough?

(Benchmarks on my MBP show t=1 with 64M and 4 threads taking around 30ms, t=2 taking around 60ms. bcrypt with 10 rounds taking ~70ms)

alexedwards commented 4 years ago

Hi,

Sure, I'd be happy to discuss changing the default parameters. If we can determine better ones then I am all for it.

Would it be safe to recommend always setting this to at least uint8(runtime.NumCPU())

I would guess that most machines running this are doing other things too, like serving some form of web application/API. So they need to still be able to do other useful work at the same time as hashing a password. For that reason, I'm not sure if using all CPUs would be a good default.

Maybe min(1, floor(numCPU/2)) would be a better default than the current default of 2? The only thing that I feel uncomfortable about with that is that the default parallelism value is then dynamic, and that could potentially cause issues/confusion when running the same code in different environments.

With regard to the number of iterations, it would probably be sensible to align with the RFC recommendations and the /x/crypto/argon2 docs and default this to 1. I think the reason that I set it higher in the defaults is because I found in benchmarking that the hashing tended to complete quite quickly, even when bound to 2 CPUs, and guessed that most applications could afford a longer delay.

Should we always aim to make the hashing process as slow as possible, or is there a point at which the result is good enough?

It's worth bearing in mind that slow is not necessarily the same thing as expensive. Potentially, a bcrypt hash may take longer to create than a argon2 hash, but it doesn't mean that it is necessarily less expensive (due to the argon2 memory and parallelism requirements).

More expensive is better, especially when trying to defend against an offline attack (weak/common passwords will still be broken, even with a very expensive hashing algorithm), but this has to be balanced against other requirements like signup/login calls not taking too long to complete.

I think it might be worth adding a stronger note to the documentation for the default parameters. Something like:

The default parameters should generally be used for development/testing purposes only. Custom parameters should be set for production applications depending on available memory/CPU resources and business requirements.

bojanz commented 4 years ago

Thanks for the clarifications! I agree that it doesn't make sense to make the parallelism default dynamic, that's better left for the parent application. The confusing part about 3 iterations was just that the comments didn't explain why that specific default was chosen.

So, the proposed changes would be: 1) Document the "between 1 and numCPU" guideline for parallelism, keeping the current default (2). 2) Document that the primary knob to turn is memory. 3) Modify the default iterations to 1, documenting that it was chosen because it is recommended by the spec (quoting the relevant part perhaps), or to 2 (if we want more expensive defaults), documenting that it was chosen to match libsodium as a more expensive alternative to the spec recommendation.

alexedwards commented 4 years ago

Those changes sound sensible to me. For point 3, let's use 1 iteration and quote the spec.

As a point 4, like I mentioned above, I think we should a warning to the default parameter similar to this:

The default parameters should generally be used for development/testing purposes only. Custom parameters should be set for production applications depending on available memory/CPU resources and business requirements.

Would you like to make the changes and send a PR?

bojanz commented 4 years ago

Opened a PR.

Also, in case you aren't subscribed to https://github.com/golang/go/issues/16971, it looks like it's getting accepted.