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 #ifndef TINK_HYBRID_INTERNAL_HPKE_CONTEXT_BORINGSSL_H_ 18 #define TINK_HYBRID_INTERNAL_HPKE_CONTEXT_BORINGSSL_H_ 19 20 #include <stddef.h> 21 22 #include <memory> 23 #include <string> 24 #include <utility> 25 26 #include "absl/strings/string_view.h" 27 #include "openssl/base.h" 28 #include "openssl/hpke.h" 29 #include "tink/hybrid/internal/hpke_util.h" 30 #include "tink/internal/ssl_unique_ptr.h" 31 #include "tink/util/secret_data.h" 32 #include "tink/util/statusor.h" 33 34 namespace crypto { 35 namespace tink { 36 namespace internal { 37 38 struct SenderHpkeContextBoringSsl; 39 40 class HpkeContextBoringSsl { 41 public: 42 // Sets up an HPKE sender context. Returns an error if initialization 43 // fails. Otherwise, returns a unique pointer to the sender context. 44 // 45 // `params`: HPKE parameters (KEM, KDF, and AEAD). 46 // `recipient_public_key`: KEM-encoding of recipient public key. 47 // `info`: Application-specific context for key derivation. 48 static crypto::tink::util::StatusOr<SenderHpkeContextBoringSsl> 49 SetupSender(const HpkeParams& params, absl::string_view recipient_public_key, 50 absl::string_view info); 51 52 // Sets up an HPKE recipient context. Returns an error if initialization 53 // fails. Otherwise, returns a unique pointer to the recipient context. 54 // 55 // `params`: HPKE parameters (KEM, KDF, and AEAD). 56 // `recipient_private_key`: Recipient private key. 57 // `encapsulated_key`: Encapsulated key. 58 // `info`: Application-specific context for key derivation. 59 static crypto::tink::util::StatusOr<std::unique_ptr<HpkeContextBoringSsl>> 60 SetupRecipient(const HpkeParams& params, 61 const util::SecretData& recipient_private_key, 62 absl::string_view encapsulated_key, absl::string_view info); 63 64 // Performs an AEAD encryption of `plaintext` with `associated_data`. Returns 65 // an error if encryption fails. Otherwise, returns the ciphertext. 66 crypto::tink::util::StatusOr<std::string> Seal( 67 absl::string_view plaintext, absl::string_view associated_data); 68 69 // Performs an AEAD decryption of `ciphertext` with `associated_data`. Returns 70 // an error if decryption fails. Otherwise, returns the plaintext. 71 crypto::tink::util::StatusOr<std::string> Open( 72 absl::string_view ciphertext, absl::string_view associated_data); 73 74 // Exports `secret_length` bytes of secret material using `exporter_context` 75 // for the input context. Returns an error if export fails. Otherwise, 76 // returns a secret of the requested length. 77 crypto::tink::util::StatusOr<util::SecretData> Export( 78 absl::string_view exporter_context, int64_t secret_length); 79 80 protected: HpkeContextBoringSsl(SslUniquePtr<EVP_HPKE_CTX> context)81 explicit HpkeContextBoringSsl(SslUniquePtr<EVP_HPKE_CTX> context) 82 : context_(std::move(context)) {} 83 84 private: 85 SslUniquePtr<EVP_HPKE_CTX> context_; 86 }; 87 88 struct SenderHpkeContextBoringSsl { 89 std::unique_ptr<HpkeContextBoringSsl> context; 90 std::string encapsulated_key; 91 }; 92 93 } // namespace internal 94 } // namespace tink 95 } // namespace crypto 96 97 #endif // TINK_HYBRID_INTERNAL_HPKE_CONTEXT_BORINGSSL_H_ 98