1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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 276986 2009-03-10 23:40:06Z helly $ */
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 : (php_hash_copy_func_t) php_hash_copy,
29 : 16,
30 : 64,
31 : sizeof(PHP_MD5_CTX)
32 : };
33 :
34 : const php_hash_ops php_hash_md4_ops = {
35 : (php_hash_init_func_t) PHP_MD4Init,
36 : (php_hash_update_func_t) PHP_MD4Update,
37 : (php_hash_final_func_t) PHP_MD4Final,
38 : (php_hash_copy_func_t) php_hash_copy,
39 : 16,
40 : 64,
41 : sizeof(PHP_MD4_CTX)
42 : };
43 :
44 : const php_hash_ops php_hash_md2_ops = {
45 : (php_hash_init_func_t) PHP_MD2Init,
46 : (php_hash_update_func_t) PHP_MD2Update,
47 : (php_hash_final_func_t) PHP_MD2Final,
48 : (php_hash_copy_func_t) php_hash_copy,
49 : 16,
50 : 16,
51 : sizeof(PHP_MD2_CTX)
52 : };
53 :
54 : /* MD common stuff */
55 :
56 : static const unsigned char PADDING[64] =
57 : {
58 : 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
61 : };
62 :
63 : /* {{{ Encode
64 : Encodes input (php_hash_uint32) into output (unsigned char). Assumes len is
65 : a multiple of 4.
66 : */
67 : static void Encode(unsigned char *output, php_hash_uint32 *input, unsigned int len)
68 32 : {
69 : unsigned int i, j;
70 :
71 128 : for (i = 0, j = 0; j < len; i++, j += 4) {
72 96 : output[j] = (unsigned char) (input[i] & 0xff);
73 96 : output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
74 96 : output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
75 96 : output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
76 : }
77 32 : }
78 : /* }}} */
79 :
80 : /* {{{ Decode
81 : Decodes input (unsigned char) into output (php_hash_uint32). Assumes len is
82 : a multiple of 4.
83 : */
84 : static void Decode(php_hash_uint32 *output, const unsigned char *input, unsigned int len)
85 26 : {
86 : unsigned int i, j;
87 :
88 442 : for (i = 0, j = 0; j < len; i++, j += 4)
89 416 : output[i] = ((php_hash_uint32) input[j]) | (((php_hash_uint32) input[j + 1]) << 8) |
90 : (((php_hash_uint32) input[j + 2]) << 16) | (((php_hash_uint32) input[j + 3]) << 24);
91 26 : }
92 : /* }}} */
93 :
94 : /* MD4 */
95 :
96 : #define MD4_F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
97 : #define MD4_G(x,y,z) (((x) & ((y) | (z))) | ((y) & (z)))
98 : #define MD4_H(x,y,z) ((x) ^ (y) ^ (z))
99 :
100 : #define ROTL32(s,v) (((v) << (s)) | ((v) >> (32 - (s))))
101 :
102 : #define MD4_R1(a,b,c,d,k,s) a = ROTL32(s, a + MD4_F(b,c,d) + x[k])
103 : #define MD4_R2(a,b,c,d,k,s) a = ROTL32(s, a + MD4_G(b,c,d) + x[k] + 0x5A827999)
104 : #define MD4_R3(a,b,c,d,k,s) a = ROTL32(s, a + MD4_H(b,c,d) + x[k] + 0x6ED9EBA1)
105 :
106 : static void MD4Transform(php_hash_uint32 state[4], const unsigned char block[64])
107 26 : {
108 26 : php_hash_uint32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
109 :
110 26 : Decode(x, block, 64);
111 :
112 : /* Round 1 */
113 26 : MD4_R1(a,b,c,d, 0, 3);
114 26 : MD4_R1(d,a,b,c, 1, 7);
115 26 : MD4_R1(c,d,a,b, 2,11);
116 26 : MD4_R1(b,c,d,a, 3,19);
117 26 : MD4_R1(a,b,c,d, 4, 3);
118 26 : MD4_R1(d,a,b,c, 5, 7);
119 26 : MD4_R1(c,d,a,b, 6,11);
120 26 : MD4_R1(b,c,d,a, 7,19);
121 26 : MD4_R1(a,b,c,d, 8, 3);
122 26 : MD4_R1(d,a,b,c, 9, 7);
123 26 : MD4_R1(c,d,a,b,10,11);
124 26 : MD4_R1(b,c,d,a,11,19);
125 26 : MD4_R1(a,b,c,d,12, 3);
126 26 : MD4_R1(d,a,b,c,13, 7);
127 26 : MD4_R1(c,d,a,b,14,11);
128 26 : MD4_R1(b,c,d,a,15,19);
129 :
130 : /* Round 2 */
131 26 : MD4_R2(a,b,c,d, 0, 3);
132 26 : MD4_R2(d,a,b,c, 4, 5);
133 26 : MD4_R2(c,d,a,b, 8, 9);
134 26 : MD4_R2(b,c,d,a,12,13);
135 26 : MD4_R2(a,b,c,d, 1, 3);
136 26 : MD4_R2(d,a,b,c, 5, 5);
137 26 : MD4_R2(c,d,a,b, 9, 9);
138 26 : MD4_R2(b,c,d,a,13,13);
139 26 : MD4_R2(a,b,c,d, 2, 3);
140 26 : MD4_R2(d,a,b,c, 6, 5);
141 26 : MD4_R2(c,d,a,b,10, 9);
142 26 : MD4_R2(b,c,d,a,14,13);
143 26 : MD4_R2(a,b,c,d, 3, 3);
144 26 : MD4_R2(d,a,b,c, 7, 5);
145 26 : MD4_R2(c,d,a,b,11, 9);
146 26 : MD4_R2(b,c,d,a,15,13);
147 :
148 : /* Round 3 */
149 26 : MD4_R3(a,b,c,d, 0, 3);
150 26 : MD4_R3(d,a,b,c, 8, 9);
151 26 : MD4_R3(c,d,a,b, 4,11);
152 26 : MD4_R3(b,c,d,a,12,15);
153 26 : MD4_R3(a,b,c,d, 2, 3);
154 26 : MD4_R3(d,a,b,c,10, 9);
155 26 : MD4_R3(c,d,a,b, 6,11);
156 26 : MD4_R3(b,c,d,a,14,15);
157 26 : MD4_R3(a,b,c,d, 1, 3);
158 26 : MD4_R3(d,a,b,c, 9, 9);
159 26 : MD4_R3(c,d,a,b, 5,11);
160 26 : MD4_R3(b,c,d,a,13,15);
161 26 : MD4_R3(a,b,c,d, 3, 3);
162 26 : MD4_R3(d,a,b,c,11, 9);
163 26 : MD4_R3(c,d,a,b, 7,11);
164 26 : MD4_R3(b,c,d,a,15,15);
165 :
166 26 : state[0] += a;
167 26 : state[1] += b;
168 26 : state[2] += c;
169 26 : state[3] += d;
170 26 : }
171 :
172 : /* {{{ PHP_MD4Init
173 : * MD4 initialization. Begins an MD4 operation, writing a new context.
174 : */
175 : PHP_HASH_API void PHP_MD4Init(PHP_MD4_CTX * context)
176 16 : {
177 16 : context->count[0] = context->count[1] = 0;
178 : /* Load magic initialization constants.
179 : */
180 16 : context->state[0] = 0x67452301;
181 16 : context->state[1] = 0xefcdab89;
182 16 : context->state[2] = 0x98badcfe;
183 16 : context->state[3] = 0x10325476;
184 16 : }
185 : /* }}} */
186 :
187 : /* {{{ PHP_MD4Update
188 : MD4 block update operation. Continues an MD4 message-digest
189 : operation, processing another message block, and updating the
190 : context.
191 : */
192 : PHP_HASH_API void PHP_MD4Update(PHP_MD4_CTX * context, const unsigned char *input, unsigned int inputLen)
193 51 : {
194 : unsigned int i, index, partLen;
195 :
196 : /* Compute number of bytes mod 64 */
197 51 : index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
198 :
199 : /* Update number of bits */
200 51 : if ((context->count[0] += ((php_hash_uint32) inputLen << 3))
201 : < ((php_hash_uint32) inputLen << 3))
202 0 : context->count[1]++;
203 51 : context->count[1] += ((php_hash_uint32) inputLen >> 29);
204 :
205 51 : partLen = 64 - index;
206 :
207 : /* Transform as many times as possible.
208 : */
209 51 : if (inputLen >= partLen) {
210 26 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
211 26 : MD4Transform(context->state, context->buffer);
212 :
213 26 : for (i = partLen; i + 63 < inputLen; i += 64) {
214 0 : MD4Transform(context->state, &input[i]);
215 : }
216 :
217 26 : index = 0;
218 : } else {
219 25 : i = 0;
220 : }
221 :
222 : /* Buffer remaining input */
223 51 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
224 51 : }
225 : /* }}} */
226 :
227 : /* {{{ PHP_MD4Final
228 : MD4 finalization. Ends an MD4 message-digest operation, writing the
229 : the message digest and zeroizing the context.
230 : */
231 : PHP_HASH_API void PHP_MD4Final(unsigned char digest[16], PHP_MD4_CTX * context)
232 16 : {
233 : unsigned char bits[8];
234 : unsigned int index, padLen;
235 :
236 : /* Save number of bits */
237 16 : Encode(bits, context->count, 8);
238 :
239 : /* Pad out to 56 mod 64.
240 : */
241 16 : index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
242 16 : padLen = (index < 56) ? (56 - index) : (120 - index);
243 16 : PHP_MD4Update(context, PADDING, padLen);
244 :
245 : /* Append length (before padding) */
246 16 : PHP_MD4Update(context, bits, 8);
247 :
248 : /* Store state in digest */
249 16 : Encode(digest, context->state, 16);
250 :
251 : /* Zeroize sensitive information.
252 : */
253 16 : memset((unsigned char*) context, 0, sizeof(*context));
254 16 : }
255 : /* }}} */
256 :
257 : /* MD2 */
258 :
259 : static const unsigned char MD2_S[256] = {
260 : 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, 19,
261 : 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 76, 130, 202,
262 : 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 138, 23, 229, 18,
263 : 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 245, 142, 187, 47, 238, 122,
264 : 169, 104, 121, 145, 21, 178, 7, 63, 148, 194, 16, 137, 11, 34, 95, 33,
265 : 128, 127, 93, 154, 90, 144, 50, 39, 53, 62, 204, 231, 191, 247, 151, 3,
266 : 255, 25, 48, 179, 72, 165, 181, 209, 215, 94, 146, 42, 172, 86, 170, 198,
267 : 79, 184, 56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241,
268 : 69, 157, 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2,
269 : 27, 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
270 : 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, 234, 38,
271 : 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, 129, 77, 82,
272 : 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74,
273 : 120, 136, 149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57,
274 : 242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117, 75, 10,
275 : 49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 };
276 :
277 : PHP_HASH_API void PHP_MD2Init(PHP_MD2_CTX *context)
278 16 : {
279 16 : memset(context, 0, sizeof(PHP_MD2_CTX));
280 16 : }
281 :
282 : static void MD2_Transform(PHP_MD2_CTX *context, const unsigned char *block)
283 67 : {
284 67 : unsigned char i,j,t = 0;
285 :
286 1139 : for(i = 0; i < 16; i++) {
287 1072 : context->state[16+i] = block[i];
288 1072 : context->state[32+i] = (context->state[16+i] ^ context->state[i]);
289 : }
290 :
291 1273 : for(i = 0; i < 18; i++) {
292 59094 : for(j = 0; j < 48; j++) {
293 57888 : t = context->state[j] = context->state[j] ^ MD2_S[t];
294 : }
295 1206 : t += i;
296 : }
297 :
298 : /* Update checksum -- must be after transform to avoid fouling up last message block */
299 67 : t = context->checksum[15];
300 1139 : for(i = 0; i < 16; i++) {
301 1072 : t = context->checksum[i] ^= MD2_S[block[i] ^ t];
302 : }
303 67 : }
304 :
305 : PHP_HASH_API void PHP_MD2Update(PHP_MD2_CTX *context, const unsigned char *buf, unsigned int len)
306 19 : {
307 19 : const unsigned char *p = buf, *e = buf + len;
308 :
309 19 : if (context->in_buffer) {
310 1 : if (context->in_buffer + len < 16) {
311 : /* Not enough for block, just pass into buffer */
312 0 : memcpy(context->buffer + context->in_buffer, p, len);
313 0 : context->in_buffer += len;
314 0 : return;
315 : }
316 : /* Put buffered data together with inbound for a single block */
317 1 : memcpy(context->buffer + context->in_buffer, p, 16 - context->in_buffer);
318 1 : MD2_Transform(context, context->buffer);
319 1 : p += 16 - context->in_buffer;
320 1 : context->in_buffer = 0;
321 : }
322 :
323 : /* Process as many whole blocks as remain */
324 72 : while ((p + 16) <= e) {
325 34 : MD2_Transform(context, p);
326 34 : p += 16;
327 : }
328 :
329 : /* Copy remaining data to buffer */
330 19 : if (p < e) {
331 10 : memcpy(context->buffer, p, e - p);
332 10 : context->in_buffer = e - p;
333 : }
334 : }
335 :
336 : PHP_HASH_API void PHP_MD2Final(unsigned char output[16], PHP_MD2_CTX *context)
337 16 : {
338 16 : memset(context->buffer + context->in_buffer, 16 - context->in_buffer, 16 - context->in_buffer);
339 16 : MD2_Transform(context, context->buffer);
340 16 : MD2_Transform(context, context->checksum);
341 :
342 16 : memcpy(output, context->state, 16);
343 16 : }
344 :
345 : /*
346 : * Local variables:
347 : * tab-width: 4
348 : * c-basic-offset: 4
349 : * End:
350 : * vim600: sw=4 ts=4 fdm=marker
351 : * vim<600: sw=4 ts=4
352 : */
|