44 * Copyright (c) 2017-2019 Linaro LTD
55 * Copyright (c) 2016-2019 JUUL Labs
66 * Copyright (c) 2019-2024 Arm Limited
7+ * Copyright (c) 2025 Nordic Semiconductor ASA
78 *
89 * Original license:
910 *
2930#include <stdint.h>
3031#include <inttypes.h>
3132#include <string.h>
33+ #include <errno.h>
3234
3335#include <flash_map_backend/flash_map_backend.h>
3436
6365 * SHA256 otherwise).
6466 */
6567static int
66- bootutil_img_hash (struct enc_key_data * enc_state , int image_index ,
68+ bootutil_img_hash (struct boot_loader_state * state ,
6769 struct image_header * hdr , const struct flash_area * fap ,
6870 uint8_t * tmp_buf , uint32_t tmp_buf_sz , uint8_t * hash_result ,
6971 uint8_t * seed , int seed_len )
@@ -78,11 +80,14 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
7880 uint32_t off ;
7981 uint32_t blk_sz ;
8082#endif
83+ #if defined(MCUBOOT_ENC_IMAGES )
84+ struct enc_key_data * enc_state ;
85+ int image_index ;
86+ #endif
8187
8288#if (BOOT_IMAGE_NUMBER == 1 ) || !defined(MCUBOOT_ENC_IMAGES ) || \
8389 defined(MCUBOOT_RAM_LOAD )
84- (void )enc_state ;
85- (void )image_index ;
90+ (void )state ;
8691 (void )hdr_size ;
8792 (void )blk_off ;
8893 (void )tlv_off ;
@@ -97,6 +102,14 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
97102#endif
98103
99104#ifdef MCUBOOT_ENC_IMAGES
105+ if (state == NULL ) {
106+ enc_state = NULL ;
107+ image_index = 0 ;
108+ } else {
109+ enc_state = BOOT_CURR_ENC (state );
110+ image_index = BOOT_CURR_IMG (state );
111+ }
112+
100113 /* Encrypted images only exist in the secondary slot */
101114 if (MUST_DECRYPT (fap , image_index , hdr ) &&
102115 !boot_enc_valid (enc_state , 1 )) {
@@ -306,15 +319,16 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
306319/**
307320 * Reads the value of an image's security counter.
308321 *
309- * @param hdr Pointer to the image header structure.
322+ * @param state Pointer to the boot state object.
323+ * @param slot Slot of the current image to get the security counter of.
310324 * @param fap Pointer to a description structure of the image's
311325 * flash area.
312326 * @param security_cnt Pointer to store the security counter value.
313327 *
314328 * @return 0 on success; nonzero on failure.
315329 */
316330int32_t
317- bootutil_get_img_security_cnt (struct image_header * hdr ,
331+ bootutil_get_img_security_cnt (struct boot_loader_state * state , int slot ,
318332 const struct flash_area * fap ,
319333 uint32_t * img_security_cnt )
320334{
@@ -323,19 +337,20 @@ bootutil_get_img_security_cnt(struct image_header *hdr,
323337 uint16_t len ;
324338 int32_t rc ;
325339
326- if ((hdr == NULL ) ||
340+ if ((state == NULL ) ||
341+ (boot_img_hdr (state , slot ) == NULL ) ||
327342 (fap == NULL ) ||
328343 (img_security_cnt == NULL )) {
329344 /* Invalid parameter. */
330345 return BOOT_EBADARGS ;
331346 }
332347
333348 /* The security counter TLV is in the protected part of the TLV area. */
334- if (hdr -> ih_protect_tlv_size == 0 ) {
349+ if (boot_img_hdr ( state , slot ) -> ih_protect_tlv_size == 0 ) {
335350 return BOOT_EBADIMAGE ;
336351 }
337352
338- rc = bootutil_tlv_iter_begin (& it , hdr , fap , IMAGE_TLV_SEC_CNT , true);
353+ rc = bootutil_tlv_iter_begin (& it , boot_img_hdr ( state , slot ) , fap , IMAGE_TLV_SEC_CNT , true);
339354 if (rc ) {
340355 return rc ;
341356 }
@@ -355,7 +370,7 @@ bootutil_get_img_security_cnt(struct image_header *hdr,
355370 return BOOT_EBADIMAGE ;
356371 }
357372
358- rc = LOAD_IMAGE_DATA (hdr , fap , off , img_security_cnt , len );
373+ rc = LOAD_IMAGE_DATA (boot_img_hdr ( state , slot ) , fap , off , img_security_cnt , len );
359374 if (rc != 0 ) {
360375 return BOOT_EFLASH ;
361376 }
@@ -434,11 +449,14 @@ static const uint16_t allowed_unprot_tlvs[] = {
434449 * Return non-zero if image could not be validated/does not validate.
435450 */
436451fih_ret
437- bootutil_img_validate (struct enc_key_data * enc_state , int image_index ,
452+ bootutil_img_validate (struct boot_loader_state * state ,
438453 struct image_header * hdr , const struct flash_area * fap ,
439454 uint8_t * tmp_buf , uint32_t tmp_buf_sz , uint8_t * seed ,
440455 int seed_len , uint8_t * out_hash )
441456{
457+ #if (defined(EXPECTED_KEY_TLV ) && defined(MCUBOOT_HW_KEY )) || defined(MCUBOOT_HW_ROLLBACK_PROT )
458+ int image_index = (state == NULL ? 0 : BOOT_CURR_IMG (state ));
459+ #endif
442460 uint32_t off ;
443461 uint16_t len ;
444462 uint16_t type ;
@@ -471,7 +489,7 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
471489#endif
472490
473491#if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
474- rc = bootutil_img_hash (enc_state , image_index , hdr , fap , tmp_buf ,
492+ rc = bootutil_img_hash (state , hdr , fap , tmp_buf ,
475493 tmp_buf_sz , hash , seed , seed_len );
476494 if (rc ) {
477495 goto out ;
0 commit comments