Botan 3.12.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 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.start(nonce, nonce_len); });
49}
50
51int botan_mac_output_length(botan_mac_t mac, size_t* out) {
52 if(out == nullptr) {
54 }
55 return BOTAN_FFI_VISIT(mac, [=](const auto& m) { *out = m.output_length(); });
56}
57
59 return BOTAN_FFI_VISIT(mac, [](auto& m) { m.clear(); });
60}
61
62int botan_mac_update(botan_mac_t mac, const uint8_t* buf, size_t len) {
63 if(len == 0) {
64 return BOTAN_FFI_SUCCESS;
65 }
66 if(buf == nullptr) {
68 }
69 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.update(buf, len); });
70}
71
72int botan_mac_final(botan_mac_t mac, uint8_t out[]) {
73 if(out == nullptr) {
75 }
76 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.final(out); });
77}
78
79int botan_mac_name(botan_mac_t mac, char* name, size_t* name_len) {
80 return BOTAN_FFI_VISIT(mac, [=](const auto& m) { return write_str_output(name, name_len, m.name()); });
81}
82
84 size_t* out_minimum_keylength,
85 size_t* out_maximum_keylength,
86 size_t* out_keylength_modulo) {
87 return BOTAN_FFI_VISIT(mac, [=](auto& m) {
88 if(out_minimum_keylength) {
89 *out_minimum_keylength = m.minimum_keylength();
90 }
91 if(out_maximum_keylength) {
92 *out_maximum_keylength = m.maximum_keylength();
93 }
94 if(out_keylength_modulo) {
95 *out_keylength_modulo = m.key_spec().keylength_multiple();
96 }
97 });
98}
99}
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:570
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:140
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:132
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:133
@ BOTAN_FFI_SUCCESS
Definition ffi.h:116
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:72
int botan_mac_update(botan_mac_t mac, const uint8_t *buf, size_t len)
Definition ffi_mac.cpp:62
int botan_mac_output_length(botan_mac_t mac, size_t *out)
Definition ffi_mac.cpp:51
int botan_mac_name(botan_mac_t mac, char *name, size_t *name_len)
Definition ffi_mac.cpp:79
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:83
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:58
#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:268