@@ -10539,6 +10539,7 @@ int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
1053910539 if (ret == 0 )
1054010540 ret = wc_AesGcmEncrypt_ex (aes , NULL , NULL , 0 , iv , ivSz ,
1054110541 authTag , authTagSz , authIn , authInSz );
10542+ aes -> isAllocated = 0 ;
1054210543 wc_AesFree (aes );
1054310544 }
1054410545 ForceZero (aes , sizeof * aes );
@@ -10580,6 +10581,8 @@ int wc_GmacVerify(const byte* key, word32 keySz,
1058010581 if (ret == 0 )
1058110582 ret = wc_AesGcmDecrypt (aes , NULL , NULL , 0 , iv , ivSz ,
1058210583 authTag , authTagSz , authIn , authInSz );
10584+
10585+ aes -> isAllocated = 0 ;
1058310586 wc_AesFree (aes );
1058410587 }
1058510588 ForceZero (aes , sizeof * aes );
@@ -11296,6 +11299,20 @@ int wc_AesCcmEncrypt_ex(Aes* aes, byte* out, const byte* in, word32 sz,
1129611299
1129711300#endif /* HAVE_AESCCM */
1129811301
11302+ Aes * wc_AesNew (void * heap , int devId )
11303+ {
11304+ Aes * aes = (Aes * )XMALLOC (sizeof (Aes ), heap , DYNAMIC_TYPE_AES );
11305+ if (aes != NULL ) {
11306+ if (wc_AesInit (aes , heap , devId ) != 0 ) {
11307+ XFREE (aes , heap , DYNAMIC_TYPE_AES );
11308+ aes = NULL ;
11309+ }
11310+ else {
11311+ aes -> isAllocated = 1 ;
11312+ }
11313+ }
11314+ return aes ;
11315+ }
1129911316
1130011317/* Initialize Aes for use with async hardware */
1130111318int wc_AesInit (Aes * aes , void * heap , int devId )
@@ -11305,6 +11322,7 @@ int wc_AesInit(Aes* aes, void* heap, int devId)
1130511322 if (aes == NULL )
1130611323 return BAD_FUNC_ARG ;
1130711324
11325+ aes -> isAllocated = 0 ;
1130811326 aes -> heap = heap ;
1130911327 aes -> rounds = 0 ;
1131011328
@@ -11430,11 +11448,18 @@ int wc_AesInit_Label(Aes* aes, const char* label, void* heap, int devId)
1143011448/* Free Aes from use with async hardware */
1143111449void wc_AesFree (Aes * aes )
1143211450{
11433- if (aes == NULL )
11451+ void * heap ;
11452+ byte isAllocated ;
11453+
11454+ if (aes == NULL ) {
1143411455 return ;
11456+ }
11457+
11458+ heap = aes -> heap ;
11459+ isAllocated = aes -> isAllocated ;
1143511460
1143611461#ifdef WC_DEBUG_CIPHER_LIFECYCLE
11437- (void )wc_debug_CipherLifecycleFree (& aes -> CipherLifecycleTag , aes -> heap , 1 );
11462+ (void )wc_debug_CipherLifecycleFree (& aes -> CipherLifecycleTag , heap , 1 );
1143811463#endif
1143911464
1144011465#if defined(WOLFSSL_ASYNC_CRYPT ) && defined(WC_ASYNC_ENABLE_AES )
@@ -11472,7 +11497,7 @@ void wc_AesFree(Aes* aes)
1147211497#endif
1147311498#if defined(WOLFSSL_AESGCM_STREAM ) && defined(WOLFSSL_SMALL_STACK ) && \
1147411499 !defined(WOLFSSL_AESNI )
11475- XFREE (aes -> streamData , aes -> heap , DYNAMIC_TYPE_AES );
11500+ XFREE (aes -> streamData , heap , DYNAMIC_TYPE_AES );
1147611501 aes -> streamData = NULL ;
1147711502#endif
1147811503
@@ -11499,6 +11524,11 @@ void wc_AesFree(Aes* aes)
1149911524#ifdef WOLFSSL_CHECK_MEM_ZERO
1150011525 wc_MemZero_Check (aes , sizeof (Aes ));
1150111526#endif
11527+
11528+ if (isAllocated ) {
11529+ XFREE (aes , heap , DYNAMIC_TYPE_AES );
11530+ }
11531+
1150211532}
1150311533
1150411534int wc_AesGetKeySize (Aes * aes , word32 * keySize )
@@ -14003,6 +14033,13 @@ static WARN_UNUSED_RESULT int AesSivCipher(
1400314033 }
1400414034 }
1400514035
14036+ #ifndef WOLFSSL_SMALL_STACK
14037+ /* make aes has heap hint and isAllocated initialized for cleanup below */
14038+ if (ret != 0 ) {
14039+ XMEMSET (aes , 0 , sizeof (Aes ));
14040+ }
14041+ #endif
14042+
1400614043 if (ret == 0 && dataSz > 0 ) {
1400714044 sivTmp [12 ] &= 0x7f ;
1400814045 sivTmp [8 ] &= 0x7f ;
@@ -14032,10 +14069,15 @@ static WARN_UNUSED_RESULT int AesSivCipher(
1403214069 }
1403314070 }
1403414071
14035- wc_AesFree (aes );
1403614072#ifdef WOLFSSL_SMALL_STACK
14037- XFREE (aes , NULL , DYNAMIC_TYPE_AES );
14073+ if (aes != NULL )
1403814074#endif
14075+ {
14076+ wc_AesFree (aes );
14077+ #ifdef WOLFSSL_SMALL_STACK
14078+ XFREE (aes , NULL , DYNAMIC_TYPE_AES );
14079+ #endif
14080+ }
1403914081
1404014082 return ret ;
1404114083}
0 commit comments