~zooko/cryptopp/trunk

« back to all changes in this revision

Viewing changes to iterhash.h

  • Committer: weidai
  • Date: 2007-08-13 23:48:17 UTC
  • Revision ID: svn-v4:57ff6487-cd31-0410-9ec3-f628ee90f5f0:trunk/c5:383
fixed Salsa20 initialization crash on non-SSE2 machines

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
        typedef T HashWordType;
25
25
 
26
26
        IteratedHashBase() : m_countLo(0), m_countHi(0) {}
27
 
        unsigned int BlockSize() const {return (unsigned int)m_data.size() * sizeof(T);}
28
 
        unsigned int OptimalBlockSize() const {return BlockSize();}
29
 
        unsigned int OptimalDataAlignment() const {return sizeof(T);}
 
27
        unsigned int OptimalBlockSize() const {return this->BlockSize();}
 
28
        unsigned int OptimalDataAlignment() const {return GetAlignmentOf<T>();}
30
29
        void Update(const byte *input, size_t length);
31
30
        byte * CreateUpdateSpace(size_t &size);
32
31
        void Restart();
33
32
        void TruncatedFinal(byte *digest, size_t size);
34
33
 
35
34
protected:
36
 
        void SetBlockSize(unsigned int blockSize) {m_data.resize(blockSize / sizeof(HashWordType));}
37
 
        void SetStateSize(unsigned int stateSize) {m_digest.resize(stateSize / sizeof(HashWordType));}
38
 
 
39
 
        T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
40
 
        T GetBitCountLo() const {return m_countLo << 3;}
 
35
        inline T GetBitCountHi() const {return (m_countLo >> (8*sizeof(T)-3)) + (m_countHi << 3);}
 
36
        inline T GetBitCountLo() const {return m_countLo << 3;}
41
37
 
42
38
        void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80);
43
39
        virtual void Init() =0;
45
41
        virtual ByteOrder GetByteOrder() const =0;
46
42
        virtual void HashEndianCorrectedBlock(const HashWordType *data) =0;
47
43
        virtual size_t HashMultipleBlocks(const T *input, size_t length);
48
 
        void HashBlock(const HashWordType *input) {HashMultipleBlocks(input, BlockSize());}
 
44
        void HashBlock(const HashWordType *input) {HashMultipleBlocks(input, this->BlockSize());}
49
45
 
50
 
        SecBlock<T> m_data;                     // Data buffer
51
 
        SecBlock<T> m_digest;           // Message digest
 
46
        virtual T* DataBuf() =0;
 
47
        virtual T* StateBuf() =0;
52
48
 
53
49
private:
54
50
        T m_countLo, m_countHi;
62
58
        typedef T_Endianness ByteOrderClass;
63
59
        typedef T_HashWordType HashWordType;
64
60
 
65
 
        enum {BLOCKSIZE = T_BlockSize};
66
 
        CRYPTOPP_COMPILE_ASSERT((BLOCKSIZE & (BLOCKSIZE - 1)) == 0);            // blockSize is a power of 2
 
61
        CRYPTOPP_CONSTANT(BLOCKSIZE = T_BlockSize)
 
62
        // BCB2006 workaround: can't use BLOCKSIZE here
 
63
        CRYPTOPP_COMPILE_ASSERT((T_BlockSize & (T_BlockSize - 1)) == 0);        // blockSize is a power of 2
 
64
        unsigned int BlockSize() const {return T_BlockSize;}
67
65
 
68
66
        ByteOrder GetByteOrder() const {return T_Endianness::ToEnum();}
69
67
 
73
71
        }
74
72
 
75
73
protected:
76
 
        IteratedHash() {this->SetBlockSize(T_BlockSize);}
 
74
        T_HashWordType* DataBuf() {return this->m_data;}
 
75
        FixedSizeSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType)> m_data;
77
76
};
78
77
 
79
78
//! _
80
 
template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, unsigned int T_StateSize, class T_Transform, unsigned int T_DigestSize = T_StateSize>
 
79
template <class T_HashWordType, class T_Endianness, unsigned int T_BlockSize, unsigned int T_StateSize, class T_Transform, unsigned int T_DigestSize = 0>
81
80
class CRYPTOPP_NO_VTABLE IteratedHashWithStaticTransform
82
81
        : public ClonableImpl<T_Transform, AlgorithmImpl<IteratedHash<T_HashWordType, T_Endianness, T_BlockSize>, T_Transform> >
83
82
{
84
83
public:
85
 
        enum {DIGESTSIZE = T_DigestSize};
 
84
        CRYPTOPP_CONSTANT(DIGESTSIZE = T_DigestSize ? T_DigestSize : T_StateSize)
86
85
        unsigned int DigestSize() const {return DIGESTSIZE;};
87
86
 
88
87
protected:
89
 
        IteratedHashWithStaticTransform()
90
 
        {
91
 
                this->SetStateSize(T_StateSize);
92
 
                Init();
93
 
        }
94
 
        void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_digest, data);}
95
 
        void Init() {T_Transform::InitState(this->m_digest);}
 
88
        IteratedHashWithStaticTransform() {this->Init();}
 
89
        void HashEndianCorrectedBlock(const T_HashWordType *data) {T_Transform::Transform(this->m_state, data);}
 
90
        void Init() {T_Transform::InitState(this->m_state);}
 
91
 
 
92
        T_HashWordType* StateBuf() {return this->m_state;}
 
93
        FixedSizeSecBlock<T_HashWordType, T_BlockSize/sizeof(T_HashWordType)> m_state;
96
94
};
97
95
 
98
96
NAMESPACE_END