@@ -282,12 +282,12 @@ bootutil_img_hash(struct boot_loader_state *state,
282
282
283
283
#if !defined(MCUBOOT_HW_KEY )
284
284
static 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 )
286
286
{
287
287
bootutil_sha_context sha_ctx ;
288
288
int i ;
289
289
const struct bootutil_key * key ;
290
- uint8_t hash [ IMAGE_HASH_SIZE ] ;
290
+ ( void ) image_index ;
291
291
292
292
BOOT_LOG_DBG ("bootutil_find_key" );
293
293
@@ -347,6 +347,32 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
347
347
return -1 ;
348
348
}
349
349
#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
+ }
350
376
#endif /* !MCUBOOT_BUILTIN_KEY */
351
377
#endif /* EXPECTED_SIG_TLV */
352
378
@@ -462,6 +488,7 @@ static int bootutil_check_for_pure(const struct image_header *hdr,
462
488
static const uint16_t allowed_unprot_tlvs [] = {
463
489
IMAGE_TLV_KEYHASH ,
464
490
IMAGE_TLV_PUBKEY ,
491
+ IMAGE_TLV_KEYID ,
465
492
IMAGE_TLV_SHA256 ,
466
493
IMAGE_TLV_SHA384 ,
467
494
IMAGE_TLV_SHA512 ,
@@ -506,14 +533,7 @@ bootutil_img_validate(struct boot_loader_state *state,
506
533
uint32_t img_sz ;
507
534
#ifdef EXPECTED_SIG_TLV
508
535
FIH_DECLARE (valid_signature , FIH_FAILURE );
509
- #ifndef MCUBOOT_BUILTIN_KEY
510
536
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 */
517
537
#ifdef MCUBOOT_HW_KEY
518
538
uint8_t key_buf [KEY_BUF_SIZE ];
519
539
#endif
@@ -651,7 +671,7 @@ bootutil_img_validate(struct boot_loader_state *state,
651
671
if (rc ) {
652
672
goto out ;
653
673
}
654
- key_id = bootutil_find_key (buf , len );
674
+ key_id = bootutil_find_key (image_index , buf , len );
655
675
#else
656
676
rc = LOAD_IMAGE_DATA (hdr , fap , off , key_buf , len );
657
677
if (rc ) {
0 commit comments