fix(crypto): use constant-time comparison in Argon2Digest.Validate
Summary
Argon2Digest.Validate in packages/go/crypto/argon2.go compares a stored argon2 digest against the re-derived digest with an early-return byte loop:
for idx := 0; idx < len(contentDigest); idx++ {
if s.Digest[idx] != contentDigest[idx] {
return false
}
}
return true
The loop exits at the first mismatching byte, so validation time varies with the position of the first difference. On a secret/hash comparison path this is a timing side channel; digest comparisons should run in constant time regardless of where (or whether) bytes differ.
Proposed fix
Replace the loop with crypto/subtle.ConstantTimeCompare, which returns 0 in constant time when lengths differ as well:
return subtle.ConstantTimeCompare(s.Digest, contentDigest) == 1
Lengths are already guaranteed equal here because contentDigest is derived with uint32(len(s.Digest)), but relying on the library's length-mismatch behavior is safer than the hand-written loop anyway.
Scope / notes
- This is a pre-existing issue, unrelated to and intentionally excluded from #3315 (digest length validation), where it was flagged in review.
- No behavior change for valid/invalid inputs; only the comparison mechanism changes.
- Test coverage: existing
TestArgon2Digest_Validate*cases already assert true/false outcomes and remain valid; a regression can be covered by those plus an explicit different-length digest case. - Happy to take this one after #3315 lands.
Hi @sanjio3, thanks for submitting this issue! Fortunately, all login attempts are guarded against with a 1.9-2.1 second wait preventing side channel attacks targeting the login endpoint. However, I believe that this change and the change in #3315 are good changes that we should make. If you'd like to make a PR for this change on top of #3315 I'd be happy to review both when time allows 😄
Thanks for the confirmation @wes-mil! Opened #3618 as a stacked PR on top of #3315 — the only new change is the comparison loop swap to subtle.ConstantTimeCompare. Happy to rebase it onto main once #3315 lands.