Skip to content

Check AEAD tags on EVP_Cipher finalization and fix ARIA-GCM, derive_init and PKCS#7 PWRI defects - #12415

Open
dgarske wants to merge 1 commit into
wolfSSL:masterfrom
dgarske:evp_pkcs7_checks
Open

dgarske wants to merge 1 commit into
wolfSSL:masterfrom
dgarske:evp_pkcs7_checks

Conversation

@dgarske

@dgarske dgarske commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Description

  • EVP_Cipher(ctx, NULL, NULL, 0) decryption finalization for AES-GCM, AES-CCM and SM4 never ran a decrypt when no payload call had been made, so a forged empty message was accepted. It now checks the tag of the empty message and returns the payload call's authentication error if that failed, matching OpenSSL. (F-16663)
  • ChaCha20-Poly1305 was missing from IsCipherTypeAEAD(), so its EVP_Cipher() finalization returned success without checking the Poly1305 tag. (F-17537)
  • The ARIA-GCM EVP_Cipher() and EVP_CipherFinal() paths wrote the tag past the caller's output on encrypt and appended it to the caller's input on decrypt. Both now go through an internal buffer sized for the tag. (F-15320)
  • EVP_PKEY_derive_init() freed peerKey without clearing it, so re-initializing a derive context used and then double-freed it. (F-18634)
  • PKCS#7 PWRI key unwrap took the CEK length from the unauthenticated IV, and the decoders zero-padded a short CEK. A zero length is now rejected, and both decoders reject a CEK whose length doesn't match the content cipher's key size. (F-19085)
  • The AF_ALG wc_AesGcmDecrypt() asked the kernel for zero bytes of output for an empty message with no AAD, so the kernel never ran the decrypt and any tag was accepted, also through wc_GmacVerify(). It now reads into a 1-byte buffer so the tag is checked. (F-19169)

The EVP cipher context gains authMsgTried/authMsgDone, and wc_PKCS7 gains pwriCekSz (last member). #11689 adds the same two EVP fields for its CCM rework, so whichever lands second rebases.

Resolves F-16663, F-17537, F-15320, F-18634, F-19085, F-19169.

Testing

New tests in tests/api (test_evp_cipher.c, test_evp_pkey.c, test_pkcs7.c) each fail without the fix: forged empty GCM/CCM/ChaCha20-Poly1305 messages accepted, an ASan overflow (ARIA-GCM), an ASan use-after-free (derive_init), a short PWRI CEK decoded.

  • --enable-all, --enable-opensslextra with AES-GCM/CCM and ChaCha20-Poly1305 (non-streaming GCM), --enable-opensslall --enable-pkcs7 --enable-pwdbased, --disable-aesgcm, and --enable-cryptonly, with ASan and -Werror.
  • The AF_ALG GCM change was compile-checked only; the AF_ALG CI job runs the new test_wc_AesGcmDecrypt_EmptyTag.
  • ARIA-GCM was tested against a stub of the MagicCrypto library. SM4 was not built (no SM4 package here); its changes mirror the AES-GCM ones.

Copilot AI balanced review requested due to automatic review settings December 4, 2026 21:22
@dgarske dgarske self-assigned this Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +256 B (+0.0%, 808,296 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: .text +64 B (+0.0%, 227,452 B / 262,144 B, total: 87% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +140 B, text +14,576 B (+5.2%, 297,188 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Stale authentication flags let reused SM4-GCM/CCM contexts accept empty messages without verifying tags, while ChaCha20-Poly1305 rejects valid empty messages without AAD.

2 open issues
What changed in this PR

This PR hardens wolfSSL’s EVP compatibility layer and PKCS#7 decoding against authentication, buffer-handling, and key-lifetime defects.

Changes:

  • Checks AEAD authentication during finalization and isolates ARIA-GCM tag storage.
  • Clears freed derive-context peer keys and rejects invalid PWRI content-key lengths.
  • Adds regression tests for authentication, exact-sized buffers, context reuse, and malformed keys.
File Description
wolfssl/​wolfcrypt/​pkcs7.h Tracks unwrapped PWRI key length.
wolfssl/​openssl/​evp.h Adds AEAD message-state flags.
wolfcrypt/​src/​pkcs7.c Validates PWRI content-key lengths.
wolfcrypt/​src/​evp.c Updates authentication, ARIA buffering, and peer-key cleanup.
tests/​api/​test_pkcs7.h Registers PWRI regression tests.
tests/​api/​test_pkcs7.c Tests shortened and zero-length PWRI keys.
tests/​api/​test_evp_pkey.c Tests repeated derive initialization.
tests/​api/​test_evp_cipher.h Registers cipher regression tests.
tests/​api/​test_evp_cipher.c Tests AEAD finalization and exact-sized ARIA buffers.

Review effort: Balanced

Comment thread wolfcrypt/src/evp.c Resolved
Comment thread wolfcrypt/src/evp.c Resolved
@dgarske
dgarske force-pushed the evp_pkcs7_checks branch 2 times, most recently from 952a534 to 4e7a37c Compare December 5, 2026 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants