Skeleton contracts / contracts (pull_request) Blocked by required conditions
Smoke/security/grade4/persistence/failover пройдены; REPORT и DEFENSE.
157 lines
3.8 KiB
Go
157 lines
3.8 KiB
Go
package credentials
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"crypto/subtle"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"golang.org/x/crypto/argon2"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
const tokenSize = 32
|
|
|
|
var (
|
|
ErrPasswordMismatch = errors.New("password mismatch")
|
|
defaultMemoryKiB = 19456
|
|
defaultIterations = 2
|
|
defaultParallelism = 1
|
|
defaultSaltLen = 16
|
|
defaultKeyLen = 32
|
|
)
|
|
|
|
func init() {
|
|
if v := os.Getenv("ARGON2_MEMORY_KIB"); v != "" {
|
|
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
|
defaultMemoryKiB = n
|
|
}
|
|
}
|
|
if v := os.Getenv("ARGON2_ITERATIONS"); v != "" {
|
|
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
|
defaultIterations = n
|
|
}
|
|
}
|
|
if v := os.Getenv("ARGON2_PARALLELISM"); v != "" {
|
|
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
|
defaultParallelism = n
|
|
}
|
|
}
|
|
}
|
|
|
|
var dummyHash, _ = HashPassword("dummy-password-for-timing")
|
|
|
|
func DummyVerify(plain string) {
|
|
_ = VerifyPassword(dummyHash, plain)
|
|
}
|
|
|
|
func HashPassword(plain string) (string, error) {
|
|
salt := make([]byte, defaultSaltLen)
|
|
if _, err := rand.Read(salt); err != nil {
|
|
return "", err
|
|
}
|
|
hash := argon2.IDKey([]byte(plain), salt, uint32(defaultIterations), uint32(defaultMemoryKiB), uint8(defaultParallelism), uint32(defaultKeyLen))
|
|
b64Salt := base64.RawStdEncoding.EncodeToString(salt)
|
|
b64Hash := base64.RawStdEncoding.EncodeToString(hash)
|
|
return fmt.Sprintf("$argon2id$v=19$m=%d,t=%d,p=%d$%s$%s",
|
|
defaultMemoryKiB, defaultIterations, defaultParallelism, b64Salt, b64Hash), nil
|
|
}
|
|
|
|
func VerifyPassword(encoded, plain string) error {
|
|
if strings.HasPrefix(encoded, "$argon2id$") {
|
|
return verifyArgon2id(encoded, plain)
|
|
}
|
|
err := bcrypt.CompareHashAndPassword([]byte(encoded), prehashBcrypt(plain))
|
|
if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
|
|
return ErrPasswordMismatch
|
|
}
|
|
return err
|
|
}
|
|
|
|
func NeedsRehash(encoded string) bool {
|
|
if !strings.HasPrefix(encoded, "$argon2id$") {
|
|
return true
|
|
}
|
|
mem, iter, par, ok := parseArgon2Params(encoded)
|
|
if !ok {
|
|
return true
|
|
}
|
|
return mem != defaultMemoryKiB || iter != defaultIterations || par != defaultParallelism
|
|
}
|
|
|
|
func verifyArgon2id(encoded, plain string) error {
|
|
mem, iter, par, ok := parseArgon2Params(encoded)
|
|
if !ok {
|
|
return fmt.Errorf("invalid argon2 hash")
|
|
}
|
|
parts := strings.Split(encoded, "$")
|
|
if len(parts) != 6 {
|
|
return fmt.Errorf("invalid argon2 hash")
|
|
}
|
|
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
want, err := base64.RawStdEncoding.DecodeString(parts[5])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
got := argon2.IDKey([]byte(plain), salt, uint32(iter), uint32(mem), uint8(par), uint32(len(want)))
|
|
if subtle.ConstantTimeCompare(got, want) != 1 {
|
|
return ErrPasswordMismatch
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func parseArgon2Params(encoded string) (memory, iterations, parallelism int, ok bool) {
|
|
idx := strings.Index(encoded, "$m=")
|
|
if idx < 0 {
|
|
return 0, 0, 0, false
|
|
}
|
|
rest := encoded[idx+1:]
|
|
fields := strings.Split(rest, "$")
|
|
if len(fields) < 1 {
|
|
return 0, 0, 0, false
|
|
}
|
|
for _, part := range strings.Split(fields[0], ",") {
|
|
kv := strings.SplitN(part, "=", 2)
|
|
if len(kv) != 2 {
|
|
continue
|
|
}
|
|
n, err := strconv.Atoi(kv[1])
|
|
if err != nil {
|
|
return 0, 0, 0, false
|
|
}
|
|
switch kv[0] {
|
|
case "m":
|
|
memory = n
|
|
case "t":
|
|
iterations = n
|
|
case "p":
|
|
parallelism = n
|
|
}
|
|
}
|
|
return memory, iterations, parallelism, memory > 0 && iterations > 0 && parallelism > 0
|
|
}
|
|
|
|
func prehashBcrypt(plain string) []byte {
|
|
sum := sha256.Sum256([]byte(plain))
|
|
return []byte(base64.StdEncoding.EncodeToString(sum[:]))
|
|
}
|
|
|
|
func NewToken() string {
|
|
raw := make([]byte, tokenSize)
|
|
_, _ = rand.Read(raw)
|
|
return base64.RawURLEncoding.EncodeToString(raw)
|
|
}
|
|
|
|
func HashToken(token string) string {
|
|
sum := sha256.Sum256([]byte(token))
|
|
return hex.EncodeToString(sum[:])
|
|
} |