Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DecodeHash fixes #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 27 additions & 9 deletions argon2id.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
package argon2id

import (
"bytes"
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"runtime"
"strings"

Expand Down Expand Up @@ -148,17 +150,16 @@ func generateRandomBytes(n uint32) ([]byte, error) {
// DecodeHash expects a hash created from this package, and parses it to return the params used to
// create it, as well as the salt and key (password hash).
func DecodeHash(hash string) (params *Params, salt, key []byte, err error) {
vals := strings.Split(hash, "$")
if len(vals) != 6 {
return nil, nil, nil, ErrInvalidHash
}

if vals[1] != "argon2id" {
r := strings.NewReader(hash)

_, err = fmt.Fscanf(r, "$argon2id$")
if err != nil {
return nil, nil, nil, ErrIncompatibleVariant
}

var version int
_, err = fmt.Sscanf(vals[2], "v=%d", &version)
_, err = fmt.Fscanf(r, "v=%d$", &version)
if err != nil {
return nil, nil, nil, err
}
Expand All @@ -167,18 +168,35 @@ func DecodeHash(hash string) (params *Params, salt, key []byte, err error) {
}

params = &Params{}
_, err = fmt.Sscanf(vals[3], "m=%d,t=%d,p=%d", &params.Memory, &params.Iterations, &params.Parallelism)
_, err = fmt.Fscanf(r, "m=%d,t=%d,p=%d$", &params.Memory, &params.Iterations, &params.Parallelism)
if err != nil {
return nil, nil, nil, err
}

rest, err := ioutil.ReadAll(r)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ioutil is deprecated, could use io.ReadAll if we can assume Go 1.16+. The only info in the package I could locate about the supported version is 1.12 in go.mod.

if err != nil {
return nil, nil, nil, err
}
if bytes.ContainsAny(rest, "\r\n") { // base64 decoder ignores these
return nil, nil, nil, ErrInvalidHash
}

var i int
if i = bytes.IndexByte(rest, '$'); i == -1 {
return nil, nil, nil, ErrInvalidHash
}

b64Enc := base64.RawStdEncoding.Strict()

salt, err = base64.RawStdEncoding.Strict().DecodeString(vals[4])
salt = make([]byte, b64Enc.DecodedLen(i))
_, err = b64Enc.Decode(salt, rest[:i])
if err != nil {
return nil, nil, nil, err
}
params.SaltLength = uint32(len(salt))

key, err = base64.RawStdEncoding.Strict().DecodeString(vals[5])
key = make([]byte, b64Enc.DecodedLen(len(rest)-i-1))
_, err = b64Enc.Decode(key, rest[i+1:])
if err != nil {
return nil, nil, nil, err
}
Expand Down
25 changes: 22 additions & 3 deletions argon2id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func TestComparePasswordAndHash(t *testing.T) {
}
}

const bugHash = "$argon2id$v=19$m=65536,t=1,p=2$UDk0zEuIzbt0x3bwkf8Bgw$ihSfHWUJpTgDvNWiojrgcN4E0pJdUVmqCEdRZesx9tE"

func TestDecodeHash(t *testing.T) {
hash, err := CreateHash("pa$$word", DefaultParams)
if err != nil {
Expand All @@ -69,6 +71,24 @@ func TestDecodeHash(t *testing.T) {
if *params != *DefaultParams {
t.Fatalf("expected %#v got %#v", *DefaultParams, *params)
}

for _, c := range []string{"v", "m", "t", "p"} {
re := regexp.MustCompile("([$,])(" + c + "=[^$,]+)")
_, _, _, err = DecodeHash(re.ReplaceAllString(bugHash, "$1JUNK+$2"))
if err == nil {
t.Fatalf("leading %s key junk should fail decode", c)
}
_, _, _, err = DecodeHash(re.ReplaceAllString(bugHash, "$1$2+JUNK"))
if err == nil {
t.Fatalf("trailing %s value junk should fail decode", c)
}
}

i := strings.LastIndex(bugHash, "$")
_, _, _, err = DecodeHash(bugHash[:i] + "\r$\n" + bugHash[i+1:])
if err == nil {
t.Fatalf(`\r and \n in base64 data should fail decode`)
}
}

func TestCheckHash(t *testing.T) {
Expand All @@ -90,8 +110,7 @@ func TestCheckHash(t *testing.T) {
}

func TestStrictDecoding(t *testing.T) {
// "bug" valid hash: $argon2id$v=19$m=65536,t=1,p=2$UDk0zEuIzbt0x3bwkf8Bgw$ihSfHWUJpTgDvNWiojrgcN4E0pJdUVmqCEdRZesx9tE
ok, _, err := CheckHash("bug", "$argon2id$v=19$m=65536,t=1,p=2$UDk0zEuIzbt0x3bwkf8Bgw$ihSfHWUJpTgDvNWiojrgcN4E0pJdUVmqCEdRZesx9tE")
ok, _, err := CheckHash("bug", bugHash)
if err != nil {
t.Fatal(err)
}
Expand All @@ -100,7 +119,7 @@ func TestStrictDecoding(t *testing.T) {
}

// changed one last character of the hash
ok, _, err = CheckHash("bug", "$argon2id$v=19$m=65536,t=1,p=2$UDk0zEuIzbt0x3bwkf8Bgw$ihSfHWUJpTgDvNWiojrgcN4E0pJdUVmqCEdRZesx9tF")
ok, _, err = CheckHash("bug", bugHash[:len(bugHash)-1]+"F")
if err == nil {
t.Fatal("Hash validation should fail")
}
Expand Down