Botan  2.4.0
Crypto and TLS for C++11
mac.h
Go to the documentation of this file.
1 /*
2 * Base class for message authentiction 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 <string>
14 #include <memory>
15 
16 namespace Botan {
17 
18 /**
19 * This class represents Message Authentication Code (MAC) objects.
20 */
22  public SymmetricAlgorithm
23  {
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>
33  create(const std::string& algo_spec,
34  const std::string& provider = "");
35 
36  /*
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>
44  create_or_throw(const std::string& algo_spec,
45  const std::string& provider = "");
46 
47  /**
48  * @return list of available providers for this algorithm, empty if not available
49  */
50  static std::vector<std::string> providers(const std::string& algo_spec);
51 
52  virtual ~MessageAuthenticationCode() = default;
53 
54  /**
55  * Prepare for processing a message under the specified nonce
56  *
57  * Most MACs neither require nor support a nonce; for these algorithms
58  * calling `start_msg` is optional and calling it with anything other than
59  * an empty string is an error. One MAC which *requires* a per-message
60  * nonce be specified is GMAC.
61  *
62  * @param nonce the message nonce bytes
63  * @param nonce_len the size of len in bytes
64  * Default implementation simply rejects all non-empty nonces
65  * since most hash/MAC algorithms do not support randomization
66  */
67  virtual void start_msg(const uint8_t nonce[], size_t nonce_len)
68  {
69  BOTAN_UNUSED(nonce);
70  if(nonce_len > 0)
71  throw Invalid_IV_Length(name(), nonce_len);
72  }
73 
74  /**
75  * Begin processing a message with a nonce
76  *
77  * @param nonce the per message nonce
78  */
79  template<typename Alloc>
80  void start(const std::vector<uint8_t, Alloc>& nonce)
81  {
82  start_msg(nonce.data(), nonce.size());
83  }
84 
85  /**
86  * Begin processing a message.
87  * @param nonce the per message nonce
88  * @param nonce_len length of nonce
89  */
90  void start(const uint8_t nonce[], size_t nonce_len)
91  {
92  start_msg(nonce, nonce_len);
93  }
94 
95  /**
96  * Begin processing a message.
97  */
98  void start()
99  {
100  return start_msg(nullptr, 0);
101  }
102 
103  /**
104  * Verify a MAC.
105  * @param in the MAC to verify as a byte array
106  * @param length the length of param in
107  * @return true if the MAC is valid, false otherwise
108  */
109  virtual bool verify_mac(const uint8_t in[], size_t length);
110 
111  /**
112  * Verify a MAC.
113  * @param in the MAC to verify as a byte array
114  * @return true if the MAC is valid, false otherwise
115  */
116  virtual bool verify_mac(const std::vector<uint8_t>& in)
117  {
118  return verify_mac(in.data(), in.size());
119  }
120 
121  /**
122  * Verify a MAC.
123  * @param in the MAC to verify as a byte array
124  * @return true if the MAC is valid, false otherwise
125  */
126  virtual bool verify_mac(const secure_vector<uint8_t>& in)
127  {
128  return verify_mac(in.data(), in.size());
129  }
130 
131  /**
132  * Get a new object representing the same algorithm as *this
133  */
134  virtual MessageAuthenticationCode* clone() const = 0;
135 
136  /**
137  * @return provider information about this implementation. Default is "base",
138  * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
139  */
140  virtual std::string provider() const { return "base"; }
141 
142  };
143 
145 
146 }
147 
148 #endif
MessageAuthenticationCode MAC
Definition: mac.h:144
virtual std::string provider() const
Definition: mac.h:140
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
virtual void start_msg(const uint8_t nonce[], size_t nonce_len)
Definition: mac.h:67
Definition: alg_id.cpp:13
#define BOTAN_UNUSED(...)
Definition: assert.h:106
virtual bool verify_mac(const std::vector< uint8_t > &in)
Definition: mac.h:116
virtual bool verify_mac(const secure_vector< uint8_t > &in)
Definition: mac.h:126
void start(const std::vector< uint8_t, Alloc > &nonce)
Definition: mac.h:80
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
void start(const uint8_t nonce[], size_t nonce_len)
Definition: mac.h:90