Skip to content

Fix compressed ECC point decompression for secp224k1 and reject an x with no square root - #11982

Open
dgarske wants to merge 2 commits into
wolfSSL:masterfrom
dgarske:ecc_sqrtmod
Open

dgarske wants to merge 2 commits into
wolfSSL:masterfrom
dgarske:ecc_sqrtmod

Conversation

@dgarske

@dgarske dgarske commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Under SQRTMOD_USE_MOD_EXP, mp_sqrtmod_prime() used the exponent (p-1)/4 for a prime that is 5 mod 8, which never yields a root, so a compressed secp224k1 point decompressed to a wrong y; it also returned a value for an x with no square root. It now uses Atkin's method for p = 5 mod 8, squares the result and returns MP_VAL when it is not a root, and keeps its small stack footprint; a prime that is 1 mod 8 (P-224) stays unsupported under that define and the crypt test skips it there. The generated sp_ecc_uncompress_256/384/521() now square the recovered y as well, reducing both sides below the modulus first as sp_<n>_map_<w>() does, so a trusted compressed import in SP builds can no longer produce a point off the curve. Also reported by @mimoex (ZD 22735) and Resolves F-16899.

@dgarske
dgarske requested a review from SparkiDev November 3, 2026 23:02
@dgarske dgarske self-assigned this Nov 3, 2026
Copilot AI balanced review requested due to automatic review settings November 3, 2026 23:02
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

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.

🟡 Changes recommended

Critical issues remain: an unguarded test helper breaks feature-disabled builds, and noncanonical field comparisons reject valid compressed P-256 and P-521 points.

3 open issues
What changed in this PR

This PR corrects compressed ECC point recovery in wolfCrypt and adds checks to reject coordinates with no valid square root.

Changes:

  • Uses Atkin’s method for primes congruent to 5 modulo 8 under SQRTMOD_USE_MOD_EXP, with square-back validation.
  • Adds recovered-root validation across nine generated single-precision arithmetic backends.
  • Adds compressed-point regression tests and skips unsupported P-224 round trips under SQRTMOD_USE_MOD_EXP.
File Description
wolfcrypt/​test/​test.c Skips unsupported compressed P-224 round trips.
wolfcrypt/​src/​sp_x86_64.c Adds root validation for x86-64 decompression.
wolfcrypt/​src/​sp_riscv64.c Adds root validation for RISC-V decompression.
wolfcrypt/​src/​sp_dsp32.c Adds root validation for DSP P-256 decompression.
wolfcrypt/​src/​sp_cortexm.c Adds root validation for Cortex-M decompression.
wolfcrypt/​src/​sp_c64.c Adds root validation for portable 64-bit decompression.
wolfcrypt/​src/​sp_c32.c Adds root validation for portable 32-bit decompression.
wolfcrypt/​src/​sp_armthumb.c Adds root validation for ARM Thumb decompression.
wolfcrypt/​src/​sp_arm64.c Adds root validation for ARM64 decompression.
wolfcrypt/​src/​sp_arm32.c Adds root validation for ARM32 decompression.
wolfcrypt/​src/​ecc.c Corrects modular square-root calculation and validates results.
tests/​api/​test_ecc.h Registers the compressed-point regression test.
tests/​api/​test_ecc.c Adds known-point, no-root, and round-trip tests.

Review effort: Balanced

Comment thread tests/api/test_ecc.c Resolved
Comment thread wolfcrypt/src/sp_c32.c Resolved
Comment thread wolfcrypt/src/sp_c64.c Resolved
@dgarske
dgarske force-pushed the ecc_sqrtmod branch 2 times, most recently from c0576a4 to 9bf5d5f Compare November 3, 2026 23:42
@dgarske

dgarske commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

Jenkins retest this please "Found unhandled java.io.IOException exception:" and FIleExceptions.

@dgarske dgarske assigned wolfSSL-Bot and SparkiDev and unassigned dgarske Nov 4, 2026
Comment thread wolfcrypt/src/ecc.c
else if (res == MP_OKAY && i == 5) {
res = mp_sub_d(prime, 1, &e);

/* prime mod 8 = 5: v = (2N)^((p-5)/8), i = 2N.v^2, ret = N.v.(i-1) */

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.

Put #if guard around the prime mod 8 = 5 code.
It is not regularly needed and should be dropped when the user defines something.

@dgarske dgarske self-assigned this Nov 4, 2026
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