@@ -1006,6 +1006,7 @@ int wc_SRTP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
10061006#else
10071007 Aes aes [1 ];
10081008#endif
1009+ int aes_inited = 0 ;
10091010
10101011 /* Validate parameters. */
10111012 if ((key == NULL ) || (keySz > AES_256_KEY_SIZE ) || (salt == NULL ) ||
@@ -1031,6 +1032,7 @@ int wc_SRTP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
10311032 ret = wc_AesInit (aes , NULL , INVALID_DEVID );
10321033 }
10331034 if (ret == 0 ) {
1035+ aes_inited = 1 ;
10341036 ret = wc_AesSetKey (aes , key , keySz , NULL , AES_ENCRYPTION );
10351037 }
10361038
@@ -1056,8 +1058,8 @@ int wc_SRTP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
10561058 WC_SRTP_LABEL_SALT , key3 , key3Sz , aes );
10571059 }
10581060
1059- /* AES object memset so can always free. */
1060- wc_AesFree (aes );
1061+ if ( aes_inited )
1062+ wc_AesFree (aes );
10611063#ifdef WOLFSSL_SMALL_STACK
10621064 XFREE (aes , NULL , DYNAMIC_TYPE_CIPHER );
10631065#endif
@@ -1099,6 +1101,7 @@ int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
10991101#else
11001102 Aes aes [1 ];
11011103#endif
1104+ int aes_inited = 0 ;
11021105
11031106 /* Validate parameters. */
11041107 if ((key == NULL ) || (keySz > AES_256_KEY_SIZE ) || (salt == NULL ) ||
@@ -1124,6 +1127,7 @@ int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
11241127 ret = wc_AesInit (aes , NULL , INVALID_DEVID );
11251128 }
11261129 if (ret == 0 ) {
1130+ aes_inited = 1 ;
11271131 ret = wc_AesSetKey (aes , key , keySz , NULL , AES_ENCRYPTION );
11281132 }
11291133
@@ -1149,8 +1153,8 @@ int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
11491153 WC_SRTCP_LABEL_SALT , key3 , key3Sz , aes );
11501154 }
11511155
1152- /* AES object memset so can always free. */
1153- wc_AesFree (aes );
1156+ if ( aes_inited )
1157+ wc_AesFree (aes );
11541158#ifdef WOLFSSL_SMALL_STACK
11551159 XFREE (aes , NULL , DYNAMIC_TYPE_CIPHER );
11561160#endif
@@ -1189,6 +1193,7 @@ int wc_SRTP_KDF_label(const byte* key, word32 keySz, const byte* salt,
11891193#else
11901194 Aes aes [1 ];
11911195#endif
1196+ int aes_inited = 0 ;
11921197
11931198 /* Validate parameters. */
11941199 if ((key == NULL ) || (keySz > AES_256_KEY_SIZE ) || (salt == NULL ) ||
@@ -1215,6 +1220,7 @@ int wc_SRTP_KDF_label(const byte* key, word32 keySz, const byte* salt,
12151220 ret = wc_AesInit (aes , NULL , INVALID_DEVID );
12161221 }
12171222 if (ret == 0 ) {
1223+ aes_inited = 1 ;
12181224 ret = wc_AesSetKey (aes , key , keySz , NULL , AES_ENCRYPTION );
12191225 }
12201226
@@ -1229,8 +1235,8 @@ int wc_SRTP_KDF_label(const byte* key, word32 keySz, const byte* salt,
12291235 outKeySz , aes );
12301236 }
12311237
1232- /* AES object memset so can always free. */
1233- wc_AesFree (aes );
1238+ if ( aes_inited )
1239+ wc_AesFree (aes );
12341240#ifdef WOLFSSL_SMALL_STACK
12351241 XFREE (aes , NULL , DYNAMIC_TYPE_CIPHER );
12361242#endif
@@ -1270,6 +1276,7 @@ int wc_SRTCP_KDF_label(const byte* key, word32 keySz, const byte* salt,
12701276#else
12711277 Aes aes [1 ];
12721278#endif
1279+ int aes_inited = 0 ;
12731280
12741281 /* Validate parameters. */
12751282 if ((key == NULL ) || (keySz > AES_256_KEY_SIZE ) || (salt == NULL ) ||
@@ -1296,6 +1303,7 @@ int wc_SRTCP_KDF_label(const byte* key, word32 keySz, const byte* salt,
12961303 ret = wc_AesInit (aes , NULL , INVALID_DEVID );
12971304 }
12981305 if (ret == 0 ) {
1306+ aes_inited = 1 ;
12991307 ret = wc_AesSetKey (aes , key , keySz , NULL , AES_ENCRYPTION );
13001308 }
13011309
@@ -1310,8 +1318,8 @@ int wc_SRTCP_KDF_label(const byte* key, word32 keySz, const byte* salt,
13101318 outKeySz , aes );
13111319 }
13121320
1313- /* AES object memset so can always free. */
1314- wc_AesFree (aes );
1321+ if ( aes_inited )
1322+ wc_AesFree (aes );
13151323#ifdef WOLFSSL_SMALL_STACK
13161324 XFREE (aes , NULL , DYNAMIC_TYPE_CIPHER );
13171325#endif
0 commit comments