• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.8
/src/lib/mac/mac.cpp
1
/*
2
* Message Authentication Code base class
3
* (C) 1999-2008 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/mac.h>
9

10
#include <botan/exceptn.h>
11
#include <botan/internal/ct_utils.h>
12
#include <botan/internal/scan_name.h>
13

14
#if defined(BOTAN_HAS_CMAC)
15
   #include <botan/internal/cmac.h>
16
#endif
17

18
#if defined(BOTAN_HAS_GMAC)
19
   #include <botan/block_cipher.h>
20
   #include <botan/internal/gmac.h>
21
#endif
22

23
#if defined(BOTAN_HAS_HMAC)
24
   #include <botan/hash.h>
25
   #include <botan/internal/hmac.h>
26
#endif
27

28
#if defined(BOTAN_HAS_POLY1305)
29
   #include <botan/internal/poly1305.h>
30
#endif
31

32
#if defined(BOTAN_HAS_SIPHASH)
33
   #include <botan/internal/siphash.h>
34
#endif
35

36
#if defined(BOTAN_HAS_ANSI_X919_MAC)
37
   #include <botan/internal/x919_mac.h>
38
#endif
39

40
#if defined(BOTAN_HAS_BLAKE2BMAC)
41
   #include <botan/internal/blake2bmac.h>
42
#endif
43

44
#if defined(BOTAN_HAS_KMAC)
45
   #include <botan/internal/kmac.h>
46
#endif
47

48
namespace Botan {
49

50
std::unique_ptr<MessageAuthenticationCode> MessageAuthenticationCode::create(std::string_view algo_spec,
35,524✔
51
                                                                             std::string_view provider) {
52
   const SCAN_Name req(algo_spec);
35,524✔
53

54
#if defined(BOTAN_HAS_BLAKE2BMAC)
55
   if(req.algo_name() == "Blake2b" || req.algo_name() == "BLAKE2b") {
35,524✔
56
      return std::make_unique<BLAKE2bMAC>(req.arg_as_integer(0, 512));
546✔
57
   }
58
#endif
59

60
#if defined(BOTAN_HAS_GMAC)
61
   if(req.algo_name() == "GMAC" && req.arg_count() == 1) {
34,978✔
62
      if(provider.empty() || provider == "base") {
241✔
63
         if(auto bc = BlockCipher::create(req.arg(0))) {
242✔
64
            return std::make_unique<GMAC>(std::move(bc));
121✔
65
         }
121✔
66
      }
67
   }
68
#endif
69

70
#if defined(BOTAN_HAS_HMAC)
71
   if(req.algo_name() == "HMAC" && req.arg_count() == 1) {
34,857✔
72
      if(provider.empty() || provider == "base") {
21,270✔
73
         if(auto hash = HashFunction::create(req.arg(0))) {
42,242✔
74
            return std::make_unique<HMAC>(std::move(hash));
19,253✔
75
         }
21,121✔
76
      }
77
   }
78
#endif
79

80
#if defined(BOTAN_HAS_POLY1305)
81
   if(req.algo_name() == "Poly1305" && req.arg_count() == 0) {
15,604✔
82
      if(provider.empty() || provider == "base") {
13,287✔
83
         return std::make_unique<Poly1305>();
13,115✔
84
      }
85
   }
86
#endif
87

88
#if defined(BOTAN_HAS_SIPHASH)
89
   if(req.algo_name() == "SipHash") {
2,489✔
90
      if(provider.empty() || provider == "base") {
237✔
91
         return std::make_unique<SipHash>(req.arg_as_integer(0, 2), req.arg_as_integer(1, 4));
119✔
92
      }
93
   }
94
#endif
95

96
#if defined(BOTAN_HAS_CMAC)
97
   if((req.algo_name() == "CMAC" || req.algo_name() == "OMAC") && req.arg_count() == 1) {
2,370✔
98
      if(provider.empty() || provider == "base") {
553✔
99
         if(auto bc = BlockCipher::create(req.arg(0))) {
954✔
100
            return std::make_unique<CMAC>(std::move(bc));
477✔
101
         }
477✔
102
      }
103
   }
104
#endif
105

106
#if defined(BOTAN_HAS_ANSI_X919_MAC)
107
   if(req.algo_name() == "X9.19-MAC") {
1,893✔
108
      if(provider.empty() || provider == "base") {
24✔
109
         return std::make_unique<ANSI_X919_MAC>();
12✔
110
      }
111
   }
112
#endif
113

114
#if defined(BOTAN_HAS_KMAC)
115
   if(req.algo_name() == "KMAC-128") {
1,881✔
116
      if(provider.empty() || provider == "base") {
12✔
117
         if(req.arg_count() != 1) {
6✔
118
            throw Invalid_Argument(
×
119
               "invalid algorithm specification for KMAC-128: need exactly one argument for output bit length");
×
120
         }
121
         return std::make_unique<KMAC128>(req.arg_as_integer(0));
6✔
122
      }
123
   }
124

125
   if(req.algo_name() == "KMAC-256") {
1,875✔
126
      if(provider.empty() || provider == "base") {
12✔
127
         if(req.arg_count() != 1) {
6✔
128
            throw Invalid_Argument(
×
129
               "invalid algorithm specification for KMAC-256: need exactly one argument for output bit length");
×
130
         }
131
         return std::make_unique<KMAC256>(req.arg_as_integer(0));
6✔
132
      }
133
   }
134
#endif
135

136
   BOTAN_UNUSED(req);
1,869✔
137
   BOTAN_UNUSED(provider);
1,869✔
138

139
   return nullptr;
1,869✔
140
}
35,524✔
141

142
std::vector<std::string> MessageAuthenticationCode::providers(std::string_view algo_spec) {
603✔
143
   return probe_providers_of<MessageAuthenticationCode>(algo_spec);
603✔
144
}
145

146
//static
147
std::unique_ptr<MessageAuthenticationCode> MessageAuthenticationCode::create_or_throw(std::string_view algo,
8,576✔
148
                                                                                      std::string_view provider) {
149
   if(auto mac = MessageAuthenticationCode::create(algo, provider)) {
8,576✔
150
      return mac;
8,575✔
151
   }
8,575✔
152
   throw Lookup_Error("MAC", algo, provider);
1✔
153
}
154

155
void MessageAuthenticationCode::start_msg(std::span<const uint8_t> nonce) {
5,300✔
156
   BOTAN_UNUSED(nonce);
5,300✔
157
   if(!nonce.empty()) {
5,300✔
158
      throw Invalid_IV_Length(name(), nonce.size());
×
159
   }
160
}
5,300✔
161

162
/*
163
* Default (deterministic) MAC verification operation
164
*/
165
bool MessageAuthenticationCode::verify_mac_result(std::span<const uint8_t> mac) {
1,821✔
166
   secure_vector<uint8_t> our_mac = final();
1,821✔
167

168
   if(our_mac.size() != mac.size()) {
1,821✔
169
      return false;
170
   }
171

172
   return CT::is_equal(our_mac.data(), mac.data(), mac.size()).as_bool();
3,638✔
173
}
1,821✔
174

175
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc