-
Notifications
You must be signed in to change notification settings - Fork 9
/
password_test.go
388 lines (350 loc) · 13.4 KB
/
password_test.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
goSecretBoxPassword - Golang Password Hashing & Encryption Library
Copyright (C) 2017 Darwin Smith
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package password
import (
"fmt"
"testing"
"github.com/icrowley/fake"
)
func TestBench(t *testing.T) {
// 2<<14 = 16384
params := ScryptParams{N: 2 << 14, R: 8, P: 1}
runs := 10
total := 0.0
for i := 0; i < runs; i++ {
result, err := Benchmark(params)
if err != nil {
t.Log(err)
t.FailNow()
}
total += result
}
fmt.Printf("Benchmark Result (avg): %v of %v runs, with Scrypt params: N:%v R:%v P:%v\n", total/float64(runs), runs, params.N, params.R, params.P)
// Test Bench hash error
result, err := Benchmark(ScryptParams{N: 2048, R: 8, P: 1})
if err != ErrScryptParamN && result != 0 {
t.Log(err)
t.FailNow()
}
}
func TestHash(t *testing.T) {
var total int
runs := 52
// Test expect success
for i := 2; i < runs; i++ {
// Generate Test Password
testUserPass := fake.Password(i*4, 1000, true, true, true)
testMasterPass := fake.Password(i*4, 1000, true, true, true)
// Hash Password
output, err := Hash(testUserPass, testMasterPass, 0, ScryptParams{N: 32768, R: 16, P: 1}, DefaultParams)
if err != nil {
t.Log(err)
t.FailNow()
}
t.Logf("Output: " + output)
// Check Output Length
lgth := len(output)
if lgth > 225 {
t.Logf("Output Length over 225 chars at %v for input userpass length %v", lgth, len(testUserPass))
}
total = total + lgth
// Verify Password
if err := Verify(testUserPass, testMasterPass, output); err != nil {
t.Log(err)
t.FailNow()
}
}
fmt.Printf("Average length of %v runs: %v\n", runs, total/runs)
// Test with Bad Params
_, err := Hash("password1234", "masterpassphrase", 0, ScryptParams{N: 2048, R: 16, P: 1}, DefaultParams)
if err != ErrScryptParamN {
t.Log("Expected Scrypt N failure for user params")
t.FailNow()
}
_, err = Hash("password1234", "masterpassphrase", 0, DefaultParams, ScryptParams{N: 2048, R: 16, P: 1})
if err != ErrScryptParamN {
t.Log("Expected Scrypt N failure for master params")
t.FailNow()
}
}
func TestGetHashVersion(t *testing.T) {
v, err := GetHashVersion("secBoxv1$0$tKTBCfdTcn5gA9xRR9yPNczryWV/f+7MVkdDgxFtuYuzvTcNGMNTHBE2pCoPjRjTDIN1449gwVHfrkzvkzWdwZBEUCVWVZFjlRTdu8kCD7uBDmfozwyX+U/T7k8cyfaHFgB8y8cPEvk=$Ek8NWSL34KE=$32768$16$1$16384$8$1")
if err != nil {
t.Log(err)
t.FailNow()
}
fmt.Println("Hash Version: ", v)
// Test with Bad Version; secBoxvb instead of expected secBoxv1
_, err = GetHashVersion("secBoxvb$0$tKTBCfdTcn5gA9xRR9yPNczryWV/f+7MVkdDgxFtuYuzvTcNGMNTHBE2pCoPjRjTDIN1449gwVHfrkzvkzWdwZBEUCVWVZFjlRTdu8kCD7uBDmfozwyX+U/T7k8cyfaHFgB8y8cPEvk=$Ek8NWSL34KE=$32768$16$1$16384$8$1")
if err == nil {
t.Log("Expected int parse from string error")
t.FailNow()
}
}
func TestGetMasterVersion(t *testing.T) {
v, err := GetMasterVersion("secBoxv1$0$tKTBCfdTcn5gA9xRR9yPNczryWV/f+7MVkdDgxFtuYuzvTcNGMNTHBE2pCoPjRjTDIN1449gwVHfrkzvkzWdwZBEUCVWVZFjlRTdu8kCD7uBDmfozwyX+U/T7k8cyfaHFgB8y8cPEvk=$Ek8NWSL34KE=$32768$16$1$16384$8$1")
if err != nil {
t.Log(err)
t.FailNow()
}
fmt.Println("Master Version: ", v)
// Test Bad Master Version; x instead of 0
_, err = GetMasterVersion("secBoxv1$x$tKTBCfdTcn5gA9xRR9yPNczryWV/f+7MVkdDgxFtuYuzvTcNGMNTHBE2pCoPjRjTDIN1449gwVHfrkzvkzWdwZBEUCVWVZFjlRTdu8kCD7uBDmfozwyX+U/T7k8cyfaHFgB8y8cPEvk=$Ek8NWSL34KE=$32768$16$1$16384$8$1")
if err == nil {
t.Log("Expected string to int parse failure")
t.FailNow()
}
}
func TestGetParamsFromHash(t *testing.T) {
user, master, err := GetParams("secBoxv1$1$5DxIID0p4uz073qNngNsxYhXKPJITbjdvpjLju/XKbbzKDjdXVvgCSVbNIjCAg2QvA8O4mC+/fZpExJJx9rVpgxeL4xH16kN5/AGHtaa3kPNlP0tB5dJjDbFsJVr7u/ar9v4hzwQYhk=$xGvsvszfJDY=$32768$16$1$16384$8$1")
if err != nil {
t.Log(err)
t.FailNow()
}
fmt.Printf("From Hash-> User Params: N-%v R-%v P-%v Master Params: N-%v R-%v P-%v\n", user.N, user.R, user.P, master.N, master.R, master.P)
// Test with Bad Format
user, master, err = GetParams("secBoxv1$5DxIID0p4uz073qNngNsxYhXKPJITbjdvpjLju/XKbbzKDjdXVvgCSVbNIjCAg2QvA8O4mC+/fZpExJJx9rVpgxeL4xH16kN5/AGHtaa3kPNlP0tB5dJjDbFsJVr7u/ar9v4hzwQYhk=$xGvsvszfJDY=$32768$16$1$16384$8$1")
if err != ErrCiphertextFormat {
t.Log("Expected invalid format error")
t.FailNow()
}
}
func TestGetParams(t *testing.T) {
parts := []string{"0", "1", "2", "3", "8192", "8", "1", "8192", "8", "1"}
user, master, err := getParams(parts)
if err != nil {
t.Log(err)
t.FailNow()
}
if user.N != 8192 || user.R != 8 || user.P != 1 {
t.Log("Returned parameters do not match given")
t.FailNow()
}
if master.N != 8192 || master.R != 8 || master.P != 1 {
t.Log("Returned parameters do not match given")
t.FailNow()
}
fmt.Printf("User Params: N-%v R-%v P-%v Master Params: N-%v R-%v P-%v\n", user.N, user.R, user.P, master.N, master.R, master.P)
// Invalid Params parts[4]-parts[9], should return err for each
for i := 4; i < len(parts); i++ {
parts = []string{"0", "1", "2", "3", "8192", "8", "1", "8192", "8", "1"}
parts[i] = "x"
user, master, err = getParams(parts)
if err == nil {
t.Log(err)
t.FailNow()
}
}
}
func TestUpdateMaster(t *testing.T) {
output, err := Hash("password1234", "masterpassphrase", 0, ScryptParams{N: 32768, R: 16, P: 1}, DefaultParams)
if err != nil {
t.Log(err)
t.FailNow()
}
fmt.Println("Output: " + output)
fmt.Printf("Length: %v\n", len(output))
err = Verify("password1234", "masterpassphrase", output)
if err != nil {
t.Log(err)
t.FailNow()
}
// Update then re-verify
updated, err := UpdateMaster("masterpassphrase2", "masterpassphrase", 1, output, DefaultParams)
if err != nil {
t.Log(err)
t.FailNow()
}
fmt.Println("Updated Output: " + updated)
fmt.Printf("Updated Length: %v\n", len(updated))
err = Verify("password1234", "masterpassphrase2", updated)
if err != nil {
t.Log(err)
t.FailNow()
}
// Test Bad Format Fail
_, err = UpdateMaster("masterpassphrase2", "masterpassphrase", 1, "secBoxv1$l8W69jygGur7sa0669mAJnIuYgjsbkx4wd+RdDzwIn2Z49FJurWkJDx2NA8g+ED9Nn6vGCLNFoHXSDIDeDBvJXouxs5zyX6mVozceVAVO7IadrL4+KKohV3MzoVlgodUYeNToOVB/5A=$4LZVjQ8P9pA=$32768$16$1$16384$8$1", DefaultParams)
if err != ErrCiphertextFormat {
t.Log("Expected Ciphertext format failure")
t.FailNow()
}
// Test Bad Params Fail
_, err = UpdateMaster("masterpassphrase2", "masterpassphrase", 1, "secBoxv1$0$l8W69jygGur7sa0669mAJnIuYgjsbkx4wd+RdDzwIn2Z49FJurWkJDx2NA8g+ED9Nn6vGCLNFoHXSDIDeDBvJXouxs5zyX6mVozceVAVO7IadrL4+KKohV3MzoVlgodUYeNToOVB/5A=$4LZVjQ8P9pA=$32768$16$1$16384$8$1", ScryptParams{N: 2048, R: 8, P: 1})
if err != ErrScryptParamN {
t.Log(err)
t.Log("Expected Scrypt N param failure")
t.FailNow()
}
// Test Bad Old Master passphrase, decrypt fail
_, err = UpdateMaster("masterpassphrase2", "incorrectmaster", 1, "secBoxv1$0$l8W69jygGur7sa0669mAJnIuYgjsbkx4wd+RdDzwIn2Z49FJurWkJDx2NA8g+ED9Nn6vGCLNFoHXSDIDeDBvJXouxs5zyX6mVozceVAVO7IadrL4+KKohV3MzoVlgodUYeNToOVB/5A=$4LZVjQ8P9pA=$32768$16$1$16384$8$1", DefaultParams)
if err != ErrSecretBoxDecryptFail {
t.Log(err)
t.Log("Expected decryption failure")
t.FailNow()
}
}
func TestUpdateMasterBadVersion(t *testing.T) {
output, err := Hash("password1234", "masterpassphrase", 0, ScryptParams{N: 32768, R: 16, P: 1}, DefaultParams)
if err != nil {
t.Log(err)
t.FailNow()
}
fmt.Println("Output: " + output)
fmt.Printf("Length: %v\n", len(output))
err = Verify("password1234", "masterpassphrase", output)
if err != nil {
t.Log(err)
t.FailNow()
}
// Update then re-verify
_, err = UpdateMaster("masterpassphrase2", "masterpassphrase", 0, output, DefaultParams)
if err != ErrInvalidVersionUpdate {
t.Log("Expected Invalid Version update error")
t.FailNow()
}
}
func TestHashShortPassphrase(t *testing.T) {
// Errors should not be nil, fail if errors nil
_, err := Hash("pass", "masterpassphrase", 0, ScryptParams{N: 32768, R: 16, P: 1}, DefaultParams)
if err != ErrPassphraseLength {
t.Log("Expected Passphrase length failure")
t.FailNow()
}
_, err = Hash("password1234", "master", 0, ScryptParams{N: 32768, R: 16, P: 1}, DefaultParams)
if err != ErrPassphraseLength {
t.Log("Expected Passphrase length failure")
t.FailNow()
}
}
func TestVerify(t *testing.T) {
err := Verify("password1234", "masterpassphrase", "secBoxv1$0$Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=$2ZVcHyy/p9Q=$32768$16$1$16384$8$1")
if err != nil {
t.Log(err)
t.FailNow()
}
}
func TestVerifyV1(t *testing.T) {
// Fail Length
err := verifyV1("password1234", "masterpassphrase", []string{"secBoxv1", "Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=", "2ZVcHyy/p9Q=", "32768", "16", "1", "16384", "8", "1"})
if err != ErrCiphertextFormat {
t.Log("Expected Format Failure")
t.FailNow()
}
// Fail ciphertext version check
err = verifyV1("password1234", "masterpassphrase", []string{"secBoxv0", "0", "Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=", "2ZVcHyy/p9Q=", "32768", "16", "1", "16384", "8", "1"})
if err != ErrCiphertextVer {
t.Log("Expect Version Failure")
t.FailNow()
}
}
func TestVerifyFormat(t *testing.T) {
err := Verify("password", "masterpassphrase", "secBoxv1$0$Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=$2ZVcHyy/p9Q=$32768$16$1$16384$8$1")
if err == nil {
t.Log(err)
t.FailNow()
}
}
func TestVerifyVersion(t *testing.T) {
err := Verify("password", "masterpassphrase", "secBoxv0$0$Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=$2ZVcHyy/p9Q=$32768$16$1$16384$8$1")
if err == nil {
t.Log(err)
t.FailNow()
}
}
func TestVerifyParseParams(t *testing.T) {
// These should fail if errors are not returned
err := Verify("password", "masterpassphrase", "secBoxv0$0$Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=$2ZVcHyy/p9Q=$32768$16$1$16384$8$a")
if err == nil {
t.Log(err)
t.FailNow()
}
err = Verify("password", "masterpassphrase", "secBoxv0$0$Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=$2ZVcHyy/p9Q=$32768$16$1$16384$b$1")
if err == nil {
t.Log(err)
t.FailNow()
}
err = Verify("password", "masterpassphrase", "secBoxv0$0$Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=$2ZVcHyy/p9Q=$32768$16$1$c$b$1")
if err == nil {
t.Log(err)
t.FailNow()
}
}
func TestVerifyBadParams(t *testing.T) {
err := Verify("password", "masterpassphrase", "secBoxv0$0$Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=$2ZVcHyy/p9Q=$32768$16$1$16384$999$1")
if err == nil {
t.Log(err)
t.FailNow()
}
}
func TestVerifyBadPassphrase(t *testing.T) {
err := Verify("passw0rd", "masterpassphrase", "secBoxv1$0$Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=$2ZVcHyy/p9Q=$32768$16$1$16384$8$1")
if err == nil {
t.Log(err)
t.FailNow()
}
}
func TestVerifyBadMasterPass(t *testing.T) {
err := Verify("password", "mast3rpassphrase", "secBoxv1$0$Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=$2ZVcHyy/p9Q=$32768$16$1$16384$8$1")
if err == nil {
t.Log(err)
t.FailNow()
}
}
func TestVerifyBadSalt(t *testing.T) {
err := Verify("password", "mast3rpassphrase", "secBoxv1$0$Qk09Tgzi2w+z9mtPiwe6uLWPXMY8WQyI3oC7Sqz11PMcRzvqrOhd70fdBXEUmOeM91z2MytB9Lt4VQzjOs21KTYqMx9FwUR2qDa38fmQhT6pLOJCaptpMzgYLC1fvbq4suuW9XpB7RE=$2ZVbHyy/p9Q=$32768$16$1$16384$8$1")
if err == nil {
t.Log(err)
t.FailNow()
}
}
func TestValidateParams(t *testing.T) {
// This should pass
err := validateParams(ScryptParams{N: 65536, R: 32, P: 2})
if err != nil {
t.Log(err)
t.FailNow()
}
// These should fail
err = validateParams(ScryptParams{N: 10, R: 32, P: 2})
if err == nil {
t.Log(err)
t.FailNow()
}
err = validateParams(ScryptParams{N: 601000, R: 32, P: 2})
if err == nil {
t.Log(err)
t.FailNow()
}
err = validateParams(ScryptParams{N: 32500, R: 2, P: 2})
if err == nil {
t.Log(err)
t.FailNow()
}
err = validateParams(ScryptParams{N: 32500, R: 130, P: 2})
if err == nil {
t.Log(err)
t.FailNow()
}
err = validateParams(ScryptParams{N: 32500, R: 32, P: 0})
if err == nil {
t.Log(err)
t.FailNow()
}
err = validateParams(ScryptParams{N: 32500, R: 32, P: 22})
if err == nil {
t.Log(err)
t.FailNow()
}
}