test: add digest context dup tests

Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/17529)
This commit is contained in:
Pauli 2022-01-07 11:47:20 +11:00 committed by Pauli
parent 0324ae3e98
commit 0be4b0403d
2 changed files with 41 additions and 12 deletions

View File

@ -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;
}

View File

@ -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 {