diff options
author | Pauli <pauli@openssl.org> | 2022-01-07 11:47:20 +1100 |
---|---|---|
committer | Tomas Mraz <tomas@openssl.org> | 2022-11-09 15:32:29 +0100 |
commit | 15e6cb6223af17b0d27f26245f5057f735dc6b36 (patch) | |
tree | fbf3a21c1f2718080af1acfc8c0d1b8a10194e9c /test | |
parent | 0e1634db8e181b2d078cb2f1cf1f91bbce9bc249 (diff) |
test: add digest context dup tests
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/17529)
(cherry picked from commit 0be4b0403d2f65adf0d037581223dbebd0fa135e)
Reviewed-by: Hugo Landau <hlandau@openssl.org>
Diffstat (limited to 'test')
-rw-r--r-- | test/evp_extra_test2.c | 16 | ||||
-rw-r--r-- | test/evp_test.c | 37 |
2 files changed, 41 insertions, 12 deletions
diff --git a/test/evp_extra_test2.c b/test/evp_extra_test2.c index cacd04ce3c..fc73994ca3 100644 --- a/test/evp_extra_test2.c +++ b/test/evp_extra_test2.c @@ -1044,6 +1044,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; @@ -1122,6 +1137,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); ADD_ALL_TESTS(test_provider_unload_effective, 2); #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5 diff --git a/test/evp_test.c b/test/evp_test.c index 19cabd6dce..252bb11ae1 100644 --- a/test/evp_test.c +++ b/test/evp_test.c @@ -395,6 +395,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; @@ -436,26 +456,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)) { @@ -826,7 +839,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 { |