85 lines
2.9 KiB
Go
85 lines
2.9 KiB
Go
package password
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/crypto/argon2"
|
|
)
|
|
|
|
func TestVerifyRejectsMalformedAndCostlyPHCBeforeDerivation(t *testing.T) {
|
|
salt := base64.RawStdEncoding.EncodeToString(make([]byte, argonSaltLen))
|
|
hash := base64.RawStdEncoding.EncodeToString(make([]byte, argonKeyLen))
|
|
prefix := "$argon2id$v=19$"
|
|
suffix := "$" + salt + "$" + hash
|
|
invalid := []string{
|
|
strings.Repeat("x", 513),
|
|
prefix + "m=65537,t=2,p=1" + suffix,
|
|
prefix + "m=19456,t=5,p=1" + suffix,
|
|
prefix + "m=19456,t=2,p=5" + suffix,
|
|
prefix + "m=7,t=2,p=1" + suffix,
|
|
prefix + "m=31,t=2,p=4" + suffix,
|
|
prefix + "m=0,t=2,p=1" + suffix,
|
|
prefix + "m=19456,t=0,p=1" + suffix,
|
|
prefix + "m=19456,t=2,p=0" + suffix,
|
|
prefix + "m=-1,t=2,p=1" + suffix,
|
|
prefix + "m=+19456,t=2,p=1" + suffix,
|
|
prefix + "m=019456,t=2,p=1" + suffix,
|
|
prefix + "m=4294967296,t=2,p=1" + suffix,
|
|
prefix + "m=19456,t=2,p=1,x=1" + suffix,
|
|
prefix + "m=19456,t=2,t=1" + suffix,
|
|
"$argon2id$v=16$m=19456,t=2,p=1" + suffix,
|
|
prefix + "m=19456,t=2,p=1$" + salt + "$" + hash[:42] + "!",
|
|
prefix + "m=19456,t=2,p=1$" + salt + "=$" + hash,
|
|
"argon2id$" + salt + "$" + hash + "$extra",
|
|
}
|
|
// Exhausting the budget proves these inputs never reach Argon2: a valid
|
|
// encoding returns ErrBusy here, while malformed encodings are denied.
|
|
require.True(t, acquire())
|
|
defer release()
|
|
require.True(t, acquire())
|
|
defer release()
|
|
for _, encoded := range invalid {
|
|
got, err := Verify("password", encoded)
|
|
require.NoError(t, err)
|
|
require.False(t, got.Match)
|
|
require.False(t, got.NeedsRehash)
|
|
}
|
|
}
|
|
|
|
func TestDerivationBudgetIsNonblockingAndShared(t *testing.T) {
|
|
encoded, err := Hash("secret-password")
|
|
require.NoError(t, err)
|
|
require.True(t, acquire())
|
|
defer release()
|
|
require.True(t, acquire())
|
|
defer release()
|
|
require.False(t, acquire())
|
|
_, err = Hash("secret-password")
|
|
require.ErrorIs(t, err, ErrBusy)
|
|
_, err = Verify("secret-password", encoded)
|
|
require.ErrorIs(t, err, ErrBusy)
|
|
}
|
|
|
|
func TestLegacyAndObsoletePHCRequireOnlySuccessfulRehash(t *testing.T) {
|
|
var salt [argonSaltLen]byte
|
|
legacy := argon2.IDKey([]byte("secret-password"), salt[:], 1, 64*1024, 4, argonKeyLen)
|
|
legacyEncoded := "argon2id$" + base64.RawStdEncoding.EncodeToString(salt[:]) + "$" +
|
|
base64.RawStdEncoding.EncodeToString(legacy)
|
|
obsolete := argon2.IDKey([]byte("secret-password"), salt[:], 1, 32, 1, argonKeyLen)
|
|
obsoleteEncoded := "$argon2id$v=19$m=32,t=1,p=1$" + base64.RawStdEncoding.EncodeToString(salt[:]) + "$" +
|
|
base64.RawStdEncoding.EncodeToString(obsolete)
|
|
for _, encoded := range []string{legacyEncoded, obsoleteEncoded} {
|
|
got, err := Verify("wrong-password", encoded)
|
|
require.NoError(t, err)
|
|
require.False(t, got.Match)
|
|
require.False(t, got.NeedsRehash)
|
|
got, err = Verify("secret-password", encoded)
|
|
require.NoError(t, err)
|
|
require.True(t, got.Match)
|
|
require.True(t, got.NeedsRehash)
|
|
}
|
|
}
|