Source file
src/crypto/pbkdf2/pbkdf2_wycheproof_test.go
1
2
3
4
5 package pbkdf2_test
6
7 import (
8 "bytes"
9 "crypto/internal/cryptotest/wycheproof"
10 "crypto/pbkdf2"
11 "crypto/sha1"
12 "crypto/sha256"
13 "crypto/sha512"
14 "hash"
15 "testing"
16 )
17
18 func TestWycheproof(t *testing.T) {
19 filesToHash := map[string]func() hash.Hash{
20 "pbkdf2_hmacsha1_test.json": sha1.New,
21 "pbkdf2_hmacsha224_test.json": sha256.New224,
22 "pbkdf2_hmacsha256_test.json": sha256.New,
23 "pbkdf2_hmacsha384_test.json": sha512.New384,
24 "pbkdf2_hmacsha512_test.json": sha512.New,
25 }
26
27 for file, h := range filesToHash {
28 var testdata wycheproof.PbkdfTestSchemaJson
29 wycheproof.LoadVectorFile(t, file, &testdata)
30
31 for _, tg := range testdata.TestGroups {
32 for _, tv := range tg.Tests {
33 t.Run(wycheproof.TestName(file, tv), func(t *testing.T) {
34 t.Parallel()
35
36 password := wycheproof.MustDecodeHex(tv.Password)
37 salt := wycheproof.MustDecodeHex(tv.Salt)
38 expectedDk := wycheproof.MustDecodeHex(tv.Dk)
39 wantPass := wycheproof.ShouldPass(t, tv.Result, tv.Flags, nil)
40
41 dk, err := pbkdf2.Key(h, string(password), salt, tv.IterationCount, tv.DkLen)
42 if err != nil {
43 if wantPass {
44 t.Fatalf("Key: %v", err)
45 }
46 return
47 }
48 if !wantPass {
49 t.Fatalf("Key unexpectedly succeeded")
50 return
51 }
52 if !bytes.Equal(dk, expectedDk) {
53 t.Errorf("derived key mismatch: got %x, want %x", dk, expectedDk)
54 }
55 })
56 }
57 }
58 }
59 }
60
View as plain text