Secure messenger with TLS identities, scoped OpenBao secrets and worker compromise experiments
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/argon2"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Fixed, bounded parameters: 64 MiB, 3 passes, 2 lanes, 16-byte salt,
|
||||
// 32-byte result. The HTTP layer bounds simultaneous KDF work to 2 calls.
|
||||
func hashPassword(password string) string {
|
||||
salt := make([]byte, 16)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
hash := argon2.IDKey([]byte(password), salt, 3, 64*1024, 2, 32)
|
||||
return "$argon2id$v=19$m=65536,t=3,p=2$" + base64.RawStdEncoding.EncodeToString(salt) + "$" + base64.RawStdEncoding.EncodeToString(hash)
|
||||
}
|
||||
func verifyPassword(password, encoded string) bool {
|
||||
parts := strings.Split(encoded, "$")
|
||||
if len(parts) != 6 || parts[1] != "argon2id" || parts[2] != "v=19" {
|
||||
return false
|
||||
}
|
||||
var memory, passes, lanes uint32
|
||||
if n, e := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &passes, &lanes); e != nil || n != 3 || parts[3] != fmt.Sprintf("m=%d,t=%d,p=%d", memory, passes, lanes) || memory < 8192 || memory > 65536 || passes < 1 || passes > 3 || lanes < 1 || lanes > 2 {
|
||||
return false
|
||||
}
|
||||
salt, e1 := base64.RawStdEncoding.DecodeString(parts[4])
|
||||
want, e2 := base64.RawStdEncoding.DecodeString(parts[5])
|
||||
if e1 != nil || e2 != nil || len(salt) != 16 || len(want) != 32 {
|
||||
return false
|
||||
}
|
||||
got := argon2.IDKey([]byte(password), salt, passes, memory, uint8(lanes), 32)
|
||||
return subtle.ConstantTimeCompare(got, want) == 1
|
||||
}
|
||||
|
||||
func needsRehash(hash string) bool {
|
||||
return !strings.HasPrefix(hash, "$argon2id$v=19$m=65536,t=3,p=2$")
|
||||
}
|
||||
Reference in New Issue
Block a user