Botan 3.13.0
Crypto and TLS for C&
ffi_pkey_algs.cpp File Reference
#include <botan/ffi.h>
#include <botan/assert.h>
#include <botan/ec_group.h>
#include <botan/hash.h>
#include <botan/mem_ops.h>
#include <botan/pem.h>
#include <botan/internal/ffi_ec.h>
#include <botan/internal/ffi_mp.h>
#include <botan/internal/ffi_pkey.h>
#include <botan/internal/ffi_rng.h>
#include <botan/internal/ffi_util.h>

Go to the source code of this file.

Functions

int botan_ec_privkey_get_group (botan_privkey_t key, botan_ec_group_t *ec_group)
int botan_ec_privkey_get_private_key (botan_privkey_t key, botan_ec_scalar_t *value)
int botan_ec_pubkey_get_group (botan_pubkey_t key, botan_ec_group_t *ec_group)
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_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_privkey_create_dh (botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
int botan_privkey_create_dsa (botan_privkey_t *key, botan_rng_t rng_obj, size_t pbits, size_t qbits)
int botan_privkey_create_ecdh (botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
int botan_privkey_create_ecdsa (botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
int botan_privkey_create_elgamal (botan_privkey_t *key, botan_rng_t rng_obj, size_t pbits, size_t qbits)
int botan_privkey_create_mceliece (botan_privkey_t *key_obj, botan_rng_t rng_obj, size_t n, size_t t)
int botan_privkey_create_rsa (botan_privkey_t *key_obj, botan_rng_t rng_obj, size_t n_bits)
int botan_privkey_dsa_get_x (botan_mp_t x, botan_privkey_t key)
int botan_privkey_ed25519_get_privkey (botan_privkey_t key, uint8_t output[64])
int botan_privkey_ed448_get_privkey (botan_privkey_t key, uint8_t output[57])
int botan_privkey_get_field (botan_mp_t output, botan_privkey_t key, const char *field_name_cstr)
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_dh (botan_privkey_t *key, botan_mp_t p, botan_mp_t g, botan_mp_t x)
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_load_ecdh (botan_privkey_t *key, const botan_mp_t scalar, const char *curve_name)
int botan_privkey_load_ecdsa (botan_privkey_t *key, const botan_mp_t scalar, const char *curve_name)
int botan_privkey_load_ed25519 (botan_privkey_t *key, const uint8_t privkey[32])
int botan_privkey_load_ed448 (botan_privkey_t *key, const uint8_t privkey[57])
int botan_privkey_load_elgamal (botan_privkey_t *key, botan_mp_t p, botan_mp_t g, botan_mp_t x)
int botan_privkey_load_frodokem (botan_privkey_t *key, const uint8_t privkey[], size_t key_len, const char *frodo_mode)
int botan_privkey_load_kyber (botan_privkey_t *key, const uint8_t privkey[], size_t key_len)
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_load_ml_kem (botan_privkey_t *key, const uint8_t privkey[], size_t key_len, const char *mlkem_mode)
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_privkey_load_rsa_pkcs1 (botan_privkey_t *key, const uint8_t bits[], size_t len)
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_load_sm2 (botan_privkey_t *key, const botan_mp_t scalar, const char *curve_name)
int botan_privkey_load_sm2_enc (botan_privkey_t *key, const botan_mp_t scalar, const char *curve_name)
int botan_privkey_load_x25519 (botan_privkey_t *key, const uint8_t privkey[32])
int botan_privkey_load_x448 (botan_privkey_t *key, const uint8_t privkey[56])
int botan_privkey_rsa_get_d (botan_mp_t d, botan_privkey_t key)
int botan_privkey_rsa_get_e (botan_mp_t e, botan_privkey_t key)
int botan_privkey_rsa_get_n (botan_mp_t n, botan_privkey_t key)
int botan_privkey_rsa_get_p (botan_mp_t p, botan_privkey_t key)
int botan_privkey_rsa_get_privkey (botan_privkey_t rsa_key, uint8_t out[], size_t *out_len, uint32_t flags)
int botan_privkey_rsa_get_q (botan_mp_t q, botan_privkey_t key)
int botan_privkey_view_kyber_raw_key (botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_privkey_x25519_get_privkey (botan_privkey_t key, uint8_t output[32])
int botan_privkey_x448_get_privkey (botan_privkey_t key, uint8_t output[56])
int botan_pubkey_dsa_get_g (botan_mp_t g, botan_pubkey_t key)
int botan_pubkey_dsa_get_p (botan_mp_t p, botan_pubkey_t key)
int botan_pubkey_dsa_get_q (botan_mp_t q, botan_pubkey_t key)
int botan_pubkey_dsa_get_y (botan_mp_t y, botan_pubkey_t key)
int botan_pubkey_ecc_key_used_explicit_encoding (botan_pubkey_t key)
int botan_pubkey_ed25519_get_pubkey (botan_pubkey_t key, uint8_t output[32])
int botan_pubkey_ed448_get_pubkey (botan_pubkey_t key, uint8_t output[57])
int botan_pubkey_get_field (botan_mp_t output, botan_pubkey_t key, const char *field_name_cstr)
int botan_pubkey_load_classic_mceliece (botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len, const char *cmce_mode)
int botan_pubkey_load_dh (botan_pubkey_t *key, botan_mp_t p, botan_mp_t g, botan_mp_t y)
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_ecdh (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_sec1 (botan_pubkey_t *key, const uint8_t sec1[], size_t sec1_len, const char *curve_name)
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_ecdsa_sec1 (botan_pubkey_t *key, const uint8_t sec1[], size_t sec1_len, const char *curve_name)
int botan_pubkey_load_ed25519 (botan_pubkey_t *key, const uint8_t pubkey[32])
int botan_pubkey_load_ed448 (botan_pubkey_t *key, const uint8_t pubkey[57])
int botan_pubkey_load_elgamal (botan_pubkey_t *key, botan_mp_t p, botan_mp_t g, botan_mp_t y)
int botan_pubkey_load_frodokem (botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len, const char *frodo_mode)
int botan_pubkey_load_kyber (botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len)
int botan_pubkey_load_ml_dsa (botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len, const char *mldsa_mode)
int botan_pubkey_load_ml_kem (botan_pubkey_t *key, const uint8_t pubkey[], size_t key_len, const char *mlkem_mode)
int botan_pubkey_load_rsa (botan_pubkey_t *key, botan_mp_t n, botan_mp_t e)
int botan_pubkey_load_rsa_pkcs1 (botan_pubkey_t *key, const uint8_t bits[], size_t len)
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_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_sm2_enc (botan_pubkey_t *key, const botan_mp_t public_x, const botan_mp_t public_y, const char *curve_name)
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_load_x25519 (botan_pubkey_t *key, const uint8_t pubkey[32])
int botan_pubkey_load_x448 (botan_pubkey_t *key, const uint8_t pubkey[56])
int botan_pubkey_rsa_get_e (botan_mp_t e, botan_pubkey_t key)
int botan_pubkey_rsa_get_n (botan_mp_t n, botan_pubkey_t key)
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_view_ec_public_point (const botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_pubkey_view_kyber_raw_key (botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_pubkey_x25519_get_pubkey (botan_pubkey_t key, uint8_t output[32])
int botan_pubkey_x448_get_pubkey (botan_pubkey_t key, uint8_t output[56])

Function Documentation

◆ botan_ec_privkey_get_group()

int botan_ec_privkey_get_group ( botan_privkey_t key,
botan_ec_group_t * ec_group )

Get the group of an elliptic curve private key

Parameters
keythe EC private key
EC Groupthe new object will be placed here
Returns
0 on success, a negative value on failure

Definition at line 859 of file ffi_pkey_algs.cpp.

859 {
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}
#define BOTAN_UNUSED
Definition assert.h:144
const EC_Group & domain() const
Definition ecc_key.cpp:76
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:132
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
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan::EC_PublicKey::domain(), Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_ec_privkey_get_private_key()

int botan_ec_privkey_get_private_key ( botan_privkey_t key,
botan_ec_scalar_t * value )

Loads Diffie Hellman private key

Parameters
keyvariable populated with key material
pprime order of a Z_p group
ggroup generator
xprivate key
Precondition
key is NULL on input
Postcondition
function allocates memory and assigns to `key'
Returns
0 on success, a negative value on failure */ int botan_privkey_load_dh(botan_privkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t x); /** Loads Diffie Hellman public key
Parameters
keyvariable populated with key material
pprime order of a Z_p group
ggroup generator
ypublic key
Precondition
key is NULL on input
Postcondition
function allocates memory and assigns to `key'
Returns
0 on success, a negative value on failure */ int botan_pubkey_load_dh(botan_pubkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t y);

/* Algorithm specific key operations: ElGamal Loads ElGamal public key

Parameters
keyvariable populated with key material
pprime order of a Z_p group
ggroup generator
ypublic key
Precondition
key is NULL on input
Postcondition
function allocates memory and assigns to `key'
Returns
0 on success, a negative value on failure */ int botan_pubkey_load_elgamal(botan_pubkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t y);

/** Loads ElGamal private key

Parameters
keyvariable populated with key material
pprime order of a Z_p group
ggroup generator
xprivate key
Precondition
key is NULL on input
Postcondition
function allocates memory and assigns to `key'
Returns
0 on success, a negative value on failure */ int botan_privkey_load_elgamal(botan_privkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t x);

/* Algorithm specific key operations: EC keys Get the secret scalar of an elliptic curve private key

Parameters
keythe EC private key
valuethe new object will be placed here
Returns
0 on success, a negative value on failure

Definition at line 841 of file ffi_pkey_algs.cpp.

841 {
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}
const EC_Scalar & _private_key() const
Definition ecc_key.cpp:135

References Botan::EC_PrivateKey::_private_key(), Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_ec_pubkey_get_group()

int botan_ec_pubkey_get_group ( botan_pubkey_t key,
botan_ec_group_t * ec_group )

Get the group of an elliptic curve public key

Parameters
keythe EC public key
EC Groupthe new object will be placed here
Returns
0 on success, a negative value on failure

Definition at line 878 of file ffi_pkey_algs.cpp.

878 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan::EC_PublicKey::domain(), Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_mceies_decrypt()

int botan_mceies_decrypt ( botan_privkey_t mce_key,
const char * aead,
const uint8_t ct[],
size_t ct_len,
const uint8_t ad[],
size_t ad_len,
uint8_t pt[],
size_t * pt_len )

Formerly decrypted a message using McEliece with an AEAD.

Always returns BOTAN_FFI_ERROR_NOT_IMPLEMENTED

Parameters
mce_keyignored
AEAD Modesignored
ctignored
ct_lenignored
adignored
ad_lenignored
ptignored
pt_lenignored
Returns
BOTAN_FFI_ERROR_NOT_IMPLEMENTED

Definition at line 1564 of file ffi_pkey_algs.cpp.

1571 {
1572 BOTAN_UNUSED(mce_key_obj, aead, ct, ct_len, ad, ad_len, out, out_len);
1574}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, and BOTAN_UNUSED.

◆ botan_mceies_encrypt()

int botan_mceies_encrypt ( botan_pubkey_t mce_key,
botan_rng_t rng,
const char * aead,
const uint8_t pt[],
size_t pt_len,
const uint8_t ad[],
size_t ad_len,
uint8_t ct[],
size_t * ct_len )

Formerly encrypted a message using McEliece with an AEAD.

Always returns BOTAN_FFI_ERROR_NOT_IMPLEMENTED

Parameters
mce_keyignored
Random Number Generatorsignored
AEAD Modesignored
ptignored
pt_lenignored
adignored
ad_lenignored
ctignored
ct_lenignored
Returns
BOTAN_FFI_ERROR_NOT_IMPLEMENTED

Definition at line 1576 of file ffi_pkey_algs.cpp.

1584 {
1585 BOTAN_UNUSED(mce_key_obj, rng_obj, aead, pt, pt_len, ad, ad_len, out, out_len);
1587}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, and BOTAN_UNUSED.

◆ botan_privkey_create_dh()

int botan_privkey_create_dh ( botan_privkey_t * key,
botan_rng_t rng,
const char * param )

Create a new Diffie-Hellman private key

Parameters
keythe new object will be placed here
Random Number Generatorsa random number generator
paramthe name of the group, eg "modp/ietf/2048"
Returns
0 on success, a negative value on failure

Definition at line 598 of file ffi_pkey_algs.cpp.

598 {
599 return botan_privkey_create(key_obj, "DH", param_str, rng_obj);
600}
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

References botan_privkey_create().

◆ botan_privkey_create_dsa()

int botan_privkey_create_dsa ( botan_privkey_t * key,
botan_rng_t rng_obj,
size_t pbits,
size_t qbits )

Definition at line 360 of file ffi_pkey_algs.cpp.

360 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan::DL_Group::Prime_Subgroup, and Botan_FFI::safe_get().

◆ botan_privkey_create_ecdh()

int botan_privkey_create_ecdh ( botan_privkey_t * key,
botan_rng_t rng,
const char * params )

Create a new ECDH private key

Parameters
keythe new object will be placed here
Random Number Generatorsa random number generator
paramsthe name of the curve, eg "secp256r1"
Returns
0 on success, a negative value on failure

Definition at line 638 of file ffi_pkey_algs.cpp.

638 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NULL_POINTER, and botan_privkey_create().

◆ botan_privkey_create_ecdsa()

int botan_privkey_create_ecdsa ( botan_privkey_t * key,
botan_rng_t rng,
const char * params )

Create a new ECDSA private key

Parameters
keythe new object will be placed here
Random Number Generatorsa random number generator
paramsthe name of the curve, eg "secp256r1"
Returns
0 on success, a negative value on failure

Definition at line 439 of file ffi_pkey_algs.cpp.

439 {
440 return botan_privkey_create(key_obj, "ECDSA", param_str, rng_obj);
441}

References botan_privkey_create().

◆ botan_privkey_create_elgamal()

int botan_privkey_create_elgamal ( botan_privkey_t * key,
botan_rng_t rng_obj,
size_t pbits,
size_t qbits )

Definition at line 536 of file ffi_pkey_algs.cpp.

536 {
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 =
548 ((pbits - 1) == qbits) ? Botan::DL_Group::Strong : Botan::DL_Group::Prime_Subgroup;
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan::DL_Group::Prime_Subgroup, Botan_FFI::safe_get(), and Botan::DL_Group::Strong.

◆ botan_privkey_create_mceliece()

int botan_privkey_create_mceliece ( botan_privkey_t * key,
botan_rng_t rng,
size_t n,
size_t t )

Create a new McEliece private key

Parameters
keythe new object will be placed here
Random Number Generatorsa random number generator
nthe code length
tthe error correction capability
Returns
0 on success, a negative value on failure

Definition at line 1559 of file ffi_pkey_algs.cpp.

1559 {
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}

References botan_privkey_create().

◆ botan_privkey_create_rsa()

int botan_privkey_create_rsa ( botan_privkey_t * key,
botan_rng_t rng,
size_t n_bits )

Create a new RSA private key

Parameters
keythe new object will be placed here
Random Number Generatorsa random number generator
n_bitsthe bit length of the modulus
Returns
0 on success, a negative value on failure

Definition at line 228 of file ffi_pkey_algs.cpp.

228 {
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}

References BOTAN_FFI_ERROR_BAD_PARAMETER, and botan_privkey_create().

◆ botan_privkey_dsa_get_x()

int botan_privkey_dsa_get_x ( botan_mp_t n,
botan_privkey_t key )

Get the secret value of a DSA private key

Parameters
nset to the value of the private key
keythe DSA private key
Returns
0 on success, a negative value on failure

Definition at line 419 of file ffi_pkey_algs.cpp.

419 {
420 return botan_privkey_get_field(x, key, "x");
421}
int botan_privkey_get_field(botan_mp_t output, botan_privkey_t key, const char *field_name_cstr)

References botan_privkey_get_field().

◆ botan_privkey_ed25519_get_privkey()

int botan_privkey_ed25519_get_privkey ( botan_privkey_t key,
uint8_t output[64] )

Get the raw encoding of an Ed25519 private key, followed by the public key

Parameters
keythe Ed25519 private key
outputset to the 64 byte concatenation of the private and public keys
Returns
0 on success, a negative value on failure

Definition at line 932 of file ffi_pkey_algs.cpp.

932 {
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}
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:122
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan::copy_mem().

◆ botan_privkey_ed448_get_privkey()

int botan_privkey_ed448_get_privkey ( botan_privkey_t key,
uint8_t output[57] )

Get the raw 57 byte encoding of an Ed448 private key

Parameters
keythe Ed448 private key
outputset to the raw private key
Returns
0 on success, a negative value on failure

Definition at line 1012 of file ffi_pkey_algs.cpp.

1012 {
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}
A private key for Ed448/Ed448ph according to RFC 8032.
Definition ed448.h:87

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan::copy_mem().

◆ botan_privkey_get_field()

int botan_privkey_get_field ( botan_mp_t output,
botan_privkey_t key,
const char * field_name )

Get an arbitrary named field from a private key, eg "p" or "q" for RSA

Parameters
outputset to the value of the requested field
keythe private key to query
field_namethe name of the field to retrieve
Returns
0 on success, a negative value on failure

Definition at line 216 of file ffi_pkey_algs.cpp.

216 {
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}

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

Referenced by botan_privkey_dsa_get_x(), botan_privkey_rsa_get_d(), botan_privkey_rsa_get_e(), botan_privkey_rsa_get_n(), botan_privkey_rsa_get_p(), and botan_privkey_rsa_get_q().

◆ botan_privkey_load_classic_mceliece()

int botan_privkey_load_classic_mceliece ( botan_privkey_t * key,
const uint8_t privkey[],
size_t key_len,
const char * cmce_mode )

Load a Classic McEliece private key from its raw encoding

Parameters
keythe new object will be placed here
privkeythe raw private key
key_lenlength of privkey in bytes
cmce_modethe parameter set, eg "348864"
Returns
0 on success, a negative value on failure

Definition at line 1497 of file ffi_pkey_algs.cpp.

1500 {
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}
static Classic_McEliece_Parameter_Set from_string(std::string_view param_name)
Get the parameter set for a given parameter set name.

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan::Classic_McEliece_Parameter_Set::from_string().

◆ botan_privkey_load_dh()

int botan_privkey_load_dh ( botan_privkey_t * key,
botan_mp_t p,
botan_mp_t g,
botan_mp_t x )

Definition at line 602 of file ffi_pkey_algs.cpp.

602 {
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}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_privkey_load_dsa()

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 )

Load a DSA private key from its group parameters and secret value

Parameters
keythe new object will be placed here
pthe group prime
qthe order of the subgroup
gthe subgroup generator
xthe private key
Returns
0 on success, a negative value on failure

Definition at line 383 of file ffi_pkey_algs.cpp.

383 {
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}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_privkey_load_ecdh()

int botan_privkey_load_ecdh ( botan_privkey_t * key,
botan_mp_t scalar,
const char * curve_name )

Load an ECDH private key from its secret scalar

Parameters
keythe new object will be placed here
scalarthe private scalar
curve_namethe name of the curve, eg "secp256r1"
Returns
0 on success, a negative value on failure

Definition at line 704 of file ffi_pkey_algs.cpp.

704 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_privkey_load_ecdsa()

int botan_privkey_load_ecdsa ( botan_privkey_t * key,
botan_mp_t scalar,
const char * curve_name )

Load an ECDSA private key from its secret scalar

Parameters
keythe new object will be placed here
scalarthe private scalar
curve_namethe name of the curve, eg "secp256r1"
Returns
0 on success, a negative value on failure

Definition at line 514 of file ffi_pkey_algs.cpp.

514 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_privkey_load_ed25519()

int botan_privkey_load_ed25519 ( botan_privkey_t * key,
const uint8_t privkey[32] )

Load an Ed25519 private key from its raw 32 byte encoding

Parameters
keythe new object will be placed here
privkeythe raw private key
Returns
0 on success, a negative value on failure

Definition at line 898 of file ffi_pkey_algs.cpp.

898 {
899#if defined(BOTAN_HAS_ED25519)
900 if(any_null_pointers(key, privkey)) {
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}
static Ed25519_PrivateKey from_seed(std::span< const uint8_t > seed)
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan::Ed25519_PrivateKey::from_seed().

◆ botan_privkey_load_ed448()

int botan_privkey_load_ed448 ( botan_privkey_t * key,
const uint8_t privkey[57] )

Load an Ed448 private key from its raw 57 byte encoding

Parameters
keythe new object will be placed here
privkeythe raw private key
Returns
0 on success, a negative value on failure

Definition at line 980 of file ffi_pkey_algs.cpp.

980 {
981#if defined(BOTAN_HAS_ED448)
982 if(any_null_pointers(key, privkey)) {
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}

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_privkey_load_elgamal()

int botan_privkey_load_elgamal ( botan_privkey_t * key,
botan_mp_t p,
botan_mp_t g,
botan_mp_t x )

Definition at line 579 of file ffi_pkey_algs.cpp.

579 {
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}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_privkey_load_frodokem()

int botan_privkey_load_frodokem ( botan_privkey_t * key,
const uint8_t privkey[],
size_t key_len,
const char * frodo_mode )

Load a FrodoKEM private key from its raw encoding

Parameters
keythe new object will be placed here
privkeythe raw private key
key_lenlength of privkey in bytes
frodo_modethe parameter set, eg "FrodoKEM-640-SHAKE"
Returns
0 on success, a negative value on failure

Definition at line 1455 of file ffi_pkey_algs.cpp.

1455 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_privkey_load_kyber()

int botan_privkey_load_kyber ( botan_privkey_t * key,
const uint8_t privkey[],
size_t key_len )

Load a Kyber private key from its raw encoding. The parameter set is inferred from the key length.

Parameters
keythe new object will be placed here
privkeythe raw private key
key_lenlength of privkey in bytes
Returns
0 on success, a negative value on failure

Definition at line 1205 of file ffi_pkey_algs.cpp.

1205 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan::KyberMode::Kyber1024_R3, Botan::KyberMode::Kyber512_R3, and Botan::KyberMode::Kyber768_R3.

◆ botan_privkey_load_ml_dsa()

int botan_privkey_load_ml_dsa ( botan_privkey_t * key,
const uint8_t privkey[],
size_t key_len,
const char * mldsa_mode )

Load an ML-DSA private key from its raw encoding

Parameters
keythe new object will be placed here
privkeythe raw private key
key_lenlength of privkey in bytes
mldsa_modethe parameter set, eg "ML-DSA-6x5"
Returns
0 on success, a negative value on failure

Definition at line 1355 of file ffi_pkey_algs.cpp.

1355 {
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}
DilithiumMode ML_DSA_Mode
Definition ml_dsa.h:21

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_privkey_load_ml_kem()

int botan_privkey_load_ml_kem ( botan_privkey_t * key,
const uint8_t privkey[],
size_t key_len,
const char * mlkem_mode )

Load an ML-KEM private key from its raw encoding

Parameters
keythe new object will be placed here
privkeythe raw private key
key_lenlength of privkey in bytes
mlkem_modethe parameter set, eg "ML-KEM-768"
Returns
0 on success, a negative value on failure

Definition at line 1305 of file ffi_pkey_algs.cpp.

1305 {
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}
KyberMode ML_KEM_Mode
Definition ml_kem.h:21

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_privkey_load_rsa()

int botan_privkey_load_rsa ( botan_privkey_t * key,
botan_mp_t p,
botan_mp_t q,
botan_mp_t e )

Load an RSA private key from its factors and public exponent

Parameters
keythe new object will be placed here
pthe first prime factor
qthe second prime factor
ethe public exponent
Returns
0 on success, a negative value on failure

Definition at line 238 of file ffi_pkey_algs.cpp.

238 {
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}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_privkey_load_rsa_pkcs1()

int botan_privkey_load_rsa_pkcs1 ( botan_privkey_t * key,
const uint8_t bits[],
size_t len )

Load an RSA private key from a PKCS #1 RSAPrivateKey structure

Parameters
keythe new object will be placed here
bitsthe DER encoding to load
lenlength of bits in bytes
Returns
0 on success, a negative value on failure

Definition at line 255 of file ffi_pkey_algs.cpp.

255 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan::AlgorithmIdentifier::USE_NULL_PARAM.

◆ botan_privkey_load_slh_dsa()

int botan_privkey_load_slh_dsa ( botan_privkey_t * key,
const uint8_t privkey[],
size_t key_len,
const char * slhdsa_mode )

Load an SLH-DSA private key from its raw encoding

Parameters
keythe new object will be placed here
privkeythe raw private key
key_lenlength of privkey in bytes
slhdsa_modethe parameter set, eg "SLH-DSA-SHA2-128s"
Returns
0 on success, a negative value on failure

Definition at line 1405 of file ffi_pkey_algs.cpp.

1405 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_privkey_load_sm2()

int botan_privkey_load_sm2 ( botan_privkey_t * key,
botan_mp_t scalar,
const char * curve_name )

Load an SM2 private key from its secret scalar

Parameters
keythe new object will be placed here
scalarthe private scalar
curve_namethe name of the curve, typically "sm2p256v1"
Returns
0 on success, a negative value on failure

Definition at line 806 of file ffi_pkey_algs.cpp.

806 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

Referenced by botan_privkey_load_sm2_enc().

◆ botan_privkey_load_sm2_enc()

int botan_privkey_load_sm2_enc ( botan_privkey_t * key,
botan_mp_t scalar,
const char * curve_name )

Load an SM2 encryption private key; identical to botan_privkey_load_sm2

Parameters
keythe new object will be placed here
scalarthe private scalar
curve_namethe name of the curve, typically "sm2p256v1"
Returns
0 on success, a negative value on failure

Definition at line 835 of file ffi_pkey_algs.cpp.

835 {
836 return botan_privkey_load_sm2(key, scalar, curve_name);
837}
int botan_privkey_load_sm2(botan_privkey_t *key, const botan_mp_t scalar, const char *curve_name)

References botan_privkey_load_sm2().

◆ botan_privkey_load_x25519()

int botan_privkey_load_x25519 ( botan_privkey_t * key,
const uint8_t privkey[32] )

Load an X25519 private key from its raw 32 byte encoding

Parameters
keythe new object will be placed here
privkeythe raw private key
Returns
0 on success, a negative value on failure

Definition at line 1054 of file ffi_pkey_algs.cpp.

1054 {
1055#if defined(BOTAN_HAS_X25519)
1056 if(any_null_pointers(key, privkey)) {
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}

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_privkey_load_x448()

int botan_privkey_load_x448 ( botan_privkey_t * key,
const uint8_t privkey[56] )

Load an X448 private key from its raw 56 byte encoding

Parameters
keythe new object will be placed here
privkeythe raw private key
Returns
0 on success, a negative value on failure

Definition at line 1130 of file ffi_pkey_algs.cpp.

1130 {
1131#if defined(BOTAN_HAS_X448)
1132 if(any_null_pointers(key, privkey)) {
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}
Point448 x448(const ScalarX448 &k, const Point448 &u)
Multiply a scalar k with a point u.

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_privkey_rsa_get_d()

int botan_privkey_rsa_get_d ( botan_mp_t d,
botan_privkey_t rsa_key )

Get the private exponent of an RSA private key

Parameters
dset to the value of the private exponent
rsa_keythe RSA private key
Returns
0 on success, a negative value on failure

Definition at line 323 of file ffi_pkey_algs.cpp.

323 {
324 return botan_privkey_get_field(d, key, "d");
325}

References botan_privkey_get_field().

◆ botan_privkey_rsa_get_e()

int botan_privkey_rsa_get_e ( botan_mp_t e,
botan_privkey_t rsa_key )

Get the public exponent of an RSA private key

Parameters
eset to the value of the public exponent
rsa_keythe RSA private key
Returns
0 on success, a negative value on failure

Definition at line 319 of file ffi_pkey_algs.cpp.

319 {
320 return botan_privkey_get_field(e, key, "e");
321}

References botan_privkey_get_field().

◆ botan_privkey_rsa_get_n()

int botan_privkey_rsa_get_n ( botan_mp_t n,
botan_privkey_t rsa_key )

Get the modulus of an RSA private key

Parameters
nset to the value of the modulus
rsa_keythe RSA private key
Returns
0 on success, a negative value on failure

Definition at line 315 of file ffi_pkey_algs.cpp.

315 {
316 return botan_privkey_get_field(n, key, "n");
317}

References botan_privkey_get_field().

◆ botan_privkey_rsa_get_p()

int botan_privkey_rsa_get_p ( botan_mp_t p,
botan_privkey_t rsa_key )

Get the first prime factor of an RSA private key

Parameters
pset to the value of the factor
rsa_keythe RSA private key
Returns
0 on success, a negative value on failure

Definition at line 307 of file ffi_pkey_algs.cpp.

307 {
308 return botan_privkey_get_field(p, key, "p");
309}

References botan_privkey_get_field().

◆ botan_privkey_rsa_get_privkey()

int botan_privkey_rsa_get_privkey ( botan_privkey_t rsa_key,
uint8_t out[],
size_t * out_len,
uint32_t flags )

Export an RSA private key as a PKCS #1 RSAPrivateKey structure

Parameters
rsa_keythe RSA private key
outoutput buffer
out_lenon input the size of out, on output the number of bytes written or required
flagseither BOTAN_PRIVKEY_EXPORT_FLAG_DER or BOTAN_PRIVKEY_EXPORT_FLAG_PEM
Returns
0 on success, a negative value on failure

Definition at line 335 of file ffi_pkey_algs.cpp.

335 {
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}
#define BOTAN_PRIVKEY_EXPORT_FLAG_PEM
Definition ffi.h:1920
#define BOTAN_PRIVKEY_EXPORT_FLAG_DER
Definition ffi.h:1919
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:130
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
Definition pem.cpp:39
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:267
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:271

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_VISIT, BOTAN_PRIVKEY_EXPORT_FLAG_DER, BOTAN_PRIVKEY_EXPORT_FLAG_PEM, BOTAN_UNUSED, Botan::PEM_Code::encode(), Botan_FFI::write_str_output(), and Botan_FFI::write_vec_output().

◆ botan_privkey_rsa_get_q()

int botan_privkey_rsa_get_q ( botan_mp_t q,
botan_privkey_t rsa_key )

Get the second prime factor of an RSA private key

Parameters
qset to the value of the factor
rsa_keythe RSA private key
Returns
0 on success, a negative value on failure

Definition at line 311 of file ffi_pkey_algs.cpp.

311 {
312 return botan_privkey_get_field(q, key, "q");
313}

References botan_privkey_get_field().

◆ botan_privkey_view_kyber_raw_key()

int botan_privkey_view_kyber_raw_key ( botan_privkey_t key,
botan_view_ctx ctx,
botan_view_bin_fn view )

View the raw encoding of a Kyber private key

Parameters
keythe Kyber private key
ctxan application context passed to the view function
viewthe view callback which receives the encoding
Returns
0 on success, a negative value on failure

Definition at line 1271 of file ffi_pkey_algs.cpp.

1271 {
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}
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:190

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan_FFI::invoke_view_callback().

◆ botan_privkey_x25519_get_privkey()

int botan_privkey_x25519_get_privkey ( botan_privkey_t key,
uint8_t output[32] )

Get the raw 32 byte encoding of an X25519 private key

Parameters
keythe X25519 private key
outputset to the raw private key
Returns
0 on success, a negative value on failure

Definition at line 1086 of file ffi_pkey_algs.cpp.

1086 {
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}

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan::copy_mem().

◆ botan_privkey_x448_get_privkey()

int botan_privkey_x448_get_privkey ( botan_privkey_t key,
uint8_t output[56] )

Get the raw 56 byte encoding of an X448 private key

Parameters
keythe X448 private key
outputset to the raw private key
Returns
0 on success, a negative value on failure

Definition at line 1162 of file ffi_pkey_algs.cpp.

1162 {
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}
A private key for the X448 key agreement scheme according to RFC 7748.
Definition x448.h:74

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan::copy_mem().

◆ botan_pubkey_dsa_get_g()

int botan_pubkey_dsa_get_g ( botan_mp_t d,
botan_pubkey_t key )

Get the subgroup generator of a DSA public key

Parameters
dset to the value of the generator
keythe DSA public key
Returns
0 on success, a negative value on failure

Definition at line 431 of file ffi_pkey_algs.cpp.

431 {
432 return botan_pubkey_get_field(g, key, "g");
433}
int botan_pubkey_get_field(botan_mp_t output, botan_pubkey_t key, const char *field_name_cstr)

References botan_pubkey_get_field().

◆ botan_pubkey_dsa_get_p()

int botan_pubkey_dsa_get_p ( botan_mp_t p,
botan_pubkey_t key )

Get the group prime of a DSA public key

Parameters
pset to the value of the group prime
keythe DSA public key
Returns
0 on success, a negative value on failure

Definition at line 423 of file ffi_pkey_algs.cpp.

423 {
424 return botan_pubkey_get_field(p, key, "p");
425}

References botan_pubkey_get_field().

◆ botan_pubkey_dsa_get_q()

int botan_pubkey_dsa_get_q ( botan_mp_t q,
botan_pubkey_t key )

Get the subgroup order of a DSA public key

Parameters
qset to the value of the subgroup order
keythe DSA public key
Returns
0 on success, a negative value on failure

Definition at line 427 of file ffi_pkey_algs.cpp.

427 {
428 return botan_pubkey_get_field(q, key, "q");
429}

References botan_pubkey_get_field().

◆ botan_pubkey_dsa_get_y()

int botan_pubkey_dsa_get_y ( botan_mp_t y,
botan_pubkey_t key )

Get the public value of a DSA public key

Parameters
yset to the value of the public key
keythe DSA public key
Returns
0 on success, a negative value on failure

Definition at line 435 of file ffi_pkey_algs.cpp.

435 {
436 return botan_pubkey_get_field(y, key, "y");
437}

References botan_pubkey_get_field().

◆ botan_pubkey_ecc_key_used_explicit_encoding()

int botan_pubkey_ecc_key_used_explicit_encoding ( botan_pubkey_t key)

Check if the group of this EC key was encoded using explicit curve parameters rather than a named curve OID

Parameters
keythe EC public key to examine
Returns
1 if explicit encoding was used, 0 if not, negative on error

Definition at line 445 of file ffi_pkey_algs.cpp.

445 {
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}
bool used_explicit_encoding() const
Definition ec_group.h:305

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_UNUSED, Botan::EC_PublicKey::domain(), Botan_FFI::ffi_guard_thunk(), Botan_FFI::safe_get(), and Botan::EC_Group::used_explicit_encoding().

◆ botan_pubkey_ed25519_get_pubkey()

int botan_pubkey_ed25519_get_pubkey ( botan_pubkey_t key,
uint8_t pubkey[32] )

Get the raw 32 byte encoding of an Ed25519 public key

Parameters
keythe Ed25519 public key
Public Key Algorithmsset to the raw public key
Returns
0 on success, a negative value on failure

Definition at line 955 of file ffi_pkey_algs.cpp.

955 {
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}

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan::copy_mem().

◆ botan_pubkey_ed448_get_pubkey()

int botan_pubkey_ed448_get_pubkey ( botan_pubkey_t key,
uint8_t pubkey[57] )

Get the raw 57 byte encoding of an Ed448 public key

Parameters
keythe Ed448 public key
Public Key Algorithmsset to the raw public key
Returns
0 on success, a negative value on failure

Definition at line 1032 of file ffi_pkey_algs.cpp.

1032 {
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}
A public key for Ed448/Ed448ph according to RFC 8032.
Definition ed448.h:31

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan::copy_mem().

◆ botan_pubkey_get_field()

int botan_pubkey_get_field ( botan_mp_t output,
botan_pubkey_t key,
const char * field_name )

Get an arbitrary named field from a public key, eg "n" or "e" for RSA

Parameters
outputset to the value of the requested field
keythe public key to query
field_namethe name of the field to retrieve
Returns
0 on success, a negative value on failure

Definition at line 206 of file ffi_pkey_algs.cpp.

206 {
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}

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

Referenced by botan_pubkey_dsa_get_g(), botan_pubkey_dsa_get_p(), botan_pubkey_dsa_get_q(), botan_pubkey_dsa_get_y(), botan_pubkey_rsa_get_e(), and botan_pubkey_rsa_get_n().

◆ botan_pubkey_load_classic_mceliece()

int botan_pubkey_load_classic_mceliece ( botan_pubkey_t * key,
const uint8_t pubkey[],
size_t key_len,
const char * cmce_mode )

Load a Classic McEliece public key from its raw encoding

Parameters
keythe new object will be placed here
Public Key Algorithmsthe raw public key
key_lenlength of pubkey in bytes
cmce_modethe parameter set, eg "348864"
Returns
0 on success, a negative value on failure

Definition at line 1519 of file ffi_pkey_algs.cpp.

1522 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan::Classic_McEliece_Parameter_Set::from_string().

◆ botan_pubkey_load_dh()

int botan_pubkey_load_dh ( botan_pubkey_t * key,
botan_mp_t p,
botan_mp_t g,
botan_mp_t y )

Definition at line 619 of file ffi_pkey_algs.cpp.

619 {
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}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_pubkey_load_dsa()

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 )

Load a DSA public key from its group parameters and public value

Parameters
keythe new object will be placed here
pthe group prime
qthe order of the subgroup
gthe subgroup generator
ythe public key
Returns
0 on success, a negative value on failure

Definition at line 401 of file ffi_pkey_algs.cpp.

401 {
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}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_pubkey_load_ecdh()

int botan_pubkey_load_ecdh ( botan_pubkey_t * key,
botan_mp_t public_x,
botan_mp_t public_y,
const char * curve_name )

Load an ECDH public key from the affine coordinates of the public point

Parameters
keythe new object will be placed here
public_xthe x coordinate of the public point
public_ythe y coordinate of the public point
curve_namethe name of the curve, eg "secp256r1"
Returns
0 on success, a negative value on failure

Definition at line 657 of file ffi_pkey_algs.cpp.

660 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_pubkey_load_ecdh_sec1()

int botan_pubkey_load_ecdh_sec1 ( botan_pubkey_t * key,
const uint8_t sec1[],
size_t sec1_len,
const char * curve_name )

Load an ECDH public key from the SEC1 encoding of the public point

Parameters
keythe new object will be placed here
sec1the SEC1 compressed or uncompressed point encoding
sec1_lenlength of sec1 in bytes
curve_namethe name of the curve, eg "secp256r1"
Returns
0 on success, a negative value on failure

Definition at line 681 of file ffi_pkey_algs.cpp.

681 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_pubkey_load_ecdsa()

int botan_pubkey_load_ecdsa ( botan_pubkey_t * key,
botan_mp_t public_x,
botan_mp_t public_y,
const char * curve_name )

Load an ECDSA public key from the affine coordinates of the public point

Parameters
keythe new object will be placed here
public_xthe x coordinate of the public point
public_ythe y coordinate of the public point
curve_namethe name of the curve, eg "secp256r1"
Returns
0 on success, a negative value on failure

Definition at line 465 of file ffi_pkey_algs.cpp.

468 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_pubkey_load_ecdsa_sec1()

int botan_pubkey_load_ecdsa_sec1 ( botan_pubkey_t * key,
const uint8_t sec1[],
size_t sec1_len,
const char * curve_name )

Load an ECDSA public key from the SEC1 encoding of the public point

Parameters
keythe new object will be placed here
sec1the SEC1 compressed or uncompressed point encoding
sec1_lenlength of sec1 in bytes
curve_namethe name of the curve, eg "secp256r1"
Returns
0 on success, a negative value on failure

Definition at line 491 of file ffi_pkey_algs.cpp.

491 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_pubkey_load_ed25519()

int botan_pubkey_load_ed25519 ( botan_pubkey_t * key,
const uint8_t pubkey[32] )

Load an Ed25519 public key from its raw 32 byte encoding

Parameters
keythe new object will be placed here
Public Key Algorithmsthe raw public key
Returns
0 on success, a negative value on failure

Definition at line 915 of file ffi_pkey_algs.cpp.

915 {
916#if defined(BOTAN_HAS_ED25519)
917 if(any_null_pointers(key, pubkey)) {
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}

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_pubkey_load_ed448()

int botan_pubkey_load_ed448 ( botan_pubkey_t * key,
const uint8_t pubkey[57] )

Load an Ed448 public key from its raw 57 byte encoding

Parameters
keythe new object will be placed here
Public Key Algorithmsthe raw public key
Returns
0 on success, a negative value on failure

Definition at line 996 of file ffi_pkey_algs.cpp.

996 {
997#if defined(BOTAN_HAS_ED448)
998 if(any_null_pointers(key, pubkey)) {
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}

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_pubkey_load_elgamal()

int botan_pubkey_load_elgamal ( botan_pubkey_t * key,
botan_mp_t p,
botan_mp_t g,
botan_mp_t y )

Definition at line 562 of file ffi_pkey_algs.cpp.

562 {
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}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_pubkey_load_frodokem()

int botan_pubkey_load_frodokem ( botan_pubkey_t * key,
const uint8_t pubkey[],
size_t key_len,
const char * frodo_mode )

Load a FrodoKEM public key from its raw encoding

Parameters
keythe new object will be placed here
Public Key Algorithmsthe raw public key
key_lenlength of pubkey in bytes
frodo_modethe parameter set, eg "FrodoKEM-640-SHAKE"
Returns
0 on success, a negative value on failure

Definition at line 1474 of file ffi_pkey_algs.cpp.

1474 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_pubkey_load_kyber()

int botan_pubkey_load_kyber ( botan_pubkey_t * key,
const uint8_t pubkey[],
size_t key_len )

Load a Kyber public key from its raw encoding. The parameter set is inferred from the key length.

Parameters
keythe new object will be placed here
Public Key Algorithmsthe raw public key
key_lenlength of pubkey in bytes
Returns
0 on success, a negative value on failure

Definition at line 1238 of file ffi_pkey_algs.cpp.

1238 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan::KyberMode::Kyber1024_R3, Botan::KyberMode::Kyber512_R3, and Botan::KyberMode::Kyber768_R3.

◆ botan_pubkey_load_ml_dsa()

int botan_pubkey_load_ml_dsa ( botan_pubkey_t * key,
const uint8_t pubkey[],
size_t key_len,
const char * mldsa_mode )

Load an ML-DSA public key from its raw encoding

Parameters
keythe new object will be placed here
Public Key Algorithmsthe raw public key
key_lenlength of pubkey in bytes
mldsa_modethe parameter set, eg "ML-DSA-6x5"
Returns
0 on success, a negative value on failure

Definition at line 1378 of file ffi_pkey_algs.cpp.

1378 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_pubkey_load_ml_kem()

int botan_pubkey_load_ml_kem ( botan_pubkey_t * key,
const uint8_t pubkey[],
size_t key_len,
const char * mlkem_mode )

Load an ML-KEM public key from its raw encoding

Parameters
keythe new object will be placed here
Public Key Algorithmsthe raw public key
key_lenlength of pubkey in bytes
mlkem_modethe parameter set, eg "ML-KEM-768"
Returns
0 on success, a negative value on failure

Definition at line 1328 of file ffi_pkey_algs.cpp.

1328 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_pubkey_load_rsa()

int botan_pubkey_load_rsa ( botan_pubkey_t * key,
botan_mp_t n,
botan_mp_t e )

Load an RSA public key from its modulus and public exponent

Parameters
keythe new object will be placed here
nthe modulus
ethe public exponent
Returns
0 on success, a negative value on failure

Definition at line 273 of file ffi_pkey_algs.cpp.

273 {
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}

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

◆ botan_pubkey_load_rsa_pkcs1()

int botan_pubkey_load_rsa_pkcs1 ( botan_pubkey_t * key,
const uint8_t bits[],
size_t len )

Load an RSA public key from a PKCS #1 RSAPublicKey structure

Parameters
keythe new object will be placed here
bitsthe DER encoding to load
lenlength of bits in bytes
Returns
0 on success, a negative value on failure

Definition at line 289 of file ffi_pkey_algs.cpp.

289 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan::AlgorithmIdentifier::USE_NULL_PARAM.

◆ botan_pubkey_load_slh_dsa()

int botan_pubkey_load_slh_dsa ( botan_pubkey_t * key,
const uint8_t pubkey[],
size_t key_len,
const char * slhdsa_mode )

Load an SLH-DSA public key from its raw encoding

Parameters
keythe new object will be placed here
Public Key Algorithmsthe raw public key
key_lenlength of pubkey in bytes
slhdsa_modethe parameter set, eg "SLH-DSA-SHA2-128s"
Returns
0 on success, a negative value on failure

Definition at line 1428 of file ffi_pkey_algs.cpp.

1428 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_pubkey_load_sm2()

int botan_pubkey_load_sm2 ( botan_pubkey_t * key,
botan_mp_t public_x,
botan_mp_t public_y,
const char * curve_name )

Load an SM2 public key from the affine coordinates of the public point

Parameters
keythe new object will be placed here
public_xthe x coordinate of the public point
public_ythe y coordinate of the public point
curve_namethe name of the curve, typically "sm2p256v1"
Returns
0 on success, a negative value on failure

Definition at line 759 of file ffi_pkey_algs.cpp.

762 {
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}
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition ffi.h:146

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_ERROR_UNKNOWN_ERROR, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().

Referenced by botan_pubkey_load_sm2_enc().

◆ botan_pubkey_load_sm2_enc()

int botan_pubkey_load_sm2_enc ( botan_pubkey_t * key,
botan_mp_t public_x,
botan_mp_t public_y,
const char * curve_name )

Load an SM2 encryption public key; identical to botan_pubkey_load_sm2

Parameters
keythe new object will be placed here
public_xthe x coordinate of the public point
public_ythe y coordinate of the public point
curve_namethe name of the curve, typically "sm2p256v1"
Returns
0 on success, a negative value on failure

Definition at line 828 of file ffi_pkey_algs.cpp.

831 {
832 return botan_pubkey_load_sm2(key, public_x, public_y, curve_name);
833}
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)

References botan_pubkey_load_sm2().

◆ botan_pubkey_load_sm2_sec1()

int botan_pubkey_load_sm2_sec1 ( botan_pubkey_t * key,
const uint8_t sec1[],
size_t sec1_len,
const char * curve_name )

Load an SM2 public key from the SEC1 encoding of the public point

Parameters
keythe new object will be placed here
sec1the SEC1 compressed or uncompressed point encoding
sec1_lenlength of sec1 in bytes
curve_namethe name of the curve, typically "sm2p256v1"
Returns
0 on success, a negative value on failure

Definition at line 783 of file ffi_pkey_algs.cpp.

783 {
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}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_pubkey_load_x25519()

int botan_pubkey_load_x25519 ( botan_pubkey_t * key,
const uint8_t pubkey[32] )

Load an X25519 public key from its raw 32 byte encoding

Parameters
keythe new object will be placed here
Public Key Algorithmsthe raw public key
Returns
0 on success, a negative value on failure

Definition at line 1070 of file ffi_pkey_algs.cpp.

1070 {
1071#if defined(BOTAN_HAS_X25519)
1072 if(any_null_pointers(key, pubkey)) {
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}

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_pubkey_load_x448()

int botan_pubkey_load_x448 ( botan_pubkey_t * key,
const uint8_t pubkey[56] )

Load an X448 public key from its raw 56 byte encoding

Parameters
keythe new object will be placed here
Public Key Algorithmsthe raw public key
Returns
0 on success, a negative value on failure

Definition at line 1146 of file ffi_pkey_algs.cpp.

1146 {
1147#if defined(BOTAN_HAS_X448)
1148 if(any_null_pointers(key, pubkey)) {
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}

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_pubkey_rsa_get_e()

int botan_pubkey_rsa_get_e ( botan_mp_t e,
botan_pubkey_t rsa_key )

Get the public exponent of an RSA public key

Parameters
eset to the value of the public exponent
rsa_keythe RSA public key
Returns
0 on success, a negative value on failure

Definition at line 327 of file ffi_pkey_algs.cpp.

327 {
328 return botan_pubkey_get_field(e, key, "e");
329}

References botan_pubkey_get_field().

◆ botan_pubkey_rsa_get_n()

int botan_pubkey_rsa_get_n ( botan_mp_t n,
botan_pubkey_t rsa_key )

Get the modulus of an RSA public key

Parameters
nset to the value of the modulus
rsa_keythe RSA public key
Returns
0 on success, a negative value on failure

Definition at line 331 of file ffi_pkey_algs.cpp.

331 {
332 return botan_pubkey_get_field(n, key, "n");
333}

References botan_pubkey_get_field().

◆ botan_pubkey_sm2_compute_za()

int botan_pubkey_sm2_compute_za ( uint8_t out[],
size_t * out_len,
const char * ident,
const char * hash_algo,
botan_pubkey_t key )

Compute the SM2 ZA value, the hash of the user identity and public key which is prefixed to the message before signing

Parameters
outoutput buffer
out_lenon input the size of out, on output the number of bytes written or required
identthe user identifier
hash_algothe hash to use, typically "SM3"
keythe SM2 public key
Returns
0 on success, a negative value on failure

Definition at line 726 of file ffi_pkey_algs.cpp.

727 {
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}
virtual std::string algo_name() const =0
const EC_AffinePoint & _public_ec_point() const
Definition ecc_key.cpp:88
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:308
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:124

References Botan::EC_PublicKey::_public_ec_point(), Botan::Asymmetric_Key::algo_name(), Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, Botan::HashFunction::create_or_throw(), Botan::EC_PublicKey::domain(), Botan_FFI::ffi_guard_thunk(), Botan_FFI::safe_get(), Botan::sm2_compute_za(), and Botan_FFI::write_vec_output().

◆ botan_pubkey_view_ec_public_point()

int botan_pubkey_view_ec_public_point ( botan_pubkey_t key,
botan_view_ctx ctx,
botan_view_bin_fn view )

View the uncompressed public point associated with the key

Definition at line 1541 of file ffi_pkey_algs.cpp.

1541 {
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}

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan_FFI::invoke_view_callback().

◆ botan_pubkey_view_kyber_raw_key()

int botan_pubkey_view_kyber_raw_key ( botan_pubkey_t key,
botan_view_ctx ctx,
botan_view_bin_fn view )

View the raw encoding of a Kyber public key

Parameters
keythe Kyber public key
ctxan application context passed to the view function
viewthe view callback which receives the encoding
Returns
0 on success, a negative value on failure

Definition at line 1286 of file ffi_pkey_algs.cpp.

1286 {
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}

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan_FFI::invoke_view_callback().

◆ botan_pubkey_x25519_get_pubkey()

int botan_pubkey_x25519_get_pubkey ( botan_pubkey_t key,
uint8_t pubkey[32] )

Get the raw 32 byte encoding of an X25519 public key

Parameters
keythe X25519 public key
Public Key Algorithmsset to the raw public key
Returns
0 on success, a negative value on failure

Definition at line 1109 of file ffi_pkey_algs.cpp.

1109 {
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}

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan::copy_mem().

◆ botan_pubkey_x448_get_pubkey()

int botan_pubkey_x448_get_pubkey ( botan_pubkey_t key,
uint8_t pubkey[56] )

Get the raw 56 byte encoding of an X448 public key

Parameters
keythe X448 public key
Public Key Algorithmsset to the raw public key
Returns
0 on success, a negative value on failure

Definition at line 1182 of file ffi_pkey_algs.cpp.

1182 {
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}
A public key for the X448 key agreement scheme according to RFC 7748.
Definition x448.h:24

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, BOTAN_UNUSED, and Botan::copy_mem().