Skip to content

Commit df3a0a9

Browse files
authored
Merge pull request #183 from jippeholwerda/main
Add SHA256-HMAC mechanism
2 parents c68fdf2 + 84d569a commit df3a0a9

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

cryptoki/src/mechanism/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ impl MechanismType {
251251
pub const SHA512_RSA_PKCS_PSS: MechanismType = MechanismType {
252252
val: CKM_SHA512_RSA_PKCS_PSS,
253253
};
254+
/// SHA256-HMAC mechanism
255+
pub const SHA256_HMAC: MechanismType = MechanismType {
256+
val: CKM_SHA256_HMAC,
257+
};
254258
/// GENERIC-SECRET-KEY-GEN mechanism
255259
pub const GENERIC_SECRET_KEY_GEN: MechanismType = MechanismType {
256260
val: CKM_GENERIC_SECRET_KEY_GEN,
@@ -663,6 +667,7 @@ impl TryFrom<CK_MECHANISM_TYPE> for MechanismType {
663667
CKM_SHA256_RSA_PKCS => Ok(MechanismType::SHA256_RSA_PKCS),
664668
CKM_SHA384_RSA_PKCS => Ok(MechanismType::SHA384_RSA_PKCS),
665669
CKM_SHA512_RSA_PKCS => Ok(MechanismType::SHA512_RSA_PKCS),
670+
CKM_SHA256_HMAC => Ok(MechanismType::SHA256_HMAC),
666671
CKM_GENERIC_SECRET_KEY_GEN => Ok(MechanismType::GENERIC_SECRET_KEY_GEN),
667672
other => {
668673
error!("Mechanism type {} is not supported.", other);
@@ -842,7 +847,8 @@ pub enum Mechanism<'a> {
842847
Sha384RsaPkcsPss(rsa::PkcsPssParams),
843848
/// SHA256-RSA-PKCS-PSS mechanism
844849
Sha512RsaPkcsPss(rsa::PkcsPssParams),
845-
850+
/// SHA256-HMAC mechanism
851+
Sha256Hmac,
846852
/// GENERIC-SECRET-KEY-GEN mechanism
847853
GenericSecretKeyGen,
848854
}
@@ -905,6 +911,8 @@ impl Mechanism<'_> {
905911
Mechanism::Sha384RsaPkcsPss(_) => MechanismType::SHA384_RSA_PKCS_PSS,
906912
Mechanism::Sha512RsaPkcsPss(_) => MechanismType::SHA512_RSA_PKCS_PSS,
907913

914+
Mechanism::Sha256Hmac => MechanismType::SHA256_HMAC,
915+
908916
Mechanism::GenericSecretKeyGen => MechanismType::GENERIC_SECRET_KEY_GEN,
909917
}
910918
}
@@ -971,6 +979,7 @@ impl From<&Mechanism<'_>> for CK_MECHANISM {
971979
| Mechanism::Sha256RsaPkcs
972980
| Mechanism::Sha384RsaPkcs
973981
| Mechanism::Sha512RsaPkcs
982+
| Mechanism::Sha256Hmac
974983
| Mechanism::GenericSecretKeyGen => CK_MECHANISM {
975984
mechanism,
976985
pParameter: null_mut(),

cryptoki/tests/basic.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,3 +1255,32 @@ fn aes_cmac_verify() -> TestResult {
12551255
session.verify(&Mechanism::AesCMac, key, &message, &expected_mac)?;
12561256
Ok(())
12571257
}
1258+
1259+
#[test]
1260+
#[serial]
1261+
fn sign_verify_sha256_hmac() -> TestResult {
1262+
let (pkcs11, slot) = init_pins();
1263+
let session = pkcs11.open_rw_session(slot)?;
1264+
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
1265+
1266+
let priv_key_template = vec![
1267+
Attribute::Token(true),
1268+
Attribute::Private(true),
1269+
Attribute::Sensitive(true),
1270+
Attribute::Sign(true),
1271+
Attribute::KeyType(KeyType::GENERIC_SECRET),
1272+
Attribute::Class(ObjectClass::SECRET_KEY),
1273+
Attribute::ValueLen(256.into()),
1274+
];
1275+
1276+
let private = session.generate_key(&Mechanism::GenericSecretKeyGen, &priv_key_template)?;
1277+
1278+
let data = vec![0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
1279+
1280+
let signature = session.sign(&Mechanism::Sha256Hmac, private, &data)?;
1281+
1282+
session.verify(&Mechanism::Sha256Hmac, private, &data, &signature)?;
1283+
1284+
session.destroy_object(private)?;
1285+
Ok(())
1286+
}

0 commit comments

Comments
 (0)