From 2cde1569cb20fb5748ea1d08a7b9cb54a832c7fb Mon Sep 17 00:00:00 2001 From: Josh Hoffer Date: Fri, 13 Nov 2026 20:06:01 -0800 Subject: [PATCH] Add `CKM_AES_KEY_WRAP_KWP` (RFC 5649 key wrap with padding) 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 Signed-off-by: Josh Hoffer --- cryptoki/src/mechanism/mod.rs | 10 ++++ cryptoki/tests/basic.rs | 92 +++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/cryptoki/src/mechanism/mod.rs b/cryptoki/src/mechanism/mod.rs index 5db0e73b..594615dd 100644 --- a/cryptoki/src/mechanism/mod.rs +++ b/cryptoki/src/mechanism/mod.rs @@ -74,6 +74,10 @@ impl MechanismType { pub const AES_KEY_WRAP_PAD: MechanismType = MechanismType { val: CKM_AES_KEY_WRAP_PAD, }; + /// AES key wrap with padding as specified in RFC 5649 (PKCS#11 3.0 `CKM_AES_KEY_WRAP_KWP`). + pub const AES_KEY_WRAP_KWP: MechanismType = MechanismType { + val: CKM_AES_KEY_WRAP_KWP, + }; /// AES-CTR mechanism pub const AES_CTR: MechanismType = MechanismType { val: CKM_AES_CTR }; /// AES-GCM mechanism @@ -859,6 +863,7 @@ impl MechanismType { } CKM_AES_KEY_WRAP => String::from(stringify!(CKM_AES_KEY_WRAP)), CKM_AES_KEY_WRAP_PAD => String::from(stringify!(CKM_AES_KEY_WRAP_PAD)), + CKM_AES_KEY_WRAP_KWP => String::from(stringify!(CKM_AES_KEY_WRAP_KWP)), CKM_RSA_PKCS_TPM_1_1 => String::from(stringify!(CKM_RSA_PKCS_TPM_1_1)), CKM_RSA_PKCS_OAEP_TPM_1_1 => String::from(stringify!(CKM_RSA_PKCS_OAEP_TPM_1_1)), CKM_EC_EDWARDS_KEY_PAIR_GEN => String::from(stringify!(CKM_EC_EDWARDS_KEY_PAIR_GEN)), @@ -938,6 +943,7 @@ impl TryFrom for MechanismType { CKM_AES_ECB => Ok(MechanismType::AES_ECB), CKM_AES_KEY_WRAP => Ok(MechanismType::AES_KEY_WRAP), CKM_AES_KEY_WRAP_PAD => Ok(MechanismType::AES_KEY_WRAP_PAD), + CKM_AES_KEY_WRAP_KWP => Ok(MechanismType::AES_KEY_WRAP_KWP), CKM_AES_CTR => Ok(MechanismType::AES_CTR), CKM_AES_GCM => Ok(MechanismType::AES_GCM), CKM_AES_CBC_ENCRYPT_DATA => Ok(MechanismType::AES_CBC_ENCRYPT_DATA), @@ -1067,6 +1073,8 @@ pub enum Mechanism<'a> { AesKeyWrap, /// AES key wrap with padding block AesKeyWrapPad, + /// AES key wrap with padding as specified in RFC 5649 + AesKeyWrapKwp, /// AES-GCM mechanism AesGcm(aead::GcmParams<'a>), /// AES-GCM mechanism with message based API and parameters @@ -1347,6 +1355,7 @@ impl Mechanism<'_> { Mechanism::AesCtr(_) => MechanismType::AES_CTR, Mechanism::AesKeyWrap => MechanismType::AES_KEY_WRAP, Mechanism::AesKeyWrapPad => MechanismType::AES_KEY_WRAP_PAD, + Mechanism::AesKeyWrapKwp => MechanismType::AES_KEY_WRAP_KWP, Mechanism::AesGcm(_) => MechanismType::AES_GCM, Mechanism::AesGcmMessage(_) => MechanismType::AES_GCM, Mechanism::AesCbcEncryptData(_) => MechanismType::AES_CBC_ENCRYPT_DATA, @@ -1548,6 +1557,7 @@ impl From<&Mechanism<'_>> for CK_MECHANISM { | Mechanism::AesEcb | Mechanism::AesKeyWrap | Mechanism::AesKeyWrapPad + | Mechanism::AesKeyWrapKwp | Mechanism::AesCMac | Mechanism::RsaPkcsKeyPairGen | Mechanism::RsaPkcs diff --git a/cryptoki/tests/basic.rs b/cryptoki/tests/basic.rs index 0aa246b3..22eb8521 100644 --- a/cryptoki/tests/basic.rs +++ b/cryptoki/tests/basic.rs @@ -1718,6 +1718,98 @@ fn wrap_and_unwrap_key_oaep() { pkcs11.finalize().unwrap(); } +#[test] +#[serial] +fn wrap_and_unwrap_key_kwp() { + let (pkcs11, slot) = init_pins(); + + // SoftHSM offers RFC 5649 as CKM_AES_KEY_WRAP_PAD only + if !pkcs11 + .get_mechanism_list(slot) + .unwrap() + .contains(&MechanismType::AES_KEY_WRAP_KWP) + { + eprintln!("Skipping test: the token does not offer CKM_AES_KEY_WRAP_KWP"); + pkcs11.finalize().unwrap(); + return; + } + + // open a session + let session = pkcs11.open_rw_session(slot).unwrap(); + + // log in the session + session + .login(UserType::User, Some(&AuthPin::new(USER_PIN.into()))) + .unwrap(); + + let key_to_be_wrapped_template = vec![ + Attribute::Token(true), + Attribute::ValueLen(32.into()), + // the key needs to be extractable to be suitable for being wrapped + Attribute::Extractable(true), + Attribute::Encrypt(true), + ]; + + // generate a secret key that will be wrapped + let key_to_be_wrapped = session + .generate_key(&Mechanism::AesKeyGen, &key_to_be_wrapped_template) + .unwrap(); + + // AesEcb input length must be a multiple of 16 + let encrypted_with_original = session + .encrypt( + &Mechanism::AesEcb, + key_to_be_wrapped, + &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], + ) + .unwrap(); + + // the wrapping key needs the "wrap" and "unwrap" attributes + let wrapping_key_template = vec![ + Attribute::Token(true), + Attribute::ValueLen(32.into()), + Attribute::Wrap(true), + Attribute::Unwrap(true), + ]; + + let wrapping_key = session + .generate_key(&Mechanism::AesKeyGen, &wrapping_key_template) + .unwrap(); + + let wrapped_key = session + .wrap_key(&Mechanism::AesKeyWrapKwp, wrapping_key, key_to_be_wrapped) + .unwrap(); + // the 32-byte key, padded and wrapped: 32 + 8 + assert_eq!(wrapped_key.len(), 40); + + let unwrapped_key = session + .unwrap_key( + &Mechanism::AesKeyWrapKwp, + wrapping_key, + &wrapped_key, + &[ + Attribute::Token(true), + Attribute::Private(true), + Attribute::Encrypt(true), + Attribute::Class(ObjectClass::SECRET_KEY), + Attribute::KeyType(KeyType::AES), + ], + ) + .unwrap(); + + let encrypted_with_unwrapped = session + .encrypt( + &Mechanism::AesEcb, + unwrapped_key, + &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], + ) + .unwrap(); + assert_eq!(encrypted_with_original, encrypted_with_unwrapped); + + session.close().unwrap(); + pkcs11.finalize().unwrap(); +} + #[test] #[serial] fn login_feast() {