56
56
57
57
#include "bootutil_priv.h"
58
58
59
+ #ifndef MCUBOOT_SIGN_PURE
59
60
/*
60
61
* Compute SHA hash over the image.
61
62
* (SHA384 if ECDSA-P384 is being used,
@@ -175,6 +176,7 @@ bootutil_img_hash(struct enc_key_data *enc_state, int image_index,
175
176
176
177
return 0 ;
177
178
}
179
+ #endif
178
180
179
181
/*
180
182
* Currently, we only support being able to verify one type of
@@ -361,6 +363,35 @@ bootutil_get_img_security_cnt(struct image_header *hdr,
361
363
return 0 ;
362
364
}
363
365
366
+ #if defined(MCUBOOT_SIGN_PURE )
367
+ /* Returns:
368
+ * 0 -- found
369
+ * 1 -- not found
370
+ * -1 -- failed for some reason
371
+ *
372
+ * Value of TLV does not matter, presence decides.
373
+ */
374
+ static int bootutil_check_for_pure (const struct image_header * hdr ,
375
+ const struct flash_area * fap )
376
+ {
377
+ struct image_tlv_iter it ;
378
+ uint32_t off ;
379
+ uint16_t len ;
380
+ int32_t rc ;
381
+
382
+ rc = bootutil_tlv_iter_begin (& it , hdr , fap , IMAGE_TLV_SIG_PURE , false);
383
+ if (rc ) {
384
+ return rc ;
385
+ }
386
+
387
+ /* Search for the TLV */
388
+ rc = bootutil_tlv_iter_next (& it , & off , & len , NULL );
389
+
390
+ return rc ;
391
+ }
392
+ #endif
393
+
394
+
364
395
#ifndef ALLOW_ROGUE_TLVS
365
396
/*
366
397
* The following list of TLVs are the only entries allowed in the unprotected
@@ -377,6 +408,9 @@ static const uint16_t allowed_unprot_tlvs[] = {
377
408
IMAGE_TLV_ECDSA_SIG ,
378
409
IMAGE_TLV_RSA3072_PSS ,
379
410
IMAGE_TLV_ED25519 ,
411
+ #if defined(MCUBOOT_SIGN_PURE )
412
+ IMAGE_TLV_SIG_PURE ,
413
+ #endif
380
414
IMAGE_TLV_ENC_RSA2048 ,
381
415
IMAGE_TLV_ENC_KW ,
382
416
IMAGE_TLV_ENC_EC256 ,
@@ -399,7 +433,6 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
399
433
uint32_t off ;
400
434
uint16_t len ;
401
435
uint16_t type ;
402
- int image_hash_valid = 0 ;
403
436
#ifdef EXPECTED_SIG_TLV
404
437
FIH_DECLARE (valid_signature , FIH_FAILURE );
405
438
#ifndef MCUBOOT_BUILTIN_KEY
@@ -416,7 +449,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
416
449
#endif /* EXPECTED_SIG_TLV */
417
450
struct image_tlv_iter it ;
418
451
uint8_t buf [SIG_BUF_SIZE ];
452
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
453
+ int image_hash_valid = 0 ;
419
454
uint8_t hash [IMAGE_HASH_SIZE ];
455
+ #endif
420
456
int rc = 0 ;
421
457
FIH_DECLARE (fih_rc , FIH_FAILURE );
422
458
#ifdef MCUBOOT_HW_ROLLBACK_PROT
@@ -425,6 +461,7 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
425
461
FIH_DECLARE (security_counter_valid , FIH_FAILURE );
426
462
#endif
427
463
464
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
428
465
rc = bootutil_img_hash (enc_state , image_index , hdr , fap , tmp_buf ,
429
466
tmp_buf_sz , hash , seed , seed_len );
430
467
if (rc ) {
@@ -434,6 +471,15 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
434
471
if (out_hash ) {
435
472
memcpy (out_hash , hash , IMAGE_HASH_SIZE );
436
473
}
474
+ #endif
475
+
476
+ #if defined(MCUBOOT_SIGN_PURE )
477
+ /* If Pure type signature is expected then it has to be there */
478
+ rc = bootutil_check_for_pure (hdr , fap );
479
+ if (rc != 0 ) {
480
+ goto out ;
481
+ }
482
+ #endif
437
483
438
484
rc = bootutil_tlv_iter_begin (& it , hdr , fap , IMAGE_TLV_ANY , false);
439
485
if (rc ) {
@@ -477,8 +523,10 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
477
523
}
478
524
}
479
525
#endif
480
-
481
- if (type == EXPECTED_HASH_TLV ) {
526
+ switch (type ) {
527
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
528
+ case EXPECTED_HASH_TLV :
529
+ {
482
530
/* Verify the image hash. This must always be present. */
483
531
if (len != sizeof (hash )) {
484
532
rc = -1 ;
@@ -496,8 +544,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
496
544
}
497
545
498
546
image_hash_valid = 1 ;
547
+ break ;
548
+ }
549
+ #endif /* defined(EXPECTED_HASH_TLV) && !defined(MCUBOOT_SIGN_PURE) */
499
550
#ifdef EXPECTED_KEY_TLV
500
- } else if (type == EXPECTED_KEY_TLV ) {
551
+ case EXPECTED_KEY_TLV :
552
+ {
501
553
/*
502
554
* Determine which key we should be checking.
503
555
*/
@@ -522,9 +574,12 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
522
574
* The key may not be found, which is acceptable. There
523
575
* can be multiple signatures, each preceded by a key.
524
576
*/
577
+ break ;
578
+ }
525
579
#endif /* EXPECTED_KEY_TLV */
526
580
#ifdef EXPECTED_SIG_TLV
527
- } else if (type == EXPECTED_SIG_TLV ) {
581
+ case EXPECTED_SIG_TLV :
582
+ {
528
583
/* Ignore this signature if it is out of bounds. */
529
584
if (key_id < 0 || key_id >= bootutil_key_cnt ) {
530
585
key_id = -1 ;
@@ -538,12 +593,25 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
538
593
if (rc ) {
539
594
goto out ;
540
595
}
596
+ #ifndef MCUBOOT_SIGN_PURE
541
597
FIH_CALL (bootutil_verify_sig , valid_signature , hash , sizeof (hash ),
542
598
buf , len , key_id );
599
+ #else
600
+ /* Directly check signature on the image, by using the mapping of
601
+ * a device to memory. The pointer is beginning of image in flash,
602
+ * so offset of area, the range is header + image + protected tlvs.
603
+ */
604
+ FIH_CALL (bootutil_verify_img , valid_signature , (void * )flash_area_get_off (fap ),
605
+ hdr -> ih_hdr_size + hdr -> ih_img_size + hdr -> ih_protect_tlv_size ,
606
+ buf , len , key_id );
607
+ #endif
543
608
key_id = -1 ;
609
+ break ;
610
+ }
544
611
#endif /* EXPECTED_SIG_TLV */
545
612
#ifdef MCUBOOT_HW_ROLLBACK_PROT
546
- } else if (type == IMAGE_TLV_SEC_CNT ) {
613
+ case IMAGE_TLV_SEC_CNT :
614
+ {
547
615
/*
548
616
* Verify the image's security counter.
549
617
* This must always be present.
@@ -578,14 +646,21 @@ bootutil_img_validate(struct enc_key_data *enc_state, int image_index,
578
646
579
647
/* The image's security counter has been successfully verified. */
580
648
security_counter_valid = fih_rc ;
649
+ break ;
650
+ }
581
651
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
582
652
}
583
653
}
584
654
655
+ #if defined(EXPECTED_HASH_TLV ) && !defined(MCUBOOT_SIGN_PURE )
585
656
rc = !image_hash_valid ;
586
657
if (rc ) {
587
658
goto out ;
588
659
}
660
+ #elif defined(MCUBOOT_SIGN_PURE )
661
+ /* This returns true on EQ, rc is err on non-0 */
662
+ rc = !FIH_EQ (valid_signature , FIH_SUCCESS );
663
+ #endif
589
664
#ifdef EXPECTED_SIG_TLV
590
665
FIH_SET (fih_rc , valid_signature );
591
666
#endif
0 commit comments