feat(libSceLibreSsl3): derive ECDH secrets through EVP_PKEY_CTX #19487

Merged
MarrasFederico merged 6 commits from upstream-pr/19487 into main 2026-11-14 11:51:44 +00:00
MarrasFederico commented 2026-11-13 12:13:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarrasFederico on 2026-11-13T12:13:38Z.


What

ECDH key agreement through EVP_PKEY_CTX in libSceLibreSsl3: EVP_PKEY_derive_init, EVP_PKEY_derive_set_peer and EVP_PKEY_derive for EVP_PKEY_EC keys, plus EVP_PKEY_copy_parameters for EC keys. It builds on the EVP_PKEY_CTX from #17528 and reuses the parent library's ECDH (ECDH_compute_key, EC_KEY_* in libSceLibreSsl/src/Ec.cpp) instead of a second implementation.

  • Checks, in pmeth_fn.c order:
    • an unsupported method returns -2;
    • an operation that was not initialised returns -1;
    • in derive_set_peer, a missing own key, a different key type or a peer on another curve each return -1, with EVP_R_NO_KEY_SET, EVP_R_DIFFERENT_KEY_TYPES or EVP_R_DIFFERENT_PARAMETERS respectively;
    • the peer is reference-counted, and a previous peer is released.
  • pkey_ec_derive:
    • key == NULL reports (degree + 7) / 8;
    • missing keys queue EC_R_KEYS_NOT_SET;
    • a shorter output buffer gets a truncated secret, not an error.
    • The parent ECDH_compute_key rejects short buffers and zeroes the tail of long ones, so the wrapper computes into a scratch buffer and copies min(*keylen, secret length).
  • EVP_PKEY_copy_parameters: follows p_lib.c and ec_copy_parameters. Different types give EVP_R_DIFFERENT_KEY_TYPES, a source without a group gives EVP_R_MISSING_PARAMETERS, and otherwise the group is set with EC_KEY_set_group. Non-EC types still throw.
  • EVP_PKEY_free: now releases EC payloads through EC_KEY_free.
  • Not modelled: the KDF (X9_63) and cofactor ECDH controls. The debt entry states both.

Source: LibreSSL 4.0.0 evp/pmeth_fn.c, evp/p_lib.c, ec/ec_pmeth.c, ec/ec_ameth.c, at the same revision as the neighbouring entries. The ported files carry the upstream notices and a Derived from line.

Overlap: I checked the diffs of every open PR touching libSceLibreSsl3 or its debt file, and none implements these functions. The EC point and ECDSA PRs (#16918, #17621, #17622, #17626, #19033, #19044, #19134) touch different exports.

Tested

Ubuntu 26.04.1, GCC 15.2.0, Release, BUILD_TESTING=ON.

guest_libressl3_pkey_context gains ECDH cases:

  • the RFC 5903 P-256 vector, derived in both directions;
  • two freshly generated keys that agree on the same secret;
  • the length query with a null key;
  • a long buffer with sentinel bytes before and after the output, and a short buffer that truncates;
  • a null length pointer;
  • a peer on another curve, a derive after a failed derive_set_peer, a derive before derive_init, and an own key without a private part, each checked against the error queue;
  • copy_parameters with null arguments, a source without a group, a different key type, and a successful copy.

Results:

  • ctest -R libressl3: 69 of 69 pass at 3ede9bc1ae (after merging upstream/main d5663ee9a3).
  • Against the #17528 base the new cases stop at EVP_PKEY_derive_init not implemented.
  • Negative check: flipping the first byte of the derived secret makes the RFC 5903 comparison fail.
  • tools/check_conventions.py --base upstream/main: All conventions checks passed (0 errors, 0 warnings); python3 core/libs/tests/test_public_headers.py --audit-only: 413 libraries, 34548 exports verified; git diff --check upstream/main...HEAD: clean.

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none (#17528, which this branch built on, is merged; the branch was brought up to date with main by a merge)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5295268681 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19487). Originally posted by [MarrasFederico](https://github.com/MarrasFederico) on 2026-11-13T12:13:38Z. --- ### What ECDH key agreement through `EVP_PKEY_CTX` in `libSceLibreSsl3`: `EVP_PKEY_derive_init`, `EVP_PKEY_derive_set_peer` and `EVP_PKEY_derive` for `EVP_PKEY_EC` keys, plus `EVP_PKEY_copy_parameters` for EC keys. It builds on the `EVP_PKEY_CTX` from #17528 and reuses the parent library's ECDH (`ECDH_compute_key`, `EC_KEY_*` in `libSceLibreSsl/src/Ec.cpp`) instead of a second implementation. - **Checks, in `pmeth_fn.c` order:** - an unsupported method returns -2; - an operation that was not initialised returns -1; - in `derive_set_peer`, a missing own key, a different key type or a peer on another curve each return -1, with `EVP_R_NO_KEY_SET`, `EVP_R_DIFFERENT_KEY_TYPES` or `EVP_R_DIFFERENT_PARAMETERS` respectively; - the peer is reference-counted, and a previous peer is released. - **`pkey_ec_derive`:** - `key == NULL` reports `(degree + 7) / 8`; - missing keys queue `EC_R_KEYS_NOT_SET`; - a shorter output buffer gets a truncated secret, not an error. - The parent `ECDH_compute_key` rejects short buffers and zeroes the tail of long ones, so the wrapper computes into a scratch buffer and copies `min(*keylen, secret length)`. - **`EVP_PKEY_copy_parameters`:** follows `p_lib.c` and `ec_copy_parameters`. Different types give `EVP_R_DIFFERENT_KEY_TYPES`, a source without a group gives `EVP_R_MISSING_PARAMETERS`, and otherwise the group is set with `EC_KEY_set_group`. Non-EC types still throw. - **`EVP_PKEY_free`:** now releases EC payloads through `EC_KEY_free`. - **Not modelled:** the KDF (`X9_63`) and cofactor ECDH controls. The debt entry states both. Source: LibreSSL 4.0.0 [`evp/pmeth_fn.c`, `evp/p_lib.c`, `ec/ec_pmeth.c`, `ec/ec_ameth.c`](https://github.com/libressl/openbsd/tree/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto), at the same revision as the neighbouring entries. The ported files carry the upstream notices and a Derived from line. Overlap: I checked the diffs of every open PR touching `libSceLibreSsl3` or its debt file, and none implements these functions. The EC point and ECDSA PRs (#16918, #17621, #17622, #17626, #19033, #19044, #19134) touch different exports. ### Tested Ubuntu 26.04.1, GCC 15.2.0, Release, `BUILD_TESTING=ON`. `guest_libressl3_pkey_context` gains ECDH cases: - the RFC 5903 P-256 vector, derived in both directions; - two freshly generated keys that agree on the same secret; - the length query with a null key; - a long buffer with sentinel bytes before and after the output, and a short buffer that truncates; - a null length pointer; - a peer on another curve, a derive after a failed `derive_set_peer`, a derive before `derive_init`, and an own key without a private part, each checked against the error queue; - `copy_parameters` with null arguments, a source without a group, a different key type, and a successful copy. Results: - `ctest -R libressl3`: 69 of 69 pass at `3ede9bc1ae` (after merging `upstream/main` `d5663ee9a3`). - Against the #17528 base the new cases stop at `EVP_PKEY_derive_init not implemented`. - Negative check: flipping the first byte of the derived secret makes the RFC 5903 comparison fail. - `tools/check_conventions.py --base upstream/main`: `All conventions checks passed` (0 errors, 0 warnings); `python3 core/libs/tests/test_public_headers.py --audit-only`: 413 libraries, 34548 exports verified; `git diff --check upstream/main...HEAD`: clean. ### Checklist - [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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none (#17528, which this branch built on, is merged; the branch was brought up to date with main by a merge) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!19487
No description provided.