Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/auth/pass_table/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ func computeArgon2(opts HashOpts, pass string) (string, error) {

func verifyArgon2(pass, hashSalt string) error {
parts := strings.SplitN(hashSalt, ":", 5)
if len(parts) != 5 {
return fmt.Errorf("pass_table: malformed hash string: expected 5 fields, got %d", len(parts))
}

time, err := strconv.ParseUint(parts[0], 10, 32)
if err != nil {
Expand All @@ -119,6 +122,10 @@ func verifyArgon2(pass, hashSalt string) error {
return fmt.Errorf("pass_table: malformed hash string: %w", err)
}

if time == 0 || threads == 0 {
return fmt.Errorf("pass_table: malformed hash string: time and threads must be non-zero")
}

passHash := argon2.IDKey([]byte(pass), salt, uint32(time), uint32(memory), uint8(threads), Argon2Size)
if subtle.ConstantTimeCompare(passHash, hash) != 1 {
return fmt.Errorf("pass_table: hash mismatch")
Expand Down
28 changes: 28 additions & 0 deletions internal/auth/pass_table/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package pass_table

import "testing"

func TestVerifyArgon2_Malformed(t *testing.T) {
for _, hash := range []string{
"",
"1",
"1:8:1",
"1:8:1:U0FBQUFBTFQ=",
"0:8:1:U0FBQUFBTFQ=:KHUshl3DcpHR3AoVd28ZeBGmZ1Fj1gwJgNn98Ia8DAvGHqI0BvFOMJPxtaAfO8F+qomm2O3h0P0yV50QGwXI/Q==",
"1:8:0:U0FBQUFBTFQ=:KHUshl3DcpHR3AoVd28ZeBGmZ1Fj1gwJgNn98Ia8DAvGHqI0BvFOMJPxtaAfO8F+qomm2O3h0P0yV50QGwXI/Q==",
} {
if err := verifyArgon2("password", hash); err == nil {
t.Errorf("verifyArgon2(%q) returned nil error", hash)
}
}
}

func TestVerifyArgon2(t *testing.T) {
hash := "1:8:1:U0FBQUFBTFQ=:KHUshl3DcpHR3AoVd28ZeBGmZ1Fj1gwJgNn98Ia8DAvGHqI0BvFOMJPxtaAfO8F+qomm2O3h0P0yV50QGwXI/Q=="
if err := verifyArgon2("password", hash); err != nil {
t.Errorf("unexpected error: %v", err)
}
if err := verifyArgon2("different-password", hash); err == nil {
t.Error("expected mismatch error")
}
}
Loading