Repository navigation
feat(libSceLibreSsl): implement the BN_ BIGNUM, BN_CTX, conversion, word, BN_mul and BN_mod_inverse functions - #9446
6 commits merged into
Conversation
…ord, BN_mul and BN_mod_inverse functions
Progress report (a6dcea9 → 5c3dc05)📈 System libraries: 42.47% (+0.14%, +15 implemented functions) ✅ 15 implemented
|
|
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.
|
MarcZX10
left a comment
There was a problem hiding this comment.
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.
|
Windows run of 5c3dc05 merged onto Built |
|
@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. |
|
@AlxcBTW firmware 13.42 data for the 15 BN exports. It covers the cases the description lists as not compared:
Matches
Differences
|
|
@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 |
|
@AlxcBTW I am refreshing the owned #9535 → #9536 → #9571 stack after the maintainer's latest main merge. #9446 is still open at 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. |
b9cd424
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
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 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 on 0c6a5bd.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)