fix(libSceLibreSsl3): run cipher updates with the LibreSSL 4.0.0 prologue #18861

Merged
Ducengarth merged 3 commits from upstream-pr/18861 into main 2026-11-16 15:52:53 +00:00
Ducengarth commented 2026-11-12 12:39:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ducengarth on 2026-11-12T12:39:56Z.


What

Give libSceLibreSsl3 its own EVP_EncryptUpdate, EVP_DecryptUpdate and EVP_CipherUpdate (NIDs OnoZKqFGNjA, IXiXAHDFjeU, w03a1pfG8oM) with the LibreSSL 4.0.0 prologue, instead of forwarding them to libSceLibreSsl's 2.5.4 updates. This is the follow-up #16249 left open. Source: LibreSSL 4.0.0 evp/evp_cipher.c at ce488b78, the revision the library's debt file already uses and the version OpenSSL_version reports.

The forwarded 2.5.4 updates differ from 4.0.0 in ways that callers of libSceLibreSsl3 can see:

  • Empty update: 2.5.4 passes a zero-length update to a custom cipher. With a null input, EVP_aes_*_gcm and EVP_chacha20_poly1305 take it as the final call and compute or check the tag early. 4.0.0 returns 1 without calling the cipher, except in CCM mode.
  • Negative length: 2.5.4 passes it to a custom cipher's do_cipher cast to size_t. 4.0.0 returns 0.
  • *outl: 4.0.0 sets it to 0 first, so it is 0 whenever the call fails. 2.5.4 leaves it unset on several failure paths.
  • Held decrypt block: 4.0.0 clears final_used before the inner update, so a failed update drops the held block.
  • Invalid state: 4.0.0 checks that the block size is a power of two, and queues EVP_R_BAD_BLOCK_LENGTH (136) for an out-of-range buffered count instead of failing silently. It also queues EVP_R_TOO_LARGE (164) for its overflow checks.

The code is a line-for-line port over libSceLibreSsl's EVP_CIPHER_CTX, which libSceLibreSsl3 already shares (buf_len stands for 4.0.0's partial_len; buf, final, final_used and flags match). It calls the cipher's do_cipher directly and never the parent's update exports, so the Linux NID patcher cannot turn a call to the parent into a call to itself.

  • New src/EvpCipherUpdate.cpp. The exports use the _nid_disambig1_nid_postfix names, as EVP_PKEY_free and EVP_PKEY_get1_RSA already do, so they hash to the same NIDs as the parent's (checked with tools/nid_names.py). They are declared once in include/Api.hpp.
  • The three Windows forwarders are removed from ParentExports.cpp, so the NIDs are defined only once in the DLL. That leaves 387 forwarders, the count the existing debt entry already gives.
  • Undefined upstream cases: a null context, cipher or outl, a cipher without do_cipher, and a null buffer that a non-custom cipher or a memcpy would read or write throw std::invalid_argument. LibreSSL dereferences null in these cases.
  • Debt: docs/dev/debt/libSceLibreSsl3.md gets an entry for the new file. The ChaCha entry loses its zero-length-update gap, and the AES-CCM entry now names only the forwarded finals.

Known gap, recorded in the new entry: on Linux and macOS, imports bind by NID alone. So libSceLibreSsl importers and libSceLibreSsl3 importers both reach whichever of the two modules the loader searches first. The overrides that already exist (EVP_PKEY_free, EVP_PKEY_new) have the same limitation. The finals (EVP_EncryptFinal_ex, EVP_DecryptFinal_ex, EVP_CipherFinal_ex) stay forwarded here; #18942, stacked on this one, ports them.

No title's trace or disassembly was used, and nothing was measured on a PS5.

Overlap: before writing the code I indexed the diffs of all 1755 open pull requests. Only #17299 and #17303 touch these three NIDs: they rename the same ParentExports.cpp forwarders that this PR removes. That is a line conflict, not the same function. Whichever merges second drops or rebases those six lines, and I will rebase this one as needed. pr_overlap --branch reports the same two pull requests and no other with the same exports or new files. It also flags these three exports as defined in libSceLibreSsl too, which is intentional: that is the override. 33 pull requests that conflict with main could not be compared by the tool. I scanned the lines each of them adds: none mentions these exports, their NIDs or an EvpCipherUpdate file. Their shared files are Api.hpp, CMakeLists.txt and the debt file, so the conflicts there are adjacent-line ones.

Tested

Rebased onto main 4e4132f468. No title or homebrew. Release builds of every guest_libressl* and guest_crypto_memcmp test target and their dependencies, on two systems:

  • Windows 11, MinGW-w64 GCC 15.2 (WinLibs), Ninja.
  • Linux: Ubuntu 24.04 under WSL2 (kernel 6.18), GCC 13.3, CMake 3.28, Ninja, built from an LF checkout.

Results:

  • Windows: ctest -R '^guest_(libressl|crypto_memcmp)' passes 142/142, including the new guest_libressl3_evp_cipher_update.
  • Linux: the same selection passes 144/144. That includes guest_libressl3_mixed_evp and guest_libressl3_pkey_lifecycle, which load the NID-patched libSceLibreSsl3.prx together with libSceLibreSsl.prx.
  • The existing cipher tests still pass on both (guest_libressl3_chacha, guest_libressl3_aes_ccm, guest_libressl3_evp_cipher_init, guest_libressl_evp_cipher*). Their checks call libSceLibreSsl's own updates.
  • Patched modules: on Windows, objdump -p on the NID-patched libSceLibreSsl3.prx lists OnoZKqFGNjA, IXiXAHDFjeU and w03a1pfG8oM once each, and no EVP_*Update name. On Linux, nm -D --defined-only on the patched module lists the same three NIDs as defined symbols.
  • Repository checks on Windows:
    • the 65 public_headers* tests, all conventions_* and debt_* tests, export_gap, unbuilt_exports, tools_nid_name_audit, nid_elf_duplicates and the pr_overlap_* tests pass;
    • debt_split_DebtSplitTests_test_main_reports_non_git_repository fails only because this checkout keeps its temporary directory inside the repository;
    • guest_export_lookup, windows_guest_export_aliases and strict_nid_filter need the relinker executable, which was not built.
  • python3 tools/check_conventions.py --base upstream/main passes.
  • Earlier, on main f0c378b784: the same Windows (141/141) and Linux (143/143) runs passed before the rebase.
  • Mutations: the new test kills 13 deliberate mutations of the implementation. Twelve remove the empty-update return, the CCM exception, the negative-length check, the upfront *outl = 0, the final_used clear, the power-of-two check, the buf_len advance, the input advance, the tail buffering, the no-padding path, the held block and the error queueing. The thirteenth inverts the EVP_CipherUpdate dispatch.
  • Review: an independent line-by-line comparison with 4.0.0 evp_cipher.c covered check order, block sizes 0, 1, 12, negative and INT_MIN, the signed/unsigned size comparisons, the overflow checks and when state changes on failure. It found no difference outside the documented throws.

Not covered: macOS, and a run inside a title.

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:5277548615 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18861). Originally posted by [Ducengarth](https://github.com/Ducengarth) on 2026-11-12T12:39:56Z. --- ### What Give `libSceLibreSsl3` its own `EVP_EncryptUpdate`, `EVP_DecryptUpdate` and `EVP_CipherUpdate` (NIDs `OnoZKqFGNjA`, `IXiXAHDFjeU`, `w03a1pfG8oM`) with the LibreSSL 4.0.0 prologue, instead of forwarding them to libSceLibreSsl's 2.5.4 updates. This is the follow-up #16249 left open. Source: LibreSSL 4.0.0 [`evp/evp_cipher.c`](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/evp/evp_cipher.c) at `ce488b78`, the revision the library's debt file already uses and the version `OpenSSL_version` reports. The forwarded 2.5.4 updates differ from 4.0.0 in ways that callers of libSceLibreSsl3 can see: - **Empty update**: 2.5.4 passes a zero-length update to a custom cipher. With a null input, `EVP_aes_*_gcm` and `EVP_chacha20_poly1305` take it as the final call and compute or check the tag early. 4.0.0 returns 1 without calling the cipher, except in CCM mode. - **Negative length**: 2.5.4 passes it to a custom cipher's `do_cipher` cast to `size_t`. 4.0.0 returns 0. - **`*outl`**: 4.0.0 sets it to 0 first, so it is 0 whenever the call fails. 2.5.4 leaves it unset on several failure paths. - **Held decrypt block**: 4.0.0 clears `final_used` before the inner update, so a failed update drops the held block. - **Invalid state**: 4.0.0 checks that the block size is a power of two, and queues `EVP_R_BAD_BLOCK_LENGTH` (136) for an out-of-range buffered count instead of failing silently. It also queues `EVP_R_TOO_LARGE` (164) for its overflow checks. The code is a line-for-line port over libSceLibreSsl's `EVP_CIPHER_CTX`, which libSceLibreSsl3 already shares (`buf_len` stands for 4.0.0's `partial_len`; `buf`, `final`, `final_used` and `flags` match). It calls the cipher's `do_cipher` directly and never the parent's update exports, so the Linux NID patcher cannot turn a call to the parent into a call to itself. - New `src/EvpCipherUpdate.cpp`. The exports use the `_nid_disambig1_nid_postfix` names, as `EVP_PKEY_free` and `EVP_PKEY_get1_RSA` already do, so they hash to the same NIDs as the parent's (checked with `tools/nid_names.py`). They are declared once in `include/Api.hpp`. - The three Windows forwarders are removed from `ParentExports.cpp`, so the NIDs are defined only once in the DLL. That leaves 387 forwarders, the count the existing debt entry already gives. - **Undefined upstream cases**: a null context, cipher or `outl`, a cipher without `do_cipher`, and a null buffer that a non-custom cipher or a `memcpy` would read or write throw `std::invalid_argument`. LibreSSL dereferences null in these cases. - **Debt**: `docs/dev/debt/libSceLibreSsl3.md` gets an entry for the new file. The ChaCha entry loses its zero-length-update gap, and the AES-CCM entry now names only the forwarded finals. **Known gap, recorded in the new entry**: on Linux and macOS, imports bind by NID alone. So libSceLibreSsl importers and libSceLibreSsl3 importers both reach whichever of the two modules the loader searches first. The overrides that already exist (`EVP_PKEY_free`, `EVP_PKEY_new`) have the same limitation. The finals (`EVP_EncryptFinal_ex`, `EVP_DecryptFinal_ex`, `EVP_CipherFinal_ex`) stay forwarded here; #18942, stacked on this one, ports them. No title's trace or disassembly was used, and nothing was measured on a PS5. **Overlap**: before writing the code I indexed the diffs of all 1755 open pull requests. Only #17299 and #17303 touch these three NIDs: they rename the same `ParentExports.cpp` forwarders that this PR removes. That is a line conflict, not the same function. Whichever merges second drops or rebases those six lines, and I will rebase this one as needed. `pr_overlap --branch` reports the same two pull requests and no other with the same exports or new files. It also flags these three exports as defined in libSceLibreSsl too, which is intentional: that is the override. 33 pull requests that conflict with `main` could not be compared by the tool. I scanned the lines each of them adds: none mentions these exports, their NIDs or an `EvpCipherUpdate` file. Their shared files are `Api.hpp`, `CMakeLists.txt` and the debt file, so the conflicts there are adjacent-line ones. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Rebased onto `main` `4e4132f468`. No title or homebrew. Release builds of every `guest_libressl*` and `guest_crypto_memcmp` test target and their dependencies, on two systems: - **Windows 11**, MinGW-w64 GCC 15.2 (WinLibs), Ninja. - **Linux**: Ubuntu 24.04 under WSL2 (kernel 6.18), GCC 13.3, CMake 3.28, Ninja, built from an LF checkout. Results: - **Windows**: `ctest -R '^guest_(libressl|crypto_memcmp)'` passes 142/142, including the new `guest_libressl3_evp_cipher_update`. - **Linux**: the same selection passes 144/144. That includes `guest_libressl3_mixed_evp` and `guest_libressl3_pkey_lifecycle`, which load the NID-patched `libSceLibreSsl3.prx` together with `libSceLibreSsl.prx`. - **The existing cipher tests still pass on both** (`guest_libressl3_chacha`, `guest_libressl3_aes_ccm`, `guest_libressl3_evp_cipher_init`, `guest_libressl_evp_cipher*`). Their checks call libSceLibreSsl's own updates. - **Patched modules**: on Windows, `objdump -p` on the NID-patched `libSceLibreSsl3.prx` lists `OnoZKqFGNjA`, `IXiXAHDFjeU` and `w03a1pfG8oM` once each, and no `EVP_*Update` name. On Linux, `nm -D --defined-only` on the patched module lists the same three NIDs as defined symbols. - **Repository checks on Windows**: - the 65 `public_headers*` tests, all `conventions_*` and `debt_*` tests, `export_gap`, `unbuilt_exports`, `tools_nid_name_audit`, `nid_elf_duplicates` and the `pr_overlap_*` tests pass; - `debt_split_DebtSplitTests_test_main_reports_non_git_repository` fails only because this checkout keeps its temporary directory inside the repository; - `guest_export_lookup`, `windows_guest_export_aliases` and `strict_nid_filter` need the relinker executable, which was not built. - `python3 tools/check_conventions.py --base upstream/main` passes. - **Earlier, on `main` `f0c378b784`**: the same Windows (141/141) and Linux (143/143) runs passed before the rebase. - **Mutations**: the new test kills 13 deliberate mutations of the implementation. Twelve remove the empty-update return, the CCM exception, the negative-length check, the upfront `*outl = 0`, the `final_used` clear, the power-of-two check, the `buf_len` advance, the input advance, the tail buffering, the no-padding path, the held block and the error queueing. The thirteenth inverts the `EVP_CipherUpdate` dispatch. - **Review**: an independent line-by-line comparison with 4.0.0 `evp_cipher.c` covered check order, block sizes 0, 1, 12, negative and `INT_MIN`, the signed/unsigned size comparisons, the overflow checks and when state changes on failure. It found no difference outside the documented throws. Not covered: macOS, and a run inside a title. ### 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!18861
No description provided.