feat(libSceLibreSsl3): implement BN_dup #15627

Merged
alvaroelpob merged 10 commits from upstream-pr/15627 into main 2026-11-10 06:34:52 +00:00
alvaroelpob commented 2026-11-03 22:22:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by alvaroelpob on 2026-11-03T22:22:11Z.


What

Implements BN_dup in libSceLibreSsl3, following LibreSSL 4.0.0 bn_lib.c: a null source returns null; otherwise a new libSceLibreSsl BIGNUM gets a guest-heap copy of the words, the sign and the constant-time flag. A failed allocation or a source over INT_MAX / 256 words returns null and queues ERR_R_MALLOC_FAILURE or BN_R_BIGNUM_TOO_LONG on the shared libSceLibreSsl error queue.

Tested

Linux x86-64 (WSL2), Release: the full build passes and ctest --test-dir build -j16 passes 3829 of 3829 tests, including the new guest_libressl3_bn_dup. python3 tools/check_conventions.py --base upstream/main passes. Not run on a console or in a title.

Checklist

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5145413411 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15627). Originally posted by [alvaroelpob](https://github.com/alvaroelpob) on 2026-11-03T22:22:11Z. --- ### What Implements `BN_dup` in libSceLibreSsl3, following LibreSSL 4.0.0 [`bn_lib.c`](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/bn/bn_lib.c#L71-L307): a null source returns null; otherwise a new libSceLibreSsl `BIGNUM` gets a guest-heap copy of the words, the sign and the constant-time flag. A failed allocation or a source over `INT_MAX / 256` words returns null and queues `ERR_R_MALLOC_FAILURE` or `BN_R_BIGNUM_TOO_LONG` on the shared libSceLibreSsl error queue. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux x86-64 (WSL2), Release: the full build passes and `ctest --test-dir build -j16` passes 3829 of 3829 tests, including the new `guest_libressl3_bn_dup`. `python3 tools/check_conventions.py --base upstream/main` passes. Not run on a console or in a title. ### 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: #12945, #14366, #14889, #14890, #14992, #15031, #15045, #15119, #15289, #15303, #15472, #15514, #15544, #15591, #15599, #15616, #15618, #15620, #15622, #15623, #15624, #15625 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
XairRrR commented 2026-11-03 23:43:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T23:43:41Z.

Review: APPROVED


Reviewed — approved

BN_dup matches LibreSSL 4.0.0 BN_new + bn_wexpand + BN_copy, including the queued error codes and the BN_FLG_CONSTTIME copy. 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, main 1c8c0efc8c41314402e6966da5bef6ea3d639d10, merged tree d481db5aaf5d72a9b6dee0b1ea8604ba7864bce0 (--no-ff), on Debian 13, GCC 14.2.0, Release, Ninja.

git diff --check 1c8c0efc8c41314402e6966da5bef6ea3d639d10...a87c18213efb31970a888f7dbf02bd7997429226
python3 tools/check_conventions.py --base 1c8c0efc8c41314402e6966da5bef6ea3d639d10
ctest --test-dir build --output-on-failure -R "^guest_libressl3_"
ctest --test-dir build --output-on-failure -R "public_headers"
# negative control 1: src/BignumDup.cpp replaced by main's throwing stub (NotImplemented_nid_no_patch) with the new signature
ninja -C build -j32 -l32 guest_libressl3_bn_dup_tests
ctest --test-dir build --output-on-failure -R "^guest_libressl3_bn_dup$"
# negative control 2: merged src/BignumDup.cpp without the `copy->flags |= source->flags & FlagConstantTime;` line
ninja -C build -j32 -l32 guest_libressl3_bn_dup_tests
ctest --test-dir build --output-on-failure -R "^guest_libressl3_bn_dup$"
# restore merged file, rebuild, rerun, confirm clean tree
git show d481db5aaf5d72a9b6dee0b1ea8604ba7864bce0:core/libs/prx/libSceLibreSsl3/src/BignumDup.cpp > core/libs/prx/libSceLibreSsl3/src/BignumDup.cpp
git status --porcelain
# reference check against the LibreSSL commit cited in the PR (present in the 3rdparty/libressl-openbsd object store)
git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/bn/bn_lib.c
git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/bn/bn.h
git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/err/err.h

Results

Check Result
git diff --check clean
check_conventions.py All conventions checks passed
Build (script, merged tree) OK
Selected tests (script): 835 ran 831 passed, 3 skipped (all expected), 0 new failures, 0 timeouts. One known failure (unwrap_self_..._inherits_the_destination_directory_group) also fails on main and is unrelated
ctest -R ^guest_libressl3_ (11 tests) 11/11 passed; guest_libressl3_bn_dup takes 0.01 s
ctest -R public_headers (65 tests) 65/65 passed
Negative control 1 (export back to a stub) fails: BN_dup_nid_postfix not implemented
Negative control 2 (no constant-time flag copy) fails at test line 90
Worktree after restore git status --porcelain empty; test passes again
Vulkan validation not applicable (no AGC/shader/VideoOut files)
Negative control 1 output
1/1 Test #2874: guest_libressl3_bn_dup ...........Subprocess aborted***Exception:   0.01 sec
[libc] unhandled exception 'St13runtime_error' thrown from 0x7f01a74cbb79
terminate called after throwing an instance of 'std::runtime_error'
  what():  BN_dup_nid_postfix not implemented
0% tests passed, 1 tests failed out of 1
Negative control 2 output
LibreSSL3 BN_dup check failed at line 90
0% tests passed, 1 tests failed out of 1

Reference check

I checked the implementation line by line against LibreSSL at the commit the PR cites (ce488b78e0bd):

  • BN_new uses calloc and sets BN_FLG_MALLOCED (0x01). On failure it queues ERR_R_MALLOC_FAILURE (1|ERR_R_FATAL = 65).
  • bn_wexpand returns 0 for a negative word count without queuing anything. That matches the silent null for a negative top.
  • bn_expand_internal queues BN_R_BIGNUM_TOO_LONG (114) above INT_MAX / (4 * BN_BITS2), which is INT_MAX / 256 with 64-bit words. It allocates a zeroed array of exactly words, sets dmax = words, and queues ERR_R_MALLOC_FAILURE on failure.
  • BN_copy copies the words, top, neg, and b->flags & BN_FLG_CONSTTIME (0x04).
  • BNerror uses function code 0xfff and ERR_LIB_BN = 3. With ERR_PACK, the test's expected codes are 0x03fff041 and 0x03fff072.
  • The 24-byte bignum_st layout matches the static_asserts in libSceLibreSsl/src/Bignum.cpp.

The copy's d comes from calloc_nid_postfix, and the struct has FlagMalloced without FlagStaticData. So the existing BN_free_nid_postfix frees both, and the test's liveBlocks == 0 check confirms it. A source with top > 0 and null d throws 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 with main's production behaviour: a NotImplemented_nid_no_patch stub, with the new signature so the test still compiles. The test aborted as expected. As a second check, I removed only the BN_FLG_CONSTTIME copy, 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:28 cites bn_lib.c#L71-L276, which runs from BN_new to BN_dup. The word, sign and BN_FLG_CONSTTIME copy that core/libs/prx/libSceLibreSsl3/src/BignumDup.cpp:48 reproduces is in BN_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

  • Behaviour on a console (the debt entry says so).
  • Windows/MinGW build and the new ENVIRONMENT_MODIFICATION block. I only built on Linux with GCC.
  • The description's claim of 3829/3829 tests passing. I ran only the selected tests, not the full suite.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5761074959 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15627#pullrequestreview-5761074959). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-11-03T23:43:41Z. **Review:** APPROVED --- # Reviewed — approved `BN_dup` matches LibreSSL 4.0.0 `BN_new` + `bn_wexpand` + `BN_copy`, including the queued error codes and the `BN_FLG_CONSTTIME` copy. 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`, `main` `1c8c0efc8c41314402e6966da5bef6ea3d639d10`, merged tree `d481db5aaf5d72a9b6dee0b1ea8604ba7864bce0` (`--no-ff`), on Debian 13, GCC 14.2.0, Release, Ninja. ``` git diff --check 1c8c0efc8c41314402e6966da5bef6ea3d639d10...a87c18213efb31970a888f7dbf02bd7997429226 python3 tools/check_conventions.py --base 1c8c0efc8c41314402e6966da5bef6ea3d639d10 ctest --test-dir build --output-on-failure -R "^guest_libressl3_" ctest --test-dir build --output-on-failure -R "public_headers" # negative control 1: src/BignumDup.cpp replaced by main's throwing stub (NotImplemented_nid_no_patch) with the new signature ninja -C build -j32 -l32 guest_libressl3_bn_dup_tests ctest --test-dir build --output-on-failure -R "^guest_libressl3_bn_dup$" # negative control 2: merged src/BignumDup.cpp without the `copy->flags |= source->flags & FlagConstantTime;` line ninja -C build -j32 -l32 guest_libressl3_bn_dup_tests ctest --test-dir build --output-on-failure -R "^guest_libressl3_bn_dup$" # restore merged file, rebuild, rerun, confirm clean tree git show d481db5aaf5d72a9b6dee0b1ea8604ba7864bce0:core/libs/prx/libSceLibreSsl3/src/BignumDup.cpp > core/libs/prx/libSceLibreSsl3/src/BignumDup.cpp git status --porcelain # reference check against the LibreSSL commit cited in the PR (present in the 3rdparty/libressl-openbsd object store) git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/bn/bn_lib.c git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/bn/bn.h git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/err/err.h ``` ## Results | Check | Result | |---|---| | `git diff --check` | clean | | `check_conventions.py` | All conventions checks passed | | Build (script, merged tree) | OK | | Selected tests (script): 835 ran | 831 passed, 3 skipped (all expected), 0 new failures, 0 timeouts. One known failure (`unwrap_self_..._inherits_the_destination_directory_group`) also fails on `main` and is unrelated | | `ctest -R ^guest_libressl3_` (11 tests) | 11/11 passed; `guest_libressl3_bn_dup` takes 0.01 s | | `ctest -R public_headers` (65 tests) | 65/65 passed | | Negative control 1 (export back to a stub) | fails: `BN_dup_nid_postfix not implemented` | | Negative control 2 (no constant-time flag copy) | fails at test line 90 | | Worktree after restore | `git status --porcelain` empty; test passes again | | Vulkan validation | not applicable (no AGC/shader/VideoOut files) | <details><summary>Negative control 1 output</summary> ``` 1/1 Test #2874: guest_libressl3_bn_dup ...........Subprocess aborted***Exception: 0.01 sec [libc] unhandled exception 'St13runtime_error' thrown from 0x7f01a74cbb79 terminate called after throwing an instance of 'std::runtime_error' what(): BN_dup_nid_postfix not implemented 0% tests passed, 1 tests failed out of 1 ``` </details> <details><summary>Negative control 2 output</summary> ``` LibreSSL3 BN_dup check failed at line 90 0% tests passed, 1 tests failed out of 1 ``` </details> ### Reference check I checked the implementation line by line against LibreSSL at the commit the PR cites (`ce488b78e0bd`): - `BN_new` uses `calloc` and sets `BN_FLG_MALLOCED` (0x01). On failure it queues `ERR_R_MALLOC_FAILURE` (`1|ERR_R_FATAL` = 65). - `bn_wexpand` returns 0 for a negative word count without queuing anything. That matches the silent null for a negative `top`. - `bn_expand_internal` queues `BN_R_BIGNUM_TOO_LONG` (114) above `INT_MAX / (4 * BN_BITS2)`, which is `INT_MAX / 256` with 64-bit words. It allocates a zeroed array of exactly `words`, sets `dmax = words`, and queues `ERR_R_MALLOC_FAILURE` on failure. - `BN_copy` copies the words, `top`, `neg`, and `b->flags & BN_FLG_CONSTTIME` (0x04). - `BNerror` uses function code 0xfff and `ERR_LIB_BN` = 3. With `ERR_PACK`, the test's expected codes are 0x03fff041 and 0x03fff072. - The 24-byte `bignum_st` layout matches the `static_assert`s in `libSceLibreSsl/src/Bignum.cpp`. The copy's `d` comes from `calloc_nid_postfix`, and the struct has `FlagMalloced` without `FlagStaticData`. So the existing `BN_free_nid_postfix` frees both, and the test's `liveBlocks == 0` check confirms it. A source with `top > 0` and null `d` throws 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 with `main`'s production behaviour: a `NotImplemented_nid_no_patch` stub, with the new signature so the test still compiles. The test aborted as expected. As a second check, I removed only the `BN_FLG_CONSTTIME` copy, 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:28` cites [`bn_lib.c#L71-L276`](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/bn/bn_lib.c#L71-L276), which runs from `BN_new` to `BN_dup`. The word, sign and `BN_FLG_CONSTTIME` copy that `core/libs/prx/libSceLibreSsl3/src/BignumDup.cpp:48` reproduces is in `BN_copy`, at [L289-L307](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/bn/bn_lib.c#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 - Behaviour on a console (the debt entry says so). - Windows/MinGW build and the new `ENVIRONMENT_MODIFICATION` block. I only built on Linux with GCC. - The description's claim of 3829/3829 tests passing. I ran only the selected tests, not the full suite. <!-- anyps5-bots job=3697 head=a87c18213efb31970a888f7dbf02bd7997429226 -->
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!15627
No description provided.