@@ -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
if (keyhash_len > IMAGE_HASH_SIZE ) {
284
284
return -1 ;
@@ -334,6 +334,32 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
334
334
return -1 ;
335
335
}
336
336
#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
+ }
337
363
#endif /* !MCUBOOT_BUILTIN_KEY */
338
364
#endif /* EXPECTED_SIG_TLV */
339
365
@@ -449,6 +475,7 @@ static int bootutil_check_for_pure(const struct image_header *hdr,
449
475
static const uint16_t allowed_unprot_tlvs [] = {
450
476
IMAGE_TLV_KEYHASH ,
451
477
IMAGE_TLV_PUBKEY ,
478
+ IMAGE_TLV_KEYID ,
452
479
IMAGE_TLV_SHA256 ,
453
480
IMAGE_TLV_SHA384 ,
454
481
IMAGE_TLV_SHA512 ,
@@ -492,14 +519,7 @@ bootutil_img_validate(struct boot_loader_state *state,
492
519
uint32_t img_sz ;
493
520
#ifdef EXPECTED_SIG_TLV
494
521
FIH_DECLARE (valid_signature , FIH_FAILURE );
495
- #ifndef MCUBOOT_BUILTIN_KEY
496
522
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 */
503
523
#ifdef MCUBOOT_HW_KEY
504
524
uint8_t key_buf [KEY_BUF_SIZE ];
505
525
#endif
@@ -637,7 +657,7 @@ bootutil_img_validate(struct boot_loader_state *state,
637
657
if (rc ) {
638
658
goto out ;
639
659
}
640
- key_id = bootutil_find_key (buf , len );
660
+ key_id = bootutil_find_key (image_index , buf , len );
641
661
#else
642
662
rc = LOAD_IMAGE_DATA (hdr , fap , off , key_buf , len );
643
663
if (rc ) {
0 commit comments