password.go103 lines, 3.4 KB
| 1 | package auth |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "crypto/subtle" |
| 7 | "encoding/base64" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "runtime" |
| 11 | "strings" |
| 12 | |
| 13 | "golang.org/x/crypto/argon2" |
| 14 | ) |
| 15 | |
| 16 | // Params are argon2id's cost settings. Every hash records the ones it was |
| 17 | // made with, so we can raise them later, and old hashes still verify. |
| 18 | type Params struct { |
| 19 | Memory uint32 // in KiB |
| 20 | Time uint32 // passes over the memory |
| 21 | Threads uint8 |
| 22 | } |
| 23 | |
| 24 | // DefaultParams is RFC 9106's second recommendation: 64 MiB of memory, three |
| 25 | // passes and four lanes. About 50 ms here, and very expensive for anyone |
| 26 | // guessing passwords on GPUs. |
| 27 | var DefaultParams = Params{Memory: 64 * 1024, Time: 3, Threads: 4} |
| 28 | |
| 29 | const ( |
| 30 | saltLen = 16 // bytes |
| 31 | keyLen = 32 // bytes |
| 32 | ) |
| 33 | |
| 34 | var errMalformedHash = errors.New("malformed password hash") |
| 35 | |
| 36 | // hashSlots limits how many hashes run at once. Each one holds 64 MiB, so a |
| 37 | // burst of logins waits here, instead of running us out of memory. |
| 38 | var hashSlots = make(chan struct{}, runtime.GOMAXPROCS(0)) |
| 39 | |
| 40 | // b64 is the PHC string format's base64: standard alphabet, no padding. |
| 41 | var b64 = base64.RawStdEncoding |
| 42 | |
| 43 | // HashPassword hashes password with argon2id and a new random salt. It |
| 44 | // returns a PHC string, like $argon2id$v=19$m=65536,t=3,p=4$salt$hash, which |
| 45 | // holds everything VerifyPassword needs. |
| 46 | func HashPassword(ctx context.Context, password string) (string, error) { |
| 47 | salt := make([]byte, saltLen) |
| 48 | rand.Read(salt) // never fails: it crashes the program instead |
| 49 | p := DefaultParams |
| 50 | key, err := derive(ctx, password, salt, p, keyLen) |
| 51 | if err != nil { |
| 52 | return "", err |
| 53 | } |
| 54 | return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", argon2.Version, |
| 55 | p.Memory, p.Time, p.Threads, b64.EncodeToString(salt), b64.EncodeToString(key)), nil |
| 56 | } |
| 57 | |
| 58 | // VerifyPassword reports whether password matches encoded, a hash made by |
| 59 | // HashPassword. It hashes password again, with the stored salt and params. |
| 60 | func VerifyPassword(ctx context.Context, password, encoded string) (bool, error) { |
| 61 | p, salt, key, err := decodeHash(encoded) |
| 62 | if err != nil { |
| 63 | return false, err |
| 64 | } |
| 65 | other, err := derive(ctx, password, salt, p, uint32(len(key))) |
| 66 | if err != nil { |
| 67 | return false, err |
| 68 | } |
| 69 | // Constant time: how long this takes says nothing about where the two |
| 70 | // hashes start to differ. |
| 71 | return subtle.ConstantTimeCompare(key, other) == 1, nil |
| 72 | } |
| 73 | |
| 74 | // derive runs argon2id as soon as a slot is free, or gives up when ctx ends. |
| 75 | func derive(ctx context.Context, password string, salt []byte, p Params, n uint32) ([]byte, error) { |
| 76 | select { |
| 77 | case hashSlots <- struct{}{}: |
| 78 | defer func() { <-hashSlots }() |
| 79 | case <-ctx.Done(): |
| 80 | return nil, ctx.Err() |
| 81 | } |
| 82 | return argon2.IDKey([]byte(password), salt, p.Time, p.Memory, p.Threads, n), nil |
| 83 | } |
| 84 | |
| 85 | // decodeHash splits a PHC string into its params, salt and hash. |
| 86 | func decodeHash(encoded string) (p Params, salt, key []byte, err error) { |
| 87 | parts := strings.Split(encoded, "$") // "", "argon2id", "v=19", params, salt, hash |
| 88 | if len(parts) != 6 || parts[1] != "argon2id" || parts[2] != fmt.Sprintf("v=%d", argon2.Version) { |
| 89 | return p, nil, nil, errMalformedHash |
| 90 | } |
| 91 | if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &p.Memory, &p.Time, &p.Threads); err != nil { |
| 92 | return p, nil, nil, errMalformedHash |
| 93 | } |
| 94 | salt, err = b64.DecodeString(parts[4]) |
| 95 | if err != nil { |
| 96 | return p, nil, nil, errMalformedHash |
| 97 | } |
| 98 | key, err = b64.DecodeString(parts[5]) |
| 99 | if err != nil || len(key) == 0 { |
| 100 | return p, nil, nil, errMalformedHash |
| 101 | } |
| 102 | return p, salt, key, nil |
| 103 | } |