-
Notifications
You must be signed in to change notification settings - Fork 0
/
srp.go
242 lines (192 loc) · 5.66 KB
/
srp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package srp
import (
"crypto"
"errors"
"fmt"
"math"
"math/big"
"github.com/bodgit/srp/internal/util"
)
// SRP manages the various computations used in the SRP protocol.
type SRP struct {
h crypto.Hash
g *Group
x func(*SRP, []byte, []byte, []byte) *big.Int
k func(*SRP) *big.Int
u func(*SRP, *big.Int, *big.Int) *big.Int
}
var (
// ErrInvalidPublicKey means the public key is invalid.
ErrInvalidPublicKey = errors.New("invalid public key")
// ErrTrailingBytes means there were additional trailing bytes when
// unmarshalling.
ErrTrailingBytes = errors.New("trailing bytes")
// ErrTooBig means the length of the value exceeds the size of a 16-bit
// integer.
ErrTooBig = fmt.Errorf("value exceeds %d bytes", math.MaxUint16)
errMismatchedProof = errors.New("mismatched proof")
)
// NewSRP returns a new SRP using the chosen hash and group along with any
// options.
func NewSRP(hash crypto.Hash, group *Group, options ...func(*SRP) error) (*SRP, error) {
s := &SRP{
h: hash,
g: group,
}
if err := s.setOption(options...); err != nil {
return nil, err
}
return s, nil
}
func (s *SRP) setOption(options ...func(*SRP) error) error {
for _, option := range options {
if err := option(s); err != nil {
return err
}
}
return nil
}
// K overrides the default function for computing the multiplier.
func K(f func(*SRP) *big.Int) func(*SRP) error {
return func(s *SRP) error {
s.k = f
return nil
}
}
// SetK overrides the default function for computing the multiplier.
func (s *SRP) SetK(f func(*SRP) *big.Int) error {
return s.setOption(K(f))
}
// X overrides the default function for computing the X value.
func X(f func(*SRP, []byte, []byte, []byte) *big.Int) func(*SRP) error {
return func(s *SRP) error {
s.x = f
return nil
}
}
// SetX overrides the default function for computing the X value.
func (s *SRP) SetX(f func(*SRP, []byte, []byte, []byte) *big.Int) error {
return s.setOption(X(f))
}
// U overrides the default function for computing the U value.
func U(f func(*SRP, *big.Int, *big.Int) *big.Int) func(*SRP) error {
return func(s *SRP) error {
s.u = f
return nil
}
}
// SetU overrides the default function for computing the U value.
func (s *SRP) SetU(f func(*SRP, *big.Int, *big.Int) *big.Int) error {
return s.setOption(U(f))
}
// Group returns the Group in use.
func (s *SRP) Group() *Group {
return s.g
}
func (s *SRP) multiplier() *big.Int {
if s.k != nil {
return s.k(s)
}
return s.HashInt(s.Group().N.Bytes(), util.Pad(s.Group().G, s.Group().Size))
}
func (s *SRP) computeA(a *big.Int) *big.Int {
return new(big.Int).Exp(s.Group().G, a, s.Group().N)
}
func (s *SRP) computeB(b, k, v *big.Int) *big.Int {
// B = k*v + g^b % N
return new(big.Int).Mod(
new(big.Int).Add(
new(big.Int).Mul(k, v),
new(big.Int).Exp(s.Group().G, b, s.Group().N)),
s.Group().N)
}
func (s *SRP) computeX(identity, password, salt []byte) *big.Int {
if s.x != nil {
return s.x(s, identity, password, salt)
}
// x = H(s | H(I | ":" | P))
return s.HashInt(salt, s.HashBytes(identity, []byte(":"), password))
}
func (s *SRP) computeV(x *big.Int) *big.Int {
return new(big.Int).Exp(s.Group().G, x, s.Group().N)
}
func (s *SRP) computeU(xA, xB *big.Int) (*big.Int, error) {
var u *big.Int
if s.u != nil {
u = s.u(s, xA, xB)
} else {
// u = H(A | B)
u = s.HashInt(util.Pad(xA, s.Group().Size), util.Pad(xB, s.Group().Size))
}
if u.Sign() == 0 {
return nil, ErrInvalidPublicKey
}
return u, nil
}
func (s *SRP) computeClientS(a, xB, k, u, x *big.Int) *big.Int {
// S = ((B - kg^x) ^ (a + ux)) % N
return new(big.Int).Exp(
new(big.Int).Sub(xB, new(big.Int).Mul(k, new(big.Int).Exp(s.Group().G, x, s.Group().N))),
new(big.Int).Add(a, new(big.Int).Mul(u, x)),
s.Group().N)
}
func (s *SRP) computeServerS(xA, b, u, v *big.Int) *big.Int {
// S = ((Av^u) ^ b) % N
return new(big.Int).Exp(new(big.Int).Mul(xA, new(big.Int).Exp(v, u, s.Group().N)), b, s.Group().N)
}
func (s *SRP) computeK(xS *big.Int) []byte {
return s.HashBytes(xS.Bytes())
}
func (s *SRP) computeM1(xA, xB *big.Int, xK, identity, salt []byte) []byte {
// M1 = H(H(N) XOR H(g) | H(U) | s | A | B | K)
xor := make([]byte, s.h.New().Size())
_ = xorBytes(xor, s.HashBytes(s.Group().N.Bytes()), s.HashBytes(s.Group().G.Bytes()))
return s.HashBytes(xor, s.HashBytes(identity), salt, xA.Bytes(), xB.Bytes(), xK)
}
func (s *SRP) computeM2(xA *big.Int, m1, xK []byte) []byte {
// M2 = H(A | M | K)
return s.HashBytes(xA.Bytes(), m1, xK)
}
// HashBytes hashes each passed byte slice and returns the digest.
func (s *SRP) HashBytes(a ...[]byte) []byte {
h := s.h.New()
for _, z := range a {
_, _ = h.Write(z)
}
return h.Sum(nil)
}
// HashInt hashes each passed byte slice and returns the digest as a big.Int.
func (s *SRP) HashInt(a ...[]byte) *big.Int {
return new(big.Int).SetBytes(s.HashBytes(a...))
}
// NewISV creates a new ISV containing the identity, salt and verifier.
func (s *SRP) NewISV(identity, password []byte) (*ISV, error) {
salt, err := randBytes(s.Group().Size)
if err != nil {
return nil, err
}
return &ISV{
Identity: identity,
Salt: salt,
Verifier: s.computeV(s.computeX(identity, password, salt)).Bytes(),
}, nil
}
// NewClient creates a new Client using the identity and password.
func (s *SRP) NewClient(identity, password []byte) (*Client, error) {
a, err := randBigInt(s.Group().Size)
if err != nil {
return nil, err
}
return &Client{
s: s,
identity: identity,
password: password,
a: a,
xA: s.computeA(a),
}, nil
}
// NewServer creates a new Server using the ISV and the client public value.
func (s *SRP) NewServer(i *ISV, xA []byte) (*Server, error) {
server := new(Server)
return server, server.Reset(s, i, xA)
}