@@ -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,43 @@ 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 or found but not true
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
+ if (rc == 0 && len == 1 ) {
399
+ bool val ;
400
+
401
+ rc = LOAD_IMAGE_DATA (hdr , fap , off , & val , 1 );
402
+ if (rc == 0 ) {
403
+ rc = !val ;
404
+ }
405
+ }
406
+
407
+ return rc ;
408
+ }
409
+ #endif
410
+
411
+
373
412
#ifndef ALLOW_ROGUE_TLVS
374
413
/*
375
414
* The following list of TLVs are the only entries allowed in the unprotected
@@ -386,6 +425,9 @@ static const uint16_t allowed_unprot_tlvs[] = {
386
425
IMAGE_TLV_ECDSA_SIG ,
387
426
IMAGE_TLV_RSA3072_PSS ,
388
427
IMAGE_TLV_ED25519 ,
428
+ #if defined(MCUBOOT_SIGN_PURE )
429
+ IMAGE_TLV_SIG_PURE ,
430
+ #endif
389
431
IMAGE_TLV_ENC_RSA2048 ,
390
432
IMAGE_TLV_ENC_KW ,
391
433
IMAGE_TLV_ENC_EC256 ,
@@ -408,7 +450,6 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
408
450
uint32_t off ;
409
451
uint16_t len ;
410
452
uint16_t type ;
411
- int image_hash_valid = 0 ;
412
453
#ifdef EXPECTED_SIG_TLV
413
454
FIH_DECLARE (valid_signature , FIH_FAILURE );
414
455
#ifndef MCUBOOT_BUILTIN_KEY
@@ -425,7 +466,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
425
466
#endif /* EXPECTED_SIG_TLV */
426
467
struct image_tlv_iter it ;
427
468
uint8_t buf [SIG_BUF_SIZE ];
469
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
470
+ int image_hash_valid = 0 ;
428
471
uint8_t hash [IMAGE_HASH_SIZE ];
472
+ #endif
429
473
int rc = 0 ;
430
474
FIH_DECLARE (fih_rc , FIH_FAILURE );
431
475
#ifdef MCUBOOT_HW_ROLLBACK_PROT
@@ -496,6 +540,7 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
496
540
}
497
541
#endif
498
542
543
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
499
544
rc = bootutil_img_hash (enc_state , image_index , hdr , fap , tmp_buf ,
500
545
tmp_buf_sz , hash , seed , seed_len );
501
546
if (rc ) {
@@ -505,6 +550,15 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
505
550
if (out_hash ) {
506
551
memcpy (out_hash , hash , IMAGE_HASH_SIZE );
507
552
}
553
+ #endif
554
+
555
+ #if defined(MCUBOOT_SIGN_PURE )
556
+ /* If Pure type signature is expected then it has to be there */
557
+ rc = bootutil_check_for_pure (hdr , fap );
558
+ if (rc != 0 ) {
559
+ goto out ;
560
+ }
561
+ #endif
508
562
509
563
rc = bootutil_tlv_iter_begin (& it , hdr , fap , IMAGE_TLV_ANY , false);
510
564
if (rc ) {
@@ -548,8 +602,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
548
602
}
549
603
}
550
604
#endif
551
-
552
- if (type == EXPECTED_HASH_TLV ) {
605
+ switch (type ) {
606
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
607
+ case EXPECTED_HASH_TLV :
608
+ {
553
609
/* Verify the image hash. This must always be present. */
554
610
if (len != sizeof (hash )) {
555
611
rc = -1 ;
@@ -567,8 +623,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
567
623
}
568
624
569
625
image_hash_valid = 1 ;
626
+ break ;
627
+ }
628
+ #endif /* defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE) */
570
629
#ifdef EXPECTED_KEY_TLV
571
- } else if (type == EXPECTED_KEY_TLV ) {
630
+ case EXPECTED_KEY_TLV :
631
+ {
572
632
/*
573
633
* Determine which key we should be checking.
574
634
*/
@@ -593,9 +653,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
593
653
* The key may not be found, which is acceptable. There
594
654
* can be multiple signatures, each preceded by a key.
595
655
*/
656
+ break ;
657
+ }
596
658
#endif /* EXPECTED_KEY_TLV */
597
659
#ifdef EXPECTED_SIG_TLV
598
- } else if (type == EXPECTED_SIG_TLV ) {
660
+ case EXPECTED_SIG_TLV :
661
+ {
599
662
/* Ignore this signature if it is out of bounds. */
600
663
if (key_id < 0 || key_id >= bootutil_key_cnt ) {
601
664
key_id = -1 ;
@@ -609,12 +672,25 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
609
672
if (rc ) {
610
673
goto out ;
611
674
}
675
+ #ifndef MCUBOOT_SIGN_PURE
612
676
FIH_CALL (bootutil_verify_sig , valid_signature , hash , sizeof (hash ),
613
677
buf , len , key_id );
678
+ #else
679
+ /* Directly check signature on the image, by using the mapping of
680
+ * a device to memory. The pointer is beginning of image in flash,
681
+ * so offset of area, the range is header + image + protected tlvs.
682
+ */
683
+ FIH_CALL (bootutil_verify_img , valid_signature , (void * )flash_area_get_off (fap ),
684
+ hdr -> ih_hdr_size + hdr -> ih_img_size + hdr -> ih_protect_tlv_size ,
685
+ buf , len , key_id );
686
+ #endif
614
687
key_id = -1 ;
688
+ break ;
689
+ }
615
690
#endif /* EXPECTED_SIG_TLV */
616
691
#ifdef MCUBOOT_HW_ROLLBACK_PROT
617
- } else if (type == IMAGE_TLV_SEC_CNT ) {
692
+ case IMAGE_TLV_SEC_CNT :
693
+ {
618
694
/*
619
695
* Verify the image's security counter.
620
696
* This must always be present.
@@ -649,14 +725,21 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
649
725
650
726
/* The image's security counter has been successfully verified. */
651
727
security_counter_valid = fih_rc ;
728
+ break ;
729
+ }
652
730
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
653
731
}
654
732
}
655
733
734
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
656
735
rc = !image_hash_valid ;
657
736
if (rc ) {
658
737
goto out ;
659
738
}
739
+ #elif defined(MCUBOOT_SIGN_PURE )
740
+ /* This returns true on EQ, rc is err on non-0 */
741
+ rc = FIH_NOT_EQ (valid_signature , FIH_SUCCESS );
742
+ #endif
660
743
#ifdef EXPECTED_SIG_TLV
661
744
FIH_SET (fih_rc , valid_signature );
662
745
#endif
0 commit comments