Crypto++  5.6.3
Free C++ class library of cryptographic schemes
vmac.h
1 #ifndef CRYPTOPP_VMAC_H
2 #define CRYPTOPP_VMAC_H
3 
4 #include "cryptlib.h"
5 #include "iterhash.h"
6 #include "seckey.h"
7 
8 #if CRYPTOPP_BOOL_X32
9 # define CRYPTOPP_DISABLE_VMAC_ASM
10 #endif
11 
12 NAMESPACE_BEGIN(CryptoPP)
13 
14 //! \class VMAC_Base
15 //! \brief Class specific methods used to operate the MAC.
17 {
18 public:
19  std::string AlgorithmName() const {return std::string("VMAC(") + GetCipher().AlgorithmName() + ")-" + IntToString(DigestSize()*8);}
20  unsigned int IVSize() const {return GetCipher().BlockSize();}
21  unsigned int MinIVLength() const {return 1;}
22  void Resynchronize(const byte *nonce, int length=-1);
23  void GetNextIV(RandomNumberGenerator &rng, byte *IV);
24  unsigned int DigestSize() const {return m_is128 ? 16 : 8;};
25  void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
26  void TruncatedFinal(byte *mac, size_t size);
27  unsigned int BlockSize() const {return m_L1KeyLength;}
28  ByteOrder GetByteOrder() const {return LITTLE_ENDIAN_ORDER;}
29  unsigned int OptimalDataAlignment() const;
30 
31 protected:
32  virtual BlockCipher & AccessCipher() =0;
33  virtual int DefaultDigestSize() const =0;
34  const BlockCipher & GetCipher() const {return const_cast<VMAC_Base *>(this)->AccessCipher();}
35  void HashEndianCorrectedBlock(const word64 *data);
36  size_t HashMultipleBlocks(const word64 *input, size_t length);
37  void Init() {}
38  word64* StateBuf() {return NULL;}
39  word64* DataBuf() {return (word64 *)m_data();}
40 
41  void VHASH_Update_SSE2(const word64 *data, size_t blocksRemainingInWord64, int tagPart);
42 #if !(defined(_MSC_VER) && _MSC_VER < 1300) // can't use function template here with VC6
43  template <bool T_128BitTag>
44 #endif
45  void VHASH_Update_Template(const word64 *data, size_t blockRemainingInWord128);
46  void VHASH_Update(const word64 *data, size_t blocksRemainingInWord128);
47 
48 #if CRYPTOPP_DOXYGEN_PROCESSING
49  private: // hide from documentation
50 #endif
51 
52  CRYPTOPP_BLOCK_1(polyState, word64, 4*(m_is128+1))
53  CRYPTOPP_BLOCK_2(nhKey, word64, m_L1KeyLength/sizeof(word64) + 2*m_is128)
54  CRYPTOPP_BLOCK_3(data, byte, m_L1KeyLength)
55  CRYPTOPP_BLOCK_4(l3Key, word64, 2*(m_is128+1))
56  CRYPTOPP_BLOCK_5(nonce, byte, IVSize())
57  CRYPTOPP_BLOCK_6(pad, byte, IVSize())
58  CRYPTOPP_BLOCKS_END(6)
59 
60  bool m_is128, m_padCached, m_isFirstBlock;
61  int m_L1KeyLength;
62 };
63 
64 //! \class VMAC
65 //! \brief The VMAC message authentication code
66 //! \details VMAC is a block cipher-based message authentication code algorithm
67 //! using a universal hash proposed by Ted Krovetz and Wei Dai in April 2007. The
68 //! algorithm was designed for high performance backed by a formal analysis.
69 //! \tparam T_BlockCipher block cipher
70 //! \tparam T_DigestBitSize digest size, in bits
71 //! \sa <a href="http://www.cryptolounge.org/wiki/VMAC">VMAC</a> at the Crypto Lounge.
72 template <class T_BlockCipher, int T_DigestBitSize = 128>
73 class VMAC : public SimpleKeyingInterfaceImpl<VMAC_Base, SameKeyLengthAs<T_BlockCipher, SimpleKeyingInterface::UNIQUE_IV, T_BlockCipher::BLOCKSIZE> >
74 {
75 public:
76  static std::string StaticAlgorithmName() {return std::string("VMAC(") + T_BlockCipher::StaticAlgorithmName() + ")-" + IntToString(T_DigestBitSize);}
77 
78 private:
79  BlockCipher & AccessCipher() {return m_cipher;}
80  int DefaultDigestSize() const {return T_DigestBitSize/8;}
81  typename T_BlockCipher::Encryption m_cipher;
82 };
83 
84 NAMESPACE_END
85 
86 #endif
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: vmac.h:19
The VMAC message authentication code.
Definition: vmac.h:73
const char * DigestSize()
int, in bytes
Definition: argnames.h:78
Interface for message authentication codes.
Definition: cryptlib.h:1027
ByteOrder
Provides the byte ordering.
Definition: cryptlib.h:116
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: vmac.h:24
Class specific methods used to operate the MAC.
Definition: vmac.h:16
Provides class member functions to access SimpleKeyingInterface constants.
Definition: seckey.h:251
Abstract base classes that provide a uniform interface to this library.
unsigned int BlockSize() const
Provides the block size of the compression function.
Definition: vmac.h:27
Interface for random number generators.
Definition: cryptlib.h:1085
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1013
Classes and functions for implementing secret key algorithms.
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
Definition: argnames.h:21
unsigned int MinIVLength() const
returns minimal length of IVs accepted by this object
Definition: vmac.h:21
unsigned int IVSize() const
Returns length of the IV accepted by this object.
Definition: vmac.h:20
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:449
Crypto++ library namespace.
Interface for retrieving values given their names.
Definition: cryptlib.h:261