Botan 3.12.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/assert.h>
12#include <botan/ec_group.h>
13#include <botan/hash.h>
14#include <botan/mem_ops.h>
15#include <botan/pem.h>
16#include <botan/internal/ffi_ec.h>
17#include <botan/internal/ffi_mp.h>
18#include <botan/internal/ffi_pkey.h>
19#include <botan/internal/ffi_rng.h>
20#include <botan/internal/ffi_util.h>
21
22#if defined(BOTAN_HAS_DL_GROUP)
23 #include <botan/dl_group.h>
24#endif
25
26#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
27 #include <botan/ecc_key.h>
28#endif
29
30#if defined(BOTAN_HAS_RSA)
31 #include <botan/rsa.h>
32#endif
33
34#if defined(BOTAN_HAS_ELGAMAL)
35 #include <botan/elgamal.h>
36#endif
37
38#if defined(BOTAN_HAS_DSA)
39 #include <botan/dsa.h>
40#endif
41
42#if defined(BOTAN_HAS_ECDSA)
43 #include <botan/ecdsa.h>
44#endif
45
46#if defined(BOTAN_HAS_SM2)
47 #include <botan/sm2.h>
48#endif
49
50#if defined(BOTAN_HAS_ECDH)
51 #include <botan/ecdh.h>
52#endif
53
54#if defined(BOTAN_HAS_X25519)
55 #include <botan/x25519.h>
56#endif
57
58#if defined(BOTAN_HAS_X448)
59 #include <botan/x448.h>
60#endif
61
62#if defined(BOTAN_HAS_ED25519)
63 #include <botan/ed25519.h>
64#endif
65
66#if defined(BOTAN_HAS_ED448)
67 #include <botan/ed448.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
142template <class ECPublicKey_t>
143int pubkey_load_ec_sec1(std::unique_ptr<ECPublicKey_t>& key,
144 std::span<const uint8_t> sec1,
145 std::string_view curve_name) {
148 }
149
150 const auto group = Botan::EC_Group::from_name(curve_name);
151
152 if(auto pt = Botan::EC_AffinePoint::deserialize(group, sec1)) {
153 key.reset(new ECPublicKey_t(group, pt.value()));
154 return BOTAN_FFI_SUCCESS;
155 } else {
157 }
158}
159
160#endif
161
162Botan::BigInt pubkey_get_field(const Botan::Public_Key& key, std::string_view field) {
163#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
164 // Not currently handled by get_int_field
165 if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key)) {
166 if(field == "public_x") {
167 return Botan::BigInt::from_bytes(ecc->_public_ec_point().x_bytes());
168 } else if(field == "public_y") {
169 return Botan::BigInt::from_bytes(ecc->_public_ec_point().y_bytes());
170 }
171 }
172#endif
173
174 try {
175 return key.get_int_field(field);
177 throw Botan_FFI::FFI_Error("Unknown key field", BOTAN_FFI_ERROR_BAD_PARAMETER);
178 }
179}
180
181Botan::BigInt privkey_get_field(const Botan::Private_Key& key, std::string_view field) {
182#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
183 // Not currently handled by get_int_field
184 if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key)) {
185 if(field == "public_x") {
186 return Botan::BigInt::from_bytes(ecc->_public_ec_point().x_bytes());
187 } else if(field == "public_y") {
188 return Botan::BigInt::from_bytes(ecc->_public_ec_point().y_bytes());
189 }
190 }
191#endif
192
193 try {
194 return key.get_int_field(field);
196 throw Botan_FFI::FFI_Error("Unknown key field", BOTAN_FFI_ERROR_BAD_PARAMETER);
197 }
198}
199
200} // namespace
201
202extern "C" {
203
204using namespace Botan_FFI;
205
206int botan_pubkey_get_field(botan_mp_t output, botan_pubkey_t key, const char* field_name_cstr) {
207 if(field_name_cstr == nullptr) {
209 }
210
211 const std::string field_name(field_name_cstr);
212
213 return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = pubkey_get_field(k, field_name); });
214}
215
216int botan_privkey_get_field(botan_mp_t output, botan_privkey_t key, const char* field_name_cstr) {
217 if(field_name_cstr == nullptr) {
219 }
220
221 const std::string field_name(field_name_cstr);
222
223 return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = privkey_get_field(k, field_name); });
224}
225
226/* RSA specific operations */
227
228int botan_privkey_create_rsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n_bits) {
229 if(n_bits < 1024 || n_bits > 16 * 1024) {
231 }
232
233 const std::string n_str = std::to_string(n_bits);
234
235 return botan_privkey_create(key_obj, "RSA", n_str.c_str(), rng_obj);
236}
237
239#if defined(BOTAN_HAS_RSA)
240 if(key == nullptr) {
242 }
243 *key = nullptr;
244
245 return ffi_guard_thunk(__func__, [=]() -> int {
246 auto rsa = std::make_unique<Botan::RSA_PrivateKey>(safe_get(rsa_p), safe_get(rsa_q), safe_get(rsa_e));
247 return ffi_new_object(key, std::move(rsa));
248 });
249#else
250 BOTAN_UNUSED(key, rsa_p, rsa_q, rsa_e);
252#endif
253}
254
255int botan_privkey_load_rsa_pkcs1(botan_privkey_t* key, const uint8_t bits[], size_t len) {
256#if defined(BOTAN_HAS_RSA)
257 if(Botan::any_null_pointers(key, bits)) {
259 }
260 *key = nullptr;
261
262 return ffi_guard_thunk(__func__, [=]() -> int {
264 auto rsa = std::make_unique<Botan::RSA_PrivateKey>(alg_id, std::span{bits, len});
265 return ffi_new_object(key, std::move(rsa));
266 });
267#else
268 BOTAN_UNUSED(key, bits, len);
270#endif
271}
272
274#if defined(BOTAN_HAS_RSA)
275 if(key == nullptr) {
277 }
278 *key = nullptr;
279 return ffi_guard_thunk(__func__, [=]() -> int {
280 auto rsa = std::make_unique<Botan::RSA_PublicKey>(safe_get(n), safe_get(e));
281 return ffi_new_object(key, std::move(rsa));
282 });
283#else
284 BOTAN_UNUSED(key, n, e);
286#endif
287}
288
289int botan_pubkey_load_rsa_pkcs1(botan_pubkey_t* key, const uint8_t bits[], size_t len) {
290#if defined(BOTAN_HAS_RSA)
291 if(Botan::any_null_pointers(key, bits)) {
293 }
294 *key = nullptr;
295
296 return ffi_guard_thunk(__func__, [=]() -> int {
298 auto rsa = std::make_unique<Botan::RSA_PublicKey>(alg_id, std::span{bits, len});
299 return ffi_new_object(key, std::move(rsa));
300 });
301#else
302 BOTAN_UNUSED(key, bits, len);
304#endif
305}
306
310
314
318
322
326
328 return botan_pubkey_get_field(e, key, "e");
329}
330
332 return botan_pubkey_get_field(n, key, "n");
333}
334
335int botan_privkey_rsa_get_privkey(botan_privkey_t rsa_key, uint8_t out[], size_t* out_len, uint32_t flags) {
336#if defined(BOTAN_HAS_RSA)
337 return BOTAN_FFI_VISIT(rsa_key, [=](const auto& k) -> int {
338 if(const Botan::RSA_PrivateKey* rsa = dynamic_cast<const Botan::RSA_PrivateKey*>(&k)) {
339 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
340 return write_vec_output(out, out_len, rsa->private_key_bits());
341 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
342 // TODO define new generic functions for this
343 return write_str_output(reinterpret_cast<char*>(out),
344 out_len,
345 Botan::PEM_Code::encode(rsa->private_key_bits(), "RSA PRIVATE KEY"));
346 } else {
348 }
349 } else {
351 }
352 });
353#else
354 BOTAN_UNUSED(rsa_key, out, out_len, flags);
356#endif
357}
358
359/* DSA specific operations */
360int botan_privkey_create_dsa(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
361#if defined(BOTAN_HAS_DSA)
362
363 if(Botan::any_null_pointers(rng_obj, key)) {
365 }
366
367 if((pbits % 64 != 0) || (qbits % 8 != 0) || (pbits < 1024) || (pbits > 3072) || (qbits < 160) || (qbits > 256)) {
369 }
370
371 return ffi_guard_thunk(__func__, [=]() -> int {
373 const Botan::DL_Group group(rng, Botan::DL_Group::Prime_Subgroup, pbits, qbits);
374 auto dsa = std::make_unique<Botan::DSA_PrivateKey>(rng, group);
375 return ffi_new_object(key, std::move(dsa));
376 });
377#else
378 BOTAN_UNUSED(key, rng_obj, pbits, qbits);
380#endif
381}
382
384#if defined(BOTAN_HAS_DSA)
385 if(key == nullptr) {
387 }
388 *key = nullptr;
389
390 return ffi_guard_thunk(__func__, [=]() -> int {
391 const Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
392 auto dsa = std::make_unique<Botan::DSA_PrivateKey>(group, safe_get(x));
393 return ffi_new_object(key, std::move(dsa));
394 });
395#else
396 BOTAN_UNUSED(key, p, q, g, x);
398#endif
399}
400
402#if defined(BOTAN_HAS_DSA)
403 if(key == nullptr) {
405 }
406 *key = nullptr;
407
408 return ffi_guard_thunk(__func__, [=]() -> int {
409 const Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
410 auto dsa = std::make_unique<Botan::DSA_PublicKey>(group, safe_get(y));
411 return ffi_new_object(key, std::move(dsa));
412 });
413#else
414 BOTAN_UNUSED(key, p, q, g, y);
416#endif
417}
418
422
424 return botan_pubkey_get_field(p, key, "p");
425}
426
428 return botan_pubkey_get_field(q, key, "q");
429}
430
432 return botan_pubkey_get_field(g, key, "g");
433}
434
436 return botan_pubkey_get_field(y, key, "y");
437}
438
439int botan_privkey_create_ecdsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
440 return botan_privkey_create(key_obj, "ECDSA", param_str, rng_obj);
441}
442
443/* ECDSA specific operations */
444
446#if defined(BOTAN_HAS_ECC_KEY)
447 return ffi_guard_thunk(__func__, [=]() -> int {
448 const Botan::Public_Key& pub_key = safe_get(key);
449 const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
450
451 if(ec_key == nullptr) {
453 }
454
455 return ec_key->domain().used_explicit_encoding() ? 1 : 0;
456 });
457#else
458 BOTAN_UNUSED(key);
460#endif
461}
462
463// NOLINTBEGIN(misc-misplaced-const)
464
466 const botan_mp_t public_x,
467 const botan_mp_t public_y,
468 const char* curve_name) {
469#if defined(BOTAN_HAS_ECDSA)
470 if(Botan::any_null_pointers(key, curve_name)) {
472 }
473 *key = nullptr;
474
475 return ffi_guard_thunk(__func__, [=]() -> int {
476 std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
477
478 const int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
479 if(rc == BOTAN_FFI_SUCCESS) {
480 ffi_new_object(key, std::move(p_key));
481 }
482
483 return rc;
484 });
485#else
486 BOTAN_UNUSED(key, public_x, public_y, curve_name);
488#endif
489}
490
491int botan_pubkey_load_ecdsa_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) {
492#if defined(BOTAN_HAS_ECDSA)
493 if(Botan::any_null_pointers(key, sec1, curve_name)) {
495 }
496 *key = nullptr;
497
498 return ffi_guard_thunk(__func__, [=]() -> int {
499 std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
500
501 const int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name);
502 if(rc == BOTAN_FFI_SUCCESS) {
503 ffi_new_object(key, std::move(p_key));
504 }
505
506 return rc;
507 });
508#else
509 BOTAN_UNUSED(key, sec1, sec1_len, curve_name);
511#endif
512}
513
514int botan_privkey_load_ecdsa(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
515#if defined(BOTAN_HAS_ECDSA)
516 if(Botan::any_null_pointers(key, curve_name)) {
518 }
519 *key = nullptr;
520
521 return ffi_guard_thunk(__func__, [=]() -> int {
522 std::unique_ptr<Botan::ECDSA_PrivateKey> p_key;
523 const int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
524 if(rc == BOTAN_FFI_SUCCESS) {
525 ffi_new_object(key, std::move(p_key));
526 }
527 return rc;
528 });
529#else
530 BOTAN_UNUSED(key, scalar, curve_name);
532#endif
533}
534
535/* ElGamal specific operations */
536int botan_privkey_create_elgamal(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
537#if defined(BOTAN_HAS_ELGAMAL)
538 if(Botan::any_null_pointers(key, rng_obj)) {
540 }
541 *key = nullptr;
542
543 if(pbits < 1024 || qbits < 160) {
545 }
546
547 const Botan::DL_Group::PrimeType prime_type =
549
550 return ffi_guard_thunk(__func__, [=]() -> int {
552 const Botan::DL_Group group(rng, prime_type, pbits, qbits);
553 auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(rng, group);
554 return ffi_new_object(key, std::move(elg));
555 });
556#else
557 BOTAN_UNUSED(key, rng_obj, pbits, qbits);
559#endif
560}
561
563#if defined(BOTAN_HAS_ELGAMAL)
564 if(key == nullptr) {
566 }
567 *key = nullptr;
568 return ffi_guard_thunk(__func__, [=]() -> int {
569 const Botan::DL_Group group(safe_get(p), safe_get(g));
570 auto elg = std::make_unique<Botan::ElGamal_PublicKey>(group, safe_get(y));
571 return ffi_new_object(key, std::move(elg));
572 });
573#else
574 BOTAN_UNUSED(key, p, g, y);
576#endif
577}
578
580#if defined(BOTAN_HAS_ELGAMAL)
581 if(key == nullptr) {
583 }
584 *key = nullptr;
585 return ffi_guard_thunk(__func__, [=]() -> int {
586 const Botan::DL_Group group(safe_get(p), safe_get(g));
587 auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(group, safe_get(x));
588 return ffi_new_object(key, std::move(elg));
589 });
590#else
591 BOTAN_UNUSED(key, p, g, x);
593#endif
594}
595
596/* Diffie Hellman specific operations */
597
598int botan_privkey_create_dh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
599 return botan_privkey_create(key_obj, "DH", param_str, rng_obj);
600}
601
603#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
604 if(key == nullptr) {
606 }
607 *key = nullptr;
608 return ffi_guard_thunk(__func__, [=]() -> int {
609 const Botan::DL_Group group(safe_get(p), safe_get(g));
610 auto dh = std::make_unique<Botan::DH_PrivateKey>(group, safe_get(x));
611 return ffi_new_object(key, std::move(dh));
612 });
613#else
614 BOTAN_UNUSED(key, p, g, x);
616#endif
617}
618
620#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
621 if(key == nullptr) {
623 }
624 *key = nullptr;
625 return ffi_guard_thunk(__func__, [=]() -> int {
626 const Botan::DL_Group group(safe_get(p), safe_get(g));
627 auto dh = std::make_unique<Botan::DH_PublicKey>(group, safe_get(y));
628 return ffi_new_object(key, std::move(dh));
629 });
630#else
631 BOTAN_UNUSED(key, p, g, y);
633#endif
634}
635
636/* ECDH + x25519/x448 specific operations */
637
638int botan_privkey_create_ecdh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
639 if(Botan::any_null_pointers(key_obj, param_str)) {
641 }
642 *key_obj = nullptr;
643
644 const std::string params(param_str);
645
646 if(params == "X25519" || params == "x25519" || params == "curve25519") {
647 return botan_privkey_create(key_obj, "X25519", "", rng_obj);
648 }
649
650 if(params == "X448" || params == "x448") {
651 return botan_privkey_create(key_obj, "X448", "", rng_obj);
652 }
653
654 return botan_privkey_create(key_obj, "ECDH", param_str, rng_obj);
655}
656
658 const botan_mp_t public_x,
659 const botan_mp_t public_y,
660 const char* curve_name) {
661#if defined(BOTAN_HAS_ECDH)
662 if(Botan::any_null_pointers(key, curve_name)) {
664 }
665 *key = nullptr;
666 return ffi_guard_thunk(__func__, [=]() -> int {
667 std::unique_ptr<Botan::ECDH_PublicKey> p_key;
668 const int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
669
670 if(rc == BOTAN_FFI_SUCCESS) {
671 ffi_new_object(key, std::move(p_key));
672 }
673 return rc;
674 });
675#else
676 BOTAN_UNUSED(key, public_x, public_y, curve_name);
678#endif
679}
680
681int botan_pubkey_load_ecdh_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) {
682#if defined(BOTAN_HAS_ECDH)
683 if(Botan::any_null_pointers(key, sec1, curve_name)) {
685 }
686 *key = nullptr;
687
688 return ffi_guard_thunk(__func__, [=]() -> int {
689 std::unique_ptr<Botan::ECDH_PublicKey> p_key;
690
691 const int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name);
692 if(rc == BOTAN_FFI_SUCCESS) {
693 ffi_new_object(key, std::move(p_key));
694 }
695
696 return rc;
697 });
698#else
699 BOTAN_UNUSED(key, sec1, sec1_len, curve_name);
701#endif
702}
703
704int botan_privkey_load_ecdh(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
705#if defined(BOTAN_HAS_ECDH)
706 if(Botan::any_null_pointers(key, curve_name)) {
708 }
709 *key = nullptr;
710 return ffi_guard_thunk(__func__, [=]() -> int {
711 std::unique_ptr<Botan::ECDH_PrivateKey> p_key;
712 const int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
713 if(rc == BOTAN_FFI_SUCCESS) {
714 ffi_new_object(key, std::move(p_key));
715 }
716 return rc;
717 });
718#else
719 BOTAN_UNUSED(key, scalar, curve_name);
721#endif
722}
723
724/* SM2 specific operations */
725
727 uint8_t out[], size_t* out_len, const char* ident, const char* hash_algo, const botan_pubkey_t key) {
728 if(Botan::any_null_pointers(out, out_len, ident, hash_algo, key)) {
730 }
731
732#if defined(BOTAN_HAS_SM2)
733 return ffi_guard_thunk(__func__, [=]() -> int {
734 const Botan::Public_Key& pub_key = safe_get(key);
735 const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
736
737 if(ec_key == nullptr) {
739 }
740
741 if(ec_key->algo_name() != "SM2") {
743 }
744
745 const std::string ident_str(ident);
746 std::unique_ptr<Botan::HashFunction> hash = Botan::HashFunction::create_or_throw(hash_algo);
747
748 const auto& pt = ec_key->_public_ec_point();
749
750 const auto za = Botan::sm2_compute_za(*hash, ident_str, ec_key->domain(), pt);
751
752 return write_vec_output(out, out_len, za);
753 });
754#else
756#endif
757}
758
760 const botan_mp_t public_x,
761 const botan_mp_t public_y,
762 const char* curve_name) {
763#if defined(BOTAN_HAS_SM2)
764 if(Botan::any_null_pointers(key, curve_name)) {
766 }
767 *key = nullptr;
768
769 return ffi_guard_thunk(__func__, [=]() -> int {
770 std::unique_ptr<Botan::SM2_PublicKey> p_key;
771 if(pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name) == 0) {
772 return ffi_new_object(key, std::move(p_key));
773 } else {
775 }
776 });
777#else
778 BOTAN_UNUSED(key, public_x, public_y, curve_name);
780#endif
781}
782
783int botan_pubkey_load_sm2_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) {
784#if defined(BOTAN_HAS_SM2)
785 if(Botan::any_null_pointers(key, sec1, curve_name)) {
787 }
788 *key = nullptr;
789
790 return ffi_guard_thunk(__func__, [=]() -> int {
791 std::unique_ptr<Botan::SM2_PublicKey> p_key;
792
793 const int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name);
794 if(rc == BOTAN_FFI_SUCCESS) {
795 ffi_new_object(key, std::move(p_key));
796 }
797
798 return rc;
799 });
800#else
801 BOTAN_UNUSED(key, sec1, sec1_len, curve_name);
803#endif
804}
805
806int botan_privkey_load_sm2(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
807#if defined(BOTAN_HAS_SM2)
808 if(Botan::any_null_pointers(key, curve_name)) {
810 }
811 *key = nullptr;
812
813 return ffi_guard_thunk(__func__, [=]() -> int {
814 std::unique_ptr<Botan::SM2_PrivateKey> p_key;
815 const int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
816
817 if(rc == BOTAN_FFI_SUCCESS) {
818 ffi_new_object(key, std::move(p_key));
819 }
820 return rc;
821 });
822#else
823 BOTAN_UNUSED(key, scalar, curve_name);
825#endif
826}
827
829 const botan_mp_t public_x,
830 const botan_mp_t public_y,
831 const char* curve_name) {
832 return botan_pubkey_load_sm2(key, public_x, public_y, curve_name);
833}
834
835int botan_privkey_load_sm2_enc(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
836 return botan_privkey_load_sm2(key, scalar, curve_name);
837}
838
839/* EC key specific operations */
840
842 if(Botan::any_null_pointers(value)) {
844 }
845#if defined(BOTAN_HAS_ECC_KEY)
846 return ffi_guard_thunk(__func__, [=]() -> int {
847 const Botan::EC_PrivateKey* ec_key = dynamic_cast<const Botan::EC_PrivateKey*>(&safe_get(key));
848 if(ec_key == nullptr) {
850 }
851 return ffi_new_object(value, std::make_unique<Botan::EC_Scalar>(ec_key->_private_key()));
852 });
853#else
854 BOTAN_UNUSED(key, value);
856#endif
857}
858
860 if(Botan::any_null_pointers(ec_group)) {
862 }
863
864#if defined(BOTAN_HAS_ECC_KEY)
865 return ffi_guard_thunk(__func__, [=]() -> int {
866 const Botan::EC_PrivateKey* ec_key = dynamic_cast<const Botan::EC_PrivateKey*>(&safe_get(key));
867 if(ec_key == nullptr) {
869 }
870 return ffi_new_object(ec_group, std::make_unique<Botan::EC_Group>(ec_key->domain()));
871 });
872#else
873 BOTAN_UNUSED(key, ec_group);
875#endif
876}
877
879 if(Botan::any_null_pointers(ec_group)) {
881 }
882#if defined(BOTAN_HAS_ECC_KEY)
883 return ffi_guard_thunk(__func__, [=]() -> int {
884 const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&safe_get(key));
885 if(ec_key == nullptr) {
887 }
888 return ffi_new_object(ec_group, std::make_unique<Botan::EC_Group>(ec_key->domain()));
889 });
890#else
891 BOTAN_UNUSED(key, ec_group);
893#endif
894}
895
896/* Ed25519 specific operations */
897
898int botan_privkey_load_ed25519(botan_privkey_t* key, const uint8_t privkey[32]) {
899#if defined(BOTAN_HAS_ED25519)
900 if(key == nullptr) {
902 }
903 *key = nullptr;
904 return ffi_guard_thunk(__func__, [=]() -> int {
905 auto ed25519 =
906 std::make_unique<Botan::Ed25519_PrivateKey>(Botan::Ed25519_PrivateKey::from_seed(std::span{privkey, 32}));
907 return ffi_new_object(key, std::move(ed25519));
908 });
909#else
910 BOTAN_UNUSED(key, privkey);
912#endif
913}
914
915int botan_pubkey_load_ed25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
916#if defined(BOTAN_HAS_ED25519)
917 if(key == nullptr) {
919 }
920 *key = nullptr;
921 return ffi_guard_thunk(__func__, [=]() -> int {
922 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32);
923 auto ed25519 = std::make_unique<Botan::Ed25519_PublicKey>(pubkey_vec);
924 return ffi_new_object(key, std::move(ed25519));
925 });
926#else
927 BOTAN_UNUSED(key, pubkey);
929#endif
930}
931
933 if(output == nullptr) {
935 }
936#if defined(BOTAN_HAS_ED25519)
937 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
938 if(auto ed = dynamic_cast<const Botan::Ed25519_PrivateKey*>(&k)) {
939 const auto ed_key = ed->raw_private_key_bits();
940 if(ed_key.size() != 64) {
942 }
943 Botan::copy_mem(output, ed_key.data(), ed_key.size());
944 return BOTAN_FFI_SUCCESS;
945 } else {
947 }
948 });
949#else
950 BOTAN_UNUSED(key, output);
952#endif
953}
954
956 if(output == nullptr) {
958 }
959#if defined(BOTAN_HAS_ED25519)
960 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
961 if(auto ed = dynamic_cast<const Botan::Ed25519_PublicKey*>(&k)) {
962 const std::vector<uint8_t>& ed_key = ed->get_public_key();
963 if(ed_key.size() != 32) {
965 }
966 Botan::copy_mem(output, ed_key.data(), ed_key.size());
967 return BOTAN_FFI_SUCCESS;
968 } else {
970 }
971 });
972#else
973 BOTAN_UNUSED(key, output);
975#endif
976}
977
978/* Ed448 specific operations */
979
980int botan_privkey_load_ed448(botan_privkey_t* key, const uint8_t privkey[57]) {
981#if defined(BOTAN_HAS_ED448)
982 if(key == nullptr) {
984 }
985 *key = nullptr;
986 return ffi_guard_thunk(__func__, [=]() -> int {
987 auto ed448 = std::make_unique<Botan::Ed448_PrivateKey>(std::span(privkey, 57));
988 return ffi_new_object(key, std::move(ed448));
989 });
990#else
991 BOTAN_UNUSED(key, privkey);
993#endif
994}
995
996int botan_pubkey_load_ed448(botan_pubkey_t* key, const uint8_t pubkey[57]) {
997#if defined(BOTAN_HAS_ED448)
998 if(key == nullptr) {
1000 }
1001 *key = nullptr;
1002 return ffi_guard_thunk(__func__, [=]() -> int {
1003 auto ed448 = std::make_unique<Botan::Ed448_PublicKey>(std::span(pubkey, 57));
1004 return ffi_new_object(key, std::move(ed448));
1005 });
1006#else
1007 BOTAN_UNUSED(key, pubkey);
1009#endif
1010}
1011
1013 if(output == nullptr) {
1015 }
1016#if defined(BOTAN_HAS_ED448)
1017 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
1018 if(auto ed = dynamic_cast<const Botan::Ed448_PrivateKey*>(&k)) {
1019 const auto ed_key = ed->raw_private_key_bits();
1020 Botan::copy_mem(std::span(output, 57), ed_key);
1021 return BOTAN_FFI_SUCCESS;
1022 } else {
1024 }
1025 });
1026#else
1027 BOTAN_UNUSED(key, output);
1029#endif
1030}
1031
1032int botan_pubkey_ed448_get_pubkey(botan_pubkey_t key, uint8_t output[57]) {
1033 if(output == nullptr) {
1035 }
1036#if defined(BOTAN_HAS_ED448)
1037 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
1038 if(auto ed = dynamic_cast<const Botan::Ed448_PublicKey*>(&k)) {
1039 const auto ed_key = ed->public_key_bits();
1040 Botan::copy_mem(std::span(output, 57), ed_key);
1041 return BOTAN_FFI_SUCCESS;
1042 } else {
1044 }
1045 });
1046#else
1047 BOTAN_UNUSED(key, output);
1049#endif
1050}
1051
1052/* X25519 specific operations */
1053
1054int botan_privkey_load_x25519(botan_privkey_t* key, const uint8_t privkey[32]) {
1055#if defined(BOTAN_HAS_X25519)
1056 if(key == nullptr) {
1058 }
1059 *key = nullptr;
1060 return ffi_guard_thunk(__func__, [=]() -> int {
1061 auto x25519 = std::make_unique<Botan::X25519_PrivateKey>(std::span{privkey, 32});
1062 return ffi_new_object(key, std::move(x25519));
1063 });
1064#else
1065 BOTAN_UNUSED(key, privkey);
1067#endif
1068}
1069
1070int botan_pubkey_load_x25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
1071#if defined(BOTAN_HAS_X25519)
1072 if(key == nullptr) {
1074 }
1075 *key = nullptr;
1076 return ffi_guard_thunk(__func__, [=]() -> int {
1077 auto x25519 = std::make_unique<Botan::X25519_PublicKey>(std::span{pubkey, 32});
1078 return ffi_new_object(key, std::move(x25519));
1079 });
1080#else
1081 BOTAN_UNUSED(key, pubkey);
1083#endif
1084}
1085
1087 if(output == nullptr) {
1089 }
1090#if defined(BOTAN_HAS_X25519)
1091 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
1092 if(auto x25519 = dynamic_cast<const Botan::X25519_PrivateKey*>(&k)) {
1093 const auto x25519_key = x25519->raw_private_key_bits();
1094 if(x25519_key.size() != 32) {
1096 }
1097 Botan::copy_mem(output, x25519_key.data(), x25519_key.size());
1098 return BOTAN_FFI_SUCCESS;
1099 } else {
1101 }
1102 });
1103#else
1104 BOTAN_UNUSED(key, output);
1106#endif
1107}
1108
1110 if(output == nullptr) {
1112 }
1113#if defined(BOTAN_HAS_X25519)
1114 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
1115 if(auto x25519 = dynamic_cast<const Botan::X25519_PublicKey*>(&k)) {
1116 Botan::copy_mem(std::span{output, 32}, x25519->raw_public_key_bits());
1117 return BOTAN_FFI_SUCCESS;
1118 } else {
1120 }
1121 });
1122#else
1123 BOTAN_UNUSED(key, output);
1125#endif
1126}
1127
1128/* X448 specific operations */
1129
1130int botan_privkey_load_x448(botan_privkey_t* key, const uint8_t privkey[56]) {
1131#if defined(BOTAN_HAS_X448)
1132 if(key == nullptr) {
1134 }
1135 *key = nullptr;
1136 return ffi_guard_thunk(__func__, [=]() -> int {
1137 auto x448 = std::make_unique<Botan::X448_PrivateKey>(std::span{privkey, 56});
1138 return ffi_new_object(key, std::move(x448));
1139 });
1140#else
1141 BOTAN_UNUSED(key, privkey);
1143#endif
1144}
1145
1146int botan_pubkey_load_x448(botan_pubkey_t* key, const uint8_t pubkey[56]) {
1147#if defined(BOTAN_HAS_X448)
1148 if(key == nullptr) {
1150 }
1151 *key = nullptr;
1152 return ffi_guard_thunk(__func__, [=]() -> int {
1153 auto x448 = std::make_unique<Botan::X448_PublicKey>(std::span{pubkey, 56});
1154 return ffi_new_object(key, std::move(x448));
1155 });
1156#else
1157 BOTAN_UNUSED(key, pubkey);
1159#endif
1160}
1161
1163 if(output == nullptr) {
1165 }
1166#if defined(BOTAN_HAS_X448)
1167 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
1168 if(auto x448 = dynamic_cast<const Botan::X448_PrivateKey*>(&k)) {
1169 const auto x448_key = x448->raw_private_key_bits();
1170 Botan::copy_mem(std::span{output, 56}, x448_key);
1171 return BOTAN_FFI_SUCCESS;
1172 } else {
1174 }
1175 });
1176#else
1177 BOTAN_UNUSED(key, output);
1179#endif
1180}
1181
1182int botan_pubkey_x448_get_pubkey(botan_pubkey_t key, uint8_t output[56]) {
1183 if(output == nullptr) {
1185 }
1186#if defined(BOTAN_HAS_X448)
1187 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
1188 if(auto x448 = dynamic_cast<const Botan::X448_PublicKey*>(&k)) {
1189 Botan::copy_mem(std::span{output, 56}, x448->raw_public_key_bits());
1190 return BOTAN_FFI_SUCCESS;
1191 } else {
1193 }
1194 });
1195#else
1196 BOTAN_UNUSED(key, output);
1198#endif
1199}
1200
1201/*
1202* Algorithm specific key operations: Kyber
1203*/
1204
1205int botan_privkey_load_kyber(botan_privkey_t* key, const uint8_t privkey[], size_t key_len) {
1206#if defined(BOTAN_HAS_KYBER)
1207 if(Botan::any_null_pointers(key, privkey)) {
1209 }
1210 *key = nullptr;
1211
1212 const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
1213 if(len == 1632) {
1215 } else if(len == 2400) {
1217 } else if(len == 3168) {
1219 } else {
1220 return {};
1221 }
1222 }(key_len);
1223
1224 if(mode.has_value()) {
1225 return ffi_guard_thunk(__func__, [=]() -> int {
1226 auto kyber = std::make_unique<Botan::Kyber_PrivateKey>(std::span{privkey, key_len}, *mode);
1227 return ffi_new_object(key, std::move(kyber));
1228 });
1229 } else {
1231 }
1232#else
1233 BOTAN_UNUSED(key, key_len, privkey);
1235#endif
1236}
1237
1238int botan_pubkey_load_kyber(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len) {
1239#if defined(BOTAN_HAS_KYBER)
1240 if(Botan::any_null_pointers(key, pubkey)) {
1242 }
1243 *key = nullptr;
1244
1245 const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
1246 if(len == 800) {
1248 } else if(len == 1184) {
1250 } else if(len == 1568) {
1252 } else {
1253 return {};
1254 }
1255 }(key_len);
1256
1257 if(mode.has_value()) {
1258 return ffi_guard_thunk(__func__, [=]() -> int {
1259 auto kyber = std::make_unique<Botan::Kyber_PublicKey>(std::span{pubkey, key_len}, *mode);
1260 return ffi_new_object(key, std::move(kyber));
1261 });
1262 } else {
1264 }
1265#else
1266 BOTAN_UNUSED(key, pubkey, key_len);
1268#endif
1269}
1270
1272#if defined(BOTAN_HAS_KYBER)
1273 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1274 if(auto kyber = dynamic_cast<const Botan::Kyber_PrivateKey*>(&k)) {
1275 return invoke_view_callback(view, ctx, kyber->raw_private_key_bits());
1276 } else {
1278 }
1279 });
1280#else
1281 BOTAN_UNUSED(key, ctx, view);
1283#endif
1284}
1285
1287#if defined(BOTAN_HAS_KYBER)
1288 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1289 if(auto kyber = dynamic_cast<const Botan::Kyber_PublicKey*>(&k)) {
1290 return invoke_view_callback(view, ctx, kyber->public_key_bits());
1291 } else {
1293 }
1294 });
1295#else
1296 BOTAN_UNUSED(key, ctx, view);
1298#endif
1299}
1300
1301/*
1302* Algorithm specific key operations: ML-KEM
1303*/
1304
1305int botan_privkey_load_ml_kem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mlkem_mode) {
1306#if defined(BOTAN_HAS_ML_KEM)
1307 if(Botan::any_null_pointers(key, privkey, mlkem_mode)) {
1309 }
1310
1311 *key = nullptr;
1312
1313 return ffi_guard_thunk(__func__, [=]() -> int {
1314 auto mode = Botan::ML_KEM_Mode(mlkem_mode);
1315 if(!mode.is_ml_kem()) {
1317 }
1318
1319 auto mlkem_key = std::make_unique<Botan::ML_KEM_PrivateKey>(std::span{privkey, key_len}, mode);
1320 return ffi_new_object(key, std::move(mlkem_key));
1321 });
1322#else
1323 BOTAN_UNUSED(key, key_len, privkey, mlkem_mode);
1325#endif
1326}
1327
1328int botan_pubkey_load_ml_kem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mlkem_mode) {
1329#if defined(BOTAN_HAS_ML_KEM)
1330 if(Botan::any_null_pointers(key, pubkey, mlkem_mode)) {
1332 }
1333
1334 *key = nullptr;
1335
1336 return ffi_guard_thunk(__func__, [=]() -> int {
1337 auto mode = Botan::ML_KEM_Mode(mlkem_mode);
1338 if(!mode.is_ml_kem()) {
1340 }
1341
1342 auto mlkem_key = std::make_unique<Botan::ML_KEM_PublicKey>(std::span{pubkey, key_len}, mode.mode());
1343 return ffi_new_object(key, std::move(mlkem_key));
1344 });
1345#else
1346 BOTAN_UNUSED(key, key_len, pubkey, mlkem_mode);
1348#endif
1349}
1350
1351/*
1352* Algorithm specific key operations: ML-DSA
1353*/
1354
1355int botan_privkey_load_ml_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mldsa_mode) {
1356#if defined(BOTAN_HAS_ML_DSA)
1357 if(Botan::any_null_pointers(key, privkey, mldsa_mode)) {
1359 }
1360
1361 *key = nullptr;
1362
1363 return ffi_guard_thunk(__func__, [=]() -> int {
1364 auto mode = Botan::ML_DSA_Mode(mldsa_mode);
1365 if(!mode.is_ml_dsa()) {
1367 }
1368
1369 auto mldsa_key = std::make_unique<Botan::ML_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
1370 return ffi_new_object(key, std::move(mldsa_key));
1371 });
1372#else
1373 BOTAN_UNUSED(key, key_len, privkey, mldsa_mode);
1375#endif
1376}
1377
1378int botan_pubkey_load_ml_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mldsa_mode) {
1379#if defined(BOTAN_HAS_ML_DSA)
1380 if(Botan::any_null_pointers(key, pubkey, mldsa_mode)) {
1382 }
1383
1384 *key = nullptr;
1385
1386 return ffi_guard_thunk(__func__, [=]() -> int {
1387 auto mode = Botan::ML_DSA_Mode(mldsa_mode);
1388 if(!mode.is_ml_dsa()) {
1390 }
1391
1392 auto mldsa_key = std::make_unique<Botan::ML_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
1393 return ffi_new_object(key, std::move(mldsa_key));
1394 });
1395#else
1396 BOTAN_UNUSED(key, key_len, pubkey, mldsa_mode);
1398#endif
1399}
1400
1401/*
1402* Algorithm specific key operations: SLH-DSA
1403*/
1404
1405int botan_privkey_load_slh_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* slhdsa_mode) {
1406#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1407 if(Botan::any_null_pointers(key, privkey, slhdsa_mode)) {
1409 }
1410
1411 *key = nullptr;
1412
1413 return ffi_guard_thunk(__func__, [=]() -> int {
1414 auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
1415 if(!mode.is_slh_dsa()) {
1417 }
1418
1419 auto slhdsa_key = std::make_unique<Botan::SLH_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
1420 return ffi_new_object(key, std::move(slhdsa_key));
1421 });
1422#else
1423 BOTAN_UNUSED(key, key_len, privkey, slhdsa_mode);
1425#endif
1426}
1427
1428int botan_pubkey_load_slh_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* slhdsa_mode) {
1429#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1430 if(Botan::any_null_pointers(key, pubkey, slhdsa_mode)) {
1432 }
1433
1434 *key = nullptr;
1435
1436 return ffi_guard_thunk(__func__, [=]() -> int {
1437 auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
1438 if(!mode.is_slh_dsa()) {
1440 }
1441
1442 auto mldsa_key = std::make_unique<Botan::SLH_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
1443 return ffi_new_object(key, std::move(mldsa_key));
1444 });
1445#else
1446 BOTAN_UNUSED(key, key_len, pubkey, slhdsa_mode);
1448#endif
1449}
1450
1451/*
1452* Algorithm specific key operations: FrodoKEM
1453*/
1454
1455int botan_privkey_load_frodokem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* frodo_mode) {
1456#if defined(BOTAN_HAS_FRODOKEM)
1457 if(Botan::any_null_pointers(key, privkey, frodo_mode)) {
1459 }
1460
1461 *key = nullptr;
1462
1463 return ffi_guard_thunk(__func__, [=]() -> int {
1464 const auto mode = Botan::FrodoKEMMode(frodo_mode);
1465 auto frodo_key = std::make_unique<Botan::FrodoKEM_PrivateKey>(std::span{privkey, key_len}, mode);
1466 return ffi_new_object(key, std::move(frodo_key));
1467 });
1468#else
1469 BOTAN_UNUSED(key, privkey, key_len, frodo_mode);
1471#endif
1472}
1473
1474int botan_pubkey_load_frodokem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* frodo_mode) {
1475#if defined(BOTAN_HAS_FRODOKEM)
1476 if(Botan::any_null_pointers(key, pubkey, frodo_mode)) {
1478 }
1479
1480 *key = nullptr;
1481
1482 return ffi_guard_thunk(__func__, [=]() -> int {
1483 const auto mode = Botan::FrodoKEMMode(frodo_mode);
1484 auto frodo_key = std::make_unique<Botan::FrodoKEM_PublicKey>(std::span{pubkey, key_len}, mode);
1485 return ffi_new_object(key, std::move(frodo_key));
1486 });
1487#else
1488 BOTAN_UNUSED(key, pubkey, key_len, frodo_mode);
1490#endif
1491}
1492
1493/*
1494* Algorithm specific key operations : Classic McEliece
1495*/
1496
1498 const uint8_t privkey[],
1499 size_t key_len,
1500 const char* cmce_mode) {
1501#if defined(BOTAN_HAS_CLASSICMCELIECE)
1502 if(Botan::any_null_pointers(key, privkey, cmce_mode)) {
1504 }
1505
1506 *key = nullptr;
1507
1508 return ffi_guard_thunk(__func__, [=]() -> int {
1509 const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
1510 auto cmce_key = std::make_unique<Botan::Classic_McEliece_PrivateKey>(std::span{privkey, key_len}, mode);
1511 return ffi_new_object(key, std::move(cmce_key));
1512 });
1513#else
1514 BOTAN_UNUSED(key, privkey, key_len, cmce_mode);
1516#endif
1517}
1518
1520 const uint8_t pubkey[],
1521 size_t key_len,
1522 const char* cmce_mode) {
1523#if defined(BOTAN_HAS_CLASSICMCELIECE)
1524 if(Botan::any_null_pointers(key, pubkey, cmce_mode)) {
1526 }
1527
1528 *key = nullptr;
1529
1530 return ffi_guard_thunk(__func__, [=]() -> int {
1531 const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
1532 auto cmce_key = std::make_unique<Botan::Classic_McEliece_PublicKey>(std::span{pubkey, key_len}, mode);
1533 return ffi_new_object(key, std::move(cmce_key));
1534 });
1535#else
1536 BOTAN_UNUSED(key, pubkey, key_len, cmce_mode);
1538#endif
1539}
1540
1542#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
1543 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1544 if(auto ecc = dynamic_cast<const Botan::EC_PublicKey*>(&k)) {
1545 auto pt = ecc->_public_ec_point().serialize_uncompressed();
1546 return invoke_view_callback(view, ctx, pt);
1547 } else {
1549 }
1550 });
1551#else
1552 BOTAN_UNUSED(key, view, ctx);
1554#endif
1555}
1556
1557// NOLINTEND(misc-misplaced-const)
1558
1559int botan_privkey_create_mceliece(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n, size_t t) {
1560 const std::string mce_params = std::to_string(n) + "," + std::to_string(t);
1561 return botan_privkey_create(key_obj, "McEliece", mce_params.c_str(), rng_obj);
1562}
1563
1565 const char* aead,
1566 const uint8_t ct[],
1567 size_t ct_len,
1568 const uint8_t ad[],
1569 size_t ad_len,
1570 uint8_t out[],
1571 size_t* out_len) {
1572 BOTAN_UNUSED(mce_key_obj, aead, ct, ct_len, ad, ad_len, out, out_len);
1574}
1575
1577 botan_rng_t rng_obj,
1578 const char* aead,
1579 const uint8_t pt[],
1580 size_t pt_len,
1581 const uint8_t ad[],
1582 size_t ad_len,
1583 uint8_t out[],
1584 size_t* out_len) {
1585 BOTAN_UNUSED(mce_key_obj, rng_obj, aead, pt, pt_len, ad, ad_len, out, out_len);
1587}
1588}
#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:83
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 std::optional< EC_AffinePoint > deserialize(const EC_Group &group, std::span< const uint8_t > bytes)
static EC_Group from_name(std::string_view name)
Definition ec_group.cpp:468
bool used_explicit_encoding() const
Definition ec_group.h:264
static bool supports_named_group(std::string_view name)
Definition ec_group.cpp:412
const EC_Scalar & _private_key() const
Definition ecc_key.cpp:123
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:308
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:1797
struct botan_privkey_struct * botan_privkey_t
Definition ffi.h:1564
struct botan_ec_group_struct * botan_ec_group_t
Definition ffi.h:1302
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
Definition ffi.h:163
int botan_privkey_create(botan_privkey_t *key, const char *algo_name, const char *algo_params, botan_rng_t rng)
Definition ffi_pkey.cpp:30
#define BOTAN_PRIVKEY_EXPORT_FLAG_PEM
Definition ffi.h:1658
struct botan_mp_struct * botan_mp_t
Definition ffi.h:1032
void * botan_view_ctx
Definition ffi.h:154
struct botan_rng_struct * botan_rng_t
Definition ffi.h:291
#define BOTAN_PRIVKEY_EXPORT_FLAG_DER
Definition ffi.h:1657
struct botan_ec_scalar_struct * botan_ec_scalar_t
Definition ffi.h:1446
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:140
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition ffi.h:148
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:132
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:133
@ BOTAN_FFI_SUCCESS
Definition ffi.h:116
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:124
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:134
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_ec_privkey_get_private_key(botan_privkey_t key, botan_ec_scalar_t *value)
int botan_pubkey_dsa_get_p(botan_mp_t p, botan_pubkey_t key)
int botan_pubkey_load_ecdh_sec1(botan_pubkey_t *key, const uint8_t sec1[], size_t sec1_len, const char *curve_name)
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_pubkey_load_rsa_pkcs1(botan_pubkey_t *key, const uint8_t bits[], size_t len)
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_load_sm2_sec1(botan_pubkey_t *key, const uint8_t sec1[], size_t sec1_len, const char *curve_name)
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_ec_pubkey_get_group(botan_pubkey_t key, botan_ec_group_t *ec_group)
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_ec_privkey_get_group(botan_privkey_t key, botan_ec_group_t *ec_group)
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_load_ecdsa_sec1(botan_pubkey_t *key, const uint8_t sec1[], size_t sec1_len, const char *curve_name)
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:190
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:264
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:268
DilithiumMode ML_DSA_Mode
Definition ml_dsa.h:21
KyberMode ML_KEM_Mode
Definition ml_kem.h:21
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
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:82
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54