@@ -2546,7 +2546,7 @@ int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
25462546 return BAD_FUNC_ARG ;
25472547 }
25482548
2549- WC_ALLOC_VAR_EX (tmpSha224 , wc_Sha224 , 1 , NULL ,
2549+ WC_CALLOC_VAR_EX (tmpSha224 , wc_Sha224 , 1 , NULL ,
25502550 DYNAMIC_TYPE_TMP_BUFFER , return MEMORY_E );
25512551
25522552 ret = wc_Sha224Copy (sha224 , tmpSha224 );
@@ -2582,6 +2582,9 @@ int wc_Sha224_Grow(wc_Sha224* sha224, const byte* in, int inSz)
25822582 ret = 0 ; /* Reset ret to 0 to avoid returning the callback error code */
25832583#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
25842584
2585+ /* Free dst resources before copy to prevent memory leaks (e.g., msg
2586+ * buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
2587+ wc_Sha224Free (dst );
25852588 XMEMCPY (dst , src , sizeof (wc_Sha224 ));
25862589
25872590 #ifdef WOLFSSL_SMALL_STACK_CACHE
@@ -2691,7 +2694,7 @@ int wc_Sha256GetHash(wc_Sha256* sha256, byte* hash)
26912694 return BAD_FUNC_ARG ;
26922695 }
26932696
2694- WC_ALLOC_VAR_EX (tmpSha256 , wc_Sha256 , 1 , NULL , DYNAMIC_TYPE_TMP_BUFFER ,
2697+ WC_CALLOC_VAR_EX (tmpSha256 , wc_Sha256 , 1 , NULL , DYNAMIC_TYPE_TMP_BUFFER ,
26952698 return MEMORY_E );
26962699
26972700 ret = wc_Sha256Copy (sha256 , tmpSha256 );
@@ -2728,6 +2731,9 @@ int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
27282731 ret = 0 ; /* Reset ret to 0 to avoid returning the callback error code */
27292732#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */
27302733
2734+ /* Free dst resources before copy to prevent memory leaks (e.g., msg
2735+ * buffer, W cache, hardware contexts). XMEMCPY overwrites dst. */
2736+ wc_Sha256Free (dst );
27312737 XMEMCPY (dst , src , sizeof (wc_Sha256 ));
27322738
27332739#ifdef WOLFSSL_MAXQ10XX_CRYPTO
0 commit comments