@@ -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 );
@@ -198,9 +193,15 @@ int wc_Sha256GetHash(wc_Sha256* sha, byte* hash)
198193 {
199194 int ret ;
200195 wc_Sha256 cpy ;
201- wc_Sha256Copy (sha , & cpy );
202-
203- if ((ret = HashUpdate (& cpy , CRYPTO_SHA2_256 , cpy .msg , cpy .used )) == 0 ) {
196+ XMEMSET (& cpy , 0 , sizeof (cpy )); /* ZII */
197+ /* mark as having no /dev/crypto session yet so the wc_Sha256Free()
198+ * in wc_Sha256Copy() does not close fd 0 (cfd == -1 is the
199+ * "no session" sentinel, matching wc_AesInit()) */
200+ cpy .ctx .cfd = -1 ;
201+ ret = wc_Sha256Copy (sha , & cpy );
202+
203+ if (ret == 0 &&
204+ (ret = HashUpdate (& cpy , CRYPTO_SHA2_256 , cpy .msg , cpy .used )) == 0 ) {
204205 /* help static analysis tools out */
205206 XMEMSET (hash , 0 , WC_SHA256_DIGEST_SIZE );
206207 ret = GetDigest (& cpy , CRYPTO_SHA2_256 , hash );
@@ -219,22 +220,37 @@ int wc_Sha256GetHash(wc_Sha256* sha, byte* hash)
219220
220221int wc_Sha256Copy (wc_Sha256 * src , wc_Sha256 * dst )
221222{
223+ int ret = 0 ;
224+
222225 if (src == NULL || dst == NULL ) {
223226 return BAD_FUNC_ARG ;
224227 }
225228
226- wc_InitSha256_ex (dst , src -> heap , 0 );
227229#ifdef WOLFSSL_DEVCRYPTO_HASH_KEEP
230+ if (dst -> ctx .cfd > 0 ) {
231+ wc_Sha256Free (dst );
232+ }
233+ if ((ret = wc_InitSha256_ex (dst , src -> heap , 0 )) != 0 ) {
234+ dst -> ctx .cfd = -1 ;
235+ return ret ;
236+ }
228237 dst -> len = src -> len ;
229238 dst -> used = src -> used ;
230239 dst -> msg = (byte * )XMALLOC (src -> len , dst -> heap , DYNAMIC_TYPE_TMP_BUFFER );
231240 if (dst -> msg == NULL ) {
241+ wc_Sha256Free (dst );
232242 return MEMORY_E ;
233243 }
234244 XMEMCPY (dst -> msg , src -> msg , src -> len );
235- #endif
236245
237- return 0 ;
246+ return ret ;
247+ #else
248+ (void )src ;
249+ (void )dst ;
250+
251+ WOLFSSL_MSG ("Compile with WOLFSSL_DEVCRYPTO_HASH_KEEP for this feature" );
252+ return NOT_COMPILED_IN ;
253+ #endif
238254}
239255
240256#endif /* !NO_SHA256 */
0 commit comments