Crypto++  7.0
Free C++ class library of cryptographic schemes
mdc.h
Go to the documentation of this file.
1 // mdc.h - originally written and placed in the public domain by Wei Dai
2 
3 /// \file mdc.h
4 /// \brief Classes for the MDC message digest
5 
6 #ifndef CRYPTOPP_MDC_H
7 #define CRYPTOPP_MDC_H
8 
9 #include "seckey.h"
10 #include "secblock.h"
11 #include "misc.h"
12 
13 // GCC cast warning
14 #define HashWordPtr(x) ((HashWordType*)(void*)(x))
15 #define ConstHashWordPtr(x) ((const HashWordType*)(const void*)(x))
16 
17 NAMESPACE_BEGIN(CryptoPP)
18 
19 /// \tparam B BlockCipher derived class
20 /// \brief MDC_Info cipher information
21 template <class B>
22 struct MDC_Info : public FixedBlockSize<B::DIGESTSIZE>, public FixedKeyLength<B::BLOCKSIZE>
23 {
24  static std::string StaticAlgorithmName() {return std::string("MDC/")+B::StaticAlgorithmName();}
25 };
26 
27 /// \brief MDC cipher
28 /// \tparam H HashTransformation derived class
29 /// \details MDC() is a construction by Peter Gutmann to turn an iterated hash function into a PRF
30 /// \sa <a href="http://www.cryptopp.com/wiki/MDC">MDC</a>
31 template <class H>
32 class MDC : public MDC_Info<H>
33 {
34  /// \brief MDC cipher encryption operation
35  class CRYPTOPP_NO_VTABLE Enc : public BlockCipherImpl<MDC_Info<H> >
36  {
37  typedef typename H::HashWordType HashWordType;
38 
39  public:
40  void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
41  {
42  CRYPTOPP_UNUSED(params);
43  this->AssertValidKeyLength(length);
44  ConditionalByteReverse(BIG_ENDIAN_ORDER, Key(), ConstHashWordPtr(userKey), this->KEYLENGTH);
45  }
46 
47  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
48  {
49  ConditionalByteReverse(BIG_ENDIAN_ORDER, Buffer(), ConstHashWordPtr(inBlock), this->BLOCKSIZE);
50  H::Transform(Buffer(), Key());
51 
52  if (xorBlock)
53  {
54  ConditionalByteReverse(BIG_ENDIAN_ORDER, Buffer(), Buffer(), this->BLOCKSIZE);
55  xorbuf(outBlock, xorBlock, m_buffer, this->BLOCKSIZE);
56  }
57  else
58  {
59  ConditionalByteReverse(BIG_ENDIAN_ORDER, HashWordPtr(outBlock), Buffer(), this->BLOCKSIZE);
60  }
61  }
62 
63  bool IsPermutation() const {return false;}
64 
65  unsigned int OptimalDataAlignment() const {return sizeof(HashWordType);}
66 
67  private:
68  HashWordType *Key() {return HashWordPtr(m_key.data());}
69  const HashWordType *Key() const {return ConstHashWordPtr(m_key.data());}
70  HashWordType *Buffer() const {return HashWordPtr(m_buffer.data());}
71 
72  // VC60 workaround: bug triggered if using FixedSizeAllocatorWithCleanup
75  };
76 
77 public:
78  // use BlockCipher interface
80 };
81 
82 NAMESPACE_END
83 
84 #endif
MDC_Info cipher information.
Definition: mdc.h:22
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:147
Utility functions for the Crypto++ library.
static const int KEYLENGTH
The default key length used by the algorithm provided as a constant.
Definition: seckey.h:152
MDC cipher.
Definition: mdc.h:32
Classes and functions for secure memory allocations.
Inherited by algorithms with fixed block size.
Definition: seckey.h:40
Classes and functions for implementing secret key algorithms.
T ConditionalByteReverse(ByteOrder order, T value)
Reverses bytes in a value depending upon endianness.
Definition: misc.h:2028
Fixed size stack-based SecBlock.
Definition: secblock.h:841
byte order is big-endian
Definition: cryptlib.h:145
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Definition: misc.cpp:32
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:328
Crypto++ library namespace.
static const int BLOCKSIZE
The block size of the algorithm provided as a constant.
Definition: seckey.h:44
const char * Key()
ConstByteArrayParameter.
Definition: argnames.h:20
Interface for retrieving values given their names.
Definition: cryptlib.h:291