xref: /aosp_15_r20/external/tink/cc/hybrid/internal/hpke_encrypt_boringssl_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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_encrypt_boringssl.h"
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "gtest/gtest.h"
24 #include "absl/status/status.h"
25 #include "absl/strings/escaping.h"
26 #include "openssl/hpke.h"
27 #include "tink/hybrid/internal/hpke_test_util.h"
28 #include "tink/util/status.h"
29 #include "tink/util/test_matchers.h"
30 #include "tink/util/test_util.h"
31 #include "proto/hpke.pb.h"
32 
33 namespace crypto {
34 namespace tink {
35 namespace internal {
36 namespace {
37 
38 using ::crypto::tink::test::IsOk;
39 using ::crypto::tink::test::IsOkAndHolds;
40 using ::crypto::tink::test::StatusIs;
41 using ::google::crypto::tink::HpkeAead;
42 using ::google::crypto::tink::HpkeKdf;
43 using ::google::crypto::tink::HpkeKem;
44 using ::google::crypto::tink::HpkeParams;
45 using ::testing::Values;
46 
47 class HpkeEncapsulateKeyThenEncryptBoringSslTest
48     : public testing::TestWithParam<HpkeParams> {};
49 
50 INSTANTIATE_TEST_SUITE_P(
51     HpkeEncapsulateKeyThenEncryptBoringSslTestSuite,
52     HpkeEncapsulateKeyThenEncryptBoringSslTest,
53     Values(CreateHpkeParams(HpkeKem::DHKEM_X25519_HKDF_SHA256,
54                             HpkeKdf::HKDF_SHA256, HpkeAead::AES_128_GCM),
55            CreateHpkeParams(HpkeKem::DHKEM_X25519_HKDF_SHA256,
56                             HpkeKdf::HKDF_SHA256,
57                             HpkeAead::CHACHA20_POLY1305)));
58 
TEST_P(HpkeEncapsulateKeyThenEncryptBoringSslTest,EncapsulateKeyThenEncrypt)59 TEST_P(HpkeEncapsulateKeyThenEncryptBoringSslTest, EncapsulateKeyThenEncrypt) {
60   HpkeParams hpke_params = GetParam();
61   util::StatusOr<HpkeTestParams> params = CreateHpkeTestParams(hpke_params);
62   ASSERT_THAT(params, IsOk());
63   util::StatusOr<std::unique_ptr<HpkeEncryptBoringSsl>> hpke_encrypt =
64       HpkeEncryptBoringSsl::NewForTesting(
65           hpke_params, params->recipient_public_key, params->application_info,
66           params->seed_for_testing);
67   ASSERT_THAT(hpke_encrypt, IsOk());
68   util::StatusOr<std::string> ciphertext =
69       (*hpke_encrypt)
70           ->EncapsulateKeyThenEncrypt(params->plaintext,
71                                       params->associated_data);
72   ASSERT_THAT(ciphertext, IsOkAndHolds(absl::StrCat(params->encapsulated_key,
73                                                     params->ciphertext)));
74 }
75 
76 class HpkeEncryptBoringSslWithBadParamTest
77     : public testing::TestWithParam<HpkeParams> {};
78 
79 INSTANTIATE_TEST_SUITE_P(
80     HpkeEncryptionBoringSslWithBadParamTestSuite,
81     HpkeEncryptBoringSslWithBadParamTest,
82     Values(CreateHpkeParams(HpkeKem::KEM_UNKNOWN, HpkeKdf::HKDF_SHA256,
83                             HpkeAead::AES_128_GCM),
84            CreateHpkeParams(HpkeKem::DHKEM_X25519_HKDF_SHA256,
85                             HpkeKdf::KDF_UNKNOWN, HpkeAead::AES_128_GCM),
86            CreateHpkeParams(HpkeKem::DHKEM_X25519_HKDF_SHA256,
87                             HpkeKdf::HKDF_SHA256, HpkeAead::AEAD_UNKNOWN)));
88 
TEST_P(HpkeEncryptBoringSslWithBadParamTest,BadParamFails)89 TEST_P(HpkeEncryptBoringSslWithBadParamTest, BadParamFails) {
90   HpkeParams hpke_params = GetParam();
91   HpkeTestParams params = DefaultHpkeTestParams();
92   util::StatusOr<std::unique_ptr<HpkeEncryptBoringSsl>> result =
93       HpkeEncryptBoringSsl::NewForTesting(
94           hpke_params, params.recipient_public_key, params.application_info,
95           params.seed_for_testing);
96   ASSERT_THAT(result.status(), StatusIs(absl::StatusCode::kInvalidArgument));
97 }
98 
TEST(HpkeEncryptBoringSslWithMissingPublicKeyTest,ZeroLengthPublicKeyFails)99 TEST(HpkeEncryptBoringSslWithMissingPublicKeyTest, ZeroLengthPublicKeyFails) {
100   HpkeParams hpke_params =
101       CreateHpkeParams(HpkeKem::DHKEM_X25519_HKDF_SHA256, HpkeKdf::HKDF_SHA256,
102                        HpkeAead::AES_128_GCM);
103   HpkeTestParams params = DefaultHpkeTestParams();
104   util::StatusOr<std::unique_ptr<HpkeEncryptBoringSsl>> result =
105       HpkeEncryptBoringSsl::NewForTesting(
106           hpke_params, /*recipient_public_key=*/"", params.application_info,
107           params.seed_for_testing);
108   ASSERT_THAT(result.status(), StatusIs(absl::StatusCode::kUnknown));
109 }
110 
111 }  // namespace
112 }  // namespace internal
113 }  // namespace tink
114 }  // namespace crypto
115