Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cryptoki/src/mechanism/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -938,6 +943,7 @@ impl TryFrom<CK_MECHANISM_TYPE> 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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1548,6 +1557,7 @@ impl From<&Mechanism<'_>> for CK_MECHANISM {
| Mechanism::AesEcb
| Mechanism::AesKeyWrap
| Mechanism::AesKeyWrapPad
| Mechanism::AesKeyWrapKwp
| Mechanism::AesCMac
| Mechanism::RsaPkcsKeyPairGen
| Mechanism::RsaPkcs
Expand Down
92 changes: 92 additions & 0 deletions cryptoki/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading