Debian Codegraph @

keyderivation/internal/streamingprf/hkdf_streaming_prf_test.go

raw symbols SCIP
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package streamingprfscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/
16
17 import (
18 "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/"
19 "encoding/hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/"
20 "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/"
21 "ioscip-go gomod github.com/golang/go/src go1.24.0 io/"
22 "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/"
23 "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/"
24
25 "github.com/tink-crypto/tink-go/v2/internal/testing/wycheproofscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/"
26 "github.com/tink-crypto/tink-go/v2/subtle/randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/"
27 "github.com/tink-crypto/tink-go/v2/testutilscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testutil`/"
28 )
29
30 func TestNewHKDFStreamingPRFscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/TestNewHKDFStreamingPRF().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
31 for _, test := range []struct {
32 name string
33 hash string
34 salt []byte
35 }{
36 {
37 name: "SHA256_nil_salt",
38 hash: "SHA256",
39 },
40 {
41 name: "SHA256_random_salt",
42 hash: "SHA256",
43 salt: randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(16),
44 },
45 {
46 name: "SHA512_nil_salt",
47 hash: "SHA512",
48 },
49 {
50 name: "SHA512_random_salt",
51 hash: "SHA512",
52 salt: randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(16),
53 },
54 } {
55 t.Runscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Run().(test.name, func(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
56 key := randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(32)
57 h, err := NewHKDFStreamingPRFscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/NewHKDFStreamingPRF().(test.hash, key, test.salt)
58 if err != nil {
59 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("NewHKDFStreamingPRF() err = %v, want nil", err)
60 }
61 if !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(h.keyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/HKDFStreamingPRF#key., key) {
62 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("key = %v, want %v", h.keyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/HKDFStreamingPRF#key., key)
63 }
64 if !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(h.saltscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/HKDFStreamingPRF#salt., test.salt) {
65 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("salt = %v, want %v", h.saltscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/HKDFStreamingPRF#salt., test.salt)
66 }
67 })
68 }
69 }
70
71 func TestNewHKDFStreamingPRFFailsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/TestNewHKDFStreamingPRFFails().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
72 for _, test := range []struct {
73 hash string
74 keySize uint32
75 }{
76 {
77 hash: "SHA256",
78 keySize: 16,
79 },
80 {
81 hash: "SHA512",
82 keySize: 16},
83 {
84 hash: "SHA1",
85 keySize: 20,
86 },
87 } {
88 t.Runscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Run().(test.hash, func(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
89 if _, err := NewHKDFStreamingPRFscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/NewHKDFStreamingPRF().(test.hash, randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(test.keySize), nil); err == nil {
90 t.Errorscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Error().("NewHKDFStreamingPRF() err = nil, want non-nil")
91 }
92 })
93 }
94 }
95
96 func TestHKDFStreamingPRFWithRFCVectorscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/TestHKDFStreamingPRFWithRFCVector().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
97 // This is the only vector that uses an accepted hash function and has key
98 // size >= minHKDFStreamingPRFKeySize.
99 // https://www.rfc-editor.org/rfc/rfc5869#appendix-A.2
100 vec := struct {
101 hash string
102 key string
103 salt string
104 info string
105 outLen int
106 okm string
107 }{
108 hash: "SHA256",
109 key: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
110 salt: "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
111 info: "b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
112 outLen: 82,
113 okm: "b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c59045a99cac7827271cb41c65e590e09da3275600c2f09b8367793a9aca3db71cc30c58179ec3e87c14c01d5c1f3434f1d87",
114 }
115 key, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(vec.key)
116 if err != nil {
117 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString err = %v, want nil", err)
118 }
119 salt, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(vec.salt)
120 if err != nil {
121 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString err = %v, want nil", err)
122 }
123 info, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(vec.info)
124 if err != nil {
125 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString err = %v, want nil", err)
126 }
127
128 h, err := NewHKDFStreamingPRFscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/NewHKDFStreamingPRF().(vec.hash, key, salt)
129 if err != nil {
130 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("NewHKDFStreamingPRF() err = %v, want nil", err)
131 }
132 r, err := h.Computescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/HKDFStreamingPRF#Compute().(info)
133 if err != nil {
134 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("Compute() err = %v, want nil", err)
135 }
136 out := make([]byte, vec.outLen)
137 if _, err := ioscip-go gomod github.com/golang/go/src go1.24.0 io/.ReadAtLeastscip-go gomod github.com/golang/go/src go1.24.0 io/ReadAtLeast().(r, out, len(out)); err != nil {
138 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("io.ReadAtLeast err = %v, want nil", err)
139 }
140 if hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.EncodeToStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/EncodeToString().(out) != vec.okm {
141 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Compute() = %v, want %v", hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.EncodeToStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/EncodeToString().(out), vec.okm)
142 }
143 }
144
145 func TestHKDFStreamingPRFWithWycheproofscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/TestHKDFStreamingPRFWithWycheproof().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
146 type hkdfCase struct {
147 wycheproofscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/.Casescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/Case#
148 IKM testutilscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testutil`/.HexBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testutil`/HexBytes# `json:"ikm"`
149 Salt testutilscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testutil`/.HexBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testutil`/HexBytes# `json:"salt"`
150 Info testutilscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testutil`/.HexBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testutil`/HexBytes# `json:"info"`
151 Size uint32 `json:"size"`
152 OKM testutilscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testutil`/.HexBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testutil`/HexBytes# `json:"okm"`
153 }
154 type hkdfGroup struct {
155 wycheproofscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/.Groupscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/Group#
156 KeySize uint32 `json:"keySize"`
157 Tests []*hkdfCase `json:"tests"`
158 }
159 type hkdfSuite struct {
160 wycheproofscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/.Suitescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/Suite#
161 TestGroups []*hkdfGroup `json:"testGroups"`
162 }
163
164 count := 0
165 for _, hash := range []string{"SHA256", "SHA512"} {
166 filename := fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("hkdf_%s_test.json", stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.24.0 strings/ToLower().(hash))
167 suite := new(hkdfSuite)
168 wycheproofscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/.PopulateSuiteV1scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/PopulateSuiteV1().(t, suite, filename)
169
170 for _, group := range suite.TestGroups {
171 for _, test := range group.Tests {
172 caseName := fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%s(%d):Case-%d", hash, group.KeySize, test.CaseIDscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/Case#CaseID.)
173 t.Runscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Run().(caseName, func(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) {
174 if got, want := len(test.IKM), int(group.KeySize/8); got != want {
175 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("invalid key length = %d, want %d", got, want)
176 }
177 count++
178
179 h, err := NewHKDFStreamingPRFscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/NewHKDFStreamingPRF().(hash, test.IKM, test.Salt)
180 switch test.Resultscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/Case#Result. {
181 case "valid":
182 if len(test.IKM) < minHKDFStreamingPRFKeySizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/minHKDFStreamingPRFKeySize. {
183 if err == nil {
184 t.Errorscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Error().("NewHKDFStreamingPRF err = nil, want non-nil")
185 }
186 return
187 }
188 if err != nil {
189 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("NewHKDFStreamingPRF err = %v, want nil", err)
190 }
191 r, err := h.Computescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/HKDFStreamingPRF#Compute().(test.Info)
192 if err != nil {
193 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("Compute() err = %v, want nil", err)
194 }
195 out := make([]byte, test.Size)
196 if _, err := ioscip-go gomod github.com/golang/go/src go1.24.0 io/.ReadAtLeastscip-go gomod github.com/golang/go/src go1.24.0 io/ReadAtLeast().(r, out, len(out)); err != nil {
197 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("io.ReadAtLeast err = %v, want nil", err)
198 }
199 if !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(out, test.OKM) {
200 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Compute() = %v, want %v", out, test.OKM)
201 }
202
203 case "invalid":
204 if err != nil {
205 return
206 }
207 r, err := h.Computescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation/internal/streamingprf`/HKDFStreamingPRF#Compute().(test.Info)
208 if err != nil {
209 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("Compute() err = %v, want nil", err)
210 }
211 out := make([]byte, test.Size)
212 if _, err := ioscip-go gomod github.com/golang/go/src go1.24.0 io/.ReadAtLeastscip-go gomod github.com/golang/go/src go1.24.0 io/ReadAtLeast().(r, out, len(out)); err == nil {
213 t.Errorscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Error().("io.ReadAtLeast err = nil, want non-nil")
214 }
215
216 default:
217 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("unsupported test result: %s", test.Resultscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/Case#Result.)
218 }
219 })
220 }
221 }
222 }
223 if count < 169 {
224 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("number of test cases = %d, want >= 169", count)
225 }
226 }