feat(libSceLibreSsl): implement RSA_flags and RSA_size #10449

Merged
alvaroelpob merged 3 commits from upstream-pr/10449 into main 2026-10-26 20:26:38 +00:00
alvaroelpob commented 2026-10-24 00:59:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by alvaroelpob on 2026-10-24T00:59:31Z.


What

Implements RSA_flags and RSA_size in libSceLibreSsl, which threw and were declared without parameters.

The module is LibreSSL 2.5.4 (firmware 13.40 measurements in #8981). Signatures, layouts and results follow that release's rsa.h and rsa_crpt.c, with bn.h and bn_lib.c for the modulus:

Export Behaviour
int RSA_flags(const RSA*) 0 for a null key, otherwise meth->flags
int RSA_size(const RSA*) BN_num_bytes(n): 0 when n->top is 0, otherwise ((top - 1) * 64 + bits(d[top - 1]) + 7) / 8

The layouts are 152 bytes for RSA (meth at 16, n at 32, flags at 108), 112 for RSA_METHOD (flags at 72) and 24 for BIGNUM (d, top, dmax, neg, flags), checked with static_asserts. Like BN_num_bits, RSA_size reads the top word as it is: a zero top word left by a caller isn't skipped, and the sign is ignored.

LibreSSL dereferences these without checking them, so these throw: a null method in RSA_flags, and in RSA_size a null key, a null modulus, or a positive word count with null words. A negative word count and a bit count that overflows int, which are undefined there, throw too. No export of this module creates a key yet, so for now only keys built by the caller reach these functions.

#9446 and #9571 also define the 2.5.4 BIGNUM layout for their BN_* exports. This PR only reads it, in its own file, and changes none of their exports.

The debt entry records the source and what is unverified, and the pending-export count drops by 2 (342 to 340 on current main).

Tested

Ubuntu 26.04 (WSL2, kernel 6.18), GCC 15.2.0, Release, full build with BUILD_TESTING=ON on main e2b9700a9. There is one commit per function, and each adds that function with its test block and debt update. Each was built and its guest_libressl_rsa test and check_conventions.py run on its own.

  • New guest_libressl_rsa test. RSA_flags returns 0 for a null key, and for a method it returns 0, a set flag and -1; a null method throws. RSA_size gives 0 for a zero word count, even with null words, and 1, 1, 2 and 8 bytes for top words 1, 0xFF, 0x100 and 2^63. It gives 9 for two words and 256 for a 2048-bit modulus, unchanged when negative. A 16-word modulus with a 56-bit top word gives 127, and with a zero top word 120. A negative word count, null words, a null modulus and a null key throw. On main the first call throws.
  • Full ctest -j16 at the last commit: 2003/2004 pass. agc_driver_multisample_retirement fails the same way on main on this machine.
  • Not tested: Windows, a title.
  • Rebased on main 1e740a23a3 (after #9033), head 3b76876201: the conflicts were only the add_library source list, the end of the test block, the pending-export count and the last debt entry; both sides are kept and the count is 357 minus this PR's exports. Full build and ctest -j16: 2043/2044 pass; agc_driver_multisample_retirement fails the same way on that main. check_conventions.py --base upstream/main passes.
  • Rebased on main e7e3e9c198, head 0bf45bf1b2, to resolve the conflict from #9529 on the shared CMakeLists.txt and debt-file lines; both sides are kept and the count is 353 minus this PR's exports. 0bf45bf1b2 (refactor(libSceLibreSsl): declare the RSA exports in the public header) puts the real signatures and their structs in libSceLibreSsl/include/Api.hpp and drops the test's redeclarations, which public_headers requires since #10706. Full build and ctest -j16: 2202/2203 pass; agc_driver_multisample_retirement fails the same way on that main. check_conventions.py --base upstream/main passes.
  • Rebased on main ae7d59258e (after #9949), head e44f645642, to resolve the conflicts with main on the shared CMakeLists.txt and debt-file lines; both sides are kept and the count is 342 minus this PR's exports. Full build and ctest -j16: 2275/2276 pass; agc_driver_multisample_retirement fails the same way on that main. guest_posix_spawn, a libc test, also failed once in that run and passed 5 of 5 runs on its own. check_conventions.py --base upstream/main passes.

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: #9448, #9571, #10078, #10373, #10376, #10395, #10398, #10418, #10447, #10448
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4991183289 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10449). Originally posted by [alvaroelpob](https://github.com/alvaroelpob) on 2026-10-24T00:59:31Z. --- ### What Implements `RSA_flags` and `RSA_size` in libSceLibreSsl, which threw and were declared without parameters. The module is LibreSSL 2.5.4 ([firmware 13.40 measurements in #8981](https://github.com/boykopovar/AnyPS5/discussions/8981#discussioncomment-19111214)). Signatures, layouts and results follow that release's [`rsa.h` and `rsa_crpt.c`](https://github.com/libressl/openbsd/tree/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/rsa), with [`bn.h` and `bn_lib.c`](https://github.com/libressl/openbsd/tree/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/bn) for the modulus: | Export | Behaviour | |---|---| | `int RSA_flags(const RSA*)` | 0 for a null key, otherwise `meth->flags` | | `int RSA_size(const RSA*)` | `BN_num_bytes(n)`: 0 when `n->top` is 0, otherwise `((top - 1) * 64 + bits(d[top - 1]) + 7) / 8` | The layouts are 152 bytes for `RSA` (`meth` at 16, `n` at 32, `flags` at 108), 112 for `RSA_METHOD` (`flags` at 72) and 24 for `BIGNUM` (`d`, `top`, `dmax`, `neg`, `flags`), checked with `static_assert`s. Like `BN_num_bits`, `RSA_size` reads the top word as it is: a zero top word left by a caller isn't skipped, and the sign is ignored. LibreSSL dereferences these without checking them, so these throw: a null method in `RSA_flags`, and in `RSA_size` a null key, a null modulus, or a positive word count with null words. A negative word count and a bit count that overflows `int`, which are undefined there, throw too. No export of this module creates a key yet, so for now only keys built by the caller reach these functions. #9446 and #9571 also define the 2.5.4 `BIGNUM` layout for their `BN_*` exports. This PR only reads it, in its own file, and changes none of their exports. The debt entry records the source and what is unverified, and the pending-export count drops by 2 (342 to 340 on current `main`). ### 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 (WSL2, kernel 6.18), GCC 15.2.0, Release, full build with `BUILD_TESTING=ON` on `main` e2b9700a9. There is one commit per function, and each adds that function with its test block and debt update. Each was built and its `guest_libressl_rsa` test and `check_conventions.py` run on its own. - New `guest_libressl_rsa` test. `RSA_flags` returns 0 for a null key, and for a method it returns 0, a set flag and -1; a null method throws. `RSA_size` gives 0 for a zero word count, even with null words, and 1, 1, 2 and 8 bytes for top words 1, 0xFF, 0x100 and 2^63. It gives 9 for two words and 256 for a 2048-bit modulus, unchanged when negative. A 16-word modulus with a 56-bit top word gives 127, and with a zero top word 120. A negative word count, null words, a null modulus and a null key throw. On `main` the first call throws. - Full `ctest -j16` at the last commit: 2003/2004 pass. `agc_driver_multisample_retirement` fails the same way on `main` on this machine. - Not tested: Windows, a title. - Rebased on `main` 1e740a23a3 (after #9033), head 3b76876201: the conflicts were only the `add_library` source list, the end of the test block, the pending-export count and the last debt entry; both sides are kept and the count is 357 minus this PR's exports. Full build and `ctest -j16`: 2043/2044 pass; `agc_driver_multisample_retirement` fails the same way on that `main`. `check_conventions.py --base upstream/main` passes. - Rebased on `main` e7e3e9c198, head 0bf45bf1b2, to resolve the conflict from #9529 on the shared `CMakeLists.txt` and debt-file lines; both sides are kept and the count is 353 minus this PR's exports. 0bf45bf1b2 (`refactor(libSceLibreSsl): declare the RSA exports in the public header`) puts the real signatures and their structs in `libSceLibreSsl/include/Api.hpp` and drops the test's redeclarations, which `public_headers` requires since #10706. Full build and `ctest -j16`: 2202/2203 pass; `agc_driver_multisample_retirement` fails the same way on that `main`. `check_conventions.py --base upstream/main` passes. - Rebased on `main` ae7d59258e (after #9949), head e44f645642, to resolve the conflicts with `main` on the shared `CMakeLists.txt` and debt-file lines; both sides are kept and the count is 342 minus this PR's exports. Full build and `ctest -j16`: 2275/2276 pass; `agc_driver_multisample_retirement` fails the same way on that `main`. `guest_posix_spawn`, a libc test, also failed once in that run and passed 5 of 5 runs on its own. `check_conventions.py --base upstream/main` passes. ### 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: #9448, #9571, #10078, #10373, #10376, #10395, #10398, #10418, #10447, #10448 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
copilot-pull-request-reviewer[bot] commented 2026-10-24 00:59:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-10-24T00:59:39Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5635954636 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10449#pullrequestreview-5635954636). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-10-24T00:59:39Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
MarcZX10 commented 2026-10-24 01:24:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T01:24:49Z.

Review: DISMISSED


I compiled Rsa.cpp and GuestLibreSslRsa.cpp directly from exact head 7b78f0e069e4f4f02d97bcf14074371b5458d57e with GCC 12/C++20 and ran the test executable; libSceLibreSsl RSA tests passed. The implementation's RSA_flags null-key behavior and RSA_size delegation to the modulus byte count match the pinned LibreSSL 2.5.4 sources cited in the PR. The regression cases cover zero and high-word boundaries, sign handling, layouts, and invalid pointers/counts.

I am requesting an update before approval because the repository's current-main check reports this branch is 2 commits ahead and 19 behind upstream/main (merge base e2b9700a9), although the merge is clean. Please refresh it against current main when its declared dependencies permit, then rerun the focused RSA test and conventions check on the refreshed exact head. This is the branch-sync requirement; I found no behavior issue in the reviewed change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5636140564 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10449#pullrequestreview-5636140564). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T01:24:49Z. **Review:** DISMISSED --- I compiled `Rsa.cpp` and `GuestLibreSslRsa.cpp` directly from exact head `7b78f0e069e4f4f02d97bcf14074371b5458d57e` with GCC 12/C++20 and ran the test executable; `libSceLibreSsl RSA tests passed`. The implementation's `RSA_flags` null-key behavior and `RSA_size` delegation to the modulus byte count match the pinned LibreSSL 2.5.4 sources cited in the PR. The regression cases cover zero and high-word boundaries, sign handling, layouts, and invalid pointers/counts. I am requesting an update before approval because the repository's current-main check reports this branch is 2 commits ahead and 19 behind `upstream/main` (merge base `e2b9700a9`), although the merge is clean. Please refresh it against current `main` when its declared dependencies permit, then rerun the focused RSA test and conventions check on the refreshed exact head. This is the branch-sync requirement; I found no behavior issue in the reviewed change.
ahmadmuj commented 2026-10-26 05:57:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-26T05:57:55Z.

Review: APPROVED


Reviewed head e44f645642d8 merged onto current main 14c99b52ace3 (clean merge; PR base ae7d59258ea7, and main hasn't touched libSceLibreSsl since then). Ubuntu 24.04 on WSL2, GCC 13.3, Release, focused build. This is the rerun MarcZX10 asked for, on the refreshed head.

What I ran:

  • guest_libressl_rsa: passes, 5 of 5 runs. The other built guest_libressl_* tests (digest, des, runtime, aes, error, ocsp, cipher) pass too.
  • public_headers and all 64 public_headers_compile_*: 65/65 pass, so the new Api.hpp declarations compile on their own.
  • check_conventions.py --base origin/main: passes.
  • Red control: the test doesn't compile against main's Api.hpp ('LibreSslRsa' does not name a type, RSA_flags_nid_postfix() takes no arguments).
  • Mutations in Rsa.cpp, each one fails the test: reading rsa->flags instead of meth->flags, dropping the + 7 round-up, throwing on a null key in RSA_flags, skipping a zero top word, and reading d when top is 0.

Sources, LibreSSL 2.5.4 at 2e2c6120:

  • rsa_crpt.c: RSA_size is BN_num_bytes(r->n) and RSA_flags is r == NULL ? 0 : r->meth->flags. Matches.
  • bn_lib.c BN_num_bits: returns 0 for top == 0 before it reads d, else (top - 1) * 64 + BN_num_bits_word(d[top - 1]), so a zero top word isn't skipped. Matches, and the 127/120 cases cover it.
  • rsa.h: on LP64 rsa_st has meth at 16, n at 32, flags at 108, size 152 (CRYPTO_EX_DATA is a single pointer in LibreSSL's crypto.h). rsa_meth_st has flags at 72, size 112. LibreSslRsa and LibreSslRsaMethod in Api.hpp match field by field, and bignum_st is 24 bytes with a 64-bit BN_ULONG.
  • Debt count: 340 NotImplemented_nid_no_patch stubs left in Export.cpp on the merged tree, same as the debt file.

One nit: the What section of the body still says the count drops "from 357 to 355"; the diff and the last Tested bullet have 342 -> 340.

Not checked: Windows, and anything on a console (the debt entry already says so).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5647941386 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10449#pullrequestreview-5647941386). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-26T05:57:55Z. **Review:** APPROVED --- Reviewed head `e44f645642d8` merged onto current `main` `14c99b52ace3` (clean merge; PR base `ae7d59258ea7`, and `main` hasn't touched `libSceLibreSsl` since then). Ubuntu 24.04 on WSL2, GCC 13.3, Release, focused build. This is the rerun MarcZX10 asked for, on the refreshed head. What I ran: - `guest_libressl_rsa`: passes, 5 of 5 runs. The other built `guest_libressl_*` tests (digest, des, runtime, aes, error, ocsp, cipher) pass too. - `public_headers` and all 64 `public_headers_compile_*`: 65/65 pass, so the new `Api.hpp` declarations compile on their own. - `check_conventions.py --base origin/main`: passes. - Red control: the test doesn't compile against `main`'s `Api.hpp` (`'LibreSslRsa' does not name a type`, `RSA_flags_nid_postfix()` takes no arguments). - Mutations in `Rsa.cpp`, each one fails the test: reading `rsa->flags` instead of `meth->flags`, dropping the `+ 7` round-up, throwing on a null key in `RSA_flags`, skipping a zero top word, and reading `d` when `top` is 0. Sources, LibreSSL 2.5.4 at `2e2c6120`: - `rsa_crpt.c`: `RSA_size` is `BN_num_bytes(r->n)` and `RSA_flags` is `r == NULL ? 0 : r->meth->flags`. Matches. - `bn_lib.c` `BN_num_bits`: returns 0 for `top == 0` before it reads `d`, else `(top - 1) * 64 + BN_num_bits_word(d[top - 1])`, so a zero top word isn't skipped. Matches, and the 127/120 cases cover it. - `rsa.h`: on LP64 `rsa_st` has `meth` at 16, `n` at 32, `flags` at 108, size 152 (`CRYPTO_EX_DATA` is a single pointer in LibreSSL's `crypto.h`). `rsa_meth_st` has `flags` at 72, size 112. `LibreSslRsa` and `LibreSslRsaMethod` in `Api.hpp` match field by field, and `bignum_st` is 24 bytes with a 64-bit `BN_ULONG`. - Debt count: 340 `NotImplemented_nid_no_patch` stubs left in `Export.cpp` on the merged tree, same as the debt file. One nit: the What section of the body still says the count drops "from 357 to 355"; the diff and the last Tested bullet have 342 -> 340. Not checked: Windows, and anything on a console (the debt entry already says so).
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!10449
No description provided.