Debian Codegraph @

keyderivation/keyderivation_key_templates_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 keyderivation_testscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation_test`/
16
17 import (
18 "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/"
19 "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/"
20 "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/"
21
22 "github.com/tink-crypto/tink-go/v2/aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/"
23 "github.com/tink-crypto/tink-go/v2/keyderivationscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation`/"
24 "github.com/tink-crypto/tink-go/v2/keysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/"
25 "github.com/tink-crypto/tink-go/v2/prfscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/prf`/"
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 tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/ "github.com/tink-crypto/tink-go/v2/proto/tink_go_protoscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/"
28 )
29
30 func TestPRFBasedKeyTemplateDerivesAESGCMKeysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation_test`/TestPRFBasedKeyTemplateDerivesAESGCMKeyset().(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 plaintext := 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)
32 associatedData := 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().(8)
33 prfs := []struct {
34 name string
35 template *tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/.KeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#
36 }{
37 {
38 name: "HKDF-SHA256",
39 template: prfscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/prf`/.HKDFSHA256PRFKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/prf`/HKDFSHA256PRFKeyTemplate().(),
40 },
41 }
42 derivations := []struct {
43 name string
44 template *tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/.KeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#
45 }{
46 {
47 name: "AES128GCM",
48 template: aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.AES128GCMKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/AES128GCMKeyTemplate().(),
49 },
50 {
51 name: "AES256GCM",
52 template: aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.AES256GCMKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/AES256GCMKeyTemplate().(),
53 },
54 {
55 name: "AES256GCMNoPrefix",
56 template: aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.AES256GCMNoPrefixKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/AES256GCMNoPrefixKeyTemplate().(),
57 },
58 }
59 for _, prf := range prfs {
60 for _, der := range derivations {
61 for _, salt := range [][]byte{nil, []byte("salt")} {
62 name := 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_%s", prf.name, der.name)
63 if salt != nil {
64 name += "_with_salt"
65 }
66 t.Runscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Run().(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#) {
67 template, err := keyderivationscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation`/.CreatePRFBasedKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation`/CreatePRFBasedKeyTemplate().(prf.template, der.template)
68 if err != nil {
69 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("CreatePRFBasedKeyTemplate() err = %v, want nil", err)
70 }
71 handle, err := keysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/.NewHandlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/NewHandle().(template)
72 if err != nil {
73 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("keyset.NewHandle() err = %v, want nil", err)
74 }
75 d, err := keyderivationscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation`/.Newscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation`/New().(handle)
76 if err != nil {
77 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("keyderivation.New() err = %v, want nil", err)
78 }
79 derivedHandle, err := d.DeriveKeysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation`/KeysetDeriver#DeriveKeyset.(salt)
80 if err != nil {
81 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("DeriveKeyset() err = %v, want nil", err)
82 }
83 a, err := aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.Newscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/New().(derivedHandle)
84 if err != nil {
85 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("aead.New() err = %v, want nil", err)
86 }
87 ciphertext, err := a.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#Encrypt.(plaintext, associatedData)
88 if err != nil {
89 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("Encrypt() err = %v, want nil", err)
90 }
91 gotPlaintext, err := a.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#Decrypt.(ciphertext, associatedData)
92 if err != nil {
93 t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("Decrypt() err = %v, want nil", err)
94 }
95 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().(gotPlaintext, plaintext) {
96 t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Decrypt() = %v, want %v", gotPlaintext, plaintext)
97 }
98 })
99 }
100 }
101 }
102 }
103
104 func TestInvalidPRFBasedDeriverKeyTemplatesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation_test`/TestInvalidPRFBasedDeriverKeyTemplates().(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#) {
105 for _, test := range []struct {
106 name string
107 prfKeyTemplate *tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/.KeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#
108 derivedKeyTemplate *tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/.KeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#
109 }{
110 {
111 name: "nil templates",
112 },
113 {
114 name: "nil PRF key template",
115 derivedKeyTemplate: aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.AES128GCMKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/AES128GCMKeyTemplate().(),
116 },
117 {
118 name: "nil derived key template",
119 prfKeyTemplate: prfscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/prf`/.HKDFSHA256PRFKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/prf`/HKDFSHA256PRFKeyTemplate().(),
120 },
121 {
122 name: "malformed PRF key template",
123 prfKeyTemplate: &tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/.KeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#{TypeUrlscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate#TypeUrl.: "\xff"},
124 derivedKeyTemplate: aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.AES128GCMKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/AES128GCMKeyTemplate().(),
125 },
126 // AES128CTRHMACSHA256KeyTemplate() is an unsupported derived key template
127 // because DeriveKey() is not implemented in the AES-CTR-HMAC key manager.
128 // TODO(b/227682336): Add mock key manager that doesn't derive keys.
129 {
130 name: "unsupported templates",
131 prfKeyTemplate: aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.AES128GCMKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/AES128GCMKeyTemplate().(),
132 derivedKeyTemplate: aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.AES128CTRHMACSHA256KeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/AES128CTRHMACSHA256KeyTemplate().()},
133 {
134 name: "unsupported PRF key template",
135 prfKeyTemplate: aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.AES128GCMKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/AES128GCMKeyTemplate().(),
136 derivedKeyTemplate: aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.AES128GCMKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/AES128GCMKeyTemplate().(),
137 },
138 {
139 name: "unsupported derived key template",
140 prfKeyTemplate: prfscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/prf`/.HKDFSHA256PRFKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/prf`/HKDFSHA256PRFKeyTemplate().(),
141 derivedKeyTemplate: aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/.AES128CTRHMACSHA256KeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead`/AES128CTRHMACSHA256KeyTemplate().(),
142 },
143 } {
144 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#) {
145 if _, err := keyderivationscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation`/.CreatePRFBasedKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyderivation`/CreatePRFBasedKeyTemplate().(test.prfKeyTemplate, test.derivedKeyTemplate); err == nil {
146 t.Errorscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Error().("CreatePRFBasedKeyTemplate() err = nil, want non-nil")
147 }
148 })
149 }
150 }