xref: /aosp_15_r20/external/tink/cc/hybrid/internal/hpke_key_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_key_boringssl.h"
18 
19 #include <memory>
20 #include <string>
21 
22 #include "gtest/gtest.h"
23 #include "absl/status/status.h"
24 #include "absl/strings/escaping.h"
25 #include "openssl/hpke.h"
26 #include "tink/hybrid/internal/hpke_test_util.h"
27 #include "tink/util/status.h"
28 #include "tink/util/test_matchers.h"
29 #include "tink/util/test_util.h"
30 #include "proto/hpke.pb.h"
31 
32 namespace crypto {
33 namespace tink {
34 namespace internal {
35 namespace {
36 
37 using ::crypto::tink::test::IsOk;
38 using ::crypto::tink::test::StatusIs;
39 using ::google::crypto::tink::HpkeAead;
40 using ::google::crypto::tink::HpkeKdf;
41 using ::google::crypto::tink::HpkeKem;
42 using ::google::crypto::tink::HpkeParams;
43 
TEST(HpkeKeyBoringSslTest,CreateValidHpkeKey)44 TEST(HpkeKeyBoringSslTest, CreateValidHpkeKey) {
45   HpkeParams hpke_params =
46       CreateHpkeParams(HpkeKem::DHKEM_X25519_HKDF_SHA256, HpkeKdf::HKDF_SHA256,
47                        HpkeAead::AES_128_GCM);
48   HpkeTestParams params = DefaultHpkeTestParams();
49   util::StatusOr<std::unique_ptr<HpkeKeyBoringSsl>> hpke_key =
50       HpkeKeyBoringSsl::New(hpke_params.kem(), params.recipient_private_key);
51   ASSERT_THAT(hpke_key, IsOk());
52 }
53 
TEST(HpkeKeyBoringSslTest,BadKemFails)54 TEST(HpkeKeyBoringSslTest, BadKemFails) {
55   HpkeTestParams params = DefaultHpkeTestParams();
56   util::StatusOr<std::unique_ptr<HpkeKeyBoringSsl>> result =
57       HpkeKeyBoringSsl::New(HpkeKem::KEM_UNKNOWN,
58                             params.recipient_private_key);
59   ASSERT_THAT(result.status(), StatusIs(absl::StatusCode::kInvalidArgument));
60 }
61 
TEST(HpkeKeyBoringSslTest,ZeroLengthPrivateKeyFails)62 TEST(HpkeKeyBoringSslTest, ZeroLengthPrivateKeyFails) {
63   util::StatusOr<std::unique_ptr<HpkeKeyBoringSsl>> result =
64       HpkeKeyBoringSsl::New(HpkeKem::DHKEM_X25519_HKDF_SHA256,
65                             /*recipient_private_key=*/"");
66   ASSERT_THAT(result.status(), StatusIs(absl::StatusCode::kUnknown));
67 }
68 
69 }  // namespace
70 }  // namespace internal
71 }  // namespace tink
72 }  // namespace crypto
73