9#include <botan/internal/hybrid_kem_ops.h>
11#include <botan/internal/stl_util.h>
16 std::string_view provider) :
17 m_encapsulated_key_length(0) {
18 m_encryptors.reserve(public_keys.size());
19 for(
const auto& pk : public_keys) {
20 const auto& newenc = m_encryptors.emplace_back(*pk,
"Raw", provider);
21 m_encapsulated_key_length += newenc.encapsulated_key_length();
26 std::span<uint8_t> out_shared_key,
28 size_t desired_shared_key_len,
29 std::span<const uint8_t> salt) {
31 "Encapsulated key output buffer has wrong size");
33 "Shared key output buffer has wrong size");
35 std::vector<secure_vector<uint8_t>> shared_secrets;
36 shared_secrets.reserve(m_encryptors.size());
38 std::vector<std::vector<uint8_t>> ciphertexts;
39 ciphertexts.reserve(m_encryptors.size());
41 for(
auto& encryptor : m_encryptors) {
43 shared_secrets.push_back(std::move(ss));
44 ciphertexts.push_back(std::move(ct));
51 const std::vector<std::vector<uint8_t>>& ciphertexts,
52 std::span<const uint8_t> salt) {
54 BOTAN_ARG_CHECK(ciphertexts.size() == m_encryptors.size(),
"Invalid number of ciphertexts");
57 for(
size_t idx = 0; idx < ciphertexts.size(); idx++) {
58 BOTAN_ARG_CHECK(ciphertexts.at(idx).size() == m_encryptors.at(idx).encapsulated_key_length(),
59 "Invalid ciphertext length");
60 ct_stuffer.
append(ciphertexts.at(idx));
66 const std::vector<std::unique_ptr<Private_Key>>& private_keys,
68 std::string_view provider) :
69 m_encapsulated_key_length(0) {
70 m_decryptors.reserve(private_keys.size());
71 for(
const auto& sk : private_keys) {
72 const auto& newenc = m_decryptors.emplace_back(*sk, rng,
"Raw", provider);
73 m_encapsulated_key_length += newenc.encapsulated_key_length();
78 std::span<const uint8_t> encapsulated_key,
79 size_t desired_shared_key_len,
80 std::span<const uint8_t> salt) {
84 std::vector<secure_vector<uint8_t>> shared_secrets;
85 shared_secrets.reserve(m_decryptors.size());
87 BOTAN_ASSERT(ciphertexts.size() == m_decryptors.size(),
"Correct number of ciphertexts");
89 for(
size_t idx = 0; idx < m_decryptors.size(); idx++) {
90 shared_secrets.push_back(m_decryptors.at(idx).decrypt(ciphertexts.at(idx), 0 ));
97 std::span<const uint8_t> concat_ciphertext) {
99 std::vector<std::vector<uint8_t>> ciphertexts;
100 ciphertexts.reserve(m_decryptors.size());
102 for(
const auto& decryptor : m_decryptors) {
103 ciphertexts.push_back(ct_slicer.
copy_as_vector(decryptor.encapsulated_key_length()));
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
auto copy_as_vector(const size_t count)
Helper class to ease in-place marshalling of concatenated fixed-length values.
constexpr void append(std::span< const uint8_t > buffer)
constexpr bool full() const
void kem_decrypt(std::span< uint8_t > out_shared_key, std::span< const uint8_t > encapsulated_key, size_t desired_shared_key_len, std::span< const uint8_t > salt) final
KEM_Decryption_with_Combiner(const std::vector< std::unique_ptr< Private_Key > > &private_keys, RandomNumberGenerator &rng, std::string_view provider)
size_t encapsulated_key_length() const override
The default implementation returns the sum of the encapsulated key lengths of the underlying KEMs.
virtual void combine_shared_secrets(std::span< uint8_t > out_shared_secret, const std::vector< secure_vector< uint8_t > > &shared_secrets, const std::vector< std::vector< uint8_t > > &ciphertexts, size_t desired_shared_key_len, std::span< const uint8_t > salt)=0
Describes how the shared secrets are combined to derive the final shared secret.
virtual std::vector< std::vector< uint8_t > > split_ciphertexts(std::span< const uint8_t > concat_ciphertext)
Defines how the individual ciphertexts are extracted from the combined ciphertext.
static std::pair< std::vector< uint8_t >, secure_vector< uint8_t > > destructure(KEM_Encapsulation &&kem)
KEM_Encryption_with_Combiner(const std::vector< std::unique_ptr< Public_Key > > &public_keys, std::string_view provider)
virtual void combine_ciphertexts(std::span< uint8_t > out_ciphertext, const std::vector< std::vector< uint8_t > > &ciphertexts, std::span< const uint8_t > salt)
Defines how multiple ciphertexts are combined into a single ciphertext.
void kem_encrypt(std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_shared_key, RandomNumberGenerator &rng, size_t desired_shared_key_len, std::span< const uint8_t > salt) final
size_t encapsulated_key_length() const override
The default implementation returns the sum of the encapsulated key lengths of the underlying KEMs.
virtual void combine_shared_secrets(std::span< uint8_t > out_shared_secret, const std::vector< secure_vector< uint8_t > > &shared_secrets, const std::vector< std::vector< uint8_t > > &ciphertexts, size_t desired_shared_key_len, std::span< const uint8_t > salt)=0
Describes how the shared secrets are combined to derive the final shared secret.
virtual size_t shared_key_length(size_t desired_shared_key_len) const =0
virtual size_t shared_key_length(size_t desired_shared_key_len) const =0