Skip to content

Fix -Wshorten-64-to-32 in the PKCS#11 ML-DSA callbacks - #11819

Open
LinuxJedi wants to merge 1 commit into
wolfSSL:masterfrom
LinuxJedi:pkcs11-mldsa-word32-casts
Open

LinuxJedi wants to merge 1 commit into
wolfSSL:masterfrom
LinuxJedi:pkcs11-mldsa-word32-casts

Conversation

@LinuxJedi

Copy link
Copy Markdown
Member

Description

wolfcrypt/src/wc_pkcs11.c does not build from a git checkout configured with --enable-pkcs11 --enable-mldsa --enable-debug using clang. Pkcs11GetMldsaPublicKey passes the CK_ULONG public key size to wc_MlDsaKey_ImportPubRaw, and the ML-DSA sign callback stores the CK_ULONG signature length in a word32, both without a cast. The debug build enables -Wshorten-64-to-32, and a git checkout adds -Werror:

wolfcrypt/src/wc_pkcs11.c:4922:53: error: implicit conversion loses integer precision: 'CK_ULONG' (aka 'unsigned long') to 'word32' (aka 'unsigned int') [-Werror,-Wshorten-64-to-32]
wolfcrypt/src/wc_pkcs11.c:5234:37: error: implicit conversion loses integer precision: 'CK_ULONG' (aka 'unsigned long') to 'word32' (aka 'unsigned int') [-Werror,-Wshorten-64-to-32]

This change casts both, as the ML-KEM and RSA code in the same file already does. Neither cast can truncate: the public key size has already been matched against the three ML-DSA public key sizes, and C_Sign returns at most the buffer length it was given, which came from the word32 output length.

Found by a new wolfPKCS11 CI job that builds wolfSSL with --enable-pkcs11 (wolfSSL/wolfPKCS11#261).

Testing

  • Configured with --enable-cryptocb --enable-aescfb --enable-rsapss --enable-keygen --enable-pwdbased --enable-scrypt --enable-md5 --enable-debug --enable-mldsa --enable-pkcs11 and C_EXTRA_FLAGS="-DWOLFSSL_PUBLIC_MP -DWC_RSA_DIRECT -DHAVE_AES_ECB -DHAVE_AES_KEYWRAP -DWOLFSSL_MLKEM_DYNAMIC_KEYS -DWOLFSSL_MLDSA_DYNAMIC_KEYS", from a git checkout so -Werror is on:
    • Debian trixie, clang 19, ASAN: the build failed with the two errors above before the change and builds with no warnings after it. wolfPKCS11 make check against that build passes (75 pass, 9 skip).
    • macOS, Apple clang: builds with no warnings.

Checklist

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

Pkcs11GetMldsaPublicKey passes the CK_ULONG public key size to
wc_MlDsaKey_ImportPubRaw, and the ML-DSA sign callback stores the
CK_ULONG signature length in a word32, both without a cast. clang
reports them with -Wshorten-64-to-32, which the debug build enables, so
a git checkout configured with --enable-pkcs11 --enable-mldsa
--enable-debug fails to build under -Werror.

Cast both, as the ML-KEM and RSA code in this file does. The public key
size has already been matched against the ML-DSA key sizes, and C_Sign
returns at most the buffer length it was given.

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

Both conversions are bounded by existing size constraints, with no unresolved correctness issues.

No open issues 🎉

What changed in this PR

Fixes Clang narrowing-conversion errors in wolfSSL’s PKCS#11 ML-DSA callbacks.

Changes:

  • Adds explicit word32 casts for public-key and signature lengths.
  • Preserves existing behavior under the validated size and buffer-length constraints.
File Description
wolfcrypt/​src/​wc_pkcs11.c Casts two ML-DSA lengths to resolve -Wshorten-64-to-32 errors.

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 #11819

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs
Coverage: 1 of 1 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

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