~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to extra/yassl/taocrypt/src/hash.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2000-2007 MySQL AB
 
3
 
 
4
   This program is free software; you can redistribute it and/or modify
 
5
   it under the terms of the GNU General Public License as published by
 
6
   the Free Software Foundation; version 2 of the License.
 
7
 
 
8
   This program is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
   GNU General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU General Public License
 
14
   along with this program; see the file COPYING. If not, write to the
 
15
   Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 
16
   MA  02110-1301  USA.
 
17
*/
 
18
 
 
19
/* hash.cpp implements a base for digest types
 
20
*/
 
21
 
 
22
#include "runtime.hpp"
 
23
#include <string.h>
 
24
#include <assert.h>
 
25
 
 
26
#include "hash.hpp"
 
27
 
 
28
 
 
29
namespace TaoCrypt {
 
30
 
 
31
 
 
32
HASHwithTransform::HASHwithTransform(word32 digSz, word32 buffSz)
 
33
{
 
34
    assert(digSz  <= MaxDigestSz);
 
35
    assert(buffSz <= MaxBufferSz);
 
36
}
 
37
 
 
38
 
 
39
void HASHwithTransform::AddLength(word32 len)
 
40
{
 
41
    HashLengthType tmp = loLen_;
 
42
    if ( (loLen_ += len) < tmp)
 
43
        hiLen_++;                       // carry low to high
 
44
    hiLen_ += SafeRightShift<8*sizeof(HashLengthType)>(len);
 
45
}
 
46
 
 
47
 
 
48
// Update digest with data of size len, do in blocks
 
49
void HASHwithTransform::Update(const byte* data, word32 len)
 
50
{
 
51
    // do block size increments
 
52
    word32 blockSz = getBlockSize();
 
53
    byte*  local   = reinterpret_cast<byte*>(buffer_);
 
54
 
 
55
    while (len) {
 
56
        word32 add = min(len, blockSz - buffLen_);
 
57
        memcpy(&local[buffLen_], data, add);
 
58
 
 
59
        buffLen_ += add;
 
60
        data     += add;
 
61
        len      -= add;
 
62
 
 
63
        if (buffLen_ == blockSz) {
 
64
            ByteReverseIf(local, local, blockSz, getByteOrder());
 
65
            Transform();
 
66
            AddLength(blockSz);
 
67
            buffLen_ = 0;
 
68
        }
 
69
    }
 
70
}
 
71
 
 
72
 
 
73
// Final process, place digest in hash
 
74
void HASHwithTransform::Final(byte* hash)
 
75
{
 
76
    word32    blockSz   = getBlockSize();
 
77
    word32    digestSz  = getDigestSize();
 
78
    word32    padSz     = getPadSize();
 
79
    ByteOrder order     = getByteOrder();
 
80
 
 
81
    AddLength(buffLen_);                        // before adding pads
 
82
    HashLengthType preLoLen = GetBitCountLo();
 
83
    HashLengthType preHiLen = GetBitCountHi();
 
84
    byte*     local     = reinterpret_cast<byte*>(buffer_);
 
85
 
 
86
    local[buffLen_++] = 0x80;  // add 1
 
87
 
 
88
    // pad with zeros
 
89
    if (buffLen_ > padSz) {
 
90
        memset(&local[buffLen_], 0, blockSz - buffLen_);
 
91
        buffLen_ += blockSz - buffLen_;
 
92
 
 
93
        ByteReverseIf(local, local, blockSz, order);
 
94
        Transform();
 
95
        buffLen_ = 0;
 
96
    }
 
97
    memset(&local[buffLen_], 0, padSz - buffLen_);
 
98
 
 
99
    ByteReverseIf(local, local, blockSz, order);
 
100
    
 
101
    memcpy(&local[padSz],   order ? &preHiLen : &preLoLen, sizeof(preLoLen));
 
102
    memcpy(&local[padSz+4], order ? &preLoLen : &preHiLen, sizeof(preLoLen));
 
103
 
 
104
    Transform();
 
105
    ByteReverseIf(digest_, digest_, digestSz, order);
 
106
    memcpy(hash, digest_, digestSz);
 
107
 
 
108
    Init();  // reset state
 
109
}
 
110
 
 
111
 
 
112
#ifdef WORD64_AVAILABLE
 
113
 
 
114
HASH64withTransform::HASH64withTransform(word32 digSz, word32 buffSz)
 
115
{
 
116
    assert(digSz  <= MaxDigestSz);
 
117
    assert(buffSz <= MaxBufferSz);
 
118
}
 
119
 
 
120
 
 
121
void HASH64withTransform::AddLength(word32 len)
 
122
{
 
123
    HashLengthType tmp = loLen_;
 
124
    if ( (loLen_ += len) < tmp)
 
125
        hiLen_++;                       // carry low to high
 
126
    hiLen_ += SafeRightShift<8*sizeof(HashLengthType)>(len);
 
127
}
 
128
 
 
129
 
 
130
// Update digest with data of size len, do in blocks
 
131
void HASH64withTransform::Update(const byte* data, word32 len)
 
132
{
 
133
    // do block size increments
 
134
    word32 blockSz = getBlockSize();
 
135
    byte*  local   = reinterpret_cast<byte*>(buffer_);
 
136
 
 
137
    while (len) {
 
138
        word32 add = min(len, blockSz - buffLen_);
 
139
        memcpy(&local[buffLen_], data, add);
 
140
 
 
141
        buffLen_ += add;
 
142
        data     += add;
 
143
        len      -= add;
 
144
 
 
145
        if (buffLen_ == blockSz) {
 
146
            ByteReverseIf(buffer_, buffer_, blockSz, getByteOrder());
 
147
            Transform();
 
148
            AddLength(blockSz);
 
149
            buffLen_ = 0;
 
150
        }
 
151
    }
 
152
}
 
153
 
 
154
 
 
155
// Final process, place digest in hash
 
156
void HASH64withTransform::Final(byte* hash)
 
157
{
 
158
    word32    blockSz  = getBlockSize();
 
159
    word32    digestSz = getDigestSize();
 
160
    word32    padSz    = getPadSize();
 
161
    ByteOrder order    = getByteOrder();
 
162
 
 
163
    AddLength(buffLen_);                        // before adding pads
 
164
    HashLengthType preLoLen = GetBitCountLo();
 
165
    HashLengthType preHiLen = GetBitCountHi();
 
166
    byte*     local         = reinterpret_cast<byte*>(buffer_);
 
167
 
 
168
    local[buffLen_++] = 0x80;  // add 1
 
169
 
 
170
    // pad with zeros
 
171
    if (buffLen_ > padSz) {
 
172
        memset(&local[buffLen_], 0, blockSz - buffLen_);
 
173
        buffLen_ += blockSz - buffLen_;
 
174
 
 
175
        ByteReverseIf(buffer_, buffer_, blockSz, order);
 
176
        Transform();
 
177
        buffLen_ = 0;
 
178
    }
 
179
    memset(&local[buffLen_], 0, padSz - buffLen_);
 
180
   
 
181
    ByteReverseIf(buffer_, buffer_, padSz, order);
 
182
    
 
183
    buffer_[blockSz / sizeof(word64) - 2] = order ? preHiLen : preLoLen;
 
184
    buffer_[blockSz / sizeof(word64) - 1] = order ? preLoLen : preHiLen;
 
185
 
 
186
    Transform();
 
187
    ByteReverseIf(digest_, digest_, digestSz, order);
 
188
    memcpy(hash, digest_, digestSz);
 
189
 
 
190
    Init();  // reset state
 
191
}
 
192
 
 
193
#endif // WORD64_AVAILABLE
 
194
 
 
195
 
 
196
} // namespace