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/hybrid/hybrid_key_templates.h"
18
19 #include <string>
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "tink/aead/aead_key_templates.h"
24 #include "tink/daead/deterministic_aead_key_templates.h"
25 #include "tink/hybrid/ecies_aead_hkdf_private_key_manager.h"
26 #include "tink/hybrid/hybrid_config.h"
27 #include "tink/hybrid/internal/hpke_private_key_manager.h"
28 #include "tink/util/test_matchers.h"
29 #include "proto/common.pb.h"
30 #include "proto/ecies_aead_hkdf.pb.h"
31 #include "proto/hpke.pb.h"
32 #include "proto/tink.pb.h"
33
34 namespace crypto {
35 namespace tink {
36 namespace {
37
38 using ::crypto::tink::internal::HpkePrivateKeyManager;
39 using ::crypto::tink::test::IsOk;
40 using ::google::crypto::tink::EciesAeadHkdfKeyFormat;
41 using ::google::crypto::tink::EcPointFormat;
42 using ::google::crypto::tink::EllipticCurveType;
43 using ::google::crypto::tink::HashType;
44 using ::google::crypto::tink::HpkeAead;
45 using ::google::crypto::tink::HpkeKdf;
46 using ::google::crypto::tink::HpkeKem;
47 using ::google::crypto::tink::HpkeKeyFormat;
48 using ::google::crypto::tink::KeyTemplate;
49 using ::google::crypto::tink::OutputPrefixType;
50 using ::testing::Eq;
51
52 class HybridKeyTemplatesTest : public ::testing::Test {
53 protected:
SetUpTestSuite()54 static void SetUpTestSuite() {
55 // Initialize the registry, so that the templates can be tested.
56 ASSERT_THAT(HybridConfig::Register(), IsOk());
57 }
58 };
59
TEST_F(HybridKeyTemplatesTest,EciesP256HkdfHmacSha256Aes128Gcm)60 TEST_F(HybridKeyTemplatesTest, EciesP256HkdfHmacSha256Aes128Gcm) {
61 // Check that returned template is correct.
62 std::string type_url =
63 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
64 const KeyTemplate& key_template =
65 HybridKeyTemplates::EciesP256HkdfHmacSha256Aes128Gcm();
66 EXPECT_EQ(type_url, key_template.type_url());
67 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
68 EciesAeadHkdfKeyFormat key_format;
69 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
70 EXPECT_EQ(EcPointFormat::UNCOMPRESSED, key_format.params().ec_point_format());
71 auto dem_params = key_format.mutable_params()->mutable_dem_params();
72 auto expected_dem = AeadKeyTemplates::Aes128Gcm();
73 EXPECT_EQ(expected_dem.output_prefix_type(),
74 dem_params->aead_dem().output_prefix_type());
75 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
76 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
77 auto kem_params = key_format.mutable_params()->mutable_kem_params();
78 EXPECT_EQ(EllipticCurveType::NIST_P256, kem_params->curve_type());
79 EXPECT_EQ(HashType::SHA256, kem_params->hkdf_hash_type());
80 EXPECT_EQ("", kem_params->hkdf_salt());
81
82 // Check that reference to the same object is returned.
83 const KeyTemplate& key_template_2 =
84 HybridKeyTemplates::EciesP256HkdfHmacSha256Aes128Gcm();
85 EXPECT_EQ(&key_template, &key_template_2);
86
87 // Check that the template works with the key manager.
88 EciesAeadHkdfPrivateKeyManager key_manager;
89 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
90 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
91 }
92
TEST_F(HybridKeyTemplatesTest,EciesP256HkdfHmacSha512Aes128Gcm)93 TEST_F(HybridKeyTemplatesTest, EciesP256HkdfHmacSha512Aes128Gcm) {
94 // Check that returned template is correct.
95 std::string type_url =
96 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
97 const KeyTemplate& key_template =
98 HybridKeyTemplates::EciesP256HkdfHmacSha512Aes128Gcm();
99 EXPECT_EQ(type_url, key_template.type_url());
100 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
101 EciesAeadHkdfKeyFormat key_format;
102 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
103 EXPECT_EQ(EcPointFormat::UNCOMPRESSED, key_format.params().ec_point_format());
104 auto dem_params = key_format.mutable_params()->mutable_dem_params();
105 auto expected_dem = AeadKeyTemplates::Aes128Gcm();
106 EXPECT_EQ(expected_dem.output_prefix_type(),
107 dem_params->aead_dem().output_prefix_type());
108 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
109 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
110 auto kem_params = key_format.mutable_params()->mutable_kem_params();
111 EXPECT_EQ(EllipticCurveType::NIST_P256, kem_params->curve_type());
112 EXPECT_EQ(HashType::SHA512, kem_params->hkdf_hash_type());
113 EXPECT_EQ("", kem_params->hkdf_salt());
114
115 // Check that reference to the same object is returned.
116 const KeyTemplate& key_template_2 =
117 HybridKeyTemplates::EciesP256HkdfHmacSha512Aes128Gcm();
118 EXPECT_EQ(&key_template, &key_template_2);
119
120 // Check that the template works with the key manager.
121 EciesAeadHkdfPrivateKeyManager key_manager;
122 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
123 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
124 }
125
TEST_F(HybridKeyTemplatesTest,EciesP256HkdfHmacSha256Aes128GcmCompressedWithoutPrefix)126 TEST_F(HybridKeyTemplatesTest,
127 EciesP256HkdfHmacSha256Aes128GcmCompressedWithoutPrefix) {
128 // Check that returned template is correct.
129 std::string type_url =
130 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
131 const KeyTemplate& key_template = HybridKeyTemplates::
132 EciesP256HkdfHmacSha256Aes128GcmCompressedWithoutPrefix();
133 EXPECT_EQ(type_url, key_template.type_url());
134 EXPECT_EQ(OutputPrefixType::RAW, key_template.output_prefix_type());
135 EciesAeadHkdfKeyFormat key_format;
136 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
137 EXPECT_EQ(EcPointFormat::COMPRESSED, key_format.params().ec_point_format());
138 auto dem_params = key_format.mutable_params()->mutable_dem_params();
139 auto expected_dem = AeadKeyTemplates::Aes128Gcm();
140 EXPECT_EQ(expected_dem.output_prefix_type(),
141 dem_params->aead_dem().output_prefix_type());
142 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
143 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
144 auto kem_params = key_format.mutable_params()->mutable_kem_params();
145 EXPECT_EQ(EllipticCurveType::NIST_P256, kem_params->curve_type());
146 EXPECT_EQ(HashType::SHA256, kem_params->hkdf_hash_type());
147 EXPECT_EQ("", kem_params->hkdf_salt());
148
149 // Check that reference to the same object is returned.
150 const KeyTemplate& key_template_2 = HybridKeyTemplates::
151 EciesP256HkdfHmacSha256Aes128GcmCompressedWithoutPrefix();
152 EXPECT_EQ(&key_template, &key_template_2);
153
154 // Check that the template works with the key manager.
155 EciesAeadHkdfPrivateKeyManager key_manager;
156 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
157 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
158 }
159
TEST_F(HybridKeyTemplatesTest,EciesP256HkdfHmacSha256Aes128CtrHmacSha256)160 TEST_F(HybridKeyTemplatesTest, EciesP256HkdfHmacSha256Aes128CtrHmacSha256) {
161 // Check that returned template is correct.
162 std::string type_url =
163 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
164 const KeyTemplate& key_template =
165 HybridKeyTemplates::EciesP256HkdfHmacSha256Aes128CtrHmacSha256();
166 EXPECT_EQ(type_url, key_template.type_url());
167 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
168 EciesAeadHkdfKeyFormat key_format;
169 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
170 EXPECT_EQ(EcPointFormat::UNCOMPRESSED, key_format.params().ec_point_format());
171 auto dem_params = key_format.mutable_params()->mutable_dem_params();
172 auto expected_dem = AeadKeyTemplates::Aes128CtrHmacSha256();
173 EXPECT_EQ(expected_dem.output_prefix_type(),
174 dem_params->aead_dem().output_prefix_type());
175 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
176 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
177 auto kem_params = key_format.mutable_params()->mutable_kem_params();
178 EXPECT_EQ(EllipticCurveType::NIST_P256, kem_params->curve_type());
179 EXPECT_EQ(HashType::SHA256, kem_params->hkdf_hash_type());
180 EXPECT_EQ("", kem_params->hkdf_salt());
181
182 // Check that reference to the same object is returned.
183 const KeyTemplate& key_template_2 =
184 HybridKeyTemplates::EciesP256HkdfHmacSha256Aes128CtrHmacSha256();
185 EXPECT_EQ(&key_template, &key_template_2);
186
187 // Check that the template works with the key manager.
188 EciesAeadHkdfPrivateKeyManager key_manager;
189 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
190 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
191 }
192
TEST_F(HybridKeyTemplatesTest,EciesP256HkdfHmacSha512Aes128CtrHmacSha256)193 TEST_F(HybridKeyTemplatesTest, EciesP256HkdfHmacSha512Aes128CtrHmacSha256) {
194 // Check that returned template is correct.
195 std::string type_url =
196 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
197 const KeyTemplate& key_template =
198 HybridKeyTemplates::EciesP256HkdfHmacSha512Aes128CtrHmacSha256();
199 EXPECT_EQ(type_url, key_template.type_url());
200 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
201 EciesAeadHkdfKeyFormat key_format;
202 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
203 EXPECT_EQ(EcPointFormat::UNCOMPRESSED, key_format.params().ec_point_format());
204 auto dem_params = key_format.mutable_params()->mutable_dem_params();
205 auto expected_dem = AeadKeyTemplates::Aes128CtrHmacSha256();
206 EXPECT_EQ(expected_dem.output_prefix_type(),
207 dem_params->aead_dem().output_prefix_type());
208 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
209 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
210 auto kem_params = key_format.mutable_params()->mutable_kem_params();
211 EXPECT_EQ(EllipticCurveType::NIST_P256, kem_params->curve_type());
212 EXPECT_EQ(HashType::SHA512, kem_params->hkdf_hash_type());
213 EXPECT_EQ("", kem_params->hkdf_salt());
214
215 // Check that reference to the same object is returned.
216 const KeyTemplate& key_template_2 =
217 HybridKeyTemplates::EciesP256HkdfHmacSha512Aes128CtrHmacSha256();
218 EXPECT_EQ(&key_template, &key_template_2);
219
220 // Check that the template works with the key manager.
221 EciesAeadHkdfPrivateKeyManager key_manager;
222 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
223 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
224 }
225
TEST_F(HybridKeyTemplatesTest,EciesP256CompressedHkdfHmacSha256Aes128Gcm)226 TEST_F(HybridKeyTemplatesTest, EciesP256CompressedHkdfHmacSha256Aes128Gcm) {
227 // Check that returned template is correct.
228 std::string type_url =
229 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
230 const KeyTemplate& key_template =
231 HybridKeyTemplates::EciesP256CompressedHkdfHmacSha256Aes128Gcm();
232 EXPECT_EQ(type_url, key_template.type_url());
233 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
234 EciesAeadHkdfKeyFormat key_format;
235 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
236 EXPECT_EQ(EcPointFormat::COMPRESSED, key_format.params().ec_point_format());
237 auto dem_params = key_format.mutable_params()->mutable_dem_params();
238 auto expected_dem = AeadKeyTemplates::Aes128Gcm();
239 EXPECT_EQ(expected_dem.output_prefix_type(),
240 dem_params->aead_dem().output_prefix_type());
241 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
242 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
243 auto kem_params = key_format.mutable_params()->mutable_kem_params();
244 EXPECT_EQ(EllipticCurveType::NIST_P256, kem_params->curve_type());
245 EXPECT_EQ(HashType::SHA256, kem_params->hkdf_hash_type());
246 EXPECT_EQ("", kem_params->hkdf_salt());
247
248 // Check that reference to the same object is returned.
249 const KeyTemplate& key_template_2 =
250 HybridKeyTemplates::EciesP256CompressedHkdfHmacSha256Aes128Gcm();
251 EXPECT_EQ(&key_template, &key_template_2);
252
253 // Check that the template works with the key manager.
254 EciesAeadHkdfPrivateKeyManager key_manager;
255 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
256 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
257 }
258
TEST_F(HybridKeyTemplatesTest,EciesP256CompressedHkdfHmacSha256Aes128CtrHmacSha256)259 TEST_F(HybridKeyTemplatesTest,
260 EciesP256CompressedHkdfHmacSha256Aes128CtrHmacSha256) {
261 // Check that returned template is correct.
262 std::string type_url =
263 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
264 const KeyTemplate& key_template = HybridKeyTemplates::
265 EciesP256CompressedHkdfHmacSha256Aes128CtrHmacSha256();
266 EXPECT_EQ(type_url, key_template.type_url());
267 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
268 EciesAeadHkdfKeyFormat key_format;
269 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
270 EXPECT_EQ(EcPointFormat::COMPRESSED, key_format.params().ec_point_format());
271 auto dem_params = key_format.mutable_params()->mutable_dem_params();
272 auto expected_dem = AeadKeyTemplates::Aes128CtrHmacSha256();
273 EXPECT_EQ(expected_dem.output_prefix_type(),
274 dem_params->aead_dem().output_prefix_type());
275 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
276 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
277 auto kem_params = key_format.mutable_params()->mutable_kem_params();
278 EXPECT_EQ(EllipticCurveType::NIST_P256, kem_params->curve_type());
279 EXPECT_EQ(HashType::SHA256, kem_params->hkdf_hash_type());
280 EXPECT_EQ("", kem_params->hkdf_salt());
281
282 // Check that reference to the same object is returned.
283 const KeyTemplate& key_template_2 = HybridKeyTemplates::
284 EciesP256CompressedHkdfHmacSha256Aes128CtrHmacSha256();
285 EXPECT_EQ(&key_template, &key_template_2);
286
287 // Check that the template works with the key manager.
288 EciesAeadHkdfPrivateKeyManager key_manager;
289 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
290 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
291 }
292
TEST_F(HybridKeyTemplatesTest,EciesX25519HkdfHmacSha256Aes128Gcm)293 TEST_F(HybridKeyTemplatesTest, EciesX25519HkdfHmacSha256Aes128Gcm) {
294 // Check that returned template is correct.
295 std::string type_url =
296 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
297 const KeyTemplate& key_template =
298 HybridKeyTemplates::EciesX25519HkdfHmacSha256Aes128Gcm();
299 EXPECT_EQ(type_url, key_template.type_url());
300 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
301 EciesAeadHkdfKeyFormat key_format;
302 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
303 EXPECT_EQ(EcPointFormat::COMPRESSED, key_format.params().ec_point_format());
304 auto dem_params = key_format.mutable_params()->mutable_dem_params();
305 auto expected_dem = AeadKeyTemplates::Aes128Gcm();
306 EXPECT_EQ(expected_dem.output_prefix_type(),
307 dem_params->aead_dem().output_prefix_type());
308 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
309 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
310 auto kem_params = key_format.mutable_params()->mutable_kem_params();
311 EXPECT_EQ(EllipticCurveType::CURVE25519, kem_params->curve_type());
312 EXPECT_EQ(HashType::SHA256, kem_params->hkdf_hash_type());
313 EXPECT_EQ("", kem_params->hkdf_salt());
314
315 // Check that reference to the same object is returned.
316 const KeyTemplate& key_template_2 =
317 HybridKeyTemplates::EciesX25519HkdfHmacSha256Aes128Gcm();
318 EXPECT_EQ(&key_template, &key_template_2);
319
320 // Check that the template works with the key manager.
321 EciesAeadHkdfPrivateKeyManager key_manager;
322 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
323 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
324 }
325
TEST_F(HybridKeyTemplatesTest,EciesX25519HkdfHmacSha256Aes256Gcm)326 TEST_F(HybridKeyTemplatesTest, EciesX25519HkdfHmacSha256Aes256Gcm) {
327 // Check that returned template is correct.
328 std::string type_url =
329 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
330 const KeyTemplate& key_template =
331 HybridKeyTemplates::EciesX25519HkdfHmacSha256Aes256Gcm();
332 EXPECT_EQ(type_url, key_template.type_url());
333 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
334 EciesAeadHkdfKeyFormat key_format;
335 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
336 EXPECT_EQ(EcPointFormat::COMPRESSED, key_format.params().ec_point_format());
337 auto dem_params = key_format.mutable_params()->mutable_dem_params();
338 auto expected_dem = AeadKeyTemplates::Aes256Gcm();
339 EXPECT_EQ(expected_dem.output_prefix_type(),
340 dem_params->aead_dem().output_prefix_type());
341 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
342 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
343 auto kem_params = key_format.mutable_params()->mutable_kem_params();
344 EXPECT_EQ(EllipticCurveType::CURVE25519, kem_params->curve_type());
345 EXPECT_EQ(HashType::SHA256, kem_params->hkdf_hash_type());
346 EXPECT_EQ("", kem_params->hkdf_salt());
347
348 // Check that reference to the same object is returned.
349 const KeyTemplate& key_template_2 =
350 HybridKeyTemplates::EciesX25519HkdfHmacSha256Aes256Gcm();
351 EXPECT_EQ(&key_template, &key_template_2);
352
353 // Check that the template works with the key manager.
354 EciesAeadHkdfPrivateKeyManager key_manager;
355 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
356 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
357 }
358
TEST_F(HybridKeyTemplatesTest,EciesX25519HkdfHmacSha256Aes128CtrHmacSha256)359 TEST_F(HybridKeyTemplatesTest,
360 EciesX25519HkdfHmacSha256Aes128CtrHmacSha256) {
361 // Check that returned template is correct.
362 std::string type_url =
363 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
364 const KeyTemplate& key_template =
365 HybridKeyTemplates::EciesX25519HkdfHmacSha256Aes128CtrHmacSha256();
366 EXPECT_EQ(type_url, key_template.type_url());
367 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
368 EciesAeadHkdfKeyFormat key_format;
369 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
370 EXPECT_EQ(EcPointFormat::COMPRESSED, key_format.params().ec_point_format());
371 auto dem_params = key_format.mutable_params()->mutable_dem_params();
372 auto expected_dem = AeadKeyTemplates::Aes128CtrHmacSha256();
373 EXPECT_EQ(expected_dem.output_prefix_type(),
374 dem_params->aead_dem().output_prefix_type());
375 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
376 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
377 auto kem_params = key_format.mutable_params()->mutable_kem_params();
378 EXPECT_EQ(EllipticCurveType::CURVE25519, kem_params->curve_type());
379 EXPECT_EQ(HashType::SHA256, kem_params->hkdf_hash_type());
380 EXPECT_EQ("", kem_params->hkdf_salt());
381
382 // Check that reference to the same object is returned.
383 const KeyTemplate& key_template_2 =
384 HybridKeyTemplates::EciesX25519HkdfHmacSha256Aes128CtrHmacSha256();
385 EXPECT_EQ(&key_template, &key_template_2);
386
387 // Check that the template works with the key manager.
388 EciesAeadHkdfPrivateKeyManager key_manager;
389 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
390 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
391 }
392
TEST_F(HybridKeyTemplatesTest,EciesX25519HkdfHmacSha256XChaCha20Poly1305)393 TEST_F(HybridKeyTemplatesTest, EciesX25519HkdfHmacSha256XChaCha20Poly1305) {
394 // Check that returned template is correct.
395 std::string type_url =
396 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
397 const KeyTemplate& key_template =
398 HybridKeyTemplates::EciesX25519HkdfHmacSha256XChaCha20Poly1305();
399 EXPECT_EQ(type_url, key_template.type_url());
400 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
401 EciesAeadHkdfKeyFormat key_format;
402 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
403 EXPECT_EQ(EcPointFormat::COMPRESSED, key_format.params().ec_point_format());
404 auto dem_params = key_format.mutable_params()->mutable_dem_params();
405 auto expected_dem = AeadKeyTemplates::XChaCha20Poly1305();
406 EXPECT_EQ(expected_dem.output_prefix_type(),
407 dem_params->aead_dem().output_prefix_type());
408 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
409 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
410 auto kem_params = key_format.mutable_params()->mutable_kem_params();
411 EXPECT_EQ(EllipticCurveType::CURVE25519, kem_params->curve_type());
412 EXPECT_EQ(HashType::SHA256, kem_params->hkdf_hash_type());
413 EXPECT_EQ("", kem_params->hkdf_salt());
414
415 // Check that reference to the same object is returned.
416 const KeyTemplate& key_template_2 =
417 HybridKeyTemplates::EciesX25519HkdfHmacSha256XChaCha20Poly1305();
418 EXPECT_EQ(&key_template, &key_template_2);
419
420 // Check that the template works with the key manager.
421 EciesAeadHkdfPrivateKeyManager key_manager;
422 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
423 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
424 }
425
TEST_F(HybridKeyTemplatesTest,EciesX25519HkdfHmacSha256DeterministicAesSiv)426 TEST_F(HybridKeyTemplatesTest, EciesX25519HkdfHmacSha256DeterministicAesSiv) {
427 // Check that returned template is correct.
428 std::string type_url =
429 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
430 const KeyTemplate& key_template =
431 HybridKeyTemplates::EciesX25519HkdfHmacSha256DeterministicAesSiv();
432 EXPECT_EQ(type_url, key_template.type_url());
433 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
434 EciesAeadHkdfKeyFormat key_format;
435 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
436 EXPECT_EQ(EcPointFormat::COMPRESSED, key_format.params().ec_point_format());
437 auto dem_params = key_format.mutable_params()->mutable_dem_params();
438 auto expected_dem = DeterministicAeadKeyTemplates::Aes256Siv();
439 EXPECT_EQ(expected_dem.output_prefix_type(),
440 dem_params->aead_dem().output_prefix_type());
441 EXPECT_EQ(expected_dem.type_url(), dem_params->aead_dem().type_url());
442 EXPECT_EQ(expected_dem.value(), dem_params->aead_dem().value());
443 auto kem_params = key_format.mutable_params()->mutable_kem_params();
444 EXPECT_EQ(EllipticCurveType::CURVE25519, kem_params->curve_type());
445 EXPECT_EQ(HashType::SHA256, kem_params->hkdf_hash_type());
446 EXPECT_EQ("", kem_params->hkdf_salt());
447
448 // Check that reference to the same object is returned.
449 const KeyTemplate& key_template_2 =
450 HybridKeyTemplates::EciesX25519HkdfHmacSha256DeterministicAesSiv();
451 EXPECT_EQ(&key_template, &key_template_2);
452
453 // Check that the template works with the key manager.
454 EciesAeadHkdfPrivateKeyManager key_manager;
455 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
456 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
457 }
458
TEST_F(HybridKeyTemplatesTest,HpkeX25519HkdfSha256Aes128Gcm)459 TEST_F(HybridKeyTemplatesTest, HpkeX25519HkdfSha256Aes128Gcm) {
460 // Check that returned template is correct.
461 std::string type_url =
462 "type.googleapis.com/google.crypto.tink.HpkePrivateKey";
463 const KeyTemplate& key_template =
464 HybridKeyTemplates::HpkeX25519HkdfSha256Aes128Gcm();
465 EXPECT_EQ(type_url, key_template.type_url());
466 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
467 HpkeKeyFormat key_format;
468 ASSERT_TRUE(key_format.ParseFromString(key_template.value()));
469 ASSERT_TRUE(key_format.has_params());
470 EXPECT_THAT(key_format.params().kem(), Eq(HpkeKem::DHKEM_X25519_HKDF_SHA256));
471 EXPECT_THAT(key_format.params().kdf(), Eq(HpkeKdf::HKDF_SHA256));
472 EXPECT_THAT(key_format.params().aead(), Eq(HpkeAead::AES_128_GCM));
473
474 // Check that reference to the same object is returned.
475 const KeyTemplate& key_template_2 =
476 HybridKeyTemplates::HpkeX25519HkdfSha256Aes128Gcm();
477 EXPECT_EQ(&key_template, &key_template_2);
478
479 // Check that the template works with the key manager.
480 HpkePrivateKeyManager key_manager;
481 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
482 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
483 }
484
TEST_F(HybridKeyTemplatesTest,HpkeX25519HkdfSha256Aes128GcmRaw)485 TEST_F(HybridKeyTemplatesTest, HpkeX25519HkdfSha256Aes128GcmRaw) {
486 // Check that returned template is correct.
487 std::string type_url =
488 "type.googleapis.com/google.crypto.tink.HpkePrivateKey";
489 const KeyTemplate& key_template =
490 HybridKeyTemplates::HpkeX25519HkdfSha256Aes128GcmRaw();
491 EXPECT_EQ(type_url, key_template.type_url());
492 EXPECT_EQ(OutputPrefixType::RAW, key_template.output_prefix_type());
493 HpkeKeyFormat key_format;
494 ASSERT_TRUE(key_format.ParseFromString(key_template.value()));
495 ASSERT_TRUE(key_format.has_params());
496 EXPECT_THAT(key_format.params().kem(), Eq(HpkeKem::DHKEM_X25519_HKDF_SHA256));
497 EXPECT_THAT(key_format.params().kdf(), Eq(HpkeKdf::HKDF_SHA256));
498 EXPECT_THAT(key_format.params().aead(), Eq(HpkeAead::AES_128_GCM));
499
500 // Check that reference to the same object is returned.
501 const KeyTemplate& key_template_2 =
502 HybridKeyTemplates::HpkeX25519HkdfSha256Aes128GcmRaw();
503 EXPECT_EQ(&key_template, &key_template_2);
504
505 // Check that the template works with the key manager.
506 HpkePrivateKeyManager key_manager;
507 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
508 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
509 }
510
TEST_F(HybridKeyTemplatesTest,HpkeX25519HkdfSha256Aes256Gcm)511 TEST_F(HybridKeyTemplatesTest, HpkeX25519HkdfSha256Aes256Gcm) {
512 // Check that returned template is correct.
513 std::string type_url =
514 "type.googleapis.com/google.crypto.tink.HpkePrivateKey";
515 const KeyTemplate& key_template =
516 HybridKeyTemplates::HpkeX25519HkdfSha256Aes256Gcm();
517 EXPECT_EQ(type_url, key_template.type_url());
518 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
519 HpkeKeyFormat key_format;
520 ASSERT_TRUE(key_format.ParseFromString(key_template.value()));
521 ASSERT_TRUE(key_format.has_params());
522 EXPECT_THAT(key_format.params().kem(), Eq(HpkeKem::DHKEM_X25519_HKDF_SHA256));
523 EXPECT_THAT(key_format.params().kdf(), Eq(HpkeKdf::HKDF_SHA256));
524 EXPECT_THAT(key_format.params().aead(), Eq(HpkeAead::AES_256_GCM));
525
526 // Check that reference to the same object is returned.
527 const KeyTemplate& key_template_2 =
528 HybridKeyTemplates::HpkeX25519HkdfSha256Aes256Gcm();
529 EXPECT_EQ(&key_template, &key_template_2);
530
531 // Check that the template works with the key manager.
532 HpkePrivateKeyManager key_manager;
533 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
534 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
535 }
536
TEST_F(HybridKeyTemplatesTest,HpkeX25519HkdfSha256Aes256GcmRaw)537 TEST_F(HybridKeyTemplatesTest, HpkeX25519HkdfSha256Aes256GcmRaw) {
538 // Check that returned template is correct.
539 std::string type_url =
540 "type.googleapis.com/google.crypto.tink.HpkePrivateKey";
541 const KeyTemplate& key_template =
542 HybridKeyTemplates::HpkeX25519HkdfSha256Aes256GcmRaw();
543 EXPECT_EQ(type_url, key_template.type_url());
544 EXPECT_EQ(OutputPrefixType::RAW, key_template.output_prefix_type());
545 HpkeKeyFormat key_format;
546 ASSERT_TRUE(key_format.ParseFromString(key_template.value()));
547 ASSERT_TRUE(key_format.has_params());
548 EXPECT_THAT(key_format.params().kem(), Eq(HpkeKem::DHKEM_X25519_HKDF_SHA256));
549 EXPECT_THAT(key_format.params().kdf(), Eq(HpkeKdf::HKDF_SHA256));
550 EXPECT_THAT(key_format.params().aead(), Eq(HpkeAead::AES_256_GCM));
551
552 // Check that reference to the same object is returned.
553 const KeyTemplate& key_template_2 =
554 HybridKeyTemplates::HpkeX25519HkdfSha256Aes256GcmRaw();
555 EXPECT_EQ(&key_template, &key_template_2);
556
557 // Check that the template works with the key manager.
558 HpkePrivateKeyManager key_manager;
559 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
560 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
561 }
562
TEST_F(HybridKeyTemplatesTest,HpkeX25519HkdfSha256ChaCha20Poly1305)563 TEST_F(HybridKeyTemplatesTest, HpkeX25519HkdfSha256ChaCha20Poly1305) {
564 // Check that returned template is correct.
565 std::string type_url =
566 "type.googleapis.com/google.crypto.tink.HpkePrivateKey";
567 const KeyTemplate& key_template =
568 HybridKeyTemplates::HpkeX25519HkdfSha256ChaCha20Poly1305();
569 EXPECT_EQ(type_url, key_template.type_url());
570 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
571 HpkeKeyFormat key_format;
572 ASSERT_TRUE(key_format.ParseFromString(key_template.value()));
573 ASSERT_TRUE(key_format.has_params());
574 EXPECT_THAT(key_format.params().kem(), Eq(HpkeKem::DHKEM_X25519_HKDF_SHA256));
575 EXPECT_THAT(key_format.params().kdf(), Eq(HpkeKdf::HKDF_SHA256));
576 EXPECT_THAT(key_format.params().aead(), Eq(HpkeAead::CHACHA20_POLY1305));
577
578 // Check that reference to the same object is returned.
579 const KeyTemplate& key_template_2 =
580 HybridKeyTemplates::HpkeX25519HkdfSha256ChaCha20Poly1305();
581 EXPECT_EQ(&key_template, &key_template_2);
582
583 // Check that the template works with the key manager.
584 HpkePrivateKeyManager key_manager;
585 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
586 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
587 }
588
TEST_F(HybridKeyTemplatesTest,HpkeX25519HkdfSha256ChaCha20Poly1305Raw)589 TEST_F(HybridKeyTemplatesTest, HpkeX25519HkdfSha256ChaCha20Poly1305Raw) {
590 // Check that returned template is correct.
591 std::string type_url =
592 "type.googleapis.com/google.crypto.tink.HpkePrivateKey";
593 const KeyTemplate& key_template =
594 HybridKeyTemplates::HpkeX25519HkdfSha256ChaCha20Poly1305Raw();
595 EXPECT_EQ(type_url, key_template.type_url());
596 EXPECT_EQ(OutputPrefixType::RAW, key_template.output_prefix_type());
597 HpkeKeyFormat key_format;
598 ASSERT_TRUE(key_format.ParseFromString(key_template.value()));
599 ASSERT_TRUE(key_format.has_params());
600 EXPECT_THAT(key_format.params().kem(), Eq(HpkeKem::DHKEM_X25519_HKDF_SHA256));
601 EXPECT_THAT(key_format.params().kdf(), Eq(HpkeKdf::HKDF_SHA256));
602 EXPECT_THAT(key_format.params().aead(), Eq(HpkeAead::CHACHA20_POLY1305));
603
604 // Check that reference to the same object is returned.
605 const KeyTemplate& key_template_2 =
606 HybridKeyTemplates::HpkeX25519HkdfSha256ChaCha20Poly1305Raw();
607 EXPECT_EQ(&key_template, &key_template_2);
608
609 // Check that the template works with the key manager.
610 HpkePrivateKeyManager key_manager;
611 EXPECT_EQ(key_manager.get_key_type(), key_template.type_url());
612 EXPECT_THAT(key_manager.ValidateKeyFormat(key_format), IsOk());
613 }
614
615 } // namespace
616 } // namespace tink
617 } // namespace crypto
618