1//
2// MD4Engine.cpp
3//
4// Library: Foundation
5// Package: Crypt
6// Module: MD4Engine
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13//
14// MD4 (RFC 1320) algorithm:
15// Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
16// rights reserved.
17//
18// License to copy and use this software is granted provided that it
19// is identified as the "RSA Data Security, Inc. MD4 Message-Digest
20// Algorithm" in all material mentioning or referencing this software
21// or this function.
22//
23// License is also granted to make and use derivative works provided
24// that such works are identified as "derived from the RSA Data
25// Security, Inc. MD4 Message-Digest Algorithm" in all material
26// mentioning or referencing the derived work.
27//
28// RSA Data Security, Inc. makes no representations concerning either
29// the merchantability of this software or the suitability of this
30// software for any particular purpose. It is provided "as is"
31// without express or implied warranty of any kind.
32//
33// These notices must be retained in any copies of any part of this
34// documentation and/or software.
35//
36
37
38#include "Poco/MD4Engine.h"
39#include <cstring>
40
41
42namespace Poco {
43
44
45MD4Engine::MD4Engine()
46{
47 _digest.reserve(16);
48 reset();
49}
50
51
52MD4Engine::~MD4Engine()
53{
54 reset();
55}
56
57
58void MD4Engine::updateImpl(const void* input_, std::size_t inputLen)
59{
60 const unsigned char* input = (const unsigned char*) input_;
61 unsigned int i, index, partLen;
62
63 /* Compute number of bytes mod 64 */
64 index = (unsigned int)((_context.count[0] >> 3) & 0x3F);
65
66 /* Update number of bits */
67 if ((_context.count[0] += ((UInt32) inputLen << 3)) < ((UInt32) inputLen << 3))
68 _context.count[1]++;
69 _context.count[1] += ((UInt32) inputLen >> 29);
70
71 partLen = 64 - index;
72
73 /* Transform as many times as possible. */
74 if (inputLen >= partLen)
75 {
76 std::memcpy(&_context.buffer[index], input, partLen);
77 transform(_context.state, _context.buffer);
78
79 for (i = partLen; i + 63 < inputLen; i += 64)
80 transform(_context.state, &input[i]);
81
82 index = 0;
83 }
84 else i = 0;
85
86 /* Buffer remaining input */
87 std::memcpy(&_context.buffer[index], &input[i], inputLen-i);
88}
89
90
91std::size_t MD4Engine::digestLength() const
92{
93 return DIGEST_SIZE;
94}
95
96
97void MD4Engine::reset()
98{
99 std::memset(&_context, 0, sizeof(_context));
100 _context.count[0] = _context.count[1] = 0;
101 _context.state[0] = 0x67452301;
102 _context.state[1] = 0xefcdab89;
103 _context.state[2] = 0x98badcfe;
104 _context.state[3] = 0x10325476;
105}
106
107
108const DigestEngine::Digest& MD4Engine::digest()
109{
110 static const unsigned char PADDING[64] =
111 {
112 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
115 };
116 unsigned char bits[8];
117 unsigned int index, padLen;
118
119 /* Save number of bits */
120 encode(bits, _context.count, 8);
121
122 /* Pad out to 56 mod 64. */
123 index = (unsigned int)((_context.count[0] >> 3) & 0x3f);
124 padLen = (index < 56) ? (56 - index) : (120 - index);
125 update(PADDING, padLen);
126
127 /* Append length (before padding) */
128 update(bits, 8);
129
130 /* Store state in digestArray */
131 unsigned char digestArray[16];
132 encode(digestArray, _context.state, 16);
133 _digest.clear();
134 _digest.insert(_digest.begin(), digestArray, digestArray + sizeof(digestArray));
135
136 /* Zeroize sensitive information. */
137 std::memset(&_context, 0, sizeof (_context));
138 reset();
139 return _digest;
140}
141
142
143/* Constants for MD4Transform routine. */
144#define S11 3
145#define S12 7
146#define S13 11
147#define S14 19
148#define S21 3
149#define S22 5
150#define S23 9
151#define S24 13
152#define S31 3
153#define S32 9
154#define S33 11
155#define S34 15
156
157
158/* F, G and H are basic MD4 functions. */
159#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
160#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
161#define H(x, y, z) ((x) ^ (y) ^ (z))
162
163
164/* ROTATE_LEFT rotates x left n bits. */
165#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
166
167
168/* FF, GG and HH are transformations for rounds 1, 2 and 3 */
169/* Rotation is separate from addition to prevent recomputation */
170#define FF(a, b, c, d, x, s) { \
171 (a) += F ((b), (c), (d)) + (x); \
172 (a) = ROTATE_LEFT ((a), (s)); \
173 }
174#define GG(a, b, c, d, x, s) { \
175 (a) += G ((b), (c), (d)) + (x) + (UInt32)0x5a827999; \
176 (a) = ROTATE_LEFT ((a), (s)); \
177 }
178#define HH(a, b, c, d, x, s) { \
179 (a) += H ((b), (c), (d)) + (x) + (UInt32)0x6ed9eba1; \
180 (a) = ROTATE_LEFT ((a), (s)); \
181 }
182
183
184void MD4Engine::transform (UInt32 state[4], const unsigned char block[64])
185{
186 UInt32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
187
188 decode(x, block, 64);
189
190 /* Round 1 */
191 FF (a, b, c, d, x[ 0], S11); /* 1 */
192 FF (d, a, b, c, x[ 1], S12); /* 2 */
193 FF (c, d, a, b, x[ 2], S13); /* 3 */
194 FF (b, c, d, a, x[ 3], S14); /* 4 */
195 FF (a, b, c, d, x[ 4], S11); /* 5 */
196 FF (d, a, b, c, x[ 5], S12); /* 6 */
197 FF (c, d, a, b, x[ 6], S13); /* 7 */
198 FF (b, c, d, a, x[ 7], S14); /* 8 */
199 FF (a, b, c, d, x[ 8], S11); /* 9 */
200 FF (d, a, b, c, x[ 9], S12); /* 10 */
201 FF (c, d, a, b, x[10], S13); /* 11 */
202 FF (b, c, d, a, x[11], S14); /* 12 */
203 FF (a, b, c, d, x[12], S11); /* 13 */
204 FF (d, a, b, c, x[13], S12); /* 14 */
205 FF (c, d, a, b, x[14], S13); /* 15 */
206 FF (b, c, d, a, x[15], S14); /* 16 */
207
208 /* Round 2 */
209 GG (a, b, c, d, x[ 0], S21); /* 17 */
210 GG (d, a, b, c, x[ 4], S22); /* 18 */
211 GG (c, d, a, b, x[ 8], S23); /* 19 */
212 GG (b, c, d, a, x[12], S24); /* 20 */
213 GG (a, b, c, d, x[ 1], S21); /* 21 */
214 GG (d, a, b, c, x[ 5], S22); /* 22 */
215 GG (c, d, a, b, x[ 9], S23); /* 23 */
216 GG (b, c, d, a, x[13], S24); /* 24 */
217 GG (a, b, c, d, x[ 2], S21); /* 25 */
218 GG (d, a, b, c, x[ 6], S22); /* 26 */
219 GG (c, d, a, b, x[10], S23); /* 27 */
220 GG (b, c, d, a, x[14], S24); /* 28 */
221 GG (a, b, c, d, x[ 3], S21); /* 29 */
222 GG (d, a, b, c, x[ 7], S22); /* 30 */
223 GG (c, d, a, b, x[11], S23); /* 31 */
224 GG (b, c, d, a, x[15], S24); /* 32 */
225
226 /* Round 3 */
227 HH (a, b, c, d, x[ 0], S31); /* 33 */
228 HH (d, a, b, c, x[ 8], S32); /* 34 */
229 HH (c, d, a, b, x[ 4], S33); /* 35 */
230 HH (b, c, d, a, x[12], S34); /* 36 */
231 HH (a, b, c, d, x[ 2], S31); /* 37 */
232 HH (d, a, b, c, x[10], S32); /* 38 */
233 HH (c, d, a, b, x[ 6], S33); /* 39 */
234 HH (b, c, d, a, x[14], S34); /* 40 */
235 HH (a, b, c, d, x[ 1], S31); /* 41 */
236 HH (d, a, b, c, x[ 9], S32); /* 42 */
237 HH (c, d, a, b, x[ 5], S33); /* 43 */
238 HH (b, c, d, a, x[13], S34); /* 44 */
239 HH (a, b, c, d, x[ 3], S31); /* 45 */
240 HH (d, a, b, c, x[11], S32); /* 46 */
241 HH (c, d, a, b, x[ 7], S33); /* 47 */
242 HH (b, c, d, a, x[15], S34); /* 48 */
243
244 state[0] += a;
245 state[1] += b;
246 state[2] += c;
247 state[3] += d;
248
249 /* Zeroize sensitive information. */
250 std::memset(x, 0, sizeof(x));
251}
252
253
254void MD4Engine::encode(unsigned char* output, const UInt32* input, std::size_t len)
255{
256 unsigned int i, j;
257
258 for (i = 0, j = 0; j < len; i++, j += 4)
259 {
260 output[j] = (unsigned char)(input[i] & 0xff);
261 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
262 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
263 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
264 }
265}
266
267
268void MD4Engine::decode(UInt32* output, const unsigned char* input, std::size_t len)
269{
270 unsigned int i, j;
271
272 for (i = 0, j = 0; j < len; i++, j += 4)
273 output[i] = ((UInt32)input[j]) | (((UInt32)input[j+1]) << 8) |
274 (((UInt32)input[j+2]) << 16) | (((UInt32)input[j+3]) << 24);
275}
276
277
278} // namespace Poco
279