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) {
95 if(ptext_len ==
nullptr) {
98 return BOTAN_FFI_VISIT(op, [=](
const auto& o) { *ptext_len = o.plaintext_length(ctext_len); });
102 botan_pk_op_decrypt_t op, uint8_t out[],
size_t* out_len,
const uint8_t ciphertext[],
size_t ciphertext_len) {
104 op, [=](
const auto& o) {
return write_vec_output(out, out_len, o.decrypt(ciphertext, ciphertext_len)); });
135 if(sig_len ==
nullptr) {
139 return BOTAN_FFI_VISIT(op, [=](
const auto& o) { *sig_len = o.signature_length(); });
163 auto pk = std::make_unique<Botan::PK_Verifier>(
safe_get(key_obj), hash, format);
178 const bool legit = o.check_signature(sig, sig_len);
224 if(out_len ==
nullptr) {
227 *out_len = o.agreed_value_size();
235 const uint8_t other_key[],
236 size_t other_key_len,
237 const uint8_t salt[],
240 auto k = o.derive_key(*out_len, other_key, other_key_len, salt, salt_len).bits_of();
246 if(op ==
nullptr || padding ==
nullptr) {
251 auto pk = std::make_unique<Botan::PK_KEM_Encryptor>(
safe_get(key_obj), padding);
261 size_t desired_shared_key_length,
262 size_t* output_shared_key_length) {
263 if(output_shared_key_length ==
nullptr) {
268 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
274 size_t* output_encapsulated_key_length) {
275 if(output_encapsulated_key_length ==
nullptr) {
280 *output_encapsulated_key_length = kem.encapsulated_key_length();
287 const uint8_t salt[],
289 size_t desired_shared_key_len,
290 uint8_t shared_key_out[],
291 size_t* shared_key_len,
292 uint8_t encapsulated_key_out[],
293 size_t* encapsulated_key_len) {
295 const auto result = kem.encrypt(
safe_get(rng), desired_shared_key_len, {salt, salt_len});
297 int rc =
write_vec_output(encapsulated_key_out, encapsulated_key_len, result.encapsulated_shared_key());
303 return write_vec_output(shared_key_out, shared_key_len, result.shared_key());
308 if(op ==
nullptr || padding ==
nullptr) {
319 size_t desired_shared_key_length,
320 size_t* output_shared_key_length) {
321 if(output_shared_key_length ==
nullptr) {
326 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
332 const uint8_t salt[],
334 const uint8_t encapsulated_key[],
335 size_t encapsulated_key_len,
336 size_t desired_shared_key_len,
337 uint8_t shared_key_out[],
338 size_t* shared_key_len) {
340 const auto shared_key =
341 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()