1 // Copyright 2022 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_util.h"
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "tink/hybrid/internal/hpke_test_util.h"
22 #include "tink/util/test_matchers.h"
23 #include "proto/hpke.pb.h"
24
25 namespace crypto {
26 namespace tink {
27 namespace internal {
28 namespace {
29
30 using ::crypto::tink::test::IsOk;
31 using ::crypto::tink::test::IsOkAndHolds;
32 using ::crypto::tink::test::StatusIs;
33 using ::testing::Eq;
34 using ::testing::Values;
35
36 struct HpkeParamsConversionTestCase {
37 google::crypto::tink::HpkeParams input;
38 HpkeParams expected;
39 };
40
41 using HpkeParamsConversionTest =
42 testing::TestWithParam<HpkeParamsConversionTestCase>;
43
44 INSTANTIATE_TEST_SUITE_P(
45 HpkeParamsConversionTestSuite, HpkeParamsConversionTest,
46 Values(
47 HpkeParamsConversionTestCase{
48 CreateHpkeParams(google::crypto::tink::DHKEM_X25519_HKDF_SHA256,
49 google::crypto::tink::HKDF_SHA256,
50 google::crypto::tink::AES_128_GCM),
51 HpkeParams{HpkeKem::kX25519HkdfSha256, HpkeKdf::kHkdfSha256,
52 HpkeAead::kAes128Gcm}},
53 HpkeParamsConversionTestCase{
54 CreateHpkeParams(google::crypto::tink::DHKEM_X25519_HKDF_SHA256,
55 google::crypto::tink::HKDF_SHA256,
56 google::crypto::tink::AES_256_GCM),
57 HpkeParams{HpkeKem::kX25519HkdfSha256, HpkeKdf::kHkdfSha256,
58 HpkeAead::kAes256Gcm}},
59 HpkeParamsConversionTestCase{
60 CreateHpkeParams(google::crypto::tink::DHKEM_X25519_HKDF_SHA256,
61 google::crypto::tink::HKDF_SHA256,
62 google::crypto::tink::CHACHA20_POLY1305),
63 HpkeParams{HpkeKem::kX25519HkdfSha256, HpkeKdf::kHkdfSha256,
64 HpkeAead::kChaCha20Poly1305}}));
65
TEST_P(HpkeParamsConversionTest,HpkeParamsProtoToStruct)66 TEST_P(HpkeParamsConversionTest, HpkeParamsProtoToStruct) {
67 HpkeParamsConversionTestCase test_case = GetParam();
68 util::StatusOr<HpkeParams> params = HpkeParamsProtoToStruct(test_case.input);
69 ASSERT_THAT(params, IsOk());
70
71 EXPECT_THAT(params->kem, Eq(test_case.expected.kem));
72 EXPECT_THAT(params->kdf, Eq(test_case.expected.kdf));
73 EXPECT_THAT(params->aead, Eq(test_case.expected.aead));
74 }
75
76 using HpkeBadParamsTest =
77 testing::TestWithParam<google::crypto::tink::HpkeParams>;
78
79 INSTANTIATE_TEST_SUITE_P(
80 HpkeBadParamsTestSuite, HpkeBadParamsTest,
81 Values(CreateHpkeParams(google::crypto::tink::KEM_UNKNOWN,
82 google::crypto::tink::HKDF_SHA256,
83 google::crypto::tink::AES_128_GCM),
84 CreateHpkeParams(google::crypto::tink::DHKEM_X25519_HKDF_SHA256,
85 google::crypto::tink::KDF_UNKNOWN,
86 google::crypto::tink::AES_256_GCM),
87 CreateHpkeParams(google::crypto::tink::DHKEM_X25519_HKDF_SHA256,
88 google::crypto::tink::HKDF_SHA256,
89 google::crypto::tink::AEAD_UNKNOWN)));
90
TEST_P(HpkeBadParamsTest,HpkeParamsProtoToStruct)91 TEST_P(HpkeBadParamsTest, HpkeParamsProtoToStruct) {
92 google::crypto::tink::HpkeParams params = GetParam();
93 EXPECT_THAT(HpkeParamsProtoToStruct(params).status(),
94 StatusIs(absl::StatusCode::kInvalidArgument));
95 }
96
TEST(HpkeKemEncodingSizeTest,HpkeEncapsulatedKeyLength)97 TEST(HpkeKemEncodingSizeTest, HpkeEncapsulatedKeyLength) {
98 // Encapsulated key length should match 'Nenc' column from
99 // https://www.rfc-editor.org/rfc/rfc9180.html#section-7.1.
100 EXPECT_THAT(
101 HpkeEncapsulatedKeyLength(google::crypto::tink::DHKEM_X25519_HKDF_SHA256),
102 IsOkAndHolds(32));
103 }
104
105 } // namespace
106 } // namespace internal
107 } // namespace tink
108 } // namespace crypto
109