@@ -65,11 +65,12 @@ extern int uart_printf(const char* format, ...);
6565static CK_FUNCTION_LIST * funcList ;
6666static CK_SLOT_ID slot = WOLFPKCS11_DLL_SLOT ;
6767
68- static byte * userPin = (byte * )"wolfpkcs11-test" ;
68+ static byte * userDefaultPin = (byte * )"wolfpkcs11-test" ;
6969static CK_ULONG userPinLen ;
7070
7171
72- static CK_RV pkcs11_init (CK_SESSION_HANDLE * session )
72+ static CK_RV pkcs11_init (CK_SESSION_HANDLE * session , char * userPin ,
73+ int userPinLen )
7374{
7475 CK_RV ret = CKR_OK ;
7576
@@ -126,7 +127,7 @@ CK_RV pkcs11_add_aes_dhuk_key(CK_SESSION_HANDLE session)
126127{
127128 CK_RV ret ;
128129 CK_ULONG devId = WOLFSSL_STM32U5_DHUK_DEVID ;/* signal use of hardware key */
129- CK_ATTRIBUTE aes_dhuk_secret_key [] = {
130+ CK_ATTRIBUTE aesDhukSecretKey [] = {
130131 { CKA_CLASS , & secretKeyClass , sizeof (secretKeyClass ) },
131132#ifndef NO_AES
132133 { CKA_KEY_TYPE , & aesKeyType , sizeof (aesKeyType ) },
@@ -135,14 +136,13 @@ CK_RV pkcs11_add_aes_dhuk_key(CK_SESSION_HANDLE session)
135136#endif
136137 { CKA_WRAP , & ckTrue , sizeof (ckTrue ) },
137138 { CKA_UNWRAP , & ckTrue , sizeof (ckTrue ) },
138- { CKA_TOKEN , & ckTrue , sizeof (ckTrue ) },
139139 { CKA_VALUE , aes256Key , sizeof (aes256Key ) },
140140 { CKA_WOLFSSL_DEVID , & devId , sizeof (devId ) },
141141 };
142- CK_ULONG cnt = sizeof (aes_dhuk_secret_key )/sizeof (* aes_dhuk_secret_key );
142+ CK_ULONG cnt = sizeof (aesDhukSecretKey )/sizeof (* aesDhukSecretKey );
143143 CK_OBJECT_HANDLE obj ;
144144
145- ret = funcList -> C_CreateObject (session , aes_dhuk_secret_key , cnt , & obj );
145+ ret = funcList -> C_CreateObject (session , aesDhukSecretKey , cnt , & obj );
146146 CHECK_CKR (ret , "CreateObject AES DHUK key" );
147147
148148 return ret ;
@@ -162,7 +162,6 @@ CK_RV pkcs11_add_aes_software_key(CK_SESSION_HANDLE session)
162162#endif
163163 { CKA_ENCRYPT , & ckTrue , sizeof (ckTrue ) },
164164 { CKA_DECRYPT , & ckTrue , sizeof (ckTrue ) },
165- { CKA_TOKEN , & ckTrue , sizeof (ckTrue ) },
166165 { CKA_VALUE , aes256Key , sizeof (aes256Key ) },
167166 { CKA_WOLFSSL_DEVID , & devId , sizeof (devId ) },
168167 };
@@ -268,21 +267,27 @@ CK_RV pkcs11_wrap_aes_key(CK_SESSION_HANDLE session)
268267 CK_BYTE wrappedKeyBuffer [32 ];
269268 CK_ULONG wrappedKeyBufferLen = sizeof (wrappedKeyBuffer );
270269 CK_ULONG devId = WOLFSSL_STM32U5_DHUK_WRAPPED_DEVID ;
271- CK_MECHANISM mech = {CKM_AES_ECB , NULL , 0 };
270+ byte iv [16 ];
271+ /* CK_MECHANISM mech = {CKM_AES_ECB, NULL, 0}; */
272+ CK_MECHANISM mech = {CKM_AES_CBC_PAD , iv , 16 };
272273 int i ;
273274 CK_RV rv ;
274275 CK_ATTRIBUTE wrappedKeyTemplate [] = {
275276 { CKA_CLASS , & secretKeyClass , sizeof (secretKeyClass ) },
276277 { CKA_KEY_TYPE , & aesKeyType , sizeof (aesKeyType ) },
277278 { CKA_VALUE , wrappedKeyBuffer , wrappedKeyBufferLen },
278- { CKA_ENCRYPT , & ckTrue , sizeof (ckTrue ) },
279- { CKA_DECRYPT , & ckTrue , sizeof (ckTrue ) },
280- { CKA_TOKEN , & ckTrue , sizeof (ckTrue ) },
281- { CKA_WOLFSSL_DEVID , & devId , sizeof (devId ) },
279+ { CKA_ENCRYPT , & ckTrue , sizeof (ckTrue ) },
280+ { CKA_DECRYPT , & ckTrue , sizeof (ckTrue ) },
281+ { CKA_TOKEN , & ckTrue , sizeof (ckTrue ) },
282+ { CKA_WOLFSSL_DHUK_IV , iv , sizeof (iv ) },
283+ { CKA_WOLFSSL_DEVID , & devId , sizeof (devId ) },
282284 };
283285 CK_ULONG wrappedKeyTemplateLen = sizeof (wrappedKeyTemplate ) /
284286 sizeof (CK_ATTRIBUTE );
285287
288+ for (i = 0 ; i < 16 ; i ++ ) {
289+ iv [i ] = i ;
290+ }
286291
287292 key = find_software_key (session );
288293 if (key == 0 ) {
@@ -388,7 +393,6 @@ static CK_RV pkcs11_compare_results(CK_SESSION_HANDLE session)
388393 for (i = 0 ; i < 16 ; i ++ ) {
389394 iv [i ] = i ;
390395 }
391-
392396 /* Encrypt plain text using software only key */
393397 key = find_software_key (session );
394398 memset (cipher , 0 , sizeof (cipher ));
@@ -435,6 +439,25 @@ static CK_RV pkcs11_compare_results(CK_SESSION_HANDLE session)
435439 return ret ;
436440}
437441
442+ /* Match the command line argument with the string.
443+ *
444+ * arg Command line argument.
445+ * str String to check for.
446+ * return 1 if the command line argument matches the string, 0 otherwise.
447+ */
448+ static int string_matches (const char * arg , const char * str )
449+ {
450+ int len = (int )XSTRLEN (str ) + 1 ;
451+ return XSTRNCMP (arg , str , len ) == 0 ;
452+ }
453+
454+ /* Display the usage options of the benchmark program. */
455+ static void Usage (void )
456+ {
457+ printf ("stm32_dhuk_aes_key\n" );
458+ printf ("-? Help, print this usage\n" );
459+ printf ("-userPin <string> User PIN\n" );
460+ }
438461
439462#ifndef NO_MAIN_DRIVER
440463int main (int argc , char * argv [])
@@ -445,16 +468,39 @@ int stm32_dhuk_aes_key(int argc, char* argv[])
445468 int ret ;
446469 CK_RV rv ;
447470 CK_SESSION_HANDLE session = CK_INVALID_HANDLE ;
471+ char * userPin = userDefaultPin ;
448472
449- #ifndef WOLFPKCS11_NO_ENV
450- if (!XGETENV ("WOLFPKCS11_TOKEN_PATH" )) {
451- XSETENV ("WOLFPKCS11_TOKEN_PATH" , "./store" , 1 );
452- }
453- #endif
454473 printf ("Example PKCS11 DHUK AES use\n\r" );
455474
475+ argc -- ;
476+ argv ++ ;
477+ while (argc > 0 ) {
478+ if (string_matches (* argv , "-?" )) {
479+ Usage ();
480+ return 0 ;
481+ }
482+ else if (string_matches (* argv , "-userPin" )) {
483+ argc -- ;
484+ argv ++ ;
485+ if (argc == 0 ) {
486+ printf ("User PIN not supplied\n" );
487+ return 1 ;
488+ }
489+ userPin = (byte * )* argv ;
490+ }
491+ else {
492+ printf ("Unrecognized command line argument\n %s\n" ,
493+ argv [0 ]);
494+ return 1 ;
495+ }
496+
497+ argc -- ;
498+ argv ++ ;
499+ }
500+ userPinLen = (int )XSTRLEN ((const char * )userPin );
501+
456502
457- rv = pkcs11_init (& session );
503+ rv = pkcs11_init (& session , userPin , userPinLen );
458504 if (rv == CKR_OK ) {
459505 rv = pkcs11_add_aes_dhuk_key (session );
460506 }
0 commit comments