@@ -958,7 +958,7 @@ static void wp11_Session_Final(WP11_Session* session)
958958 if ((session -> init & ~WP11_INIT_DIGEST_MASK ) == WP11_INIT_HMAC_SIGN ||
959959 (session -> init & ~WP11_INIT_DIGEST_MASK ) == WP11_INIT_HMAC_VERIFY ) {
960960 wc_HmacFree (& session -> params .hmac .hmac );
961- session -> init = 0 ;
961+ session -> init &= WP11_INIT_DIGEST_MASK ;
962962 }
963963#endif
964964#ifdef HAVE_AESCMAC
@@ -970,14 +970,16 @@ static void wp11_Session_Final(WP11_Session* session)
970970 wc_ForceZero (& session -> params .cmac .cmac ,
971971 sizeof (session -> params .cmac .cmac ));
972972#endif
973- session -> init = 0 ;
973+ session -> init &= WP11_INIT_DIGEST_MASK ;
974974 }
975975#endif
976976 if ((session -> init & ~WP11_INIT_DIGEST_MASK ) == WP11_INIT_DIGEST ) {
977977 wc_HashFree (& session -> params .digest .hash ,
978978 session -> params .digest .hashType );
979- session -> init = 0 ;
979+ session -> init &= ~ WP11_INIT_DIGEST_MASK ;
980980 }
981+ /* Ensure no stale bits remain after all cleanup. */
982+ session -> init = 0 ;
981983}
982984
983985#ifndef WOLFPKCS11_NO_STORE
@@ -6971,8 +6973,8 @@ int WP11_Slot_IsLoggedIn(WP11_Slot* slot)
69716973
69726974void WP11_Slot_Logout (WP11_Slot * slot )
69736975{
6974- #ifndef WOLFPKCS11_NO_STORE
69756976 int state ;
6977+ #ifndef WOLFPKCS11_NO_STORE
69766978 int ret = 0 ;
69776979#endif
69786980
@@ -6986,9 +6988,15 @@ void WP11_Slot_Logout(WP11_Slot* slot)
69866988 ret = wp11_Object_Encode (object , 1 );
69876989 object = object -> next ;
69886990 }
6989- wc_ForceZero (slot -> token .key , sizeof (slot -> token .key ));
69906991 }
6992+ #else
6993+ state = slot -> token .loginState ;
69916994#endif
6995+ /* Zero token key only on user logout — SO logout must preserve it
6996+ * for subsequent object encryption (e.g., empty-PIN flow). */
6997+ if (state == WP11_APP_STATE_RO_USER || state == WP11_APP_STATE_RW_USER ) {
6998+ wc_ForceZero (slot -> token .key , sizeof (slot -> token .key ));
6999+ }
69927000 slot -> token .loginState = WP11_APP_STATE_RW_PUBLIC ;
69937001
69947002 WP11_Lock_UnlockRW (& slot -> lock );
@@ -14261,8 +14269,11 @@ int WP11_Digest_Single(unsigned char* data, word32 dataLen,
1426114269 WP11_Digest * digest = & session -> params .digest ;
1426214270
1426314271 blockLen = wc_HashGetDigestSize (digest -> hashType );
14264- if (blockLen < 0 )
14272+ if (blockLen < 0 ) {
14273+ wc_HashFree (& digest -> hash , digest -> hashType );
14274+ session -> init = 0 ;
1426514275 return CKR_FUNCTION_FAILED ;
14276+ }
1426614277
1426714278 if (dataOut == NULL ) {
1426814279 * dataOutLen = (word32 )blockLen ;
0 commit comments