@@ -135,20 +135,14 @@ int wc_Sha256Update(wc_Sha256* sha, const byte* in, word32 sz)
135135#ifdef WOLFSSL_DEVCRYPTO_HASH_KEEP
136136 /* keep full message to hash at end instead of incremental updates */
137137 if (sha -> len < sha -> used + sz ) {
138- if (sha -> msg == NULL ) {
139- sha -> msg = (byte * )XMALLOC (sha -> used + sz , sha -> heap ,
140- DYNAMIC_TYPE_TMP_BUFFER );
141- } else {
142- byte * pt = (byte * )XREALLOC (sha -> msg , sha -> used + sz , sha -> heap ,
143- DYNAMIC_TYPE_TMP_BUFFER );
144- if (pt == NULL ) {
145- return MEMORY_E ;
146- }
147- sha -> msg = pt ;
148- }
149- if (sha -> msg == NULL ) {
138+ byte * pt = (byte * )XREALLOC (sha -> msg , sha -> used + sz , sha -> heap ,
139+ DYNAMIC_TYPE_TMP_BUFFER );
140+ if (pt == NULL ) {
150141 return MEMORY_E ;
151142 }
143+
144+ sha -> msg = pt ;
145+
152146 sha -> len = sha -> used + sz ;
153147 }
154148 XMEMCPY (sha -> msg + sha -> used , in , sz );
@@ -180,7 +174,8 @@ int wc_Sha256Final(wc_Sha256* sha, byte* hash)
180174#endif
181175 ret = GetDigest (sha , CRYPTO_SHA2_256 , hash );
182176 if (ret != 0 ) {
183- return ret ;
177+ wc_Sha256Free (sha );
178+ return ret ;
184179 }
185180
186181 wc_Sha256Free (sha );
@@ -190,6 +185,7 @@ int wc_Sha256Final(wc_Sha256* sha, byte* hash)
190185
191186int wc_Sha256GetHash (wc_Sha256 * sha , byte * hash )
192187{
188+
193189 if (sha == NULL || hash == NULL ) {
194190 return BAD_FUNC_ARG ;
195191 }
@@ -198,9 +194,15 @@ int wc_Sha256GetHash(wc_Sha256* sha, byte* hash)
198194 {
199195 int ret ;
200196 wc_Sha256 cpy ;
201- wc_Sha256Copy (sha , & cpy );
202-
203- if ((ret = HashUpdate (& cpy , CRYPTO_SHA2_256 , cpy .msg , cpy .used )) == 0 ) {
197+ XMEMSET (& cpy , 0 , sizeof (cpy )); /* ZII */
198+ /* mark as having no /dev/crypto session yet so the wc_Sha256Free()
199+ * in wc_Sha256Copy() does not close fd 0 (cfd == -1 is the
200+ * "no session" sentinel, matching wc_AesInit()) */
201+ cpy .ctx .cfd = -1 ;
202+ ret = wc_Sha256Copy (sha , & cpy );
203+
204+ if (ret == 0 &&
205+ (ret = HashUpdate (& cpy , CRYPTO_SHA2_256 , cpy .msg , cpy .used )) == 0 ) {
204206 /* help static analysis tools out */
205207 XMEMSET (hash , 0 , WC_SHA256_DIGEST_SIZE );
206208 ret = GetDigest (& cpy , CRYPTO_SHA2_256 , hash );
@@ -219,22 +221,37 @@ int wc_Sha256GetHash(wc_Sha256* sha, byte* hash)
219221
220222int wc_Sha256Copy (wc_Sha256 * src , wc_Sha256 * dst )
221223{
224+ int ret = 0 ;
225+
222226 if (src == NULL || dst == NULL ) {
223227 return BAD_FUNC_ARG ;
224228 }
225229
226- wc_InitSha256_ex (dst , src -> heap , 0 );
227230#ifdef WOLFSSL_DEVCRYPTO_HASH_KEEP
231+ if (dst -> ctx .cfd > 0 ) {
232+ wc_Sha256Free (dst );
233+ }
234+ if ((ret = wc_InitSha256_ex (dst , src -> heap , 0 )) != 0 ) {
235+ dst -> ctx .cfd = -1 ;
236+ return ret ;
237+ }
228238 dst -> len = src -> len ;
229239 dst -> used = src -> used ;
230240 dst -> msg = (byte * )XMALLOC (src -> len , dst -> heap , DYNAMIC_TYPE_TMP_BUFFER );
231241 if (dst -> msg == NULL ) {
242+ wc_Sha256Free (dst );
232243 return MEMORY_E ;
233244 }
234245 XMEMCPY (dst -> msg , src -> msg , src -> len );
235- #endif
236246
237- return 0 ;
247+ return ret ;
248+ #else
249+ (void )src ;
250+ (void )dst ;
251+
252+ WOLFSSL_MSG ("Compile with WOLFSSL_DEVCRYPTO_HASH_KEEP for this feature" );
253+ return NOT_COMPILED_IN ;
254+ #endif
238255}
239256
240257#endif /* !NO_SHA256 */
0 commit comments