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