The Pedigree Project 0.1
md5.cc
1/*
2 * hashlib++ - a simple hash library for C++
3 *
4 * Copyright (c) 2007-2010 Benjamin Grüdelbach
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1) Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30//----------------------------------------------------------------------
31
32/*
33 * The hashlib++ MD5 implementation is derivative from the sourcecode
34 * published in RFC 1321
35 *
36 * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
37 * rights reserved.
38 *
39 * License to copy and use this software is granted provided that it
40 * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
41 * Algorithm" in all material mentioning or referencing this software
42 * or this function.
43 *
44 * License is also granted to make and use derivative works provided
45 * that such works are identified as "derived from the RSA Data
46 * Security, Inc. MD5 Message-Digest Algorithm" in all material
47 * mentioning or referencing the derived work.
48 *
49 * RSA Data Security, Inc. makes no representations concerning either
50 * the merchantability of this software or the suitability of this
51 * software for any particular purpose. It is provided "as is"
52 * without express or implied warranty of any kind.
53 *
54 * These notices must be retained in any copies of any part of this
55 * documentation and/or software.
56 */
57
58//----------------------------------------------------------------------
59
66//----------------------------------------------------------------------
67// Pedigree includes
68#include "pedigree/kernel/utilities/md5/md5.h"
69#include "pedigree/kernel/utilities/utility.h"
70
71//----------------------------------------------------------------------
72// defines
73
74// Constants for MD5Transform routine.
75#define S11 7
76#define S12 12
77#define S13 17
78#define S14 22
79#define S21 5
80#define S22 9
81#define S23 14
82#define S24 20
83#define S31 4
84#define S32 11
85#define S33 16
86#define S34 23
87#define S41 6
88#define S42 10
89#define S43 15
90#define S44 21
91
92static unsigned char PADDING[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
96
97/* F, G, H and I are basic MD5 functions. */
98#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
99#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
100#define H(x, y, z) ((x) ^ (y) ^ (z))
101#define I(x, y, z) ((y) ^ ((x) | (~z)))
102
103/*
104 * ROTATE_LEFT rotates x left n bits.
105 * cast to unsigned int to guarantee support for 64Bit System
106 */
107#define ROTATE_LEFT(x, n) (((x) << (n)) | (((unsigned int)x) >> (32 - (n))))
108
109/*
110FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
111Rotation is separate from addition to prevent recomputation.
112*/
113#define FF(a, b, c, d, x, s, ac) \
114 { \
115 (a) += F((b), (c), (d)) + (x) + (unsigned long int)(ac); \
116 (a) = ROTATE_LEFT((a), (s)); \
117 (a) += (b); \
118 }
119
120#define GG(a, b, c, d, x, s, ac) \
121 { \
122 (a) += G((b), (c), (d)) + (x) + (unsigned long int)(ac); \
123 (a) = ROTATE_LEFT((a), (s)); \
124 (a) += (b); \
125 }
126#define HH(a, b, c, d, x, s, ac) \
127 { \
128 (a) += H((b), (c), (d)) + (x) + (unsigned long int)(ac); \
129 (a) = ROTATE_LEFT((a), (s)); \
130 (a) += (b); \
131 }
132#define II(a, b, c, d, x, s, ac) \
133 { \
134 (a) += I((b), (c), (d)) + (x) + (unsigned long int)(ac); \
135 (a) = ROTATE_LEFT((a), (s)); \
136 (a) += (b); \
137 }
138
139//----------------------------------------------------------------------
140// private member-functions
141
147void MD5::MD5Transform(unsigned long int state[4], unsigned char block[64]) {
148 unsigned long int a = state[0], b = state[1], c = state[2], d = state[3], x[16];
149
150 Decode(x, block, 64);
151
152 /* Round 1 */
153 FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
154 FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
155 FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
156 FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
157 FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
158 FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
159 FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
160 FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
161 FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
162 FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
163 FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
164 FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
165 FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
166 FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
167 FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
168 FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
169
170 /* Round 2 */
171 GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
172 GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
173 GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
174 GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
175 GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
176 GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
177 GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
178 GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
179 GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
180 GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
181 GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
182
183 GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
184 GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
185 GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
186 GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
187 GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
188
189 /* Round 3 */
190 HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
191 HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
192 HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
193 HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
194 HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
195 HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
196 HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
197 HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
198 HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
199 HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
200 HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
201 HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
202 HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
203 HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
204 HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
205 HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
206
207 /* Round 4 */
208 II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
209 II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
210 II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
211 II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
212 II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
213 II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
214 II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
215 II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
216 II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
217 II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
218 II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
219 II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
220 II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
221 II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
222 II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
223 II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
224
225 state[0] += a;
226 state[1] += b;
227 state[2] += c;
228 state[3] += d;
229
230 /*
231 * Zeroize sensitive information.
232 */
233 MD5_memset((POINTER)x, 0, sizeof(x));
234}
235
243void MD5::Encode(unsigned char* output, unsigned long int* input, unsigned int len) {
244 unsigned int i, j;
245
246 for (i = 0, j = 0; j < len; i++, j += 4) {
247 output[j] = (unsigned char)(input[i] & 0xff);
248 output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff);
249 output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff);
250 output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff);
251 }
252}
253
261void MD5::Decode(unsigned long int* output, unsigned char* input, unsigned int len) {
262 unsigned int i, j;
263
264 for (i = 0, j = 0; j < len; i++, j += 4)
265 output[i] = ((unsigned long int)input[j]) | (((unsigned long int)input[j + 1]) << 8) |
266 (((unsigned long int)input[j + 2]) << 16) |
267 (((unsigned long int)input[j + 3]) << 24);
268}
269
277void MD5::MD5_memcpy(POINTER output, POINTER input, unsigned int len) {
278 MemoryCopy(output, input, len);
279}
280
289void MD5::MD5_memset(POINTER output, int value, unsigned int len) {
290 ByteSet(output, value, len);
291}
292
293//----------------------------------------------------------------------
294// public member-functions
295
301 count[0] = count[1] = 0;
302 state[0] = 0x67452301;
303 state[1] = 0xefcdab89;
304 state[2] = 0x98badcfe;
305 state[3] = 0x10325476;
306}
307
315void MD5::Input(unsigned char* input, unsigned int inputLen) {
316 unsigned int i, index, partLen;
317
318 /* Compute number of bytes mod 64 */
319 index = (unsigned int)((count[0] >> 3) & 0x3F);
320
321 /* Update number of bits */
322 if ((count[0] += ((unsigned long int)inputLen << 3)) < ((unsigned long int)inputLen << 3))
323 count[1]++;
324
325 count[1] += ((unsigned long int)inputLen >> 29);
326 partLen = 64 - index;
327
328 /*
329 * Transform as many times as possible.
330 */
331 if (inputLen >= partLen) {
332 MD5_memcpy((POINTER)&buffer[index], (POINTER)input, partLen);
334
335 for (i = partLen; i + 63 < inputLen; i += 64)
336 MD5Transform(state, &input[i]);
337
338 index = 0;
339 } else
340 i = 0;
341
342 /* Buffer remaining input */
343 MD5_memcpy((POINTER)&buffer[index], (POINTER)&input[i], inputLen - i);
344}
345
353void MD5::Result(unsigned char digest[16]) {
354 unsigned char bits[8];
355 unsigned int index, padLen;
356
357 /* Save number of bits */
358 Encode(bits, count, 8);
359
360 /*
361 * Pad out to 56 mod 64.
362 */
363 index = (unsigned int)((count[0] >> 3) & 0x3f);
364 padLen = (index < 56) ? (56 - index) : (120 - index);
365 Input(PADDING, padLen);
366
367 /* Append length (before padding) */
368 Input(bits, 8);
369
370 /* Store state in digest */
371 Encode(digest, state, 16);
372}
373
374//----------------------------------------------------------------------
375// EOF
void Result(unsigned char digest[16])
Finalization ends the md5 message-digest operation, writing the the message digest and zeroizing the ...
Definition md5.cc:353
void MD5_memset(POINTER output, int value, unsigned int len)
internal memory management
Definition md5.cc:289
void MD5_memcpy(POINTER output, POINTER input, unsigned int len)
internal memory management
Definition md5.cc:277
void MD5Transform(unsigned long int state[4], unsigned char block[64])
Basic transformation. Transforms state based on block.
Definition md5.cc:147
void Reset()
Initialization begins an operation, writing a new context.
Definition md5.cc:300
void Decode(unsigned long int *output, unsigned char *input, unsigned int len)
Decodes input data into output.
Definition md5.cc:261
void Input(unsigned char *input, unsigned int inputLen)
Block update operation. Continues an md5 message-digest operation, processing another message block,...
Definition md5.cc:315
void Encode(unsigned char *output, unsigned long int *input, unsigned int len)
Encodes input data.
Definition md5.cc:243