Skip to content

Commit

Permalink
Test HKDF with empty IKM
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kaduk authored and tmshort committed Dec 11, 2020
1 parent 3e7fb5a commit 232c9a1
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/evp_extra_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,47 @@ static int test_HKDF(void)
return ret;
}

static int test_emptyikm_HKDF(void)
{
EVP_PKEY_CTX *pctx;
unsigned char out[20];
size_t outlen;
int ret = 0;
unsigned char salt[] = "9876543210";
unsigned char key[] = "";
unsigned char info[] = "stringinfo";
const unsigned char expected[] = {
0x68, 0x81, 0xa5, 0x3e, 0x5b, 0x9c, 0x7b, 0x6f, 0x2e, 0xec, 0xc8, 0x47,
0x7c, 0xfa, 0x47, 0x35, 0x66, 0x82, 0x15, 0x30
};
size_t expectedlen = sizeof(expected);

if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)))
goto done;

outlen = sizeof(out);
memset(out, 0, outlen);

if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
|| !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
|| !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
sizeof(salt) - 1), 0)
|| !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
sizeof(key) - 1), 0)
|| !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
sizeof(info) - 1), 0)
|| !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
|| !TEST_mem_eq(out, outlen, expected, expectedlen))
goto done;

ret = 1;

done:
EVP_PKEY_CTX_free(pctx);

return ret;
}

#ifndef OPENSSL_NO_EC
static int test_X509_PUBKEY_inplace(void)
{
Expand Down Expand Up @@ -1197,6 +1238,7 @@ int setup_tests(void)
return 0;
ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata));
ADD_TEST(test_HKDF);
ADD_TEST(test_emptyikm_HKDF);
#ifndef OPENSSL_NO_EC
ADD_TEST(test_X509_PUBKEY_inplace);
ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode,
Expand Down

0 comments on commit 232c9a1

Please sign in to comment.