xref: /aosp_15_r20/external/tink/cc/hybrid/internal/hpke_decrypt_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_decrypt_boringssl.h"
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "absl/status/status.h"
24 #include "absl/strings/str_cat.h"
25 #include "openssl/base.h"
26 #include "openssl/err.h"
27 #include "openssl/hpke.h"
28 #include "tink/hybrid/internal/hpke_util_boringssl.h"
29 #include "tink/subtle/subtle_util.h"
30 #include "tink/util/status.h"
31 #include "tink/util/statusor.h"
32 #include "proto/hpke.pb.h"
33 
34 namespace crypto {
35 namespace tink {
36 namespace internal {
37 
New(const google::crypto::tink::HpkeParams & params,const HpkeKeyBoringSsl & hpke_key,absl::string_view encapsulated_key,absl::string_view context_info)38 util::StatusOr<std::unique_ptr<HpkeDecryptBoringSsl>> HpkeDecryptBoringSsl::New(
39     const google::crypto::tink::HpkeParams& params,
40     const HpkeKeyBoringSsl& hpke_key, absl::string_view encapsulated_key,
41     absl::string_view context_info) {
42   auto hpke_decrypt = absl::WrapUnique(new HpkeDecryptBoringSsl());
43   util::Status status =
44       hpke_decrypt->Init(params, hpke_key, encapsulated_key, context_info);
45   if (!status.ok()) {
46     return status;
47   }
48   return std::move(hpke_decrypt);
49 }
50 
Init(const google::crypto::tink::HpkeParams & params,const HpkeKeyBoringSsl & hpke_key,absl::string_view encapsulated_key,absl::string_view context_info)51 util::Status HpkeDecryptBoringSsl::Init(
52     const google::crypto::tink::HpkeParams& params,
53     const HpkeKeyBoringSsl& hpke_key, absl::string_view encapsulated_key,
54     absl::string_view context_info) {
55   util::StatusOr<const EVP_HPKE_KEM *> kem = KemParam(params);
56   if (!kem.ok()) {
57     return kem.status();
58   }
59   if (params.kem() != hpke_key.kem()) {
60     return util::Status(
61         absl::StatusCode::kInvalidArgument,
62         absl::StrCat("Specified KEM parameter '", params.kem(),
63                      "' does not match given HPKE key's KEM parameter '",
64                      hpke_key.kem(), "'."));
65   }
66   util::StatusOr<const EVP_HPKE_KDF *> kdf = KdfParam(params);
67   if (!kdf.ok()) {
68     return kdf.status();
69   }
70   util::StatusOr<const EVP_HPKE_AEAD *> aead = AeadParam(params);
71   if (!aead.ok()) {
72     return aead.status();
73   }
74   if (!EVP_HPKE_CTX_setup_recipient(
75           recipient_ctx_.get(), hpke_key.recipient_private_key(), *kdf, *aead,
76           reinterpret_cast<const uint8_t *>(encapsulated_key.data()),
77           encapsulated_key.size(),
78           reinterpret_cast<const uint8_t *>(context_info.data()),
79           context_info.size())) {
80     return util::Status(absl::StatusCode::kUnknown,
81                         "Unable to set up BoringSSL HPKE recipient context.");
82   }
83   return util::OkStatus();
84 }
85 
Decrypt(absl::string_view ciphertext,absl::string_view associated_data)86 util::StatusOr<std::string> HpkeDecryptBoringSsl::Decrypt(
87     absl::string_view ciphertext, absl::string_view associated_data) {
88   std::string plaintext;
89   subtle::ResizeStringUninitialized(&plaintext, ciphertext.size());
90   size_t plaintext_size;
91   if (!EVP_HPKE_CTX_open(
92           recipient_ctx_.get(), reinterpret_cast<uint8_t *>(&plaintext[0]),
93           &plaintext_size, plaintext.size(),
94           reinterpret_cast<const uint8_t *>(ciphertext.data()),
95           ciphertext.size(),
96           reinterpret_cast<const uint8_t *>(associated_data.data()),
97           associated_data.size())) {
98     return util::Status(absl::StatusCode::kUnknown,
99                         "BoringSSL HPKE decryption failed.");
100   }
101   subtle::ResizeStringUninitialized(&plaintext, plaintext_size);
102   return plaintext;
103 }
104 
105 }  // namespace internal
106 }  // namespace tink
107 }  // namespace crypto
108