Skip to content

Add LMS/HSS private-key generation and signing - #313

Merged
LinuxJedi merged 6 commits into
wolfSSL:masterfrom
Frauschi:lms-private-software
Nov 24, 2026
Merged

LinuxJedi merged 6 commits into
wolfSSL:masterfrom
Frauschi:lms-private-software

Conversation

@Frauschi

Copy link
Copy Markdown
Member

Adds HSS key generation and signing behind --enable-lms-private (experimental, off by default). The one-time-signature state lives in the key record, sealed to the token key and the key's policy, and a signature is released only after the advanced state is durably stored. Keys held by a crypto callback device follow in a separate PR.

Changes

  • C_GenerateKeyPair(CKM_HSS_KEY_PAIR_GEN) and C_Sign(CKM_HSS) for token-only private keys, with a user login
  • State sealed with AES-GCM under a per-write key derived from the token key; the AAD binds parameters, policy digest and signature counter
  • A failed state write disables the key until it reloads from the stored record; rolled-back, foreign or tampered records are refused
  • C_SetPIN, policy changes, destroy moves and C_InitToken treat the record like any key record and keep a newer state another process stored
  • File store: a record that exists but cannot be read is reported apart from a missing one
  • configure checks for a sign-capable wolfSSL LMS build; CMake option WOLFPKCS11_LMS_PRIVATE
  • README section on the stateful key model, multi-process signing and store requirements

Testing

  • New hbs_private_test (functional, corruption, durability, multi-process and debug-hook cases) and hss_concurrency_test (unique leaves across threads)
  • token_store_test: unreadable token record; custom_store_test: HSS moves and failures on a close-commit store
  • CI: lms_private, lms_private_static, lms_private_debug and lms_private_small jobs, plus CMake and sanitizer entries

Copilot AI balanced review requested due to automatic review settings November 20, 2026 15:19
@Frauschi Frauschi self-assigned this Nov 20, 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

Unresolved critical flaws can allow HSS OTS leaf reuse after power loss and out-of-bounds reads when comparing malformed key-generation attributes.

4 open issues
What changed in this PR

Adds opt-in, experimental LMS/HSS private-key support to wolfPKCS11, extending its existing public-key import and verification capabilities.

Changes:

  • Enables HSS key-pair generation and signing with token-resident private keys.
  • Seals signature state and persists advances before releasing signatures, integrating with token storage and key lifecycle operations.
  • Adds build options, storage and concurrency tests, CI coverage, and operational documentation.
File Description
wolfpkcs11/​store.h Activates the HSS private-key record type.
wolfpkcs11/​pkcs11.h Defines the key-exhaustion return code.
wolfpkcs11/​internal.h Adds dependency guards, HSS APIs, and test hooks.
tests/​token_store_test.c Tests unreadable token-record handling.
tests/​pkcs11v3test.c Updates HSS expectations for signing-enabled builds.
tests/​include.am Registers new tests and adjusts custom-store test selection.
tests/​hss_concurrency_test.c Checks concurrent signing, verification, and unique leaf use.
tests/​custom_store_test.c Covers HSS record moves and custom-store failures.
src/​wp11_types.h Adds stateful-key fields and storage helper declarations.
src/​slot.c Advertises HSS signing and key-generation mechanisms.
src/​internal.c Integrates durable state with storage, PIN changes, and object lifecycle operations.
src/​hbs.h Declares stateful-key flags and helper interfaces.
src/​hbs.c Implements HSS generation, signing, sealed records, and state recovery.
src/​crypto.c Connects HSS operations to PKCS#11 entry points and template validation.
README.md Documents configuration, security limits, and storage requirements.
configure.ac Adds the private-LMS option and sign-capable wolfSSL check.
CMakeLists.txt Adds the private-LMS option and test registration.
cmake/​options.h.in Exposes the private-LMS build definition.
.github/​workflows/​unit-test.yml Adds signing-enabled, static, debug, and small-state jobs.
.github/​workflows/​sanitizer-tests.yml Adds private-LMS coverage with empty PINs allowed.
.github/​workflows/​cmake.yml Enables private LMS in CMake CI.
.github/​workflows/​build-workflow.yml Makes wolfSSL’s LMS build mode configurable.

Review effort: Balanced

Comment thread src/crypto.c Resolved
Comment thread src/internal.c Resolved
Comment thread tests/hss_concurrency_test.c Resolved
Comment thread wolfpkcs11/internal.h Resolved
@Frauschi
Frauschi force-pushed the lms-private-software branch from 49692e1 to 650b4f6 Compare November 20, 2026 15:42
@Frauschi
Frauschi requested a balanced review from Copilot November 20, 2026 15:43
@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.

🔵 Needs a closer look

Security-critical one-time-signature state spans crash recovery, cross-process locking, and multiple storage backends, requiring cryptographic and storage maintainer review plus fault-injection validation of the no-signature-reuse guarantee.

No open issues 🎉

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

Findings marked 🟡 are optional suggestions and need no follow-up push.

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

Scan targets checked: wolfpkcs11-src, wolfpkcs11-bugs
Not reviewed (no scan target covers them): .github/workflows/build-workflow.yml, .github/workflows/cmake.yml, .github/workflows/sanitizer-tests.yml, .github/workflows/unit-test.yml, CMakeLists.txt, README.md, cmake/options.h.in, configure.ac, src/hbs.h, src/wp11_types.h, tests/include.am
Coverage: 11 of 12 in-scope changed file(s) opened by the reviewer; not opened: wolfpkcs11/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 Frauschi assigned wolfSSL-Bot and unassigned Frauschi Nov 20, 2026
@Frauschi
Frauschi requested a review from LinuxJedi November 20, 2026 16:35
@Frauschi
Frauschi force-pushed the lms-private-software branch from 00d039e to 77b7329 Compare November 21, 2026 19:02
Opening a file store record for reading returned NOT_AVAILABLE_E for
any fopen failure, so a record that exists but could not be read, for
example after EACCES or EMFILE, looked the same as one that was never
written. For the token record that meant the token was reported as
uninitialized and could be initialized over the stored one. Only a
missing file now gives NOT_AVAILABLE_E; any other failure gives
READ_ONLY_E and stops the load. A read that fails after the open, such
as EIO, also gives READ_ONLY_E instead of reading as a short record.

The TPM store makes the same distinction. Opening a record for reading
gives NOT_AVAILABLE_E only for an NV index that does not exist
(TPM_RC_HANDLE), and READ_ONLY_E for any other NV open failure; a failed
NV read gives READ_ONLY_E too. Before, every open failure read as a
missing record, and a read failure returned the negated TPM code, which
can alias a wolfCrypt error such as ASN_PARSE_E. A debug-build hook,
WP11_Test_TpmStoreOpenRc, fails the NV opens for the TPM token store test.
test_hss_verify_only_no_keygen, test_hss_mech_info, and
test_hss_empty_template_rejected asserted verify-only behaviour (keygen
rejected, no CKF_SIGN, empty public-key template rejected) that inverts once
--enable-lms-private is built. Branch these on WOLFPKCS11_LMS_PRIVATE and
the specific mechanism/key type, since XMSS stays verify-only in this build.
An HSS private key import is rejected with CKR_ATTRIBUTE_VALUE_INVALID in
both builds.
…cord

Add C_GenerateKeyPair(CKM_HSS_KEY_PAIR_GEN) and C_Sign(CKM_HSS) behind
--enable-lms-private (WOLFPKCS11_LMS_PRIVATE in CMake, EXPERIMENTAL, off by
default). It implies --enable-lms and needs a sign-capable wolfSSL LMS
build; configure checks for one instead of failing in the middle of hbs.c.

LMS/HSS is stateful: every one-time-signature leaf may be used once. The
whole design follows from never releasing a signature whose leaf could be
handed out again.

State in the key record

The record of an HSS private key holds a shell (parameters and public key)
and the wolfSSL private state sealed with AES-GCM. Every write draws a
random 16-byte salt and seals under HMAC-SHA256(token key, label || salt)
with a fixed nonce, so the token key never seals a record itself and the
number of signatures does not run into the limit of random GCM nonces under
one key. The AAD covers the shell, a SHA-256 digest of the policy
attributes and the signature counter, so editing the record, or the policy
in the object record, makes the key refuse to sign. The policy digest is
taken over a serialization of its own (class, key type, keygen mechanism,
local, the flags other than the trusted, TPM and runtime ones, and the
attribute templates) rather than the policy AAD of other keys, so a change
to that format leaves existing records readable. Keeping the state in the
ordinary key record means destroy, the id moves that destroy makes,
C_InitToken and the store batch handle it like any other record, and that a
custom store or the TPM store can hold it.

- Signing: wolfSSL hands the advanced state to the write callback before it
  releases a signature. The callback seals it and writes the key record
  durably, outside any store batch, and wolfSSL zeroizes the signature if
  that fails. A failure disables the key, and the next signature reloads
  it from the stored record, refusing a state behind the one in memory or a
  record that does not authenticate; that stored state never belongs to a
  released signature. Argument errors that use no leaf do not disable the
  key, unless wolfSSL marked the key state bad, and C_Sign refuses a data
  length over INT_MAX with CKR_DATA_LEN_RANGE before it uses one. On the
  file store C_Sign first checks that the store directory can be flushed,
  which the durable write needs, so a filesystem without directory
  flushing fails each C_Sign before it uses a leaf.
- Keygen requires a user login, or an empty-PIN token, because the state is
  sealed with the user's token key. MakeKey only buffers the first state;
  AddObject seals and stores it under the token lock, so a C_SetPIN cannot
  slip in between and leave the key sealed under the old token key, and a
  keygen that fails leaves nothing on storage.
- C_SetPIN re-seals the state in the same prepare/swap step that re-keys
  every other object's key data, and a policy change re-seals it in the
  store pass that writes the object record. Both first take over a newer
  stored record and re-seal it with the current policy, also when a
  policy re-seal left by a failed store pass is still pending. The C_SetPIN
  re-seal, and the binding of pending policy changes before it, run without
  the lock file, so another process can sign before the store pass; when
  the pass then finds a newer record than the re-sealed one, it re-seals
  that record under the lock, from the token key being replaced, which the
  pass is given. Whether a flag change needs the re-seal is decided with the
  flags the record binds, not those other keys bind, so the two cannot
  drift apart. A policy change on a key whose record does not load is
  refused, since that record cannot be re-sealed and every later store
  pass would fail on it.
- A key that has used every signature, or whose record does not load,
  stays visible: C_FindObjects returns it and C_DestroyObject removes it.
  C_Sign returns CKR_KEY_EXHAUSTED or CKR_DEVICE_ERROR, and
  CKA_KEYS_REMAINING reports 0 for a spent key, and the full count up
  to the CK_ULONG range otherwise. A KEY_EXHAUSTED_E from wolfSSL marks the
  key spent as well. A missing or short record no longer stops
  C_Initialize, and the key returns CKR_DEVICE_ERROR, also after a logout
  and login. CKR_KEY_EXHAUSTED (PKCS#11 3.1) is added to pkcs11.h.
- Logging in authenticates each key's record but leaves rebuilding the
  wolfSSL trees to the key's first signature, so a login does not take as
  long as generating every key on the token again.
- Private keys cannot be imported or copied: they report CKA_COPYABLE
  CK_FALSE, a keygen template asking for CK_TRUE is refused, and
  WP11_Object_Copy refuses them before its shortcut for undecoded keys.

Several processes and forked children

Each signature takes the store lock, which with the file store includes the
batch lock file that orders store passes across processes, and finishes an
interrupted pass. When the lock file cannot be taken the signature fails
rather than run without it. It then reads the stored record: when it holds
another key the stored layout has moved and the key is disabled; when its
counter is behind the state in memory, the state went back and the key is
disabled; when its counter is ahead, or the record was re-sealed at the
same count, another process changed it and the key reloads from it. A
reload that does not authenticate, for example after another process
changed the policy, disables the key instead of writing the old policy over
the new record.

Before a store pass writes a key record, it reads the record at the id the
key was last stored at. When that one is further on, the pass writes it
instead of the older copy in memory, and the next signature reloads from
it. When the key moved or was re-sealed, that record must be readable and
of the same key, or the store fails rather than write a copy that may be
older. A key still at its own id whose record is missing or damaged is left
as it is: the pass skips it, and neither C_SetPIN nor a policy change re-
seals it, so the key returns CKR_DEVICE_ERROR and the rest of the token
keeps working. A C_SetPIN that fails puts back the marks of the keys it re-
sealed, so those keys are not taken for ones that must be written. The id a
key was last stored at only moves once the pass that wrote it commits, so a
failed pass leaves later ones reading the record that is really there. So a
process holding an older copy, at C_Finalize or when a destroy moves the
key into a freed id, neither rolls the state back nor fails the pass, and
C_SetPIN re-seals the newer record. A key with no loaded record that moves
into a freed id overwrites the record found there, which belongs to the
destroyed key, in the same store batch, so a pass that fails leaves it in
place. A destroy commits the moved key at its new id first and removes its
records at the old id afterwards, outside the store batch; a process that
still knows the key by the old id could sign from that copy in between, or
for good when the removal fails, and the two copies would hand out the same
leaves. So the pass that writes a moved key at its new id also overwrites
its record at the old id, which lies past the token's objects, with one
that does not parse, and both commit together. When an earlier store pass
failed and left the stored ids behind the object list, a sign stores the
whole token first instead of writing to a wrong id. A durable write also
removes any copy of its record that an unfinished store pass staged, so
finishing that pass cannot rename an older state over it. Without the file
store there is no cross-process lock, which the README states.

Signing holds the token lock for reading, plus the store lock for the state
I/O, so operations on other objects no longer wait for an HSS signature.
Attribute reads of an HSS private key take the store lock too.

Other changes

- wp11_Object_Scrub frees the key when a login or logout fails part way,
  and unlinking an object disables its key under the token lock, so a sign
  already past the lookup does not write the state of a destroyed key.
- WP11_INIT_HSS_SIGN is a sign-class operation, so a second C_SignInit is
  refused with CKR_OPERATION_ACTIVE.
- The runtime state of a stateful key (loaded, spent, waiting for its trees)
  is a field of its own, guarded by the store lock, rather than opFlag bits
  that signing would change while attribute reads only hold the token lock.
- HSS keys get the usage defaults of ML-DSA keys: a generated private key
  can sign without CKA_SIGN in its template, and does not report decrypt,
  sign-recover or derive. Keygen refuses a CKA_VALUE in the public template,
  and fails with CKR_DEVICE_ERROR when the private key's record was not
  stored, for example with storage turned off.
- Keygen takes CKA_HSS_LEVELS and the per-level lists CKA_HSS_LMS_TYPES
  and CKA_HSS_LMOTS_TYPES from the private key template, as PKCS#11 3.1,
  3.2 and the 3.3 draft specify. wolfSSL uses one type at every level, so
  the entries of a list must be equal. HSS attributes in the public key
  template, and the single-value types in the private key template, are
  refused with CKR_TEMPLATE_INCONSISTENT; the public key gets them from
  the generated key. The private key object is created from the template
  without the HSS parameters, which cannot be checked before the key
  exists.
- CKA_KEYS_REMAINING, the PKCS#11 3.3 name, is added with
  CKA_HSS_KEYS_REMAINING as an alias.
- The TPM store sizes the HSS private key record like the other
  variable-size records.
- In stores whose records commit one at a time, the custom and TPM stores,
  a key's last stored id moves as soon as its record is written, so a pass
  that fails after overwriting the old record does not leave later passes
  reading it.
- A signature on a key that a logout scrubbed in between is refused rather
  than reloading the key.
- A private key that is not loaded, before login or after a logout, reports
  its parameter attributes from the shell in its record, and a logout does
  not treat a key without a loaded record as one it failed to protect.
- A record is never unsealed with an all-zero token key, as it is never
  sealed with one.
- --enable-lms-private and WOLFPKCS11_LMS_PRIVATE turn on LMS and reuse its
  configuration instead of repeating it.
- WOLFPKCS11_CUSTOM_STORE needs WOLFPKCS11_CUSTOM_STORE_CLOSE_COMMIT with
  LMS_PRIVATE, so a signature is released only once the store reports the
  commit. The close-commit custom store test now also builds in LMS private
  configurations; the legacy one cannot.
- hbs.c uses the AES-GCM helpers of internal.c, and a wp11_Slot_NewRng
  wrapper rather than exporting Rng_New.
… tests

hbs_private_test.c (gated on WOLFPKCS11_LMS_PRIVATE):

- keygen -> sign -> verify, reload with no leaf rollback, keygen template
  validation (parameters on the wrong template, type lists of the wrong
  length or with differing entries, and CKA_COPYABLE=TRUE),
  CKA_KEYS_REMAINING, import and copy refusal, the
  C_Sign length contract and keygen without a login
- exhaustion reports CKR_KEY_EXHAUSTED, and a spent key stays visible and
  destroyable without breaking login for the rest of the token
- C_SetPIN with keys loaded and not loaded, with a damaged key record, and
  overtaking a keygen (debug builds, through the AddObject hook)
- destroy of the key, of an older key that moves the newest one, and of the
  public half; C_InitToken removing the old records
- a key record corruption matrix (truncated, magic, parameters, policy
  digest, tag), each leaving the key visible, refusing to sign and
  destroyable, and an object record put back to an older policy
- a failed state write and the recovery that follows, an unclean exit
  after a signature (fork), a stale
  staged record with a commit marker, and a sign after a failed store pass
  (debug builds)
- two processes signing one key without sharing a leaf, a loaded key
  picking up a state a forked child advanced, a state moved back or another
  key's record at the key's place, a C_SetPIN store pass and a destroy that
  moves the key, both of which must keep the newer state another process
  stored, a policy change in another process that a stale signer must not
  overwrite, a damaged key moved into a destroyed key's id, a sign that
  fails without the lock file, and a process with an out-of-date token whose
  C_Finalize must not roll the state back, and a process still using the id
  a moved key left, with the removal of the old record failing, a policy
  change after another process signed, a store pass that cannot read the
  newer stored record, a policy change on a key whose record does not load,
  a key without a usable record after a logout and login, and keygen
  defaults, a public key value in the keygen template and keygen with
  storage turned off
- a key record lost while the key is loaded, with C_SetPIN, signing with
  another key and C_Finalize still working, also after a policy change on
  that key, and the parameter attributes of a key that is not loaded
- a sign after a destroy whose pass failed after staging the moved key, a
  failed destroy that must leave the record it would overwrite, and a
  failed C_SetPIN store pass after another process signed, a record
  lost after a failed C_SetPIN, and a C_SetPIN that takes over another
  process's record while a policy re-seal is pending (debug builds)
- the policy digest and the record key against known answers, and a
  C_SetPIN while another process signs between the re-key and the store
  pass, through a test hook (debug builds)
- a parameter matrix (L1/L2 x W1-W8 x H5/H10, a fast subset by default,
  WOLFPKCS11_HSS_FULL_MATRIX=1 for all) and a multi-level subtree rollover

hss_concurrency_test.c: four threads sign one token key while another reads
CKA_HSS_KEYS_REMAINING and CKA_SIGN; every RFC 8554 leaf index must be
unique and the count must only go down.

custom_store_test.c gains an HSS case for the close-commit custom store: a
destroy moves the key and the token record of that pass fails to commit,
and the key still signs, also after a reload.

Both tests are registered under autotools and CMake, and pick their own
token directory under ./store.
Add lms_private, lms_private_static, lms_private_debug (for the tests
that need the debug test hooks) and a small-memory WC_LMS variant
(lms_private_small) to unit-test.yml. Each passes --enable-lms in
wolfssl_config for a sign-capable wolfSSL LMS build; it follows the
base configure's --enable-lms=verify-only, so it takes effect. The
CMake job enables WOLFPKCS11_LMS_PRIVATE, and the sanitizer matrix
gains an LMS private entry that allows an empty user PIN, so
LeakSanitizer covers the empty-PIN token load and login.
…model

Add a README section for --enable-lms-private and the CMake option row:
why stateful keys are token-only, where the state lives and what it is bound
to, what the design detects and what it cannot (offline rollback of the
token directory), signing from several processes, spent and damaged keys,
and the requirements on custom and TPM stores.
@Frauschi
Frauschi force-pushed the lms-private-software branch from 77b7329 to 461ae5b Compare November 23, 2026 15:50
@LinuxJedi
LinuxJedi merged commit 40c8272 into wolfSSL:master Nov 24, 2026
121 checks passed
@Frauschi
Frauschi deleted the lms-private-software branch November 24, 2026 15:06
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.

5 participants