Skip to content

Commit d781e62

Browse files
committed
hkdf(): Apply code changes requested by nikic
1 parent 2c1c361 commit d781e62

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

ext/hash/hash.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -613,13 +613,13 @@ PHP_FUNCTION(hkdf)
613613
return;
614614
}
615615

616-
ops = php_hash_fetch_ops(algo->val, algo->len);
616+
ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
617617
if (!ops) {
618-
php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo->val, algo->len);
618+
php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo), ZSTR_LEN(algo));
619619
RETURN_FALSE;
620620
}
621621

622-
if (ikm->len <= 0) {
622+
if (ZSTR_LEN(ikm) == 0) {
623623
php_error_docref(NULL, E_WARNING, "Input keying material cannot be empty");
624624
RETURN_FALSE;
625625
}
@@ -636,26 +636,23 @@ PHP_FUNCTION(hkdf)
636636
RETURN_FALSE;
637637
}
638638

639-
if (salt != NULL && salt->len > 0) {
640-
computed_salt = emalloc(salt->len);
641-
memcpy(computed_salt, salt->val, salt->len);
639+
if (salt != NULL && ZSTR_LEN(salt) > 0) {
640+
computed_salt = emalloc(ZSTR_LEN(salt));
641+
memcpy(computed_salt, ZSTR_VAL(salt), ZSTR_LEN(salt));
642642
}
643643
else {
644644
computed_salt = emalloc(ops->digest_size);
645-
for (i = 0; i < ops->digest_size; i++)
646-
{
647-
computed_salt[i] = 0x00;
648-
}
645+
memset(computed_salt, 0x00, ops->digest_size);
649646
}
650647

651648
context = emalloc(ops->context_size);
652649

653650
// Extract
654651
ops->hash_init(context);
655652
K = emalloc(ops->block_size);
656-
php_hash_hmac_prep_key(K, ops, context, computed_salt, (salt != NULL && salt->len ? salt->len : ops->digest_size));
657-
prk = safe_emalloc(ops->digest_size, ops->digest_size, 0);
658-
php_hash_hmac_round(prk, ops, context, K, ikm->val, ikm->len);
653+
php_hash_hmac_prep_key(K, ops, context, computed_salt, (salt != NULL && ZSTR_LEN(salt) ? ZSTR_LEN(salt) : ops->digest_size));
654+
prk = safe_emalloc(ops->block_size, 1, 0);
655+
php_hash_hmac_round(prk, ops, context, K, ZSTR_VAL(ikm), ZSTR_LEN(ikm));
659656
php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
660657
php_hash_hmac_round(prk, ops, context, K, prk, ops->digest_size);
661658
ZEND_SECURE_ZERO(K, ops->block_size);
@@ -664,7 +661,7 @@ PHP_FUNCTION(hkdf)
664661
// Expand
665662
returnval = zend_string_alloc(length, 0);
666663
digest = emalloc(ops->digest_size);
667-
for (i = 1, rounds = ceil((float) length / (float) ops->digest_size); i <= rounds; i++) {
664+
for (i = 1, rounds = (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
668665
// chr(i)
669666
unsigned char c[1];
670667
c[0] = (i & 0xFF);
@@ -677,8 +674,8 @@ PHP_FUNCTION(hkdf)
677674
ops->hash_update(context, digest, ops->digest_size);
678675
}
679676

680-
if (info != NULL && info->len > 0) {
681-
ops->hash_update(context, info->val, info->len);
677+
if (info != NULL && ZSTR_LEN(info) > 0) {
678+
ops->hash_update(context, ZSTR_VAL(info), ZSTR_LEN(info));
682679
}
683680

684681
ops->hash_update(context, c, 1);

0 commit comments

Comments
 (0)