Skip to content

sha2, sha3: refuse mixed variants, rates and SHAKE call order in FIPS v7 - #12213

Draft
kaleb-himes wants to merge 1 commit into
wolfSSL:masterfrom
kaleb-himes:kh-fips-v7-hash-variant-refusals
Draft

kaleb-himes wants to merge 1 commit into
wolfSSL:masterfrom
kaleb-himes:kh-fips-v7-hash-variant-refusals

Conversation

@kaleb-himes

Copy link
Copy Markdown
Contributor

Description

Adds WC_HASH_CHECK_VARIANT. When it is set, a SHA-2 or SHA-3/SHAKE state
records the variant that initialized it, and later calls that do not match
that variant return BAD_STATE_E. settings.h turns it on for FIPS v7 builds
only. Other builds do not change unless they define it themselves.

Changes

  • SHA-224/256: Init records digestSz. Final and FinalRaw refuse a state
    that another variant initialized, for example SHA-256 Final on a SHA-224
    state.
  • SHA-384/512, SHA-512/224, SHA-512/256: the same check in
    Sha512_Family_Final, Sha512FinalRaw, wc_Sha384Final and wc_Sha384FinalRaw.
  • SHA-3/SHAKE: each state keeps one rate from Init to Final (FIPS 202 6.1,
    6.2) and one domain (0x06 for SHA-3, 0x1f for SHAKE). This matters because
    SHA3-256 and SHAKE256 share a rate. Update and Absorb are refused after
    Absorb has padded the input (FIPS 202 Algorithm 8). SqueezeBlocks is
    refused unless Absorb ran first at the same rate.
  • Shake128/256_Absorb: a refused call keeps any buffered input. Before,
    i was cleared even when the call failed.
  • ML-DSA: mldsa_squeeze256 absorbs into the SHAKE256 state directly on the
    USE_INTEL_SPEEDUP / MLDSA_SHAKE_DIRECT path, so it now marks that state
    absorbed. Later refill squeezes still work.

Testing

Tests

  • New: test_wc_Sha224_Sha256_MixedFinal, test_wc_Sha512_Family_MixedFinal
    and test_wc_Sha3_MixedRate. They cover mixed variants, mixed rates, mixed
    SHA-3/SHAKE domains, Update after Absorb, and Squeeze before Absorb.
  • test_mldsa_make_key_from_seed gets an ML-DSA-65 seed that needs a third
    SHAKE256 block for s2[4] (FIPS 204 Alg 31, 33). This exercises the refill
    path above. The expected bytes are the BitPack encoding (Alg 17, 24).

Testing

  • --enable-fips=v7 against unchanged fips master a0a9d85d: testwolfcrypt and
    unit.test pass.
  • Non-FIPS --enable-all: passes.
  • Negative control (headers and tests kept, library .c files reverted): the
    new tests fail at tests/api/test_sha256.c:462 and tests/api/test_sha3.c
    lines 934, 1381, 1416 and 2665.

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

Copilot AI balanced review requested due to automatic review settings November 20, 2026 16:01
@kaleb-himes kaleb-himes self-assigned this Nov 20, 2026
@kaleb-himes
kaleb-himes marked this pull request as draft November 20, 2026 16:02
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +13,496 B (+4.8%, 296,076 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


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

This comment was marked as resolved.

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.

🔵 Needs a closer look

Changes to FIPS v7 hash-state enforcement and context layouts require maintainer validation against the licensed module and approved operating-environment matrix, neither of which is available in this checkout.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@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 #12213

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_sha256.h, tests/api/test_sha3.h, tests/api/test_sha512.h
Coverage: 10 of 12 in-scope changed file(s) opened by the reviewer; not opened: tests/api/test_sha256.c, wolfssl/wolfcrypt/settings.h

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

Comment thread wolfcrypt/src/sha256.c
return BAD_FUNC_ARG;
}
#ifdef WC_HASH_CHECK_VARIANT
if (sha256->digestSz != WC_SHA256_DIGEST_SIZE) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHA-256 Final always fails on ESP32-HW and Freescale MMCAU builds: their init paths never set digestSz · Logic errors

Only the generic InitSha256() (sha256.c:309) sets digestSz. The ESP32-HW InitSha256() (sha256.c:1253) and MMCAU wc_InitSha256_ex() (sha256.c:988) don't, so with WC_HASH_CHECK_VARIANT every wc_Sha256Final/wc_Sha256FinalRaw on those builds returns BAD_STATE_E.

Suggested fix: Set digestSz = WC_SHA256_DIGEST_SIZE under WC_HASH_CHECK_VARIANT in the ESP32 InitSha256() and in the Freescale MMCAU wc_InitSha256_ex().
Basis: wolfCrypt invariant added by this PR: wc_Sha256Final and wc_Sha256FinalRaw (sha256.c:2531, 2569) accept a state only if its init path recorded digestSz.

Related known findings (similar but distinct; listed for context, not part of this finding)

  • F-11173 (open): Only F-11173 is materially related, and it is not the same defect. (1) Location: the candidate is wc_Sha256Final's digestSz check (:2569), rooted in the ESP32 InitSha256 and MMCAU wc_InitSha256_ex init paths. F-11173 is the Freescale wc_InitSha256_ex (:824-877), so the two share only the Freescale init function. (2) Operation: the candidate has Final/FinalRaw reject a state whose digestSz was never recorded, returning BAD_STATE_E. F-11173 has the init dereference a NULL context. (3) Root cause: the

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.

3 participants