Skip to content

Commit 2cfa72e

Browse files
committed
Add hash_hkdf()
1 parent 62f29c0 commit 2cfa72e

File tree

6 files changed

+387
-0
lines changed

6 files changed

+387
-0
lines changed

ext/hash/hash.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,109 @@ PHP_FUNCTION(hash_algos)
597597
}
598598
/* }}} */
599599

600+
/* {{{ proto string hash_hkdf(string algo, string ikm [, int length = 0, string info = '', string salt = ''])
601+
RFC5869 HMAC-based key derivation function */
602+
PHP_FUNCTION(hash_hkdf)
603+
{
604+
zend_string *returnval, *ikm, *algo, *info = NULL, *salt = NULL;
605+
zend_long length = 0;
606+
char *prk, *computed_salt;
607+
unsigned char *digest, *K;
608+
int i, rounds;
609+
const php_hash_ops *ops;
610+
void *context;
611+
612+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lSS", &algo, &ikm, &length, &info, &salt) == FAILURE) {
613+
return;
614+
}
615+
616+
ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
617+
if (!ops) {
618+
php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo), ZSTR_LEN(algo));
619+
RETURN_FALSE;
620+
}
621+
622+
if (ZSTR_LEN(ikm) == 0) {
623+
php_error_docref(NULL, E_WARNING, "Input keying material cannot be empty");
624+
RETURN_FALSE;
625+
}
626+
627+
if (length < 0) {
628+
php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
629+
RETURN_FALSE;
630+
}
631+
else if (length == 0) {
632+
length = ops->digest_size;
633+
}
634+
else if (length > ops->digest_size * 255) {
635+
php_error_docref(NULL, E_WARNING, "Length must be less than or equal to %d: " ZEND_LONG_FMT, ops->digest_size * 255, length);
636+
RETURN_FALSE;
637+
}
638+
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));
642+
}
643+
else {
644+
computed_salt = emalloc(ops->digest_size);
645+
memset(computed_salt, 0x00, ops->digest_size);
646+
}
647+
648+
context = emalloc(ops->context_size);
649+
650+
// Extract
651+
ops->hash_init(context);
652+
K = emalloc(ops->block_size);
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));
656+
php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
657+
php_hash_hmac_round(prk, ops, context, K, prk, ops->digest_size);
658+
ZEND_SECURE_ZERO(K, ops->block_size);
659+
efree(computed_salt);
660+
661+
// Expand
662+
returnval = zend_string_alloc(length, 0);
663+
digest = emalloc(ops->digest_size);
664+
for (i = 1, rounds = (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
665+
// chr(i)
666+
unsigned char c[1];
667+
c[0] = (i & 0xFF);
668+
669+
php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size);
670+
ops->hash_init(context);
671+
ops->hash_update(context, K, ops->block_size);
672+
673+
if (i > 1) {
674+
ops->hash_update(context, digest, ops->digest_size);
675+
}
676+
677+
if (info != NULL && ZSTR_LEN(info) > 0) {
678+
ops->hash_update(context, ZSTR_VAL(info), ZSTR_LEN(info));
679+
}
680+
681+
ops->hash_update(context, c, 1);
682+
ops->hash_final(digest, context);
683+
php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
684+
php_hash_hmac_round(digest, ops, context, K, digest, ops->digest_size);
685+
memcpy(
686+
returnval->val + ((i - 1) * ops->digest_size),
687+
digest,
688+
(i == rounds ? length - ((i - 1) * ops->digest_size) : ops->digest_size)
689+
);
690+
}
691+
692+
ZEND_SECURE_ZERO(K, ops->block_size);
693+
ZEND_SECURE_ZERO(digest, ops->block_size);
694+
ZEND_SECURE_ZERO(prk, ops->block_size);
695+
efree(K);
696+
efree(context);
697+
efree(prk);
698+
efree(digest);
699+
returnval->val[length] = 0;
700+
RETURN_STR(returnval);
701+
}
702+
600703
/* {{{ proto string hash_pbkdf2(string algo, string password, string salt, int iterations [, int length = 0, bool raw_output = false])
601704
Generate a PBKDF2 hash of the given password and salt
602705
Returns lowercase hexits by default */
@@ -1205,6 +1308,14 @@ ZEND_BEGIN_ARG_INFO(arginfo_hash_equals, 0)
12051308
ZEND_ARG_INFO(0, user_string)
12061309
ZEND_END_ARG_INFO()
12071310

1311+
ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hkdf, 0, 0, 2)
1312+
ZEND_ARG_INFO(0, ikm)
1313+
ZEND_ARG_INFO(0, algo)
1314+
ZEND_ARG_INFO(0, length)
1315+
ZEND_ARG_INFO(0, string)
1316+
ZEND_ARG_INFO(0, salt)
1317+
ZEND_END_ARG_INFO()
1318+
12081319
/* BC Land */
12091320
#ifdef PHP_MHASH_BC
12101321
ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_block_size, 0)
@@ -1253,6 +1364,7 @@ const zend_function_entry hash_functions[] = {
12531364
PHP_FE(hash_algos, arginfo_hash_algos)
12541365
PHP_FE(hash_pbkdf2, arginfo_hash_pbkdf2)
12551366
PHP_FE(hash_equals, arginfo_hash_equals)
1367+
PHP_FE(hash_hkdf, arginfo_hash_hkdf)
12561368

12571369
/* BC Land */
12581370
#ifdef PHP_HASH_MD5_NOT_IN_CORE

ext/hash/php_hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ extern zend_module_entry hash_module_entry;
130130

131131
PHP_FUNCTION(hash);
132132
PHP_FUNCTION(hash_file);
133+
PHP_FUNCTION(hash_hkdf);
133134
PHP_FUNCTION(hash_hmac);
134135
PHP_FUNCTION(hash_hmac_file);
135136
PHP_FUNCTION(hash_init);

ext/hash/tests/hash_hkdf_basic.phpt

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
--TEST--
2+
Test hash_hkdf() function: basic functionality
3+
--SKIPIF--
4+
<?php extension_loaded('hash') or die('skip: hash extension not loaded.'); ?>
5+
--FILE--
6+
<?php
7+
8+
/* Prototype : string hkdf ( string $algo , string $ikm [, int $length , string $info = '' , string $salt = '' ] )
9+
* Description: HMAC-based Key Derivation Function
10+
* Source code: ext/hash/hash.c
11+
*/
12+
13+
echo "*** Testing hash_hkdf(): basic functionality ***\n";
14+
15+
$ikm = 'input key material';
16+
17+
echo 'md2: ', bin2hex(hash_hkdf('md2', $ikm)), "\n";
18+
echo 'md4: ', bin2hex(hash_hkdf('md4', $ikm)), "\n";
19+
echo 'md5: ', bin2hex(hash_hkdf('md5', $ikm)), "\n";
20+
echo 'sha1: ', bin2hex(hash_hkdf('sha1', $ikm)), "\n";
21+
echo 'sha224: ', bin2hex(hash_hkdf('sha224', $ikm)), "\n";
22+
echo 'sha256: ', bin2hex(hash_hkdf('sha256', $ikm)), "\n";
23+
echo 'sha384: ', bin2hex(hash_hkdf('sha384', $ikm)), "\n";
24+
echo 'sha512: ', bin2hex(hash_hkdf('sha512', $ikm)), "\n";
25+
echo 'ripemd128: ', bin2hex(hash_hkdf('ripemd128', $ikm)), "\n";
26+
echo 'ripemd160: ', bin2hex(hash_hkdf('ripemd160', $ikm)), "\n";
27+
echo 'ripemd256: ', bin2hex(hash_hkdf('ripemd256', $ikm)), "\n";
28+
echo 'ripemd320: ', bin2hex(hash_hkdf('ripemd320', $ikm)), "\n";
29+
echo 'whirlpool: ', bin2hex(hash_hkdf('whirlpool', $ikm)), "\n";
30+
echo 'tiger128,3: ', bin2hex(hash_hkdf('tiger128,3', $ikm)), "\n";
31+
echo 'tiger160,3: ', bin2hex(hash_hkdf('tiger160,3', $ikm)), "\n";
32+
echo 'tiger192,3: ', bin2hex(hash_hkdf('tiger192,3', $ikm)), "\n";
33+
echo 'tiger128,4: ', bin2hex(hash_hkdf('tiger128,4', $ikm)), "\n";
34+
echo 'tiger160,4: ', bin2hex(hash_hkdf('tiger160,4', $ikm)), "\n";
35+
echo 'tiger192,4: ', bin2hex(hash_hkdf('tiger192,4', $ikm)), "\n";
36+
echo 'haval128,3: ', bin2hex(hash_hkdf('haval128,3', $ikm)), "\n";
37+
echo 'haval160,3: ', bin2hex(hash_hkdf('haval160,3', $ikm)), "\n";
38+
echo 'haval192,3: ', bin2hex(hash_hkdf('haval192,3', $ikm)), "\n";
39+
echo 'haval224,3: ', bin2hex(hash_hkdf('haval224,3', $ikm)), "\n";
40+
echo 'haval256,3: ', bin2hex(hash_hkdf('haval256,3', $ikm)), "\n";
41+
echo 'haval128,4: ', bin2hex(hash_hkdf('haval128,4', $ikm)), "\n";
42+
echo 'haval160,4: ', bin2hex(hash_hkdf('haval160,4', $ikm)), "\n";
43+
echo 'haval192,4: ', bin2hex(hash_hkdf('haval192,4', $ikm)), "\n";
44+
echo 'haval224,4: ', bin2hex(hash_hkdf('haval224,4', $ikm)), "\n";
45+
echo 'haval256,4: ', bin2hex(hash_hkdf('haval256,4', $ikm)), "\n";
46+
echo 'haval128,5: ', bin2hex(hash_hkdf('haval128,5', $ikm)), "\n";
47+
echo 'haval160,5: ', bin2hex(hash_hkdf('haval160,5', $ikm)), "\n";
48+
echo 'haval192,5: ', bin2hex(hash_hkdf('haval192,5', $ikm)), "\n";
49+
echo 'haval224,5: ', bin2hex(hash_hkdf('haval224,5', $ikm)), "\n";
50+
echo 'haval256,5: ', bin2hex(hash_hkdf('haval256,5', $ikm)), "\n";
51+
echo 'snefru: ', bin2hex(hash_hkdf('snefru', $ikm)), "\n";
52+
echo 'snefru256: ', bin2hex(hash_hkdf('snefru256', $ikm)), "\n";
53+
echo 'gost: ', bin2hex(hash_hkdf('gost', $ikm)), "\n";
54+
echo 'adler32: ', bin2hex(hash_hkdf('adler32', $ikm)), "\n";
55+
echo 'crc32: ', bin2hex(hash_hkdf('crc32', $ikm)), "\n";
56+
echo 'crc32b: ', bin2hex(hash_hkdf('crc32b', $ikm)), "\n";
57+
echo 'fnv132: ', bin2hex(hash_hkdf('fnv132', $ikm)), "\n";
58+
echo 'fnv164: ', bin2hex(hash_hkdf('fnv164', $ikm)), "\n";
59+
echo 'joaat: ', bin2hex(hash_hkdf('joaat', $ikm)), "\n";
60+
61+
?>
62+
--EXPECTF--
63+
*** Testing hash_hkdf(): basic functionality ***
64+
md2: 87779851d2377dab25da16fd7aadfdf5
65+
md4: 422c6bd8dd2a6baae8abadef618c3ede
66+
md5: 98b16391063ecee006a3ca8ee5776b1e
67+
sha1: a71863230e3782240265126a53e137af6667e988
68+
sha224: 51678ceb17e803505187b2cf6451c30fbc572fda165bb69bbd117c7a
69+
sha256: d8f0bede4b652933c32a92eccf7723f7eeb4701744c81325dc3f0fa9fda24499
70+
sha384: f600680e677bb417a7a22a4da8b167c0d91823a7a5d56a49aeb1838bb2320c05068d15d6d980824fee542a279d310c3a
71+
sha512: fb1b86549e941b81821a89ac6ba7c4f93465077b3f2af94352ebf1d041efcd3c5694469c1ae31bb10db4c1d2ab84f07e4518ba33a3eadd4a149425750285c640
72+
ripemd128: cb6418fc0dc9efaeb7e9654390fa7f14
73+
ripemd160: ba42dbb34f08e9337ace15295f218754a41d6c39
74+
ripemd256: f2e96b292935e2395b59833ed89d928ac1197ff62c8031ebc06a3f5bad19513f
75+
ripemd320: a13a682072525ceb4c4a5fef59096e682096e1096e6e7e238c7bd48a6f6c6a9ba3d7d9fbee6b68c4
76+
whirlpool: 497c717e04d896c3d582742c614435b7d0963b39de12dcf532540d39164b3b85214014620dfdff4a089a06b06aff43c39a3b4d9b806913cf6309de58ff1151f5
77+
tiger128,3: e13c2e7262892c6bd8dfc24121e7cb34
78+
tiger160,3: 48cc5a9f5e5d7029eb0544662222c0ba13822b7b
79+
tiger192,3: 5a665d23b6cbb405668160e58b01aebef74eba979f4bc70b
80+
tiger128,4: 8acf517ecf58cccbd65c1186d71e4116
81+
tiger160,4: cc0e33ee26700a2eb9a994bbb0e6cef29b429441
82+
tiger192,4: 97fa02d42331321fdc05c7f8dbc756d751ca36ce1aee69b0
83+
haval128,3: 2accab8029d42fb15fdbe9d3e2a470ca
84+
haval160,3: 496fd29e7fc8351d2971b96a3733a7b3de000064
85+
haval192,3: 238a731801439b1f195e1a1568ce75251e1dd719d904a8a2
86+
haval224,3: d863e596ff6b2bdba1ed7b313df1c3d177176312e81b47e9290f7566
87+
haval256,3: 96f555fe41255c34fe57b275f1ae40bbb8f07c6a2a6d68c849748fbb393ff443
88+
haval128,4: 9822af229cc59527a72e231a690fad3b
89+
haval160,4: 1bbbc4d632daaf94d5ba167efaa70af5b753effe
90+
haval192,4: dd12a8f8919cbf5632497f0918b30236371dd1b55f71e824
91+
haval224,4: 8af449fb4eb627eb8887507c1279a116ac4325b5806dd22e2f2af410
92+
haval256,4: bd74a6d5fa1ec23a92ce1fd76c36bc8be36f5eddbea821545a91810e1f8d6fc5
93+
haval128,5: 84564f3450a6ccf6041162207dc8acba
94+
haval160,5: b55cd1b3c514457b9e61c51ad22f302f6ec7cca1
95+
haval192,5: d1db7a8e69b327455d530d1ac60f774023b8b4bdd6bbbf92
96+
haval224,5: c5a2576511f1143c6e29f63d82d6e0be8f67d0bea448e27238be5000
97+
haval256,5: 9dbab73d13f1fd3a1b41398fe90ba1f298329681d861b023373c33f1051bd4d3
98+
snefru: 798eac954e5ece38e9acb63b50c1c2ecb799d34356358cec5a80eeeea91c8de9
99+
snefru256: 798eac954e5ece38e9acb63b50c1c2ecb799d34356358cec5a80eeeea91c8de9
100+
gost: 64edd584b87a2dfdd1f2b44ed2db8bd27af8386aafe751c2aebaed32dfa3852e
101+
adler32: 111203fb
102+
crc32: d402e91f
103+
crc32b: 63a85b24
104+
fnv132: 353e847c
105+
fnv164: 9e8849af1a6d29ac
106+
joaat: 1bde5739

ext/hash/tests/hash_hkdf_edges.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Test hash_hkdf() function: edge cases
3+
--SKIPIF--
4+
<?php extension_loaded('hash') or die('skip: hash extension not loaded.'); ?>
5+
--FILE--
6+
<?php
7+
8+
/* Prototype : string hkdf ( string $algo , string $ikm [, int $length , string $info = '' , string $salt = '' ] )
9+
* Description: HMAC-based Key Derivation Function
10+
* Source code: ext/hash/hash.c
11+
*/
12+
13+
echo "*** Testing hash_hkdf(): edge cases ***\n";
14+
15+
$ikm = 'input key material';
16+
17+
echo 'Length < digestSize: ', bin2hex(hash_hkdf('md5', $ikm, 7)), "\n";
18+
echo 'Length % digestSize != 0: ', bin2hex(hash_hkdf('md5', $ikm, 17)), "\n";
19+
20+
?>
21+
--EXPECTF--
22+
*** Testing hash_hkdf(): edge cases ***
23+
Length < digestSize: 98b16391063ece
24+
Length % digestSize != 0: 98b16391063ecee006a3ca8ee5776b1e5f

ext/hash/tests/hash_hkdf_error.phpt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
--TEST--
2+
Test hash_hkdf() function: error conditions
3+
--SKIPIF--
4+
<?php extension_loaded('hash') or die('skip: hash extension not loaded.'); ?>
5+
--FILE--
6+
<?php
7+
8+
/* Prototype : string hkdf ( string $algo , string $ikm [, int $length , string $info = '' , string $salt = '' ] )
9+
* Description: HMAC-based Key Derivation Function
10+
* Source code: ext/hash/hash.c
11+
*/
12+
13+
$ikm = 'input key material';
14+
15+
echo "*** Testing hash_hkdf(): error conditions ***\n";
16+
17+
echo "\n-- Testing hash_hkdf() function with less than expected no. of arguments --\n";
18+
var_dump(hash_hkdf());
19+
var_dump(hash_hkdf('sha1'));
20+
21+
echo "\n-- Testing hash_hkdf() function with more than expected no. of arguments --\n";
22+
var_dump(hash_hkdf('sha1', $ikm, 20, '', '', 'extra parameter'));
23+
24+
echo "\n-- Testing hash_hkdf() function with invalid hash algorithm --\n";
25+
var_dump(hash_hkdf('foo', $ikm));
26+
27+
echo "\n-- Testing hash_hkdf() function with invalid parameters --\n";
28+
var_dump(hash_hkdf('sha1', ''));
29+
var_dump(hash_hkdf('sha1', $ikm, -1));
30+
var_dump(hash_hkdf('sha1', $ikm, 20 * 255 + 1)); // Length can't be more than 255 times the hash digest size
31+
?>
32+
===Done===
33+
--EXPECTF--
34+
*** Testing hash_hkdf(): error conditions ***
35+
36+
-- Testing hash_hkdf() function with less than expected no. of arguments --
37+
38+
Warning: hash_hkdf() expects at least 2 parameters, 0 given in %s on line %d
39+
NULL
40+
41+
Warning: hash_hkdf() expects at least 2 parameters, 1 given in %s on line %d
42+
NULL
43+
44+
-- Testing hash_hkdf() function with more than expected no. of arguments --
45+
46+
Warning: hash_hkdf() expects at most 5 parameters, 6 given in %s on line %d
47+
NULL
48+
49+
-- Testing hash_hkdf() function with invalid hash algorithm --
50+
51+
Warning: hash_hkdf(): Unknown hashing algorithm: foo in %s on line %d
52+
bool(false)
53+
54+
-- Testing hash_hkdf() function with invalid parameters --
55+
56+
Warning: hash_hkdf(): Input keying material cannot be empty in %s on line %d
57+
bool(false)
58+
59+
Warning: hash_hkdf(): Length must be greater than or equal to 0: -1 in %s on line %d
60+
bool(false)
61+
62+
Warning: hash_hkdf(): Length must be less than or equal to 5100: 5101 in %s on line %d
63+
bool(false)
64+
===Done===

0 commit comments

Comments
 (0)