Botan 3.13.0
Crypto and TLS for C&
ffi_mac.cpp
Go to the documentation of this file.
1/*
2* (C) 2015,2017 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/ffi.h>
8
9#include <botan/mac.h>
10#include <botan/internal/ffi_util.h>
11
12extern "C" {
13
14using namespace Botan_FFI;
15
17
18int botan_mac_init(botan_mac_t* mac, const char* mac_name, uint32_t flags) {
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}
35
39
40int botan_mac_set_key(botan_mac_t mac, const uint8_t* key, size_t key_len) {
41 if(key_len > 0 && key == nullptr) {
43 }
44 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.set_key(key, key_len); });
45}
46
47int botan_mac_set_nonce(botan_mac_t mac, const uint8_t* nonce, size_t nonce_len) {
48 if(nonce_len > 0 && nonce == nullptr) {
50 }
51
52 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.start(nonce, nonce_len); });
53}
54
55int botan_mac_output_length(botan_mac_t mac, size_t* out) {
56 if(out == nullptr) {
58 }
59 return BOTAN_FFI_VISIT(mac, [=](const auto& m) { *out = m.output_length(); });
60}
61
63 return BOTAN_FFI_VISIT(mac, [](auto& m) { m.clear(); });
64}
65
66int botan_mac_update(botan_mac_t mac, const uint8_t* buf, size_t len) {
67 if(len == 0) {
68 return BOTAN_FFI_SUCCESS;
69 }
70 if(buf == nullptr) {
72 }
73 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.update(buf, len); });
74}
75
76int botan_mac_final(botan_mac_t mac, uint8_t out[]) {
77 if(out == nullptr) {
79 }
80 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.final(out); });
81}
82
83int botan_mac_name(botan_mac_t mac, char* name, size_t* name_len) {
84 return BOTAN_FFI_VISIT(mac, [=](const auto& m) { return write_str_output(name, name_len, m.name()); });
85}
86
88 size_t* out_minimum_keylength,
89 size_t* out_maximum_keylength,
90 size_t* out_keylength_modulo) {
91 return BOTAN_FFI_VISIT(mac, [=](auto& m) {
92 if(out_minimum_keylength) {
93 *out_minimum_keylength = m.minimum_keylength();
94 }
95 if(out_maximum_keylength) {
96 *out_maximum_keylength = m.maximum_keylength();
97 }
98 if(out_keylength_modulo) {
99 *out_keylength_modulo = m.key_spec().keylength_multiple();
100 }
101 });
102}
103}
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:50
struct botan_mac_struct * botan_mac_t
Definition ffi.h:578
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:130
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
int botan_mac_init(botan_mac_t *mac, const char *mac_name, uint32_t flags)
Definition ffi_mac.cpp:18
int botan_mac_destroy(botan_mac_t mac)
Definition ffi_mac.cpp:36
int botan_mac_set_nonce(botan_mac_t mac, const uint8_t *nonce, size_t nonce_len)
Definition ffi_mac.cpp:47
int botan_mac_final(botan_mac_t mac, uint8_t out[])
Definition ffi_mac.cpp:76
int botan_mac_update(botan_mac_t mac, const uint8_t *buf, size_t len)
Definition ffi_mac.cpp:66
int botan_mac_output_length(botan_mac_t mac, size_t *out)
Definition ffi_mac.cpp:55
int botan_mac_name(botan_mac_t mac, char *name, size_t *name_len)
Definition ffi_mac.cpp:83
int botan_mac_get_keyspec(botan_mac_t mac, size_t *out_minimum_keylength, size_t *out_maximum_keylength, size_t *out_keylength_modulo)
Definition ffi_mac.cpp:87
int botan_mac_set_key(botan_mac_t mac, const uint8_t *key, size_t key_len)
Definition ffi_mac.cpp:40
int botan_mac_clear(botan_mac_t mac)
Definition ffi_mac.cpp:62
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:188
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:61
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:54
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:271