Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:10

0001 // vmac.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file vmac.h

0004 /// \brief Classes for the VMAC message authentication code

0005 /// \since Crypto++ 5.5

0006 
0007 #ifndef CRYPTOPP_VMAC_H
0008 #define CRYPTOPP_VMAC_H
0009 
0010 #include "cryptlib.h"
0011 #include "iterhash.h"
0012 #include "seckey.h"
0013 
0014 // Clang 3.3 integrated assembler crash on Linux. Clang 3.4 due to compiler

0015 // error with .intel_syntax, http://llvm.org/bugs/show_bug.cgi?id=24232

0016 #if CRYPTOPP_BOOL_X32 || defined(CRYPTOPP_DISABLE_MIXED_ASM)
0017 # define CRYPTOPP_DISABLE_VMAC_ASM 1
0018 #endif
0019 
0020 NAMESPACE_BEGIN(CryptoPP)
0021 
0022 /// \brief VMAC message authentication code base class

0023 /// \since Crypto++ 5.5

0024 class VMAC_Base : public IteratedHashBase<word64, MessageAuthenticationCode>
0025 {
0026 public:
0027     std::string AlgorithmName() const {return std::string("VMAC(") + GetCipher().AlgorithmName() + ")-" + IntToString(DigestSize()*8);}
0028     std::string AlgorithmProvider() const {return GetCipher().AlgorithmProvider();}
0029     unsigned int IVSize() const {return GetCipher().BlockSize();}
0030     unsigned int MinIVLength() const {return 1;}
0031     void Resynchronize(const byte *nonce, int length=-1);
0032     void GetNextIV(RandomNumberGenerator &rng, byte *IV);
0033     unsigned int DigestSize() const {return m_is128 ? 16 : 8;};
0034     void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
0035     void TruncatedFinal(byte *mac, size_t size);
0036     unsigned int BlockSize() const {return m_L1KeyLength;}
0037     ByteOrder GetByteOrder() const {return LITTLE_ENDIAN_ORDER;}
0038     unsigned int OptimalDataAlignment() const;
0039 
0040 protected:
0041     virtual BlockCipher & AccessCipher() =0;
0042     virtual int DefaultDigestSize() const =0;
0043     const BlockCipher & GetCipher() const {return const_cast<VMAC_Base *>(this)->AccessCipher();}
0044     void HashEndianCorrectedBlock(const word64 *data);
0045     size_t HashMultipleBlocks(const word64 *input, size_t length);
0046     void Init() {}
0047     word64* StateBuf() {return NULLPTR;}
0048     word64* DataBuf() {return (word64 *)(void*)m_data();}
0049 
0050     void VHASH_Update_SSE2(const word64 *data, size_t blocksRemainingInWord64, int tagPart);
0051     template <bool T_128BitTag>
0052         void VHASH_Update_Template(const word64 *data, size_t blockRemainingInWord128);
0053     void VHASH_Update(const word64 *data, size_t blocksRemainingInWord128);
0054 
0055     CRYPTOPP_BLOCK_1(polyState, word64, (m_is128 ? 8 : 4))
0056     CRYPTOPP_BLOCK_2(nhKey, word64, m_L1KeyLength/sizeof(word64) + 2*m_is128)
0057     CRYPTOPP_BLOCK_3(data, byte, m_L1KeyLength)
0058     CRYPTOPP_BLOCK_4(l3Key, word64, (m_is128 ? 4 : 2))
0059     CRYPTOPP_BLOCK_5(nonce, byte, IVSize())
0060     CRYPTOPP_BLOCK_6(pad, byte, IVSize())
0061     CRYPTOPP_BLOCKS_END(6)
0062 
0063     bool m_is128, m_padCached, m_isFirstBlock;
0064     unsigned int m_L1KeyLength;
0065 };
0066 
0067 /// \brief VMAC message authentication code

0068 /// \tparam T_BlockCipher block cipher

0069 /// \tparam T_DigestBitSize digest size, in bits

0070 /// \details VMAC is a block cipher-based message authentication code algorithm

0071 ///  using a universal hash proposed by Ted Krovetz and Wei Dai in April 2007. The

0072 ///  algorithm was designed for high performance backed by a formal analysis.

0073 /// \details The implementation is based on Ted Krovetz's public domain vmac.c

0074 ///  and <a href="http://tools.ietf.org/html/draft-krovetz-vmac-01">draft-krovetz-vmac-01.txt</a>.

0075 /// \sa <a href="http://www.cryptolounge.org/wiki/VMAC">VMAC</a>.

0076 /// \since Crypto++ 5.5

0077 template <class T_BlockCipher, int T_DigestBitSize = 128>
0078 class VMAC : public SimpleKeyingInterfaceImpl<VMAC_Base, SameKeyLengthAs<T_BlockCipher, SimpleKeyingInterface::UNIQUE_IV, T_BlockCipher::BLOCKSIZE> >
0079 {
0080 public:
0081     static std::string StaticAlgorithmName() {return std::string("VMAC(") + T_BlockCipher::StaticAlgorithmName() + ")-" + IntToString(T_DigestBitSize);}
0082 
0083 private:
0084     BlockCipher & AccessCipher() {return m_cipher;}
0085     int DefaultDigestSize() const {return T_DigestBitSize/8;}
0086     typename T_BlockCipher::Encryption m_cipher;
0087 };
0088 
0089 NAMESPACE_END
0090 
0091 #endif