1 // Copyright 2022 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/test_hpke_context_boringssl.h"
18
19 #include <string>
20 #include <utility>
21
22 #include "absl/strings/string_view.h"
23 #include "openssl/base.h"
24 #include "openssl/hpke.h"
25 #include "tink/hybrid/internal/hpke_util.h"
26 #include "tink/hybrid/internal/hpke_util_boringssl.h"
27 #include "tink/internal/ssl_unique_ptr.h"
28 #include "tink/util/statusor.h"
29
30 namespace crypto {
31 namespace tink {
32 namespace internal {
33
34 util::StatusOr<SenderHpkeContextBoringSsl>
SetupSender(const HpkeParams & params,absl::string_view recipient_public_key,absl::string_view context_info,absl::string_view seed_for_testing)35 TestHpkeContextBoringSsl::SetupSender(
36 const HpkeParams ¶ms, absl::string_view recipient_public_key,
37 absl::string_view context_info, absl::string_view seed_for_testing) {
38 util::StatusOr<const EVP_HPKE_KEM *> kem = KemParam(params);
39 if (!kem.ok()) {
40 return kem.status();
41 }
42 util::StatusOr<const EVP_HPKE_KDF *> kdf = KdfParam(params);
43 if (!kdf.ok()) {
44 return kdf.status();
45 }
46 util::StatusOr<const EVP_HPKE_AEAD *> aead = AeadParam(params);
47 if (!aead.ok()) {
48 return aead.status();
49 }
50 uint8_t enc[EVP_HPKE_MAX_ENC_LENGTH];
51 size_t enc_len;
52 SslUniquePtr<EVP_HPKE_CTX> context(EVP_HPKE_CTX_new());
53 if (!EVP_HPKE_CTX_setup_sender_with_seed_for_testing(
54 context.get(), enc, &enc_len, sizeof(enc), *kem, *kdf, *aead,
55 reinterpret_cast<const uint8_t *>(recipient_public_key.data()),
56 recipient_public_key.size(),
57 reinterpret_cast<const uint8_t *>(context_info.data()),
58 context_info.size(),
59 reinterpret_cast<const uint8_t *>(seed_for_testing.data()),
60 seed_for_testing.size())) {
61 return util::Status(absl::StatusCode::kUnknown,
62 "Unable to set up HPKE sender context.");
63 }
64 SenderHpkeContextBoringSsl tuple;
65 tuple.context =
66 absl::WrapUnique(new TestHpkeContextBoringSsl(std::move(context)));
67 tuple.encapsulated_key =
68 std::string(reinterpret_cast<const char *>(enc), enc_len);
69 return std::move(tuple);
70 }
71
72 } // namespace internal
73 } // namespace tink
74 } // namespace crypto
75