@@ -1197,22 +1197,89 @@ fn ekdf_aes_cbc_encrypt_data() -> TestResult {
11971197
11981198#[ test]
11991199#[ serial]
1200- fn aes_cmac_sign ( ) -> TestResult {
1200+ fn sign_verify_sha256_hmac ( ) -> TestResult {
12011201 let ( pkcs11, slot) = init_pins ( ) ;
12021202 let session = pkcs11. open_rw_session ( slot) ?;
12031203 session. login ( UserType :: User , Some ( & AuthPin :: new ( USER_PIN . into ( ) ) ) ) ?;
1204+
1205+ let priv_key_template = vec ! [
1206+ Attribute :: Token ( true ) ,
1207+ Attribute :: Private ( true ) ,
1208+ Attribute :: Sensitive ( true ) ,
1209+ Attribute :: Sign ( true ) ,
1210+ Attribute :: KeyType ( KeyType :: GENERIC_SECRET ) ,
1211+ Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
1212+ Attribute :: ValueLen ( 256 . into( ) ) ,
1213+ ] ;
1214+
1215+ let private = session. generate_key ( & Mechanism :: GenericSecretKeyGen , & priv_key_template) ?;
1216+
1217+ let data = vec ! [ 0xAA , 0xBB , 0xCC , 0xDD , 0xEE , 0xFF ] ;
1218+
1219+ let signature = session. sign ( & Mechanism :: Sha256Hmac , private, & data) ?;
1220+
1221+ session. verify ( & Mechanism :: Sha256Hmac , private, & data, & signature) ?;
1222+
1223+ session. destroy_object ( private) ?;
1224+ Ok ( ( ) )
1225+ }
1226+
1227+ /// AES-CMAC test vectors from RFC 4493
1228+ #[ test]
1229+ #[ serial]
1230+ fn aes_cmac_sign ( ) -> TestResult {
12041231 let key: [ u8 ; 16 ] = [
12051232 0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f ,
12061233 0x3c ,
12071234 ] ;
1208- let message: [ u8 ; 16 ] = [
1235+
1236+ let message_len0: [ u8 ; 0 ] = [ ] ;
1237+ let expected_mac_len0: [ u8 ; 16 ] = [
1238+ 0xbb , 0x1d , 0x69 , 0x29 , 0xe9 , 0x59 , 0x37 , 0x28 , 0x7f , 0xa3 , 0x7d , 0x12 , 0x9b , 0x75 , 0x67 ,
1239+ 0x46 ,
1240+ ] ;
1241+ aes_cmac_sign_impl ( key, & message_len0, expected_mac_len0) ?;
1242+
1243+ let message_len16: [ u8 ; 16 ] = [
12091244 0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 ,
12101245 0x2a ,
12111246 ] ;
1212- let expected_mac : [ u8 ; 16 ] = [
1247+ let expected_mac_len16 : [ u8 ; 16 ] = [
12131248 0x07 , 0x0a , 0x16 , 0xb4 , 0x6b , 0x4d , 0x41 , 0x44 , 0xf7 , 0x9b , 0xdd , 0x9d , 0xd0 , 0x4a , 0x28 ,
12141249 0x7c ,
12151250 ] ;
1251+ aes_cmac_sign_impl ( key, & message_len16, expected_mac_len16) ?;
1252+
1253+ let message_len40: [ u8 ; 40 ] = [
1254+ 0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 ,
1255+ 0x2a , 0xae , 0x2d , 0x8a , 0x57 , 0x1e , 0x03 , 0xac , 0x9c , 0x9e , 0xb7 , 0x6f , 0xac , 0x45 , 0xaf ,
1256+ 0x8e , 0x51 , 0x30 , 0xc8 , 0x1c , 0x46 , 0xa3 , 0x5c , 0xe4 , 0x11 ,
1257+ ] ;
1258+
1259+ let expected_mac_len40: [ u8 ; 16 ] = [
1260+ 0xdf , 0xa6 , 0x67 , 0x47 , 0xde , 0x9a , 0xe6 , 0x30 , 0x30 , 0xca , 0x32 , 0x61 , 0x14 , 0x97 , 0xc8 ,
1261+ 0x27 ,
1262+ ] ;
1263+ aes_cmac_sign_impl ( key, & message_len40, expected_mac_len40) ?;
1264+
1265+ let message_len64: [ u8 ; 64 ] = [
1266+ 0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 ,
1267+ 0x2a , 0xae , 0x2d , 0x8a , 0x57 , 0x1e , 0x03 , 0xac , 0x9c , 0x9e , 0xb7 , 0x6f , 0xac , 0x45 , 0xaf ,
1268+ 0x8e , 0x51 , 0x30 , 0xc8 , 0x1c , 0x46 , 0xa3 , 0x5c , 0xe4 , 0x11 , 0xe5 , 0xfb , 0xc1 , 0x19 , 0x1a ,
1269+ 0x0a , 0x52 , 0xef , 0xf6 , 0x9f , 0x24 , 0x45 , 0xdf , 0x4f , 0x9b , 0x17 , 0xad , 0x2b , 0x41 , 0x7b ,
1270+ 0xe6 , 0x6c , 0x37 , 0x10 ,
1271+ ] ;
1272+ let expected_mac_len64: [ u8 ; 16 ] = [
1273+ 0x51 , 0xf0 , 0xbe , 0xbf , 0x7e , 0x3b , 0x9d , 0x92 , 0xfc , 0x49 , 0x74 , 0x17 , 0x79 , 0x36 , 0x3c ,
1274+ 0xfe ,
1275+ ] ;
1276+ aes_cmac_sign_impl ( key, & message_len64, expected_mac_len64)
1277+ }
1278+
1279+ fn aes_cmac_sign_impl ( key : [ u8 ; 16 ] , message : & [ u8 ] , expected_mac : [ u8 ; 16 ] ) -> TestResult {
1280+ let ( pkcs11, slot) = init_pins ( ) ;
1281+ let session = pkcs11. open_rw_session ( slot) ?;
1282+ session. login ( UserType :: User , Some ( & AuthPin :: new ( USER_PIN . into ( ) ) ) ) ?;
12161283
12171284 let key_template = vec ! [
12181285 Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
@@ -1224,70 +1291,79 @@ fn aes_cmac_sign() -> TestResult {
12241291 Attribute :: Sign ( true ) ,
12251292 ] ;
12261293 let key = session. create_object ( & key_template) ?;
1227- let signature = session. sign ( & Mechanism :: AesCMac , key, & message) ?;
1294+ let signature = session. sign ( & Mechanism :: AesCMac , key, message) ?;
12281295
12291296 assert_eq ! ( expected_mac. as_slice( ) , signature. as_slice( ) ) ;
12301297 Ok ( ( ) )
12311298}
12321299
1300+ /// AES-CMAC test vectors from RFC 4493
12331301#[ test]
12341302#[ serial]
12351303fn aes_cmac_verify ( ) -> TestResult {
1236- let ( pkcs11, slot) = init_pins ( ) ;
1237- let session = pkcs11. open_rw_session ( slot) ?;
1238- session. login ( UserType :: User , Some ( & AuthPin :: new ( USER_PIN . into ( ) ) ) ) ?;
12391304 let key: [ u8 ; 16 ] = [
12401305 0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f ,
12411306 0x3c ,
12421307 ] ;
1243- let message: [ u8 ; 16 ] = [
1308+
1309+ let message_len0: [ u8 ; 0 ] = [ ] ;
1310+ let expected_mac_len0: [ u8 ; 16 ] = [
1311+ 0xbb , 0x1d , 0x69 , 0x29 , 0xe9 , 0x59 , 0x37 , 0x28 , 0x7f , 0xa3 , 0x7d , 0x12 , 0x9b , 0x75 , 0x67 ,
1312+ 0x46 ,
1313+ ] ;
1314+ aes_cmac_verify_impl ( key, & message_len0, expected_mac_len0) ?;
1315+
1316+ let message_len16: [ u8 ; 16 ] = [
12441317 0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 ,
12451318 0x2a ,
12461319 ] ;
1247- let expected_mac : [ u8 ; 16 ] = [
1320+ let expected_mac_len16 : [ u8 ; 16 ] = [
12481321 0x07 , 0x0a , 0x16 , 0xb4 , 0x6b , 0x4d , 0x41 , 0x44 , 0xf7 , 0x9b , 0xdd , 0x9d , 0xd0 , 0x4a , 0x28 ,
12491322 0x7c ,
12501323 ] ;
1324+ aes_cmac_verify_impl ( key, & message_len16, expected_mac_len16) ?;
12511325
1252- let key_template = vec ! [
1253- Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
1254- Attribute :: KeyType ( KeyType :: AES ) ,
1255- Attribute :: Token ( true ) ,
1256- Attribute :: Sensitive ( true ) ,
1257- Attribute :: Private ( true ) ,
1258- Attribute :: Value ( key. into( ) ) ,
1259- Attribute :: Verify ( true ) ,
1326+ let message_len40: [ u8 ; 40 ] = [
1327+ 0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 ,
1328+ 0x2a , 0xae , 0x2d , 0x8a , 0x57 , 0x1e , 0x03 , 0xac , 0x9c , 0x9e , 0xb7 , 0x6f , 0xac , 0x45 , 0xaf ,
1329+ 0x8e , 0x51 , 0x30 , 0xc8 , 0x1c , 0x46 , 0xa3 , 0x5c , 0xe4 , 0x11 ,
12601330 ] ;
1261- let key = session. create_object ( & key_template) ?;
1262- session. verify ( & Mechanism :: AesCMac , key, & message, & expected_mac) ?;
1263- Ok ( ( ) )
1331+
1332+ let expected_mac_len40: [ u8 ; 16 ] = [
1333+ 0xdf , 0xa6 , 0x67 , 0x47 , 0xde , 0x9a , 0xe6 , 0x30 , 0x30 , 0xca , 0x32 , 0x61 , 0x14 , 0x97 , 0xc8 ,
1334+ 0x27 ,
1335+ ] ;
1336+ aes_cmac_verify_impl ( key, & message_len40, expected_mac_len40) ?;
1337+
1338+ let message_len64: [ u8 ; 64 ] = [
1339+ 0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 ,
1340+ 0x2a , 0xae , 0x2d , 0x8a , 0x57 , 0x1e , 0x03 , 0xac , 0x9c , 0x9e , 0xb7 , 0x6f , 0xac , 0x45 , 0xaf ,
1341+ 0x8e , 0x51 , 0x30 , 0xc8 , 0x1c , 0x46 , 0xa3 , 0x5c , 0xe4 , 0x11 , 0xe5 , 0xfb , 0xc1 , 0x19 , 0x1a ,
1342+ 0x0a , 0x52 , 0xef , 0xf6 , 0x9f , 0x24 , 0x45 , 0xdf , 0x4f , 0x9b , 0x17 , 0xad , 0x2b , 0x41 , 0x7b ,
1343+ 0xe6 , 0x6c , 0x37 , 0x10 ,
1344+ ] ;
1345+ let expected_mac_len64: [ u8 ; 16 ] = [
1346+ 0x51 , 0xf0 , 0xbe , 0xbf , 0x7e , 0x3b , 0x9d , 0x92 , 0xfc , 0x49 , 0x74 , 0x17 , 0x79 , 0x36 , 0x3c ,
1347+ 0xfe ,
1348+ ] ;
1349+ aes_cmac_verify_impl ( key, & message_len64, expected_mac_len64)
12641350}
12651351
1266- #[ test]
1267- #[ serial]
1268- fn sign_verify_sha256_hmac ( ) -> TestResult {
1352+ fn aes_cmac_verify_impl ( key : [ u8 ; 16 ] , message : & [ u8 ] , expected_mac : [ u8 ; 16 ] ) -> TestResult {
12691353 let ( pkcs11, slot) = init_pins ( ) ;
12701354 let session = pkcs11. open_rw_session ( slot) ?;
12711355 session. login ( UserType :: User , Some ( & AuthPin :: new ( USER_PIN . into ( ) ) ) ) ?;
12721356
1273- let priv_key_template = vec ! [
1357+ let key_template = vec ! [
1358+ Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
1359+ Attribute :: KeyType ( KeyType :: AES ) ,
12741360 Attribute :: Token ( true ) ,
1275- Attribute :: Private ( true ) ,
12761361 Attribute :: Sensitive ( true ) ,
1277- Attribute :: Sign ( true ) ,
1278- Attribute :: KeyType ( KeyType :: GENERIC_SECRET ) ,
1279- Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
1280- Attribute :: ValueLen ( 256 . into( ) ) ,
1362+ Attribute :: Private ( true ) ,
1363+ Attribute :: Value ( key. into( ) ) ,
1364+ Attribute :: Verify ( true ) ,
12811365 ] ;
1282-
1283- let private = session. generate_key ( & Mechanism :: GenericSecretKeyGen , & priv_key_template) ?;
1284-
1285- let data = vec ! [ 0xAA , 0xBB , 0xCC , 0xDD , 0xEE , 0xFF ] ;
1286-
1287- let signature = session. sign ( & Mechanism :: Sha256Hmac , private, & data) ?;
1288-
1289- session. verify ( & Mechanism :: Sha256Hmac , private, & data, & signature) ?;
1290-
1291- session. destroy_object ( private) ?;
1366+ let key = session. create_object ( & key_template) ?;
1367+ session. verify ( & Mechanism :: AesCMac , key, message, & expected_mac) ?;
12921368 Ok ( ( ) )
12931369}
0 commit comments