Skip to content

Commit c68fdf2

Browse files
authored
Merge pull request #181 from xaqq/main
Expose AES-CMAC algorithm
2 parents d7ea453 + bd5ded9 commit c68fdf2

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

cryptoki/src/mechanism/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ impl MechanismType {
7171
val: CKM_AES_CBC_ENCRYPT_DATA,
7272
};
7373

74+
/// AES-CMAC mechanism
75+
pub const AES_CMAC: MechanismType = MechanismType { val: CKM_AES_CMAC };
76+
7477
// RSA
7578
/// PKCS #1 RSA key pair generation mechanism
7679
pub const RSA_PKCS_KEY_PAIR_GEN: MechanismType = MechanismType {
@@ -710,6 +713,8 @@ pub enum Mechanism<'a> {
710713
/// For derivation, the message length must be a multiple of the block
711714
/// size. See <https://www.cryptsoft.com/pkcs11doc/v220/>.
712715
AesCbcEncryptData(ekdf::AesCbcDeriveParams<'a>),
716+
/// AES CMAC
717+
AesCMac,
713718

714719
// RSA
715720
/// PKCS #1 RSA key pair generation mechanism
@@ -854,6 +859,7 @@ impl Mechanism<'_> {
854859
Mechanism::AesKeyWrapPad => MechanismType::AES_KEY_WRAP_PAD,
855860
Mechanism::AesGcm(_) => MechanismType::AES_GCM,
856861
Mechanism::AesCbcEncryptData(_) => MechanismType::AES_CBC_ENCRYPT_DATA,
862+
Mechanism::AesCMac => MechanismType::AES_CMAC,
857863
Mechanism::RsaPkcsKeyPairGen => MechanismType::RSA_PKCS_KEY_PAIR_GEN,
858864
Mechanism::RsaPkcs => MechanismType::RSA_PKCS,
859865
Mechanism::RsaPkcsPss(_) => MechanismType::RSA_PKCS_PSS,
@@ -936,6 +942,7 @@ impl From<&Mechanism<'_>> for CK_MECHANISM {
936942
| Mechanism::AesEcb
937943
| Mechanism::AesKeyWrap
938944
| Mechanism::AesKeyWrapPad
945+
| Mechanism::AesCMac
939946
| Mechanism::RsaPkcsKeyPairGen
940947
| Mechanism::RsaPkcs
941948
| Mechanism::RsaX509

cryptoki/tests/basic.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,3 +1187,71 @@ fn ekdf_aes_cbc_encrypt_data() -> TestResult {
11871187

11881188
Ok(())
11891189
}
1190+
1191+
#[test]
1192+
#[serial]
1193+
fn aes_cmac_sign() -> TestResult {
1194+
let (pkcs11, slot) = init_pins();
1195+
let session = pkcs11.open_rw_session(slot)?;
1196+
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
1197+
let key: [u8; 16] = [
1198+
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f,
1199+
0x3c,
1200+
];
1201+
let message: [u8; 16] = [
1202+
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17,
1203+
0x2a,
1204+
];
1205+
let expected_mac: [u8; 16] = [
1206+
0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28,
1207+
0x7c,
1208+
];
1209+
1210+
let key_template = vec![
1211+
Attribute::Class(ObjectClass::SECRET_KEY),
1212+
Attribute::KeyType(KeyType::AES),
1213+
Attribute::Token(true),
1214+
Attribute::Sensitive(true),
1215+
Attribute::Private(true),
1216+
Attribute::Value(key.into()),
1217+
Attribute::Sign(true),
1218+
];
1219+
let key = session.create_object(&key_template)?;
1220+
let signature = session.sign(&Mechanism::AesCMac, key, &message)?;
1221+
1222+
assert_eq!(expected_mac.as_slice(), signature.as_slice());
1223+
Ok(())
1224+
}
1225+
1226+
#[test]
1227+
#[serial]
1228+
fn aes_cmac_verify() -> TestResult {
1229+
let (pkcs11, slot) = init_pins();
1230+
let session = pkcs11.open_rw_session(slot)?;
1231+
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
1232+
let key: [u8; 16] = [
1233+
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f,
1234+
0x3c,
1235+
];
1236+
let message: [u8; 16] = [
1237+
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17,
1238+
0x2a,
1239+
];
1240+
let expected_mac: [u8; 16] = [
1241+
0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28,
1242+
0x7c,
1243+
];
1244+
1245+
let key_template = vec![
1246+
Attribute::Class(ObjectClass::SECRET_KEY),
1247+
Attribute::KeyType(KeyType::AES),
1248+
Attribute::Token(true),
1249+
Attribute::Sensitive(true),
1250+
Attribute::Private(true),
1251+
Attribute::Value(key.into()),
1252+
Attribute::Verify(true),
1253+
];
1254+
let key = session.create_object(&key_template)?;
1255+
session.verify(&Mechanism::AesCMac, key, &message, &expected_mac)?;
1256+
Ok(())
1257+
}

0 commit comments

Comments
 (0)