@@ -65,6 +65,7 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
65
65
66
66
#include "bootutil_priv.h"
67
67
68
+ #ifndef MCUBOOT_SIGN_PURE
68
69
/*
69
70
* Compute SHA hash over the image.
70
71
* (SHA384 if ECDSA-P384 is being used,
@@ -184,6 +185,7 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
184
185
185
186
return 0 ;
186
187
}
188
+ #endif
187
189
188
190
/*
189
191
* Currently, we only support being able to verify one type of
@@ -370,6 +372,35 @@ bootutil_get_img_security_cnt(struct image_header *hdr,
370
372
return 0 ;
371
373
}
372
374
375
+ #if defined(MCUBOOT_SIGN_PURE )
376
+ /* Returns:
377
+ * 0 -- found
378
+ * 1 -- not found
379
+ * -1 -- failed for some reason
380
+ *
381
+ * Value of TLV does not matter, presence decides.
382
+ */
383
+ static int bootutil_check_for_pure (const struct image_header * hdr ,
384
+ const struct flash_area * fap )
385
+ {
386
+ struct image_tlv_iter it ;
387
+ uint32_t off ;
388
+ uint16_t len ;
389
+ int32_t rc ;
390
+
391
+ rc = bootutil_tlv_iter_begin (& it , hdr , fap , IMAGE_TLV_SIG_PURE , false);
392
+ if (rc ) {
393
+ return rc ;
394
+ }
395
+
396
+ /* Search for the TLV */
397
+ rc = bootutil_tlv_iter_next (& it , & off , & len , NULL );
398
+
399
+ return rc ;
400
+ }
401
+ #endif
402
+
403
+
373
404
#ifndef ALLOW_ROGUE_TLVS
374
405
/*
375
406
* The following list of TLVs are the only entries allowed in the unprotected
@@ -386,6 +417,9 @@ static const uint16_t allowed_unprot_tlvs[] = {
386
417
IMAGE_TLV_ECDSA_SIG ,
387
418
IMAGE_TLV_RSA3072_PSS ,
388
419
IMAGE_TLV_ED25519 ,
420
+ #if defined(MCUBOOT_SIGN_PURE )
421
+ IMAGE_TLV_SIG_PURE ,
422
+ #endif
389
423
IMAGE_TLV_ENC_RSA2048 ,
390
424
IMAGE_TLV_ENC_KW ,
391
425
IMAGE_TLV_ENC_EC256 ,
@@ -408,7 +442,6 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
408
442
uint32_t off ;
409
443
uint16_t len ;
410
444
uint16_t type ;
411
- int image_hash_valid = 0 ;
412
445
#ifdef EXPECTED_SIG_TLV
413
446
FIH_DECLARE (valid_signature , FIH_FAILURE );
414
447
#ifndef MCUBOOT_BUILTIN_KEY
@@ -425,7 +458,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
425
458
#endif /* EXPECTED_SIG_TLV */
426
459
struct image_tlv_iter it ;
427
460
uint8_t buf [SIG_BUF_SIZE ];
461
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
462
+ int image_hash_valid = 0 ;
428
463
uint8_t hash [IMAGE_HASH_SIZE ];
464
+ #endif
429
465
int rc = 0 ;
430
466
FIH_DECLARE (fih_rc , FIH_FAILURE );
431
467
#ifdef MCUBOOT_HW_ROLLBACK_PROT
@@ -494,6 +530,7 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
494
530
}
495
531
#endif
496
532
533
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
497
534
rc = bootutil_img_hash (enc_state , image_index , hdr , fap , tmp_buf ,
498
535
tmp_buf_sz , hash , seed , seed_len );
499
536
if (rc ) {
@@ -503,6 +540,15 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
503
540
if (out_hash ) {
504
541
memcpy (out_hash , hash , IMAGE_HASH_SIZE );
505
542
}
543
+ #endif
544
+
545
+ #if defined(MCUBOOT_SIGN_PURE )
546
+ /* If Pure type signature is expected then it has to be there */
547
+ rc = bootutil_check_for_pure (hdr , fap );
548
+ if (rc != 0 ) {
549
+ goto out ;
550
+ }
551
+ #endif
506
552
507
553
rc = bootutil_tlv_iter_begin (& it , hdr , fap , IMAGE_TLV_ANY , false);
508
554
if (rc ) {
@@ -546,8 +592,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
546
592
}
547
593
}
548
594
#endif
549
-
550
- if (type == EXPECTED_HASH_TLV ) {
595
+ switch (type ) {
596
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
597
+ case EXPECTED_HASH_TLV :
598
+ {
551
599
/* Verify the image hash. This must always be present. */
552
600
if (len != sizeof (hash )) {
553
601
rc = -1 ;
@@ -565,8 +613,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
565
613
}
566
614
567
615
image_hash_valid = 1 ;
616
+ break ;
617
+ }
618
+ #endif /* defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE) */
568
619
#ifdef EXPECTED_KEY_TLV
569
- } else if (type == EXPECTED_KEY_TLV ) {
620
+ case EXPECTED_KEY_TLV :
621
+ {
570
622
/*
571
623
* Determine which key we should be checking.
572
624
*/
@@ -591,9 +643,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
591
643
* The key may not be found, which is acceptable. There
592
644
* can be multiple signatures, each preceded by a key.
593
645
*/
646
+ break ;
647
+ }
594
648
#endif /* EXPECTED_KEY_TLV */
595
649
#ifdef EXPECTED_SIG_TLV
596
- } else if (type == EXPECTED_SIG_TLV ) {
650
+ case EXPECTED_SIG_TLV :
651
+ {
597
652
/* Ignore this signature if it is out of bounds. */
598
653
if (key_id < 0 || key_id >= bootutil_key_cnt ) {
599
654
key_id = -1 ;
@@ -607,12 +662,25 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
607
662
if (rc ) {
608
663
goto out ;
609
664
}
665
+ #ifndef MCUBOOT_SIGN_PURE
610
666
FIH_CALL (bootutil_verify_sig , valid_signature , hash , sizeof (hash ),
611
667
buf , len , key_id );
668
+ #else
669
+ /* Directly check signature on the image, by using the mapping of
670
+ * a device to memory. The pointer is beginning of image in flash,
671
+ * so offset of area, the range is header + image + protected tlvs.
672
+ */
673
+ FIH_CALL (bootutil_verify_img , valid_signature , (void * )flash_area_get_off (fap ),
674
+ hdr -> ih_hdr_size + hdr -> ih_img_size + hdr -> ih_protect_tlv_size ,
675
+ buf , len , key_id );
676
+ #endif
612
677
key_id = -1 ;
678
+ break ;
679
+ }
613
680
#endif /* EXPECTED_SIG_TLV */
614
681
#ifdef MCUBOOT_HW_ROLLBACK_PROT
615
- } else if (type == IMAGE_TLV_SEC_CNT ) {
682
+ case IMAGE_TLV_SEC_CNT :
683
+ {
616
684
/*
617
685
* Verify the image's security counter.
618
686
* This must always be present.
@@ -647,14 +715,21 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
647
715
648
716
/* The image's security counter has been successfully verified. */
649
717
security_counter_valid = fih_rc ;
718
+ break ;
719
+ }
650
720
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
651
721
}
652
722
}
653
723
724
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
654
725
rc = !image_hash_valid ;
655
726
if (rc ) {
656
727
goto out ;
657
728
}
729
+ #elif defined(MCUBOOT_SIGN_PURE )
730
+ /* This returns true on EQ, rc is err on non-0 */
731
+ rc = !FIH_EQ (valid_signature , FIH_SUCCESS );
732
+ #endif
658
733
#ifdef EXPECTED_SIG_TLV
659
734
FIH_SET (fih_rc , valid_signature );
660
735
#endif
0 commit comments