From 0be4b0403d2f65adf0d037581223dbebd0fa135e Mon Sep 17 00:00:00 2001 From: Pauli Date: Fri, 7 Jan 2022 11:47:20 +1100 Subject: test: add digest context dup tests Reviewed-by: Tomas Mraz (Merged from https://github.com/openssl/openssl/pull/17529) --- test/evp_extra_test2.c | 16 ++++++++++++++++ test/evp_test.c | 37 +++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 12 deletions(-) (limited to 'test') diff --git a/test/evp_extra_test2.c b/test/evp_extra_test2.c index b70c168d9d..287f3d1443 100644 --- a/test/evp_extra_test2.c +++ b/test/evp_extra_test2.c @@ -853,6 +853,21 @@ static int test_rsa_pss_sign(void) return ret; } +static int test_evp_md_ctx_dup(void) +{ + EVP_MD_CTX *mdctx; + EVP_MD_CTX *copyctx = NULL; + int ret; + + /* test copying freshly initialized context */ + ret = TEST_ptr(mdctx = EVP_MD_CTX_new()) + && TEST_ptr(copyctx = EVP_MD_CTX_dup(mdctx)); + + EVP_MD_CTX_free(mdctx); + EVP_MD_CTX_free(copyctx); + return ret; +} + static int test_evp_md_ctx_copy(void) { EVP_MD_CTX *mdctx = NULL; @@ -895,6 +910,7 @@ int setup_tests(void) #endif ADD_ALL_TESTS(test_PEM_read_bio_negative, OSSL_NELEM(keydata)); ADD_TEST(test_rsa_pss_sign); + ADD_TEST(test_evp_md_ctx_dup); ADD_TEST(test_evp_md_ctx_copy); return 1; } diff --git a/test/evp_test.c b/test/evp_test.c index d068d6fa8e..22fc48df58 100644 --- a/test/evp_test.c +++ b/test/evp_test.c @@ -396,6 +396,26 @@ static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen) return EVP_DigestUpdate(ctx, buf, buflen); } +static int test_duplicate_md_ctx(EVP_TEST *t, EVP_MD_CTX *mctx) +{ + char dont[] = "touch"; + + if (!TEST_ptr(mctx)) + return 0; + if (!EVP_DigestFinalXOF(mctx, (unsigned char *)dont, 0)) { + EVP_MD_CTX_free(mctx); + t->err = "DIGESTFINALXOF_ERROR"; + return 0; + } + if (!TEST_str_eq(dont, "touch")) { + EVP_MD_CTX_free(mctx); + t->err = "DIGESTFINALXOF_ERROR"; + return 0; + } + EVP_MD_CTX_free(mctx); + return 1; +} + static int digest_test_run(EVP_TEST *t) { DIGEST_DATA *expected = t->data; @@ -437,26 +457,19 @@ static int digest_test_run(EVP_TEST *t) xof = (EVP_MD_get_flags(expected->digest) & EVP_MD_FLAG_XOF) != 0; if (xof) { EVP_MD_CTX *mctx_cpy; - char dont[] = "touch"; if (!TEST_ptr(mctx_cpy = EVP_MD_CTX_new())) { goto err; } - if (!EVP_MD_CTX_copy(mctx_cpy, mctx)) { + if (!TEST_true(EVP_MD_CTX_copy(mctx_cpy, mctx))) { EVP_MD_CTX_free(mctx_cpy); goto err; - } - if (!EVP_DigestFinalXOF(mctx_cpy, (unsigned char *)dont, 0)) { - EVP_MD_CTX_free(mctx_cpy); - t->err = "DIGESTFINALXOF_ERROR"; + } else if (!test_duplicate_md_ctx(t, mctx_cpy)) { goto err; } - if (!TEST_str_eq(dont, "touch")) { - EVP_MD_CTX_free(mctx_cpy); - t->err = "DIGESTFINALXOF_ERROR"; + + if (!test_duplicate_md_ctx(t, EVP_MD_CTX_dup(mctx))) goto err; - } - EVP_MD_CTX_free(mctx_cpy); got_len = expected->output_len; if (!EVP_DigestFinalXOF(mctx, got, got_len)) { @@ -825,7 +838,7 @@ static int cipher_test_enc(EVP_TEST *t, int enc, /* Test that the cipher dup functions correctly if it is supported */ ERR_set_mark(); - if (EVP_CIPHER_CTX_copy(ctx, ctx_base)) { + if (EVP_CIPHER_CTX_copy(ctx, ctx_base) != NULL) { EVP_CIPHER_CTX_free(ctx_base); ctx_base = NULL; } else { -- cgit v1.2.3