Repository navigation
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The P-256 test guard omits the minimum-key-size condition, causing the new test to fail on valid builds where ECC_MIN_KEY_SZ exceeds 256 and P-256 is excluded.
1 open issue
What changed in this PR
Follows up on #12023 by hardening wolfCrypt’s ECC scalar-length checks and correcting random-integer generation when reusing heap-math values.
Changes:
- Replaces scalar bit-count checks with digit-based checks that avoid timing dependence on the top set bit while preserving existing limits.
- Clears heap-math
mp_rand()output before sizing it, preventing stale high digits and sign from surviving. - Adds regression tests for ECC scalar-length boundaries and random-integer reuse.
| File | Description |
|---|---|
wolfcrypt/src/wolfmath.c |
Clears reused heap-math integers before random generation. |
wolfcrypt/src/ecc.c |
Adds and applies the scalar-length helper across multiplication paths. |
tests/api/test_wolfmath.c |
Tests random generation into a previously wider integer. |
tests/api/test_ecc.h |
Declares and registers the scalar-length test. |
tests/api/test_ecc.c |
Adds P-256 and P-521 scalar-length boundary tests. |
Review effort: Balanced
|
7443f90 to
85091b4
Compare
b999b63 to
d4ffd30
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
Secret-scalar timing changes require human validation of target/compiler side-channel behavior on targets without bit-scan instructions, which source inspection and a standalone boundary check cannot establish.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #12129
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_ecc.h
Coverage: 4 of 4 in-scope changed file(s) opened by the reviewer
Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
Review tier: Max
| } | ||
|
|
||
| /* Check the scalar length limit of wc_ecc_mulmod() on one curve. */ | ||
| static int test_ecc_mulmod_k_len_curve(int curveId) |
There was a problem hiding this comment.
Static test helper unused when no per-curve guard holds, breaking -Werror builds · Dead/unreachable code
Static test_ecc_mulmod_k_len_curve is guarded only by TEST_ECC_MULMOD_K_LEN, which has no curve conditions. With WOLFSSL_SP_MATH+WOLFSSL_SP_NO_256 and no WOLFSSL_SP_521, or P-384-only ECC_USER_CURVES, both call sites drop out and -Wunused-function under -Werror fails the build.
Suggested fix: Add the OR of the P-256 and P-521 call-site conditions to the TEST_ECC_MULMOD_K_LEN guard, as test_ecc_import_compressed_no_root does at line 1501.
Basis: GCC manual, -Wunused-function: warns whenever a non-inline static function is unused and is enabled by -Wall; wolfSSL's AX_HARDEN_CC_COMPILER_FLAGS adds -Werror in VCS checkouts.

Description
Two follow-ups to #12023.
ECC scalar length. The generic point multiplication entry points (
wc_ecc_mulmod_ex(),wc_ecc_mulmod_ex2()and the SP wrapper) range-checked the scalar withmp_count_bits(), and the FP_ECC comb compared its byte length. With fastmath and heap math, and with SP math on targets that have no bit-scan instruction,mp_count_bits()loops over the bits of the top digit, so the time it takes depends on the scalar's bit length.ecc_k_too_long()now ORs together the bits of the scalar at and above the limit instead, so only the number of digits in the scalar affects the time. The limits and error codes are unchanged. The remaining value-dependent branches in the generic point arithmetic, and the separatewc_ecc_mulmod_ex()implementations in the NXP KSDK and STM32 ports, are not changed here.mp_rand on heap math.
mp_rand()sized its output withmp_set_bit(), which only ORs one bit into whateveraalready holds. Whenahad more digits than requested, the result kept the old high digits and their sign.ais now zeroed first, withmp_2expt(). SP math and fastmath setuseddirectly and are unchanged.Testing
test_wc_ecc_mulmod_k_lencovers the scalar length boundary on P-256 and P-521, andtest_mp_randcovers a call on anmp_intthat already holds a wider value, on every math back end.ZD 22745.