feat(libSceLibreSsl): implement the BN_ BIGNUM, BN_CTX, conversion, word, BN_mul and BN_mod_inverse functions #9446
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9446"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-21T22:23:49Z.
What
Implements 15
libSceLibreSslexports that wereNotImplemented_nid_no_patchstubs: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_mulandBN_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 toExport.cppis the removal of exactly these 15 stubs (BN_printstays a stub because it needs aBIO). The library had noCMakeLists.txtonmain, so one is added the way #9025, #9033, #9041 and #9043 add it (this file and theExport.cppremovals are where the textual conflicts are).BIGNUMis 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 readtopandnegdirectly (astatic_assertfixes size and offsets).BN_newreturns a zero value withdNULL andflagsset toBN_FLG_MALLOCED;BN_free(NULL)does nothing;dand the struct are allocated and freed through the libccallocandfree, soOPENSSL_free/CRYPTO_freework on them.BN_CTXis opaque and only countsBN_CTX_start/BN_CTX_end;BN_CTX_getis not part of this PR andBN_mulandBN_mod_inversedo not use the context.BN_bin2bnreads a big-endian magnitude (allocates whenretis NULL, returns NULL for a negative length or on allocation failure);BN_bn2binwrites the minimal big-endian length (0 bytes for zero) and returns it;BN_num_bitsis 0 for zero, else the bit length.BN_hex2bntakes 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*ais NULL, and-0is not negative.BN_set_word,BN_add_wordandBN_sub_wordcarry and borrow across limbs;BN_sub_wordbelow zero flipsneg.BN_mulis schoolbook multiplication on 64-bit limbs withunsigned __int128partial products and sign handling;rmay aliasaorb.BN_mod_inverseis the extended Euclidean algorithm; it returnsr(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_inversewith a negative modulus uses its magnitude and with a modulus of 1 returns 0, which is not compared with LibreSSL.BN_hex2bnrejects-alone, which older LibreSSL versions accept. NullBIGNUMpointers, which LibreSSL does not check, throw.Textual overlaps with open PRs: #9025, #9033, #9041, #9043 and #9047 also add
libSceLibreSsl/CMakeLists.txtand remove other stubs fromExport.cppand lines from the debt document; none implements aBN_function (a title search of the open PRs forBN_andBIGNUMreturns nothing).Tested
5c3dc05f05plus the CI scaffolding commit; run 39683521493, filterlibressl|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 WindowsPATHmodification 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 withraliasingaandb, negative and zero operands;BN_bin2bn/BN_bn2binwith leading zero bytes, reuse ofret, zero length and negative length;BN_num_bitsof 0, 1, 0x100 and 2^64 (65);BN_hex2bnwith-1F(returns 3,neg), odd digit count,FFzz(returns 2), empty,-,-0, a 24-digit value, a NULL*aand a NULLa;0xFFFFFFFFFFFFFFFF + 1(top2, value 2^64),2^64 - 1back to one limb,3 - 5 = -2withneg, carries across three limbs and negative values;BN_set_word(0);BN_mod_inverseof 3 mod 11 = 4, 7 mod 40 = 23, 4 mod 8 = NULL, negative and unreduceda,raliasinga, and a 128-bit prime (2^127 - 1) case wherea * inverseis checked withBN_mulagainstk * n + 1for a precomputedk. The expected values were computed with Python's integers.python tools/check_conventions.py --base origin/mainpasses. Based on0c6a5bd593.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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
BN_CTX_end_nid_postfixBN_CTX_free_nid_postfixBN_CTX_new_nid_postfixBN_CTX_start_nid_postfixBN_add_word_nid_postfixBN_bin2bn_nid_postfixBN_bn2bin_nid_postfixBN_free_nid_postfixBN_hex2bn_nid_postfixBN_mod_inverse_nid_postfixBN_mul_nid_postfixBN_new_nid_postfixBN_num_bits_nid_postfixBN_set_word_nid_postfixBN_sub_word_nid_postfixImported 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_mulis 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 isa6dcea927, while current main is110a18ba01. 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.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: noneis allowed), and it contained no code review.