monotone

monotone Mtn Source Tree

Root/cryptopp/iterhash.h

1#ifndef CRYPTOPP_ITERHASH_H
2#define CRYPTOPP_ITERHASH_H
3
4#include "cryptlib.h"
5#include "secblock.h"
6#include "misc.h"
7
8NAMESPACE_BEGIN(CryptoPP)
9
10template <class T, class BASE>
11class IteratedHashBase : public BASE
12{
13public:
14typedef T HashWordType;
15
16IteratedHashBase(unsigned int blockSize, unsigned int digestSize);
17unsigned int DigestSize() const {return m_digest.size() * sizeof(T);};
18unsigned int OptimalBlockSize() const {return BlockSize();}
19unsigned int OptimalDataAlignment() const {return sizeof(T);}
20void Update(const byte *input, unsigned int length);
21byte * CreateUpdateSpace(unsigned int &size);
22void Restart();
23
24protected:
25T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
26T GetBitCountLo() const {return m_countLo << 3;}
27
28virtual unsigned int HashMultipleBlocks(const T *input, unsigned int length);
29void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80);
30virtual void Init() =0;
31virtual void HashBlock(const T *input) =0;
32virtual unsigned int BlockSize() const =0;
33
34SecBlock<T> m_data;// Data buffer
35SecBlock<T> m_digest;// Message digest
36
37private:
38T m_countLo, m_countHi;
39};
40
41//! .
42template <class T, class B, class BASE>
43class IteratedHashBase2 : public IteratedHashBase<T, BASE>
44{
45public:
46IteratedHashBase2(unsigned int blockSize, unsigned int digestSize)
47: IteratedHashBase<T, BASE>(blockSize, digestSize) {}
48
49typedef B ByteOrderClass;
50typedef typename IteratedHashBase<T, BASE>::HashWordType HashWordType;
51
52inline static void CorrectEndianess(HashWordType *out, const HashWordType *in, unsigned int byteCount)
53{
54ConditionalByteReverse(B::ToEnum(), out, in, byteCount);
55}
56
57void TruncatedFinal(byte *hash, unsigned int size);
58
59protected:
60void HashBlock(const HashWordType *input);
61
62virtual void vTransform(const HashWordType *data) =0;
63};
64
65//! .
66template <class T, class B, unsigned int S, class BASE = HashTransformation>
67class IteratedHash : public IteratedHashBase2<T, B, BASE>
68{
69public:
70enum {BLOCKSIZE = S};
71
72private:
73CRYPTOPP_COMPILE_ASSERT((BLOCKSIZE & (BLOCKSIZE - 1)) == 0);// blockSize is a power of 2
74
75protected:
76IteratedHash(unsigned int digestSize) : IteratedHashBase2<T, B, BASE>(BLOCKSIZE, digestSize) {}
77unsigned int BlockSize() const {return BLOCKSIZE;}
78};
79
80template <class T, class B, unsigned int S, class M>
81class IteratedHashWithStaticTransform : public IteratedHash<T, B, S>
82{
83protected:
84IteratedHashWithStaticTransform(unsigned int digestSize) : IteratedHash<T, B, S>(digestSize) {}
85void vTransform(const T *data) {M::Transform(m_digest, data);}
86std::string AlgorithmName() const {return M::StaticAlgorithmName();}
87};
88
89// *************************************************************
90
91template <class T, class B, class BASE> void IteratedHashBase2<T, B, BASE>::TruncatedFinal(byte *hash, unsigned int size)
92{
93ThrowIfInvalidTruncatedSize(size);
94
95PadLastBlock(BlockSize() - 2*sizeof(HashWordType));
96CorrectEndianess(m_data, m_data, BlockSize() - 2*sizeof(HashWordType));
97
98m_data[m_data.size()-2] = B::ToEnum() ? GetBitCountHi() : GetBitCountLo();
99m_data[m_data.size()-1] = B::ToEnum() ? GetBitCountLo() : GetBitCountHi();
100
101vTransform(m_data);
102CorrectEndianess(m_digest, m_digest, DigestSize());
103memcpy(hash, m_digest, size);
104
105Restart();// reinit for next use
106}
107
108template <class T, class B, class BASE> void IteratedHashBase2<T, B, BASE>::HashBlock(const HashWordType *input)
109{
110if (NativeByteOrderIs(B::ToEnum()))
111vTransform(input);
112else
113{
114ByteReverse(m_data.begin(), input, BlockSize());
115vTransform(m_data);
116}
117}
118
119NAMESPACE_END
120
121#endif

Archive Download this file

Branches

Tags

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