Botan 3.0.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#include <botan/internal/ffi_util.h>
9#include <botan/mac.h>
10
11extern "C" {
12
13using namespace Botan_FFI;
14
16
17int botan_mac_init(botan_mac_t* mac, const char* mac_name, uint32_t flags)
18 {
19 return ffi_guard_thunk(__func__, [=]() -> int {
20 if(!mac || !mac_name || flags != 0)
22
23 std::unique_ptr<Botan::MessageAuthenticationCode> m =
25
26 if(m == nullptr)
28
29 *mac = new botan_mac_struct(std::move(m));
30 return BOTAN_FFI_SUCCESS;
31 });
32 }
33
35 {
36 return BOTAN_FFI_CHECKED_DELETE(mac);
37 }
38
39int botan_mac_set_key(botan_mac_t mac, const uint8_t* key, size_t key_len)
40 {
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 {
46 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.start(nonce, nonce_len); });
47 }
48
50 {
51 return BOTAN_FFI_VISIT(mac, [=](const auto& m) { *out = m.output_length(); });
52 }
53
55 {
56 return BOTAN_FFI_VISIT(mac, [](auto& m) { m.clear(); });
57 }
58
59int botan_mac_update(botan_mac_t mac, const uint8_t* buf, size_t len)
60 {
61 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.update(buf, len); });
62 }
63
64int botan_mac_final(botan_mac_t mac, uint8_t out[])
65 {
66 return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.final(out); });
67 }
68
69int botan_mac_name(botan_mac_t mac, char* name, size_t* name_len)
70 {
71 return BOTAN_FFI_VISIT(mac, [=](const auto& m) {
72 return write_str_output(name, name_len, m.name()); });
73 }
74
76 size_t* out_minimum_keylength,
77 size_t* out_maximum_keylength,
78 size_t* out_keylength_modulo)
79 {
80 return BOTAN_FFI_VISIT(mac, [=](auto& m) {
81 if(out_minimum_keylength)
82 *out_minimum_keylength = m.minimum_keylength();
83 if(out_maximum_keylength)
84 *out_maximum_keylength = m.maximum_keylength();
85 if(out_keylength_modulo)
86 *out_keylength_modulo = m.key_spec().keylength_multiple();
87 });
88 }
89
90}
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition: mac.cpp:46
std::string name
struct botan_mac_struct * botan_mac_t
Definition: ffi.h:398
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:91
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:85
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:70
int botan_mac_init(botan_mac_t *mac, const char *mac_name, uint32_t flags)
Definition: ffi_mac.cpp:17
int botan_mac_destroy(botan_mac_t mac)
Definition: ffi_mac.cpp:34
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:64
int botan_mac_update(botan_mac_t mac, const uint8_t *buf, size_t len)
Definition: ffi_mac.cpp:59
int botan_mac_output_length(botan_mac_t mac, size_t *out)
Definition: ffi_mac.cpp:49
int botan_mac_name(botan_mac_t mac, char *name, size_t *name_len)
Definition: ffi_mac.cpp:69
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:75
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:54
#define BOTAN_FFI_VISIT(obj, lambda)
Definition: ffi_util.h:126
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:145
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition: ffi_util.h:55
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition: ffi_util.h:219
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition: ffi.cpp:120