Skip to content

wolfcrypt/src/wc_pkcs11.c: cache PKCS#11 session across multi-call HMAC#10335

Open
julek-wolfssl wants to merge 1 commit intowolfSSL:masterfrom
julek-wolfssl:pkcs11-hmac-session
Open

wolfcrypt/src/wc_pkcs11.c: cache PKCS#11 session across multi-call HMAC#10335
julek-wolfssl wants to merge 1 commit intowolfSSL:masterfrom
julek-wolfssl:pkcs11-hmac-session

Conversation

@julek-wolfssl
Copy link
Copy Markdown
Member

The cryptocb dispatcher opened and closed a fresh PKCS#11 session around
each HMAC invocation. PKCS#11 sign operations are session-scoped, so a
multi-call HMAC (wc_HmacUpdate then wc_HmacFinal, which arrive as
separate cryptocb dispatches) had its C_SignFinal land on a session
that never saw a C_SignInit, returning CKR_OPERATION_NOT_INITIALIZED
and surfacing as WC_HW_E. This broke any code path that drives Update
and Final separately under PKCS#11 routing.

Cache the PKCS#11 session handle on Hmac.devCtx (cast through wc_ptr_t,
matching the existing pattern for cached PKCS#11 object handles) and
rebuild the Pkcs11Session on the stack. The session is opened on the
first dispatch when the operation enters
WC_HMAC_INNER_HASH_KEYED_DEV state and released when it leaves that
state (Final completed or hard error).

@julek-wolfssl
Copy link
Copy Markdown
Member Author

Tested in wolfSSL/wolfPKCS11#181

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes PKCS#11-routed multi-call HMAC failures by keeping the same PKCS#11 session alive across separate wc_HmacUpdate / wc_HmacFinal CryptoCB dispatches, avoiding C_SignFinal running on a different session than C_SignInit.

Changes:

  • Cache the PKCS#11 CK_SESSION_HANDLE in Hmac.devCtx for the duration of a device-backed HMAC operation.
  • Reconstruct a Pkcs11Session on the stack from the cached handle on subsequent dispatches, and close/clear the cached session when leaving device-keyed state or on error.

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

Comment thread wolfcrypt/src/wc_pkcs11.c
Comment thread wolfcrypt/src/wc_pkcs11.c
The cryptocb dispatcher opened and closed a fresh PKCS#11 session around
each HMAC invocation. PKCS#11 sign operations are session-scoped, so a
multi-call HMAC (wc_HmacUpdate then wc_HmacFinal, which arrive as
separate cryptocb dispatches) had its C_SignFinal land on a session
that never saw a C_SignInit, returning CKR_OPERATION_NOT_INITIALIZED
and surfacing as WC_HW_E. This broke any code path that drives Update
and Final separately under PKCS#11 routing.

Cache the PKCS#11 session handle on Hmac.devCtx (cast through wc_ptr_t,
matching the existing pattern for cached PKCS#11 object handles) and
rebuild the Pkcs11Session on the stack. The session is opened on the
first dispatch when the operation enters
WC_HMAC_INNER_HASH_KEYED_DEV state and released when it leaves that
state (Final completed or hard error).
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 28, 2026

MemBrowse Memory Report

No memory changes detected for:

@julek-wolfssl
Copy link
Copy Markdown
Member Author

retest this please flaky test

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