Skip to content

Fix ML-DSA key cache size check for verify-only builds#452

Open
miyazakh wants to merge 5 commits into
wolfSSL:mainfrom
miyazakh:fix_mldsa_macro
Open

Fix ML-DSA key cache size check for verify-only builds#452
miyazakh wants to merge 5 commits into
wolfSSL:mainfrom
miyazakh:fix_mldsa_macro

Conversation

@miyazakh

Copy link
Copy Markdown
Contributor

Summary

When WOLFSSL_DILITHIUM_VERIFY_ONLY is defined, the server only needs to
cache the public key DER, not the full keypair. The previous static assert
unconditionally required the cache buffer to fit a full keypair DER, causing
unnecessary failures in verify-only configurations.

This change introduces WH_SERVER_MLDSA_MAX_CACHE_DER_SIZE to select the
correct maximum size at compile time:

  • WOLFSSL_DILITHIUM_VERIFY_ONLY defined: MLDSA_MAX_PUB_KEY_DER_SIZE
  • Otherwise: MLDSA_MAX_BOTH_KEY_DER_SIZE

The static assert is updated to use this macro so it reflects the actual
requirement for each build configuration.

Testing

  • Build with and without WOLFSSL_DILITHIUM_VERIFY_ONLY and confirm the
    static assert passes in both cases.

@miyazakh miyazakh self-assigned this Jul 14, 2026
Copilot AI review requested due to automatic review settings July 14, 2026 22:40

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.

Pull request overview

Fixes an ML-DSA server key-cache sizing check to accommodate WOLFSSL_DILITHIUM_VERIFY_ONLY builds where only the public key DER needs to be cached, avoiding unnecessary compile-time failures in verify-only configurations.

Changes:

  • Adds a configuration-dependent ML-DSA maximum cache DER size selection in wh_server_crypto.c.
  • Updates the key-cache static assert logic for verify-only vs full-keypair builds.
Comments suppressed due to low confidence (1)

src/wh_server_crypto.c:787

  • In WOLFSSL_DILITHIUM_VERIFY_ONLY builds the static assert now allows WOLFHSM_CFG_SERVER_KEYCACHE_BIG_BUFSIZE to be sized for only the public-key DER, but wh_Server_MlDsaKeyCacheImport() still hard-codes MLDSA_MAX_BOTH_KEY_DER_SIZE when acquiring a cache slot and serializing. That can make verify-only builds pass the compile-time check but fail key cache import at runtime due to the larger requested size. Use WH_SERVER_MLDSA_MAX_CACHE_DER_SIZE for the cache-slot check and serialization sizing.
#endif

int wh_Server_MlDsaKeyCacheImport(whServerContext* ctx, wc_MlDsaKey* key,
                                  whKeyId keyId, whNvmFlags flags,
                                  uint16_t label_len, uint8_t* label)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/wh_server_crypto.c Outdated
Comment thread src/wh_server_crypto.c Outdated
@miyazakh
miyazakh marked this pull request as ready for review July 15, 2026 05:41
@miyazakh miyazakh assigned wolfSSL-Bot and unassigned miyazakh Jul 15, 2026

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

Looks good, but can you please add simple test cases to build-and-test.yml and build-and-test-refactor.yml

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

Scan targets checked: wolfhsm-crypto-bugs, wolfhsm-src
Findings: 1

Medium (1)

wh_Server_MlDsaKeyCacheImport hardcodes MLDSA_MAX_BOTH_KEY_DER_SIZE after static assert is relaxed

File: src/wh_server_crypto.c:800
Function: wh_Server_MlDsaKeyCacheImport
Category: Logic errors

wh_Server_MlDsaKeyCacheImport() still requests MLDSA_MAX_BOTH_KEY_DER_SIZE from wh_Server_KeystoreGetCacheSlotChecked(), but the PR now allows WOLFHSM_CFG_SERVER_KEYCACHE_BIG_BUFSIZE >= MLDSA_MAX_PUB_KEY_DER_SIZE in verify-only mode, so key import fails at runtime in any minimally-configured verify-only build.

Recommendation: Replace both MLDSA_MAX_BOTH_KEY_DER_SIZE occurrences in wh_Server_MlDsaKeyCacheImport() with WH_SERVER_MLDSA_MAX_CACHE_DER_SIZE.

Referenced code: src/wh_server_crypto.c:800-804 (5 lines)


This review was generated automatically by Fenrir. Findings are non-blocking.

@padelsbach padelsbach assigned miyazakh and unassigned wolfSSL-Bot Jul 15, 2026
@padelsbach

Copy link
Copy Markdown
Contributor

Agree with the Fenrir finding. Please correct uses of WH_SERVER_MLDSA_MAX_CACHE_DER_SIZE.

Fix fenrir comments

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

Scan targets checked: wolfhsm-crypto-bugs, wolfhsm-src

Findings: 2
2 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Findings are non-blocking.

Comment thread src/wh_server_crypto.c Outdated
Comment thread src/wh_server_crypto.c Outdated
@miyazakh miyazakh assigned padelsbach and unassigned miyazakh Jul 16, 2026
Comment thread src/wh_server_crypto.c
Comment thread .github/workflows/build-and-test-refactor.yml
@padelsbach padelsbach assigned miyazakh and unassigned padelsbach Jul 16, 2026
@padelsbach

Copy link
Copy Markdown
Contributor

@miyazakh there was a github outage earlier which caused test failures. Can you push again to re-trigger CI?

@miyazakh

Copy link
Copy Markdown
Contributor Author

@padelsbach
It seems that all the tests passed after manual re-testing.

@miyazakh miyazakh assigned padelsbach and unassigned miyazakh Jul 17, 2026
@padelsbach

Copy link
Copy Markdown
Contributor

LGTM, will give @bigbrett a chance for a final review.

@padelsbach padelsbach assigned bigbrett and unassigned padelsbach Jul 17, 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.

6 participants