Skip to content

Commit d62e8ae

Browse files
dgarskedanielinux
authored andcommitted
Improve measured boot support
1 parent fa4d1a0 commit d62e8ae

4 files changed

Lines changed: 48 additions & 18 deletions

File tree

docs/TPM.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ In wolfBoot we support TPM based root of trust, sealing/unsealing, cryptographic
1111
| `WOLFBOOT_TPM_KEYSTORE=1` | `WOLFBOOT_TPM_KEYSTORE` | Enables TPM based root of trust. NV Index must store a hash of the trusted public key. |
1212
| `WOLFBOOT_TPM_KEYSTORE_NV_BASE=0x` | `WOLFBOOT_TPM_KEYSTORE_NV_BASE=0x` | NV index in platform range 0x1400000 - 0x17FFFFF. |
1313
| `WOLFBOOT_TPM_KEYSTORE_AUTH=secret` | `WOLFBOOT_TPM_KEYSTORE_AUTH` | Password for NV access |
14-
| `MEASURED_BOOT=1` | `WOLFBOOT_MEASURED_BOOT` | Enable measured boot. Extend PCR with wolfBoot hash. |
14+
| `MEASURED_BOOT=1` | `WOLFBOOT_MEASURED_BOOT` | Enable measured boot. Extends PCR with a hash of the wolfBoot bootloader code. |
1515
| `MEASURED_PCR_A=16` | `WOLFBOOT_MEASURED_PCR_A=16` | The PCR index to use. See [docs/measured_boot.md](/docs/measured_boot.md). |
16+
| `MEASURED_BOOT_APP_PARTITION=1` | `WOLFBOOT_MEASURED_BOOT_APP_PARTITION` | Legacy: measure the boot (application) partition instead of wolfBoot code. |
1617
| `WOLFBOOT_TPM_SEAL=1` | `WOLFBOOT_TPM_SEAL` | Enables support for sealing/unsealing based on PCR policy signed externally. |
1718
| `WOLFBOOT_TPM_SEAL_NV_BASE=0x01400300` | `WOLFBOOT_TPM_SEAL_NV_BASE` | To override the default sealed blob storage location in the platform hierarchy. |
1819
| `WOLFBOOT_TPM_SEAL_AUTH=secret` | `WOLFBOOT_TPM_SEAL_AUTH` | Password for sealing/unsealing secrets, if omitted the PCR policy will be used |
@@ -30,7 +31,9 @@ NOTE: The TPM's RSA verify requires ASN.1 encoding, so use SIGN=RSA2048ENC
3031

3132
## Measured Boot
3233

33-
The wolfBoot image is hashed and extended to the indicated PCR. This can be used later in the application to prove the boot process was not tampered with. Enabled with `WOLFBOOT_MEASURED_BOOT` and exposes API `wolfBoot_tpm2_extend`.
34+
The wolfBoot bootloader code is hashed and extended to the indicated PCR. This can be used later in the application to prove the boot process was not tampered with. Enabled with `WOLFBOOT_MEASURED_BOOT` and exposes API `wolfBoot_tpm2_extend`.
35+
36+
By default, the measurement covers wolfBoot's own code region (from `_start_text` to `_stored_data` linker symbols). To use the legacy behavior of measuring the boot (application) partition instead, set `MEASURED_BOOT_APP_PARTITION=1`.
3437

3538
## Sealing and Unsealing a secret
3639

docs/measured_boot.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ Having TPM measurements provide a way for the firmware or Operating System(OS),
3030
like Windows or Linux, to know that the software loaded before it gained control
3131
over system, is trustworthy and not modified.
3232

33-
In wolfBoot the concept is simplified to measuring a single component, the main
34-
firmware image. However, this can easily be extended by using more PCR registers.
33+
In wolfBoot the concept is simplified to measuring a single component, the
34+
wolfBoot bootloader code itself. This ensures the bootloader has not been
35+
tampered with before it verifies and loads the application. However, this can
36+
easily be extended by using more PCR registers.
37+
38+
To use the legacy behavior of measuring the boot (application) partition instead
39+
of wolfBoot's own code, set `MEASURED_BOOT_APP_PARTITION=1` in your config.
3540

3641
## Configuration
3742

@@ -81,6 +86,6 @@ MEASURED_PCR_A?=16
8186
### Code
8287

8388
wolfBoot offers out-of-the-box solution. There is zero need of the developer to touch wolfBoot code
84-
in order to use measured boot. If you would want to check the code, then look in `src/image.c` and
85-
more specifically the `measure_boot()` function. There you would find several TPM2 native API calls
86-
to wolfTPM. For more information about wolfTPM you can check its GitHub repository.
89+
in order to use measured boot. If you would want to check the code, then look in `src/tpm.c` and
90+
more specifically the `self_hash()` and `measure_boot()` functions. There you would find several TPM2
91+
native API calls to wolfTPM. For more information about wolfTPM you can check its GitHub repository.

options.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ ifeq ($(MEASURED_BOOT),1)
3838
WOLFTPM:=1
3939
CFLAGS+=-D"WOLFBOOT_MEASURED_BOOT"
4040
CFLAGS+=-D"WOLFBOOT_MEASURED_PCR_A=$(MEASURED_PCR_A)"
41+
ifeq ($(MEASURED_BOOT_APP_PARTITION),1)
42+
CFLAGS+=-D"WOLFBOOT_MEASURED_BOOT_APP_PARTITION"
43+
endif
4144
endif
4245

4346
## TPM keystore

src/tpm.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,29 @@ static int TPM2_IoCb(TPM2_CTX* ctx, const uint8_t* txBuf, uint8_t* rxBuf,
229229

230230
#ifdef WOLFBOOT_MEASURED_BOOT
231231

232-
#ifndef WOLFBOOT_NO_PARTITIONS
232+
#ifdef WOLFBOOT_MEASURED_BOOT_APP_PARTITION
233+
/* Legacy: measure the boot (application) partition */
234+
#ifndef WOLFBOOT_NO_PARTITIONS
235+
#define SELF_HASH_ADDR ((uintptr_t)WOLFBOOT_PARTITION_BOOT_ADDRESS)
236+
#define SELF_HASH_SZ ((uint32_t)WOLFBOOT_PARTITION_SIZE)
237+
#endif
238+
#else
239+
/* Default: measure wolfBoot's own code region
240+
* (from ARCH_FLASH_OFFSET to WOLFBOOT_PARTITION_BOOT_ADDRESS) */
241+
#if defined(WOLFBOOT_PARTITION_BOOT_ADDRESS) && defined(ARCH_FLASH_OFFSET)
242+
#define SELF_HASH_ADDR ((uintptr_t)ARCH_FLASH_OFFSET)
243+
#define SELF_HASH_SZ ((uint32_t)((uintptr_t)WOLFBOOT_PARTITION_BOOT_ADDRESS - \
244+
(uintptr_t)ARCH_FLASH_OFFSET))
245+
#endif
246+
#endif
247+
248+
#ifdef SELF_HASH_ADDR
233249
#ifdef WOLFBOOT_HASH_SHA256
234250
#include <wolfssl/wolfcrypt/sha256.h>
235251
static int self_sha256(uint8_t *hash)
236252
{
237-
uintptr_t p = (uintptr_t)WOLFBOOT_PARTITION_BOOT_ADDRESS;
238-
uint32_t sz = (uint32_t)WOLFBOOT_PARTITION_SIZE;
253+
uintptr_t p = SELF_HASH_ADDR;
254+
uint32_t sz = SELF_HASH_SZ;
239255
uint32_t blksz, position = 0;
240256
wc_Sha256 sha256_ctx;
241257

@@ -244,7 +260,8 @@ static int self_sha256(uint8_t *hash)
244260
blksz = WOLFBOOT_SHA_BLOCK_SIZE;
245261
if (position + blksz > sz)
246262
blksz = sz - position;
247-
#if defined(EXT_FLASH) && defined(NO_XIP)
263+
#if defined(EXT_FLASH) && defined(NO_XIP) && \
264+
defined(WOLFBOOT_MEASURED_BOOT_APP_PARTITION)
248265
rc = ext_flash_read(p, ext_hash_block, WOLFBOOT_SHA_BLOCK_SIZE);
249266
if (rc != WOLFBOOT_SHA_BLOCK_SIZE)
250267
return -1;
@@ -264,8 +281,8 @@ static int self_sha256(uint8_t *hash)
264281
#include <wolfssl/wolfcrypt/sha512.h>
265282
static int self_sha384(uint8_t *hash)
266283
{
267-
uintptr_t p = (uintptr_t)WOLFBOOT_PARTITION_BOOT_ADDRESS;
268-
uint32_t sz = (uint32_t)WOLFBOOT_PARTITION_SIZE;
284+
uintptr_t p = SELF_HASH_ADDR;
285+
uint32_t sz = SELF_HASH_SZ;
269286
uint32_t blksz, position = 0;
270287
wc_Sha384 sha384_ctx;
271288

@@ -274,7 +291,8 @@ static int self_sha384(uint8_t *hash)
274291
blksz = WOLFBOOT_SHA_BLOCK_SIZE;
275292
if (position + blksz > sz)
276293
blksz = sz - position;
277-
#if defined(EXT_FLASH) && defined(NO_XIP)
294+
#if defined(EXT_FLASH) && defined(NO_XIP) && \
295+
defined(WOLFBOOT_MEASURED_BOOT_APP_PARTITION)
278296
rc = ext_flash_read(p, ext_hash_block, WOLFBOOT_SHA_BLOCK_SIZE);
279297
if (rc != WOLFBOOT_SHA_BLOCK_SIZE)
280298
return -1;
@@ -290,7 +308,7 @@ static int self_sha384(uint8_t *hash)
290308
return 0;
291309
}
292310
#endif /* HASH type */
293-
#endif /* WOLFBOOT_NO_PARTITIONS */
311+
#endif /* SELF_HASH_ADDR */
294312

295313
/**
296314
* @brief Extends a PCR in the TPM with a hash.
@@ -1442,8 +1460,9 @@ int wolfBoot_tpm2_init(void)
14421460
}
14431461
#endif /* WOLFBOOT_TPM_KEYSTORE | WOLFBOOT_TPM_SEAL */
14441462

1445-
#if defined(WOLFBOOT_MEASURED_BOOT) && !defined(WOLFBOOT_NO_PARTITIONS)
1446-
/* hash wolfBoot and extend PCR */
1463+
#if defined(WOLFBOOT_MEASURED_BOOT) && defined(SELF_HASH_ADDR)
1464+
/* measured boot: hash wolfBoot code (or boot partition if
1465+
* WOLFBOOT_MEASURED_BOOT_APP_PARTITION) and extend PCR */
14471466
if (rc == 0) {
14481467
rc = self_hash(digest);
14491468
if (rc == 0) {
@@ -1453,7 +1472,7 @@ int wolfBoot_tpm2_init(void)
14531472
wolfBoot_printf("Error %d performing wolfBoot measurement!\n", rc);
14541473
}
14551474
}
1456-
#endif /* defined(WOLFBOOT_MEASURED_BOOT) && !defined(WOLFBOOT_NO_PARTITIONS) */
1475+
#endif /* WOLFBOOT_MEASURED_BOOT && SELF_HASH_ADDR */
14571476

14581477
return rc;
14591478
}

0 commit comments

Comments
 (0)