@@ -140,7 +140,11 @@ bootutil_img_hash(struct boot_loader_state *state,
140140 /* in some cases (split image) the hash is seeded with data from
141141 * the loader image */
142142 if (seed && (seed_len > 0 )) {
143- bootutil_sha_update (& sha_ctx , seed , seed_len );
143+ rc = bootutil_sha_update (& sha_ctx , seed , seed_len );
144+ if (rc ){
145+ bootutil_sha_drop (& sha_ctx );
146+ return rc ;
147+ }
144148 }
145149
146150 /* Hash is computed over image header and image itself. */
@@ -155,12 +159,21 @@ bootutil_img_hash(struct boot_loader_state *state,
155159 /* No chunk loading, storage is mapped to address space and can
156160 * be directly given to hashing function.
157161 */
158- bootutil_sha_update (& sha_ctx , (void * )flash_area_get_off (fap ), size );
162+ rc = bootutil_sha_update (& sha_ctx , (void * )flash_area_get_off (fap ), size );
163+ if (rc ){
164+ bootutil_sha_drop (& sha_ctx );
165+ return rc ;
166+ }
159167#else /* MCUBOOT_HASH_STORAGE_DIRECTLY */
160168#ifdef MCUBOOT_RAM_LOAD
161- bootutil_sha_update (& sha_ctx ,
169+ rc = bootutil_sha_update (& sha_ctx ,
162170 (void * )(IMAGE_RAM_BASE + hdr -> ih_load_addr ),
163171 size );
172+ if (rc ){
173+ bootutil_sha_drop (& sha_ctx );
174+ return rc ;
175+ }
176+
164177#else
165178 for (off = 0 ; off < size ; off += blk_sz ) {
166179 blk_sz = size - off ;
@@ -202,14 +215,18 @@ bootutil_img_hash(struct boot_loader_state *state,
202215 }
203216 }
204217#endif
205- bootutil_sha_update (& sha_ctx , tmp_buf , blk_sz );
218+ rc = bootutil_sha_update (& sha_ctx , tmp_buf , blk_sz );
219+ if (rc ){
220+ bootutil_sha_drop (& sha_ctx );
221+ return rc ;
222+ }
206223 }
207224#endif /* MCUBOOT_RAM_LOAD */
208225#endif /* MCUBOOT_HASH_STORAGE_DIRECTLY */
209- bootutil_sha_finish (& sha_ctx , hash_result );
226+ rc = bootutil_sha_finish (& sha_ctx , hash_result );
210227 bootutil_sha_drop (& sha_ctx );
211228
212- return 0 ;
229+ return rc ;
213230}
214231#endif
215232
@@ -287,8 +304,12 @@ bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
287304 for (i = 0 ; i < bootutil_key_cnt ; i ++ ) {
288305 key = & bootutil_keys [i ];
289306 bootutil_sha_init (& sha_ctx );
290- bootutil_sha_update (& sha_ctx , key -> key , * key -> len );
291- bootutil_sha_finish (& sha_ctx , hash );
307+ if (bootutil_sha_update (& sha_ctx , key -> key , * key -> len )){
308+ break ;
309+ }
310+ if (bootutil_sha_finish (& sha_ctx , hash )){
311+ break ;
312+ }
292313 if (!memcmp (hash , keyhash , keyhash_len )) {
293314 bootutil_sha_drop (& sha_ctx );
294315 return i ;
@@ -310,9 +331,16 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
310331 FIH_DECLARE (fih_rc , FIH_FAILURE );
311332
312333 bootutil_sha_init (& sha_ctx );
313- bootutil_sha_update (& sha_ctx , key , key_len );
314- bootutil_sha_finish (& sha_ctx , hash );
334+ rc = bootutil_sha_update (& sha_ctx , key , key_len );
335+ if (rc ){
336+ bootutil_sha_drop (& sha_ctx );
337+ return rc ;
338+ }
339+ rc = bootutil_sha_finish (& sha_ctx , hash );
315340 bootutil_sha_drop (& sha_ctx );
341+ if (rc ){
342+ return rc ;
343+ }
316344
317345 rc = boot_retrieve_public_key_hash (image_index , key_hash , & key_hash_size );
318346 if (rc ) {
0 commit comments