xref: /aosp_15_r20/external/tink/cc/hybrid/internal/hpke_key_boringssl.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 <utility>
21 
22 #include "absl/status/status.h"
23 #include "openssl/base.h"
24 #include "openssl/err.h"
25 #include "openssl/hpke.h"
26 #include "tink/hybrid/internal/hpke_util_boringssl.h"
27 #include "tink/util/status.h"
28 #include "tink/util/statusor.h"
29 #include "proto/hpke.pb.h"
30 
31 namespace crypto {
32 namespace tink {
33 namespace internal {
34 
New(const google::crypto::tink::HpkeKem & kem,absl::string_view recipient_private_key)35 util::StatusOr<std::unique_ptr<HpkeKeyBoringSsl>> HpkeKeyBoringSsl::New(
36     const google::crypto::tink::HpkeKem& kem,
37     absl::string_view recipient_private_key) {
38   std::unique_ptr<HpkeKeyBoringSsl> hpke_key =
39       absl::WrapUnique(new HpkeKeyBoringSsl(kem));
40   util::Status status = hpke_key->Init(recipient_private_key);
41   if (!status.ok()) {
42     return status;
43   }
44   return std::move(hpke_key);
45 }
46 
Init(absl::string_view recipient_private_key)47 util::Status HpkeKeyBoringSsl::Init(absl::string_view recipient_private_key) {
48   util::StatusOr<const EVP_HPKE_KEM*> hpke_kem = KemParam(kem_);
49   if (!hpke_kem.ok()) {
50     return hpke_kem.status();
51   }
52   if (!EVP_HPKE_KEY_init(
53           recipient_private_key_.get(), *hpke_kem,
54           reinterpret_cast<const uint8_t*>(recipient_private_key.data()),
55           recipient_private_key.size())) {
56     return util::Status(
57         absl::StatusCode::kUnknown,
58         "Unable to initialize BoringSSL HPKE recipient private key.");
59   }
60   return util::OkStatus();
61 }
62 
63 }  // namespace internal
64 }  // namespace tink
65 }  // namespace crypto
66