@@ -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,43 @@ 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 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+
373412#ifndef ALLOW_ROGUE_TLVS
374413/*
375414 * The following list of TLVs are the only entries allowed in the unprotected
@@ -386,6 +425,9 @@ static const uint16_t allowed_unprot_tlvs[] = {
386425 IMAGE_TLV_ECDSA_SIG ,
387426 IMAGE_TLV_RSA3072_PSS ,
388427 IMAGE_TLV_ED25519 ,
428+ #if defined(MCUBOOT_SIGN_PURE )
429+ IMAGE_TLV_SIG_PURE ,
430+ #endif
389431 IMAGE_TLV_ENC_RSA2048 ,
390432 IMAGE_TLV_ENC_KW ,
391433 IMAGE_TLV_ENC_EC256 ,
@@ -408,7 +450,6 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
408450 uint32_t off ;
409451 uint16_t len ;
410452 uint16_t type ;
411- int image_hash_valid = 0 ;
412453#ifdef EXPECTED_SIG_TLV
413454 FIH_DECLARE (valid_signature , FIH_FAILURE );
414455#ifndef MCUBOOT_BUILTIN_KEY
@@ -425,7 +466,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
425466#endif /* EXPECTED_SIG_TLV */
426467 struct image_tlv_iter it ;
427468 uint8_t buf [SIG_BUF_SIZE ];
469+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
470+ int image_hash_valid = 0 ;
428471 uint8_t hash [IMAGE_HASH_SIZE ];
472+ #endif
429473 int rc = 0 ;
430474 FIH_DECLARE (fih_rc , FIH_FAILURE );
431475#ifdef MCUBOOT_HW_ROLLBACK_PROT
@@ -496,6 +540,7 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
496540 }
497541#endif
498542
543+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
499544 rc = bootutil_img_hash (enc_state , image_index , hdr , fap , tmp_buf ,
500545 tmp_buf_sz , hash , seed , seed_len );
501546 if (rc ) {
@@ -505,6 +550,15 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
505550 if (out_hash ) {
506551 memcpy (out_hash , hash , IMAGE_HASH_SIZE );
507552 }
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
508562
509563 rc = bootutil_tlv_iter_begin (& it , hdr , fap , IMAGE_TLV_ANY , false);
510564 if (rc ) {
@@ -548,8 +602,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
548602 }
549603 }
550604#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+ {
553609 /* Verify the image hash. This must always be present. */
554610 if (len != sizeof (hash )) {
555611 rc = -1 ;
@@ -567,8 +623,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
567623 }
568624
569625 image_hash_valid = 1 ;
626+ break ;
627+ }
628+ #endif /* defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE) */
570629#ifdef EXPECTED_KEY_TLV
571- } else if (type == EXPECTED_KEY_TLV ) {
630+ case EXPECTED_KEY_TLV :
631+ {
572632 /*
573633 * Determine which key we should be checking.
574634 */
@@ -593,9 +653,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
593653 * The key may not be found, which is acceptable. There
594654 * can be multiple signatures, each preceded by a key.
595655 */
656+ break ;
657+ }
596658#endif /* EXPECTED_KEY_TLV */
597659#ifdef EXPECTED_SIG_TLV
598- } else if (type == EXPECTED_SIG_TLV ) {
660+ case EXPECTED_SIG_TLV :
661+ {
599662 /* Ignore this signature if it is out of bounds. */
600663 if (key_id < 0 || key_id >= bootutil_key_cnt ) {
601664 key_id = -1 ;
@@ -609,12 +672,25 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
609672 if (rc ) {
610673 goto out ;
611674 }
675+ #ifndef MCUBOOT_SIGN_PURE
612676 FIH_CALL (bootutil_verify_sig , valid_signature , hash , sizeof (hash ),
613677 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
614687 key_id = -1 ;
688+ break ;
689+ }
615690#endif /* EXPECTED_SIG_TLV */
616691#ifdef MCUBOOT_HW_ROLLBACK_PROT
617- } else if (type == IMAGE_TLV_SEC_CNT ) {
692+ case IMAGE_TLV_SEC_CNT :
693+ {
618694 /*
619695 * Verify the image's security counter.
620696 * This must always be present.
@@ -649,14 +725,21 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
649725
650726 /* The image's security counter has been successfully verified. */
651727 security_counter_valid = fih_rc ;
728+ break ;
729+ }
652730#endif /* MCUBOOT_HW_ROLLBACK_PROT */
653731 }
654732 }
655733
734+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
656735 rc = !image_hash_valid ;
657736 if (rc ) {
658737 goto out ;
659738 }
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
660743#ifdef EXPECTED_SIG_TLV
661744 FIH_SET (fih_rc , valid_signature );
662745#endif
0 commit comments