feat(libSceLibreSsl3): implement BN_dup #15627
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/15627"
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 alvaroelpob on 2026-11-03T22:22:11Z.
What
Implements
BN_dupin libSceLibreSsl3, following LibreSSL 4.0.0bn_lib.c: a null source returns null; otherwise a new libSceLibreSslBIGNUMgets a guest-heap copy of the words, the sign and the constant-time flag. A failed allocation or a source overINT_MAX / 256words returns null and queuesERR_R_MALLOC_FAILUREorBN_R_BIGNUM_TOO_LONGon the shared libSceLibreSsl error queue.Tested
Linux x86-64 (WSL2), Release: the full build passes and
ctest --test-dir build -j16passes 3829 of 3829 tests, including the newguest_libressl3_bn_dup.python3 tools/check_conventions.py --base upstream/mainpasses. Not run on a console or in a title.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 XairRrR on 2026-11-03T23:43:41Z.
Review: APPROVED
Reviewed — approved
BN_dupmatches LibreSSL 4.0.0BN_new+bn_wexpand+BN_copy, including the queued error codes and theBN_FLG_CONSTTIMEcopy. It builds, the new test passes, and the test fails both when the export is put back to a throwing stub and when the constant-time copy is removed.What I ran
Head
a87c18213efb31970a888f7dbf02bd7997429226,main1c8c0efc8c41314402e6966da5bef6ea3d639d10, merged treed481db5aaf5d72a9b6dee0b1ea8604ba7864bce0(--no-ff), on Debian 13, GCC 14.2.0, Release, Ninja.Results
git diff --checkcheck_conventions.pyunwrap_self_..._inherits_the_destination_directory_group) also fails onmainand is unrelatedctest -R ^guest_libressl3_(11 tests)guest_libressl3_bn_duptakes 0.01 sctest -R public_headers(65 tests)BN_dup_nid_postfix not implementedgit status --porcelainempty; test passes againNegative control 1 output
Negative control 2 output
Reference check
I checked the implementation line by line against LibreSSL at the commit the PR cites (
ce488b78e0bd):BN_newusescallocand setsBN_FLG_MALLOCED(0x01). On failure it queuesERR_R_MALLOC_FAILURE(1|ERR_R_FATAL= 65).bn_wexpandreturns 0 for a negative word count without queuing anything. That matches the silent null for a negativetop.bn_expand_internalqueuesBN_R_BIGNUM_TOO_LONG(114) aboveINT_MAX / (4 * BN_BITS2), which isINT_MAX / 256with 64-bit words. It allocates a zeroed array of exactlywords, setsdmax = words, and queuesERR_R_MALLOC_FAILUREon failure.BN_copycopies the words,top,neg, andb->flags & BN_FLG_CONSTTIME(0x04).BNerroruses function code 0xfff andERR_LIB_BN= 3. WithERR_PACK, the test's expected codes are 0x03fff041 and 0x03fff072.bignum_stlayout matches thestatic_asserts inlibSceLibreSsl/src/Bignum.cpp.The copy's
dcomes fromcalloc_nid_postfix, and the struct hasFlagMallocedwithoutFlagStaticData. So the existingBN_free_nid_postfixfrees both, and the test'sliveBlocks == 0check confirms it. A source withtop > 0and nulldthrows where LibreSSL would dereference null, as the project rules require.Negative control
The PR adds
src/BignumDup.cpp. Moving it aside would break the CMake source list, so I replaced it withmain's production behaviour: aNotImplemented_nid_no_patchstub, with the new signature so the test still compiles. The test aborted as expected. As a second check, I removed only theBN_FLG_CONSTTIMEcopy, and the test failed at line 90. I then restored the merged file, rebuilt and reran the test (it passed), and confirmed the worktree is back to the merged tree.Findings
Blocking
None.
Non-blocking
docs/dev/debt/libSceLibreSsl3.md:28citesbn_lib.c#L71-L276, which runs fromBN_newtoBN_dup. The word, sign andBN_FLG_CONSTTIMEcopy thatcore/libs/prx/libSceLibreSsl3/src/BignumDup.cpp:48reproduces is inBN_copy, at L289-L307, outside the cited range. Extending the range to L307 (in the doc and the PR description) would make the source of the flag behaviour explicit.Not verified
ENVIRONMENT_MODIFICATIONblock. I only built on Linux with GCC.