1 // Copyright 2017 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/hybrid/ecies_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/hybrid/ecies_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/ecies_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::EciesAeadHkdfKeyFormat;
42 using ::google::crypto::tink::EciesAeadHkdfParams;
43 using ::google::crypto::tink::EciesAeadHkdfPublicKey;
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
TEST(EciesAeadHkdfPublicKeyManagerTest,Basics)50 TEST(EciesAeadHkdfPublicKeyManagerTest, Basics) {
51 EXPECT_THAT(EciesAeadHkdfPublicKeyManager().get_version(), Eq(0));
52 EXPECT_THAT(EciesAeadHkdfPublicKeyManager().key_material_type(),
53 Eq(KeyData::ASYMMETRIC_PUBLIC));
54 EXPECT_THAT(
55 EciesAeadHkdfPublicKeyManager().get_key_type(),
56 Eq("type.googleapis.com/google.crypto.tink.EciesAeadHkdfPublicKey"));
57 }
58
TEST(EciesAeadHkdfPublicKeyManagerTest,ValidateEmptyKey)59 TEST(EciesAeadHkdfPublicKeyManagerTest, ValidateEmptyKey) {
60 EXPECT_THAT(
61 EciesAeadHkdfPublicKeyManager().ValidateKey(EciesAeadHkdfPublicKey()),
62 StatusIs(absl::StatusCode::kInvalidArgument));
63 }
64
CreatePublicKey()65 EciesAeadHkdfPublicKey CreatePublicKey() {
66 EciesAeadHkdfKeyFormat key_format;
67 key_format.mutable_params()->set_ec_point_format(EcPointFormat::UNCOMPRESSED);
68 auto dem_params = key_format.mutable_params()->mutable_dem_params();
69 *(dem_params->mutable_aead_dem()) = AeadKeyTemplates::Aes128Gcm();
70 auto kem_params = key_format.mutable_params()->mutable_kem_params();
71 kem_params->set_curve_type(EllipticCurveType::NIST_P256);
72 kem_params->set_hkdf_hash_type(HashType::SHA256);
73 kem_params->set_hkdf_salt("");
74 auto private_key_manager = EciesAeadHkdfPrivateKeyManager();
75 return private_key_manager
76 .GetPublicKey(private_key_manager.CreateKey(key_format).value())
77 .value();
78 }
79
TEST(EciesAeadHkdfPublicKeyManagerTest,ValidateParams)80 TEST(EciesAeadHkdfPublicKeyManagerTest, ValidateParams) {
81 EXPECT_THAT(EciesAeadHkdfPublicKeyManager().ValidateParams(
82 CreatePublicKey().params()),
83 IsOk());
84 }
85
TEST(EciesAeadHkdfPublicKeyManagerTest,ValidateKeyNoPoint)86 TEST(EciesAeadHkdfPublicKeyManagerTest, ValidateKeyNoPoint) {
87 EciesAeadHkdfParams params = CreatePublicKey().params();
88 params.set_ec_point_format(EcPointFormat::UNKNOWN_FORMAT);
89 EXPECT_THAT(EciesAeadHkdfPublicKeyManager().ValidateParams(params),
90 StatusIs(absl::StatusCode::kInvalidArgument));
91 }
92
TEST(EciesAeadHkdfPublicKeyManagerTest,ValidateKeyNoDem)93 TEST(EciesAeadHkdfPublicKeyManagerTest, ValidateKeyNoDem) {
94 EciesAeadHkdfParams params = CreatePublicKey().params();
95 params.mutable_dem_params()->clear_aead_dem();
96 EXPECT_THAT(EciesAeadHkdfPublicKeyManager().ValidateParams(params),
97 StatusIs(absl::StatusCode::kInvalidArgument));
98 }
99
TEST(EciesAeadHkdfPublicKeyManagerTest,ValidateKeyNoKemCurve)100 TEST(EciesAeadHkdfPublicKeyManagerTest, ValidateKeyNoKemCurve) {
101 EciesAeadHkdfParams params = CreatePublicKey().params();
102 params.mutable_kem_params()->set_curve_type(EllipticCurveType::UNKNOWN_CURVE);
103 EXPECT_THAT(EciesAeadHkdfPublicKeyManager().ValidateParams(params),
104 StatusIs(absl::StatusCode::kInvalidArgument));
105 }
106
TEST(EciesAeadHkdfPublicKeyManagerTest,ValidateKeyNoKemHash)107 TEST(EciesAeadHkdfPublicKeyManagerTest, ValidateKeyNoKemHash) {
108 EciesAeadHkdfParams params = CreatePublicKey().params();
109 params.mutable_kem_params()->set_hkdf_hash_type(HashType::UNKNOWN_HASH);
110 EXPECT_THAT(EciesAeadHkdfPublicKeyManager().ValidateParams(params),
111 StatusIs(absl::StatusCode::kInvalidArgument));
112 }
113
TEST(EciesAeadHkdfPublicKeyManagerTest,ValidateGeneratedKey)114 TEST(EciesAeadHkdfPublicKeyManagerTest, ValidateGeneratedKey) {
115 EXPECT_THAT(EciesAeadHkdfPublicKeyManager().ValidateKey(CreatePublicKey()),
116 IsOk());
117 }
118
119 } // namespace
120 } // namespace tink
121 } // namespace crypto
122