@@ -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 ;
@@ -334,6 +334,32 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
334334 return -1 ;
335335}
336336#endif /* !MCUBOOT_HW_KEY */
337+
338+ #else
339+ /* For MCUBOOT_BUILTIN_KEY, key id is passed */
340+ #define EXPECTED_KEY_TLV IMAGE_TLV_KEYID
341+ #define KEY_BUF_SIZE sizeof(int32_t)
342+
343+ static int bootutil_find_key (uint8_t image_index , uint8_t * key_id_buf , uint8_t key_id_buf_len )
344+ {
345+ int rc ;
346+ FIH_DECLARE (fih_rc , FIH_FAILURE );
347+
348+ /* Key id is passed */
349+ assert (key_id_buf_len == sizeof (int32_t ));
350+ int32_t key_id = (((int32_t )key_id_buf [0 ] << 24 ) |
351+ ((int32_t )key_id_buf [1 ] << 16 ) |
352+ ((int32_t )key_id_buf [2 ] << 8 ) |
353+ ((int32_t )key_id_buf [3 ]));
354+
355+ /* Check if key id is associated with the image */
356+ FIH_CALL (boot_verify_key_id_for_image , fih_rc , image_index , key_id );
357+ if (FIH_EQ (fih_rc , FIH_SUCCESS )) {
358+ return key_id ;
359+ }
360+
361+ return -1 ;
362+ }
337363#endif /* !MCUBOOT_BUILTIN_KEY */
338364#endif /* EXPECTED_SIG_TLV */
339365
@@ -449,6 +475,7 @@ static int bootutil_check_for_pure(const struct image_header *hdr,
449475static const uint16_t allowed_unprot_tlvs [] = {
450476 IMAGE_TLV_KEYHASH ,
451477 IMAGE_TLV_PUBKEY ,
478+ IMAGE_TLV_KEYID ,
452479 IMAGE_TLV_SHA256 ,
453480 IMAGE_TLV_SHA384 ,
454481 IMAGE_TLV_SHA512 ,
@@ -492,14 +519,7 @@ bootutil_img_validate(struct boot_loader_state *state,
492519 uint32_t img_sz ;
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
@@ -637,7 +657,7 @@ bootutil_img_validate(struct boot_loader_state *state,
637657 if (rc ) {
638658 goto out ;
639659 }
640- key_id = bootutil_find_key (buf , len );
660+ key_id = bootutil_find_key (image_index , buf , len );
641661#else
642662 rc = LOAD_IMAGE_DATA (hdr , fap , off , key_buf , len );
643663 if (rc ) {
0 commit comments