1
2
3
4
5 package wycheproof
6
7 import (
8 "bytes"
9 "io"
10 "testing"
11
12 "golang.org/x/crypto/hkdf"
13 )
14
15 func TestHkdf(t *testing.T) {
16
17
18 type HkdfTestVector struct {
19
20
21 Comment string `json:"comment,omitempty"`
22
23
24 Flags []string `json:"flags,omitempty"`
25
26
27 Ikm string `json:"ikm,omitempty"`
28
29
30 Info string `json:"info,omitempty"`
31
32
33 Okm string `json:"okm,omitempty"`
34
35
36 Result string `json:"result,omitempty"`
37
38
39 Salt string `json:"salt,omitempty"`
40
41
42 Size int `json:"size,omitempty"`
43
44
45 TcId int `json:"tcId,omitempty"`
46 }
47
48
49 type Notes struct {
50 }
51
52
53 type HkdfTestGroup struct {
54
55
56 KeySize int `json:"keySize,omitempty"`
57 Tests []*HkdfTestVector `json:"tests,omitempty"`
58 Type interface{} `json:"type,omitempty"`
59 }
60
61
62 type Root struct {
63
64
65 Algorithm string `json:"algorithm,omitempty"`
66
67
68 GeneratorVersion string `json:"generatorVersion,omitempty"`
69
70
71 Header []string `json:"header,omitempty"`
72
73
74 Notes *Notes `json:"notes,omitempty"`
75
76
77 NumberOfTests int `json:"numberOfTests,omitempty"`
78 Schema interface{} `json:"schema,omitempty"`
79 TestGroups []*HkdfTestGroup `json:"testGroups,omitempty"`
80 }
81
82 fileHashAlgorithms := map[string]string{
83 "hkdf_sha1_test.json": "SHA-1",
84 "hkdf_sha256_test.json": "SHA-256",
85 "hkdf_sha384_test.json": "SHA-384",
86 "hkdf_sha512_test.json": "SHA-512",
87 }
88
89 for f := range fileHashAlgorithms {
90 var root Root
91 readTestVector(t, f, &root)
92 for _, tg := range root.TestGroups {
93 for _, tv := range tg.Tests {
94 h := parseHash(fileHashAlgorithms[f]).New
95 hkdf := hkdf.New(h, decodeHex(tv.Ikm), decodeHex(tv.Salt), decodeHex(tv.Info))
96 key := make([]byte, tv.Size)
97 wantPass := shouldPass(tv.Result, tv.Flags, nil)
98 _, err := io.ReadFull(hkdf, key)
99 if (err == nil) != wantPass {
100 t.Errorf("tcid: %d, type: %s, comment: %q, wanted success: %t, got: %v", tv.TcId, tv.Result, tv.Comment, wantPass, err)
101 }
102 if err != nil {
103 continue
104 }
105 if got, want := key, decodeHex(tv.Okm); !bytes.Equal(got, want) {
106 t.Errorf("tcid: %d, type: %s, comment: %q, output bytes don't match", tv.TcId, tv.Result, tv.Comment)
107 }
108 }
109 }
110 }
111 }
112
View as plain text