1 // Copyright 2018 Google Inc.
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
17 #include "tink/mac/mac_key_templates.h"
18
19 #include <string>
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "tink/core/key_manager_impl.h"
24 #include "tink/mac/aes_cmac_key_manager.h"
25 #include "tink/mac/hmac_key_manager.h"
26 #include "tink/util/test_matchers.h"
27 #include "proto/aes_cmac.pb.h"
28 #include "proto/common.pb.h"
29 #include "proto/hmac.pb.h"
30 #include "proto/tink.pb.h"
31
32 namespace crypto {
33 namespace tink {
34 namespace {
35
36 using ::crypto::tink::test::IsOk;
37 using ::google::crypto::tink::AesCmacKeyFormat;
38 using ::google::crypto::tink::HashType;
39 using ::google::crypto::tink::HmacKeyFormat;
40 using ::google::crypto::tink::KeyTemplate;
41 using ::google::crypto::tink::OutputPrefixType;
42 using ::testing::Eq;
43 using ::testing::Ref;
44
TEST(MacKeyTemplatesTest,testHmacKeyTemplates)45 TEST(MacKeyTemplatesTest, testHmacKeyTemplates) {
46 std::string type_url = "type.googleapis.com/google.crypto.tink.HmacKey";
47
48 { // Test Hmac128BittagSha256().
49 // Check that returned template is correct.
50 const KeyTemplate& key_template = MacKeyTemplates::HmacSha256HalfSizeTag();
51 EXPECT_EQ(type_url, key_template.type_url());
52 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
53 HmacKeyFormat key_format;
54 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
55 EXPECT_EQ(32, key_format.key_size());
56 EXPECT_EQ(16, key_format.params().tag_size());
57 EXPECT_EQ(HashType::SHA256, key_format.params().hash());
58
59 // Check that reference to the same object is returned.
60 const KeyTemplate& key_template_2 =
61 MacKeyTemplates::HmacSha256HalfSizeTag();
62 EXPECT_EQ(&key_template, &key_template_2);
63
64 // Check that the template works with the key manager.
65 HmacKeyManager key_type_manager;
66 auto key_manager = internal::MakeKeyManager<Mac>(&key_type_manager);
67 EXPECT_EQ(key_manager->get_key_type(), key_template.type_url());
68 auto new_key_result = key_manager->get_key_factory().NewKey(key_format);
69 EXPECT_TRUE(new_key_result.ok()) << new_key_result.status();
70 }
71
72 { // Test Hmac256BittagSha256().
73 // Check that returned template is correct.
74 const KeyTemplate& key_template = MacKeyTemplates::HmacSha256();
75 EXPECT_EQ(type_url, key_template.type_url());
76 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
77 HmacKeyFormat key_format;
78 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
79 EXPECT_EQ(32, key_format.key_size());
80 EXPECT_EQ(32, key_format.params().tag_size());
81 EXPECT_EQ(HashType::SHA256, key_format.params().hash());
82
83 // Check that reference to the same object is returned.
84 const KeyTemplate& key_template_2 = MacKeyTemplates::HmacSha256();
85 EXPECT_EQ(&key_template, &key_template_2);
86
87 // Check that the template works with the key manager.
88 HmacKeyManager key_type_manager;
89 auto key_manager = internal::MakeKeyManager<Mac>(&key_type_manager);
90 EXPECT_EQ(key_manager->get_key_type(), key_template.type_url());
91 auto new_key_result = key_manager->get_key_factory().NewKey(key_format);
92 EXPECT_TRUE(new_key_result.ok()) << new_key_result.status();
93 }
94
95 { // Test Hmac256BittagSha512().
96 // Check that returned template is correct.
97 const KeyTemplate& key_template = MacKeyTemplates::HmacSha512HalfSizeTag();
98 EXPECT_EQ(type_url, key_template.type_url());
99 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
100 HmacKeyFormat key_format;
101 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
102 EXPECT_EQ(64, key_format.key_size());
103 EXPECT_EQ(32, key_format.params().tag_size());
104 EXPECT_EQ(HashType::SHA512, key_format.params().hash());
105
106 // Check that reference to the same object is returned.
107 const KeyTemplate& key_template_2 =
108 MacKeyTemplates::HmacSha512HalfSizeTag();
109 EXPECT_EQ(&key_template, &key_template_2);
110
111 // Check that the template works with the key manager.
112 HmacKeyManager key_type_manager;
113 auto key_manager = internal::MakeKeyManager<Mac>(&key_type_manager);
114 EXPECT_EQ(key_manager->get_key_type(), key_template.type_url());
115 auto new_key_result = key_manager->get_key_factory().NewKey(key_format);
116 EXPECT_TRUE(new_key_result.ok()) << new_key_result.status();
117 }
118
119 { // Test Hmac512BittagSha512().
120 // Check that returned template is correct.
121 const KeyTemplate& key_template = MacKeyTemplates::HmacSha512();
122 EXPECT_EQ(type_url, key_template.type_url());
123 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
124 HmacKeyFormat key_format;
125 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
126 EXPECT_EQ(64, key_format.key_size());
127 EXPECT_EQ(64, key_format.params().tag_size());
128 EXPECT_EQ(HashType::SHA512, key_format.params().hash());
129
130 // Check that reference to the same object is returned.
131 const KeyTemplate& key_template_2 = MacKeyTemplates::HmacSha512();
132 EXPECT_EQ(&key_template, &key_template_2);
133
134 // Check that the template works with the key manager.
135 HmacKeyManager key_type_manager;
136 auto key_manager = internal::MakeKeyManager<Mac>(&key_type_manager);
137 EXPECT_EQ(key_manager->get_key_type(), key_template.type_url());
138 auto new_key_result = key_manager->get_key_factory().NewKey(key_format);
139 EXPECT_TRUE(new_key_result.ok()) << new_key_result.status();
140 }
141 }
142
TEST(AesCmac,Basics)143 TEST(AesCmac, Basics) {
144 EXPECT_THAT(MacKeyTemplates::AesCmac().type_url(),
145 Eq("type.googleapis.com/google.crypto.tink.AesCmacKey"));
146 EXPECT_THAT(MacKeyTemplates::AesCmac().type_url(),
147 Eq(AesCmacKeyManager().get_key_type()));
148 }
149
TEST(AesCmac,OutputPrefixType)150 TEST(AesCmac, OutputPrefixType) {
151 EXPECT_THAT(MacKeyTemplates::AesCmac().output_prefix_type(),
152 Eq(OutputPrefixType::TINK));
153 }
154
TEST(AesCmac,MultipleCallsSameReference)155 TEST(AesCmac, MultipleCallsSameReference) {
156 EXPECT_THAT(MacKeyTemplates::AesCmac(), Ref(MacKeyTemplates::AesCmac()));
157 }
158
TEST(AesCmac,WorksWithKeyTypeManager)159 TEST(AesCmac, WorksWithKeyTypeManager) {
160 AesCmacKeyFormat key_format;
161 EXPECT_TRUE(key_format.ParseFromString(MacKeyTemplates::AesCmac().value()));
162 EXPECT_THAT(AesCmacKeyManager().ValidateKeyFormat(key_format), IsOk());
163 }
164
TEST(AesCmac,CheckValues)165 TEST(AesCmac, CheckValues) {
166 AesCmacKeyFormat key_format;
167 EXPECT_TRUE(key_format.ParseFromString(MacKeyTemplates::AesCmac().value()));
168 EXPECT_THAT(key_format.key_size(), Eq(32));
169 EXPECT_THAT(key_format.params().tag_size(), Eq(16));
170 }
171
172 } // namespace
173 } // namespace tink
174 } // namespace crypto
175