Botan 3.9.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 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.set_key(key, key_len); });
42}
43
44int botan_mac_set_nonce(botan_mac_t mac, const uint8_t* nonce, size_t nonce_len) {
45 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.start(nonce, nonce_len); });
46}
47
48int botan_mac_output_length(botan_mac_t mac, size_t* out) {
49 return BOTAN_FFI_VISIT(mac, [=](const auto& m) { *out = m.output_length(); });
50}
51
53 return BOTAN_FFI_VISIT(mac, [](auto& m) { m.clear(); });
54}
55
56int botan_mac_update(botan_mac_t mac, const uint8_t* buf, size_t len) {
57 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.update(buf, len); });
58}
59
60int botan_mac_final(botan_mac_t mac, uint8_t out[]) {
61 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.final(out); });
62}
63
64int botan_mac_name(botan_mac_t mac, char* name, size_t* name_len) {
65 return BOTAN_FFI_VISIT(mac, [=](const auto& m) { return write_str_output(name, name_len, m.name()); });
66}
67
69 size_t* out_minimum_keylength,
70 size_t* out_maximum_keylength,
71 size_t* out_keylength_modulo) {
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}
84}
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:51
struct botan_mac_struct * botan_mac_t
Definition ffi.h:461
@ 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
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:44
int botan_mac_final(botan_mac_t mac, uint8_t out[])
Definition ffi_mac.cpp:60
int botan_mac_update(botan_mac_t mac, const uint8_t *buf, size_t len)
Definition ffi_mac.cpp:56
int botan_mac_output_length(botan_mac_t mac, size_t *out)
Definition ffi_mac.cpp:48
int botan_mac_name(botan_mac_t mac, char *name, size_t *name_len)
Definition ffi_mac.cpp:64
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:68
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:52
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:185
#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:23
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:251