Skip to content

Validate the hash input of the signature hash APIs - #12263

Open
Frauschi wants to merge 1 commit into
wolfSSL:masterfrom
Frauschi:signature-rsa-w-enc-digestinfo
Open

Frauschi wants to merge 1 commit into
wolfSSL:masterfrom
Frauschi:signature-rsa-w-enc-digestinfo

Conversation

@Frauschi

Copy link
Copy Markdown
Member

Description

wc_SignatureGenerateHash_ex() signed any input, so an RSA_W_ENC caller passing a bare hash got a signature no other implementation accepts, and wc_SignatureVerifyHash() accepted a DigestInfo with the wrong OID or trailing data. Both now validate the hash input the same way.

Fixes #12231, F-16574 and F-16575.

  • RSA_W_ENC hash input must be exactly one DER DigestInfo whose OID and digest size match hash_type, on generate and verify.
  • Generate rejects an ECC or plain RSA hash whose length differs from the hash_type digest size, as verify already did.
  • The RSA_W_ENC hash APIs return SIG_TYPE_E without ASN, matching the data APIs.
  • Doxygen lists the new return codes, and the WC_SIGNATURE_TYPE_RSA_W_ENC enum comment says which APIs add the DigestInfo.

Behavior change

  • wc_SignatureGenerateHash_ex(..., verify=0) now fails for input it used to sign: a bare hash with RSA_W_ENC, or a wrong-length hash with ECC or RSA. With verify=1 these already failed in the self-verify.

Testing

  • New cases in test_wc_SignatureFeatureCoverage: DigestInfo round trip, bare hash, trailing data, nested and non-NULL AlgorithmIdentifier, missing NULL parameter, OID mismatch, wrong digest size, BLAKE2B, and wrong-length ECC and RSA hashes on generate.

Copilot AI balanced review requested due to automatic review settings November 25, 2026 10:55
@Frauschi Frauschi self-assigned this Nov 25, 2026

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

Under WOLFSSL_ASN_TEMPLATE, the DigestInfo validator accepts malformed 05 80 NULL parameters, allowing them to be signed and verified despite the DER-only contract.

2 open issues
What changed in this PR

This PR tightens wolfCrypt’s signature hash APIs to reject malformed or mismatched hash inputs consistently, addressing the RSA_W_ENC behavior reported in #12231.

Changes:

  • Adds shared DigestInfo validation for RSA_W_ENC generation and verification, with SIG_TYPE_E when ASN support is unavailable.
  • Rejects wrong-length ECC and plain RSA hashes during generation.
  • Adds regression tests and clarifies API input requirements and return codes.
File Description
wolfssl/​wolfcrypt/​signature.h Clarifies which APIs add or accept DigestInfo.
wolfcrypt/​src/​signature.c Adds shared DigestInfo validation and generation length checks.
tests/​api/​test_signature.c Covers valid encodings, malformed inputs, and hash-length mismatches.
doc/​dox_comments/​header_files/​signature.h Documents hash input requirements and validation errors.

Review effort: Balanced

Comment thread wolfcrypt/src/signature.c Resolved
Comment thread wolfcrypt/src/signature.c Resolved
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4

  • FLASH: .text +192 B (+0.1%, 211,821 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m4-crypto-only

  • FLASH: .text +192 B (+0.1%, 184,400 B / 262,144 B, total: 70% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +192 B (+0.0%, 804,368 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: .text +192 B (+0.1%, 226,193 B / 262,144 B, total: 86% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text +192 B (+0.1%, 318,640 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text +192 B (+0.1%, 349,032 B / 1,048,576 B, total: 33% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +192 B (+0.1%, 254,919 B / 262,144 B, total: 97% used)

gcc-arm-cortex-m7

  • FLASH: .text +192 B (+0.1%, 211,821 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text +192 B (+0.1%, 319,472 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text +192 B (+0.1%, 254,919 B / 262,144 B, total: 97% used)

stm32-sim-stm32h753

  • FLASH: .text +320 B (+0.2%, 210,348 B / 2,097,152 B, total: 10% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +14,112 B (+5.0%, 296,692 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.

@Frauschi
Frauschi force-pushed the signature-rsa-w-enc-digestinfo branch 2 times, most recently from 8b1f335 to 4066e98 Compare November 25, 2026 14:09
@Frauschi
Frauschi requested a balanced review from Copilot November 25, 2026 14:22
@Frauschi

Copy link
Copy Markdown
Member Author

@claude review

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.

🟢 Approval recommended

No active review issues were identified for the signature hash-input validation changes.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread wolfcrypt/src/signature.c Resolved
@Frauschi
Frauschi force-pushed the signature-rsa-w-enc-digestinfo branch 2 times, most recently from d1e1c02 to e35b551 Compare November 26, 2026 08:19

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

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/signature.h
Coverage: 3 of 3 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

Comment thread wolfcrypt/src/signature.c Resolved
@Frauschi
Frauschi force-pushed the signature-rsa-w-enc-digestinfo branch from e35b551 to d97f663 Compare November 26, 2026 09:15

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

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): doc/dox_comments/header_files/signature.h
Coverage: 2 of 2 in-scope changed file(s) opened by the reviewer

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

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed their stale review November 26, 2026 09:28

Fenrir's latest completed scan found no issues; clearing the prior automated change request.

@Frauschi

Copy link
Copy Markdown
Member Author

Jenkins retest this please

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

The missing SM3 OID mapping causes valid SM3 DigestInfo inputs to be rejected, breaking previously accepted RSA_W_ENC signing and verification.

1 open issue

Review effort: Balanced

Comment thread wolfcrypt/src/signature.c Resolved
wc_SignatureGenerateHash_ex() signed whatever it was given. For
WC_SIGNATURE_TYPE_RSA_W_ENC a bare digest produced PKCS#1 v1.5
padding without a DigestInfo, which no other implementation accepts,
and the default self-verify then failed with ASN_PARSE_E after the
signature buffer was already written. For ECC and plain RSA a hash of
the wrong length for hash_type was signed as-is.

wc_SignatureVerifyHash() parsed the DigestInfo but skipped the
AlgorithmIdentifier OID and ignored trailing data, so a DigestInfo
for another hash, or one followed by extra bytes, passed validation.

Both functions now apply the same checks. For RSA_W_ENC, hash_data must
be exactly one DER DigestInfo whose AlgorithmIdentifier holds the OID of
hash_type with absent or NULL (05 00) parameters, and whose digest has
the hash_type size. The OID and parameters are compared byte for byte,
because GetAlgoId() matches only an OID sum in NO_VERIFY_OID builds and
for hashes OidFromId() does not know, and the template GetAlgoId() reads
an indefinite-length NULL (05 80) as an empty one. For the other types,
hash_len must be the hash_type digest size. WC_HASH_TYPE_MD5_SHA returns
HASH_TYPE_E, since MD5||SHA-1 has no DigestInfo OID and wc_HashGetOID()
maps it to the MD5 OID. A hash type whose OID OidFromId() does not
know, such as SM3, also returns HASH_TYPE_E. Without ASN support, the
RSA_W_ENC hash APIs return SIG_TYPE_E, as the data APIs already do.

Fixes wolfSSL#12231.
Fixes F-16574.
Fixes F-16575.
@Frauschi
Frauschi force-pushed the signature-rsa-w-enc-digestinfo branch from d97f663 to 4981cfc Compare November 27, 2026 08:38

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.

🟢 Approval recommended

No active review issues were identified for the changes aligning hash-input validation across signature generation and verification.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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.

[Bug]: WC_SIGNATURE_TYPE_RSA_W_ENC and wc_SignatureGenerateHash behaves strangely

4 participants