1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
/* hash.cpp
*
* Copyright (C) 2003 Sawtooth Consulting Ltd.
*
* This file is part of yaSSL.
*
* yaSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* yaSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
/* hash.cpp implements a base for digest types
*/
#include "runtime.hpp"
#include <string.h>
#include "hash.hpp"
namespace TaoCrypt {
// Update digest with data of size len, do in blocks
void HASHwithTransform::Update(const byte* data, word32 len)
{
// do block size increments
word32 blockSz = getBlockSize();
while (len) {
word32 add = min(len, blockSz - buffLen_);
memcpy(&buffer_[buffLen_], data, add);
buffLen_ += add;
data += add;
len -= add;
if (buffLen_ == blockSz) {
ByteReverseIf(buffer_, buffer_, blockSz, getByteOrder());
Transform();
}
}
}
// Final process, place digest in hash
void HASHwithTransform::Final(byte* hash)
{
word32 blockSz = getBlockSize();
word32 digestSz = getDigestSize();
word32 padSz = getPadSize();
ByteOrder order = getByteOrder();
word32 prePadLen = length_ + buffLen_ * 8; // in bits
buffer_[buffLen_++] = 0x80; // add 1
// pad with zeros
if (buffLen_ > padSz) {
while (buffLen_ < blockSz) buffer_[buffLen_++] = 0;
ByteReverseIf(buffer_, buffer_, blockSz, order);
Transform();
}
while (buffLen_ < padSz) buffer_[buffLen_++] = 0;
ByteReverseIf(buffer_, buffer_, blockSz, order);
word32 hiSize = 0; // for future 64 bit length TODO:
memcpy(&buffer_[padSz], order ? &hiSize : &prePadLen, sizeof(prePadLen));
memcpy(&buffer_[padSz+4], order ? &prePadLen : &hiSize, sizeof(prePadLen));
Transform();
ByteReverseIf(digest_, digest_, digestSz, order);
memcpy(hash, digest_, digestSz);
Init(); // reset state
}
} // namespace
|