Botan  2.1.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 
15 namespace Botan {
16 
17 /**
18 * This class represents Message Authentication Code (MAC) objects.
19 */
21  public SymmetricAlgorithm
22  {
23  public:
24  /**
25  * Create an instance based on a name
26  * If provider is empty then best available is chosen.
27  * @param algo_spec algorithm name
28  * @param provider provider implementation to use
29  * @return a null pointer if the algo/provider combination cannot be found
30  */
31  static std::unique_ptr<MessageAuthenticationCode>
32  create(const std::string& algo_spec,
33  const std::string& provider = "");
34 
35  /*
36  * Create an instance based on a name
37  * If provider is empty then best available is chosen.
38  * @param algo_spec algorithm name
39  * @param provider provider implementation to use
40  * Throws a Lookup_Error if algo/provider combination cannot be found
41  */
42  static std::unique_ptr<MessageAuthenticationCode>
43  create_or_throw(const std::string& algo_spec,
44  const std::string& provider = "");
45 
46  /**
47  * @return list of available providers for this algorithm, empty if not available
48  */
49  static std::vector<std::string> providers(const std::string& algo_spec);
50 
51  virtual ~MessageAuthenticationCode() = default;
52 
53  /**
54  * Prepare for processing a message under the specified nonce
55  *
56  * Most MACs neither require nor support a nonce; for these algorithms
57  * calling `start_msg` 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  * @param nonce the message nonce bytes
62  * @param nonce_len the size of len in bytes
63  * Default implementation simply rejects all non-empty nonces
64  * since most hash/MAC algorithms do not support randomization
65  */
66  virtual void start_msg(const uint8_t nonce[], size_t nonce_len)
67  {
68  BOTAN_UNUSED(nonce);
69  if(nonce_len > 0)
70  throw Invalid_IV_Length(name(), nonce_len);
71  }
72 
73  /**
74  * Begin processing a message with a nonce
75  *
76  * @param nonce the per message nonce
77  */
78  template<typename Alloc>
79  void start(const std::vector<uint8_t, Alloc>& nonce)
80  {
81  start_msg(nonce.data(), nonce.size());
82  }
83 
84  /**
85  * Begin processing a message.
86  * @param nonce the per message nonce
87  * @param nonce_len length of nonce
88  */
89  void start(const uint8_t nonce[], size_t nonce_len)
90  {
91  start_msg(nonce, nonce_len);
92  }
93 
94  /**
95  * Begin processing a message.
96  */
97  void start()
98  {
99  return start_msg(nullptr, 0);
100  }
101 
102  /**
103  * Verify a MAC.
104  * @param in the MAC to verify as a byte array
105  * @param length the length of param in
106  * @return true if the MAC is valid, false otherwise
107  */
108  virtual bool verify_mac(const uint8_t in[], size_t length);
109 
110  /**
111  * Verify a MAC.
112  * @param in the MAC to verify as a byte array
113  * @return true if the MAC is valid, false otherwise
114  */
115  virtual bool verify_mac(const std::vector<uint8_t>& in)
116  {
117  return verify_mac(in.data(), in.size());
118  }
119 
120  /**
121  * Verify a MAC.
122  * @param in the MAC to verify as a byte array
123  * @return true if the MAC is valid, false otherwise
124  */
125  virtual bool verify_mac(const secure_vector<uint8_t>& in)
126  {
127  return verify_mac(in.data(), in.size());
128  }
129 
130  /**
131  * Get a new object representing the same algorithm as *this
132  */
133  virtual MessageAuthenticationCode* clone() const = 0;
134 
135  /**
136  * @return provider information about this implementation. Default is "base",
137  * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
138  */
139  virtual std::string provider() const { return "base"; }
140 
141  };
142 
144 
145 }
146 
147 #endif
MessageAuthenticationCode MAC
Definition: mac.h:143
virtual std::string provider() const
Definition: mac.h:139
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
#define BOTAN_UNUSED(v)
Definition: assert.h:92
virtual void start_msg(const uint8_t nonce[], size_t nonce_len)
Definition: mac.h:66
Definition: alg_id.cpp:13
virtual bool verify_mac(const std::vector< uint8_t > &in)
Definition: mac.h:115
virtual bool verify_mac(const secure_vector< uint8_t > &in)
Definition: mac.h:125
void start(const std::vector< uint8_t, Alloc > &nonce)
Definition: mac.h:79
void start(const uint8_t nonce[], size_t nonce_len)
Definition: mac.h:89