Skip to content

Commit 1e94d0b

Browse files
committed
Use integer arithmetic for PBKDF2 length calculations
1 parent 2cf96e4 commit 1e94d0b

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

ext/hash/hash.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <config.h>
2020
#endif
2121

22-
#include <math.h>
2322
#include "php_hash.h"
2423
#include "php_hash_sha.h"
2524
#include "ext/standard/info.h"
@@ -582,10 +581,10 @@ static zend_string *php_hash_pbkdf2_sha256(const char *pass, size_t pass_len, co
582581
}
583582
digest_length = length;
584583
if (!raw_output) {
585-
digest_length = (zend_long) ceil((float) length / 2.0);
584+
digest_length = (length / 2) + (length % 2);
586585
}
587586

588-
loops = (zend_long) ceil((float) digest_length / (float) digest_size);
587+
loops = (digest_length / digest_size) + ((digest_length % digest_size) ? 1 : 0);
589588

590589
result = safe_emalloc(loops, digest_size, 0);
591590

@@ -647,7 +646,7 @@ static zend_string *php_hash_pbkdf2_sha256_commoncrypto(const char *pass, size_t
647646
length = raw_output ? 32 : 64;
648647
}
649648

650-
derived_len = raw_output ? length : (zend_long) ceil((float) length / 2.0);
649+
derived_len = raw_output ? length : (length / 2) + (length % 2);
651650
if (derived_len <= 0) {
652651
return NULL;
653652
}
@@ -1244,10 +1243,10 @@ PHP_FUNCTION(hash_pbkdf2)
12441243
}
12451244
digest_length = length;
12461245
if (!raw_output) {
1247-
digest_length = (zend_long) ceil((float) length / 2.0);
1246+
digest_length = (length / 2) + (length % 2);
12481247
}
12491248

1250-
loops = (zend_long) ceil((float) digest_length / (float) ops->digest_size);
1249+
loops = (digest_length / ops->digest_size) + ((digest_length % ops->digest_size) ? 1 : 0);
12511250

12521251
result = safe_emalloc(loops, ops->digest_size, 0);
12531252

0 commit comments

Comments
 (0)