@@ -647,10 +647,12 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
647647
648648 /* clean hmac context */
649649 if (rc != 0 || info -> hmac .digest != NULL ) {
650- wolfTPM2_UnloadHandle (tlsCtx -> dev , & hmacCtx -> hash .handle );
651- wolfTPM2_UnloadHandle (tlsCtx -> dev , & hmacCtx -> key .handle );
652- XFREE (hmacCtx , NULL , DYNAMIC_TYPE_TMP_BUFFER );
653- hmacCtx = NULL ;
650+ if (hmacCtx != NULL ) {
651+ wolfTPM2_UnloadHandle (tlsCtx -> dev , & hmacCtx -> hash .handle );
652+ wolfTPM2_UnloadHandle (tlsCtx -> dev , & hmacCtx -> key .handle );
653+ XFREE (hmacCtx , NULL , DYNAMIC_TYPE_TMP_BUFFER );
654+ hmacCtx = NULL ;
655+ }
654656 }
655657 info -> hmac .hmac -> devCtx = hmacCtx ;
656658 #endif /* WOLFTPM_USE_SYMMETRIC */
@@ -739,11 +741,19 @@ static int wolfTPM2_HashUpdateCache(WOLFTPM2_HASHCTX* hashCtx,
739741 /* determine if we need to grow buffer */
740742 else if ((hashCtx -> cacheSz + inSz ) > hashCtx -> cacheBufSz ) {
741743 byte * oldIn = hashCtx -> cacheBuf ;
744+ word32 oldBufSz = hashCtx -> cacheBufSz ;
745+ /* check for overflow */
746+ if (hashCtx -> cacheSz + inSz < hashCtx -> cacheSz ) {
747+ return BUFFER_E ;
748+ }
742749 hashCtx -> cacheBufSz = (hashCtx -> cacheSz + inSz +
743750 WOLFTPM2_HASH_BLOCK_SZ - 1 ) & ~(WOLFTPM2_HASH_BLOCK_SZ - 1 );
744- hashCtx -> cacheBuf = (byte * )XMALLOC (hashCtx -> cacheBufSz ,
751+ hashCtx -> cacheBuf = (byte * )XMALLOC (hashCtx -> cacheBufSz ,
745752 NULL , DYNAMIC_TYPE_TMP_BUFFER );
746753 if (hashCtx -> cacheBuf == NULL ) {
754+ /* restore old buffer on allocation failure */
755+ hashCtx -> cacheBuf = oldIn ;
756+ hashCtx -> cacheBufSz = oldBufSz ;
747757 return MEMORY_E ;
748758 }
749759 XMEMCPY (hashCtx -> cacheBuf , oldIn , hashCtx -> cacheSz );
@@ -919,6 +929,7 @@ static int RsaMGF1(wc_HashAlg* hash, enum wc_HashType hType,
919929 counter ++ ;
920930 } while (ret == 0 && idx < outSz );
921931
932+ TPM2_ForceZero (tmp , sizeof (tmp ));
922933 return ret ;
923934}
924935
@@ -1057,6 +1068,7 @@ static int RsaPadPss(const byte* input, word32 inputLen, byte* pkcsBlock,
10571068 xorbuf (m , salt + o , (word32 )saltLen );
10581069 }
10591070 wc_HashFree (& hashCtx , hType );
1071+ TPM2_ForceZero (salt , sizeof (salt ));
10601072 return ret ;
10611073}
10621074
0 commit comments