Prepare verified laboratory 7 implementation and evidence

This commit is contained in:
rey
2026-11-17 17:32:06 +03:00
parent 20afc2c790
commit eb1b5c7b84
677 changed files with 208256 additions and 64 deletions
@@ -0,0 +1,68 @@
package password
import (
"backend/internal/domain/port"
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"fmt"
"golang.org/x/crypto/argon2"
"strings"
)
type Argon struct{}
var kdfSlots = make(chan struct{}, 2)
func New() port.Passwords { return Argon{} }
func (Argon) Hash(password string) (string, error) {
kdfSlots <- struct{}{}
defer func() { <-kdfSlots }()
salt := make([]byte, 16)
if _, err := rand.Read(salt); err != nil {
return "", err
}
hash := argon2.IDKey([]byte(password), salt, 2, 32768, 1, 32)
return fmt.Sprintf("argon2id$v19$m32768t2p1$%s$%s", base64.RawStdEncoding.EncodeToString(salt), base64.RawStdEncoding.EncodeToString(hash)), nil
}
func parse(encoded string) (m, t uint32, p uint8, salt, want []byte, ok bool) {
if len(encoded) > 256 {
return
}
parts := strings.Split(encoded, "$")
if len(parts) != 5 || parts[0] != "argon2id" || parts[1] != "v19" {
return
}
if n, e := fmt.Sscanf(parts[2], "m%dt%dp%d", &m, &t, &p); e != nil || n != 3 {
return
}
// Bound legacy verification before invoking an attacker-influenced KDF profile.
if m < 8192 || m > 65536 || t < 1 || t > 4 || p < 1 || p > 2 || parts[2] != fmt.Sprintf("m%dt%dp%d", m, t, p) {
return
}
var e error
salt, e = base64.RawStdEncoding.DecodeString(parts[3])
if e != nil || len(salt) != 16 {
return
}
want, e = base64.RawStdEncoding.DecodeString(parts[4])
if e != nil || len(want) != 32 {
return
}
ok = true
return
}
func (Argon) Verify(password, encoded string) bool {
m, t, p, salt, want, ok := parse(encoded)
if !ok {
return false
}
kdfSlots <- struct{}{}
defer func() { <-kdfSlots }()
got := argon2.IDKey([]byte(password), salt, t, m, p, 32)
return subtle.ConstantTimeCompare(got, want) == 1
}
func (Argon) NeedsRehash(encoded string) bool {
m, t, p, _, _, ok := parse(encoded)
return !ok || m < 32768 || t < 2 || p != 1
}
@@ -0,0 +1,69 @@
package password
import (
"os"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
func BenchmarkArgonLogin(b *testing.B) {
workers, e := strconv.Atoi(os.Getenv("KDF_CONCURRENCY"))
if e != nil || workers < 1 || workers > 4 {
b.Fatal("explicit concurrency1..4 required")
}
a := Argon{}
hash, e := a.Hash("synthetic-benchmark-password")
if e != nil {
b.Fatal(e)
}
var next atomic.Int64
var group sync.WaitGroup
var mu sync.Mutex
durations := []float64{}
var failures atomic.Int64
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < workers; i++ {
group.Add(1)
go func() {
defer group.Done()
for next.Add(1) <= int64(b.N) {
start := time.Now()
if !a.Verify("synthetic-benchmark-password", hash) {
failures.Add(1)
}
ms := float64(time.Since(start)) / float64(time.Millisecond)
mu.Lock()
durations = append(durations, ms)
mu.Unlock()
}
}()
}
group.Wait()
b.StopTimer()
if failures.Load() != 0 {
b.Fatal("verification failure")
}
sort.Float64s(durations)
if len(durations) > 0 {
b.ReportMetric(durations[(len(durations)-1)*95/100], "p95-ms")
}
if raw, e := os.ReadFile("/proc/self/status"); e == nil {
for _, line := range strings.Split(string(raw), "\n") {
if strings.HasPrefix(line, "VmHWM:") {
parts := strings.Fields(line)
if len(parts) > 1 {
rss, e := strconv.ParseFloat(parts[1], 64)
if e == nil {
b.ReportMetric(rss/1024, "peak-RSS-MiB")
}
}
}
}
}
}
@@ -0,0 +1,33 @@
package password
import (
"encoding/base64"
"fmt"
"golang.org/x/crypto/argon2"
"testing"
)
func TestLegacyParametersVerifyAndRequireRehash(t *testing.T) {
salt := []byte("1234567890abcdef")
hash := argon2.IDKey([]byte("synthetic-password"), salt, 1, 8192, 1, 32)
encoded := fmt.Sprintf("argon2id$v19$m8192t1p1$%s$%s", base64.RawStdEncoding.EncodeToString(salt), base64.RawStdEncoding.EncodeToString(hash))
a := Argon{}
if !a.Verify("synthetic-password", encoded) {
t.Fatal("valid legacy password rejected")
}
if !a.NeedsRehash(encoded) {
t.Fatal("legacy cost must upgrade")
}
current, e := a.Hash("synthetic-password")
if e != nil || !a.Verify("synthetic-password", current) || a.NeedsRehash(current) {
t.Fatal("current profile must verify without repeated rehash")
}
for _, bad := range []string{"argon2id$v19$m4294967295t2p1$x$x", "argon2id$v19$m32768t9999p1$x$x", encoded + "$extra", "bad"} {
if a.Verify("synthetic-password", bad) {
t.Fatal("unsafe encoded parameters accepted")
}
}
if a.Verify("wrong-password", encoded) {
t.Fatal("wrong password accepted")
}
}