This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MD4.cpp
272 lines (239 loc) · 8.26 KB
/
MD4.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "MD4.h"
#include <algorithm>
#include <cstring>
const unsigned char hashPadding[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
CMD4::CMD4()
{
Reset();
}
void CMD4::GetHash(__in_bcount(16) uchar* pHash) const
{
std::transform(m_State.m_nState,
m_State.m_nState + sizeof(m_State.m_nState) / sizeof(m_State.m_nState[0]),
(uint32*)pHash, transformToLE< uint32 >);
}
void CMD4::Reset()
{
m_State.m_nCount = 0;
// Load magic initialization constants
m_State.m_nState[0] = 0x67452301;
m_State.m_nState[1] = 0xefcdab89;
m_State.m_nState[2] = 0x98badcfe;
m_State.m_nState[3] = 0x10325476;
}
void CMD4::Finish()
{
// Save number of bits
uint64 bits = transformToLE(m_State.m_nCount * 8);
// Pad out to 56 mod 64.
uint32 index = static_cast< uint32 >(m_State.m_nCount % m_State.blockSize);
Add(hashPadding, m_State.blockSize - sizeof(bits) - index
+ (index < m_State.blockSize - sizeof(bits) ? 0 : m_State.blockSize));
Add(&bits, sizeof(bits));
}
#ifdef HASHLIB_USE_ASM
#if defined(_WIN64) || defined(__x86_64__)
extern "C" void __fastcall MD4_x64(const void *, const void* pData, std::size_t nLength);
#else
extern "C" void __stdcall MD4_Add_p5(CMD4::MD4State*, const void* pData, std::size_t nLength);
#endif
void CMD4::Add(const void* pData, std::size_t nLength)
{
#if defined(_WIN64) || defined(__x86_64__)
// Update number of bytes
const char* input = static_cast< const char* >(pData);
{
uint32 index = static_cast< uint32 >(m_State.m_nCount % m_State.blockSize);
m_State.m_nCount += nLength;
if (index)
{
// buffer has some data already - lets fill it
// before doing the rest of the transformation on the original data
if (index + nLength < m_State.blockSize)
{
memcpy(m_State.m_oBuffer + index, input, nLength);
return;
}
memcpy(m_State.m_oBuffer + index, input, m_State.blockSize - index);
nLength -= m_State.blockSize - index;
input += m_State.blockSize - index;
MD4_x64(&(m_State.m_nState[0]), m_State.m_oBuffer, 1);
}
}
if (nLength >= m_State.blockSize)
{
// Transform as many times as possible using the original data stream
const char* const end = input + nLength - nLength % m_State.blockSize;
size_t abs = nLength / m_State.blockSize;
MD4_x64(&(m_State.m_nState[0]), input, abs);
abs *= m_State.blockSize;
input += abs;
nLength %= m_State.blockSize;
}
// Buffer remaining input
if (nLength)
memcpy(m_State.m_oBuffer, input, nLength);
#else
MD4_Add_p5(&m_State, pData, nLength);
#endif
}
#else // HASHLIB_USE_ASM
namespace
{
// Constants for MD4 Transform
template< uint32 tier, uint32 stage > struct S;
template<> struct S< 0, 0 > { static const uint32 value = 3; };
template<> struct S< 0, 1 > { static const uint32 value = 7; };
template<> struct S< 0, 2 > { static const uint32 value = 11; };
template<> struct S< 0, 3 > { static const uint32 value = 19; };
template<> struct S< 1, 0 > { static const uint32 value = 3; };
template<> struct S< 1, 1 > { static const uint32 value = 5; };
template<> struct S< 1, 2 > { static const uint32 value = 9; };
template<> struct S< 1, 3 > { static const uint32 value = 13; };
template<> struct S< 2, 0 > { static const uint32 value = 3; };
template<> struct S< 2, 1 > { static const uint32 value = 9; };
template<> struct S< 2, 2 > { static const uint32 value = 11; };
template<> struct S< 2, 3 > { static const uint32 value = 15; };
// F transformation
template< uint32 round >
__forceinline void F(const uint32* data, uint32& a, uint32 b, uint32 c, uint32 d)
{
static const uint32 x = round;
static const uint8 s = S< 0, round % 4 >::value;
a += (d ^ (b & (c ^ d))) + transformFromLE(data[x]);
a = rotateLeft(a, s);
}
// G transformation
template< uint32 round >
__forceinline void G(const uint32* data, uint32& a, uint32 b, uint32 c, uint32 d)
{
static const uint32 x = (round % 4) * 4 + (round / 4);
static const uint8 s = S< 1, round % 4 >::value;
a += ((b & c) | (d & (b | c))) + transformFromLE(data[x]) + 0x5a827999u;
a = rotateLeft(a, s);
}
// H transformation
template< uint32 round >
__forceinline void H(const uint32* data, uint32& a, uint32 b, uint32 c, uint32 d)
{
static const uint32 x = (round % 2) * 8 + (round / 2 % 2) * 4
+ (round / 4 % 2) * 2 + (round / 8);
static const uint8 s = S< 2, round % 4 >::value;
a += (b ^ c ^ d) + transformFromLE(data[x]) + 0x6ed9eba1u;
a = rotateLeft(a, s);
}
} // namespace
// MD4 basic transformation. Transforms state based on block.
void CMD4::Transform(const uint32* data)
{
uint32 a = m_State.m_nState[0];
uint32 b = m_State.m_nState[1];
uint32 c = m_State.m_nState[2];
uint32 d = m_State.m_nState[3];
F< 0 >(data, a, b, c, d);
F< 1 >(data, d, a, b, c);
F< 2 >(data, c, d, a, b);
F< 3 >(data, b, c, d, a);
F< 4 >(data, a, b, c, d);
F< 5 >(data, d, a, b, c);
F< 6 >(data, c, d, a, b);
F< 7 >(data, b, c, d, a);
F< 8 >(data, a, b, c, d);
F< 9 >(data, d, a, b, c);
F< 10 >(data, c, d, a, b);
F< 11 >(data, b, c, d, a);
F< 12 >(data, a, b, c, d);
F< 13 >(data, d, a, b, c);
F< 14 >(data, c, d, a, b);
F< 15 >(data, b, c, d, a);
G< 0 >(data, a, b, c, d);
G< 1 >(data, d, a, b, c);
G< 2 >(data, c, d, a, b);
G< 3 >(data, b, c, d, a);
G< 4 >(data, a, b, c, d);
G< 5 >(data, d, a, b, c);
G< 6 >(data, c, d, a, b);
G< 7 >(data, b, c, d, a);
G< 8 >(data, a, b, c, d);
G< 9 >(data, d, a, b, c);
G< 10 >(data, c, d, a, b);
G< 11 >(data, b, c, d, a);
G< 12 >(data, a, b, c, d);
G< 13 >(data, d, a, b, c);
G< 14 >(data, c, d, a, b);
G< 15 >(data, b, c, d, a);
H< 0 >(data, a, b, c, d);
H< 1 >(data, d, a, b, c);
H< 2 >(data, c, d, a, b);
H< 3 >(data, b, c, d, a);
H< 4 >(data, a, b, c, d);
H< 5 >(data, d, a, b, c);
H< 6 >(data, c, d, a, b);
H< 7 >(data, b, c, d, a);
H< 8 >(data, a, b, c, d);
H< 9 >(data, d, a, b, c);
H< 10 >(data, c, d, a, b);
H< 11 >(data, b, c, d, a);
H< 12 >(data, a, b, c, d);
H< 13 >(data, d, a, b, c);
H< 14 >(data, c, d, a, b);
H< 15 >(data, b, c, d, a);
m_State.m_nState[0] += a;
m_State.m_nState[1] += b;
m_State.m_nState[2] += c;
m_State.m_nState[3] += d;
}
void CMD4::Add(const void* pData, std::size_t nLength)
{
// Update number of bytes
const char* input = static_cast<const char*>(pData);
{
uint32 index = static_cast<uint32>(m_State.m_nCount % m_State.blockSize);
m_State.m_nCount += nLength;
if (index)
{
// buffer has some data already - lets fill it
// before doing the rest of the transformation on the original data
if (index + nLength < m_State.blockSize)
{
memcpy(m_State.m_oBuffer + index, input, nLength);
return;
}
memcpy(m_State.m_oBuffer + index, input, m_State.blockSize - index);
nLength -= m_State.blockSize - index;
input += m_State.blockSize - index;
Transform(reinterpret_cast<const uint32*>(m_State.m_oBuffer));
}
}
// Transform as many times as possible using the original data stream
const char* const end = input + nLength - nLength % m_State.blockSize;
nLength %= m_State.blockSize;
for (; input != end; input += m_State.blockSize)
Transform(reinterpret_cast<const uint32*>(input));
// Buffer remaining input
if (nLength)
memcpy(m_State.m_oBuffer, input, nLength);
}
#endif // HASHLIB_USE_ASM
//
// Free implementation of the MD4 hash algorithm
// MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
//
// Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
// License to copy and use this software is granted provided that it
// is identified as the "RSA Data Security, Inc. MD4 Message-Digest
// Algorithm" in all material mentioning or referencing this software
// or this function.
// License is also granted to make and use derivative works provided
// that such works are identified as "derived from the RSA Data
// Security, Inc. MD4 Message-Digest Algorithm" in all material
// mentioning or referencing the derived work.
// RSA Data Security, Inc. makes no representations concerning either
// the merchantability of this software or the suitability of this
// software for any particular purpose. It is provided "as is"
// without express or implied warranty of any kind.
// These notices must be retained in any copies of any part of this
// documentation and/or software.