1 | #ifndef LIBMD5_H |
---|
2 | #define LIBMD5_H |
---|
3 | |
---|
4 | typedef unsigned long int UINT4; |
---|
5 | typedef unsigned char *POINTER; |
---|
6 | #define PROTO_LIST(list) () |
---|
7 | |
---|
8 | typedef struct { |
---|
9 | UINT4 state[4]; /* state (ABCD) */ |
---|
10 | UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ |
---|
11 | unsigned char buffer[64]; /* input buffer */ |
---|
12 | } MD5_CTX; |
---|
13 | |
---|
14 | void MD5Init PROTO_LIST ((MD5_CTX *)); |
---|
15 | void MD5Update PROTO_LIST |
---|
16 | ((MD5_CTX *, unsigned char *, unsigned int)); |
---|
17 | void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *)); |
---|
18 | |
---|
19 | /* Length of test block, number of test blocks. |
---|
20 | */ |
---|
21 | #define TEST_BLOCK_LEN 1000 |
---|
22 | #define TEST_BLOCK_COUNT 1000 |
---|
23 | |
---|
24 | char* MDString PROTO_LIST ((char *)); |
---|
25 | char* MDFile PROTO_LIST ((char *)); |
---|
26 | char* MDPrint PROTO_LIST ((unsigned char [16])); |
---|
27 | |
---|
28 | #define MD5_CTX MD5_CTX |
---|
29 | #define MDInit MD5Init |
---|
30 | #define MDUpdate MD5Update |
---|
31 | #define MDFinal MD5Final |
---|
32 | |
---|
33 | #define S11 7 |
---|
34 | #define S12 12 |
---|
35 | #define S13 17 |
---|
36 | #define S14 22 |
---|
37 | #define S21 5 |
---|
38 | #define S22 9 |
---|
39 | #define S23 14 |
---|
40 | #define S24 20 |
---|
41 | #define S31 4 |
---|
42 | #define S32 11 |
---|
43 | #define S33 16 |
---|
44 | #define S34 23 |
---|
45 | #define S41 6 |
---|
46 | #define S42 10 |
---|
47 | #define S43 15 |
---|
48 | #define S44 21 |
---|
49 | |
---|
50 | /* F, G, H and I are basic MD5 functions. |
---|
51 | */ |
---|
52 | #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) |
---|
53 | #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) |
---|
54 | #define H(x, y, z) ((x) ^ (y) ^ (z)) |
---|
55 | #define I(x, y, z) ((y) ^ ((x) | (~z))) |
---|
56 | |
---|
57 | /* ROTATE_LEFT rotates x left n bits. |
---|
58 | */ |
---|
59 | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) |
---|
60 | |
---|
61 | /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. |
---|
62 | Rotation is separate from addition to prevent recomputation. |
---|
63 | */ |
---|
64 | #define FF(a, b, c, d, x, s, ac) { \ |
---|
65 | (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ |
---|
66 | (a) = ROTATE_LEFT ((a), (s)); \ |
---|
67 | (a) += (b); \ |
---|
68 | } |
---|
69 | #define GG(a, b, c, d, x, s, ac) { \ |
---|
70 | (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ |
---|
71 | (a) = ROTATE_LEFT ((a), (s)); \ |
---|
72 | (a) += (b); \ |
---|
73 | } |
---|
74 | #define HH(a, b, c, d, x, s, ac) { \ |
---|
75 | (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ |
---|
76 | (a) = ROTATE_LEFT ((a), (s)); \ |
---|
77 | (a) += (b); \ |
---|
78 | } |
---|
79 | #define II(a, b, c, d, x, s, ac) { \ |
---|
80 | (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ |
---|
81 | (a) = ROTATE_LEFT ((a), (s)); \ |
---|
82 | (a) += (b); \ |
---|
83 | } |
---|
84 | |
---|
85 | static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64])); |
---|
86 | static void Encode PROTO_LIST |
---|
87 | ((unsigned char *, UINT4 *, unsigned int)); |
---|
88 | static void Decode PROTO_LIST |
---|
89 | ((UINT4 *, unsigned char *, unsigned int)); |
---|
90 | |
---|
91 | |
---|
92 | static void MD5_memset (output, value, len) |
---|
93 | POINTER output; |
---|
94 | int value; |
---|
95 | unsigned int len; |
---|
96 | { |
---|
97 | unsigned int i; |
---|
98 | |
---|
99 | for (i = 0; i < len; i++) |
---|
100 | ((char *)output)[i] = (char)value; |
---|
101 | } |
---|
102 | |
---|
103 | static void Decode (output, input, len) |
---|
104 | UINT4 *output; |
---|
105 | unsigned char *input; |
---|
106 | unsigned int len; |
---|
107 | { |
---|
108 | unsigned int i, j; |
---|
109 | |
---|
110 | for (i = 0, j = 0; j < len; i++, j += 4) |
---|
111 | output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | |
---|
112 | (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); |
---|
113 | } |
---|
114 | |
---|
115 | void MD5Init (context) |
---|
116 | MD5_CTX *context; /* context */ |
---|
117 | { |
---|
118 | context->count[0] = context->count[1] = 0; |
---|
119 | /* Load magic initialization constants. |
---|
120 | */ |
---|
121 | context->state[0] = 0x67452301; |
---|
122 | context->state[1] = 0xefcdab89; |
---|
123 | context->state[2] = 0x98badcfe; |
---|
124 | context->state[3] = 0x10325476; |
---|
125 | } |
---|
126 | |
---|
127 | static unsigned char PADDING[64] = { |
---|
128 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
129 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
---|
130 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
---|
131 | }; |
---|
132 | |
---|
133 | static void Encode (output, input, len) |
---|
134 | unsigned char *output; |
---|
135 | UINT4 *input; |
---|
136 | unsigned int len; |
---|
137 | { |
---|
138 | unsigned int i, j; |
---|
139 | |
---|
140 | for (i = 0, j = 0; j < len; i++, j += 4) { |
---|
141 | output[j] = (unsigned char)(input[i] & 0xff); |
---|
142 | output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); |
---|
143 | output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); |
---|
144 | output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | static void MD5Transform (state, block) |
---|
149 | UINT4 state[4]; |
---|
150 | unsigned char block[64]; |
---|
151 | { |
---|
152 | UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; |
---|
153 | |
---|
154 | Decode (x, block, 64); |
---|
155 | |
---|
156 | /* Round 1 */ |
---|
157 | FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ |
---|
158 | FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ |
---|
159 | FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ |
---|
160 | FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ |
---|
161 | FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ |
---|
162 | FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ |
---|
163 | FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ |
---|
164 | FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ |
---|
165 | FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ |
---|
166 | FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ |
---|
167 | FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ |
---|
168 | FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ |
---|
169 | FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ |
---|
170 | FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ |
---|
171 | FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ |
---|
172 | FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ |
---|
173 | |
---|
174 | /* Round 2 */ |
---|
175 | GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ |
---|
176 | GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ |
---|
177 | GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ |
---|
178 | GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ |
---|
179 | GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ |
---|
180 | GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ |
---|
181 | GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ |
---|
182 | GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ |
---|
183 | GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ |
---|
184 | GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ |
---|
185 | GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ |
---|
186 | GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ |
---|
187 | GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ |
---|
188 | GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ |
---|
189 | GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ |
---|
190 | GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ |
---|
191 | |
---|
192 | /* Round 3 */ |
---|
193 | HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ |
---|
194 | HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ |
---|
195 | HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ |
---|
196 | HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ |
---|
197 | HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ |
---|
198 | HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ |
---|
199 | HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ |
---|
200 | HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ |
---|
201 | HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ |
---|
202 | HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ |
---|
203 | HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ |
---|
204 | HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ |
---|
205 | HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ |
---|
206 | HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ |
---|
207 | HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ |
---|
208 | HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ |
---|
209 | |
---|
210 | /* Round 4 */ |
---|
211 | II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ |
---|
212 | II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ |
---|
213 | II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ |
---|
214 | II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ |
---|
215 | II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ |
---|
216 | II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ |
---|
217 | II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ |
---|
218 | II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ |
---|
219 | II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ |
---|
220 | II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ |
---|
221 | II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ |
---|
222 | II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ |
---|
223 | II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ |
---|
224 | II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ |
---|
225 | II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ |
---|
226 | II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ |
---|
227 | |
---|
228 | state[0] += a; |
---|
229 | state[1] += b; |
---|
230 | state[2] += c; |
---|
231 | state[3] += d; |
---|
232 | |
---|
233 | /* Zeroize sensitive information. |
---|
234 | */ |
---|
235 | MD5_memset ((POINTER)x, 0, sizeof (x)); |
---|
236 | } |
---|
237 | |
---|
238 | void MD5Final (digest, context) |
---|
239 | unsigned char digest[16]; /* message digest */ |
---|
240 | MD5_CTX *context; /* context */ |
---|
241 | { |
---|
242 | unsigned char bits[8]; |
---|
243 | unsigned int index, padLen; |
---|
244 | |
---|
245 | /* Save number of bits */ |
---|
246 | Encode (bits, context->count, 8); |
---|
247 | |
---|
248 | /* Pad out to 56 mod 64. |
---|
249 | */ |
---|
250 | index = (unsigned int)((context->count[0] >> 3) & 0x3f); |
---|
251 | padLen = (index < 56) ? (56 - index) : (120 - index); |
---|
252 | MD5Update (context, PADDING, padLen); |
---|
253 | |
---|
254 | /* Append length (before padding) */ |
---|
255 | MD5Update (context, bits, 8); |
---|
256 | |
---|
257 | /* Store state in digest */ |
---|
258 | Encode (digest, context->state, 16); |
---|
259 | |
---|
260 | /* Zeroize sensitive information. |
---|
261 | */ |
---|
262 | MD5_memset ((POINTER)context, 0, sizeof (*context)); |
---|
263 | } |
---|
264 | |
---|
265 | static void MD5_memcpy (output, input, len) |
---|
266 | POINTER output; |
---|
267 | POINTER input; |
---|
268 | unsigned int len; |
---|
269 | { |
---|
270 | unsigned int i; |
---|
271 | |
---|
272 | for (i = 0; i < len; i++) |
---|
273 | output[i] = input[i]; |
---|
274 | } |
---|
275 | |
---|
276 | void MD5Update (context, input, inputLen) |
---|
277 | MD5_CTX *context; /* context */ |
---|
278 | unsigned char *input; /* input block */ |
---|
279 | unsigned int inputLen; /* length of input block */ |
---|
280 | { |
---|
281 | unsigned int i, index, partLen; |
---|
282 | |
---|
283 | /* Compute number of bytes mod 64 */ |
---|
284 | index = (unsigned int)((context->count[0] >> 3) & 0x3F); |
---|
285 | |
---|
286 | /* Update number of bits */ |
---|
287 | if ((context->count[0] += ((UINT4)inputLen << 3)) |
---|
288 | < ((UINT4)inputLen << 3)) |
---|
289 | context->count[1]++; |
---|
290 | context->count[1] += ((UINT4)inputLen >> 29); |
---|
291 | |
---|
292 | partLen = 64 - index; |
---|
293 | |
---|
294 | /* Transform as many times as possible. |
---|
295 | */ |
---|
296 | if (inputLen >= partLen) { |
---|
297 | MD5_memcpy |
---|
298 | ((POINTER)&context->buffer[index], (POINTER)input, partLen); |
---|
299 | MD5Transform (context->state, context->buffer); |
---|
300 | |
---|
301 | for (i = partLen; i + 63 < inputLen; i += 64) |
---|
302 | MD5Transform (context->state, &input[i]); |
---|
303 | |
---|
304 | index = 0; |
---|
305 | } |
---|
306 | else |
---|
307 | i = 0; |
---|
308 | |
---|
309 | /* Buffer remaining input */ |
---|
310 | MD5_memcpy |
---|
311 | ((POINTER)&context->buffer[index], (POINTER)&input[i], |
---|
312 | inputLen-i); |
---|
313 | } |
---|
314 | |
---|
315 | char* MDFile (char *filename) |
---|
316 | { |
---|
317 | FILE *file; |
---|
318 | MD5_CTX context; |
---|
319 | int len; |
---|
320 | unsigned char buffer[1024], digest[16]; |
---|
321 | |
---|
322 | if ((file = fopen (filename, "rb")) == NULL) |
---|
323 | printf ("%s can't be opened\n", filename); |
---|
324 | else |
---|
325 | { |
---|
326 | MDInit (&context); |
---|
327 | while ((len = fread(buffer, 1, 1024, file))) |
---|
328 | { |
---|
329 | MDUpdate (&context, buffer, len); |
---|
330 | } |
---|
331 | MDFinal (digest, &context); |
---|
332 | |
---|
333 | fclose (file); |
---|
334 | |
---|
335 | return MDPrint(digest); |
---|
336 | } |
---|
337 | return NULL; |
---|
338 | } |
---|
339 | |
---|
340 | char* MDString (char *string) |
---|
341 | { |
---|
342 | MD5_CTX context; |
---|
343 | unsigned char digest[16]; |
---|
344 | unsigned int len = strlen (string); |
---|
345 | |
---|
346 | MDInit (&context); |
---|
347 | MDUpdate (&context, string, len); |
---|
348 | MDFinal (digest, &context); |
---|
349 | return MDPrint(digest); |
---|
350 | } |
---|
351 | |
---|
352 | char* MDPrint (unsigned char digest[16]) |
---|
353 | { |
---|
354 | char* tmpstr = NULL; |
---|
355 | |
---|
356 | unsigned int i; |
---|
357 | |
---|
358 | for (i = 0; i < 16; i++) |
---|
359 | { |
---|
360 | char tmpstr1[16]; |
---|
361 | sprintf(tmpstr1,"%02x",(unsigned int)digest[i]); |
---|
362 | tmpstr = ostrcat(tmpstr, tmpstr1, 1, 0); |
---|
363 | } |
---|
364 | return tmpstr; |
---|
365 | } |
---|
366 | |
---|
367 | #endif |
---|