feat(libSceLibreSsl): implement the BN_ BIGNUM, BN_CTX, conversion, word, BN_mul and BN_mod_inverse functions #9446

Merged
AlxcBTW merged 6 commits from upstream-pr/9446 into main 2026-11-03 08:07:22 +00:00
AlxcBTW commented 2026-10-21 22:23:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-21T22:23:49Z.


What

Implements 15 libSceLibreSsl exports that were NotImplemented_nid_no_patch stubs: BN_new, BN_free, BN_CTX_new, BN_CTX_free, BN_CTX_start, BN_CTX_end, BN_bin2bn, BN_bn2bin, BN_hex2bn, BN_num_bits, BN_set_word, BN_add_word, BN_sub_word, BN_mul and BN_mod_inverse. They are the base for the RSA, EC and DH functions that are still stubs.

The code is the new core/libs/prx/libSceLibreSsl/src/Bignum.cpp. The only change to Export.cpp is the removal of exactly these 15 stubs (BN_print stays a stub because it needs a BIO). The library had no CMakeLists.txt on main, so one is added the way #9025, #9033, #9041 and #9043 add it (this file and the Export.cpp removals are where the textual conflicts are).

  • BIGNUM is the public LibreSSL 2.x struct { unsigned long* d; int top; int dmax; int neg; int flags; }, 24 bytes with 64-bit limbs in little-endian limb order, because guests read top and neg directly (a static_assert fixes size and offsets). BN_new returns a zero value with d NULL and flags set to BN_FLG_MALLOCED; BN_free(NULL) does nothing; d and the struct are allocated and freed through the libc calloc and free, so OPENSSL_free/CRYPTO_free work on them.
  • BN_CTX is opaque and only counts BN_CTX_start/BN_CTX_end; BN_CTX_get is not part of this PR and BN_mul and BN_mod_inverse do not use the context.
  • BN_bin2bn reads a big-endian magnitude (allocates when ret is NULL, returns NULL for a negative length or on allocation failure); BN_bn2bin writes the minimal big-endian length (0 bytes for zero) and returns it; BN_num_bits is 0 for zero, else the bit length.
  • BN_hex2bn takes an optional -, consumes hex digits, stops at the first other character and returns the number of characters consumed including the sign, or 0 for an empty string or one without a hex digit; it allocates when *a is NULL, and -0 is not negative.
  • BN_set_word, BN_add_word and BN_sub_word carry and borrow across limbs; BN_sub_word below zero flips neg.
  • BN_mul is schoolbook multiplication on 64-bit limbs with unsigned __int128 partial products and sign handling; r may alias a or b.
  • BN_mod_inverse is the extended Euclidean algorithm; it returns r (allocated when NULL) with the result in [0, n), or NULL when the gcd is not 1.

Backing: own implementation of standard multi-precision arithmetic following the public struct definition and documented API semantics; no third-party code (mbedtls is not available to this library).

Limits (recorded in docs/dev/debt/libSceLibreSsl.md, the pending-export count goes from 399 to 384): the module's LibreSSL version is unknown and nothing is checked on the console. BN_mod_inverse with a negative modulus uses its magnitude and with a modulus of 1 returns 0, which is not compared with LibreSSL. BN_hex2bn rejects - alone, which older LibreSSL versions accept. Null BIGNUM pointers, which LibreSSL does not check, throw.

Textual overlaps with open PRs: #9025, #9033, #9041, #9043 and #9047 also add libSceLibreSsl/CMakeLists.txt and remove other stubs from Export.cpp and lines from the debt document; none implements a BN_ function (a title search of the open PRs for BN_ and BIGNUM returns nothing).

Tested

  • Linux (Ubuntu 24.04), fork CI "Lab tests" at 56e58fa49f (the PR head 5c3dc05f05 plus the CI scaffolding commit; run 39683521493, filter libressl|libreSsl): 1 of 1 passed, guest_libressl_bn. The library has no other tests, so the filter selects only this one. Not run on Windows; the test is registered with the Windows PATH modification like the other library tests, but no Windows run was made.
  • guest_libressl_bn (core/libs/tests/GuestLibreSslBn.cpp, calls the exports directly, no GPU and no game) checks, among others: 0xFFFFFFFFFFFFFFFF * 0xFFFFFFFFFFFFFFFF = 0xFFFFFFFFFFFFFFFE0000000000000001; multi-limb products with r aliasing a and b, negative and zero operands; BN_bin2bn/BN_bn2bin with leading zero bytes, reuse of ret, zero length and negative length; BN_num_bits of 0, 1, 0x100 and 2^64 (65); BN_hex2bn with -1F (returns 3, neg), odd digit count, FFzz (returns 2), empty, -, -0, a 24-digit value, a NULL *a and a NULL a; 0xFFFFFFFFFFFFFFFF + 1 (top 2, value 2^64), 2^64 - 1 back to one limb, 3 - 5 = -2 with neg, carries across three limbs and negative values; BN_set_word(0); BN_mod_inverse of 3 mod 11 = 4, 7 mod 40 = 23, 4 mod 8 = NULL, negative and unreduced a, r aliasing a, and a 128-bit prime (2^127 - 1) case where a * inverse is checked with BN_mul against k * n + 1 for a precomputed k. The expected values were computed with Python's integers.
  • python tools/check_conventions.py --base origin/main passes. Based on 0c6a5bd593.

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 (generic API coverage, no title import evidence yet; the base for later RSA, EC and DH functions)
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4959169821 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9446). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-21T22:23:49Z. --- ### What Implements 15 `libSceLibreSsl` exports that were `NotImplemented_nid_no_patch` stubs: `BN_new`, `BN_free`, `BN_CTX_new`, `BN_CTX_free`, `BN_CTX_start`, `BN_CTX_end`, `BN_bin2bn`, `BN_bn2bin`, `BN_hex2bn`, `BN_num_bits`, `BN_set_word`, `BN_add_word`, `BN_sub_word`, `BN_mul` and `BN_mod_inverse`. They are the base for the RSA, EC and DH functions that are still stubs. The code is the new `core/libs/prx/libSceLibreSsl/src/Bignum.cpp`. The only change to `Export.cpp` is the removal of exactly these 15 stubs (`BN_print` stays a stub because it needs a `BIO`). The library had no `CMakeLists.txt` on `main`, so one is added the way #9025, #9033, #9041 and #9043 add it (this file and the `Export.cpp` removals are where the textual conflicts are). - `BIGNUM` is the public LibreSSL 2.x struct `{ unsigned long* d; int top; int dmax; int neg; int flags; }`, 24 bytes with 64-bit limbs in little-endian limb order, because guests read `top` and `neg` directly (a `static_assert` fixes size and offsets). `BN_new` returns a zero value with `d` NULL and `flags` set to `BN_FLG_MALLOCED`; `BN_free(NULL)` does nothing; `d` and the struct are allocated and freed through the libc `calloc` and `free`, so `OPENSSL_free`/`CRYPTO_free` work on them. - `BN_CTX` is opaque and only counts `BN_CTX_start`/`BN_CTX_end`; `BN_CTX_get` is not part of this PR and `BN_mul` and `BN_mod_inverse` do not use the context. - `BN_bin2bn` reads a big-endian magnitude (allocates when `ret` is NULL, returns NULL for a negative length or on allocation failure); `BN_bn2bin` writes the minimal big-endian length (0 bytes for zero) and returns it; `BN_num_bits` is 0 for zero, else the bit length. - `BN_hex2bn` takes an optional `-`, consumes hex digits, stops at the first other character and returns the number of characters consumed including the sign, or 0 for an empty string or one without a hex digit; it allocates when `*a` is NULL, and `-0` is not negative. - `BN_set_word`, `BN_add_word` and `BN_sub_word` carry and borrow across limbs; `BN_sub_word` below zero flips `neg`. - `BN_mul` is schoolbook multiplication on 64-bit limbs with `unsigned __int128` partial products and sign handling; `r` may alias `a` or `b`. - `BN_mod_inverse` is the extended Euclidean algorithm; it returns `r` (allocated when NULL) with the result in `[0, n)`, or NULL when the gcd is not 1. Backing: own implementation of standard multi-precision arithmetic following the public struct definition and documented API semantics; no third-party code (mbedtls is not available to this library). Limits (recorded in `docs/dev/debt/libSceLibreSsl.md`, the pending-export count goes from 399 to 384): the module's LibreSSL version is unknown and nothing is checked on the console. `BN_mod_inverse` with a negative modulus uses its magnitude and with a modulus of 1 returns 0, which is not compared with LibreSSL. `BN_hex2bn` rejects `-` alone, which older LibreSSL versions accept. Null `BIGNUM` pointers, which LibreSSL does not check, throw. Textual overlaps with open PRs: #9025, #9033, #9041, #9043 and #9047 also add `libSceLibreSsl/CMakeLists.txt` and remove other stubs from `Export.cpp` and lines from the debt document; none implements a `BN_` function (a title search of the open PRs for `BN_` and `BIGNUM` returns nothing). ### Tested - Linux (Ubuntu 24.04), fork CI "Lab tests" at 56e58fa49f (the PR head 5c3dc05f0555 plus the CI scaffolding commit; run 39683521493, filter `libressl|libreSsl`): 1 of 1 passed, `guest_libressl_bn`. The library has no other tests, so the filter selects only this one. Not run on Windows; the test is registered with the Windows `PATH` modification like the other library tests, but no Windows run was made. - `guest_libressl_bn` (`core/libs/tests/GuestLibreSslBn.cpp`, calls the exports directly, no GPU and no game) checks, among others: `0xFFFFFFFFFFFFFFFF * 0xFFFFFFFFFFFFFFFF = 0xFFFFFFFFFFFFFFFE0000000000000001`; multi-limb products with `r` aliasing `a` and `b`, negative and zero operands; `BN_bin2bn`/`BN_bn2bin` with leading zero bytes, reuse of `ret`, zero length and negative length; `BN_num_bits` of 0, 1, 0x100 and 2^64 (65); `BN_hex2bn` with `-1F` (returns 3, `neg`), odd digit count, `FFzz` (returns 2), empty, `-`, `-0`, a 24-digit value, a NULL `*a` and a NULL `a`; `0xFFFFFFFFFFFFFFFF + 1` (`top` 2, value 2^64), `2^64 - 1` back to one limb, `3 - 5 = -2` with `neg`, carries across three limbs and negative values; `BN_set_word(0)`; `BN_mod_inverse` of 3 mod 11 = 4, 7 mod 40 = 23, 4 mod 8 = NULL, negative and unreduced `a`, `r` aliasing `a`, and a 128-bit prime (2^127 - 1) case where `a * inverse` is checked with `BN_mul` against `k * n + 1` for a precomputed `k`. The expected values were computed with Python's integers. - `python tools/check_conventions.py --base origin/main` passes. Based on 0c6a5bd593. ### 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 (generic API coverage, no title import evidence yet; the base for later RSA, EC and DH functions) - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-21 22:24:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-21T22:24:40Z.


Progress report (a6dcea9 → 5c3dc05)

📈 System libraries: 42.47% (+0.14%, +15 implemented functions)

✅ 15 implemented
Library Name
libSceLibreSsl BN_CTX_end_nid_postfix
libSceLibreSsl BN_CTX_free_nid_postfix
libSceLibreSsl BN_CTX_new_nid_postfix
libSceLibreSsl BN_CTX_start_nid_postfix
libSceLibreSsl BN_add_word_nid_postfix
libSceLibreSsl BN_bin2bn_nid_postfix
libSceLibreSsl BN_bn2bin_nid_postfix
libSceLibreSsl BN_free_nid_postfix
libSceLibreSsl BN_hex2bn_nid_postfix
libSceLibreSsl BN_mod_inverse_nid_postfix
libSceLibreSsl BN_mul_nid_postfix
libSceLibreSsl BN_new_nid_postfix
libSceLibreSsl BN_num_bits_nid_postfix
libSceLibreSsl BN_set_word_nid_postfix
libSceLibreSsl BN_sub_word_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6327888611 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9446#issuecomment-6327888611). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-21T22:24:40Z. --- <!-- progress-report --> ### Progress report (a6dcea9 → 5c3dc05) 📈 **System libraries**: 42.47% (+0.14%, +15 implemented functions) <details> <summary>✅ 15 implemented</summary> | Library | Name | | - | - | | libSceLibreSsl | `BN_CTX_end_nid_postfix` | | libSceLibreSsl | `BN_CTX_free_nid_postfix` | | libSceLibreSsl | `BN_CTX_new_nid_postfix` | | libSceLibreSsl | `BN_CTX_start_nid_postfix` | | libSceLibreSsl | `BN_add_word_nid_postfix` | | libSceLibreSsl | `BN_bin2bn_nid_postfix` | | libSceLibreSsl | `BN_bn2bin_nid_postfix` | | libSceLibreSsl | `BN_free_nid_postfix` | | libSceLibreSsl | `BN_hex2bn_nid_postfix` | | libSceLibreSsl | `BN_mod_inverse_nid_postfix` | | libSceLibreSsl | `BN_mul_nid_postfix` | | libSceLibreSsl | `BN_new_nid_postfix` | | libSceLibreSsl | `BN_num_bits_nid_postfix` | | libSceLibreSsl | `BN_set_word_nid_postfix` | | libSceLibreSsl | `BN_sub_word_nid_postfix` | </details>
MarcZX10 commented 2026-10-22 04:38:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:38:09Z.

Review: CHANGES_REQUESTED


I reviewed the multi-limb representation and the reported vectors: aliasing in BN_mul is handled by loading operands before storing, word arithmetic keeps signed magnitude consistent, the conversion code covers leading zeros and empty values, and modular inverse reduces negative inputs before the Euclidean loop. The Linux test report includes useful multi-limb, aliasing and non-coprime cases. I did not run the test suite, and no Windows run is reported.

The branch is based on an older dependency snapshot: its parent is not the current #9025 head (52a439bc), and its merge base is a6dcea927, while current main is 110a18ba01. Please update onto the latest #9025 head (or wait for it to merge, then rebase on current main), preserve the shared CMake/debt edits with #9033/#9041/#9043/#9047/#9448, and rerun the BN tests, conventions and overlap checks. Then include a Windows target/test result if available; the current CI skips full product jobs.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609326943 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9446#pullrequestreview-5609326943). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T04:38:09Z. **Review:** CHANGES_REQUESTED --- I reviewed the multi-limb representation and the reported vectors: aliasing in `BN_mul` is handled by loading operands before storing, word arithmetic keeps signed magnitude consistent, the conversion code covers leading zeros and empty values, and modular inverse reduces negative inputs before the Euclidean loop. The Linux test report includes useful multi-limb, aliasing and non-coprime cases. I did not run the test suite, and no Windows run is reported. The branch is based on an older dependency snapshot: its parent is not the current #9025 head (`52a439bc`), and its merge base is `a6dcea927`, while current main is `110a18ba01`. Please update onto the latest #9025 head (or wait for it to merge, then rebase on current main), preserve the shared CMake/debt edits with #9033/#9041/#9043/#9047/#9448, and rerun the BN tests, conventions and overlap checks. Then include a Windows target/test result if available; the current CI skips full product jobs.
CnRJay commented 2026-11-01 20:01:23 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Withdrawn. This review applied a title-ID/log requirement that is not in CONTRIBUTING (Unblocks: none is allowed), and it contained no code review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729961858 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9446#pullrequestreview-5729961858). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T20:01:23Z. **Review:** COMMENTED --- Withdrawn. This review applied a title-ID/log requirement that is not in CONTRIBUTING (`Unblocks: none` is allowed), and it contained no code review.
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!9446
No description provided.