Botan 3.9.0
Crypto and TLS for C&
ffi_mac.cpp File Reference
#include <botan/ffi.h>
#include <botan/mac.h>
#include <botan/internal/ffi_util.h>

Go to the source code of this file.

Functions

 BOTAN_FFI_DECLARE_STRUCT (botan_mac_struct, Botan::MessageAuthenticationCode, 0xA06E8FC1)
int botan_mac_clear (botan_mac_t mac)
int botan_mac_destroy (botan_mac_t mac)
int botan_mac_final (botan_mac_t mac, uint8_t out[])
int botan_mac_get_keyspec (botan_mac_t mac, size_t *out_minimum_keylength, size_t *out_maximum_keylength, size_t *out_keylength_modulo)
int botan_mac_init (botan_mac_t *mac, const char *mac_name, uint32_t flags)
int botan_mac_name (botan_mac_t mac, char *name, size_t *name_len)
int botan_mac_output_length (botan_mac_t mac, size_t *out)
int botan_mac_set_key (botan_mac_t mac, const uint8_t *key, size_t key_len)
int botan_mac_set_nonce (botan_mac_t mac, const uint8_t *nonce, size_t nonce_len)
int botan_mac_update (botan_mac_t mac, const uint8_t *buf, size_t len)

Function Documentation

◆ BOTAN_FFI_DECLARE_STRUCT()

BOTAN_FFI_DECLARE_STRUCT ( botan_mac_struct ,
Botan::MessageAuthenticationCode ,
0xA06E8FC1  )

◆ botan_mac_clear()

int botan_mac_clear ( botan_mac_t mac)

Reinitializes the state of the MAC computation. A MAC can be computed (with update/final) immediately.

Parameters
macmac object
Returns
0 on success, a negative value on failure

Definition at line 52 of file ffi_mac.cpp.

52 {
53 return BOTAN_FFI_VISIT(mac, [](auto& m) { m.clear(); });
54}
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158

References BOTAN_FFI_VISIT.

◆ botan_mac_destroy()

int botan_mac_destroy ( botan_mac_t mac)

Frees all resources of the MAC object

Parameters
macmac object
Returns
0 if success, error if invalid object handle

Definition at line 36 of file ffi_mac.cpp.

36 {
37 return BOTAN_FFI_CHECKED_DELETE(mac);
38}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:185

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_mac_final()

int botan_mac_final ( botan_mac_t mac,
uint8_t out[] )

Finalizes the MAC computation and writes the output to out[0:botan_mac_output_length()] then reinitializes for computing another MAC as if botan_mac_clear had been called.

Parameters
macmac object
outoutput buffer
Returns
0 on success, a negative value on failure

Definition at line 60 of file ffi_mac.cpp.

60 {
61 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.final(out); });
62}

References BOTAN_FFI_VISIT.

◆ botan_mac_get_keyspec()

int botan_mac_get_keyspec ( botan_mac_t mac,
size_t * out_minimum_keylength,
size_t * out_maximum_keylength,
size_t * out_keylength_modulo )

Get the key length limits of this auth code

Parameters
macthe object to read
out_minimum_keylengthif non-NULL, will be set to minimum keylength of MAC
out_maximum_keylengthif non-NULL, will be set to maximum keylength of MAC
out_keylength_moduloif non-NULL will be set to byte multiple of valid keys

Definition at line 68 of file ffi_mac.cpp.

71 {
72 return BOTAN_FFI_VISIT(mac, [=](auto& m) {
73 if(out_minimum_keylength) {
74 *out_minimum_keylength = m.minimum_keylength();
75 }
76 if(out_maximum_keylength) {
77 *out_maximum_keylength = m.maximum_keylength();
78 }
79 if(out_keylength_modulo) {
80 *out_keylength_modulo = m.key_spec().keylength_multiple();
81 }
82 });
83}

References BOTAN_FFI_VISIT.

◆ botan_mac_init()

int botan_mac_init ( botan_mac_t * mac,
const char * mac_name,
uint32_t flags )

Initialize a message authentication code object

Parameters
macmac object
mac_namename of the hash function, e.g., "HMAC(SHA-384)"
flagsshould be 0 in current API revision, all other uses are reserved and return a negative value (error code)
Returns
0 on success, a negative value on failure

Definition at line 18 of file ffi_mac.cpp.

18 {
19 return ffi_guard_thunk(__func__, [=]() -> int {
20 if(any_null_pointers(mac, mac_name)) {
22 }
23
24 if(flags != 0) {
26 }
27
28 if(auto m = Botan::MessageAuthenticationCode::create(mac_name)) {
29 return ffi_new_object(mac, std::move(m));
30 } else {
32 }
33 });
34}
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:51
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:131
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:132
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:23

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, Botan::MessageAuthenticationCode::create(), Botan_FFI::ffi_guard_thunk(), and Botan_FFI::ffi_new_object().

◆ botan_mac_name()

int botan_mac_name ( botan_mac_t mac,
char * name,
size_t * name_len )

Get the name of this MAC

Parameters
macthe object to read
nameoutput buffer
name_lenon input, the length of buffer, on success the number of bytes written

Definition at line 64 of file ffi_mac.cpp.

64 {
65 return BOTAN_FFI_VISIT(mac, [=](const auto& m) { return write_str_output(name, name_len, m.name()); });
66}
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:251

References BOTAN_FFI_VISIT, and Botan_FFI::write_str_output().

◆ botan_mac_output_length()

int botan_mac_output_length ( botan_mac_t mac,
size_t * output_length )

Writes the output length of the message authentication code to *output_length

Parameters
macmac object
output_lengthoutput buffer to hold the MAC output length
Returns
0 on success, a negative value on failure

Definition at line 48 of file ffi_mac.cpp.

48 {
49 return BOTAN_FFI_VISIT(mac, [=](const auto& m) { *out = m.output_length(); });
50}

References BOTAN_FFI_VISIT.

◆ botan_mac_set_key()

int botan_mac_set_key ( botan_mac_t mac,
const uint8_t * key,
size_t key_len )

Sets the key on the MAC

Parameters
macmac object
keybuffer holding the key
key_lensize of the key buffer in bytes
Returns
0 on success, a negative value on failure

Definition at line 40 of file ffi_mac.cpp.

40 {
41 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.set_key(key, key_len); });
42}

References BOTAN_FFI_VISIT.

◆ botan_mac_set_nonce()

int botan_mac_set_nonce ( botan_mac_t mac,
const uint8_t * nonce,
size_t nonce_len )

Sets the nonce on the MAC

Parameters
macmac object
noncebuffer holding the key
nonce_lensize of the key buffer in bytes
Returns
0 on success, a negative value on failure

Definition at line 44 of file ffi_mac.cpp.

44 {
45 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.start(nonce, nonce_len); });
46}

References BOTAN_FFI_VISIT.

◆ botan_mac_update()

int botan_mac_update ( botan_mac_t mac,
const uint8_t * buf,
size_t len )

Send more input to the message authentication code

Parameters
macmac object
bufinput buffer
lennumber of bytes to read from the input buffer
Returns
0 on success, a negative value on failure

Definition at line 56 of file ffi_mac.cpp.

56 {
57 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.update(buf, len); });
58}

References BOTAN_FFI_VISIT.