@@ -10026,7 +10026,8 @@ int wc_AesGcmInit(Aes* aes, const byte* key, word32 len, const byte* iv,
1002610026#if defined(WOLFSSL_SMALL_STACK ) && !defined(WOLFSSL_AESNI )
1002710027 if ((ret == 0 ) && (aes -> streamData == NULL )) {
1002810028 /* Allocate buffers for streaming. */
10029- aes -> streamData = (byte * )XMALLOC (5 * AES_BLOCK_SIZE , aes -> heap ,
10029+ aes -> streamData_sz = 5 * AES_BLOCK_SIZE ;
10030+ aes -> streamData = (byte * )XMALLOC (aes -> streamData_sz , aes -> heap ,
1003010031 DYNAMIC_TYPE_AES );
1003110032 if (aes -> streamData == NULL ) {
1003210033 ret = MEMORY_E ;
@@ -10513,7 +10514,7 @@ int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
1051310514 byte * authTag , word32 authTagSz , WC_RNG * rng )
1051410515{
1051510516#ifdef WOLFSSL_SMALL_STACK
10516- Aes * aes = NULL ;
10517+ Aes * aes ;
1051710518#else
1051810519 Aes aes [1 ];
1051910520#endif
@@ -10526,25 +10527,24 @@ int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
1052610527 }
1052710528
1052810529#ifdef WOLFSSL_SMALL_STACK
10529- if (( aes = ( Aes * ) XMALLOC ( sizeof * aes , NULL ,
10530- DYNAMIC_TYPE_AES )) == NULL )
10531- return MEMORY_E ;
10530+ aes = wc_AesNew ( NULL , INVALID_DEVID , & ret );
10531+ #else
10532+ ret = wc_AesInit ( aes , NULL , INVALID_DEVID ) ;
1053210533#endif
10534+ if (ret != 0 )
10535+ return ret ;
1053310536
10534- ret = wc_AesInit (aes , NULL , INVALID_DEVID );
10535- if (ret == 0 ) {
10536- ret = wc_AesGcmSetKey (aes , key , keySz );
10537- if (ret == 0 )
10538- ret = wc_AesGcmSetIV (aes , ivSz , NULL , 0 , rng );
10539- if (ret == 0 )
10540- ret = wc_AesGcmEncrypt_ex (aes , NULL , NULL , 0 , iv , ivSz ,
10537+ ret = wc_AesGcmSetKey (aes , key , keySz );
10538+ if (ret == 0 )
10539+ ret = wc_AesGcmSetIV (aes , ivSz , NULL , 0 , rng );
10540+ if (ret == 0 )
10541+ ret = wc_AesGcmEncrypt_ex (aes , NULL , NULL , 0 , iv , ivSz ,
1054110542 authTag , authTagSz , authIn , authInSz );
10542- aes -> isAllocated = 0 ;
10543- wc_AesFree (aes );
10544- }
10545- ForceZero (aes , sizeof * aes );
10543+
1054610544#ifdef WOLFSSL_SMALL_STACK
10547- XFREE (aes , NULL , DYNAMIC_TYPE_AES );
10545+ wc_AesDelete (aes , NULL );
10546+ #else
10547+ wc_AesFree (aes );
1054810548#endif
1054910549
1055010550 return ret ;
@@ -10570,24 +10570,21 @@ int wc_GmacVerify(const byte* key, word32 keySz,
1057010570 }
1057110571
1057210572#ifdef WOLFSSL_SMALL_STACK
10573- if ((aes = (Aes * )XMALLOC (sizeof * aes , NULL ,
10574- DYNAMIC_TYPE_AES )) == NULL )
10575- return MEMORY_E ;
10576- #endif
10577-
10573+ aes = wc_AesNew (NULL , INVALID_DEVID , & ret );
10574+ #else
1057810575 ret = wc_AesInit (aes , NULL , INVALID_DEVID );
10576+ #endif
1057910577 if (ret == 0 ) {
1058010578 ret = wc_AesGcmSetKey (aes , key , keySz );
1058110579 if (ret == 0 )
1058210580 ret = wc_AesGcmDecrypt (aes , NULL , NULL , 0 , iv , ivSz ,
1058310581 authTag , authTagSz , authIn , authInSz );
1058410582
10585- aes -> isAllocated = 0 ;
10586- wc_AesFree (aes );
1058710583 }
10588- ForceZero (aes , sizeof * aes );
1058910584#ifdef WOLFSSL_SMALL_STACK
10590- XFREE (aes , NULL , DYNAMIC_TYPE_AES );
10585+ wc_AesDelete (aes , NULL );
10586+ #else
10587+ wc_AesFree (aes );
1059110588#endif
1059210589#else
1059310590 (void )key ;
@@ -11299,41 +11296,54 @@ int wc_AesCcmEncrypt_ex(Aes* aes, byte* out, const byte* in, word32 sz,
1129911296
1130011297#endif /* HAVE_AESCCM */
1130111298
11302- Aes * wc_AesNew (void * heap , int devId )
11299+ #ifndef WC_NO_CONSTRUCTORS
11300+ Aes * wc_AesNew (void * heap , int devId , int * result_code )
1130311301{
11302+ int ret ;
1130411303 Aes * aes = (Aes * )XMALLOC (sizeof (Aes ), heap , DYNAMIC_TYPE_AES );
11305- if (aes != NULL ) {
11306- if (wc_AesInit (aes , heap , devId ) != 0 ) {
11304+ if (aes == NULL ) {
11305+ ret = MEMORY_E ;
11306+ }
11307+ else {
11308+ ret = wc_AesInit (aes , heap , devId );
11309+ if (ret != 0 ) {
1130711310 XFREE (aes , heap , DYNAMIC_TYPE_AES );
1130811311 aes = NULL ;
1130911312 }
11310- else {
11311- aes -> isAllocated = 1 ;
11312- }
1131311313 }
11314+
11315+ if (result_code != NULL )
11316+ * result_code = ret ;
11317+
1131411318 return aes ;
1131511319}
1131611320
11317- /* Initialize Aes for use with async hardware */
11321+ int wc_AesDelete (Aes * aes , Aes * * aes_p )
11322+ {
11323+ if (aes == NULL )
11324+ return BAD_FUNC_ARG ;
11325+ wc_AesFree (aes );
11326+ XFREE (aes , aes -> heap , DYNAMIC_TYPE_AES );
11327+ if (aes_p != NULL )
11328+ * aes_p = NULL ;
11329+ return 0 ;
11330+ }
11331+ #endif /* !WC_NO_CONSTRUCTORS */
11332+
11333+ /* Initialize Aes */
1131811334int wc_AesInit (Aes * aes , void * heap , int devId )
1131911335{
1132011336 int ret = 0 ;
1132111337
1132211338 if (aes == NULL )
1132311339 return BAD_FUNC_ARG ;
1132411340
11325- aes -> isAllocated = 0 ;
11326- aes -> heap = heap ;
11327- aes -> rounds = 0 ;
11341+ XMEMSET (aes , 0 , sizeof (* aes ));
1132811342
11329- #ifdef WOLFSSL_AESNI
11330- /* clear here for the benefit of wc_AesGcmInit(). */
11331- aes -> use_aesni = 0 ;
11332- #endif
11343+ aes -> heap = heap ;
1133311344
1133411345#ifdef WOLF_CRYPTO_CB
1133511346 aes -> devId = devId ;
11336- aes -> devCtx = NULL ;
1133711347#else
1133811348 (void )devId ;
1133911349#endif
@@ -11346,51 +11356,18 @@ int wc_AesInit(Aes* aes, void* heap, int devId)
1134611356 aes -> alFd = WC_SOCK_NOTSET ;
1134711357 aes -> rdFd = WC_SOCK_NOTSET ;
1134811358#endif
11349- #ifdef WOLFSSL_KCAPI_AES
11350- aes -> handle = NULL ;
11351- aes -> init = 0 ;
11352- #endif
1135311359#if defined(WOLFSSL_DEVCRYPTO ) && \
1135411360 (defined(WOLFSSL_DEVCRYPTO_AES ) || defined(WOLFSSL_DEVCRYPTO_CBC ))
1135511361 aes -> ctx .cfd = -1 ;
1135611362#endif
11357- #if defined(WOLFSSL_CRYPTOCELL ) && defined(WOLFSSL_CRYPTOCELL_AES )
11358- XMEMSET (& aes -> ctx , 0 , sizeof (aes -> ctx ));
11359- #endif
1136011363#if defined(WOLFSSL_IMXRT_DCP )
1136111364 DCPAesInit (aes );
1136211365#endif
1136311366
11364- #ifdef WOLFSSL_MAXQ10XX_CRYPTO
11365- XMEMSET (& aes -> maxq_ctx , 0 , sizeof (aes -> maxq_ctx ));
11366- #endif
11367-
11368- #ifdef HAVE_AESGCM
11369- #ifdef OPENSSL_EXTRA
11370- XMEMSET (aes -> gcm .aadH , 0 , sizeof (aes -> gcm .aadH ));
11371- aes -> gcm .aadLen = 0 ;
11372- #endif
11373- #endif
11374-
11375- #ifdef WOLFSSL_AESGCM_STREAM
11376- #if defined(WOLFSSL_SMALL_STACK ) && !defined(WOLFSSL_AESNI )
11377- aes -> streamData = NULL ;
11378- #endif
11379- aes -> keylen = 0 ;
11380- aes -> nonceSz = 0 ;
11381- aes -> gcmKeySet = 0 ;
11382- aes -> nonceSet = 0 ;
11383- aes -> ctrSet = 0 ;
11384- #endif
11385-
1138611367#if defined(WOLFSSL_HAVE_PSA ) && !defined(WOLFSSL_PSA_NO_AES )
1138711368 ret = wc_psa_aes_init (aes );
1138811369#endif
1138911370
11390- #if defined(WOLFSSL_RENESAS_FSPSM )
11391- XMEMSET (& aes -> ctx , 0 , sizeof (aes -> ctx ));
11392- #endif
11393-
1139411371#ifdef WC_DEBUG_CIPHER_LIFECYCLE
1139511372 if (ret == 0 )
1139611373 ret = wc_debug_CipherLifecycleInit (& aes -> CipherLifecycleTag , aes -> heap );
@@ -11445,21 +11422,15 @@ int wc_AesInit_Label(Aes* aes, const char* label, void* heap, int devId)
1144511422}
1144611423#endif
1144711424
11448- /* Free Aes from use with async hardware */
11425+ /* Free Aes resources */
1144911426void wc_AesFree (Aes * aes )
1145011427{
11451- void * heap ;
11452- byte isAllocated ;
11453-
1145411428 if (aes == NULL ) {
1145511429 return ;
1145611430 }
1145711431
11458- heap = aes -> heap ;
11459- isAllocated = aes -> isAllocated ;
11460-
1146111432#ifdef WC_DEBUG_CIPHER_LIFECYCLE
11462- (void )wc_debug_CipherLifecycleFree (& aes -> CipherLifecycleTag , heap , 1 );
11433+ (void )wc_debug_CipherLifecycleFree (& aes -> CipherLifecycleTag , aes -> heap , 1 );
1146311434#endif
1146411435
1146511436#if defined(WOLFSSL_ASYNC_CRYPT ) && defined(WC_ASYNC_ENABLE_AES )
@@ -11497,8 +11468,11 @@ void wc_AesFree(Aes* aes)
1149711468#endif
1149811469#if defined(WOLFSSL_AESGCM_STREAM ) && defined(WOLFSSL_SMALL_STACK ) && \
1149911470 !defined(WOLFSSL_AESNI )
11500- XFREE (aes -> streamData , heap , DYNAMIC_TYPE_AES );
11501- aes -> streamData = NULL ;
11471+ if (aes -> streamData != NULL ) {
11472+ ForceZero (aes -> streamData , aes -> streamData_sz );
11473+ XFREE (aes -> streamData , aes -> heap , DYNAMIC_TYPE_AES );
11474+ aes -> streamData = NULL ;
11475+ }
1150211476#endif
1150311477
1150411478#if defined(WOLFSSL_SE050 ) && defined(WOLFSSL_SE050_CRYPT )
@@ -11521,14 +11495,11 @@ void wc_AesFree(Aes* aes)
1152111495 wc_fspsm_Aesfree (aes );
1152211496#endif
1152311497
11498+ ForceZero (aes , sizeof (Aes ));
11499+
1152411500#ifdef WOLFSSL_CHECK_MEM_ZERO
1152511501 wc_MemZero_Check (aes , sizeof (Aes ));
1152611502#endif
11527-
11528- if (isAllocated ) {
11529- XFREE (aes , heap , DYNAMIC_TYPE_AES );
11530- }
11531-
1153211503}
1153311504
1153411505int wc_AesGetKeySize (Aes * aes , word32 * keySize )
@@ -14017,29 +13988,17 @@ static WARN_UNUSED_RESULT int AesSivCipher(
1401713988 }
1401813989 }
1401913990
14020- #ifdef WOLFSSL_SMALL_STACK
14021- if (ret == 0 ) {
14022- aes = (Aes * )XMALLOC (sizeof (Aes ), NULL , DYNAMIC_TYPE_AES );
14023- if (aes == NULL ) {
14024- ret = MEMORY_E ;
14025- }
14026- }
14027- #endif
14028-
1402913991 if (ret == 0 ) {
13992+ #ifdef WOLFSSL_SMALL_STACK
13993+ aes = wc_AesNew (NULL , INVALID_DEVID , & ret );
13994+ #else
1403013995 ret = wc_AesInit (aes , NULL , INVALID_DEVID );
13996+ #endif
1403113997 if (ret != 0 ) {
1403213998 WOLFSSL_MSG ("Failed to initialized AES object." );
1403313999 }
1403414000 }
1403514001
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-
1404314002 if (ret == 0 && dataSz > 0 ) {
1404414003 sivTmp [12 ] &= 0x7f ;
1404514004 sivTmp [8 ] &= 0x7f ;
@@ -14070,14 +14029,10 @@ static WARN_UNUSED_RESULT int AesSivCipher(
1407014029 }
1407114030
1407214031#ifdef WOLFSSL_SMALL_STACK
14073- if (aes != NULL )
14032+ wc_AesDelete (aes , NULL );
14033+ #else
14034+ wc_AesFree (aes );
1407414035#endif
14075- {
14076- wc_AesFree (aes );
14077- #ifdef WOLFSSL_SMALL_STACK
14078- XFREE (aes , NULL , DYNAMIC_TYPE_AES );
14079- #endif
14080- }
1408114036
1408214037 return ret ;
1408314038}
0 commit comments