PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/hash - hash.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 443 504 87.9 %
Date: 2016-02-09 Functions: 30 32 93.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 09 Feb 2016 10:48:41 +0000 (2 days ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.