feat(libSceLibreSsl3): implement EVP_PKEY_CTX RSA encryption, signing and verification #17528

Merged
MarrasFederico merged 16 commits from upstream-pr/17528 into main 2026-11-13 15:36:54 +00:00
MarrasFederico commented 2026-11-08 20:27:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarrasFederico on 2026-11-08T20:27:54Z.


What

Real RSA public-key encryption, signing and verification through EVP_PKEY_CTX in libSceLibreSsl3. It is built on the module's native 32-byte EVP_PKEY and 152-byte RSA objects, with mbedTLS doing the big-number work. Behaviour, return values and error-queue codes follow LibreSSL 4.0.0: evp/pmeth_lib.c, evp/pmeth_fn.c, evp/m_sigver.c, evp/p_sign.c, evp/p_verify.c, rsa/rsa_pmeth.c, rsa/rsa_eay.c, rsa/rsa_pk1.c, rsa/rsa_oaep.c, rsa/rsa_sign.c, rsa/rsa_pss.c, rsa/rsa_chk.c and rsa/rsa_lib.c.

Implemented exports:

  • Contexts:
    • EVP_PKEY_CTX_new and EVP_PKEY_CTX_free, with a native RSA / RSA-PSS key method and ASN.1 method;
    • EVP_PKEY_CTX_ctrl and RSA_pkey_ctx_ctrl, covering padding, signature digest, PSS salt length, MGF1 digest, OAEP digest and label, and the matching getters.
  • Encryption:
    • EVP_PKEY_encrypt_init, EVP_PKEY_encrypt, EVP_PKEY_decrypt_init and EVP_PKEY_decrypt, with PKCS#1 v1.5, OAEP and no padding, the out == NULL length query and too-small buffers.
  • Signatures:
    • EVP_PKEY_sign_init, EVP_PKEY_sign, EVP_PKEY_verify_init and EVP_PKEY_verify, with PKCS#1 v1.5 (DigestInfo), PSS (salt length digest/auto/max/fixed, separate MGF1 digest) and raw signatures;
    • EVP_DigestSignInit and EVP_DigestSignFinal (with the key's default SHA-256 digest when none is given), EVP_SignFinal and EVP_VerifyFinal, which finalize a copy of the digest context as LibreSSL does;
    • RSA_padding_add_PKCS1_PSS_mgf1.
  • Keys:
    • RSA_check_key;
    • RSA_new (_nid_disambig1).

Native RSA keys are freed through the ASN.1 key-free callback in EVP_PKEY_free, and EVP_MD_CTX_free releases a native EVP_PKEY_CTX attached by EVP_DigestSignInit.

The following keep throwing NotImplemented_nid_no_patch and are listed in the technical debt:

  • X9.31 padding;
  • SHA-3 and truncated SHA-512 digests;
  • RSA-PSS keys carrying parameter restrictions;
  • DigestInfo for object NIDs outside the digest table;
  • key-held blinding or Montgomery state;
  • an engine passed to EVP_DigestSignInit.

EVP_DigestVerifyInit and EVP_DigestVerifyFinal are still forwarded to the parent module: #17299 and #17303 are editing those forwarders. The overlapping null-only PR #16690 is closed. Key decoding (DER/PEM/PKCS#8) is not part of this PR.

Tested

Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic, GCC 15.2.0, Release, BUILD_TESTING=ON, CMake 4.4.4 with Ninja 1.13 from a virtualenv. Merged upstream/main into the published head 56af0fba3a with normal merge commits 7285751da1 (main 3b7f31fa70) and then 622487a245 (main 714de16abf); the published history was not rebased, squashed or force-pushed. On top of the merges the branch adds local commit(s): 702707574e (fix(libSceLibreSsl3): keep RSA pkey helpers distinct from libSceLibreSsl). Final head: 82fd15af14 (full 82fd15af1431b18df84bd7c2f151f24a6c6b83cd). The final commit corrects only the inventory summary; executable sources are identical to tested head 93d05b3c3c. Windows and macOS were not compiled here; no game title was run.

  • On the merged head, built the 7 guest_libressl3_pkey* test executables (including guest_libressl3_pkey_bits, whose executable target is guest_libressl3_key_reference_tests) and ran ctest -R '^guest_libressl3_pkey' --output-on-failure --repeat until-fail:3: 100% tests passed out of 7, 3 runs each, Not Run = 0 (exit 0).

  • On the pre-merge head, the whole module was built (core/libs/prx/libSceLibreSsl3/all plus every libSceLibreSsl3 test executable) and ctest -R libressl3 passed 100% of 42 tests.

  • Absorbing main (which merged #17138) pulled in three helpers that libSceLibreSsl/src/DigestVerify.cpp already exports under the same names, so they would have been real duplicate exports between the two modules; commit 702707574e renames our internal copies to PkeyRsaVerify, PkeyRsaContextCopy and PkeyRsaControl (used only inside src/PkeyContext.cpp). Verified afterwards: one definition per library for both name sets, and the inventory summary is corrected to match 68 throwing AdditionalExports.cpp definitions and 68 table rows: 64 named and 4 known only by NID.

  • python3 tools/check_conventions.py --base upstream/main: "All conventions checks passed" (exit 0, 0 errors).

  • Windows: the run reported earlier by JJenkx (Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, guest_libressl3_pkey_context 3 of 3) is on head 95a512a39b, not on this merged head; the hosted Windows check was not re-run here.

  • guest_libressl3_pkey_context uses a fixed 2048-bit key. For encryption it covers:

    • an encrypt/decrypt round trip;
    • known PKCS#1 v1.5 and OAEP ciphertexts, each checked with a separate Python implementation;
    • CRT decryption with a key that has no private exponent.

    For signatures it covers:

    • PKCS#1 v1.5 SHA-1, SHA-224 and SHA-256 signatures that match byte for byte the Python cryptography output for the same key and digest, also when the key has only CRT components;
    • PSS sign/verify round trips with random salts;
    • verification of a PSS signature produced by cryptography, with automatic, digest-length and fixed salt lengths;
    • RSA_padding_add_PKCS1_PSS_mgf1 blocks that verify after a raw private operation;
    • EVP_DigestSignInit/Final, EVP_SignFinal and EVP_VerifyFinal on a streamed message.

    Error and lifecycle checks:

    • every failure path checks the exact error-queue codes (bad signature, wrong signature or digest length, salt-length mismatch, invalid salt length, operation guards);
    • RSA_check_key is tested on valid keys, a missing component, an altered d, an altered iqmp and a composite p;
    • allocation balance on the guest heap.
  • Negative controls:

    • breaking the CRT reduction makes the test fail;
    • skipping the PSS hash comparison makes it fail.
  • While wiring signatures I found that the private-key encryption and public-key decryption helpers added in the first commit used PKCS#1 block type 2. LibreSSL rsa_eay.c uses block type 1 there and rejects OAEP; they now do the same.

  • ctest -R '^guest_libressl3_(pkey|rsa|key_reference|evp)' --output-on-failure --repeat until-fail:3: 100% tests passed out of 12, 3 runs each, Not Run = 0 (exit 0).

  • Upstream decf2615a3 feat(libSceLibreSsl3): implement X509_get0_pubkey was absorbed by the merge: the parameterless placeholder in AdditionalExports.cpp is gone, and the implementation, declaration and tests come from upstream only (no duplicate definition).

  • python3 tools/check_conventions.py --base upstream/main: "All conventions checks passed" (exit 0, 0 errors).

Publication checks: head 82fd15af1431b18df84bd7c2f151f24a6c6b83cd passed an independent conventions/diff check and a clean merge-tree check against upstream main fa289273974bb03ffcf6c6432f0b15a2cb65f46d. Linux execution results above come from the recorded verification runs, reviewed rather than rerun during publication.

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:5214841131 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17528). Originally posted by [MarrasFederico](https://github.com/MarrasFederico) on 2026-11-08T20:27:54Z. --- ### What Real RSA public-key encryption, signing and verification through `EVP_PKEY_CTX` in libSceLibreSsl3. It is built on the module's native 32-byte `EVP_PKEY` and 152-byte `RSA` objects, with mbedTLS doing the big-number work. Behaviour, return values and error-queue codes follow [LibreSSL 4.0.0](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto): `evp/pmeth_lib.c`, `evp/pmeth_fn.c`, `evp/m_sigver.c`, `evp/p_sign.c`, `evp/p_verify.c`, `rsa/rsa_pmeth.c`, `rsa/rsa_eay.c`, `rsa/rsa_pk1.c`, `rsa/rsa_oaep.c`, `rsa/rsa_sign.c`, `rsa/rsa_pss.c`, `rsa/rsa_chk.c` and `rsa/rsa_lib.c`. Implemented exports: - Contexts: - `EVP_PKEY_CTX_new` and `EVP_PKEY_CTX_free`, with a native RSA / RSA-PSS key method and ASN.1 method; - `EVP_PKEY_CTX_ctrl` and `RSA_pkey_ctx_ctrl`, covering padding, signature digest, PSS salt length, MGF1 digest, OAEP digest and label, and the matching getters. - Encryption: - `EVP_PKEY_encrypt_init`, `EVP_PKEY_encrypt`, `EVP_PKEY_decrypt_init` and `EVP_PKEY_decrypt`, with PKCS#1 v1.5, OAEP and no padding, the `out == NULL` length query and too-small buffers. - Signatures: - `EVP_PKEY_sign_init`, `EVP_PKEY_sign`, `EVP_PKEY_verify_init` and `EVP_PKEY_verify`, with PKCS#1 v1.5 (DigestInfo), PSS (salt length digest/auto/max/fixed, separate MGF1 digest) and raw signatures; - `EVP_DigestSignInit` and `EVP_DigestSignFinal` (with the key's default SHA-256 digest when none is given), `EVP_SignFinal` and `EVP_VerifyFinal`, which finalize a copy of the digest context as LibreSSL does; - `RSA_padding_add_PKCS1_PSS_mgf1`. - Keys: - `RSA_check_key`; - `RSA_new` (`_nid_disambig1`). Native RSA keys are freed through the ASN.1 key-free callback in `EVP_PKEY_free`, and `EVP_MD_CTX_free` releases a native `EVP_PKEY_CTX` attached by `EVP_DigestSignInit`. The following keep throwing `NotImplemented_nid_no_patch` and are listed in the technical debt: - X9.31 padding; - SHA-3 and truncated SHA-512 digests; - RSA-PSS keys carrying parameter restrictions; - DigestInfo for object NIDs outside the digest table; - key-held blinding or Montgomery state; - an engine passed to `EVP_DigestSignInit`. `EVP_DigestVerifyInit` and `EVP_DigestVerifyFinal` are still forwarded to the parent module: #17299 and #17303 are editing those forwarders. The overlapping null-only PR #16690 is closed. Key decoding (DER/PEM/PKCS#8) is not part of this PR. ### 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. --> Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic, GCC 15.2.0, Release, `BUILD_TESTING=ON`, CMake 4.4.4 with Ninja 1.13 from a virtualenv. Merged `upstream/main` into the published head `56af0fba3a` with normal merge commits `7285751da1` (main `3b7f31fa70`) and then `622487a245` (main `714de16abf`); the published history was not rebased, squashed or force-pushed. On top of the merges the branch adds local commit(s): `702707574e` (fix(libSceLibreSsl3): keep RSA pkey helpers distinct from libSceLibreSsl). Final head: `82fd15af14` (full `82fd15af1431b18df84bd7c2f151f24a6c6b83cd`). The final commit corrects only the inventory summary; executable sources are identical to tested head `93d05b3c3c`. Windows and macOS were not compiled here; no game title was run. - On the merged head, built the 7 `guest_libressl3_pkey*` test executables (including `guest_libressl3_pkey_bits`, whose executable target is `guest_libressl3_key_reference_tests`) and ran `ctest -R '^guest_libressl3_pkey' --output-on-failure --repeat until-fail:3`: **100% tests passed out of 7**, 3 runs each, `Not Run` = 0 (exit 0). - On the pre-merge head, the whole module was built (`core/libs/prx/libSceLibreSsl3/all` plus every libSceLibreSsl3 test executable) and `ctest -R libressl3` passed 100% of 42 tests. - Absorbing `main` (which merged #17138) pulled in three helpers that `libSceLibreSsl/src/DigestVerify.cpp` already exports under the same names, so they would have been real duplicate exports between the two modules; commit `702707574e` renames our internal copies to `PkeyRsaVerify`, `PkeyRsaContextCopy` and `PkeyRsaControl` (used only inside `src/PkeyContext.cpp`). Verified afterwards: one definition per library for both name sets, and the inventory summary is corrected to match 68 throwing AdditionalExports.cpp definitions and 68 table rows: 64 named and 4 known only by NID. - `python3 tools/check_conventions.py --base upstream/main`: "All conventions checks passed" (exit 0, 0 errors). - Windows: the run reported earlier by JJenkx (Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, `guest_libressl3_pkey_context` 3 of 3) is on head `95a512a39b`, not on this merged head; the hosted Windows check was not re-run here. - `guest_libressl3_pkey_context` uses a fixed 2048-bit key. For encryption it covers: - an encrypt/decrypt round trip; - known PKCS#1 v1.5 and OAEP ciphertexts, each checked with a separate Python implementation; - CRT decryption with a key that has no private exponent. For signatures it covers: - PKCS#1 v1.5 SHA-1, SHA-224 and SHA-256 signatures that match byte for byte the Python `cryptography` output for the same key and digest, also when the key has only CRT components; - PSS sign/verify round trips with random salts; - verification of a PSS signature produced by `cryptography`, with automatic, digest-length and fixed salt lengths; - `RSA_padding_add_PKCS1_PSS_mgf1` blocks that verify after a raw private operation; - `EVP_DigestSignInit`/`Final`, `EVP_SignFinal` and `EVP_VerifyFinal` on a streamed message. Error and lifecycle checks: - every failure path checks the exact error-queue codes (bad signature, wrong signature or digest length, salt-length mismatch, invalid salt length, operation guards); - `RSA_check_key` is tested on valid keys, a missing component, an altered `d`, an altered `iqmp` and a composite `p`; - allocation balance on the guest heap. - Negative controls: - breaking the CRT reduction makes the test fail; - skipping the PSS hash comparison makes it fail. - While wiring signatures I found that the private-key encryption and public-key decryption helpers added in the first commit used PKCS#1 block type 2. LibreSSL `rsa_eay.c` uses block type 1 there and rejects OAEP; they now do the same. - `ctest -R '^guest_libressl3_(pkey|rsa|key_reference|evp)' --output-on-failure --repeat until-fail:3`: **100% tests passed out of 12**, 3 runs each, `Not Run` = 0 (exit 0). - Upstream `decf2615a3 feat(libSceLibreSsl3): implement X509_get0_pubkey` was absorbed by the merge: the parameterless placeholder in `AdditionalExports.cpp` is gone, and the implementation, declaration and tests come from upstream only (no duplicate definition). - `python3 tools/check_conventions.py --base upstream/main`: "All conventions checks passed" (exit 0, 0 errors). Publication checks: head `82fd15af1431b18df84bd7c2f151f24a6c6b83cd` passed an independent conventions/diff check and a clean merge-tree check against upstream main `fa289273974bb03ffcf6c6432f0b15a2cb65f46d`. Linux execution results above come from the recorded verification runs, reviewed rather than rerun during publication. ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
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!17528
No description provided.