feat(libSceLibreSsl): implement RSA_private_encrypt, RSA_private_decrypt and RSA_sign #11151

Merged
ahmadmuj merged 45 commits from upstream-pr/11151 into main 2026-11-06 07:57:17 +00:00
ahmadmuj commented 2026-10-25 19:42:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T19:42:36Z.


What

Implements RSA_private_encrypt, RSA_private_decrypt and RSA_sign on the RSA object from #10915, following LibreSSL 2.5.4 rsa_eay.c, the padding files next to it, rsa_sign.c and bn_blind.c.

#10915 and #10449 are merged. The original implementation is 823aab9 and 6aa3fd5 (test-only). Current-main integration also guards RSA verification against registered OIDs and makes signing throw for unsupported dynamic identifiers. It does not add dynamic digest support.

  • Encrypt pads with PKCS#1 type 1, X9.31 or none. Decrypt checks PKCS#1 type 2, OAEP (SHA-1, empty label), SSLv23 or none. Bad padding, sizes and lengths return -1 like 2.5.4.
  • The private op goes through the method's slots like RSA_eay_*: blinding (unless RSA_FLAG_NO_BLINDING), the CRT path with its e verify and d fallback, and plain d when CRT fields are missing. Same order as 2.5.4, mbedtls for the math (reuses #10915's ModExp).
  • RSA_sign builds the DER DigestInfo from #10915's 15-entry digest table, or signs the raw 36 bytes for NID_md5_sha1. A guest method's rsa_sign slot gets called when RSA_FLAG_SIGN_VER is set.
  • Not cached: the blinding factor (fresh host entropy per call, not stored in rsa->blinding) and the Montgomery contexts. Outputs are the same for a consistent key. The differences are in the debt entry.
  • Throws where 2.5.4 hits UB or where we can't match it: a blinding or Montgomery pointer the caller set, a custom rsa_mod_exp/bn_mod_exp, numbers over the 8192-bit mbedtls limit, non-normalised BIGNUMs, n == 1 with blinding, and an RSA_sign NID that's in range but not a digest. The full list is in the debt file.

BN_new and BN_bin2bn are now implemented on main. RSA_generate_key_ex stays outside this PR. The PSS pair is in #11175.

Current alignment

Head 944b114d0ea56a8868473c81165ffa86ff598f4f merges main 08e73174f230de4f475c04299170d8c14d6e3425, including #10915. Published history is preserved. The merged count-free debt wording is preserved.

The registered-OID guards remain in RSA verification and public-key decoding. Their tests accept the correct result or an unsupported-path refusal. The unsupported custom-method and non-digest signing throw assertions are removed. The other private RSA checks remain.

The final main merge changes only debt wording. Fresh checks: focused RSA, private RSA and error tests pass (3/3). Public headers pass (65/65). Conventions and whitespace pass. Prior differential and mutation results below were not repeated. Windows CI remains external. The final test-only correction also passes the focused private-RSA test on #11151; dependent branches inherit that exact test.

Tested

The following feature checks predate this alignment. Fresh checks appear above.
Ubuntu 24.04 on WSL2, GCC 13.3, Release, on main + #10915.

  • Differential against real LibreSSL 2.5.4 static libcrypto.a: a host generator recorded 2124 cases on 6 keys (512 to 4096 bits, e = 3 and 65537) in about 20 shapes each: missing d, e, CRT fields, a bad dmp1, even n, n = 1, the flag combos. Output buffers are prefilled and compared in full, so extra writes count. A guest harness (not committed) got 1904/1904 byte-exact, 54/54 matches on the return value only (cases where the output bytes aren't comparable), and 166/166 deliberate throws.
  • New guest_libressl_rsa_private: exact 2.5.4 vectors on a 512-bit key for each padding, the key shapes, RSA_sign with SHA-256 and MD5-SHA1, empty and longest digests, and the refused types, and method-slot dispatch with recording callbacks.
  • Red: with the stub bodies back, the test aborts with RSA_eay_private_encrypt not implemented (and the decrypt/sign variants). Mutations that each fail the test: skip blinding, drop the CRT fallback, always output ret for X9.31, write siglen on a failed sign, wrong DigestInfo NULL parameter.
  • ctest -R libressl: 15/15 pass on main 9857eee + #10915 939a194, including #10915's guest_libressl_rsa. check_conventions.py --base origin/main passes.
  • Not tested: no console or game dump, and no Windows or macOS build.

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 (#10915 is merged)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5008034259 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11151). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T19:42:36Z. --- ### What Implements `RSA_private_encrypt`, `RSA_private_decrypt` and `RSA_sign` on the `RSA` object from #10915, following LibreSSL 2.5.4 [rsa_eay.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/rsa/rsa_eay.c), the padding files next to it, [rsa_sign.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/rsa/rsa_sign.c) and [bn_blind.c](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/bn/bn_blind.c). #10915 and #10449 are merged. The original implementation is 823aab9 and 6aa3fd5 (test-only). Current-main integration also guards RSA verification against registered OIDs and makes signing throw for unsupported dynamic identifiers. It does not add dynamic digest support. - Encrypt pads with PKCS#1 type 1, X9.31 or none. Decrypt checks PKCS#1 type 2, OAEP (SHA-1, empty label), SSLv23 or none. Bad padding, sizes and lengths return -1 like 2.5.4. - The private op goes through the method's slots like `RSA_eay_*`: blinding (unless `RSA_FLAG_NO_BLINDING`), the CRT path with its `e` verify and `d` fallback, and plain `d` when CRT fields are missing. Same order as 2.5.4, mbedtls for the math (reuses #10915's `ModExp`). - `RSA_sign` builds the DER `DigestInfo` from #10915's 15-entry digest table, or signs the raw 36 bytes for `NID_md5_sha1`. A guest method's `rsa_sign` slot gets called when `RSA_FLAG_SIGN_VER` is set. - Not cached: the blinding factor (fresh host entropy per call, not stored in `rsa->blinding`) and the Montgomery contexts. Outputs are the same for a consistent key. The differences are in the debt entry. - Throws where 2.5.4 hits UB or where we can't match it: a blinding or Montgomery pointer the caller set, a custom `rsa_mod_exp`/`bn_mod_exp`, numbers over the 8192-bit mbedtls limit, non-normalised BIGNUMs, n == 1 with blinding, and an `RSA_sign` NID that's in range but not a digest. The full list is in the debt file. `BN_new` and `BN_bin2bn` are now implemented on main. `RSA_generate_key_ex` stays outside this PR. The PSS pair is in #11175. ### Current alignment Head `944b114d0ea56a8868473c81165ffa86ff598f4f` merges main `08e73174f230de4f475c04299170d8c14d6e3425`, including #10915. Published history is preserved. The merged count-free debt wording is preserved. The registered-OID guards remain in RSA verification and public-key decoding. Their tests accept the correct result or an unsupported-path refusal. The unsupported custom-method and non-digest signing throw assertions are removed. The other private RSA checks remain. The final main merge changes only debt wording. Fresh checks: focused RSA, private RSA and error tests pass (3/3). Public headers pass (65/65). Conventions and whitespace pass. Prior differential and mutation results below were not repeated. Windows CI remains external. The final test-only correction also passes the focused private-RSA test on #11151; dependent branches inherit that exact test. ### Tested The following feature checks predate this alignment. Fresh checks appear above. Ubuntu 24.04 on WSL2, GCC 13.3, Release, on `main` + #10915. - Differential against real LibreSSL 2.5.4 static `libcrypto.a`: a host generator recorded 2124 cases on 6 keys (512 to 4096 bits, e = 3 and 65537) in about 20 shapes each: missing `d`, `e`, CRT fields, a bad `dmp1`, even `n`, n = 1, the flag combos. Output buffers are prefilled and compared in full, so extra writes count. A guest harness (not committed) got 1904/1904 byte-exact, 54/54 matches on the return value only (cases where the output bytes aren't comparable), and 166/166 deliberate throws. - New `guest_libressl_rsa_private`: exact 2.5.4 vectors on a 512-bit key for each padding, the key shapes, `RSA_sign` with SHA-256 and MD5-SHA1, empty and longest digests, and the refused types, and method-slot dispatch with recording callbacks. - Red: with the stub bodies back, the test aborts with `RSA_eay_private_encrypt not implemented` (and the decrypt/sign variants). Mutations that each fail the test: skip blinding, drop the CRT fallback, always output `ret` for X9.31, write `siglen` on a failed sign, wrong `DigestInfo` NULL parameter. - `ctest -R libressl`: 15/15 pass on `main` 9857eee + #10915 939a194, including #10915's `guest_libressl_rsa`. `check_conventions.py --base origin/main` passes. - Not tested: no console or game dump, and no Windows or macOS build. ### 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 (#10915 is merged) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 19:47:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T19:47:37Z.

Review: APPROVED


Reviewed exact head 9343653603, focusing on private RSA padding, blinding, CRT fallback, method-slot dispatch, DigestInfo construction, and the associated tests and debt notes. Local checks passed: check_conventions.py --base upstream/main, git diff --check upstream/main...HEAD, the public-header audit (368 libraries / 12,196 exports), and a C++20 syntax compile of Rsa.cpp against the pinned Mbed TLS headers. The PR reports a LibreSSL 2.5.4 differential run and 13/13 libressl CTest cases; I could not independently rerun those runtime tests here because this checkout has no configured project build. Product CI jobs are skipped under the repository PR policy. I found no blocking issue in the source review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5644940248 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11151#pullrequestreview-5644940248). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T19:47:37Z. **Review:** APPROVED --- Reviewed exact head 9343653603b0c77e85ab84ff7f893fd1b8bc2710, focusing on private RSA padding, blinding, CRT fallback, method-slot dispatch, DigestInfo construction, and the associated tests and debt notes. Local checks passed: check_conventions.py --base upstream/main, git diff --check upstream/main...HEAD, the public-header audit (368 libraries / 12,196 exports), and a C++20 syntax compile of Rsa.cpp against the pinned Mbed TLS headers. The PR reports a LibreSSL 2.5.4 differential run and 13/13 libressl CTest cases; I could not independently rerun those runtime tests here because this checkout has no configured project build. Product CI jobs are skipped under the repository PR policy. I found no blocking issue in the source review.
CnRJay commented 2026-11-01 20:02:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T20:02:10Z.

Review: COMMENTED


Correction (2026-11-01): this review's claim that maintainers close PRs without a title ID is wrong. CONTRIBUTING allows Unblocks: none; a title is only required when the PR fixes a title. The technical points below are unaffected.

No title ID and no log line. The description says Unblocks: none and states the exports "matter once #9446 or a PEM reader lands", because guests cannot build a private key through this module while BN_new and BN_bin2bn are stubs. So no title can reach RSA_private_encrypt, RSA_private_decrypt or RSA_sign on main today, and the 2,400-line diff (Rsa.cpp plus two test files) is unreachable code until another PR lands.

  1. Order. This sits on #10915 (carried commits) and cannot be exercised before #9446. I would hold it until #9446 is merged and a title or a test build of the module can actually produce a private key, so the exports are reviewed against a real caller.
  2. Evidence of reach. Once a title imports one of these, please add its ID and the log line from main showing the import being hit. Without it this is export-only work, which is what maintainers have been closing.
  3. Blinding. The description says the blinding factor is fresh host entropy per call and not stored in rsa->blinding, and that this differs from 2.5.4. Please state in the debt entry what a guest that reads rsa->blinding afterwards observes.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729964478 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11151#pullrequestreview-5729964478). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T20:02:10Z. **Review:** COMMENTED --- **Correction (2026-11-01):** this review's claim that maintainers close PRs without a title ID is wrong. CONTRIBUTING allows `Unblocks: none`; a title is only required when the PR fixes a title. The technical points below are unaffected. No title ID and no log line. The description says `Unblocks: none` and states the exports "matter once #9446 or a PEM reader lands", because guests cannot build a private key through this module while `BN_new` and `BN_bin2bn` are stubs. So no title can reach `RSA_private_encrypt`, `RSA_private_decrypt` or `RSA_sign` on `main` today, and the 2,400-line diff (Rsa.cpp plus two test files) is unreachable code until another PR lands. 1. **Order.** This sits on #10915 (carried commits) and cannot be exercised before #9446. I would hold it until #9446 is merged and a title or a test build of the module can actually produce a private key, so the exports are reviewed against a real caller. 2. **Evidence of reach.** Once a title imports one of these, please add its ID and the log line from `main` showing the import being hit. Without it this is export-only work, which is what maintainers have been closing. 3. **Blinding.** The description says the blinding factor is fresh host entropy per call and not stored in `rsa->blinding`, and that this differs from 2.5.4. Please state in the debt entry what a guest that reads `rsa->blinding` afterwards observes.
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!11151
No description provided.