2929#define DEBUG_WARN (...) ZCRYPT_DBF(DBF_WARN, ##__VA_ARGS__)
3030#define DEBUG_ERR (...) ZCRYPT_DBF(DBF_ERR, ##__VA_ARGS__)
3131
32+ #define EP11_PINBLOB_V1_BYTES 56
33+
3234/* default iv used here */
3335static const u8 def_iv [16 ] = { 0x00 , 0x11 , 0x22 , 0x33 , 0x44 , 0x55 , 0x66 , 0x77 ,
3436 0x88 , 0x99 , 0xaa , 0xbb , 0xcc , 0xdd , 0xee , 0xff };
@@ -592,7 +594,7 @@ static int ep11_query_info(u16 cardnr, u16 domain, u32 query_type,
592594 struct ep11_cprb * req = NULL , * rep = NULL ;
593595 struct ep11_target_dev target ;
594596 struct ep11_urb * urb = NULL ;
595- int api = 1 , rc = - ENOMEM ;
597+ int api = EP11_API_V1 , rc = - ENOMEM ;
596598
597599 /* request cprb and payload */
598600 req = alloc_cprb (sizeof (struct ep11_info_req_pl ));
@@ -789,8 +791,7 @@ static int _ep11_genaeskey(u16 card, u16 domain,
789791 u32 attr_bool_bits ;
790792 u32 attr_val_len_type ;
791793 u32 attr_val_len_value ;
792- u8 pin_tag ;
793- u8 pin_len ;
794+ /* followed by empty pin tag or empty pinblob tag */
794795 } __packed * req_pl ;
795796 struct keygen_rep_pl {
796797 struct pl_head head ;
@@ -803,9 +804,11 @@ static int _ep11_genaeskey(u16 card, u16 domain,
803804 u8 data [512 ];
804805 } __packed * rep_pl ;
805806 struct ep11_cprb * req = NULL , * rep = NULL ;
807+ size_t req_pl_size , pinblob_size = 0 ;
806808 struct ep11_target_dev target ;
807809 struct ep11_urb * urb = NULL ;
808810 int api , rc = - ENOMEM ;
811+ u8 * p ;
809812
810813 switch (keybitsize ) {
811814 case 128 :
@@ -821,12 +824,22 @@ static int _ep11_genaeskey(u16 card, u16 domain,
821824 }
822825
823826 /* request cprb and payload */
824- req = alloc_cprb (sizeof (struct keygen_req_pl ));
827+ api = (!keygenflags || keygenflags & 0x00200000 ) ?
828+ EP11_API_V4 : EP11_API_V1 ;
829+ if (ap_is_se_guest ()) {
830+ /*
831+ * genkey within SE environment requires API ordinal 6
832+ * with empty pinblob
833+ */
834+ api = EP11_API_V6 ;
835+ pinblob_size = EP11_PINBLOB_V1_BYTES ;
836+ }
837+ req_pl_size = sizeof (struct keygen_req_pl ) + ASN1TAGLEN (pinblob_size );
838+ req = alloc_cprb (req_pl_size );
825839 if (!req )
826840 goto out ;
827841 req_pl = (struct keygen_req_pl * )(((u8 * )req ) + sizeof (* req ));
828- api = (!keygenflags || keygenflags & 0x00200000 ) ? 4 : 1 ;
829- prep_head (& req_pl -> head , sizeof (* req_pl ), api , 21 ); /* GenerateKey */
842+ prep_head (& req_pl -> head , req_pl_size , api , 21 ); /* GenerateKey */
830843 req_pl -> var_tag = 0x04 ;
831844 req_pl -> var_len = sizeof (u32 );
832845 req_pl -> keybytes_tag = 0x04 ;
@@ -842,7 +855,10 @@ static int _ep11_genaeskey(u16 card, u16 domain,
842855 req_pl -> attr_bool_bits = keygenflags ? keygenflags : KEY_ATTR_DEFAULTS ;
843856 req_pl -> attr_val_len_type = 0x00000161 ; /* CKA_VALUE_LEN */
844857 req_pl -> attr_val_len_value = keybitsize / 8 ;
845- req_pl -> pin_tag = 0x04 ;
858+ p = ((u8 * )req_pl ) + sizeof (* req_pl );
859+ /* pin tag */
860+ * p ++ = 0x04 ;
861+ * p ++ = pinblob_size ;
846862
847863 /* reply cprb and payload */
848864 rep = alloc_cprb (sizeof (struct keygen_rep_pl ));
@@ -857,7 +873,7 @@ static int _ep11_genaeskey(u16 card, u16 domain,
857873 target .ap_id = card ;
858874 target .dom_id = domain ;
859875 prep_urb (urb , & target , 1 ,
860- req , sizeof (* req ) + sizeof ( * req_pl ) ,
876+ req , sizeof (* req ) + req_pl_size ,
861877 rep , sizeof (* rep ) + sizeof (* rep_pl ));
862878
863879 rc = zcrypt_send_ep11_cprb (urb );
@@ -965,7 +981,7 @@ static int ep11_cryptsingle(u16 card, u16 domain,
965981 struct ep11_target_dev target ;
966982 struct ep11_urb * urb = NULL ;
967983 size_t req_pl_size , rep_pl_size ;
968- int n , api = 1 , rc = - ENOMEM ;
984+ int n , api = EP11_API_V1 , rc = - ENOMEM ;
969985 u8 * p ;
970986
971987 /* the simple asn1 coding used has length limits */
@@ -1084,7 +1100,7 @@ static int _ep11_unwrapkey(u16 card, u16 domain,
10841100 * maybe followed by iv data
10851101 * followed by kek tag + kek blob
10861102 * followed by empty mac tag
1087- * followed by empty pin tag
1103+ * followed by empty pin tag or empty pinblob tag
10881104 * followed by encryted key tag + bytes
10891105 */
10901106 } __packed * req_pl ;
@@ -1099,20 +1115,30 @@ static int _ep11_unwrapkey(u16 card, u16 domain,
10991115 u8 data [512 ];
11001116 } __packed * rep_pl ;
11011117 struct ep11_cprb * req = NULL , * rep = NULL ;
1118+ size_t req_pl_size , pinblob_size = 0 ;
11021119 struct ep11_target_dev target ;
11031120 struct ep11_urb * urb = NULL ;
1104- size_t req_pl_size ;
11051121 int api , rc = - ENOMEM ;
11061122 u8 * p ;
11071123
11081124 /* request cprb and payload */
1125+ api = (!keygenflags || keygenflags & 0x00200000 ) ?
1126+ EP11_API_V4 : EP11_API_V1 ;
1127+ if (ap_is_se_guest ()) {
1128+ /*
1129+ * unwrap within SE environment requires API ordinal 6
1130+ * with empty pinblob
1131+ */
1132+ api = EP11_API_V6 ;
1133+ pinblob_size = EP11_PINBLOB_V1_BYTES ;
1134+ }
11091135 req_pl_size = sizeof (struct uw_req_pl ) + (iv ? 16 : 0 )
1110- + ASN1TAGLEN (keksize ) + 4 + ASN1TAGLEN (enckeysize );
1136+ + ASN1TAGLEN (keksize ) + ASN1TAGLEN (0 )
1137+ + ASN1TAGLEN (pinblob_size ) + ASN1TAGLEN (enckeysize );
11111138 req = alloc_cprb (req_pl_size );
11121139 if (!req )
11131140 goto out ;
11141141 req_pl = (struct uw_req_pl * )(((u8 * )req ) + sizeof (* req ));
1115- api = (!keygenflags || keygenflags & 0x00200000 ) ? 4 : 1 ;
11161142 prep_head (& req_pl -> head , req_pl_size , api , 34 ); /* UnwrapKey */
11171143 req_pl -> attr_tag = 0x04 ;
11181144 req_pl -> attr_len = 7 * sizeof (u32 );
@@ -1137,9 +1163,10 @@ static int _ep11_unwrapkey(u16 card, u16 domain,
11371163 /* empty mac key tag */
11381164 * p ++ = 0x04 ;
11391165 * p ++ = 0 ;
1140- /* empty pin tag */
1166+ /* pin tag */
11411167 * p ++ = 0x04 ;
1142- * p ++ = 0 ;
1168+ * p ++ = pinblob_size ;
1169+ p += pinblob_size ;
11431170 /* encrypted key value tag and bytes */
11441171 p += asn1tag_write (p , 0x04 , enckey , enckeysize );
11451172
@@ -1275,7 +1302,8 @@ static int _ep11_wrapkey(u16 card, u16 domain,
12751302 if (!mech || mech == 0x80060001 )
12761303 req -> flags |= 0x20 ; /* CPACF_WRAP needs special bit */
12771304 req_pl = (struct wk_req_pl * )(((u8 * )req ) + sizeof (* req ));
1278- api = (!mech || mech == 0x80060001 ) ? 4 : 1 ; /* CKM_IBM_CPACF_WRAP */
1305+ api = (!mech || mech == 0x80060001 ) ? /* CKM_IBM_CPACF_WRAP */
1306+ EP11_API_V4 : EP11_API_V1 ;
12791307 prep_head (& req_pl -> head , req_pl_size , api , 33 ); /* WrapKey */
12801308 req_pl -> var_tag = 0x04 ;
12811309 req_pl -> var_len = sizeof (u32 );
0 commit comments