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 #ifndef TINK_HYBRID_INTERNAL_HPKE_ENCRYPT_BORINGSSL_H_ 18 #define TINK_HYBRID_INTERNAL_HPKE_ENCRYPT_BORINGSSL_H_ 19 20 #include <memory> 21 #include <string> 22 #include <utility> 23 24 #include "absl/base/attributes.h" 25 #include "openssl/hpke.h" 26 #include "tink/util/statusor.h" 27 #include "proto/hpke.pb.h" 28 29 namespace crypto { 30 namespace tink { 31 namespace internal { 32 33 class ABSL_DEPRECATED("Use HpkeContext.") HpkeEncryptBoringSsl { 34 public: 35 // Sets up an HPKE sender context. Returns an error if initialization 36 // fails. Otherwise, returns a unique pointer to the sender context. 37 // 38 // `params`: HPKE parameters proto (KEM, KDF, and AEAD). 39 // `recipient_public_key`: KEM-encoding of recipient public key. 40 // `context_info`: Application-specific context for key derivation. 41 static util::StatusOr<std::unique_ptr<HpkeEncryptBoringSsl>> New( 42 const google::crypto::tink::HpkeParams& params, 43 absl::string_view recipient_public_key, absl::string_view context_info); 44 45 // NOTE: The following method SHOULD ONLY BE USED FOR TESTING. 46 // 47 // Sets up an HPKE sender context. Returns an error if initialization 48 // fails. Otherwise, returns a unique pointer to the sender context. 49 // 50 // `params`: HPKE parameters proto (KEM, KDF, and AEAD). 51 // `recipient_public_key`: KEM-encoding of recipient public key. 52 // `context_info`: Application-specific context for key derivation. 53 // `seed_for_testing`: Seed used to match test vector values. 54 static util::StatusOr<std::unique_ptr<HpkeEncryptBoringSsl>> NewForTesting( 55 const google::crypto::tink::HpkeParams& params, 56 absl::string_view recipient_public_key, absl::string_view context_info, 57 absl::string_view seed_for_testing); 58 59 // Performs an AEAD encryption of `plaintext` with `associated_data`. 60 // Returns an error if encryption fails. Otherwise, returns the ciphertext 61 // appended to the encapsulated key. 62 util::StatusOr<std::string> EncapsulateKeyThenEncrypt( 63 absl::string_view plaintext, absl::string_view associated_data); 64 encapsulated_key()65 const std::string& encapsulated_key() const { return encapsulated_key_; } 66 67 private: HpkeEncryptBoringSsl()68 HpkeEncryptBoringSsl() {} 69 70 util::Status Init(const google::crypto::tink::HpkeParams& params, 71 absl::string_view recipient_public_key, 72 absl::string_view context_info); 73 74 util::Status InitForTesting(const google::crypto::tink::HpkeParams& params, 75 absl::string_view recipient_public_key, 76 absl::string_view context_info, 77 absl::string_view seed_for_testing); 78 79 bssl::ScopedEVP_HPKE_CTX sender_ctx_; 80 std::string encapsulated_key_; 81 }; 82 83 } // namespace internal 84 } // namespace tink 85 } // namespace crypto 86 87 #endif // TINK_HYBRID_INTERNAL_HPKE_ENCRYPT_BORINGSSL_H_ 88