forked from mtusi2026/mtusi2026lab3
91 lines
3.3 KiB
Go
91 lines
3.3 KiB
Go
// Package auth implements the lab3 credential primitives: a library password
|
|
// KDF (Argon2id), opaque session tokens and RFC 7617 Basic parsing. None of
|
|
// these helpers log or serialise the secret material they receive.
|
|
package auth
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/subtle"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"golang.org/x/crypto/argon2"
|
|
)
|
|
|
|
// Argon2id parameters. 19 MiB / 2 passes / 1 lane is the OWASP password
|
|
// storage minimum for Argon2id, chosen so eight concurrent registrations fit
|
|
// inside the 256 MiB API container limit. The parameters are stored in every
|
|
// PHC string, so a future change remains backward compatible.
|
|
const (
|
|
argonMemory uint32 = 19 * 1024 // KiB
|
|
argonIterations uint32 = 2
|
|
argonParallelism uint8 = 1
|
|
argonSaltLength = 16
|
|
argonKeyLength uint32 = 32
|
|
)
|
|
|
|
// ErrInvalidHash means a stored PHC string is malformed or uses unsupported
|
|
// parameters. It is an internal error, never shown to a client.
|
|
var ErrInvalidHash = errors.New("invalid password hash")
|
|
|
|
// Hash derives an Argon2id PHC string for password. Callers must validate the
|
|
// password policy before calling; the KDF itself accepts arbitrary UTF-8.
|
|
func Hash(password string) (string, error) {
|
|
salt := make([]byte, argonSaltLength)
|
|
if _, err := rand.Read(salt); err != nil {
|
|
return "", fmt.Errorf("generate password salt: %w", err)
|
|
}
|
|
key := argon2.IDKey([]byte(password), salt, argonIterations, argonMemory, argonParallelism, argonKeyLength)
|
|
return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
|
argon2.Version, argonMemory, argonIterations, argonParallelism,
|
|
base64.RawStdEncoding.EncodeToString(salt),
|
|
base64.RawStdEncoding.EncodeToString(key),
|
|
), nil
|
|
}
|
|
|
|
// Verify checks password against a PHC string in constant time and reports
|
|
// whether the encoded hash was structurally valid. A malformed hash is not a
|
|
// password mismatch and should be treated as an internal error by the caller.
|
|
func Verify(password, encoded string) (match bool, err error) {
|
|
params, salt, key, err := parse(encoded)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
candidate := argon2.IDKey([]byte(password), salt, params.iterations, params.memory, params.parallelism, uint32(len(key)))
|
|
return subtle.ConstantTimeCompare(candidate, key) == 1, nil
|
|
}
|
|
|
|
type params struct {
|
|
memory uint32
|
|
iterations uint32
|
|
parallelism uint8
|
|
}
|
|
|
|
func parse(encoded string) (params, []byte, []byte, error) {
|
|
parts := strings.Split(encoded, "$")
|
|
if len(parts) != 6 || parts[0] != "" || parts[1] != "argon2id" {
|
|
return params{}, nil, nil, ErrInvalidHash
|
|
}
|
|
if parts[2] != fmt.Sprintf("v=%d", argon2.Version) {
|
|
return params{}, nil, nil, ErrInvalidHash
|
|
}
|
|
var p params
|
|
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &p.memory, &p.iterations, &p.parallelism); err != nil {
|
|
return params{}, nil, nil, ErrInvalidHash
|
|
}
|
|
if p.memory < 8*1024 || p.memory > 256*1024 || p.iterations < 1 || p.iterations > 10 || p.parallelism < 1 || p.parallelism > 16 {
|
|
return params{}, nil, nil, ErrInvalidHash
|
|
}
|
|
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
|
|
if err != nil || len(salt) < 8 || len(salt) > 64 {
|
|
return params{}, nil, nil, ErrInvalidHash
|
|
}
|
|
key, err := base64.RawStdEncoding.DecodeString(parts[5])
|
|
if err != nil || len(key) < 16 || len(key) > 64 {
|
|
return params{}, nil, nil, ErrInvalidHash
|
|
}
|
|
return p, salt, key, nil
|
|
}
|