Skip to content

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

Merged
6 commits merged into
boykopovar:mainfrom
AlxcBTW:pr/libssl-bignum
Nov 3, 2026
Merged

6 commits merged into
boykopovar:mainfrom
AlxcBTW:pr/libssl-bignum

Conversation

@AlxcBTW

@AlxcBTW AlxcBTW commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

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 56e58fa (the PR head 5c3dc05 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 0c6a5bd.

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

@github-actions

Copy link
Copy Markdown

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

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 9536 core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 10395 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 10418 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 10508 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 10570 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 10834 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 10878 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 10915 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 11003 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 11128 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 11146 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 11151 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 11174 core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 11175 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 11466 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 11553 docs/dev/debt/libSceLibreSsl.md
PR 11662 core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 12774 core/libs/prx/libSceLibreSsl/CMakeLists.txt
PR 12775 core/libs/prx/libSceLibreSsl/CMakeLists.txt
PR 12928 core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 13860 core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 14115 core/libs/prx/libSceLibreSsl/Export.cpp
docs/dev/debt/libSceLibreSsl.md
PR 14193 docs/dev/debt/libSceLibreSsl.md
PR 14212 docs/dev/debt/libSceLibreSsl.md
PR 14283 core/libs/prx/libSceLibreSsl/CMakeLists.txt
PR 14717 docs/dev/debt/libSceLibreSsl.md

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ycbuzi

ycbuzi commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Windows run of 5c3dc05 merged onto main 6a6a35f (154d7a7092, clean), the platform the description lists as not run. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

Built libSceLibreSsl and guest_libressl_bn_tests with no errors or warnings. ctest --repeat until-fail:2 on guest_libressl_bn: passes on both repeats. No negative control: the functions are implemented in src/Bignum.cpp, which this PR adds. Not run: a title.

@Felipebmaitan

Copy link
Copy Markdown
Contributor

@AlxcBTW oneandonlydean asked us to settle the adjacent BN/CMAC export and debt insertions for #9571. I propose #9446 before #9571 and will make #9446 an explicit prerequisite of the CMAC PR, alongside its existing BIO prerequisite (#9536).

I am preparing the integration so the 15 BN implementations, all CMAC/AES-CBC implementations, and both sets of debt entries are retained. I will keep #9571 waiting on #9446. Please confirm this order works for your branch; I will record your confirmation rather than treat silence as agreement.

@BunnyFl

BunnyFl commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@AlxcBTW firmware 13.42 data for the 15 BN exports. It covers the cases the description lists as not compared: BN_mod_inverse with a modulus of 1 or a negative modulus, and BN_hex2bn on - alone. I ran the decrypted libSceLibreSsl.sprx's exports natively on a PC (not a console), against src/Bignum.cpp from 5c3dc05f05 compiled unchanged.

  • Operands were built the same way on both sides: the side's own BN_new, then d/top/dmax/neg written at the PR's offsets.
  • Each call printed the return value (NULL, the passed BIGNUM or a new one), the resulting sign, limbs, top, neg and flags, and the queued errors.
  • The module's _Getpctype import was a stand-in table that sets bit 0x01 (the bit BN_hex2bn tests) for 0-9, a-f and A-F.
  • Three identical runs, 7,283 compared calls.

Matches

  • BN_new: d NULL, top 0, dmax 0, neg 0 and flags 1 on both. BN_CTX_start/BN_CTX_end nested three deep work on both.
  • BN_bn2bin and BN_num_bits: 600 of 600 values, up to 8 limbs, with negative and zero values included.
  • BN_set_word, BN_add_word and BN_sub_word: 900 of 900, including words 0, 2^63 and 2^64-1.
  • BN_mul: 900 of 900, including r aliasing a, b or both.
  • BN_mod_inverse: the result, or NULL, matches in all 1,500 cases. Both cases the description lists as not compared behave the same as the PR on the module:
    • a modulus of 1 or -1 gives 0;
    • a negative modulus uses its magnitude. For example, 3 mod -11 gives 4, -3 mod -11 gives 7 and 7 mod -40 gives 23.

Differences

  1. BN_hex2bn (114 of 2,781 cases). The module accepts - alone, the older behaviour the debt note mentions:
Input Module PR
no hex digit, after an optional - (-, -zz, --1, zz, 1, +1) returns 1 when there is a -, otherwise 0. When a is not NULL it still stores a zero in *a: a new BIGNUM if *a was NULL, or the given one zeroed, with neg 1 after a -. returns 0, *a untouched
-0, -000, -0000 same return value; *a holds a negative zero (top 0, neg 1) neg 0
  1. BN_bin2bn with length 0 into a given negative BIGNUM: the module sets top to 0 but keeps neg 1, while the PR clears it.

  2. Queued errors. The PR queues none in these functions; the module queues these:

    • BN_bin2bn with length -1: 0x03FFF072, both returning NULL;
    • BN_mod_inverse without an inverse: 0x03FFF06C;
    • BN_mod_inverse with modulus 0: 0x03FFF067.

    All 967 BN_mod_inverse mismatches are these two error codes alone.

  3. dmax (the allocated limb count) differs in 350 cases, with equal values.

  • Controls: copies of Bignum.cpp with one change each are caught. These are:
    • a dropped final carry in the multiply;
    • bit length minus 1;
    • no upper-case hex digits;
    • little-endian BN_bn2bin;
    • no sign fix-up in the inverse;
    • an unsigned product;
    • a negative length treated as 0;
    • no borrow propagation in the subtraction.
  • Not run: NULL BIGNUM pointers (the PR throws), BN_FLG_STATIC_DATA, and characters above 0x7F in BN_hex2bn.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@AlxcBTW thanks for the BN_ work. 15 of these exports are still stubs on main, so this is still wanted, but the branch now conflicts with main in libSceLibreSsl/CMakeLists.txt and docs/dev/debt/libSceLibreSsl.md. Could you rebase it? While you are there, BunnyFl's firmware 13.42 data covers the cases the description lists as not compared (BN_mod_inverse with a modulus of 1 or a negative modulus, BN_hex2bn on - alone); please compare against it and update the debt entry. Note that #9448 edits the same two files, so whichever lands second rebases.

@Felipebmaitan

Copy link
Copy Markdown
Contributor

@AlxcBTW I am refreshing the owned #9535 → #9536 → #9571 stack after the maintainer's latest main merge. #9446 is still open at 5c3dc05f0555b066beab1184f666829d3f4af780, and #9571 already preserves that published BN head and its debt alongside CMAC.

Could you explicitly confirm #9446 before #9571, as proposed in #9446 (comment)? I will retain #9446 under Depends on and keep #9571 waiting until this is settled. If you prefer the reverse order, please say so; we would need to record that order and preserve your BN fixes and debt when reconciling the branches.

I have read the maintainer's rebase/firmware-evidence request here. I am not changing your branch or treating the inherited older BN implementation as a resolution of that separate request. No agreement is assumed from silence. AI-assisted: yes.

@CnRJay CnRJay left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@oneandonlydean oneandonlydean closed this pull request by merging all changes into boykopovar:main in b9cd424 Nov 3, 2026
@JaceTheBlu JaceTheBlu mentioned this pull request Nov 3, 2026
12 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs needs rebase Conflicts with main, rebase needed prx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants