Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit 239cf37

Browse files
juergwcopybara-github
authored andcommitted
Add test for non-standard use-case of KMS Envelope Keys.
In tink-crypto/tink-go#10 it was mentioned that KMS Envelope Keys can and are used with a TINK prefix. Add this test to make sure that we don't accidentally break this. PiperOrigin-RevId: 614604104
1 parent 64ea420 commit 239cf37

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

go/aead/kms_envelope_aead_key_manager_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package aead_test
1818

1919
import (
20+
"bytes"
2021
"testing"
2122

2223
"google.golang.org/protobuf/proto"
2324
"github.com/google/tink/go/aead"
2425
"github.com/google/tink/go/core/registry"
26+
"github.com/google/tink/go/insecurecleartextkeyset"
2527
"github.com/google/tink/go/keyset"
2628
"github.com/google/tink/go/mac"
2729
"github.com/google/tink/go/testing/fakekms"
@@ -127,3 +129,54 @@ func TestNewKMSEnvelopeAEADKeyWithInvalidSerializedKeyFormat(t *testing.T) {
127129
t.Errorf("a.Encrypt() err = nil, want error")
128130
}
129131
}
132+
133+
func TestKMSEnvelopeAEADWithTinkPrefix(t *testing.T) {
134+
keyURI := "fake-kms://CM2b3_MDElQKSAowdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUuY3J5cHRvLnRpbmsuQWVzR2NtS2V5EhIaEIK75t5L-adlUwVhWvRuWUwYARABGM2b3_MDIAE"
135+
client, err := fakekms.NewClient(keyURI)
136+
if err != nil {
137+
t.Fatal(err)
138+
}
139+
registry.RegisterKMSClient(client)
140+
defer registry.ClearKMSClients()
141+
142+
// Keyset that was created with
143+
// aead.CreateKMSEnvelopeAEADKeyTemplate(keyURI, aead.AES256GCMKeyTemplate()), and then serialized
144+
// with Tink's JSON keyset witer. Then, the output prefix type was changed from "RAW" to "TINK".
145+
jsonKeysetWithTinkPrefix := `{"primaryKeyId":3980895889, "key":[{"keyData":{"typeUrl":"type.googleapis.com/google.crypto.tink.KmsEnvelopeAeadKey", "value":"EsMBCoYBZmFrZS1rbXM6Ly9DTTJiM19NREVsUUtTQW93ZEhsd1pTNW5iMjluYkdWaGNHbHpMbU52YlM5bmIyOW5iR1V1WTNKNWNIUnZMblJwYm1zdVFXVnpSMk50UzJWNUVoSWFFSUs3NXQ1TC1hZGxVd1ZoV3ZSdVdVd1lBUkFCR00yYjNfTURJQUUSOAowdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUuY3J5cHRvLnRpbmsuQWVzR2NtS2V5EgIQIBgB", "keyMaterialType":"REMOTE"}, "status":"ENABLED", "keyId":3980895889, "outputPrefixType":"TINK"}]}`
146+
147+
parsedHandle, err := insecurecleartextkeyset.Read(
148+
keyset.NewJSONReader(bytes.NewBuffer([]byte(jsonKeysetWithTinkPrefix))))
149+
if err != nil {
150+
t.Fatalf("insecurecleartextkeyset.Read() err = %v, want nil", err)
151+
}
152+
153+
primitive, err := aead.New(parsedHandle)
154+
if err != nil {
155+
t.Fatal(err)
156+
}
157+
158+
plaintext := []byte("message")
159+
associatedData := []byte("example KMS envelope AEAD encryption")
160+
161+
ciphertext, err := primitive.Encrypt(plaintext, associatedData)
162+
if err != nil {
163+
t.Fatal(err)
164+
}
165+
gotPlaintext, err := primitive.Decrypt(ciphertext, associatedData)
166+
if err != nil {
167+
t.Fatal(err)
168+
}
169+
if !bytes.Equal(gotPlaintext, plaintext) {
170+
t.Fatalf("got plaintext %q, want %q", gotPlaintext, plaintext)
171+
}
172+
173+
// Also verify that the ciphertext has a TINK prefix
174+
gotPrefix := ciphertext[:5]
175+
// The Tink prefix is 0x01 followed by the 4 bytes key ID. The key ID is 3980895889, which is
176+
// equal to 0xed47a691.
177+
wantPrefix := []byte{0x01, 0xed, 0x47, 0xa6, 0x91}
178+
if !bytes.Equal(gotPrefix, wantPrefix) {
179+
t.Fatalf("ciphertext[:5] = %q, want %q", gotPrefix, wantPrefix)
180+
}
181+
182+
}

0 commit comments

Comments
 (0)