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