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 "absl/algorithm/container.h"
24 #include "absl/status/status.h"
25 #include "absl/strings/str_cat.h"
26 #include "openssl/base.h"
27 #include "openssl/err.h"
28 #include "openssl/hpke.h"
29 #include "tink/hybrid/internal/hpke_util_boringssl.h"
30 #include "tink/subtle/subtle_util.h"
31 #include "tink/util/status.h"
32 #include "tink/util/statusor.h"
33 #include "proto/hpke.pb.h"
34
35 namespace crypto {
36 namespace tink {
37 namespace internal {
38
New(const google::crypto::tink::HpkeParams & params,absl::string_view recipient_public_key,absl::string_view context_info)39 util::StatusOr<std::unique_ptr<HpkeEncryptBoringSsl>> HpkeEncryptBoringSsl::New(
40 const google::crypto::tink::HpkeParams& params,
41 absl::string_view recipient_public_key, absl::string_view context_info) {
42 auto hpke_encrypt = absl::WrapUnique(new HpkeEncryptBoringSsl());
43 util::Status status =
44 hpke_encrypt->Init(params, recipient_public_key, context_info);
45 if (!status.ok()) {
46 return status;
47 }
48 return std::move(hpke_encrypt);
49 }
50
51 util::StatusOr<std::unique_ptr<HpkeEncryptBoringSsl>>
NewForTesting(const google::crypto::tink::HpkeParams & params,absl::string_view recipient_public_key,absl::string_view context_info,absl::string_view seed_for_testing)52 HpkeEncryptBoringSsl::NewForTesting(
53 const google::crypto::tink::HpkeParams& params,
54 absl::string_view recipient_public_key, absl::string_view context_info,
55 absl::string_view seed_for_testing) {
56 auto hpke_encrypt = absl::WrapUnique(new HpkeEncryptBoringSsl());
57 util::Status status = hpke_encrypt->InitForTesting(
58 params, recipient_public_key, context_info, seed_for_testing);
59 if (!status.ok()) {
60 return status;
61 }
62 return std::move(hpke_encrypt);
63 }
64
Init(const google::crypto::tink::HpkeParams & params,absl::string_view recipient_public_key,absl::string_view context_info)65 util::Status HpkeEncryptBoringSsl::Init(
66 const google::crypto::tink::HpkeParams& params,
67 absl::string_view recipient_public_key, absl::string_view context_info) {
68 util::StatusOr<const EVP_HPKE_KEM *> kem = KemParam(params);
69 if (!kem.ok()) {
70 return kem.status();
71 }
72 util::StatusOr<const EVP_HPKE_KDF *> kdf = KdfParam(params);
73 if (!kdf.ok()) {
74 return kdf.status();
75 }
76 util::StatusOr<const EVP_HPKE_AEAD *> aead = AeadParam(params);
77 if (!aead.ok()) {
78 return aead.status();
79 }
80 uint8_t enc[EVP_HPKE_MAX_ENC_LENGTH];
81 size_t enc_len;
82 if (!EVP_HPKE_CTX_setup_sender(
83 sender_ctx_.get(), enc, &enc_len, sizeof(enc), *kem, *kdf, *aead,
84 reinterpret_cast<const uint8_t *>(recipient_public_key.data()),
85 recipient_public_key.size(),
86 reinterpret_cast<const uint8_t *>(context_info.data()),
87 context_info.size())) {
88 return util::Status(absl::StatusCode::kUnknown,
89 "Unable to set up HPKE sender context.");
90 }
91 encapsulated_key_ = std::string(reinterpret_cast<const char *>(enc), enc_len);
92 return util::OkStatus();
93 }
94
InitForTesting(const google::crypto::tink::HpkeParams & params,absl::string_view recipient_public_key,absl::string_view context_info,absl::string_view seed_for_testing)95 util::Status HpkeEncryptBoringSsl::InitForTesting(
96 const google::crypto::tink::HpkeParams& params,
97 absl::string_view recipient_public_key, absl::string_view context_info,
98 absl::string_view seed_for_testing) {
99 util::StatusOr<const EVP_HPKE_KEM *> kem = KemParam(params);
100 if (!kem.ok()) {
101 return kem.status();
102 }
103 util::StatusOr<const EVP_HPKE_KDF *> kdf = KdfParam(params);
104 if (!kdf.ok()) {
105 return kdf.status();
106 }
107 util::StatusOr<const EVP_HPKE_AEAD *> aead = AeadParam(params);
108 if (!aead.ok()) {
109 return aead.status();
110 }
111 uint8_t enc[EVP_HPKE_MAX_ENC_LENGTH];
112 size_t enc_len;
113 if (!EVP_HPKE_CTX_setup_sender_with_seed_for_testing(
114 sender_ctx_.get(), enc, &enc_len, sizeof(enc), *kem, *kdf, *aead,
115 reinterpret_cast<const uint8_t *>(recipient_public_key.data()),
116 recipient_public_key.size(),
117 reinterpret_cast<const uint8_t *>(context_info.data()),
118 context_info.size(),
119 reinterpret_cast<const uint8_t *>(seed_for_testing.data()),
120 seed_for_testing.size())) {
121 return util::Status(absl::StatusCode::kUnknown,
122 "Unable to set up HPKE sender context.");
123 }
124 encapsulated_key_ = std::string(reinterpret_cast<const char *>(enc), enc_len);
125 return util::OkStatus();
126 }
127
EncapsulateKeyThenEncrypt(absl::string_view plaintext,absl::string_view associated_data)128 util::StatusOr<std::string> HpkeEncryptBoringSsl::EncapsulateKeyThenEncrypt(
129 absl::string_view plaintext, absl::string_view associated_data) {
130 size_t enc_size = encapsulated_key_.size();
131 std::string ciphertext(encapsulated_key_);
132 subtle::ResizeStringUninitialized(
133 &ciphertext, enc_size + plaintext.size() +
134 EVP_HPKE_CTX_max_overhead(sender_ctx_.get()));
135 size_t max_out_len = ciphertext.size() - enc_size;
136 size_t ciphertext_size;
137 if (!EVP_HPKE_CTX_seal(
138 sender_ctx_.get(), reinterpret_cast<uint8_t *>(&ciphertext[enc_size]),
139 &ciphertext_size, max_out_len,
140 reinterpret_cast<const uint8_t *>(plaintext.data()), plaintext.size(),
141 reinterpret_cast<const uint8_t *>(associated_data.data()),
142 associated_data.size())) {
143 return util::Status(absl::StatusCode::kUnknown,
144 "BoringSSL HPKE encryption failed.");
145 }
146 return ciphertext;
147 }
148
149 } // namespace internal
150 } // namespace tink
151 } // namespace crypto
152