Skip to content

feat(libSceLibreSsl): implement EC_KEY_get0_private_key and EC_KEY_set_private_key - #11146

Open
ahmadmuj wants to merge 61 commits into
boykopovar:mainfrom
ahmadmuj:fix/anyps5-lssl-ec-privkey
Open

ahmadmuj wants to merge 61 commits into
boykopovar:mainfrom
ahmadmuj:fix/anyps5-lssl-ec-privkey

Conversation

@ahmadmuj

@ahmadmuj ahmadmuj commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

What

Implements EC_KEY_get0_private_key and EC_KEY_set_private_key with a guest-heap BIGNUM. Copying, freeing and generation follow LibreSSL 2.5.4. Checking, ECDSA and ECDH use that stored key.

Head 596aeb860cc9fb1df689c6be571d9ebf2817cbe5 merges main 648ba19b3edec93a073217bda7087dedcfd85454 without rewriting history. Replacement #15827 is now merged. This keeps main's point operations, ASN.1 flags and 64-byte EcKey layout, with references at offset 48.

Native DER decoding and encoding now use the public BIGNUM APIs instead of the replaced private-key layout. The decoder keeps its range checks and error queue. Encoding keeps fixed-width scalar padding. Existing native test helpers use the same APIs. The EC parameters test registers its allocator before key generation.

The existing private-key regression and maintainer removals stay unchanged. Known limits stay in the debt fragment: unsupported negative or non-normalized keys throw; signing and ECDH reject out-of-range keys; some failures queue no error. No console layout claim is made. #11466 carries this feature as a dependency; its own point decoder changes different functions.

Tested

Linux x86_64, Ubuntu 24.04 on WSL2, GCC 13.3, Release, Intel Core i7-13700K. No title or console run.

On the exact head above, 17 focused CPU tests pass: legacy EC, EC keys, EC private key, EC point multiplication and PKEY lifecycle; native ASN.1 flag, EC group, EC parameters, PKEY getters, ECDSA, public key, key reference, DER, PEM and PKEY context. Main's SSL lifecycle and ex-data tests also pass.

The DER test checks exact P-256/P-384 round trips, scalar-one public-key derivation and padding, zero/order rejection, exact error codes and allocation balance. The earlier padding mutation fails, and its restoration passes, at 4bd52813585b11b9a768f66faeba39c5e3adace4. That source and test remain byte-identical here; this merge does not repeat the mutation.

Commands to reproduce the checks, from a credential-free shell (the build targets ran in groups):

cmake -S . -B ../../builds/align-11146-054 -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DBUILD_TESTING=ON -DANYPS5_LIBRESSL_ORACLE=OFF
cmake --build ../../builds/align-11146-054 --parallel 16 --target libSceLibreSsl guest_libressl_ec_tests guest_libressl_ec_keys_tests guest_libressl_ec_private_key_tests guest_libressl_ec_point_mul_tests guest_libressl_pkey_lifecycle_tests guest_libressl3_ec_key_asn1_flag_tests guest_libressl3_ec_group_tests guest_libressl3_ec_parameters_tests guest_libressl3_pkey_getters_tests guest_libressl3_ecdsa_tests guest_libressl3_ec_public_key_tests guest_libressl3_key_reference_tests guest_libressl3_key_der_tests guest_libressl3_pem_keys_tests guest_libressl3_pkey_context_tests libressl3_pkey_lifecycle_modules guest_libressl_ssl_lifecycle_tests guest_libressl_ex_data_tests
ctest --test-dir ../../builds/align-11146-054 -R '^(guest_libressl_(ec|ec_keys|ec_private_key|ec_point_mul|pkey_lifecycle|ssl_lifecycle|ex_data)|guest_libressl3_(ec_key_asn1_flag|ec_group|ec_parameters|pkey_getters|ecdsa|ec_public_key|key_reference|key_der|pem_keys|pkey_context))$' --output-on-failure -j4
python3 core/libs/tests/test_public_headers.py --compiler g++
python3 tools/check_conventions.py --base origin/main
python3 tools/debt_fold.py --check
git diff 648ba19b3edec93a073217bda7087dedcfd85454 HEAD --check

The focused targets build. Public headers, conventions, debt and whitespace checks pass. The upstream native import-binding harness passes against six freshly built and patched modules. This merge removes adjacent obsolete DTLS/private-key stubs. The existing private-key code, DER corrections, tests and debt remain unchanged. Main's method and lifecycle implementations remain intact. The fresh configure first misses the nested mbedTLS framework checkout; restoring its pinned submodule fixes setup. All scoped build and runtime checks then pass.

No full product build, full ctest, ASan, new oracle comparison, Windows, macOS, console, game or GPU run. Earlier Windows evidence checks an older head.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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; feat(libSceLibreSsl): implement EC points, key generation, ECDSA and ECDH #15827 merged as 27555b3f7c169e1053fa022244963450a5619658
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

MatteoSid and others added 4 commits October 25, 2026 19:51
EC_GROUP_new_by_curve_name, EC_GROUP_free, EC_GROUP_get_degree, EC_KEY_new,
EC_KEY_new_by_curve_name, EC_KEY_free, EC_KEY_set_group, EC_KEY_get0_group
and ECDSA_size follow LibreSSL 2.5.4 on top of mbedtls's named curves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	core/libs/prx/libSceLibreSsl/CMakeLists.txt
#	core/libs/prx/libSceLibreSsl/include/Api.hpp
#	docs/dev/debt/libSceLibreSsl.md
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Progress report (bea1b1d → 596aeb8)

➖ System libraries: 42.48% (+0.0%, +2 implemented, -2 reverted functions)

✅ 2 implemented
Library Name
libSceLibreSsl EC_KEY_get0_private_key_nid_postfix
libSceLibreSsl EC_KEY_set_private_key_nid_postfix
⚠️ 2 went back to stubs
Library Name
libSceLibreSsl ECDSA_sign_nid_postfix
libSceLibreSsl EC_KEY_check_key_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libSceLibreSsl 309/473 (65.33%) 309/473 (65.33%) +0.00%

@MatteoSid

Copy link
Copy Markdown
Contributor

@ahmadmuj thanks for the heads-up and for making the clash fail at compile time. Proposed order, per the overlap rule in CONTRIBUTING: #10407 goes in right after #10395. It was opened on 2026-10-23, was closed only by the open-PR limit and is already under review. It keeps EcKey::privateKey as EcPrivateKey (an mbedtls_mpi), since none of its exports hands the private key to the guest. This PR then rebases on it and swaps that member for the guest BIGNUM, as you describe. EC_KEY_generate_key writes into the existing BIGNUM, and EC_KEY_check_key, ECDSA_sign and ECDH_compute_key read it into an mbedtls_mpi at use; EC_KEY_set_group only checks that the key is on the same curve. Those are the only places in #10407 that touch the private key. Could you add #10407 to Depends on here? The current stack on #10395 is MatteoSid:feat/libressl-ec-points-ecdsa-ecdh-main.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 714a0abb61c173ec9d3ca4ddeb1099b6ad8ad40e. The private-key copy and clear/free behavior is consistent with the cited LibreSSL 2.5.4 paths; local conventions and diff checks pass, and the PR reports focused EC/RSA test coverage plus a LibreSSL differential.

Before this stack is ready to merge, please resolve the shared EcKey::privateKey integration order called out in MatteoSid's review comment. #10407 adds the EC point/key operations using that same member with an mbedtls_mpi representation, while this PR replaces it with the guest BIGNUM representation. The current checklist only declares #10395 and #10915, and this branch does not contain or rebase on #10407. Please add the dependency and update/rebase the stack in the agreed order after #10395, or coordinate an alternative order with the other author and document it. #10407 is currently closed by the intake limit; I have not reopened it or used its code.

I am requesting changes for this unresolved integration dependency. The current upstream eligibility, conventions, intake, labels and report checks pass; the product build/test jobs are skipped by repository policy. I did not independently run the reported full build because this workspace's AnyPS5 submodules are uninitialized.

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 27502 EC_KEY_get0_private_key
EC_KEY_set_private_key

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.

Pull request Shared files not compared
PR 13266 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 13546 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 13547 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 13877 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 15278 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 15280 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 16529 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 17844 core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 18460 core/libs/prx/libSceLibreSsl/CMakeLists.txt
PR 21701 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 23336 core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 23573 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 27430 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 27502 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
core/libs/prx/libSceLibreSsl/src/Ec.cpp
PR 27504 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 27517 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 27518 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp
PR 27521 core/libs/prx/libSceLibreSsl3/src/KeyDer.cpp
PR 27526 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/Export.cpp
core/libs/prx/libSceLibreSsl/include/Api.hpp

@BunnyFl

BunnyFl commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@ahmadmuj firmware 13.42 data for the two new functions. The decrypted libSceLibreSsl.sprx exports EC_KEY_get0_private_key (0x30b00) and EC_KEY_set_private_key (0x30b10). I ran them natively on a PC (not a console) against Ec.cpp from 714a0abb61 compiled unchanged, with the same case list on both sides. The PR side uses mbedtls from current main's build and its MbedConfig.hpp.

  • 15 EC_KEY_set_private_key calls, each followed by EC_KEY_get0_private_key, on a key without a group and on an EC_KEY_new_by_curve_name(415) key. Values: 1, 8, 9, 32 and 66 bytes, zero, a 4-word value in a 9-word buffer, a negative value, source flags 0xd and 0x3, a null private key, then a value again. On the P-256 key: 0, 2^256−1 and a 66-byte value.
  • What the module does, matched by the PR line for line:
    • It stores a copy: a new pointer and its own words, equal to the source's, with top copied, dmax = top (also for the 9-word buffer) and neg copied.
    • flags is always 0x1, even for source flags 0xd and 0x3.
    • Whenever a previous key is stored, setting frees it.
    • A null private key returns 0, frees the previous key and leaves get0 returning NULL.
    • Neither key rejects a value: the key without a group and the P-256 key take every non-null value above, including 2^256−1 and 66 bytes, and return 1.
  • Three identical runs on each side.
  • Controls: copies of Ec.cpp with one change each are caught. These are copying the source flags, allocating and keeping the source's dmax, not copying neg, keeping the previous key when the new one is null, and rejecting values over 8 words.
  • Per the disassembly, EC_KEY_set_private_key calls the code at 0x218e0 on the stored key before calling 0x21c40 on the new one. This matches the free-then-copy order the debt entry gives for throwing on the key's own private key.
  • Not run: the PR's throw cases (the key's own private key or words, a negative word count, null words), allocation failure and the 8388607-word limit.

@ycbuzi

ycbuzi commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

A Windows run of this head, since the description doesn't cover Windows: Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release. 714a0abb61 merged onto main 97d33464c5 with no conflicts.

  • The test executables build without errors. There are no warnings.
  • guest_libressl_ec, guest_libressl_ec_private_key, guest_libressl_rsa pass both runs of ctest --repeat until-fail:2.
  • No negative control: the functions live in source files this PR adds.

AI-assisted: yes (Claude Code)

@ahmadmuj

Copy link
Copy Markdown
Contributor Author

@MatteoSid @MarcZX10 thanks. I tried MatteoSid's order first: merged feat/libressl-ec-points-ecdsa-ecdh-main (0f6d851) onto this branch. The conflicts are mechanical (CMakeLists, Export.cpp, Api.hpp, Ec.hpp, Ec.cpp, debt file), but the swap is more than a type change.

Once EC_KEY_set_private_key exists, ECDSA_sign, ECDH_compute_key and EC_KEY_check_key get keys that mbedtls rejects and 2.5.4 accepts. A probe against 2.5.4 libcrypto.a on P-256:

  • key -7, pub -7G: check_key 1, sign then verify 1
  • key n+7, pub 7G: check_key 0, but its signature verifies against 7G, and ECDH equals ECDH with 7
  • key 0: check_key 0, ECDH -1; key n: ECDH -1
  • key 7 with zero high words (top 5): check_key 0
  • EC_KEY_generate_key reuses the stored BIGNUM: same pointer and words buffer across calls, dmax 4 on P-256, 9 on P-521, 7 on brainpoolP384r1
  • EC_KEY_set_group to another curve keeps the private key and returns 1

So the swap adds new behaviour to five of #10407's functions (set_group, generate_key, check_key, ECDSA_sign, ECDH_compute_key) and needs tests for it. I'd rather not write that inside this PR, since those are your functions and tests.

Counter-proposal: #10395 + #10915, then this PR, then your stack. Then this PR doesn't depend on a closed PR. You mentioned #10407 can't be reopened after the force push, so the stack comes back as a new PR anyway. When it rebases, EcPrivateKey goes away and those five functions use privateKey. I can share the probe source and the full value list.

I've put the proposed order in the description. MarcZX10, I'll re-request your review once we agree.

@MatteoSid

Copy link
Copy Markdown
Contributor

@ahmadmuj thanks for trying the merge and for the 2.5.4 probe; those cases are useful. One correction first: #10407 can be reopened. I left its branch untouched so that GitHub allows it, and pushed the rebased stack to a separate branch instead. It waits only for #10395.

On the order, I would still put #10407 right after #10395. It is ready and reviewed, and it needs neither #10915 nor a BIGNUM, since none of its exports takes a private key from the guest. Out-of-range keys only become reachable through EC_KEY_set_private_key, so here is a split that keeps this PR small:

  • This PR swaps EcPrivateKey for the guest BIGNUM. EC_KEY_generate_key writes into the stored BIGNUM, and EC_KEY_check_key, ECDSA_sign and ECDH_compute_key read it into an mbedtls_mpi. EC_KEY_set_group stops checking the private key's curve, as in 2.5.4.
  • A stored key that mbedtls cannot take as is (outside [1, n-1], negative, or with zero high words) throws NotImplemented_nid_no_patch in those three functions, with a debt entry and no test pinning it.
  • I then add the exact 2.5.4 behaviour for those keys in a follow-up, with tests built from your probe. Could you share the probe source and the full value list?

If that works for you, please put #10407 in Depends on here.

MatteoSid and others added 2 commits October 26, 2026 13:05
EC_GROUP_new_by_curve_name, EC_GROUP_free, EC_GROUP_get_degree, EC_KEY_new,
EC_KEY_new_by_curve_name, EC_KEY_free, EC_KEY_set_group, EC_KEY_get0_group
and ECDSA_size follow LibreSSL 2.5.4 on top of mbedtls's named curves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ECDH

EC_POINT_new, EC_POINT_free, EC_POINT_point2oct, EC_POINT_oct2point,
EC_KEY_generate_key, EC_KEY_check_key, EC_KEY_get0_public_key,
EC_KEY_set_public_key, ECDSA_sign, ECDSA_verify and ECDH_compute_key
follow LibreSSL 2.5.4 on top of mbedtls's named curves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
ahmadmuj and others added 10 commits October 26, 2026 17:52
EC_GROUP_new_by_curve_name, EC_GROUP_free, EC_GROUP_get_degree, EC_KEY_new,
EC_KEY_new_by_curve_name, EC_KEY_free, EC_KEY_set_group, EC_KEY_get0_group
and ECDSA_size follow LibreSSL 2.5.4 on top of mbedtls's named curves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ECDH

EC_POINT_new, EC_POINT_free, EC_POINT_point2oct, EC_POINT_oct2point,
EC_KEY_generate_key, EC_KEY_check_key, EC_KEY_get0_public_key,
EC_KEY_set_public_key, ECDSA_sign, ECDSA_verify and ECDH_compute_key
follow LibreSSL 2.5.4 on top of mbedtls's named curves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ahmadmuj

Copy link
Copy Markdown
Contributor Author

Checked this PR at 596aeb860cc9fb1df689c6be571d9ebf2817cbe5 and #25114 at 4c70af631621aed6fd3548e1f27a169977d58151 against main 9e77521f28d389f5b7558848b439f083c4b83f2c. Both change EC_KEY_generate_key, EC_KEY_check_key, ECDSA_sign and ECDH_compute_key. This PR moves private-key storage to guest BIGNUMs; #25114 adds error queues while retaining EcPrivateKey storage. Its overlap note describes an older version of this PR.

I suggest landing this PR first, then adapting #25114 to the new storage while preserving its error queues. I am holding the alignment push until that order is agreed. No new test result is claimed for the pending merge.

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built the focused checks against the existing Linux runtime. The GuestLibreSslEcKeys and GuestLibreSslEcPrivateKey fixtures passed. The separate LibreSSL3 fixtures did not build with this standalone setup.

AI-assisted: yes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs needs rebase Conflicts with main, rebase needed prx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants