Botan 3.14.1
Crypto and TLS for C&
mac.cpp
Go to the documentation of this file.
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/algorithm_spec.h>
12#include <botan/internal/ct_utils.h>
13#include <botan/internal/probe_providers.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
49namespace Botan {
50
51std::unique_ptr<MessageAuthenticationCode> MessageAuthenticationCode::create(std::string_view algo_spec,
52 std::string_view provider) {
53 const AlgorithmSpec req(algo_spec);
54
55#if defined(BOTAN_HAS_BLAKE2BMAC)
56 if(auto m = req.match("BLAKE2b|Blake2b({bits:int=512})")) {
57 if(provider.empty() || provider == "base") {
58 return std::make_unique<BLAKE2bMAC>(m->integer("bits"));
59 }
60 }
61#endif
62
63#if defined(BOTAN_HAS_GMAC)
64 if(auto m = req.match("GMAC({cipher})")) {
65 if(provider.empty() || provider == "base") {
66 if(auto bc = BlockCipher::create(m->str("cipher"))) {
67 return std::make_unique<GMAC>(std::move(bc));
68 }
69 }
70 }
71#endif
72
73#if defined(BOTAN_HAS_HMAC)
74 if(auto m = req.match("HMAC({hash})")) {
75 if(provider.empty() || provider == "base") {
76 if(auto hash = HashFunction::create(m->str("hash"))) {
77 return std::make_unique<HMAC>(std::move(hash));
78 }
79 }
80 }
81#endif
82
83#if defined(BOTAN_HAS_POLY1305)
84 if(req.matches("Poly1305")) {
85 if(provider.empty() || provider == "base") {
86 return std::make_unique<Poly1305>();
87 }
88 }
89#endif
90
91#if defined(BOTAN_HAS_SIPHASH)
92 if(auto m = req.match("SipHash({c:int=2},{d:int=4})")) {
93 if(provider.empty() || provider == "base") {
94 return std::make_unique<SipHash>(m->integer("c"), m->integer("d"));
95 }
96 }
97#endif
98
99#if defined(BOTAN_HAS_CMAC)
100 if(auto m = req.match("CMAC|OMAC({cipher})")) {
101 if(provider.empty() || provider == "base") {
102 if(auto bc = BlockCipher::create(m->str("cipher"))) {
103 return std::make_unique<CMAC>(std::move(bc));
104 }
105 }
106 }
107#endif
108
109#if defined(BOTAN_HAS_ANSI_X919_MAC)
110 if(req.matches("X9.19-MAC")) {
111 if(provider.empty() || provider == "base") {
112 return std::make_unique<ANSI_X919_MAC>();
113 }
114 }
115#endif
116
117#if defined(BOTAN_HAS_KMAC)
118 if(auto m = req.match("KMAC-128({bits:int})")) {
119 if(provider.empty() || provider == "base") {
120 return std::make_unique<KMAC128>(m->integer("bits"));
121 }
122 }
123
124 if(auto m = req.match("KMAC-256({bits:int})")) {
125 if(provider.empty() || provider == "base") {
126 return std::make_unique<KMAC256>(m->integer("bits"));
127 }
128 }
129#endif
130
131 BOTAN_UNUSED(req);
133
134 return nullptr;
135}
136
137std::vector<std::string> MessageAuthenticationCode::providers(std::string_view algo_spec) {
139}
140
141//static
142std::unique_ptr<MessageAuthenticationCode> MessageAuthenticationCode::create_or_throw(std::string_view algo,
143 std::string_view provider) {
144 if(auto mac = MessageAuthenticationCode::create(algo, provider)) {
145 return mac;
146 }
147 throw Lookup_Error("MAC", algo, provider);
148}
149
150/*
151* Default (deterministic) MAC verification operation
152*/
153bool MessageAuthenticationCode::verify_mac_result(std::span<const uint8_t> mac) {
154 secure_vector<uint8_t> our_mac = final();
155
156 if(our_mac.size() != mac.size()) {
157 return false;
158 }
159
160 return CT::is_equal(our_mac.data(), mac.data(), mac.size()).as_bool();
161}
162
163} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
bool matches(const AlgorithmPattern &pattern) const
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
static std::unique_ptr< BlockCipher > create(std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:112
virtual bool verify_mac_result(std::span< const uint8_t > in)
Definition mac.cpp:153
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:142
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:51
virtual std::string provider() const
Definition mac.h:111
static std::vector< std::string > providers(std::string_view algo_spec)
Definition mac.cpp:137
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:803
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128