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/hybrid/internal/hpke_public_key_manager.h"
18
19 #include "gtest/gtest.h"
20 #include "absl/status/status.h"
21 #include "tink/hybrid/internal/hpke_test_util.h"
22 #include "tink/util/test_matchers.h"
23 #include "tink/util/test_util.h"
24 #include "proto/hpke.pb.h"
25
26 namespace crypto {
27 namespace tink {
28 namespace internal {
29 namespace {
30
31 using ::crypto::tink::internal::CreateHpkeParams;
32 using ::crypto::tink::internal::CreateHpkePublicKey;
33 using ::crypto::tink::test::IsOk;
34 using ::crypto::tink::test::StatusIs;
35 using ::google::crypto::tink::HpkeAead;
36 using ::google::crypto::tink::HpkeKdf;
37 using ::google::crypto::tink::HpkeKem;
38 using ::google::crypto::tink::HpkePublicKey;
39 using ::google::crypto::tink::KeyData;
40 using ::testing::Eq;
41
TEST(HpkePublicKeyManagerTest,BasicAccessors)42 TEST(HpkePublicKeyManagerTest, BasicAccessors) {
43 EXPECT_THAT(HpkePublicKeyManager().get_version(), Eq(0));
44 EXPECT_THAT(HpkePublicKeyManager().key_material_type(),
45 Eq(KeyData::ASYMMETRIC_PUBLIC));
46 EXPECT_THAT(HpkePublicKeyManager().get_key_type(),
47 Eq("type.googleapis.com/google.crypto.tink.HpkePublicKey"));
48 }
49
TEST(HpkePublicKeyManagerTest,ValidateEmptyKeyFails)50 TEST(HpkePublicKeyManagerTest, ValidateEmptyKeyFails) {
51 EXPECT_THAT(HpkePublicKeyManager().ValidateKey(HpkePublicKey()),
52 StatusIs(absl::StatusCode::kInvalidArgument));
53 }
54
TEST(HpkePublicKeyManagerTest,ValidateKeySucceeds)55 TEST(HpkePublicKeyManagerTest, ValidateKeySucceeds) {
56 EXPECT_THAT(HpkePublicKeyManager().ValidateKey(CreateHpkePublicKey(
57 CreateHpkeParams(HpkeKem::DHKEM_X25519_HKDF_SHA256,
58 HpkeKdf::HKDF_SHA256, HpkeAead::AES_128_GCM),
59 /*raw_key_bytes=*/"")),
60 IsOk());
61 }
62
TEST(HpkePublicKeyManagerTest,ValidateKeyWithInvalidKemFails)63 TEST(HpkePublicKeyManagerTest, ValidateKeyWithInvalidKemFails) {
64 EXPECT_THAT(HpkePublicKeyManager().ValidateKey(CreateHpkePublicKey(
65 CreateHpkeParams(HpkeKem::KEM_UNKNOWN, HpkeKdf::HKDF_SHA256,
66 HpkeAead::AES_128_GCM),
67 /*raw_key_bytes=*/"")),
68 StatusIs(absl::StatusCode::kInvalidArgument));
69 }
70
TEST(HpkePublicKeyManagerTest,ValidateKeyWithInvalidKdfFails)71 TEST(HpkePublicKeyManagerTest, ValidateKeyWithInvalidKdfFails) {
72 EXPECT_THAT(HpkePublicKeyManager().ValidateKey(CreateHpkePublicKey(
73 CreateHpkeParams(HpkeKem::DHKEM_X25519_HKDF_SHA256,
74 HpkeKdf::KDF_UNKNOWN, HpkeAead::AES_128_GCM),
75 /*raw_key_bytes=*/"")),
76 StatusIs(absl::StatusCode::kInvalidArgument));
77 }
78
TEST(HpkePublicKeyManagerTest,ValidateKeyWithInvalidAeadFails)79 TEST(HpkePublicKeyManagerTest, ValidateKeyWithInvalidAeadFails) {
80 EXPECT_THAT(
81 HpkePublicKeyManager().ValidateKey(CreateHpkePublicKey(
82 CreateHpkeParams(HpkeKem::DHKEM_X25519_HKDF_SHA256,
83 HpkeKdf::HKDF_SHA256, HpkeAead::AEAD_UNKNOWN),
84 /*raw_key_bytes=*/"")),
85 StatusIs(absl::StatusCode::kInvalidArgument));
86 }
87
88 } // namespace
89 } // namespace internal
90 } // namespace tink
91 } // namespace crypto
92