@@ -974,8 +974,7 @@ static int header_sha256(wc_Sha256 *sha256_ctx, struct wolfBoot_image *img)
974974{
975975 uint8_t * stored_sha , * end_sha ;
976976 uint16_t stored_sha_len ;
977- uint8_t * p ;
978- int blksz ;
977+ uint8_t * p ;
979978 if (!img )
980979 return -1 ;
981980
@@ -989,13 +988,20 @@ static int header_sha256(wc_Sha256 *sha256_ctx, struct wolfBoot_image *img)
989988 wc_InitSha256 (sha256_ctx );
990989#endif
991990 end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
992- while (p < end_sha ) {
993- blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
994- if (end_sha - p < blksz )
995- blksz = end_sha - p ;
996- wc_Sha256Update (sha256_ctx , p , blksz );
997- p += blksz ;
991+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
992+ wc_Sha256Update (sha256_ctx , p , (word32 )(end_sha - p ));
993+ #else
994+ {
995+ int blksz ;
996+ while (p < end_sha ) {
997+ blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
998+ if (end_sha - p < blksz )
999+ blksz = end_sha - p ;
1000+ wc_Sha256Update (sha256_ctx , p , blksz );
1001+ p += blksz ;
1002+ }
9981003 }
1004+ #endif
9991005 return 0 ;
10001006}
10011007
@@ -1008,23 +1014,29 @@ static int header_sha256(wc_Sha256 *sha256_ctx, struct wolfBoot_image *img)
10081014 */
10091015static int image_sha256 (struct wolfBoot_image * img , uint8_t * hash )
10101016{
1011- uint32_t position = 0 ;
1012- uint8_t * p ;
1013- int blksz ;
10141017 wc_Sha256 sha256_ctx ;
10151018
10161019 if (header_sha256 (& sha256_ctx , img ) != 0 )
10171020 return -1 ;
1018- do {
1019- p = get_sha_block (img , position );
1020- if (p == NULL )
1021- break ;
1022- blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
1023- if (position + blksz > img -> fw_size )
1024- blksz = img -> fw_size - position ;
1025- wc_Sha256Update (& sha256_ctx , p , blksz );
1026- position += blksz ;
1027- } while (position < img -> fw_size );
1021+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
1022+ wc_Sha256Update (& sha256_ctx , img -> fw_base , img -> fw_size );
1023+ #else
1024+ {
1025+ uint32_t position = 0 ;
1026+ uint8_t * p ;
1027+ int blksz ;
1028+ do {
1029+ p = get_sha_block (img , position );
1030+ if (p == NULL )
1031+ break ;
1032+ blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
1033+ if (position + blksz > img -> fw_size )
1034+ blksz = img -> fw_size - position ;
1035+ wc_Sha256Update (& sha256_ctx , p , blksz );
1036+ position += blksz ;
1037+ } while (position < img -> fw_size );
1038+ }
1039+ #endif
10281040
10291041 wc_Sha256Final (& sha256_ctx , hash );
10301042 wc_Sha256Free (& sha256_ctx );
@@ -1065,8 +1077,7 @@ static int header_sha384(wc_Sha384 *sha384_ctx, struct wolfBoot_image *img)
10651077{
10661078 uint16_t stored_sha_len ;
10671079 uint8_t * stored_sha , * end_sha ;
1068- uint8_t * p ;
1069- int blksz ;
1080+ uint8_t * p ;
10701081 if (!img )
10711082 return -1 ;
10721083
@@ -1080,13 +1091,20 @@ static int header_sha384(wc_Sha384 *sha384_ctx, struct wolfBoot_image *img)
10801091 wc_InitSha384 (sha384_ctx );
10811092#endif
10821093 end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
1083- while (p < end_sha ) {
1084- blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
1085- if (end_sha - p < blksz )
1086- blksz = end_sha - p ;
1087- wc_Sha384Update (sha384_ctx , p , blksz );
1088- p += blksz ;
1094+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
1095+ wc_Sha384Update (sha384_ctx , p , (word32 )(end_sha - p ));
1096+ #else
1097+ {
1098+ int blksz ;
1099+ while (p < end_sha ) {
1100+ blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
1101+ if (end_sha - p < blksz )
1102+ blksz = end_sha - p ;
1103+ wc_Sha384Update (sha384_ctx , p , blksz );
1104+ p += blksz ;
1105+ }
10891106 }
1107+ #endif
10901108 return 0 ;
10911109}
10921110
@@ -1102,23 +1120,29 @@ static int header_sha384(wc_Sha384 *sha384_ctx, struct wolfBoot_image *img)
11021120 */
11031121static int image_sha384 (struct wolfBoot_image * img , uint8_t * hash )
11041122{
1105- uint32_t position = 0 ;
1106- uint8_t * p ;
1107- int blksz ;
11081123 wc_Sha384 sha384_ctx ;
11091124
11101125 if (header_sha384 (& sha384_ctx , img ) != 0 )
11111126 return -1 ;
1112- do {
1113- p = get_sha_block (img , position );
1114- if (p == NULL )
1115- break ;
1116- blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
1117- if (position + blksz > img -> fw_size )
1118- blksz = img -> fw_size - position ;
1119- wc_Sha384Update (& sha384_ctx , p , blksz );
1120- position += blksz ;
1121- } while (position < img -> fw_size );
1127+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
1128+ wc_Sha384Update (& sha384_ctx , img -> fw_base , img -> fw_size );
1129+ #else
1130+ {
1131+ uint32_t position = 0 ;
1132+ uint8_t * p ;
1133+ int blksz ;
1134+ do {
1135+ p = get_sha_block (img , position );
1136+ if (p == NULL )
1137+ break ;
1138+ blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
1139+ if (position + blksz > img -> fw_size )
1140+ blksz = img -> fw_size - position ;
1141+ wc_Sha384Update (& sha384_ctx , p , blksz );
1142+ position += blksz ;
1143+ } while (position < img -> fw_size );
1144+ }
1145+ #endif
11221146
11231147 wc_Sha384Final (& sha384_ctx , hash );
11241148 wc_Sha384Free (& sha384_ctx );
@@ -1165,8 +1189,7 @@ static int header_sha3_384(wc_Sha3 *sha3_ctx, struct wolfBoot_image *img)
11651189{
11661190 uint16_t stored_sha_len ;
11671191 uint8_t * stored_sha , * end_sha ;
1168- uint8_t * p ;
1169- int blksz ;
1192+ uint8_t * p ;
11701193
11711194 if (!img )
11721195 return -1 ;
@@ -1177,13 +1200,20 @@ static int header_sha3_384(wc_Sha3 *sha3_ctx, struct wolfBoot_image *img)
11771200 return -1 ;
11781201 wc_InitSha3_384 (sha3_ctx , NULL , INVALID_DEVID );
11791202 end_sha = stored_sha - (2 * sizeof (uint16_t )); /* Subtract 2 Type + 2 Len */
1180- while (p < end_sha ) {
1181- blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
1182- if (end_sha - p < blksz )
1183- blksz = end_sha - p ;
1184- wc_Sha3_384_Update (sha3_ctx , p , blksz );
1185- p += blksz ;
1203+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
1204+ wc_Sha3_384_Update (sha3_ctx , p , (word32 )(end_sha - p ));
1205+ #else
1206+ {
1207+ int blksz ;
1208+ while (p < end_sha ) {
1209+ blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
1210+ if (end_sha - p < blksz )
1211+ blksz = end_sha - p ;
1212+ wc_Sha3_384_Update (sha3_ctx , p , blksz );
1213+ p += blksz ;
1214+ }
11861215 }
1216+ #endif
11871217 return 0 ;
11881218}
11891219
@@ -1198,23 +1228,29 @@ static int header_sha3_384(wc_Sha3 *sha3_ctx, struct wolfBoot_image *img)
11981228 */
11991229static int image_sha3_384 (struct wolfBoot_image * img , uint8_t * hash )
12001230{
1201- uint8_t * p ;
1202- int blksz ;
1203- uint32_t position = 0 ;
12041231 wc_Sha3 sha3_ctx ;
12051232
12061233 if (header_sha3_384 (& sha3_ctx , img ) != 0 )
12071234 return -1 ;
1208- do {
1209- p = get_sha_block (img , position );
1210- if (p == NULL )
1211- break ;
1212- blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
1213- if (position + blksz > img -> fw_size )
1214- blksz = img -> fw_size - position ;
1215- wc_Sha3_384_Update (& sha3_ctx , p , blksz );
1216- position += blksz ;
1217- } while (position < img -> fw_size );
1235+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
1236+ wc_Sha3_384_Update (& sha3_ctx , img -> fw_base , img -> fw_size );
1237+ #else
1238+ {
1239+ uint8_t * p ;
1240+ int blksz ;
1241+ uint32_t position = 0 ;
1242+ do {
1243+ p = get_sha_block (img , position );
1244+ if (p == NULL )
1245+ break ;
1246+ blksz = WOLFBOOT_SHA_BLOCK_SIZE ;
1247+ if (position + blksz > img -> fw_size )
1248+ blksz = img -> fw_size - position ;
1249+ wc_Sha3_384_Update (& sha3_ctx , p , blksz );
1250+ position += blksz ;
1251+ } while (position < img -> fw_size );
1252+ }
1253+ #endif
12181254
12191255 wc_Sha3_384_Final (& sha3_ctx , hash );
12201256 wc_Sha3_384_Free (& sha3_ctx );
@@ -1733,6 +1769,10 @@ static int update_hash_flash_fwimg(wolfBoot_hash_t* ctx,
17331769 struct wolfBoot_image * img , uint32_t offset ,
17341770 uint32_t size )
17351771{
1772+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
1773+ update_hash (ctx , img -> fw_base + offset , size );
1774+ return 0 ;
1775+ #else
17361776 uint32_t current_offset = offset ;
17371777 uint32_t remaining_size = size ;
17381778 uint8_t read_buf [WOLFBOOT_SHA_BLOCK_SIZE ] XALIGNED_STACK (4 ); /* Use local buffer */
@@ -1755,6 +1795,7 @@ static int update_hash_flash_fwimg(wolfBoot_hash_t* ctx,
17551795 current_offset += read_size ;
17561796 }
17571797 return 0 ;
1798+ #endif
17581799}
17591800
17601801/*
@@ -1764,6 +1805,11 @@ static int update_hash_flash_fwimg(wolfBoot_hash_t* ctx,
17641805static int update_hash_flash_addr (wolfBoot_hash_t * ctx , uintptr_t addr ,
17651806 uint32_t size , int src_ext )
17661807{
1808+ #ifdef WOLFBOOT_IMG_HASH_ONESHOT
1809+ (void )src_ext ;
1810+ update_hash (ctx , (uint8_t * )addr , size );
1811+ return 0 ;
1812+ #else
17671813 uint8_t buffer [WOLFBOOT_SHA_BLOCK_SIZE ] XALIGNED_STACK (4 );
17681814 uint32_t remaining_size = size ;
17691815 uintptr_t current_addr = addr ;
@@ -1788,6 +1834,7 @@ static int update_hash_flash_addr(wolfBoot_hash_t* ctx, uintptr_t addr,
17881834 }
17891835
17901836 return 0 ;
1837+ #endif
17911838}
17921839
17931840int wolfBoot_check_flash_image_elf (uint8_t part , unsigned long * entry_out )
0 commit comments