feat(libSceLibreSsl): verify RSA signatures through EVP_DigestVerify #16719

Merged
MarrasFederico merged 8 commits from upstream-pr/16719 into main 2026-11-10 10:57:23 +00:00
MarrasFederico commented 2026-11-07 13:30:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarrasFederico on 2026-11-07T13:30:36Z.


Supersedes #16389, which GitHub closed when the head fork was deleted.

What

This PR now verifies RSA signatures through EVP_DigestVerifyInit and EVP_DigestVerifyFinal, the path a title uses. Before, it only rejected an uninitialized context.

  • EVP_DigestVerifyInit with an RSA key:
    • builds an EVP_PKEY_CTX and stores it in the digest context;
    • writes pctx if the caller asks for it;
    • uses the caller's digest, or the key's default when type is null (SHA-1, as 2.5.4 does);
    • selects the verify operation and initialises the digest.
  • EVP_DigestVerifyFinal finalises a copy of the context and verifies PKCS#1 v1.5 through the existing RSA_verify.
    • Return values and queued reason codes match 2.5.4 for valid, bit-flipped, truncated, over-long, empty and above-modulus signatures, and for a mismatched digest.
    • An uninitialized context still returns -1 with EVP error 111, as before.
  • RSA_verify and RSA_public_decrypt now queue the RSA reason codes 2.5.4 queues on failure. Before, they failed silently.
  • Copying and cleaning up a digest context duplicates and frees the key context instead of throwing.
  • What still throws: ENGINE, non-RSA keys, key contexts this module didn't create, and PSS and X9.31 padding. No export of this module can reach any of them: EVP_PKEY_CTX_new, _dup, _free and _ctrl are not in the PS5 export table, so they exist here only as internal helpers.

The 208-byte EVP_PKEY_METHOD, 80-byte EVP_PKEY_CTX and 64-byte RSA_PKEY_CTX layouts and all results follow pinned LibreSSL 2.5.4: m_sigver.c, pmeth_lib.c, pmeth_fn.c, rsa_pmeth.c and rsa_eay.c. The debt entries list what is not console-verified.

Overlap:

  • #16079 changes src/Evp.cpp in three places this PR also edits: the include block, the failed allocation in DigestInit, and CopyContext. Whichever lands second will merge the other. If #16079 lands first, CopyContext queues EVP error 111 itself, so EVP_DigestVerifyFinal must stop queueing it a second time.
  • #16865 changes RSA_sign in src/Rsa.cpp, a different function, and merges cleanly with this branch.

The old guest_libressl_digest_verify_final test is folded into the new guest_libressl_digest_verify test.

Tested

Linux, GCC 15.2, Release. No console or title was run.

  • guest_libressl_digest_verify: a 1024-bit RSA key with fixed signatures generated by LibreSSL 2.5.4. It covers MD5, SHA-256, SHA-384 and SHA-512, plus the SHA-1 default, rejected signatures with their codes, pctx, context copy and cleanup, and the key reference count.
  • guest_libressl_digest_verify_allocator: every failed guest allocation returns what 2.5.4 returns and leaks nothing.
  • LibreSSL 2.5.4 oracle: 3,379 new comparisons with 0 differences (Init, repeated Init, Update, Final and cleanup) on 512, 1024 and 2048-bit keys signed with MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512. The signatures are tested intact and with eight kinds of corruption.
    • Across the whole oracle (49,900 cases), the only differences are the 6 in AES_set_encrypt_key and AES_set_decrypt_key, which are on main too.
  • Negative control: with the PKCS#1 dispatch broken, both new tests abort and the oracle reports 804 more differences. Restoring the line makes everything pass again.
  • Regression: all 117 guest_libressl*, guest_libre_ssl* and guest_digest* tests pass.
  • Checks: tools/check_conventions.py, test_public_headers.py --audit-only and git diff --check pass.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5202073092 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16719). Originally posted by [MarrasFederico](https://github.com/MarrasFederico) on 2026-11-07T13:30:36Z. --- Supersedes #16389, which GitHub closed when the head fork was deleted. ### What This PR now verifies RSA signatures through `EVP_DigestVerifyInit` and `EVP_DigestVerifyFinal`, the path a title uses. Before, it only rejected an uninitialized context. - **`EVP_DigestVerifyInit` with an RSA key:** - builds an `EVP_PKEY_CTX` and stores it in the digest context; - writes `pctx` if the caller asks for it; - uses the caller's digest, or the key's default when `type` is null (SHA-1, as 2.5.4 does); - selects the verify operation and initialises the digest. - **`EVP_DigestVerifyFinal`** finalises a copy of the context and verifies PKCS#1 v1.5 through the existing `RSA_verify`. - Return values and queued reason codes match 2.5.4 for valid, bit-flipped, truncated, over-long, empty and above-modulus signatures, and for a mismatched digest. - An uninitialized context still returns -1 with EVP error 111, as before. - **`RSA_verify` and `RSA_public_decrypt`** now queue the RSA reason codes 2.5.4 queues on failure. Before, they failed silently. - **Copying and cleaning up a digest context** duplicates and frees the key context instead of throwing. - **What still throws:** `ENGINE`, non-RSA keys, key contexts this module didn't create, and PSS and X9.31 padding. No export of this module can reach any of them: `EVP_PKEY_CTX_new`, `_dup`, `_free` and `_ctrl` are not in the PS5 export table, so they exist here only as internal helpers. The 208-byte `EVP_PKEY_METHOD`, 80-byte `EVP_PKEY_CTX` and 64-byte `RSA_PKEY_CTX` layouts and all results follow pinned LibreSSL 2.5.4: `m_sigver.c`, `pmeth_lib.c`, `pmeth_fn.c`, `rsa_pmeth.c` and `rsa_eay.c`. The debt entries list what is not console-verified. Overlap: - **#16079** changes `src/Evp.cpp` in three places this PR also edits: the include block, the failed allocation in `DigestInit`, and `CopyContext`. Whichever lands second will merge the other. If #16079 lands first, `CopyContext` queues EVP error 111 itself, so `EVP_DigestVerifyFinal` must stop queueing it a second time. - **#16865** changes `RSA_sign` in `src/Rsa.cpp`, a different function, and merges cleanly with this branch. The old `guest_libressl_digest_verify_final` test is folded into the new `guest_libressl_digest_verify` test. ### Tested Linux, GCC 15.2, Release. No console or title was run. - **`guest_libressl_digest_verify`:** a 1024-bit RSA key with fixed signatures generated by LibreSSL 2.5.4. It covers MD5, SHA-256, SHA-384 and SHA-512, plus the SHA-1 default, rejected signatures with their codes, `pctx`, context copy and cleanup, and the key reference count. - **`guest_libressl_digest_verify_allocator`:** every failed guest allocation returns what 2.5.4 returns and leaks nothing. - **LibreSSL 2.5.4 oracle:** 3,379 new comparisons with 0 differences (Init, repeated Init, Update, Final and cleanup) on 512, 1024 and 2048-bit keys signed with MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512. The signatures are tested intact and with eight kinds of corruption. - Across the whole oracle (49,900 cases), the only differences are the 6 in `AES_set_encrypt_key` and `AES_set_decrypt_key`, which are on `main` too. - **Negative control:** with the PKCS#1 dispatch broken, both new tests abort and the oracle reports 804 more differences. Restoring the line makes everything pass again. - **Regression:** all 117 `guest_libressl*`, `guest_libre_ssl*` and `guest_digest*` tests pass. - **Checks:** `tools/check_conventions.py`, `test_public_headers.py --audit-only` and `git diff --check` pass. ### 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
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!16719
No description provided.