百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

go-srp

> 安全
开源

纯 Golang 中的 SRP 实现

182 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

纯 Golang 中的 SRP 实现

Standalone SRP-6a implementation in go-lang

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,

Design Overview

Conventions

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 verifier

Differences from SRP-6a and RFC 5054

We differ from the SRP-6a spec and RFC 5054 in a couple of key ways:

  • We hash the identity I; this provides some (minimal) protection against dictionary attacks on the username.
  • We hash the user passphrase p; this expands shorter passphrase into longer ones and extends the alphabet used in the passphrase.
  • We differ from RFC 5054 in our choice of hash function; we use Blake-2b. SHA-1 is getting long in the tooth, Blake2b is the current state-of-the art. Equivalently, one may use SHA3 (see below for using a user supplied hash function).

Generating and Storing the Password Verifier

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.

Authentication Protocol

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.,:
go
    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.

Using scrypt as the KDF

Here is a example KDF using scrypt:

go
    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
    }

Using argon2 as the KDF

Argon 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:

go
    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))
    }

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Gogogolangsecure-authenticationsrp

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类安全
定价开源

> 相关工具

O
OWASP ZAP
开源 Web 应用安全扫描器
O
owasp-wstg-tracker
Simple web app to track OWASP WSTG security testing progress
H
homebridge-mi-gateway-security
XiaoMi Gateway Security plugin for HomeBridge.