9#include <botan/pubkey.h>
10#include <botan/system_rng.h>
11#include <botan/internal/ffi_pkey.h>
12#include <botan/internal/ffi_rng.h>
13#include <botan/internal/ffi_util.h>
50 if(ctext_len ==
nullptr) {
53 return BOTAN_FFI_VISIT(op, [=](
const auto& o) { *ctext_len = o.ciphertext_length(ptext_len); });
60 const uint8_t plaintext[],
61 size_t plaintext_len) {
62 if(plaintext_len > 0 && plaintext ==
nullptr) {
99 if(ptext_len ==
nullptr) {
102 return BOTAN_FFI_VISIT(op, [=](
const auto& o) { *ptext_len = o.plaintext_length(ctext_len); });
106 botan_pk_op_decrypt_t op, uint8_t out[],
size_t* out_len,
const uint8_t ciphertext[],
size_t ciphertext_len) {
107 if(ciphertext_len > 0 && ciphertext ==
nullptr) {
112 op, [=](
const auto& o) {
return write_vec_output(out, out_len, o.decrypt(ciphertext, ciphertext_len)); });
143 if(sig_len ==
nullptr) {
147 return BOTAN_FFI_VISIT(op, [=](
const auto& o) { *sig_len = o.signature_length(); });
151 if(in_len > 0 && in ==
nullptr) {
175 auto pk = std::make_unique<Botan::PK_Verifier>(
safe_get(key_obj), hash, format);
185 if(in_len > 0 && in ==
nullptr) {
193 if(sig_len > 0 && sig ==
nullptr) {
198 const bool legit = o.check_signature(sig, sig_len);
244 if(out_len ==
nullptr) {
247 *out_len = o.agreed_value_size();
255 const uint8_t other_key[],
256 size_t other_key_len,
257 const uint8_t salt[],
259 if(out_len ==
nullptr) {
262 if(other_key_len > 0 && other_key ==
nullptr) {
265 if(salt_len > 0 && salt ==
nullptr) {
270 auto k = o.derive_key(*out_len, other_key, other_key_len, salt, salt_len).bits_of();
281 auto pk = std::make_unique<Botan::PK_KEM_Encryptor>(
safe_get(key_obj), padding);
291 size_t desired_shared_key_length,
292 size_t* output_shared_key_length) {
293 if(output_shared_key_length ==
nullptr) {
298 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
304 size_t* output_encapsulated_key_length) {
305 if(output_encapsulated_key_length ==
nullptr) {
310 *output_encapsulated_key_length = kem.encapsulated_key_length();
317 const uint8_t salt[],
319 size_t desired_shared_key_len,
320 uint8_t shared_key_out[],
321 size_t* shared_key_len,
322 uint8_t encapsulated_key_out[],
323 size_t* encapsulated_key_len) {
324 if(salt_len > 0 && salt ==
nullptr) {
329 const auto result = kem.encrypt(
safe_get(rng), desired_shared_key_len, {salt, salt_len});
331 const int rc =
write_vec_output(encapsulated_key_out, encapsulated_key_len, result.encapsulated_shared_key());
337 return write_vec_output(shared_key_out, shared_key_len, result.shared_key());
353 size_t desired_shared_key_length,
354 size_t* output_shared_key_length) {
355 if(output_shared_key_length ==
nullptr) {
360 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
366 const uint8_t salt[],
368 const uint8_t encapsulated_key[],
369 size_t encapsulated_key_len,
370 size_t desired_shared_key_len,
371 uint8_t shared_key_out[],
372 size_t* shared_key_len) {
373 if(salt_len > 0 && salt ==
nullptr) {
381 const auto shared_key =
382 kem.decrypt(encapsulated_key, encapsulated_key_len, desired_shared_key_len, salt, salt_len);
struct botan_pk_op_kem_decrypt_struct * botan_pk_op_kem_decrypt_t
struct botan_pubkey_struct * botan_pubkey_t
#define BOTAN_PUBKEY_DER_FORMAT_SIGNATURE
struct botan_pk_op_decrypt_struct * botan_pk_op_decrypt_t
struct botan_privkey_struct * botan_privkey_t
struct botan_pk_op_encrypt_struct * botan_pk_op_encrypt_t
struct botan_pk_op_kem_encrypt_struct * botan_pk_op_kem_encrypt_t
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
struct botan_pk_op_ka_struct * botan_pk_op_ka_t
int botan_pk_op_key_agreement_view_public(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
struct botan_pk_op_sign_struct * botan_pk_op_sign_t
struct botan_rng_struct * botan_rng_t
@ BOTAN_FFI_INVALID_VERIFIER
@ BOTAN_FFI_ERROR_BAD_FLAG
@ BOTAN_FFI_ERROR_INVALID_INPUT
@ BOTAN_FFI_ERROR_NULL_POINTER
struct botan_pk_op_verify_struct * botan_pk_op_verify_t
int botan_pk_op_encrypt(botan_pk_op_encrypt_t op, botan_rng_t rng_obj, uint8_t out[], size_t *out_len, const uint8_t plaintext[], size_t plaintext_len)
int botan_pk_op_decrypt(botan_pk_op_decrypt_t op, uint8_t out[], size_t *out_len, const uint8_t ciphertext[], size_t ciphertext_len)
int botan_pk_op_kem_decrypt_create(botan_pk_op_kem_decrypt_t *op, botan_privkey_t key_obj, const char *padding)
int botan_pk_op_sign_output_length(botan_pk_op_sign_t op, size_t *sig_len)
int botan_pk_op_key_agreement_export_public(botan_privkey_t key, uint8_t out[], size_t *out_len)
int botan_pk_op_kem_decrypt_shared_key(botan_pk_op_kem_decrypt_t op, const uint8_t salt[], size_t salt_len, const uint8_t encapsulated_key[], size_t encapsulated_key_len, size_t desired_shared_key_len, uint8_t shared_key_out[], size_t *shared_key_len)
int botan_pk_op_kem_encrypt_shared_key_length(botan_pk_op_kem_encrypt_t op, size_t desired_shared_key_length, size_t *output_shared_key_length)
int botan_pk_op_key_agreement_size(botan_pk_op_ka_t op, size_t *out_len)
int botan_pk_op_sign_update(botan_pk_op_sign_t op, const uint8_t in[], size_t in_len)
int botan_pk_op_decrypt_destroy(botan_pk_op_decrypt_t op)
int botan_pk_op_verify_create(botan_pk_op_verify_t *op, botan_pubkey_t key_obj, const char *hash, uint32_t flags)
int botan_pk_op_kem_encrypt_create_shared_key(botan_pk_op_kem_encrypt_t op, botan_rng_t rng, const uint8_t salt[], size_t salt_len, size_t desired_shared_key_len, uint8_t shared_key_out[], size_t *shared_key_len, uint8_t encapsulated_key_out[], size_t *encapsulated_key_len)
int botan_pk_op_key_agreement_destroy(botan_pk_op_ka_t op)
int botan_pk_op_sign_finish(botan_pk_op_sign_t op, botan_rng_t rng_obj, uint8_t out[], size_t *out_len)
int botan_pk_op_verify_update(botan_pk_op_verify_t op, const uint8_t in[], size_t in_len)
int botan_pk_op_encrypt_create(botan_pk_op_encrypt_t *op, botan_pubkey_t key_obj, const char *padding, uint32_t flags)
int botan_pk_op_verify_destroy(botan_pk_op_verify_t op)
int botan_pk_op_kem_encrypt_create(botan_pk_op_kem_encrypt_t *op, botan_pubkey_t key_obj, const char *padding)
int botan_pk_op_key_agreement_view_public(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_pk_op_kem_encrypt_encapsulated_key_length(botan_pk_op_kem_encrypt_t op, size_t *output_encapsulated_key_length)
int botan_pk_op_verify_finish(botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len)
int botan_pk_op_decrypt_output_length(botan_pk_op_decrypt_t op, size_t ctext_len, size_t *ptext_len)
int botan_pk_op_encrypt_output_length(botan_pk_op_encrypt_t op, size_t ptext_len, size_t *ctext_len)
int botan_pk_op_key_agreement(botan_pk_op_ka_t op, uint8_t out[], size_t *out_len, const uint8_t other_key[], size_t other_key_len, const uint8_t salt[], size_t salt_len)
int botan_pk_op_kem_encrypt_destroy(botan_pk_op_kem_encrypt_t op)
int botan_pk_op_decrypt_create(botan_pk_op_decrypt_t *op, botan_privkey_t key_obj, const char *padding, uint32_t flags)
int botan_pk_op_encrypt_destroy(botan_pk_op_encrypt_t op)
int botan_pk_op_kem_decrypt_shared_key_length(botan_pk_op_kem_decrypt_t op, size_t desired_shared_key_length, size_t *output_shared_key_length)
int botan_pk_op_sign_create(botan_pk_op_sign_t *op, botan_privkey_t key_obj, const char *hash, uint32_t flags)
int botan_pk_op_kem_decrypt_destroy(botan_pk_op_kem_decrypt_t op)
int botan_pk_op_sign_destroy(botan_pk_op_sign_t op)
int botan_pk_op_key_agreement_create(botan_pk_op_ka_t *op, botan_privkey_t key_obj, const char *kdf, uint32_t flags)
#define BOTAN_FFI_VISIT(obj, lambda)
#define BOTAN_FFI_CHECKED_DELETE(o)
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
int copy_view_bin(uint8_t out[], size_t *out_len, Fn fn, Args... args)
T & safe_get(botan_struct< T, M > *p)
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
int ffi_guard_thunk(const char *func_name, T thunk)
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
RandomNumberGenerator & system_rng()
bool any_null_pointers(Ptrs... ptr)