Coverage Report

Created: 2025-07-18 06:20

/src/Botan-3.4.0/src/lib/ffi/ffi_mac.cpp
Line
Count
Source (jump to first uncovered line)
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
12
extern "C" {
13
14
using namespace Botan_FFI;
15
16
BOTAN_FFI_DECLARE_STRUCT(botan_mac_struct, Botan::MessageAuthenticationCode, 0xA06E8FC1);
17
18
0
int botan_mac_init(botan_mac_t* mac, const char* mac_name, uint32_t flags) {
19
0
   return ffi_guard_thunk(__func__, [=]() -> int {
20
0
      if(!mac || !mac_name || flags != 0) {
21
0
         return BOTAN_FFI_ERROR_NULL_POINTER;
22
0
      }
23
24
0
      std::unique_ptr<Botan::MessageAuthenticationCode> m = Botan::MessageAuthenticationCode::create(mac_name);
25
26
0
      if(m == nullptr) {
27
0
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
28
0
      }
29
30
0
      *mac = new botan_mac_struct(std::move(m));
31
0
      return BOTAN_FFI_SUCCESS;
32
0
   });
33
0
}
34
35
0
int botan_mac_destroy(botan_mac_t mac) {
36
0
   return BOTAN_FFI_CHECKED_DELETE(mac);
37
0
}
38
39
0
int botan_mac_set_key(botan_mac_t mac, const uint8_t* key, size_t key_len) {
40
0
   return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.set_key(key, key_len); });
41
0
}
42
43
0
int botan_mac_set_nonce(botan_mac_t mac, const uint8_t* nonce, size_t nonce_len) {
44
0
   return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.start(nonce, nonce_len); });
45
0
}
46
47
0
int botan_mac_output_length(botan_mac_t mac, size_t* out) {
48
0
   return BOTAN_FFI_VISIT(mac, [=](const auto& m) { *out = m.output_length(); });
49
0
}
50
51
0
int botan_mac_clear(botan_mac_t mac) {
52
0
   return BOTAN_FFI_VISIT(mac, [](auto& m) { m.clear(); });
53
0
}
54
55
0
int botan_mac_update(botan_mac_t mac, const uint8_t* buf, size_t len) {
56
0
   return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.update(buf, len); });
57
0
}
58
59
0
int botan_mac_final(botan_mac_t mac, uint8_t out[]) {
60
0
   return BOTAN_FFI_VISIT(mac, [=](auto& m) { m.final(out); });
61
0
}
62
63
0
int botan_mac_name(botan_mac_t mac, char* name, size_t* name_len) {
64
0
   return BOTAN_FFI_VISIT(mac, [=](const auto& m) { return write_str_output(name, name_len, m.name()); });
65
0
}
66
67
int botan_mac_get_keyspec(botan_mac_t mac,
68
                          size_t* out_minimum_keylength,
69
                          size_t* out_maximum_keylength,
70
0
                          size_t* out_keylength_modulo) {
71
0
   return BOTAN_FFI_VISIT(mac, [=](auto& m) {
72
0
      if(out_minimum_keylength)
73
0
         *out_minimum_keylength = m.minimum_keylength();
74
0
      if(out_maximum_keylength)
75
0
         *out_maximum_keylength = m.maximum_keylength();
76
0
      if(out_keylength_modulo)
77
0
         *out_keylength_modulo = m.key_spec().keylength_multiple();
78
0
   });
79
0
}
80
}