summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKalle Sommer Nielsen <kalle@php.net>2018-10-03 13:36:05 +0200
committerKalle Sommer Nielsen <kalle@php.net>2018-10-03 13:36:05 +0200
commit5268f367ce8a1c6e6053cc029224f877b3c0bc8b (patch)
treecd0ee3a86e49efda89ec448a599759679267ac07
parentb1f6390f1669bb716e3d427e425e5602080653f7 (diff)
downloadphp-git-5268f367ce8a1c6e6053cc029224f877b3c0bc8b.tar.gz
Hopefully last round of size_t fixes, this makes the remaining php_hash_ops use size_t over an int
-rw-r--r--ext/hash/hash.c36
-rw-r--r--ext/hash/hash_sha3.c16
-rw-r--r--ext/hash/php_hash.h8
3 files changed, 35 insertions, 25 deletions
diff --git a/ext/hash/hash.c b/ext/hash/hash.c
index e92185c4d5..0ac643160a 100644
--- a/ext/hash/hash.c
+++ b/ext/hash/hash.c
@@ -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);
diff --git a/ext/hash/hash_sha3.c b/ext/hash/hash_sha3.c
index 52d98d80d7..0f61a8836b 100644
--- a/ext/hash/hash_sha3.c
+++ b/ext/hash/hash_sha3.c
@@ -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;
diff --git a/ext/hash/php_hash.h b/ext/hash/php_hash.h
index b2d9e26377..08279f27ad 100644
--- a/ext/hash/php_hash.h
+++ b/ext/hash/php_hash.h
@@ -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;