@@ -1197,22 +1197,89 @@ fn ekdf_aes_cbc_encrypt_data() -> TestResult {
1197
1197
1198
1198
#[ test]
1199
1199
#[ serial]
1200
- fn aes_cmac_sign ( ) -> TestResult {
1200
+ fn sign_verify_sha256_hmac ( ) -> TestResult {
1201
1201
let ( pkcs11, slot) = init_pins ( ) ;
1202
1202
let session = pkcs11. open_rw_session ( slot) ?;
1203
1203
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 {
1204
1231
let key: [ u8 ; 16 ] = [
1205
1232
0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f ,
1206
1233
0x3c ,
1207
1234
] ;
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 ] = [
1209
1244
0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 ,
1210
1245
0x2a ,
1211
1246
] ;
1212
- let expected_mac : [ u8 ; 16 ] = [
1247
+ let expected_mac_len16 : [ u8 ; 16 ] = [
1213
1248
0x07 , 0x0a , 0x16 , 0xb4 , 0x6b , 0x4d , 0x41 , 0x44 , 0xf7 , 0x9b , 0xdd , 0x9d , 0xd0 , 0x4a , 0x28 ,
1214
1249
0x7c ,
1215
1250
] ;
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 ( ) ) ) ) ?;
1216
1283
1217
1284
let key_template = vec ! [
1218
1285
Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
@@ -1224,70 +1291,79 @@ fn aes_cmac_sign() -> TestResult {
1224
1291
Attribute :: Sign ( true ) ,
1225
1292
] ;
1226
1293
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) ?;
1228
1295
1229
1296
assert_eq ! ( expected_mac. as_slice( ) , signature. as_slice( ) ) ;
1230
1297
Ok ( ( ) )
1231
1298
}
1232
1299
1300
+ /// AES-CMAC test vectors from RFC 4493
1233
1301
#[ test]
1234
1302
#[ serial]
1235
1303
fn 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 ( ) ) ) ) ?;
1239
1304
let key: [ u8 ; 16 ] = [
1240
1305
0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f ,
1241
1306
0x3c ,
1242
1307
] ;
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 ] = [
1244
1317
0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 ,
1245
1318
0x2a ,
1246
1319
] ;
1247
- let expected_mac : [ u8 ; 16 ] = [
1320
+ let expected_mac_len16 : [ u8 ; 16 ] = [
1248
1321
0x07 , 0x0a , 0x16 , 0xb4 , 0x6b , 0x4d , 0x41 , 0x44 , 0xf7 , 0x9b , 0xdd , 0x9d , 0xd0 , 0x4a , 0x28 ,
1249
1322
0x7c ,
1250
1323
] ;
1324
+ aes_cmac_verify_impl ( key, & message_len16, expected_mac_len16) ?;
1251
1325
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 ,
1260
1330
] ;
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)
1264
1350
}
1265
1351
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 {
1269
1353
let ( pkcs11, slot) = init_pins ( ) ;
1270
1354
let session = pkcs11. open_rw_session ( slot) ?;
1271
1355
session. login ( UserType :: User , Some ( & AuthPin :: new ( USER_PIN . into ( ) ) ) ) ?;
1272
1356
1273
- let priv_key_template = vec ! [
1357
+ let key_template = vec ! [
1358
+ Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
1359
+ Attribute :: KeyType ( KeyType :: AES ) ,
1274
1360
Attribute :: Token ( true ) ,
1275
- Attribute :: Private ( true ) ,
1276
1361
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 ) ,
1281
1365
] ;
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) ?;
1292
1368
Ok ( ( ) )
1293
1369
}
0 commit comments