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