fix(libSceLibreSsl): report BN_R_NO_INVERSE from BN_mod_inverse #16741

Merged
AlexWhitehouse merged 1 commit from upstream-pr/16741 into main 2026-11-11 05:40:44 +00:00
AlexWhitehouse commented 2026-11-07 14:20:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexWhitehouse on 2026-11-07T14:20:42Z.


What

In libSceLibreSsl (the older LibreSSL module), BN_mod_inverse (NID Gz+I4uxmv7M) returned null when the operand has no inverse, but left the error queue empty. On main, BN_mod_inverse(nullptr, 2, 4, ctx) returns null and ERR_get_error() returns 0.

LibreSSL 2.5.4's bn_gcd.c, the version the module's other BIGNUM functions follow, handles this case as follows:

  • it works modulo |n| (A = n; A->neg = 0);
  • when the gcd is not 1 it calls BNerror(BN_R_NO_INVERSE) and returns null, leaving a caller-supplied result untouched. The constant-time variant does the same at lines 701–722.
  • In err.h, BNerror uses library ERR_LIB_BN (3, line 160) and function 0xfff (line 241). bn.h defines BN_R_NO_INVERSE as 108 (line 705).

When InverseMod finds no inverse, BN_mod_inverse in core/libs/prx/libSceLibreSsl/src/Bignum.cpp now queues that error through the module's existing LibreSslPutError_nid_no_patch, so ERR_get_error() returns 0x03fff06c. That applies to a nonzero modulus of either sign. Other paths are unchanged:

  • a zero modulus still returns null without an error (upstream reports a division by zero there);
  • a modulus of 1 still gives 0, as upstream;
  • allocation failures are unchanged;
  • this module has no constant-time dispatch.

A new debt entry records that this is not measured on the console, and that other BIGNUM error paths still queue nothing.

Tested

  • guest_libressl_bn (core/libs/tests/GuestLibreSslBn.cpp) already checked that (4, 8) has no inverse. It now also checks:

    • that call queues exactly 0x03fff06c, after which the queue is empty;
    • the same for (2, 4), (6, 9), (0, 11), (2, -4) and (-6, -9), each with and without a caller-supplied result, whose value must stay unchanged;
    • that the existing successful inverses leave the queue empty.

    Every existing assertion is kept.

  • Linux x86-64 (Ubuntu 26.04, GCC 15, Release), the libSceLibreSsl tests:

    • with main's Bignum.cpp, guest_libressl_bn fails at its first error-queue check (check failed at line 212);
    • with this PR (4a05e40a2d), 61/61 pass.
  • Windows 11 (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release):

    • the same 61 tests pass at 4a05e40a2d, with no warnings in the changed files;
    • with main's Bignum.cpp, guest_libressl_bn fails at the same check.
  • Mutation checks:

    • changing the reason from 108 to 109 fails the test;
    • queueing the error only for positive moduli fails the negative-modulus cases.
  • python3 tools/check_conventions.py --base origin/main: all conventions checks passed.

  • python3 tools/pr_overlap.py --branch fix/libscelibressl-bn-mod-inverse-error: no open PR changes BN_mod_inverse.

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
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5202491552 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16741). Originally posted by [AlexWhitehouse](https://github.com/AlexWhitehouse) on 2026-11-07T14:20:42Z. --- ### What In libSceLibreSsl (the older LibreSSL module), `BN_mod_inverse` (NID `Gz+I4uxmv7M`) returned null when the operand has no inverse, but left the error queue empty. On `main`, `BN_mod_inverse(nullptr, 2, 4, ctx)` returns null and `ERR_get_error()` returns 0. [LibreSSL 2.5.4's `bn_gcd.c`](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/bn/bn_gcd.c#L519-L540), the version the module's other BIGNUM functions follow, handles this case as follows: - it works modulo `|n|` (`A = n; A->neg = 0`); - when the gcd is not 1 it calls `BNerror(BN_R_NO_INVERSE)` and returns null, leaving a caller-supplied result untouched. The constant-time variant does the same at lines 701–722. - In [`err.h`](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/err/err.h#L241), `BNerror` uses library `ERR_LIB_BN` (3, line 160) and function 0xfff (line 241). [`bn.h`](https://github.com/libressl/openbsd/blob/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto/bn/bn.h#L705) defines `BN_R_NO_INVERSE` as 108 (line 705). When `InverseMod` finds no inverse, `BN_mod_inverse` in `core/libs/prx/libSceLibreSsl/src/Bignum.cpp` now queues that error through the module's existing `LibreSslPutError_nid_no_patch`, so `ERR_get_error()` returns `0x03fff06c`. That applies to a nonzero modulus of either sign. Other paths are unchanged: - a zero modulus still returns null without an error (upstream reports a division by zero there); - a modulus of 1 still gives 0, as upstream; - allocation failures are unchanged; - this module has no constant-time dispatch. A new debt entry records that this is not measured on the console, and that other BIGNUM error paths still queue nothing. ### Tested - **`guest_libressl_bn`** (`core/libs/tests/GuestLibreSslBn.cpp`) already checked that `(4, 8)` has no inverse. It now also checks: - that call queues exactly `0x03fff06c`, after which the queue is empty; - the same for `(2, 4)`, `(6, 9)`, `(0, 11)`, `(2, -4)` and `(-6, -9)`, each with and without a caller-supplied result, whose value must stay unchanged; - that the existing successful inverses leave the queue empty. Every existing assertion is kept. - **Linux x86-64** (Ubuntu 26.04, GCC 15, Release), the libSceLibreSsl tests: - with `main`'s `Bignum.cpp`, `guest_libressl_bn` fails at its first error-queue check (`check failed at line 212`); - with this PR (4a05e40a2d), 61/61 pass. - **Windows 11** (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release): - the same 61 tests pass at 4a05e40a2d, with no warnings in the changed files; - with `main`'s `Bignum.cpp`, `guest_libressl_bn` fails at the same check. - **Mutation checks:** - changing the reason from 108 to 109 fails the test; - queueing the error only for positive moduli fails the negative-modulus cases. - `python3 tools/check_conventions.py --base origin/main`: all conventions checks passed. - **`python3 tools/pr_overlap.py --branch fix/libscelibressl-bn-mod-inverse-error`:** no open PR changes `BN_mod_inverse`. - It could not compare 18 PRs, all of which already conflict with `main`. Trial merges of each add no conflict beyond the ones they already have with `main`: #10508, #11466, #13266, #13285, #13546, #13547, #13877, #14115, #14212, #15480, #15737, #15994, #16241, #16242, #16249, #16255, #16529 and #16543. - #16079 (digest/HMAC errors) merges cleanly. - My #16649 (`EVP_DecryptFinal_ex` errors) touches the same debt file in a different entry. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-09 08:52:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-09T08:52:41Z.

Review: COMMENTED


Windows run of head 4a05e40a2d, merged onto main 286b14d2f7 in one tree with 7 other small open PRs that touch different files. Setup: Windows 11, WinLibs GCC 15.2 r7 (x86_64-ucrt-posix-seh, the CI toolchain), Release, BUILD_TESTING=ON.

  • guest_libressl_bn with --repeat until-fail:5: 5 / 5 pass.
  • Negative control, with main's Bignum.cpp and this PR's test: 5 / 5 runs fail.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5823554305 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16741#pullrequestreview-5823554305). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-09T08:52:41Z. **Review:** COMMENTED --- Windows run of head `4a05e40a2d`, merged onto `main` `286b14d2f7` in one tree with 7 other small open PRs that touch different files. Setup: Windows 11, WinLibs GCC 15.2 r7 (`x86_64-ucrt-posix-seh`, the CI toolchain), Release, `BUILD_TESTING=ON`. - `guest_libressl_bn` with `--repeat until-fail:5`: 5 / 5 pass. - **Negative control,** with `main`'s `Bignum.cpp` and this PR's test: 5 / 5 runs fail.
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!16741
No description provided.