Skip to content

Commit 232c9a1

Browse files
kaduktmshort
authored andcommitted
Test HKDF with empty IKM
Add an extra EVP test that provides empty input key material. It currently fails, since attempting to set a zero-length key on an EVP_PKEY_CTX results in a call to OPENSSL_memdup() with length zero, which returns NULL and is detected as failure.
1 parent 3e7fb5a commit 232c9a1

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

test/evp_extra_test.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,47 @@ static int test_HKDF(void)
10391039
return ret;
10401040
}
10411041

1042+
static int test_emptyikm_HKDF(void)
1043+
{
1044+
EVP_PKEY_CTX *pctx;
1045+
unsigned char out[20];
1046+
size_t outlen;
1047+
int ret = 0;
1048+
unsigned char salt[] = "9876543210";
1049+
unsigned char key[] = "";
1050+
unsigned char info[] = "stringinfo";
1051+
const unsigned char expected[] = {
1052+
0x68, 0x81, 0xa5, 0x3e, 0x5b, 0x9c, 0x7b, 0x6f, 0x2e, 0xec, 0xc8, 0x47,
1053+
0x7c, 0xfa, 0x47, 0x35, 0x66, 0x82, 0x15, 0x30
1054+
};
1055+
size_t expectedlen = sizeof(expected);
1056+
1057+
if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)))
1058+
goto done;
1059+
1060+
outlen = sizeof(out);
1061+
memset(out, 0, outlen);
1062+
1063+
if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
1064+
|| !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
1065+
|| !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
1066+
sizeof(salt) - 1), 0)
1067+
|| !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
1068+
sizeof(key) - 1), 0)
1069+
|| !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
1070+
sizeof(info) - 1), 0)
1071+
|| !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
1072+
|| !TEST_mem_eq(out, outlen, expected, expectedlen))
1073+
goto done;
1074+
1075+
ret = 1;
1076+
1077+
done:
1078+
EVP_PKEY_CTX_free(pctx);
1079+
1080+
return ret;
1081+
}
1082+
10421083
#ifndef OPENSSL_NO_EC
10431084
static int test_X509_PUBKEY_inplace(void)
10441085
{
@@ -1197,6 +1238,7 @@ int setup_tests(void)
11971238
return 0;
11981239
ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata));
11991240
ADD_TEST(test_HKDF);
1241+
ADD_TEST(test_emptyikm_HKDF);
12001242
#ifndef OPENSSL_NO_EC
12011243
ADD_TEST(test_X509_PUBKEY_inplace);
12021244
ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode,

0 commit comments

Comments
 (0)