Skip to content

Add LMS/HSS key generation, signing and verification over PKCS#11 - #12283

Open
Frauschi wants to merge 1 commit into
wolfSSL:masterfrom
Frauschi:pkcs11-lms
Open

Frauschi wants to merge 1 commit into
wolfSSL:masterfrom
Frauschi:pkcs11-lms

Conversation

@Frauschi

@Frauschi Frauschi commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

wc_Pkcs11_CryptoDevCb now handles LMS/HSS stateful signatures through the PKCS#11 HSS mechanisms, so LMS keys can be generated and used on a token that keeps the one-time-signature state, such as wolfPKCS11 since wolfSSL/wolfPKCS11#313.

Description

  • Key generation sends CKA_HSS_LEVELS with the per-level CKA_HSS_LMS_TYPES/CKA_HSS_LMOTS_TYPES lists (PKCS#11 3.1-3.3); the private key is a sensitive, non-extractable, non-copyable token object.
  • Token keys need an id or label, and key generation refuses a name an HSS object already has.
  • Sign and SigsLeft compare the token key's levels and types with the LmsKey parameters and fail with WC_KEY_MISMATCH_E before a leaf is used.
  • CKR_KEY_EXHAUSTED maps to KEY_EXHAUSTED_E; signatures left come from CKA_KEYS_REMAINING.
  • Verify uses the key's own public key or the token's public key found by name, falling back to software.
  • Software LMS keys that carry a devId keep signing in software.
  • wc_LmsKey_Reload/wc_XmssKey_Reload keep a key named by id or label on the device even with a read callback.
  • NO_PKCS11_LMS leaves LMS out; verify-only LMS builds get verification only.
  • Adds the HSS constants, CKA_KEYS_REMAINING and CKR_BUFFER_TOO_SMALL to pkcs11.h.

Testing

  • New test_wc_Pkcs11_Lms in tests/api/test_pkcs11.c, using a mock token that runs real software LMS: key templates, id/label lookup, duplicate names, parameter mismatch, exhaustion, failure cleanup and verify fallbacks.
  • Named-key cases in test_wc_LmsKey_reload_devid/test_wc_XmssKey_reload_devid.

Copilot AI balanced review requested due to automatic review settings November 26, 2026 14:14
@Frauschi Frauschi self-assigned this Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 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 +14,152 B (+5.1%, 296,732 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.

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

Token session failures prevent software LMS fallback and mark keys bad, while improperly initialized test fixtures can dispatch cleanup to an unrelated device.

3 open issues
What changed in this PR

Adds LMS/HSS support to wolfSSL’s PKCS#11 crypto callback, allowing tokens to manage stateful signing keys and their remaining signatures.

Changes:

  • Adds token-backed key generation, signing, verification, and signature-count queries, with parameter checks and software fallbacks.
  • Defines HSS constants and registers the LMS opt-out macro.
  • Adds mock-token tests and clarifies LMS reload documentation.
File Description
wolfssl/​wolfcrypt/​pkcs11.h Adds HSS mechanisms, attributes, types, and error constants.
wolfcrypt/​src/​wc_pkcs11.c Implements LMS/HSS token operations and callback dispatch.
wolfcrypt/​src/​wc_lms.c Clarifies reload behavior in comments; no functional change.
tests/​api/​test_pkcs11.h Registers the LMS PKCS#11 test.
tests/​api/​test_pkcs11.c Adds mock-token coverage for operations, failures, and fallbacks.
.wolfssl_known_macro_extras Registers NO_PKCS11_LMS.

Review effort: Balanced

Comment thread tests/api/test_pkcs11.c Resolved
Comment thread wolfcrypt/src/wc_pkcs11.c Resolved
Comment thread wolfcrypt/src/wc_pkcs11.c Resolved
@Frauschi

Copy link
Copy Markdown
Member Author

@claude review

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

Beyond the inline finding, I checked whether the change to Pkcs11FindKeyOfClass (it now returns 0 with a NULL_PTR handle when no key is found) breaks existing callers. It doesn't. The only caller from before this PR is Pkcs11FindKeyById under WC_PKCS11_FIND_WITH_ID_ONLY, and that function now does the not-found check itself and still returns WC_HW_E.

Extended reasoning...

I traced every call to Pkcs11FindKeyOfClass in wolfcrypt/src/wc_pkcs11.c. Besides the new LMS lookup, which checks for a NULL handle itself, the only other caller is the WC_PKCS11_FIND_WITH_ID_ONLY branch of Pkcs11FindKeyById, and it now returns WC_HW_E when no key is found.

Comment thread wolfcrypt/src/wc_pkcs11.c Resolved

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

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): .wolfssl_known_macro_extras, tests/api/test_pkcs11.h
Coverage: 3 of 4 in-scope changed file(s) opened by the reviewer; not opened: wolfssl/wolfcrypt/pkcs11.h

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: Max

@Frauschi

Copy link
Copy Markdown
Member Author

@claude review

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

Besides the inline finding, I checked whether moving the not-found check out of Pkcs11FindKeyOfClass affects other callers. It doesn't: its only existing caller, the WC_PKCS11_FIND_WITH_ID_ONLY branch of Pkcs11FindKeyById, now does the same check itself. The only new callers are in the LMS duplicate-name check.

Extended reasoning...

Pkcs11FindKeyOfClass is called at wc_pkcs11.c:2722 (Pkcs11FindKeyById, which now returns WC_HW_E itself when no key is found) and at 6263/6266 (new LMS code). So the RSA, ECC, ML-DSA, ML-KEM and AES lookups still fail as before when no key is found.

Comment thread wolfcrypt/src/wc_pkcs11.c Resolved
@Frauschi

Copy link
Copy Markdown
Member Author

@claude review

Comment thread wolfcrypt/src/wc_xmss.c Resolved
@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.

🟡 Changes recommended

Failed or malformed PKCS#11 class-attribute reads can permit duplicate key generation, while named LMS/XMSS keys with unregistered device IDs can reload without private state and fail signing.

3 open issues
3 resolved since last review

Review effort: Balanced

Comment thread wolfcrypt/src/wc_pkcs11.c Resolved
Comment thread wolfcrypt/src/wc_lms.c Resolved
Comment thread wolfcrypt/src/wc_xmss.c Resolved

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

I reviewed the latest push and found no new bugs. This is a large addition to the PKCS#11 crypto callback for stateful LMS/HSS signatures, so a human look would still be worthwhile.

What was reviewed:

  • The wc_LmsKey_Reload/wc_XmssKey_Reload change. A named key now skips the software reload only under WC_CRYPTOCB_KEYGEN_ID_CHECK or ONLY_LMS/ONLY_XMSS. Otherwise the skip depends on devCtx, so named keys generated in software reload from the read callback again.
  • The new HSS keygen/sign/verify/sigs-left paths and their dispatch in wc_Pkcs11_CryptoDevCb, along with the new pkcs11.h constants and build gates.
Extended reasoning...

The change adds about 660 lines of PKCS#11 LMS/HSS handling to wc_pkcs11.c, new PKCS#11 constants, and about 1000 lines of mock-token tests. It also changes when LMS/XMSS Reload becomes a no-op for devId keys. This is crypto key-management code, and signing depends on the device keeping the one-time-signature state. The bug hunt this round found nothing new, and the latest commit appears to address the Reload concern raised earlier. Because of the size and the security-sensitive stateful-signature surface, a human review is still recommended over automatic approval.

wc_Pkcs11_CryptoDevCb now handles the stateful signature callbacks for
LMS keys, using the PKCS#11 HSS mechanisms. Key generation passes
CKA_HSS_LEVELS with the per-level lists CKA_HSS_LMS_TYPES and
CKA_HSS_LMOTS_TYPES in the private key template, as PKCS#11 3.1, 3.2 and
the 3.3 draft define it; the single-value CKA_HSS_LMS_TYPE and
CKA_HSS_LMOTS_TYPE are attributes the token gives the public key. The
private key is a token object, private, sensitive, not extractable and
not copyable, since the token keeps the one-time-signature state; the
public key is kept on the token too so a later process can verify by id
or label.

Keys without an id or label are declined, so software LMS keys that
carry a devId still sign in software, and a token key without a name
would be left behind on the token. Key generation also fails with
BAD_STATE_E when an HSS object, token or session, already has the key's
id or label, as a later lookup by name could otherwise find the older
key; with
WC_PKCS11_FIND_WITH_ID_ONLY the id check covers every private and public
key, whatever its type, as the lookups do, and Pkcs11FindKeyOfClass now
tells a failed search apart from no match so either stops key
generation. Keys are always found by id or label; no object handle is
kept in devCtx, as a handle no longer names the key once the library is
set up again. A key whose state software holds signs, counts
its signatures and verifies in software. These declines, and every XMSS
request, are decided from the key before a session is opened, so a token
that cannot be opened or logged into never fails an operation software
can do, and never leaves a software LMS key marked bad.

Before signing or counting signatures left, the token key's
CKA_HSS_LEVELS and type lists are compared with the LmsKey's parameters,
and a mismatch fails with WC_KEY_MISMATCH_E before the token uses a
one-time key, so SigsLeft reports 0 for a key Sign refuses. That includes a
token key with more levels than the build supports, whose type lists
come back CKR_BUFFER_TOO_SMALL. CKR_KEY_EXHAUSTED
maps to KEY_EXHAUSTED_E; any other signing failure, including a
signature whose length does not match the parameters, is reported as
WC_HW_E and wolfCrypt treats the key as bad. Signatures left come from
CKA_KEYS_REMAINING, the PKCS#11 3.3 name, added with
CKA_HSS_KEYS_REMAINING as an alias.

Verification uses the public key held by the LmsKey when it has one,
falling back to software when no session opens, or the token does not
take it or fails with it, and otherwise the public key named on the
token.
CKR_SIGNATURE_LEN_RANGE is reported as SIG_VERIFY_E like
CKR_SIGNATURE_INVALID. There is no free handler, as generated private
keys are always token objects and freeing a key never destroys one.

wc_LmsKey_Reload and wc_XmssKey_Reload now leave a key named by id or
label on the device even when a read callback is set, when a device is
registered for its devId, as key generation already does, so a named key
is never reloaded in software from storage its key generation never
wrote. A named key whose devId has no registered device is generated in
software and still reloads from its read callback. The exception is
WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK without WOLF_CRYPTO_CB_ONLY_LMS/XMSS:
there a device may decline a named key's generation and leave its state
in software, so the read callback still decides. A devId on an unnamed
key with a read callback still means the caller holds the state. The
keygen-sw-fallback cryptocb-only CI leg now also builds LMS, XMSS and
PKCS#11 to cover it.

NO_PKCS11_LMS leaves LMS out; verify-only LMS builds only get
verification. The HSS constants and CKR_BUFFER_TOO_SMALL are added to
pkcs11.h.

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

The mock tests do not establish durable LMS/HSS state advancement across signing failures, session closure, and process restarts, requiring human validation of real-token persistence and failure cleanup.

No open issues 🎉

3 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 #12283

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): .github/workflows/cryptocb-only.yml, .wolfssl_known_macro_extras, doc/dox_comments/header_files/wc_lms.h, doc/dox_comments/header_files/wc_xmss.h, tests/api/test_pkcs11.h
Coverage: 3 of 5 in-scope changed file(s) opened by the reviewer; not opened: tests/api/test_pkcs11.c, wolfcrypt/src/wc_xmss.c

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