Botan 3.5.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(!mac || !mac_name || flags != 0) {
22 }
23
24 std::unique_ptr<Botan::MessageAuthenticationCode> m = Botan::MessageAuthenticationCode::create(mac_name);
25
26 if(m == nullptr) {
28 }
29
30 *mac = new botan_mac_struct(std::move(m));
31 return BOTAN_FFI_SUCCESS;
32 });
33}
34
38
39int botan_mac_set_key(botan_mac_t mac, const uint8_t* key, size_t key_len) {
40 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.set_key(key, key_len); });
41}
42
43int botan_mac_set_nonce(botan_mac_t mac, const uint8_t* nonce, size_t nonce_len) {
44 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.start(nonce, nonce_len); });
45}
46
47int botan_mac_output_length(botan_mac_t mac, size_t* out) {
48 return BOTAN_FFI_VISIT(mac, [=](const auto& m) { *out = m.output_length(); });
49}
50
52 return BOTAN_FFI_VISIT(mac, [](auto& m) { m.clear(); });
53}
54
55int botan_mac_update(botan_mac_t mac, const uint8_t* buf, size_t len) {
56 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.update(buf, len); });
57}
58
59int botan_mac_final(botan_mac_t mac, uint8_t out[]) {
60 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.final(out); });
61}
62
63int botan_mac_name(botan_mac_t mac, char* name, size_t* name_len) {
64 return BOTAN_FFI_VISIT(mac, [=](const auto& m) { return write_str_output(name, name_len, m.name()); });
65}
66
68 size_t* out_minimum_keylength,
69 size_t* out_maximum_keylength,
70 size_t* out_keylength_modulo) {
71 return BOTAN_FFI_VISIT(mac, [=](auto& m) {
72 if(out_minimum_keylength)
73 *out_minimum_keylength = m.minimum_keylength();
74 if(out_maximum_keylength)
75 *out_maximum_keylength = m.maximum_keylength();
76 if(out_keylength_modulo)
77 *out_keylength_modulo = m.key_spec().keylength_multiple();
78 });
79}
80}
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:51
std::string name
struct botan_mac_struct * botan_mac_t
Definition ffi.h:428
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:124
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:118
@ BOTAN_FFI_SUCCESS
Definition ffi.h:103
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:35
int botan_mac_set_nonce(botan_mac_t mac, const uint8_t *nonce, size_t nonce_len)
Definition ffi_mac.cpp:43
int botan_mac_final(botan_mac_t mac, uint8_t out[])
Definition ffi_mac.cpp:59
int botan_mac_update(botan_mac_t mac, const uint8_t *buf, size_t len)
Definition ffi_mac.cpp:55
int botan_mac_output_length(botan_mac_t mac, size_t *out)
Definition ffi_mac.cpp:47
int botan_mac_name(botan_mac_t mac, char *name, size_t *name_len)
Definition ffi_mac.cpp:63
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:67
int botan_mac_set_key(botan_mac_t mac, const uint8_t *key, size_t key_len)
Definition ffi_mac.cpp:39
int botan_mac_clear(botan_mac_t mac)
Definition ffi_mac.cpp:51
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:124
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:143
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:51
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition ffi_util.h:205
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:116