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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

97.96
/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/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
namespace Botan {
45

46
std::unique_ptr<MessageAuthenticationCode> MessageAuthenticationCode::create(std::string_view algo_spec,
30,663✔
47
                                                                             std::string_view provider) {
48
   const SCAN_Name req(algo_spec);
30,663✔
49

50
#if defined(BOTAN_HAS_BLAKE2BMAC)
51
   if(req.algo_name() == "Blake2b" || req.algo_name() == "BLAKE2b") {
122,652✔
52
      return std::make_unique<BLAKE2bMAC>(req.arg_as_integer(0, 512));
546✔
53
   }
54
#endif
55

56
#if defined(BOTAN_HAS_GMAC)
57
   if(req.algo_name() == "GMAC" && req.arg_count() == 1) {
90,351✔
58
      if(provider.empty() || provider == "base") {
241✔
59
         if(auto bc = BlockCipher::create(req.arg(0))) {
242✔
60
            return std::make_unique<GMAC>(std::move(bc));
121✔
61
         }
121✔
62
      }
63
   }
64
#endif
65

66
#if defined(BOTAN_HAS_HMAC)
67
   if(req.algo_name() == "HMAC" && req.arg_count() == 1) {
89,988✔
68
      if(provider.empty() || provider == "base") {
16,237✔
69
         if(auto hash = HashFunction::create(req.arg(0))) {
32,176✔
70
            return std::make_unique<HMAC>(std::move(hash));
14,679✔
71
         }
16,088✔
72
      }
73
   }
74
#endif
75

76
#if defined(BOTAN_HAS_POLY1305)
77
   if(req.algo_name() == "Poly1305" && req.arg_count() == 0) {
45,951✔
78
      if(provider.empty() || provider == "base") {
13,422✔
79
         return std::make_unique<Poly1305>();
13,266✔
80
      }
81
   }
82
#endif
83

84
#if defined(BOTAN_HAS_SIPHASH)
85
   if(req.algo_name() == "SipHash") {
4,102✔
86
      if(provider.empty() || provider == "base") {
237✔
87
         return std::make_unique<SipHash>(req.arg_as_integer(0, 2), req.arg_as_integer(1, 4));
119✔
88
      }
89
   }
90
#endif
91

92
#if defined(BOTAN_HAS_CMAC)
93
   if((req.algo_name() == "CMAC" || req.algo_name() == "OMAC") && req.arg_count() == 1) {
6,792✔
94
      if(provider.empty() || provider == "base") {
544✔
95
         if(auto bc = BlockCipher::create(req.arg(0))) {
936✔
96
            return std::make_unique<CMAC>(std::move(bc));
468✔
97
         }
468✔
98
      }
99
   }
100
#endif
101

102
#if defined(BOTAN_HAS_ANSI_X919_MAC)
103
   if(req.algo_name() == "X9.19-MAC") {
2,928✔
104
      if(provider.empty() || provider == "base") {
24✔
105
         return std::make_unique<ANSI_X919_MAC>();
12✔
106
      }
107
   }
108
#endif
109

110
   BOTAN_UNUSED(req);
1,452✔
111
   BOTAN_UNUSED(provider);
1,452✔
112

113
   return nullptr;
1,452✔
114
}
30,663✔
115

116
std::vector<std::string> MessageAuthenticationCode::providers(std::string_view algo_spec) {
631✔
117
   return probe_providers_of<MessageAuthenticationCode>(algo_spec);
1,262✔
118
}
119

120
//static
121
std::unique_ptr<MessageAuthenticationCode> MessageAuthenticationCode::create_or_throw(std::string_view algo,
6,116✔
122
                                                                                      std::string_view provider) {
123
   if(auto mac = MessageAuthenticationCode::create(algo, provider)) {
6,116✔
124
      return mac;
6,115✔
125
   }
6,115✔
126
   throw Lookup_Error("MAC", algo, provider);
1✔
127
}
128

129
void MessageAuthenticationCode::start_msg(const uint8_t nonce[], size_t nonce_len) {
4,989✔
130
   BOTAN_UNUSED(nonce);
4,989✔
131
   if(nonce_len > 0) {
4,989✔
132
      throw Invalid_IV_Length(name(), nonce_len);
×
133
   }
134
}
4,989✔
135

136
/*
137
* Default (deterministic) MAC verification operation
138
*/
139
bool MessageAuthenticationCode::verify_mac_result(const uint8_t mac[], size_t length) {
1,733✔
140
   secure_vector<uint8_t> our_mac = final();
1,733✔
141

142
   if(our_mac.size() != length) {
1,733✔
143
      return false;
144
   }
145

146
   return constant_time_compare(our_mac.data(), mac, length);
1,731✔
147
}
1,733✔
148

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