xref: /aosp_15_r20/external/tink/go/aead/aes_ctr_hmac_aead_key_manager_test.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2018 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////////////////////////////////////////////////////////////////////////////////
16
17package aead_test
18
19import (
20	"testing"
21
22	"google.golang.org/protobuf/proto"
23	"github.com/google/tink/go/aead"
24	"github.com/google/tink/go/core/registry"
25	"github.com/google/tink/go/testutil"
26	ctrhmacpb "github.com/google/tink/go/proto/aes_ctr_hmac_aead_go_proto"
27	tinkpb "github.com/google/tink/go/proto/tink_go_proto"
28)
29
30func TestNewKeyMultipleTimes(t *testing.T) {
31	keyTemplate := aead.AES128CTRHMACSHA256KeyTemplate()
32	aeadKeyFormat := new(ctrhmacpb.AesCtrHmacAeadKeyFormat)
33	if err := proto.Unmarshal(keyTemplate.Value, aeadKeyFormat); err != nil {
34		t.Fatalf("cannot unmarshal AES128CTRHMACSHA256 key template")
35	}
36
37	keyManager, err := registry.GetKeyManager(testutil.AESCTRHMACAEADTypeURL)
38	if err != nil {
39		t.Errorf("cannot obtain AES-CTR-HMAC-AEAD key manager: %s", err)
40	}
41
42	keys := make(map[string]bool)
43	const numTests = 24
44	for i := 0; i < numTests/2; i++ {
45		k, _ := keyManager.NewKey(keyTemplate.Value)
46		sk, err := proto.Marshal(k)
47		if err != nil {
48			t.Fatalf("cannot serialize key, error: %v", err)
49		}
50
51		key := new(ctrhmacpb.AesCtrHmacAeadKey)
52		proto.Unmarshal(sk, key)
53
54		keys[string(key.AesCtrKey.KeyValue)] = true
55		keys[string(key.HmacKey.KeyValue)] = true
56		if len(key.AesCtrKey.KeyValue) != 16 {
57			t.Errorf("unexpected AES key size, got: %d, want: 16", len(key.AesCtrKey.KeyValue))
58		}
59		if len(key.HmacKey.KeyValue) != 32 {
60			t.Errorf("unexpected HMAC key size, got: %d, want: 32", len(key.HmacKey.KeyValue))
61		}
62	}
63	if len(keys) != numTests {
64		t.Errorf("unexpected number of keys in set, got: %d, want: %d", len(keys), numTests)
65	}
66}
67
68func TestNewKeyWithCorruptedFormat(t *testing.T) {
69	keyTemplate := new(tinkpb.KeyTemplate)
70
71	keyTemplate.TypeUrl = testutil.AESCTRHMACAEADTypeURL
72	keyTemplate.Value = make([]byte, 128)
73
74	keyManager, err := registry.GetKeyManager(testutil.AESCTRHMACAEADTypeURL)
75	if err != nil {
76		t.Errorf("cannot obtain AES-CTR-HMAC-AEAD key manager: %s", err)
77	}
78
79	_, err = keyManager.NewKey(keyTemplate.Value)
80	if err == nil {
81		t.Error("NewKey got: success, want: error due to corrupted format")
82	}
83
84	_, err = keyManager.NewKeyData(keyTemplate.Value)
85	if err == nil {
86		t.Error("NewKeyData got: success, want: error due to corrupted format")
87	}
88}
89