File: kms_envelope_aead_test.go

package info (click to toggle)
golang-github-tink-crypto-tink-go 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,952 kB
  • sloc: sh: 864; makefile: 6
file content (287 lines) | stat: -rw-r--r-- 11,188 bytes parent folder | download
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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package aead_test

import (
	"bytes"
	"context"
	"encoding/hex"
	"testing"

	"github.com/tink-crypto/tink-go/v2/aead"
	"github.com/tink-crypto/tink-go/v2/testing/fakekms"
	tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
)

func TestKMSEnvelopeWorksWithTinkKeyTemplatesAsDekTemplate(t *testing.T) {
	keyURI := "fake-kms://CM2b3_MDElQKSAowdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUuY3J5cHRvLnRpbmsuQWVzR2NtS2V5EhIaEIK75t5L-adlUwVhWvRuWUwYARABGM2b3_MDIAE"
	kekAEAD, err := fakekms.NewAEAD(keyURI)
	if err != nil {
		t.Fatalf("fakekms.NewAEAD(keyURI) err = %q, want nil", err)
	}
	kekAEADWithContext, err := fakekms.NewAEADWithContext(keyURI)
	if err != nil {
		t.Fatalf("fakekms.NewAEADWithContext(keyURI) err = %q, want nil", err)
	}
	plaintext := []byte("plaintext")
	associatedData := []byte("associatedData")
	invalidAssociatedData := []byte("invalidAssociatedData")

	var kmsEnvelopeAeadDekTestCases = []struct {
		name        string
		dekTemplate *tinkpb.KeyTemplate
	}{
		{
			name:        "AES128_GCM",
			dekTemplate: aead.AES128GCMKeyTemplate(),
		}, {
			name:        "AES256_GCM",
			dekTemplate: aead.AES256GCMKeyTemplate(),
		}, {
			name:        "AES256_GCM_NO_PREFIX",
			dekTemplate: aead.AES256GCMNoPrefixKeyTemplate(),
		}, {
			name:        "AES128_GCM_SIV",
			dekTemplate: aead.AES128GCMSIVKeyTemplate(),
		}, {
			name:        "AES256_GCM_SIV",
			dekTemplate: aead.AES256GCMSIVKeyTemplate(),
		}, {
			name:        "AES256_GCM_SIV_NO_PREFIX",
			dekTemplate: aead.AES256GCMSIVNoPrefixKeyTemplate(),
		}, {
			name:        "AES128_CTR_HMAC_SHA256",
			dekTemplate: aead.AES128CTRHMACSHA256KeyTemplate(),
		}, {
			name:        "AES256_CTR_HMAC_SHA256",
			dekTemplate: aead.AES256CTRHMACSHA256KeyTemplate(),
		}, {
			name:        "CHACHA20_POLY1305",
			dekTemplate: aead.ChaCha20Poly1305KeyTemplate(),
		}, {
			name:        "XCHACHA20_POLY1305",
			dekTemplate: aead.XChaCha20Poly1305KeyTemplate(),
		},
	}
	for _, tc := range kmsEnvelopeAeadDekTestCases {
		t.Run(tc.name, func(t *testing.T) {
			a := aead.NewKMSEnvelopeAEAD2(tc.dekTemplate, kekAEAD)
			ciphertext, err := a.Encrypt(plaintext, associatedData)
			if err != nil {
				t.Fatalf("a.Encrypt(plaintext, associatedData) err = %q, want nil", err)
			}
			gotPlaintext, err := a.Decrypt(ciphertext, associatedData)
			if err != nil {
				t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %q, want nil", err)
			}
			if !bytes.Equal(gotPlaintext, plaintext) {
				t.Fatalf("got plaintext %q, want %q", gotPlaintext, plaintext)
			}
			if _, err = a.Decrypt(ciphertext, invalidAssociatedData); err == nil {
				t.Error("a.Decrypt(ciphertext, invalidAssociatedData) err = nil, want error")
			}

			ctx := context.Background()
			r, err := aead.NewKMSEnvelopeAEADWithContext(tc.dekTemplate, kekAEADWithContext)
			if err != nil {
				t.Error("a.DecryptWithContext(ctx, ciphertext, invalidAssociatedData) err = nil, want error")
			}
			ciphertext2, err := r.EncryptWithContext(ctx, plaintext, associatedData)
			if err != nil {
				t.Fatalf("a.EncryptWithContext(ctx, plaintext, associatedData) err = %q, want nil", err)
			}
			gotPlaintext2, err := r.DecryptWithContext(ctx, ciphertext2, associatedData)
			if err != nil {
				t.Fatalf("a.DecryptWithContext(ctx, ciphertext2, associatedData) err = %q, want nil", err)
			}
			if !bytes.Equal(gotPlaintext2, plaintext) {
				t.Fatalf("got plaintext %q, want %q", gotPlaintext, plaintext)
			}
			if _, err = r.DecryptWithContext(ctx, ciphertext2, invalidAssociatedData); err == nil {
				t.Error("a.DecryptWithContext(ctx, ciphertext2, invalidAssociatedData) err = nil, want error")
			}

			// check that DecryptWithContext is compatible with Decrypt
			gotPlaintext3, err := r.DecryptWithContext(ctx, ciphertext, associatedData)
			if err != nil {
				t.Fatalf("r.DecryptWithContext(ctx, ciphertext, associatedData) err = %q, want nil", err)
			}
			if !bytes.Equal(gotPlaintext3, plaintext) {
				t.Fatalf("got plaintext %q, want %q", gotPlaintext3, plaintext)
			}
			gotPlaintext4, err := a.Decrypt(ciphertext2, associatedData)
			if err != nil {
				t.Fatalf("a.Decrypt(ciphertext2, associatedData) err = %q, want nil", err)
			}
			if !bytes.Equal(gotPlaintext4, plaintext) {
				t.Fatalf("got plaintext %q, want %q", gotPlaintext4, plaintext)
			}
		})
	}
}

func TestKMSEnvelopeDecryptTestVector(t *testing.T) {
	keyURI := "fake-kms://CM2b3_MDElQKSAowdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUuY3J5cHRvLnRpbmsuQWVzR2NtS2V5EhIaEIK75t5L-adlUwVhWvRuWUwYARABGM2b3_MDIAE"
	plaintext := []byte("plaintext")
	associatedData := []byte("associatedData")
	// Generated by running a.Decrypt([]byte("plaintext"), []byte("associatedData"))
	ciphertextHex := "00000043013e77cdcd3ac4f38c7312f97a9b8c6e2b7b481ce3540006d24abb658938b52d6474c5fa569212c023e06229c0335f414244a32748778baed5e24a55b12b82920d1f06f17ef8a86eee808d2c9d2026fb45371cf60696eb77790524642c5f067a529793c251b068f8"
	ciphertext, err := hex.DecodeString(ciphertextHex)
	if err != nil {
		t.Fatalf("hex.DecodeString(ciphertextHex) err = %q, want nil", err)
	}

	// with NewKMSEnvelopeAEAD2.
	kekAEAD, err := fakekms.NewAEAD(keyURI)
	if err != nil {
		t.Fatalf("fakekms.NewAEAD(keyURI) err = %q, want nil", err)
	}
	a := aead.NewKMSEnvelopeAEAD2(aead.AES256GCMKeyTemplate(), kekAEAD)
	gotPlaintext, err := a.Decrypt(ciphertext, associatedData)
	if err != nil {
		t.Fatalf("a.Decrypt(ciphertext, associatedData) err = %q, want nil", err)
	}
	if !bytes.Equal(gotPlaintext, plaintext) {
		t.Fatalf("got plaintext %q, want %q", gotPlaintext, plaintext)
	}

	// with NewKMSEnvelopeAEADWithContext.
	ctx := context.Background()
	kekAEADWithContext, err := fakekms.NewAEADWithContext(keyURI)
	if err != nil {
		t.Fatalf("fakekms.NewAEADWithContext(keyURI) err = %q, want nil", err)
	}
	r, err := aead.NewKMSEnvelopeAEADWithContext(aead.AES256GCMKeyTemplate(), kekAEADWithContext)
	if err != nil {
		t.Fatalf("aead.NewKMSEnvelopeAEADWithContext() err = %q, want nil", err)
	}
	gotPlaintext2, err := r.DecryptWithContext(ctx, ciphertext, associatedData)
	if err != nil {
		t.Fatalf("r.DecryptWithContext(ciphertext, associatedData) err = %q, want nil", err)
	}
	if !bytes.Equal(gotPlaintext2, plaintext) {
		t.Fatalf("got plaintext %q, want %q", gotPlaintext, plaintext)
	}
}

func TestKMSEnvelopeWithKmsEnvelopeKeyTemplatesAsDekTemplate_fails(t *testing.T) {
	keyURI := "fake-kms://CM2b3_MDElQKSAowdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUuY3J5cHRvLnRpbmsuQWVzR2NtS2V5EhIaEIK75t5L-adlUwVhWvRuWUwYARABGM2b3_MDIAE"
	plaintext := []byte("plaintext")
	associatedData := []byte("associatedData")
	// Use a KmsEnvelopeAeadKeyTemplate as DEK template.
	envelopeDEKTemplate, err := aead.CreateKMSEnvelopeAEADKeyTemplate(keyURI, aead.AES128GCMKeyTemplate())
	if err != nil {
		t.Fatalf("aead.CreateKMSEnvelopAEADKeyTemplate() err = %q, want nil", err)
	}

	// NewKMSEnvelopeAEAD2 can't return an error. But it always fails when calling Encrypt.
	kekAEAD, err := fakekms.NewAEAD(keyURI)
	if err != nil {
		t.Fatalf("fakekms.NewAEAD(keyURI) err = %q, want nil", err)
	}
	a := aead.NewKMSEnvelopeAEAD2(envelopeDEKTemplate, kekAEAD)
	_, err = a.Encrypt(plaintext, associatedData)
	if err == nil {
		t.Error("a.Encrypt(plaintext, associatedData) err = nil, want error")
	}

	// NewKMSEnvelopeAEADWithContext returns an error.
	kekAEADWithContext, err := fakekms.NewAEADWithContext(keyURI)
	if err != nil {
		t.Fatalf("fakekms.NewAEADWithContext(keyURI) err = %q, want nil", err)
	}
	_, err = aead.NewKMSEnvelopeAEADWithContext(envelopeDEKTemplate, kekAEADWithContext)
	if err == nil {
		t.Error("NewKMSEnvelopeAEADWithContext() err = nil, want error")
	}
}

func TestKMSEnvelopeShortCiphertext(t *testing.T) {
	keyURI := "fake-kms://CM2b3_MDElQKSAowdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUuY3J5cHRvLnRpbmsuQWVzR2NtS2V5EhIaEIK75t5L-adlUwVhWvRuWUwYARABGM2b3_MDIAE"

	// with NewKMSEnvelopeAEAD2.
	kekAEAD, err := fakekms.NewAEAD(keyURI)
	if err != nil {
		t.Fatalf("fakekms.NewAEAD(keyURI) err = %q, want nil", err)
	}
	a := aead.NewKMSEnvelopeAEAD2(aead.AES256GCMKeyTemplate(), kekAEAD)
	if _, err = a.Decrypt([]byte{1}, nil); err == nil {
		t.Error("a.Decrypt([]byte{1}, nil) err = nil, want error")
	}

	// with NewKMSEnvelopeAEADWithContext.
	kekAEADWithContext, err := fakekms.NewAEADWithContext(keyURI)
	if err != nil {
		t.Fatalf("fakekms.NewAEADWithContext(keyURI) err = %q, want nil", err)
	}
	r, err := aead.NewKMSEnvelopeAEADWithContext(aead.AES256GCMKeyTemplate(), kekAEADWithContext)
	if err != nil {
		t.Fatalf("fakekms.NewKMSEnvelopeAEADWithContext() err = %q, want nil", err)
	}
	if _, err = r.DecryptWithContext(context.Background(), []byte{1}, nil); err == nil {
		t.Error("a.DecryptWithContext([]byte{1}, nil) err = nil, want error")
	}
}

func TestKMSEnvelopeDecryptHugeEncryptedDek(t *testing.T) {
	keyURI := "fake-kms://CM2b3_MDElQKSAowdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUuY3J5cHRvLnRpbmsuQWVzR2NtS2V5EhIaEIK75t5L-adlUwVhWvRuWUwYARABGM2b3_MDIAE"
	// A ciphertext with a huge encrypted DEK length
	ciphertext := []byte{0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88}

	// with NewKMSEnvelopeAEAD2.
	kekAEAD, err := fakekms.NewAEAD(keyURI)
	if err != nil {
		t.Fatalf("fakekms.NewAEAD(keyURI) err = %q, want nil", err)
	}
	a := aead.NewKMSEnvelopeAEAD2(aead.AES256GCMKeyTemplate(), kekAEAD)

	if _, err = a.Decrypt(ciphertext, nil); err == nil {
		t.Error("a.Decrypt([]byte{1}, nil) err = nil, want error")
	}

	// with NewKMSEnvelopeAEADWithContext.
	ctx := context.Background()
	kekAEADWithContext, err := fakekms.NewAEADWithContext(keyURI)
	if err != nil {
		t.Fatalf("fakekms.NewAEADWithContext(keyURI) err = %q, want nil", err)
	}
	r, err := aead.NewKMSEnvelopeAEADWithContext(aead.AES256GCMKeyTemplate(), kekAEADWithContext)
	if err != nil {
		t.Fatalf("fakekms.NewKMSEnvelopeAEADWithContext() err = %q, want nil", err)
	}
	if _, err = r.DecryptWithContext(ctx, ciphertext, nil); err == nil {
		t.Error("a.Decrypt([]byte{1}, nil) err = nil, want error")
	}
}

type invalidAEAD struct {
}

func (a *invalidAEAD) Encrypt(plaintext, associatedData []byte) ([]byte, error) {
	return []byte{}, nil
}

func (a *invalidAEAD) Decrypt(ciphertext, associatedData []byte) ([]byte, error) {
	return []byte{}, nil
}

func TestKMSEnvelopeEncryptWithInvalidAEADFails(t *testing.T) {
	invalidKEKAEAD := &invalidAEAD{}
	envAEADWithInvalidKEK := aead.NewKMSEnvelopeAEAD2(aead.AES256GCMKeyTemplate(), invalidKEKAEAD)

	if _, err := envAEADWithInvalidKEK.Encrypt([]byte("plaintext"), []byte("associatedData")); err == nil {
		t.Error("envAEADWithInvalidKEK.Encrypt(plaintext, associatedData) err = nil, want error")
	}
}