Commit 46415f5e37 for openssl.org

commit 46415f5e3759fd0d68f02dd0281e19256854b530
Author: Hamzah Yamani <hamzah.yamani125@gmail.com>
Date:   Fri Nov 6 18:26:51 2026 -0500

    Migrate test_aes_siv_ctx_dec_retval to evp_aead_test.c

    Move test_aes_siv_ctx_dec_retval() out of evp_extra_test.c into
    evp_aead_test.c, rename it to test_evp_aead_dec_retval() and run it over
    aead_list instead of only AES-128-SIV.

    The check is that after a completed decrypt, a further
    EVP_DecryptUpdate() on the same context is rejected, and the
    EVP_DecryptFinal_ex() that follows fails as well, instead of reporting
    the earlier success. GCM, OCB and ASCON mark the context finished. SIV
    stores the result of the last operation, so a rejected Update makes the
    following Final fail.

    CCM, GCM-SIV and ChaCha20-Poly1305 are skipped with TEST_skip(): what
    Final should do after the rejected Update is not settled for them yet.
    ChaCha20-Poly1305 reports the same mode as ASCON, so it is matched by
    name.

    Key and IV are sized from the resolved cipher parameters, the contexts
    are set up with EVP_*Init_ex2(), the tag is passed with OSSL_PARAM, and
    the plaintext is compared with TEST_mem_eq().

    Assisted-by: Claude:claude-sonnet-5
    Reviewed-by: Neil Horman <nhorman@openssl.org>
    Reviewed-by: Eugene Syromiatnikov <esyr@openssl.org>
    Reviewed-by: Billy Bob Brumley <bbb@iki.fi>
    Merge-date: Thu Nov 19 15:33:48 2026
    Merged-from: https://github.com/openssl/openssl/pull/33582

diff --git a/test/evp_aead_test.c b/test/evp_aead_test.c
index 6c7e610400..391799b04d 100644
--- a/test/evp_aead_test.c
+++ b/test/evp_aead_test.c
@@ -858,6 +858,88 @@ err:
     return ret;
 }

+/*
+ * After a completed decrypt, a further Update on the same context must be
+ * rejected, and the Final that follows must fail as well instead of reporting
+ * the earlier success.
+ *
+ * How a cipher gets there differs: GCM, OCB and ASCON mark the context
+ * finished, while SIV does its single crypto operation inside the payload
+ * Update and lets any later failed operation overwrite the stored result.
+ *
+ * CCM, GCM-SIV and ChaCha20-Poly1305 are skipped, as in
+ * test_evp_aead_enc_retval(): what Final should do after the rejected Update
+ * is not settled for them yet.
+ */
+static int test_evp_aead_dec_retval(int idx)
+{
+    static const unsigned char in[] = "input";
+
+    const AEAD_DATA *info = &aead_list[idx];
+    EVP_CIPHER_CTX *enc_ctx = NULL;
+    EVP_CIPHER_CTX *dec_ctx = NULL;
+    OSSL_PARAM tagparams[2];
+
+    unsigned char key[EVP_MAX_KEY_LENGTH] = { 0 };
+    unsigned char iv[EVP_MAX_IV_LENGTH] = { 0 };
+    unsigned char tag[EVPTEST_TAG_LEN_MAX] = { 0 };
+    unsigned char ct[sizeof(in) + EVP_MAX_BLOCK_LENGTH] = { 0 };
+    unsigned char out[sizeof(in) + EVP_MAX_BLOCK_LENGTH] = { 0 };
+
+    int i = 0, len = 0, ret = 0;
+
+    if (info->mode == EVP_CIPH_CCM_MODE
+        || info->mode == EVP_CIPH_GCM_SIV_MODE
+        || EVP_CIPHER_is_a(info->ciph, "ChaCha20-Poly1305"))
+        return TEST_skip("Cipher %s skipped, Final after a rejected Update "
+                         "is not settled for it",
+            info->name);
+
+    for (i = 0; i < info->keylen; i++)
+        key[i] = (unsigned char)(0xA0 + i);
+    for (i = 0; i < info->ivlen; i++)
+        iv[i] = (unsigned char)(0xB0 + i);
+
+    tagparams[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
+        tag, info->taglen);
+    tagparams[1] = OSSL_PARAM_construct_end();
+
+    /* encrypt, to get a valid ciphertext and tag */
+    if (!TEST_ptr(enc_ctx = EVP_CIPHER_CTX_new())
+        || !TEST_true(EVP_EncryptInit_ex2(enc_ctx, info->ciph, key, iv, NULL))
+        || !TEST_true(EVP_EncryptUpdate(enc_ctx, ct, &len, in, sizeof(in)))
+        || !TEST_true(EVP_EncryptFinal_ex(enc_ctx, ct + len, &len))
+        || !TEST_true(EVP_CIPHER_CTX_get_params(enc_ctx, tagparams))) {
+        TEST_info("%s: encrypt", info->name);
+        goto err;
+    }
+
+    /* decrypt on a separate context */
+    if (!TEST_ptr(dec_ctx = EVP_CIPHER_CTX_new())
+        || !TEST_true(EVP_DecryptInit_ex2(dec_ctx, info->ciph, key, iv, NULL))
+        || !TEST_true(EVP_CIPHER_CTX_set_params(dec_ctx, tagparams))
+        || !TEST_true(EVP_DecryptUpdate(dec_ctx, out, &len, ct, sizeof(in)))
+        || !TEST_true(EVP_DecryptFinal_ex(dec_ctx, out + len, &len))
+        || !TEST_mem_eq(out, sizeof(in), in, sizeof(in))) {
+        TEST_info("%s: decrypt", info->name);
+        goto err;
+    }
+
+    /* Decryption is fine, provoke error by repeating it on the same context */
+    if (!TEST_false(EVP_DecryptUpdate(dec_ctx, out, &len, ct, sizeof(in)))
+        || !TEST_false(EVP_DecryptFinal_ex(dec_ctx, out + len, &len))) {
+        TEST_info("%s: decrypt Update, Final after Final", info->name);
+        goto err;
+    }
+
+    ret = 1;
+err:
+    ERR_clear_error();
+    EVP_CIPHER_CTX_free(dec_ctx);
+    EVP_CIPHER_CTX_free(enc_ctx);
+    return ret;
+}
+
 /*
  * An update with no input and a nonzero length must fail, except for the
  * CCM message length, which is passed with no input and no output. This is
@@ -1949,6 +2031,7 @@ int setup_tests(void)
     ADD_ALL_TESTS(test_evp_aead_get_tag_pairwise, OSSL_NELEM(aead_list));
     ADD_ALL_TESTS(test_evp_final_no_tag, OSSL_NELEM(aead_list));
     ADD_ALL_TESTS(test_evp_aead_enc_retval, OSSL_NELEM(aead_list));
+    ADD_ALL_TESTS(test_evp_aead_dec_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));
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c
index 7940d26866..822be5fc4c 100644
--- a/test/evp_extra_test.c
+++ b/test/evp_extra_test.c
@@ -8312,58 +8312,6 @@ err:
     return ret;
 }

-static int test_aes_siv_ctx_dec_retval(void)
-{
-    unsigned char key[32] = { 7 };
-    unsigned char in[6] = "input";
-    unsigned char ct[6] = { 0 };
-
-    unsigned char tagbuf[16], out[16] = { 0 };
-    int len, ret = 0;
-    EVP_CIPHER_CTX *enc_ctx = NULL;
-    EVP_CIPHER_CTX *dec_ctx = NULL;
-
-    EVP_CIPHER *cipher = EVP_CIPHER_fetch(testctx, "AES-128-SIV", testpropq);
-
-    if (cipher == NULL)
-        return TEST_skip("AES-128-SIV cipher is not available");
-
-    enc_ctx = EVP_CIPHER_CTX_new();
-    if (!TEST_ptr(enc_ctx)
-        || !TEST_true(EVP_EncryptInit_ex(enc_ctx, cipher, NULL, key, NULL))
-        || !TEST_true(EVP_EncryptUpdate(enc_ctx, ct, &len, in, sizeof(in)))
-        || !TEST_true(EVP_CIPHER_CTX_ctrl(enc_ctx, EVP_CTRL_AEAD_GET_TAG, sizeof(tagbuf), tagbuf))
-        || !TEST_true(EVP_EncryptFinal_ex(enc_ctx, ct + len, &len)))
-        goto err;
-
-    dec_ctx = EVP_CIPHER_CTX_new();
-    if (!TEST_ptr(dec_ctx)
-        || !TEST_true(EVP_DecryptInit_ex(dec_ctx, cipher, NULL, key, NULL))
-        || !TEST_true(EVP_CIPHER_CTX_ctrl(dec_ctx, EVP_CTRL_AEAD_SET_TAG,
-            sizeof(tagbuf), tagbuf))
-        || !TEST_true(EVP_DecryptUpdate(dec_ctx, out, &len, ct, sizeof(in)))
-        || !TEST_true(EVP_DecryptFinal_ex(dec_ctx, out + len, &len))
-        || !TEST_true(0 == memcmp(out, in, sizeof(in)))) {
-        goto err;
-    }
-
-    /*
-     * Positive usecase successful,
-     * provoke the error, by repeating decrypt on same context.
-     */
-    if (!TEST_false(EVP_DecryptUpdate(dec_ctx, out, &len, ct, sizeof(ct)))
-        || (!TEST_false(EVP_DecryptFinal_ex(dec_ctx, out + len, &len))))
-        goto err;
-
-    ret = 1;
-
-err:
-    EVP_CIPHER_CTX_free(dec_ctx);
-    EVP_CIPHER_CTX_free(enc_ctx);
-    EVP_CIPHER_free(cipher);
-    return ret;
-}
-
 static int test_invalid_ctx_for_digest(void)
 {
     int ret;
@@ -9708,7 +9656,6 @@ int setup_tests(void)
     /* Test cases for CVE-2026-45446 */
     ADD_TEST(test_aes_gcm_siv_empty_data);
     ADD_TEST(test_aes_siv_ctx_reuse);
-    ADD_TEST(test_aes_siv_ctx_dec_retval);
     ADD_TEST(test_aes_gcm_siv_reinit_aad_replay);
     ADD_TEST(test_aes_gcm_siv_reinit_aad_prefix);
     ADD_TEST(test_aes_gcm_siv_reinit_aad_replay_no_nonce);