@@ -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 ;
@@ -335,6 +335,32 @@ bootutil_find_key(uint8_t image_index, uint8_t *key, uint16_t key_len)
335
335
return -1 ;
336
336
}
337
337
#endif /* !MCUBOOT_HW_KEY */
338
+
339
+ #else
340
+ /* For MCUBOOT_BUILTIN_KEY, key id is passed */
341
+ #define EXPECTED_KEY_TLV IMAGE_TLV_KEYID
342
+ #define KEY_BUF_SIZE sizeof(int32_t)
343
+
344
+ static int bootutil_find_key (uint8_t image_index , uint8_t * key_id_buf , uint8_t key_id_buf_len )
345
+ {
346
+ int rc ;
347
+ FIH_DECLARE (fih_rc , FIH_FAILURE );
348
+
349
+ /* Key id is passed */
350
+ assert (key_id_buf_len == sizeof (int32_t ));
351
+ int32_t key_id = (((int32_t )key_id_buf [0 ] << 24 ) |
352
+ ((int32_t )key_id_buf [1 ] << 16 ) |
353
+ ((int32_t )key_id_buf [2 ] << 8 ) |
354
+ ((int32_t )key_id_buf [3 ]));
355
+
356
+ /* Check if key id is associated with the image */
357
+ FIH_CALL (boot_verify_key_id_for_image , fih_rc , image_index , key_id );
358
+ if (FIH_EQ (fih_rc , FIH_SUCCESS )) {
359
+ return key_id ;
360
+ }
361
+
362
+ return -1 ;
363
+ }
338
364
#endif /* !MCUBOOT_BUILTIN_KEY */
339
365
#endif /* EXPECTED_SIG_TLV */
340
366
@@ -450,6 +476,7 @@ static int bootutil_check_for_pure(const struct image_header *hdr,
450
476
static const uint16_t allowed_unprot_tlvs [] = {
451
477
IMAGE_TLV_KEYHASH ,
452
478
IMAGE_TLV_PUBKEY ,
479
+ IMAGE_TLV_KEYID ,
453
480
IMAGE_TLV_SHA256 ,
454
481
IMAGE_TLV_SHA384 ,
455
482
IMAGE_TLV_SHA512 ,
@@ -492,14 +519,7 @@ bootutil_img_validate(struct boot_loader_state *state,
492
519
uint16_t type ;
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
@@ -631,7 +651,7 @@ bootutil_img_validate(struct boot_loader_state *state,
631
651
if (rc ) {
632
652
goto out ;
633
653
}
634
- key_id = bootutil_find_key (buf , len );
654
+ key_id = bootutil_find_key (image_index , buf , len );
635
655
#else
636
656
rc = LOAD_IMAGE_DATA (hdr , fap , off , key_buf , len );
637
657
if (rc ) {
0 commit comments