feat(libSceLibreSsl3): implement o2i_ECPublicKey #18946

Merged
MatteoSid merged 2 commits from upstream-pr/18946 into main 2026-11-12 20:10:01 +00:00
MatteoSid commented 2026-11-12 15:16:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MatteoSid on 2026-11-12T15:16:50Z.


What

Implements o2i_ECPublicKey in libSceLibreSsl3 (NID OpC9gfPLl5g), following LibreSSL 4.0.0's o2i_ECPublicKey:

  • a null key pointer, a null key or a key without a group queues EC ERR_R_PASSED_NULL_PARAMETER and returns null;
  • a key without a public point gets one from EC_POINT_new; if that fails it queues ERR_R_MALLOC_FAILURE and returns null;
  • the octets are decoded with EC_POINT_oct2point. On failure it queues ERR_R_EC_LIB and returns null, leaving *in and the conversion form unchanged, and the newly created point stays on the key, as upstream. An off-curve uncompressed or hybrid point still overwrites the key's existing point, as LibreSSL's EC_POINT_set_affine_coordinates stores the coordinates before its on-curve check; every earlier rejection leaves it unchanged;
  • on success the conversion form becomes the first octet without its low bit (4, 2, 6, or 0 for the point at infinity), *in advances by len and the key is returned. An existing public point is reused, not replaced.

The key, group and point are libSceLibreSsl's (EC_KEY_new_by_curve_name, EC_POINT_new, EC_POINT_oct2point in libSceLibreSsl/src/Ec.cpp), which share the 64-byte LibreSSL EC_KEY layout already used by EC_KEY_set_asn1_flag and EC_KEY_up_ref. A null in throws invalid_argument instead of faulting. src/EcAsn1.cpp follows the control flow of ec_asn1.c, so it carries that file's notice.

Gaps, in a new debt entry: a failed decode queues only ERR_R_EC_LIB, because libSceLibreSsl's EC_POINT_oct2point queues no reason of its own where LibreSSL first queues the decoder's (such as EC_R_INVALID_ENCODING); a compressed point on a curve whose prime is not 3 mod 4 (secp224r1, secp224k1) throws in that function; nothing is checked on the console. Export.cpp goes from 36 to 35 NotImplemented_nid_no_patch stubs.

d2i_ECParameters is not included: it needs a DER decoder for ECPKParameters with the ASN.1 error queue, which is larger than this change.

Tested

Linux (Ubuntu 24.04 container, GCC 13.3), configure with -DBUILD_TESTING=ON, Release, on head d4a8574a32 based on main 5e7ec34f68: built every LibreSSL test target and the patched .prx files. ctest -R 'libressl|libre_ssl|crypto_memcmp|public_headers' passes 212 of 212.

Windows: JJenkx ran guest_libressl3_ec_public_key on head d4a8574a32 merged onto main (Windows 11, WinLibs GCC 15.2 UCRT, Release); it passes in 3 of 3 runs, none skipped (comment).

The new guest_libressl3_ec_public_key uses the public keys of RFC 6979 A.2.5 (P-256) and A.2.6 (P-384), cross-checked with Python cryptography 46.0.2, and checks:

  • uncompressed, compressed and hybrid P-256 and uncompressed and compressed P-384 encodings: the key itself is returned, *in advances by exactly len inside a longer buffer, the conversion form is 4, 2 or 6, the point re-encodes to the RFC's uncompressed point, EC_KEY_check_key returns 1 and the error queue stays empty;
  • a key that already has a public point keeps the same point object; the one-byte point at infinity is accepted with conversion form 0;
  • a null key pointer, a null key and a key without a group each queue exactly 0x10fff043, create no point, leave the form and *in unchanged;
  • a bad form byte, a hybrid point with the wrong parity, an x above the field prime, a compressed x with no point on the curve, a short length, a zero length and a P-384 point on a P-256 key each queue exactly 0x10fff010, leave *in and the conversion form unchanged, and leave the key's existing point at the RFC point; an off-curve uncompressed or hybrid point does the same but leaves its own coordinates in that point, re-encoded byte for byte, and a point created for the attempt stays on the key;
  • a null in throws without creating a point.

Every one of these mutations makes the test fail: keeping the low bit in the conversion form, not advancing *in, storing the form before decoding, ERR_R_MALLOC_FAILURE instead of either queued reason, no error on a decode failure, no group check, no point created, a new point on every call, and decoding uncompressed points into a temporary in libSceLibreSsl's EC_POINT_oct2point. nm -D on the patched libSceLibreSsl3.prx lists OpC9gfPLl5g. tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch finds no overlap with the open PRs it could compare. The 25 that share files and conflict with main were checked by hand, and none changes o2i_ECPublicKey: none has a hunk next to its Export.cpp stub, Api.hpp declaration or the new CMakeLists.txt block (placed after the RSA setters block, away from the end of the file where several open PRs append), and only #14212, which adds a blank line between all debt entries, has the neighbouring debt entry as context. No title tested.

The second commit, d4a8574a32, follows MarcZX10's review: it adds the point checks after each rejection and states the overwrite in the debt entry (937 characters). The code is unchanged.

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; proposed to go before #19024, which inserts its CMakeLists.txt block at the same place, and rebased onto it if #19024 merges first (comment)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5280000328 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18946). Originally posted by [MatteoSid](https://github.com/MatteoSid) on 2026-11-12T15:16:50Z. --- ### What Implements `o2i_ECPublicKey` in libSceLibreSsl3 (NID `OpC9gfPLl5g`), following LibreSSL 4.0.0's [`o2i_ECPublicKey`](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/ec/ec_asn1.c#L1275-L1300): - a null key pointer, a null key or a key without a group queues `EC` `ERR_R_PASSED_NULL_PARAMETER` and returns null; - a key without a public point gets one from `EC_POINT_new`; if that fails it queues `ERR_R_MALLOC_FAILURE` and returns null; - the octets are decoded with `EC_POINT_oct2point`. On failure it queues `ERR_R_EC_LIB` and returns null, leaving `*in` and the conversion form unchanged, and the newly created point stays on the key, as upstream. An off-curve uncompressed or hybrid point still overwrites the key's existing point, as LibreSSL's [`EC_POINT_set_affine_coordinates`](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/ec/ec_lib.c#L882-L888) stores the coordinates before its on-curve check; every earlier rejection leaves it unchanged; - on success the conversion form becomes the first octet without its low bit (4, 2, 6, or 0 for the point at infinity), `*in` advances by `len` and the key is returned. An existing public point is reused, not replaced. The key, group and point are libSceLibreSsl's (`EC_KEY_new_by_curve_name`, `EC_POINT_new`, `EC_POINT_oct2point` in `libSceLibreSsl/src/Ec.cpp`), which share the 64-byte LibreSSL `EC_KEY` layout already used by `EC_KEY_set_asn1_flag` and `EC_KEY_up_ref`. A null `in` throws `invalid_argument` instead of faulting. `src/EcAsn1.cpp` follows the control flow of `ec_asn1.c`, so it carries that file's notice. Gaps, in a new debt entry: a failed decode queues only `ERR_R_EC_LIB`, because libSceLibreSsl's `EC_POINT_oct2point` queues no reason of its own where LibreSSL first queues the decoder's (such as `EC_R_INVALID_ENCODING`); a compressed point on a curve whose prime is not 3 mod 4 (secp224r1, secp224k1) throws in that function; nothing is checked on the console. `Export.cpp` goes from 36 to 35 `NotImplemented_nid_no_patch` stubs. `d2i_ECParameters` is not included: it needs a DER decoder for `ECPKParameters` with the ASN.1 error queue, which is larger than this change. ### Tested Linux (Ubuntu 24.04 container, GCC 13.3), configure with `-DBUILD_TESTING=ON`, Release, on head `d4a8574a32` based on `main` `5e7ec34f68`: built every LibreSSL test target and the patched `.prx` files. `ctest -R 'libressl|libre_ssl|crypto_memcmp|public_headers'` passes 212 of 212. Windows: JJenkx ran `guest_libressl3_ec_public_key` on head `d4a8574a32` merged onto `main` (Windows 11, WinLibs GCC 15.2 UCRT, Release); it passes in 3 of 3 runs, none skipped ([comment](https://github.com/boykopovar/AnyPS5/pull/18946#issuecomment-6831788082)). The new `guest_libressl3_ec_public_key` uses the public keys of RFC 6979 A.2.5 (P-256) and A.2.6 (P-384), cross-checked with Python `cryptography` 46.0.2, and checks: - uncompressed, compressed and hybrid P-256 and uncompressed and compressed P-384 encodings: the key itself is returned, `*in` advances by exactly `len` inside a longer buffer, the conversion form is 4, 2 or 6, the point re-encodes to the RFC's uncompressed point, `EC_KEY_check_key` returns 1 and the error queue stays empty; - a key that already has a public point keeps the same point object; the one-byte point at infinity is accepted with conversion form 0; - a null key pointer, a null key and a key without a group each queue exactly `0x10fff043`, create no point, leave the form and `*in` unchanged; - a bad form byte, a hybrid point with the wrong parity, an x above the field prime, a compressed x with no point on the curve, a short length, a zero length and a P-384 point on a P-256 key each queue exactly `0x10fff010`, leave `*in` and the conversion form unchanged, and leave the key's existing point at the RFC point; an off-curve uncompressed or hybrid point does the same but leaves its own coordinates in that point, re-encoded byte for byte, and a point created for the attempt stays on the key; - a null `in` throws without creating a point. Every one of these mutations makes the test fail: keeping the low bit in the conversion form, not advancing `*in`, storing the form before decoding, `ERR_R_MALLOC_FAILURE` instead of either queued reason, no error on a decode failure, no group check, no point created, a new point on every call, and decoding uncompressed points into a temporary in libSceLibreSsl's `EC_POINT_oct2point`. `nm -D` on the patched `libSceLibreSsl3.prx` lists `OpC9gfPLl5g`. `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` finds no overlap with the open PRs it could compare. The 25 that share files and conflict with `main` were checked by hand, and none changes `o2i_ECPublicKey`: none has a hunk next to its `Export.cpp` stub, `Api.hpp` declaration or the new `CMakeLists.txt` block (placed after the RSA setters block, away from the end of the file where several open PRs append), and only #14212, which adds a blank line between all debt entries, has the neighbouring debt entry as context. No title tested. The second commit, `d4a8574a32`, follows MarcZX10's review: it adds the point checks after each rejection and states the overwrite in the debt entry (937 characters). The code is unchanged. ### 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; proposed to go before #19024, which inserts its `CMakeLists.txt` block at the same place, and rebased onto it if #19024 merges first ([comment](https://github.com/boykopovar/AnyPS5/pull/18946#issuecomment-6832481625)) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-11-12 15:21:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-12T15:21:01Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this; the points below are optional.

Head reviewed: 30cbfaec6646d60cbc15829e62bbeac0f2612301 (PR #18946, base main).

What I ran

Read-only checks against the pinned head, with the repository reader (repo_read.py). No build, no tests.

repo_read.py search "EC_POINT_oct2point_nid_postfix" core/libs/prx/libSceLibreSsl
repo_read.py search "EcKey" core/libs/prx/libSceLibreSsl/include
repo_read.py cat core/libs/prx/libSceLibreSsl/src/Ec.cpp 540 600
repo_read.py cat core/libs/prx/libSceLibreSsl/include/Ec.hpp 40 70
repo_read.py search "APS5_INVALID_ARG_EX" core
repo_read.py search "LibreSslPutError_nid_no_patch" core/libs/prx/libSceLibreSsl/include
repo_read.py search "EC_POINT_new_nid_postfix" core/libs/prx/libSceLibreSsl3 core/libs/prx/libSceLibreSsl/include
repo_read.py search "o2i_ECPublicKey" core
repo_read.py search "EcKey" core/libs/prx/libSceLibreSsl3/include
repo_read.py search "libSceLibreSsl/include" core/libs/prx/libSceLibreSsl3
repo_read.py search "OpenBSD" core/libs/prx/libSceLibreSsl3/src

Results

Check Result Note
Diff of the six changed files passed Every changed hunk read
Callees, types and macros at head passed Signatures match their call sites
Build and ctest not run Author-reported only
RFC 6979 vectors not run Not checked
check_conventions.py, pr_overlap.py not run Author-reported only

Findings

Blocking

None.

Non-blocking

  • core/libs/prx/libSceLibreSsl3/src/EcAsn1.cpp:94 — when the key already has a public point, a failed uncompressed or hybrid decode leaves that point changed. EC_POINT_oct2point writes into point->point (core/libs/prx/libSceLibreSsl/src/Ec.cpp:584) and only then checks it is on the curve (Ec.cpp:585), so the rejected coordinates stay on the key. The compressed branch decodes into a temporary and copies only on success (Ec.cpp:573-576). The description says the existing point is "reused, not replaced" and does not say its value survives a failed call. core/libs/prx/libSceLibreSsl3/tests/EcPublicKey.cpp:147 checks only that the point is non-null after rejections, so the tests would not catch this. Either decode into a temporary and copy on success, or add a test that the point's bytes are unchanged after a rejected decode and name the behaviour in the debt entry.

Sources

Not verified

  • The build, the ctest run (212 of 212 on the stated head), and the check_conventions.py and pr_overlap.py runs are the author's report. I did not run them.
  • The mutation checks in the description were not run, so no negative control was observed.
  • The RFC 6979 public-key vectors and the Python cryptography cross-check were not checked.
  • The numeric constants in EcAsn1.cpp (16, 65, 67) were not checked against the LibreSSL headers.
  • The Export stub count (36 to 35) was not counted.
  • The checklist says the new debt entry sits under a "Silent stubs" heading. A search of docs/dev/debt/libSceLibreSsl3.md at head found no "Silent" text, so that placement is not verified.
  • Whether LibreSSL 4.0.0 also overwrites an existing point on a failed decode was not checked.
  • The "no comments in code" item: the OpenBSD notice at the top of EcAsn1.cpp matches the notice headers already in Aes.cpp, Key.cpp and Stack.cpp. The rule in CONTRIBUTING.md was not read.
  • Files read: the diff hunks of all six files in files.json under reviewed (CMakeLists.txt, Export.cpp, include/Api.hpp, src/EcAsn1.cpp, tests/EcPublicKey.cpp, docs/dev/debt/libSceLibreSsl3.md). Outside the hunks, only EcAsn1.cpp and the test file were read in full, since both are new files. The files/ folder was not read. Test helpers (EC_POINT_point2oct, ERR_clear_error) and the CMake link list were not compared with other targets.

To get this to a good state

Nothing is required. The non-blocking point above is optional.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5877002910 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18946#pullrequestreview-5877002910). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-12T15:21:01Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this; the points below are optional. Head reviewed: `30cbfaec6646d60cbc15829e62bbeac0f2612301` (PR #18946, base `main`). ## What I ran Read-only checks against the pinned head, with the repository reader (`repo_read.py`). No build, no tests. ``` repo_read.py search "EC_POINT_oct2point_nid_postfix" core/libs/prx/libSceLibreSsl repo_read.py search "EcKey" core/libs/prx/libSceLibreSsl/include repo_read.py cat core/libs/prx/libSceLibreSsl/src/Ec.cpp 540 600 repo_read.py cat core/libs/prx/libSceLibreSsl/include/Ec.hpp 40 70 repo_read.py search "APS5_INVALID_ARG_EX" core repo_read.py search "LibreSslPutError_nid_no_patch" core/libs/prx/libSceLibreSsl/include repo_read.py search "EC_POINT_new_nid_postfix" core/libs/prx/libSceLibreSsl3 core/libs/prx/libSceLibreSsl/include repo_read.py search "o2i_ECPublicKey" core repo_read.py search "EcKey" core/libs/prx/libSceLibreSsl3/include repo_read.py search "libSceLibreSsl/include" core/libs/prx/libSceLibreSsl3 repo_read.py search "OpenBSD" core/libs/prx/libSceLibreSsl3/src ``` ## Results | Check | Result | Note | |---|---|---| | Diff of the six changed files | passed | Every changed hunk read | | Callees, types and macros at head | passed | Signatures match their call sites | | Build and `ctest` | not run | Author-reported only | | RFC 6979 vectors | not run | Not checked | | `check_conventions.py`, `pr_overlap.py` | not run | Author-reported only | ## Findings **Blocking** None. **Non-blocking** - `core/libs/prx/libSceLibreSsl3/src/EcAsn1.cpp:94` — when the key already has a public point, a failed uncompressed or hybrid decode leaves that point changed. `EC_POINT_oct2point` writes into `point->point` (`core/libs/prx/libSceLibreSsl/src/Ec.cpp:584`) and only then checks it is on the curve (`Ec.cpp:585`), so the rejected coordinates stay on the key. The compressed branch decodes into a temporary and copies only on success (`Ec.cpp:573-576`). The description says the existing point is "reused, not replaced" and does not say its value survives a failed call. `core/libs/prx/libSceLibreSsl3/tests/EcPublicKey.cpp:147` checks only that the point is non-null after rejections, so the tests would not catch this. Either decode into a temporary and copy on success, or add a test that the point's bytes are unchanged after a rejected decode and name the behaviour in the debt entry. ## Sources - [`EC_POINT_oct2point_nid_postfix`](https://github.com/boykopovar/AnyPS5/blob/30cbfaec6646d60cbc15829e62bbeac0f2612301/core/libs/prx/libSceLibreSsl/src/Ec.cpp#L551-L586) - [`o2i_ECPublicKey_nid_postfix`](https://github.com/boykopovar/AnyPS5/blob/30cbfaec6646d60cbc15829e62bbeac0f2612301/core/libs/prx/libSceLibreSsl3/src/EcAsn1.cpp#L83-L103) ## Not verified - The build, the `ctest` run (212 of 212 on the stated head), and the `check_conventions.py` and `pr_overlap.py` runs are the author's report. I did not run them. - The mutation checks in the description were not run, so no negative control was observed. - The RFC 6979 public-key vectors and the Python `cryptography` cross-check were not checked. - The numeric constants in `EcAsn1.cpp` (16, 65, 67) were not checked against the LibreSSL headers. - The Export stub count (36 to 35) was not counted. - The checklist says the new debt entry sits under a "Silent stubs" heading. A search of `docs/dev/debt/libSceLibreSsl3.md` at head found no "Silent" text, so that placement is not verified. - Whether LibreSSL 4.0.0 also overwrites an existing point on a failed decode was not checked. - The "no comments in code" item: the OpenBSD notice at the top of `EcAsn1.cpp` matches the notice headers already in `Aes.cpp`, `Key.cpp` and `Stack.cpp`. The rule in `CONTRIBUTING.md` was not read. - Files read: the diff hunks of all six files in `files.json` under `reviewed` (`CMakeLists.txt`, `Export.cpp`, `include/Api.hpp`, `src/EcAsn1.cpp`, `tests/EcPublicKey.cpp`, `docs/dev/debt/libSceLibreSsl3.md`). Outside the hunks, only `EcAsn1.cpp` and the test file were read in full, since both are new files. The `files/` folder was not read. Test helpers (`EC_POINT_point2oct`, `ERR_clear_error`) and the CMake link list were not compared with other targets. ## To get this to a good state Nothing is required. The non-blocking point above is optional.
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!18946
No description provided.