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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
| //
// Creator: http://www.dicelocksecurity.com
// Version: vers.4.0.0.1
//
// Copyright © 2009-2010 DiceLock Security, LLC. All rights reserved.
//
// DISCLAIMER
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// DICELOCK IS A REGISTERED TRADEMARK OR TRADEMARK OF THE OWNERS.
//
#include <memory.h>
#include <stdlib.h>
#include "sha384.h"
namespace DiceLockSecurity {
namespace Hash {
// Hash Algorithms Class enumerator name
const Hashes Sha384::hashName = SHA_384;
// Number of hash bits
const unsigned short int Sha384::hashBits = SHA384_DIGESTBITS;
// Number of hash unsigned chars
const unsigned short int Sha384::hashUCs = SHA384_DIGESTUCHARS;
// Number of hash unsigned short ints
const unsigned short int Sha384::hashUSs = SHA384_DIGESTUSHORTS;
// Number of hash unsigned long ints
const unsigned short int Sha384::hashULs = SHA384_DIGESTULONGS;
// Number of hash unsigned 64 bits
const unsigned short int Sha384::hash64s = SHA384_DIGESTULG64S;
// Initial hash values of SHA384
const unsigned __int64 Sha384::initials[SHA384_DIGESTULONGS] =
{0xcbbb9d5dc1059ed8,
0x629a292a367cd507,
0x9159015a3070dd17,
0x152fecd8f70e5939,
0x67332667ffc00b31,
0x8eb44a8768581511,
0xdb0c2e0d64f98fa7,
0x47b5481dbefa4fa4};
// Constructor, default
Sha384::Sha384() {
this->workingDigest512 = NULL;
this->autoWorkingDigest = false;
}
// Destructor
Sha384::~Sha384() {
if (autoWorkingDigest) {
delete this->workingDigest512;
this->workingDigest512 = NULL;
this->autoWorkingDigest = false;
}
}
// Set the Working Digest BaseCryptoRandomStream for underlaying SHA512 algorithm
void Sha384::SetWorkingDigest(BaseCryptoRandomStream* workDigest) {
this->workingDigest512 = workDigest;
}
// Set the Working Digest BaseCryptoRandomStream for underlaying SHA512 algorithm
unsigned short int Sha384::GetWorkingDigestUCLength(void) {
return this->Sha512::GetUCHashLength();
}
// Initializes common states of Sha1 algorithm
void Sha384::Initialize(void) {
if (this->workingDigest512 == NULL) {
this->workingDigest512 = new DefaultCryptoRandomStream();
this->workingDigest512->SetCryptoRandomStreamUC(this->GetWorkingDigestUCLength());
this->autoWorkingDigest = true;
}
this->workingDigest512->Set64Position(0, this->initials[0]);
this->workingDigest512->Set64Position(1, this->initials[1]);
this->workingDigest512->Set64Position(2, this->initials[2]);
this->workingDigest512->Set64Position(3, this->initials[3]);
this->workingDigest512->Set64Position(4, this->initials[4]);
this->workingDigest512->Set64Position(5, this->initials[5]);
this->workingDigest512->Set64Position(6, this->initials[6]);
this->workingDigest512->Set64Position(7, this->initials[7]);
this->remainingBytesLength = 0;
this->messageBitLengthHigh = 0;
this->messageBitLengthLow = 0;
}
// Adds the BaseCryptoRandomStream to the hash
void Sha384::Add(BaseCryptoRandomStream* stream) {
unsigned long int startStreamByte = 0, processBytes = 0;
long int numBytes = 0;
unsigned long int i = 0;
// If bytes left from previous added stream, then they will be processed now with added data from new stream
if (this->remainingBytesLength) {
if ((this->remainingBytesLength + stream->GetUCLength()) > ((unsigned long int)this->GetDataHashUCs() - 1)) {
// Setting the point to start the current stream processed
startStreamByte = this->GetDataHashUCs() - this->remainingBytesLength;
processBytes = stream->GetUCLength() - (this->GetDataHashUCs() - this->remainingBytesLength);
memcpy(this->remainingBytes + this->remainingBytesLength, stream->GetUCAddressPosition(0), this->GetDataHashUCs() - this->remainingBytesLength);
// Process remaining bytes of previous streams adn 64 byte padding of current stream
this->Compress(this->workingDigest512, this->remainingBytes);
// Updating message byt length processed
this->AddMessageLength(this->GetDataHashUCs());
// Remaining bytes of previous strema set to 0
this->remainingBytesLength = 0;
}
else {
processBytes = stream->GetUCLength();
}
}
else {
processBytes = stream->GetUCLength();
startStreamByte = 0;
}
for (numBytes = 0; processBytes > ((unsigned long int)this->GetDataHashUCs() - 1); numBytes += this->GetDataHashUCs()) {
// Process the chunk
this->Compress(this->workingDigest512, stream->GetUCAddressPosition(startStreamByte + numBytes));
// Updating message byt length processed
this->AddMessageLength(this->GetDataHashUCs());
processBytes -= this->GetDataHashUCs();
}
// If remaining bytes left, they will be copied for the next added stream
if (processBytes > 0) {
memcpy(this->remainingBytes + this->remainingBytesLength, stream->GetUCAddressPosition(stream->GetUCLength() - processBytes), processBytes);
this->remainingBytesLength += processBytes;
}
i = this->Get64HashLength();
for (i = 0; i < this->Get64HashLength(); i++) {
this->messageDigest->Set64Position(i, this->workingDigest512->Get64Position(i));
}
}
// Finalize the hash
void Sha384::Finalize(void) {
unsigned short int i;
this->remainingBytes[this->remainingBytesLength] = 0x80;
if ((this->remainingBytesLength * BYTEBITS) % this->dataHashBits >= this->equationModulo) {
memset(this->remainingBytes + this->remainingBytesLength + 1, 0, this->GetDataHashUCs() - this->remainingBytesLength -1);
this->Compress(this->workingDigest512, this->remainingBytes);
this->AddMessageLength(this->remainingBytesLength);
int i; i=this->GetDataHashUCs();
memset(this->remainingBytes, 0, this->GetDataHashUCs());
this->remainingBytesLength = 0;
}
else {
memset(this->remainingBytes + this->remainingBytesLength + 1, 0, this->GetDataHashUCs() - this->remainingBytesLength -1);
}
this->AddMessageLength(this->remainingBytesLength);
this->remainingBytes[112] = (((unsigned __int64)this->messageBitLengthHigh) >> 56) & 255;
this->remainingBytes[113] = (((unsigned __int64)this->messageBitLengthHigh) >> 48) & 255;
this->remainingBytes[114] = (((unsigned __int64)this->messageBitLengthHigh) >> 40) & 255;
this->remainingBytes[115] = (((unsigned __int64)this->messageBitLengthHigh) >> 32) & 255;
this->remainingBytes[116] = (((unsigned __int64)this->messageBitLengthHigh) >> 24) & 255;
this->remainingBytes[117] = (((unsigned __int64)this->messageBitLengthHigh) >> 16) & 255;
this->remainingBytes[118] = (((unsigned __int64)this->messageBitLengthHigh) >> 8) & 255;
this->remainingBytes[119] = (((unsigned __int64)this->messageBitLengthHigh)) & 255;
this->remainingBytes[120] = (((unsigned __int64)this->messageBitLengthLow) >> 56) & 255;
this->remainingBytes[121] = (((unsigned __int64)this->messageBitLengthLow) >> 48) & 255;
this->remainingBytes[122] = (((unsigned __int64)this->messageBitLengthLow) >> 40) & 255;
this->remainingBytes[123] = (((unsigned __int64)this->messageBitLengthLow) >> 32) & 255;
this->remainingBytes[124] = (((unsigned __int64)this->messageBitLengthLow) >> 24) & 255;
this->remainingBytes[125] = (((unsigned __int64)this->messageBitLengthLow) >> 16) & 255;
this->remainingBytes[126] = (((unsigned __int64)this->messageBitLengthLow) >> 8) & 255;
this->remainingBytes[127] = (((unsigned __int64)this->messageBitLengthLow)) & 255;
this->Compress(this->workingDigest512, this->remainingBytes);
for (i = 0; i < this->Get64HashLength(); i++) {
this->messageDigest->Set64Position(i, this->workingDigest512->Get64Position(i));
}
}
// Gets hash length in bits
unsigned short int Sha384::GetBitHashLength(void) {
return this->hashBits;
}
// Gets hash length in unsigned chars
unsigned short int Sha384::GetUCHashLength(void) {
return this->hashUCs;
}
// Gets hash length in unsigned short ints
unsigned short int Sha384::GetUSHashLength(void) {
return this->hashUSs;
}
// Gets hash length in unsigned long ints
unsigned short int Sha384::GetULHashLength(void) {
return this->hashULs;
}
// Gets hash length in unsigned 64 bits
unsigned short int Sha384::Get64HashLength(void) {
return this->hash64s;
}
// Gets the type of the object
Hashes Sha384::GetType(void) {
return this->hashName;
}
}
}
|