fix(libSceLibreSsl): report why EVP_DecryptFinal_ex fails #16649

Merged
AlexWhitehouse merged 1 commit from upstream-pr/16649 into main 2026-11-11 23:37:54 +00:00
AlexWhitehouse commented 2026-11-07 13:04:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexWhitehouse on 2026-11-07T13:04:25Z.


What

In libSceLibreSsl, EVP_DecryptFinal_ex (NID urpDB9rHb4s), and EVP_CipherFinal_ex (FgP0jMzeUU0) in decrypt mode, returned 0 for bad padding or a wrong final block without queueing an error. A caller that checks ERR_get_error() after a failed decrypt found an empty queue. On main, a 16-byte block whose last padding byte is 0 gives ret=0 length=0 error=0.

LibreSSL 2.5.4's evp_enc.c, lines 514–539, is the version the existing port and its debt entries follow, and it queues a reason on each failing branch:

Branch Reason (from evp.h)
EVP_CIPH_NO_PADDING with buffered bytes left EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH (138)
bytes still buffered, or no final block held EVP_R_WRONG_FINAL_BLOCK_LENGTH (109)
block size larger than the final buffer EVP_R_BAD_BLOCK_LENGTH (136)
padding byte 0 or larger than the block, or padding bytes that don't match EVP_R_BAD_DECRYPT (100)

The non-custom-cipher path of DecryptFinal in core/libs/prx/libSceLibreSsl/src/EvpCipherCtx.cpp now queues the same reasons through the module's existing LibreSslPutError_nid_no_patch:

  • library ERR_LIB_EVP (6) and function 0xfff, as EVPerror does in that version's err.h;
  • so ERR_get_error() returns, for example, 0x06fff064 for BAD_DECRYPT.

Return values, output lengths and output bytes are unchanged. Nothing else in the file changes.

The existing debt entry's clause that failures queue no error code now names this exception. The rest of the entry is unchanged.

Tested

  • New TestDecryptFinalErrors in the registered guest_libressl_evp_cipher_ctx (core/libs/tests/GuestLibreSslEvpCipherCtx.cpp). It runs nine cases through both EVP_DecryptFinal_ex and EVP_CipherFinal_ex in decrypt mode:

    • padding byte 0, 17 and mismatched padding;
    • no-padding with 15 bytes left;
    • no final block, and a partial block;
    • an oversized block size;
    • valid padding, and valid no-padding.

    Each case checks the return value, *outl == 0, untouched output bytes, the library, function and reason fields of ERR_get_error(), and that the queue is empty afterwards (for the valid cases, empty to begin with). Every existing assertion is kept.

  • Linux x86-64 (Ubuntu 26.04, GCC 15, Release), the libSceLibreSsl tests:

    • on main, the new test fails with decrypt final scenario 0: ret=0 length=0 error=0x0 expected EVP/fff/100;
    • with this PR, 63/63 pass (63/63 on main without the new test).
  • Mutation check: changing the BAD_DECRYPT reason from 100 to 109 fails the test.

  • Windows 11 (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release):

    • the libSceLibreSsl tests pass 61/61 at 7ad4690251, with no warnings in the changed files;
    • with main's EvpCipherCtx.cpp, guest_libressl_evp_cipher_ctx fails with the same decrypt final scenario 0: ret=0 length=0 error=0x0 expected EVP/fff/100.
  • python3 tools/check_conventions.py --base origin/main: all conventions checks passed.

  • python3 tools/pr_overlap.py --branch fix/libscelibressl-decrypt-final-errors: no open PR changes EVP_DecryptFinal_ex or EVP_CipherFinal_ex.

    • Same source file: #16539 (wrap handling), #16255 and #16249 (allocation) edit EVP_CipherInit_ex and merge cleanly in code.
    • Same test file: #16541 and #16543 add tests to it, and the new test function sits away from their insertion points.
    • Debt entry: #14212, #16249 and #16255 rewrite the same entry, so the debt line conflicts; whichever merges second rebases it.
    • #16079 (digest/HMAC errors in Evp.cpp) touches different functions.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5201877685 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16649). Originally posted by [AlexWhitehouse](https://github.com/AlexWhitehouse) on 2026-11-07T13:04:25Z. --- ### What In libSceLibreSsl, `EVP_DecryptFinal_ex` (NID `urpDB9rHb4s`), and `EVP_CipherFinal_ex` (`FgP0jMzeUU0`) in decrypt mode, returned 0 for bad padding or a wrong final block without queueing an error. A caller that checks `ERR_get_error()` after a failed decrypt found an empty queue. On `main`, a 16-byte block whose last padding byte is 0 gives `ret=0 length=0 error=0`. [LibreSSL 2.5.4's `evp_enc.c`](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/evp/evp_enc.c#L514-L539), lines 514–539, is the version the existing port and its debt entries follow, and it queues a reason on each failing branch: | Branch | Reason (from `evp.h`) | |---|---| | `EVP_CIPH_NO_PADDING` with buffered bytes left | `EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH` (138) | | bytes still buffered, or no final block held | `EVP_R_WRONG_FINAL_BLOCK_LENGTH` (109) | | block size larger than the `final` buffer | `EVP_R_BAD_BLOCK_LENGTH` (136) | | padding byte 0 or larger than the block, or padding bytes that don't match | `EVP_R_BAD_DECRYPT` (100) | The non-custom-cipher path of `DecryptFinal` in `core/libs/prx/libSceLibreSsl/src/EvpCipherCtx.cpp` now queues the same reasons through the module's existing `LibreSslPutError_nid_no_patch`: - library `ERR_LIB_EVP` (6) and function 0xfff, as `EVPerror` does in that version's `err.h`; - so `ERR_get_error()` returns, for example, `0x06fff064` for `BAD_DECRYPT`. Return values, output lengths and output bytes are unchanged. Nothing else in the file changes. The existing debt entry's clause that failures queue no error code now names this exception. The rest of the entry is unchanged. ### Tested - **New `TestDecryptFinalErrors`** in the registered `guest_libressl_evp_cipher_ctx` (`core/libs/tests/GuestLibreSslEvpCipherCtx.cpp`). It runs nine cases through both `EVP_DecryptFinal_ex` and `EVP_CipherFinal_ex` in decrypt mode: - padding byte 0, 17 and mismatched padding; - no-padding with 15 bytes left; - no final block, and a partial block; - an oversized block size; - valid padding, and valid no-padding. Each case checks the return value, `*outl == 0`, untouched output bytes, the library, function and reason fields of `ERR_get_error()`, and that the queue is empty afterwards (for the valid cases, empty to begin with). Every existing assertion is kept. - **Linux x86-64** (Ubuntu 26.04, GCC 15, Release), the libSceLibreSsl tests: - on `main`, the new test fails with `decrypt final scenario 0: ret=0 length=0 error=0x0 expected EVP/fff/100`; - with this PR, 63/63 pass (63/63 on `main` without the new test). - **Mutation check:** changing the `BAD_DECRYPT` reason from 100 to 109 fails the test. - **Windows 11** (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release): - the libSceLibreSsl tests pass 61/61 at 7ad4690251, with no warnings in the changed files; - with `main`'s `EvpCipherCtx.cpp`, `guest_libressl_evp_cipher_ctx` fails with the same `decrypt final scenario 0: ret=0 length=0 error=0x0 expected EVP/fff/100`. - `python3 tools/check_conventions.py --base origin/main`: all conventions checks passed. - **`python3 tools/pr_overlap.py --branch fix/libscelibressl-decrypt-final-errors`:** no open PR changes `EVP_DecryptFinal_ex` or `EVP_CipherFinal_ex`. - **Same source file:** #16539 (wrap handling), #16255 and #16249 (allocation) edit `EVP_CipherInit_ex` and merge cleanly in code. - **Same test file:** #16541 and #16543 add tests to it, and the new test function sits away from their insertion points. - **Debt entry:** #14212, #16249 and #16255 rewrite the same entry, so the debt line conflicts; whichever merges second rebases it. - **#16079** (digest/HMAC errors in `Evp.cpp`) touches different functions. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!16649
No description provided.