@@ -65,6 +65,7 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
6565
6666#include "bootutil_priv.h"
6767
68+ #ifndef MCUBOOT_SIGN_PURE
6869/*
6970 * Compute SHA hash over the image.
7071 * (SHA384 if ECDSA-P384 is being used,
@@ -184,6 +185,7 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
184185
185186 return 0 ;
186187}
188+ #endif
187189
188190/*
189191 * Currently, we only support being able to verify one type of
@@ -370,6 +372,35 @@ bootutil_get_img_security_cnt(struct image_header *hdr,
370372 return 0 ;
371373}
372374
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+
373404#ifndef ALLOW_ROGUE_TLVS
374405/*
375406 * The following list of TLVs are the only entries allowed in the unprotected
@@ -386,6 +417,9 @@ static const uint16_t allowed_unprot_tlvs[] = {
386417 IMAGE_TLV_ECDSA_SIG ,
387418 IMAGE_TLV_RSA3072_PSS ,
388419 IMAGE_TLV_ED25519 ,
420+ #if defined(MCUBOOT_SIGN_PURE )
421+ IMAGE_TLV_SIG_PURE ,
422+ #endif
389423 IMAGE_TLV_ENC_RSA2048 ,
390424 IMAGE_TLV_ENC_KW ,
391425 IMAGE_TLV_ENC_EC256 ,
@@ -408,7 +442,6 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
408442 uint32_t off ;
409443 uint16_t len ;
410444 uint16_t type ;
411- int image_hash_valid = 0 ;
412445#ifdef EXPECTED_SIG_TLV
413446 FIH_DECLARE (valid_signature , FIH_FAILURE );
414447#ifndef MCUBOOT_BUILTIN_KEY
@@ -425,7 +458,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
425458#endif /* EXPECTED_SIG_TLV */
426459 struct image_tlv_iter it ;
427460 uint8_t buf [SIG_BUF_SIZE ];
461+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
462+ int image_hash_valid = 0 ;
428463 uint8_t hash [IMAGE_HASH_SIZE ];
464+ #endif
429465 int rc = 0 ;
430466 FIH_DECLARE (fih_rc , FIH_FAILURE );
431467#ifdef MCUBOOT_HW_ROLLBACK_PROT
@@ -494,6 +530,7 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
494530 }
495531#endif
496532
533+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
497534 rc = bootutil_img_hash (enc_state , image_index , hdr , fap , tmp_buf ,
498535 tmp_buf_sz , hash , seed , seed_len );
499536 if (rc ) {
@@ -503,6 +540,15 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
503540 if (out_hash ) {
504541 memcpy (out_hash , hash , IMAGE_HASH_SIZE );
505542 }
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
506552
507553 rc = bootutil_tlv_iter_begin (& it , hdr , fap , IMAGE_TLV_ANY , false);
508554 if (rc ) {
@@ -546,8 +592,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
546592 }
547593 }
548594#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+ {
551599 /* Verify the image hash. This must always be present. */
552600 if (len != sizeof (hash )) {
553601 rc = -1 ;
@@ -565,8 +613,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
565613 }
566614
567615 image_hash_valid = 1 ;
616+ break ;
617+ }
618+ #endif /* defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE) */
568619#ifdef EXPECTED_KEY_TLV
569- } else if (type == EXPECTED_KEY_TLV ) {
620+ case EXPECTED_KEY_TLV :
621+ {
570622 /*
571623 * Determine which key we should be checking.
572624 */
@@ -591,9 +643,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
591643 * The key may not be found, which is acceptable. There
592644 * can be multiple signatures, each preceded by a key.
593645 */
646+ break ;
647+ }
594648#endif /* EXPECTED_KEY_TLV */
595649#ifdef EXPECTED_SIG_TLV
596- } else if (type == EXPECTED_SIG_TLV ) {
650+ case EXPECTED_SIG_TLV :
651+ {
597652 /* Ignore this signature if it is out of bounds. */
598653 if (key_id < 0 || key_id >= bootutil_key_cnt ) {
599654 key_id = -1 ;
@@ -607,12 +662,25 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
607662 if (rc ) {
608663 goto out ;
609664 }
665+ #ifndef MCUBOOT_SIGN_PURE
610666 FIH_CALL (bootutil_verify_sig , valid_signature , hash , sizeof (hash ),
611667 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
612677 key_id = -1 ;
678+ break ;
679+ }
613680#endif /* EXPECTED_SIG_TLV */
614681#ifdef MCUBOOT_HW_ROLLBACK_PROT
615- } else if (type == IMAGE_TLV_SEC_CNT ) {
682+ case IMAGE_TLV_SEC_CNT :
683+ {
616684 /*
617685 * Verify the image's security counter.
618686 * This must always be present.
@@ -647,14 +715,21 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
647715
648716 /* The image's security counter has been successfully verified. */
649717 security_counter_valid = fih_rc ;
718+ break ;
719+ }
650720#endif /* MCUBOOT_HW_ROLLBACK_PROT */
651721 }
652722 }
653723
724+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
654725 rc = !image_hash_valid ;
655726 if (rc ) {
656727 goto out ;
657728 }
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
658733#ifdef EXPECTED_SIG_TLV
659734 FIH_SET (fih_rc , valid_signature );
660735#endif
0 commit comments