1 // Copyright 2021 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
17 #include "tink/experimental/pqcrypto/kem/cecpq2_hybrid_key_templates.h"
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "tink/experimental/pqcrypto/kem/cecpq2_aead_hkdf_private_key_manager.h"
22 #include "tink/experimental/pqcrypto/kem/cecpq2_hybrid_config.h"
23 #include "tink/util/test_matchers.h"
24 #include "proto/experimental/pqcrypto/cecpq2_aead_hkdf.pb.h"
25
26 namespace crypto {
27 namespace tink {
28 namespace {
29
30 using ::crypto::tink::test::IsOk;
31 using google::crypto::tink::KeyTemplate;
32 using ::testing::Eq;
33
34 class Cecpq2HybridKeyTemplatesTest : public ::testing::Test {
35 protected:
SetUpTestSuite()36 static void SetUpTestSuite() {
37 // Initialize the registry, so that the templates can be tested
38 ASSERT_THAT(Cecpq2HybridConfigRegister(), IsOk());
39 }
40 };
41
TEST_F(Cecpq2HybridKeyTemplatesTest,ValidateX25519HkdfHmacSha256Aes256GcmKeyFormat)42 TEST_F(Cecpq2HybridKeyTemplatesTest,
43 ValidateX25519HkdfHmacSha256Aes256GcmKeyFormat) {
44 const KeyTemplate& key_template =
45 Cecpq2HybridKeyTemplateX25519HkdfHmacSha256Aes256Gcm();
46 google::crypto::tink::Cecpq2AeadHkdfKeyFormat key_format;
47 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
48 EXPECT_THAT(Cecpq2AeadHkdfPrivateKeyManager().ValidateKeyFormat(key_format),
49 IsOk());
50 }
51
TEST_F(Cecpq2HybridKeyTemplatesTest,ValidateX25519HkdfHmacSha256XChaCha20Poly1305KeyFormat)52 TEST_F(Cecpq2HybridKeyTemplatesTest,
53 ValidateX25519HkdfHmacSha256XChaCha20Poly1305KeyFormat) {
54 const KeyTemplate& key_template =
55 Cecpq2HybridKeyTemplateX25519HkdfHmacSha256XChaCha20Poly1305();
56 google::crypto::tink::Cecpq2AeadHkdfKeyFormat key_format;
57 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
58 EXPECT_THAT(Cecpq2AeadHkdfPrivateKeyManager().ValidateKeyFormat(key_format),
59 IsOk());
60 }
61
TEST_F(Cecpq2HybridKeyTemplatesTest,ValidateX25519HkdfHmacSha256DeterministicAesSivKeyFormat)62 TEST_F(Cecpq2HybridKeyTemplatesTest,
63 ValidateX25519HkdfHmacSha256DeterministicAesSivKeyFormat) {
64 const KeyTemplate& key_template =
65 Cecpq2HybridKeyTemplateX25519HkdfHmacSha256DeterministicAesSiv();
66 google::crypto::tink::Cecpq2AeadHkdfKeyFormat key_format;
67 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
68 EXPECT_THAT(Cecpq2AeadHkdfPrivateKeyManager().ValidateKeyFormat(key_format),
69 IsOk());
70 }
71
TEST_F(Cecpq2HybridKeyTemplatesTest,CheckX25519HkdfHmacSha256Aes256GcmTypeUrl)72 TEST_F(Cecpq2HybridKeyTemplatesTest,
73 CheckX25519HkdfHmacSha256Aes256GcmTypeUrl) {
74 const KeyTemplate& key_template =
75 Cecpq2HybridKeyTemplateX25519HkdfHmacSha256Aes256Gcm();
76 EXPECT_THAT(
77 key_template.type_url(),
78 Eq("type.googleapis.com/google.crypto.tink.Cecpq2AeadHkdfPrivateKey"));
79 google::crypto::tink::Cecpq2AeadHkdfKeyFormat key_format;
80 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
81 EXPECT_THAT(key_format.params().dem_params().aead_dem().type_url(),
82 Eq("type.googleapis.com/google.crypto.tink.AesGcmKey"));
83 }
84
TEST_F(Cecpq2HybridKeyTemplatesTest,CheckX25519HkdfHmacSha256XChaCha20Poly1305TypeUrl)85 TEST_F(Cecpq2HybridKeyTemplatesTest,
86 CheckX25519HkdfHmacSha256XChaCha20Poly1305TypeUrl) {
87 const KeyTemplate& key_template =
88 Cecpq2HybridKeyTemplateX25519HkdfHmacSha256XChaCha20Poly1305();
89 EXPECT_THAT(
90 key_template.type_url(),
91 Eq("type.googleapis.com/google.crypto.tink.Cecpq2AeadHkdfPrivateKey"));
92 google::crypto::tink::Cecpq2AeadHkdfKeyFormat key_format;
93 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
94 EXPECT_THAT(
95 key_format.params().dem_params().aead_dem().type_url(),
96 Eq("type.googleapis.com/google.crypto.tink.XChaCha20Poly1305Key"));
97 }
98
TEST_F(Cecpq2HybridKeyTemplatesTest,CheckX25519HkdfHmacSha256DeterministicAesSivTypeUrl)99 TEST_F(Cecpq2HybridKeyTemplatesTest,
100 CheckX25519HkdfHmacSha256DeterministicAesSivTypeUrl) {
101 const KeyTemplate& key_template =
102 Cecpq2HybridKeyTemplateX25519HkdfHmacSha256DeterministicAesSiv();
103 EXPECT_THAT(
104 key_template.type_url(),
105 Eq("type.googleapis.com/google.crypto.tink.Cecpq2AeadHkdfPrivateKey"));
106 google::crypto::tink::Cecpq2AeadHkdfKeyFormat key_format;
107 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
108 EXPECT_THAT(key_format.params().dem_params().aead_dem().type_url(),
109 Eq("type.googleapis.com/google.crypto.tink.AesSivKey"));
110 }
111
112 } // namespace
113 } // namespace tink
114 } // namespace crypto
115