SRP implementation in pure golang
This is a standalone implementation of SRP in golang. It uses the go standard libraries and has no other external dependencies. This library can be used by SRP clients or servers.
SRP is a protocol to authenticate a user and derive safe session keys. It is the latest in the category of "strong authentication protocols".
SRP is documented here: . Briefly,
N A large safe prime (N = 2q+1, where q is prime)
All arithmetic is done modulo N.
g A generator modulo N
k Multiplier parameter (k = H(N, g) in SRP-6a, k = 3 for legacy SRP-6)
s User's salt
I Username
p Cleartext Password
H() One-way hash function
^ (Modular) Exponentiation
u Random scrambling parameter
a,b Secret ephemeral values
A,B Public ephemeral values
x Private key (derived from p and s)
v Password verifierWe differ from the SRP-6a spec and RFC 5054 in a couple of key ways:
I; this provides some (minimal) protection against
dictionary attacks on the username.p; this expands shorter passphrase
into longer ones and extends the alphabet used in the passphrase.The host calculates the password verifier using the following formula:
s = randomsalt() (same length as N)
I = H(I)
p = H(p) (hash/expand I & p)
t = H(I, ":", p)
x = H(s, t)
v = g^x (computes password verifier)The host then stores {I, s, v} in its password database - such that
the triple can be retrieved by using I as the index/key.
The authentication protocol itself goes as follows:
Client Server
-------------- ----------------
un, pw =
I = H(un)
p = H(pw)
a = random()
A = g^a % N
I, A -->
s, v = lookup(I)
b = random()
B = (kv + g^b) % N
u = H(A, B)
S = ((A * v^u) ^ b) % N
K = H(S)
M' = H(K, A, B, I, s, N, g)
M must be equal to M'
Z = H(M, K)
server and server-\>client). e.g.,: c2s_k = KDF(rawkey, counter, "C2S")
s2s_k = KDF(rawkey, counter, "S2C")KDF above can be a reputable key derivation function such as PBKDF2 or Scrypt. The "counter" is incremented every time you derive a new key.
I am not a cryptographer. Please consult your favorite crypto book for deriving encryption keys from a master key.
scrypt as the KDFHere is a example KDF using scrypt:
import "golang.org/x/crypto/scrypt"
// Safe values for Scrypt() parameters
const _N int = 65536
const _r int = 1024
const _p int = 64
// Kdf derives a 'sz' byte key for use 'usage'
func Kdf(key []byte, salt []byte, usage string, sz int) []byte {
u0 := []byte(usage)
pw := append(key, u0...)
k, _ := scrypt.Key(pw, salt, _N, _r, _p, sz)
return k
}argon2 as the KDFArgon is
the new state of the art (2018) key derivation algorithm. The
Argon2id variant is resistant to timing, side-channel and
Time-memory tradeoff attacks. Here is an example using the Argon2id
variant:
import (
"runtime"
"golang.org/x/crypto/argon2"
)
// safe values for IDKey() borrowed from libsodium
const _Time uint32 = 3
const _Mem uint32 = 256 * 1048576 // 256 MB
// Kdf derives a 'sz' byte key for use 'usage'
func Kdf(key, salt []byte, usage string, sz int) []byte {
u0 := []byte(usage)
pw := append(key, u0...)
return argon2.IDKey(pw, salt, _Time, _Mem, runtime.NumCPU(), uint32(sz))
}No open issues yet, or sync has not completed.