-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.go
More file actions
167 lines (128 loc) · 3.16 KB
/
Copy pathserver.go
File metadata and controls
167 lines (128 loc) · 3.16 KB
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
package srp
import (
"bytes"
"crypto/subtle"
"io"
"math/big"
)
// Server represents the server-side of an SRP session. It implements
// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler so it can be
// serialized to and from persistent storage.
type Server struct {
xA, b, xB, xS *big.Int
salt, xK, m1, m2 []byte
}
// Reset resets s to its initial state using the passed parameters.
func (s *Server) Reset(srp *SRP, i *ISV, xA []byte) error {
a := new(big.Int).SetBytes(xA)
if new(big.Int).Mod(a, srp.Group().N).Sign() == 0 {
return ErrInvalidPublicKey
}
b, err := randBigInt(srp.Group().Size)
if err != nil {
return err
}
v := new(big.Int).SetBytes(i.Verifier)
s.xA = a
s.b, s.xB = b, srp.computeB(b, srp.multiplier(), v)
s.salt = i.Salt
u, err := srp.computeU(s.xA, s.xB)
if err != nil {
return err
}
s.xS = srp.computeServerS(s.xA, s.b, u, v)
s.xK = srp.computeK(s.xS)
s.m1 = srp.computeM1(s.xA, s.xB, s.xK, i.Identity, s.salt)
s.m2 = srp.computeM2(s.xA, s.m1, s.xK)
return nil
}
// Salt returns the client salt value.
func (s *Server) Salt() []byte {
return s.salt
}
// B returns the server public value.
func (s *Server) B() []byte {
return s.xB.Bytes()
}
// Check compares the M1 proof computed by the client with the servers copy.
// If it is identical then the servers M2 proof is returned to be sent back to
// the client.
func (s *Server) Check(m1 []byte) ([]byte, error) {
if subtle.ConstantTimeCompare(m1, s.m1) != 1 {
return nil, errMismatchedProof
}
return s.m2, nil
}
// Key returns the key shared with the client.
func (s *Server) Key() []byte {
return s.xK
}
// MarshalBinary satisfies the encoding.BinaryMarshaler interface.
func (s *Server) MarshalBinary() ([]byte, error) {
b := new(bytes.Buffer)
if err := writeBytes(b, s.xA.Bytes()); err != nil {
return nil, err
}
if err := writeBytes(b, s.b.Bytes()); err != nil {
return nil, err
}
if err := writeBytes(b, s.xB.Bytes()); err != nil {
return nil, err
}
if err := writeBytes(b, s.xS.Bytes()); err != nil {
return nil, err
}
if err := writeBytes(b, s.salt); err != nil {
return nil, err
}
if err := writeBytes(b, s.xK); err != nil {
return nil, err
}
if err := writeBytes(b, s.m1); err != nil {
return nil, err
}
if err := writeBytes(b, s.m2); err != nil {
return nil, err
}
return b.Bytes(), nil
}
// UnmarshalBinary satisfies the encoding.BinaryUnmarshaler interface.
func (s *Server) UnmarshalBinary(b []byte) error {
r := bytes.NewReader(b)
xA, err := readBytes(r)
if err != nil {
return err
}
s.xA = new(big.Int).SetBytes(xA)
bb, err := readBytes(r)
if err != nil {
return err
}
s.b = new(big.Int).SetBytes(bb)
xB, err := readBytes(r)
if err != nil {
return err
}
s.xB = new(big.Int).SetBytes(xB)
xS, err := readBytes(r)
if err != nil {
return err
}
s.xS = new(big.Int).SetBytes(xS)
if s.salt, err = readBytes(r); err != nil {
return err
}
if s.xK, err = readBytes(r); err != nil {
return err
}
if s.m1, err = readBytes(r); err != nil {
return err
}
if s.m2, err = readBytes(r); err != nil {
return err
}
if n, _ := io.CopyN(io.Discard, r, 1); n > 0 {
return ErrTrailingBytes
}
return nil
}