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

randombit / botan / 6546858227

17 Oct 2023 12:02PM UTC coverage: 91.71% (+0.002%) from 91.708%
6546858227

push

github

randombit
Merge GH #3760 Move constant time memory comparisons to ct_utils.h

80095 of 87335 relevant lines covered (91.71%)

8508512.25 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/mem_ops.h>
12
#include <botan/internal/ct_utils.h>
13
#include <botan/internal/scan_name.h>
14

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

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

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

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

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

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

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

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

49
namespace Botan {
50

51
std::unique_ptr<MessageAuthenticationCode> MessageAuthenticationCode::create(std::string_view algo_spec,
30,946✔
52
                                                                             std::string_view provider) {
53
   const SCAN_Name req(algo_spec);
30,946✔
54

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

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

71
#if defined(BOTAN_HAS_HMAC)
72
   if(req.algo_name() == "HMAC" && req.arg_count() == 1) {
30,279✔
73
      if(provider.empty() || provider == "base") {
16,483✔
74
         if(auto hash = HashFunction::create(req.arg(0))) {
32,668✔
75
            return std::make_unique<HMAC>(std::move(hash));
14,876✔
76
         }
16,334✔
77
      }
78
   }
79
#endif
80

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

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

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

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

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

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

137
   BOTAN_UNUSED(req);
1,502✔
138
   BOTAN_UNUSED(provider);
1,502✔
139

140
   return nullptr;
1,502✔
141
}
30,946✔
142

143
std::vector<std::string> MessageAuthenticationCode::providers(std::string_view algo_spec) {
638✔
144
   return probe_providers_of<MessageAuthenticationCode>(algo_spec);
1,276✔
145
}
146

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

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

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

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

173
   return CT::is_equal(our_mac.data(), mac.data(), mac.size()).as_bool();
3,512✔
174
}
1,758✔
175

176
}  // 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

© 2025 Coveralls, Inc