Botan 3.7.1
Crypto and TLS for C&
ffi_pkey_algs.cpp
Go to the documentation of this file.
1/*
2* (C) 2015,2017 Jack Lloyd
3* (C) 2017 Ribose Inc
4* (C) 2018 René Korthaus, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/ffi.h>
10
11#include <botan/hash.h>
12#include <botan/pem.h>
13#include <botan/internal/ffi_mp.h>
14#include <botan/internal/ffi_pkey.h>
15#include <botan/internal/ffi_rng.h>
16#include <botan/internal/ffi_util.h>
17
18#if defined(BOTAN_HAS_DL_GROUP)
19 #include <botan/dl_group.h>
20#endif
21
22#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
23 #include <botan/ecc_key.h>
24#endif
25
26#if defined(BOTAN_HAS_RSA)
27 #include <botan/rsa.h>
28#endif
29
30#if defined(BOTAN_HAS_ELGAMAL)
31 #include <botan/elgamal.h>
32#endif
33
34#if defined(BOTAN_HAS_DSA)
35 #include <botan/dsa.h>
36#endif
37
38#if defined(BOTAN_HAS_ECDSA)
39 #include <botan/ecdsa.h>
40#endif
41
42#if defined(BOTAN_HAS_SM2)
43 #include <botan/sm2.h>
44#endif
45
46#if defined(BOTAN_HAS_ECDH)
47 #include <botan/ecdh.h>
48#endif
49
50#if defined(BOTAN_HAS_X25519)
51 #include <botan/x25519.h>
52#endif
53
54#if defined(BOTAN_HAS_X448)
55 #include <botan/x448.h>
56#endif
57
58#if defined(BOTAN_HAS_ED25519)
59 #include <botan/ed25519.h>
60#endif
61
62#if defined(BOTAN_HAS_ED448)
63 #include <botan/ed448.h>
64#endif
65
66#if defined(BOTAN_HAS_MCELIECE)
67 #include <botan/mceliece.h>
68#endif
69
70#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
71 #include <botan/dh.h>
72#endif
73
74#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
75 #include <botan/kyber.h>
76#endif
77
78#if defined(BOTAN_HAS_ML_KEM)
79 #include <botan/ml_kem.h>
80#endif
81
82#if defined(BOTAN_HAS_FRODOKEM)
83 #include <botan/frodokem.h>
84#endif
85
86#if defined(BOTAN_HAS_ML_DSA)
87 #include <botan/ml_dsa.h>
88#endif
89
90#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
91 #include <botan/slh_dsa.h>
92#endif
93
94#if defined(BOTAN_HAS_CLASSICMCELIECE)
95 #include <botan/cmce.h>
96#endif
97
98namespace {
99
100#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
101
102// These are always called within an existing try/catch block
103
104template <class ECPrivateKey_t>
105int privkey_load_ec(std::unique_ptr<ECPrivateKey_t>& key, const Botan::BigInt& scalar, const char* curve_name) {
106 if(curve_name == nullptr) {
108 }
109
110 Botan::Null_RNG null_rng;
111 const auto grp = Botan::EC_Group::from_name(curve_name);
112 key.reset(new ECPrivateKey_t(null_rng, grp, scalar));
113 return BOTAN_FFI_SUCCESS;
114}
115
116template <class ECPublicKey_t>
117int pubkey_load_ec(std::unique_ptr<ECPublicKey_t>& key,
118 const Botan::BigInt& public_x,
119 const Botan::BigInt& public_y,
120 const char* curve_name) {
121 if(curve_name == nullptr) {
123 }
124
125 const auto group = Botan::EC_Group::from_name(curve_name);
126
127 if(auto pt = Botan::EC_AffinePoint::from_bigint_xy(group, public_x, public_y)) {
128 key.reset(new ECPublicKey_t(group, pt.value()));
129 return BOTAN_FFI_SUCCESS;
130 } else {
132 }
133}
134
135#endif
136
137Botan::BigInt pubkey_get_field(const Botan::Public_Key& key, std::string_view field) {
138#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
139 // Not currently handled by get_int_field
140 if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key)) {
141 if(field == "public_x") {
142 return Botan::BigInt::from_bytes(ecc->_public_ec_point().x_bytes());
143 } else if(field == "public_y") {
144 return Botan::BigInt::from_bytes(ecc->_public_ec_point().y_bytes());
145 }
146 }
147#endif
148
149 try {
150 return key.get_int_field(field);
152 throw Botan_FFI::FFI_Error("Unknown key field", BOTAN_FFI_ERROR_BAD_PARAMETER);
153 }
154}
155
156Botan::BigInt privkey_get_field(const Botan::Private_Key& key, std::string_view field) {
157#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
158 // Not currently handled by get_int_field
159 if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key)) {
160 if(field == "public_x") {
161 return Botan::BigInt::from_bytes(ecc->_public_ec_point().x_bytes());
162 } else if(field == "public_y") {
163 return Botan::BigInt::from_bytes(ecc->_public_ec_point().y_bytes());
164 }
165 }
166#endif
167
168 try {
169 return key.get_int_field(field);
171 throw Botan_FFI::FFI_Error("Unknown key field", BOTAN_FFI_ERROR_BAD_PARAMETER);
172 }
173}
174
175} // namespace
176
177extern "C" {
178
179using namespace Botan_FFI;
180
181int botan_pubkey_get_field(botan_mp_t output, botan_pubkey_t key, const char* field_name_cstr) {
182 if(field_name_cstr == nullptr) {
184 }
185
186 const std::string field_name(field_name_cstr);
187
188 return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = pubkey_get_field(k, field_name); });
189}
190
191int botan_privkey_get_field(botan_mp_t output, botan_privkey_t key, const char* field_name_cstr) {
192 if(field_name_cstr == nullptr) {
194 }
195
196 const std::string field_name(field_name_cstr);
197
198 return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = privkey_get_field(k, field_name); });
199}
200
201/* RSA specific operations */
202
203int botan_privkey_create_rsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n_bits) {
204 if(n_bits < 1024 || n_bits > 16 * 1024) {
206 }
207
208 std::string n_str = std::to_string(n_bits);
209
210 return botan_privkey_create(key_obj, "RSA", n_str.c_str(), rng_obj);
211}
212
214#if defined(BOTAN_HAS_RSA)
215 *key = nullptr;
216
217 return ffi_guard_thunk(__func__, [=]() -> int {
218 auto rsa = std::make_unique<Botan::RSA_PrivateKey>(safe_get(rsa_p), safe_get(rsa_q), safe_get(rsa_e));
219 *key = new botan_privkey_struct(std::move(rsa));
220 return BOTAN_FFI_SUCCESS;
221 });
222#else
223 BOTAN_UNUSED(key, rsa_p, rsa_q, rsa_e);
225#endif
226}
227
228int botan_privkey_load_rsa_pkcs1(botan_privkey_t* key, const uint8_t bits[], size_t len) {
229#if defined(BOTAN_HAS_RSA)
230 *key = nullptr;
231
232 Botan::secure_vector<uint8_t> src(bits, bits + len);
233 return ffi_guard_thunk(__func__, [=]() -> int {
235 auto rsa = std::make_unique<Botan::RSA_PrivateKey>(alg_id, src);
236 *key = new botan_privkey_struct(std::move(rsa));
237 return BOTAN_FFI_SUCCESS;
238 });
239#else
240 BOTAN_UNUSED(key, bits, len);
242#endif
243}
244
246#if defined(BOTAN_HAS_RSA)
247 *key = nullptr;
248 return ffi_guard_thunk(__func__, [=]() -> int {
249 auto rsa = std::make_unique<Botan::RSA_PublicKey>(safe_get(n), safe_get(e));
250 *key = new botan_pubkey_struct(std::move(rsa));
251 return BOTAN_FFI_SUCCESS;
252 });
253#else
254 BOTAN_UNUSED(key, n, e);
256#endif
257}
258
262
266
270
274
278
280 return botan_pubkey_get_field(e, key, "e");
281}
282
284 return botan_pubkey_get_field(n, key, "n");
285}
286
287int botan_privkey_rsa_get_privkey(botan_privkey_t rsa_key, uint8_t out[], size_t* out_len, uint32_t flags) {
288#if defined(BOTAN_HAS_RSA)
289 return BOTAN_FFI_VISIT(rsa_key, [=](const auto& k) -> int {
290 if(const Botan::RSA_PrivateKey* rsa = dynamic_cast<const Botan::RSA_PrivateKey*>(&k)) {
291 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
292 return write_vec_output(out, out_len, rsa->private_key_bits());
293 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
294 return write_str_output(out, out_len, Botan::PEM_Code::encode(rsa->private_key_bits(), "RSA PRIVATE KEY"));
295 } else {
297 }
298 } else {
300 }
301 });
302#else
303 BOTAN_UNUSED(rsa_key, out, out_len, flags);
305#endif
306}
307
308/* DSA specific operations */
309int botan_privkey_create_dsa(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
310#if defined(BOTAN_HAS_DSA)
311
312 if((rng_obj == nullptr) || (key == nullptr)) {
314 }
315
316 if((pbits % 64) || (qbits % 8) || (pbits < 1024) || (pbits > 3072) || (qbits < 160) || (qbits > 256)) {
318 }
319
320 return ffi_guard_thunk(__func__, [=]() -> int {
322 Botan::DL_Group group(rng, Botan::DL_Group::Prime_Subgroup, pbits, qbits);
323 auto dsa = std::make_unique<Botan::DSA_PrivateKey>(rng, group);
324 *key = new botan_privkey_struct(std::move(dsa));
325 return BOTAN_FFI_SUCCESS;
326 });
327#else
328 BOTAN_UNUSED(key, rng_obj, pbits, qbits);
330#endif
331}
332
334#if defined(BOTAN_HAS_DSA)
335 *key = nullptr;
336
337 return ffi_guard_thunk(__func__, [=]() -> int {
338 Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
339 auto dsa = std::make_unique<Botan::DSA_PrivateKey>(group, safe_get(x));
340 *key = new botan_privkey_struct(std::move(dsa));
341 return BOTAN_FFI_SUCCESS;
342 });
343#else
344 BOTAN_UNUSED(key, p, q, g, x);
346#endif
347}
348
350#if defined(BOTAN_HAS_DSA)
351 *key = nullptr;
352
353 return ffi_guard_thunk(__func__, [=]() -> int {
354 Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
355 auto dsa = std::make_unique<Botan::DSA_PublicKey>(group, safe_get(y));
356 *key = new botan_pubkey_struct(std::move(dsa));
357 return BOTAN_FFI_SUCCESS;
358 });
359#else
360 BOTAN_UNUSED(key, p, q, g, y);
362#endif
363}
364
368
370 return botan_pubkey_get_field(p, key, "p");
371}
372
374 return botan_pubkey_get_field(q, key, "q");
375}
376
378 return botan_pubkey_get_field(g, key, "g");
379}
380
382 return botan_pubkey_get_field(y, key, "y");
383}
384
385int botan_privkey_create_ecdsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
386 return botan_privkey_create(key_obj, "ECDSA", param_str, rng_obj);
387}
388
389/* ECDSA specific operations */
390
392#if defined(BOTAN_HAS_ECC_KEY)
393 return ffi_guard_thunk(__func__, [=]() -> int {
394 const Botan::Public_Key& pub_key = safe_get(key);
395 const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
396
397 if(ec_key == nullptr) {
399 }
400
401 return ec_key->domain().used_explicit_encoding() ? 1 : 0;
402 });
403#else
404 BOTAN_UNUSED(key);
406#endif
407}
408
410 const botan_mp_t public_x,
411 const botan_mp_t public_y,
412 const char* curve_name) {
413#if defined(BOTAN_HAS_ECDSA)
414 return ffi_guard_thunk(__func__, [=]() -> int {
415 std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
416
417 int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
418 if(rc == BOTAN_FFI_SUCCESS) {
419 *key = new botan_pubkey_struct(std::move(p_key));
420 }
421
422 return rc;
423 });
424#else
425 BOTAN_UNUSED(key, public_x, public_y, curve_name);
427#endif
428}
429
430int botan_privkey_load_ecdsa(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
431#if defined(BOTAN_HAS_ECDSA)
432 return ffi_guard_thunk(__func__, [=]() -> int {
433 std::unique_ptr<Botan::ECDSA_PrivateKey> p_key;
434 int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
435 if(rc == BOTAN_FFI_SUCCESS) {
436 *key = new botan_privkey_struct(std::move(p_key));
437 }
438 return rc;
439 });
440#else
441 BOTAN_UNUSED(key, scalar, curve_name);
443#endif
444}
445
446/* ElGamal specific operations */
447int botan_privkey_create_elgamal(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
448#if defined(BOTAN_HAS_ELGAMAL)
449
450 if((rng_obj == nullptr) || (key == nullptr)) {
452 }
453
454 if((pbits < 1024) || (qbits < 160)) {
456 }
457
458 Botan::DL_Group::PrimeType prime_type =
460
461 return ffi_guard_thunk(__func__, [=]() -> int {
463 Botan::DL_Group group(rng, prime_type, pbits, qbits);
464 auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(rng, group);
465 *key = new botan_privkey_struct(std::move(elg));
466 return BOTAN_FFI_SUCCESS;
467 });
468#else
469 BOTAN_UNUSED(key, rng_obj, pbits, qbits);
471#endif
472}
473
475#if defined(BOTAN_HAS_ELGAMAL)
476 *key = nullptr;
477 return ffi_guard_thunk(__func__, [=]() -> int {
478 Botan::DL_Group group(safe_get(p), safe_get(g));
479 auto elg = std::make_unique<Botan::ElGamal_PublicKey>(group, safe_get(y));
480 *key = new botan_pubkey_struct(std::move(elg));
481 return BOTAN_FFI_SUCCESS;
482 });
483#else
484 BOTAN_UNUSED(key, p, g, y);
486#endif
487}
488
490#if defined(BOTAN_HAS_ELGAMAL)
491 *key = nullptr;
492 return ffi_guard_thunk(__func__, [=]() -> int {
493 Botan::DL_Group group(safe_get(p), safe_get(g));
494 auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(group, safe_get(x));
495 *key = new botan_privkey_struct(std::move(elg));
496 return BOTAN_FFI_SUCCESS;
497 });
498#else
499 BOTAN_UNUSED(key, p, g, x);
501#endif
502}
503
504/* Diffie Hellman specific operations */
505
506int botan_privkey_create_dh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
507 return botan_privkey_create(key_obj, "DH", param_str, rng_obj);
508}
509
511#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
512 *key = nullptr;
513 return ffi_guard_thunk(__func__, [=]() -> int {
514 Botan::DL_Group group(safe_get(p), safe_get(g));
515 auto dh = std::make_unique<Botan::DH_PrivateKey>(group, safe_get(x));
516 *key = new botan_privkey_struct(std::move(dh));
517 return BOTAN_FFI_SUCCESS;
518 });
519#else
520 BOTAN_UNUSED(key, p, g, x);
522#endif
523}
524
526#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
527 *key = nullptr;
528 return ffi_guard_thunk(__func__, [=]() -> int {
529 Botan::DL_Group group(safe_get(p), safe_get(g));
530 auto dh = std::make_unique<Botan::DH_PublicKey>(group, safe_get(y));
531 *key = new botan_pubkey_struct(std::move(dh));
532 return BOTAN_FFI_SUCCESS;
533 });
534#else
535 BOTAN_UNUSED(key, p, g, y);
537#endif
538}
539
540/* ECDH + x25519/x448 specific operations */
541
542int botan_privkey_create_ecdh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
543 if(param_str == nullptr) {
545 }
546
547 const std::string params(param_str);
548
549 if(params == "x25519" || params == "curve25519") {
550 return botan_privkey_create(key_obj, "X25519", "", rng_obj);
551 }
552
553 if(params == "x448") {
554 return botan_privkey_create(key_obj, "X448", "", rng_obj);
555 }
556
557 return botan_privkey_create(key_obj, "ECDH", param_str, rng_obj);
558}
559
561 const botan_mp_t public_x,
562 const botan_mp_t public_y,
563 const char* curve_name) {
564#if defined(BOTAN_HAS_ECDH)
565 return ffi_guard_thunk(__func__, [=]() -> int {
566 std::unique_ptr<Botan::ECDH_PublicKey> p_key;
567 int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
568
569 if(rc == BOTAN_FFI_SUCCESS) {
570 *key = new botan_pubkey_struct(std::move(p_key));
571 }
572 return rc;
573 });
574#else
575 BOTAN_UNUSED(key, public_x, public_y, curve_name);
577#endif
578}
579
580int botan_privkey_load_ecdh(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
581#if defined(BOTAN_HAS_ECDH)
582 return ffi_guard_thunk(__func__, [=]() -> int {
583 std::unique_ptr<Botan::ECDH_PrivateKey> p_key;
584 int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
585 if(rc == BOTAN_FFI_SUCCESS) {
586 *key = new botan_privkey_struct(std::move(p_key));
587 }
588 return rc;
589 });
590#else
591 BOTAN_UNUSED(key, scalar, curve_name);
593#endif
594}
595
596/* SM2 specific operations */
597
599 uint8_t out[], size_t* out_len, const char* ident, const char* hash_algo, const botan_pubkey_t key) {
600 if(out == nullptr || out_len == nullptr) {
602 }
603 if(ident == nullptr || hash_algo == nullptr || key == nullptr) {
605 }
606
607#if defined(BOTAN_HAS_SM2)
608 return ffi_guard_thunk(__func__, [=]() -> int {
609 const Botan::Public_Key& pub_key = safe_get(key);
610 const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
611
612 if(ec_key == nullptr) {
614 }
615
616 if(ec_key->algo_name() != "SM2") {
618 }
619
620 const std::string ident_str(ident);
621 std::unique_ptr<Botan::HashFunction> hash = Botan::HashFunction::create_or_throw(hash_algo);
622
623 const std::vector<uint8_t> za =
624 Botan::sm2_compute_za(*hash, ident_str, ec_key->domain(), ec_key->_public_ec_point());
625
626 return write_vec_output(out, out_len, za);
627 });
628#else
630#endif
631}
632
634 const botan_mp_t public_x,
635 const botan_mp_t public_y,
636 const char* curve_name) {
637#if defined(BOTAN_HAS_SM2)
638 return ffi_guard_thunk(__func__, [=]() -> int {
639 std::unique_ptr<Botan::SM2_PublicKey> p_key;
640 if(!pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name)) {
641 *key = new botan_pubkey_struct(std::move(p_key));
642 return BOTAN_FFI_SUCCESS;
643 }
645 });
646#else
647 BOTAN_UNUSED(key, public_x, public_y, curve_name);
649#endif
650}
651
652int botan_privkey_load_sm2(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
653#if defined(BOTAN_HAS_SM2)
654 return ffi_guard_thunk(__func__, [=]() -> int {
655 std::unique_ptr<Botan::SM2_PrivateKey> p_key;
656 int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
657
658 if(rc == BOTAN_FFI_SUCCESS) {
659 *key = new botan_privkey_struct(std::move(p_key));
660 }
661 return rc;
662 });
663#else
664 BOTAN_UNUSED(key, scalar, curve_name);
666#endif
667}
668
670 const botan_mp_t public_x,
671 const botan_mp_t public_y,
672 const char* curve_name) {
673 return botan_pubkey_load_sm2(key, public_x, public_y, curve_name);
674}
675
676int botan_privkey_load_sm2_enc(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
677 return botan_privkey_load_sm2(key, scalar, curve_name);
678}
679
680/* Ed25519 specific operations */
681
682int botan_privkey_load_ed25519(botan_privkey_t* key, const uint8_t privkey[32]) {
683#if defined(BOTAN_HAS_ED25519)
684 *key = nullptr;
685 return ffi_guard_thunk(__func__, [=]() -> int {
686 const Botan::secure_vector<uint8_t> privkey_vec(privkey, privkey + 32);
687 auto ed25519 = std::make_unique<Botan::Ed25519_PrivateKey>(privkey_vec);
688 *key = new botan_privkey_struct(std::move(ed25519));
689 return BOTAN_FFI_SUCCESS;
690 });
691#else
692 BOTAN_UNUSED(key, privkey);
694#endif
695}
696
697int botan_pubkey_load_ed25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
698#if defined(BOTAN_HAS_ED25519)
699 *key = nullptr;
700 return ffi_guard_thunk(__func__, [=]() -> int {
701 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32);
702 auto ed25519 = std::make_unique<Botan::Ed25519_PublicKey>(pubkey_vec);
703 *key = new botan_pubkey_struct(std::move(ed25519));
704 return BOTAN_FFI_SUCCESS;
705 });
706#else
707 BOTAN_UNUSED(key, pubkey);
709#endif
710}
711
713#if defined(BOTAN_HAS_ED25519)
714 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
715 if(auto ed = dynamic_cast<const Botan::Ed25519_PrivateKey*>(&k)) {
716 const auto ed_key = ed->raw_private_key_bits();
717 if(ed_key.size() != 64) {
719 }
720 Botan::copy_mem(output, ed_key.data(), ed_key.size());
721 return BOTAN_FFI_SUCCESS;
722 } else {
724 }
725 });
726#else
727 BOTAN_UNUSED(key, output);
729#endif
730}
731
733#if defined(BOTAN_HAS_ED25519)
734 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
735 if(auto ed = dynamic_cast<const Botan::Ed25519_PublicKey*>(&k)) {
736 const std::vector<uint8_t>& ed_key = ed->get_public_key();
737 if(ed_key.size() != 32) {
739 }
740 Botan::copy_mem(output, ed_key.data(), ed_key.size());
741 return BOTAN_FFI_SUCCESS;
742 } else {
744 }
745 });
746#else
747 BOTAN_UNUSED(key, output);
749#endif
750}
751
752/* Ed448 specific operations */
753
754int botan_privkey_load_ed448(botan_privkey_t* key, const uint8_t privkey[57]) {
755#if defined(BOTAN_HAS_ED448)
756 *key = nullptr;
757 return ffi_guard_thunk(__func__, [=]() -> int {
758 auto ed448 = std::make_unique<Botan::Ed448_PrivateKey>(std::span(privkey, 57));
759 *key = new botan_privkey_struct(std::move(ed448));
760 return BOTAN_FFI_SUCCESS;
761 });
762#else
763 BOTAN_UNUSED(key, privkey);
765#endif
766}
767
768int botan_pubkey_load_ed448(botan_pubkey_t* key, const uint8_t pubkey[57]) {
769#if defined(BOTAN_HAS_ED448)
770 *key = nullptr;
771 return ffi_guard_thunk(__func__, [=]() -> int {
772 auto ed448 = std::make_unique<Botan::Ed448_PublicKey>(std::span(pubkey, 57));
773 *key = new botan_pubkey_struct(std::move(ed448));
774 return BOTAN_FFI_SUCCESS;
775 });
776#else
777 BOTAN_UNUSED(key, pubkey);
779#endif
780}
781
783#if defined(BOTAN_HAS_ED448)
784 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
785 if(auto ed = dynamic_cast<const Botan::Ed448_PrivateKey*>(&k)) {
786 const auto ed_key = ed->raw_private_key_bits();
787 Botan::copy_mem(std::span(output, 57), ed_key);
788 return BOTAN_FFI_SUCCESS;
789 } else {
791 }
792 });
793#else
794 BOTAN_UNUSED(key, output);
796#endif
797}
798
799int botan_pubkey_ed448_get_pubkey(botan_pubkey_t key, uint8_t output[57]) {
800#if defined(BOTAN_HAS_ED448)
801 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
802 if(auto ed = dynamic_cast<const Botan::Ed448_PublicKey*>(&k)) {
803 const auto ed_key = ed->public_key_bits();
804 Botan::copy_mem(std::span(output, 57), ed_key);
805 return BOTAN_FFI_SUCCESS;
806 } else {
808 }
809 });
810#else
811 BOTAN_UNUSED(key, output);
813#endif
814}
815
816/* X25519 specific operations */
817
818int botan_privkey_load_x25519(botan_privkey_t* key, const uint8_t privkey[32]) {
819#if defined(BOTAN_HAS_X25519)
820 *key = nullptr;
821 return ffi_guard_thunk(__func__, [=]() -> int {
822 const Botan::secure_vector<uint8_t> privkey_vec(privkey, privkey + 32);
823 auto x25519 = std::make_unique<Botan::X25519_PrivateKey>(privkey_vec);
824 *key = new botan_privkey_struct(std::move(x25519));
825 return BOTAN_FFI_SUCCESS;
826 });
827#else
828 BOTAN_UNUSED(key, privkey);
830#endif
831}
832
833int botan_pubkey_load_x25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
834#if defined(BOTAN_HAS_X25519)
835 *key = nullptr;
836 return ffi_guard_thunk(__func__, [=]() -> int {
837 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32);
838 auto x25519 = std::make_unique<Botan::X25519_PublicKey>(pubkey_vec);
839 *key = new botan_pubkey_struct(std::move(x25519));
840 return BOTAN_FFI_SUCCESS;
841 });
842#else
843 BOTAN_UNUSED(key, pubkey);
845#endif
846}
847
849#if defined(BOTAN_HAS_X25519)
850 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
851 if(auto x25519 = dynamic_cast<const Botan::X25519_PrivateKey*>(&k)) {
852 const auto x25519_key = x25519->raw_private_key_bits();
853 if(x25519_key.size() != 32) {
855 }
856 Botan::copy_mem(output, x25519_key.data(), x25519_key.size());
857 return BOTAN_FFI_SUCCESS;
858 } else {
860 }
861 });
862#else
863 BOTAN_UNUSED(key, output);
865#endif
866}
867
868int botan_pubkey_x25519_get_pubkey(botan_pubkey_t key, uint8_t output[32]) {
869#if defined(BOTAN_HAS_X25519)
870 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
871 if(auto x25519 = dynamic_cast<const Botan::X25519_PublicKey*>(&k)) {
872 const std::vector<uint8_t>& x25519_key = x25519->public_value();
873 if(x25519_key.size() != 32) {
875 }
876 Botan::copy_mem(output, x25519_key.data(), x25519_key.size());
877 return BOTAN_FFI_SUCCESS;
878 } else {
880 }
881 });
882#else
883 BOTAN_UNUSED(key, output);
885#endif
886}
887
888/* X448 specific operations */
889
890int botan_privkey_load_x448(botan_privkey_t* key, const uint8_t privkey[56]) {
891#if defined(BOTAN_HAS_X448)
892 *key = nullptr;
893 return ffi_guard_thunk(__func__, [=]() -> int {
894 auto x448 = std::make_unique<Botan::X448_PrivateKey>(std::span(privkey, 56));
895 *key = new botan_privkey_struct(std::move(x448));
896 return BOTAN_FFI_SUCCESS;
897 });
898#else
899 BOTAN_UNUSED(key, privkey);
901#endif
902}
903
904int botan_pubkey_load_x448(botan_pubkey_t* key, const uint8_t pubkey[56]) {
905#if defined(BOTAN_HAS_X448)
906 *key = nullptr;
907 return ffi_guard_thunk(__func__, [=]() -> int {
908 auto x448 = std::make_unique<Botan::X448_PublicKey>(std::span(pubkey, 56));
909 *key = new botan_pubkey_struct(std::move(x448));
910 return BOTAN_FFI_SUCCESS;
911 });
912#else
913 BOTAN_UNUSED(key, pubkey);
915#endif
916}
917
919#if defined(BOTAN_HAS_X448)
920 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
921 if(auto x448 = dynamic_cast<const Botan::X448_PrivateKey*>(&k)) {
922 const auto x448_key = x448->raw_private_key_bits();
923 Botan::copy_mem(std::span(output, 56), x448_key);
924 return BOTAN_FFI_SUCCESS;
925 } else {
927 }
928 });
929#else
930 BOTAN_UNUSED(key, output);
932#endif
933}
934
935int botan_pubkey_x448_get_pubkey(botan_pubkey_t key, uint8_t output[56]) {
936#if defined(BOTAN_HAS_X448)
937 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
938 if(auto x448 = dynamic_cast<const Botan::X448_PublicKey*>(&k)) {
939 const std::vector<uint8_t>& x448_key = x448->public_value();
940 Botan::copy_mem(std::span(output, 56), x448_key);
941 return BOTAN_FFI_SUCCESS;
942 } else {
944 }
945 });
946#else
947 BOTAN_UNUSED(key, output);
949#endif
950}
951
952/*
953* Algorithm specific key operations: Kyber
954*/
955
956int botan_privkey_load_kyber(botan_privkey_t* key, const uint8_t privkey[], size_t key_len) {
957#if defined(BOTAN_HAS_KYBER)
958 *key = nullptr;
959 switch(key_len) {
960 case 1632:
961 return ffi_guard_thunk(__func__, [=]() -> int {
962 const Botan::secure_vector<uint8_t> privkey_vec(privkey, privkey + 1632);
963 auto kyber512 = std::make_unique<Botan::Kyber_PrivateKey>(privkey_vec, Botan::KyberMode::Kyber512_R3);
964 *key = new botan_privkey_struct(std::move(kyber512));
965 return BOTAN_FFI_SUCCESS;
966 });
967 case 2400:
968 return ffi_guard_thunk(__func__, [=]() -> int {
969 const Botan::secure_vector<uint8_t> privkey_vec(privkey, privkey + 2400);
970 auto kyber768 = std::make_unique<Botan::Kyber_PrivateKey>(privkey_vec, Botan::KyberMode::Kyber768_R3);
971 *key = new botan_privkey_struct(std::move(kyber768));
972 return BOTAN_FFI_SUCCESS;
973 });
974 case 3168:
975 return ffi_guard_thunk(__func__, [=]() -> int {
976 const Botan::secure_vector<uint8_t> privkey_vec(privkey, privkey + 3168);
977 auto kyber1024 = std::make_unique<Botan::Kyber_PrivateKey>(privkey_vec, Botan::KyberMode::Kyber1024_R3);
978 *key = new botan_privkey_struct(std::move(kyber1024));
979 return BOTAN_FFI_SUCCESS;
980 });
981 default:
982 BOTAN_UNUSED(key, privkey, key_len);
984 }
985#else
986 BOTAN_UNUSED(key, key_len, privkey);
988#endif
989}
990
991int botan_pubkey_load_kyber(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len) {
992#if defined(BOTAN_HAS_KYBER)
993 *key = nullptr;
994 switch(key_len) {
995 case 800:
996 return ffi_guard_thunk(__func__, [=]() -> int {
997 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 800);
998 auto kyber512 = std::make_unique<Botan::Kyber_PublicKey>(pubkey_vec, Botan::KyberMode::Kyber512_R3);
999 *key = new botan_pubkey_struct(std::move(kyber512));
1000 return BOTAN_FFI_SUCCESS;
1001 });
1002 case 1184:
1003 return ffi_guard_thunk(__func__, [=]() -> int {
1004 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 1184);
1005 auto kyber768 = std::make_unique<Botan::Kyber_PublicKey>(pubkey_vec, Botan::KyberMode::Kyber768_R3);
1006 *key = new botan_pubkey_struct(std::move(kyber768));
1007 return BOTAN_FFI_SUCCESS;
1008 });
1009 case 1568:
1010 return ffi_guard_thunk(__func__, [=]() -> int {
1011 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 1568);
1012 auto kyber1024 = std::make_unique<Botan::Kyber_PublicKey>(pubkey_vec, Botan::KyberMode::Kyber1024_R3);
1013 *key = new botan_pubkey_struct(std::move(kyber1024));
1014 return BOTAN_FFI_SUCCESS;
1015 });
1016 default:
1017 BOTAN_UNUSED(key, pubkey, key_len);
1019 }
1020#else
1021 BOTAN_UNUSED(key, pubkey, key_len);
1023#endif
1024}
1025
1027#if defined(BOTAN_HAS_KYBER)
1028 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1029 if(auto kyber = dynamic_cast<const Botan::Kyber_PrivateKey*>(&k)) {
1030 return invoke_view_callback(view, ctx, kyber->raw_private_key_bits());
1031 } else {
1033 }
1034 });
1035#else
1036 BOTAN_UNUSED(key, ctx, view);
1038#endif
1039}
1040
1042#if defined(BOTAN_HAS_KYBER)
1043 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1044 if(auto kyber = dynamic_cast<const Botan::Kyber_PublicKey*>(&k)) {
1045 return invoke_view_callback(view, ctx, kyber->public_key_bits());
1046 } else {
1048 }
1049 });
1050#else
1051 BOTAN_UNUSED(key, ctx, view);
1053#endif
1054}
1055
1056/*
1057* Algorithm specific key operations: ML-KEM
1058*/
1059
1060int botan_privkey_load_ml_kem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mlkem_mode) {
1061#if defined(BOTAN_HAS_ML_KEM)
1062 if(key == nullptr || privkey == nullptr || mlkem_mode == nullptr) {
1064 }
1065
1066 *key = nullptr;
1067
1068 return ffi_guard_thunk(__func__, [=]() -> int {
1069 auto mode = Botan::ML_KEM_Mode(mlkem_mode);
1070 if(!mode.is_ml_kem()) {
1072 }
1073
1074 auto mlkem_key = std::make_unique<Botan::ML_KEM_PrivateKey>(std::span{privkey, key_len}, mode);
1075 *key = new botan_privkey_struct(std::move(mlkem_key));
1076 return BOTAN_FFI_SUCCESS;
1077 });
1078#else
1079 BOTAN_UNUSED(key, key_len, privkey, mlkem_mode);
1081#endif
1082}
1083
1084int botan_pubkey_load_ml_kem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mlkem_mode) {
1085#if defined(BOTAN_HAS_ML_KEM)
1086 if(key == nullptr || pubkey == nullptr || mlkem_mode == nullptr) {
1088 }
1089
1090 *key = nullptr;
1091
1092 return ffi_guard_thunk(__func__, [=]() -> int {
1093 auto mode = Botan::ML_KEM_Mode(mlkem_mode);
1094 if(!mode.is_ml_kem()) {
1096 }
1097
1098 auto mlkem_key = std::make_unique<Botan::ML_KEM_PublicKey>(std::span{pubkey, key_len}, mode.mode());
1099 *key = new botan_pubkey_struct(std::move(mlkem_key));
1100 return BOTAN_FFI_SUCCESS;
1101 });
1102#else
1103 BOTAN_UNUSED(key, key_len, pubkey, mlkem_mode);
1105#endif
1106}
1107
1108/*
1109* Algorithm specific key operations: ML-DSA
1110*/
1111
1112int botan_privkey_load_ml_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mldsa_mode) {
1113#if defined(BOTAN_HAS_ML_DSA)
1114 if(key == nullptr || privkey == nullptr || mldsa_mode == nullptr) {
1116 }
1117
1118 *key = nullptr;
1119
1120 return ffi_guard_thunk(__func__, [=]() -> int {
1121 auto mode = Botan::ML_DSA_Mode(mldsa_mode);
1122 if(!mode.is_ml_dsa()) {
1124 }
1125
1126 auto mldsa_key = std::make_unique<Botan::ML_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
1127 *key = new botan_privkey_struct(std::move(mldsa_key));
1128 return BOTAN_FFI_SUCCESS;
1129 });
1130#else
1131 BOTAN_UNUSED(key, key_len, privkey, mldsa_mode);
1133#endif
1134}
1135
1136int botan_pubkey_load_ml_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mldsa_mode) {
1137#if defined(BOTAN_HAS_ML_DSA)
1138 if(key == nullptr || pubkey == nullptr || mldsa_mode == nullptr) {
1140 }
1141
1142 *key = nullptr;
1143
1144 return ffi_guard_thunk(__func__, [=]() -> int {
1145 auto mode = Botan::ML_DSA_Mode(mldsa_mode);
1146 if(!mode.is_ml_dsa()) {
1148 }
1149
1150 auto mldsa_key = std::make_unique<Botan::ML_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
1151 *key = new botan_pubkey_struct(std::move(mldsa_key));
1152 return BOTAN_FFI_SUCCESS;
1153 });
1154#else
1155 BOTAN_UNUSED(key, key_len, pubkey, mldsa_mode);
1157#endif
1158}
1159
1160/*
1161* Algorithm specific key operations: SLH-DSA
1162*/
1163
1164int botan_privkey_load_slh_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* slhdsa_mode) {
1165#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1166 if(key == nullptr || privkey == nullptr || slhdsa_mode == nullptr) {
1168 }
1169
1170 *key = nullptr;
1171
1172 return ffi_guard_thunk(__func__, [=]() -> int {
1173 auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
1174 if(!mode.is_slh_dsa()) {
1176 }
1177
1178 auto slhdsa_key = std::make_unique<Botan::SLH_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
1179 *key = new botan_privkey_struct(std::move(slhdsa_key));
1180 return BOTAN_FFI_SUCCESS;
1181 });
1182#else
1183 BOTAN_UNUSED(key, key_len, privkey, slhdsa_mode);
1185#endif
1186}
1187
1188int botan_pubkey_load_slh_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* slhdsa_mode) {
1189#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1190 if(key == nullptr || pubkey == nullptr || slhdsa_mode == nullptr) {
1192 }
1193
1194 *key = nullptr;
1195
1196 return ffi_guard_thunk(__func__, [=]() -> int {
1197 auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
1198 if(!mode.is_slh_dsa()) {
1200 }
1201
1202 auto mldsa_key = std::make_unique<Botan::SLH_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
1203 *key = new botan_pubkey_struct(std::move(mldsa_key));
1204 return BOTAN_FFI_SUCCESS;
1205 });
1206#else
1207 BOTAN_UNUSED(key, key_len, pubkey, slhdsa_mode);
1209#endif
1210}
1211
1212/*
1213* Algorithm specific key operations: FrodoKEM
1214*/
1215
1216int botan_privkey_load_frodokem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* frodo_mode) {
1217#if defined(BOTAN_HAS_FRODOKEM)
1218 if(key == nullptr || privkey == nullptr || frodo_mode == nullptr) {
1220 }
1221
1222 *key = nullptr;
1223
1224 return ffi_guard_thunk(__func__, [=]() -> int {
1225 const auto mode = Botan::FrodoKEMMode(frodo_mode);
1226 auto frodo_key = std::make_unique<Botan::FrodoKEM_PrivateKey>(std::span{privkey, key_len}, mode);
1227 *key = new botan_privkey_struct(std::move(frodo_key));
1228 return BOTAN_FFI_SUCCESS;
1229 });
1230#else
1231 BOTAN_UNUSED(key, privkey, key_len, frodo_mode);
1233#endif
1234}
1235
1236int botan_pubkey_load_frodokem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* frodo_mode) {
1237#if defined(BOTAN_HAS_FRODOKEM)
1238 if(key == nullptr || pubkey == nullptr || frodo_mode == nullptr) {
1240 }
1241
1242 *key = nullptr;
1243
1244 return ffi_guard_thunk(__func__, [=]() -> int {
1245 const auto mode = Botan::FrodoKEMMode(frodo_mode);
1246 auto frodo_key = std::make_unique<Botan::FrodoKEM_PublicKey>(std::span{pubkey, key_len}, mode);
1247 *key = new botan_pubkey_struct(std::move(frodo_key));
1248 return BOTAN_FFI_SUCCESS;
1249 });
1250#else
1251 BOTAN_UNUSED(key, pubkey, key_len, frodo_mode);
1253#endif
1254}
1255
1256/*
1257* Algorithm specific key operations : Classic McEliece
1258*/
1259
1261 const uint8_t privkey[],
1262 size_t key_len,
1263 const char* cmce_mode) {
1264#if defined(BOTAN_HAS_CLASSICMCELIECE)
1265 if(key == nullptr || privkey == nullptr || cmce_mode == nullptr) {
1267 }
1268
1269 *key = nullptr;
1270
1271 return ffi_guard_thunk(__func__, [=]() -> int {
1272 const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
1273 auto cmce_key = std::make_unique<Botan::Classic_McEliece_PrivateKey>(std::span{privkey, key_len}, mode);
1274 *key = new botan_privkey_struct(std::move(cmce_key));
1275 return BOTAN_FFI_SUCCESS;
1276 });
1277#else
1278 BOTAN_UNUSED(key, privkey, key_len, cmce_mode);
1280#endif
1281}
1282
1284 const uint8_t pubkey[],
1285 size_t key_len,
1286 const char* cmce_mode) {
1287#if defined(BOTAN_HAS_CLASSICMCELIECE)
1288 if(key == nullptr || pubkey == nullptr || cmce_mode == nullptr) {
1290 }
1291
1292 *key = nullptr;
1293
1294 return ffi_guard_thunk(__func__, [=]() -> int {
1295 const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
1296 auto cmce_key = std::make_unique<Botan::Classic_McEliece_PublicKey>(std::span{pubkey, key_len}, mode);
1297 *key = new botan_pubkey_struct(std::move(cmce_key));
1298 return BOTAN_FFI_SUCCESS;
1299 });
1300#else
1301 BOTAN_UNUSED(key, pubkey, key_len, cmce_mode);
1303#endif
1304}
1305
1307#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
1308 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1309 if(auto ecc = dynamic_cast<const Botan::EC_PublicKey*>(&k)) {
1310 auto pt = ecc->_public_ec_point().serialize_uncompressed();
1311 return invoke_view_callback(view, ctx, pt);
1312 } else {
1314 }
1315 });
1316#else
1317 BOTAN_UNUSED(key, view, ctx);
1319#endif
1320}
1321
1322int botan_privkey_create_mceliece(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n, size_t t) {
1323 const std::string mce_params = std::to_string(n) + "," + std::to_string(t);
1324 return botan_privkey_create(key_obj, "McEliece", mce_params.c_str(), rng_obj);
1325}
1326
1328 const char* aead,
1329 const uint8_t ct[],
1330 size_t ct_len,
1331 const uint8_t ad[],
1332 size_t ad_len,
1333 uint8_t out[],
1334 size_t* out_len) {
1335 BOTAN_UNUSED(mce_key_obj, aead, ct, ct_len, ad, ad_len, out, out_len);
1337}
1338
1340 botan_rng_t rng_obj,
1341 const char* aead,
1342 const uint8_t pt[],
1343 size_t pt_len,
1344 const uint8_t ad[],
1345 size_t ad_len,
1346 uint8_t out[],
1347 size_t* out_len) {
1348 BOTAN_UNUSED(mce_key_obj, rng_obj, aead, pt, pt_len, ad, ad_len, out, out_len);
1350}
1351}
#define BOTAN_UNUSED
Definition assert.h:118
virtual std::string algo_name() const =0
virtual const BigInt & get_int_field(std::string_view field) const
Definition pk_keys.cpp:18
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:95
static Classic_McEliece_Parameter_Set from_string(std::string_view param_name)
Get the parameter set for a given parameter set name.
static std::optional< EC_AffinePoint > from_bigint_xy(const EC_Group &group, const BigInt &x, const BigInt &y)
Definition ec_apoint.cpp:92
static EC_Group from_name(std::string_view name)
Definition ec_group.cpp:354
bool used_explicit_encoding() const
Definition ec_group.h:263
const EC_Group & domain() const
Definition ecc_key.cpp:63
const EC_AffinePoint & _public_ec_point() const
Definition ecc_key.cpp:75
A private key for Ed448/Ed448ph according to RFC 8032.
Definition ed448.h:83
A public key for Ed448/Ed448ph according to RFC 8032.
Definition ed448.h:27
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:298
A private key for the X448 key agreement scheme according to RFC 7748.
Definition x448.h:67
A public key for the X448 key agreement scheme according to RFC 7748.
Definition x448.h:19
struct botan_pubkey_struct * botan_pubkey_t
Definition ffi.h:1319
struct botan_privkey_struct * botan_privkey_t
Definition ffi.h:1099
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
Definition ffi.h:155
int botan_privkey_create(botan_privkey_t *key, const char *algo_name, const char *algo_params, botan_rng_t rng)
Definition ffi_pkey.cpp:27
#define BOTAN_PRIVKEY_EXPORT_FLAG_PEM
Definition ffi.h:1183
struct botan_mp_struct * botan_mp_t
Definition ffi.h:902
void * botan_view_ctx
Definition ffi.h:146
struct botan_rng_struct * botan_rng_t
Definition ffi.h:272
#define BOTAN_PRIVKEY_EXPORT_FLAG_DER
Definition ffi.h:1182
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:135
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition ffi.h:143
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:128
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:129
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:120
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:130
int botan_privkey_create_elgamal(botan_privkey_t *key, botan_rng_t rng_obj, size_t pbits, size_t qbits)
int botan_pubkey_rsa_get_n(botan_mp_t n, botan_pubkey_t key)
int botan_privkey_load_x448(botan_privkey_t *key, const uint8_t privkey[56])
int botan_pubkey_get_field(botan_mp_t output, botan_pubkey_t key, const char *field_name_cstr)
int botan_pubkey_load_ml_dsa(botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len, const char *mldsa_mode)
int botan_privkey_load_ecdh(botan_privkey_t *key, const botan_mp_t scalar, const char *curve_name)
int botan_privkey_view_kyber_raw_key(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_pubkey_load_dh(botan_pubkey_t *key, botan_mp_t p, botan_mp_t g, botan_mp_t y)
int botan_pubkey_ed25519_get_pubkey(botan_pubkey_t key, uint8_t output[32])
int botan_privkey_rsa_get_privkey(botan_privkey_t rsa_key, uint8_t out[], size_t *out_len, uint32_t flags)
int botan_privkey_load_rsa_pkcs1(botan_privkey_t *key, const uint8_t bits[], size_t len)
int botan_pubkey_load_sm2(botan_pubkey_t *key, const botan_mp_t public_x, const botan_mp_t public_y, const char *curve_name)
int botan_pubkey_load_slh_dsa(botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len, const char *slhdsa_mode)
int botan_pubkey_sm2_compute_za(uint8_t out[], size_t *out_len, const char *ident, const char *hash_algo, const botan_pubkey_t key)
int botan_pubkey_load_classic_mceliece(botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len, const char *cmce_mode)
int botan_privkey_load_classic_mceliece(botan_privkey_t *key, const uint8_t privkey[], size_t key_len, const char *cmce_mode)
int botan_privkey_load_x25519(botan_privkey_t *key, const uint8_t privkey[32])
int botan_pubkey_dsa_get_p(botan_mp_t p, botan_pubkey_t key)
int botan_privkey_rsa_get_q(botan_mp_t q, botan_privkey_t key)
int botan_pubkey_ecc_key_used_explicit_encoding(botan_pubkey_t key)
int botan_pubkey_load_frodokem(botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len, const char *frodo_mode)
int botan_privkey_ed25519_get_privkey(botan_privkey_t key, uint8_t output[64])
int botan_privkey_load_sm2_enc(botan_privkey_t *key, const botan_mp_t scalar, const char *curve_name)
int botan_privkey_get_field(botan_mp_t output, botan_privkey_t key, const char *field_name_cstr)
int botan_pubkey_x448_get_pubkey(botan_pubkey_t key, uint8_t output[56])
int botan_privkey_load_rsa(botan_privkey_t *key, botan_mp_t rsa_p, botan_mp_t rsa_q, botan_mp_t rsa_e)
int botan_pubkey_dsa_get_y(botan_mp_t y, botan_pubkey_t key)
int botan_pubkey_load_ml_kem(botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len, const char *mlkem_mode)
int botan_privkey_load_ed448(botan_privkey_t *key, const uint8_t privkey[57])
int botan_pubkey_view_ec_public_point(const botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_privkey_load_dh(botan_privkey_t *key, botan_mp_t p, botan_mp_t g, botan_mp_t x)
int botan_privkey_load_sm2(botan_privkey_t *key, const botan_mp_t scalar, const char *curve_name)
int botan_pubkey_load_ed25519(botan_pubkey_t *key, const uint8_t pubkey[32])
int botan_privkey_load_ecdsa(botan_privkey_t *key, const botan_mp_t scalar, const char *curve_name)
int botan_mceies_encrypt(botan_pubkey_t mce_key_obj, botan_rng_t rng_obj, const char *aead, const uint8_t pt[], size_t pt_len, const uint8_t ad[], size_t ad_len, uint8_t out[], size_t *out_len)
int botan_pubkey_x25519_get_pubkey(botan_pubkey_t key, uint8_t output[32])
int botan_pubkey_load_elgamal(botan_pubkey_t *key, botan_mp_t p, botan_mp_t g, botan_mp_t y)
int botan_privkey_load_slh_dsa(botan_privkey_t *key, const uint8_t privkey[], size_t key_len, const char *slhdsa_mode)
int botan_privkey_create_dsa(botan_privkey_t *key, botan_rng_t rng_obj, size_t pbits, size_t qbits)
int botan_privkey_rsa_get_p(botan_mp_t p, botan_privkey_t key)
int botan_privkey_create_mceliece(botan_privkey_t *key_obj, botan_rng_t rng_obj, size_t n, size_t t)
int botan_pubkey_load_ed448(botan_pubkey_t *key, const uint8_t pubkey[57])
int botan_privkey_load_ml_kem(botan_privkey_t *key, const uint8_t privkey[], size_t key_len, const char *mlkem_mode)
int botan_privkey_create_ecdh(botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
int botan_privkey_rsa_get_d(botan_mp_t d, botan_privkey_t key)
int botan_pubkey_load_sm2_enc(botan_pubkey_t *key, const botan_mp_t public_x, const botan_mp_t public_y, const char *curve_name)
int botan_mceies_decrypt(botan_privkey_t mce_key_obj, const char *aead, const uint8_t ct[], size_t ct_len, const uint8_t ad[], size_t ad_len, uint8_t out[], size_t *out_len)
int botan_pubkey_load_x25519(botan_pubkey_t *key, const uint8_t pubkey[32])
int botan_privkey_load_elgamal(botan_privkey_t *key, botan_mp_t p, botan_mp_t g, botan_mp_t x)
int botan_privkey_create_rsa(botan_privkey_t *key_obj, botan_rng_t rng_obj, size_t n_bits)
int botan_pubkey_load_dsa(botan_pubkey_t *key, botan_mp_t p, botan_mp_t q, botan_mp_t g, botan_mp_t y)
int botan_pubkey_load_kyber(botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len)
int botan_pubkey_ed448_get_pubkey(botan_pubkey_t key, uint8_t output[57])
int botan_privkey_create_dh(botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
int botan_privkey_load_dsa(botan_privkey_t *key, botan_mp_t p, botan_mp_t q, botan_mp_t g, botan_mp_t x)
int botan_privkey_rsa_get_n(botan_mp_t n, botan_privkey_t key)
int botan_privkey_load_kyber(botan_privkey_t *key, const uint8_t privkey[], size_t key_len)
int botan_pubkey_rsa_get_e(botan_mp_t e, botan_pubkey_t key)
int botan_pubkey_dsa_get_g(botan_mp_t g, botan_pubkey_t key)
int botan_pubkey_dsa_get_q(botan_mp_t q, botan_pubkey_t key)
int botan_privkey_load_frodokem(botan_privkey_t *key, const uint8_t privkey[], size_t key_len, const char *frodo_mode)
int botan_pubkey_load_ecdsa(botan_pubkey_t *key, const botan_mp_t public_x, const botan_mp_t public_y, const char *curve_name)
int botan_pubkey_load_ecdh(botan_pubkey_t *key, const botan_mp_t public_x, const botan_mp_t public_y, const char *curve_name)
int botan_privkey_load_ml_dsa(botan_privkey_t *key, const uint8_t privkey[], size_t key_len, const char *mldsa_mode)
int botan_privkey_x448_get_privkey(botan_privkey_t key, uint8_t output[56])
int botan_privkey_load_ed25519(botan_privkey_t *key, const uint8_t privkey[32])
int botan_privkey_rsa_get_e(botan_mp_t e, botan_privkey_t key)
int botan_privkey_x25519_get_privkey(botan_privkey_t key, uint8_t output[32])
int botan_privkey_ed448_get_privkey(botan_privkey_t key, uint8_t output[57])
int botan_privkey_dsa_get_x(botan_mp_t x, botan_privkey_t key)
int botan_pubkey_load_rsa(botan_pubkey_t *key, botan_mp_t n, botan_mp_t e)
int botan_privkey_create_ecdsa(botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
int botan_pubkey_view_kyber_raw_key(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_pubkey_load_x448(botan_pubkey_t *key, const uint8_t pubkey[56])
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:124
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:39
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition ffi_util.h:205
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:63
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, const std::vector< uint8_t, Alloc > &buf)
Definition ffi_util.h:146
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:128
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Definition ffi_util.h:201
DilithiumMode ML_DSA_Mode
Definition ml_dsa.h:21
KyberMode ML_KEM_Mode
Definition ml_kem.h:21
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:147
std::vector< uint8_t > sm2_compute_za(HashFunction &hash, std::string_view user_id, const EC_Group &group, const EC_AffinePoint &pubkey)
Definition sm2.cpp:68