11#include <botan/internal/hybrid_public_key.h>
13#include <botan/pk_algs.h>
15#include <botan/internal/fmt.h>
16#include <botan/internal/kex_to_kem_adapter.h>
17#include <botan/internal/pk_ops_impl.h>
18#include <botan/internal/stl_util.h>
24std::vector<std::pair<std::string, std::string>> algorithm_specs_for_group(Group_Params group) {
27 switch(group.code()) {
39 case Group_Params::HYBRID_X25519_ML_KEM_768:
40 return {{
"ML-KEM",
"ML-KEM-768"}, {
"X25519",
"X25519"}};
41 case Group_Params::HYBRID_SECP256R1_ML_KEM_768:
42 return {{
"ECDH",
"secp256r1"}, {
"ML-KEM",
"ML-KEM-768"}};
44 case Group_Params::HYBRID_X25519_KYBER_512_R3_OQS:
45 case Group_Params::HYBRID_X25519_KYBER_512_R3_CLOUDFLARE:
46 return {{
"X25519",
"X25519"}, {
"Kyber",
"Kyber-512-r3"}};
47 case Group_Params::HYBRID_X25519_KYBER_768_R3_OQS:
48 return {{
"X25519",
"X25519"}, {
"Kyber",
"Kyber-768-r3"}};
49 case Group_Params::HYBRID_X448_KYBER_768_R3_OQS:
50 return {{
"X448",
"X448"}, {
"Kyber",
"Kyber-768-r3"}};
51 case Group_Params::HYBRID_X25519_eFRODOKEM_640_SHAKE_OQS:
52 return {{
"X25519",
"X25519"}, {
"FrodoKEM",
"eFrodoKEM-640-SHAKE"}};
53 case Group_Params::HYBRID_X25519_eFRODOKEM_640_AES_OQS:
54 return {{
"X25519",
"X25519"}, {
"FrodoKEM",
"eFrodoKEM-640-AES"}};
55 case Group_Params::HYBRID_X448_eFRODOKEM_976_SHAKE_OQS:
56 return {{
"X448",
"X448"}, {
"FrodoKEM",
"eFrodoKEM-976-SHAKE"}};
57 case Group_Params::HYBRID_X448_eFRODOKEM_976_AES_OQS:
58 return {{
"X448",
"X448"}, {
"FrodoKEM",
"eFrodoKEM-976-AES"}};
60 case Group_Params::HYBRID_SECP256R1_KYBER_512_R3_OQS:
61 return {{
"ECDH",
"secp256r1"}, {
"Kyber",
"Kyber-512-r3"}};
62 case Group_Params::HYBRID_SECP256R1_KYBER_768_R3_OQS:
63 return {{
"ECDH",
"secp256r1"}, {
"Kyber",
"Kyber-768-r3"}};
64 case Group_Params::HYBRID_SECP256R1_eFRODOKEM_640_SHAKE_OQS:
65 return {{
"ECDH",
"secp256r1"}, {
"FrodoKEM",
"eFrodoKEM-640-SHAKE"}};
66 case Group_Params::HYBRID_SECP256R1_eFRODOKEM_640_AES_OQS:
67 return {{
"ECDH",
"secp256r1"}, {
"FrodoKEM",
"eFrodoKEM-640-AES"}};
69 case Group_Params::HYBRID_SECP384R1_KYBER_768_R3_OQS:
70 return {{
"ECDH",
"secp384r1"}, {
"Kyber",
"Kyber-768-r3"}};
71 case Group_Params::HYBRID_SECP384R1_eFRODOKEM_976_SHAKE_OQS:
72 return {{
"ECDH",
"secp384r1"}, {
"FrodoKEM",
"eFrodoKEM-976-SHAKE"}};
73 case Group_Params::HYBRID_SECP384R1_eFRODOKEM_976_AES_OQS:
74 return {{
"ECDH",
"secp384r1"}, {
"FrodoKEM",
"eFrodoKEM-976-AES"}};
76 case Group_Params::HYBRID_SECP521R1_KYBER_1024_R3_OQS:
77 return {{
"ECDH",
"secp521r1"}, {
"Kyber",
"Kyber-1024-r3"}};
78 case Group_Params::HYBRID_SECP521R1_eFRODOKEM_1344_SHAKE_OQS:
79 return {{
"ECDH",
"secp521r1"}, {
"FrodoKEM",
"eFrodoKEM-1344-SHAKE"}};
80 case Group_Params::HYBRID_SECP521R1_eFRODOKEM_1344_AES_OQS:
81 return {{
"ECDH",
"secp521r1"}, {
"FrodoKEM",
"eFrodoKEM-1344-AES"}};
88std::vector<AlgorithmIdentifier> algorithm_identifiers_for_group(Group_Params group) {
91 const auto specs = algorithm_specs_for_group(group);
92 std::vector<AlgorithmIdentifier> result;
93 result.reserve(specs.size());
102 for(
const auto& spec : specs) {
109std::vector<size_t> public_value_lengths_for_group(Group_Params group) {
116 switch(group.code()) {
117 case Group_Params::HYBRID_X25519_ML_KEM_768:
119 case Group_Params::HYBRID_SECP256R1_ML_KEM_768:
122 case Group_Params::HYBRID_X25519_KYBER_512_R3_CLOUDFLARE:
123 case Group_Params::HYBRID_X25519_KYBER_512_R3_OQS:
125 case Group_Params::HYBRID_X25519_KYBER_768_R3_OQS:
127 case Group_Params::HYBRID_X448_KYBER_768_R3_OQS:
129 case Group_Params::HYBRID_X25519_eFRODOKEM_640_SHAKE_OQS:
131 case Group_Params::HYBRID_X25519_eFRODOKEM_640_AES_OQS:
133 case Group_Params::HYBRID_X448_eFRODOKEM_976_SHAKE_OQS:
135 case Group_Params::HYBRID_X448_eFRODOKEM_976_AES_OQS:
138 case Group_Params::HYBRID_SECP256R1_KYBER_512_R3_OQS:
140 case Group_Params::HYBRID_SECP256R1_KYBER_768_R3_OQS:
142 case Group_Params::HYBRID_SECP256R1_eFRODOKEM_640_SHAKE_OQS:
144 case Group_Params::HYBRID_SECP256R1_eFRODOKEM_640_AES_OQS:
147 case Group_Params::HYBRID_SECP384R1_KYBER_768_R3_OQS:
149 case Group_Params::HYBRID_SECP384R1_eFRODOKEM_976_SHAKE_OQS:
151 case Group_Params::HYBRID_SECP384R1_eFRODOKEM_976_AES_OQS:
154 case Group_Params::HYBRID_SECP521R1_eFRODOKEM_1344_SHAKE_OQS:
156 case Group_Params::HYBRID_SECP521R1_eFRODOKEM_1344_AES_OQS:
158 case Group_Params::HYBRID_SECP521R1_KYBER_1024_R3_OQS:
169 Group_Params group, std::span<const uint8_t> concatenated_public_values) {
170 const auto public_value_lengths = public_value_lengths_for_group(group);
171 auto alg_ids = algorithm_identifiers_for_group(group);
174 const auto expected_public_values_length =
175 reduce(public_value_lengths,
size_t(0), [](
size_t acc,
size_t len) {
return acc + len; });
176 if(expected_public_values_length != concatenated_public_values.size()) {
177 throw Decoding_Error(
"Concatenated public values have an unexpected length");
180 BufferSlicer public_value_slicer(concatenated_public_values);
181 std::vector<std::unique_ptr<Public_Key>> pks;
182 for(
size_t idx = 0; idx < alg_ids.size(); ++idx) {
183 pks.emplace_back(
load_public_key(alg_ids[idx], public_value_slicer.
take(public_value_lengths[idx])));
186 return std::make_unique<Hybrid_KEM_PublicKey>(std::move(pks));
190 BOTAN_ARG_CHECK(pks.size() >= 2,
"List of public keys must include at least two keys");
191 BOTAN_ARG_CHECK(std::all_of(pks.begin(), pks.end(), [](
const auto& pk) { return pk != nullptr; }),
192 "List of public keys contains a nullptr");
196 return pk->supports_operation(PublicKeyOperation::KeyEncapsulation) ||
197 pk->supports_operation(PublicKeyOperation::KeyAgreement);
199 "Some provided public key is not compatible with this hybrid wrapper");
202 pks.begin(), pks.end(), std::back_inserter(
m_public_keys), [](
auto& key) -> std::unique_ptr<Public_Key> {
203 if(key->supports_operation(PublicKeyOperation::KeyAgreement) &&
204 !key->supports_operation(PublicKeyOperation::KeyEncapsulation)) {
205 return std::make_unique<KEX_to_KEM_Adapter_PublicKey>(std::move(key));
207 return std::move(key);
212 reduce(m_public_keys,
size_t(0), [](
size_t kl,
const auto& key) {
return std::max(kl, key->key_length()); });
213 m_estimated_strength =
reduce(
214 m_public_keys,
size_t(0), [](
size_t es,
const auto& key) {
return std::max(es, key->estimated_strength()); });
217std::string Hybrid_KEM_PublicKey::algo_name()
const {
218 std::ostringstream algo_name(
"Hybrid(");
219 for(
size_t i = 0; i < m_public_keys.size(); ++i) {
223 algo_name << m_public_keys[i]->algo_name();
226 return algo_name.str();
229size_t Hybrid_KEM_PublicKey::estimated_strength()
const {
230 return m_estimated_strength;
233size_t Hybrid_KEM_PublicKey::key_length()
const {
238 return reduce(m_public_keys,
true, [&](
bool ckr,
const auto& key) {
return ckr && key->check_key(rng, strong); });
245std::vector<uint8_t> Hybrid_KEM_PublicKey::public_key_bits()
const {
246 return raw_public_key_bits();
249std::vector<uint8_t> Hybrid_KEM_PublicKey::raw_public_key_bits()
const {
257 return reduce(m_public_keys, std::vector<uint8_t>(), [](
auto pkb,
const auto& key) {
258 return concat(pkb, key->raw_public_key_bits());
263 std::vector<std::unique_ptr<Private_Key>> new_private_keys;
265 m_public_keys.begin(), m_public_keys.end(), std::back_inserter(new_private_keys), [&](
const auto& public_key) {
266 return public_key->generate_another(rng);
268 return std::make_unique<Hybrid_KEM_PrivateKey>(std::move(new_private_keys));
280 std::string_view kdf,
281 std::string_view provider) :
282 PK_Ops::KEM_Encryption_with_KDF(kdf), m_raw_kem_shared_key_length(0), m_encapsulated_key_length(0) {
283 m_kem_encryptors.reserve(key.
public_keys().size());
285 const auto& newenc = m_kem_encryptors.emplace_back(*k,
"Raw", provider);
286 m_raw_kem_shared_key_length += newenc.shared_key_length(0 );
287 m_encapsulated_key_length += newenc.encapsulated_key_length();
291 size_t raw_kem_shared_key_length()
const override {
return m_raw_kem_shared_key_length; }
293 size_t encapsulated_key_length()
const override {
return m_encapsulated_key_length; }
295 void raw_kem_encrypt(std::span<uint8_t> out_encapsulated_key,
296 std::span<uint8_t> raw_shared_key,
301 BufferStuffer encaps_key_stuffer(out_encapsulated_key);
302 BufferStuffer shared_key_stuffer(raw_shared_key);
304 for(
auto& kem_enc : m_kem_encryptors) {
305 kem_enc.encrypt(encaps_key_stuffer.next(kem_enc.encapsulated_key_length()),
306 shared_key_stuffer.next(kem_enc.shared_key_length(0 )),
312 std::vector<PK_KEM_Encryptor> m_kem_encryptors;
313 size_t m_raw_kem_shared_key_length;
314 size_t m_encapsulated_key_length;
319std::unique_ptr<Botan::PK_Ops::KEM_Encryption> Hybrid_KEM_PublicKey::create_kem_encryption_op(
320 std::string_view kdf, std::string_view provider)
const {
321 return std::make_unique<Hybrid_KEM_Encryption_Operation>(*
this, kdf, provider);
326auto extract_public_keys(
const std::vector<std::unique_ptr<Private_Key>>& private_keys) {
327 std::vector<std::unique_ptr<Public_Key>> public_keys;
328 public_keys.reserve(private_keys.size());
329 for(
const auto& private_key : private_keys) {
330 BOTAN_ARG_CHECK(private_key !=
nullptr,
"List of private keys contains a nullptr");
331 public_keys.push_back(private_key->public_key());
338std::unique_ptr<Hybrid_KEM_PrivateKey> Hybrid_KEM_PrivateKey::generate_from_group(
Group_Params group,
340 const auto algo_spec = algorithm_specs_for_group(group);
341 std::vector<std::unique_ptr<Private_Key>> private_keys;
342 private_keys.reserve(algo_spec.size());
343 for(
const auto& spec : algo_spec) {
346 return std::make_unique<Hybrid_KEM_PrivateKey>(std::move(private_keys));
349Hybrid_KEM_PrivateKey::Hybrid_KEM_PrivateKey(std::vector<std::unique_ptr<Private_Key>> sks) :
351 BOTAN_ARG_CHECK(sks.size() >= 2,
"List of private keys must include at least two keys");
355 return sk->supports_operation(PublicKeyOperation::KeyEncapsulation) ||
356 sk->supports_operation(PublicKeyOperation::KeyAgreement);
358 "Some provided private key is not compatible with this hybrid wrapper");
361 sks.begin(), sks.end(), std::back_inserter(m_private_keys), [](
auto& key) -> std::unique_ptr<Private_Key> {
362 if(key->supports_operation(PublicKeyOperation::KeyAgreement) &&
363 !key->supports_operation(PublicKeyOperation::KeyEncapsulation)) {
364 auto ka_key = dynamic_cast<PK_Key_Agreement_Key*>(key.get());
365 BOTAN_ASSERT_NONNULL(ka_key);
367 return std::make_unique<KEX_to_KEM_Adapter_PrivateKey>(std::unique_ptr<PK_Key_Agreement_Key>(ka_key));
369 return std::move(key);
379 return std::make_unique<Hybrid_KEM_PublicKey>(extract_public_keys(m_private_keys));
392 const std::string_view kdf,
393 const std::string_view provider) :
394 PK_Ops::KEM_Decryption_with_KDF(kdf), m_encapsulated_key_length(0), m_raw_kem_shared_key_length(0) {
397 const auto& newdec = m_decryptors.emplace_back(*private_key, rng,
"Raw", provider);
398 m_encapsulated_key_length += newdec.encapsulated_key_length();
399 m_raw_kem_shared_key_length += newdec.shared_key_length(0 );
403 void raw_kem_decrypt(std::span<uint8_t> out_shared_key, std::span<const uint8_t> encap_key)
override {
410 for(
auto& decryptor : m_decryptors) {
411 decryptor.decrypt(shared_secret_stuffer.next(decryptor.shared_key_length(0 )),
412 encap_key_slicer.take(decryptor.encapsulated_key_length()));
416 size_t encapsulated_key_length()
const override {
return m_encapsulated_key_length; }
418 size_t raw_kem_shared_key_length()
const override {
return m_raw_kem_shared_key_length; }
421 std::vector<PK_KEM_Decryptor> m_decryptors;
422 size_t m_encapsulated_key_length;
423 size_t m_raw_kem_shared_key_length;
430 return std::make_unique<Hybrid_KEM_Decryption>(*
this, rng, kdf, provider);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
std::span< const uint8_t > take(const size_t count)
Helper class to ease in-place marshalling of concatenated fixed-length values.
bool check_key(RandomNumberGenerator &rng, bool strong) const override
std::unique_ptr< Public_Key > public_key() const override
std::unique_ptr< PK_Ops::KEM_Decryption > create_kem_decryption_op(RandomNumberGenerator &rng, std::string_view kdf, std::string_view provider="base") const override
const auto & private_keys() const
secure_vector< uint8_t > private_key_bits() const override
std::vector< std::unique_ptr< Public_Key > > m_public_keys
const auto & public_keys() const
Hybrid_KEM_PublicKey(std::vector< std::unique_ptr< Public_Key > > pks)
bool check_key(RandomNumberGenerator &rng, bool strong) const override
static std::unique_ptr< Hybrid_KEM_PublicKey > load_for_group(Group_Params group, std::span< const uint8_t > concatenated_public_values)
int(* final)(unsigned char *, CTX *)
std::unique_ptr< Private_Key > create_private_key(std::string_view alg_name, RandomNumberGenerator &rng, std::string_view params, std::string_view provider)
RetT reduce(const std::vector< KeyT > &keys, RetT acc, ReducerT reducer)
constexpr auto concat(Rs &&... ranges)
std::vector< T, secure_allocator< T > > secure_vector
std::unique_ptr< Public_Key > load_public_key(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)