Botan 3.13.0
Crypto and TLS for C&
mac.h
Go to the documentation of this file.
1/*
2* Base class for message authentication codes
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_MESSAGE_AUTH_CODE_BASE_H_
9#define BOTAN_MESSAGE_AUTH_CODE_BASE_H_
10
11#include <botan/buf_comp.h>
12#include <botan/sym_algo.h>
13#include <memory>
14#include <span>
15#include <string>
16
17namespace Botan {
18
19/**
20* This class represents Message Authentication Code (MAC) objects.
21*/
23 public SymmetricAlgorithm {
24 public:
25 /**
26 * Create an instance based on a name
27 * If provider is empty then best available is chosen.
28 * @param algo_spec algorithm name
29 * @param provider provider implementation to use
30 * @return a null pointer if the algo/provider combination cannot be found
31 */
32 static std::unique_ptr<MessageAuthenticationCode> create(std::string_view algo_spec,
33 std::string_view provider = "");
34
35 /**
36 * Create an instance based on a name, throwing if it is not available
37 * Create an instance based on a name
38 * If provider is empty then best available is chosen.
39 * @param algo_spec algorithm name
40 * @param provider provider implementation to use
41 * Throws a Lookup_Error if algo/provider combination cannot be found
42 */
43 static std::unique_ptr<MessageAuthenticationCode> create_or_throw(std::string_view algo_spec,
44 std::string_view provider = "");
45
46 /**
47 * List the providers available for a given MAC
48 * @return list of available providers for this algorithm, empty if not available
49 */
50 static std::vector<std::string> providers(std::string_view algo_spec);
51
52 /**
53 * Prepare for processing a message under the specified nonce
54 * Calling start() abandons any partial message and begins a new one.
55 *
56 * Most MACs neither require nor support a nonce; for these algorithms
57 * calling start() is optional and calling it with anything other than
58 * an empty string is an error. One MAC which *requires* a per-message
59 * nonce be specified is GMAC.
60 *
61 * Default implementation simply rejects all non-empty nonces
62 * since most hash/MAC algorithms do not support randomization
63 *
64 * @param nonce the message nonce bytes
65 */
66 void start(std::span<const uint8_t> nonce) { start_msg(nonce); }
67
68 /**
69 * Begin processing a message.
70 * @param nonce the per message nonce
71 * @param nonce_len length of nonce
72 */
73 void start(const uint8_t nonce[], size_t nonce_len) { start_msg({nonce, nonce_len}); }
74
75 /**
76 * Begin processing a message.
77 */
78 void start() { return start_msg({}); }
79
80 /**
81 * Verify a MAC.
82 * @param in the MAC to verify as a byte array
83 * @param length the length of param in
84 * @return true if the MAC is valid, false otherwise
85 */
86 bool verify_mac(const uint8_t in[], size_t length) { return verify_mac_result({in, length}); }
87
88 /**
89 * Verify a MAC.
90 * @param in the MAC to verify as a byte array
91 * @return true if the MAC is valid, false otherwise
92 */
93 bool verify_mac(std::span<const uint8_t> in) { return verify_mac_result(in); }
94
95 /**
96 * Create a new uninitialized object of the same type
97 * @return new object representing the same algorithm as *this
98 */
99 virtual std::unique_ptr<MessageAuthenticationCode> new_object() const = 0;
100
101 /**
102 * Get a new object representing the same algorithm as *this
103 */
104 MessageAuthenticationCode* clone() const { return this->new_object().release(); }
105
106 /**
107 * Return the name of the provider implementing this object
108 * @return provider information about this implementation. Default is "base",
109 * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
110 */
111 virtual std::string provider() const { return "base"; }
112
113 /**
114 * @return if a fresh key must be set for each message that is processed.
115 *
116 * This is required for certain polynomial-based MACs which are insecure
117 * if a key is ever reused for two different messages.
118 */
119 virtual bool fresh_key_required_per_message() const { return false; }
120
121 protected:
122 /**
123 * Prepare for processing a message under the specified nonce
124 *
125 * This should reset any state associated with any message currently being
126 * processed.
127 */
128 virtual void start_msg(std::span<const uint8_t> nonce) = 0;
129
130 /**
131 * Verify the MACs final result
132 */
133 virtual bool verify_mac_result(std::span<const uint8_t> in);
134};
135
136/**
137* A shorter alias for MessageAuthenticationCode
138*/
140
141} // namespace Botan
142
143#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
virtual bool verify_mac_result(std::span< const uint8_t > in)
Definition mac.cpp:160
bool verify_mac(std::span< const uint8_t > in)
Definition mac.h:93
virtual bool fresh_key_required_per_message() const
Definition mac.h:119
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:149
virtual std::unique_ptr< MessageAuthenticationCode > new_object() const =0
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:50
virtual void start_msg(std::span< const uint8_t > nonce)=0
virtual std::string provider() const
Definition mac.h:111
bool verify_mac(const uint8_t in[], size_t length)
Definition mac.h:86
static std::vector< std::string > providers(std::string_view algo_spec)
Definition mac.cpp:144
void start(std::span< const uint8_t > nonce)
Definition mac.h:66
MessageAuthenticationCode * clone() const
Definition mac.h:104
void start(const uint8_t nonce[], size_t nonce_len)
Definition mac.h:73
MessageAuthenticationCode MAC
Definition mac.h:139