|
pred@2125
|
1 |
// MD5.CC - source code for the C++/object oriented translation and |
|
pred@2125
|
2 |
// modification of MD5. |
|
pred@2125
|
3 |
|
|
pred@2125
|
4 |
// Translation and modification (c) 1995 by Mordechai T. Abzug |
|
pred@2125
|
5 |
|
|
pred@2125
|
6 |
// This translation/ modification is provided "as is," without express or |
|
pred@2125
|
7 |
// implied warranty of any kind. |
|
pred@2125
|
8 |
|
|
pred@2125
|
9 |
// The translator/ modifier does not claim (1) that MD5 will do what you think |
|
pred@2125
|
10 |
// it does; (2) that this translation/ modification is accurate; or (3) that |
|
pred@2125
|
11 |
// this software is "merchantible." (Language for this disclaimer partially |
|
pred@2125
|
12 |
// copied from the disclaimer below). |
|
pred@2125
|
13 |
|
|
pred@2125
|
14 |
/* based on: |
|
pred@2125
|
15 |
|
|
pred@2125
|
16 |
MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm |
|
pred@2125
|
17 |
MDDRIVER.C - test driver for MD2, MD4 and MD5 |
|
pred@2125
|
18 |
|
|
pred@2125
|
19 |
|
|
pred@2125
|
20 |
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All |
|
pred@2125
|
21 |
rights reserved. |
|
pred@2125
|
22 |
|
|
pred@2125
|
23 |
License to copy and use this software is granted provided that it |
|
pred@2125
|
24 |
is identified as the "RSA Data Security, Inc. MD5 Message-Digest |
|
pred@2125
|
25 |
Algorithm" in all material mentioning or referencing this software |
|
pred@2125
|
26 |
or this function. |
|
pred@2125
|
27 |
|
|
pred@2125
|
28 |
License is also granted to make and use derivative works provided |
|
pred@2125
|
29 |
that such works are identified as "derived from the RSA Data |
|
pred@2125
|
30 |
Security, Inc. MD5 Message-Digest Algorithm" in all material |
|
pred@2125
|
31 |
mentioning or referencing the derived work. |
|
pred@2125
|
32 |
|
|
pred@2125
|
33 |
RSA Data Security, Inc. makes no representations concerning either |
|
pred@2125
|
34 |
the merchantability of this software or the suitability of this |
|
pred@2125
|
35 |
software for any particular purpose. It is provided "as is" |
|
pred@2125
|
36 |
without express or implied warranty of any kind. |
|
pred@2125
|
37 |
|
|
pred@2125
|
38 |
These notices must be retained in any copies of any part of this |
|
pred@2125
|
39 |
documentation and/or software. |
|
pred@2125
|
40 |
|
|
pred@2125
|
41 |
*/ |
|
pred@2125
|
42 |
|
|
pred@2125
|
43 |
#include "md5.h" |
|
pred@2125
|
44 |
|
|
pred@2125
|
45 |
#include <assert.h> |
|
pred@2125
|
46 |
#include <string.h> |
|
pred@2125
|
47 |
|
|
pred@2125
|
48 |
// MD5 simple initialization method |
|
pred@2125
|
49 |
|
|
pred@2125
|
50 |
MD5::MD5(){ |
|
pred@2125
|
51 |
|
|
pred@2125
|
52 |
init(); |
|
pred@2125
|
53 |
|
|
pred@2125
|
54 |
} |
|
pred@2125
|
55 |
|
|
pred@2125
|
56 |
|
|
pred@2125
|
57 |
// MD5 block update operation. Continues an MD5 message-digest |
|
pred@2125
|
58 |
// operation, processing another message block, and updating the |
|
pred@2125
|
59 |
// context. |
|
pred@2125
|
60 |
|
|
pred@2125
|
61 |
void MD5::update (uint1 *input, uint4 input_length) { |
|
pred@2125
|
62 |
|
|
pred@2125
|
63 |
uint4 input_index, buffer_index; |
|
pred@2125
|
64 |
uint4 buffer_space; // how much space is left in buffer |
|
pred@2125
|
65 |
|
|
pred@2125
|
66 |
if (finalized){ // so we can't update! |
|
pred@2125
|
67 |
/*cerr << "MD5::update: Can't update a finalized digest!" << endl;*/ |
|
pred@2125
|
68 |
return; |
|
pred@2125
|
69 |
} |
|
pred@2125
|
70 |
|
|
pred@2125
|
71 |
// Compute number of bytes mod 64 |
|
pred@2125
|
72 |
buffer_index = (unsigned int)((count[0] >> 3) & 0x3F); |
|
pred@2125
|
73 |
|
|
pred@2125
|
74 |
// Update number of bits |
|
pred@2125
|
75 |
if ( (count[0] += ((uint4) input_length << 3))<((uint4) input_length << 3) ) |
|
pred@2125
|
76 |
count[1]++; |
|
pred@2125
|
77 |
|
|
pred@2125
|
78 |
count[1] += ((uint4)input_length >> 29); |
|
pred@2125
|
79 |
|
|
pred@2125
|
80 |
|
|
pred@2125
|
81 |
buffer_space = 64 - buffer_index; // how much space is left in buffer |
|
pred@2125
|
82 |
|
|
pred@2125
|
83 |
// Transform as many times as possible. |
|
pred@2125
|
84 |
if (input_length >= buffer_space) { // ie. we have enough to fill the buffer |
|
pred@2125
|
85 |
// fill the rest of the buffer and transform |
|
pred@2125
|
86 |
memcpy (buffer + buffer_index, input, buffer_space); |
|
pred@2125
|
87 |
transform (buffer); |
|
pred@2125
|
88 |
|
|
pred@2125
|
89 |
// now, transform each 64-byte piece of the input, bypassing the buffer |
|
pred@2125
|
90 |
for (input_index = buffer_space; input_index + 63 < input_length; |
|
pred@2125
|
91 |
input_index += 64) |
|
pred@2125
|
92 |
transform (input+input_index); |
|
pred@2125
|
93 |
|
|
pred@2125
|
94 |
buffer_index = 0; // so we can buffer remaining |
|
pred@2125
|
95 |
} |
|
pred@2125
|
96 |
else |
|
pred@2125
|
97 |
input_index=0; // so we can buffer the whole input |
|
pred@2125
|
98 |
|
|
pred@2125
|
99 |
|
|
pred@2125
|
100 |
// and here we do the buffering: |
|
pred@2125
|
101 |
memcpy(buffer+buffer_index, input+input_index, input_length-input_index); |
|
pred@2125
|
102 |
} |
|
pred@2125
|
103 |
|
|
pred@2125
|
104 |
|
|
pred@2125
|
105 |
|
|
pred@2125
|
106 |
// MD5 update for files. |
|
pred@2125
|
107 |
// Like above, except that it works on files (and uses above as a primitive.) |
|
pred@2125
|
108 |
|
|
pred@2125
|
109 |
void MD5::update(FILE *file){ |
|
pred@2125
|
110 |
|
|
pred@2125
|
111 |
unsigned char buffer[1024]; |
|
pred@2125
|
112 |
int len; |
|
pred@2125
|
113 |
|
|
pred@2125
|
114 |
while ((len=fread(buffer, 1, 1024, file))) |
|
pred@2125
|
115 |
update(buffer, len); |
|
pred@2125
|
116 |
|
|
pred@2125
|
117 |
fclose (file); |
|
pred@2125
|
118 |
|
|
pred@2125
|
119 |
} |
|
pred@2125
|
120 |
|
|
pred@2125
|
121 |
|
|
pred@2125
|
122 |
// MD5 finalization. Ends an MD5 message-digest operation, writing the |
|
pred@2125
|
123 |
// the message digest and zeroizing the context. |
|
pred@2125
|
124 |
|
|
pred@2125
|
125 |
|
|
pred@2125
|
126 |
void MD5::finalize (){ |
|
pred@2125
|
127 |
|
|
pred@2125
|
128 |
unsigned char bits[8]; |
|
pred@2125
|
129 |
unsigned int index, padLen; |
|
pred@2125
|
130 |
static uint1 PADDING[64]={ |
|
pred@2125
|
131 |
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
pred@2125
|
132 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
pred@2125
|
133 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
|
pred@2125
|
134 |
}; |
|
pred@2125
|
135 |
|
|
pred@2125
|
136 |
if (finalized){ |
|
pred@2125
|
137 |
/* cerr << "MD5::finalize: Already finalized this digest!" << endl;*/ |
|
pred@2125
|
138 |
return; |
|
pred@2125
|
139 |
} |
|
pred@2125
|
140 |
|
|
pred@2125
|
141 |
// Save number of bits |
|
pred@2125
|
142 |
encode (bits, count, 8); |
|
pred@2125
|
143 |
|
|
pred@2125
|
144 |
// Pad out to 56 mod 64. |
|
pred@2125
|
145 |
index = (uint4) ((count[0] >> 3) & 0x3f); |
|
pred@2125
|
146 |
padLen = (index < 56) ? (56 - index) : (120 - index); |
|
pred@2125
|
147 |
update (PADDING, padLen); |
|
pred@2125
|
148 |
|
|
pred@2125
|
149 |
// Append length (before padding) |
|
pred@2125
|
150 |
update (bits, 8); |
|
pred@2125
|
151 |
|
|
pred@2125
|
152 |
// Store state in digest |
|
pred@2125
|
153 |
encode (digest, state, 16); |
|
pred@2125
|
154 |
|
|
pred@2125
|
155 |
// Zeroize sensitive information |
|
pred@2125
|
156 |
memset (buffer, 0, sizeof(*buffer)); |
|
pred@2125
|
157 |
|
|
pred@2125
|
158 |
finalized=1; |
|
pred@2125
|
159 |
|
|
pred@2125
|
160 |
} |
|
pred@2125
|
161 |
|
|
pred@2125
|
162 |
|
|
pred@2125
|
163 |
|
|
pred@2125
|
164 |
|
|
pred@2125
|
165 |
MD5::MD5(FILE *file){ |
|
pred@2125
|
166 |
|
|
pred@2125
|
167 |
init(); // must be called be all constructors |
|
pred@2125
|
168 |
update(file); |
|
pred@2125
|
169 |
finalize (); |
|
pred@2125
|
170 |
} |
|
pred@2125
|
171 |
|
|
pred@2125
|
172 |
unsigned char *MD5::raw_digest(){ |
|
pred@2125
|
173 |
|
|
pred@2125
|
174 |
if (!finalized){ |
|
pred@2125
|
175 |
/* cerr << "MD5::raw_digest: Can't get digest if you haven't "<< |
|
pred@2125
|
176 |
"finalized the digest!" <<endl;*/ |
|
pred@2125
|
177 |
return ( (unsigned char*) ""); |
|
pred@2125
|
178 |
} |
|
pred@2125
|
179 |
|
|
kyle@3995
|
180 |
uint1 *s = new uint1[16]; |
|
pred@2125
|
181 |
memcpy(s, digest, 16); |
|
pred@2125
|
182 |
return s; |
|
pred@2125
|
183 |
} |
|
pred@2125
|
184 |
|
|
pred@2125
|
185 |
unsigned char *MD5::raw_digest(unsigned char buffer[16]) |
|
pred@2125
|
186 |
{ |
|
pred@2125
|
187 |
if (!finalized) |
|
pred@2125
|
188 |
{ |
|
pred@2125
|
189 |
return ( (unsigned char*) ""); |
|
pred@2125
|
190 |
} |
|
pred@2125
|
191 |
|
|
pred@2125
|
192 |
memcpy(buffer, digest, 16); |
|
pred@2125
|
193 |
return buffer; |
|
pred@2125
|
194 |
} |
|
pred@2125
|
195 |
|
|
pred@2125
|
196 |
|
|
pred@2125
|
197 |
|
|
pred@2125
|
198 |
char *MD5::hex_digest(){ |
|
pred@2125
|
199 |
|
|
pred@2125
|
200 |
if (!finalized){ |
|
pred@2125
|
201 |
/* cerr << "MD5::hex_digest: Can't get digest if you haven't "<< |
|
pred@2125
|
202 |
"finalized the digest!" <<endl;*/ |
|
pred@2125
|
203 |
return ""; |
|
pred@2125
|
204 |
} |
|
pred@2125
|
205 |
|
|
kyle@3995
|
206 |
int i; |
|
kyle@3995
|
207 |
char *s= new char[33]; |
|
kyle@3995
|
208 |
|
|
pred@2125
|
209 |
for (i=0; i<16; i++) |
|
pred@2125
|
210 |
sprintf(s+i*2, "%02x", digest[i]); |
|
pred@2125
|
211 |
|
|
pred@2125
|
212 |
s[32]='\0'; |
|
pred@2125
|
213 |
|
|
pred@2125
|
214 |
return s; |
|
pred@2125
|
215 |
} |
|
pred@2125
|
216 |
|
|
pred@2125
|
217 |
|
|
pred@2125
|
218 |
char *MD5::hex_digest(char buffer[33]){ |
|
pred@2125
|
219 |
|
|
pred@2125
|
220 |
int i; |
|
pred@2125
|
221 |
|
|
pred@2125
|
222 |
if (!finalized) |
|
pred@2125
|
223 |
{ |
|
pred@2125
|
224 |
/* cerr << "MD5::hex_digest: Can't get digest if you haven't "<< |
|
pred@2125
|
225 |
"finalized the digest!" <<endl;*/ |
|
pred@2125
|
226 |
return ""; |
|
pred@2125
|
227 |
} |
|
pred@2125
|
228 |
|
|
pred@2125
|
229 |
for (i=0; i<16; i++) |
|
pred@2125
|
230 |
sprintf(buffer+i*2, "%02x", digest[i]); |
|
pred@2125
|
231 |
|
|
pred@2125
|
232 |
buffer[32]='\0'; |
|
pred@2125
|
233 |
|
|
pred@2125
|
234 |
return buffer; |
|
pred@2125
|
235 |
} |
|
pred@2125
|
236 |
|
|
pred@2125
|
237 |
|
|
pred@2125
|
238 |
// PRIVATE METHODS: |
|
pred@2125
|
239 |
|
|
pred@2125
|
240 |
|
|
pred@2125
|
241 |
|
|
pred@2125
|
242 |
void MD5::init(){ |
|
pred@2125
|
243 |
finalized=0; // we just started! |
|
pred@2125
|
244 |
|
|
pred@2125
|
245 |
// Nothing counted, so count=0 |
|
pred@2125
|
246 |
count[0] = 0; |
|
pred@2125
|
247 |
count[1] = 0; |
|
pred@2125
|
248 |
|
|
pred@2125
|
249 |
// Load magic initialization constants. |
|
pred@2125
|
250 |
state[0] = 0x67452301; |
|
pred@2125
|
251 |
state[1] = 0xefcdab89; |
|
pred@2125
|
252 |
state[2] = 0x98badcfe; |
|
pred@2125
|
253 |
state[3] = 0x10325476; |
|
pred@2125
|
254 |
} |
|
pred@2125
|
255 |
|
|
pred@2125
|
256 |
|
|
pred@2125
|
257 |
|
|
pred@2125
|
258 |
// Constants for MD5Transform routine. |
|
pred@2125
|
259 |
// Although we could use C++ style constants, defines are actually better, |
|
pred@2125
|
260 |
// since they let us easily evade scope clashes. |
|
pred@2125
|
261 |
|
|
pred@2125
|
262 |
#define S11 7 |
|
pred@2125
|
263 |
#define S12 12 |
|
pred@2125
|
264 |
#define S13 17 |
|
pred@2125
|
265 |
#define S14 22 |
|
pred@2125
|
266 |
#define S21 5 |
|
pred@2125
|
267 |
#define S22 9 |
|
pred@2125
|
268 |
#define S23 14 |
|
pred@2125
|
269 |
#define S24 20 |
|
pred@2125
|
270 |
#define S31 4 |
|
pred@2125
|
271 |
#define S32 11 |
|
pred@2125
|
272 |
#define S33 16 |
|
pred@2125
|
273 |
#define S34 23 |
|
pred@2125
|
274 |
#define S41 6 |
|
pred@2125
|
275 |
#define S42 10 |
|
pred@2125
|
276 |
#define S43 15 |
|
pred@2125
|
277 |
#define S44 21 |
|
pred@2125
|
278 |
|
|
pred@2125
|
279 |
|
|
pred@2125
|
280 |
|
|
pred@2125
|
281 |
|
|
pred@2125
|
282 |
// MD5 basic transformation. Transforms state based on block. |
|
pred@2125
|
283 |
void MD5::transform (uint1 block[64]){ |
|
pred@2125
|
284 |
|
|
pred@2125
|
285 |
uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; |
|
pred@2125
|
286 |
|
|
pred@2125
|
287 |
decode (x, block, 64); |
|
pred@2125
|
288 |
|
|
pred@2125
|
289 |
assert(!finalized); // not just a user error, since the method is private |
|
pred@2125
|
290 |
|
|
pred@2125
|
291 |
/* Round 1 */ |
|
pred@2125
|
292 |
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ |
|
pred@2125
|
293 |
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ |
|
pred@2125
|
294 |
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ |
|
pred@2125
|
295 |
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ |
|
pred@2125
|
296 |
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ |
|
pred@2125
|
297 |
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ |
|
pred@2125
|
298 |
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ |
|
pred@2125
|
299 |
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ |
|
pred@2125
|
300 |
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ |
|
pred@2125
|
301 |
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ |
|
pred@2125
|
302 |
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ |
|
pred@2125
|
303 |
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ |
|
pred@2125
|
304 |
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ |
|
pred@2125
|
305 |
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ |
|
pred@2125
|
306 |
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ |
|
pred@2125
|
307 |
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ |
|
pred@2125
|
308 |
|
|
pred@2125
|
309 |
/* Round 2 */ |
|
pred@2125
|
310 |
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ |
|
pred@2125
|
311 |
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ |
|
pred@2125
|
312 |
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ |
|
pred@2125
|
313 |
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ |
|
pred@2125
|
314 |
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ |
|
pred@2125
|
315 |
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ |
|
pred@2125
|
316 |
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ |
|
pred@2125
|
317 |
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ |
|
pred@2125
|
318 |
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ |
|
pred@2125
|
319 |
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ |
|
pred@2125
|
320 |
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ |
|
pred@2125
|
321 |
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ |
|
pred@2125
|
322 |
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ |
|
pred@2125
|
323 |
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ |
|
pred@2125
|
324 |
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ |
|
pred@2125
|
325 |
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ |
|
pred@2125
|
326 |
|
|
pred@2125
|
327 |
/* Round 3 */ |
|
pred@2125
|
328 |
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ |
|
pred@2125
|
329 |
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ |
|
pred@2125
|
330 |
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ |
|
pred@2125
|
331 |
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ |
|
pred@2125
|
332 |
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ |
|
pred@2125
|
333 |
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ |
|
pred@2125
|
334 |
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ |
|
pred@2125
|
335 |
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ |
|
pred@2125
|
336 |
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ |
|
pred@2125
|
337 |
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ |
|
pred@2125
|
338 |
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ |
|
pred@2125
|
339 |
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ |
|
pred@2125
|
340 |
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ |
|
pred@2125
|
341 |
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ |
|
pred@2125
|
342 |
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ |
|
pred@2125
|
343 |
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ |
|
pred@2125
|
344 |
|
|
pred@2125
|
345 |
/* Round 4 */ |
|
pred@2125
|
346 |
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ |
|
pred@2125
|
347 |
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ |
|
pred@2125
|
348 |
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ |
|
pred@2125
|
349 |
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ |
|
pred@2125
|
350 |
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ |
|
pred@2125
|
351 |
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ |
|
pred@2125
|
352 |
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ |
|
pred@2125
|
353 |
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ |
|
pred@2125
|
354 |
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ |
|
pred@2125
|
355 |
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ |
|
pred@2125
|
356 |
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ |
|
pred@2125
|
357 |
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ |
|
pred@2125
|
358 |
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ |
|
pred@2125
|
359 |
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ |
|
pred@2125
|
360 |
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ |
|
pred@2125
|
361 |
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ |
|
pred@2125
|
362 |
|
|
pred@2125
|
363 |
state[0] += a; |
|
pred@2125
|
364 |
state[1] += b; |
|
pred@2125
|
365 |
state[2] += c; |
|
pred@2125
|
366 |
state[3] += d; |
|
pred@2125
|
367 |
|
|
pred@2125
|
368 |
// Zeroize sensitive information. |
|
pred@2125
|
369 |
memset ( (uint1 *) x, 0, sizeof(x)); |
|
pred@2125
|
370 |
|
|
pred@2125
|
371 |
} |
|
pred@2125
|
372 |
|
|
pred@2125
|
373 |
|
|
pred@2125
|
374 |
|
|
pred@2125
|
375 |
// Encodes input (UINT4) into output (unsigned char). Assumes len is |
|
pred@2125
|
376 |
// a multiple of 4. |
|
pred@2125
|
377 |
void MD5::encode (uint1 *output, uint4 *input, uint4 len) { |
|
pred@2125
|
378 |
|
|
pred@2125
|
379 |
unsigned int i, j; |
|
pred@2125
|
380 |
|
|
pred@2125
|
381 |
for (i = 0, j = 0; j < len; i++, j += 4) { |
|
pred@2125
|
382 |
output[j] = (uint1) (input[i] & 0xff); |
|
pred@2125
|
383 |
output[j+1] = (uint1) ((input[i] >> 8) & 0xff); |
|
pred@2125
|
384 |
output[j+2] = (uint1) ((input[i] >> 16) & 0xff); |
|
pred@2125
|
385 |
output[j+3] = (uint1) ((input[i] >> 24) & 0xff); |
|
pred@2125
|
386 |
} |
|
pred@2125
|
387 |
} |
|
pred@2125
|
388 |
|
|
pred@2125
|
389 |
|
|
pred@2125
|
390 |
|
|
pred@2125
|
391 |
|
|
pred@2125
|
392 |
// Decodes input (unsigned char) into output (UINT4). Assumes len is |
|
pred@2125
|
393 |
// a multiple of 4. |
|
pred@2125
|
394 |
void MD5::decode (uint4 *output, uint1 *input, uint4 len){ |
|
pred@2125
|
395 |
|
|
pred@2125
|
396 |
unsigned int i, j; |
|
pred@2125
|
397 |
|
|
pred@2125
|
398 |
for (i = 0, j = 0; j < len; i++, j += 4) |
|
pred@2125
|
399 |
output[i] = ((uint4)input[j]) | (((uint4)input[j+1]) << 8) | |
|
pred@2125
|
400 |
(((uint4)input[j+2]) << 16) | (((uint4)input[j+3]) << 24); |
|
pred@2125
|
401 |
} |
|
pred@2125
|
402 |
|
|
pred@2125
|
403 |
|
|
pred@2125
|
404 |
|
|
pred@2125
|
405 |
|
|
pred@2125
|
406 |
|
|
pred@2125
|
407 |
// Note: Replace "for loop" with standard memcpy if possible. |
|
pred@2125
|
408 |
void MD5::memcpy (uint1 *output, uint1 *input, uint4 len){ |
|
pred@2125
|
409 |
|
|
pred@2125
|
410 |
unsigned int i; |
|
pred@2125
|
411 |
|
|
pred@2125
|
412 |
for (i = 0; i < len; i++) |
|
pred@2125
|
413 |
output[i] = input[i]; |
|
pred@2125
|
414 |
} |
|
pred@2125
|
415 |
|
|
pred@2125
|
416 |
|
|
pred@2125
|
417 |
|
|
pred@2125
|
418 |
// Note: Replace "for loop" with standard memset if possible. |
|
pred@2125
|
419 |
void MD5::memset (uint1 *output, uint1 value, uint4 len){ |
|
pred@2125
|
420 |
|
|
pred@2125
|
421 |
unsigned int i; |
|
pred@2125
|
422 |
|
|
pred@2125
|
423 |
for (i = 0; i < len; i++) |
|
pred@2125
|
424 |
output[i] = value; |
|
pred@2125
|
425 |
} |
|
pred@2125
|
426 |
|
|
pred@2125
|
427 |
|
|
pred@2125
|
428 |
|
|
pred@2125
|
429 |
// ROTATE_LEFT rotates x left n bits. |
|
pred@2125
|
430 |
|
|
pred@2125
|
431 |
inline unsigned int MD5::rotate_left (uint4 x, uint4 n){ |
|
pred@2125
|
432 |
return (x << n) | (x >> (32-n)) ; |
|
pred@2125
|
433 |
} |
|
pred@2125
|
434 |
|
|
pred@2125
|
435 |
|
|
pred@2125
|
436 |
|
|
pred@2125
|
437 |
|
|
pred@2125
|
438 |
// F, G, H and I are basic MD5 functions. |
|
pred@2125
|
439 |
|
|
pred@2125
|
440 |
inline unsigned int MD5::F (uint4 x, uint4 y, uint4 z){ |
|
pred@2125
|
441 |
return (x & y) | (~x & z); |
|
pred@2125
|
442 |
} |
|
pred@2125
|
443 |
|
|
pred@2125
|
444 |
inline unsigned int MD5::G (uint4 x, uint4 y, uint4 z){ |
|
pred@2125
|
445 |
return (x & z) | (y & ~z); |
|
pred@2125
|
446 |
} |
|
pred@2125
|
447 |
|
|
pred@2125
|
448 |
inline unsigned int MD5::H (uint4 x, uint4 y, uint4 z){ |
|
pred@2125
|
449 |
return x ^ y ^ z; |
|
pred@2125
|
450 |
} |
|
pred@2125
|
451 |
|
|
pred@2125
|
452 |
inline unsigned int MD5::I (uint4 x, uint4 y, uint4 z){ |
|
pred@2125
|
453 |
return y ^ (x | ~z); |
|
pred@2125
|
454 |
} |
|
pred@2125
|
455 |
|
|
pred@2125
|
456 |
|
|
pred@2125
|
457 |
|
|
pred@2125
|
458 |
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. |
|
pred@2125
|
459 |
// Rotation is separate from addition to prevent recomputation. |
|
pred@2125
|
460 |
|
|
pred@2125
|
461 |
|
|
pred@2125
|
462 |
inline void MD5::FF(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, |
|
pred@2125
|
463 |
uint4 s, uint4 ac){ |
|
pred@2125
|
464 |
a += F(b, c, d) + x + ac; |
|
pred@2125
|
465 |
a = rotate_left (a, s) +b; |
|
pred@2125
|
466 |
} |
|
pred@2125
|
467 |
|
|
pred@2125
|
468 |
inline void MD5::GG(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, |
|
pred@2125
|
469 |
uint4 s, uint4 ac){ |
|
pred@2125
|
470 |
a += G(b, c, d) + x + ac; |
|
pred@2125
|
471 |
a = rotate_left (a, s) +b; |
|
pred@2125
|
472 |
} |
|
pred@2125
|
473 |
|
|
pred@2125
|
474 |
inline void MD5::HH(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, |
|
pred@2125
|
475 |
uint4 s, uint4 ac){ |
|
pred@2125
|
476 |
a += H(b, c, d) + x + ac; |
|
pred@2125
|
477 |
a = rotate_left (a, s) +b; |
|
pred@2125
|
478 |
} |
|
pred@2125
|
479 |
|
|
pred@2125
|
480 |
inline void MD5::II(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x, |
|
pred@2125
|
481 |
uint4 s, uint4 ac){ |
|
pred@2125
|
482 |
a += I(b, c, d) + x + ac; |
|
pred@2125
|
483 |
a = rotate_left (a, s) +b; |
|
pred@2125
|
484 |
} |