Skip to content

Let a crypto callback device hold LMS/HSS private key state - #315

Open
Frauschi wants to merge 4 commits into
wolfSSL:masterfrom
Frauschi:lms-private-device
Open

Frauschi wants to merge 4 commits into
wolfSSL:masterfrom
Frauschi:lms-private-device

Conversation

@Frauschi

Copy link
Copy Markdown
Member

Lets a crypto callback device generate and hold the state of an HSS private key, so the leaf counter can live in hardware and the offline rollback a software state cannot prevent is ruled out. Follows #313; needs wolfSSL 5.9.4 or later with WOLF_CRYPTO_CB and WOLF_PRIVATE_KEY_ID.

Changes

  • CKA_WOLFSSL_DEVID on the private keygen template makes the key on that device, with a random device key id
  • The key record seals the device id and key id in place of a state, bound to the key's policy like a software record
  • Signing goes to the device; the key is spent only when the device returns KEY_EXHAUSTED_E
  • A device that declines leaves the key usable; any other device error disables it until the next login
  • Keygen fails with CKR_DEVICE_ERROR when no device at that id makes the key, instead of falling back to a software state
  • Slot devId now defaults to INVALID_DEVID instead of device 0 (behaviour change; WOLFPKCS11_DEFAULT_DEVID=0 restores it)
  • CKA_KEYS_REMAINING is not available for a device key
  • README section on device-held keys

Testing

  • hbs_private_test: a software test device covering keygen, sign, reload, decline, failure, spent keys, refused keygen, logout/login, destroy moves, C_SetPIN and policy changes
  • New slot_devid_default_test: keys stay in software unless CKA_WOLFSSL_DEVID names a device
  • Existing CI LMS jobs build wolfSSL with --enable-cryptocb, so the device tests run there

@Frauschi Frauschi self-assigned this Nov 24, 2026
Copilot AI balanced review requested due to automatic review settings November 24, 2026 20:15

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 ownership check in src/hbs.c:412 fails to authenticate the stored record, allowing payload tampering or restoration of a stale policy digest to bypass validation and permit signing.

1 open issue
What changed in this PR

Extends LMS/HSS private-key support so crypto callback devices can hold signing state, enabling hardware protection against counter rollback.

Changes:

  • Adds device-backed key generation, sealed device references, and signing with device-specific error handling.
  • Defaults unconfigured slots to software and preserves device routing when decoding ML-DSA keys.
  • Adds device lifecycle tests and documents requirements, limitations, and compatibility changes.
File Description
wolfpkcs11/​internal.h Gates device support and updates the key-generation interface.
tests/​slot_devid_default_test.c Tests software defaults and explicit device selection.
tests/​include.am Adds the slot-device test to Automake builds.
tests/​hbs_private_test.c Tests device-backed key generation, signing, persistence, and failures.
src/​wp11_types.h Clarifies that pending state may contain a device reference.
src/​internal.c Updates device defaults, ML-DSA decoding, and device-ID restrictions.
src/​hbs.h Declares device-backed state and its query helper.
src/​hbs.c Implements device-backed key records and signing lifecycle.
src/​crypto.c Routes explicit device templates into HSS key generation.
README.md Documents device-held keys and default-device migration.
CMakeLists.txt Registers the slot-device regression test.

Review effort: Balanced

Comment thread src/hbs.c 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.

🟡 Changes recommended

The unresolved device-error path can wipe past the caller’s signature buffer when a callback increases the reported output size on failure.

3 open issues
1 resolved since last review

Review effort: Balanced

Comment thread src/hbs.c Resolved
Comment thread src/hbs.c Resolved
Comment thread tests/slot_devid_default_test.c 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.

🟡 Changes recommended

Device-handling defects can leave signing blocked after callback declines and trigger costly software key generation for unregistered devices before failing.

2 open issues
3 resolved since last review

Review effort: Balanced

Comment thread src/hbs.c Resolved
Comment thread src/hbs.c 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

Stateful-signature safety depends on durable device counters and key retention that the in-memory test device cannot validate, requiring device/security review of rollback and failure behavior.

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

Scan targets checked: wolfpkcs11-src, wolfpkcs11-bugs
Not reviewed (no scan target covers them): CMakeLists.txt, README.md, src/hbs.h, src/wp11_types.h, tests/include.am
Coverage: 5 of 6 in-scope changed file(s) opened by the reviewer; not opened: tests/slot_devid_default_test.c

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 src/internal.c Resolved
@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed their stale review November 24, 2026 22:30

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

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

Scan targets checked: wolfpkcs11-src, wolfpkcs11-bugs
Not reviewed (no scan target covers them): CMakeLists.txt, README.md, src/hbs.h, src/wp11_types.h, tests/include.am
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

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

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

Comment thread README.md Resolved
@Frauschi

Copy link
Copy Markdown
Member Author

The debian-package-test failure is unrelated: wolfSSL master breaks under gcc 11 LTO with -Werror=array-bounds in wolfcrypt/test/test.c since wolfSSL #12094. Fixed by wolfSSL/wolfssl#12262; I will rerun the job once it merges.

@Frauschi Frauschi assigned wolfSSL-Bot and unassigned Frauschi Nov 25, 2026
@Frauschi
Frauschi requested a review from LinuxJedi November 25, 2026 09:15
…igured

wp11_Slot_Init left slot->devId at the 0 from the XMEMSET, but 0 is a valid
crypto-callback device slot, so every key inherited a 0 devId and was routed
through wolfSSL's crypto-callback dispatch even with no device registered.
Default to INVALID_DEVID and let the TPM/MAXQ/STM32 paths override it for
real hardware. Also thread the object devId through the ML-DSA decode
(MldsaKeyTryDecode) instead of its own hard-coded INVALID_DEVID, matching
the ML-DSA import/copy paths and re-enabling ML-DSA crypto-callback offload.
The level is still found by trial decodes with INVALID_DEVID, and only the
level that decodes is decoded again with the object devId, so a device with
WOLF_CRYPTO_CB_FREE is never sent frees for keys it never saw.
A key loaded from the store gets the slot devId, since CKA_WOLFSSL_DEVID is
not stored.

slot_devid_default_test checks that keys stay in software unless
CKA_WOLFSSL_DEVID names a device. It counts AES-CBC callbacks, so it skips
unless both wolfSSL and wolfPKCS11 have AES-CBC.

Behaviour change: a no-device build that registered a crypto callback at
devId 0 and relied on the default must now build with
WOLFPKCS11_DEFAULT_DEVID=0. Setting CKA_WOLFSSL_DEVID on a key does not
survive a restart: token keys are decoded at login with the slot devId.
wolfSSL 5.9.2 added crypto callbacks for stateful hash-based signatures, so
a device can generate an LMS key, keep its private state and sign with it.
That is the only way to rule out the offline rollback a software state
cannot prevent, since the device can hold the counter in hardware.

When the private key template carries CKA_WOLFSSL_DEVID, keygen initialises
the key with wc_LmsKey_InitId and a random device key id. A devId that only
comes from the slot, as in TPM, MAXQ10XX and STM32 DHUK builds, keeps the
software path. If the device makes the key, wolfSSL never calls the write
callback; the key record then seals the device id and the device key id in
place of a state, with the same AAD, so editing it or the policy attributes
in the object record makes the key refuse to sign. Records are sealed with
the slot's devId rather than the key's: CKA_WOLFSSL_DEVID is not stored, so
a load unseals the record before it knows the device. Loading the token
re-attaches the key to the device. Signing goes to the device, without the
stored-record checks, and the key is spent only when the device returns
KEY_EXHAUSTED_E: a device need not report a signature count, and
wc_LmsKey_SigsLeft returns 0 for a device key when it does not.

A device key has no software state, so device keygen never installs the
state callbacks, and a loaded device key never has them. A sign the device
declines then falls through to wolfSSL's software path and fails on the
missing callbacks before it uses a leaf; wolfSSL 5.9.2 and 5.9.4 have no
other check there and would sign from an empty state. That argument error
leaves the key usable. Under WOLF_CRYPTO_CB_ONLY_LMS the decline comes back
as NO_VALID_DEVID, which wolfSSL does not mark bad, and leaves the key
usable too. Any other device error disables it until the next login, since
the device may have used a leaf, and wolfSSL marks the key bad. The sign
returns CKR_DEVICE_ERROR. A failed sign wipes the caller's whole buffer
rather than the length wolfSSL reports, since wolfSSL hands the device that
length to update and a failing device may raise it past the buffer. A sign
the device reports as done with a length past the buffer is a device error
too, so C_Sign never reports a signature longer than the caller's buffer.
wolfSSL 5.9.2 leaves the key state alone on a device error, so a device that
used a leaf and then failed with an argument error could not be told from
one that declined; the device path therefore needs 5.9.4.

Before each sign the record at the key's id must still be this key's and
bound to the same policy, so a process whose token is out of date does not
sign with a device key another process destroyed or changed the policy of. A
record that differs from the loaded one must also unseal under the token key
to the same device id and device key id, as a software record does when it
is reloaded, so a record whose ciphertext or plain policy digest was edited
does not pass. An allocation failure in that check still gives
CKR_DEVICE_MEMORY. A loaded key takes its devId from the record, so
CKA_WOLFSSL_DEVID reads back the device, and a change on a device key is
refused with CKR_ATTRIBUTE_READ_ONLY. The device key id the record may hold
is bounded by wolfSSL's LMS_MAX_ID_LEN. A sign does not reload a device key
whose load failed, since that would set its devId under the token read lock;
the key stays disabled until the next login.

If no device makes the key, keygen fails with CKR_DEVICE_ERROR instead of
falling back to a software state, since a caller that asked for a device
would otherwise get the rollback exposure it asked to avoid without
noticing. That covers a registered device that does not handle LMS, for
which wolfSSL refuses keygen by key id with NO_VALID_DEVID, a device that
fails keygen, for example with a full key table, and a device id with no
device registered or a wolfSSL built with WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK,
for which wolfSSL falls back to software keygen. With no state callbacks
that fallback fails with BAD_FUNC_ARG before it builds a tree, so a mistyped
device id fails at once even for a large parameter set rather than after
computing every leaf. Builds without the device path refuse a template that
names a device the same way. A CKA_WOLFSSL_DEVID of INVALID_DEVID asks for
no device and gives a software key that reports the slot's devId, in every
build. Every error of a keygen by key id other than MEMORY_E gives
CKR_DEVICE_ERROR, since the parameters were set before the device was asked.
RNG and parameter errors keep their usual codes.

Built when wolfSSL has WOLF_CRYPTO_CB and WOLF_PRIVATE_KEY_ID, is at least
5.9.4 and has an LMS_MAX_ID_LEN that holds the 16-byte device key id;
WOLF_CRYPTO_CB_FIND builds keep the software path.
CKA_KEYS_REMAINING is not available for a device key, whose count only
the device knows.
hbs_private_test.c gains a software crypto callback device that makes and
holds HSS keys by id, and tests with it:

- a device key, made and signed by the test device, after a reload too
- a device without a signature count, one that reports the key spent, and
  one that does not do LMS, where keygen fails and leaves no key behind
- a device key record bound to the policy attributes, a change of a device
  key's CKA_WOLFSSL_DEVID refused with CKR_ATTRIBUTE_READ_ONLY, and setting
  it to the value it already has
- a device key whose record ciphertext changed, which does not sign until
  the record is restored
- a device that raises the signature length and fails, which must not get
  the caller's buffer written past its end, and a device with a full key
  table, whose keygen gives CKR_DEVICE_ERROR and leaves no key
- a device that declines a sign, for a new key and a reloaded one, and a
  device that fails one, which disables the key until a reload
- a device key across a logout and login, which also re-enables it after
  a device error, and a device that fails keygen
- a device id with no device registered, which fails keygen
- a device key moved by a destroy, a device key another process destroyed,
  and a device key after C_SetPIN and after a policy change, each with a
  reload

A build without the device path refuses a keygen template that names a
device and keeps no key, and every build makes a software key for a
CKA_WOLFSSL_DEVID of INVALID_DEVID. The device tests skip only when the
library never calls the test device, as when it links its own wolfSSL, so
a failing device keygen does not pass as a skip.

The test device frees its keys between tests and is unregistered at the
end.

They build with wolfSSL 5.9.4 or later with WOLF_CRYPTO_CB and
WOLF_PRIVATE_KEY_ID and without WOLF_CRYPTO_CB_FIND. The CI workflows build
wolfSSL with --enable-cryptocb, which defines WOLF_PRIVATE_KEY_ID, so the
device tests run there too.
Add the README part on device-held keys: the wolfSSL build they need, what
the key record keeps, that keygen fails when no device at the id does LMS,
and that destroy, C_InitToken and a failed keygen leave the key on the
device, and that CKA_KEYS_REMAINING is not available for a device key,
written as short paragraphs and a numbered list.

The offline rollback note now points to RFC 10033 section 4, which
recommends dedicated hardware for state management.
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.

4 participants