Fabcoin Core  0.16.2
P2P Digital Currency
vmac.h
Go to the documentation of this file.
1 // vmac.h - written and placed in the public domain by Wei Dai
2 
6 
7 #ifndef CRYPTOPP_VMAC_H
8 #define CRYPTOPP_VMAC_H
9 
10 #include "cryptlib.h"
11 #include "iterhash.h"
12 #include "seckey.h"
13 
14 // Clang 3.3 integrated assembler crash on Linux
15 // http://github.com/weidai11/cryptopp/issues/264
16 #if (defined(CRYPTOPP_LLVM_CLANG_VERSION) && (CRYPTOPP_LLVM_CLANG_VERSION < 30400)) || CRYPTOPP_BOOL_X32
17 # define CRYPTOPP_DISABLE_VMAC_ASM
18 #endif
19 
21 
26 {
27 public:
28  std::string AlgorithmName() const {return std::string("VMAC(") + GetCipher().AlgorithmName() + ")-" + IntToString(DigestSize()*8);}
29  unsigned int IVSize() const {return GetCipher().BlockSize();}
30  unsigned int MinIVLength() const {return 1;}
31  void Resynchronize(const byte *nonce, int length=-1);
32  void GetNextIV(RandomNumberGenerator &rng, byte *IV);
33  unsigned int DigestSize() const {return m_is128 ? 16 : 8;};
34  void UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &params);
35  void TruncatedFinal(byte *mac, size_t size);
36  unsigned int BlockSize() const {return m_L1KeyLength;}
38  unsigned int OptimalDataAlignment() const;
39 
40 protected:
41  virtual BlockCipher & AccessCipher() =0;
42  virtual int DefaultDigestSize() const =0;
43  const BlockCipher & GetCipher() const {return const_cast<VMAC_Base *>(this)->AccessCipher();}
44  void HashEndianCorrectedBlock(const word64 *data);
45  size_t HashMultipleBlocks(const word64 *input, size_t length);
46  void Init() {}
47  word64* StateBuf() {return NULL;}
48  word64* DataBuf() {return (word64 *)(void*)m_data();}
49 
50  void VHASH_Update_SSE2(const word64 *data, size_t blocksRemainingInWord64, int tagPart);
51  template <bool T_128BitTag>
52  void VHASH_Update_Template(const word64 *data, size_t blockRemainingInWord128);
53  void VHASH_Update(const word64 *data, size_t blocksRemainingInWord128);
54 
55  CRYPTOPP_BLOCK_1(polyState, word64, 4*(m_is128+1))
56  CRYPTOPP_BLOCK_2(nhKey, word64, m_L1KeyLength/sizeof(word64) + 2*m_is128)
57  CRYPTOPP_BLOCK_3(data, byte, m_L1KeyLength)
58  CRYPTOPP_BLOCK_4(l3Key, word64, 2*(m_is128+1))
59  CRYPTOPP_BLOCK_5(nonce, byte, IVSize())
60  CRYPTOPP_BLOCK_6(pad, byte, IVSize())
62 
63  bool m_is128, m_padCached, m_isFirstBlock;
64  unsigned int m_L1KeyLength;
65 };
66 
78 template <class T_BlockCipher, int T_DigestBitSize = 128>
79 class VMAC : public SimpleKeyingInterfaceImpl<VMAC_Base, SameKeyLengthAs<T_BlockCipher, SimpleKeyingInterface::UNIQUE_IV, T_BlockCipher::BLOCKSIZE> >
80 {
81 public:
82  static std::string StaticAlgorithmName() {return std::string("VMAC(") + T_BlockCipher::StaticAlgorithmName() + ")-" + IntToString(T_DigestBitSize);}
83 
84 private:
85  BlockCipher & AccessCipher() {return m_cipher;}
86  int DefaultDigestSize() const {return T_DigestBitSize/8;}
87  typename T_BlockCipher::Encryption m_cipher;
88 };
89 
91 
92 #endif
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: vmac.h:28
VMAC message authentication code.
Definition: vmac.h:79
Interface for message authentication codes.
Definition: cryptlib.h:1111
uint8_t byte
Definition: Common.h:57
#define CRYPTOPP_BLOCKS_END(i)
Definition: misc.h:2368
ByteOrder
Provides the byte ordering.
Definition: cryptlib.h:124
unsigned int DigestSize() const
Provides the digest size of the hash.
Definition: vmac.h:33
VMAC message authentication code base class.
Definition: vmac.h:25
Provides a base implementation of SimpleKeyingInterface.
Definition: seckey.h:263
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
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:36
BlockCipher & AccessCipher()
Definition: vmac.h:85
ByteOrder GetByteOrder() const
Definition: vmac.h:37
Interface for random number generators.
Definition: cryptlib.h:1188
const BlockCipher & GetCipher() const
Definition: vmac.h:43
#define CRYPTOPP_BLOCK_2(n, t, s)
Definition: misc.h:2361
byte order is little-endian
Definition: cryptlib.h:126
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1095
#define CRYPTOPP_BLOCK_1(n, t, s)
Definition: misc.h:2360
T_BlockCipher::Encryption m_cipher
Definition: vmac.h:87
#define CRYPTOPP_BLOCK_4(n, t, s)
Definition: misc.h:2363
Classes and functions for implementing secret key algorithms.
Interface for algorithms that take byte strings as keys.
Definition: cryptlib.h:524
unsigned long long word64
Definition: config.h:240
Provides key lengths based on another class&#39;s key length.
Definition: seckey.h:222
#define CRYPTOPP_BLOCK_6(n, t, s)
Definition: misc.h:2365
unsigned int MinIVLength() const
Provides the minimum size of an IV.
Definition: vmac.h:30
unsigned int IVSize() const
Returns length of the IV accepted by this object.
Definition: vmac.h:29
void Init()
Definition: vmac.h:46
#define CRYPTOPP_BLOCK_3(n, t, s)
Definition: misc.h:2362
word64 * DataBuf()
Definition: vmac.h:48
uint8_t const size_t const size
Definition: sha3.h:20
Iterated hash base class.
Definition: iterhash.h:27
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:539
#define NAMESPACE_END
Definition: config.h:201
word64 * StateBuf()
Definition: vmac.h:47
int DefaultDigestSize() const
Definition: vmac.h:86
uint8_t const * data
Definition: sha3.h:19
static std::string StaticAlgorithmName()
Definition: vmac.h:82
#define CRYPTOPP_BLOCK_5(n, t, s)
Definition: misc.h:2364
Interface for retrieving values given their names.
Definition: cryptlib.h:279