Skip to content

fix(libSceLibreSsl): queue the EC, ECDSA and ECDH errors of LibreSSL 2.5.4 - #26417

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
ToRaNek:fix/libressl-ec-error-queue
Nov 24, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
ToRaNek:fix/libressl-ec-error-queue

Conversation

@ToRaNek

@ToRaNek ToRaNek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What

Resubmission of #25114, which was closed when my fork left the fork network (I switched it to private by mistake) and cannot be reopened. Same head commit 4c70af63162; the reviews and the discussion are on #25114.

The EC, ECDSA and ECDH exports of libSceLibreSsl (src/Ec.cpp) returned their failure values with an empty error queue: the file did not queue a single error. LibreSSL 2.5.4 queues a reason for each of these failures, and the debt file said so ("queue no error code", "return null without queuing EC_R_UNKNOWN_GROUP"). A caller that reads ERR_get_error after a failed call got 0. This is the EC counterpart of #25035 (RSA).

No return value and no output byte changes; only the queue does. Libraries: EC 16, ECDSA 42, ECDH 43 (err.h); function 0xfff as elsewhere in this library.

Export Failure Queue Source (2.5.4, 2e2c6120)
EC_GROUP_new_by_curve_name, EC_KEY_new_by_curve_name positive NID that is not a curve (0 or negative: nothing) EC_R_UNKNOWN_GROUP ec_curve.c L3255-L3272, ec_key.c L94-L105
EC_POINT_point2oct form that is not 2, 4 or 6 EC_R_INVALID_FORM ecp_oct.c L210-L215
buffer too short, for the point at infinity and for any other point EC_R_BUFFER_TOO_SMALL ecp_oct.c L219-L221, L233-L235
EC_KEY_generate_key null key or key without a group EC ERR_R_PASSED_NULL_PARAMETER ec_key.c L219-L222
EC_KEY_set_public_key a point given to a key without a group EC ERR_R_PASSED_NULL_PARAMETER ec_key.c L442-L447 through EC_POINT_dup
EC_KEY_check_key null key, no group or no public key EC ERR_R_PASSED_NULL_PARAMETER ec_key.c L276-L279
public key at infinity / off the curve / not the private key times the generator EC_R_POINT_AT_INFINITY / EC_R_POINT_IS_NOT_ON_CURVE / EC_R_INVALID_PRIVATE_KEY L280-L283, L290-L293, L322-L326
ECDSA_sign no group or no private key ECDSA ERR_R_PASSED_NULL_PARAMETER ecs_ossl.c L220-L223
ECDSA_verify no group or no public key ECDSA_R_MISSING_PARAMETERS ecs_ossl.c L323-L327
r or s outside [1, n-1] (returns 0) ECDSA_R_BAD_SIGNATURE L350-L356
u1·G + u2·Q is the point at infinity (returns -1) EC_R_POINT_AT_INFINITY, then ECDSA ERR_R_EC_LIB ecp_smpl.c L535-L538, ecs_ossl.c L399-L403
ECDH_compute_key output length above INT_MAX ECDH ERR_R_MALLOC_FAILURE ech_key.c L107-L111
no private key ECDH_R_NO_PRIVATE_VALUE L121-L125
no group EC ERR_R_PASSED_NULL_PARAMETER, then ECDH ERR_R_MALLOC_FAILURE L127-L131
peer point null or at infinity EC_R_POINT_AT_INFINITY, then ECDH_R_POINT_ARITHMETIC_FAILURE ecp_smpl.c L535-L538, ech_key.c L140-L144
output shorter than the field without a KDF / KDF returns null ECDH_R_KEY_TRUNCATION / ECDH_R_KDF_FAILED L162-L166, L179-L182

Errors already in the queue before the call stay in front of the new ones.

Left as it was (written in the debt fragment):

  • EC_POINT_new and EC_POINT_oct2point: fix(libSceLibreSsl): decode compressed 224-bit EC points and report errors #11466 works on the decoding errors of these two, on another implementation of the file, so this PR does not touch them;
  • a signature that does not decode in ECDSA_verify still returns -1 with an empty queue (the source queues ASN.1 errors);
  • allocation, BN and arithmetic failures still throw;
  • a peer point at infinity that belongs to another curve in ECDH_compute_key: -1 with an empty queue, because the source's answer depends on the curve methods of its build;
  • LibreSSL built without OPENSSL_BN_ASM_MONT clears the queue when EC_GROUP_new_by_curve_name loads a non-NIST prime curve (ec_cvt.c); whether the module is built that way is not known, and the port keeps the queue.

Effect on libSceLibreSsl3. That library forwards ten of these exports to this one, so its callers now see these errors too, with the 2.5.4 library numbers (42 and 43) where 4.0.0 queues everything under ERR_LIB_EC. Before this PR they saw an empty queue. The debt fragment records it.

Tested

Windows 11, MinGW GCC 15.2. Not tested on a console.

  • New test guest_libressl_ec_error_queue (new file). Each call checks the return value and the whole queue in order, and next to every failure a call that succeeds checks that the queue stays empty: curve names; point encoding in the three forms, at infinity and with buffers one byte short; key generation, EC_KEY_set_public_key and EC_KEY_check_key on keys missing each part, with a public key at infinity, off the curve and belonging to another private key; signing without a private key; verification with r and s at each bound; ECDH without a private key, without a group, with a null or infinite peer, one byte short, and with a KDF that fails. Several cases also run with an error queued beforehand.
  • The "point at infinity" branch of ECDSA_verify needs a signature built for it: with the RFC 6979 P-256 test key, r = -m·d⁻¹ mod n makes u1·G + u2·Q vanish for any s. A script computes r and checks the sum for three values of s; the test uses those three.
  • With Ec.cpp from main the new test fails at its first expected error; with the change it passes.
  • The other EC tests still pass: guest_libressl_ec, guest_libressl_ec_keys, guest_libressl_ec_point_mul, and on the libSceLibreSsl3 side, which forwards to this code, guest_libressl3_key_der, pem_keys, pkey_context, pkey_context_new_id, ecdsa, ecdsa_sig_lifetime, ec_group, ec_key_asn1_flag, ec_parameters and ec_public_key.
  • Negative controls: 43 mutations of Ec.cpp, each rebuilt and run on its own, all make the test fail: each queued error removed, replaced by a neighbouring reason or queued under another library, the order of the two-error pairs swapped, and the three guards (positive NID, non-null point, same curve) removed.
  • python tools/check_conventions.py --base origin/main, python tools/debt_fold.py --check and python core/libs/tests/test_public_headers.py --audit-only pass.

Not verified. The expected queues come from reading the 2.5.4 source, not from running a LibreSSL 2.5.4 binary or a console.

Overlap (python tools/pr_overlap.py --branch fix/libressl-ec-error-queue):

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

ToRaNek and others added 2 commits November 21, 2026 17:45
…2.5.4

Ec.cpp returned every failure with an empty error queue. LibreSSL 2.5.4
queues a reason for each of them in ec_curve.c, ec_key.c, ecp_oct.c,
ecs_ossl.c and ech_key.c:

- an unknown curve NID;
- an invalid form or a short buffer in EC_POINT_point2oct;
- missing parts of a key in EC_KEY_generate_key, EC_KEY_set_public_key,
  EC_KEY_check_key, ECDSA_sign, ECDSA_verify and ECDH_compute_key;
- a public key at infinity, off the curve or not matching the private
  key in EC_KEY_check_key;
- r or s out of range and a sum at infinity in ECDSA_verify;
- a peer point at infinity, a short output and a failing KDF in
  ECDH_compute_key.

Return values are unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…-queue

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@catminou4 catminou4 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.

Built libSceLibreSsl and ran guest_libressl_ec_error_queue_tests on Linux (x86-64, GCC 11).

  • On this branch: passes.
  • Control — reverted src/Ec.cpp to origin/main, kept the new test: aborts at line 171. The test genuinely exercises the queued reason codes.

This queues the LibreSSL 2.5.4 ERR_LIB_EC/ERR_LIB_ECDSA/ERR_LIB_ECDH reason codes for the failures the port returns, pinned to specific 2.5.4 source files. The debt fragment is unusually thorough: it enumerates exactly which failures queue codes and which throw instead (ASN.1 decode, allocation/BN/arithmetic, EC_POINT_new/oct2point, infinity peer point), documents the OPENSSL_BN_ASM_MONT divergence, and — importantly — flags that libSceLibreSsl3 forwards these exports so its callers now get the 2.5.4 ERR_LIB_ECDSA/ERR_LIB_ECDH codes where 4.0.0 would queue ERR_LIB_EC ones. That cross-library consequence is exactly the kind of thing a reviewer should confirm is intended.

@malekaifa malekaifa 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 4c70af631621aed6fd3548e1f27a169977d58151 against LibreSSL 2.5.4 EC/ECDSA/ECDH source at 2e2c612066aa993717528a0249c15c51128d61cb.

The added queue behavior matches the source branches covered by this port: unknown groups and EC key validity failures use the EC reasons; ECDSA missing parameters, out-of-range r/s and EC-arithmetic propagation use the 2.5.4 ECDSA library/reasons; and ECDH preserves the source's unusual check order, including outlen > INT_MAX before key validation, no-private-value before group use, key-truncation before buffer allocation, and the EC→ECDH nested failures. The new tests also preserve earlier queued errors rather than accidentally clearing them. The independent Linux build/test plus old-code control exercises these changes, and exact-head conventions/intake/labels/eligibility/report/portability are green.

The debt entry explicitly records the intentional gaps and the libSceLibreSsl3 cross-version consequence (2.5.4 ECDSA/ECDH library codes where 4.0.0 differs). A current hosted Windows run is still in progress, so this review is not a claim that that run has finished.

AI-assisted: yes

@github-actions

github-actions Bot commented Nov 23, 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 25891 core/libs/prx/libSceLibreSsl/CMakeLists.txt L499

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 11146 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/src/Ec.cpp
PR 11466 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/src/Ec.cpp
PR 13589 core/libs/prx/libSceLibreSsl/CMakeLists.txt
core/libs/prx/libSceLibreSsl/src/Ec.cpp
PR 24322 core/libs/prx/libSceLibreSsl/CMakeLists.txt
PR 24910 core/libs/prx/libSceLibreSsl/CMakeLists.txt
PR 24917 core/libs/prx/libSceLibreSsl/CMakeLists.txt

@PrivacyOnion PrivacyOnion 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.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 4c70af6316:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_libressl_ec_error_queue_tests built cleanly.
  • CTest (guest_libressl_ec_error_queue):
    1/1 Test #4701: guest_libressl_ec_error_queue ....   Passed    0.12 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceLibreSsl/src/Ec.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4701: guest_libressl_ec_error_queue ....Subprocess aborted***Exception:   0.55 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified that EC_POINT_point2oct returns 0 and queues EC_R_INVALID_FORM on invalid form, and EC_R_BUFFER_TOO_SMALL on insufficient buffer length
    • Verified that EC_KEY_generate_key, EC_KEY_set_public_key, and EC_KEY_check_key correctly queue ERR_R_PASSED_NULL_PARAMETER, EC_R_POINT_AT_INFINITY, EC_R_POINT_IS_NOT_ON_CURVE, and EC_R_INVALID_PRIVATE_KEY on their respective error paths
    • Verified that ECDSA_sign and ECDSA_verify queue ERR_LIB_ECDSA with ERR_R_PASSED_NULL_PARAMETER, ECDSA_R_MISSING_PARAMETERS, ECDSA_R_BAD_SIGNATURE, and properly chain EC_R_POINT_AT_INFINITY / ECDSA_R_ERR_LIB_EC on infinity sum
    • Verified that ECDH_compute_key preserves LibreSSL 2.5.4 check order: outLength > INT_MAX (ERR_LIB_ECDH/ERR_R_MALLOC_FAILURE), missing private key (ECDH_R_NO_PRIVATE_VALUE), missing group (ERR_R_PASSED_NULL_PARAMETER + ERR_R_MALLOC_FAILURE), peer point at infinity (EC_R_POINT_AT_INFINITY + ECDH_R_POINT_ARITHMETIC_FAILURE), key truncation, and KDF failure
    • Verified docs/dev/debt/changes/fix-libressl-ec-error-queue.md accurately documents intentional deviations and cross-library behavior with libSceLibreSsl3
    • Verified LP64/LLP64 type safety across size_t, unsigned int, and int in all modified functions and test cases

Looks Good To Me

@oneandonlydean
oneandonlydean merged commit c5c9134 into boykopovar:main Nov 24, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants