xref: /php-src/ext/hash/hash.c (revision 77ee92a5)
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, 3)
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 Hash Context resource"); \
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 
842 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zhash, php_hashcontext_ce) == FAILURE) {
843 		RETURN_THROWS();
844 	}
845 
846 	RETVAL_OBJ(Z_OBJ_HANDLER_P(zhash, clone_obj)(Z_OBJ_P(zhash)));
847 
848 	if (php_hashcontext_from_object(Z_OBJ_P(return_value))->context == NULL) {
849 		zval_ptr_dtor(return_value);
850 
851 		zend_throw_error(NULL, "Cannot copy hash");
852 		RETURN_THROWS();
853 	}
854 }
855 /* }}} */
856 
857 /* {{{ Return a list of registered hashing algorithms */
PHP_FUNCTION(hash_algos)858 PHP_FUNCTION(hash_algos)
859 {
860 	zend_string *str;
861 
862 	if (zend_parse_parameters_none() == FAILURE) {
863 		RETURN_THROWS();
864 	}
865 
866 	array_init(return_value);
867 	ZEND_HASH_MAP_FOREACH_STR_KEY(&php_hash_hashtable, str) {
868 		add_next_index_str(return_value, zend_string_copy(str));
869 	} ZEND_HASH_FOREACH_END();
870 }
871 /* }}} */
872 
873 /* {{{ Return a list of registered hashing algorithms suitable for hash_hmac() */
PHP_FUNCTION(hash_hmac_algos)874 PHP_FUNCTION(hash_hmac_algos)
875 {
876 	zend_string *str;
877 	const php_hash_ops *ops;
878 
879 	if (zend_parse_parameters_none() == FAILURE) {
880 		RETURN_THROWS();
881 	}
882 
883 	array_init(return_value);
884 	ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&php_hash_hashtable, str, ops) {
885 		if (ops->is_crypto) {
886 			add_next_index_str(return_value, zend_string_copy(str));
887 		}
888 	} ZEND_HASH_FOREACH_END();
889 }
890 /* }}} */
891 
892 /* {{{ RFC5869 HMAC-based key derivation function */
PHP_FUNCTION(hash_hkdf)893 PHP_FUNCTION(hash_hkdf)
894 {
895 	zend_string *returnval, *ikm, *algo, *info = NULL, *salt = NULL;
896 	zend_long length = 0;
897 	unsigned char *prk, *digest, *K;
898 	size_t i;
899 	size_t rounds;
900 	const php_hash_ops *ops;
901 	void *context;
902 
903 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lSS", &algo, &ikm, &length, &info, &salt) == FAILURE) {
904 		RETURN_THROWS();
905 	}
906 
907 	ops = php_hash_fetch_ops(algo);
908 	if (!ops || !ops->is_crypto) {
909 		zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
910 		RETURN_THROWS();
911 	}
912 
913 	if (ZSTR_LEN(ikm) == 0) {
914 		zend_argument_value_error(2, "cannot be empty");
915 		RETURN_THROWS();
916 	}
917 
918 	if (length < 0) {
919 		zend_argument_value_error(3, "must be greater than or equal to 0");
920 		RETURN_THROWS();
921 	} else if (length == 0) {
922 		length = ops->digest_size;
923 	} else if (length > (zend_long) (ops->digest_size * 255)) {
924 		zend_argument_value_error(3, "must be less than or equal to %zd", ops->digest_size * 255);
925 		RETURN_THROWS();
926 	}
927 
928 	context = php_hash_alloc_context(ops);
929 
930 	// Extract
931 	ops->hash_init(context, NULL);
932 	K = emalloc(ops->block_size);
933 	php_hash_hmac_prep_key(K, ops, context,
934 		(unsigned char *) (salt ? ZSTR_VAL(salt) : ""), salt ? ZSTR_LEN(salt) : 0);
935 
936 	prk = emalloc(ops->digest_size);
937 	php_hash_hmac_round(prk, ops, context, K, (unsigned char *) ZSTR_VAL(ikm), ZSTR_LEN(ikm));
938 	php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
939 	php_hash_hmac_round(prk, ops, context, K, prk, ops->digest_size);
940 	ZEND_SECURE_ZERO(K, ops->block_size);
941 
942 	// Expand
943 	returnval = zend_string_alloc(length, 0);
944 	digest = emalloc(ops->digest_size);
945 	for (i = 1, rounds = (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
946 		// chr(i)
947 		unsigned char c[1];
948 		c[0] = (i & 0xFF);
949 
950 		php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size);
951 		ops->hash_init(context, NULL);
952 		ops->hash_update(context, K, ops->block_size);
953 
954 		if (i > 1) {
955 			ops->hash_update(context, digest, ops->digest_size);
956 		}
957 
958 		if (info != NULL && ZSTR_LEN(info) > 0) {
959 			ops->hash_update(context, (unsigned char *) ZSTR_VAL(info), ZSTR_LEN(info));
960 		}
961 
962 		ops->hash_update(context, c, 1);
963 		ops->hash_final(digest, context);
964 		php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
965 		php_hash_hmac_round(digest, ops, context, K, digest, ops->digest_size);
966 		memcpy(
967 			ZSTR_VAL(returnval) + ((i - 1) * ops->digest_size),
968 			digest,
969 			(i == rounds ? length - ((i - 1) * ops->digest_size) : ops->digest_size)
970 		);
971 	}
972 
973 	ZEND_SECURE_ZERO(K, ops->block_size);
974 	ZEND_SECURE_ZERO(digest, ops->digest_size);
975 	ZEND_SECURE_ZERO(prk, ops->digest_size);
976 	efree(K);
977 	efree(context);
978 	efree(prk);
979 	efree(digest);
980 	ZSTR_VAL(returnval)[length] = 0;
981 	RETURN_STR(returnval);
982 }
983 
984 /* {{{ Generate a PBKDF2 hash of the given password and salt
985 Returns lowercase hexits by default */
PHP_FUNCTION(hash_pbkdf2)986 PHP_FUNCTION(hash_pbkdf2)
987 {
988 	zend_string *returnval, *algo;
989 	char *salt, *pass = NULL;
990 	unsigned char *computed_salt, *digest, *temp, *result, *K1, *K2 = NULL;
991 	zend_long loops, i, j, iterations, digest_length = 0, length = 0;
992 	size_t pass_len, salt_len = 0;
993 	bool raw_output = 0;
994 	const php_hash_ops *ops;
995 	void *context;
996 	HashTable *args;
997 
998 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lbh", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output, &args) == FAILURE) {
999 		RETURN_THROWS();
1000 	}
1001 
1002 	ops = php_hash_fetch_ops(algo);
1003 	if (!ops || !ops->is_crypto) {
1004 		zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
1005 		RETURN_THROWS();
1006 	}
1007 
1008 	if (salt_len > INT_MAX - 4) {
1009 		zend_argument_value_error(3, "must be less than or equal to INT_MAX - 4 bytes");
1010 		RETURN_THROWS();
1011 	}
1012 
1013 	if (iterations <= 0) {
1014 		zend_argument_value_error(4, "must be greater than 0");
1015 		RETURN_THROWS();
1016 	}
1017 
1018 	if (length < 0) {
1019 		zend_argument_value_error(5, "must be greater than or equal to 0");
1020 		RETURN_THROWS();
1021 	}
1022 
1023 	context = php_hash_alloc_context(ops);
1024 	ops->hash_init(context, args);
1025 
1026 	K1 = emalloc(ops->block_size);
1027 	K2 = emalloc(ops->block_size);
1028 	digest = emalloc(ops->digest_size);
1029 	temp = emalloc(ops->digest_size);
1030 
1031 	/* Setup Keys that will be used for all hmac rounds */
1032 	php_hash_hmac_prep_key(K1, ops, context, (unsigned char *) pass, pass_len);
1033 	/* Convert K1 to opad -- 0x6A = 0x36 ^ 0x5C */
1034 	php_hash_string_xor_char(K2, K1, 0x6A, ops->block_size);
1035 
1036 	/* Setup Main Loop to build a long enough result */
1037 	if (length == 0) {
1038 		length = ops->digest_size;
1039 		if (!raw_output) {
1040 			length = length * 2;
1041 		}
1042 	}
1043 	digest_length = length;
1044 	if (!raw_output) {
1045 		digest_length = (zend_long) ceil((float) length / 2.0);
1046 	}
1047 
1048 	loops = (zend_long) ceil((float) digest_length / (float) ops->digest_size);
1049 
1050 	result = safe_emalloc(loops, ops->digest_size, 0);
1051 
1052 	computed_salt = safe_emalloc(salt_len, 1, 4);
1053 	memcpy(computed_salt, (unsigned char *) salt, salt_len);
1054 
1055 	for (i = 1; i <= loops; i++) {
1056 		/* digest = hash_hmac(salt + pack('N', i), password) { */
1057 
1058 		/* pack("N", i) */
1059 		computed_salt[salt_len] = (unsigned char) (i >> 24);
1060 		computed_salt[salt_len + 1] = (unsigned char) ((i & 0xFF0000) >> 16);
1061 		computed_salt[salt_len + 2] = (unsigned char) ((i & 0xFF00) >> 8);
1062 		computed_salt[salt_len + 3] = (unsigned char) (i & 0xFF);
1063 
1064 		php_hash_hmac_round(digest, ops, context, K1, computed_salt, (zend_long) salt_len + 4);
1065 		php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
1066 		/* } */
1067 
1068 		/* temp = digest */
1069 		memcpy(temp, digest, ops->digest_size);
1070 
1071 		/*
1072 		 * Note that the loop starting at 1 is intentional, since we've already done
1073 		 * the first round of the algorithm.
1074 		 */
1075 		for (j = 1; j < iterations; j++) {
1076 			/* digest = hash_hmac(digest, password) { */
1077 			php_hash_hmac_round(digest, ops, context, K1, digest, ops->digest_size);
1078 			php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
1079 			/* } */
1080 			/* temp ^= digest */
1081 			php_hash_string_xor(temp, temp, digest, ops->digest_size);
1082 		}
1083 		/* result += temp */
1084 		memcpy(result + ((i - 1) * ops->digest_size), temp, ops->digest_size);
1085 	}
1086 	/* Zero potentially sensitive variables */
1087 	ZEND_SECURE_ZERO(K1, ops->block_size);
1088 	ZEND_SECURE_ZERO(K2, ops->block_size);
1089 	ZEND_SECURE_ZERO(computed_salt, salt_len + 4);
1090 	efree(K1);
1091 	efree(K2);
1092 	efree(computed_salt);
1093 	efree(context);
1094 	efree(digest);
1095 	efree(temp);
1096 
1097 	returnval = zend_string_alloc(length, 0);
1098 	if (raw_output) {
1099 		memcpy(ZSTR_VAL(returnval), result, length);
1100 	} else {
1101 		php_hash_bin2hex(ZSTR_VAL(returnval), result, digest_length);
1102 	}
1103 	ZSTR_VAL(returnval)[length] = 0;
1104 	efree(result);
1105 	RETURN_NEW_STR(returnval);
1106 }
1107 /* }}} */
1108 
1109 /* {{{ Compares two strings using the same time whether they're equal or not.
1110    A difference in length will leak */
PHP_FUNCTION(hash_equals)1111 PHP_FUNCTION(hash_equals)
1112 {
1113 	zval *known_zval, *user_zval;
1114 	int result = 0;
1115 
1116 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &known_zval, &user_zval) == FAILURE) {
1117 		RETURN_THROWS();
1118 	}
1119 
1120 	/* We only allow comparing string to prevent unexpected results. */
1121 	if (Z_TYPE_P(known_zval) != IS_STRING) {
1122 		zend_argument_type_error(1, "must be of type string, %s given", zend_zval_type_name(known_zval));
1123 		RETURN_THROWS();
1124 	}
1125 
1126 	if (Z_TYPE_P(user_zval) != IS_STRING) {
1127 		zend_argument_type_error(2, "must be of type string, %s given", zend_zval_type_name(user_zval));
1128 		RETURN_THROWS();
1129 	}
1130 
1131 	/* This is security sensitive code. Do not optimize this for speed. */
1132 	result = php_safe_bcmp(Z_STR_P(known_zval), Z_STR_P(user_zval));
1133 
1134 	RETURN_BOOL(0 == result);
1135 }
1136 /* }}} */
1137 
1138 /* {{{ */
PHP_METHOD(HashContext,__construct)1139 PHP_METHOD(HashContext, __construct) {
1140 	/* Normally unreachable as private/final */
1141 	zend_throw_exception(zend_ce_error, "Illegal call to private/final constructor", 0);
1142 }
1143 /* }}} */
1144 
1145 /* Module Housekeeping */
1146 
1147 #define PHP_HASH_HAVAL_REGISTER(p,b)	php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
1148 
1149 #ifdef PHP_MHASH_BC
1150 
1151 #if 0
1152 /* See #69823, we should not insert module into module_registry while doing startup */
1153 
1154 PHP_MINFO_FUNCTION(mhash)
1155 {
1156 	php_info_print_table_start();
1157 	php_info_print_table_row(2, "MHASH support", "Enabled");
1158 	php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
1159 	php_info_print_table_end();
1160 }
1161 
1162 zend_module_entry mhash_module_entry = {
1163 	STANDARD_MODULE_HEADER,
1164 	"mhash",
1165 	NULL,
1166 	NULL,
1167 	NULL,
1168 	NULL,
1169 	NULL,
1170 	PHP_MINFO(mhash),
1171 	PHP_MHASH_VERSION,
1172 	STANDARD_MODULE_PROPERTIES,
1173 };
1174 #endif
1175 
mhash_init(INIT_FUNC_ARGS)1176 static void mhash_init(INIT_FUNC_ARGS)
1177 {
1178 	char buf[128];
1179 	int len;
1180 	int algo_number = 0;
1181 
1182 	for (algo_number = 0; algo_number < MHASH_NUM_ALGOS; algo_number++) {
1183 		struct mhash_bc_entry algorithm = mhash_to_hash[algo_number];
1184 		if (algorithm.mhash_name == NULL) {
1185 			continue;
1186 		}
1187 
1188 		len = slprintf(buf, 127, "MHASH_%s", algorithm.mhash_name);
1189 		zend_register_long_constant(buf, len, algorithm.value, CONST_PERSISTENT, module_number);
1190 	}
1191 
1192 	/* TODO: this cause #69823 zend_register_internal_module(&mhash_module_entry); */
1193 }
1194 
1195 /* {{{ Hash data with hash */
PHP_FUNCTION(mhash)1196 PHP_FUNCTION(mhash)
1197 {
1198 	zend_long algorithm;
1199 	zend_string *algo = NULL;
1200 	char *data, *key = NULL;
1201 	size_t data_len, key_len = 0;
1202 
1203 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!", &algorithm, &data, &data_len, &key, &key_len) == FAILURE) {
1204 		RETURN_THROWS();
1205 	}
1206 
1207 	/* need to convert the first parameter from int constant to string algorithm name */
1208 	if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1209 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1210 		if (algorithm_lookup.hash_name) {
1211 			algo = zend_string_init(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name), 0);
1212 		}
1213 	}
1214 
1215 	if (key) {
1216 		php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, 1, 0);
1217 	} else {
1218 		php_hash_do_hash(return_value, algo, data, data_len, 1, 0, NULL);
1219 	}
1220 
1221 	if (algo) {
1222 		zend_string_release(algo);
1223 	}
1224 }
1225 /* }}} */
1226 
1227 /* {{{ Gets the name of hash */
PHP_FUNCTION(mhash_get_hash_name)1228 PHP_FUNCTION(mhash_get_hash_name)
1229 {
1230 	zend_long algorithm;
1231 
1232 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
1233 		RETURN_THROWS();
1234 	}
1235 
1236 	if (algorithm >= 0 && algorithm  < MHASH_NUM_ALGOS) {
1237 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1238 		if (algorithm_lookup.mhash_name) {
1239 			RETURN_STRING(algorithm_lookup.mhash_name);
1240 		}
1241 	}
1242 	RETURN_FALSE;
1243 }
1244 /* }}} */
1245 
1246 /* {{{ Gets the number of available hashes */
PHP_FUNCTION(mhash_count)1247 PHP_FUNCTION(mhash_count)
1248 {
1249 	if (zend_parse_parameters_none() == FAILURE) {
1250 		RETURN_THROWS();
1251 	}
1252 	RETURN_LONG(MHASH_NUM_ALGOS - 1);
1253 }
1254 /* }}} */
1255 
1256 /* {{{ Gets the block size of hash */
PHP_FUNCTION(mhash_get_block_size)1257 PHP_FUNCTION(mhash_get_block_size)
1258 {
1259 	zend_long algorithm;
1260 
1261 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
1262 		RETURN_THROWS();
1263 	}
1264 	RETVAL_FALSE;
1265 
1266 	if (algorithm >= 0 && algorithm  < MHASH_NUM_ALGOS) {
1267 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1268 		if (algorithm_lookup.mhash_name) {
1269 			const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1270 			if (ops) {
1271 				RETVAL_LONG(ops->digest_size);
1272 			}
1273 		}
1274 	}
1275 }
1276 /* }}} */
1277 
1278 #define SALT_SIZE 8
1279 
1280 /* {{{ Generates a key using hash functions */
PHP_FUNCTION(mhash_keygen_s2k)1281 PHP_FUNCTION(mhash_keygen_s2k)
1282 {
1283 	zend_long algorithm, l_bytes;
1284 	int bytes;
1285 	char *password, *salt;
1286 	size_t password_len, salt_len;
1287 	char padded_salt[SALT_SIZE];
1288 
1289 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes) == FAILURE) {
1290 		RETURN_THROWS();
1291 	}
1292 
1293 	bytes = (int)l_bytes;
1294 	if (bytes <= 0){
1295 		zend_argument_value_error(4, "must be a greater than 0");
1296 		RETURN_THROWS();
1297 	}
1298 
1299 	salt_len = MIN(salt_len, SALT_SIZE);
1300 
1301 	memcpy(padded_salt, salt, salt_len);
1302 	if (salt_len < SALT_SIZE) {
1303 		memset(padded_salt + salt_len, 0, SALT_SIZE - salt_len);
1304 	}
1305 	salt_len = SALT_SIZE;
1306 
1307 	RETVAL_FALSE;
1308 	if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1309 		struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1310 		if (algorithm_lookup.mhash_name) {
1311 			const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1312 			if (ops) {
1313 				unsigned char null = '\0';
1314 				void *context;
1315 				char *key, *digest;
1316 				int i = 0, j = 0;
1317 				size_t block_size = ops->digest_size;
1318 				size_t times = bytes / block_size;
1319 
1320 				if ((bytes % block_size) != 0) {
1321 					times++;
1322 				}
1323 
1324 				context = php_hash_alloc_context(ops);
1325 				ops->hash_init(context, NULL);
1326 
1327 				key = ecalloc(1, times * block_size);
1328 				digest = emalloc(ops->digest_size + 1);
1329 
1330 				for (i = 0; i < times; i++) {
1331 					ops->hash_init(context, NULL);
1332 
1333 					for (j=0;j<i;j++) {
1334 						ops->hash_update(context, &null, 1);
1335 					}
1336 					ops->hash_update(context, (unsigned char *)padded_salt, salt_len);
1337 					ops->hash_update(context, (unsigned char *)password, password_len);
1338 					ops->hash_final((unsigned char *)digest, context);
1339 					memcpy( &key[i*block_size], digest, block_size);
1340 				}
1341 
1342 				RETVAL_STRINGL(key, bytes);
1343 				ZEND_SECURE_ZERO(key, bytes);
1344 				efree(digest);
1345 				efree(context);
1346 				efree(key);
1347 			}
1348 		}
1349 	}
1350 }
1351 /* }}} */
1352 
1353 #endif
1354 
1355 /* ----------------------------------------------------------------------- */
1356 
1357 /* {{{ php_hashcontext_create */
php_hashcontext_create(zend_class_entry * ce)1358 static zend_object* php_hashcontext_create(zend_class_entry *ce) {
1359 	php_hashcontext_object *objval = zend_object_alloc(sizeof(php_hashcontext_object), ce);
1360 	zend_object *zobj = &objval->std;
1361 
1362 	zend_object_std_init(zobj, ce);
1363 	object_properties_init(zobj, ce);
1364 	zobj->handlers = &php_hashcontext_handlers;
1365 
1366 	return zobj;
1367 }
1368 /* }}} */
1369 
1370 /* {{{ php_hashcontext_dtor */
php_hashcontext_dtor(zend_object * obj)1371 static void php_hashcontext_dtor(zend_object *obj) {
1372 	php_hashcontext_object *hash = php_hashcontext_from_object(obj);
1373 
1374 	if (hash->context) {
1375 		efree(hash->context);
1376 		hash->context = NULL;
1377 	}
1378 
1379 	if (hash->key) {
1380 		ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
1381 		efree(hash->key);
1382 		hash->key = NULL;
1383 	}
1384 }
1385 /* }}} */
1386 
php_hashcontext_free(zend_object * obj)1387 static void php_hashcontext_free(zend_object *obj) {
1388 	php_hashcontext_dtor(obj);
1389 	zend_object_std_dtor(obj);
1390 }
1391 
1392 /* {{{ php_hashcontext_clone */
php_hashcontext_clone(zend_object * zobj)1393 static zend_object *php_hashcontext_clone(zend_object *zobj) {
1394 	php_hashcontext_object *oldobj = php_hashcontext_from_object(zobj);
1395 	zend_object *znew = php_hashcontext_create(zobj->ce);
1396 	php_hashcontext_object *newobj = php_hashcontext_from_object(znew);
1397 
1398 	zend_objects_clone_members(znew, zobj);
1399 
1400 	newobj->ops = oldobj->ops;
1401 	newobj->options = oldobj->options;
1402 	newobj->context = php_hash_alloc_context(newobj->ops);
1403 	newobj->ops->hash_init(newobj->context, NULL);
1404 
1405 	if (SUCCESS != newobj->ops->hash_copy(newobj->ops, oldobj->context, newobj->context)) {
1406 		efree(newobj->context);
1407 		newobj->context = NULL;
1408 		return znew;
1409 	}
1410 
1411 	newobj->key = ecalloc(1, newobj->ops->block_size);
1412 	if (oldobj->key) {
1413 		memcpy(newobj->key, oldobj->key, newobj->ops->block_size);
1414 	}
1415 
1416 	return znew;
1417 }
1418 /* }}} */
1419 
1420 /* Serialization format: 5-element array
1421    Index 0: hash algorithm (string)
1422    Index 1: options (long, 0)
1423    Index 2: hash-determined serialization of context state (usually array)
1424    Index 3: magic number defining layout of context state (long, usually 2)
1425    Index 4: properties (array)
1426 
1427    HashContext serializations are not necessarily portable between architectures or
1428    PHP versions. If the format of a serialized hash context changes, that should
1429    be reflected in either a different value of `magic` or a different format of
1430    the serialized context state. Most context states are unparsed and parsed using
1431    a spec string, such as "llb128.", using the format defined by
1432    `php_hash_serialize_spec`/`php_hash_unserialize_spec`. Some hash algorithms must
1433    also check the unserialized state for validity, to ensure that using an
1434    unserialized context is safe from memory errors.
1435 
1436    Currently HASH_HMAC contexts cannot be serialized, because serializing them
1437    would require serializing the HMAC key in plaintext. */
1438 
1439 /* {{{ Serialize the object */
PHP_METHOD(HashContext,__serialize)1440 PHP_METHOD(HashContext, __serialize)
1441 {
1442 	zval *object = ZEND_THIS;
1443 	php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
1444 	zend_long magic = 0;
1445 	zval tmp;
1446 
1447 	if (zend_parse_parameters_none() == FAILURE) {
1448 		RETURN_THROWS();
1449 	}
1450 
1451 	array_init(return_value);
1452 
1453 	if (!hash->ops->hash_serialize) {
1454 		goto serialize_failure;
1455 	} else if (hash->options & PHP_HASH_HMAC) {
1456 		zend_throw_exception(NULL, "HashContext with HASH_HMAC option cannot be serialized", 0);
1457 		RETURN_THROWS();
1458 	}
1459 
1460 	ZVAL_STRING(&tmp, hash->ops->algo);
1461 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1462 
1463 	ZVAL_LONG(&tmp, hash->options);
1464 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1465 
1466 	if (hash->ops->hash_serialize(hash, &magic, &tmp) != SUCCESS) {
1467 		goto serialize_failure;
1468 	}
1469 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1470 
1471 	ZVAL_LONG(&tmp, magic);
1472 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1473 
1474 	/* members */
1475 	ZVAL_ARR(&tmp, zend_std_get_properties(&hash->std));
1476 	Z_TRY_ADDREF(tmp);
1477 	zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1478 
1479 	return;
1480 
1481 serialize_failure:
1482 	zend_throw_exception_ex(NULL, 0, "HashContext for algorithm \"%s\" cannot be serialized", hash->ops->algo);
1483 	RETURN_THROWS();
1484 }
1485 /* }}} */
1486 
1487 /* {{{ unserialize the object */
PHP_METHOD(HashContext,__unserialize)1488 PHP_METHOD(HashContext, __unserialize)
1489 {
1490 	zval *object = ZEND_THIS;
1491 	php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
1492 	HashTable *data;
1493 	zval *algo_zv, *magic_zv, *options_zv, *hash_zv, *members_zv;
1494 	zend_long magic, options;
1495 	int unserialize_result;
1496 	const php_hash_ops *ops;
1497 
1498 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1499 		RETURN_THROWS();
1500 	}
1501 
1502 	if (hash->context) {
1503 		zend_throw_exception(NULL, "HashContext::__unserialize called on initialized object", 0);
1504 		RETURN_THROWS();
1505 	}
1506 
1507 	algo_zv = zend_hash_index_find(data, 0);
1508 	options_zv = zend_hash_index_find(data, 1);
1509 	hash_zv = zend_hash_index_find(data, 2);
1510 	magic_zv = zend_hash_index_find(data, 3);
1511 	members_zv = zend_hash_index_find(data, 4);
1512 
1513 	if (!algo_zv || Z_TYPE_P(algo_zv) != IS_STRING
1514 		|| !magic_zv || Z_TYPE_P(magic_zv) != IS_LONG
1515 		|| !options_zv || Z_TYPE_P(options_zv) != IS_LONG
1516 		|| !hash_zv
1517 		|| !members_zv || Z_TYPE_P(members_zv) != IS_ARRAY) {
1518 		zend_throw_exception(NULL, "Incomplete or ill-formed serialization data", 0);
1519 		RETURN_THROWS();
1520 	}
1521 
1522 	magic = Z_LVAL_P(magic_zv);
1523 	options = Z_LVAL_P(options_zv);
1524 	if (options & PHP_HASH_HMAC) {
1525 		zend_throw_exception(NULL, "HashContext with HASH_HMAC option cannot be serialized", 0);
1526 		RETURN_THROWS();
1527 	}
1528 
1529 	ops = php_hash_fetch_ops(Z_STR_P(algo_zv));
1530 	if (!ops) {
1531 		zend_throw_exception(NULL, "Unknown hash algorithm", 0);
1532 		RETURN_THROWS();
1533 	} else if (!ops->hash_unserialize) {
1534 		zend_throw_exception_ex(NULL, 0, "Hash algorithm \"%s\" cannot be unserialized", ops->algo);
1535 		RETURN_THROWS();
1536 	}
1537 
1538 	hash->ops = ops;
1539 	hash->context = php_hash_alloc_context(ops);
1540 	hash->options = options;
1541 	ops->hash_init(hash->context, NULL);
1542 
1543 	unserialize_result = ops->hash_unserialize(hash, magic, hash_zv);
1544 	if (unserialize_result != SUCCESS) {
1545 		zend_throw_exception_ex(NULL, 0, "Incomplete or ill-formed serialization data (\"%s\" code %d)", ops->algo, unserialize_result);
1546 		/* free context */
1547 		php_hashcontext_dtor(Z_OBJ_P(object));
1548 		RETURN_THROWS();
1549 	}
1550 
1551 	object_properties_load(&hash->std, Z_ARRVAL_P(members_zv));
1552 }
1553 /* }}} */
1554 
1555 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(hash)1556 PHP_MINIT_FUNCTION(hash)
1557 {
1558 	zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
1559 
1560 	php_hash_register_algo("md2",			&php_hash_md2_ops);
1561 	php_hash_register_algo("md4",			&php_hash_md4_ops);
1562 	php_hash_register_algo("md5",			&php_hash_md5_ops);
1563 	php_hash_register_algo("sha1",			&php_hash_sha1_ops);
1564 	php_hash_register_algo("sha224",		&php_hash_sha224_ops);
1565 	php_hash_register_algo("sha256",		&php_hash_sha256_ops);
1566 	php_hash_register_algo("sha384",		&php_hash_sha384_ops);
1567 	php_hash_register_algo("sha512/224",            &php_hash_sha512_224_ops);
1568 	php_hash_register_algo("sha512/256",            &php_hash_sha512_256_ops);
1569 	php_hash_register_algo("sha512",		&php_hash_sha512_ops);
1570 	php_hash_register_algo("sha3-224",		&php_hash_sha3_224_ops);
1571 	php_hash_register_algo("sha3-256",		&php_hash_sha3_256_ops);
1572 	php_hash_register_algo("sha3-384",		&php_hash_sha3_384_ops);
1573 	php_hash_register_algo("sha3-512",		&php_hash_sha3_512_ops);
1574 	php_hash_register_algo("ripemd128",		&php_hash_ripemd128_ops);
1575 	php_hash_register_algo("ripemd160",		&php_hash_ripemd160_ops);
1576 	php_hash_register_algo("ripemd256",		&php_hash_ripemd256_ops);
1577 	php_hash_register_algo("ripemd320",		&php_hash_ripemd320_ops);
1578 	php_hash_register_algo("whirlpool",		&php_hash_whirlpool_ops);
1579 	php_hash_register_algo("tiger128,3",	&php_hash_3tiger128_ops);
1580 	php_hash_register_algo("tiger160,3",	&php_hash_3tiger160_ops);
1581 	php_hash_register_algo("tiger192,3",	&php_hash_3tiger192_ops);
1582 	php_hash_register_algo("tiger128,4",	&php_hash_4tiger128_ops);
1583 	php_hash_register_algo("tiger160,4",	&php_hash_4tiger160_ops);
1584 	php_hash_register_algo("tiger192,4",	&php_hash_4tiger192_ops);
1585 	php_hash_register_algo("snefru",		&php_hash_snefru_ops);
1586 	php_hash_register_algo("snefru256",		&php_hash_snefru_ops);
1587 	php_hash_register_algo("gost",			&php_hash_gost_ops);
1588 	php_hash_register_algo("gost-crypto",		&php_hash_gost_crypto_ops);
1589 	php_hash_register_algo("adler32",		&php_hash_adler32_ops);
1590 	php_hash_register_algo("crc32",			&php_hash_crc32_ops);
1591 	php_hash_register_algo("crc32b",		&php_hash_crc32b_ops);
1592 	php_hash_register_algo("crc32c",		&php_hash_crc32c_ops);
1593 	php_hash_register_algo("fnv132",		&php_hash_fnv132_ops);
1594 	php_hash_register_algo("fnv1a32",		&php_hash_fnv1a32_ops);
1595 	php_hash_register_algo("fnv164",		&php_hash_fnv164_ops);
1596 	php_hash_register_algo("fnv1a64",		&php_hash_fnv1a64_ops);
1597 	php_hash_register_algo("joaat",			&php_hash_joaat_ops);
1598 	php_hash_register_algo("murmur3a",		&php_hash_murmur3a_ops);
1599 	php_hash_register_algo("murmur3c",		&php_hash_murmur3c_ops);
1600 	php_hash_register_algo("murmur3f",		&php_hash_murmur3f_ops);
1601 	php_hash_register_algo("xxh32",		&php_hash_xxh32_ops);
1602 	php_hash_register_algo("xxh64",		&php_hash_xxh64_ops);
1603 	php_hash_register_algo("xxh3",		&php_hash_xxh3_64_ops);
1604 	php_hash_register_algo("xxh128",		&php_hash_xxh3_128_ops);
1605 
1606 	PHP_HASH_HAVAL_REGISTER(3,128);
1607 	PHP_HASH_HAVAL_REGISTER(3,160);
1608 	PHP_HASH_HAVAL_REGISTER(3,192);
1609 	PHP_HASH_HAVAL_REGISTER(3,224);
1610 	PHP_HASH_HAVAL_REGISTER(3,256);
1611 
1612 	PHP_HASH_HAVAL_REGISTER(4,128);
1613 	PHP_HASH_HAVAL_REGISTER(4,160);
1614 	PHP_HASH_HAVAL_REGISTER(4,192);
1615 	PHP_HASH_HAVAL_REGISTER(4,224);
1616 	PHP_HASH_HAVAL_REGISTER(4,256);
1617 
1618 	PHP_HASH_HAVAL_REGISTER(5,128);
1619 	PHP_HASH_HAVAL_REGISTER(5,160);
1620 	PHP_HASH_HAVAL_REGISTER(5,192);
1621 	PHP_HASH_HAVAL_REGISTER(5,224);
1622 	PHP_HASH_HAVAL_REGISTER(5,256);
1623 
1624 	register_hash_symbols(module_number);
1625 
1626 	php_hashcontext_ce = register_class_HashContext();
1627 	php_hashcontext_ce->create_object = php_hashcontext_create;
1628 
1629 	memcpy(&php_hashcontext_handlers, &std_object_handlers,
1630 	       sizeof(zend_object_handlers));
1631 	php_hashcontext_handlers.offset = XtOffsetOf(php_hashcontext_object, std);
1632 	php_hashcontext_handlers.free_obj = php_hashcontext_free;
1633 	php_hashcontext_handlers.clone_obj = php_hashcontext_clone;
1634 
1635 #ifdef PHP_MHASH_BC
1636 	mhash_init(INIT_FUNC_ARGS_PASSTHRU);
1637 #endif
1638 
1639 	return SUCCESS;
1640 }
1641 /* }}} */
1642 
1643 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(hash)1644 PHP_MSHUTDOWN_FUNCTION(hash)
1645 {
1646 	zend_hash_destroy(&php_hash_hashtable);
1647 
1648 	return SUCCESS;
1649 }
1650 /* }}} */
1651 
1652 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(hash)1653 PHP_MINFO_FUNCTION(hash)
1654 {
1655 	char buffer[2048];
1656 	zend_string *str;
1657 	char *s = buffer, *e = s + sizeof(buffer);
1658 
1659 	ZEND_HASH_MAP_FOREACH_STR_KEY(&php_hash_hashtable, str) {
1660 		s += slprintf(s, e - s, "%s ", ZSTR_VAL(str));
1661 	} ZEND_HASH_FOREACH_END();
1662 	*s = 0;
1663 
1664 	php_info_print_table_start();
1665 	php_info_print_table_row(2, "hash support", "enabled");
1666 	php_info_print_table_row(2, "Hashing Engines", buffer);
1667 	php_info_print_table_end();
1668 
1669 #ifdef PHP_MHASH_BC
1670 	php_info_print_table_start();
1671 	php_info_print_table_row(2, "MHASH support", "Enabled");
1672 	php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
1673 	php_info_print_table_end();
1674 #endif
1675 
1676 }
1677 /* }}} */
1678 
1679 /* {{{ hash_module_entry */
1680 zend_module_entry hash_module_entry = {
1681 	STANDARD_MODULE_HEADER,
1682 	PHP_HASH_EXTNAME,
1683 	ext_functions,
1684 	PHP_MINIT(hash),
1685 	PHP_MSHUTDOWN(hash),
1686 	NULL, /* RINIT */
1687 	NULL, /* RSHUTDOWN */
1688 	PHP_MINFO(hash),
1689 	PHP_HASH_VERSION,
1690 	STANDARD_MODULE_PROPERTIES
1691 };
1692 /* }}} */
1693