xref: /PHP-7.1/ext/hash/hash.c (revision 03f3b847)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 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   | Author: Sara Golemon <pollita@php.net>                               |
16   |         Scott MacVicar <scottmac@php.net>                            |
17   +----------------------------------------------------------------------+
18 */
19 
20 /* $Id$ */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <math.h>
27 #include "php_hash.h"
28 #include "ext/standard/info.h"
29 #include "ext/standard/file.h"
30 
31 static int php_hash_le_hash;
32 HashTable php_hash_hashtable;
33 
34 #ifdef PHP_MHASH_BC
35 struct mhash_bc_entry {
36 	char *mhash_name;
37 	char *hash_name;
38 	int value;
39 };
40 
41 #define MHASH_NUM_ALGOS 34
42 
43 static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
44 	{"CRC32", "crc32", 0},
45 	{"MD5", "md5", 1},
46 	{"SHA1", "sha1", 2},
47 	{"HAVAL256", "haval256,3", 3},
48 	{NULL, NULL, 4},
49 	{"RIPEMD160", "ripemd160", 5},
50 	{NULL, NULL, 6},
51 	{"TIGER", "tiger192,3", 7},
52 	{"GOST", "gost", 8},
53 	{"CRC32B", "crc32b", 9},
54 	{"HAVAL224", "haval224,3", 10},
55 	{"HAVAL192", "haval192,3", 11},
56 	{"HAVAL160", "haval160,3", 12},
57 	{"HAVAL128", "haval128,3", 13},
58 	{"TIGER128", "tiger128,3", 14},
59 	{"TIGER160", "tiger160,3", 15},
60 	{"MD4", "md4", 16},
61 	{"SHA256", "sha256", 17},
62 	{"ADLER32", "adler32", 18},
63 	{"SHA224", "sha224", 19},
64 	{"SHA512", "sha512", 20},
65 	{"SHA384", "sha384", 21},
66 	{"WHIRLPOOL", "whirlpool", 22},
67 	{"RIPEMD128", "ripemd128", 23},
68 	{"RIPEMD256", "ripemd256", 24},
69 	{"RIPEMD320", "ripemd320", 25},
70 	{NULL, NULL, 26}, /* support needs to be added for snefru 128 */
71 	{"SNEFRU256", "snefru256", 27},
72 	{"MD2", "md2", 28},
73 	{"FNV132", "fnv132", 29},
74 	{"FNV1A32", "fnv1a32", 30},
75 	{"FNV164", "fnv164", 31},
76 	{"FNV1A64", "fnv1a64", 32},
77 	{"JOAAT", "joaat", 33},
78 };
79 #endif
80 
81 /* Hash Registry Access */
82 
php_hash_fetch_ops(const char * algo,size_t algo_len)83 PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, size_t algo_len) /* {{{ */
84 {
85 	char *lower = zend_str_tolower_dup(algo, algo_len);
86 	php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, lower, algo_len);
87 	efree(lower);
88 
89 	return ops;
90 }
91 /* }}} */
92 
php_hash_register_algo(const char * algo,const php_hash_ops * ops)93 PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops) /* {{{ */
94 {
95 	size_t algo_len = strlen(algo);
96 	char *lower = zend_str_tolower_dup(algo, algo_len);
97 	zend_hash_str_add_ptr(&php_hash_hashtable, lower, algo_len, (void *) ops);
98 	efree(lower);
99 }
100 /* }}} */
101 
php_hash_copy(const void * ops,void * orig_context,void * dest_context)102 PHP_HASH_API int php_hash_copy(const void *ops, void *orig_context, void *dest_context) /* {{{ */
103 {
104 	php_hash_ops *hash_ops = (php_hash_ops *)ops;
105 
106 	memcpy(dest_context, orig_context, hash_ops->context_size);
107 	return SUCCESS;
108 }
109 /* }}} */
110 
111 /* Userspace */
112 
php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS,int isfilename,zend_bool raw_output_default)113 static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
114 {
115 	zend_string *digest;
116 	char *algo, *data;
117 	size_t algo_len, data_len;
118 	zend_bool raw_output = raw_output_default;
119 	const php_hash_ops *ops;
120 	void *context;
121 	php_stream *stream = NULL;
122 
123 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|b", &algo, &algo_len, &data, &data_len, &raw_output) == FAILURE) {
124 		return;
125 	}
126 
127 	ops = php_hash_fetch_ops(algo, algo_len);
128 	if (!ops) {
129 		php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
130 		RETURN_FALSE;
131 	}
132 	if (isfilename) {
133 		if (CHECK_NULL_PATH(data, data_len)) {
134 			php_error_docref(NULL, E_WARNING, "Invalid path");
135 			RETURN_FALSE;
136 		}
137 		stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
138 		if (!stream) {
139 			/* Stream will report errors opening file */
140 			RETURN_FALSE;
141 		}
142 	}
143 
144 	context = emalloc(ops->context_size);
145 	ops->hash_init(context);
146 
147 	if (isfilename) {
148 		char buf[1024];
149 		size_t n;
150 
151 		while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
152 			ops->hash_update(context, (unsigned char *) buf, n);
153 		}
154 		php_stream_close(stream);
155 	} else {
156 		ops->hash_update(context, (unsigned char *) data, data_len);
157 	}
158 
159 	digest = zend_string_alloc(ops->digest_size, 0);
160 	ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
161 	efree(context);
162 
163 	if (raw_output) {
164 		ZSTR_VAL(digest)[ops->digest_size] = 0;
165 		RETURN_NEW_STR(digest);
166 	} else {
167 		zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
168 
169 		php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
170 		ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
171 		zend_string_release(digest);
172 		RETURN_NEW_STR(hex_digest);
173 	}
174 }
175 /* }}} */
176 
177 /* {{{ proto string hash(string algo, string data[, bool raw_output = false])
178 Generate a hash of a given input string
179 Returns lowercase hexits by default */
PHP_FUNCTION(hash)180 PHP_FUNCTION(hash)
181 {
182 	php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
183 }
184 /* }}} */
185 
186 /* {{{ proto string hash_file(string algo, string filename[, bool raw_output = false])
187 Generate a hash of a given file
188 Returns lowercase hexits by default */
PHP_FUNCTION(hash_file)189 PHP_FUNCTION(hash_file)
190 {
191 	php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
192 }
193 /* }}} */
194 
php_hash_string_xor_char(unsigned char * out,const unsigned char * in,const unsigned char xor_with,const int length)195 static inline void php_hash_string_xor_char(unsigned char *out, const unsigned char *in, const unsigned char xor_with, const int length) {
196 	int i;
197 	for (i=0; i < length; i++) {
198 		out[i] = in[i] ^ xor_with;
199 	}
200 }
201 
php_hash_string_xor(unsigned char * out,const unsigned char * in,const unsigned char * xor_with,const int length)202 static inline void php_hash_string_xor(unsigned char *out, const unsigned char *in, const unsigned char *xor_with, const int length) {
203 	int i;
204 	for (i=0; i < length; i++) {
205 		out[i] = in[i] ^ xor_with[i];
206 	}
207 }
208 
php_hash_hmac_prep_key(unsigned char * K,const php_hash_ops * ops,void * context,const unsigned char * key,const size_t key_len)209 static inline void php_hash_hmac_prep_key(unsigned char *K, const php_hash_ops *ops, void *context, const unsigned char *key, const size_t key_len) {
210 	memset(K, 0, ops->block_size);
211 	if (key_len > (size_t)ops->block_size) {
212 		/* Reduce the key first */
213 		ops->hash_init(context);
214 		ops->hash_update(context, key, key_len);
215 		ops->hash_final(K, context);
216 	} else {
217 		memcpy(K, key, key_len);
218 	}
219 	/* XOR the key with 0x36 to get the ipad) */
220 	php_hash_string_xor_char(K, K, 0x36, ops->block_size);
221 }
222 
php_hash_hmac_round(unsigned char * final,const php_hash_ops * ops,void * context,const unsigned char * key,const unsigned char * data,const zend_long data_size)223 static inline void php_hash_hmac_round(unsigned char *final, const php_hash_ops *ops, void *context, const unsigned char *key, const unsigned char *data, const zend_long data_size) {
224 	ops->hash_init(context);
225 	ops->hash_update(context, key, ops->block_size);
226 	ops->hash_update(context, data, data_size);
227 	ops->hash_final(final, context);
228 }
229 
php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS,int isfilename,zend_bool raw_output_default)230 static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
231 {
232 	zend_string *digest;
233 	char *algo, *data, *key;
234 	unsigned char *K;
235 	size_t algo_len, data_len, key_len;
236 	zend_bool raw_output = raw_output_default;
237 	const php_hash_ops *ops;
238 	void *context;
239 	php_stream *stream = NULL;
240 
241 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|b", &algo, &algo_len, &data, &data_len,
242 																  &key, &key_len, &raw_output) == FAILURE) {
243 		return;
244 	}
245 
246 	ops = php_hash_fetch_ops(algo, algo_len);
247 	if (!ops) {
248 		php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
249 		RETURN_FALSE;
250 	}
251 	if (isfilename) {
252 		if (CHECK_NULL_PATH(data, data_len)) {
253 			php_error_docref(NULL, E_WARNING, "Invalid path");
254 			RETURN_FALSE;
255 		}
256 		stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
257 		if (!stream) {
258 			/* Stream will report errors opening file */
259 			RETURN_FALSE;
260 		}
261 	}
262 
263 	context = emalloc(ops->context_size);
264 
265 	K = emalloc(ops->block_size);
266 	digest = zend_string_alloc(ops->digest_size, 0);
267 
268 	php_hash_hmac_prep_key(K, ops, context, (unsigned char *) key, key_len);
269 
270 	if (isfilename) {
271 		char buf[1024];
272 		int n;
273 		ops->hash_init(context);
274 		ops->hash_update(context, K, ops->block_size);
275 		while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
276 			ops->hash_update(context, (unsigned char *) buf, n);
277 		}
278 		php_stream_close(stream);
279 		ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
280 	} else {
281 		php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) data, data_len);
282 	}
283 
284 	php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
285 
286 	php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
287 
288 	/* Zero the key */
289 	ZEND_SECURE_ZERO(K, ops->block_size);
290 	efree(K);
291 	efree(context);
292 
293 	if (raw_output) {
294 		ZSTR_VAL(digest)[ops->digest_size] = 0;
295 		RETURN_NEW_STR(digest);
296 	} else {
297 		zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
298 
299 		php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
300 		ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
301 		zend_string_release(digest);
302 		RETURN_NEW_STR(hex_digest);
303 	}
304 }
305 /* }}} */
306 
307 /* {{{ proto string hash_hmac(string algo, string data, string key[, bool raw_output = false])
308 Generate a hash of a given input string with a key using HMAC
309 Returns lowercase hexits by default */
PHP_FUNCTION(hash_hmac)310 PHP_FUNCTION(hash_hmac)
311 {
312 	php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
313 }
314 /* }}} */
315 
316 /* {{{ proto string hash_hmac_file(string algo, string filename, string key[, bool raw_output = false])
317 Generate a hash of a given file with a key using HMAC
318 Returns lowercase hexits by default */
PHP_FUNCTION(hash_hmac_file)319 PHP_FUNCTION(hash_hmac_file)
320 {
321 	php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
322 }
323 /* }}} */
324 
325 
326 /* {{{ proto resource hash_init(string algo[, int options, string key])
327 Initialize a hashing context */
PHP_FUNCTION(hash_init)328 PHP_FUNCTION(hash_init)
329 {
330 	char *algo, *key = NULL;
331 	size_t algo_len, key_len = 0;
332 	int argc = ZEND_NUM_ARGS();
333 	zend_long options = 0;
334 	void *context;
335 	const php_hash_ops *ops;
336 	php_hash_data *hash;
337 
338 	if (zend_parse_parameters(argc, "s|ls", &algo, &algo_len, &options, &key, &key_len) == FAILURE) {
339 		return;
340 	}
341 
342 	ops = php_hash_fetch_ops(algo, algo_len);
343 	if (!ops) {
344 		php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
345 		RETURN_FALSE;
346 	}
347 
348 	if (options & PHP_HASH_HMAC &&
349 		key_len <= 0) {
350 		/* Note: a zero length key is no key at all */
351 		php_error_docref(NULL, E_WARNING, "HMAC requested without a key");
352 		RETURN_FALSE;
353 	}
354 
355 	context = emalloc(ops->context_size);
356 	ops->hash_init(context);
357 
358 	hash = emalloc(sizeof(php_hash_data));
359 	hash->ops = ops;
360 	hash->context = context;
361 	hash->options = options;
362 	hash->key = NULL;
363 
364 	if (options & PHP_HASH_HMAC) {
365 		char *K = emalloc(ops->block_size);
366 		int i;
367 
368 		memset(K, 0, ops->block_size);
369 
370 		if (key_len > (size_t)ops->block_size) {
371 			/* Reduce the key first */
372 			ops->hash_update(context, (unsigned char *) key, key_len);
373 			ops->hash_final((unsigned char *) K, context);
374 			/* Make the context ready to start over */
375 			ops->hash_init(context);
376 		} else {
377 			memcpy(K, key, key_len);
378 		}
379 
380 		/* XOR ipad */
381 		for(i=0; i < ops->block_size; i++) {
382 			K[i] ^= 0x36;
383 		}
384 		ops->hash_update(context, (unsigned char *) K, ops->block_size);
385 		hash->key = (unsigned char *) K;
386 	}
387 
388 	RETURN_RES(zend_register_resource(hash, php_hash_le_hash));
389 }
390 /* }}} */
391 
392 /* {{{ proto bool hash_update(resource context, string data)
393 Pump data into the hashing algorithm */
PHP_FUNCTION(hash_update)394 PHP_FUNCTION(hash_update)
395 {
396 	zval *zhash;
397 	php_hash_data *hash;
398 	char *data;
399 	size_t data_len;
400 
401 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &zhash, &data, &data_len) == FAILURE) {
402 		return;
403 	}
404 
405 	if ((hash = (php_hash_data *)zend_fetch_resource(Z_RES_P(zhash), PHP_HASH_RESNAME, php_hash_le_hash)) == NULL) {
406 		RETURN_FALSE;
407 	}
408 
409 	hash->ops->hash_update(hash->context, (unsigned char *) data, data_len);
410 
411 	RETURN_TRUE;
412 }
413 /* }}} */
414 
415 /* {{{ proto int hash_update_stream(resource context, resource handle[, integer length])
416 Pump data into the hashing algorithm from an open stream */
PHP_FUNCTION(hash_update_stream)417 PHP_FUNCTION(hash_update_stream)
418 {
419 	zval *zhash, *zstream;
420 	php_hash_data *hash;
421 	php_stream *stream = NULL;
422 	zend_long length = -1, didread = 0;
423 
424 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr|l", &zhash, &zstream, &length) == FAILURE) {
425 		return;
426 	}
427 
428 	if ((hash = (php_hash_data *)zend_fetch_resource(Z_RES_P(zhash), PHP_HASH_RESNAME, php_hash_le_hash)) == NULL) {
429 		RETURN_FALSE;
430 	}
431 
432 	php_stream_from_zval(stream, zstream);
433 
434 	while (length) {
435 		char buf[1024];
436 		zend_long n, toread = 1024;
437 
438 		if (length > 0 && toread > length) {
439 			toread = length;
440 		}
441 
442 		if ((n = php_stream_read(stream, buf, toread)) <= 0) {
443 			/* Nada mas */
444 			RETURN_LONG(didread);
445 		}
446 		hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
447 		length -= n;
448 		didread += n;
449 	}
450 
451 	RETURN_LONG(didread);
452 }
453 /* }}} */
454 
455 /* {{{ proto bool hash_update_file(resource context, string filename[, resource context])
456 Pump data into the hashing algorithm from a file */
PHP_FUNCTION(hash_update_file)457 PHP_FUNCTION(hash_update_file)
458 {
459 	zval *zhash, *zcontext = NULL;
460 	php_hash_data *hash;
461 	php_stream_context *context;
462 	php_stream *stream;
463 	char *filename, buf[1024];
464 	size_t filename_len, n;
465 
466 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rp|r", &zhash, &filename, &filename_len, &zcontext) == FAILURE) {
467 		return;
468 	}
469 
470 	if ((hash = (php_hash_data *)zend_fetch_resource(Z_RES_P(zhash), PHP_HASH_RESNAME, php_hash_le_hash)) == NULL) {
471 		RETURN_FALSE;
472 	}
473 	context = php_stream_context_from_zval(zcontext, 0);
474 
475 	stream = php_stream_open_wrapper_ex(filename, "rb", REPORT_ERRORS, NULL, context);
476 	if (!stream) {
477 		/* Stream will report errors opening file */
478 		RETURN_FALSE;
479 	}
480 
481 	while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
482 		hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
483 	}
484 	php_stream_close(stream);
485 
486 	RETURN_TRUE;
487 }
488 /* }}} */
489 
490 /* {{{ proto string hash_final(resource context[, bool raw_output=false])
491 Output resulting digest */
PHP_FUNCTION(hash_final)492 PHP_FUNCTION(hash_final)
493 {
494 	zval *zhash;
495 	php_hash_data *hash;
496 	zend_bool raw_output = 0;
497 	zend_string *digest;
498 	int digest_len;
499 
500 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|b", &zhash, &raw_output) == FAILURE) {
501 		return;
502 	}
503 
504 	if ((hash = (php_hash_data *)zend_fetch_resource(Z_RES_P(zhash), PHP_HASH_RESNAME, php_hash_le_hash)) == NULL) {
505 		RETURN_FALSE;
506 	}
507 
508 	digest_len = hash->ops->digest_size;
509 	digest = zend_string_alloc(digest_len, 0);
510 	hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
511 	if (hash->options & PHP_HASH_HMAC) {
512 		int i;
513 
514 		/* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
515 		for(i=0; i < hash->ops->block_size; i++) {
516 			hash->key[i] ^= 0x6A;
517 		}
518 
519 		/* Feed this result into the outter hash */
520 		hash->ops->hash_init(hash->context);
521 		hash->ops->hash_update(hash->context, hash->key, hash->ops->block_size);
522 		hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(digest), hash->ops->digest_size);
523 		hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
524 
525 		/* Zero the key */
526 		ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
527 		efree(hash->key);
528 		hash->key = NULL;
529 	}
530 	ZSTR_VAL(digest)[digest_len] = 0;
531 	efree(hash->context);
532 	hash->context = NULL;
533 	zend_list_close(Z_RES_P(zhash));
534 
535 	if (raw_output) {
536 		RETURN_NEW_STR(digest);
537 	} else {
538 		zend_string *hex_digest = zend_string_safe_alloc(digest_len, 2, 0, 0);
539 
540 		php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), digest_len);
541 		ZSTR_VAL(hex_digest)[2 * digest_len] = 0;
542 		zend_string_release(digest);
543 		RETURN_NEW_STR(hex_digest);
544 	}
545 }
546 /* }}} */
547 
548 /* {{{ proto resource hash_copy(resource context)
549 Copy hash resource */
PHP_FUNCTION(hash_copy)550 PHP_FUNCTION(hash_copy)
551 {
552 	zval *zhash;
553 	php_hash_data *hash, *copy_hash;
554 	void *context;
555 	int res;
556 
557 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zhash) == FAILURE) {
558 		return;
559 	}
560 
561 	if ((hash = (php_hash_data *)zend_fetch_resource(Z_RES_P(zhash), PHP_HASH_RESNAME, php_hash_le_hash)) == NULL) {
562 		RETURN_FALSE;
563 	}
564 
565 
566 	context = emalloc(hash->ops->context_size);
567 	hash->ops->hash_init(context);
568 
569 	res = hash->ops->hash_copy(hash->ops, hash->context, context);
570 	if (res != SUCCESS) {
571 		efree(context);
572 		RETURN_FALSE;
573 	}
574 
575 	copy_hash = emalloc(sizeof(php_hash_data));
576 	copy_hash->ops = hash->ops;
577 	copy_hash->context = context;
578 	copy_hash->options = hash->options;
579 	copy_hash->key = ecalloc(1, hash->ops->block_size);
580 	if (hash->key) {
581 		memcpy(copy_hash->key, hash->key, hash->ops->block_size);
582 	}
583 	RETURN_RES(zend_register_resource(copy_hash, php_hash_le_hash));
584 }
585 /* }}} */
586 
587 /* {{{ proto array hash_algos(void)
588 Return a list of registered hashing algorithms */
PHP_FUNCTION(hash_algos)589 PHP_FUNCTION(hash_algos)
590 {
591 	zend_string *str;
592 
593 	array_init(return_value);
594 	ZEND_HASH_FOREACH_STR_KEY(&php_hash_hashtable, str) {
595 		add_next_index_str(return_value, zend_string_copy(str));
596 	} ZEND_HASH_FOREACH_END();
597 }
598 /* }}} */
599 
php_hash_is_crypto(const char * algo,size_t algo_len)600 static inline zend_bool php_hash_is_crypto(const char *algo, size_t algo_len) {
601 
602 	char *blacklist[] = { "adler32", "crc32", "crc32b", "fnv132", "fnv1a32", "fnv164", "fnv1a64", "joaat", NULL };
603 	char *lower = zend_str_tolower_dup(algo, algo_len);
604 	int i = 0;
605 
606 	while (blacklist[i]) {
607 		if (strcmp(lower, blacklist[i]) == 0) {
608 			efree(lower);
609 			return 0;
610 		}
611 
612 		i++;
613 	}
614 
615 	efree(lower);
616 	return 1;
617 }
618 
619 /* {{{ proto string hash_hkdf(string algo, string ikm [, int length = 0, string info = '', string salt = ''])
620 RFC5869 HMAC-based key derivation function */
PHP_FUNCTION(hash_hkdf)621 PHP_FUNCTION(hash_hkdf)
622 {
623 	zend_string *returnval, *ikm, *algo, *info = NULL, *salt = NULL;
624 	zend_long length = 0;
625 	unsigned char *prk, *digest, *K;
626 	int i, rounds;
627 	const php_hash_ops *ops;
628 	void *context;
629 
630 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lSS", &algo, &ikm, &length, &info, &salt) == FAILURE) {
631 		return;
632 	}
633 
634 	ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
635 	if (!ops) {
636 		php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
637 		RETURN_FALSE;
638 	}
639 
640 	if (!php_hash_is_crypto(ZSTR_VAL(algo), ZSTR_LEN(algo))) {
641 		php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
642 		RETURN_FALSE;
643 	}
644 
645 	if (ZSTR_LEN(ikm) == 0) {
646 		php_error_docref(NULL, E_WARNING, "Input keying material cannot be empty");
647 		RETURN_FALSE;
648 	}
649 
650 	if (length < 0) {
651 		php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
652 		RETURN_FALSE;
653 	} else if (length == 0) {
654 		length = ops->digest_size;
655 	} else if (length > ops->digest_size * 255) {
656 		php_error_docref(NULL, E_WARNING, "Length must be less than or equal to %d: " ZEND_LONG_FMT, ops->digest_size * 255, length);
657 		RETURN_FALSE;
658 	}
659 
660 	context = emalloc(ops->context_size);
661 
662 	// Extract
663 	ops->hash_init(context);
664 	K = emalloc(ops->block_size);
665 	php_hash_hmac_prep_key(K, ops, context,
666 		(unsigned char *) (salt ? ZSTR_VAL(salt) : ""), salt ? ZSTR_LEN(salt) : 0);
667 
668 	prk = emalloc(ops->digest_size);
669 	php_hash_hmac_round(prk, ops, context, K, (unsigned char *) ZSTR_VAL(ikm), ZSTR_LEN(ikm));
670 	php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
671 	php_hash_hmac_round(prk, ops, context, K, prk, ops->digest_size);
672 	ZEND_SECURE_ZERO(K, ops->block_size);
673 
674 	// Expand
675 	returnval = zend_string_alloc(length, 0);
676 	digest = emalloc(ops->digest_size);
677 	for (i = 1, rounds = (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
678 		// chr(i)
679 		unsigned char c[1];
680 		c[0] = (i & 0xFF);
681 
682 		php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size);
683 		ops->hash_init(context);
684 		ops->hash_update(context, K, ops->block_size);
685 
686 		if (i > 1) {
687 			ops->hash_update(context, digest, ops->digest_size);
688 		}
689 
690 		if (info != NULL && ZSTR_LEN(info) > 0) {
691 			ops->hash_update(context, (unsigned char *) ZSTR_VAL(info), ZSTR_LEN(info));
692 		}
693 
694 		ops->hash_update(context, c, 1);
695 		ops->hash_final(digest, context);
696 		php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
697 		php_hash_hmac_round(digest, ops, context, K, digest, ops->digest_size);
698 		memcpy(
699 			ZSTR_VAL(returnval) + ((i - 1) * ops->digest_size),
700 			digest,
701 			(i == rounds ? length - ((i - 1) * ops->digest_size) : ops->digest_size)
702 		);
703 	}
704 
705 	ZEND_SECURE_ZERO(K, ops->block_size);
706 	ZEND_SECURE_ZERO(digest, ops->digest_size);
707 	ZEND_SECURE_ZERO(prk, ops->digest_size);
708 	efree(K);
709 	efree(context);
710 	efree(prk);
711 	efree(digest);
712 	ZSTR_VAL(returnval)[length] = 0;
713 	RETURN_STR(returnval);
714 }
715 
716 /* {{{ proto string hash_pbkdf2(string algo, string password, string salt, int iterations [, int length = 0, bool raw_output = false])
717 Generate a PBKDF2 hash of the given password and salt
718 Returns lowercase hexits by default */
PHP_FUNCTION(hash_pbkdf2)719 PHP_FUNCTION(hash_pbkdf2)
720 {
721 	zend_string *returnval;
722 	char *algo, *salt, *pass = NULL;
723 	unsigned char *computed_salt, *digest, *temp, *result, *K1, *K2 = NULL;
724 	zend_long loops, i, j, iterations, digest_length = 0, length = 0;
725 	size_t algo_len, pass_len, salt_len = 0;
726 	zend_bool raw_output = 0;
727 	const php_hash_ops *ops;
728 	void *context;
729 
730 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssl|lb", &algo, &algo_len, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output) == FAILURE) {
731 		return;
732 	}
733 
734 	ops = php_hash_fetch_ops(algo, algo_len);
735 	if (!ops) {
736 		php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
737 		RETURN_FALSE;
738 	}
739 
740 	if (iterations <= 0) {
741 		php_error_docref(NULL, E_WARNING, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
742 		RETURN_FALSE;
743 	}
744 
745 	if (length < 0) {
746 		php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
747 		RETURN_FALSE;
748 	}
749 
750 	if (salt_len > INT_MAX - 4) {
751 		php_error_docref(NULL, E_WARNING, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
752 		RETURN_FALSE;
753 	}
754 
755 	context = emalloc(ops->context_size);
756 	ops->hash_init(context);
757 
758 	K1 = emalloc(ops->block_size);
759 	K2 = emalloc(ops->block_size);
760 	digest = emalloc(ops->digest_size);
761 	temp = emalloc(ops->digest_size);
762 
763 	/* Setup Keys that will be used for all hmac rounds */
764 	php_hash_hmac_prep_key(K1, ops, context, (unsigned char *) pass, pass_len);
765 	/* Convert K1 to opad -- 0x6A = 0x36 ^ 0x5C */
766 	php_hash_string_xor_char(K2, K1, 0x6A, ops->block_size);
767 
768 	/* Setup Main Loop to build a long enough result */
769 	if (length == 0) {
770 		length = ops->digest_size;
771 		if (!raw_output) {
772 			length = length * 2;
773 		}
774 	}
775 	digest_length = length;
776 	if (!raw_output) {
777 		digest_length = (zend_long) ceil((float) length / 2.0);
778 	}
779 
780 	loops = (zend_long) ceil((float) digest_length / (float) ops->digest_size);
781 
782 	result = safe_emalloc(loops, ops->digest_size, 0);
783 
784 	computed_salt = safe_emalloc(salt_len, 1, 4);
785 	memcpy(computed_salt, (unsigned char *) salt, salt_len);
786 
787 	for (i = 1; i <= loops; i++) {
788 		/* digest = hash_hmac(salt + pack('N', i), password) { */
789 
790 		/* pack("N", i) */
791 		computed_salt[salt_len] = (unsigned char) (i >> 24);
792 		computed_salt[salt_len + 1] = (unsigned char) ((i & 0xFF0000) >> 16);
793 		computed_salt[salt_len + 2] = (unsigned char) ((i & 0xFF00) >> 8);
794 		computed_salt[salt_len + 3] = (unsigned char) (i & 0xFF);
795 
796 		php_hash_hmac_round(digest, ops, context, K1, computed_salt, (zend_long) salt_len + 4);
797 		php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
798 		/* } */
799 
800 		/* temp = digest */
801 		memcpy(temp, digest, ops->digest_size);
802 
803 		/*
804 		 * Note that the loop starting at 1 is intentional, since we've already done
805 		 * the first round of the algorithm.
806 		 */
807 		for (j = 1; j < iterations; j++) {
808 			/* digest = hash_hmac(digest, password) { */
809 			php_hash_hmac_round(digest, ops, context, K1, digest, ops->digest_size);
810 			php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
811 			/* } */
812 			/* temp ^= digest */
813 			php_hash_string_xor(temp, temp, digest, ops->digest_size);
814 		}
815 		/* result += temp */
816 		memcpy(result + ((i - 1) * ops->digest_size), temp, ops->digest_size);
817 	}
818 	/* Zero potentially sensitive variables */
819 	ZEND_SECURE_ZERO(K1, ops->block_size);
820 	ZEND_SECURE_ZERO(K2, ops->block_size);
821 	ZEND_SECURE_ZERO(computed_salt, salt_len + 4);
822 	efree(K1);
823 	efree(K2);
824 	efree(computed_salt);
825 	efree(context);
826 	efree(digest);
827 	efree(temp);
828 
829 	returnval = zend_string_alloc(length, 0);
830 	if (raw_output) {
831 		memcpy(ZSTR_VAL(returnval), result, length);
832 	} else {
833 		php_hash_bin2hex(ZSTR_VAL(returnval), result, digest_length);
834 	}
835 	ZSTR_VAL(returnval)[length] = 0;
836 	efree(result);
837 	RETURN_NEW_STR(returnval);
838 }
839 /* }}} */
840 
841 /* {{{ proto bool hash_equals(string known_string, string user_string)
842    Compares two strings using the same time whether they're equal or not.
843    A difference in length will leak */
PHP_FUNCTION(hash_equals)844 PHP_FUNCTION(hash_equals)
845 {
846 	zval *known_zval, *user_zval;
847 	char *known_str, *user_str;
848 	int result = 0;
849 	size_t j;
850 
851 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &known_zval, &user_zval) == FAILURE) {
852 		return;
853 	}
854 
855 	/* We only allow comparing string to prevent unexpected results. */
856 	if (Z_TYPE_P(known_zval) != IS_STRING) {
857 		php_error_docref(NULL, E_WARNING, "Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
858 		RETURN_FALSE;
859 	}
860 
861 	if (Z_TYPE_P(user_zval) != IS_STRING) {
862 		php_error_docref(NULL, E_WARNING, "Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
863 		RETURN_FALSE;
864 	}
865 
866 	if (Z_STRLEN_P(known_zval) != Z_STRLEN_P(user_zval)) {
867 		RETURN_FALSE;
868 	}
869 
870 	known_str = Z_STRVAL_P(known_zval);
871 	user_str = Z_STRVAL_P(user_zval);
872 
873 	/* This is security sensitive code. Do not optimize this for speed. */
874 	for (j = 0; j < Z_STRLEN_P(known_zval); j++) {
875 		result |= known_str[j] ^ user_str[j];
876 	}
877 
878 	RETURN_BOOL(0 == result);
879 }
880 /* }}} */
881 
882 /* Module Housekeeping */
883 
php_hash_dtor(zend_resource * rsrc)884 static void php_hash_dtor(zend_resource *rsrc) /* {{{ */
885 {
886 	php_hash_data *hash = (php_hash_data*)rsrc->ptr;
887 
888 	/* Just in case the algo has internally allocated resources */
889 	if (hash->context) {
890 		unsigned char *dummy = emalloc(hash->ops->digest_size);
891 		hash->ops->hash_final(dummy, hash->context);
892 		efree(dummy);
893 		efree(hash->context);
894 	}
895 
896 	if (hash->key) {
897 		ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
898 		efree(hash->key);
899 	}
900 	efree(hash);
901 }
902 /* }}} */
903 
904 #define PHP_HASH_HAVAL_REGISTER(p,b)	php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
905 
906 #ifdef PHP_MHASH_BC
907 
908 #if 0
909 /* See #69823, we should not insert module into module_registry while doing startup */
910 
911 PHP_MINFO_FUNCTION(mhash)
912 {
913 	php_info_print_table_start();
914 	php_info_print_table_row(2, "MHASH support", "Enabled");
915 	php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
916 	php_info_print_table_end();
917 }
918 
919 zend_module_entry mhash_module_entry = {
920 	STANDARD_MODULE_HEADER,
921 	"mhash",
922 	NULL,
923 	NULL,
924 	NULL,
925 	NULL,
926 	NULL,
927 	PHP_MINFO(mhash),
928 	PHP_MHASH_VERSION,
929 	STANDARD_MODULE_PROPERTIES,
930 };
931 #endif
932 
mhash_init(INIT_FUNC_ARGS)933 static void mhash_init(INIT_FUNC_ARGS)
934 {
935 	char buf[128];
936 	int len;
937 	int algo_number = 0;
938 
939 	for (algo_number = 0; algo_number < MHASH_NUM_ALGOS; algo_number++) {
940 		struct mhash_bc_entry algorithm = mhash_to_hash[algo_number];
941 		if (algorithm.mhash_name == NULL) {
942 			continue;
943 		}
944 
945 		len = slprintf(buf, 127, "MHASH_%s", algorithm.mhash_name, strlen(algorithm.mhash_name));
946 		zend_register_long_constant(buf, len, algorithm.value, CONST_CS | CONST_PERSISTENT, module_number);
947 	}
948 
949 	/* TODO: this cause #69823 zend_register_internal_module(&mhash_module_entry); */
950 }
951 
952 /* {{{ proto string mhash(int hash, string data [, string key])
953    Hash data with hash */
PHP_FUNCTION(mhash)954 PHP_FUNCTION(mhash)
955 {
956 	zval *z_algorithm;
957 	zend_long algorithm;
958 
959 	if (zend_parse_parameters(1, "z", &z_algorithm) == FAILURE) {
960 		return;
961 	}
962 
963 	algorithm = zval_get_long(z_algorithm);
964 
965 	/* need to convert the first parameter from int constant to string algorithm name */
966 	if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
967 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
968 		if (algorithm_lookup.hash_name) {
969 			ZVAL_STRING(z_algorithm, algorithm_lookup.hash_name);
970 		}
971 	}
972 
973 	if (ZEND_NUM_ARGS() == 3) {
974 		php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
975 	} else if (ZEND_NUM_ARGS() == 2) {
976 		php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
977 	} else {
978 		WRONG_PARAM_COUNT;
979 	}
980 }
981 /* }}} */
982 
983 /* {{{ proto string mhash_get_hash_name(int hash)
984    Gets the name of hash */
PHP_FUNCTION(mhash_get_hash_name)985 PHP_FUNCTION(mhash_get_hash_name)
986 {
987 	zend_long algorithm;
988 
989 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
990 		return;
991 	}
992 
993 	if (algorithm >= 0 && algorithm  < MHASH_NUM_ALGOS) {
994 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
995 		if (algorithm_lookup.mhash_name) {
996 			RETURN_STRING(algorithm_lookup.mhash_name);
997 		}
998 	}
999 	RETURN_FALSE;
1000 }
1001 /* }}} */
1002 
1003 /* {{{ proto int mhash_count(void)
1004    Gets the number of available hashes */
PHP_FUNCTION(mhash_count)1005 PHP_FUNCTION(mhash_count)
1006 {
1007 	if (zend_parse_parameters_none() == FAILURE) {
1008 		return;
1009 	}
1010 	RETURN_LONG(MHASH_NUM_ALGOS - 1);
1011 }
1012 /* }}} */
1013 
1014 /* {{{ proto int mhash_get_block_size(int hash)
1015    Gets the block size of hash */
PHP_FUNCTION(mhash_get_block_size)1016 PHP_FUNCTION(mhash_get_block_size)
1017 {
1018 	zend_long algorithm;
1019 
1020 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
1021 		return;
1022 	}
1023 	RETVAL_FALSE;
1024 
1025 	if (algorithm >= 0 && algorithm  < MHASH_NUM_ALGOS) {
1026 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1027 		if (algorithm_lookup.mhash_name) {
1028 			const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1029 			if (ops) {
1030 				RETVAL_LONG(ops->digest_size);
1031 			}
1032 		}
1033 	}
1034 }
1035 /* }}} */
1036 
1037 #define SALT_SIZE 8
1038 
1039 /* {{{ proto string mhash_keygen_s2k(int hash, string input_password, string salt, int bytes)
1040    Generates a key using hash functions */
PHP_FUNCTION(mhash_keygen_s2k)1041 PHP_FUNCTION(mhash_keygen_s2k)
1042 {
1043 	zend_long algorithm, l_bytes;
1044 	int bytes;
1045 	char *password, *salt;
1046 	size_t password_len, salt_len;
1047 	char padded_salt[SALT_SIZE];
1048 
1049 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes) == FAILURE) {
1050 		return;
1051 	}
1052 
1053 	bytes = (int)l_bytes;
1054 	if (bytes <= 0){
1055 		php_error_docref(NULL, E_WARNING, "the byte parameter must be greater than 0");
1056 		RETURN_FALSE;
1057 	}
1058 
1059 	salt_len = MIN(salt_len, SALT_SIZE);
1060 
1061 	memcpy(padded_salt, salt, salt_len);
1062 	if (salt_len < SALT_SIZE) {
1063 		memset(padded_salt + salt_len, 0, SALT_SIZE - salt_len);
1064 	}
1065 	salt_len = SALT_SIZE;
1066 
1067 	RETVAL_FALSE;
1068 	if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1069 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1070 		if (algorithm_lookup.mhash_name) {
1071 			const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1072 			if (ops) {
1073 				unsigned char null = '\0';
1074 				void *context;
1075 				char *key, *digest;
1076 				int i = 0, j = 0;
1077 				int block_size = ops->digest_size;
1078 				int times = bytes / block_size;
1079 				if (bytes % block_size  != 0) times++;
1080 
1081 				context = emalloc(ops->context_size);
1082 				ops->hash_init(context);
1083 
1084 				key = ecalloc(1, times * block_size);
1085 				digest = emalloc(ops->digest_size + 1);
1086 
1087 				for (i = 0; i < times; i++) {
1088 					ops->hash_init(context);
1089 
1090 					for (j=0;j<i;j++) {
1091 						ops->hash_update(context, &null, 1);
1092 					}
1093 					ops->hash_update(context, (unsigned char *)padded_salt, salt_len);
1094 					ops->hash_update(context, (unsigned char *)password, password_len);
1095 					ops->hash_final((unsigned char *)digest, context);
1096 					memcpy( &key[i*block_size], digest, block_size);
1097 				}
1098 
1099 				RETVAL_STRINGL(key, bytes);
1100 				ZEND_SECURE_ZERO(key, bytes);
1101 				efree(digest);
1102 				efree(context);
1103 				efree(key);
1104 			}
1105 		}
1106 	}
1107 }
1108 /* }}} */
1109 
1110 #endif
1111 
1112 /* {{{ PHP_MINIT_FUNCTION
1113  */
PHP_MINIT_FUNCTION(hash)1114 PHP_MINIT_FUNCTION(hash)
1115 {
1116 	php_hash_le_hash = zend_register_list_destructors_ex(php_hash_dtor, NULL, PHP_HASH_RESNAME, module_number);
1117 
1118 	zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
1119 
1120 	php_hash_register_algo("md2",			&php_hash_md2_ops);
1121 	php_hash_register_algo("md4",			&php_hash_md4_ops);
1122 	php_hash_register_algo("md5",			&php_hash_md5_ops);
1123 	php_hash_register_algo("sha1",			&php_hash_sha1_ops);
1124 	php_hash_register_algo("sha224",		&php_hash_sha224_ops);
1125 	php_hash_register_algo("sha256",		&php_hash_sha256_ops);
1126 	php_hash_register_algo("sha384",		&php_hash_sha384_ops);
1127 	php_hash_register_algo("sha512/224",            &php_hash_sha512_224_ops);
1128 	php_hash_register_algo("sha512/256",            &php_hash_sha512_256_ops);
1129 	php_hash_register_algo("sha512",		&php_hash_sha512_ops);
1130 	php_hash_register_algo("sha3-224",		&php_hash_sha3_224_ops);
1131 	php_hash_register_algo("sha3-256",		&php_hash_sha3_256_ops);
1132 	php_hash_register_algo("sha3-384",		&php_hash_sha3_384_ops);
1133 	php_hash_register_algo("sha3-512",		&php_hash_sha3_512_ops);
1134 	php_hash_register_algo("ripemd128",		&php_hash_ripemd128_ops);
1135 	php_hash_register_algo("ripemd160",		&php_hash_ripemd160_ops);
1136 	php_hash_register_algo("ripemd256",		&php_hash_ripemd256_ops);
1137 	php_hash_register_algo("ripemd320",		&php_hash_ripemd320_ops);
1138 	php_hash_register_algo("whirlpool",		&php_hash_whirlpool_ops);
1139 	php_hash_register_algo("tiger128,3",	&php_hash_3tiger128_ops);
1140 	php_hash_register_algo("tiger160,3",	&php_hash_3tiger160_ops);
1141 	php_hash_register_algo("tiger192,3",	&php_hash_3tiger192_ops);
1142 	php_hash_register_algo("tiger128,4",	&php_hash_4tiger128_ops);
1143 	php_hash_register_algo("tiger160,4",	&php_hash_4tiger160_ops);
1144 	php_hash_register_algo("tiger192,4",	&php_hash_4tiger192_ops);
1145 	php_hash_register_algo("snefru",		&php_hash_snefru_ops);
1146 	php_hash_register_algo("snefru256",		&php_hash_snefru_ops);
1147 	php_hash_register_algo("gost",			&php_hash_gost_ops);
1148 	php_hash_register_algo("gost-crypto",		&php_hash_gost_crypto_ops);
1149 	php_hash_register_algo("adler32",		&php_hash_adler32_ops);
1150 	php_hash_register_algo("crc32",			&php_hash_crc32_ops);
1151 	php_hash_register_algo("crc32b",		&php_hash_crc32b_ops);
1152 	php_hash_register_algo("fnv132",		&php_hash_fnv132_ops);
1153 	php_hash_register_algo("fnv1a32",		&php_hash_fnv1a32_ops);
1154 	php_hash_register_algo("fnv164",		&php_hash_fnv164_ops);
1155 	php_hash_register_algo("fnv1a64",		&php_hash_fnv1a64_ops);
1156 	php_hash_register_algo("joaat",			&php_hash_joaat_ops);
1157 
1158 	PHP_HASH_HAVAL_REGISTER(3,128);
1159 	PHP_HASH_HAVAL_REGISTER(3,160);
1160 	PHP_HASH_HAVAL_REGISTER(3,192);
1161 	PHP_HASH_HAVAL_REGISTER(3,224);
1162 	PHP_HASH_HAVAL_REGISTER(3,256);
1163 
1164 	PHP_HASH_HAVAL_REGISTER(4,128);
1165 	PHP_HASH_HAVAL_REGISTER(4,160);
1166 	PHP_HASH_HAVAL_REGISTER(4,192);
1167 	PHP_HASH_HAVAL_REGISTER(4,224);
1168 	PHP_HASH_HAVAL_REGISTER(4,256);
1169 
1170 	PHP_HASH_HAVAL_REGISTER(5,128);
1171 	PHP_HASH_HAVAL_REGISTER(5,160);
1172 	PHP_HASH_HAVAL_REGISTER(5,192);
1173 	PHP_HASH_HAVAL_REGISTER(5,224);
1174 	PHP_HASH_HAVAL_REGISTER(5,256);
1175 
1176 	REGISTER_LONG_CONSTANT("HASH_HMAC",		PHP_HASH_HMAC,	CONST_CS | CONST_PERSISTENT);
1177 
1178 #ifdef PHP_MHASH_BC
1179 	mhash_init(INIT_FUNC_ARGS_PASSTHRU);
1180 #endif
1181 
1182 	return SUCCESS;
1183 }
1184 /* }}} */
1185 
1186 /* {{{ PHP_MSHUTDOWN_FUNCTION
1187  */
PHP_MSHUTDOWN_FUNCTION(hash)1188 PHP_MSHUTDOWN_FUNCTION(hash)
1189 {
1190 	zend_hash_destroy(&php_hash_hashtable);
1191 
1192 	return SUCCESS;
1193 }
1194 /* }}} */
1195 
1196 /* {{{ PHP_MINFO_FUNCTION
1197  */
PHP_MINFO_FUNCTION(hash)1198 PHP_MINFO_FUNCTION(hash)
1199 {
1200 	char buffer[2048];
1201 	zend_string *str;
1202 	char *s = buffer, *e = s + sizeof(buffer);
1203 
1204 	ZEND_HASH_FOREACH_STR_KEY(&php_hash_hashtable, str) {
1205 		s += slprintf(s, e - s, "%s ", ZSTR_VAL(str));
1206 	} ZEND_HASH_FOREACH_END();
1207 	*s = 0;
1208 
1209 	php_info_print_table_start();
1210 	php_info_print_table_row(2, "hash support", "enabled");
1211 	php_info_print_table_row(2, "Hashing Engines", buffer);
1212 	php_info_print_table_end();
1213 
1214 #ifdef PHP_MHASH_BC
1215 	php_info_print_table_start();
1216 	php_info_print_table_row(2, "MHASH support", "Enabled");
1217 	php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
1218 	php_info_print_table_end();
1219 #endif
1220 
1221 }
1222 /* }}} */
1223 
1224 /* {{{ arginfo */
1225 #ifdef PHP_HASH_MD5_NOT_IN_CORE
1226 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5, 0, 0, 1)
1227 	ZEND_ARG_INFO(0, str)
1228 	ZEND_ARG_INFO(0, raw_output)
1229 ZEND_END_ARG_INFO()
1230 
1231 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5_file, 0, 0, 1)
1232 	ZEND_ARG_INFO(0, filename)
1233 	ZEND_ARG_INFO(0, raw_output)
1234 ZEND_END_ARG_INFO()
1235 #endif
1236 
1237 #ifdef PHP_HASH_SHA1_NOT_IN_CORE
1238 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1, 0, 0, 1)
1239 	ZEND_ARG_INFO(0, str)
1240 	ZEND_ARG_INFO(0, raw_output)
1241 ZEND_END_ARG_INFO()
1242 
1243 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1_file, 0, 0, 1)
1244 	ZEND_ARG_INFO(0, filename)
1245 	ZEND_ARG_INFO(0, raw_output)
1246 ZEND_END_ARG_INFO()
1247 #endif
1248 
1249 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash, 0, 0, 2)
1250 	ZEND_ARG_INFO(0, algo)
1251 	ZEND_ARG_INFO(0, data)
1252 	ZEND_ARG_INFO(0, raw_output)
1253 ZEND_END_ARG_INFO()
1254 
1255 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_file, 0, 0, 2)
1256 	ZEND_ARG_INFO(0, algo)
1257 	ZEND_ARG_INFO(0, filename)
1258 	ZEND_ARG_INFO(0, raw_output)
1259 ZEND_END_ARG_INFO()
1260 
1261 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac, 0, 0, 3)
1262 	ZEND_ARG_INFO(0, algo)
1263 	ZEND_ARG_INFO(0, data)
1264 	ZEND_ARG_INFO(0, key)
1265 	ZEND_ARG_INFO(0, raw_output)
1266 ZEND_END_ARG_INFO()
1267 
1268 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac_file, 0, 0, 3)
1269 	ZEND_ARG_INFO(0, algo)
1270 	ZEND_ARG_INFO(0, filename)
1271 	ZEND_ARG_INFO(0, key)
1272 	ZEND_ARG_INFO(0, raw_output)
1273 ZEND_END_ARG_INFO()
1274 
1275 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_init, 0, 0, 1)
1276 	ZEND_ARG_INFO(0, algo)
1277 	ZEND_ARG_INFO(0, options)
1278 	ZEND_ARG_INFO(0, key)
1279 ZEND_END_ARG_INFO()
1280 
1281 ZEND_BEGIN_ARG_INFO(arginfo_hash_update, 0)
1282 	ZEND_ARG_INFO(0, context)
1283 	ZEND_ARG_INFO(0, data)
1284 ZEND_END_ARG_INFO()
1285 
1286 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_stream, 0, 0, 2)
1287 	ZEND_ARG_INFO(0, context)
1288 	ZEND_ARG_INFO(0, handle)
1289 	ZEND_ARG_INFO(0, length)
1290 ZEND_END_ARG_INFO()
1291 
1292 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_file, 0, 0, 2)
1293 	ZEND_ARG_INFO(0, context)
1294 	ZEND_ARG_INFO(0, filename)
1295 	ZEND_ARG_INFO(0, stream_context)
1296 ZEND_END_ARG_INFO()
1297 
1298 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_final, 0, 0, 1)
1299 	ZEND_ARG_INFO(0, context)
1300 	ZEND_ARG_INFO(0, raw_output)
1301 ZEND_END_ARG_INFO()
1302 
1303 ZEND_BEGIN_ARG_INFO(arginfo_hash_copy, 0)
1304 	ZEND_ARG_INFO(0, context)
1305 ZEND_END_ARG_INFO()
1306 
1307 ZEND_BEGIN_ARG_INFO(arginfo_hash_algos, 0)
1308 ZEND_END_ARG_INFO()
1309 
1310 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_pbkdf2, 0, 0, 4)
1311 	ZEND_ARG_INFO(0, algo)
1312 	ZEND_ARG_INFO(0, password)
1313 	ZEND_ARG_INFO(0, salt)
1314 	ZEND_ARG_INFO(0, iterations)
1315 	ZEND_ARG_INFO(0, length)
1316 	ZEND_ARG_INFO(0, raw_output)
1317 ZEND_END_ARG_INFO()
1318 
1319 ZEND_BEGIN_ARG_INFO(arginfo_hash_equals, 0)
1320 	ZEND_ARG_INFO(0, known_string)
1321 	ZEND_ARG_INFO(0, user_string)
1322 ZEND_END_ARG_INFO()
1323 
1324 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hkdf, 0, 0, 2)
1325 	ZEND_ARG_INFO(0, ikm)
1326 	ZEND_ARG_INFO(0, algo)
1327 	ZEND_ARG_INFO(0, length)
1328 	ZEND_ARG_INFO(0, string)
1329 	ZEND_ARG_INFO(0, salt)
1330 ZEND_END_ARG_INFO()
1331 
1332 /* BC Land */
1333 #ifdef PHP_MHASH_BC
1334 ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_block_size, 0)
1335 	ZEND_ARG_INFO(0, hash)
1336 ZEND_END_ARG_INFO()
1337 
1338 ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_hash_name, 0)
1339 	ZEND_ARG_INFO(0, hash)
1340 ZEND_END_ARG_INFO()
1341 
1342 ZEND_BEGIN_ARG_INFO(arginfo_mhash_keygen_s2k, 0)
1343 	ZEND_ARG_INFO(0, hash)
1344 	ZEND_ARG_INFO(0, input_password)
1345 	ZEND_ARG_INFO(0, salt)
1346 	ZEND_ARG_INFO(0, bytes)
1347 ZEND_END_ARG_INFO()
1348 
1349 ZEND_BEGIN_ARG_INFO(arginfo_mhash_count, 0)
1350 ZEND_END_ARG_INFO()
1351 
1352 ZEND_BEGIN_ARG_INFO_EX(arginfo_mhash, 0, 0, 2)
1353 	ZEND_ARG_INFO(0, hash)
1354 	ZEND_ARG_INFO(0, data)
1355 	ZEND_ARG_INFO(0, key)
1356 ZEND_END_ARG_INFO()
1357 #endif
1358 
1359 /* }}} */
1360 
1361 /* {{{ hash_functions[]
1362  */
1363 const zend_function_entry hash_functions[] = {
1364 	PHP_FE(hash,									arginfo_hash)
1365 	PHP_FE(hash_file,								arginfo_hash_file)
1366 
1367 	PHP_FE(hash_hmac,								arginfo_hash_hmac)
1368 	PHP_FE(hash_hmac_file,							arginfo_hash_hmac_file)
1369 
1370 	PHP_FE(hash_init,								arginfo_hash_init)
1371 	PHP_FE(hash_update,								arginfo_hash_update)
1372 	PHP_FE(hash_update_stream,						arginfo_hash_update_stream)
1373 	PHP_FE(hash_update_file,						arginfo_hash_update_file)
1374 	PHP_FE(hash_final,								arginfo_hash_final)
1375 	PHP_FE(hash_copy,								arginfo_hash_copy)
1376 
1377 	PHP_FE(hash_algos,								arginfo_hash_algos)
1378 	PHP_FE(hash_pbkdf2,								arginfo_hash_pbkdf2)
1379 	PHP_FE(hash_equals,								arginfo_hash_equals)
1380 	PHP_FE(hash_hkdf,								arginfo_hash_hkdf)
1381 
1382 	/* BC Land */
1383 #ifdef PHP_HASH_MD5_NOT_IN_CORE
1384 	PHP_NAMED_FE(md5, php_if_md5,					arginfo_hash_md5)
1385 	PHP_NAMED_FE(md5_file, php_if_md5_file,			arginfo_hash_md5_file)
1386 #endif /* PHP_HASH_MD5_NOT_IN_CORE */
1387 
1388 #ifdef PHP_HASH_SHA1_NOT_IN_CORE
1389 	PHP_NAMED_FE(sha1, php_if_sha1,					arginfo_hash_sha1)
1390 	PHP_NAMED_FE(sha1_file, php_if_sha1_file,		arginfo_hash_sha1_file)
1391 #endif /* PHP_HASH_SHA1_NOT_IN_CORE */
1392 
1393 #ifdef PHP_MHASH_BC
1394 	PHP_FE(mhash_keygen_s2k, arginfo_mhash_keygen_s2k)
1395 	PHP_FE(mhash_get_block_size, arginfo_mhash_get_block_size)
1396 	PHP_FE(mhash_get_hash_name, arginfo_mhash_get_hash_name)
1397 	PHP_FE(mhash_count, arginfo_mhash_count)
1398 	PHP_FE(mhash, arginfo_mhash)
1399 #endif
1400 
1401 	PHP_FE_END
1402 };
1403 /* }}} */
1404 
1405 /* {{{ hash_module_entry
1406  */
1407 zend_module_entry hash_module_entry = {
1408 	STANDARD_MODULE_HEADER,
1409 	PHP_HASH_EXTNAME,
1410 	hash_functions,
1411 	PHP_MINIT(hash),
1412 	PHP_MSHUTDOWN(hash),
1413 	NULL, /* RINIT */
1414 	NULL, /* RSHUTDOWN */
1415 	PHP_MINFO(hash),
1416 	PHP_HASH_VERSION,
1417 	STANDARD_MODULE_PROPERTIES
1418 };
1419 /* }}} */
1420 
1421 #ifdef COMPILE_DL_HASH
1422 ZEND_GET_MODULE(hash)
1423 #endif
1424 
1425 /*
1426  * Local variables:
1427  * tab-width: 4
1428  * c-basic-offset: 4
1429  * End:
1430  * vim600: noet sw=4 ts=4 fdm=marker
1431  * vim<600: noet sw=4 ts=4
1432  */
1433