@@ -2476,3 +2476,82 @@ fn aes_cmac_verify_impl(key: [u8; 16], message: &[u8], expected_mac: [u8; 16]) -
24762476 session. verify ( & Mechanism :: AesCMac , key, message, & expected_mac) ?;
24772477 Ok ( ( ) )
24782478}
2479+
2480+ /// AES-CMAC test vectors from RFC 4493
2481+ #[ test]
2482+ #[ serial]
2483+ fn unique_id ( ) -> TestResult {
2484+ let ( pkcs11, slot) = init_pins ( ) ;
2485+ let session = pkcs11. open_rw_session ( slot) ?;
2486+ session. login ( UserType :: User , Some ( & AuthPin :: new ( USER_PIN . into ( ) ) ) ) ?;
2487+
2488+ let key: [ u8 ; 16 ] = [
2489+ 0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f ,
2490+ 0x3c ,
2491+ ] ;
2492+
2493+ // Can not create object with Unique Id
2494+ let key_template = vec ! [
2495+ Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
2496+ Attribute :: KeyType ( KeyType :: AES ) ,
2497+ Attribute :: Token ( true ) ,
2498+ Attribute :: Sensitive ( true ) ,
2499+ Attribute :: Private ( true ) ,
2500+ Attribute :: Value ( key. into( ) ) ,
2501+ Attribute :: UniqueId ( vec![ 0x00 , 0x00 , 0x00 , 0x01 ] ) ,
2502+ ] ;
2503+ let res = session. create_object ( & key_template) ;
2504+ assert ! ( res. is_err( ) ) ;
2505+ assert ! ( matches!(
2506+ res,
2507+ Err ( Error :: Pkcs11 (
2508+ RvError :: AttributeTypeInvalid ,
2509+ Function :: CreateObject
2510+ ) )
2511+ ) ) ;
2512+
2513+ let generate_template = vec ! [
2514+ Attribute :: Token ( true ) ,
2515+ Attribute :: ValueLen ( 32 . into( ) ) ,
2516+ Attribute :: Encrypt ( true ) ,
2517+ ] ;
2518+
2519+ // generate a secret key
2520+ let key = session. generate_key ( & Mechanism :: AesKeyGen , & generate_template) ?;
2521+
2522+ // we can get the UniqueId attribute
2523+ let attrs = session. get_attributes ( key, & [ AttributeType :: UniqueId ] ) ?;
2524+ if is_softhsm ( ) {
2525+ // SoftHSM does not support this attribute at all
2526+ assert_eq ! ( attrs. len( ) , 0 ) ;
2527+ } else {
2528+ assert ! ( matches!( attrs. first( ) , Some ( Attribute :: UniqueId ( _) ) ) ) ;
2529+ }
2530+
2531+ // we can not set the UniqueId attribute
2532+ let update_template = vec ! [ Attribute :: UniqueId ( vec![ 0x01 , 0x02 , 0x03 ] ) ] ;
2533+ let res = session. update_attributes ( key, & update_template) ;
2534+ assert ! ( res. is_err( ) ) ;
2535+ if is_softhsm ( ) {
2536+ // SoftHSM does not support this attribute at all
2537+ assert ! ( matches!(
2538+ res,
2539+ Err ( Error :: Pkcs11 (
2540+ RvError :: AttributeTypeInvalid ,
2541+ Function :: SetAttributeValue
2542+ ) )
2543+ ) ) ;
2544+ } else {
2545+ assert ! ( matches!(
2546+ res,
2547+ Err ( Error :: Pkcs11 (
2548+ RvError :: AttributeReadOnly ,
2549+ Function :: SetAttributeValue
2550+ ) )
2551+ ) ) ;
2552+ }
2553+
2554+ session. destroy_object ( key) ?;
2555+
2556+ Ok ( ( ) )
2557+ }
0 commit comments