Skip to content

Commit b195628

Browse files
committed
wolfcrypt/src/sha512.c: fix underinitialization and config-dependent leak paths in InitSha512_Family().
1 parent fc68137 commit b195628

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

wolfcrypt/src/sha512.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@ static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId,
867867
return BAD_FUNC_ARG;
868868
}
869869

870+
XMEMSET(sha512, 0, sizeof(*sha512));
870871

871872
sha512->heap = heap;
872873
#ifdef WOLFSSL_SMALL_STACK_CACHE
@@ -884,26 +885,33 @@ static int InitSha512_Family(wc_Sha512* sha512, void* heap, int devId,
884885
sha512->devCtx = NULL;
885886
#endif
886887

887-
/* call the initialization function pointed to by initfp */
888-
ret = initfp(sha512);
889-
if (ret != 0)
890-
return ret;
891-
892888
#ifdef WOLFSSL_HASH_KEEP
893889
sha512->msg = NULL;
894-
sha512->len = 0;
895-
sha512->used = 0;
896890
#endif
897891

892+
/* call the initialization function pointed to by initfp */
893+
ret = initfp(sha512);
894+
898895
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
899-
ret = wolfAsync_DevCtxInit(&sha512->asyncDev,
896+
if (ret == 0) {
897+
ret = wolfAsync_DevCtxInit(&sha512->asyncDev,
900898
WOLFSSL_ASYNC_MARKER_SHA512, sha512->heap, devId);
899+
}
901900
#else
902901
(void)devId;
903902
#endif /* WOLFSSL_ASYNC_CRYPT */
904903
#ifdef WOLFSSL_IMXRT1170_CAAM
905-
ret = wc_CAAM_HashInit(&sha512->hndl, &sha512->ctx, WC_HASH_TYPE_SHA512);
904+
if (ret == 0)
905+
ret = wc_CAAM_HashInit(&sha512->hndl, &sha512->ctx, WC_HASH_TYPE_SHA512);
906+
#endif
907+
908+
#ifdef WOLFSSL_SMALL_STACK_CACHE
909+
if (ret != 0) {
910+
XFREE(sha512->W, sha512->heap, DYNAMIC_TYPE_DIGEST);
911+
sha512->W = NULL;
912+
}
906913
#endif
914+
907915
return ret;
908916
} /* InitSha512_Family */
909917

0 commit comments

Comments
 (0)