Botan 3.10.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/internal/fmt.h>
12#include <botan/internal/stl_util.h>
13
14namespace Botan {
15
16Hybrid_PublicKey::Hybrid_PublicKey(std::vector<std::unique_ptr<Public_Key>> pks) :
17 m_pks(std::move(pks)), m_key_length(0), m_estimated_strength(0) {
18 BOTAN_ARG_CHECK(m_pks.size() >= 2, "List of public keys must include at least two keys");
19 for(const auto& pk : m_pks) {
20 BOTAN_ARG_CHECK(pk != nullptr, "List of public keys contains a nullptr");
22 fmt("Public key type '{}' does not support key encapsulation", pk->algo_name()).c_str());
23 m_key_length = std::max(m_key_length, pk->key_length());
24 m_estimated_strength = std::max(m_estimated_strength, pk->estimated_strength());
25 }
26}
27
29 return reduce(public_keys(), true, [&](bool ckr, const auto& key) { return ckr && key->check_key(rng, strong); });
30}
31
32std::vector<uint8_t> Hybrid_PublicKey::raw_public_key_bits() const {
33 return reduce(public_keys(), std::vector<uint8_t>(), [](auto pkb, const auto& key) {
34 return concat(pkb, key->raw_public_key_bits());
35 });
36}
37
41
42std::vector<std::unique_ptr<Private_Key>> Hybrid_PublicKey::generate_other_sks_from_pks(
43 RandomNumberGenerator& rng) const {
44 std::vector<std::unique_ptr<Private_Key>> new_private_keys;
45 new_private_keys.reserve(public_keys().size());
46 for(const auto& pk : public_keys()) {
47 new_private_keys.push_back(pk->generate_another(rng));
48 }
49 return new_private_keys;
50}
51
52Hybrid_PrivateKey::Hybrid_PrivateKey(std::vector<std::unique_ptr<Private_Key>> private_keys) :
53 m_sks(std::move(private_keys)) {
54 BOTAN_ARG_CHECK(m_sks.size() >= 2, "List of secret keys must include at least two keys");
55 for(const auto& sk : m_sks) {
56 BOTAN_ARG_CHECK(sk != nullptr, "List of secret keys contains a nullptr");
58 "Some provided secret key is not compatible with this hybrid wrapper");
59 }
60}
61
63 throw Not_Implemented("Hybrid private keys cannot be serialized");
64}
65
67 return reduce(private_keys(), true, [&](bool ckr, const auto& key) { return ckr && key->check_key(rng, strong); });
68}
69
70std::vector<std::unique_ptr<Public_Key>> Hybrid_PrivateKey::extract_public_keys(
71 const std::vector<std::unique_ptr<Private_Key>>& private_keys) {
72 std::vector<std::unique_ptr<Public_Key>> public_keys;
73 public_keys.reserve(private_keys.size());
74 for(const auto& sk : private_keys) {
75 BOTAN_ARG_CHECK(sk != nullptr, "List of private keys contains a nullptr");
76 public_keys.push_back(sk->public_key());
77 }
78 return public_keys;
79}
80
81} // 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:38
PublicKeyOperation
Definition pk_keys.h:46
constexpr auto concat(Rs &&... ranges)
Definition stl_util.h:254
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69