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