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
| //
// Creator: http://www.dicelocksecurity.com
// Version: vers.3.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.
#include <memory.h>
#include "sha1.h"
#define SHA1_Operation_Ini(f, a, b, c, d, e, temp, j, K)\
(*temp) = SHA1_RotateLeft((*a), 5) + f((*b), (*c), (*d)) + (*e) + (K) + (messageSchedule[j]);\
(*e) = (*d);\
(*d) = (*c);\
(*c) = SHA1_RotateLeft((*b), 30) ;\
(*b) = (*a);\
(*a) = (*temp);
#define SHA1_Operation_Tail(f, a, b, c, d, e, temp, j, K)\
messageSchedule[j] = (SHA1_RotateLeft(messageSchedule[j-3] ^ messageSchedule[j-8] ^ messageSchedule[j-14] ^ messageSchedule[j-16], 1));\
(*temp) = SHA1_RotateLeft((*a), 5) + f((*b), (*c), (*d)) + (*e) + (K) + (messageSchedule[j]);\
(*e) = (*d);\
(*d) = (*c);\
(*c) = SHA1_RotateLeft((*b), 30) ;\
(*b) = (*a);\
(*a) = (*temp);
namespace DiceLockSecurity {
namespace Hash {
// Hash Algorithms Class enumerator name
const Hashes Sha1::hashName = SHA_1;
// Number of hash bits
const unsigned short int Sha1::hashBits = SHA1_DIGESTBITS;
// Number of hash unsigned chars
const unsigned short int Sha1::hashUCs = SHA1_DIGESTUCHARS;
// Number of hash unsigned short ints
const unsigned short int Sha1::hashUSs = SHA1_DIGESTUSHORTS;
// Number of hash unsigned long ints
const unsigned short int Sha1::hashULs = SHA1_DIGESTULONGS;
// Number of schedule words
const unsigned short int Sha1::scheduleNumber = SHA1_MESSAGESCHEDULE;
// Initial hash values of SHA1
const unsigned long int Sha1::initials[SHA1_DIGESTULONGS] = {0x67452301UL,
0xefcdab89UL,
0x98badcfeUL,
0x10325476UL,
0xc3d2e1f0UL};
// Computational constant values of SHA1
const unsigned long int Sha1::constants[SHA1_COMPUTECONSTANTS] = {0x5a827999UL,
0x6ed9eba1UL,
0x8f1bbcdcUL,
0xca62c1d6UL};
// Computes the chunk block of information
void Sha1::Compress(BaseCryptoRandomStream* digest, unsigned char* stream) {
unsigned long int a, b, c, d, e, temp;
unsigned short int i;
// Initilizing working variables
a = digest->GetULPosition(0);
b = digest->GetULPosition(1);
c = digest->GetULPosition(2);
d = digest->GetULPosition(3);
e = digest->GetULPosition(4);
for (i = 0; i < this->dataHashULs; i++) {
messageSchedule[i] = (stream[i*4] << 24) | (stream[i*4+1] << 16) | (stream[i*4+2] << 8) | (stream[i*4+3]);
}
// 0 <= t <= 19
for (i = 0; i < 16; i++) {
SHA1_Operation_Ini(BASESHA_32_Ch, &a, &b, &c, &d, &e, &temp, i, this->constants[0]);
}
for (i = 16; i < 20; i++) {
SHA1_Operation_Tail(BASESHA_32_Ch, &a, &b, &c, &d, &e, &temp, i, this->constants[0]);
}
// 20 <= t <= 39
for (i = 20; i < 40; i++) {
SHA1_Operation_Tail(SHA1_Parity, &a, &b, &c, &d, &e, &temp, i, this->constants[1]);
}
// 40 <= t <= 59
for (i = 40; i < 60; i++) {
SHA1_Operation_Tail(BASESHA_32_Maj, &a, &b, &c, &d, &e, &temp, i, this->constants[2]);
}
// 60 <= t <= 79
for (i = 60; i < SHA1_OPERATIONS; i++) {
SHA1_Operation_Tail(SHA1_Parity, &a, &b, &c, &d, &e, &temp, i, this->constants[3]);
}
// Upgrading hash values
digest->SetULPosition(0, digest->GetULPosition(0) + a);
digest->SetULPosition(1, digest->GetULPosition(1) + b);
digest->SetULPosition(2, digest->GetULPosition(2) + c);
digest->SetULPosition(3, digest->GetULPosition(3) + d);
digest->SetULPosition(4, digest->GetULPosition(4) + e);
}
// Constructor, default
Sha1::Sha1() {
}
// Destructor
Sha1::~Sha1() {
}
// Initializes common states of Sha1 algorithm
void Sha1::Initialize(void) {
this->messageDigest->SetULPosition(0, this->initials[0]);
this->messageDigest->SetULPosition(1, this->initials[1]);
this->messageDigest->SetULPosition(2, this->initials[2]);
this->messageDigest->SetULPosition(3, this->initials[3]);
this->messageDigest->SetULPosition(4, this->initials[4]);
this->remainingBytesLength = 0;
this->messageBitLengthHigh = 0;
this->messageBitLengthLow = 0;
}
// Gets hash length in bits
unsigned short int Sha1::GetBitHashLength(void) {
return this->hashBits;
}
// Gets hash length in unsigned chars
unsigned short int Sha1::GetUCHashLength(void) {
return this->hashUCs;
}
// Gets hash length in unsigned short ints
unsigned short int Sha1::GetUSHashLength(void) {
return this->hashUSs;
}
// Gets hash length in unsigned long ints
unsigned short int Sha1::GetULHashLength(void) {
return this->hashULs;
}
// Gets the type of the object
Hashes Sha1::GetType(void) {
return this->hashName;
}
}
}
|