@@ -282,12 +282,12 @@ bootutil_img_hash(struct boot_loader_state *state,
282282
283283#if !defined(MCUBOOT_HW_KEY )
284284static int
285- bootutil_find_key (uint8_t * keyhash , uint8_t keyhash_len )
285+ bootutil_find_key (uint8_t image_index , uint8_t * keyhash , uint8_t keyhash_len )
286286{
287287 bootutil_sha_context sha_ctx ;
288288 int i ;
289289 const struct bootutil_key * key ;
290- uint8_t hash [ IMAGE_HASH_SIZE ] ;
290+ ( void ) image_index ;
291291
292292 BOOT_LOG_DBG ("bootutil_find_key" );
293293
@@ -347,6 +347,32 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
347347 return -1 ;
348348}
349349#endif /* !MCUBOOT_HW_KEY */
350+
351+ #else
352+ /* For MCUBOOT_BUILTIN_KEY, key id is passed */
353+ #define EXPECTED_KEY_TLV IMAGE_TLV_KEYID
354+ #define KEY_BUF_SIZE sizeof(int32_t)
355+
356+ static int bootutil_find_key (uint8_t image_index , uint8_t * key_id_buf , uint8_t key_id_buf_len )
357+ {
358+ int rc ;
359+ FIH_DECLARE (fih_rc , FIH_FAILURE );
360+
361+ /* Key id is passed */
362+ assert (key_id_buf_len == sizeof (int32_t ));
363+ int32_t key_id = (((int32_t )key_id_buf [0 ] << 24 ) |
364+ ((int32_t )key_id_buf [1 ] << 16 ) |
365+ ((int32_t )key_id_buf [2 ] << 8 ) |
366+ ((int32_t )key_id_buf [3 ]));
367+
368+ /* Check if key id is associated with the image */
369+ FIH_CALL (boot_verify_key_id_for_image , fih_rc , image_index , key_id );
370+ if (FIH_EQ (fih_rc , FIH_SUCCESS )) {
371+ return key_id ;
372+ }
373+
374+ return -1 ;
375+ }
350376#endif /* !MCUBOOT_BUILTIN_KEY */
351377#endif /* EXPECTED_SIG_TLV */
352378
@@ -462,6 +488,7 @@ static int bootutil_check_for_pure(const struct image_header *hdr,
462488static const uint16_t allowed_unprot_tlvs [] = {
463489 IMAGE_TLV_KEYHASH ,
464490 IMAGE_TLV_PUBKEY ,
491+ IMAGE_TLV_KEYID ,
465492 IMAGE_TLV_SHA256 ,
466493 IMAGE_TLV_SHA384 ,
467494 IMAGE_TLV_SHA512 ,
@@ -506,14 +533,7 @@ bootutil_img_validate(struct boot_loader_state *state,
506533 uint32_t img_sz ;
507534#ifdef EXPECTED_SIG_TLV
508535 FIH_DECLARE (valid_signature , FIH_FAILURE );
509- #ifndef MCUBOOT_BUILTIN_KEY
510536 int key_id = -1 ;
511- #else
512- /* Pass a key ID equal to the image index, the underlying crypto library
513- * is responsible for mapping the image index to a builtin key ID.
514- */
515- int key_id = image_index ;
516- #endif /* !MCUBOOT_BUILTIN_KEY */
517537#ifdef MCUBOOT_HW_KEY
518538 uint8_t key_buf [KEY_BUF_SIZE ];
519539#endif
@@ -651,7 +671,7 @@ bootutil_img_validate(struct boot_loader_state *state,
651671 if (rc ) {
652672 goto out ;
653673 }
654- key_id = bootutil_find_key (buf , len );
674+ key_id = bootutil_find_key (image_index , buf , len );
655675#else
656676 rc = LOAD_IMAGE_DATA (hdr , fap , off , key_buf , len );
657677 if (rc ) {
0 commit comments