@@ -273,12 +273,12 @@ bootutil_img_hash(struct boot_loader_state *state,
273
273
274
274
#if !defined(MCUBOOT_HW_KEY )
275
275
static 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 )
277
277
{
278
278
bootutil_sha_context sha_ctx ;
279
279
int i ;
280
280
const struct bootutil_key * key ;
281
- uint8_t hash [ IMAGE_HASH_SIZE ] ;
281
+ ( void ) image_index ;
282
282
283
283
BOOT_LOG_DBG ("bootutil_find_key" );
284
284
@@ -338,6 +338,32 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
338
338
return -1 ;
339
339
}
340
340
#endif /* !MCUBOOT_HW_KEY */
341
+
342
+ #else
343
+ /* For MCUBOOT_BUILTIN_KEY, key id is passed */
344
+ #define EXPECTED_KEY_TLV IMAGE_TLV_KEYID
345
+ #define KEY_BUF_SIZE sizeof(int32_t)
346
+
347
+ static int bootutil_find_key (uint8_t image_index , uint8_t * key_id_buf , uint8_t key_id_buf_len )
348
+ {
349
+ int rc ;
350
+ FIH_DECLARE (fih_rc , FIH_FAILURE );
351
+
352
+ /* Key id is passed */
353
+ assert (key_id_buf_len == sizeof (int32_t ));
354
+ int32_t key_id = (((int32_t )key_id_buf [0 ] << 24 ) |
355
+ ((int32_t )key_id_buf [1 ] << 16 ) |
356
+ ((int32_t )key_id_buf [2 ] << 8 ) |
357
+ ((int32_t )key_id_buf [3 ]));
358
+
359
+ /* Check if key id is associated with the image */
360
+ FIH_CALL (boot_verify_key_id_for_image , fih_rc , image_index , key_id );
361
+ if (FIH_EQ (fih_rc , FIH_SUCCESS )) {
362
+ return key_id ;
363
+ }
364
+
365
+ return -1 ;
366
+ }
341
367
#endif /* !MCUBOOT_BUILTIN_KEY */
342
368
#endif /* EXPECTED_SIG_TLV */
343
369
@@ -453,6 +479,7 @@ static int bootutil_check_for_pure(const struct image_header *hdr,
453
479
static const uint16_t allowed_unprot_tlvs [] = {
454
480
IMAGE_TLV_KEYHASH ,
455
481
IMAGE_TLV_PUBKEY ,
482
+ IMAGE_TLV_KEYID ,
456
483
IMAGE_TLV_SHA256 ,
457
484
IMAGE_TLV_SHA384 ,
458
485
IMAGE_TLV_SHA512 ,
@@ -497,14 +524,7 @@ bootutil_img_validate(struct boot_loader_state *state,
497
524
uint32_t img_sz ;
498
525
#ifdef EXPECTED_SIG_TLV
499
526
FIH_DECLARE (valid_signature , FIH_FAILURE );
500
- #ifndef MCUBOOT_BUILTIN_KEY
501
527
int key_id = -1 ;
502
- #else
503
- /* Pass a key ID equal to the image index, the underlying crypto library
504
- * is responsible for mapping the image index to a builtin key ID.
505
- */
506
- int key_id = image_index ;
507
- #endif /* !MCUBOOT_BUILTIN_KEY */
508
528
#ifdef MCUBOOT_HW_KEY
509
529
uint8_t key_buf [KEY_BUF_SIZE ];
510
530
#endif
@@ -642,7 +662,7 @@ bootutil_img_validate(struct boot_loader_state *state,
642
662
if (rc ) {
643
663
goto out ;
644
664
}
645
- key_id = bootutil_find_key (buf , len );
665
+ key_id = bootutil_find_key (image_index , buf , len );
646
666
#else
647
667
rc = LOAD_IMAGE_DATA (hdr , fap , off , key_buf , len );
648
668
if (rc ) {
0 commit comments