Skip to content

add support for PKCS11 v3.2 - #191

Draft
Nicolas-Peiffer wants to merge 41 commits into
miekg:masterfrom
ThalesGroup:pkcs11-v3.2-pr
Draft

add support for PKCS11 v3.2#191
Nicolas-Peiffer wants to merge 41 commits into
miekg:masterfrom
ThalesGroup:pkcs11-v3.2-pr

Conversation

@Nicolas-Peiffer

@Nicolas-Peiffer Nicolas-Peiffer commented Apr 22, 2026

Copy link
Copy Markdown
  • Add support for PKCS11 v3.2
  • Use softhsmv3 for integration testing
  • Update files headers with SPDX licence ID
  • Update files headers copyrights
  • Update Github Action CICD

Related

#189

Tests

Tests are passing with softhsmv3:

Copie d'écran_20260422_181329

Waiting on your feedbacks, testing and improvements 😄

miekg and others added 30 commits January 4, 2022 14:09
proof of concept to upgrade to v3 of pkcs11

Signed-off-by: Miek Gieben <miek@miek.nl>
Signed-off-by: Miek Gieben <miek@miek.nl>
… CKA_TRUST_EMAIL_PROTECTION, CKA_TRUST_TIME_STAMPING, and CKA_TRUST_OCSP_SIGNING are now defined as standard PKCS#11 v3.2 attributes in pkcs11t.h/zconst.go.
…golang.org/x/tools/go/analysis/passes/printf

Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com>
C_AsyncComplete allocates CK_ASYNC_DATA.pValue on the token side. The caller is responsible for freeing it. Add defer C.free() after copying the bytes into Go-managed memory via C.GoBytes, preventing a memory leak on every successful async result retrieval.

Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com>
Add Encapsulate/Decapsulate on PublicKey/PrivateKey to expose the
PKCS miekg#11 v3.2 ML-KEM key encapsulation API (§5.19) at the p11 layer.
Add VerifyStateless on PublicKey for the v3.2 stateless signature
verification flow (§5.15) where the signature is bound at init time.
Add KEMKeyPair to give callers a typed container for KEM key pairs.

Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com>
TestV32AuthenticatedWrap — renamed to skippedTestV32AuthenticatedWrap with a //nolint:unused directive and a TODO comment explaining the SoftHSMv3 bug (wrap produces the right blob size but unwrap returns wrong key value). The test body is preserved in full so it can be re-enabled once the upstream issue is fixed.

TestV32GetSessionValidationFlags — new test for C_GetSessionValidationFlags (v3.2 §5.8.4). Performs an RSA sign, then queries the FIPS validation status of the session. Skips gracefully if the token doesn't support it (acceptable for non-FIPS mode).

TestV32PQCParamSetReadback — new table-driven test that verifies CKA_PARAMETER_SET is correctly stored and returned by C_GetAttributeValue for all three PQC families (ML-KEM-768, ML-DSA-65, SLH-DSA-SHA2-128s). This exercises the v3.2 attribute infrastructure and ensures key templates are persisted correctly — useful because a broken attribute store would silently break all PQC key usage downstream.

Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com>

nolint
@Nicolas-Peiffer
Nicolas-Peiffer force-pushed the pkcs11-v3.2-pr branch 2 times, most recently from a21ac90 to 042baa6 Compare April 22, 2026 16:51
Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com>
Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com>
Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com>
@Nicolas-Peiffer
Nicolas-Peiffer marked this pull request as ready for review April 23, 2026 09:29
@Nicolas-Peiffer
Nicolas-Peiffer marked this pull request as draft April 23, 2026 14:33
Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com>
…atest SoftHSMv3

needMech now probes via GetMechanismList instead of GetMechanismInfo; newer SoftHSMv3 returns CKR_MECHANISM_INVALID from C_GetMechanismInfo for mechanisms that are present in C_GetMechanismList.

TestPSSParams switches from CKM_RSA_PKCS_PSS (raw pre-hashed digest) to CKM_SHA256_RSA_PKCS_PSS (hash-then-sign); the raw PSS mechanism is no longer advertised by the latest SoftHSMv3.

TestOAEPParams was failing with CKR_KEY_FUNCTION_NOT_PERMITTED at EncryptInit because getRSA delegated to generateRSAKeyPair which only sets CKA_VERIFY/CKA_SIGN. getRSA now generates the key pair inline with CKA_ENCRYPT and CKA_DECRYPT set. Also fixes a typo in getRSA where the private key lookup used CKO_PUBLIC_KEY.

Tested with pqctoday-org/pqctoday-hsm@37bf66a

Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com>
Tested with pqctoday-org/pqctoday-hsm@37bf66a

Signed-off-by: Nicolas-Peiffer <102670102+Nicolas-Peiffer@users.noreply.github.com>
Comment thread params.go
{
params->pOAEPParams = pOAEPParams;
}
*/

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing bindings for CK_SIGN_ADDITIONAL_CONTEXT. Should have a C method to populate this struct and Go methods to create it.

ML-DSA Signature
...
The mechanism has an optional parameter CK_SIGN_ADDITIONAL_CONTEXT. If no parameter is supplied the HedgeVariant is set to CKH_HEDGE_PREFERRED, ulContextLen is set to zero and pContext is set to NULL.

https://www.thalesdocs.com/gphsm/luna/7/docs/network/Content/sdk/extensions/pqc/ML-DSA_programming_guide.htm

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi, TY @spencerpogo for your review.
I tried to cover your request in commit ed5b5af. To be transparent, I used a LLM to take into account your comment.

My current level of confidence is the tests that are passing. But I do not know if this is enough.

image

Comment thread types.go Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Need support for ML-DSA signing context here

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

As per message #191 (comment) and commit ed5b5af

Comment thread types.go
param, arena = cECDH1DeriveParams(p, arena)
case *RSAAESKeyWrapParams:
param, arena = cRSAAESKeyWrapParams(p, arena)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Need support for ML-DSA signing context here

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

As per message #191 (comment) and commit ed5b5af

Comment thread LICENSE Outdated
Implement Go wrappers for the two PKCS#11 v3.2 ML-DSA mechanism
parameter structures:

- MLDSAParams (CK_SIGN_ADDITIONAL_CONTEXT): hedge variant + optional
  context bytes, for use with CKM_ML_DSA.
- HashMLDSAParams (CK_HASH_SIGN_ADDITIONAL_CONTEXT): hedge variant +
  optional context bytes + hash mechanism, for use with CKM_HASH_ML_DSA
  and its SHA/SHAKE variants.

Both types are accepted by NewMechanism and serialized to their C
counterparts in cMechanism, following the same pointer-deferral pattern
used by OAEPParams and RSAAESKeyWrapParams.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

CC
miekg#191 (comment)
miekg#191 (comment)
miekg#191 (comment)
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