Skip to content

Add CKM_AES_KEY_WRAP_KWP (RFC 5649 key wrap with padding) - #376

Open
Princemachiavelli wants to merge 1 commit into
parallaxsecond:mainfrom
Princemachiavelli:jhoffer/Add-CKM_AES_KEY_WRAP_KWP
Open

Princemachiavelli wants to merge 1 commit into
parallaxsecond:mainfrom
Princemachiavelli:jhoffer/Add-CKM_AES_KEY_WRAP_KWP

Conversation

@Princemachiavelli

Copy link
Copy Markdown

PKCS#11 3.0 added CKM_AES_KEY_WRAP_KWP (0x210B) for AES key wrap with padding as specified in RFC 5649, because the 2.40 CKM_AES_KEY_WRAP_PAD (0x210A) was ambiguous about which padding it means. Modules implementing 3.x offer the new ID; kryoptic, for example, lists 0x210B (and 0x210C, CKM_AES_KEY_WRAP_PKCS7) but not 0x210A. cryptoki-sys already has the constant; cryptoki has no MechanismType or Mechanism for it, and a raw MechanismType can only be built in the vendor range, so the mechanism cannot be used.

This PR adds MechanismType::AES_KEY_WRAP_KWP (with its Display name and TryFrom arm) and a parameterless Mechanism::AesKeyWrapKwp, mirroring AES_KEY_WRAP_PAD.

We need it to move private keys between tokens: wrapped with RFC 5649 on one module and unwrapped on another. Between SoftHSM 2.7 (CKM_AES_KEY_WRAP_PAD) and kryoptic 1.5.4 (CKM_AES_KEY_WRAP_KWP) the wrapped bytes are interchangeable in both directions, for secret keys and for PKCS#8 private keys, so the two IDs name one format there, but the mechanism must be requested by the ID the module offers.

@wiktor-k

Copy link
Copy Markdown
Collaborator

Looks good in general. Would it be possible to add tests for using this mechanism? 🤔

PKCS#11 3.0 added `CKM_AES_KEY_WRAP_KWP` (0x210B) for AES key wrap with padding as specified in
RFC 5649, because the 2.40 `CKM_AES_KEY_WRAP_PAD` (0x210A) was ambiguous about which padding it
means. Modules implementing 3.x offer the new ID; kryoptic, for example, lists 0x210B (and 0x210C,
`CKM_AES_KEY_WRAP_PKCS7`) but not 0x210A. `cryptoki-sys` already has the constant; `cryptoki` has
no `MechanismType` or `Mechanism` for it, and a raw `MechanismType` can only be built in the vendor
range, so the mechanism cannot be used.

This PR adds `MechanismType::AES_KEY_WRAP_KWP` (with its `Display` name and `TryFrom` arm) and a
parameterless `Mechanism::AesKeyWrapKwp`, mirroring `AES_KEY_WRAP_PAD`. A wrap/unwrap test runs
where the token offers the mechanism (kryoptic) and is skipped elsewhere (SoftHSM).

We need it to move private keys between tokens: wrapped with RFC 5649 on one module and unwrapped on
another. Between SoftHSM 2.7 (`CKM_AES_KEY_WRAP_PAD`) and kryoptic 1.5.4 (`CKM_AES_KEY_WRAP_KWP`) the
wrapped bytes are interchangeable in both directions, for secret keys and for PKCS#8 private keys,
so the two IDs name one format there, but the mechanism must be requested by the ID the module
offers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Josh Hoffer <jhoffer@sansorgan.es>
@Princemachiavelli
Princemachiavelli force-pushed the jhoffer/Add-CKM_AES_KEY_WRAP_KWP branch from 26925d0 to 2cde156 Compare November 15, 2026 19:21
@Princemachiavelli

Copy link
Copy Markdown
Author

@wiktor-k Yup, added test to this and also it's companion PR #377.

I'll give both a second look over when I get home in a bit. With these mechanisms added, we should be able to natively migrate objects between common cloud HSM providers. (I think I have already tested... AWS <-> GCP HSM should work).

@wiktor-k

Copy link
Copy Markdown
Collaborator

With these mechanisms added, we should be able to natively migrate objects between common cloud HSM providers. (I think I have already tested... AWS <-> GCP HSM should work).

That's really cool! I'm looking forward to this getting merged! It seems we've got a minor issue with the CI but otherwise I like it 👏

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.

2 participants