feat(libSceLibreSsl): implement EC points, key generation, ECDSA and ECDH #15827

Merged
MatteoSid merged 1 commit from upstream-pr/15827 into main 2026-11-08 12:37:51 +00:00
MatteoSid commented 2026-11-04 18:07:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MatteoSid on 2026-11-04T18:07:10Z.


What

Implements eleven libSceLibreSsl exports that were throwing NotImplemented: 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.

Replaces #10407, which the open PR limit closed while #10395 was open; GitHub no longer reopens it. MarcZX10's review there asked for this stack to be refreshed onto current main after #10395 and its LibreSSL tests rerun on the composed head; this is that head, with only this commit left on top of main.

  • Signatures and results follow LibreSSL 2.5.4 at 2e2c612: ec/ec_lib.c, ec/ec_key.c, ec/ec_oct.c, ec/ecp_oct.c, ecdsa/ecs_sign.c, ecdsa/ecs_vrf.c, ecdsa/ecs_ossl.c and ecdh/ech_key.c.
  • EC_POINT is a host object holding an mbedtls_ecp_point and its curve; an EC_KEY now also holds its public point and private scalar. Api.hpp declares EcPoint as opaque next to EcGroup and EcKey, with the exports' signatures; the layouts stay in the internal include/Ec.hpp. The three exports that take or return a BIGNUM (EC_POINT_mul, EC_KEY_get0_private_key, EC_KEY_set_private_key) still throw: #9446's BIGNUM landed after this branch was written, so they are left for a follow-up.
  • EC_POINT_point2oct and EC_POINT_oct2point handle the compressed, uncompressed and hybrid forms and the single zero octet for infinity, with LibreSSL's checks: form and y bit, exact length, coordinates below the prime, hybrid parity and the X9.62 on-curve test. A point that fails only the on-curve test keeps the decoded coordinates, as in LibreSSL, so EC_KEY_check_key can reject it later.
  • EC_KEY_generate_key draws the scalar in [1, n-1] from host entropy and reuses the key's existing point object, as LibreSSL does. EC_KEY_check_key checks infinity, the curve equation and, with a private key, that it gives the public key; the order test always passes because all 11 curves have cofactor 1.
  • ECDSA_sign signs through mbedtls with a random nonce and writes LibreSSL's DER (unsigned INTEGERs, 0x00 pad when the top bit is set); a null buffer only returns the length. ECDSA_verify returns -1 unless the signature is canonical DER with nothing after it (what LibreSSL's decode and re-encode comparison accepts), 0 for r or s outside [1, n-1] or a mismatch, and -1 when u1·G + u2·Q is at infinity. Digests longer than the order are truncated to its bit length, like ecdsa_do_sign and ecdsa_do_verify.
  • ECDH_compute_key returns the x coordinate padded to the field size: -1 without a private key, group or peer key, for a peer at infinity, for a length above INT_MAX or a buffer shorter than the secret without a KDF; a larger buffer is zero-filled. A KDF is called through the guest ABI and its output length is returned.
  • Ec.cpp starts with the copyright notices, licence conditions and disclaimers of LibreSSL 2.5.4's ec/ecp_oct.c, ec/ec_key.c, ecdsa/ecs_ossl.c and ecdh/ech_key.c, verbatim from 2e2c612, because EC_POINT_point2oct, EC_POINT_oct2point, EC_KEY_check_key, ECDSA_verify and ECDH_compute_key follow their control flow.

Gaps, in the debt file: no error is queued; a point used with a group of another curve throws, since LibreSSL's result depends on the EC methods its build picks; so does EC_KEY_set_group to another curve on a key that already has a public or private key; compressed points on secp224k1 and secp224r1 throw (mbedtls only decompresses for primes that are 3 mod 4); ECDSA_verify with a public key at infinity or off the curve and ECDH_compute_key with a peer key off the curve throw, where LibreSSL computes with them. The EC debt entry is split in two to stay under 1000 characters. Export.cpp goes from 121 to 110 NotImplemented_nid_no_patch stubs; since #11553 the debt file no longer states that count.

Tested

Linux (Ubuntu 24.04 container, GCC 13.3), full configure with -DBUILD_TESTING=ON, Release: built libSceLibreSsl, libSceLibreSsl3, their patched .prx files and the eighty-eight LibreSSL tests; ctest -R 'libressl|crypto_memcmp|public_headers' passes 153 of 153 (the eighty-eight LibreSSL tests, public_headers and its 64 compile checks) on head 6e04d09675, based on main 06a9092274; the new guest_libressl_ec_keys takes 0.6 s. Built with AddressSanitizer and leak detection, guest_libressl_ec_keys, guest_libressl_ec and guest_libressl3_key_reference are clean. It checks:

  • the P-256 generator in all three forms, its decompression with both parities (the even root is checked against the negated generator), hybrid parity, 11 malformed encodings that leave the point unchanged, a point off the curve that keeps its coordinates, and infinity;
  • on all 11 curves: key generation, EC_KEY_check_key, encoded sizes, sign and verify (another key, a changed signature, another digest, an empty digest), ECDH agreement on both sides and regeneration keeping the point object;
  • the RFC 6979 A.2.5 P-256/SHA-256 "sample" signature, checked independently with OpenSSL through cryptography 46, and a secp224k1 signature from a Python reference implementation whose 225-bit order makes the truncation drop seven bits; ten non-DER signatures return -1 and five out-of-range ones return 0;
  • ECDH with the generator as peer returns the x coordinate of the own public key, plus padding, short buffers, KDF output, failing KDF and a peer at infinity;
  • EC_KEY_set_public_key copies, own key, null, infinity and off-curve points, and EC_KEY_set_group with and without keys; null arguments throw. Unimplemented paths that throw (other curves, 224-bit compressed points, verify and ECDH with an off-curve or infinite key) are in the debt file and not tested.

Windows: JJenkx ran guest_libressl_ec_keys on head 6e04d09675 merged onto main (Windows 11, WinLibs GCC 15.2 UCRT, Release); it passes in 3 of 3 runs (comment).

The test fails when the truncation shift, the hybrid y bit, the DER minimal-integer check or the ECDH zero fill is removed. nm -D on the patched libSceLibreSsl.prx lists the eleven NIDs. tools/check_conventions.py --base origin/main passes; The overlap comment on 6e04d09675 lists no open PR with the same exports. #12729 and #12730, stacked on it, remove neighbouring stubs in Export.cpp and Api.hpp, #16079 edits the EVP debt entry next to the EC entries, and #16719 adds an EVP_DigestVerifyFinal entry at the same place. I proposed this PR first and offered to rebase after theirs instead (comment). For #16079 the order is agreed: this PR first, with no Depends on on either side (reply). #12729, #12730 and #16719 have not answered yet. #14212 adds a blank line between debt entries across the repository; if it lands first, the rebase separates the two EC entries. #11146 depends on this PR and #11466 on #11146. 40 PRs that share these files conflict with main, so they could not be compared. No title tested.

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:5159184079 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15827). Originally posted by [MatteoSid](https://github.com/MatteoSid) on 2026-11-04T18:07:10Z. --- ### What Implements eleven libSceLibreSsl exports that were throwing `NotImplemented`: `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`. Replaces #10407, which the open PR limit closed while #10395 was open; GitHub no longer reopens it. MarcZX10's review there asked for this stack to be refreshed onto current `main` after #10395 and its LibreSSL tests rerun on the composed head; this is that head, with only this commit left on top of `main`. - Signatures and results follow LibreSSL 2.5.4 at [2e2c612](https://github.com/libressl/openbsd/tree/2e2c612066aa993717528a0249c15c51128d61cb/src/lib/libcrypto): `ec/ec_lib.c`, `ec/ec_key.c`, `ec/ec_oct.c`, `ec/ecp_oct.c`, `ecdsa/ecs_sign.c`, `ecdsa/ecs_vrf.c`, `ecdsa/ecs_ossl.c` and `ecdh/ech_key.c`. - `EC_POINT` is a host object holding an `mbedtls_ecp_point` and its curve; an `EC_KEY` now also holds its public point and private scalar. `Api.hpp` declares `EcPoint` as opaque next to `EcGroup` and `EcKey`, with the exports' signatures; the layouts stay in the internal `include/Ec.hpp`. The three exports that take or return a `BIGNUM` (`EC_POINT_mul`, `EC_KEY_get0_private_key`, `EC_KEY_set_private_key`) still throw: #9446's `BIGNUM` landed after this branch was written, so they are left for a follow-up. - `EC_POINT_point2oct` and `EC_POINT_oct2point` handle the compressed, uncompressed and hybrid forms and the single zero octet for infinity, with LibreSSL's checks: form and y bit, exact length, coordinates below the prime, hybrid parity and the X9.62 on-curve test. A point that fails only the on-curve test keeps the decoded coordinates, as in LibreSSL, so `EC_KEY_check_key` can reject it later. - `EC_KEY_generate_key` draws the scalar in [1, n-1] from host entropy and reuses the key's existing point object, as LibreSSL does. `EC_KEY_check_key` checks infinity, the curve equation and, with a private key, that it gives the public key; the order test always passes because all 11 curves have cofactor 1. - `ECDSA_sign` signs through mbedtls with a random nonce and writes LibreSSL's DER (unsigned INTEGERs, 0x00 pad when the top bit is set); a null buffer only returns the length. `ECDSA_verify` returns -1 unless the signature is canonical DER with nothing after it (what LibreSSL's decode and re-encode comparison accepts), 0 for r or s outside [1, n-1] or a mismatch, and -1 when u1·G + u2·Q is at infinity. Digests longer than the order are truncated to its bit length, like `ecdsa_do_sign` and `ecdsa_do_verify`. - `ECDH_compute_key` returns the x coordinate padded to the field size: -1 without a private key, group or peer key, for a peer at infinity, for a length above `INT_MAX` or a buffer shorter than the secret without a KDF; a larger buffer is zero-filled. A KDF is called through the guest ABI and its output length is returned. - `Ec.cpp` starts with the copyright notices, licence conditions and disclaimers of LibreSSL 2.5.4's `ec/ecp_oct.c`, `ec/ec_key.c`, `ecdsa/ecs_ossl.c` and `ecdh/ech_key.c`, verbatim from 2e2c612, because `EC_POINT_point2oct`, `EC_POINT_oct2point`, `EC_KEY_check_key`, `ECDSA_verify` and `ECDH_compute_key` follow their control flow. Gaps, in the debt file: no error is queued; a point used with a group of another curve throws, since LibreSSL's result depends on the EC methods its build picks; so does `EC_KEY_set_group` to another curve on a key that already has a public or private key; compressed points on secp224k1 and secp224r1 throw (mbedtls only decompresses for primes that are 3 mod 4); `ECDSA_verify` with a public key at infinity or off the curve and `ECDH_compute_key` with a peer key off the curve throw, where LibreSSL computes with them. The EC debt entry is split in two to stay under 1000 characters. `Export.cpp` goes from 121 to 110 `NotImplemented_nid_no_patch` stubs; since #11553 the debt file no longer states that count. ### Tested Linux (Ubuntu 24.04 container, GCC 13.3), full configure with `-DBUILD_TESTING=ON`, Release: built `libSceLibreSsl`, `libSceLibreSsl3`, their patched `.prx` files and the eighty-eight LibreSSL tests; `ctest -R 'libressl|crypto_memcmp|public_headers'` passes 153 of 153 (the eighty-eight LibreSSL tests, `public_headers` and its 64 compile checks) on head `6e04d09675`, based on `main` `06a9092274`; the new `guest_libressl_ec_keys` takes 0.6 s. Built with AddressSanitizer and leak detection, `guest_libressl_ec_keys`, `guest_libressl_ec` and `guest_libressl3_key_reference` are clean. It checks: - the P-256 generator in all three forms, its decompression with both parities (the even root is checked against the negated generator), hybrid parity, 11 malformed encodings that leave the point unchanged, a point off the curve that keeps its coordinates, and infinity; - on all 11 curves: key generation, `EC_KEY_check_key`, encoded sizes, sign and verify (another key, a changed signature, another digest, an empty digest), ECDH agreement on both sides and regeneration keeping the point object; - the RFC 6979 A.2.5 P-256/SHA-256 "sample" signature, checked independently with OpenSSL through `cryptography` 46, and a secp224k1 signature from a Python reference implementation whose 225-bit order makes the truncation drop seven bits; ten non-DER signatures return -1 and five out-of-range ones return 0; - ECDH with the generator as peer returns the x coordinate of the own public key, plus padding, short buffers, KDF output, failing KDF and a peer at infinity; - `EC_KEY_set_public_key` copies, own key, null, infinity and off-curve points, and `EC_KEY_set_group` with and without keys; null arguments throw. Unimplemented paths that throw (other curves, 224-bit compressed points, verify and ECDH with an off-curve or infinite key) are in the debt file and not tested. Windows: JJenkx ran `guest_libressl_ec_keys` on head `6e04d09675` merged onto `main` (Windows 11, WinLibs GCC 15.2 UCRT, Release); it passes in 3 of 3 runs ([comment](https://github.com/boykopovar/AnyPS5/pull/15827#issuecomment-6719617012)). The test fails when the truncation shift, the hybrid y bit, the DER minimal-integer check or the ECDH zero fill is removed. `nm -D` on the patched `libSceLibreSsl.prx` lists the eleven NIDs. `tools/check_conventions.py --base origin/main` passes; The [overlap comment](https://github.com/boykopovar/AnyPS5/pull/15827#issuecomment-6700915072) on `6e04d09675` lists no open PR with the same exports. #12729 and #12730, stacked on it, remove neighbouring stubs in `Export.cpp` and `Api.hpp`, #16079 edits the EVP debt entry next to the EC entries, and #16719 adds an `EVP_DigestVerifyFinal` entry at the same place. I proposed this PR first and offered to rebase after theirs instead ([comment](https://github.com/boykopovar/AnyPS5/pull/15827#issuecomment-6703512796)). For #16079 the order is agreed: this PR first, with no `Depends on` on either side ([reply](https://github.com/boykopovar/AnyPS5/pull/15827#issuecomment-6711003415)). #12729, #12730 and #16719 have not answered yet. #14212 adds a blank line between debt entries across the repository; if it lands first, the rebase separates the two EC entries. #11146 depends on this PR and #11466 on #11146. 40 PRs that share these files conflict with `main`, so they could not be compared. No title tested. ### 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)
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!15827
No description provided.