Botan 3.14.1
Crypto and TLS for C&
hpke.cpp
Go to the documentation of this file.
1/*
2* (C) 2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/hpke.h>
8
9#include <botan/aead.h>
10#include <botan/exceptn.h>
11#include <botan/mac.h>
12#include <botan/pk_keys.h>
13#include <botan/rng.h>
14#include <botan/internal/concat_util.h>
15#include <botan/internal/fmt.h>
16#include <botan/internal/hpke_kem.h>
17#include <botan/internal/mem_utils.h>
18#include <array>
19#include <limits>
20
21namespace Botan::HPKE {
22
23namespace {
24
25struct KEM_Info {
26 size_t nsecret;
27 size_t nenc;
28 size_t npk;
29 size_t nsk;
30 const char* name;
31};
32
33std::optional<KEM_Info> kem_info(KEM_Code code) {
34 // See RFC 9180 Section 7.1
35 switch(code) {
37 return KEM_Info{32, 65, 65, 32, "DHKEM(P-256, HKDF-SHA256)"};
39 return KEM_Info{48, 97, 97, 48, "DHKEM(P-384, HKDF-SHA384)"};
41 return KEM_Info{64, 133, 133, 66, "DHKEM(P-521, HKDF-SHA512)"};
43 return KEM_Info{32, 32, 32, 32, "DHKEM(X25519, HKDF-SHA256)"};
45 return KEM_Info{64, 56, 56, 56, "DHKEM(X448, HKDF-SHA512)"};
46 }
47 return std::nullopt;
48}
49
50KEM_Info require_kem_info(const KEM_Id& kem) {
51 if(const auto info = kem_info(kem.code())) {
52 return *info;
53 }
54 throw Invalid_State(fmt("Unknown HPKE KEM {}", kem.wire_code()));
55}
56
57struct KDF_Info {
58 size_t nh;
59 const char* hash;
60 const char* name;
61};
62
63std::optional<KDF_Info> kdf_info(KDF_Code code) {
64 // See RFC 9180 Section 7.2
65 switch(code) {
67 return KDF_Info{32, "SHA-256", "HKDF-SHA256"};
69 return KDF_Info{48, "SHA-384", "HKDF-SHA384"};
71 return KDF_Info{64, "SHA-512", "HKDF-SHA512"};
72 }
73 return std::nullopt;
74}
75
76std::string kdf_hash_name(KDF_Id kdf) {
77 if(const auto info = kdf_info(kdf.code())) {
78 return info->hash;
79 }
80 throw Not_Implemented(fmt("Unknown HPKE KDF {}", kdf.wire_code()));
81}
82
83struct AEAD_Info {
84 size_t nk;
85 size_t nn;
86 size_t nt;
87 const char* name;
88 const char* spec;
89};
90
91std::optional<AEAD_Info> aead_info(AEAD_Code code) {
92 // See RFC 9180 Section 7.3
93 switch(code) {
95 return AEAD_Info{16, 12, 16, "AES-128-GCM", "AES-128/GCM"};
97 return AEAD_Info{32, 12, 16, "AES-256-GCM", "AES-256/GCM"};
99 return AEAD_Info{32, 12, 16, "ChaCha20Poly1305", "ChaCha20Poly1305"};
101 return std::nullopt;
102 }
103 return std::nullopt;
104}
105
106std::string aead_spec(AEAD_Id aead) {
107 if(const auto info = aead_info(aead.code())) {
108 return info->spec;
109 }
110 throw Not_Implemented(fmt("Unknown HPKE AEAD {}", aead.wire_code()));
111}
112
113} // namespace
114
115/*
116* Identifier metadata
117*/
118
120 try {
121 KEM_Ops::create(*this);
122 return true;
123 } catch(Exception&) {
124 return false;
125 }
126}
127
129 return require_kem_info(*this).nsecret;
130}
131
133 return require_kem_info(*this).nenc;
134}
135
137 return require_kem_info(*this).npk;
138}
139
141 return require_kem_info(*this).nsk;
142}
143
144std::optional<std::string> KEM_Id::to_string() const {
145 if(const auto info = kem_info(m_code)) {
146 return std::string(info->name);
147 }
148 return std::nullopt;
149}
150
152 if(const auto info = kdf_info(m_code)) {
153 return MessageAuthenticationCode::create(fmt("HMAC({})", info->hash)) != nullptr;
154 }
155 return false;
156}
157
158size_t KDF_Id::output_length() const {
159 if(const auto info = kdf_info(m_code)) {
160 return info->nh;
161 }
162 throw Invalid_State(fmt("Unknown HPKE KDF {}", wire_code()));
163}
164
165std::optional<std::string> KDF_Id::to_string() const {
166 if(const auto info = kdf_info(m_code)) {
167 return std::string(info->name);
168 }
169 return std::nullopt;
170}
171
173 if(is_export_only()) {
174 return true;
175 }
176 if(const auto info = aead_info(m_code)) {
177 return AEAD_Mode::create(info->spec, Cipher_Dir::Encryption) != nullptr;
178 }
179 return false;
180}
181
182size_t AEAD_Id::key_length() const {
183 if(const auto info = aead_info(m_code)) {
184 return info->nk;
185 }
186 throw Invalid_State(fmt("HPKE AEAD {} does not define a key length", wire_code()));
187}
188
189size_t AEAD_Id::nonce_length() const {
190 if(const auto info = aead_info(m_code)) {
191 return info->nn;
192 }
193 throw Invalid_State(fmt("HPKE AEAD {} does not define a nonce length", wire_code()));
194}
195
196size_t AEAD_Id::tag_length() const {
197 if(const auto info = aead_info(m_code)) {
198 return info->nt;
199 }
200 throw Invalid_State(fmt("HPKE AEAD {} does not define a tag length", wire_code()));
201}
202
203std::optional<std::string> AEAD_Id::to_string() const {
204 if(is_export_only()) {
205 return std::string("export-only");
206 }
207 if(const auto info = aead_info(m_code)) {
208 return std::string(info->name);
209 }
210 return std::nullopt;
211}
212
213std::optional<std::string> Suite::to_string() const {
214 const auto kem = m_kem.to_string();
215 const auto kdf = m_kdf.to_string();
216 const auto aead = m_aead.to_string();
217 if(kem && kdf && aead) {
218 return fmt("{}/{}/{}", *kem, *kdf, *aead);
219 }
220 return std::nullopt;
221}
222
223/*
224* Keys
225*/
226
227class Public_Key::Data final {
228 public:
229 Data(std::shared_ptr<const KEM_Ops> ops, std::unique_ptr<const Botan::Public_Key> key) :
230 m_ops(std::move(ops)), m_key(std::move(key)), m_serialized(m_ops->serialize_public(*m_key)) {}
231
232 const KEM_Ops& ops() const { return *m_ops; }
233
234 const std::shared_ptr<const KEM_Ops>& ops_ptr() const { return m_ops; }
235
236 const Botan::Public_Key& key() const { return *m_key; }
237
238 const std::vector<uint8_t>& serialized() const { return m_serialized; }
239
240 private:
241 std::shared_ptr<const KEM_Ops> m_ops;
242 std::unique_ptr<const Botan::Public_Key> m_key;
243 std::vector<uint8_t> m_serialized;
244};
245
246Public_Key Public_Key::deserialize(KEM_Id kem, std::span<const uint8_t> bytes) {
247 std::shared_ptr<const KEM_Ops> ops = KEM_Ops::create(kem);
248 auto key = ops->deserialize_public(bytes);
249 return Public_Key(std::make_shared<Data>(std::move(ops), std::move(key)));
250}
251
252Public_Key Public_Key::from_key(KEM_Id kem, std::unique_ptr<Botan::Public_Key> key) {
253 BOTAN_ARG_CHECK(key != nullptr, "HPKE::Public_Key::from_key: key is null");
254 std::shared_ptr<const KEM_Ops> ops = KEM_Ops::create(kem);
255 auto adopted = ops->adopt_public(std::move(key));
256 return Public_Key(std::make_shared<Data>(std::move(ops), std::move(adopted)));
257}
258
260 return m_data->ops().kem_id();
261}
262
263std::vector<uint8_t> Public_Key::serialize() const {
264 return m_data->serialized();
265}
266
268 return m_data->key();
269}
270
271class Private_Key::Data final {
272 public:
273 Data(std::shared_ptr<const KEM_Ops> ops, std::unique_ptr<const Botan::Private_Key> key) :
274 m_ops(std::move(ops)), m_key(std::move(key)) {}
275
276 const KEM_Ops& ops() const { return *m_ops; }
277
278 const std::shared_ptr<const KEM_Ops>& ops_ptr() const { return m_ops; }
279
280 const Botan::Private_Key& key() const { return *m_key; }
281
282 private:
283 std::shared_ptr<const KEM_Ops> m_ops;
284 std::unique_ptr<const Botan::Private_Key> m_key;
285};
286
288 std::shared_ptr<const KEM_Ops> ops = KEM_Ops::create(kem);
289 auto key = ops->generate_key(rng);
290 return Private_Key(std::make_shared<Data>(std::move(ops), std::move(key)));
291}
292
293Private_Key Private_Key::derive(KEM_Id kem, std::span<const uint8_t> ikm) {
294 std::shared_ptr<const KEM_Ops> ops = KEM_Ops::create(kem);
295 auto key = ops->derive_key_pair(ikm);
296 return Private_Key(std::make_shared<Data>(std::move(ops), std::move(key)));
297}
298
299Private_Key Private_Key::deserialize(KEM_Id kem, std::span<const uint8_t> bytes) {
300 std::shared_ptr<const KEM_Ops> ops = KEM_Ops::create(kem);
301 auto key = ops->deserialize_private(bytes);
302 return Private_Key(std::make_shared<Data>(std::move(ops), std::move(key)));
303}
304
305Private_Key Private_Key::from_key(KEM_Id kem, std::unique_ptr<Botan::Private_Key> key) {
306 BOTAN_ARG_CHECK(key != nullptr, "HPKE::Private_Key::from_key: key is null");
307 std::shared_ptr<const KEM_Ops> ops = KEM_Ops::create(kem);
308 auto adopted = ops->adopt_private(std::move(key));
309 return Private_Key(std::make_shared<Data>(std::move(ops), std::move(adopted)));
310}
311
313 return m_data->ops().kem_id();
314}
315
317 auto pub = m_data->key().public_key();
318 return Public_Key(std::make_shared<Public_Key::Data>(m_data->ops_ptr(), std::move(pub)));
319}
320
322 return m_data->ops().serialize_private(m_data->key());
323}
324
326 return m_data->key();
327}
328
329/*
330* PSK
331*/
332
333PSK::PSK(std::span<const uint8_t> psk, std::span<const uint8_t> psk_id) :
334 m_psk(psk.begin(), psk.end()), m_psk_id(psk_id.begin(), psk_id.end()) {
335 // See RFC 9180 Section 5.1
336 BOTAN_ARG_CHECK(!m_psk.empty() && !m_psk_id.empty(), "HPKE PSK and PSK identity must both be non-empty");
337 // See RFC 9180 Section 9.5
338 BOTAN_ARG_CHECK(m_psk.size() >= 32, "HPKE PSK must be at least 32 bytes");
339}
340
341/*
342* Key schedule and contexts (RFC 9180 Section 5)
343*/
344
345namespace detail {
346
347class Context_Data final {
348 public:
349 Context_Data(const Suite& suite,
350 Mode mode,
351 Cipher_Dir direction,
352 std::span<const uint8_t> shared_secret,
353 std::span<const uint8_t> info,
354 const PSK* psk,
355 std::vector<uint8_t> enc) :
356 m_suite(suite), m_mode(mode), m_hash(kdf_hash_name(suite.kdf())), m_enc(std::move(enc)) {
357 // See RFC 9180 Section 5.1
358 const auto id_octets = [](uint16_t code) {
359 return std::array<uint8_t, 2>{static_cast<uint8_t>(code >> 8), static_cast<uint8_t>(code & 0xFF)};
360 };
361
362 m_suite_id = concat<std::vector<uint8_t>>(as_span_of_bytes(std::string_view("HPKE")),
363 id_octets(suite.kem().wire_code()),
364 id_octets(suite.kdf().wire_code()),
365 id_octets(suite.aead().wire_code()));
366
367 std::span<const uint8_t> psk_bytes;
368 std::span<const uint8_t> psk_id;
369 if(psk != nullptr) {
370 psk_bytes = psk->secret();
371 psk_id = psk->identity();
372 }
373
374 const auto psk_id_hash = labeled_extract(m_hash, m_suite_id, {}, "psk_id_hash", psk_id);
375 const auto info_hash = labeled_extract(m_hash, m_suite_id, {}, "info_hash", info);
376 const std::array<uint8_t, 1> mode_octet{static_cast<uint8_t>(mode)};
377 const auto ks_context = concat<std::vector<uint8_t>>(mode_octet, psk_id_hash, info_hash);
378
379 const auto secret = labeled_extract(m_hash, m_suite_id, shared_secret, "secret", psk_bytes);
380
381 // See RFC 9180 Section 5.3
382 if(!suite.is_export_only()) {
383 m_aead = AEAD_Mode::create_or_throw(aead_spec(suite.aead()), direction);
384
385 const auto key = labeled_expand(m_hash, m_suite_id, secret, "key", ks_context, suite.aead().key_length());
386 m_base_nonce =
387 labeled_expand(m_hash, m_suite_id, secret, "base_nonce", ks_context, suite.aead().nonce_length());
388 m_aead->set_key(key);
389
390 // RFC 9180 Section 5.2 permits sequence numbers up to 2^(8*Nn) - 2.
391 // Every AEAD defined there has Nn = 12, which exceeds the 64-bit
392 // counter used here, so the effective limit becomes 2^64 - 2: the
393 // final counter value is never used, so that the increment following
394 // a successful operation cannot overflow.
395 const size_t nn = m_base_nonce.size();
396 m_seq_limit = (nn >= 8) ? std::numeric_limits<uint64_t>::max() : ((uint64_t(1) << (8 * nn)) - 1);
397 }
398
399 m_exporter_secret = labeled_expand(m_hash, m_suite_id, secret, "exp", ks_context, suite.kdf().output_length());
400 }
401
402 Suite suite() const { return m_suite; }
403
404 Mode mode() const { return m_mode; }
405
406 const std::vector<uint8_t>& enc() const { return m_enc; }
407
408 uint64_t seq() const { return m_seq; }
409
410 template <typename T>
411 T crypt(std::optional<uint64_t> at_seq, std::span<const uint8_t> aad, std::span<const uint8_t> msg) {
412 // See RFC 9180 Section 7.3
413 if(!m_aead) {
414 throw Invalid_State("This HPKE suite is export-only and cannot seal or open messages");
415 }
416
417 // See RFC 9180 Section 5.2
418 const uint64_t seq = at_seq.value_or(m_seq);
419 if(seq >= m_seq_limit) {
420 throw Invalid_State("HPKE context message limit reached");
421 }
422
423 auto nonce = m_base_nonce;
424 for(size_t i = 0; i != 8 && i != nonce.size(); ++i) {
425 nonce[nonce.size() - 1 - i] ^= static_cast<uint8_t>(seq >> (8 * i));
426 }
427
428 if(msg.size() < m_aead->minimum_final_size()) {
429 throw Invalid_Authentication_Tag("HPKE ciphertext is shorter than the authentication tag");
430 }
431
432 T buf(msg.begin(), msg.end());
433 try {
434 m_aead->set_associated_data(aad);
435 m_aead->start(nonce);
436 m_aead->finish(buf);
437 } catch(...) {
438 // Failed messages must not leave the AEAD in a partially processed
439 // state: the caller may retry at the same sequence number.
440 m_aead->reset(); // NOLINT(*-ambiguous-smartptr-reset-call)
441 throw;
442 }
443
444 // Consume the sequence number only on success, and only when
445 // an explicit sequence number was not provided
446 if(!at_seq.has_value()) {
447 m_seq += 1;
448 }
449
450 return buf;
451 }
452
453 secure_vector<uint8_t> export_secret(std::span<const uint8_t> exporter_context, size_t length) const {
454 // See RFC 9180 Section 5.3
455 BOTAN_ARG_CHECK(length <= 255 * m_exporter_secret.size(), "HPKE export length too large");
456 return labeled_expand(m_hash, m_suite_id, m_exporter_secret, "sec", exporter_context, length);
457 }
458
459 private:
460 Suite m_suite;
461 Mode m_mode;
462 std::string m_hash;
463 std::vector<uint8_t> m_suite_id;
464 std::unique_ptr<AEAD_Mode> m_aead;
465 secure_vector<uint8_t> m_base_nonce;
466 secure_vector<uint8_t> m_exporter_secret;
467 uint64_t m_seq = 0;
468 uint64_t m_seq_limit = 0;
469 std::vector<uint8_t> m_enc;
470};
471
472} // namespace detail
473
474/*
475* Sender context
476*/
477
478Sender_Context::Sender_Context(std::unique_ptr<detail::Context_Data> data) : m_data(std::move(data)) {}
479
480Sender_Context::Sender_Context(Sender_Context&&) noexcept = default;
481Sender_Context& Sender_Context::operator=(Sender_Context&&) noexcept = default;
482Sender_Context::~Sender_Context() = default;
483
484Sender_Context Sender_Context::setup(Mode mode,
485 const Suite& suite,
486 const Public_Key& recipient_key,
487 const Private_Key* sender_identity_key,
488 const PSK* psk,
490 std::span<const uint8_t> info) {
491 // See RFC 9180 Section 5.1
492 if(recipient_key.kem() != suite.kem()) {
493 throw Invalid_Argument("HPKE recipient key does not match the suite's KEM");
494 }
495
496 const auto& ops = recipient_key.m_data->ops();
497
498 auto encap = [&]() {
499 if(sender_identity_key != nullptr) {
500 if(sender_identity_key->kem() != suite.kem()) {
501 throw Invalid_Argument("HPKE sender identity key does not match the suite's KEM");
502 }
503 return ops.auth_encap(recipient_key.m_data->key(), sender_identity_key->m_data->key(), rng);
504 }
505 return ops.encap(recipient_key.m_data->key(), rng);
506 }();
507
508 return Sender_Context(std::make_unique<detail::Context_Data>(
509 suite, mode, Cipher_Dir::Encryption, encap.shared_secret, info, psk, std::move(encap.enc)));
510}
511
513 const Public_Key& recipient_key,
515 std::span<const uint8_t> info) {
516 // See RFC 9180 Section 5.1.1
517 return setup(Mode::Base, suite, recipient_key, nullptr, nullptr, rng, info);
518}
519
521 const Public_Key& recipient_key,
523 const PSK& psk,
524 std::span<const uint8_t> info) {
525 // See RFC 9180 Section 5.1.2
526 return setup(Mode::PSK, suite, recipient_key, nullptr, &psk, rng, info);
527}
528
530 const Public_Key& recipient_key,
531 const Private_Key& sender_identity_key,
533 std::span<const uint8_t> info) {
534 // See RFC 9180 Section 5.1.3
535 return setup(Mode::Auth, suite, recipient_key, &sender_identity_key, nullptr, rng, info);
536}
537
539 const Public_Key& recipient_key,
540 const Private_Key& sender_identity_key,
542 const PSK& psk,
543 std::span<const uint8_t> info) {
544 // See RFC 9180 Section 5.1.4
545 return setup(Mode::AuthPSK, suite, recipient_key, &sender_identity_key, &psk, rng, info);
546}
547
548const std::vector<uint8_t>& Sender_Context::encapsulated_key() const {
549 return m_data->enc();
550}
551
552std::vector<uint8_t> Sender_Context::seal(std::span<const uint8_t> aad, std::span<const uint8_t> ptext) {
553 // See RFC 9180 Section 5.2
554 return m_data->crypt<std::vector<uint8_t>>(std::nullopt, aad, ptext);
555}
556
557secure_vector<uint8_t> Sender_Context::export_secret(std::span<const uint8_t> exporter_context, size_t length) const {
558 // See RFC 9180 Section 5.3
559 return m_data->export_secret(exporter_context, length);
560}
561
563 return m_data->seq();
564}
565
567 return m_data->mode();
568}
569
571 return m_data->suite();
572}
573
574/*
575* Recipient context
576*/
577
578Recipient_Context::Recipient_Context(std::unique_ptr<detail::Context_Data> data) : m_data(std::move(data)) {}
579
580Recipient_Context::Recipient_Context(Recipient_Context&&) noexcept = default;
581Recipient_Context& Recipient_Context::operator=(Recipient_Context&&) noexcept = default;
582Recipient_Context::~Recipient_Context() = default;
583
584Recipient_Context Recipient_Context::setup(Mode mode,
585 const Suite& suite,
586 const Private_Key& recipient_key,
587 std::span<const uint8_t> enc,
588 const Public_Key* sender_identity_key,
589 const PSK* psk,
591 std::span<const uint8_t> info) {
592 // See RFC 9180 Section 5.1
593 if(recipient_key.kem() != suite.kem()) {
594 throw Invalid_Argument("HPKE recipient key does not match the suite's KEM");
595 }
596
597 const auto& ops = recipient_key.m_data->ops();
598
599 auto shared_secret = [&]() {
600 if(sender_identity_key != nullptr) {
601 if(sender_identity_key->kem() != suite.kem()) {
602 throw Invalid_Argument("HPKE sender identity key does not match the suite's KEM");
603 }
604 return ops.auth_decap(enc, recipient_key.m_data->key(), sender_identity_key->m_data->key(), rng);
605 }
606 return ops.decap(enc, recipient_key.m_data->key(), rng);
607 }();
608
609 return Recipient_Context(std::make_unique<detail::Context_Data>(
610 suite, mode, Cipher_Dir::Decryption, shared_secret, info, psk, std::vector<uint8_t>{}));
611}
612
614 const Private_Key& recipient_key,
615 std::span<const uint8_t> enc,
617 std::span<const uint8_t> info) {
618 // See RFC 9180 Section 5.1.1
619 return setup(Mode::Base, suite, recipient_key, enc, nullptr, nullptr, rng, info);
620}
621
623 const Private_Key& recipient_key,
624 std::span<const uint8_t> enc,
626 const PSK& psk,
627 std::span<const uint8_t> info) {
628 // See RFC 9180 Section 5.1.2
629 return setup(Mode::PSK, suite, recipient_key, enc, nullptr, &psk, rng, info);
630}
631
633 const Private_Key& recipient_key,
634 std::span<const uint8_t> enc,
635 const Public_Key& sender_identity_key,
637 std::span<const uint8_t> info) {
638 // See RFC 9180 Section 5.1.3
639 return setup(Mode::Auth, suite, recipient_key, enc, &sender_identity_key, nullptr, rng, info);
640}
641
643 const Private_Key& recipient_key,
644 std::span<const uint8_t> enc,
645 const Public_Key& sender_identity_key,
647 const PSK& psk,
648 std::span<const uint8_t> info) {
649 // See RFC 9180 Section 5.1.4
650 return setup(Mode::AuthPSK, suite, recipient_key, enc, &sender_identity_key, &psk, rng, info);
651}
652
653secure_vector<uint8_t> Recipient_Context::open(std::span<const uint8_t> aad, std::span<const uint8_t> ctext) {
654 // See RFC 9180 Section 5.2
655 return m_data->crypt<secure_vector<uint8_t>>(std::nullopt, aad, ctext);
656}
657
659 std::span<const uint8_t> aad,
660 std::span<const uint8_t> ctext) {
661 return m_data->crypt<secure_vector<uint8_t>>(seq, aad, ctext);
662}
663
664secure_vector<uint8_t> Recipient_Context::export_secret(std::span<const uint8_t> exporter_context,
665 size_t length) const {
666 // See RFC 9180 Section 5.3
667 return m_data->export_secret(exporter_context, length);
668}
669
671 return m_data->seq();
672}
673
675 return m_data->mode();
676}
677
679 return m_data->suite();
680}
681
682} // namespace Botan::HPKE
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
static std::unique_ptr< AEAD_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
Definition aead.cpp:51
static std::unique_ptr< AEAD_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
Definition aead.cpp:61
constexpr uint16_t wire_code() const
Definition hpke.h:196
std::optional< std::string > to_string() const
Definition hpke.cpp:203
size_t nonce_length() const
Return the constant Nn from RFC 9180 Section 7.3, throws for invalid code points.
Definition hpke.cpp:189
bool is_available() const
Definition hpke.cpp:172
size_t tag_length() const
Return the constant Nt from RFC 9180 Section 7.3, throws for invalid code points.
Definition hpke.cpp:196
constexpr AEAD_Code code() const
Definition hpke.h:194
constexpr bool is_export_only() const
Definition hpke.h:217
size_t key_length() const
Return the constant Nk from RFC 9180 Section 7.3, throws for invalid code points.
Definition hpke.cpp:182
size_t output_length() const
Definition hpke.cpp:158
std::optional< std::string > to_string() const
Definition hpke.cpp:165
constexpr KDF_Code code() const
Definition hpke.h:150
bool is_available() const
Definition hpke.cpp:151
constexpr uint16_t wire_code() const
Definition hpke.h:152
size_t encapsulation_length() const
Return the constant Nenc from RFC 9180 Section 7.1, throws for invalid code points.
Definition hpke.cpp:132
std::optional< std::string > to_string() const
Returns std::nullopt for unknown codepoints.
Definition hpke.cpp:144
bool is_available() const
True if the algorithms this KEM requires are available in this build.
Definition hpke.cpp:119
constexpr KEM_Code code() const
Definition hpke.h:83
size_t public_key_length() const
Return the constant Npk from RFC 9180 Section 7.1, throws for invalid code points.
Definition hpke.cpp:136
constexpr uint16_t wire_code() const
Definition hpke.h:85
size_t shared_secret_length() const
Return the constant Nsecret from RFC 9180 Section 7.1, throws for invalid code points.
Definition hpke.cpp:128
size_t private_key_length() const
Return the constant Nsk from RFC 9180 Section 7.1, throws for invalid code points.
Definition hpke.cpp:140
static std::unique_ptr< const KEM_Ops > create(KEM_Id kem)
Definition hpke_kem.cpp:545
std::span< const uint8_t > identity() const
Definition hpke.h:432
std::span< const uint8_t > secret() const
Definition hpke.h:430
PSK(std::span< const uint8_t > psk, std::span< const uint8_t > psk_id)
Definition hpke.cpp:333
Public_Key public_key() const
The corresponding public key.
Definition hpke.cpp:316
static Private_Key generate(KEM_Id kem, RandomNumberGenerator &rng)
GenerateKeyPair from RFC 9180 Section 4.
Definition hpke.cpp:287
const Botan::Private_Key & underlying() const
Definition hpke.cpp:325
static Private_Key derive(KEM_Id kem, std::span< const uint8_t > ikm)
Definition hpke.cpp:293
static Private_Key deserialize(KEM_Id kem, std::span< const uint8_t > bytes)
Definition hpke.cpp:299
KEM_Id kem() const
Definition hpke.cpp:312
static Private_Key from_key(KEM_Id kem, std::unique_ptr< Botan::Private_Key > key)
Definition hpke.cpp:305
secure_vector< uint8_t > serialize() const
Definition hpke.cpp:321
static Public_Key deserialize(KEM_Id kem, std::span< const uint8_t > bytes)
Definition hpke.cpp:246
std::vector< uint8_t > serialize() const
SerializePublicKey from RFC 9180 Section 7.1.1.
Definition hpke.cpp:263
static Public_Key from_key(KEM_Id kem, std::unique_ptr< Botan::Public_Key > key)
Definition hpke.cpp:252
const Botan::Public_Key & underlying() const
Definition hpke.cpp:267
KEM_Id kem() const
Definition hpke.cpp:259
secure_vector< uint8_t > open_at_sequence(uint64_t seq, std::span< const uint8_t > aad, std::span< const uint8_t > ctext)
Definition hpke.cpp:658
secure_vector< uint8_t > export_secret(std::span< const uint8_t > exporter_context, size_t length) const
Definition hpke.cpp:664
static Recipient_Context setup_auth(const Suite &suite, const Private_Key &recipient_key, std::span< const uint8_t > enc, const Public_Key &sender_identity_key, RandomNumberGenerator &rng, std::span< const uint8_t > info={})
Definition hpke.cpp:632
secure_vector< uint8_t > open(std::span< const uint8_t > aad, std::span< const uint8_t > ctext)
Definition hpke.cpp:653
uint64_t next_sequence() const
Definition hpke.cpp:670
Recipient_Context(const Recipient_Context &)=delete
static Recipient_Context setup_psk(const Suite &suite, const Private_Key &recipient_key, std::span< const uint8_t > enc, RandomNumberGenerator &rng, const PSK &psk, std::span< const uint8_t > info={})
Definition hpke.cpp:622
static Recipient_Context setup_auth_psk(const Suite &suite, const Private_Key &recipient_key, std::span< const uint8_t > enc, const Public_Key &sender_identity_key, RandomNumberGenerator &rng, const PSK &psk, std::span< const uint8_t > info={})
Definition hpke.cpp:642
static Recipient_Context setup_base(const Suite &suite, const Private_Key &recipient_key, std::span< const uint8_t > enc, RandomNumberGenerator &rng, std::span< const uint8_t > info={})
Definition hpke.cpp:613
static Sender_Context setup_psk(const Suite &suite, const Public_Key &recipient_key, RandomNumberGenerator &rng, const PSK &psk, std::span< const uint8_t > info={})
Definition hpke.cpp:520
static Sender_Context setup_auth(const Suite &suite, const Public_Key &recipient_key, const Private_Key &sender_identity_key, RandomNumberGenerator &rng, std::span< const uint8_t > info={})
Definition hpke.cpp:529
static Sender_Context setup_base(const Suite &suite, const Public_Key &recipient_key, RandomNumberGenerator &rng, std::span< const uint8_t > info={})
Definition hpke.cpp:512
secure_vector< uint8_t > export_secret(std::span< const uint8_t > exporter_context, size_t length) const
Definition hpke.cpp:557
std::vector< uint8_t > seal(std::span< const uint8_t > aad, std::span< const uint8_t > ptext)
Definition hpke.cpp:552
Sender_Context(const Sender_Context &)=delete
uint64_t next_sequence() const
The sequence number the next call to seal() will use.
Definition hpke.cpp:562
const std::vector< uint8_t > & encapsulated_key() const
The KEM encapsulation ("enc") to be transmitted to the recipient.
Definition hpke.cpp:548
static Sender_Context setup_auth_psk(const Suite &suite, const Public_Key &recipient_key, const Private_Key &sender_identity_key, RandomNumberGenerator &rng, const PSK &psk, std::span< const uint8_t > info={})
Definition hpke.cpp:538
constexpr AEAD_Id aead() const
Definition hpke.h:249
constexpr KDF_Id kdf() const
Definition hpke.h:247
constexpr bool is_export_only() const
Definition hpke.h:259
std::optional< std::string > to_string() const
Definition hpke.cpp:213
constexpr KEM_Id kem() const
Definition hpke.h:245
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:51
const char * name
Definition ffi_tls.h:59
Mode
The four HPKE modes, with their codepoints from RFC 9180 Section 5.
Definition hpke.h:440
secure_vector< uint8_t > labeled_extract(std::string_view hash, std::span< const uint8_t > suite_id, std::span< const uint8_t > salt, std::string_view label, std::span< const uint8_t > ikm)
Definition hpke_kem.cpp:49
secure_vector< uint8_t > labeled_expand(std::string_view hash, std::span< const uint8_t > suite_id, std::span< const uint8_t > prk, std::string_view label, std::span< const uint8_t > info, size_t length)
Definition hpke_kem.cpp:65
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:59
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr auto concat(Rs &&... ranges)
Definition concat_util.h:95
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128