Skip to content

Commit

Permalink
Add negative test for iv length change
Browse files Browse the repository at this point in the history
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from #22590)
  • Loading branch information
t8m committed Nov 3, 2023
1 parent 3a95d1e commit 82750a0
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion test/evp_extra_test.c
Expand Up @@ -4061,7 +4061,7 @@ static int test_evp_reset(int idx)
TEST_info("test_evp_reset %d: %s", idx, errmsg);
EVP_CIPHER_CTX_free(ctx);
EVP_CIPHER_free(type);
return testresult;
return testresult;
}

typedef struct {
Expand Down Expand Up @@ -4298,6 +4298,64 @@ static int test_gcm_reinit(int idx)
return testresult;
}

static const char *ivlen_change_ciphers[] = {
"AES-256-GCM",
#ifndef OPENSSL_NO_OCB
"AES-256-OCB",
#endif
"AES-256-CCM"
};

/* Negative test for ivlen change after iv being set */
static int test_ivlen_change(int idx)
{
int outlen;
int res = 0;
unsigned char outbuf[1024];

static const unsigned char iv[] = {
0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34
};
EVP_CIPHER_CTX *ctx = NULL;
EVP_CIPHER *ciph = NULL;
OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
size_t ivlen = 13; /* non-default IV length */

if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
goto err;

if (!TEST_ptr(ciph = EVP_CIPHER_fetch(testctx, ivlen_change_ciphers[idx],
testpropq)))
goto err;

if (!TEST_true(EVP_CipherInit_ex(ctx, ciph, NULL, kGCMDefaultKey, iv, 1)))
goto err;

if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
sizeof(gcmDefaultPlaintext))))
goto err;

params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
&ivlen);
if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params)))
goto err;

ERR_set_mark();
if (!TEST_false(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
sizeof(gcmDefaultPlaintext)))) {
ERR_clear_last_mark();
goto err;
}
ERR_pop_to_mark();

res = 1;
err:
EVP_CIPHER_CTX_free(ctx);
EVP_CIPHER_free(ciph);
return res;
}

#ifndef OPENSSL_NO_DEPRECATED_3_0
static EVP_PKEY_METHOD *custom_pmeth = NULL;
static const EVP_PKEY_METHOD *orig_pmeth = NULL;
Expand Down Expand Up @@ -5419,6 +5477,7 @@ int setup_tests(void)
ADD_ALL_TESTS(test_evp_reset, OSSL_NELEM(evp_reset_tests));
ADD_ALL_TESTS(test_gcm_reinit, OSSL_NELEM(gcm_reinit_tests));
ADD_ALL_TESTS(test_evp_updated_iv, OSSL_NELEM(evp_updated_iv_tests));
ADD_ALL_TESTS(test_ivlen_change, OSSL_NELEM(ivlen_change_ciphers));

#ifndef OPENSSL_NO_DEPRECATED_3_0
ADD_ALL_TESTS(test_custom_pmeth, 12);
Expand Down

0 comments on commit 82750a0

Please sign in to comment.