Skip to content

ecc: return infinity for P + (-P) with equal z in the projective add - #11921

Open
yosuke-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-16891
Open

yosuke-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-16891

Conversation

@yosuke-wolfssl

Copy link
Copy Markdown
Contributor

Problem

_ecc_projective_add_point doubles instead of adding when the two operands have equal stored x and z and P->y == p - Q->y. That input is P = -Q, whose sum is the point at infinity, so the function returns 2P.

Fix (wolfcrypt/src/ecc.c)

  • _ecc_projective_add_point doubles only when x, y and z are all equal. P = -Q now goes through the general formula and returns z = 0, which ecc_map turns into (0, 0, 1). The unused p - Q->y computation is removed, and the doc comment states the z = 0 form of infinity.
  • ecc_projective_add_point_safe drops nInf. Its existing z == 0 branch now handles A = -B for both equal and different z, and still sets the infinity flag.

The SP math sp_ecc_proj_add_point_* path compares U/S values instead and is unaffected.

Closes f-16891 and f-16382.

Tests

  • test_wolfSSL_EC_POINT: EC_POINT_add(P, -P) in both orders is at infinity.
  • test_wolfSSL_EC_POINT: EC_POINT_mul(1*G + 1*(-G)) is at infinity. This reaches _safe with A = -B and equal z through ecc_mul2add, and covers the nInf removal.
  • test_ecc_fault_whitebox.c: comments updated for the new path. No change to test vectors.

Verification

  • --enable-opensslextra, --disable-eccshamir, --disable-harden, --enable-all: test_wolfSSL_EC_POINT and testwolfcrypt pass.
  • GCC with -Werror (Ubuntu 24.04): clean build, tests pass.
  • Negative controls: with the old comparison restored, both new checks fail; with the original code (comparison plus nInf), only the EC_POINT_add check fails.
  • Whitebox smoke: test_ecc_whitebox and test_ecc_fault_whitebox pass.

- _ecc_projective_add_point doubles only when the stored x, y and z
  of both points are equal; the p - Q->y operand is removed.
- ecc_projective_add_point_safe drops the nInf note and override
- test_wolfSSL_EC_POINT checks EC_POINT_add(P, -P) in both orders and
  EC_POINT_mul(1*G + 1*(-G)) give the point at infinity.
- test_ecc_fault_whitebox comments describe the add's z == 0 result
  for A = -B.

Issue: F-16891, F-16382
@yosuke-wolfssl yosuke-wolfssl self-assigned this Oct 29, 2026
Copilot AI balanced review requested due to automatic review settings October 29, 2026 05:18

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@yosuke-wolfssl

Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4

  • FLASH: .text -128 B (-0.1%, 207,088 B / 262,144 B, total: 79% used)

gcc-arm-cortex-m4-crypto-only

  • FLASH: .text -128 B (-0.1%, 179,731 B / 262,144 B, total: 69% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text -128 B (-0.1%, 194,200 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text -128 B (-0.0%, 793,436 B / 1,048,576 B, total: 76% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: .text -128 B (-0.1%, 220,756 B / 262,144 B, total: 84% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text -128 B (-0.0%, 308,944 B / 1,048,576 B, total: 29% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text -128 B (-0.1%, 246,434 B / 262,144 B, total: 94% used)

gcc-arm-cortex-m7

  • FLASH: .text -128 B (-0.1%, 207,088 B / 262,144 B, total: 79% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text -128 B (-0.0%, 309,840 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text -128 B (-0.1%, 246,434 B / 262,144 B, total: 94% used)

zephyr-frdm_rw612

  • FLASH: log_const_area -8 B, rodata -8 B, text +160 B (+0.1%, 282,608 B / 67,108,864 B, total: 0% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot 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.

Fenrir Automated Review — PR #11921

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-bugs
Coverage: 2 of 3 in-scope changed file(s) opened by the reviewer; not opened: tests/unit-mcdc/test_ecc_fault_whitebox.c

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

@claude claude Bot 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.

Code review found no issues

No high-confidence issues detected in this change.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants