Commit 985ab86794 for openssl.org
commit 985ab867940ce5d58a5c01d168961d2f57210c23
Author: user <kronoswq@gmail.com>
Date: Tue Sep 22 18:20:23 2026 -0400
test: migrate test_evp_stale_key_reinit to evp_aead_test.c
Move test_evp_stale_key_reinit from evp_extra_test.c to evp_aead_test.c
and clean it up a bit.
Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.foundation>
Merge-date: Tue Nov 10 15:03:03 2026
Merged-from: https://github.com/openssl/openssl/pull/32936
diff --git a/test/evp_aead_test.c b/test/evp_aead_test.c
index 892ba645bb..54b870c426 100644
--- a/test/evp_aead_test.c
+++ b/test/evp_aead_test.c
@@ -947,6 +947,255 @@ err:
return testresult;
}
+/*
+ * Verify stale key is not being used after providing a new key in multiple steps.
+ * This test performs a full round of encryption and then changes the
+ * key and then the IV in a multi-step init.
+ * This is compared to another context without reinitialization to check
+ * if the new key is loaded correctly.
+ */
+static int test_evp_stale_key_reinit(int idx)
+{
+ const AEAD_DATA *info = &aead_list[idx];
+ EVP_CIPHER_CTX *ctx_reinit = NULL; /* used to test multi step init KEY->IV */
+ EVP_CIPHER_CTX *ctx_onestep = NULL; /* base test with single step init */
+
+ OSSL_PARAM ivparams[2];
+ OSSL_PARAM tagparams[2];
+ OSSL_PARAM get_tagparams[2];
+
+ int ivlen = info->ivlen;
+ unsigned char key[EVP_MAX_KEY_LENGTH] = { 0 };
+ unsigned char key2[EVP_MAX_KEY_LENGTH] = { 0 };
+ unsigned char iv[EVP_MAX_IV_LENGTH] = { 0 };
+ unsigned char iv2[EVP_MAX_IV_LENGTH] = { 0 };
+
+ size_t pt_size = 0, j = 0;
+ unsigned char pt[128] = { 0 };
+
+ unsigned char ct[128] = { 0 };
+ int ct_len = 0;
+ int ct_fin_len = 0;
+
+ unsigned char ct_reinit[128] = { 0 };
+ int ct_reinit_len = 0;
+ int ct_reinit_fin_len = 0;
+
+ unsigned char ct_onestep[128] = { 0 };
+ int ct_onestep_len = 0;
+ int ct_onestep_fin_len = 0;
+
+ int taglen = info->taglen;
+ unsigned char tag[EVPTEST_TAG_LEN_MAX] = { 0 };
+ unsigned char tag_reinit[EVPTEST_TAG_LEN_MAX] = { 0 };
+ unsigned char tag_onestep[EVPTEST_TAG_LEN_MAX] = { 0 };
+
+ int blocksz = 0, tmplen = 0, testresult = 0, i = 0;
+ char *errmsg = NULL;
+
+ ivparams[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD_IVLEN, &ivlen);
+ ivparams[1] = OSSL_PARAM_construct_end();
+ tagparams[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &taglen);
+ tagparams[1] = OSSL_PARAM_construct_end();
+
+ blocksz = EVP_CIPHER_get_block_size(info->ciph);
+ pt_size = (blocksz > 1) ? (size_t)blocksz * 2 : 31;
+
+ for (i = 0; i < info->keylen && i < (int)sizeof(key); i++)
+ key[i] = (unsigned char)(0xA0 + i);
+ for (i = 0; i < info->keylen && i < (int)sizeof(key2); i++)
+ key2[i] = (unsigned char)(0xF0 + i);
+
+ for (i = 0; i < info->ivlen && i < (int)sizeof(iv); i++)
+ iv[i] = (unsigned char)(0xB0 + i);
+ for (i = 0; i < info->ivlen && i < (int)sizeof(iv2); i++)
+ iv2[i] = (unsigned char)(0xDA + i);
+
+ for (j = 0; j < pt_size; j++)
+ pt[j] = (unsigned char)(0xA7);
+
+ if (!TEST_ptr(ctx_reinit = EVP_CIPHER_CTX_new())) {
+ errmsg = "CTX_REINIT_ALLOC";
+ goto err;
+ }
+
+ if (!TEST_true(EVP_EncryptInit_ex(ctx_reinit, info->ciph,
+ NULL, NULL, NULL))) {
+ errmsg = "CTX_FIRST_INIT";
+ goto err;
+ }
+
+ if (info->mode == EVP_CIPH_CCM_MODE) {
+ if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx_reinit, ivparams))) {
+ errmsg = "CCM_SET_IVLEN";
+ goto err;
+ }
+ if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx_reinit, tagparams))) {
+ errmsg = "CCM_SET_TAGLEN";
+ goto err;
+ }
+ }
+
+ /* Initialize first with key */
+ if (!TEST_true(EVP_EncryptInit_ex(ctx_reinit, NULL, NULL, key, iv))) {
+ errmsg = "FIRST_KEY_INIT";
+ goto err;
+ }
+
+ if (info->mode == EVP_CIPH_CCM_MODE
+ && !TEST_true(EVP_EncryptUpdate(ctx_reinit, NULL,
+ &tmplen, NULL, (int)pt_size))) {
+ errmsg = "CCM_DECLARE_PTLEN";
+ goto err;
+ }
+
+ if (!TEST_true(EVP_EncryptUpdate(ctx_reinit, ct, &ct_len,
+ pt, (int)pt_size))) {
+ errmsg = "ENCRYPT_UPDATE";
+ goto err;
+ }
+
+ if (!TEST_true(EVP_EncryptFinal_ex(ctx_reinit, ct + ct_len,
+ &ct_fin_len))) {
+ errmsg = "ENCRYPT_FINAL";
+ goto err;
+ }
+
+ ct_len += ct_fin_len;
+
+ get_tagparams[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, tag, taglen);
+ get_tagparams[1] = OSSL_PARAM_construct_end();
+
+ if (!TEST_true(EVP_CIPHER_CTX_get_params(ctx_reinit, get_tagparams))) {
+ errmsg = "AEAD_GET_TAG";
+ goto err;
+ }
+
+ /* use same context with a different key and iv in multiple steps */
+ if (!TEST_true(EVP_EncryptInit_ex(ctx_reinit, NULL, NULL, key2, NULL))) {
+ errmsg = "REINIT_KEY";
+ goto err;
+ }
+
+ if (!TEST_true(EVP_EncryptInit_ex(ctx_reinit, NULL, NULL, NULL, iv2))) {
+ errmsg = "REINIT_IV";
+ goto err;
+ }
+
+ if (info->mode == EVP_CIPH_CCM_MODE
+ && !TEST_true(EVP_EncryptUpdate(ctx_reinit, NULL,
+ &tmplen, NULL, (int)pt_size))) {
+ errmsg = "CCM_DECLARE_PTLEN";
+ goto err;
+ }
+
+ if (!TEST_true(EVP_EncryptUpdate(ctx_reinit, ct_reinit,
+ &ct_reinit_len, pt, (int)pt_size))) {
+ errmsg = "ENCRYPT_UPDATE";
+ goto err;
+ }
+
+ if (!TEST_true(EVP_EncryptFinal_ex(ctx_reinit,
+ ct_reinit + ct_reinit_len, &ct_reinit_fin_len))) {
+ errmsg = "ENCRYPT_FINAL";
+ goto err;
+ }
+
+ ct_reinit_len += ct_reinit_fin_len;
+ get_tagparams[0] = OSSL_PARAM_construct_octet_string(
+ OSSL_CIPHER_PARAM_AEAD_TAG, tag_reinit, taglen);
+
+ if (!TEST_true(EVP_CIPHER_CTX_get_params(ctx_reinit, get_tagparams))) {
+ errmsg = "AEAD_GET_TAG";
+ goto err;
+ }
+
+ /* Initialize in a single step */
+ if (!TEST_ptr(ctx_onestep = EVP_CIPHER_CTX_new())) {
+ errmsg = "CTX_ALLOC_BASE_CASE";
+ goto err;
+ }
+
+ if (!TEST_true(EVP_EncryptInit_ex(ctx_onestep, info->ciph,
+ NULL, NULL, NULL))) {
+ errmsg = "BASE_CASE_INIT";
+ goto err;
+ }
+
+ if (info->mode == EVP_CIPH_CCM_MODE) {
+ if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx_onestep, ivparams))) {
+ errmsg = "CCM_SET_IVLEN";
+ goto err;
+ }
+ if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx_onestep, tagparams))) {
+ errmsg = "CCM_SET_TAGLEN";
+ goto err;
+ }
+ }
+
+ if (!TEST_true(EVP_EncryptInit_ex(ctx_onestep, NULL, NULL, key2, iv2))) {
+ errmsg = "SINGLE_STEP_INIT";
+ goto err;
+ }
+
+ if (blocksz > 1)
+ EVP_CIPHER_CTX_set_padding(ctx_onestep, 0);
+
+ if (info->mode == EVP_CIPH_CCM_MODE
+ && !TEST_true(EVP_EncryptUpdate(ctx_onestep, NULL,
+ &tmplen, NULL, (int)pt_size))) {
+ errmsg = "CCM_DECLARE_PTLEN";
+ goto err;
+ }
+
+ if (!TEST_true(EVP_EncryptUpdate(ctx_onestep, ct_onestep,
+ &ct_onestep_len, pt, (int)pt_size))) {
+ errmsg = "ENCRYPT_UPDATE";
+ goto err;
+ }
+
+ if (!TEST_true(EVP_EncryptFinal_ex(ctx_onestep,
+ ct_onestep + ct_onestep_len, &ct_onestep_fin_len))) {
+ errmsg = "ENCRYPT_FINAL_ONE_STEP";
+ goto err;
+ }
+
+ ct_onestep_len += ct_onestep_fin_len;
+ get_tagparams[0] = OSSL_PARAM_construct_octet_string(
+ OSSL_CIPHER_PARAM_AEAD_TAG, tag_onestep, taglen);
+
+ if (!TEST_true(EVP_CIPHER_CTX_get_params(ctx_onestep,
+ get_tagparams))) {
+ errmsg = "AEAD_GET_TAG";
+ goto err;
+ }
+
+ /* Compare single-call vs key->iv */
+ if (!TEST_int_eq(ct_reinit_len, ct_onestep_len)
+ || !TEST_mem_eq(ct_reinit, ct_reinit_len,
+ ct_onestep, ct_onestep_len)) {
+ errmsg = "CT_MISMATCH_SINGLE_vs_REINIT";
+ goto err;
+ }
+
+ if (!TEST_mem_eq(tag_onestep, taglen, tag_reinit, taglen)) {
+ errmsg = "TAG_MISMATCH_SINGLE_vs_REINIT";
+ goto err;
+ }
+
+ testresult = 1;
+
+err:
+ if (errmsg != NULL) {
+ TEST_info("evp_stale_key_reinit_test %d, %s: %s",
+ idx, errmsg, info->name);
+ }
+
+ EVP_CIPHER_CTX_free(ctx_onestep);
+ EVP_CIPHER_CTX_free(ctx_reinit);
+ return testresult;
+}
+
static const unsigned char kGCMResetKey[] = {
0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94,
0x67, 0x30, 0x83, 0x08, 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
@@ -1354,6 +1603,7 @@ int setup_tests(void)
ADD_ALL_TESTS(test_evp_aead_enc_retval, OSSL_NELEM(aead_list));
ADD_ALL_TESTS(test_evp_aead_update_null_input, OSSL_NELEM(aead_list));
ADD_ALL_TESTS(test_gcm_reinit, OSSL_NELEM(gcm_reinit_tests));
+ ADD_ALL_TESTS(test_evp_stale_key_reinit, OSSL_NELEM(aead_list));
ADD_TEST(test_aes_gcm_ivlen_change_cve_2023_5363);
return 1;
}
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c
index 7c7940cf2b..9c1ea56fc5 100644
--- a/test/evp_extra_test.c
+++ b/test/evp_extra_test.c
@@ -6560,264 +6560,6 @@ err:
return testresult;
}
-/*
- * Verify stale key is not being used after providing a new key in multiple steps.
- * This test performs a full round of encryption and then changes the
- * key and then the IV in a multi-step init.
- * This is compared to another context without reinitialization to check
- * if the new key is loaded correctly.
- */
-static int test_evp_stale_key_reinit(int idx)
-{
- const EVP_CIPHER_TEST_INFO *info = &cipher_list[idx];
- EVP_CIPHER_CTX *ctx_reinit = NULL; /* used to test multi step init KEY->IV */
- EVP_CIPHER_CTX *ctx_onestep = NULL; /* base test with single step init */
-
- OSSL_PARAM ivparams[2];
- OSSL_PARAM tagparams[2];
- OSSL_PARAM get_tagparams[2];
-
- int ivlen = info->ivlen;
- unsigned char key[EVP_MAX_KEY_LENGTH] = { 0 };
- unsigned char key2[EVP_MAX_KEY_LENGTH] = { 0 };
- unsigned char iv[EVP_MAX_IV_LENGTH] = { 0 };
- unsigned char iv2[EVP_MAX_IV_LENGTH] = { 0 };
-
- size_t pt_size = 0, j = 0;
- unsigned char pt[128] = { 0 };
-
- unsigned char ct[128] = { 0 };
- int ct_len = 0;
- int ct_fin_len = 0;
-
- unsigned char ct_reinit[128] = { 0 };
- int ct_reinit_len = 0;
- int ct_reinit_fin_len = 0;
-
- unsigned char ct_onestep[128] = { 0 };
- int ct_onestep_len = 0;
- int ct_onestep_fin_len = 0;
-
- int taglen = info->taglen;
- unsigned char tag[EVPTEST_TAG_LEN_MAX] = { 0 };
- unsigned char tag_reinit[EVPTEST_TAG_LEN_MAX] = { 0 };
- unsigned char tag_onestep[EVPTEST_TAG_LEN_MAX] = { 0 };
-
- int blocksz = 0, tmplen = 0, testresult = 1, i = 0;
- char *errmsg = NULL;
-
- ivparams[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD_IVLEN, &ivlen);
- ivparams[1] = OSSL_PARAM_construct_end();
- tagparams[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_AEAD_TAGLEN, &taglen);
- tagparams[1] = OSSL_PARAM_construct_end();
-
- blocksz = EVP_CIPHER_get_block_size(info->ciph);
- pt_size = (blocksz > 1) ? (size_t)blocksz * 2 : 31;
-
- for (i = 0; i < info->keylen && i < (int)sizeof(key); i++)
- key[i] = (unsigned char)(0xA0 + i);
- for (i = 0; i < info->keylen && i < (int)sizeof(key2); i++)
- key2[i] = (unsigned char)(0xF0 + i);
-
- for (i = 0; i < info->ivlen && i < (int)sizeof(iv); i++)
- iv[i] = (unsigned char)(0xB0 + i);
- for (i = 0; i < info->ivlen && i < (int)sizeof(iv2); i++)
- iv2[i] = (unsigned char)(0xDA + i);
-
- for (j = 0; j < pt_size; j++)
- pt[j] = (unsigned char)(0xA7);
-
- if (!TEST_ptr(ctx_reinit = EVP_CIPHER_CTX_new())) {
- errmsg = "CTX_REINIT_ALLOC";
- goto err;
- }
-
- if (!TEST_true(EVP_EncryptInit_ex(ctx_reinit, info->ciph,
- NULL, NULL, NULL))) {
- errmsg = "CTX_FIRST_INIT";
- goto err;
- }
-
- if (info->taglen > 0 && info->mode == EVP_CIPH_CCM_MODE) {
- if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx_reinit, ivparams))) {
- errmsg = "CCM_SET_IVLEN";
- goto err;
- }
- if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx_reinit, tagparams))) {
- errmsg = "CCM_SET_TAGLEN";
- goto err;
- }
- }
-
- /* Initialize first with key */
- if (!TEST_true(EVP_EncryptInit_ex(ctx_reinit, NULL, NULL, key, iv))) {
- errmsg = "FIRST_KEY_INIT";
- goto err;
- }
-
- /* disable padding for non-aead block-aligned pt */
- if (info->taglen == 0 && blocksz > 1)
- EVP_CIPHER_CTX_set_padding(ctx_reinit, 0);
-
- if (info->taglen > 0 && info->mode == EVP_CIPH_CCM_MODE
- && !TEST_true(EVP_EncryptUpdate(ctx_reinit, NULL,
- &tmplen, NULL, (int)pt_size))) {
- errmsg = "CCM_DECLARE_PTLEN";
- goto err;
- }
-
- if (!TEST_true(EVP_EncryptUpdate(ctx_reinit, ct, &ct_len,
- pt, (int)pt_size))) {
- errmsg = "ENCRYPT_UPDATE";
- goto err;
- }
-
- if (!TEST_true(EVP_EncryptFinal_ex(ctx_reinit, ct + ct_len,
- &ct_fin_len))) {
- errmsg = "ENCRYPT_FINAL";
- goto err;
- }
-
- ct_len += ct_fin_len;
- if (info->taglen > 0) {
- /* override taglen from context if available */
- int tl = EVP_CIPHER_CTX_get_tag_length(ctx_reinit);
-
- if (tl > 0)
- taglen = tl;
-
- get_tagparams[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
- tag, taglen);
- get_tagparams[1] = OSSL_PARAM_construct_end();
- if (!TEST_true(EVP_CIPHER_CTX_get_params(ctx_reinit, get_tagparams))) {
- errmsg = "AEAD_GET_TAG";
- goto err;
- }
- }
-
- /* use same context with a different key and iv in multiple steps */
- if (!TEST_true(EVP_EncryptInit_ex(ctx_reinit, NULL, NULL, key2, NULL))) {
- errmsg = "REINIT_KEY";
- goto err;
- }
- if (!TEST_true(EVP_EncryptInit_ex(ctx_reinit, NULL, NULL, NULL, iv2))) {
- errmsg = "REINIT_IV";
- goto err;
- }
-
- if (info->taglen > 0 && info->mode == EVP_CIPH_CCM_MODE
- && !TEST_true(EVP_EncryptUpdate(ctx_reinit, NULL,
- &tmplen, NULL, (int)pt_size))) {
- errmsg = "CCM_DECLARE_PTLEN";
- goto err;
- }
-
- if (!TEST_true(EVP_EncryptUpdate(ctx_reinit, ct_reinit, &ct_reinit_len,
- pt, (int)pt_size))) {
- errmsg = "ENCRYPT_UPDATE";
- goto err;
- }
-
- if (!TEST_true(EVP_EncryptFinal_ex(ctx_reinit, ct_reinit + ct_reinit_len,
- &ct_reinit_fin_len))) {
- errmsg = "ENCRYPT_FINAL";
- goto err;
- }
-
- ct_reinit_len += ct_reinit_fin_len;
- get_tagparams[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
- tag_reinit, taglen);
-
- if (info->taglen > 0
- && !TEST_true(EVP_CIPHER_CTX_get_params(ctx_reinit, get_tagparams))) {
- errmsg = "AEAD_GET_TAG";
- goto err;
- }
-
- /* Initialize in a single step */
- if (!TEST_ptr(ctx_onestep = EVP_CIPHER_CTX_new())) {
- errmsg = "CTX_ALLOC_BASE_CASE";
- goto err;
- }
-
- if (!TEST_true(EVP_EncryptInit_ex(ctx_onestep, info->ciph,
- NULL, NULL, NULL))) {
- errmsg = "BASE_CASE_INIT";
- goto err;
- }
-
- if (info->taglen > 0 && info->mode == EVP_CIPH_CCM_MODE) {
- if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx_onestep, ivparams))) {
- errmsg = "CCM_SET_IVLEN";
- goto err;
- }
- if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx_onestep, tagparams))) {
- errmsg = "CCM_SET_TAGLEN";
- goto err;
- }
- }
-
- if (!TEST_true(EVP_EncryptInit_ex(ctx_onestep, NULL, NULL, key2, iv2))) {
- errmsg = "SINGLE_STEP_INIT";
- goto err;
- }
-
- if (info->taglen == 0 && blocksz > 1)
- EVP_CIPHER_CTX_set_padding(ctx_onestep, 0);
-
- if (info->taglen > 0 && info->mode == EVP_CIPH_CCM_MODE
- && !TEST_true(EVP_EncryptUpdate(ctx_onestep, NULL,
- &tmplen, NULL, (int)pt_size))) {
- errmsg = "CCM_DECLARE_PTLEN";
- goto err;
- }
-
- if (!TEST_true(EVP_EncryptUpdate(ctx_onestep, ct_onestep,
- &ct_onestep_len, pt, (int)pt_size))) {
- errmsg = "ENCRYPT_UPDATE";
- goto err;
- }
-
- if (!TEST_true(EVP_EncryptFinal_ex(ctx_onestep, ct_onestep + ct_onestep_len,
- &ct_onestep_fin_len))) {
- errmsg = "ENCRYPT_FINAL_ONE_STEP";
- goto err;
- }
-
- ct_onestep_len += ct_onestep_fin_len;
- get_tagparams[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
- tag_onestep, taglen);
-
- if (info->taglen > 0
- && !TEST_true(EVP_CIPHER_CTX_get_params(ctx_onestep, get_tagparams))) {
- errmsg = "AEAD_GET_TAG";
- goto err;
- }
-
- /* Compare single-call vs key->iv */
- if (!TEST_int_eq(ct_reinit_len, ct_onestep_len)
- || !TEST_mem_eq(ct_reinit, ct_reinit_len, ct_onestep, ct_onestep_len)) {
- errmsg = "CT_MISMATCH_SINGLE_vs_REINIT";
- goto err;
- }
-
- if (info->taglen > 0
- && !TEST_mem_eq(tag_onestep, taglen, tag_reinit, taglen)) {
- errmsg = "TAG_MISMATCH_SINGLE_vs_REINIT";
- goto err;
- }
-err:
- if (errmsg != NULL) {
- TEST_info("evp_stale_key_integrity_test %d, %s: %s",
- idx, errmsg, info->name);
- testresult = 0;
- }
- EVP_CIPHER_CTX_free(ctx_onestep);
- EVP_CIPHER_CTX_free(ctx_reinit);
-
- return testresult;
-}
-
/*
* Decrypt roundtrip:
* Encrypt single-call, decrypt via multi-step init, verify plaintext recovery.
@@ -10287,7 +10029,6 @@ int setup_tests(void)
setup_cipher_list();
ADD_ALL_TESTS(test_evp_diff_order_init, cipher_list_n);
- ADD_ALL_TESTS(test_evp_stale_key_reinit, cipher_list_n);
ADD_ALL_TESTS(test_evp_decrypt_roundtrip_multistep, cipher_list_n);
ADD_ALL_TESTS(test_evp_aead_tag_direction, cipher_list_n);
ADD_ALL_TESTS(test_evp_aead_tag_reject, cipher_list_n);