@@ -982,15 +982,18 @@ static int header_sha256(wc_Sha256 *sha256_ctx, struct wolfBoot_image *img)
982982 stored_sha_len = get_header (img , HDR_SHA256 , & stored_sha );
983983 if (stored_sha_len != WOLFBOOT_SHA_DIGEST_SIZE )
984984 return -1 ;
985+ end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
986+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
987+ if (end_sha <= p ) {
988+ return -1 ;
989+ }
990+ #endif
985991#ifdef WOLFBOOT_ENABLE_WOLFHSM_CLIENT
986992 (void )wc_InitSha256_ex (sha256_ctx , NULL , hsmDevIdHash );
987993#else
988994 wc_InitSha256 (sha256_ctx );
989995#endif
990- end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
991996#ifdef WOLFBOOT_IMG_HASH_ONESHOT
992- if (end_sha <= p )
993- return -1 ;
994997 wc_Sha256Update (sha256_ctx , p , (word32 )(end_sha - p ));
995998#else
996999 {
@@ -1021,8 +1024,10 @@ static int image_sha256(struct wolfBoot_image *img, uint8_t *hash)
10211024 if (header_sha256 (& sha256_ctx , img ) != 0 )
10221025 return -1 ;
10231026#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1024- if (img -> fw_base == NULL )
1027+ if (img -> fw_base == NULL ) {
1028+ wc_Sha256Free (& sha256_ctx );
10251029 return -1 ;
1030+ }
10261031 wc_Sha256Update (& sha256_ctx , img -> fw_base , img -> fw_size );
10271032#else
10281033 {
@@ -1089,15 +1094,18 @@ static int header_sha384(wc_Sha384 *sha384_ctx, struct wolfBoot_image *img)
10891094 stored_sha_len = get_header (img , HDR_SHA384 , & stored_sha );
10901095 if (stored_sha_len != WOLFBOOT_SHA_DIGEST_SIZE )
10911096 return -1 ;
1097+ end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
1098+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
1099+ if (end_sha <= p ) {
1100+ return -1 ;
1101+ }
1102+ #endif
10921103#ifdef WOLFBOOT_ENABLE_WOLFHSM_CLIENT
10931104 (void )wc_InitSha384_ex (sha384_ctx , NULL , hsmDevIdHash );
10941105#else
10951106 wc_InitSha384 (sha384_ctx );
10961107#endif
1097- end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
10981108#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1099- if (end_sha <= p )
1100- return -1 ;
11011109 wc_Sha384Update (sha384_ctx , p , (word32 )(end_sha - p ));
11021110#else
11031111 {
@@ -1131,8 +1139,10 @@ static int image_sha384(struct wolfBoot_image *img, uint8_t *hash)
11311139 if (header_sha384 (& sha384_ctx , img ) != 0 )
11321140 return -1 ;
11331141#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1134- if (img -> fw_base == NULL )
1142+ if (img -> fw_base == NULL ) {
1143+ wc_Sha384Free (& sha384_ctx );
11351144 return -1 ;
1145+ }
11361146 wc_Sha384Update (& sha384_ctx , img -> fw_base , img -> fw_size );
11371147#else
11381148 {
@@ -1206,11 +1216,14 @@ static int header_sha3_384(wc_Sha3 *sha3_ctx, struct wolfBoot_image *img)
12061216 stored_sha_len = get_header (img , HDR_SHA3_384 , & stored_sha );
12071217 if (stored_sha_len != WOLFBOOT_SHA_DIGEST_SIZE )
12081218 return -1 ;
1209- wc_InitSha3_384 (sha3_ctx , NULL , INVALID_DEVID );
12101219 end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
12111220#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1212- if (end_sha <= p )
1221+ if (end_sha <= p ) {
12131222 return -1 ;
1223+ }
1224+ #endif
1225+ wc_InitSha3_384 (sha3_ctx , NULL , INVALID_DEVID );
1226+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
12141227 wc_Sha3_384_Update (sha3_ctx , p , (word32 )(end_sha - p ));
12151228#else
12161229 {
@@ -1243,8 +1256,10 @@ static int image_sha3_384(struct wolfBoot_image *img, uint8_t *hash)
12431256 if (header_sha3_384 (& sha3_ctx , img ) != 0 )
12441257 return -1 ;
12451258#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1246- if (img -> fw_base == NULL )
1259+ if (img -> fw_base == NULL ) {
1260+ wc_Sha3_384_Free (& sha3_ctx );
12471261 return -1 ;
1262+ }
12481263 wc_Sha3_384_Update (& sha3_ctx , img -> fw_base , img -> fw_size );
12491264#else
12501265 {
@@ -1782,6 +1797,12 @@ static int update_hash_flash_fwimg(wolfBoot_hash_t* ctx,
17821797 uint32_t size )
17831798{
17841799#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1800+ if (img -> fw_base == NULL ) {
1801+ return -1 ;
1802+ }
1803+ if ((uint64_t )offset + size > img -> fw_size ) {
1804+ return -1 ;
1805+ }
17851806 update_hash (ctx , img -> fw_base + offset , size );
17861807 return 0 ;
17871808#else
0 commit comments