xref: /PHP-8.3/ext/hash/hash.c (revision 4bb27e2d)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Sara Golemon <pollita@php.net>                               |
14   |         Scott MacVicar <scottmac@php.net>                            |
15   +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include <math.h>
23 #include "php_hash.h"
24 #include "ext/standard/info.h"
25 #include "ext/standard/file.h"
26 #include "ext/standard/php_var.h"
27 #include "ext/spl/spl_exceptions.h"
28 
29 #include "zend_attributes.h"
30 #include "zend_exceptions.h"
31 #include "zend_interfaces.h"
32 #include "zend_smart_str.h"
33 
34 #include "hash_arginfo.h"
35 
36 #ifdef PHP_WIN32
37 # define __alignof__ __alignof
38 #else
39 # ifndef HAVE_ALIGNOF
40 #  include <stddef.h>
41 #  define __alignof__(type) offsetof (struct { char c; type member;}, member)
42 # endif
43 #endif
44 
45 HashTable php_hash_hashtable;
46 zend_class_entry *php_hashcontext_ce;
47 static zend_object_handlers php_hashcontext_handlers;
48 
49 #ifdef PHP_MHASH_BC
50 struct mhash_bc_entry {
51 	char *mhash_name;
52 	char *hash_name;
53 	int value;
54 };
55 
56 #define MHASH_NUM_ALGOS 42
57 
58 static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
59 	{"CRC32", "crc32", 0}, /* used by bzip */
60 	{"MD5", "md5", 1},
61 	{"SHA1", "sha1", 2},
62 	{"HAVAL256", "haval256,3", 3},
63 	{NULL, NULL, 4},
64 	{"RIPEMD160", "ripemd160", 5},
65 	{NULL, NULL, 6},
66 	{"TIGER", "tiger192,3", 7},
67 	{"GOST", "gost", 8},
68 	{"CRC32B", "crc32b", 9}, /* used by ethernet (IEEE 802.3), gzip, zip, png, etc */
69 	{"HAVAL224", "haval224,3", 10},
70 	{"HAVAL192", "haval192,3", 11},
71 	{"HAVAL160", "haval160,3", 12},
72 	{"HAVAL128", "haval128,3", 13},
73 	{"TIGER128", "tiger128,3", 14},
74 	{"TIGER160", "tiger160,3", 15},
75 	{"MD4", "md4", 16},
76 	{"SHA256", "sha256", 17},
77 	{"ADLER32", "adler32", 18},
78 	{"SHA224", "sha224", 19},
79 	{"SHA512", "sha512", 20},
80 	{"SHA384", "sha384", 21},
81 	{"WHIRLPOOL", "whirlpool", 22},
82 	{"RIPEMD128", "ripemd128", 23},
83 	{"RIPEMD256", "ripemd256", 24},
84 	{"RIPEMD320", "ripemd320", 25},
85 	{NULL, NULL, 26}, /* support needs to be added for snefru 128 */
86 	{"SNEFRU256", "snefru256", 27},
87 	{"MD2", "md2", 28},
88 	{"FNV132", "fnv132", 29},
89 	{"FNV1A32", "fnv1a32", 30},
90 	{"FNV164", "fnv164", 31},
91 	{"FNV1A64", "fnv1a64", 32},
92 	{"JOAAT", "joaat", 33},
93 	{"CRC32C", "crc32c", 34}, /* Castagnoli's CRC, used by iSCSI, SCTP, Btrfs, ext4, etc */
94 	{"MURMUR3A", "murmur3a", 35},
95 	{"MURMUR3C", "murmur3c", 36},
96 	{"MURMUR3F", "murmur3f", 37},
97 	{"XXH32", "xxh32", 38},
98 	{"XXH64", "xxh64", 39},
99 	{"XXH3", "xxh3", 40},
100 	{"XXH128", "xxh128", 41},
101 };
102 #endif
103 
104 /* Hash Registry Access */
105 
php_hash_fetch_ops(zend_string * algo)106 PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(zend_string *algo) /* {{{ */
107 {
108 	zend_string *lower = zend_string_tolower(algo);
109 	php_hash_ops *ops = zend_hash_find_ptr(&php_hash_hashtable, lower);
110 	zend_string_release(lower);
111 
112 	return ops;
113 }
114 /* }}} */
115 
php_hash_register_algo(const char * algo,const php_hash_ops * ops)116 PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops) /* {{{ */
117 {
118 	size_t algo_len = strlen(algo);
119 	char *lower = zend_str_tolower_dup(algo, algo_len);
120 	zend_hash_add_ptr(&php_hash_hashtable, zend_string_init_interned(lower, algo_len, 1), (void *) ops);
121 	efree(lower);
122 }
123 /* }}} */
124 
php_hash_copy(const void * ops,void * orig_context,void * dest_context)125 PHP_HASH_API int php_hash_copy(const void *ops, void *orig_context, void *dest_context) /* {{{ */
126 {
127 	php_hash_ops *hash_ops = (php_hash_ops *)ops;
128 
129 	memcpy(dest_context, orig_context, hash_ops->context_size);
130 	return SUCCESS;
131 }
132 /* }}} */
133 
134 
align_to(size_t pos,size_t alignment)135 static inline size_t align_to(size_t pos, size_t alignment) {
136 	size_t offset = pos & (alignment - 1);
137 	return pos + (offset ? alignment - offset : 0);
138 }
139 
parse_serialize_spec(const char ** specp,size_t * pos,size_t * sz,size_t * max_alignment)140 static size_t parse_serialize_spec(
141 		const char **specp, size_t *pos, size_t *sz, size_t *max_alignment) {
142 	size_t count, alignment;
143 	const char *spec = *specp;
144 	/* parse size */
145 	if (*spec == 's' || *spec == 'S') {
146 		*sz = 2;
147 		alignment = __alignof__(uint16_t); /* usually 2 */
148 	} else if (*spec == 'l' || *spec == 'L') {
149 		*sz = 4;
150 		alignment = __alignof__(uint32_t); /* usually 4 */
151 	} else if (*spec == 'q' || *spec == 'Q') {
152 		*sz = 8;
153 		alignment = __alignof__(uint64_t); /* usually 8 */
154 	} else if (*spec == 'i' || *spec == 'I') {
155 		*sz = sizeof(int);
156 		alignment = __alignof__(int);      /* usually 4 */
157 	} else {
158 		ZEND_ASSERT(*spec == 'b' || *spec == 'B');
159 		*sz = 1;
160 		alignment = 1;
161 	}
162 	/* process alignment */
163 	*pos = align_to(*pos, alignment);
164 	*max_alignment = *max_alignment < alignment ? alignment : *max_alignment;
165 	/* parse count */
166 	++spec;
167 	if (isdigit((unsigned char) *spec)) {
168 		count = 0;
169 		while (isdigit((unsigned char) *spec)) {
170 			count = 10 * count + *spec - '0';
171 			++spec;
172 		}
173 	} else {
174 		count = 1;
175 	}
176 	*specp = spec;
177 	return count;
178 }
179 
one_from_buffer(size_t sz,const unsigned char * buf)180 static uint64_t one_from_buffer(size_t sz, const unsigned char *buf) {
181 	if (sz == 2) {
182 		const uint16_t *x = (const uint16_t *) buf;
183 		return *x;
184 	} else if (sz == 4) {
185 		const uint32_t *x = (const uint32_t *) buf;
186 		return *x;
187 	} else if (sz == 8) {
188 		const uint64_t *x = (const uint64_t *) buf;
189 		return *x;
190 	} else {
191 		ZEND_ASSERT(sz == 1);
192 		return *buf;
193 	}
194 }
195 
one_to_buffer(size_t sz,unsigned char * buf,uint64_t val)196 static void one_to_buffer(size_t sz, unsigned char *buf, uint64_t val) {
197 	if (sz == 2) {
198 		uint16_t *x = (uint16_t *) buf;
199 		*x = val;
200 	} else if (sz == 4) {
201 		uint32_t *x = (uint32_t *) buf;
202 		*x = val;
203 	} else if (sz == 8) {
204 		uint64_t *x = (uint64_t *) buf;
205 		*x = val;
206 	} else {
207 		ZEND_ASSERT(sz == 1);
208 		*buf = val;
209 	}
210 }
211 
212 /* Serialize a hash context according to a `spec` string.
213    Spec contents:
214    b[COUNT] -- serialize COUNT bytes
215    s[COUNT] -- serialize COUNT 16-bit integers
216    l[COUNT] -- serialize COUNT 32-bit integers
217    q[COUNT] -- serialize COUNT 64-bit integers
218    i[COUNT] -- serialize COUNT `int`s
219    B[COUNT] -- skip COUNT bytes
220    S[COUNT], L[COUNT], etc. -- uppercase versions skip instead of read
221    . (must be last character) -- assert that the hash context has exactly
222        this size
223    Example: "llllllb64l16." is the spec for an MD5 context: 6 32-bit
224    integers, followed by 64 bytes, then 16 32-bit integers, and that's
225    exactly the size of the context.
226 
227    The serialization result is an array. Each integer is serialized as a
228    32-bit integer, except that a run of 2 or more bytes is encoded as a
229    string, and each 64-bit integer is serialized as two 32-bit integers, least
230    significant bits first. This allows 32-bit and 64-bit architectures to
231    interchange serialized HashContexts. */
232 
php_hash_serialize_spec(const php_hashcontext_object * hash,zval * zv,const char * spec)233 PHP_HASH_API int php_hash_serialize_spec(const php_hashcontext_object *hash, zval *zv, const char *spec) /* {{{ */
234 {
235 	size_t pos = 0, max_alignment = 1;
236 	unsigned char *buf = (unsigned char *) hash->context;
237 	zval tmp;
238 	if (buf == NULL) {
239 		return FAILURE;
240 	}
241 	array_init(zv);
242 	while (*spec != '\0' && *spec != '.') {
243 		char spec_ch = *spec;
244 		size_t sz, count = parse_serialize_spec(&spec, &pos, &sz, &max_alignment);
245 		if (pos + count * sz > hash->ops->context_size) {
246 			return FAILURE;
247 		}
248 		if (isupper((unsigned char) spec_ch)) {
249 			pos += count * sz;
250 		} else if (sz == 1 && count > 1) {
251 			ZVAL_STRINGL(&tmp, (char *) buf + pos, count);
252 			zend_hash_next_index_insert(Z_ARRVAL_P(zv), &tmp);
253 			pos += count;
254 		} else {
255 			while (count > 0) {
256 				uint64_t val = one_from_buffer(sz, buf + pos);
257 				pos += sz;
258 				ZVAL_LONG(&tmp, (int32_t) val);
259 				zend_hash_next_index_insert(Z_ARRVAL_P(zv), &tmp);
260 				if (sz == 8) {
261 					ZVAL_LONG(&tmp, (int32_t) (val >> 32));
262 					zend_hash_next_index_insert(Z_ARRVAL_P(zv), &tmp);
263 				}
264 				--count;
265 			}
266 		}
267 	}
268 	if (*spec == '.' && align_to(pos, max_alignment) != hash->ops->context_size) {
269 		return FAILURE;
270 	}
271 	return SUCCESS;
272 }
273 /* }}} */
274 
275 /* Unserialize a hash context serialized by `php_hash_serialize_spec` with `spec`.
276    Returns SUCCESS on success and a negative error code on failure.
277    Codes: FAILURE (-1) == generic failure
278    -999 == spec wrong size for context
279    -1000 - POS == problem at byte offset POS */
280 
php_hash_unserialize_spec(php_hashcontext_object * hash,const zval * zv,const char * spec)281 PHP_HASH_API int php_hash_unserialize_spec(php_hashcontext_object *hash, const zval *zv, const char *spec) /* {{{ */
282 {
283 	size_t pos = 0, max_alignment = 1, j = 0;
284 	unsigned char *buf = (unsigned char *) hash->context;
285 	zval *elt;
286 	if (Z_TYPE_P(zv) != IS_ARRAY) {
287 		return FAILURE;
288 	}
289 	while (*spec != '\0' && *spec != '.') {
290 		char spec_ch = *spec;
291 		size_t sz, count = parse_serialize_spec(&spec, &pos, &sz, &max_alignment);
292 		if (pos + count * sz > hash->ops->context_size) {
293 			return -999;
294 		}
295 		if (isupper((unsigned char) spec_ch)) {
296 			pos += count * sz;
297 		} else if (sz == 1 && count > 1) {
298 			elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
299 			if (!elt || Z_TYPE_P(elt) != IS_STRING || Z_STRLEN_P(elt) != count) {
300 				return -1000 - pos;
301 			}
302 			++j;
303 			memcpy(buf + pos, Z_STRVAL_P(elt), count);
304 			pos += count;
305 		} else {
306 			while (count > 0) {
307 				uint64_t val;
308 				elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
309 				if (!elt || Z_TYPE_P(elt) != IS_LONG) {
310 					return -1000 - pos;
311 				}
312 				++j;
313 				val = (uint32_t) Z_LVAL_P(elt);
314 				if (sz == 8) {
315 					elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
316 					if (!elt || Z_TYPE_P(elt) != IS_LONG) {
317 						return -1000 - pos;
318 					}
319 					++j;
320 					val += ((uint64_t) Z_LVAL_P(elt)) << 32;
321 				}
322 				one_to_buffer(sz, buf + pos, val);
323 				pos += sz;
324 				--count;
325 			}
326 		}
327 	}
328 	if (*spec == '.' && align_to(pos, max_alignment) != hash->ops->context_size) {
329 		return -999;
330 	}
331 	return SUCCESS;
332 }
333 /* }}} */
334 
php_hash_serialize(const php_hashcontext_object * hash,zend_long * magic,zval * zv)335 PHP_HASH_API int php_hash_serialize(const php_hashcontext_object *hash, zend_long *magic, zval *zv) /* {{{ */
336 {
337 	if (hash->ops->serialize_spec) {
338 		*magic = PHP_HASH_SERIALIZE_MAGIC_SPEC;
339 		return php_hash_serialize_spec(hash, zv, hash->ops->serialize_spec);
340 	} else {
341 		return FAILURE;
342 	}
343 }
344 /* }}} */
345 
php_hash_unserialize(php_hashcontext_object * hash,zend_long magic,const zval * zv)346 PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv) /* {{{ */
347 {
348 	if (hash->ops->serialize_spec
349 		&& magic == PHP_HASH_SERIALIZE_MAGIC_SPEC) {
350 		return php_hash_unserialize_spec(hash, zv, hash->ops->serialize_spec);
351 	} else {
352 		return FAILURE;
353 	}
354 }
355 /* }}} */
356 
357 /* Userspace */
358 
php_hash_do_hash(zval * return_value,zend_string * algo,char * data,size_t data_len,bool raw_output,bool isfilename,HashTable * args)359 static void php_hash_do_hash(
360 	zval *return_value, zend_string *algo, char *data, size_t data_len, bool raw_output, bool isfilename, HashTable *args
361 ) /* {{{ */ {
362 	zend_string *digest;
363 	const php_hash_ops *ops;
364 	void *context;
365 	php_stream *stream = NULL;
366 
367 	ops = php_hash_fetch_ops(algo);
368 	if (!ops) {
369 		zend_argument_value_error(1, "must be a valid hashing algorithm");
370 		RETURN_THROWS();
371 	}
372 	if (isfilename) {
373 		if (CHECK_NULL_PATH(data, data_len)) {
374 			zend_argument_value_error(1, "must not contain any null bytes");
375 			RETURN_THROWS();
376 		}
377 		stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
378 		if (!stream) {
379 			/* Stream will report errors opening file */
380 			RETURN_FALSE;
381 		}
382 	}
383 
384 	context = php_hash_alloc_context(ops);
385 	ops->hash_init(context, args);
386 
387 	if (isfilename) {
388 		char buf[1024];
389 		ssize_t n;
390 
391 		while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
392 			ops->hash_update(context, (unsigned char *) buf, n);
393 		}
394 		php_stream_close(stream);
395 		if (n < 0) {
396 			efree(context);
397 			RETURN_FALSE;
398 		}
399 	} else {
400 		ops->hash_update(context, (unsigned char *) data, data_len);
401 	}
402 
403 	digest = zend_string_alloc(ops->digest_size, 0);
404 	ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
405 	efree(context);
406 
407 	if (raw_output) {
408 		ZSTR_VAL(digest)[ops->digest_size] = 0;
409 		RETURN_NEW_STR(digest);
410 	} else {
411 		zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
412 
413 		php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
414 		ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
415 		zend_string_release_ex(digest, 0);
416 		RETURN_NEW_STR(hex_digest);
417 	}
418 }
419 /* }}} */
420 
421 /* {{{ Generate a hash of a given input string
422 Returns lowercase hexits by default */
PHP_FUNCTION(hash)423 PHP_FUNCTION(hash)
424 {
425 	zend_string *algo;
426 	char *data;
427 	size_t data_len;
428 	bool raw_output = 0;
429 	HashTable *args = NULL;
430 
431 	ZEND_PARSE_PARAMETERS_START(2, 4)
432 		Z_PARAM_STR(algo)
433 		Z_PARAM_STRING(data, data_len)
434 		Z_PARAM_OPTIONAL
435 		Z_PARAM_BOOL(raw_output)
436 		Z_PARAM_ARRAY_HT(args)
437 	ZEND_PARSE_PARAMETERS_END();
438 
439 	php_hash_do_hash(return_value, algo, data, data_len, raw_output, 0, args);
440 }
441 /* }}} */
442 
443 /* {{{ Generate a hash of a given file
444 Returns lowercase hexits by default */
PHP_FUNCTION(hash_file)445 PHP_FUNCTION(hash_file)
446 {
447 	zend_string *algo;
448 	char *data;
449 	size_t data_len;
450 	bool raw_output = 0;
451 	HashTable *args = NULL;
452 
453 	ZEND_PARSE_PARAMETERS_START(2, 4)
454 		Z_PARAM_STR(algo)
455 		Z_PARAM_STRING(data, data_len)
456 		Z_PARAM_OPTIONAL
457 		Z_PARAM_BOOL(raw_output)
458 		Z_PARAM_ARRAY_HT(args)
459 	ZEND_PARSE_PARAMETERS_END();
460 
461 	php_hash_do_hash(return_value, algo, data, data_len, raw_output, 1, args);
462 }
463 /* }}} */
464 
php_hash_string_xor_char(unsigned char * out,const unsigned char * in,const unsigned char xor_with,const size_t length)465 static inline void php_hash_string_xor_char(unsigned char *out, const unsigned char *in, const unsigned char xor_with, const size_t length) {
466 	size_t i;
467 	for (i=0; i < length; i++) {
468 		out[i] = in[i] ^ xor_with;
469 	}
470 }
471 
php_hash_string_xor(unsigned char * out,const unsigned char * in,const unsigned char * xor_with,const size_t length)472 static inline void php_hash_string_xor(unsigned char *out, const unsigned char *in, const unsigned char *xor_with, const size_t length) {
473 	size_t i;
474 	for (i=0; i < length; i++) {
475 		out[i] = in[i] ^ xor_with[i];
476 	}
477 }
478 
php_hash_hmac_prep_key(unsigned char * K,const php_hash_ops * ops,void * context,const unsigned char * key,const size_t key_len)479 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) {
480 	memset(K, 0, ops->block_size);
481 	if (key_len > ops->block_size) {
482 		/* Reduce the key first */
483 		ops->hash_init(context, NULL);
484 		ops->hash_update(context, key, key_len);
485 		ops->hash_final(K, context);
486 	} else {
487 		memcpy(K, key, key_len);
488 	}
489 	/* XOR the key with 0x36 to get the ipad) */
490 	php_hash_string_xor_char(K, K, 0x36, ops->block_size);
491 }
492 
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)493 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) {
494 	ops->hash_init(context, NULL);
495 	ops->hash_update(context, key, ops->block_size);
496 	ops->hash_update(context, data, data_size);
497 	ops->hash_final(final, context);
498 }
499 
php_hash_do_hash_hmac(zval * return_value,zend_string * algo,char * data,size_t data_len,char * key,size_t key_len,bool raw_output,bool isfilename)500 static void php_hash_do_hash_hmac(
501 	zval *return_value, zend_string *algo, char *data, size_t data_len, char *key, size_t key_len, bool raw_output, bool isfilename
502 ) /* {{{ */ {
503 	zend_string *digest;
504 	unsigned char *K;
505 	const php_hash_ops *ops;
506 	void *context;
507 	php_stream *stream = NULL;
508 
509 	ops = php_hash_fetch_ops(algo);
510 	if (!ops || !ops->is_crypto) {
511 		zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
512 		RETURN_THROWS();
513 	}
514 
515 	if (isfilename) {
516 		if (CHECK_NULL_PATH(data, data_len)) {
517 			zend_argument_value_error(2, "must not contain any null bytes");
518 			RETURN_THROWS();
519 		}
520 		stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
521 		if (!stream) {
522 			/* Stream will report errors opening file */
523 			RETURN_FALSE;
524 		}
525 	}
526 
527 	context = php_hash_alloc_context(ops);
528 
529 	K = emalloc(ops->block_size);
530 	digest = zend_string_alloc(ops->digest_size, 0);
531 
532 	php_hash_hmac_prep_key(K, ops, context, (unsigned char *) key, key_len);
533 
534 	if (isfilename) {
535 		char buf[1024];
536 		ssize_t n;
537 		ops->hash_init(context, NULL);
538 		ops->hash_update(context, K, ops->block_size);
539 		while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
540 			ops->hash_update(context, (unsigned char *) buf, n);
541 		}
542 		php_stream_close(stream);
543 		if (n < 0) {
544 			efree(context);
545 			efree(K);
546 			zend_string_release(digest);
547 			RETURN_FALSE;
548 		}
549 
550 		ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
551 	} else {
552 		php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) data, data_len);
553 	}
554 
555 	php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
556 
557 	php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
558 
559 	/* Zero the key */
560 	ZEND_SECURE_ZERO(K, ops->block_size);
561 	efree(K);
562 	efree(context);
563 
564 	if (raw_output) {
565 		ZSTR_VAL(digest)[ops->digest_size] = 0;
566 		RETURN_NEW_STR(digest);
567 	} else {
568 		zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
569 
570 		php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
571 		ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
572 		zend_string_release_ex(digest, 0);
573 		RETURN_NEW_STR(hex_digest);
574 	}
575 }
576 /* }}} */
577 
578 /* {{{ Generate a hash of a given input string with a key using HMAC
579 Returns lowercase hexits by default */
PHP_FUNCTION(hash_hmac)580 PHP_FUNCTION(hash_hmac)
581 {
582 	zend_string *algo;
583 	char *data, *key;
584 	size_t data_len, key_len;
585 	bool raw_output = 0;
586 
587 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
588 		RETURN_THROWS();
589 	}
590 
591 	php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, raw_output, 0);
592 }
593 /* }}} */
594 
595 /* {{{ Generate a hash of a given file with a key using HMAC
596 Returns lowercase hexits by default */
PHP_FUNCTION(hash_hmac_file)597 PHP_FUNCTION(hash_hmac_file)
598 {
599 	zend_string *algo;
600 	char *data, *key;
601 	size_t data_len, key_len;
602 	bool raw_output = 0;
603 
604 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
605 		RETURN_THROWS();
606 	}
607 
608 	php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, raw_output, 1);
609 }
610 /* }}} */
611 
612 /* {{{ Initialize a hashing context */
PHP_FUNCTION(hash_init)613 PHP_FUNCTION(hash_init)
614 {
615 	zend_string *algo, *key = NULL;
616 	zend_long options = 0;
617 	void *context;
618 	const php_hash_ops *ops;
619 	php_hashcontext_object *hash;
620 	HashTable *args = NULL;
621 
622 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lSh", &algo, &options, &key, &args) == FAILURE) {
623 		RETURN_THROWS();
624 	}
625 
626 	ops = php_hash_fetch_ops(algo);
627 	if (!ops) {
628 		zend_argument_value_error(1, "must be a valid hashing algorithm");
629 		RETURN_THROWS();
630 	}
631 
632 	if (options & PHP_HASH_HMAC) {
633 		if (!ops->is_crypto) {
634 			zend_argument_value_error(1, "must be a cryptographic hashing algorithm if HMAC is requested");
635 			RETURN_THROWS();
636 		}
637 		if (!key || (ZSTR_LEN(key) == 0)) {
638 			/* Note: a zero length key is no key at all */
639 			zend_argument_value_error(3, "cannot be empty when HMAC is requested");
640 			RETURN_THROWS();
641 		}
642 	}
643 
644 	object_init_ex(return_value, php_hashcontext_ce);
645 	hash = php_hashcontext_from_object(Z_OBJ_P(return_value));
646 
647 	context = php_hash_alloc_context(ops);
648 	ops->hash_init(context, args);
649 
650 	hash->ops = ops;
651 	hash->context = context;
652 	hash->options = options;
653 	hash->key = NULL;
654 
655 	if (options & PHP_HASH_HMAC) {
656 		char *K = emalloc(ops->block_size);
657 		size_t i, block_size;
658 
659 		memset(K, 0, ops->block_size);
660 
661 		if (ZSTR_LEN(key) > ops->block_size) {
662 			/* Reduce the key first */
663 			ops->hash_update(context, (unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key));
664 			ops->hash_final((unsigned char *) K, context);
665 			/* Make the context ready to start over */
666 			ops->hash_init(context, args);
667 		} else {
668 			memcpy(K, ZSTR_VAL(key), ZSTR_LEN(key));
669 		}
670 
671 		/* XOR ipad */
672 		block_size = ops->block_size;
673 		for(i = 0; i < block_size; i++) {
674 			K[i] ^= 0x36;
675 		}
676 		ops->hash_update(context, (unsigned char *) K, ops->block_size);
677 		hash->key = (unsigned char *) K;
678 	}
679 }
680 /* }}} */
681 
682 #define PHP_HASHCONTEXT_VERIFY(hash) { \
683 	if (!hash->context) { \
684 		zend_argument_type_error(1, "must be a valid, non-finalized HashContext"); \
685 		RETURN_THROWS(); \
686 	} \
687 }
688 
689 /* {{{ Pump data into the hashing algorithm */
PHP_FUNCTION(hash_update)690 PHP_FUNCTION(hash_update)
691 {
692 	zval *zhash;
693 	php_hashcontext_object *hash;
694 	zend_string *data;
695 
696 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &zhash, php_hashcontext_ce, &data) == FAILURE) {
697 		RETURN_THROWS();
698 	}
699 
700 	hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
701 	PHP_HASHCONTEXT_VERIFY(hash);
702 	hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(data), ZSTR_LEN(data));
703 
704 	RETURN_TRUE;
705 }
706 /* }}} */
707 
708 /* {{{ Pump data into the hashing algorithm from an open stream */
PHP_FUNCTION(hash_update_stream)709 PHP_FUNCTION(hash_update_stream)
710 {
711 	zval *zhash, *zstream;
712 	php_hashcontext_object *hash;
713 	php_stream *stream = NULL;
714 	zend_long length = -1, didread = 0;
715 
716 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Or|l", &zhash, php_hashcontext_ce, &zstream, &length) == FAILURE) {
717 		RETURN_THROWS();
718 	}
719 
720 	hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
721 	PHP_HASHCONTEXT_VERIFY(hash);
722 	php_stream_from_zval(stream, zstream);
723 
724 	while (length) {
725 		char buf[1024];
726 		zend_long toread = 1024;
727 		ssize_t n;
728 
729 		if (length > 0 && toread > length) {
730 			toread = length;
731 		}
732 
733 		if ((n = php_stream_read(stream, buf, toread)) <= 0) {
734 			RETURN_LONG(didread);
735 		}
736 		hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
737 		length -= n;
738 		didread += n;
739 	}
740 
741 	RETURN_LONG(didread);
742 }
743 /* }}} */
744 
745 /* {{{ Pump data into the hashing algorithm from a file */
PHP_FUNCTION(hash_update_file)746 PHP_FUNCTION(hash_update_file)
747 {
748 	zval *zhash, *zcontext = NULL;
749 	php_hashcontext_object *hash;
750 	php_stream_context *context = NULL;
751 	php_stream *stream;
752 	zend_string *filename;
753 	char buf[1024];
754 	ssize_t n;
755 
756 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|r!", &zhash, php_hashcontext_ce, &filename, &zcontext) == FAILURE) {
757 		RETURN_THROWS();
758 	}
759 
760 	hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
761 	PHP_HASHCONTEXT_VERIFY(hash);
762 	context = php_stream_context_from_zval(zcontext, 0);
763 
764 	stream = php_stream_open_wrapper_ex(ZSTR_VAL(filename), "rb", REPORT_ERRORS, NULL, context);
765 	if (!stream) {
766 		/* Stream will report errors opening file */
767 		RETURN_FALSE;
768 	}
769 
770 	while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
771 		hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
772 	}
773 	php_stream_close(stream);
774 
775 	RETURN_BOOL(n >= 0);
776 }
777 /* }}} */
778 
779 /* {{{ Output resulting digest */
PHP_FUNCTION(hash_final)780 PHP_FUNCTION(hash_final)
781 {
782 	zval *zhash;
783 	php_hashcontext_object *hash;
784 	bool raw_output = 0;
785 	zend_string *digest;
786 	size_t digest_len;
787 
788 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &zhash, php_hashcontext_ce, &raw_output) == FAILURE) {
789 		RETURN_THROWS();
790 	}
791 
792 	hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
793 	PHP_HASHCONTEXT_VERIFY(hash);
794 
795 	digest_len = hash->ops->digest_size;
796 	digest = zend_string_alloc(digest_len, 0);
797 	hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
798 	if (hash->options & PHP_HASH_HMAC) {
799 		size_t i, block_size;
800 
801 		/* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
802 		block_size = hash->ops->block_size;
803 		for(i = 0; i < block_size; i++) {
804 			hash->key[i] ^= 0x6A;
805 		}
806 
807 		/* Feed this result into the outer hash */
808 		hash->ops->hash_init(hash->context, NULL);
809 		hash->ops->hash_update(hash->context, hash->key, hash->ops->block_size);
810 		hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(digest), hash->ops->digest_size);
811 		hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
812 
813 		/* Zero the key */
814 		ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
815 		efree(hash->key);
816 		hash->key = NULL;
817 	}
818 	ZSTR_VAL(digest)[digest_len] = 0;
819 
820 	/* Invalidate the object from further use */
821 	efree(hash->context);
822 	hash->context = NULL;
823 
824 	if (raw_output) {
825 		RETURN_NEW_STR(digest);
826 	} else {
827 		zend_string *hex_digest = zend_string_safe_alloc(digest_len, 2, 0, 0);
828 
829 		php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), digest_len);
830 		ZSTR_VAL(hex_digest)[2 * digest_len] = 0;
831 		zend_string_release_ex(digest, 0);
832 		RETURN_NEW_STR(hex_digest);
833 	}
834 }
835 /* }}} */
836 
837 /* {{{ Copy hash object */
PHP_FUNCTION(hash_copy)838 PHP_FUNCTION(hash_copy)
839 {
840 	zval *zhash;
841 	php_hashcontext_object *context;
842 
843 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zhash, php_hashcontext_ce) == FAILURE) {
844 		RETURN_THROWS();
845 	}
846 
847 	context = php_hashcontext_from_object(Z_OBJ_P(zhash));
848 	PHP_HASHCONTEXT_VERIFY(context);
849 
850 	RETVAL_OBJ(Z_OBJ_HANDLER_P(zhash, clone_obj)(Z_OBJ_P(zhash)));
851 
852 	if (php_hashcontext_from_object(Z_OBJ_P(return_value))->context == NULL) {
853 		zval_ptr_dtor(return_value);
854 
855 		zend_throw_error(NULL, "Cannot copy hash");
856 		RETURN_THROWS();
857 	}
858 }
859 /* }}} */
860 
861 /* {{{ Return a list of registered hashing algorithms */
PHP_FUNCTION(hash_algos)862 PHP_FUNCTION(hash_algos)
863 {
864 	zend_string *str;
865 
866 	if (zend_parse_parameters_none() == FAILURE) {
867 		RETURN_THROWS();
868 	}
869 
870 	array_init(return_value);
871 	ZEND_HASH_MAP_FOREACH_STR_KEY(&php_hash_hashtable, str) {
872 		add_next_index_str(return_value, zend_string_copy(str));
873 	} ZEND_HASH_FOREACH_END();
874 }
875 /* }}} */
876 
877 /* {{{ Return a list of registered hashing algorithms suitable for hash_hmac() */
PHP_FUNCTION(hash_hmac_algos)878 PHP_FUNCTION(hash_hmac_algos)
879 {
880 	zend_string *str;
881 	const php_hash_ops *ops;
882 
883 	if (zend_parse_parameters_none() == FAILURE) {
884 		RETURN_THROWS();
885 	}
886 
887 	array_init(return_value);
888 	ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&php_hash_hashtable, str, ops) {
889 		if (ops->is_crypto) {
890 			add_next_index_str(return_value, zend_string_copy(str));
891 		}
892 	} ZEND_HASH_FOREACH_END();
893 }
894 /* }}} */
895 
896 /* {{{ RFC5869 HMAC-based key derivation function */
PHP_FUNCTION(hash_hkdf)897 PHP_FUNCTION(hash_hkdf)
898 {
899 	zend_string *returnval, *ikm, *algo, *info = NULL, *salt = NULL;
900 	zend_long length = 0;
901 	unsigned char *prk, *digest, *K;
902 	size_t i;
903 	size_t rounds;
904 	const php_hash_ops *ops;
905 	void *context;
906 
907 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lSS", &algo, &ikm, &length, &info, &salt) == FAILURE) {
908 		RETURN_THROWS();
909 	}
910 
911 	ops = php_hash_fetch_ops(algo);
912 	if (!ops || !ops->is_crypto) {
913 		zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
914 		RETURN_THROWS();
915 	}
916 
917 	if (ZSTR_LEN(ikm) == 0) {
918 		zend_argument_value_error(2, "cannot be empty");
919 		RETURN_THROWS();
920 	}
921 
922 	if (length < 0) {
923 		zend_argument_value_error(3, "must be greater than or equal to 0");
924 		RETURN_THROWS();
925 	} else if (length == 0) {
926 		length = ops->digest_size;
927 	} else if (length > (zend_long) (ops->digest_size * 255)) {
928 		zend_argument_value_error(3, "must be less than or equal to %zd", ops->digest_size * 255);
929 		RETURN_THROWS();
930 	}
931 
932 	context = php_hash_alloc_context(ops);
933 
934 	// Extract
935 	ops->hash_init(context, NULL);
936 	K = emalloc(ops->block_size);
937 	php_hash_hmac_prep_key(K, ops, context,
938 		(unsigned char *) (salt ? ZSTR_VAL(salt) : ""), salt ? ZSTR_LEN(salt) : 0);
939 
940 	prk = emalloc(ops->digest_size);
941 	php_hash_hmac_round(prk, ops, context, K, (unsigned char *) ZSTR_VAL(ikm), ZSTR_LEN(ikm));
942 	php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
943 	php_hash_hmac_round(prk, ops, context, K, prk, ops->digest_size);
944 	ZEND_SECURE_ZERO(K, ops->block_size);
945 
946 	// Expand
947 	returnval = zend_string_alloc(length, 0);
948 	digest = emalloc(ops->digest_size);
949 	for (i = 1, rounds = (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
950 		// chr(i)
951 		unsigned char c[1];
952 		c[0] = (i & 0xFF);
953 
954 		php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size);
955 		ops->hash_init(context, NULL);
956 		ops->hash_update(context, K, ops->block_size);
957 
958 		if (i > 1) {
959 			ops->hash_update(context, digest, ops->digest_size);
960 		}
961 
962 		if (info != NULL && ZSTR_LEN(info) > 0) {
963 			ops->hash_update(context, (unsigned char *) ZSTR_VAL(info), ZSTR_LEN(info));
964 		}
965 
966 		ops->hash_update(context, c, 1);
967 		ops->hash_final(digest, context);
968 		php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
969 		php_hash_hmac_round(digest, ops, context, K, digest, ops->digest_size);
970 		memcpy(
971 			ZSTR_VAL(returnval) + ((i - 1) * ops->digest_size),
972 			digest,
973 			(i == rounds ? length - ((i - 1) * ops->digest_size) : ops->digest_size)
974 		);
975 	}
976 
977 	ZEND_SECURE_ZERO(K, ops->block_size);
978 	ZEND_SECURE_ZERO(digest, ops->digest_size);
979 	ZEND_SECURE_ZERO(prk, ops->digest_size);
980 	efree(K);
981 	efree(context);
982 	efree(prk);
983 	efree(digest);
984 	ZSTR_VAL(returnval)[length] = 0;
985 	RETURN_STR(returnval);
986 }
987 
988 /* {{{ Generate a PBKDF2 hash of the given password and salt
989 Returns lowercase hexits by default */
PHP_FUNCTION(hash_pbkdf2)990 PHP_FUNCTION(hash_pbkdf2)
991 {
992 	zend_string *returnval, *algo;
993 	char *salt, *pass = NULL;
994 	unsigned char *computed_salt, *digest, *temp, *result, *K1, *K2 = NULL;
995 	zend_long loops, i, j, iterations, digest_length = 0, length = 0;
996 	size_t pass_len, salt_len = 0;
997 	bool raw_output = 0;
998 	const php_hash_ops *ops;
999 	void *context;
1000 	HashTable *args = NULL;
1001 
1002 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lbh", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output, &args) == FAILURE) {
1003 		RETURN_THROWS();
1004 	}
1005 
1006 	ops = php_hash_fetch_ops(algo);
1007 	if (!ops || !ops->is_crypto) {
1008 		zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
1009 		RETURN_THROWS();
1010 	}
1011 
1012 	if (salt_len > INT_MAX - 4) {
1013 		zend_argument_value_error(3, "must be less than or equal to INT_MAX - 4 bytes");
1014 		RETURN_THROWS();
1015 	}
1016 
1017 	if (iterations <= 0) {
1018 		zend_argument_value_error(4, "must be greater than 0");
1019 		RETURN_THROWS();
1020 	}
1021 
1022 	if (length < 0) {
1023 		zend_argument_value_error(5, "must be greater than or equal to 0");
1024 		RETURN_THROWS();
1025 	}
1026 
1027 	context = php_hash_alloc_context(ops);
1028 	ops->hash_init(context, args);
1029 
1030 	K1 = emalloc(ops->block_size);
1031 	K2 = emalloc(ops->block_size);
1032 	digest = emalloc(ops->digest_size);
1033 	temp = emalloc(ops->digest_size);
1034 
1035 	/* Setup Keys that will be used for all hmac rounds */
1036 	php_hash_hmac_prep_key(K1, ops, context, (unsigned char *) pass, pass_len);
1037 	/* Convert K1 to opad -- 0x6A = 0x36 ^ 0x5C */
1038 	php_hash_string_xor_char(K2, K1, 0x6A, ops->block_size);
1039 
1040 	/* Setup Main Loop to build a long enough result */
1041 	if (length == 0) {
1042 		length = ops->digest_size;
1043 		if (!raw_output) {
1044 			length = length * 2;
1045 		}
1046 	}
1047 	digest_length = length;
1048 	if (!raw_output) {
1049 		digest_length = (zend_long) ceil((float) length / 2.0);
1050 	}
1051 
1052 	loops = (zend_long) ceil((float) digest_length / (float) ops->digest_size);
1053 
1054 	result = safe_emalloc(loops, ops->digest_size, 0);
1055 
1056 	computed_salt = safe_emalloc(salt_len, 1, 4);
1057 	memcpy(computed_salt, (unsigned char *) salt, salt_len);
1058 
1059 	for (i = 1; i <= loops; i++) {
1060 		/* digest = hash_hmac(salt + pack('N', i), password) { */
1061 
1062 		/* pack("N", i) */
1063 		computed_salt[salt_len] = (unsigned char) (i >> 24);
1064 		computed_salt[salt_len + 1] = (unsigned char) ((i & 0xFF0000) >> 16);
1065 		computed_salt[salt_len + 2] = (unsigned char) ((i & 0xFF00) >> 8);
1066 		computed_salt[salt_len + 3] = (unsigned char) (i & 0xFF);
1067 
1068 		php_hash_hmac_round(digest, ops, context, K1, computed_salt, (zend_long) salt_len + 4);
1069 		php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
1070 		/* } */
1071 
1072 		/* temp = digest */
1073 		memcpy(temp, digest, ops->digest_size);
1074 
1075 		/*
1076 		 * Note that the loop starting at 1 is intentional, since we've already done
1077 		 * the first round of the algorithm.
1078 		 */
1079 		for (j = 1; j < iterations; j++) {
1080 			/* digest = hash_hmac(digest, password) { */
1081 			php_hash_hmac_round(digest, ops, context, K1, digest, ops->digest_size);
1082 			php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
1083 			/* } */
1084 			/* temp ^= digest */
1085 			php_hash_string_xor(temp, temp, digest, ops->digest_size);
1086 		}
1087 		/* result += temp */
1088 		memcpy(result + ((i - 1) * ops->digest_size), temp, ops->digest_size);
1089 	}
1090 	/* Zero potentially sensitive variables */
1091 	ZEND_SECURE_ZERO(K1, ops->block_size);
1092 	ZEND_SECURE_ZERO(K2, ops->block_size);
1093 	ZEND_SECURE_ZERO(computed_salt, salt_len + 4);
1094 	efree(K1);
1095 	efree(K2);
1096 	efree(computed_salt);
1097 	efree(context);
1098 	efree(digest);
1099 	efree(temp);
1100 
1101 	returnval = zend_string_alloc(length, 0);
1102 	if (raw_output) {
1103 		memcpy(ZSTR_VAL(returnval), result, length);
1104 	} else {
1105 		php_hash_bin2hex(ZSTR_VAL(returnval), result, digest_length);
1106 	}
1107 	ZSTR_VAL(returnval)[length] = 0;
1108 	efree(result);
1109 	RETURN_NEW_STR(returnval);
1110 }
1111 /* }}} */
1112 
1113 /* {{{ Compares two strings using the same time whether they're equal or not.
1114    A difference in length will leak */
PHP_FUNCTION(hash_equals)1115 PHP_FUNCTION(hash_equals)
1116 {
1117 	zval *known_zval, *user_zval;
1118 	int result = 0;
1119 
1120 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &known_zval, &user_zval) == FAILURE) {
1121 		RETURN_THROWS();
1122 	}
1123 
1124 	/* We only allow comparing string to prevent unexpected results. */
1125 	if (Z_TYPE_P(known_zval) != IS_STRING) {
1126 		zend_argument_type_error(1, "must be of type string, %s given", zend_zval_value_name(known_zval));
1127 		RETURN_THROWS();
1128 	}
1129 
1130 	if (Z_TYPE_P(user_zval) != IS_STRING) {
1131 		zend_argument_type_error(2, "must be of type string, %s given", zend_zval_value_name(user_zval));
1132 		RETURN_THROWS();
1133 	}
1134 
1135 	/* This is security sensitive code. Do not optimize this for speed. */
1136 	result = php_safe_bcmp(Z_STR_P(known_zval), Z_STR_P(user_zval));
1137 
1138 	RETURN_BOOL(0 == result);
1139 }
1140 /* }}} */
1141 
1142 /* {{{ */
PHP_METHOD(HashContext,__construct)1143 PHP_METHOD(HashContext, __construct) {
1144 	/* Normally unreachable as private/final */
1145 	zend_throw_exception(zend_ce_error, "Illegal call to private/final constructor", 0);
1146 }
1147 /* }}} */
1148 
1149 /* Module Housekeeping */
1150 
1151 #define PHP_HASH_HAVAL_REGISTER(p,b)	php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
1152 
1153 #ifdef PHP_MHASH_BC
1154 
1155 #if 0
1156 /* See #69823, we should not insert module into module_registry while doing startup */
1157 
1158 PHP_MINFO_FUNCTION(mhash)
1159 {
1160 	php_info_print_table_start();
1161 	php_info_print_table_row(2, "MHASH support", "Enabled");
1162 	php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
1163 	php_info_print_table_end();
1164 }
1165 
1166 zend_module_entry mhash_module_entry = {
1167 	STANDARD_MODULE_HEADER,
1168 	"mhash",
1169 	NULL,
1170 	NULL,
1171 	NULL,
1172 	NULL,
1173 	NULL,
1174 	PHP_MINFO(mhash),
1175 	PHP_MHASH_VERSION,
1176 	STANDARD_MODULE_PROPERTIES,
1177 };
1178 #endif
1179 
mhash_init(INIT_FUNC_ARGS)1180 static void mhash_init(INIT_FUNC_ARGS)
1181 {
1182 	char buf[128];
1183 	int len;
1184 	int algo_number = 0;
1185 
1186 	for (algo_number = 0; algo_number < MHASH_NUM_ALGOS; algo_number++) {
1187 		struct mhash_bc_entry algorithm = mhash_to_hash[algo_number];
1188 		if (algorithm.mhash_name == NULL) {
1189 			continue;
1190 		}
1191 
1192 		len = slprintf(buf, 127, "MHASH_%s", algorithm.mhash_name);
1193 		zend_register_long_constant(buf, len, algorithm.value, CONST_PERSISTENT, module_number);
1194 	}
1195 
1196 	/* TODO: this cause #69823 zend_register_internal_module(&mhash_module_entry); */
1197 }
1198 
1199 /* {{{ Hash data with hash */
PHP_FUNCTION(mhash)1200 PHP_FUNCTION(mhash)
1201 {
1202 	zend_long algorithm;
1203 	zend_string *algo = NULL;
1204 	char *data, *key = NULL;
1205 	size_t data_len, key_len = 0;
1206 
1207 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!", &algorithm, &data, &data_len, &key, &key_len) == FAILURE) {
1208 		RETURN_THROWS();
1209 	}
1210 
1211 	/* need to convert the first parameter from int constant to string algorithm name */
1212 	if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1213 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1214 		if (algorithm_lookup.hash_name) {
1215 			algo = zend_string_init(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name), 0);
1216 		}
1217 	}
1218 
1219 	if (key) {
1220 		php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, 1, 0);
1221 	} else {
1222 		php_hash_do_hash(return_value, algo, data, data_len, 1, 0, NULL);
1223 	}
1224 
1225 	if (algo) {
1226 		zend_string_release(algo);
1227 	}
1228 }
1229 /* }}} */
1230 
1231 /* {{{ Gets the name of hash */
PHP_FUNCTION(mhash_get_hash_name)1232 PHP_FUNCTION(mhash_get_hash_name)
1233 {
1234 	zend_long algorithm;
1235 
1236 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
1237 		RETURN_THROWS();
1238 	}
1239 
1240 	if (algorithm >= 0 && algorithm  < MHASH_NUM_ALGOS) {
1241 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1242 		if (algorithm_lookup.mhash_name) {
1243 			RETURN_STRING(algorithm_lookup.mhash_name);
1244 		}
1245 	}
1246 	RETURN_FALSE;
1247 }
1248 /* }}} */
1249 
1250 /* {{{ Gets the number of available hashes */
PHP_FUNCTION(mhash_count)1251 PHP_FUNCTION(mhash_count)
1252 {
1253 	if (zend_parse_parameters_none() == FAILURE) {
1254 		RETURN_THROWS();
1255 	}
1256 	RETURN_LONG(MHASH_NUM_ALGOS - 1);
1257 }
1258 /* }}} */
1259 
1260 /* {{{ Gets the block size of hash */
PHP_FUNCTION(mhash_get_block_size)1261 PHP_FUNCTION(mhash_get_block_size)
1262 {
1263 	zend_long algorithm;
1264 
1265 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
1266 		RETURN_THROWS();
1267 	}
1268 	RETVAL_FALSE;
1269 
1270 	if (algorithm >= 0 && algorithm  < MHASH_NUM_ALGOS) {
1271 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1272 		if (algorithm_lookup.mhash_name) {
1273 			const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1274 			if (ops) {
1275 				RETVAL_LONG(ops->digest_size);
1276 			}
1277 		}
1278 	}
1279 }
1280 /* }}} */
1281 
1282 #define SALT_SIZE 8
1283 
1284 /* {{{ Generates a key using hash functions */
PHP_FUNCTION(mhash_keygen_s2k)1285 PHP_FUNCTION(mhash_keygen_s2k)
1286 {
1287 	zend_long algorithm, l_bytes;
1288 	int bytes;
1289 	char *password, *salt;
1290 	size_t password_len, salt_len;
1291 	char padded_salt[SALT_SIZE];
1292 
1293 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes) == FAILURE) {
1294 		RETURN_THROWS();
1295 	}
1296 
1297 	bytes = (int)l_bytes;
1298 	if (bytes <= 0){
1299 		zend_argument_value_error(4, "must be a greater than 0");
1300 		RETURN_THROWS();
1301 	}
1302 
1303 	salt_len = MIN(salt_len, SALT_SIZE);
1304 
1305 	memcpy(padded_salt, salt, salt_len);
1306 	if (salt_len < SALT_SIZE) {
1307 		memset(padded_salt + salt_len, 0, SALT_SIZE - salt_len);
1308 	}
1309 	salt_len = SALT_SIZE;
1310 
1311 	RETVAL_FALSE;
1312 	if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1313 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1314 		if (algorithm_lookup.mhash_name) {
1315 			const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1316 			if (ops) {
1317 				unsigned char null = '\0';
1318 				void *context;
1319 				char *key, *digest;
1320 				int i = 0, j = 0;
1321 				size_t block_size = ops->digest_size;
1322 				size_t times = bytes / block_size;
1323 
1324 				if ((bytes % block_size) != 0) {
1325 					times++;
1326 				}
1327 
1328 				context = php_hash_alloc_context(ops);
1329 				ops->hash_init(context, NULL);
1330 
1331 				key = ecalloc(1, times * block_size);
1332 				digest = emalloc(ops->digest_size + 1);
1333 
1334 				for (i = 0; i < times; i++) {
1335 					ops->hash_init(context, NULL);
1336 
1337 					for (j=0;j<i;j++) {
1338 						ops->hash_update(context, &null, 1);
1339 					}
1340 					ops->hash_update(context, (unsigned char *)padded_salt, salt_len);
1341 					ops->hash_update(context, (unsigned char *)password, password_len);
1342 					ops->hash_final((unsigned char *)digest, context);
1343 					memcpy( &key[i*block_size], digest, block_size);
1344 				}
1345 
1346 				RETVAL_STRINGL(key, bytes);
1347 				ZEND_SECURE_ZERO(key, bytes);
1348 				efree(digest);
1349 				efree(context);
1350 				efree(key);
1351 			}
1352 		}
1353 	}
1354 }
1355 /* }}} */
1356 
1357 #endif
1358 
1359 /* ----------------------------------------------------------------------- */
1360 
1361 /* {{{ php_hashcontext_create */
php_hashcontext_create(zend_class_entry * ce)1362 static zend_object* php_hashcontext_create(zend_class_entry *ce) {
1363 	php_hashcontext_object *objval = zend_object_alloc(sizeof(php_hashcontext_object), ce);
1364 	zend_object *zobj = &objval->std;
1365 
1366 	zend_object_std_init(zobj, ce);
1367 	object_properties_init(zobj, ce);
1368 	zobj->handlers = &php_hashcontext_handlers;
1369 
1370 	return zobj;
1371 }
1372 /* }}} */
1373 
1374 /* {{{ php_hashcontext_dtor */
php_hashcontext_dtor(zend_object * obj)1375 static void php_hashcontext_dtor(zend_object *obj) {
1376 	php_hashcontext_object *hash = php_hashcontext_from_object(obj);
1377 
1378 	if (hash->context) {
1379 		efree(hash->context);
1380 		hash->context = NULL;
1381 	}
1382 
1383 	if (hash->key) {
1384 		ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
1385 		efree(hash->key);
1386 		hash->key = NULL;
1387 	}
1388 }
1389 /* }}} */
1390 
php_hashcontext_free(zend_object * obj)1391 static void php_hashcontext_free(zend_object *obj) {
1392 	php_hashcontext_dtor(obj);
1393 	zend_object_std_dtor(obj);
1394 }
1395 
1396 /* {{{ php_hashcontext_clone */
php_hashcontext_clone(zend_object * zobj)1397 static zend_object *php_hashcontext_clone(zend_object *zobj) {
1398 	php_hashcontext_object *oldobj = php_hashcontext_from_object(zobj);
1399 	zend_object *znew = php_hashcontext_create(zobj->ce);
1400 	php_hashcontext_object *newobj = php_hashcontext_from_object(znew);
1401 
1402 	if (!oldobj->context) {
1403 		zend_throw_exception(zend_ce_value_error, "Cannot clone a finalized HashContext", 0);
1404 		return znew;
1405 	}
1406 
1407 	zend_objects_clone_members(znew, zobj);
1408 
1409 	newobj->ops = oldobj->ops;
1410 	newobj->options = oldobj->options;
1411 	newobj->context = php_hash_alloc_context(newobj->ops);
1412 	newobj->ops->hash_init(newobj->context, NULL);
1413 
1414 	if (SUCCESS != newobj->ops->hash_copy(newobj->ops, oldobj->context, newobj->context)) {
1415 		efree(newobj->context);
1416 		newobj->context = NULL;
1417 		return znew;
1418 	}
1419 
1420 	newobj->key = ecalloc(1, newobj->ops->block_size);
1421 	if (oldobj->key) {
1422 		memcpy(newobj->key, oldobj->key, newobj->ops->block_size);
1423 	}
1424 
1425 	return znew;
1426 }
1427 /* }}} */
1428 
1429 /* Serialization format: 5-element array
1430    Index 0: hash algorithm (string)
1431    Index 1: options (long, 0)
1432    Index 2: hash-determined serialization of context state (usually array)
1433    Index 3: magic number defining layout of context state (long, usually 2)
1434    Index 4: properties (array)
1435 
1436    HashContext serializations are not necessarily portable between architectures or
1437    PHP versions. If the format of a serialized hash context changes, that should
1438    be reflected in either a different value of `magic` or a different format of
1439    the serialized context state. Most context states are unparsed and parsed using
1440    a spec string, such as "llb128.", using the format defined by
1441    `php_hash_serialize_spec`/`php_hash_unserialize_spec`. Some hash algorithms must
1442    also check the unserialized state for validity, to ensure that using an
1443    unserialized context is safe from memory errors.
1444 
1445    Currently HASH_HMAC contexts cannot be serialized, because serializing them
1446    would require serializing the HMAC key in plaintext. */
1447 
1448 /* {{{ Serialize the object */
PHP_METHOD(HashContext,__serialize)1449 PHP_METHOD(HashContext, __serialize)
1450 {
1451 	zval *object = ZEND_THIS;
1452 	php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
1453 	zend_long magic = 0;
1454 	zval tmp;
1455 
1456 	if (zend_parse_parameters_none() == FAILURE) {
1457 		RETURN_THROWS();
1458 	}
1459 
1460 	array_init(return_value);
1461 
1462 	if (!hash->ops->hash_serialize) {
1463 		goto serialize_failure;
1464 	} else if (hash->options & PHP_HASH_HMAC) {
1465 		zend_throw_exception(NULL, "HashContext with HASH_HMAC option cannot be serialized", 0);
1466 		RETURN_THROWS();
1467 	}
1468 
1469 	ZVAL_STRING(&tmp, hash->ops->algo);
1470 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1471 
1472 	ZVAL_LONG(&tmp, hash->options);
1473 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1474 
1475 	if (hash->ops->hash_serialize(hash, &magic, &tmp) != SUCCESS) {
1476 		goto serialize_failure;
1477 	}
1478 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1479 
1480 	ZVAL_LONG(&tmp, magic);
1481 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1482 
1483 	/* members */
1484 	ZVAL_ARR(&tmp, zend_std_get_properties(&hash->std));
1485 	Z_TRY_ADDREF(tmp);
1486 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1487 
1488 	return;
1489 
1490 serialize_failure:
1491 	zend_throw_exception_ex(NULL, 0, "HashContext for algorithm \"%s\" cannot be serialized", hash->ops->algo);
1492 	RETURN_THROWS();
1493 }
1494 /* }}} */
1495 
1496 /* {{{ unserialize the object */
PHP_METHOD(HashContext,__unserialize)1497 PHP_METHOD(HashContext, __unserialize)
1498 {
1499 	zval *object = ZEND_THIS;
1500 	php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
1501 	HashTable *data;
1502 	zval *algo_zv, *magic_zv, *options_zv, *hash_zv, *members_zv;
1503 	zend_long magic, options;
1504 	int unserialize_result;
1505 	const php_hash_ops *ops;
1506 
1507 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1508 		RETURN_THROWS();
1509 	}
1510 
1511 	if (hash->context) {
1512 		zend_throw_exception(NULL, "HashContext::__unserialize called on initialized object", 0);
1513 		RETURN_THROWS();
1514 	}
1515 
1516 	algo_zv = zend_hash_index_find(data, 0);
1517 	options_zv = zend_hash_index_find(data, 1);
1518 	hash_zv = zend_hash_index_find(data, 2);
1519 	magic_zv = zend_hash_index_find(data, 3);
1520 	members_zv = zend_hash_index_find(data, 4);
1521 
1522 	if (!algo_zv || Z_TYPE_P(algo_zv) != IS_STRING
1523 		|| !magic_zv || Z_TYPE_P(magic_zv) != IS_LONG
1524 		|| !options_zv || Z_TYPE_P(options_zv) != IS_LONG
1525 		|| !hash_zv
1526 		|| !members_zv || Z_TYPE_P(members_zv) != IS_ARRAY) {
1527 		zend_throw_exception(NULL, "Incomplete or ill-formed serialization data", 0);
1528 		RETURN_THROWS();
1529 	}
1530 
1531 	magic = Z_LVAL_P(magic_zv);
1532 	options = Z_LVAL_P(options_zv);
1533 	if (options & PHP_HASH_HMAC) {
1534 		zend_throw_exception(NULL, "HashContext with HASH_HMAC option cannot be serialized", 0);
1535 		RETURN_THROWS();
1536 	}
1537 
1538 	ops = php_hash_fetch_ops(Z_STR_P(algo_zv));
1539 	if (!ops) {
1540 		zend_throw_exception(NULL, "Unknown hash algorithm", 0);
1541 		RETURN_THROWS();
1542 	} else if (!ops->hash_unserialize) {
1543 		zend_throw_exception_ex(NULL, 0, "Hash algorithm \"%s\" cannot be unserialized", ops->algo);
1544 		RETURN_THROWS();
1545 	}
1546 
1547 	hash->ops = ops;
1548 	hash->context = php_hash_alloc_context(ops);
1549 	hash->options = options;
1550 	ops->hash_init(hash->context, NULL);
1551 
1552 	unserialize_result = ops->hash_unserialize(hash, magic, hash_zv);
1553 	if (unserialize_result != SUCCESS) {
1554 		zend_throw_exception_ex(NULL, 0, "Incomplete or ill-formed serialization data (\"%s\" code %d)", ops->algo, unserialize_result);
1555 		/* free context */
1556 		php_hashcontext_dtor(Z_OBJ_P(object));
1557 		RETURN_THROWS();
1558 	}
1559 
1560 	object_properties_load(&hash->std, Z_ARRVAL_P(members_zv));
1561 }
1562 /* }}} */
1563 
1564 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(hash)1565 PHP_MINIT_FUNCTION(hash)
1566 {
1567 	zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
1568 
1569 	php_hash_register_algo("md2",			&php_hash_md2_ops);
1570 	php_hash_register_algo("md4",			&php_hash_md4_ops);
1571 	php_hash_register_algo("md5",			&php_hash_md5_ops);
1572 	php_hash_register_algo("sha1",			&php_hash_sha1_ops);
1573 	php_hash_register_algo("sha224",		&php_hash_sha224_ops);
1574 	php_hash_register_algo("sha256",		&php_hash_sha256_ops);
1575 	php_hash_register_algo("sha384",		&php_hash_sha384_ops);
1576 	php_hash_register_algo("sha512/224",            &php_hash_sha512_224_ops);
1577 	php_hash_register_algo("sha512/256",            &php_hash_sha512_256_ops);
1578 	php_hash_register_algo("sha512",		&php_hash_sha512_ops);
1579 	php_hash_register_algo("sha3-224",		&php_hash_sha3_224_ops);
1580 	php_hash_register_algo("sha3-256",		&php_hash_sha3_256_ops);
1581 	php_hash_register_algo("sha3-384",		&php_hash_sha3_384_ops);
1582 	php_hash_register_algo("sha3-512",		&php_hash_sha3_512_ops);
1583 	php_hash_register_algo("ripemd128",		&php_hash_ripemd128_ops);
1584 	php_hash_register_algo("ripemd160",		&php_hash_ripemd160_ops);
1585 	php_hash_register_algo("ripemd256",		&php_hash_ripemd256_ops);
1586 	php_hash_register_algo("ripemd320",		&php_hash_ripemd320_ops);
1587 	php_hash_register_algo("whirlpool",		&php_hash_whirlpool_ops);
1588 	php_hash_register_algo("tiger128,3",	&php_hash_3tiger128_ops);
1589 	php_hash_register_algo("tiger160,3",	&php_hash_3tiger160_ops);
1590 	php_hash_register_algo("tiger192,3",	&php_hash_3tiger192_ops);
1591 	php_hash_register_algo("tiger128,4",	&php_hash_4tiger128_ops);
1592 	php_hash_register_algo("tiger160,4",	&php_hash_4tiger160_ops);
1593 	php_hash_register_algo("tiger192,4",	&php_hash_4tiger192_ops);
1594 	php_hash_register_algo("snefru",		&php_hash_snefru_ops);
1595 	php_hash_register_algo("snefru256",		&php_hash_snefru_ops);
1596 	php_hash_register_algo("gost",			&php_hash_gost_ops);
1597 	php_hash_register_algo("gost-crypto",		&php_hash_gost_crypto_ops);
1598 	php_hash_register_algo("adler32",		&php_hash_adler32_ops);
1599 	php_hash_register_algo("crc32",			&php_hash_crc32_ops);
1600 	php_hash_register_algo("crc32b",		&php_hash_crc32b_ops);
1601 	php_hash_register_algo("crc32c",		&php_hash_crc32c_ops);
1602 	php_hash_register_algo("fnv132",		&php_hash_fnv132_ops);
1603 	php_hash_register_algo("fnv1a32",		&php_hash_fnv1a32_ops);
1604 	php_hash_register_algo("fnv164",		&php_hash_fnv164_ops);
1605 	php_hash_register_algo("fnv1a64",		&php_hash_fnv1a64_ops);
1606 	php_hash_register_algo("joaat",			&php_hash_joaat_ops);
1607 	php_hash_register_algo("murmur3a",		&php_hash_murmur3a_ops);
1608 	php_hash_register_algo("murmur3c",		&php_hash_murmur3c_ops);
1609 	php_hash_register_algo("murmur3f",		&php_hash_murmur3f_ops);
1610 	php_hash_register_algo("xxh32",		&php_hash_xxh32_ops);
1611 	php_hash_register_algo("xxh64",		&php_hash_xxh64_ops);
1612 	php_hash_register_algo("xxh3",		&php_hash_xxh3_64_ops);
1613 	php_hash_register_algo("xxh128",		&php_hash_xxh3_128_ops);
1614 
1615 	PHP_HASH_HAVAL_REGISTER(3,128);
1616 	PHP_HASH_HAVAL_REGISTER(3,160);
1617 	PHP_HASH_HAVAL_REGISTER(3,192);
1618 	PHP_HASH_HAVAL_REGISTER(3,224);
1619 	PHP_HASH_HAVAL_REGISTER(3,256);
1620 
1621 	PHP_HASH_HAVAL_REGISTER(4,128);
1622 	PHP_HASH_HAVAL_REGISTER(4,160);
1623 	PHP_HASH_HAVAL_REGISTER(4,192);
1624 	PHP_HASH_HAVAL_REGISTER(4,224);
1625 	PHP_HASH_HAVAL_REGISTER(4,256);
1626 
1627 	PHP_HASH_HAVAL_REGISTER(5,128);
1628 	PHP_HASH_HAVAL_REGISTER(5,160);
1629 	PHP_HASH_HAVAL_REGISTER(5,192);
1630 	PHP_HASH_HAVAL_REGISTER(5,224);
1631 	PHP_HASH_HAVAL_REGISTER(5,256);
1632 
1633 	register_hash_symbols(module_number);
1634 
1635 	php_hashcontext_ce = register_class_HashContext();
1636 	php_hashcontext_ce->create_object = php_hashcontext_create;
1637 
1638 	memcpy(&php_hashcontext_handlers, &std_object_handlers,
1639 	       sizeof(zend_object_handlers));
1640 	php_hashcontext_handlers.offset = XtOffsetOf(php_hashcontext_object, std);
1641 	php_hashcontext_handlers.free_obj = php_hashcontext_free;
1642 	php_hashcontext_handlers.clone_obj = php_hashcontext_clone;
1643 
1644 #ifdef PHP_MHASH_BC
1645 	mhash_init(INIT_FUNC_ARGS_PASSTHRU);
1646 #endif
1647 
1648 	return SUCCESS;
1649 }
1650 /* }}} */
1651 
1652 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(hash)1653 PHP_MSHUTDOWN_FUNCTION(hash)
1654 {
1655 	zend_hash_destroy(&php_hash_hashtable);
1656 
1657 	return SUCCESS;
1658 }
1659 /* }}} */
1660 
1661 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(hash)1662 PHP_MINFO_FUNCTION(hash)
1663 {
1664 	char buffer[2048];
1665 	zend_string *str;
1666 	char *s = buffer, *e = s + sizeof(buffer);
1667 
1668 	ZEND_HASH_MAP_FOREACH_STR_KEY(&php_hash_hashtable, str) {
1669 		s += slprintf(s, e - s, "%s ", ZSTR_VAL(str));
1670 	} ZEND_HASH_FOREACH_END();
1671 	*s = 0;
1672 
1673 	php_info_print_table_start();
1674 	php_info_print_table_row(2, "hash support", "enabled");
1675 	php_info_print_table_row(2, "Hashing Engines", buffer);
1676 	php_info_print_table_end();
1677 
1678 #ifdef PHP_MHASH_BC
1679 	php_info_print_table_start();
1680 	php_info_print_table_row(2, "MHASH support", "Enabled");
1681 	php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
1682 	php_info_print_table_end();
1683 #endif
1684 
1685 }
1686 /* }}} */
1687 
1688 /* {{{ hash_module_entry */
1689 zend_module_entry hash_module_entry = {
1690 	STANDARD_MODULE_HEADER,
1691 	PHP_HASH_EXTNAME,
1692 	ext_functions,
1693 	PHP_MINIT(hash),
1694 	PHP_MSHUTDOWN(hash),
1695 	NULL, /* RINIT */
1696 	NULL, /* RSHUTDOWN */
1697 	PHP_MINFO(hash),
1698 	PHP_HASH_VERSION,
1699 	STANDARD_MODULE_PROPERTIES
1700 };
1701 /* }}} */
1702