Botan 3.14.1
Crypto and TLS for C&
ffi_pk_op.cpp File Reference
#include <botan/ffi.h>
#include <botan/pubkey.h>
#include <botan/system_rng.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

 BOTAN_FFI_DECLARE_UNIQUE_STRUCT (botan_pk_op_decrypt_struct, Botan::PK_Decryptor, 0x912F3C37)
 BOTAN_FFI_DECLARE_UNIQUE_STRUCT (botan_pk_op_encrypt_struct, Botan::PK_Encryptor, 0x891F3FC3)
 BOTAN_FFI_DECLARE_UNIQUE_STRUCT (botan_pk_op_ka_struct, Botan::PK_Key_Agreement, 0x2939CAB1)
 BOTAN_FFI_DECLARE_UNIQUE_STRUCT (botan_pk_op_kem_decrypt_struct, Botan::PK_KEM_Decryptor, 0x1743D8E6)
 BOTAN_FFI_DECLARE_UNIQUE_STRUCT (botan_pk_op_kem_encrypt_struct, Botan::PK_KEM_Encryptor, 0x1D13A446)
 BOTAN_FFI_DECLARE_UNIQUE_STRUCT (botan_pk_op_sign_struct, Botan::PK_Signer, 0x1AF0C39F)
 BOTAN_FFI_DECLARE_UNIQUE_STRUCT (botan_pk_op_verify_struct, Botan::PK_Verifier, 0x2B91F936)
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_decrypt_create (botan_pk_op_decrypt_t *op, botan_privkey_t key_obj, const char *padding, uint32_t flags)
int botan_pk_op_decrypt_destroy (botan_pk_op_decrypt_t op)
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 (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_encrypt_create (botan_pk_op_encrypt_t *op, botan_pubkey_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_encrypt_output_length (botan_pk_op_encrypt_t op, size_t ptext_len, size_t *ctext_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_kem_decrypt_destroy (botan_pk_op_kem_decrypt_t op)
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_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_kem_encrypt_create (botan_pk_op_kem_encrypt_t *op, botan_pubkey_t key_obj, const char *padding)
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_kem_encrypt_destroy (botan_pk_op_kem_encrypt_t op)
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_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 (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_key_agreement_create (botan_pk_op_ka_t *op, botan_privkey_t key_obj, const char *kdf, uint32_t flags)
int botan_pk_op_key_agreement_destroy (botan_pk_op_ka_t op)
int botan_pk_op_key_agreement_export_public (botan_privkey_t key, uint8_t out[], size_t *out_len)
int botan_pk_op_key_agreement_size (botan_pk_op_ka_t op, size_t *out_len)
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_sign_create (botan_pk_op_sign_t *op, botan_privkey_t key_obj, const char *hash, uint32_t flags)
int botan_pk_op_sign_destroy (botan_pk_op_sign_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_sign_output_length (botan_pk_op_sign_t op, size_t *sig_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_verify_create (botan_pk_op_verify_t *op, botan_pubkey_t key_obj, const char *hash, uint32_t flags)
int botan_pk_op_verify_destroy (botan_pk_op_verify_t op)
int botan_pk_op_verify_finish (botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len)
int botan_pk_op_verify_update (botan_pk_op_verify_t op, const uint8_t in[], size_t in_len)

Function Documentation

◆ BOTAN_FFI_DECLARE_UNIQUE_STRUCT() [1/7]

BOTAN_FFI_DECLARE_UNIQUE_STRUCT ( botan_pk_op_decrypt_struct ,
Botan::PK_Decryptor ,
0x912F3C37  )

◆ BOTAN_FFI_DECLARE_UNIQUE_STRUCT() [2/7]

BOTAN_FFI_DECLARE_UNIQUE_STRUCT ( botan_pk_op_encrypt_struct ,
Botan::PK_Encryptor ,
0x891F3FC3  )

◆ BOTAN_FFI_DECLARE_UNIQUE_STRUCT() [3/7]

BOTAN_FFI_DECLARE_UNIQUE_STRUCT ( botan_pk_op_ka_struct ,
Botan::PK_Key_Agreement ,
0x2939CAB1  )

◆ BOTAN_FFI_DECLARE_UNIQUE_STRUCT() [4/7]

BOTAN_FFI_DECLARE_UNIQUE_STRUCT ( botan_pk_op_kem_decrypt_struct ,
Botan::PK_KEM_Decryptor ,
0x1743D8E6  )

◆ BOTAN_FFI_DECLARE_UNIQUE_STRUCT() [5/7]

BOTAN_FFI_DECLARE_UNIQUE_STRUCT ( botan_pk_op_kem_encrypt_struct ,
Botan::PK_KEM_Encryptor ,
0x1D13A446  )

◆ BOTAN_FFI_DECLARE_UNIQUE_STRUCT() [6/7]

BOTAN_FFI_DECLARE_UNIQUE_STRUCT ( botan_pk_op_sign_struct ,
Botan::PK_Signer ,
0x1AF0C39F  )

◆ BOTAN_FFI_DECLARE_UNIQUE_STRUCT() [7/7]

BOTAN_FFI_DECLARE_UNIQUE_STRUCT ( botan_pk_op_verify_struct ,
Botan::PK_Verifier ,
0x2B91F936  )

◆ botan_pk_op_decrypt()

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 )

Decrypt a message with a private key

Parameters
opthe decryption operation
outoutput buffer
out_lenon input the size of out, on output the number of bytes written or required
ciphertextthe message to decrypt
ciphertext_lenlength of ciphertext in bytes
Returns
0 on success, a negative value on failure

Definition at line 105 of file ffi_pk_op.cpp.

106 {
107 if(ciphertext_len > 0 && ciphertext == nullptr) {
109 }
110
111 return BOTAN_FFI_VISIT(
112 op, [=](const auto& o) { return write_vec_output(out, out_len, o.decrypt(ciphertext, ciphertext_len)); });
113}
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:327
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:436

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

◆ botan_pk_op_decrypt_create()

int botan_pk_op_decrypt_create ( botan_pk_op_decrypt_t * op,
botan_privkey_t key,
const char * padding,
uint32_t flags )

Create a public key decryption operation

Parameters
opthe new object will be placed here
keythe private key to decrypt with
paddingthe padding/encoding method, eg "OAEP(SHA-256)"
flagsshould be 0 in current API revision
Returns
0 on success, a negative value on failure

Definition at line 74 of file ffi_pk_op.cpp.

77 {
78 if(Botan::any_null_pointers(op, padding)) {
80 }
81
82 if(flags != 0) {
84 }
85
86 return ffi_guard_thunk(__func__, [=]() -> int {
87 *op = nullptr;
88
89 auto pk = std::make_unique<Botan::PK_Decryptor_EME>(safe_get(key_obj), Botan::system_rng(), padding);
90 return ffi_new_object(op, std::move(pk));
91 });
92}
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:130
auto safe_get(T *p) -> typename T::element_type &
Definition ffi_util.h:191
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:347
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:229
RandomNumberGenerator & system_rng()
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan_FFI::safe_get(), and Botan::system_rng().

◆ botan_pk_op_decrypt_destroy()

int botan_pk_op_decrypt_destroy ( botan_pk_op_decrypt_t op)

Frees all resources of the decryption operation object

Parameters
opthe operation to destroy
Returns
0 if success, error if invalid object handle

Definition at line 94 of file ffi_pk_op.cpp.

94 {
95 return BOTAN_FFI_CHECKED_DELETE(op);
96}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:357

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_pk_op_decrypt_output_length()

int botan_pk_op_decrypt_output_length ( botan_pk_op_decrypt_t op,
size_t ctext_len,
size_t * ptext_len )

Return an upper bound on the plaintext length for a given ciphertext length

Parameters
opthe decryption operation
ctext_lenthe ciphertext length in bytes
ptext_lenset to the maximum plaintext length in bytes
Returns
0 on success, a negative value on failure

Definition at line 98 of file ffi_pk_op.cpp.

98 {
99 if(ptext_len == nullptr) {
101 }
102 return BOTAN_FFI_VISIT(op, [=](const auto& o) { *ptext_len = o.plaintext_length(ctext_len); });
103}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_pk_op_encrypt()

int botan_pk_op_encrypt ( botan_pk_op_encrypt_t op,
botan_rng_t rng,
uint8_t out[],
size_t * out_len,
const uint8_t plaintext[],
size_t plaintext_len )

Encrypt a message with a public key

Parameters
opthe encryption operation
Random Number Generatorsa random number generator
outoutput buffer
out_lenon input the size of out, on output the number of bytes written or required
plaintextthe message to encrypt
plaintext_lenlength of plaintext in bytes
Returns
0 on success, a negative value on failure

Definition at line 56 of file ffi_pk_op.cpp.

61 {
62 if(plaintext_len > 0 && plaintext == nullptr) {
64 }
65
66 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
67 return write_vec_output(out, out_len, o.encrypt(plaintext, plaintext_len, safe_get(rng_obj)));
68 });
69}

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

◆ botan_pk_op_encrypt_create()

int botan_pk_op_encrypt_create ( botan_pk_op_encrypt_t * op,
botan_pubkey_t key,
const char * padding,
uint32_t flags )

Create a public key encryption operation

Parameters
opthe new object will be placed here
keythe public key to encrypt to
paddingthe padding/encoding method, eg "OAEP(SHA-256)"
flagsshould be 0 in current API revision
Returns
0 on success, a negative value on failure

Definition at line 28 of file ffi_pk_op.cpp.

28 {
29 if(Botan::any_null_pointers(op, padding)) {
31 }
32
33 if(flags != 0 && flags != BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) {
35 }
36
37 return ffi_guard_thunk(__func__, [=]() -> int {
38 *op = nullptr;
39
40 auto pk = std::make_unique<Botan::PK_Encryptor_EME>(safe_get(key_obj), Botan::system_rng(), padding);
41 return ffi_new_object(op, std::move(pk));
42 });
43}
#define BOTAN_PUBKEY_DER_FORMAT_SIGNATURE
Definition ffi.h:3244

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_PUBKEY_DER_FORMAT_SIGNATURE, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan_FFI::safe_get(), and Botan::system_rng().

◆ botan_pk_op_encrypt_destroy()

int botan_pk_op_encrypt_destroy ( botan_pk_op_encrypt_t op)

Frees all resources of the encryption operation object

Parameters
opthe operation to destroy
Returns
0 if success, error if invalid object handle

Definition at line 45 of file ffi_pk_op.cpp.

45 {
46 return BOTAN_FFI_CHECKED_DELETE(op);
47}

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_pk_op_encrypt_output_length()

int botan_pk_op_encrypt_output_length ( botan_pk_op_encrypt_t op,
size_t ptext_len,
size_t * ctext_len )

Return the ciphertext length for a given plaintext length

Parameters
opthe encryption operation
ptext_lenthe plaintext length in bytes
ctext_lenset to the resulting ciphertext length in bytes
Returns
0 on success, a negative value on failure

Definition at line 49 of file ffi_pk_op.cpp.

49 {
50 if(ctext_len == nullptr) {
52 }
53 return BOTAN_FFI_VISIT(op, [=](const auto& o) { *ctext_len = o.ciphertext_length(ptext_len); });
54}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_pk_op_kem_decrypt_create()

int botan_pk_op_kem_decrypt_create ( botan_pk_op_kem_decrypt_t * op,
botan_privkey_t key,
const char * kdf )

Create a key decapsulation operation

Parameters
opthe new object will be placed here
keythe private key to decapsulate with
Key Derivation Functionsthe KDF applied to the shared secret, or "Raw" for no KDF
Returns
0 on success, a negative value on failure

Definition at line 347 of file ffi_pk_op.cpp.

347 {
348 if(Botan::any_null_pointers(op, padding)) {
350 }
351
352 return ffi_guard_thunk(__func__, [=]() -> int {
353 auto pk = std::make_unique<Botan::PK_KEM_Decryptor>(safe_get(key_obj), Botan::system_rng(), padding);
354 return ffi_new_object(op, std::move(pk));
355 });
356}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan_FFI::safe_get(), and Botan::system_rng().

◆ botan_pk_op_kem_decrypt_destroy()

int botan_pk_op_kem_decrypt_destroy ( botan_pk_op_kem_decrypt_t op)

Frees all resources of the key decapsulation operation object

Parameters
opthe operation to destroy
Returns
0 if success, error if invalid object handle

Definition at line 394 of file ffi_pk_op.cpp.

394 {
395 return BOTAN_FFI_CHECKED_DELETE(op);
396}

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_pk_op_kem_decrypt_shared_key()

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[],
size_t * shared_key_len )

Recover the shared key from an encapsulated key

Parameters
opthe decapsulation operation
salta salt passed to the KDF, may be NULL if salt_len is 0
salt_lenlength of salt in bytes
encapsulated_keythe encapsulated key from the counterparty
encapsulated_key_lenlength of encapsulated_key in bytes
desired_shared_key_lenthe requested shared key length in bytes
shared_keyoutput buffer for the shared key
shared_key_lenon input the size of shared_key, on output the number of bytes written
Returns
0 on success, a negative value on failure

Definition at line 371 of file ffi_pk_op.cpp.

378 {
379 if(salt_len > 0 && salt == nullptr) {
381 }
382 if(Botan::any_null_pointers(encapsulated_key, shared_key_len)) {
384 }
385
386 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
387 const auto shared_key =
388 kem.decrypt(encapsulated_key, encapsulated_key_len, desired_shared_key_len, salt, salt_len);
389
390 return write_vec_output(shared_key_out, shared_key_len, shared_key);
391 });
392}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, and Botan_FFI::write_vec_output().

◆ botan_pk_op_kem_decrypt_shared_key_length()

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 )

Return the length of the shared key this operation will produce

Parameters
opthe decapsulation operation
desired_shared_key_lengththe requested shared key length in bytes
output_shared_key_lengthset to the shared key length that will result
Returns
0 on success, a negative value on failure

Definition at line 358 of file ffi_pk_op.cpp.

360 {
361 if(output_shared_key_length == nullptr) {
363 }
364
365 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
366 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
367 return BOTAN_FFI_SUCCESS;
368 });
369}
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and BOTAN_FFI_VISIT.

◆ botan_pk_op_kem_encrypt_create()

int botan_pk_op_kem_encrypt_create ( botan_pk_op_kem_encrypt_t * op,
botan_pubkey_t key,
const char * kdf )

Create a key encapsulation operation

Parameters
opthe new object will be placed here
keythe public key to encapsulate to
Key Derivation Functionsthe KDF applied to the shared secret, or "Raw" for no KDF
Returns
0 on success, a negative value on failure

Definition at line 281 of file ffi_pk_op.cpp.

281 {
282 if(Botan::any_null_pointers(op, padding)) {
284 }
285
286 return ffi_guard_thunk(__func__, [=]() -> int {
287 auto pk = std::make_unique<Botan::PK_KEM_Encryptor>(safe_get(key_obj), padding);
288 return ffi_new_object(op, std::move(pk));
289 });
290}

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

◆ botan_pk_op_kem_encrypt_create_shared_key()

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[],
size_t * shared_key_len,
uint8_t encapsulated_key[],
size_t * encapsulated_key_len )

Generate a shared key along with the encapsulation of it for the counterparty

Parameters
opthe encapsulation operation
Random Number Generatorsa random number generator
salta salt passed to the KDF, may be NULL if salt_len is 0
salt_lenlength of salt in bytes
desired_shared_key_lenthe requested shared key length in bytes
shared_keyoutput buffer for the shared key
shared_key_lenon input the size of shared_key, on output the number of bytes written
encapsulated_keyoutput buffer for the encapsulated key
encapsulated_key_lenon input the size of encapsulated_key, on output the number of bytes written
Returns
0 on success, a negative value on failure

Definition at line 321 of file ffi_pk_op.cpp.

329 {
330 if(salt_len > 0 && salt == nullptr) {
332 }
333
334 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
335 const auto result = kem.encrypt(safe_get(rng), desired_shared_key_len, {salt, salt_len});
336
337 const int rc = write_vec_output(encapsulated_key_out, encapsulated_key_len, result.encapsulated_shared_key());
338
339 if(rc != 0) {
340 return rc;
341 }
342
343 return write_vec_output(shared_key_out, shared_key_len, result.shared_key());
344 });
345}

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

◆ botan_pk_op_kem_encrypt_destroy()

int botan_pk_op_kem_encrypt_destroy ( botan_pk_op_kem_encrypt_t op)

Frees all resources of the key encapsulation operation object

Parameters
opthe operation to destroy
Returns
0 if success, error if invalid object handle

Definition at line 292 of file ffi_pk_op.cpp.

292 {
293 return BOTAN_FFI_CHECKED_DELETE(op);
294}

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_pk_op_kem_encrypt_encapsulated_key_length()

int botan_pk_op_kem_encrypt_encapsulated_key_length ( botan_pk_op_kem_encrypt_t op,
size_t * output_encapsulated_key_length )

Return the length of the encapsulated key this operation produces

Parameters
opthe encapsulation operation
output_encapsulated_key_lengthset to the encapsulated key length in bytes
Returns
0 on success, a negative value on failure

Definition at line 309 of file ffi_pk_op.cpp.

310 {
311 if(output_encapsulated_key_length == nullptr) {
313 }
314
315 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
316 *output_encapsulated_key_length = kem.encapsulated_key_length();
317 return BOTAN_FFI_SUCCESS;
318 });
319}

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and BOTAN_FFI_VISIT.

◆ botan_pk_op_kem_encrypt_shared_key_length()

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 )

Return the length of the shared key this operation will produce

Parameters
opthe encapsulation operation
desired_shared_key_lengththe requested shared key length in bytes
output_shared_key_lengthset to the shared key length that will result
Returns
0 on success, a negative value on failure

Definition at line 296 of file ffi_pk_op.cpp.

298 {
299 if(output_shared_key_length == nullptr) {
301 }
302
303 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
304 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
305 return BOTAN_FFI_SUCCESS;
306 });
307}

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and BOTAN_FFI_VISIT.

◆ botan_pk_op_key_agreement()

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 )

Perform key agreement with the counterparty's public value

Parameters
opthe key agreement operation
outoutput buffer
out_lenon input the size of out, on output the number of bytes written or required
other_keythe counterparty's public value
other_key_lenlength of other_key in bytes
salta salt passed to the KDF, may be NULL if salt_len is 0
salt_lenlength of salt in bytes
Returns
0 on success, a negative value on failure

Definition at line 258 of file ffi_pk_op.cpp.

264 {
265 if(out_len == nullptr) {
267 }
268 if(other_key_len > 0 && other_key == nullptr) {
270 }
271 if(salt_len > 0 && salt == nullptr) {
273 }
274
275 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
276 auto k = o.derive_key(*out_len, other_key, other_key_len, salt, salt_len).bits_of();
277 return write_vec_output(out, out_len, k);
278 });
279}

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

◆ botan_pk_op_key_agreement_create()

int botan_pk_op_key_agreement_create ( botan_pk_op_ka_t * op,
botan_privkey_t key,
const char * kdf,
uint32_t flags )

Create a key agreement operation

Parameters
opthe new object will be placed here
keyour private key
Key Derivation Functionsthe KDF applied to the shared secret, or "Raw" for no KDF
flagsshould be 0 in current API revision
Returns
0 on success, a negative value on failure

Definition at line 214 of file ffi_pk_op.cpp.

214 {
215 if(Botan::any_null_pointers(op, kdf)) {
217 }
218
219 if(flags != 0) {
221 }
222
223 return ffi_guard_thunk(__func__, [=]() -> int {
224 *op = nullptr;
225 auto pk = std::make_unique<Botan::PK_Key_Agreement>(safe_get(key_obj), Botan::system_rng(), kdf);
226 return ffi_new_object(op, std::move(pk));
227 });
228}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan_FFI::safe_get(), and Botan::system_rng().

◆ botan_pk_op_key_agreement_destroy()

int botan_pk_op_key_agreement_destroy ( botan_pk_op_ka_t op)

Frees all resources of the key agreement operation object

Parameters
opthe operation to destroy
Returns
0 if success, error if invalid object handle

Definition at line 230 of file ffi_pk_op.cpp.

230 {
231 return BOTAN_FFI_CHECKED_DELETE(op);
232}

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_pk_op_key_agreement_export_public()

int botan_pk_op_key_agreement_export_public ( botan_privkey_t key,
uint8_t out[],
size_t * out_len )

Export the public value of a key agreement private key, in the format expected by botan_pk_op_key_agreement

Parameters
keythe key agreement private key
outoutput buffer
out_lenon input the size of out, on output the number of bytes written or required
Returns
0 on success, a negative value on failure

Definition at line 234 of file ffi_pk_op.cpp.

234 {
235 return copy_view_bin(out, out_len, botan_pk_op_key_agreement_view_public, key);
236}
int botan_pk_op_key_agreement_view_public(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int copy_view_bin(uint8_t out[], size_t *out_len, Fn fn, Args... args)
Definition ffi_util.h:383

References botan_pk_op_key_agreement_view_public(), and Botan_FFI::copy_view_bin().

◆ botan_pk_op_key_agreement_size()

int botan_pk_op_key_agreement_size ( botan_pk_op_ka_t op,
size_t * out_len )

Return the length of the shared secret this operation produces

Parameters
opthe key agreement operation
out_lenset to the output length in bytes
Returns
0 on success, a negative value on failure

Definition at line 248 of file ffi_pk_op.cpp.

248 {
249 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
250 if(out_len == nullptr) {
252 }
253 *out_len = o.agreed_value_size();
254 return BOTAN_FFI_SUCCESS;
255 });
256}

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and BOTAN_FFI_VISIT.

◆ botan_pk_op_key_agreement_view_public()

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

View the public value of a key agreement private key

Parameters
keythe key agreement 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 238 of file ffi_pk_op.cpp.

238 {
239 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
240 if(auto kak = dynamic_cast<const Botan::PK_Key_Agreement_Key*>(&k)) {
241 return invoke_view_callback(view, ctx, kak->public_value());
242 } else {
244 }
245 });
246}
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition ffi.h:118
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:359

References BOTAN_FFI_ERROR_INVALID_INPUT, BOTAN_FFI_VISIT, and Botan_FFI::invoke_view_callback().

Referenced by botan_pk_op_key_agreement_export_public().

◆ botan_pk_op_sign_create()

int botan_pk_op_sign_create ( botan_pk_op_sign_t * op,
botan_privkey_t key,
const char * hash_and_padding,
uint32_t flags )

Create a signature generation operation

Parameters
opthe new object will be placed here
keythe private key to sign with
hash_and_paddingthe signature padding and hash, eg "PSS(SHA-256)"
flags0, or BOTAN_PUBKEY_DER_FORMAT_SIGNATURE to request DER encoded signatures
Returns
0 on success, a negative value on failure

Definition at line 118 of file ffi_pk_op.cpp.

118 {
119 if(Botan::any_null_pointers(op, hash)) {
121 }
122
123 if(flags != 0 && flags != BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) {
125 }
126
127 return ffi_guard_thunk(__func__, [=]() -> int {
128 *op = nullptr;
129
130 const bool use_der = (flags & BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) != 0;
132
133 auto pk = std::make_unique<Botan::PK_Signer>(safe_get(key_obj), Botan::system_rng(), hash, format);
134 return ffi_new_object(op, std::move(pk));
135 });
136}
Flags flags(Flag flags)
Definition p11.h:1235

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_PUBKEY_DER_FORMAT_SIGNATURE, Botan::DerSequence, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan_FFI::safe_get(), Botan::Standard, and Botan::system_rng().

◆ botan_pk_op_sign_destroy()

int botan_pk_op_sign_destroy ( botan_pk_op_sign_t op)

Frees all resources of the signature generation operation object

Parameters
opthe operation to destroy
Returns
0 if success, error if invalid object handle

Definition at line 138 of file ffi_pk_op.cpp.

138 {
139 return BOTAN_FFI_CHECKED_DELETE(op);
140}

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_pk_op_sign_finish()

int botan_pk_op_sign_finish ( botan_pk_op_sign_t op,
botan_rng_t rng,
uint8_t sig[],
size_t * sig_len )

Produce a signature over the message, then reset for signing another message

If sig is smaller than botan_pk_op_sign_output_length, no signature is created and the message is retained, so the call can be repeated with a larger buffer.

Parameters
opthe signature operation
Random Number Generatorsa random number generator
sigoutput buffer
sig_lenon input the size of sig, on output the number of bytes written or required
Returns
0 on success, a negative value on failure

Definition at line 158 of file ffi_pk_op.cpp.

158 {
159 return BOTAN_FFI_VISIT(op, [=](auto& o) {
160 // Check before signing, which resets the message and may consume a stateful key index
161 if(const int rc = check_and_prepare_output_space(out, out_len, o.signature_length()); rc != BOTAN_FFI_SUCCESS) {
162 return rc;
163 }
164 return write_vec_output(out, out_len, o.signature(safe_get(rng_obj)));
165 });
166}
int check_and_prepare_output_space(T out[], size_t *out_len, size_t required_len)
Definition ffi_util.h:402

References BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, Botan_FFI::check_and_prepare_output_space(), Botan_FFI::safe_get(), and Botan_FFI::write_vec_output().

◆ botan_pk_op_sign_output_length()

int botan_pk_op_sign_output_length ( botan_pk_op_sign_t op,
size_t * olen )

Return the length of the signatures this operation produces

Parameters
opthe signature operation
olenset to the signature length in bytes
Returns
0 on success, a negative value on failure

Definition at line 142 of file ffi_pk_op.cpp.

142 {
143 if(sig_len == nullptr) {
145 }
146
147 return BOTAN_FFI_VISIT(op, [=](const auto& o) { *sig_len = o.signature_length(); });
148}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_pk_op_sign_update()

int botan_pk_op_sign_update ( botan_pk_op_sign_t op,
const uint8_t in[],
size_t in_len )

Add more data to the message being signed

Parameters
opthe signature operation
ininput buffer
in_lennumber of bytes to read from the input buffer
Returns
0 on success, a negative value on failure

Definition at line 150 of file ffi_pk_op.cpp.

150 {
151 if(in_len > 0 && in == nullptr) {
153 }
154
155 return BOTAN_FFI_VISIT(op, [=](auto& o) { o.update(in, in_len); });
156}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_pk_op_verify_create()

int botan_pk_op_verify_create ( botan_pk_op_verify_t * op,
botan_pubkey_t key,
const char * hash_and_padding,
uint32_t flags )

Create a signature verification operation

Parameters
opthe new object will be placed here
keythe public key to verify with
hash_and_paddingthe signature padding and hash, eg "PSS(SHA-256)"
flags0, or BOTAN_PUBKEY_DER_FORMAT_SIGNATURE if the signature is DER encoded
Returns
0 on success, a negative value on failure

Definition at line 168 of file ffi_pk_op.cpp.

168 {
169 if(Botan::any_null_pointers(op, hash)) {
171 }
172
173 if(flags != 0 && flags != BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) {
175 }
176
177 return ffi_guard_thunk(__func__, [=]() -> int {
178 *op = nullptr;
179 const bool use_der = (flags & BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) != 0;
181 auto pk = std::make_unique<Botan::PK_Verifier>(safe_get(key_obj), hash, format);
182 return ffi_new_object(op, std::move(pk));
183 });
184}

References Botan::any_null_pointers(), BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_PUBKEY_DER_FORMAT_SIGNATURE, Botan::DerSequence, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), Botan_FFI::safe_get(), and Botan::Standard.

◆ botan_pk_op_verify_destroy()

int botan_pk_op_verify_destroy ( botan_pk_op_verify_t op)

Frees all resources of the signature verification operation object

Parameters
opthe operation to destroy
Returns
0 if success, error if invalid object handle

Definition at line 186 of file ffi_pk_op.cpp.

186 {
187 return BOTAN_FFI_CHECKED_DELETE(op);
188}

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_pk_op_verify_finish()

int botan_pk_op_verify_finish ( botan_pk_op_verify_t op,
const uint8_t sig[],
size_t sig_len )

Check the signature over the message, then reset for verifying another message

Parameters
opthe verification operation
sigthe signature to check
sig_lenlength of sig in bytes
Returns
0 if the signature is valid, BOTAN_FFI_INVALID_VERIFIER if it is not, or some other negative value on error

Definition at line 198 of file ffi_pk_op.cpp.

198 {
199 if(sig_len > 0 && sig == nullptr) {
201 }
202
203 return BOTAN_FFI_VISIT(op, [=](auto& o) {
204 const bool legit = o.check_signature(sig, sig_len);
205
206 if(legit) {
207 return BOTAN_FFI_SUCCESS;
208 } else {
210 }
211 });
212}
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:116

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_INVALID_VERIFIER, BOTAN_FFI_SUCCESS, and BOTAN_FFI_VISIT.

◆ botan_pk_op_verify_update()

int botan_pk_op_verify_update ( botan_pk_op_verify_t op,
const uint8_t in[],
size_t in_len )

Add more data to the message being verified

Parameters
opthe verification operation
ininput buffer
in_lennumber of bytes to read from the input buffer
Returns
0 on success, a negative value on failure

Definition at line 190 of file ffi_pk_op.cpp.

190 {
191 if(in_len > 0 && in == nullptr) {
193 }
194
195 return BOTAN_FFI_VISIT(op, [=](auto& o) { o.update(in, in_len); });
196}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.