@@ -981,15 +981,18 @@ static int header_sha256(wc_Sha256 *sha256_ctx, struct wolfBoot_image *img)
981981 stored_sha_len = get_header (img , HDR_SHA256 , & stored_sha );
982982 if (stored_sha_len != WOLFBOOT_SHA_DIGEST_SIZE )
983983 return -1 ;
984+ end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
985+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
986+ if (end_sha <= p ) {
987+ return -1 ;
988+ }
989+ #endif
984990#ifdef WOLFBOOT_ENABLE_WOLFHSM_CLIENT
985991 (void )wc_InitSha256_ex (sha256_ctx , NULL , hsmDevIdHash );
986992#else
987993 wc_InitSha256 (sha256_ctx );
988994#endif
989- end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
990995#ifdef WOLFBOOT_IMG_HASH_ONESHOT
991- if (end_sha <= p )
992- return -1 ;
993996 wc_Sha256Update (sha256_ctx , p , (word32 )(end_sha - p ));
994997#else
995998 {
@@ -1020,8 +1023,10 @@ static int image_sha256(struct wolfBoot_image *img, uint8_t *hash)
10201023 if (header_sha256 (& sha256_ctx , img ) != 0 )
10211024 return -1 ;
10221025#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1023- if (img -> fw_base == NULL )
1026+ if (img -> fw_base == NULL ) {
1027+ wc_Sha256Free (& sha256_ctx );
10241028 return -1 ;
1029+ }
10251030 wc_Sha256Update (& sha256_ctx , img -> fw_base , img -> fw_size );
10261031#else
10271032 {
@@ -1088,15 +1093,18 @@ static int header_sha384(wc_Sha384 *sha384_ctx, struct wolfBoot_image *img)
10881093 stored_sha_len = get_header (img , HDR_SHA384 , & stored_sha );
10891094 if (stored_sha_len != WOLFBOOT_SHA_DIGEST_SIZE )
10901095 return -1 ;
1096+ end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
1097+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
1098+ if (end_sha <= p ) {
1099+ return -1 ;
1100+ }
1101+ #endif
10911102#ifdef WOLFBOOT_ENABLE_WOLFHSM_CLIENT
10921103 (void )wc_InitSha384_ex (sha384_ctx , NULL , hsmDevIdHash );
10931104#else
10941105 wc_InitSha384 (sha384_ctx );
10951106#endif
1096- end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
10971107#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1098- if (end_sha <= p )
1099- return -1 ;
11001108 wc_Sha384Update (sha384_ctx , p , (word32 )(end_sha - p ));
11011109#else
11021110 {
@@ -1130,8 +1138,10 @@ static int image_sha384(struct wolfBoot_image *img, uint8_t *hash)
11301138 if (header_sha384 (& sha384_ctx , img ) != 0 )
11311139 return -1 ;
11321140#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1133- if (img -> fw_base == NULL )
1141+ if (img -> fw_base == NULL ) {
1142+ wc_Sha384Free (& sha384_ctx );
11341143 return -1 ;
1144+ }
11351145 wc_Sha384Update (& sha384_ctx , img -> fw_base , img -> fw_size );
11361146#else
11371147 {
@@ -1205,11 +1215,14 @@ static int header_sha3_384(wc_Sha3 *sha3_ctx, struct wolfBoot_image *img)
12051215 stored_sha_len = get_header (img , HDR_SHA3_384 , & stored_sha );
12061216 if (stored_sha_len != WOLFBOOT_SHA_DIGEST_SIZE )
12071217 return -1 ;
1208- wc_InitSha3_384 (sha3_ctx , NULL , INVALID_DEVID );
12091218 end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
12101219#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1211- if (end_sha <= p )
1220+ if (end_sha <= p ) {
12121221 return -1 ;
1222+ }
1223+ #endif
1224+ wc_InitSha3_384 (sha3_ctx , NULL , INVALID_DEVID );
1225+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
12131226 wc_Sha3_384_Update (sha3_ctx , p , (word32 )(end_sha - p ));
12141227#else
12151228 {
@@ -1242,8 +1255,10 @@ static int image_sha3_384(struct wolfBoot_image *img, uint8_t *hash)
12421255 if (header_sha3_384 (& sha3_ctx , img ) != 0 )
12431256 return -1 ;
12441257#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1245- if (img -> fw_base == NULL )
1258+ if (img -> fw_base == NULL ) {
1259+ wc_Sha3_384_Free (& sha3_ctx );
12461260 return -1 ;
1261+ }
12471262 wc_Sha3_384_Update (& sha3_ctx , img -> fw_base , img -> fw_size );
12481263#else
12491264 {
@@ -1777,6 +1792,12 @@ static int update_hash_flash_fwimg(wolfBoot_hash_t* ctx,
17771792 uint32_t size )
17781793{
17791794#ifdef WOLFBOOT_IMG_HASH_ONESHOT
1795+ if (img -> fw_base == NULL ) {
1796+ return -1 ;
1797+ }
1798+ if ((uint64_t )offset + size > img -> fw_size ) {
1799+ return -1 ;
1800+ }
17801801 update_hash (ctx , img -> fw_base + offset , size );
17811802 return 0 ;
17821803#else
0 commit comments