Skip to content

Commit

Permalink
test: add digest context dup tests
Browse files Browse the repository at this point in the history
Reviewed-by: Tomas Mraz <[email protected]>
(Merged from openssl#17529)
  • Loading branch information
paulidale committed Jan 19, 2022
1 parent 0324ae3 commit 0be4b04
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
16 changes: 16 additions & 0 deletions test/evp_extra_test2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
37 changes: 25 additions & 12 deletions test/evp_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0be4b04

Please sign in to comment.