monotone

monotone Mtn Source Tree

Root/cryptopp/mdc.h

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
7*/
8
9#include "seckey.h"
10#include "misc.h"
11
12NAMESPACE_BEGIN(CryptoPP)
13
14template <class T>
15struct MDC_Info : public FixedBlockSize<T::DIGESTSIZE>, public FixedKeyLength<T::BLOCKSIZE>
16{
17static std::string StaticAlgorithmName() {return std::string("MDC/")+T::StaticAlgorithmName();}
18};
19
20//! <a href="http://www.weidai.com/scan-mirror/cs.html#MDC">MDC</a>
21/*! a construction by Peter Gutmann to turn an iterated hash function into a PRF */
22template <class T>
23class MDC : public MDC_Info<T>
24{
25class Enc : public BlockCipherBaseTemplate<MDC_Info<T> >
26{
27typedef typename T::HashWordType HashWordType;
28
29public:
30void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length)
31{
32assert(direction == ENCRYPTION);
33AssertValidKeyLength(length);
34memcpy(Key(), userKey, KEYLENGTH);
35T::CorrectEndianess(Key(), Key(), KEYLENGTH);
36}
37
38void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
39{
40T::CorrectEndianess(Buffer(), (HashWordType *)inBlock, BLOCKSIZE);
41T::Transform(Buffer(), Key());
42if (xorBlock)
43{
44T::CorrectEndianess(Buffer(), Buffer(), BLOCKSIZE);
45xorbuf(outBlock, xorBlock, m_buffer, BLOCKSIZE);
46}
47else
48T::CorrectEndianess((HashWordType *)outBlock, Buffer(), BLOCKSIZE);
49}
50
51bool IsPermutation() const {return false;}
52
53unsigned int GetAlignment() const {return sizeof(HashWordType);}
54
55private:
56HashWordType *Key() {return (HashWordType *)m_key.data();}
57const HashWordType *Key() const {return (const HashWordType *)m_key.data();}
58HashWordType *Buffer() const {return (HashWordType *)m_buffer.data();}
59
60// VC60 workaround: bug triggered if using FixedSizeAllocatorWithCleanup
61FixedSizeSecBlock<byte, MDC_Info<T>::KEYLENGTH, AllocatorWithCleanup<byte> > m_key;
62mutable FixedSizeSecBlock<byte, MDC_Info<T>::BLOCKSIZE, AllocatorWithCleanup<byte> > m_buffer;
63};
64
65public:
66//! use BlockCipher interface
67typedef BlockCipherTemplate<ENCRYPTION, Enc> Encryption;
68};
69
70NAMESPACE_END
71
72#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status