Botan 3.10.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
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 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(key == nullptr || bits == nullptr) {
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
292
296
300
304
308
310 return botan_pubkey_get_field(e, key, "e");
311}
312
314 return botan_pubkey_get_field(n, key, "n");
315}
316
317int botan_privkey_rsa_get_privkey(botan_privkey_t rsa_key, uint8_t out[], size_t* out_len, uint32_t flags) {
318#if defined(BOTAN_HAS_RSA)
319 return BOTAN_FFI_VISIT(rsa_key, [=](const auto& k) -> int {
320 if(const Botan::RSA_PrivateKey* rsa = dynamic_cast<const Botan::RSA_PrivateKey*>(&k)) {
321 if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) {
322 return write_vec_output(out, out_len, rsa->private_key_bits());
323 } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) {
324 // TODO define new generic functions for this
325 return write_str_output(reinterpret_cast<char*>(out),
326 out_len,
327 Botan::PEM_Code::encode(rsa->private_key_bits(), "RSA PRIVATE KEY"));
328 } else {
330 }
331 } else {
333 }
334 });
335#else
336 BOTAN_UNUSED(rsa_key, out, out_len, flags);
338#endif
339}
340
341/* DSA specific operations */
342int botan_privkey_create_dsa(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
343#if defined(BOTAN_HAS_DSA)
344
345 if((rng_obj == nullptr) || (key == nullptr)) {
347 }
348
349 if((pbits % 64 != 0) || (qbits % 8 != 0) || (pbits < 1024) || (pbits > 3072) || (qbits < 160) || (qbits > 256)) {
351 }
352
353 return ffi_guard_thunk(__func__, [=]() -> int {
355 Botan::DL_Group group(rng, Botan::DL_Group::Prime_Subgroup, pbits, qbits);
356 auto dsa = std::make_unique<Botan::DSA_PrivateKey>(rng, group);
357 return ffi_new_object(key, std::move(dsa));
358 });
359#else
360 BOTAN_UNUSED(key, rng_obj, pbits, qbits);
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_PrivateKey>(group, safe_get(x));
375 return ffi_new_object(key, std::move(dsa));
376 });
377#else
378 BOTAN_UNUSED(key, p, q, g, x);
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 Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
392 auto dsa = std::make_unique<Botan::DSA_PublicKey>(group, safe_get(y));
393 return ffi_new_object(key, std::move(dsa));
394 });
395#else
396 BOTAN_UNUSED(key, p, q, g, y);
398#endif
399}
400
404
406 return botan_pubkey_get_field(p, key, "p");
407}
408
410 return botan_pubkey_get_field(q, key, "q");
411}
412
414 return botan_pubkey_get_field(g, key, "g");
415}
416
418 return botan_pubkey_get_field(y, key, "y");
419}
420
421int botan_privkey_create_ecdsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
422 return botan_privkey_create(key_obj, "ECDSA", param_str, rng_obj);
423}
424
425/* ECDSA specific operations */
426
428#if defined(BOTAN_HAS_ECC_KEY)
429 return ffi_guard_thunk(__func__, [=]() -> int {
430 const Botan::Public_Key& pub_key = safe_get(key);
431 const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
432
433 if(ec_key == nullptr) {
435 }
436
437 return ec_key->domain().used_explicit_encoding() ? 1 : 0;
438 });
439#else
440 BOTAN_UNUSED(key);
442#endif
443}
444
445// NOLINTBEGIN(misc-misplaced-const)
446
448 const botan_mp_t public_x,
449 const botan_mp_t public_y,
450 const char* curve_name) {
451#if defined(BOTAN_HAS_ECDSA)
452 if(key == nullptr || curve_name == nullptr) {
454 }
455 *key = nullptr;
456
457 return ffi_guard_thunk(__func__, [=]() -> int {
458 std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
459
460 int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
461 if(rc == BOTAN_FFI_SUCCESS) {
462 ffi_new_object(key, std::move(p_key));
463 }
464
465 return rc;
466 });
467#else
468 BOTAN_UNUSED(key, public_x, public_y, curve_name);
470#endif
471}
472
473int botan_pubkey_load_ecdsa_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) {
474#if defined(BOTAN_HAS_ECDSA)
475 if(key == nullptr || sec1 == nullptr || curve_name == nullptr) {
477 }
478 *key = nullptr;
479
480 return ffi_guard_thunk(__func__, [=]() -> int {
481 std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
482
483 int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name);
484 if(rc == BOTAN_FFI_SUCCESS) {
485 ffi_new_object(key, std::move(p_key));
486 }
487
488 return rc;
489 });
490#else
491 BOTAN_UNUSED(key, sec1, sec1_len, curve_name);
493#endif
494}
495
496int botan_privkey_load_ecdsa(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
497#if defined(BOTAN_HAS_ECDSA)
498 if(key == nullptr || curve_name == nullptr) {
500 }
501 *key = nullptr;
502
503 return ffi_guard_thunk(__func__, [=]() -> int {
504 std::unique_ptr<Botan::ECDSA_PrivateKey> p_key;
505 int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
506 if(rc == BOTAN_FFI_SUCCESS) {
507 ffi_new_object(key, std::move(p_key));
508 }
509 return rc;
510 });
511#else
512 BOTAN_UNUSED(key, scalar, curve_name);
514#endif
515}
516
517/* ElGamal specific operations */
518int botan_privkey_create_elgamal(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) {
519#if defined(BOTAN_HAS_ELGAMAL)
520 if(key == nullptr || rng_obj == nullptr) {
522 }
523 *key = nullptr;
524
525 if(pbits < 1024 || qbits < 160) {
527 }
528
529 Botan::DL_Group::PrimeType prime_type =
531
532 return ffi_guard_thunk(__func__, [=]() -> int {
534 Botan::DL_Group group(rng, prime_type, pbits, qbits);
535 auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(rng, group);
536 return ffi_new_object(key, std::move(elg));
537 });
538#else
539 BOTAN_UNUSED(key, rng_obj, pbits, qbits);
541#endif
542}
543
545#if defined(BOTAN_HAS_ELGAMAL)
546 if(key == nullptr) {
548 }
549 *key = nullptr;
550 return ffi_guard_thunk(__func__, [=]() -> int {
551 Botan::DL_Group group(safe_get(p), safe_get(g));
552 auto elg = std::make_unique<Botan::ElGamal_PublicKey>(group, safe_get(y));
553 return ffi_new_object(key, std::move(elg));
554 });
555#else
556 BOTAN_UNUSED(key, p, g, y);
558#endif
559}
560
562#if defined(BOTAN_HAS_ELGAMAL)
563 if(key == nullptr) {
565 }
566 *key = nullptr;
567 return ffi_guard_thunk(__func__, [=]() -> int {
568 Botan::DL_Group group(safe_get(p), safe_get(g));
569 auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(group, safe_get(x));
570 return ffi_new_object(key, std::move(elg));
571 });
572#else
573 BOTAN_UNUSED(key, p, g, x);
575#endif
576}
577
578/* Diffie Hellman specific operations */
579
580int botan_privkey_create_dh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
581 return botan_privkey_create(key_obj, "DH", param_str, rng_obj);
582}
583
585#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
586 if(key == nullptr) {
588 }
589 *key = nullptr;
590 return ffi_guard_thunk(__func__, [=]() -> int {
591 Botan::DL_Group group(safe_get(p), safe_get(g));
592 auto dh = std::make_unique<Botan::DH_PrivateKey>(group, safe_get(x));
593 return ffi_new_object(key, std::move(dh));
594 });
595#else
596 BOTAN_UNUSED(key, p, g, x);
598#endif
599}
600
602#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
603 if(key == nullptr) {
605 }
606 *key = nullptr;
607 return ffi_guard_thunk(__func__, [=]() -> int {
608 Botan::DL_Group group(safe_get(p), safe_get(g));
609 auto dh = std::make_unique<Botan::DH_PublicKey>(group, safe_get(y));
610 return ffi_new_object(key, std::move(dh));
611 });
612#else
613 BOTAN_UNUSED(key, p, g, y);
615#endif
616}
617
618/* ECDH + x25519/x448 specific operations */
619
620int botan_privkey_create_ecdh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) {
621 if(key_obj == nullptr || param_str == nullptr) {
623 }
624 *key_obj = nullptr;
625
626 const std::string params(param_str);
627
628 if(params == "X25519" || params == "x25519" || params == "curve25519") {
629 return botan_privkey_create(key_obj, "X25519", "", rng_obj);
630 }
631
632 if(params == "X448" || params == "x448") {
633 return botan_privkey_create(key_obj, "X448", "", rng_obj);
634 }
635
636 return botan_privkey_create(key_obj, "ECDH", param_str, rng_obj);
637}
638
640 const botan_mp_t public_x,
641 const botan_mp_t public_y,
642 const char* curve_name) {
643#if defined(BOTAN_HAS_ECDH)
644 if(key == nullptr || curve_name == nullptr) {
646 }
647 *key = nullptr;
648 return ffi_guard_thunk(__func__, [=]() -> int {
649 std::unique_ptr<Botan::ECDH_PublicKey> p_key;
650 int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name);
651
652 if(rc == BOTAN_FFI_SUCCESS) {
653 ffi_new_object(key, std::move(p_key));
654 }
655 return rc;
656 });
657#else
658 BOTAN_UNUSED(key, public_x, public_y, curve_name);
660#endif
661}
662
663int botan_pubkey_load_ecdh_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) {
664#if defined(BOTAN_HAS_ECDH)
665 if(key == nullptr || sec1 == nullptr || curve_name == nullptr) {
667 }
668 *key = nullptr;
669
670 return ffi_guard_thunk(__func__, [=]() -> int {
671 std::unique_ptr<Botan::ECDH_PublicKey> p_key;
672
673 int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name);
674 if(rc == BOTAN_FFI_SUCCESS) {
675 ffi_new_object(key, std::move(p_key));
676 }
677
678 return rc;
679 });
680#else
681 BOTAN_UNUSED(key, sec1, sec1_len, curve_name);
683#endif
684}
685
686int botan_privkey_load_ecdh(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
687#if defined(BOTAN_HAS_ECDH)
688 if(key == nullptr || curve_name == nullptr) {
690 }
691 *key = nullptr;
692 return ffi_guard_thunk(__func__, [=]() -> int {
693 std::unique_ptr<Botan::ECDH_PrivateKey> p_key;
694 int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
695 if(rc == BOTAN_FFI_SUCCESS) {
696 ffi_new_object(key, std::move(p_key));
697 }
698 return rc;
699 });
700#else
701 BOTAN_UNUSED(key, scalar, curve_name);
703#endif
704}
705
706/* SM2 specific operations */
707
709 uint8_t out[], size_t* out_len, const char* ident, const char* hash_algo, const botan_pubkey_t key) {
710 if(out == nullptr || out_len == nullptr || ident == nullptr || hash_algo == nullptr || key == nullptr) {
712 }
713
714#if defined(BOTAN_HAS_SM2)
715 return ffi_guard_thunk(__func__, [=]() -> int {
716 const Botan::Public_Key& pub_key = safe_get(key);
717 const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key);
718
719 if(ec_key == nullptr) {
721 }
722
723 if(ec_key->algo_name() != "SM2") {
725 }
726
727 const std::string ident_str(ident);
728 std::unique_ptr<Botan::HashFunction> hash = Botan::HashFunction::create_or_throw(hash_algo);
729
730 const auto& pt = ec_key->_public_ec_point();
731
732 const auto za = Botan::sm2_compute_za(*hash, ident_str, ec_key->domain(), pt);
733
734 return write_vec_output(out, out_len, za);
735 });
736#else
738#endif
739}
740
742 const botan_mp_t public_x,
743 const botan_mp_t public_y,
744 const char* curve_name) {
745#if defined(BOTAN_HAS_SM2)
746 if(key == nullptr || curve_name == nullptr) {
748 }
749 *key = nullptr;
750
751 return ffi_guard_thunk(__func__, [=]() -> int {
752 std::unique_ptr<Botan::SM2_PublicKey> p_key;
753 if(pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name) == 0) {
754 return ffi_new_object(key, std::move(p_key));
755 } else {
757 }
758 });
759#else
760 BOTAN_UNUSED(key, public_x, public_y, curve_name);
762#endif
763}
764
765int botan_pubkey_load_sm2_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) {
766#if defined(BOTAN_HAS_SM2)
767 if(key == nullptr || sec1 == nullptr || curve_name == nullptr) {
769 }
770 *key = nullptr;
771
772 return ffi_guard_thunk(__func__, [=]() -> int {
773 std::unique_ptr<Botan::SM2_PublicKey> p_key;
774
775 int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name);
776 if(rc == BOTAN_FFI_SUCCESS) {
777 ffi_new_object(key, std::move(p_key));
778 }
779
780 return rc;
781 });
782#else
783 BOTAN_UNUSED(key, sec1, sec1_len, curve_name);
785#endif
786}
787
788int botan_privkey_load_sm2(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
789#if defined(BOTAN_HAS_SM2)
790 if(key == nullptr || curve_name == nullptr) {
792 }
793 *key = nullptr;
794
795 return ffi_guard_thunk(__func__, [=]() -> int {
796 std::unique_ptr<Botan::SM2_PrivateKey> p_key;
797 int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name);
798
799 if(rc == BOTAN_FFI_SUCCESS) {
800 ffi_new_object(key, std::move(p_key));
801 }
802 return rc;
803 });
804#else
805 BOTAN_UNUSED(key, scalar, curve_name);
807#endif
808}
809
811 const botan_mp_t public_x,
812 const botan_mp_t public_y,
813 const char* curve_name) {
814 return botan_pubkey_load_sm2(key, public_x, public_y, curve_name);
815}
816
817int botan_privkey_load_sm2_enc(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) {
818 return botan_privkey_load_sm2(key, scalar, curve_name);
819}
820
821/* Ed25519 specific operations */
822
823int botan_privkey_load_ed25519(botan_privkey_t* key, const uint8_t privkey[32]) {
824#if defined(BOTAN_HAS_ED25519)
825 if(key == nullptr) {
827 }
828 *key = nullptr;
829 return ffi_guard_thunk(__func__, [=]() -> int {
830 auto ed25519 =
831 std::make_unique<Botan::Ed25519_PrivateKey>(Botan::Ed25519_PrivateKey::from_seed(std::span{privkey, 32}));
832 return ffi_new_object(key, std::move(ed25519));
833 });
834#else
835 BOTAN_UNUSED(key, privkey);
837#endif
838}
839
840int botan_pubkey_load_ed25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
841#if defined(BOTAN_HAS_ED25519)
842 if(key == nullptr) {
844 }
845 *key = nullptr;
846 return ffi_guard_thunk(__func__, [=]() -> int {
847 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32);
848 auto ed25519 = std::make_unique<Botan::Ed25519_PublicKey>(pubkey_vec);
849 return ffi_new_object(key, std::move(ed25519));
850 });
851#else
852 BOTAN_UNUSED(key, pubkey);
854#endif
855}
856
858#if defined(BOTAN_HAS_ED25519)
859 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
860 if(auto ed = dynamic_cast<const Botan::Ed25519_PrivateKey*>(&k)) {
861 const auto ed_key = ed->raw_private_key_bits();
862 if(ed_key.size() != 64) {
864 }
865 Botan::copy_mem(output, ed_key.data(), ed_key.size());
866 return BOTAN_FFI_SUCCESS;
867 } else {
869 }
870 });
871#else
872 BOTAN_UNUSED(key, output);
874#endif
875}
876
878#if defined(BOTAN_HAS_ED25519)
879 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
880 if(auto ed = dynamic_cast<const Botan::Ed25519_PublicKey*>(&k)) {
881 const std::vector<uint8_t>& ed_key = ed->get_public_key();
882 if(ed_key.size() != 32) {
884 }
885 Botan::copy_mem(output, ed_key.data(), ed_key.size());
886 return BOTAN_FFI_SUCCESS;
887 } else {
889 }
890 });
891#else
892 BOTAN_UNUSED(key, output);
894#endif
895}
896
897/* Ed448 specific operations */
898
899int botan_privkey_load_ed448(botan_privkey_t* key, const uint8_t privkey[57]) {
900#if defined(BOTAN_HAS_ED448)
901 if(key == nullptr) {
903 }
904 *key = nullptr;
905 return ffi_guard_thunk(__func__, [=]() -> int {
906 auto ed448 = std::make_unique<Botan::Ed448_PrivateKey>(std::span(privkey, 57));
907 return ffi_new_object(key, std::move(ed448));
908 });
909#else
910 BOTAN_UNUSED(key, privkey);
912#endif
913}
914
915int botan_pubkey_load_ed448(botan_pubkey_t* key, const uint8_t pubkey[57]) {
916#if defined(BOTAN_HAS_ED448)
917 if(key == nullptr) {
919 }
920 *key = nullptr;
921 return ffi_guard_thunk(__func__, [=]() -> int {
922 auto ed448 = std::make_unique<Botan::Ed448_PublicKey>(std::span(pubkey, 57));
923 return ffi_new_object(key, std::move(ed448));
924 });
925#else
926 BOTAN_UNUSED(key, pubkey);
928#endif
929}
930
932#if defined(BOTAN_HAS_ED448)
933 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
934 if(auto ed = dynamic_cast<const Botan::Ed448_PrivateKey*>(&k)) {
935 const auto ed_key = ed->raw_private_key_bits();
936 Botan::copy_mem(std::span(output, 57), ed_key);
937 return BOTAN_FFI_SUCCESS;
938 } else {
940 }
941 });
942#else
943 BOTAN_UNUSED(key, output);
945#endif
946}
947
948int botan_pubkey_ed448_get_pubkey(botan_pubkey_t key, uint8_t output[57]) {
949#if defined(BOTAN_HAS_ED448)
950 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
951 if(auto ed = dynamic_cast<const Botan::Ed448_PublicKey*>(&k)) {
952 const auto ed_key = ed->public_key_bits();
953 Botan::copy_mem(std::span(output, 57), ed_key);
954 return BOTAN_FFI_SUCCESS;
955 } else {
957 }
958 });
959#else
960 BOTAN_UNUSED(key, output);
962#endif
963}
964
965/* X25519 specific operations */
966
967int botan_privkey_load_x25519(botan_privkey_t* key, const uint8_t privkey[32]) {
968#if defined(BOTAN_HAS_X25519)
969 if(key == nullptr) {
971 }
972 *key = nullptr;
973 return ffi_guard_thunk(__func__, [=]() -> int {
974 auto x25519 = std::make_unique<Botan::X25519_PrivateKey>(std::span{privkey, 32});
975 return ffi_new_object(key, std::move(x25519));
976 });
977#else
978 BOTAN_UNUSED(key, privkey);
980#endif
981}
982
983int botan_pubkey_load_x25519(botan_pubkey_t* key, const uint8_t pubkey[32]) {
984#if defined(BOTAN_HAS_X25519)
985 if(key == nullptr) {
987 }
988 *key = nullptr;
989 return ffi_guard_thunk(__func__, [=]() -> int {
990 auto x25519 = std::make_unique<Botan::X25519_PublicKey>(std::span{pubkey, 32});
991 return ffi_new_object(key, std::move(x25519));
992 });
993#else
994 BOTAN_UNUSED(key, pubkey);
996#endif
997}
998
1000#if defined(BOTAN_HAS_X25519)
1001 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
1002 if(auto x25519 = dynamic_cast<const Botan::X25519_PrivateKey*>(&k)) {
1003 const auto x25519_key = x25519->raw_private_key_bits();
1004 if(x25519_key.size() != 32) {
1006 }
1007 Botan::copy_mem(output, x25519_key.data(), x25519_key.size());
1008 return BOTAN_FFI_SUCCESS;
1009 } else {
1011 }
1012 });
1013#else
1014 BOTAN_UNUSED(key, output);
1016#endif
1017}
1018
1020#if defined(BOTAN_HAS_X25519)
1021 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
1022 if(auto x25519 = dynamic_cast<const Botan::X25519_PublicKey*>(&k)) {
1023 Botan::copy_mem(std::span{output, 32}, x25519->raw_public_key_bits());
1024 return BOTAN_FFI_SUCCESS;
1025 } else {
1027 }
1028 });
1029#else
1030 BOTAN_UNUSED(key, output);
1032#endif
1033}
1034
1035/* X448 specific operations */
1036
1037int botan_privkey_load_x448(botan_privkey_t* key, const uint8_t privkey[56]) {
1038#if defined(BOTAN_HAS_X448)
1039 if(key == nullptr) {
1041 }
1042 *key = nullptr;
1043 return ffi_guard_thunk(__func__, [=]() -> int {
1044 auto x448 = std::make_unique<Botan::X448_PrivateKey>(std::span{privkey, 56});
1045 return ffi_new_object(key, std::move(x448));
1046 });
1047#else
1048 BOTAN_UNUSED(key, privkey);
1050#endif
1051}
1052
1053int botan_pubkey_load_x448(botan_pubkey_t* key, const uint8_t pubkey[56]) {
1054#if defined(BOTAN_HAS_X448)
1055 if(key == nullptr) {
1057 }
1058 *key = nullptr;
1059 return ffi_guard_thunk(__func__, [=]() -> int {
1060 auto x448 = std::make_unique<Botan::X448_PublicKey>(std::span{pubkey, 56});
1061 return ffi_new_object(key, std::move(x448));
1062 });
1063#else
1064 BOTAN_UNUSED(key, pubkey);
1066#endif
1067}
1068
1070#if defined(BOTAN_HAS_X448)
1071 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
1072 if(auto x448 = dynamic_cast<const Botan::X448_PrivateKey*>(&k)) {
1073 const auto x448_key = x448->raw_private_key_bits();
1074 Botan::copy_mem(std::span{output, 56}, x448_key);
1075 return BOTAN_FFI_SUCCESS;
1076 } else {
1078 }
1079 });
1080#else
1081 BOTAN_UNUSED(key, output);
1083#endif
1084}
1085
1086int botan_pubkey_x448_get_pubkey(botan_pubkey_t key, uint8_t output[56]) {
1087#if defined(BOTAN_HAS_X448)
1088 return BOTAN_FFI_VISIT(key, [=](const auto& k) {
1089 if(auto x448 = dynamic_cast<const Botan::X448_PublicKey*>(&k)) {
1090 Botan::copy_mem(std::span{output, 56}, x448->raw_public_key_bits());
1091 return BOTAN_FFI_SUCCESS;
1092 } else {
1094 }
1095 });
1096#else
1097 BOTAN_UNUSED(key, output);
1099#endif
1100}
1101
1102/*
1103* Algorithm specific key operations: Kyber
1104*/
1105
1106int botan_privkey_load_kyber(botan_privkey_t* key, const uint8_t privkey[], size_t key_len) {
1107#if defined(BOTAN_HAS_KYBER)
1108 if(key == nullptr) {
1110 }
1111 *key = nullptr;
1112
1113 const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
1114 if(len == 1632) {
1116 } else if(len == 2400) {
1118 } else if(len == 3168) {
1120 } else {
1121 return {};
1122 }
1123 }(key_len);
1124
1125 if(mode.has_value()) {
1126 return ffi_guard_thunk(__func__, [=]() -> int {
1127 auto kyber = std::make_unique<Botan::Kyber_PrivateKey>(std::span{privkey, key_len}, *mode);
1128 return ffi_new_object(key, std::move(kyber));
1129 });
1130 } else {
1132 }
1133#else
1134 BOTAN_UNUSED(key, key_len, privkey);
1136#endif
1137}
1138
1139int botan_pubkey_load_kyber(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len) {
1140#if defined(BOTAN_HAS_KYBER)
1141 if(key == nullptr) {
1143 }
1144 *key = nullptr;
1145
1146 const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> {
1147 if(len == 800) {
1149 } else if(len == 1184) {
1151 } else if(len == 1568) {
1153 } else {
1154 return {};
1155 }
1156 }(key_len);
1157
1158 if(mode.has_value()) {
1159 auto kyber = std::make_unique<Botan::Kyber_PublicKey>(std::span{pubkey, key_len}, *mode);
1160 return ffi_new_object(key, std::move(kyber));
1161 } else {
1163 }
1164#else
1165 BOTAN_UNUSED(key, pubkey, key_len);
1167#endif
1168}
1169
1171#if defined(BOTAN_HAS_KYBER)
1172 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1173 if(auto kyber = dynamic_cast<const Botan::Kyber_PrivateKey*>(&k)) {
1174 return invoke_view_callback(view, ctx, kyber->raw_private_key_bits());
1175 } else {
1177 }
1178 });
1179#else
1180 BOTAN_UNUSED(key, ctx, view);
1182#endif
1183}
1184
1186#if defined(BOTAN_HAS_KYBER)
1187 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1188 if(auto kyber = dynamic_cast<const Botan::Kyber_PublicKey*>(&k)) {
1189 return invoke_view_callback(view, ctx, kyber->public_key_bits());
1190 } else {
1192 }
1193 });
1194#else
1195 BOTAN_UNUSED(key, ctx, view);
1197#endif
1198}
1199
1200/*
1201* Algorithm specific key operations: ML-KEM
1202*/
1203
1204int botan_privkey_load_ml_kem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mlkem_mode) {
1205#if defined(BOTAN_HAS_ML_KEM)
1206 if(key == nullptr || privkey == nullptr || mlkem_mode == nullptr) {
1208 }
1209
1210 *key = nullptr;
1211
1212 return ffi_guard_thunk(__func__, [=]() -> int {
1213 auto mode = Botan::ML_KEM_Mode(mlkem_mode);
1214 if(!mode.is_ml_kem()) {
1216 }
1217
1218 auto mlkem_key = std::make_unique<Botan::ML_KEM_PrivateKey>(std::span{privkey, key_len}, mode);
1219 return ffi_new_object(key, std::move(mlkem_key));
1220 });
1221#else
1222 BOTAN_UNUSED(key, key_len, privkey, mlkem_mode);
1224#endif
1225}
1226
1227int botan_pubkey_load_ml_kem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mlkem_mode) {
1228#if defined(BOTAN_HAS_ML_KEM)
1229 if(key == nullptr || pubkey == nullptr || mlkem_mode == nullptr) {
1231 }
1232
1233 *key = nullptr;
1234
1235 return ffi_guard_thunk(__func__, [=]() -> int {
1236 auto mode = Botan::ML_KEM_Mode(mlkem_mode);
1237 if(!mode.is_ml_kem()) {
1239 }
1240
1241 auto mlkem_key = std::make_unique<Botan::ML_KEM_PublicKey>(std::span{pubkey, key_len}, mode.mode());
1242 return ffi_new_object(key, std::move(mlkem_key));
1243 });
1244#else
1245 BOTAN_UNUSED(key, key_len, pubkey, mlkem_mode);
1247#endif
1248}
1249
1250/*
1251* Algorithm specific key operations: ML-DSA
1252*/
1253
1254int botan_privkey_load_ml_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mldsa_mode) {
1255#if defined(BOTAN_HAS_ML_DSA)
1256 if(key == nullptr || privkey == nullptr || mldsa_mode == nullptr) {
1258 }
1259
1260 *key = nullptr;
1261
1262 return ffi_guard_thunk(__func__, [=]() -> int {
1263 auto mode = Botan::ML_DSA_Mode(mldsa_mode);
1264 if(!mode.is_ml_dsa()) {
1266 }
1267
1268 auto mldsa_key = std::make_unique<Botan::ML_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
1269 return ffi_new_object(key, std::move(mldsa_key));
1270 });
1271#else
1272 BOTAN_UNUSED(key, key_len, privkey, mldsa_mode);
1274#endif
1275}
1276
1277int botan_pubkey_load_ml_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mldsa_mode) {
1278#if defined(BOTAN_HAS_ML_DSA)
1279 if(key == nullptr || pubkey == nullptr || mldsa_mode == nullptr) {
1281 }
1282
1283 *key = nullptr;
1284
1285 return ffi_guard_thunk(__func__, [=]() -> int {
1286 auto mode = Botan::ML_DSA_Mode(mldsa_mode);
1287 if(!mode.is_ml_dsa()) {
1289 }
1290
1291 auto mldsa_key = std::make_unique<Botan::ML_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
1292 return ffi_new_object(key, std::move(mldsa_key));
1293 });
1294#else
1295 BOTAN_UNUSED(key, key_len, pubkey, mldsa_mode);
1297#endif
1298}
1299
1300/*
1301* Algorithm specific key operations: SLH-DSA
1302*/
1303
1304int botan_privkey_load_slh_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* slhdsa_mode) {
1305#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1306 if(key == nullptr || privkey == nullptr || slhdsa_mode == nullptr) {
1308 }
1309
1310 *key = nullptr;
1311
1312 return ffi_guard_thunk(__func__, [=]() -> int {
1313 auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
1314 if(!mode.is_slh_dsa()) {
1316 }
1317
1318 auto slhdsa_key = std::make_unique<Botan::SLH_DSA_PrivateKey>(std::span{privkey, key_len}, mode);
1319 return ffi_new_object(key, std::move(slhdsa_key));
1320 });
1321#else
1322 BOTAN_UNUSED(key, key_len, privkey, slhdsa_mode);
1324#endif
1325}
1326
1327int botan_pubkey_load_slh_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* slhdsa_mode) {
1328#if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE)
1329 if(key == nullptr || pubkey == nullptr || slhdsa_mode == nullptr) {
1331 }
1332
1333 *key = nullptr;
1334
1335 return ffi_guard_thunk(__func__, [=]() -> int {
1336 auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode);
1337 if(!mode.is_slh_dsa()) {
1339 }
1340
1341 auto mldsa_key = std::make_unique<Botan::SLH_DSA_PublicKey>(std::span{pubkey, key_len}, mode);
1342 return ffi_new_object(key, std::move(mldsa_key));
1343 });
1344#else
1345 BOTAN_UNUSED(key, key_len, pubkey, slhdsa_mode);
1347#endif
1348}
1349
1350/*
1351* Algorithm specific key operations: FrodoKEM
1352*/
1353
1354int botan_privkey_load_frodokem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* frodo_mode) {
1355#if defined(BOTAN_HAS_FRODOKEM)
1356 if(key == nullptr || privkey == nullptr || frodo_mode == nullptr) {
1358 }
1359
1360 *key = nullptr;
1361
1362 return ffi_guard_thunk(__func__, [=]() -> int {
1363 const auto mode = Botan::FrodoKEMMode(frodo_mode);
1364 auto frodo_key = std::make_unique<Botan::FrodoKEM_PrivateKey>(std::span{privkey, key_len}, mode);
1365 return ffi_new_object(key, std::move(frodo_key));
1366 });
1367#else
1368 BOTAN_UNUSED(key, privkey, key_len, frodo_mode);
1370#endif
1371}
1372
1373int botan_pubkey_load_frodokem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* frodo_mode) {
1374#if defined(BOTAN_HAS_FRODOKEM)
1375 if(key == nullptr || pubkey == nullptr || frodo_mode == nullptr) {
1377 }
1378
1379 *key = nullptr;
1380
1381 return ffi_guard_thunk(__func__, [=]() -> int {
1382 const auto mode = Botan::FrodoKEMMode(frodo_mode);
1383 auto frodo_key = std::make_unique<Botan::FrodoKEM_PublicKey>(std::span{pubkey, key_len}, mode);
1384 return ffi_new_object(key, std::move(frodo_key));
1385 });
1386#else
1387 BOTAN_UNUSED(key, pubkey, key_len, frodo_mode);
1389#endif
1390}
1391
1392/*
1393* Algorithm specific key operations : Classic McEliece
1394*/
1395
1397 const uint8_t privkey[],
1398 size_t key_len,
1399 const char* cmce_mode) {
1400#if defined(BOTAN_HAS_CLASSICMCELIECE)
1401 if(key == nullptr || privkey == nullptr || cmce_mode == nullptr) {
1403 }
1404
1405 *key = nullptr;
1406
1407 return ffi_guard_thunk(__func__, [=]() -> int {
1408 const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
1409 auto cmce_key = std::make_unique<Botan::Classic_McEliece_PrivateKey>(std::span{privkey, key_len}, mode);
1410 return ffi_new_object(key, std::move(cmce_key));
1411 });
1412#else
1413 BOTAN_UNUSED(key, privkey, key_len, cmce_mode);
1415#endif
1416}
1417
1419 const uint8_t pubkey[],
1420 size_t key_len,
1421 const char* cmce_mode) {
1422#if defined(BOTAN_HAS_CLASSICMCELIECE)
1423 if(key == nullptr || pubkey == nullptr || cmce_mode == nullptr) {
1425 }
1426
1427 *key = nullptr;
1428
1429 return ffi_guard_thunk(__func__, [=]() -> int {
1430 const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode);
1431 auto cmce_key = std::make_unique<Botan::Classic_McEliece_PublicKey>(std::span{pubkey, key_len}, mode);
1432 return ffi_new_object(key, std::move(cmce_key));
1433 });
1434#else
1435 BOTAN_UNUSED(key, pubkey, key_len, cmce_mode);
1437#endif
1438}
1439
1441#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
1442 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
1443 if(auto ecc = dynamic_cast<const Botan::EC_PublicKey*>(&k)) {
1444 auto pt = ecc->_public_ec_point().serialize_uncompressed();
1445 return invoke_view_callback(view, ctx, pt);
1446 } else {
1448 }
1449 });
1450#else
1451 BOTAN_UNUSED(key, view, ctx);
1453#endif
1454}
1455
1456// NOLINTEND(misc-misplaced-const)
1457
1458int botan_privkey_create_mceliece(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n, size_t t) {
1459 const std::string mce_params = std::to_string(n) + "," + std::to_string(t);
1460 return botan_privkey_create(key_obj, "McEliece", mce_params.c_str(), rng_obj);
1461}
1462
1464 const char* aead,
1465 const uint8_t ct[],
1466 size_t ct_len,
1467 const uint8_t ad[],
1468 size_t ad_len,
1469 uint8_t out[],
1470 size_t* out_len) {
1471 BOTAN_UNUSED(mce_key_obj, aead, ct, ct_len, ad, ad_len, out, out_len);
1473}
1474
1476 botan_rng_t rng_obj,
1477 const char* aead,
1478 const uint8_t pt[],
1479 size_t pt_len,
1480 const uint8_t ad[],
1481 size_t ad_len,
1482 uint8_t out[],
1483 size_t* out_len) {
1484 BOTAN_UNUSED(mce_key_obj, rng_obj, aead, pt, pt_len, ad, ad_len, out, out_len);
1486}
1487}
#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 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: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: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:1558
struct botan_privkey_struct * botan_privkey_t
Definition ffi.h:1325
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:1419
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:1418
@ 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_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_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_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_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: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:261
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:265
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:67