@@ -42,6 +42,10 @@ static WOLFTPM2_KEY wolftpm_srk;
42
42
#endif
43
43
#endif /* WOLFBOOT_TPM */
44
44
45
+ #if defined(WOLFBOOT_TPM_KEYSTORE ) && !defined(WOLFBOOT_TPM )
46
+ #error For TPM keystore please make sure WOLFBOOT_TPM is also defined
47
+ #endif
48
+
45
49
/* Globals */
46
50
static uint8_t digest [WOLFBOOT_SHA_DIGEST_SIZE ];
47
51
@@ -638,9 +642,7 @@ static void key_sha3_384(uint8_t key_slot, uint8_t *hash)
638
642
#endif /* SHA3-384 */
639
643
640
644
#ifdef WOLFBOOT_TPM
641
- #if !defined(ARCH_SIM ) && !defined(WOLFTPM_MMIO )
642
-
643
- #ifdef WOLFTPM_DEBUG_IO
645
+ #if defined(WOLFTPM_DEBUG_IO ) || defined(WOLFBOOT_DEBUG_TPM )
644
646
#define LINE_LEN 16
645
647
static void wolfBoot_PrintBin (const byte * buffer , word32 length )
646
648
{
@@ -676,8 +678,9 @@ static void wolfBoot_PrintBin(const byte* buffer, word32 length)
676
678
length -= sz ;
677
679
}
678
680
}
679
- #endif /* WOLFTPM_DEBUG_IO */
681
+ #endif /* WOLFTPM_DEBUG_IO || WOLFBOOT_DEBUG_TPM */
680
682
683
+ #if !defined(ARCH_SIM ) && !defined(WOLFTPM_MMIO )
681
684
#ifdef WOLFTPM_ADV_IO
682
685
static int TPM2_IoCb (TPM2_CTX * ctx , int isRead , word32 addr , byte * buf ,
683
686
word16 size , void * userCtx )
@@ -761,9 +764,10 @@ static int TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
761
764
#endif /* !ARCH_SIM */
762
765
763
766
#if defined(WOLFBOOT_TPM ) && defined(WOLFBOOT_MEASURED_BOOT )
764
- static int measure_boot (struct wolfBoot_image * img )
767
+ #define measure_boot (hash ) measure_boot_at((hash), __LINE__)
768
+ static int measure_boot_at (uint8_t * hash , int line )
765
769
{
766
- int rc = -1 ;
770
+ int rc ;
767
771
PCR_Extend_In pcrExtend ;
768
772
#ifdef WOLFBOOT_DEBUG_TPM
769
773
PCR_Read_In pcrReadCmd ;
@@ -774,20 +778,31 @@ static int measure_boot(struct wolfBoot_image *img)
774
778
pcrExtend .digests .count = 1 ;
775
779
pcrExtend .digests .digests [0 ].hashAlg = TPM_ALG_SHA256 ;
776
780
XMEMCPY (pcrExtend .digests .digests [0 ].digest .H ,
777
- hash , TPM_SHA256_DIGEST_SIZE );
781
+ hash , TPM_SHA256_DIGEST_SIZE );
778
782
779
783
rc = TPM2_PCR_Extend (& pcrExtend );
780
- if (rc == TPM_RC_SUCCESS ) {
781
- rc = 0 ;
782
- }
784
+ #ifdef DEBUG_WOLFTPM
785
+ wolfBoot_printf ("Measured boot: Res %d, Index %d, Line %d\n" ,
786
+ rc , pcrExtend .pcrHandle , line );
787
+ #endif
783
788
784
789
#ifdef WOLFBOOT_DEBUG_TPM
785
- /* Test prcRead helps debug TPM communication and print PCR value in gdb */
786
- memset (& pcrReadCmd , 0 , sizeof (pcrReadCmd ));
787
- TPM2_SetupPCRSel (& pcrReadCmd .pcrSelectionIn , TPM_ALG_SHA256 ,
788
- pcrExtend .pcrHandle );
789
- TPM2_PCR_Read (& pcrReadCmd , & pcrReadResp );
790
+ if (rc == 0 ) {
791
+ memset (& pcrReadCmd , 0 , sizeof (pcrReadCmd ));
792
+ memset (& pcrReadResp , 0 , sizeof (pcrReadResp ));
793
+ TPM2_SetupPCRSel (& pcrReadCmd .pcrSelectionIn , TPM_ALG_SHA256 ,
794
+ pcrExtend .pcrHandle );
795
+ rc = TPM2_PCR_Read (& pcrReadCmd , & pcrReadResp );
796
+
797
+ wolfBoot_printf ("PCR %d: Res %d, Digest Sz %d, Update Counter %d\n" ,
798
+ pcrExtend .pcrHandle , rc ,
799
+ (int )pcrReadResp .pcrValues .digests [0 ].size ,
800
+ (int )pcrReadResp .pcrUpdateCounter );
801
+ wolfBoot_PrintBin (pcrReadResp .pcrValues .digests [0 ].buffer ,
802
+ pcrReadResp .pcrValues .digests [0 ].size );
803
+ }
790
804
#endif
805
+ (void )line ;
791
806
792
807
return rc ;
793
808
}
@@ -822,10 +837,6 @@ int wolfBoot_tpm2_init(void)
822
837
rc = wolfTPM2_Init (& wolftpm_dev , TPM2_IoCb , NULL );
823
838
#endif
824
839
if (rc == 0 ) {
825
- #ifdef WC_RNG_SEED_CB
826
- /* setup callback for RNG seed to use TPM */
827
- wc_SetSeed_Cb (wolfRNG_GetSeedCB );
828
- #endif
829
840
/* Get device capabilities + options */
830
841
rc = wolfTPM2_GetCapabilities (& wolftpm_dev , & caps );
831
842
}
@@ -842,6 +853,11 @@ int wolfBoot_tpm2_init(void)
842
853
#ifdef WOLFBOOT_TPM_KEYSTORE
843
854
memset (& wolftpm_session , 0 , sizeof (wolftpm_session ));
844
855
856
+ #ifdef WC_RNG_SEED_CB
857
+ /* setup callback for RNG seed to use TPM */
858
+ wc_SetSeed_Cb (wolfRNG_GetSeedCB );
859
+ #endif
860
+
845
861
/* Create a primary storage key - no auth (used for parameter encryption) */
846
862
#ifdef HAVE_ECC
847
863
alg = TPM_ALG_ECC ;
0 commit comments