Skip to content

Commit f9e032e

Browse files
committed
Fixes for measured boot.
1 parent 38645da commit f9e032e

File tree

4 files changed

+82
-22
lines changed

4 files changed

+82
-22
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
ARCH=sim
2+
TARGET=sim
3+
SIGN?=ECC384
4+
HASH?=SHA384
5+
SPI_FLASH=0
6+
DEBUG=1
7+
WOLFTPM=1
8+
9+
WOLFBOOT_TPM_KEYSTORE?=1
10+
WOLFBOOT_TPM_KEYSTORE_NV_INDEX?=0x01400200
11+
12+
# sizes should be multiple of system page size
13+
WOLFBOOT_PARTITION_SIZE=0x40000
14+
WOLFBOOT_SECTOR_SIZE=0x1000
15+
WOLFBOOT_PARTITION_BOOT_ADDRESS=0x80000
16+
# if on external flash, it should be multiple of system page size
17+
WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x100000
18+
WOLFBOOT_PARTITION_SWAP_ADDRESS=0x180000
19+
20+
# required for keytools
21+
WOLFBOOT_FIXED_PARTITIONS=1
22+
23+
# TPM Logging
24+
#CFLAGS_EXTRA+=-DDEBUG_WOLFTPM
25+
#CFLAGS_EXTRA+=-DWOLFTPM_DEBUG_VERBOSE
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
ARCH=sim
2+
TARGET=sim
3+
SIGN?=ED25519
4+
HASH?=SHA256
5+
WOLFBOOT_SMALL_STACK=1
6+
SPI_FLASH=0
7+
DEBUG=1
8+
9+
# Measured boot at test PCR index 16
10+
MEASURED_BOOT?=1
11+
MEASURED_PCR_A?=16
12+
13+
# sizes should be multiple of system page size
14+
WOLFBOOT_PARTITION_SIZE=0x40000
15+
WOLFBOOT_SECTOR_SIZE=0x1000
16+
WOLFBOOT_PARTITION_BOOT_ADDRESS=0x20000
17+
# if on external flash, it should be multiple of system page size
18+
WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x60000
19+
WOLFBOOT_PARTITION_SWAP_ADDRESS=0xA0000
20+
21+
# required for keytools
22+
WOLFBOOT_FIXED_PARTITIONS=1

config/examples/sim-tpm.config

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ SPI_FLASH=0
66
DEBUG=1
77
WOLFTPM=1
88

9-
WOLFBOOT_TPM_KEYSTORE?=1
10-
WOLFBOOT_TPM_KEYSTORE_NV_INDEX?=0x01800200
11-
129
# sizes should be multiple of system page size
1310
WOLFBOOT_PARTITION_SIZE=0x40000
1411
WOLFBOOT_SECTOR_SIZE=0x1000

src/image.c

+35-19
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ static WOLFTPM2_KEY wolftpm_srk;
4242
#endif
4343
#endif /* WOLFBOOT_TPM */
4444

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+
4549
/* Globals */
4650
static uint8_t digest[WOLFBOOT_SHA_DIGEST_SIZE];
4751

@@ -638,9 +642,7 @@ static void key_sha3_384(uint8_t key_slot, uint8_t *hash)
638642
#endif /* SHA3-384 */
639643

640644
#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)
644646
#define LINE_LEN 16
645647
static void wolfBoot_PrintBin(const byte* buffer, word32 length)
646648
{
@@ -676,8 +678,9 @@ static void wolfBoot_PrintBin(const byte* buffer, word32 length)
676678
length -= sz;
677679
}
678680
}
679-
#endif /* WOLFTPM_DEBUG_IO */
681+
#endif /* WOLFTPM_DEBUG_IO || WOLFBOOT_DEBUG_TPM */
680682

683+
#if !defined(ARCH_SIM) && !defined(WOLFTPM_MMIO)
681684
#ifdef WOLFTPM_ADV_IO
682685
static int TPM2_IoCb(TPM2_CTX* ctx, int isRead, word32 addr, byte* buf,
683686
word16 size, void* userCtx)
@@ -761,9 +764,10 @@ static int TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
761764
#endif /* !ARCH_SIM */
762765

763766
#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)
765769
{
766-
int rc = -1;
770+
int rc;
767771
PCR_Extend_In pcrExtend;
768772
#ifdef WOLFBOOT_DEBUG_TPM
769773
PCR_Read_In pcrReadCmd;
@@ -774,20 +778,31 @@ static int measure_boot(struct wolfBoot_image *img)
774778
pcrExtend.digests.count = 1;
775779
pcrExtend.digests.digests[0].hashAlg = TPM_ALG_SHA256;
776780
XMEMCPY(pcrExtend.digests.digests[0].digest.H,
777-
hash, TPM_SHA256_DIGEST_SIZE);
781+
hash, TPM_SHA256_DIGEST_SIZE);
778782

779783
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
783788

784789
#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+
}
790804
#endif
805+
(void)line;
791806

792807
return rc;
793808
}
@@ -822,10 +837,6 @@ int wolfBoot_tpm2_init(void)
822837
rc = wolfTPM2_Init(&wolftpm_dev, TPM2_IoCb, NULL);
823838
#endif
824839
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
829840
/* Get device capabilities + options */
830841
rc = wolfTPM2_GetCapabilities(&wolftpm_dev, &caps);
831842
}
@@ -842,6 +853,11 @@ int wolfBoot_tpm2_init(void)
842853
#ifdef WOLFBOOT_TPM_KEYSTORE
843854
memset(&wolftpm_session, 0, sizeof(wolftpm_session));
844855

856+
#ifdef WC_RNG_SEED_CB
857+
/* setup callback for RNG seed to use TPM */
858+
wc_SetSeed_Cb(wolfRNG_GetSeedCB);
859+
#endif
860+
845861
/* Create a primary storage key - no auth (used for parameter encryption) */
846862
#ifdef HAVE_ECC
847863
alg = TPM_ALG_ECC;

0 commit comments

Comments
 (0)