Botan 3.9.0
Crypto and TLS for C&
hybrid_kem.cpp
Go to the documentation of this file.
1/**
2* Abstraction for a combined KEM public and private key.
3*
4* (C) 2024 Jack Lloyd
5* 2024 Fabian Albert, René Meusel - Rohde & Schwarz Cybersecurity
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9#include <botan/internal/hybrid_kem.h>
10
11#include <botan/pk_algs.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/kex_to_kem_adapter.h>
14#include <botan/internal/pk_ops_impl.h>
15#include <botan/internal/stl_util.h>
16
17namespace Botan {
18
19Hybrid_PublicKey::Hybrid_PublicKey(std::vector<std::unique_ptr<Public_Key>> pks) :
20 m_pks(std::move(pks)), m_key_length(0), m_estimated_strength(0) {
21 BOTAN_ARG_CHECK(m_pks.size() >= 2, "List of public keys must include at least two keys");
22 for(const auto& pk : m_pks) {
23 BOTAN_ARG_CHECK(pk != nullptr, "List of public keys contains a nullptr");
25 fmt("Public key type '{}' does not support key encapsulation", pk->algo_name()).c_str());
26 m_key_length = std::max(m_key_length, pk->key_length());
27 m_estimated_strength = std::max(m_estimated_strength, pk->estimated_strength());
28 }
29}
30
32 return reduce(public_keys(), true, [&](bool ckr, const auto& key) { return ckr && key->check_key(rng, strong); });
33}
34
35std::vector<uint8_t> Hybrid_PublicKey::raw_public_key_bits() const {
36 return reduce(public_keys(), std::vector<uint8_t>(), [](auto pkb, const auto& key) {
37 return concat(pkb, key->raw_public_key_bits());
38 });
39}
40
44
45std::vector<std::unique_ptr<Private_Key>> Hybrid_PublicKey::generate_other_sks_from_pks(
46 RandomNumberGenerator& rng) const {
47 std::vector<std::unique_ptr<Private_Key>> new_private_keys;
48 new_private_keys.reserve(public_keys().size());
49 for(const auto& pk : public_keys()) {
50 new_private_keys.push_back(pk->generate_another(rng));
51 }
52 return new_private_keys;
53}
54
55Hybrid_PrivateKey::Hybrid_PrivateKey(std::vector<std::unique_ptr<Private_Key>> private_keys) :
56 m_sks(std::move(private_keys)) {
57 BOTAN_ARG_CHECK(m_sks.size() >= 2, "List of secret keys must include at least two keys");
58 for(const auto& sk : m_sks) {
59 BOTAN_ARG_CHECK(sk != nullptr, "List of secret keys contains a nullptr");
61 "Some provided secret key is not compatible with this hybrid wrapper");
62 }
63}
64
66 throw Not_Implemented("Hybrid private keys cannot be serialized");
67}
68
70 return reduce(private_keys(), true, [&](bool ckr, const auto& key) { return ckr && key->check_key(rng, strong); });
71}
72
73std::vector<std::unique_ptr<Public_Key>> Hybrid_PrivateKey::extract_public_keys(
74 const std::vector<std::unique_ptr<Private_Key>>& private_keys) {
75 std::vector<std::unique_ptr<Public_Key>> public_keys;
76 public_keys.reserve(private_keys.size());
77 for(const auto& sk : private_keys) {
78 BOTAN_ARG_CHECK(sk != nullptr, "List of private keys contains a nullptr");
79 public_keys.push_back(sk->public_key());
80 }
81 return public_keys;
82}
83
84} // namespace Botan
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
bool check_key(RandomNumberGenerator &rng, bool strong) const override
const std::vector< std::unique_ptr< Private_Key > > & private_keys() const
Definition hybrid_kem.h:119
Hybrid_PrivateKey(const Hybrid_PrivateKey &)=delete
secure_vector< uint8_t > private_key_bits() const override
Disabled by default.
static std::vector< std::unique_ptr< Public_Key > > extract_public_keys(const std::vector< std::unique_ptr< Private_Key > > &private_keys)
bool supports_operation(PublicKeyOperation op) const override
bool check_key(RandomNumberGenerator &rng, bool strong) const override
std::vector< std::unique_ptr< Private_Key > > generate_other_sks_from_pks(RandomNumberGenerator &rng) const
Helper function for generate_another. Generate a new private key for each public key in this hybrid k...
std::vector< uint8_t > raw_public_key_bits() const override
const std::vector< std::unique_ptr< Public_Key > > & public_keys() const
Definition hybrid_kem.h:66
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
RetT reduce(const std::vector< KeyT > &keys, RetT acc, ReducerT reducer)
Definition stl_util.h:39
PublicKeyOperation
Definition pk_keys.h:46
constexpr auto concat(Rs &&... ranges)
Definition stl_util.h:255
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69