@@ -273,12 +273,12 @@ bootutil_img_hash(struct boot_loader_state *state,
273273
274274#if !defined(MCUBOOT_HW_KEY )
275275static int
276- bootutil_find_key (uint8_t * keyhash , uint8_t keyhash_len )
276+ bootutil_find_key (uint8_t image_index , uint8_t * keyhash , uint8_t keyhash_len )
277277{
278278 bootutil_sha_context sha_ctx ;
279279 int i ;
280280 const struct bootutil_key * key ;
281- uint8_t hash [ IMAGE_HASH_SIZE ] ;
281+ ( void ) image_index ;
282282
283283 if (keyhash_len > IMAGE_HASH_SIZE ) {
284284 return -1 ;
@@ -335,6 +335,32 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
335335 return -1 ;
336336}
337337#endif /* !MCUBOOT_HW_KEY */
338+
339+ #else
340+ /* For MCUBOOT_BUILTIN_KEY, key id is passed */
341+ #define EXPECTED_KEY_TLV IMAGE_TLV_KEYID
342+ #define KEY_BUF_SIZE sizeof(int32_t)
343+
344+ static int bootutil_find_key (uint8_t image_index , uint8_t * key_id_buf , uint8_t key_id_buf_len )
345+ {
346+ int rc ;
347+ FIH_DECLARE (fih_rc , FIH_FAILURE );
348+
349+ /* Key id is passed */
350+ assert (key_id_buf_len == sizeof (int32_t ));
351+ int32_t key_id = (((int32_t )key_id_buf [0 ] << 24 ) |
352+ ((int32_t )key_id_buf [1 ] << 16 ) |
353+ ((int32_t )key_id_buf [2 ] << 8 ) |
354+ ((int32_t )key_id_buf [3 ]));
355+
356+ /* Check if key id is associated with the image */
357+ FIH_CALL (boot_verify_key_id_for_image , fih_rc , image_index , key_id );
358+ if (FIH_EQ (fih_rc , FIH_SUCCESS )) {
359+ return key_id ;
360+ }
361+
362+ return -1 ;
363+ }
338364#endif /* !MCUBOOT_BUILTIN_KEY */
339365#endif /* EXPECTED_SIG_TLV */
340366
@@ -450,6 +476,7 @@ static int bootutil_check_for_pure(const struct image_header *hdr,
450476static const uint16_t allowed_unprot_tlvs [] = {
451477 IMAGE_TLV_KEYHASH ,
452478 IMAGE_TLV_PUBKEY ,
479+ IMAGE_TLV_KEYID ,
453480 IMAGE_TLV_SHA256 ,
454481 IMAGE_TLV_SHA384 ,
455482 IMAGE_TLV_SHA512 ,
@@ -492,14 +519,7 @@ bootutil_img_validate(struct boot_loader_state *state,
492519 uint16_t type ;
493520#ifdef EXPECTED_SIG_TLV
494521 FIH_DECLARE (valid_signature , FIH_FAILURE );
495- #ifndef MCUBOOT_BUILTIN_KEY
496522 int key_id = -1 ;
497- #else
498- /* Pass a key ID equal to the image index, the underlying crypto library
499- * is responsible for mapping the image index to a builtin key ID.
500- */
501- int key_id = image_index ;
502- #endif /* !MCUBOOT_BUILTIN_KEY */
503523#ifdef MCUBOOT_HW_KEY
504524 uint8_t key_buf [KEY_BUF_SIZE ];
505525#endif
@@ -631,7 +651,7 @@ bootutil_img_validate(struct boot_loader_state *state,
631651 if (rc ) {
632652 goto out ;
633653 }
634- key_id = bootutil_find_key (buf , len );
654+ key_id = bootutil_find_key (image_index , buf , len );
635655#else
636656 rc = LOAD_IMAGE_DATA (hdr , fap , off , key_buf , len );
637657 if (rc ) {
0 commit comments