Crypto++  5.6.4
Free C++ class library of cryptographic schemes
mdc.h
Go to the documentation of this file.
1 // mdc.h - written and placed in the public domain by Wei Dai
2 
3 #ifndef CRYPTOPP_MDC_H
4 #define CRYPTOPP_MDC_H
5 
6 //! \file mdc.h
7 //! \brief Classes for the MDC message digest
8 
9 #include "seckey.h"
10 #include "secblock.h"
11 #include "misc.h"
12 
13 NAMESPACE_BEGIN(CryptoPP)
14 
15 //! \class MDC_Info
16 //! \brief MDC_Info cipher information
17 template <class T>
18 struct MDC_Info : public FixedBlockSize<T::DIGESTSIZE>, public FixedKeyLength<T::BLOCKSIZE>
19 {
20  static std::string StaticAlgorithmName() {return std::string("MDC/")+T::StaticAlgorithmName();}
21 };
22 
23 
24 //! \class MDC
25 //! \brief MDC cipher
26 //! \details MDC() is a construction by Peter Gutmann to turn an iterated hash function into a PRF
27 //! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#MDC">MDC</a>
28 template <class T>
29 class MDC : public MDC_Info<T>
30 {
31  //! \class Enc
32  //! \brief MDC cipher encryption operation
33  class CRYPTOPP_NO_VTABLE Enc : public BlockCipherImpl<MDC_Info<T> >
34  {
35  typedef typename T::HashWordType HashWordType;
36 
37  public:
38  void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
39  {
40  this->AssertValidKeyLength(length);
41  memcpy_s(m_key, m_key.size(), userKey, this->KEYLENGTH);
42  T::CorrectEndianess(Key(), Key(), this->KEYLENGTH);
43  }
44 
45  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
46  {
47  T::CorrectEndianess(Buffer(), (HashWordType *)inBlock, this->BLOCKSIZE);
48  T::Transform(Buffer(), Key());
49  if (xorBlock)
50  {
51  T::CorrectEndianess(Buffer(), Buffer(), this->BLOCKSIZE);
52  xorbuf(outBlock, xorBlock, m_buffer, this->BLOCKSIZE);
53  }
54  else
55  T::CorrectEndianess((HashWordType *)outBlock, Buffer(), this->BLOCKSIZE);
56  }
57 
58  bool IsPermutation() const {return false;}
59 
60  unsigned int OptimalDataAlignment() const {return sizeof(HashWordType);}
61 
62  private:
63  HashWordType *Key() {return (HashWordType *)m_key.data();}
64  const HashWordType *Key() const {return (const HashWordType *)m_key.data();}
65  HashWordType *Buffer() const {return (HashWordType *)m_buffer.data();}
66 
67  // VC60 workaround: bug triggered if using FixedSizeAllocatorWithCleanup
70  };
71 
72 public:
73  //! use BlockCipher interface
75 };
76 
77 NAMESPACE_END
78 
79 #endif
MDC_Info cipher information.
Definition: mdc.h:18
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:128
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:133
MDC cipher.
Definition: mdc.h:29
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
Definition: misc.h:356
Classes and functions for secure memory allocations.
BlockCipherFinal< ENCRYPTION, Enc > Encryption
use BlockCipher interface
Definition: mdc.h:74
Inherited by algorithms with fixed block size.
Definition: seckey.h:34
Classes and functions for implementing secret key algorithms.
Fixed size stack-based SecBlock.
Definition: secblock.h:753
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Definition: misc.cpp:28
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:326
Crypto++ library namespace.
static const int BLOCKSIZE
The block size of the algorithm provided as a constant.
Definition: seckey.h:38
const char * Key()
ConstByteArrayParameter.
Definition: argnames.h:20
Interface for retrieving values given their names.
Definition: cryptlib.h:282