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_aead_hkdf_public_key_manager.h"
18
19 #include "gtest/gtest.h"
20 #include "absl/status/status.h"
21 #include "tink/aead/aead_key_templates.h"
22 #include "tink/aead/aes_gcm_key_manager.h"
23 #include "tink/experimental/pqcrypto/kem/cecpq2_aead_hkdf_private_key_manager.h"
24 #include "tink/hybrid_encrypt.h"
25 #include "tink/registry.h"
26 #include "tink/util/status.h"
27 #include "tink/util/statusor.h"
28 #include "tink/util/test_matchers.h"
29 #include "tink/util/test_util.h"
30 #include "proto/aes_eax.pb.h"
31 #include "proto/common.pb.h"
32 #include "proto/experimental/pqcrypto/cecpq2_aead_hkdf.pb.h"
33 #include "proto/tink.pb.h"
34
35 namespace crypto {
36 namespace tink {
37 namespace {
38
39 using ::crypto::tink::test::IsOk;
40 using ::crypto::tink::test::StatusIs;
41 using ::google::crypto::tink::Cecpq2AeadHkdfKeyFormat;
42 using ::google::crypto::tink::Cecpq2AeadHkdfParams;
43 using ::google::crypto::tink::Cecpq2AeadHkdfPublicKey;
44 using ::google::crypto::tink::EcPointFormat;
45 using ::google::crypto::tink::EllipticCurveType;
46 using ::google::crypto::tink::HashType;
47 using ::google::crypto::tink::KeyData;
48 using ::testing::Eq;
49 using ::testing::Not;
50
TEST(Cecpq2AeadHkdfPublicKeyManagerTest,Basics)51 TEST(Cecpq2AeadHkdfPublicKeyManagerTest, Basics) {
52 EXPECT_THAT(Cecpq2AeadHkdfPublicKeyManager().get_version(), Eq(0));
53 EXPECT_THAT(Cecpq2AeadHkdfPublicKeyManager().key_material_type(),
54 Eq(KeyData::ASYMMETRIC_PUBLIC));
55 EXPECT_THAT(
56 Cecpq2AeadHkdfPublicKeyManager().get_key_type(),
57 Eq("type.googleapis.com/google.crypto.tink.Cecpq2AeadHkdfPublicKey"));
58 }
59
TEST(Cecpq2AeadHkdfPublicKeyManagerTest,ValidateEmptyKey)60 TEST(Cecpq2AeadHkdfPublicKeyManagerTest, ValidateEmptyKey) {
61 EXPECT_THAT(
62 Cecpq2AeadHkdfPublicKeyManager().ValidateKey(Cecpq2AeadHkdfPublicKey()),
63 StatusIs(absl::StatusCode::kInvalidArgument));
64 }
65
CreatePublicKey()66 Cecpq2AeadHkdfPublicKey CreatePublicKey() {
67 Cecpq2AeadHkdfKeyFormat key_format;
68 key_format.mutable_params()->mutable_kem_params()->set_ec_point_format(
69 EcPointFormat::UNCOMPRESSED);
70 auto dem_params = key_format.mutable_params()->mutable_dem_params();
71 *(dem_params->mutable_aead_dem()) = AeadKeyTemplates::Aes128Gcm();
72 auto kem_params = key_format.mutable_params()->mutable_kem_params();
73 kem_params->set_curve_type(EllipticCurveType::CURVE25519);
74 kem_params->set_hkdf_hash_type(HashType::SHA256);
75 kem_params->set_hkdf_salt("");
76 auto private_key_manager = Cecpq2AeadHkdfPrivateKeyManager();
77 return private_key_manager
78 .GetPublicKey(private_key_manager.CreateKey(key_format).value())
79 .value();
80 }
81
TEST(Cecpq2AeadHkdfPublicKeyManagerTest,ValidateParams)82 TEST(Cecpq2AeadHkdfPublicKeyManagerTest, ValidateParams) {
83 EXPECT_THAT(Cecpq2AeadHkdfPublicKeyManager().ValidateParams(
84 CreatePublicKey().params()),
85 IsOk());
86 }
87
TEST(Cecpq2AeadHkdfPublicKeyManagerTest,ValidateKeyNoPoint)88 TEST(Cecpq2AeadHkdfPublicKeyManagerTest, ValidateKeyNoPoint) {
89 Cecpq2AeadHkdfParams params = CreatePublicKey().params();
90 params.mutable_kem_params()->set_ec_point_format(
91 EcPointFormat::UNKNOWN_FORMAT);
92 EXPECT_THAT(Cecpq2AeadHkdfPublicKeyManager().ValidateParams(params),
93 StatusIs(absl::StatusCode::kInvalidArgument));
94 }
95
TEST(Cecpq2AeadHkdfPublicKeyManagerTest,ValidateKeyNoDem)96 TEST(Cecpq2AeadHkdfPublicKeyManagerTest, ValidateKeyNoDem) {
97 Cecpq2AeadHkdfParams params = CreatePublicKey().params();
98 params.mutable_dem_params()->clear_aead_dem();
99 EXPECT_THAT(Cecpq2AeadHkdfPublicKeyManager().ValidateParams(params),
100 StatusIs(absl::StatusCode::kInvalidArgument));
101 }
102
TEST(Cecpq2AeadHkdfPublicKeyManagerTest,ValidateKeyNoKemCurve)103 TEST(Cecpq2AeadHkdfPublicKeyManagerTest, ValidateKeyNoKemCurve) {
104 Cecpq2AeadHkdfParams params = CreatePublicKey().params();
105 params.mutable_kem_params()->set_curve_type(EllipticCurveType::UNKNOWN_CURVE);
106 EXPECT_THAT(Cecpq2AeadHkdfPublicKeyManager().ValidateParams(params),
107 StatusIs(absl::StatusCode::kInvalidArgument));
108 }
109
TEST(Cecpq2AeadHkdfPublicKeyManagerTest,ValidateKeyNoKemHash)110 TEST(Cecpq2AeadHkdfPublicKeyManagerTest, ValidateKeyNoKemHash) {
111 Cecpq2AeadHkdfParams params = CreatePublicKey().params();
112 params.mutable_kem_params()->set_hkdf_hash_type(HashType::UNKNOWN_HASH);
113 EXPECT_THAT(Cecpq2AeadHkdfPublicKeyManager().ValidateParams(params),
114 StatusIs(absl::StatusCode::kInvalidArgument));
115 }
116
TEST(Cecpq2AeadHkdfPublicKeyManagerTest,ValidateGeneratedKey)117 TEST(Cecpq2AeadHkdfPublicKeyManagerTest, ValidateGeneratedKey) {
118 EXPECT_THAT(Cecpq2AeadHkdfPublicKeyManager().ValidateKey(CreatePublicKey()),
119 IsOk());
120 }
121
TEST(Cecpq2AeadHkdfPublicKeyManagerTest,ValidatePublicKeyVersion)122 TEST(Cecpq2AeadHkdfPublicKeyManagerTest, ValidatePublicKeyVersion) {
123 Cecpq2AeadHkdfPublicKey pk = CreatePublicKey();
124 pk.set_version(1);
125 EXPECT_THAT(Cecpq2AeadHkdfPublicKeyManager().ValidateKey(pk), Not(IsOk()));
126 }
127
128 } // namespace
129 } // namespace tink
130 } // namespace crypto
131