Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SHA256-HMAC mechanism #183

Merged
merged 1 commit into from
Nov 9, 2023
Merged
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
11 changes: 10 additions & 1 deletion cryptoki/src/mechanism/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ impl MechanismType {
pub const SHA512_RSA_PKCS_PSS: MechanismType = MechanismType {
val: CKM_SHA512_RSA_PKCS_PSS,
};
/// SHA256-HMAC mechanism
pub const SHA256_HMAC: MechanismType = MechanismType {
val: CKM_SHA256_HMAC,
};
/// GENERIC-SECRET-KEY-GEN mechanism
pub const GENERIC_SECRET_KEY_GEN: MechanismType = MechanismType {
val: CKM_GENERIC_SECRET_KEY_GEN,
Expand Down Expand Up @@ -663,6 +667,7 @@ impl TryFrom<CK_MECHANISM_TYPE> for MechanismType {
CKM_SHA256_RSA_PKCS => Ok(MechanismType::SHA256_RSA_PKCS),
CKM_SHA384_RSA_PKCS => Ok(MechanismType::SHA384_RSA_PKCS),
CKM_SHA512_RSA_PKCS => Ok(MechanismType::SHA512_RSA_PKCS),
CKM_SHA256_HMAC => Ok(MechanismType::SHA256_HMAC),
CKM_GENERIC_SECRET_KEY_GEN => Ok(MechanismType::GENERIC_SECRET_KEY_GEN),
other => {
error!("Mechanism type {} is not supported.", other);
Expand Down Expand Up @@ -842,7 +847,8 @@ pub enum Mechanism<'a> {
Sha384RsaPkcsPss(rsa::PkcsPssParams),
/// SHA256-RSA-PKCS-PSS mechanism
Sha512RsaPkcsPss(rsa::PkcsPssParams),

/// SHA256-HMAC mechanism
Sha256Hmac,
/// GENERIC-SECRET-KEY-GEN mechanism
GenericSecretKeyGen,
}
Expand Down Expand Up @@ -905,6 +911,8 @@ impl Mechanism<'_> {
Mechanism::Sha384RsaPkcsPss(_) => MechanismType::SHA384_RSA_PKCS_PSS,
Mechanism::Sha512RsaPkcsPss(_) => MechanismType::SHA512_RSA_PKCS_PSS,

Mechanism::Sha256Hmac => MechanismType::SHA256_HMAC,

Mechanism::GenericSecretKeyGen => MechanismType::GENERIC_SECRET_KEY_GEN,
}
}
Expand Down Expand Up @@ -971,6 +979,7 @@ impl From<&Mechanism<'_>> for CK_MECHANISM {
| Mechanism::Sha256RsaPkcs
| Mechanism::Sha384RsaPkcs
| Mechanism::Sha512RsaPkcs
| Mechanism::Sha256Hmac
| Mechanism::GenericSecretKeyGen => CK_MECHANISM {
mechanism,
pParameter: null_mut(),
Expand Down
29 changes: 29 additions & 0 deletions cryptoki/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1255,3 +1255,32 @@ fn aes_cmac_verify() -> TestResult {
session.verify(&Mechanism::AesCMac, key, &message, &expected_mac)?;
Ok(())
}

#[test]
#[serial]
fn sign_verify_sha256_hmac() -> TestResult {
let (pkcs11, slot) = init_pins();
let session = pkcs11.open_rw_session(slot)?;
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;

let priv_key_template = vec![
Attribute::Token(true),
Attribute::Private(true),
Attribute::Sensitive(true),
Attribute::Sign(true),
Attribute::KeyType(KeyType::GENERIC_SECRET),
Attribute::Class(ObjectClass::SECRET_KEY),
Attribute::ValueLen(256.into()),
];

let private = session.generate_key(&Mechanism::GenericSecretKeyGen, &priv_key_template)?;

let data = vec![0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];

let signature = session.sign(&Mechanism::Sha256Hmac, private, &data)?;

session.verify(&Mechanism::Sha256Hmac, private, &data, &signature)?;

session.destroy_object(private)?;
Ok(())
}
Loading