3737/* --- Local Variables -- */
3838/******************************************************************************/
3939
40+
41+ #ifdef WOLFTPM_NO_ACTIVE_THREAD_LS
42+ /* if using gHwLock and want to use a shared active TPM2_CTX between threads */
43+ static TPM2_CTX * gActiveTPM ;
44+ #else
4045static THREAD_LS_T TPM2_CTX * gActiveTPM ;
46+ #endif
47+
4148#ifndef WOLFTPM2_NO_WOLFCRYPT
4249static volatile int gWolfCryptRefCount = 0 ;
4350#endif
4451
52+ #if !defined(WOLFTPM2_NO_WOLFCRYPT ) && !defined(WOLFTPM_NO_LOCK ) && \
53+ !defined(SINGLE_THREADED )
54+ static wolfSSL_Mutex gHwLock WOLFSSL_MUTEX_INITIALIZER_CLAUSE (gHwLock );
55+ #endif
56+
4557#ifdef WOLFTPM_LINUX_DEV
4658#define INTERNAL_SEND_COMMAND TPM2_LINUX_SendCommand
4759#define TPM2_INTERNAL_CLEANUP (ctx )
@@ -61,43 +73,24 @@ static volatile int gWolfCryptRefCount = 0;
6173/******************************************************************************/
6274static TPM_RC TPM2_AcquireLock (TPM2_CTX * ctx )
6375{
64- #if defined(WOLFTPM2_NO_WOLFCRYPT ) || defined(WOLFTPM_NO_LOCK )
65- (void )ctx ;
66- #else
67- int ret ;
68-
69- if (!ctx -> hwLockInit ) {
70- if (wc_InitMutex (& ctx -> hwLock ) != 0 ) {
71- #ifdef DEBUG_WOLFTPM
72- printf ("TPM Mutex Init failed\n" );
73- #endif
74- return TPM_RC_FAILURE ;
75- }
76- ctx -> hwLockInit = 1 ;
77- ctx -> lockCount = 0 ;
78- }
79-
80- if (ctx -> lockCount == 0 ) {
81- ret = wc_LockMutex (& ctx -> hwLock );
82- if (ret != 0 )
83- return TPM_RC_FAILURE ;
76+ #if !defined(WOLFTPM2_NO_WOLFCRYPT ) && !defined(WOLFTPM_NO_LOCK ) && \
77+ !defined(SINGLE_THREADED )
78+ int ret = wc_LockMutex (& gHwLock );
79+ if (ret != 0 ) {
80+ return TPM_RC_FAILURE ;
8481 }
85- ctx -> lockCount ++ ;
8682#endif
83+ (void )ctx ;
8784 return TPM_RC_SUCCESS ;
8885}
8986
9087static void TPM2_ReleaseLock (TPM2_CTX * ctx )
9188{
92- #if defined(WOLFTPM2_NO_WOLFCRYPT ) || defined(WOLFTPM_NO_LOCK )
93- (void )ctx ;
94- #else
95- ctx -> lockCount -- ;
96- if (ctx -> lockCount == 0 ) {
97- wc_UnLockMutex (& ctx -> hwLock );
98- }
99-
89+ #if !defined(WOLFTPM2_NO_WOLFCRYPT ) && !defined(WOLFTPM_NO_LOCK ) && \
90+ !defined(SINGLE_THREADED )
91+ wc_UnLockMutex (& gHwLock );
10092#endif
93+ (void )ctx ;
10194}
10295
10396static int TPM2_CommandProcess (TPM2_CTX * ctx , TPM2_Packet * packet ,
@@ -507,6 +500,10 @@ static inline int TPM2_WolfCrypt_Init(void)
507500 if (rc == 0 )
508501 rc = wc_SetSeed_Cb (wc_GenerateSeed );
509502 #endif
503+ #if !defined(WOLFTPM_NO_LOCK ) && !defined(SINGLE_THREADED ) && \
504+ !defined(WOLFSSL_MUTEX_INITIALIZER )
505+ wc_InitMutex (& gHwLock );
506+ #endif
510507 }
511508 gWolfCryptRefCount ++ ;
512509
@@ -697,19 +694,17 @@ TPM_RC TPM2_Cleanup(TPM2_CTX* ctx)
697694 wc_FreeRng (& ctx -> rng );
698695 }
699696 #endif
700- #ifndef WOLFTPM_NO_LOCK
701- if (ctx -> hwLockInit ) {
702- ctx -> hwLockInit = 0 ;
703- wc_FreeMutex (& ctx -> hwLock );
704- }
705- #endif
706697
707698 /* track wolf initialize reference count in wolfTPM. wolfCrypt does not
708- properly track reference count in v4.1 or older releases */
699+ * properly track reference count in v4.1 or older releases */
709700 gWolfCryptRefCount -- ;
710701 if (gWolfCryptRefCount < 0 )
711702 gWolfCryptRefCount = 0 ;
712703 if (gWolfCryptRefCount == 0 ) {
704+ #if !defined(WOLFTPM_NO_LOCK ) && !defined(SINGLE_THREADED ) && \
705+ !defined(WOLFSSL_MUTEX_INITIALIZER )
706+ wc_FreeMutex (& gHwLock );
707+ #endif
713708 wolfCrypt_Cleanup ();
714709 }
715710#endif /* !WOLFTPM2_NO_WOLFCRYPT */
0 commit comments