1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2007 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Taken from: ext/standard/md5.c |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : /* $Id: hash_md.c,v 1.6.2.4.2.3 2007/01/08 22:29:25 nlopess Exp $ */
20 :
21 : #include "php_hash.h"
22 : #include "php_hash_md.h"
23 :
24 : const php_hash_ops php_hash_md5_ops = {
25 : (php_hash_init_func_t) PHP_MD5Init,
26 : (php_hash_update_func_t) PHP_MD5Update,
27 : (php_hash_final_func_t) PHP_MD5Final,
28 : 16,
29 : 64,
30 : sizeof(PHP_MD5_CTX)
31 : };
32 :
33 : const php_hash_ops php_hash_md4_ops = {
34 : (php_hash_init_func_t) PHP_MD4Init,
35 : (php_hash_update_func_t) PHP_MD4Update,
36 : (php_hash_final_func_t) PHP_MD4Final,
37 : 16,
38 : 64,
39 : sizeof(PHP_MD4_CTX)
40 : };
41 :
42 : const php_hash_ops php_hash_md2_ops = {
43 : (php_hash_init_func_t) PHP_MD2Init,
44 : (php_hash_update_func_t) PHP_MD2Update,
45 : (php_hash_final_func_t) PHP_MD2Final,
46 : 16,
47 : 16,
48 : sizeof(PHP_MD2_CTX)
49 : };
50 :
51 : /* MD common stuff */
52 :
53 : static const unsigned char PADDING[64] =
54 : {
55 : 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
58 : };
59 :
60 : /* {{{ Encode
61 : Encodes input (php_hash_uint32) into output (unsigned char). Assumes len is
62 : a multiple of 4.
63 : */
64 : static void Encode(unsigned char *output, php_hash_uint32 *input, unsigned int len)
65 0 : {
66 : unsigned int i, j;
67 :
68 0 : for (i = 0, j = 0; j < len; i++, j += 4) {
69 0 : output[j] = (unsigned char) (input[i] & 0xff);
70 0 : output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
71 0 : output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
72 0 : output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
73 : }
74 0 : }
75 : /* }}} */
76 :
77 : /* {{{ Decode
78 : Decodes input (unsigned char) into output (php_hash_uint32). Assumes len is
79 : a multiple of 4.
80 : */
81 : static void Decode(php_hash_uint32 *output, const unsigned char *input, unsigned int len)
82 0 : {
83 : unsigned int i, j;
84 :
85 0 : for (i = 0, j = 0; j < len; i++, j += 4)
86 0 : output[i] = ((php_hash_uint32) input[j]) | (((php_hash_uint32) input[j + 1]) << 8) |
87 : (((php_hash_uint32) input[j + 2]) << 16) | (((php_hash_uint32) input[j + 3]) << 24);
88 0 : }
89 : /* }}} */
90 :
91 : #ifdef PHP_HASH_MD5_NOT_IN_CORE
92 :
93 : /* MD5 */
94 :
95 : PHP_HASH_API void make_digest(char *md5str, unsigned char *digest)
96 : {
97 : php_hash_bin2hex(md5str, digest, 16);
98 : md5str[32] = '\0';
99 : }
100 :
101 : /* {{{ proto string md5(string str, [ bool raw_output])
102 : Calculate the md5 hash of a string */
103 : PHP_NAMED_FUNCTION(php_if_md5)
104 : {
105 : char *arg;
106 : int arg_len;
107 : zend_bool raw_output = 0;
108 : char md5str[33];
109 : PHP_MD5_CTX context;
110 : unsigned char digest[16];
111 :
112 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
113 : return;
114 : }
115 :
116 : md5str[0] = '\0';
117 : PHP_MD5Init(&context);
118 : PHP_MD5Update(&context, arg, arg_len);
119 : PHP_MD5Final(digest, &context);
120 : if (raw_output) {
121 : RETURN_STRINGL(digest, 16, 1);
122 : } else {
123 : make_digest(md5str, digest);
124 : RETVAL_STRING(md5str, 1);
125 : }
126 :
127 : }
128 : /* }}} */
129 :
130 : /* {{{ proto string md5_file(string filename [, bool raw_output])
131 : Calculate the md5 hash of given filename */
132 : PHP_NAMED_FUNCTION(php_if_md5_file)
133 : {
134 : char *arg;
135 : int arg_len;
136 : zend_bool raw_output = 0;
137 : char md5str[33];
138 : unsigned char buf[1024];
139 : unsigned char digest[16];
140 : PHP_MD5_CTX context;
141 : int n;
142 : php_stream *stream;
143 :
144 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
145 : return;
146 : }
147 :
148 : stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL);
149 : if (!stream) {
150 : RETURN_FALSE;
151 : }
152 :
153 : PHP_MD5Init(&context);
154 :
155 : while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
156 : PHP_MD5Update(&context, buf, n);
157 : }
158 :
159 : PHP_MD5Final(digest, &context);
160 :
161 : php_stream_close(stream);
162 :
163 : if (n<0) {
164 : RETURN_FALSE;
165 : }
166 :
167 : if (raw_output) {
168 : RETURN_STRINGL(digest, 16, 1);
169 : } else {
170 : make_digest(md5str, digest);
171 : RETVAL_STRING(md5str, 1);
172 : }
173 : }
174 : /* }}} */
175 :
176 : /*
177 : * The remaining code is the reference MD5 code (md5c.c) from rfc1321
178 : */
179 : /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
180 : */
181 :
182 : /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
183 : rights reserved.
184 :
185 : License to copy and use this software is granted provided that it
186 : is identified as the "RSA Data Security, Inc. MD5 Message-Digest
187 : Algorithm" in all material mentioning or referencing this software
188 : or this function.
189 :
190 : License is also granted to make and use derivative works provided
191 : that such works are identified as "derived from the RSA Data
192 : Security, Inc. MD5 Message-Digest Algorithm" in all material
193 : mentioning or referencing the derived work.
194 :
195 : RSA Data Security, Inc. makes no representations concerning either
196 : the merchantability of this software or the suitability of this
197 : software for any particular purpose. It is provided "as is"
198 : without express or implied warranty of any kind.
199 :
200 : These notices must be retained in any copies of any part of this
201 : documentation and/or software.
202 : */
203 :
204 : /* Constants for MD5Transform routine.
205 : */
206 :
207 : #define S11 7
208 : #define S12 12
209 : #define S13 17
210 : #define S14 22
211 : #define S21 5
212 : #define S22 9
213 : #define S23 14
214 : #define S24 20
215 : #define S31 4
216 : #define S32 11
217 : #define S33 16
218 : #define S34 23
219 : #define S41 6
220 : #define S42 10
221 : #define S43 15
222 : #define S44 21
223 :
224 : static void MD5Transform(php_hash_uint32[4], const unsigned char[64]);
225 :
226 : /* F, G, H and I are basic MD5 functions.
227 : */
228 : #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
229 : #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
230 : #define H(x, y, z) ((x) ^ (y) ^ (z))
231 : #define I(x, y, z) ((y) ^ ((x) | (~z)))
232 :
233 : /* ROTATE_LEFT rotates x left n bits.
234 : */
235 : #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
236 :
237 : /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
238 : Rotation is separate from addition to prevent recomputation.
239 : */
240 : #define FF(a, b, c, d, x, s, ac) { \
241 : (a) += F ((b), (c), (d)) + (x) + (php_hash_uint32)(ac); \
242 : (a) = ROTATE_LEFT ((a), (s)); \
243 : (a) += (b); \
244 : }
245 : #define GG(a, b, c, d, x, s, ac) { \
246 : (a) += G ((b), (c), (d)) + (x) + (php_hash_uint32)(ac); \
247 : (a) = ROTATE_LEFT ((a), (s)); \
248 : (a) += (b); \
249 : }
250 : #define HH(a, b, c, d, x, s, ac) { \
251 : (a) += H ((b), (c), (d)) + (x) + (php_hash_uint32)(ac); \
252 : (a) = ROTATE_LEFT ((a), (s)); \
253 : (a) += (b); \
254 : }
255 : #define II(a, b, c, d, x, s, ac) { \
256 : (a) += I ((b), (c), (d)) + (x) + (php_hash_uint32)(ac); \
257 : (a) = ROTATE_LEFT ((a), (s)); \
258 : (a) += (b); \
259 : }
260 :
261 : /* {{{ PHP_MD5Init
262 : * MD5 initialization. Begins an MD5 operation, writing a new context.
263 : */
264 : PHP_HASH_API void PHP_MD5Init(PHP_MD5_CTX * context)
265 : {
266 : context->count[0] = context->count[1] = 0;
267 : /* Load magic initialization constants.
268 : */
269 : context->state[0] = 0x67452301;
270 : context->state[1] = 0xefcdab89;
271 : context->state[2] = 0x98badcfe;
272 : context->state[3] = 0x10325476;
273 : }
274 : /* }}} */
275 :
276 : /* {{{ PHP_MD5Update
277 : MD5 block update operation. Continues an MD5 message-digest
278 : operation, processing another message block, and updating the
279 : context.
280 : */
281 : PHP_HASH_API void PHP_MD5Update(PHP_MD5_CTX * context, const unsigned char *input,
282 : unsigned int inputLen)
283 : {
284 : unsigned int i, index, partLen;
285 :
286 : /* Compute number of bytes mod 64 */
287 : index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
288 :
289 : /* Update number of bits */
290 : if ((context->count[0] += ((php_hash_uint32) inputLen << 3))
291 : < ((php_hash_uint32) inputLen << 3))
292 : context->count[1]++;
293 : context->count[1] += ((php_hash_uint32) inputLen >> 29);
294 :
295 : partLen = 64 - index;
296 :
297 : /* Transform as many times as possible.
298 : */
299 : if (inputLen >= partLen) {
300 : memcpy
301 : ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
302 : MD5Transform(context->state, context->buffer);
303 :
304 : for (i = partLen; i + 63 < inputLen; i += 64)
305 : MD5Transform(context->state, &input[i]);
306 :
307 : index = 0;
308 : } else
309 : i = 0;
310 :
311 : /* Buffer remaining input */
312 : memcpy
313 : ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
314 : inputLen - i);
315 : }
316 : /* }}} */
317 :
318 : /* {{{ PHP_MD5Final
319 : MD5 finalization. Ends an MD5 message-digest operation, writing the
320 : the message digest and zeroizing the context.
321 : */
322 : PHP_HASH_API void PHP_MD5Final(unsigned char digest[16], PHP_MD5_CTX * context)
323 : {
324 : unsigned char bits[8];
325 : unsigned int index, padLen;
326 :
327 : /* Save number of bits */
328 : Encode(bits, context->count, 8);
329 :
330 : /* Pad out to 56 mod 64.
331 : */
332 : index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
333 : padLen = (index < 56) ? (56 - index) : (120 - index);
334 : PHP_MD5Update(context, PADDING, padLen);
335 :
336 : /* Append length (before padding) */
337 : PHP_MD5Update(context, bits, 8);
338 :
339 : /* Store state in digest */
340 : Encode(digest, context->state, 16);
341 :
342 : /* Zeroize sensitive information.
343 : */
344 : memset((unsigned char*) context, 0, sizeof(*context));
345 : }
346 : /* }}} */
347 :
348 : /* {{{ MD5Transform
349 : * MD5 basic transformation. Transforms state based on block.
350 : */
351 : static void MD5Transform(state, block)
352 : php_hash_uint32 state[4];
353 : const unsigned char block[64];
354 : {
355 : php_hash_uint32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
356 :
357 : Decode(x, block, 64);
358 :
359 : /* Round 1 */
360 : FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
361 : FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
362 : FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
363 : FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
364 : FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
365 : FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
366 : FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
367 : FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
368 : FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
369 : FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
370 : FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
371 : FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
372 : FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
373 : FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
374 : FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
375 : FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
376 :
377 : /* Round 2 */
378 : GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
379 : GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
380 : GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
381 : GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
382 : GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
383 : GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
384 : GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
385 : GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
386 : GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
387 : GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
388 : GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
389 : GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
390 : GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
391 : GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
392 : GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
393 : GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
394 :
395 : /* Round 3 */
396 : HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
397 : HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
398 : HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
399 : HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
400 : HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
401 : HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
402 : HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
403 : HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
404 : HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
405 : HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
406 : HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
407 : HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
408 : HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
409 : HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
410 : HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
411 : HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
412 :
413 : /* Round 4 */
414 : II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
415 : II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
416 : II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
417 : II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
418 : II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
419 : II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
420 : II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
421 : II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
422 : II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
423 : II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
424 : II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
425 : II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
426 : II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
427 : II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
428 : II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
429 : II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
430 :
431 : state[0] += a;
432 : state[1] += b;
433 : state[2] += c;
434 : state[3] += d;
435 :
436 : /* Zeroize sensitive information. */
437 : memset((unsigned char*) x, 0, sizeof(x));
438 : }
439 : /* }}} */
440 :
441 : #endif /* PHP_HASH_MD5_NOT_IN_CORE */
442 :
443 : /* MD4 */
444 :
445 : #define MD4_F(x,y,z) (((x) & (y)) | ((~(x)) & (z)))
446 : #define MD4_G(x,y,z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
447 : #define MD4_H(x,y,z) ((x) ^ (y) ^ (z))
448 :
449 : #define ROTL32(s,v) (((v) << (s)) | ((v) >> (32 - (s))))
450 :
451 : #define MD4_R1(a,b,c,d,k,s) a = ROTL32(s, a + MD4_F(b,c,d) + x[k])
452 : #define MD4_R2(a,b,c,d,k,s) a = ROTL32(s, a + MD4_G(b,c,d) + x[k] + 0x5A827999)
453 : #define MD4_R3(a,b,c,d,k,s) a = ROTL32(s, a + MD4_H(b,c,d) + x[k] + 0x6ED9EBA1)
454 :
455 : static void MD4Transform(php_hash_uint32 state[4], const unsigned char block[64])
456 0 : {
457 0 : php_hash_uint32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
458 :
459 0 : Decode(x, block, 64);
460 :
461 : /* Round 1 */
462 0 : MD4_R1(a,b,c,d, 0, 3);
463 0 : MD4_R1(d,a,b,c, 1, 7);
464 0 : MD4_R1(c,d,a,b, 2,11);
465 0 : MD4_R1(b,c,d,a, 3,19);
466 0 : MD4_R1(a,b,c,d, 4, 3);
467 0 : MD4_R1(d,a,b,c, 5, 7);
468 0 : MD4_R1(c,d,a,b, 6,11);
469 0 : MD4_R1(b,c,d,a, 7,19);
470 0 : MD4_R1(a,b,c,d, 8, 3);
471 0 : MD4_R1(d,a,b,c, 9, 7);
472 0 : MD4_R1(c,d,a,b,10,11);
473 0 : MD4_R1(b,c,d,a,11,19);
474 0 : MD4_R1(a,b,c,d,12, 3);
475 0 : MD4_R1(d,a,b,c,13, 7);
476 0 : MD4_R1(c,d,a,b,14,11);
477 0 : MD4_R1(b,c,d,a,15,19);
478 :
479 : /* Round 2 */
480 0 : MD4_R2(a,b,c,d, 0, 3);
481 0 : MD4_R2(d,a,b,c, 4, 5);
482 0 : MD4_R2(c,d,a,b, 8, 9);
483 0 : MD4_R2(b,c,d,a,12,13);
484 0 : MD4_R2(a,b,c,d, 1, 3);
485 0 : MD4_R2(d,a,b,c, 5, 5);
486 0 : MD4_R2(c,d,a,b, 9, 9);
487 0 : MD4_R2(b,c,d,a,13,13);
488 0 : MD4_R2(a,b,c,d, 2, 3);
489 0 : MD4_R2(d,a,b,c, 6, 5);
490 0 : MD4_R2(c,d,a,b,10, 9);
491 0 : MD4_R2(b,c,d,a,14,13);
492 0 : MD4_R2(a,b,c,d, 3, 3);
493 0 : MD4_R2(d,a,b,c, 7, 5);
494 0 : MD4_R2(c,d,a,b,11, 9);
495 0 : MD4_R2(b,c,d,a,15,13);
496 :
497 : /* Round 3 */
498 0 : MD4_R3(a,b,c,d, 0, 3);
499 0 : MD4_R3(d,a,b,c, 8, 9);
500 0 : MD4_R3(c,d,a,b, 4,11);
501 0 : MD4_R3(b,c,d,a,12,15);
502 0 : MD4_R3(a,b,c,d, 2, 3);
503 0 : MD4_R3(d,a,b,c,10, 9);
504 0 : MD4_R3(c,d,a,b, 6,11);
505 0 : MD4_R3(b,c,d,a,14,15);
506 0 : MD4_R3(a,b,c,d, 1, 3);
507 0 : MD4_R3(d,a,b,c, 9, 9);
508 0 : MD4_R3(c,d,a,b, 5,11);
509 0 : MD4_R3(b,c,d,a,13,15);
510 0 : MD4_R3(a,b,c,d, 3, 3);
511 0 : MD4_R3(d,a,b,c,11, 9);
512 0 : MD4_R3(c,d,a,b, 7,11);
513 0 : MD4_R3(b,c,d,a,15,15);
514 :
515 0 : state[0] += a;
516 0 : state[1] += b;
517 0 : state[2] += c;
518 0 : state[3] += d;
519 0 : }
520 :
521 : /* {{{ PHP_MD4Update
522 : MD4 block update operation. Continues an MD5 message-digest
523 : operation, processing another message block, and updating the
524 : context.
525 : */
526 : PHP_HASH_API void PHP_MD4Update(PHP_MD4_CTX * context, const unsigned char *input, unsigned int inputLen)
527 0 : {
528 : unsigned int i, index, partLen;
529 :
530 : /* Compute number of bytes mod 64 */
531 0 : index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
532 :
533 : /* Update number of bits */
534 0 : if ((context->count[0] += ((php_hash_uint32) inputLen << 3))
535 : < ((php_hash_uint32) inputLen << 3))
536 0 : context->count[1]++;
537 0 : context->count[1] += ((php_hash_uint32) inputLen >> 29);
538 :
539 0 : partLen = 64 - index;
540 :
541 : /* Transform as many times as possible.
542 : */
543 0 : if (inputLen >= partLen) {
544 0 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
545 0 : MD4Transform(context->state, context->buffer);
546 :
547 0 : for (i = partLen; i + 63 < inputLen; i += 64) {
548 0 : MD4Transform(context->state, &input[i]);
549 : }
550 :
551 0 : index = 0;
552 : } else {
553 0 : i = 0;
554 : }
555 :
556 : /* Buffer remaining input */
557 0 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
558 0 : }
559 : /* }}} */
560 :
561 : /* {{{ PHP_MD4Final
562 : MD4 finalization. Ends an MD4 message-digest operation, writing the
563 : the message digest and zeroizing the context.
564 : */
565 : PHP_HASH_API void PHP_MD4Final(unsigned char digest[16], PHP_MD4_CTX * context)
566 0 : {
567 : unsigned char bits[8];
568 : unsigned int index, padLen;
569 :
570 : /* Save number of bits */
571 0 : Encode(bits, context->count, 8);
572 :
573 : /* Pad out to 56 mod 64.
574 : */
575 0 : index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
576 0 : padLen = (index < 56) ? (56 - index) : (120 - index);
577 0 : PHP_MD4Update(context, PADDING, padLen);
578 :
579 : /* Append length (before padding) */
580 0 : PHP_MD4Update(context, bits, 8);
581 :
582 : /* Store state in digest */
583 0 : Encode(digest, context->state, 16);
584 :
585 : /* Zeroize sensitive information.
586 : */
587 0 : memset((unsigned char*) context, 0, sizeof(*context));
588 0 : }
589 : /* }}} */
590 :
591 : /* MD2 */
592 :
593 : static const unsigned char MD2_S[256] = {
594 : 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, 19,
595 : 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 76, 130, 202,
596 : 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 138, 23, 229, 18,
597 : 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 245, 142, 187, 47, 238, 122,
598 : 169, 104, 121, 145, 21, 178, 7, 63, 148, 194, 16, 137, 11, 34, 95, 33,
599 : 128, 127, 93, 154, 90, 144, 50, 39, 53, 62, 204, 231, 191, 247, 151, 3,
600 : 255, 25, 48, 179, 72, 165, 181, 209, 215, 94, 146, 42, 172, 86, 170, 198,
601 : 79, 184, 56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241,
602 : 69, 157, 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2,
603 : 27, 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
604 : 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, 234, 38,
605 : 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, 129, 77, 82,
606 : 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74,
607 : 120, 136, 149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57,
608 : 242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117, 75, 10,
609 : 49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 };
610 :
611 : PHP_HASH_API void PHP_MD2Init(PHP_MD2_CTX *context)
612 0 : {
613 0 : memset(context, 0, sizeof(PHP_MD2_CTX));
614 0 : }
615 :
616 : static void MD2_Transform(PHP_MD2_CTX *context, const unsigned char *block)
617 0 : {
618 0 : unsigned char i,j,t = 0;
619 :
620 0 : for(i = 0; i < 16; i++) {
621 0 : context->state[16+i] = block[i];
622 0 : context->state[32+i] = (context->state[16+i] ^ context->state[i]);
623 : }
624 :
625 0 : for(i = 0; i < 18; i++) {
626 0 : for(j = 0; j < 48; j++) {
627 0 : t = context->state[j] = context->state[j] ^ MD2_S[t];
628 : }
629 0 : t += i;
630 : }
631 :
632 : /* Update checksum -- must be after transform to avoid fouling up last message block */
633 0 : t = context->checksum[15];
634 0 : for(i = 0; i < 16; i++) {
635 0 : t = context->checksum[i] ^= MD2_S[block[i] ^ t];
636 : }
637 0 : }
638 :
639 : PHP_HASH_API void PHP_MD2Update(PHP_MD2_CTX *context, const unsigned char *buf, unsigned int len)
640 0 : {
641 0 : const unsigned char *p = buf, *e = buf + len;
642 :
643 0 : if (context->in_buffer) {
644 0 : if (context->in_buffer + len < 16) {
645 : /* Not enough for block, just pass into buffer */
646 0 : memcpy(context->buffer + context->in_buffer, p, len);
647 0 : context->in_buffer += len;
648 0 : return;
649 : }
650 : /* Put buffered data together with inbound for a single block */
651 0 : memcpy(context->buffer + context->in_buffer, p, 16 - context->in_buffer);
652 0 : MD2_Transform(context, context->buffer);
653 0 : p += 16 - context->in_buffer;
654 0 : context->in_buffer = 0;
655 : }
656 :
657 : /* Process as many whole blocks as remain */
658 0 : while ((p + 16) <= e) {
659 0 : MD2_Transform(context, p);
660 0 : p += 16;
661 : }
662 :
663 : /* Copy remaining data to buffer */
664 0 : if (p < e) {
665 0 : memcpy(context->buffer, p, e - p);
666 0 : context->in_buffer = e - p;
667 : }
668 : }
669 :
670 : PHP_HASH_API void PHP_MD2Final(unsigned char output[16], PHP_MD2_CTX *context)
671 0 : {
672 0 : memset(context->buffer + context->in_buffer, 16 - context->in_buffer, 16 - context->in_buffer);
673 0 : MD2_Transform(context, context->buffer);
674 0 : MD2_Transform(context, context->checksum);
675 :
676 0 : memcpy(output, context->state, 16);
677 0 : }
678 :
679 :
680 : /*
681 : * Local variables:
682 : * tab-width: 4
683 : * c-basic-offset: 4
684 : * End:
685 : * vim600: sw=4 ts=4 fdm=marker
686 : * vim<600: sw=4 ts=4
687 : */
|