@@ -140,7 +140,11 @@ bootutil_img_hash(struct boot_loader_state *state,
140
140
/* in some cases (split image) the hash is seeded with data from
141
141
* the loader image */
142
142
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
+ }
144
148
}
145
149
146
150
/* Hash is computed over image header and image itself. */
@@ -155,12 +159,21 @@ bootutil_img_hash(struct boot_loader_state *state,
155
159
/* No chunk loading, storage is mapped to address space and can
156
160
* be directly given to hashing function.
157
161
*/
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
+ }
159
167
#else /* MCUBOOT_HASH_STORAGE_DIRECTLY */
160
168
#ifdef MCUBOOT_RAM_LOAD
161
- bootutil_sha_update (& sha_ctx ,
169
+ rc = bootutil_sha_update (& sha_ctx ,
162
170
(void * )(IMAGE_RAM_BASE + hdr -> ih_load_addr ),
163
171
size );
172
+ if (rc ){
173
+ bootutil_sha_drop (& sha_ctx );
174
+ return rc ;
175
+ }
176
+
164
177
#else
165
178
for (off = 0 ; off < size ; off += blk_sz ) {
166
179
blk_sz = size - off ;
@@ -202,14 +215,18 @@ bootutil_img_hash(struct boot_loader_state *state,
202
215
}
203
216
}
204
217
#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
+ }
206
223
}
207
224
#endif /* MCUBOOT_RAM_LOAD */
208
225
#endif /* MCUBOOT_HASH_STORAGE_DIRECTLY */
209
- bootutil_sha_finish (& sha_ctx , hash_result );
226
+ rc = bootutil_sha_finish (& sha_ctx , hash_result );
210
227
bootutil_sha_drop (& sha_ctx );
211
228
212
- return 0 ;
229
+ return rc ;
213
230
}
214
231
#endif
215
232
@@ -287,8 +304,12 @@ bootutil_find_key(uint8_t *keyhash, uint8_t keyhash_len)
287
304
for (i = 0 ; i < bootutil_key_cnt ; i ++ ) {
288
305
key = & bootutil_keys [i ];
289
306
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
+ }
292
313
if (!memcmp (hash , keyhash , keyhash_len )) {
293
314
bootutil_sha_drop (& sha_ctx );
294
315
return i ;
@@ -310,9 +331,16 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
310
331
FIH_DECLARE (fih_rc , FIH_FAILURE );
311
332
312
333
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 );
315
340
bootutil_sha_drop (& sha_ctx );
341
+ if (rc ){
342
+ return rc ;
343
+ }
316
344
317
345
rc = boot_retrieve_public_key_hash (image_index , key_hash , & key_hash_size );
318
346
if (rc ) {
0 commit comments