Botan 3.9.0
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 }
111 }
112
113 Botan::Null_RNG null_rng;
114 const auto grp = Botan::EC_Group::from_name(curve_name);
115 key.reset(new ECPrivateKey_t(null_rng, grp, scalar));
116 return BOTAN_FFI_SUCCESS;
117}
118
119template <class ECPublicKey_t>
120int pubkey_load_ec(std::unique_ptr<ECPublicKey_t>& key,
121 const Botan::BigInt& public_x,
122 const Botan::BigInt& public_y,
123 const char* curve_name) {
124 if(curve_name == nullptr) {
126 }
127
130 }
131
132 const auto group = Botan::EC_Group::from_name(curve_name);
133
134 if(auto pt = Botan::EC_AffinePoint::from_bigint_xy(group, public_x, public_y)) {
135 key.reset(new ECPublicKey_t(group, pt.value()));
136 return BOTAN_FFI_SUCCESS;
137 } else {
139 }
140}
141
142#endif
143
144Botan::BigInt pubkey_get_field(const Botan::Public_Key& key, std::string_view field) {
145#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
146 // Not currently handled by get_int_field
147 if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key)) {
148 if(field == "public_x") {
149 return Botan::BigInt::from_bytes(ecc->_public_ec_point().x_bytes());
150 } else if(field == "public_y") {
151 return Botan::BigInt::from_bytes(ecc->_public_ec_point().y_bytes());
152 }
153 }
154#endif
155
156 try {
157 return key.get_int_field(field);
159 throw Botan_FFI::FFI_Error("Unknown key field", BOTAN_FFI_ERROR_BAD_PARAMETER);
160 }
161}
162
163Botan::BigInt privkey_get_field(const Botan::Private_Key& key, std::string_view field) {
164#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
165 // Not currently handled by get_int_field
166 if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key)) {
167 if(field == "public_x") {
168 return Botan::BigInt::from_bytes(ecc->_public_ec_point().x_bytes());
169 } else if(field == "public_y") {
170 return Botan::BigInt::from_bytes(ecc->_public_ec_point().y_bytes());
171 }
172 }
173#endif
174
175 try {
176 return key.get_int_field(field);
178 throw Botan_FFI::FFI_Error("Unknown key field", BOTAN_FFI_ERROR_BAD_PARAMETER);
179 }
180}
181
182} // namespace
183
184extern "C" {
185
186using namespace Botan_FFI;
187
188int botan_pubkey_get_field(botan_mp_t output, botan_pubkey_t key, const char* field_name_cstr) {
189 if(field_name_cstr == nullptr) {
191 }
192
193 const std::string field_name(field_name_cstr);
194
195 return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = pubkey_get_field(k, field_name); });
196}
197
198int botan_privkey_get_field(botan_mp_t output, botan_privkey_t key, const char* field_name_cstr) {
199 if(field_name_cstr == nullptr) {
201 }
202
203 const std::string field_name(field_name_cstr);
204
205 return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = privkey_get_field(k, field_name); });
206}
207
208/* RSA specific operations */
209
210int botan_privkey_create_rsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n_bits) {
211 if(n_bits < 1024 || n_bits > 16 * 1024) {
213 }
214
215 std::string n_str = std::to_string(n_bits);
216
217 return botan_privkey_create(key_obj, "RSA", n_str.c_str(), rng_obj);
218}
219
221#if defined(BOTAN_HAS_RSA)
222 if(key == nullptr) {
224 }
225 *key = nullptr;
226
227 return ffi_guard_thunk(__func__, [=]() -> int {
228 auto rsa = std::make_unique<Botan::RSA_PrivateKey>(safe_get(rsa_p), safe_get(rsa_q), safe_get(rsa_e));
229 return ffi_new_object(key, std::move(rsa));
230 });
231#else
232 BOTAN_UNUSED(key, rsa_p, rsa_q, rsa_e);
234#endif
235}
236
237int botan_privkey_load_rsa_pkcs1(botan_privkey_t* key, const uint8_t bits[], size_t len) {
238#if defined(BOTAN_HAS_RSA)
239 if(key == nullptr || bits == nullptr) {
241 }
242 *key = nullptr;
243
244 return ffi_guard_thunk(__func__, [=]() -> int {
246 auto rsa = std::make_unique<Botan::RSA_PrivateKey>(alg_id, std::span{bits, len});
247 return ffi_new_object(key, std::move(rsa));
248 });
249#else
250 BOTAN_UNUSED(key, bits, len);
252#endif
253}
254
256#if defined(BOTAN_HAS_RSA)
257 if(key == nullptr) {
259 }
260 *key = nullptr;
261 return ffi_guard_thunk(__func__, [=]() -> int {
262 auto rsa = std::make_unique<Botan::RSA_PublicKey>(safe_get(n), safe_get(e));
263 return ffi_new_object(key, std::move(rsa));
264 });
265#else
266 BOTAN_UNUSED(key, n, e);
268#endif
269}
270
274
278
282
286
290
292 return botan_pubkey_get_field(e, key, "e");
293}
294
296 return botan_pubkey_get_field(n, key, "n");
297}
298
299int botan_privkey_rsa_get_privkey(botan_privkey_t rsa_key, uint8_t out[], size_t* out_len, uint32_t flags) {
300#if defined(BOTAN_HAS_RSA)
301 return BOTAN_FFI_VISIT(rsa_key, [=](const auto& k) -> int {
302 if(const Botan::RSA_PrivateKey* rsa = dynamic_cast<const Botan::RSA_PrivateKey*>(&k)) {
303 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
304 return write_vec_output(out, out_len, rsa->private_key_bits());
305 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
306 // TODO define new generic functions for this
307 return write_str_output(reinterpret_cast<char*>(out),
308 out_len,
309 Botan::PEM_Code::encode(rsa->private_key_bits(), "RSA PRIVATE KEY"));
310 } else {
312 }
313 } else {
315 }
316 });
317#else
318 BOTAN_UNUSED(rsa_key, out, out_len, flags);
320#endif
321}
322
323/* DSA specific operations */
324int botan_privkey_create_dsa(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
325#if defined(BOTAN_HAS_DSA)
326
327 if((rng_obj == nullptr) || (key == nullptr)) {
329 }
330
331 if((pbits % 64 != 0) || (qbits % 8 != 0) || (pbits < 1024) || (pbits > 3072) || (qbits < 160) || (qbits > 256)) {
333 }
334
335 return ffi_guard_thunk(__func__, [=]() -> int {
337 Botan::DL_Group group(rng, Botan::DL_Group::Prime_Subgroup, pbits, qbits);
338 auto dsa = std::make_unique<Botan::DSA_PrivateKey>(rng, group);
339 return ffi_new_object(key, std::move(dsa));
340 });
341#else
342 BOTAN_UNUSED(key, rng_obj, pbits, qbits);
344#endif
345}
346
348#if defined(BOTAN_HAS_DSA)
349 if(key == nullptr) {
351 }
352 *key = nullptr;
353
354 return ffi_guard_thunk(__func__, [=]() -> int {
355 Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
356 auto dsa = std::make_unique<Botan::DSA_PrivateKey>(group, safe_get(x));
357 return ffi_new_object(key, std::move(dsa));
358 });
359#else
360 BOTAN_UNUSED(key, p, q, g, x);
362#endif
363}
364
366#if defined(BOTAN_HAS_DSA)
367 if(key == nullptr) {
369 }
370 *key = nullptr;
371
372 return ffi_guard_thunk(__func__, [=]() -> int {
373 Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
374 auto dsa = std::make_unique<Botan::DSA_PublicKey>(group, safe_get(y));
375 return ffi_new_object(key, std::move(dsa));
376 });
377#else
378 BOTAN_UNUSED(key, p, q, g, y);
380#endif
381}
382
386
388 return botan_pubkey_get_field(p, key, "p");
389}
390
392 return botan_pubkey_get_field(q, key, "q");
393}
394
396 return botan_pubkey_get_field(g, key, "g");
397}
398
400 return botan_pubkey_get_field(y, key, "y");
401}
402
403int botan_privkey_create_ecdsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
404 return botan_privkey_create(key_obj, "ECDSA", param_str, rng_obj);
405}
406
407/* ECDSA specific operations */
408
410#if defined(BOTAN_HAS_ECC_KEY)
411 return ffi_guard_thunk(__func__, [=]() -> int {
412 const Botan::Public_Key& pub_key = safe_get(key);
413 const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
414
415 if(ec_key == nullptr) {
417 }
418
419 return ec_key->domain().used_explicit_encoding() ? 1 : 0;
420 });
421#else
422 BOTAN_UNUSED(key);
424#endif
425}
426
427// NOLINTBEGIN(misc-misplaced-const)
428
430 const botan_mp_t public_x,
431 const botan_mp_t public_y,
432 const char* curve_name) {
433#if defined(BOTAN_HAS_ECDSA)
434 if(key == nullptr || curve_name == nullptr) {
436 }
437 *key = nullptr;
438
439 return ffi_guard_thunk(__func__, [=]() -> int {
440 std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
441
442 int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
443 if(rc == BOTAN_FFI_SUCCESS) {
444 ffi_new_object(key, std::move(p_key));
445 }
446
447 return rc;
448 });
449#else
450 BOTAN_UNUSED(key, public_x, public_y, curve_name);
452#endif
453}
454
455int botan_privkey_load_ecdsa(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
456#if defined(BOTAN_HAS_ECDSA)
457 if(key == nullptr || curve_name == nullptr) {
459 }
460 *key = nullptr;
461
462 return ffi_guard_thunk(__func__, [=]() -> int {
463 std::unique_ptr<Botan::ECDSA_PrivateKey> p_key;
464 int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
465 if(rc == BOTAN_FFI_SUCCESS) {
466 ffi_new_object(key, std::move(p_key));
467 }
468 return rc;
469 });
470#else
471 BOTAN_UNUSED(key, scalar, curve_name);
473#endif
474}
475
476/* ElGamal specific operations */
477int botan_privkey_create_elgamal(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
478#if defined(BOTAN_HAS_ELGAMAL)
479 if(key == nullptr || rng_obj == nullptr) {
481 }
482 *key = nullptr;
483
484 if(pbits < 1024 || qbits < 160) {
486 }
487
488 Botan::DL_Group::PrimeType prime_type =
490
491 return ffi_guard_thunk(__func__, [=]() -> int {
493 Botan::DL_Group group(rng, prime_type, pbits, qbits);
494 auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(rng, group);
495 return ffi_new_object(key, std::move(elg));
496 });
497#else
498 BOTAN_UNUSED(key, rng_obj, pbits, qbits);
500#endif
501}
502
504#if defined(BOTAN_HAS_ELGAMAL)
505 if(key == nullptr) {
507 }
508 *key = nullptr;
509 return ffi_guard_thunk(__func__, [=]() -> int {
510 Botan::DL_Group group(safe_get(p), safe_get(g));
511 auto elg = std::make_unique<Botan::ElGamal_PublicKey>(group, safe_get(y));
512 return ffi_new_object(key, std::move(elg));
513 });
514#else
515 BOTAN_UNUSED(key, p, g, y);
517#endif
518}
519
521#if defined(BOTAN_HAS_ELGAMAL)
522 if(key == nullptr) {
524 }
525 *key = nullptr;
526 return ffi_guard_thunk(__func__, [=]() -> int {
527 Botan::DL_Group group(safe_get(p), safe_get(g));
528 auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(group, safe_get(x));
529 return ffi_new_object(key, std::move(elg));
530 });
531#else
532 BOTAN_UNUSED(key, p, g, x);
534#endif
535}
536
537/* Diffie Hellman specific operations */
538
539int botan_privkey_create_dh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
540 return botan_privkey_create(key_obj, "DH", param_str, rng_obj);
541}
542
544#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
545 if(key == nullptr) {
547 }
548 *key = nullptr;
549 return ffi_guard_thunk(__func__, [=]() -> int {
550 Botan::DL_Group group(safe_get(p), safe_get(g));
551 auto dh = std::make_unique<Botan::DH_PrivateKey>(group, safe_get(x));
552 return ffi_new_object(key, std::move(dh));
553 });
554#else
555 BOTAN_UNUSED(key, p, g, x);
557#endif
558}
559
561#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
562 if(key == nullptr) {
564 }
565 *key = nullptr;
566 return ffi_guard_thunk(__func__, [=]() -> int {
567 Botan::DL_Group group(safe_get(p), safe_get(g));
568 auto dh = std::make_unique<Botan::DH_PublicKey>(group, safe_get(y));
569 return ffi_new_object(key, std::move(dh));
570 });
571#else
572 BOTAN_UNUSED(key, p, g, y);
574#endif
575}
576
577/* ECDH + x25519/x448 specific operations */
578
579int botan_privkey_create_ecdh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
580 if(key_obj == nullptr || param_str == nullptr) {
582 }
583 *key_obj = nullptr;
584
585 const std::string params(param_str);
586
587 if(params == "X25519" || params == "x25519" || params == "curve25519") {
588 return botan_privkey_create(key_obj, "X25519", "", rng_obj);
589 }
590
591 if(params == "X448" || params == "x448") {
592 return botan_privkey_create(key_obj, "X448", "", rng_obj);
593 }
594
595 return botan_privkey_create(key_obj, "ECDH", param_str, rng_obj);
596}
597
599 const botan_mp_t public_x,
600 const botan_mp_t public_y,
601 const char* curve_name) {
602#if defined(BOTAN_HAS_ECDH)
603 if(key == nullptr || curve_name == nullptr) {
605 }
606 *key = nullptr;
607 return ffi_guard_thunk(__func__, [=]() -> int {
608 std::unique_ptr<Botan::ECDH_PublicKey> p_key;
609 int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
610
611 if(rc == BOTAN_FFI_SUCCESS) {
612 ffi_new_object(key, std::move(p_key));
613 }
614 return rc;
615 });
616#else
617 BOTAN_UNUSED(key, public_x, public_y, curve_name);
619#endif
620}
621
622int botan_privkey_load_ecdh(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
623#if defined(BOTAN_HAS_ECDH)
624 if(key == nullptr || curve_name == nullptr) {
626 }
627 *key = nullptr;
628 return ffi_guard_thunk(__func__, [=]() -> int {
629 std::unique_ptr<Botan::ECDH_PrivateKey> p_key;
630 int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
631 if(rc == BOTAN_FFI_SUCCESS) {
632 ffi_new_object(key, std::move(p_key));
633 }
634 return rc;
635 });
636#else
637 BOTAN_UNUSED(key, scalar, curve_name);
639#endif
640}
641
642/* SM2 specific operations */
643
645 uint8_t out[], size_t* out_len, const char* ident, const char* hash_algo, const botan_pubkey_t key) {
646 if(out == nullptr || out_len == nullptr || ident == nullptr || hash_algo == nullptr || key == nullptr) {
648 }
649
650#if defined(BOTAN_HAS_SM2)
651 return ffi_guard_thunk(__func__, [=]() -> int {
652 const Botan::Public_Key& pub_key = safe_get(key);
653 const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
654
655 if(ec_key == nullptr) {
657 }
658
659 if(ec_key->algo_name() != "SM2") {
661 }
662
663 const std::string ident_str(ident);
664 std::unique_ptr<Botan::HashFunction> hash = Botan::HashFunction::create_or_throw(hash_algo);
665
666 const auto& pt = ec_key->_public_ec_point();
667
668 const auto za = Botan::sm2_compute_za(*hash, ident_str, ec_key->domain(), pt);
669
670 return write_vec_output(out, out_len, za);
671 });
672#else
674#endif
675}
676
678 const botan_mp_t public_x,
679 const botan_mp_t public_y,
680 const char* curve_name) {
681#if defined(BOTAN_HAS_SM2)
682 if(key == nullptr || curve_name == nullptr) {
684 }
685 *key = nullptr;
686
687 return ffi_guard_thunk(__func__, [=]() -> int {
688 std::unique_ptr<Botan::SM2_PublicKey> p_key;
689 if(pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name) == 0) {
690 return ffi_new_object(key, std::move(p_key));
691 } else {
693 }
694 });
695#else
696 BOTAN_UNUSED(key, public_x, public_y, curve_name);
698#endif
699}
700
701int botan_privkey_load_sm2(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
702#if defined(BOTAN_HAS_SM2)
703 if(key == nullptr || curve_name == nullptr) {
705 }
706 *key = nullptr;
707
708 return ffi_guard_thunk(__func__, [=]() -> int {
709 std::unique_ptr<Botan::SM2_PrivateKey> p_key;
710 int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
711
712 if(rc == BOTAN_FFI_SUCCESS) {
713 ffi_new_object(key, std::move(p_key));
714 }
715 return rc;
716 });
717#else
718 BOTAN_UNUSED(key, scalar, curve_name);
720#endif
721}
722
724 const botan_mp_t public_x,
725 const botan_mp_t public_y,
726 const char* curve_name) {
727 return botan_pubkey_load_sm2(key, public_x, public_y, curve_name);
728}
729
730int botan_privkey_load_sm2_enc(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
731 return botan_privkey_load_sm2(key, scalar, curve_name);
732}
733
734/* Ed25519 specific operations */
735
736int botan_privkey_load_ed25519(botan_privkey_t* key, const uint8_t privkey[32]) {
737#if defined(BOTAN_HAS_ED25519)
738 if(key == nullptr) {
740 }
741 *key = nullptr;
742 return ffi_guard_thunk(__func__, [=]() -> int {
743 auto ed25519 =
744 std::make_unique<Botan::Ed25519_PrivateKey>(Botan::Ed25519_PrivateKey::from_seed(std::span{privkey, 32}));
745 return ffi_new_object(key, std::move(ed25519));
746 });
747#else
748 BOTAN_UNUSED(key, privkey);
750#endif
751}
752
753int botan_pubkey_load_ed25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
754#if defined(BOTAN_HAS_ED25519)
755 if(key == nullptr) {
757 }
758 *key = nullptr;
759 return ffi_guard_thunk(__func__, [=]() -> int {
760 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32);
761 auto ed25519 = std::make_unique<Botan::Ed25519_PublicKey>(pubkey_vec);
762 return ffi_new_object(key, std::move(ed25519));
763 });
764#else
765 BOTAN_UNUSED(key, pubkey);
767#endif
768}
769
771#if defined(BOTAN_HAS_ED25519)
772 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
773 if(auto ed = dynamic_cast<const Botan::Ed25519_PrivateKey*>(&k)) {
774 const auto ed_key = ed->raw_private_key_bits();
775 if(ed_key.size() != 64) {
777 }
778 Botan::copy_mem(output, ed_key.data(), ed_key.size());
779 return BOTAN_FFI_SUCCESS;
780 } else {
782 }
783 });
784#else
785 BOTAN_UNUSED(key, output);
787#endif
788}
789
791#if defined(BOTAN_HAS_ED25519)
792 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
793 if(auto ed = dynamic_cast<const Botan::Ed25519_PublicKey*>(&k)) {
794 const std::vector<uint8_t>& ed_key = ed->get_public_key();
795 if(ed_key.size() != 32) {
797 }
798 Botan::copy_mem(output, ed_key.data(), ed_key.size());
799 return BOTAN_FFI_SUCCESS;
800 } else {
802 }
803 });
804#else
805 BOTAN_UNUSED(key, output);
807#endif
808}
809
810/* Ed448 specific operations */
811
812int botan_privkey_load_ed448(botan_privkey_t* key, const uint8_t privkey[57]) {
813#if defined(BOTAN_HAS_ED448)
814 if(key == nullptr) {
816 }
817 *key = nullptr;
818 return ffi_guard_thunk(__func__, [=]() -> int {
819 auto ed448 = std::make_unique<Botan::Ed448_PrivateKey>(std::span(privkey, 57));
820 return ffi_new_object(key, std::move(ed448));
821 });
822#else
823 BOTAN_UNUSED(key, privkey);
825#endif
826}
827
828int botan_pubkey_load_ed448(botan_pubkey_t* key, const uint8_t pubkey[57]) {
829#if defined(BOTAN_HAS_ED448)
830 if(key == nullptr) {
832 }
833 *key = nullptr;
834 return ffi_guard_thunk(__func__, [=]() -> int {
835 auto ed448 = std::make_unique<Botan::Ed448_PublicKey>(std::span(pubkey, 57));
836 return ffi_new_object(key, std::move(ed448));
837 });
838#else
839 BOTAN_UNUSED(key, pubkey);
841#endif
842}
843
845#if defined(BOTAN_HAS_ED448)
846 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
847 if(auto ed = dynamic_cast<const Botan::Ed448_PrivateKey*>(&k)) {
848 const auto ed_key = ed->raw_private_key_bits();
849 Botan::copy_mem(std::span(output, 57), ed_key);
850 return BOTAN_FFI_SUCCESS;
851 } else {
853 }
854 });
855#else
856 BOTAN_UNUSED(key, output);
858#endif
859}
860
861int botan_pubkey_ed448_get_pubkey(botan_pubkey_t key, uint8_t output[57]) {
862#if defined(BOTAN_HAS_ED448)
863 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
864 if(auto ed = dynamic_cast<const Botan::Ed448_PublicKey*>(&k)) {
865 const auto ed_key = ed->public_key_bits();
866 Botan::copy_mem(std::span(output, 57), ed_key);
867 return BOTAN_FFI_SUCCESS;
868 } else {
870 }
871 });
872#else
873 BOTAN_UNUSED(key, output);
875#endif
876}
877
878/* X25519 specific operations */
879
880int botan_privkey_load_x25519(botan_privkey_t* key, const uint8_t privkey[32]) {
881#if defined(BOTAN_HAS_X25519)
882 if(key == nullptr) {
884 }
885 *key = nullptr;
886 return ffi_guard_thunk(__func__, [=]() -> int {
887 auto x25519 = std::make_unique<Botan::X25519_PrivateKey>(std::span{privkey, 32});
888 return ffi_new_object(key, std::move(x25519));
889 });
890#else
891 BOTAN_UNUSED(key, privkey);
893#endif
894}
895
896int botan_pubkey_load_x25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
897#if defined(BOTAN_HAS_X25519)
898 if(key == nullptr) {
900 }
901 *key = nullptr;
902 return ffi_guard_thunk(__func__, [=]() -> int {
903 auto x25519 = std::make_unique<Botan::X25519_PublicKey>(std::span{pubkey, 32});
904 return ffi_new_object(key, std::move(x25519));
905 });
906#else
907 BOTAN_UNUSED(key, pubkey);
909#endif
910}
911
913#if defined(BOTAN_HAS_X25519)
914 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
915 if(auto x25519 = dynamic_cast<const Botan::X25519_PrivateKey*>(&k)) {
916 const auto x25519_key = x25519->raw_private_key_bits();
917 if(x25519_key.size() != 32) {
919 }
920 Botan::copy_mem(output, x25519_key.data(), x25519_key.size());
921 return BOTAN_FFI_SUCCESS;
922 } else {
924 }
925 });
926#else
927 BOTAN_UNUSED(key, output);
929#endif
930}
931
932int botan_pubkey_x25519_get_pubkey(botan_pubkey_t key, uint8_t output[32]) {
933#if defined(BOTAN_HAS_X25519)
934 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
935 if(auto x25519 = dynamic_cast<const Botan::X25519_PublicKey*>(&k)) {
936 Botan::copy_mem(std::span{output, 32}, x25519->raw_public_key_bits());
937 return BOTAN_FFI_SUCCESS;
938 } else {
940 }
941 });
942#else
943 BOTAN_UNUSED(key, output);
945#endif
946}
947
948/* X448 specific operations */
949
950int botan_privkey_load_x448(botan_privkey_t* key, const uint8_t privkey[56]) {
951#if defined(BOTAN_HAS_X448)
952 if(key == nullptr) {
954 }
955 *key = nullptr;
956 return ffi_guard_thunk(__func__, [=]() -> int {
957 auto x448 = std::make_unique<Botan::X448_PrivateKey>(std::span{privkey, 56});
958 return ffi_new_object(key, std::move(x448));
959 });
960#else
961 BOTAN_UNUSED(key, privkey);
963#endif
964}
965
966int botan_pubkey_load_x448(botan_pubkey_t* key, const uint8_t pubkey[56]) {
967#if defined(BOTAN_HAS_X448)
968 if(key == nullptr) {
970 }
971 *key = nullptr;
972 return ffi_guard_thunk(__func__, [=]() -> int {
973 auto x448 = std::make_unique<Botan::X448_PublicKey>(std::span{pubkey, 56});
974 return ffi_new_object(key, std::move(x448));
975 });
976#else
977 BOTAN_UNUSED(key, pubkey);
979#endif
980}
981
983#if defined(BOTAN_HAS_X448)
984 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
985 if(auto x448 = dynamic_cast<const Botan::X448_PrivateKey*>(&k)) {
986 const auto x448_key = x448->raw_private_key_bits();
987 Botan::copy_mem(std::span{output, 56}, x448_key);
988 return BOTAN_FFI_SUCCESS;
989 } else {
991 }
992 });
993#else
994 BOTAN_UNUSED(key, output);
996#endif
997}
998
999int botan_pubkey_x448_get_pubkey(botan_pubkey_t key, uint8_t output[56]) {
1000#if defined(BOTAN_HAS_X448)
1001 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
1002 if(auto x448 = dynamic_cast<const Botan::X448_PublicKey*>(&k)) {
1003 Botan::copy_mem(std::span{output, 56}, x448->raw_public_key_bits());
1004 return BOTAN_FFI_SUCCESS;
1005 } else {
1007 }
1008 });
1009#else
1010 BOTAN_UNUSED(key, output);
1012#endif
1013}
1014
1015/*
1016* Algorithm specific key operations: Kyber
1017*/
1018
1019int botan_privkey_load_kyber(botan_privkey_t* key, const uint8_t privkey[], size_t key_len) {
1020#if defined(BOTAN_HAS_KYBER)
1021 if(key == nullptr) {
1023 }
1024 *key = nullptr;
1025
1026 const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
1027 if(len == 1632) {
1029 } else if(len == 2400) {
1031 } else if(len == 3168) {
1033 } else {
1034 return {};
1035 }
1036 }(key_len);
1037
1038 if(mode.has_value()) {
1039 return ffi_guard_thunk(__func__, [=]() -> int {
1040 auto kyber = std::make_unique<Botan::Kyber_PrivateKey>(std::span{privkey, key_len}, *mode);
1041 return ffi_new_object(key, std::move(kyber));
1042 });
1043 } else {
1045 }
1046#else
1047 BOTAN_UNUSED(key, key_len, privkey);
1049#endif
1050}
1051
1052int botan_pubkey_load_kyber(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len) {
1053#if defined(BOTAN_HAS_KYBER)
1054 if(key == nullptr) {
1056 }
1057 *key = nullptr;
1058
1059 const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
1060 if(len == 800) {
1062 } else if(len == 1184) {
1064 } else if(len == 1568) {
1066 } else {
1067 return {};
1068 }
1069 }(key_len);
1070
1071 if(mode.has_value()) {
1072 auto kyber = std::make_unique<Botan::Kyber_PublicKey>(std::span{pubkey, key_len}, *mode);
1073 return ffi_new_object(key, std::move(kyber));
1074 } else {
1076 }
1077#else
1078 BOTAN_UNUSED(key, pubkey, key_len);
1080#endif
1081}
1082
1084#if defined(BOTAN_HAS_KYBER)
1085 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1086 if(auto kyber = dynamic_cast<const Botan::Kyber_PrivateKey*>(&k)) {
1087 return invoke_view_callback(view, ctx, kyber->raw_private_key_bits());
1088 } else {
1090 }
1091 });
1092#else
1093 BOTAN_UNUSED(key, ctx, view);
1095#endif
1096}
1097
1099#if defined(BOTAN_HAS_KYBER)
1100 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1101 if(auto kyber = dynamic_cast<const Botan::Kyber_PublicKey*>(&k)) {
1102 return invoke_view_callback(view, ctx, kyber->public_key_bits());
1103 } else {
1105 }
1106 });
1107#else
1108 BOTAN_UNUSED(key, ctx, view);
1110#endif
1111}
1112
1113/*
1114* Algorithm specific key operations: ML-KEM
1115*/
1116
1117int botan_privkey_load_ml_kem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mlkem_mode) {
1118#if defined(BOTAN_HAS_ML_KEM)
1119 if(key == nullptr || privkey == nullptr || mlkem_mode == nullptr) {
1121 }
1122
1123 *key = nullptr;
1124
1125 return ffi_guard_thunk(__func__, [=]() -> int {
1126 auto mode = Botan::ML_KEM_Mode(mlkem_mode);
1127 if(!mode.is_ml_kem()) {
1129 }
1130
1131 auto mlkem_key = std::make_unique<Botan::ML_KEM_PrivateKey>(std::span{privkey, key_len}, mode);
1132 return ffi_new_object(key, std::move(mlkem_key));
1133 });
1134#else
1135 BOTAN_UNUSED(key, key_len, privkey, mlkem_mode);
1137#endif
1138}
1139
1140int botan_pubkey_load_ml_kem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mlkem_mode) {
1141#if defined(BOTAN_HAS_ML_KEM)
1142 if(key == nullptr || pubkey == nullptr || mlkem_mode == nullptr) {
1144 }
1145
1146 *key = nullptr;
1147
1148 return ffi_guard_thunk(__func__, [=]() -> int {
1149 auto mode = Botan::ML_KEM_Mode(mlkem_mode);
1150 if(!mode.is_ml_kem()) {
1152 }
1153
1154 auto mlkem_key = std::make_unique<Botan::ML_KEM_PublicKey>(std::span{pubkey, key_len}, mode.mode());
1155 return ffi_new_object(key, std::move(mlkem_key));
1156 });
1157#else
1158 BOTAN_UNUSED(key, key_len, pubkey, mlkem_mode);
1160#endif
1161}
1162
1163/*
1164* Algorithm specific key operations: ML-DSA
1165*/
1166
1167int botan_privkey_load_ml_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mldsa_mode) {
1168#if defined(BOTAN_HAS_ML_DSA)
1169 if(key == nullptr || privkey == nullptr || mldsa_mode == nullptr) {
1171 }
1172
1173 *key = nullptr;
1174
1175 return ffi_guard_thunk(__func__, [=]() -> int {
1176 auto mode = Botan::ML_DSA_Mode(mldsa_mode);
1177 if(!mode.is_ml_dsa()) {
1179 }
1180
1181 auto mldsa_key = std::make_unique<Botan::ML_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
1182 return ffi_new_object(key, std::move(mldsa_key));
1183 });
1184#else
1185 BOTAN_UNUSED(key, key_len, privkey, mldsa_mode);
1187#endif
1188}
1189
1190int botan_pubkey_load_ml_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mldsa_mode) {
1191#if defined(BOTAN_HAS_ML_DSA)
1192 if(key == nullptr || pubkey == nullptr || mldsa_mode == nullptr) {
1194 }
1195
1196 *key = nullptr;
1197
1198 return ffi_guard_thunk(__func__, [=]() -> int {
1199 auto mode = Botan::ML_DSA_Mode(mldsa_mode);
1200 if(!mode.is_ml_dsa()) {
1202 }
1203
1204 auto mldsa_key = std::make_unique<Botan::ML_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
1205 return ffi_new_object(key, std::move(mldsa_key));
1206 });
1207#else
1208 BOTAN_UNUSED(key, key_len, pubkey, mldsa_mode);
1210#endif
1211}
1212
1213/*
1214* Algorithm specific key operations: SLH-DSA
1215*/
1216
1217int botan_privkey_load_slh_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* slhdsa_mode) {
1218#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1219 if(key == nullptr || privkey == nullptr || slhdsa_mode == nullptr) {
1221 }
1222
1223 *key = nullptr;
1224
1225 return ffi_guard_thunk(__func__, [=]() -> int {
1226 auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
1227 if(!mode.is_slh_dsa()) {
1229 }
1230
1231 auto slhdsa_key = std::make_unique<Botan::SLH_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
1232 return ffi_new_object(key, std::move(slhdsa_key));
1233 });
1234#else
1235 BOTAN_UNUSED(key, key_len, privkey, slhdsa_mode);
1237#endif
1238}
1239
1240int botan_pubkey_load_slh_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* slhdsa_mode) {
1241#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1242 if(key == nullptr || pubkey == nullptr || slhdsa_mode == nullptr) {
1244 }
1245
1246 *key = nullptr;
1247
1248 return ffi_guard_thunk(__func__, [=]() -> int {
1249 auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
1250 if(!mode.is_slh_dsa()) {
1252 }
1253
1254 auto mldsa_key = std::make_unique<Botan::SLH_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
1255 return ffi_new_object(key, std::move(mldsa_key));
1256 });
1257#else
1258 BOTAN_UNUSED(key, key_len, pubkey, slhdsa_mode);
1260#endif
1261}
1262
1263/*
1264* Algorithm specific key operations: FrodoKEM
1265*/
1266
1267int botan_privkey_load_frodokem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* frodo_mode) {
1268#if defined(BOTAN_HAS_FRODOKEM)
1269 if(key == nullptr || privkey == nullptr || frodo_mode == nullptr) {
1271 }
1272
1273 *key = nullptr;
1274
1275 return ffi_guard_thunk(__func__, [=]() -> int {
1276 const auto mode = Botan::FrodoKEMMode(frodo_mode);
1277 auto frodo_key = std::make_unique<Botan::FrodoKEM_PrivateKey>(std::span{privkey, key_len}, mode);
1278 return ffi_new_object(key, std::move(frodo_key));
1279 });
1280#else
1281 BOTAN_UNUSED(key, privkey, key_len, frodo_mode);
1283#endif
1284}
1285
1286int botan_pubkey_load_frodokem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* frodo_mode) {
1287#if defined(BOTAN_HAS_FRODOKEM)
1288 if(key == nullptr || pubkey == nullptr || frodo_mode == nullptr) {
1290 }
1291
1292 *key = nullptr;
1293
1294 return ffi_guard_thunk(__func__, [=]() -> int {
1295 const auto mode = Botan::FrodoKEMMode(frodo_mode);
1296 auto frodo_key = std::make_unique<Botan::FrodoKEM_PublicKey>(std::span{pubkey, key_len}, mode);
1297 return ffi_new_object(key, std::move(frodo_key));
1298 });
1299#else
1300 BOTAN_UNUSED(key, pubkey, key_len, frodo_mode);
1302#endif
1303}
1304
1305/*
1306* Algorithm specific key operations : Classic McEliece
1307*/
1308
1310 const uint8_t privkey[],
1311 size_t key_len,
1312 const char* cmce_mode) {
1313#if defined(BOTAN_HAS_CLASSICMCELIECE)
1314 if(key == nullptr || privkey == nullptr || cmce_mode == nullptr) {
1316 }
1317
1318 *key = nullptr;
1319
1320 return ffi_guard_thunk(__func__, [=]() -> int {
1321 const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
1322 auto cmce_key = std::make_unique<Botan::Classic_McEliece_PrivateKey>(std::span{privkey, key_len}, mode);
1323 return ffi_new_object(key, std::move(cmce_key));
1324 });
1325#else
1326 BOTAN_UNUSED(key, privkey, key_len, cmce_mode);
1328#endif
1329}
1330
1332 const uint8_t pubkey[],
1333 size_t key_len,
1334 const char* cmce_mode) {
1335#if defined(BOTAN_HAS_CLASSICMCELIECE)
1336 if(key == nullptr || pubkey == nullptr || cmce_mode == nullptr) {
1338 }
1339
1340 *key = nullptr;
1341
1342 return ffi_guard_thunk(__func__, [=]() -> int {
1343 const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
1344 auto cmce_key = std::make_unique<Botan::Classic_McEliece_PublicKey>(std::span{pubkey, key_len}, mode);
1345 return ffi_new_object(key, std::move(cmce_key));
1346 });
1347#else
1348 BOTAN_UNUSED(key, pubkey, key_len, cmce_mode);
1350#endif
1351}
1352
1354#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
1355 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1356 if(auto ecc = dynamic_cast<const Botan::EC_PublicKey*>(&k)) {
1357 auto pt = ecc->_public_ec_point().serialize_uncompressed();
1358 return invoke_view_callback(view, ctx, pt);
1359 } else {
1361 }
1362 });
1363#else
1364 BOTAN_UNUSED(key, view, ctx);
1366#endif
1367}
1368
1369// NOLINTEND(misc-misplaced-const)
1370
1371int botan_privkey_create_mceliece(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n, size_t t) {
1372 const std::string mce_params = std::to_string(n) + "," + std::to_string(t);
1373 return botan_privkey_create(key_obj, "McEliece", mce_params.c_str(), rng_obj);
1374}
1375
1377 const char* aead,
1378 const uint8_t ct[],
1379 size_t ct_len,
1380 const uint8_t ad[],
1381 size_t ad_len,
1382 uint8_t out[],
1383 size_t* out_len) {
1384 BOTAN_UNUSED(mce_key_obj, aead, ct, ct_len, ad, ad_len, out, out_len);
1386}
1387
1389 botan_rng_t rng_obj,
1390 const char* aead,
1391 const uint8_t pt[],
1392 size_t pt_len,
1393 const uint8_t ad[],
1394 size_t ad_len,
1395 uint8_t out[],
1396 size_t* out_len) {
1397 BOTAN_UNUSED(mce_key_obj, rng_obj, aead, pt, pt_len, ad, ad_len, out, out_len);
1399}
1400}
#define BOTAN_UNUSED
Definition assert.h:144
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:87
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:93
static EC_Group from_name(std::string_view name)
Definition ec_group.cpp:384
bool used_explicit_encoding() const
Definition ec_group.h:270
static bool supports_named_group(std::string_view name)
Definition ec_group.cpp:350
const EC_Group & domain() const
Definition ecc_key.cpp:64
const EC_AffinePoint & _public_ec_point() const
Definition ecc_key.cpp:76
static Ed25519_PrivateKey from_seed(std::span< const uint8_t > seed)
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:69
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:1537
struct botan_privkey_struct * botan_privkey_t
Definition ffi.h:1304
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
Definition ffi.h:161
int botan_privkey_create(botan_privkey_t *key, const char *algo_name, const char *algo_params, botan_rng_t rng)
Definition ffi_pkey.cpp:29
#define BOTAN_PRIVKEY_EXPORT_FLAG_PEM
Definition ffi.h:1398
struct botan_mp_struct * botan_mp_t
Definition ffi.h:921
void * botan_view_ctx
Definition ffi.h:152
struct botan_rng_struct * botan_rng_t
Definition ffi.h:289
#define BOTAN_PRIVKEY_EXPORT_FLAG_DER
Definition ffi.h:1397
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition ffi.h:146
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:131
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:132
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:123
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:133
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:158
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:39
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:187
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:79
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:247
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:251
DilithiumMode ML_DSA_Mode
Definition ml_dsa.h:21
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:145
KyberMode ML_KEM_Mode
Definition ml_kem.h:21
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