- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# vmac.h

    00001 #ifndef CRYPTOPP_VMAC_H
    00002 #define CRYPTOPP_VMAC_H
    00003
    00004 #include "iterhash.h"
    00005 #include "seckey.h"
    00006
    00007 NAMESPACE_BEGIN(CryptoPP)
    00008
    00009 #define CRYPTOPP_BLOCK_1(n, t, s) t* m_##n() {return (t *)(m_aggregate+0);}     size_t SS1() {return       sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
    00010 #define CRYPTOPP_BLOCK_2(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS1());} size_t SS2() {return SS1()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
    00011 #define CRYPTOPP_BLOCK_3(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS2());} size_t SS3() {return SS2()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
    00012 #define CRYPTOPP_BLOCK_4(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS3());} size_t SS4() {return SS3()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
    00013 #define CRYPTOPP_BLOCK_5(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS4());} size_t SS5() {return SS4()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
    00014 #define CRYPTOPP_BLOCK_6(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS5());} size_t SS6() {return SS5()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
    00015 #define CRYPTOPP_BLOCK_7(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS6());} size_t SS7() {return SS6()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
    00016 #define CRYPTOPP_BLOCK_8(n, t, s) t* m_##n() {return (t *)(m_aggregate+SS7());} size_t SS8() {return SS7()+sizeof(t)*(s);} size_t m_##n##Size() {return (s);}
    00017 #define CRYPTOPP_BLOCKS_END(i) size_t SST() {return SS##i();} void AllocateBlocks() {m_aggregate.New(SST());} AlignedSecByteBlock m_aggregate;
    00018
    00019 /// .
    00020 class VMAC_Base : public IteratedHashBase<word64, MessageAuthenticationCode>
    00021 {
    00022 public:
    00023         std::string AlgorithmName() const {return std::string("VMAC(") + GetCipher().AlgorithmName() + ")-" + IntToString(DigestSize()*8);}
    00024         unsigned int IVSize() const {return GetCipher().BlockSize();}
    00025         void Resynchronize(const byte *IV);
    00026         void GetNextIV(RandomNumberGenerator &rng, byte *IV);
    00027         unsigned int DigestSize() const {return m_is128 ? 16 : 8;};
    00028         void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
    00029         void TruncatedFinal(byte *mac, size_t size);
    00030         unsigned int BlockSize() const {return m_L1KeyLength;}
    00031         ByteOrder GetByteOrder() const {return LITTLE_ENDIAN_ORDER;}
    00032
    00033 protected:
    00034         virtual BlockCipher & AccessCipher() =0;
    00035         virtual int DefaultDigestSize() const =0;
    00036         const BlockCipher & GetCipher() const {return const_cast<VMAC_Base *>(this)->AccessCipher();}
    00037         void HashEndianCorrectedBlock(const word64 *data);
    00038         size_t HashMultipleBlocks(const word64 *input, size_t length);
    00039         void Init() {}
    00040         word64* StateBuf() {return NULL;}
    00041         word64* DataBuf() {return (word64 *)m_data();}
    00042
    00043         void VHASH_Update_SSE2(const word64 *data, size_t blocksRemainingInWord64, int tagPart);
    00044 #if !(defined(_MSC_VER) && _MSC_VER < 1300)             // can't use function template here with VC6
    00045         template <bool T_128BitTag>
    00046 #endif
    00047         void VHASH_Update_Template(const word64 *data, size_t blockRemainingInWord128);
    00048         void VHASH_Update(const word64 *data, size_t blocksRemainingInWord128);
    00049
    00050         CRYPTOPP_BLOCK_1(polyState, word64, 4*(m_is128+1))
    00051         CRYPTOPP_BLOCK_2(nhKey, word64, m_L1KeyLength/sizeof(word64) + 2*m_is128)
    00052         CRYPTOPP_BLOCK_3(data, byte, m_L1KeyLength)
    00053         CRYPTOPP_BLOCK_4(l3Key, word64, 2*(m_is128+1))
    00054         CRYPTOPP_BLOCK_5(nonce, byte, IVSize())
    00055         CRYPTOPP_BLOCK_6(pad, byte, IVSize())
    00056         CRYPTOPP_BLOCKS_END(6)
    00057
    00058         bool m_is128, m_padCached, m_isFirstBlock;
    00059         int m_L1KeyLength;
    00060 };
    00061
    00062 /// <a href="http://www.cryptolounge.org/wiki/VMAC">VMAC</a>
    00063 template <class T_BlockCipher, int T_DigestBitSize = 128>
    00064 class VMAC : public SimpleKeyingInterfaceImpl<VMAC_Base, SameKeyLengthAs<T_BlockCipher, SimpleKeyingInterface::UNIQUE_IV, T_BlockCipher::BLOCKSIZE> >
    00065 {
    00066 public:
    00067         static std::string StaticAlgorithmName() {return std::string("VMAC(") + T_BlockCipher::StaticAlgorithmName() + ")-" + IntToString(T_DigestBitSize);}
    00068
    00069 private:
    00070         BlockCipher & AccessCipher() {return m_cipher;}
    00071         int DefaultDigestSize() const {return T_DigestBitSize/8;}
    00072         typename T_BlockCipher::Encryption m_cipher;
    00073 };
    00074
    00075 NAMESPACE_END
    00076
    00077 #endif