Botan 3.13.0
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_STRUCT (botan_pk_op_decrypt_struct, Botan::PK_Decryptor, 0x912F3C37)
 BOTAN_FFI_DECLARE_STRUCT (botan_pk_op_encrypt_struct, Botan::PK_Encryptor, 0x891F3FC3)
 BOTAN_FFI_DECLARE_STRUCT (botan_pk_op_ka_struct, Botan::PK_Key_Agreement, 0x2939CAB1)
 BOTAN_FFI_DECLARE_STRUCT (botan_pk_op_kem_decrypt_struct, Botan::PK_KEM_Decryptor, 0x1743D8E6)
 BOTAN_FFI_DECLARE_STRUCT (botan_pk_op_kem_encrypt_struct, Botan::PK_KEM_Encryptor, 0x1D13A446)
 BOTAN_FFI_DECLARE_STRUCT (botan_pk_op_sign_struct, Botan::PK_Signer, 0x1AF0C39F)
 BOTAN_FFI_DECLARE_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_STRUCT() [1/7]

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

◆ BOTAN_FFI_DECLARE_STRUCT() [2/7]

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

◆ BOTAN_FFI_DECLARE_STRUCT() [3/7]

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

◆ BOTAN_FFI_DECLARE_STRUCT() [4/7]

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

◆ BOTAN_FFI_DECLARE_STRUCT() [5/7]

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

◆ BOTAN_FFI_DECLARE_STRUCT() [6/7]

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

◆ BOTAN_FFI_DECLARE_STRUCT() [7/7]

BOTAN_FFI_DECLARE_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:158
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:267

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
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
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:188

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:3123

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 341 of file ffi_pk_op.cpp.

341 {
342 if(Botan::any_null_pointers(op, padding)) {
344 }
345
346 return ffi_guard_thunk(__func__, [=]() -> int {
347 auto pk = std::make_unique<Botan::PK_KEM_Decryptor>(safe_get(key_obj), Botan::system_rng(), padding);
348 return ffi_new_object(op, std::move(pk));
349 });
350}

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 388 of file ffi_pk_op.cpp.

388 {
389 return BOTAN_FFI_CHECKED_DELETE(op);
390}

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 365 of file ffi_pk_op.cpp.

372 {
373 if(salt_len > 0 && salt == nullptr) {
375 }
376 if(Botan::any_null_pointers(encapsulated_key, shared_key_len)) {
378 }
379
380 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
381 const auto shared_key =
382 kem.decrypt(encapsulated_key, encapsulated_key_len, desired_shared_key_len, salt, salt_len);
383
384 return write_vec_output(shared_key_out, shared_key_len, shared_key);
385 });
386}

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 352 of file ffi_pk_op.cpp.

354 {
355 if(output_shared_key_length == nullptr) {
357 }
358
359 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
360 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
361 return BOTAN_FFI_SUCCESS;
362 });
363}
@ 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 275 of file ffi_pk_op.cpp.

275 {
276 if(Botan::any_null_pointers(op, padding)) {
278 }
279
280 return ffi_guard_thunk(__func__, [=]() -> int {
281 auto pk = std::make_unique<Botan::PK_KEM_Encryptor>(safe_get(key_obj), padding);
282 return ffi_new_object(op, std::move(pk));
283 });
284}

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 315 of file ffi_pk_op.cpp.

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

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 286 of file ffi_pk_op.cpp.

286 {
287 return BOTAN_FFI_CHECKED_DELETE(op);
288}

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 303 of file ffi_pk_op.cpp.

304 {
305 if(output_encapsulated_key_length == nullptr) {
307 }
308
309 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
310 *output_encapsulated_key_length = kem.encapsulated_key_length();
311 return BOTAN_FFI_SUCCESS;
312 });
313}

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 290 of file ffi_pk_op.cpp.

292 {
293 if(output_shared_key_length == nullptr) {
295 }
296
297 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
298 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
299 return BOTAN_FFI_SUCCESS;
300 });
301}

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 252 of file ffi_pk_op.cpp.

258 {
259 if(out_len == nullptr) {
261 }
262 if(other_key_len > 0 && other_key == nullptr) {
264 }
265 if(salt_len > 0 && salt == nullptr) {
267 }
268
269 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
270 auto k = o.derive_key(*out_len, other_key, other_key_len, salt, salt_len).bits_of();
271 return write_vec_output(out, out_len, k);
272 });
273}

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 208 of file ffi_pk_op.cpp.

208 {
209 if(Botan::any_null_pointers(op, kdf)) {
211 }
212
213 if(flags != 0) {
215 }
216
217 return ffi_guard_thunk(__func__, [=]() -> int {
218 *op = nullptr;
219 auto pk = std::make_unique<Botan::PK_Key_Agreement>(safe_get(key_obj), Botan::system_rng(), kdf);
220 return ffi_new_object(op, std::move(pk));
221 });
222}

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 224 of file ffi_pk_op.cpp.

224 {
225 return BOTAN_FFI_CHECKED_DELETE(op);
226}

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 228 of file ffi_pk_op.cpp.

228 {
229 return copy_view_bin(out, out_len, botan_pk_op_key_agreement_view_public, key);
230}
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:214

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 242 of file ffi_pk_op.cpp.

242 {
243 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
244 if(out_len == nullptr) {
246 }
247 *out_len = o.agreed_value_size();
248 return BOTAN_FFI_SUCCESS;
249 });
250}

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 232 of file ffi_pk_op.cpp.

232 {
233 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
234 if(auto kak = dynamic_cast<const Botan::PK_Key_Agreement_Key*>(&k)) {
235 return invoke_view_callback(view, ctx, kak->public_value());
236 } else {
238 }
239 });
240}
@ 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:190

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

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) { return write_vec_output(out, out_len, o.signature(safe_get(rng_obj))); });
160}

References BOTAN_FFI_VISIT, 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 162 of file ffi_pk_op.cpp.

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

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 180 of file ffi_pk_op.cpp.

180 {
181 return BOTAN_FFI_CHECKED_DELETE(op);
182}

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 192 of file ffi_pk_op.cpp.

192 {
193 if(sig_len > 0 && sig == nullptr) {
195 }
196
197 return BOTAN_FFI_VISIT(op, [=](auto& o) {
198 const bool legit = o.check_signature(sig, sig_len);
199
200 if(legit) {
201 return BOTAN_FFI_SUCCESS;
202 } else {
204 }
205 });
206}
@ 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 184 of file ffi_pk_op.cpp.

184 {
185 if(in_len > 0 && in == nullptr) {
187 }
188
189 return BOTAN_FFI_VISIT(op, [=](auto& o) { o.update(in, in_len); });
190}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.