1#ifndef CRYPTOPP_VMAC_H
2#define CRYPTOPP_VMAC_H
3
4#include "iterhash.h"
5#include "seckey.h"
6
7NAMESPACE_BEGIN(CryptoPP)
8
9/// .
10class VMAC_Base : public IteratedHashBase<word64, MessageAuthenticationCode>
11{
12public:
13	std::string AlgorithmName() const {return std::string("VMAC(") + GetCipher().AlgorithmName() + ")-" + IntToString(DigestSize()*8);}
14	unsigned int IVSize() const {return GetCipher().BlockSize();}
15	unsigned int MinIVLength() const {return 1;}
16	void Resynchronize(const byte *nonce, int length=-1);
17	void GetNextIV(RandomNumberGenerator &rng, byte *IV);
18	unsigned int DigestSize() const {return m_is128 ? 16 : 8;};
19	void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
20	void TruncatedFinal(byte *mac, size_t size);
21	unsigned int BlockSize() const {return m_L1KeyLength;}
22	ByteOrder GetByteOrder() const {return LITTLE_ENDIAN_ORDER;}
23
24protected:
25	virtual BlockCipher & AccessCipher() =0;
26	virtual int DefaultDigestSize() const =0;
27	const BlockCipher & GetCipher() const {return const_cast<VMAC_Base *>(this)->AccessCipher();}
28	void HashEndianCorrectedBlock(const word64 *data);
29	size_t HashMultipleBlocks(const word64 *input, size_t length);
30	void Init() {}
31	word64* StateBuf() {return NULL;}
32	word64* DataBuf() {return (word64 *)m_data();}
33
34	void VHASH_Update_SSE2(const word64 *data, size_t blocksRemainingInWord64, int tagPart);
35#if !(defined(_MSC_VER) && _MSC_VER < 1300)		// can't use function template here with VC6
36	template <bool T_128BitTag>
37#endif
38	void VHASH_Update_Template(const word64 *data, size_t blockRemainingInWord128);
39	void VHASH_Update(const word64 *data, size_t blocksRemainingInWord128);
40
41	CRYPTOPP_BLOCK_1(polyState, word64, 4*(m_is128+1))
42	CRYPTOPP_BLOCK_2(nhKey, word64, m_L1KeyLength/sizeof(word64) + 2*m_is128)
43	CRYPTOPP_BLOCK_3(data, byte, m_L1KeyLength)
44	CRYPTOPP_BLOCK_4(l3Key, word64, 2*(m_is128+1))
45	CRYPTOPP_BLOCK_5(nonce, byte, IVSize())
46	CRYPTOPP_BLOCK_6(pad, byte, IVSize())
47	CRYPTOPP_BLOCKS_END(6)
48
49	bool m_is128, m_padCached, m_isFirstBlock;
50	int m_L1KeyLength;
51};
52
53/// <a href="http://www.cryptolounge.org/wiki/VMAC">VMAC</a>
54template <class T_BlockCipher, int T_DigestBitSize = 128>
55class VMAC : public SimpleKeyingInterfaceImpl<VMAC_Base, SameKeyLengthAs<T_BlockCipher, SimpleKeyingInterface::UNIQUE_IV, T_BlockCipher::BLOCKSIZE> >
56{
57public:
58	static std::string StaticAlgorithmName() {return std::string("VMAC(") + T_BlockCipher::StaticAlgorithmName() + ")-" + IntToString(T_DigestBitSize);}
59
60private:
61	BlockCipher & AccessCipher() {return m_cipher;}
62	int DefaultDigestSize() const {return T_DigestBitSize/8;}
63	typename T_BlockCipher::Encryption m_cipher;
64};
65
66NAMESPACE_END
67
68#endif
69