Botan  2.11.0
Crypto and TLS for C++11
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 
11 extern "C" {
12 
13 using namespace Botan_FFI;
14 
15 BOTAN_FFI_DECLARE_STRUCT(botan_mac_struct, Botan::MessageAuthenticationCode, 0xA06E8FC1);
16 
17 int 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(m.release());
30  return BOTAN_FFI_SUCCESS;
31  });
32  }
33 
35  {
36  return BOTAN_FFI_CHECKED_DELETE(mac);
37  }
38 
39 int botan_mac_set_key(botan_mac_t mac, const uint8_t* key, size_t key_len)
40  {
41  return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { m.set_key(key, key_len); });
42  }
43 
44 int botan_mac_output_length(botan_mac_t mac, size_t* out)
45  {
46  return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { *out = m.output_length(); });
47  }
48 
50  {
51  return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { m.clear(); });
52  }
53 
54 int botan_mac_update(botan_mac_t mac, const uint8_t* buf, size_t len)
55  {
56  return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { m.update(buf, len); });
57  }
58 
59 int botan_mac_final(botan_mac_t mac, uint8_t out[])
60  {
61  return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { m.final(out); });
62  }
63 
64 int botan_mac_name(botan_mac_t mac, char* name, size_t* name_len)
65  {
67  return write_str_output(name, name_len, m.name()); });
68  }
69 
71  size_t* out_minimum_keylength,
72  size_t* out_maximum_keylength,
73  size_t* out_keylength_modulo)
74  {
76  if(out_minimum_keylength)
77  *out_minimum_keylength = m.minimum_keylength();
78  if(out_maximum_keylength)
79  *out_maximum_keylength = m.maximum_keylength();
80  if(out_keylength_modulo)
81  *out_keylength_modulo = m.key_spec().keylength_multiple();
82  });
83  }
84 
85 }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:86
static std::unique_ptr< MessageAuthenticationCode > create(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:46
int botan_mac_final(botan_mac_t mac, uint8_t out[])
Definition: ffi_mac.cpp:59
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:70
int botan_mac_name(botan_mac_t mac, char *name, size_t *name_len)
Definition: ffi_mac.cpp:64
Flags flags(Flag flags)
Definition: p11.h:858
int botan_mac_clear(botan_mac_t mac)
Definition: ffi_mac.cpp:49
struct botan_mac_struct * botan_mac_t
Definition: ffi.h:335
void final(uint8_t out[])
Definition: buf_comp.h:98
int write_str_output(uint8_t out[], size_t *out_len, const std::string &str)
Definition: ffi_util.h:151
int botan_mac_output_length(botan_mac_t mac, size_t *out)
Definition: ffi_mac.cpp:44
std::string name
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:65
int botan_mac_update(botan_mac_t mac, const uint8_t *buf, size_t len)
Definition: ffi_mac.cpp:54
int botan_mac_destroy(botan_mac_t mac)
Definition: ffi_mac.cpp:34
virtual void clear()=0
size_t minimum_keylength() const
Definition: sym_algo.h:46
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:120
BOTAN_FFI_DECLARE_STRUCT(botan_mac_struct, Botan::MessageAuthenticationCode, 0xA06E8FC1)
void update(const uint8_t in[], size_t length)
Definition: buf_comp.h:34
int botan_mac_init(botan_mac_t *mac, const char *mac_name, uint32_t flags)
Definition: ffi_mac.cpp:17
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92
int botan_mac_set_key(botan_mac_t mac, const uint8_t *key, size_t key_len)
Definition: ffi_mac.cpp:39
virtual size_t output_length() const =0