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 
92 static unsigned char PADDING[64] = {
93  0x80, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0,
95  0, 0, 0, 0, 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 /*
110 FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
111 Rotation 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 
147 void MD5::MD5Transform(unsigned long int state[4], unsigned char block[64])
148 {
149  unsigned long int a = state[0], b = state[1], c = state[2], d = state[3],
150  x[16];
151 
152  Decode(x, block, 64);
153 
154  /* Round 1 */
155  FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
156  FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
157  FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
158  FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
159  FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
160  FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
161  FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
162  FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
163  FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
164  FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
165  FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
166  FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
167  FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
168  FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
169  FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
170  FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
171 
172  /* Round 2 */
173  GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
174  GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
175  GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
176  GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
177  GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
178  GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
179  GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
180  GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
181  GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
182  GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
183  GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
184 
185  GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
186  GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
187  GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
188  GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
189  GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
190 
191  /* Round 3 */
192  HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
193  HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
194  HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
195  HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
196  HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
197  HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
198  HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
199  HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
200  HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
201  HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
202  HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
203  HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
204  HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
205  HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
206  HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
207  HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
208 
209  /* Round 4 */
210  II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
211  II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
212  II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
213  II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
214  II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
215  II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
216  II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
217  II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
218  II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
219  II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
220  II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
221  II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
222  II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
223  II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
224  II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
225  II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
226 
227  state[0] += a;
228  state[1] += b;
229  state[2] += c;
230  state[3] += d;
231 
232  /*
233  * Zeroize sensitive information.
234  */
235  MD5_memset((POINTER) x, 0, sizeof(x));
236 }
237 
246  unsigned char *output, unsigned long int *input, unsigned int len)
247 {
248  unsigned int i, j;
249 
250  for (i = 0, j = 0; j < len; i++, j += 4)
251  {
252  output[j] = (unsigned char) (input[i] & 0xff);
253  output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
254  output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
255  output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
256  }
257 }
258 
267  unsigned long int *output, unsigned char *input, unsigned int len)
268 {
269  unsigned int i, j;
270 
271  for (i = 0, j = 0; j < len; i++, j += 4)
272  output[i] = ((unsigned long int) input[j]) |
273  (((unsigned long int) input[j + 1]) << 8) |
274  (((unsigned long int) input[j + 2]) << 16) |
275  (((unsigned long int) input[j + 3]) << 24);
276 }
277 
285 void MD5::MD5_memcpy(POINTER output, POINTER input, unsigned int len)
286 {
287  MemoryCopy(output, input, len);
288 }
289 
298 void MD5::MD5_memset(POINTER output, int value, unsigned int len)
299 {
300  ByteSet(output, value, len);
301 }
302 
303 //----------------------------------------------------------------------
304 // public member-functions
305 
311 {
312  count[0] = count[1] = 0;
313  state[0] = 0x67452301;
314  state[1] = 0xefcdab89;
315  state[2] = 0x98badcfe;
316  state[3] = 0x10325476;
317 }
318 
326 void MD5::Input(unsigned char *input, unsigned int inputLen)
327 {
328  unsigned int i, index, partLen;
329 
330  /* Compute number of bytes mod 64 */
331  index = (unsigned int) ((count[0] >> 3) & 0x3F);
332 
333  /* Update number of bits */
334  if ((count[0] += ((unsigned long int) inputLen << 3)) <
335  ((unsigned long int) inputLen << 3))
336  count[1]++;
337 
338  count[1] += ((unsigned long int) inputLen >> 29);
339  partLen = 64 - index;
340 
341  /*
342  * Transform as many times as possible.
343  */
344  if (inputLen >= partLen)
345  {
346  MD5_memcpy((POINTER) &buffer[index], (POINTER) input, partLen);
348 
349  for (i = partLen; i + 63 < inputLen; i += 64)
350  MD5Transform(state, &input[i]);
351 
352  index = 0;
353  }
354  else
355  i = 0;
356 
357  /* Buffer remaining input */
358  MD5_memcpy((POINTER) &buffer[index], (POINTER) &input[i], inputLen - i);
359 }
360 
368 void MD5::Result(unsigned char digest[16])
369 {
370  unsigned char bits[8];
371  unsigned int index, padLen;
372 
373  /* Save number of bits */
374  Encode(bits, count, 8);
375 
376  /*
377  * Pad out to 56 mod 64.
378  */
379  index = (unsigned int) ((count[0] >> 3) & 0x3f);
380  padLen = (index < 56) ? (56 - index) : (120 - index);
381  Input(PADDING, padLen);
382 
383  /* Append length (before padding) */
384  Input(bits, 8);
385 
386  /* Store state in digest */
387  Encode(digest, state, 16);
388 }
389 
390 //----------------------------------------------------------------------
391 // EOF
void Input(unsigned char *input, unsigned int inputLen)
Block update operation. Continues an md5 message-digest operation, processing another message block...
Definition: md5.cc:326
void Decode(unsigned long int *output, unsigned char *input, unsigned int len)
Decodes input data into output.
Definition: md5.cc:266
void Encode(unsigned char *output, unsigned long int *input, unsigned int len)
Encodes input data.
Definition: md5.cc:245
void MD5Transform(unsigned long int state[4], unsigned char block[64])
Basic transformation. Transforms state based on block.
Definition: md5.cc:147
void MD5_memcpy(POINTER output, POINTER input, unsigned int len)
internal memory management
Definition: md5.cc:285
void MD5_memset(POINTER output, int value, unsigned int len)
internal memory management
Definition: md5.cc:298
void Result(unsigned char digest[16])
Finalization ends the md5 message-digest operation, writing the the message digest and zeroizing the ...
Definition: md5.cc:368
void Reset()
Initialization begins an operation, writing a new context.
Definition: md5.cc:310