vmac.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // vmac.h - originally written and placed in the public domain by Wei Dai
  2. /// \file vmac.h
  3. /// \brief Classes for the VMAC message authentication code
  4. /// \since Crypto++ 5.5
  5. #ifndef CRYPTOPP_VMAC_H
  6. #define CRYPTOPP_VMAC_H
  7. #include "cryptlib.h"
  8. #include "iterhash.h"
  9. #include "seckey.h"
  10. // Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler
  11. // error with .intel_syntax, http://llvm.org/bugs/show_bug.cgi?id=24232
  12. #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_MIXED_ASM)
  13. # define CRYPTOPP_DISABLE_VMAC_ASM 1
  14. #endif
  15. NAMESPACE_BEGIN(CryptoPP)
  16. /// \brief VMAC message authentication code base class
  17. /// \since Crypto++ 5.5
  18. class VMAC_Base : public IteratedHashBase<word64, MessageAuthenticationCode>
  19. {
  20. public:
  21. std::string AlgorithmName() const {return std::string("VMAC(") + GetCipher().AlgorithmName() + ")-" + IntToString(DigestSize()*8);}
  22. std::string AlgorithmProvider() const {return GetCipher().AlgorithmProvider();}
  23. unsigned int IVSize() const {return GetCipher().BlockSize();}
  24. unsigned int MinIVLength() const {return 1;}
  25. void Resynchronize(const byte *nonce, int length=-1);
  26. void GetNextIV(RandomNumberGenerator &rng, byte *IV);
  27. unsigned int DigestSize() const {return m_is128 ? 16 : 8;};
  28. void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
  29. void TruncatedFinal(byte *mac, size_t size);
  30. unsigned int BlockSize() const {return m_L1KeyLength;}
  31. ByteOrder GetByteOrder() const {return LITTLE_ENDIAN_ORDER;}
  32. unsigned int OptimalDataAlignment() const;
  33. protected:
  34. virtual BlockCipher & AccessCipher() =0;
  35. virtual int DefaultDigestSize() const =0;
  36. const BlockCipher & GetCipher() const {return const_cast<VMAC_Base *>(this)->AccessCipher();}
  37. void HashEndianCorrectedBlock(const word64 *data);
  38. size_t HashMultipleBlocks(const word64 *input, size_t length);
  39. void Init() {}
  40. word64* StateBuf() {return NULLPTR;}
  41. word64* DataBuf() {return (word64 *)(void*)m_data();}
  42. void VHASH_Update_SSE2(const word64 *data, size_t blocksRemainingInWord64, int tagPart);
  43. template <bool T_128BitTag>
  44. void VHASH_Update_Template(const word64 *data, size_t blockRemainingInWord128);
  45. void VHASH_Update(const word64 *data, size_t blocksRemainingInWord128);
  46. CRYPTOPP_BLOCK_1(polyState, word64, (m_is128 ? 8 : 4))
  47. CRYPTOPP_BLOCK_2(nhKey, word64, m_L1KeyLength/sizeof(word64) + 2*m_is128)
  48. CRYPTOPP_BLOCK_3(data, byte, m_L1KeyLength)
  49. CRYPTOPP_BLOCK_4(l3Key, word64, (m_is128 ? 4 : 2))
  50. CRYPTOPP_BLOCK_5(nonce, byte, IVSize())
  51. CRYPTOPP_BLOCK_6(pad, byte, IVSize())
  52. CRYPTOPP_BLOCKS_END(6)
  53. bool m_is128, m_padCached, m_isFirstBlock;
  54. unsigned int m_L1KeyLength;
  55. };
  56. /// \brief VMAC message authentication code
  57. /// \tparam T_BlockCipher block cipher
  58. /// \tparam T_DigestBitSize digest size, in bits
  59. /// \details VMAC is a block cipher-based message authentication code algorithm
  60. /// using a universal hash proposed by Ted Krovetz and Wei Dai in April 2007. The
  61. /// algorithm was designed for high performance backed by a formal analysis.
  62. /// \details The implementation is based on Ted Krovetz's public domain vmac.c
  63. /// and <a href="http://tools.ietf.org/html/draft-krovetz-vmac-01">draft-krovetz-vmac-01.txt</a>.
  64. /// \sa <a href="http://www.cryptolounge.org/wiki/VMAC">VMAC</a>.
  65. /// \since Crypto++ 5.5
  66. template <class T_BlockCipher, int T_DigestBitSize = 128>
  67. class VMAC : public SimpleKeyingInterfaceImpl<VMAC_Base, SameKeyLengthAs<T_BlockCipher, SimpleKeyingInterface::UNIQUE_IV, T_BlockCipher::BLOCKSIZE> >
  68. {
  69. public:
  70. static std::string StaticAlgorithmName() {return std::string("VMAC(") + T_BlockCipher::StaticAlgorithmName() + ")-" + IntToString(T_DigestBitSize);}
  71. private:
  72. BlockCipher & AccessCipher() {return m_cipher;}
  73. int DefaultDigestSize() const {return T_DigestBitSize/8;}
  74. typename T_BlockCipher::Encryption m_cipher;
  75. };
  76. NAMESPACE_END
  77. #endif