-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscrypt.go
260 lines (217 loc) · 6.17 KB
/
scrypt.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//go:build go1.11
// +build go1.11
package passwd
import (
"bytes"
"crypto/subtle"
"fmt"
"strconv"
"golang.org/x/crypto/scrypt"
)
const (
idScrypt = "2s"
)
var (
/*
https://tools.ietf.org/html/rfc7914
Users of scrypt can tune the parameters N, r, and p according to the
amount of memory and computing power available, the latency-bandwidth
product of the memory subsystem, and the amount of parallelism
desired. At the current time, r=8 and p=1 appears to yield good
results, but as memory latency and CPU parallelism increase, it is
likely that the optimum values for both r and p will increase. Note
also that since the computations of SMix are independent, a large
value of p can be used to increase the computational cost of scrypt
...
N:
The CPU/Memory cost parameter N ("costParameter") must be larger than 1,
a power of 2, and less than 2 ^ (128 * r / 8).
R:
The parameter r ("blockSize") specifies the block size.
P:
The parallelization parameter p ("parallelizationParameter") is a positive integer
less than or equal to ((2^32-1) * 32) / (128 * r)
https://godoc.org/golang.org/x/crypto/scrypt :
The recommended parameters for interactive logins as of 2017 are N=32768, r=8 and p=1.
The parameters N, r, and p should be increased as memory latency and CPU parallelism increases;
consider setting N to the highest power of 2 you can derive within 100 milliseconds.
Remember to get a good random salt.
another source and interpretation of those numbers by crypto gopher:
https://blog.filippo.io/the-scrypt-parameters
*/
scryptMinParameters = ScryptParams{
N: 1 << 16,
R: 8,
P: 1,
Saltlen: 16,
Keylen: 32,
// salt
//Masked: false,
}
scryptCommonParameters = ScryptParams{
N: 1 << 16,
R: 8,
P: 1,
Saltlen: 16,
Keylen: 32,
// salt
//Masked: false,
}
scryptParanoidParameters = ScryptParams{
N: 1 << 17,
R: 32,
P: 2,
Saltlen: 32,
Keylen: 64,
// salt
//Masked: false,
}
)
// ScryptParams are the parameters for the scrypt key derivation.
type ScryptParams struct {
N uint32 // cpu memory cost must be > 1 && %2 == 0
R uint32 // parallelization cost param -> r*p < 2^30 (go implementation specific)
P uint32 // parallelization cost param -> r*p < 2^30 (go implementation specific)
Saltlen uint32 // 128 bits min.
Keylen uint32 // 128 bits min.
Masked bool // are parameters private
salt []byte // my salt..
secret []byte // secret for key'ed hashes..
}
// TODO must return salt
func newScryptParamsFromFields(fields []string) (*ScryptParams, error) {
if len(fields) != 6 {
return nil, ErrParse
}
// salt
salt, err := base64Decode([]byte(fields[0])) // process the salt
if err != nil {
return nil, ErrParse
}
saltlen := uint32(len(salt))
nint, err := strconv.ParseInt(fields[1], 10, 32)
if err != nil {
return nil, ErrParse
}
n := uint32(nint)
rint, err := strconv.ParseInt(fields[2], 10, 32)
if err != nil {
return nil, ErrParse
}
r := uint32(rint)
pint, err := strconv.ParseInt(fields[3], 10, 32)
if err != nil {
return nil, ErrParse
}
p := uint32(pint)
keylenint, err := strconv.ParseInt(fields[4], 10, 32)
if err != nil {
return nil, ErrParse
}
keylen := uint32(keylenint)
sp := ScryptParams{
N: n,
R: r,
P: p,
Saltlen: saltlen,
Keylen: keylen,
//salt: salt,
}
return &sp, nil
}
// function that validate custom parameters and minimal security is ok.
// will upgrade over the years
// XXX TODO
func (p *ScryptParams) validate(min *ScryptParams) error {
// XXX TODO
return nil
}
func (p *ScryptParams) deriveFromPassword(password []byte) ([]byte, error) {
key, err := scrypt.Key(password, p.salt, int(p.N), int(p.R), int(p.P), int(p.Keylen))
if err != nil {
return nil, err
}
return key, nil
}
// func (p *ScryptParams) generateFromParams(password []byte) (out []byte, err error) {
func (p *ScryptParams) generateFromParams(salt, password []byte) (out []byte, err error) {
var hash bytes.Buffer
var params string
var data []byte
// if salt mismatch, the profile dictactes, not the hash.
// the profile dictactes
psalt := make([]byte, p.Saltlen)
copy(psalt, salt)
// password
data = password
// we want to hmac a secret to have the resulting hash
if len(p.secret) > 0 {
data, err = hmacKeyHash(p.secret, psalt, password)
if err != nil {
return nil, err
}
}
key, err := scrypt.Key(data, psalt, int(p.N), int(p.R), int(p.P), int(p.Keylen))
if err != nil {
return nil, err
}
// need to b64.
salt64 := base64Encode(psalt)
// params
if !p.Masked {
params = fmt.Sprintf("%c%d%c%d%c%d%c%d",
separatorRune, p.N,
separatorRune, p.R,
separatorRune, p.P,
separatorRune, p.Keylen)
}
// encode the key
key64 := base64Encode(key)
passwordStr := fmt.Sprintf("%c%s%c%s%s%c%s",
separatorRune, idScrypt,
separatorRune, salt64,
params,
separatorRune, key64)
_, err = hash.WriteString(passwordStr)
if err != nil {
return nil, err
}
out = hash.Bytes()
//return hash.Bytes(), nil
return out, nil
}
func (p *ScryptParams) generateFromPassword(password []byte) ([]byte, error) {
salt, err := getSalt(p.Saltlen)
if err != nil {
return nil, err
}
return p.generateFromParams(salt, password)
}
func (p *ScryptParams) compare(hashed, password []byte) error {
salt, err := parseFromHashToSalt(hashed)
if err != nil {
fmt.Printf("compare parse error: %v\n", err)
return ErrMismatch
}
// generate the string to compare
compared, err := p.generateFromParams(salt, password)
if err != nil {
return ErrMismatch
}
/* the subtle package handles that already */
/*
hashlen := uint32(len(compared))
if uint32(len(hashed)) != hashlen {
return ErrMismatch
}
*/
// the hashed[:hashlen] is to avoid padded data invalid compare while the hash is actually good
// think like a wrongly defined database column type (i.e. char(255)) will return the string padded with spaces
// we end up removing those case, but we bound what we check above by making sure length are identical.
//
//if subtle.ConstantTimeCompare(compared, hashed[:hashlen]) == 1 {
if subtle.ConstantTimeCompare(compared, hashed) == 1 {
return nil
}
return ErrMismatch
}