diff options
author | Matt Caswell <matt@openssl.org> | 2018-10-26 12:45:27 +0100 |
---|---|---|
committer | Matt Caswell <matt@openssl.org> | 2018-10-29 14:06:54 +0000 |
commit | 10d5b415f9e973f44f18eeaf2713868ec813e1d7 (patch) | |
tree | d4be03f248c85beccbfa47efa557201432114b15 /test/evp_extra_test.c | |
parent | ca55d70be031746daddd8bd0611db54ed81f1737 (diff) | |
download | openssl-10d5b415f9e973f44f18eeaf2713868ec813e1d7.tar.gz |
Add a test where we reuse the EVP_PKEY_CTX for two HKDF test runs
Reviewed-by: Tim Hudson <tjh@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/7501)
Diffstat (limited to 'test/evp_extra_test.c')
-rw-r--r-- | test/evp_extra_test.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c index 7b847eed8e..e396b07f5f 100644 --- a/test/evp_extra_test.c +++ b/test/evp_extra_test.c @@ -17,6 +17,7 @@ #include <openssl/rsa.h> #include <openssl/x509.h> #include <openssl/pem.h> +#include <openssl/kdf.h> #include "testutil.h" #include "internal/nelem.h" #include "internal/evp_int.h" @@ -918,6 +919,50 @@ static int test_EVP_PKEY_check(int i) return ret; } +static int test_HKDF(void) +{ + EVP_PKEY_CTX *pctx; + unsigned char out[20]; + size_t outlen; + int i, ret = 0; + unsigned char salt[] = "0123456789"; + unsigned char key[] = "012345678901234567890123456789"; + unsigned char info[] = "infostring"; + const unsigned char expected[] = { + 0xe5, 0x07, 0x70, 0x7f, 0xc6, 0x78, 0xd6, 0x54, 0x32, 0x5f, 0x7e, 0xc5, + 0x7b, 0x59, 0x3e, 0xd8, 0x03, 0x6b, 0xed, 0xca + }; + size_t expectedlen = sizeof(expected); + + if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL))) + goto done; + + /* We do this twice to test reuse of the EVP_PKEY_CTX */ + for (i = 0; i < 2; i++) { + 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; +} + int setup_tests(void) { ADD_TEST(test_EVP_DigestSignInit); @@ -941,5 +986,6 @@ int setup_tests(void) if (!TEST_int_eq(EVP_PKEY_meth_add0(custom_pmeth), 1)) return 0; ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata)); + ADD_TEST(test_HKDF); return 1; } |