]> granicus.if.org Git - php/commitdiff
Hopefully last round of size_t fixes, this makes the remaining php_hash_ops use size_...
authorKalle Sommer Nielsen <kalle@php.net>
Wed, 3 Oct 2018 11:36:05 +0000 (13:36 +0200)
committerKalle Sommer Nielsen <kalle@php.net>
Wed, 3 Oct 2018 11:36:05 +0000 (13:36 +0200)
ext/hash/hash.c
ext/hash/hash_sha3.c
ext/hash/php_hash.h

index e92185c4d56ea9878738d6e947fba2a2faad0dba..0ac643160ac1c8bb7be7b1ed52f58fc7f528c932 100644 (file)
@@ -194,14 +194,14 @@ PHP_FUNCTION(hash_file)
 }
 /* }}} */
 
-static inline void php_hash_string_xor_char(unsigned char *out, const unsigned char *in, const unsigned char xor_with, const int length) {
+static inline void php_hash_string_xor_char(unsigned char *out, const unsigned char *in, const unsigned char xor_with, const size_t length) {
        int i;
        for (i=0; i < length; i++) {
                out[i] = in[i] ^ xor_with;
        }
 }
 
-static inline void php_hash_string_xor(unsigned char *out, const unsigned char *in, const unsigned char *xor_with, const int length) {
+static inline void php_hash_string_xor(unsigned char *out, const unsigned char *in, const unsigned char *xor_with, const size_t length) {
        int i;
        for (i=0; i < length; i++) {
                out[i] = in[i] ^ xor_with[i];
@@ -210,7 +210,7 @@ static inline void php_hash_string_xor(unsigned char *out, const unsigned char *
 
 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) {
        memset(K, 0, ops->block_size);
-       if (key_len > (size_t)ops->block_size) {
+       if (key_len > ops->block_size) {
                /* Reduce the key first */
                ops->hash_init(context);
                ops->hash_update(context, key, key_len);
@@ -373,11 +373,11 @@ static void php_hashcontext_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *objval) {
 
        if (options & PHP_HASH_HMAC) {
                char *K = emalloc(ops->block_size);
-               int i, block_size;
+               size_t i, block_size;
 
                memset(K, 0, ops->block_size);
 
-               if (ZSTR_LEN(key) > (size_t)ops->block_size) {
+               if (ZSTR_LEN(key) > ops->block_size) {
                        /* Reduce the key first */
                        ops->hash_update(context, (unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key));
                        ops->hash_final((unsigned char *) K, context);
@@ -389,7 +389,7 @@ static void php_hashcontext_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *objval) {
 
                /* XOR ipad */
                block_size = ops->block_size;
-               for(i=0; i < block_size; i++) {
+               for(i = 0; i < block_size; i++) {
                        K[i] ^= 0x36;
                }
                ops->hash_update(context, (unsigned char *) K, ops->block_size);
@@ -514,7 +514,7 @@ PHP_FUNCTION(hash_final)
        php_hashcontext_object *hash;
        zend_bool raw_output = 0;
        zend_string *digest;
-       int digest_len;
+       size_t digest_len;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &zhash, php_hashcontext_ce, &raw_output) == FAILURE) {
                return;
@@ -527,11 +527,11 @@ PHP_FUNCTION(hash_final)
        digest = zend_string_alloc(digest_len, 0);
        hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
        if (hash->options & PHP_HASH_HMAC) {
-               int i, block_size;
+               size_t i, block_size;
 
                /* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
                block_size = hash->ops->block_size;
-               for(i=0; i < block_size; i++) {
+               for(i = 0; i < block_size; i++) {
                        hash->key[i] ^= 0x6A;
                }
 
@@ -620,7 +620,8 @@ PHP_FUNCTION(hash_hkdf)
        zend_string *returnval, *ikm, *algo, *info = NULL, *salt = NULL;
        zend_long length = 0;
        unsigned char *prk, *digest, *K;
-       int i, rounds;
+       int i;
+       size_t rounds;
        const php_hash_ops *ops;
        void *context;
 
@@ -649,7 +650,7 @@ PHP_FUNCTION(hash_hkdf)
                RETURN_FALSE;
        } else if (length == 0) {
                length = ops->digest_size;
-       } else if (length > ops->digest_size * 255) {
+       } else if (length > (zend_long) (ops->digest_size * 255)) {
                php_error_docref(NULL, E_WARNING, "Length must be less than or equal to %d: " ZEND_LONG_FMT, ops->digest_size * 255, length);
                RETURN_FALSE;
        }
@@ -671,7 +672,7 @@ PHP_FUNCTION(hash_hkdf)
        // Expand
        returnval = zend_string_alloc(length, 0);
        digest = emalloc(ops->digest_size);
-       for (i = 1, rounds = (int) (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
+       for (i = 1, rounds = (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
                // chr(i)
                unsigned char c[1];
                c[0] = (i & 0xFF);
@@ -831,7 +832,7 @@ PHP_FUNCTION(hash_pbkdf2)
        if (raw_output) {
                memcpy(ZSTR_VAL(returnval), result, length);
        } else {
-               php_hash_bin2hex(ZSTR_VAL(returnval), result, (int) digest_length);
+               php_hash_bin2hex(ZSTR_VAL(returnval), result, digest_length);
        }
        ZSTR_VAL(returnval)[length] = 0;
        efree(result);
@@ -1067,9 +1068,12 @@ PHP_FUNCTION(mhash_keygen_s2k)
                                void *context;
                                char *key, *digest;
                                int i = 0, j = 0;
-                               int block_size = ops->digest_size;
-                               int times = bytes / block_size;
-                               if (bytes % block_size  != 0) times++;
+                               size_t block_size = ops->digest_size;
+                               size_t times = bytes / block_size;
+
+                               if ((bytes % block_size) != 0) {
+                                       times++;
+                               }
 
                                context = emalloc(ops->context_size);
                                ops->hash_init(context);
index 52d98d80d7562f938643d05b303df58dbaf2f923..0f61a8836b2e7b481ee9dc65a4542d3ca5822945 100644 (file)
@@ -159,12 +159,18 @@ static void PHP_SHA3_Update(PHP_SHA3_CTX* ctx,
                             size_t count,
                             size_t block_size) {
        while (count > 0) {
-               unsigned int len = block_size - ctx->pos;
-               if (len > count) len = count;
+               size_t len = block_size - ctx->pos;
+
+               if (len > count) {
+                       len = count;
+               }
+
                count -= len;
+
                while (len-- > 0) {
                        ctx->state[ctx->pos++] ^= *(buf++);
                }
+
                if (ctx->pos >= block_size) {
                        permute(ctx);
                        ctx->pos = 0;
@@ -174,9 +180,9 @@ static void PHP_SHA3_Update(PHP_SHA3_CTX* ctx,
 
 static void PHP_SHA3_Final(unsigned char* digest,
                            PHP_SHA3_CTX* ctx,
-                           int block_size,
-                           int digest_size) {
-       int len = digest_size;
+                           size_t block_size,
+                           size_t digest_size) {
+       size_t len = digest_size;
 
        // Pad state to finalize
        ctx->state[ctx->pos++] ^= 0x06;
index b2d9e263770f8a16004acb690848bc1313688b69..08279f27ad71397161b449db6f9a073871c54bcd 100644 (file)
@@ -40,9 +40,9 @@ typedef struct _php_hash_ops {
        php_hash_final_func_t hash_final;
        php_hash_copy_func_t hash_copy;
 
-       int digest_size;
-       int block_size;
-       int context_size;
+       size_t digest_size;
+       size_t block_size;
+       size_t context_size;
        unsigned is_crypto: 1;
 } php_hash_ops;
 
@@ -147,7 +147,7 @@ PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, size_t alg
 PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops);
 PHP_HASH_API int php_hash_copy(const void *ops, void *orig_context, void *dest_context);
 
-static inline void php_hash_bin2hex(char *out, const unsigned char *in, int in_len)
+static inline void php_hash_bin2hex(char *out, const unsigned char *in, size_t in_len)
 {
        static const char hexits[17] = "0123456789abcdef";
        int i;