11#include <botan/hash.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>
18#if defined(BOTAN_HAS_DL_GROUP)
19 #include <botan/dl_group.h>
22#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
23 #include <botan/ecc_key.h>
26#if defined(BOTAN_HAS_RSA)
27 #include <botan/rsa.h>
30#if defined(BOTAN_HAS_ELGAMAL)
31 #include <botan/elgamal.h>
34#if defined(BOTAN_HAS_DSA)
35 #include <botan/dsa.h>
38#if defined(BOTAN_HAS_ECDSA)
39 #include <botan/ecdsa.h>
42#if defined(BOTAN_HAS_SM2)
43 #include <botan/sm2.h>
46#if defined(BOTAN_HAS_ECDH)
47 #include <botan/ecdh.h>
50#if defined(BOTAN_HAS_X25519)
51 #include <botan/x25519.h>
54#if defined(BOTAN_HAS_X448)
55 #include <botan/x448.h>
58#if defined(BOTAN_HAS_ED25519)
59 #include <botan/ed25519.h>
62#if defined(BOTAN_HAS_ED448)
63 #include <botan/ed448.h>
66#if defined(BOTAN_HAS_MCELIECE)
67 #include <botan/mceliece.h>
70#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
74#if defined(BOTAN_HAS_KYBER)
75 #include <botan/kyber.h>
80#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
84template <
class ECPrivateKey_t>
85int privkey_load_ec(std::unique_ptr<ECPrivateKey_t>& key,
const Botan::BigInt& scalar,
const char* curve_name) {
86 if(curve_name ==
nullptr) {
92 key.reset(
new ECPrivateKey_t(null_rng, grp, scalar));
96template <
class ECPublicKey_t>
97int pubkey_load_ec(std::unique_ptr<ECPublicKey_t>& key,
100 const char* curve_name) {
101 if(curve_name ==
nullptr) {
107 key.reset(
new ECPublicKey_t(grp, uncompressed_point));
114#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
117 if(field ==
"public_x") {
118 return ecc->public_point().get_affine_x();
119 }
else if(field ==
"public_y") {
120 return ecc->public_point().get_affine_y();
133#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
136 if(field ==
"public_x") {
137 return ecc->public_point().get_affine_x();
138 }
else if(field ==
"public_y") {
139 return ecc->public_point().get_affine_y();
158 if(field_name_cstr ==
nullptr) {
162 const std::string field_name(field_name_cstr);
168 if(field_name_cstr ==
nullptr) {
172 const std::string field_name(field_name_cstr);
180 if(n_bits < 1024 || n_bits > 16 * 1024) {
184 std::string n_str = std::to_string(n_bits);
190#if defined(BOTAN_HAS_RSA)
195 *key =
new botan_privkey_struct(std::move(rsa));
205#if defined(BOTAN_HAS_RSA)
211 auto rsa = std::make_unique<Botan::RSA_PrivateKey>(alg_id, src);
212 *key =
new botan_privkey_struct(std::move(rsa));
222#if defined(BOTAN_HAS_RSA)
225 auto rsa = std::make_unique<Botan::RSA_PublicKey>(
safe_get(n),
safe_get(e));
226 *key =
new botan_pubkey_struct(std::move(rsa));
264#if defined(BOTAN_HAS_RSA)
285#if defined(BOTAN_HAS_DSA)
287 if((rng_obj ==
nullptr) || (key ==
nullptr)) {
291 if((pbits % 64) || (qbits % 8) || (pbits < 1024) || (pbits > 3072) || (qbits < 160) || (qbits > 256)) {
298 auto dsa = std::make_unique<Botan::DSA_PrivateKey>(rng, group);
299 *key =
new botan_privkey_struct(std::move(dsa));
309#if defined(BOTAN_HAS_DSA)
314 auto dsa = std::make_unique<Botan::DSA_PrivateKey>(group,
safe_get(x));
315 *key =
new botan_privkey_struct(std::move(dsa));
325#if defined(BOTAN_HAS_DSA)
330 auto dsa = std::make_unique<Botan::DSA_PublicKey>(group,
safe_get(y));
331 *key =
new botan_pubkey_struct(std::move(dsa));
367#if defined(BOTAN_HAS_ECC_KEY)
372 if(ec_key ==
nullptr) {
387 const char* curve_name) {
388#if defined(BOTAN_HAS_ECDSA)
390 std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
392 int rc = pubkey_load_ec(p_key,
safe_get(public_x),
safe_get(public_y), curve_name);
394 *key =
new botan_pubkey_struct(std::move(p_key));
406#if defined(BOTAN_HAS_ECDSA)
408 std::unique_ptr<Botan::ECDSA_PrivateKey> p_key;
409 int rc = privkey_load_ec(p_key,
safe_get(scalar), curve_name);
411 *key =
new botan_privkey_struct(std::move(p_key));
423#if defined(BOTAN_HAS_ELGAMAL)
425 if((rng_obj ==
nullptr) || (key ==
nullptr)) {
429 if((pbits < 1024) || (qbits < 160)) {
439 auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(rng, group);
440 *key =
new botan_privkey_struct(std::move(elg));
450#if defined(BOTAN_HAS_ELGAMAL)
454 auto elg = std::make_unique<Botan::ElGamal_PublicKey>(group,
safe_get(y));
455 *key =
new botan_pubkey_struct(std::move(elg));
465#if defined(BOTAN_HAS_ELGAMAL)
469 auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(group,
safe_get(x));
470 *key =
new botan_privkey_struct(std::move(elg));
486#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
490 auto dh = std::make_unique<Botan::DH_PrivateKey>(group,
safe_get(x));
491 *key =
new botan_privkey_struct(std::move(dh));
501#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
505 auto dh = std::make_unique<Botan::DH_PublicKey>(group,
safe_get(y));
506 *key =
new botan_pubkey_struct(std::move(dh));
518 if(param_str ==
nullptr) {
522 const std::string params(param_str);
524 if(params ==
"x25519" || params ==
"curve25519") {
528 if(params ==
"x448") {
538 const char* curve_name) {
539#if defined(BOTAN_HAS_ECDH)
541 std::unique_ptr<Botan::ECDH_PublicKey> p_key;
542 int rc = pubkey_load_ec(p_key,
safe_get(public_x),
safe_get(public_y), curve_name);
545 *key =
new botan_pubkey_struct(std::move(p_key));
556#if defined(BOTAN_HAS_ECDH)
558 std::unique_ptr<Botan::ECDH_PrivateKey> p_key;
559 int rc = privkey_load_ec(p_key,
safe_get(scalar), curve_name);
561 *key =
new botan_privkey_struct(std::move(p_key));
574 uint8_t out[],
size_t* out_len,
const char* ident,
const char* hash_algo,
const botan_pubkey_t key) {
575 if(out ==
nullptr || out_len ==
nullptr) {
578 if(ident ==
nullptr || hash_algo ==
nullptr || key ==
nullptr) {
582#if defined(BOTAN_HAS_SM2)
587 if(ec_key ==
nullptr) {
595 const std::string ident_str(ident);
610 const char* curve_name) {
611#if defined(BOTAN_HAS_SM2)
613 std::unique_ptr<Botan::SM2_PublicKey> p_key;
614 if(!pubkey_load_ec(p_key,
safe_get(public_x),
safe_get(public_y), curve_name)) {
615 *key =
new botan_pubkey_struct(std::move(p_key));
627#if defined(BOTAN_HAS_SM2)
629 std::unique_ptr<Botan::SM2_PrivateKey> p_key;
630 int rc = privkey_load_ec(p_key,
safe_get(scalar), curve_name);
633 *key =
new botan_privkey_struct(std::move(p_key));
646 const char* curve_name) {
657#if defined(BOTAN_HAS_ED25519)
661 auto ed25519 = std::make_unique<Botan::Ed25519_PrivateKey>(privkey_vec);
662 *key =
new botan_privkey_struct(std::move(ed25519));
672#if defined(BOTAN_HAS_ED25519)
675 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32);
676 auto ed25519 = std::make_unique<Botan::Ed25519_PublicKey>(pubkey_vec);
677 *key =
new botan_pubkey_struct(std::move(ed25519));
687#if defined(BOTAN_HAS_ED25519)
690 const auto ed_key = ed->raw_private_key_bits();
691 if(ed_key.size() != 64)
706#if defined(BOTAN_HAS_ED25519)
709 const std::vector<uint8_t>& ed_key = ed->get_public_key();
710 if(ed_key.size() != 32)
727#if defined(BOTAN_HAS_ED448)
730 auto ed448 = std::make_unique<Botan::Ed448_PrivateKey>(std::span(privkey, 57));
731 *key =
new botan_privkey_struct(std::move(ed448));
741#if defined(BOTAN_HAS_ED448)
744 auto ed448 = std::make_unique<Botan::Ed448_PublicKey>(std::span(pubkey, 57));
745 *key =
new botan_pubkey_struct(std::move(ed448));
755#if defined(BOTAN_HAS_ED448)
758 const auto ed_key = ed->raw_private_key_bits();
772#if defined(BOTAN_HAS_ED448)
775 const auto ed_key = ed->public_key_bits();
791#if defined(BOTAN_HAS_X25519)
795 auto x25519 = std::make_unique<Botan::X25519_PrivateKey>(privkey_vec);
796 *key =
new botan_privkey_struct(std::move(x25519));
806#if defined(BOTAN_HAS_X25519)
809 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32);
810 auto x25519 = std::make_unique<Botan::X25519_PublicKey>(pubkey_vec);
811 *key =
new botan_pubkey_struct(std::move(x25519));
821#if defined(BOTAN_HAS_X25519)
824 const auto x25519_key = x25519->raw_private_key_bits();
825 if(x25519_key.size() != 32)
840#if defined(BOTAN_HAS_X25519)
843 const std::vector<uint8_t>& x25519_key = x25519->public_value();
844 if(x25519_key.size() != 32)
861#if defined(BOTAN_HAS_X448)
864 auto x448 = std::make_unique<Botan::X448_PrivateKey>(std::span(privkey, 56));
865 *key =
new botan_privkey_struct(std::move(x448));
875#if defined(BOTAN_HAS_X448)
878 auto x448 = std::make_unique<Botan::X448_PublicKey>(std::span(pubkey, 56));
879 *key =
new botan_pubkey_struct(std::move(x448));
889#if defined(BOTAN_HAS_X448)
892 const auto x448_key = x448->raw_private_key_bits();
906#if defined(BOTAN_HAS_X448)
909 const std::vector<uint8_t>& x448_key = x448->public_value();
927#if defined(BOTAN_HAS_KYBER)
934 *key =
new botan_privkey_struct(std::move(kyber512));
941 *key =
new botan_privkey_struct(std::move(kyber768));
948 *key =
new botan_privkey_struct(std::move(kyber1024));
962#if defined(BOTAN_HAS_KYBER)
967 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 800);
969 *key =
new botan_pubkey_struct(std::move(kyber512));
974 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 1184);
976 *key =
new botan_pubkey_struct(std::move(kyber768));
981 const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 1568);
983 *key =
new botan_pubkey_struct(std::move(kyber1024));
997#if defined(BOTAN_HAS_KYBER)
1012#if defined(BOTAN_HAS_KYBER)
1027#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
1043 const std::string mce_params = std::to_string(n) +
"," + std::to_string(t);
1055 BOTAN_UNUSED(mce_key_obj, aead, ct, ct_len, ad, ad_len, out, out_len);
1068 BOTAN_UNUSED(mce_key_obj, rng_obj, aead, pt, pt_len, ad, ad_len, out, out_len);
virtual std::string algo_name() const =0
virtual const BigInt & get_int_field(std::string_view field) const
static EC_Group from_name(std::string_view name)
bool used_explicit_encoding() const
const EC_Group & domain() const
const EC_Point & public_point() const
A private key for Ed448/Ed448ph according to RFC 8032.
A public key for Ed448/Ed448ph according to RFC 8032.
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
A private key for the X448 key agreement scheme according to RFC 7748.
A public key for the X448 key agreement scheme according to RFC 7748.
struct botan_pubkey_struct * botan_pubkey_t
struct botan_privkey_struct * botan_privkey_t
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
int botan_privkey_create(botan_privkey_t *key, const char *algo_name, const char *algo_params, botan_rng_t rng)
#define BOTAN_PRIVKEY_EXPORT_FLAG_PEM
struct botan_mp_struct * botan_mp_t
struct botan_rng_struct * botan_rng_t
#define BOTAN_PRIVKEY_EXPORT_FLAG_DER
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
@ BOTAN_FFI_ERROR_BAD_FLAG
@ BOTAN_FFI_ERROR_NULL_POINTER
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
@ BOTAN_FFI_ERROR_BAD_PARAMETER
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_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_sm2_compute_za(uint8_t out[], size_t *out_len, const char *ident, const char *hash_algo, const botan_pubkey_t key)
int botan_privkey_load_x25519(botan_privkey_t *key, const uint8_t privkey[32])
int botan_pubkey_dsa_get_p(botan_mp_t p, botan_pubkey_t key)
int botan_privkey_rsa_get_q(botan_mp_t q, botan_privkey_t key)
int botan_pubkey_ecc_key_used_explicit_encoding(botan_pubkey_t key)
int botan_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_privkey_load_ed448(botan_privkey_t *key, const uint8_t privkey[57])
int botan_pubkey_view_ec_public_point(const botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_privkey_load_dh(botan_privkey_t *key, botan_mp_t p, botan_mp_t g, botan_mp_t x)
int botan_privkey_load_sm2(botan_privkey_t *key, const botan_mp_t scalar, const char *curve_name)
int botan_pubkey_load_ed25519(botan_pubkey_t *key, const uint8_t pubkey[32])
int botan_privkey_load_ecdsa(botan_privkey_t *key, const botan_mp_t scalar, const char *curve_name)
int botan_mceies_encrypt(botan_pubkey_t mce_key_obj, botan_rng_t rng_obj, const char *aead, const uint8_t pt[], size_t pt_len, const uint8_t ad[], size_t ad_len, uint8_t out[], size_t *out_len)
int botan_pubkey_x25519_get_pubkey(botan_pubkey_t key, uint8_t output[32])
int botan_pubkey_load_elgamal(botan_pubkey_t *key, botan_mp_t p, botan_mp_t g, botan_mp_t y)
int botan_privkey_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_create_ecdh(botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
int botan_privkey_rsa_get_d(botan_mp_t d, botan_privkey_t key)
int botan_pubkey_load_sm2_enc(botan_pubkey_t *key, const botan_mp_t public_x, const botan_mp_t public_y, const char *curve_name)
int botan_mceies_decrypt(botan_privkey_t mce_key_obj, const char *aead, const uint8_t ct[], size_t ct_len, const uint8_t ad[], size_t ad_len, uint8_t out[], size_t *out_len)
int botan_pubkey_load_x25519(botan_pubkey_t *key, const uint8_t pubkey[32])
int botan_privkey_load_elgamal(botan_privkey_t *key, botan_mp_t p, botan_mp_t g, botan_mp_t x)
int botan_privkey_create_rsa(botan_privkey_t *key_obj, botan_rng_t rng_obj, size_t n_bits)
int botan_pubkey_load_dsa(botan_pubkey_t *key, botan_mp_t p, botan_mp_t q, botan_mp_t g, botan_mp_t y)
int botan_pubkey_load_kyber(botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len)
int botan_pubkey_ed448_get_pubkey(botan_pubkey_t key, uint8_t output[57])
int botan_privkey_create_dh(botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
int botan_privkey_load_dsa(botan_privkey_t *key, botan_mp_t p, botan_mp_t q, botan_mp_t g, botan_mp_t x)
int botan_privkey_rsa_get_n(botan_mp_t n, botan_privkey_t key)
int botan_privkey_load_kyber(botan_privkey_t *key, const uint8_t privkey[], size_t key_len)
int botan_pubkey_rsa_get_e(botan_mp_t e, botan_pubkey_t key)
int botan_pubkey_dsa_get_g(botan_mp_t g, botan_pubkey_t key)
int botan_pubkey_dsa_get_q(botan_mp_t q, botan_pubkey_t key)
int botan_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_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)
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
T & safe_get(botan_struct< T, M > *p)
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, const std::vector< uint8_t, Alloc > &buf)
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
std::vector< uint8_t > sm2_compute_za(HashFunction &hash, std::string_view user_id, const EC_Group &domain, const EC_Point &pubkey)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void copy_mem(T *out, const T *in, size_t n)