Skip to content

Commit 16127e9

Browse files
committed
Add hash_hkdf() function
1 parent 543f011 commit 16127e9

File tree

5 files changed

+362
-0
lines changed

5 files changed

+362
-0
lines changed

ext/hash/hash.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,108 @@ PHP_FUNCTION(hash_algos)
606606
}
607607
/* }}} */
608608

609+
/* {{{ proto string hash_hkdf(string algo, string ikm [, int length = 0, string info = '', string salt = ''])
610+
RFC5869 HMAC-based key derivation function */
611+
PHP_FUNCTION(hash_hkdf)
612+
{
613+
zend_string *returnval;
614+
char *prk, *ikm, *algo, *info = NULL, *salt = NULL;
615+
unsigned char *digest, *K = NULL;
616+
int i, rounds;
617+
zend_long length = 0;
618+
size_t ikm_len = 0, algo_len, info_len = 0, salt_len = 0;
619+
const php_hash_ops *ops;
620+
void *context;
621+
622+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|lss", &algo, &algo_len, &ikm, &ikm_len, &length, &info, &info_len, &salt, &salt_len) == FAILURE) {
623+
return;
624+
}
625+
626+
ops = php_hash_fetch_ops(algo, algo_len);
627+
if (!ops) {
628+
php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
629+
RETURN_FALSE;
630+
}
631+
632+
if (ikm_len <= 0) {
633+
php_error_docref(NULL, E_WARNING, "Input keying material cannot be empty");
634+
RETURN_FALSE;
635+
}
636+
637+
if (length < 0) {
638+
php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
639+
RETURN_FALSE;
640+
}
641+
else if (length == 0) {
642+
length = ops->digest_size;
643+
}
644+
else if (length > ops->digest_size * 255) {
645+
php_error_docref(NULL, E_WARNING, "Length must be less than or equal to %d: " ZEND_LONG_FMT, ops->digest_size * 255, length);
646+
RETURN_FALSE;
647+
}
648+
649+
if (salt_len == 0)
650+
{
651+
salt = safe_emalloc(ops->digest_size, ops->digest_size, 0);
652+
for (i = 0; i < ops->digest_size; i++)
653+
{
654+
salt[i] = 0x00;
655+
}
656+
}
657+
658+
context = emalloc(ops->context_size);
659+
660+
// Extract
661+
ops->hash_init(context);
662+
K = emalloc(ops->block_size);
663+
php_hash_hmac_prep_key(K, ops, context, salt, salt_len ? salt_len : ops->digest_size);
664+
prk = safe_emalloc(ops->digest_size, ops->digest_size, 0);
665+
php_hash_hmac_round(prk, ops, context, K, (unsigned char *) ikm, ikm_len);
666+
php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
667+
php_hash_hmac_round(prk, ops, context, K, prk, ops->digest_size);
668+
ZEND_SECURE_ZERO(K, ops->block_size);
669+
if (salt_len == 0) {
670+
efree(salt);
671+
}
672+
673+
// Expand
674+
returnval = zend_string_alloc(length, 0);
675+
digest = emalloc(ops->digest_size);
676+
for (i = 1, rounds = ceil((float) length / (float) ops->digest_size); i <= rounds; i++) {
677+
// chr(i)
678+
unsigned char c[1];
679+
c[0] = (i & 0xFF);
680+
681+
php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size);
682+
ops->hash_init(context);
683+
ops->hash_update(context, K, ops->block_size);
684+
685+
if (i > 1) {
686+
ops->hash_update(context, digest, ops->digest_size);
687+
}
688+
689+
if (info_len) {
690+
ops->hash_update(context, (unsigned char *) info, info_len);
691+
}
692+
693+
ops->hash_update(context, c, 1);
694+
ops->hash_final(digest, context);
695+
php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
696+
php_hash_hmac_round(digest, ops, context, K, digest, ops->digest_size);
697+
memcpy(returnval->val + ((i - 1) * ops->digest_size), digest, ops->digest_size);
698+
}
699+
700+
ZEND_SECURE_ZERO(K, ops->block_size);
701+
ZEND_SECURE_ZERO(digest, ops->block_size);
702+
ZEND_SECURE_ZERO(prk, ops->block_size);
703+
efree(K);
704+
efree(context);
705+
efree(prk);
706+
efree(digest);
707+
returnval->val[length] = 0;
708+
RETURN_STR(returnval);
709+
}
710+
609711
/* {{{ proto string hash_pbkdf2(string algo, string password, string salt, int iterations [, int length = 0, bool raw_output = false])
610712
Generate a PBKDF2 hash of the given password and salt
611713
Returns lowercase hexits by default */
@@ -1182,6 +1284,14 @@ ZEND_END_ARG_INFO()
11821284
ZEND_BEGIN_ARG_INFO(arginfo_hash_algos, 0)
11831285
ZEND_END_ARG_INFO()
11841286

1287+
ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hkdf, 0, 0, 2)
1288+
ZEND_ARG_INFO(0, ikm)
1289+
ZEND_ARG_INFO(0, algo)
1290+
ZEND_ARG_INFO(0, length)
1291+
ZEND_ARG_INFO(0, string)
1292+
ZEND_ARG_INFO(0, salt)
1293+
ZEND_END_ARG_INFO()
1294+
11851295
ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_pbkdf2, 0, 0, 4)
11861296
ZEND_ARG_INFO(0, algo)
11871297
ZEND_ARG_INFO(0, password)
@@ -1242,6 +1352,7 @@ const zend_function_entry hash_functions[] = {
12421352
PHP_FE(hash_copy, arginfo_hash_copy)
12431353

12441354
PHP_FE(hash_algos, arginfo_hash_algos)
1355+
PHP_FE(hash_hkdf, arginfo_hash_hkdf)
12451356
PHP_FE(hash_pbkdf2, arginfo_hash_pbkdf2)
12461357
PHP_FE(hash_equals, arginfo_hash_equals)
12471358

ext/hash/php_hash.h

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

128128
PHP_FUNCTION(hash);
129129
PHP_FUNCTION(hash_file);
130+
PHP_FUNCTION(hash_hkdf);
130131
PHP_FUNCTION(hash_hmac);
131132
PHP_FUNCTION(hash_hmac_file);
132133
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 hash_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_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 hash_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===

ext/hash/tests/hash_hkdf_rfc5869.phpt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
--TEST--
2+
Test hash_hkdf() function: RFC 5869 test vectors
3+
--SKIPIF--
4+
<?php extension_loaded('hash') or die('skip: hash extension not loaded.'); ?>
5+
--FILE--
6+
<?php
7+
8+
/* Prototype : string hash_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(): RFC 5869 test vectors ***\n";
14+
echo "Test case 1 (SHA-256): ",
15+
bin2hex(hash_hkdf(
16+
'sha256',
17+
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
18+
42,
19+
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9",
20+
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c"
21+
)), "\n";
22+
echo "Test case 2 (SHA-256 with longer inputs/outputs): ",
23+
bin2hex(hash_hkdf(
24+
'sha256',
25+
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f",
26+
82,
27+
"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
28+
"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
29+
)), "\n";
30+
echo "Test case 3 (SHA-256 with zero-length salt, info): ",
31+
bin2hex(hash_hkdf(
32+
'sha256',
33+
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
34+
42,
35+
'',
36+
''
37+
)), "\n";
38+
echo "Test case 4 (SHA-1): ",
39+
bin2hex(hash_hkdf(
40+
'sha1',
41+
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
42+
42,
43+
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9",
44+
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c"
45+
)), "\n";
46+
echo "Test case 5 (SHA-1 with longer inputs/outputs): ",
47+
bin2hex(hash_hkdf(
48+
'sha1',
49+
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f",
50+
82,
51+
"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
52+
"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
53+
)), "\n";
54+
echo "Test case 6 (SHA-1 with zero-length salt, info): ",
55+
bin2hex(hash_hkdf(
56+
'sha1',
57+
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
58+
42,
59+
'',
60+
''
61+
)), "\n";
62+
echo "Test case 7 (SHA-1 with zero-length info, salt not provided): ",
63+
bin2hex(hash_hkdf(
64+
'sha1',
65+
"\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
66+
42,
67+
''
68+
)), "\n";
69+
?>
70+
===Done===
71+
--EXPECTF--
72+
*** Testing hash_hkdf(): RFC 5869 test vectors ***
73+
Test case 1 (SHA-256): 3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865
74+
Test case 2 (SHA-256 with longer inputs/outputs): b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c59045a99cac7827271cb41c65e590e09da3275600c2f09b8367793a9aca3db71cc30c58179ec3e87c14c01d5c1f3434f1d87
75+
Test case 3 (SHA-256 with zero-length salt, info): 8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8
76+
Test case 4 (SHA-1): 085a01ea1b10f36933068b56efa5ad81a4f14b822f5b091568a9cdd4f155fda2c22e422478d305f3f896
77+
Test case 5 (SHA-1 with longer inputs/outputs): 0bd770a74d1160f7c9f12cd5912a06ebff6adcae899d92191fe4305673ba2ffe8fa3f1a4e5ad79f3f334b3b202b2173c486ea37ce3d397ed034c7f9dfeb15c5e927336d0441f4c4300e2cff0d0900b52d3b4
78+
Test case 6 (SHA-1 with zero-length salt, info): 0ac1af7002b3d761d1e55298da9d0506b9ae52057220a306e07b6b87e8df21d0ea00033de03984d34918
79+
Test case 7 (SHA-1 with zero-length info, salt not provided): 2c91117204d745f3500d636a62f64f0ab3bae548aa53d423b0d1f27ebba6f5e5673a081d70cce7acfc48
80+
===Done===

0 commit comments

Comments
 (0)