Skip to content

Commit 7f9d8dd

Browse files
committed
Fix hash_hkdf() empty salt handling
1 parent 88c1289 commit 7f9d8dd

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

ext/hash/hash.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ PHP_FUNCTION(hash_algos)
610610
RFC5869 HMAC-based key derivation function */
611611
PHP_FUNCTION(hash_hkdf)
612612
{
613-
zend_string *returnval, *ikm, *algo, *info = NULL, *salt;
613+
zend_string *returnval, *ikm, *algo, *info = NULL, *salt = NULL;
614614
zend_long length = 0;
615615
char *prk, *computed_salt;
616616
unsigned char *digest, *K;
@@ -645,25 +645,24 @@ PHP_FUNCTION(hash_hkdf)
645645
RETURN_FALSE;
646646
}
647647

648-
if (salt->len == 0)
649-
{
648+
if (salt != NULL && salt->len > 0) {
649+
computed_salt = safe_emalloc(salt->len, salt->len, 0);
650+
memcpy(computed_salt, salt->val, salt->len);
651+
}
652+
else {
650653
computed_salt = safe_emalloc(ops->digest_size, ops->digest_size, 0);
651654
for (i = 0; i < ops->digest_size; i++)
652655
{
653656
computed_salt[i] = 0x00;
654657
}
655658
}
656-
else {
657-
computed_salt = safe_emalloc(salt->len, salt->len, 0);
658-
memcpy(computed_salt, salt->val, salt->len);
659-
}
660659

661660
context = emalloc(ops->context_size);
662661

663662
// Extract
664663
ops->hash_init(context);
665664
K = emalloc(ops->block_size);
666-
php_hash_hmac_prep_key(K, ops, context, computed_salt, salt->len ? salt->len : ops->digest_size);
665+
php_hash_hmac_prep_key(K, ops, context, computed_salt, (salt != NULL && salt->len ? salt->len : ops->digest_size));
667666
prk = safe_emalloc(ops->digest_size, ops->digest_size, 0);
668667
php_hash_hmac_round(prk, ops, context, K, ikm->val, ikm->len);
669668
php_hash_string_xor_char(K, K, 0x6A, ops->block_size);

0 commit comments

Comments
 (0)