Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/auto-pin-dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
"lib/wolfSSL|https://github.com/wolfSSL/wolfSSL.git|v*-stable|51975e27a5439668733976a584d417dfd7776026"
"lib/wolfHSM|https://github.com/wolfSSL/wolfHSM.git|wolfHSM-v*|66e2482333ff175a14d10a25c501367362212a38"
"lib/wolfIP|https://github.com/wolfSSL/wolfIP.git|v*|"
"lib/wolfTPM|https://github.com/wolfSSL/wolfTPM.git|v*|e9da7bab386f40bd8959c47590d4a9b328eb9121"
"lib/wolfTPM|https://github.com/wolfSSL/wolfTPM.git|v*|899abf15dc785d51c14592c23df071604b839a64"
"lib/wolfPKCS11|https://github.com/wolfSSL/wolfPKCS11.git|v*-stable|270a337ab6aa47feb7f60fa5a7a736e01c9da9ee"
)

Expand Down
29 changes: 29 additions & 0 deletions docs/TPM.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ interface profile follows the runner's `WT_PROFILE` unless
`WT_INTERFACE_PROFILE` is set. [Footprint](Footprint.md#tpm-20-and-pkcs11)
has the Secure image and application sizes for every port and engine.

## Application setup

The application builds wolfTPM with `WOLFTPM_USER_TRANSPORT`, which is
wolfTPM's application transport (`--enable-usertransport`, or CMake
`WOLFTPM_INTERFACE=USER`), and links `src/client/tpm_psa_transport.c`. It
passes `wt_tpm_psa_io` as the wolfTPM IO callback:

```c
#include <wolftrust/tpm_psa_transport.h>
#include <wolftpm/tpm2_wrap.h>

WOLFTPM2_DEV dev;
int rc;

rc = wolfTPM2_Init(&dev, wt_tpm_psa_io, NULL);
/* ... TPM2_* and wolfTPM2_* calls ... */
wolfTPM2_Cleanup(&dev);
wt_tpm_psa_close();
```

In this build wolfTPM's IO callback carries a whole command, so every command
that wolfTPM builds goes to `wt_tpm_psa_transact()`. That function sends the
command to `SERVICE_TPM` in one call and returns the response for wolfTPM to
parse. `wt_tpm_psa_close()` drops the PSA connection when the application is
done. The application never handles TPM frames itself, and it does not override
any wolfTPM function. `tests/firmware/interface-guest/tpm_tests.c` is the
reference client. wolfTPM's [application transport](https://github.com/wolfSSL/wolfTPM/blob/master/docs/system-interfaces.md#application-transport)
documents the callback contract.

## Service boundary

The application links the wolfTPM client and `SERVICE_TPM` transport. It does
Expand Down
9 changes: 9 additions & 0 deletions include/wolftrust/tpm_psa_transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ int wt_tpm_psa_random(uint8_t* out, size_t len);
/* Drop the cached connection (after a transport error or for teardown). */
void wt_tpm_psa_close(void);

#if defined(WOLFTPM_USER_TRANSPORT)
#include <wolftpm/tpm2.h>

/* wolfTPM IO callback for this transport: wolfTPM2_Init(&dev, wt_tpm_psa_io,
* NULL). Call wt_tpm_psa_close() after wolfTPM2_Cleanup. */
int wt_tpm_psa_io(TPM2_CTX* ctx, const BYTE* cmd, UINT32 cmd_sz, BYTE* rsp,
UINT32 rsp_max, UINT32* rsp_sz, void* user_ctx);
#endif

#endif /* WOLFTRUST_TPM_PSA_TRANSPORT_H */
2 changes: 1 addition & 1 deletion lib/wolfTPM
Submodule wolfTPM updated 282 files
77 changes: 11 additions & 66 deletions src/client/tpm_psa_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,75 +97,20 @@ int wt_tpm_psa_random(uint8_t* out, size_t len)
return rc;
}

#if defined(WOLFTPM_SWTPM)
/* wolfTPM client transport: built with WOLFTPM_SWTPM so the core routes
* every command through TPM2_SWTPM_SendCommand, which this file provides in
* place of the socket client. One synchronous call returns the response. */
#include <wolftpm/tpm2.h>
#include <wolftpm/tpm2_packet.h>
#include <wolftpm/tpm2_tis.h>
#include <wolftpm/tpm2_swtpm.h>

/* The core still runs its TIS chip-startup sequence; the mediated door has
* no TIS registers, so startup wait, locality, and device info are no-ops. */
int TPM2_TIS_StartupWait(TPM2_CTX* ctx, int timeout)
{
(void)ctx;
(void)timeout;
return TPM_RC_SUCCESS;
}

int TPM2_TIS_RequestLocality(TPM2_CTX* ctx, int timeout)
{
if (ctx == NULL) {
return BAD_FUNC_ARG;
}
(void)timeout;
ctx->locality = WOLFTPM_LOCALITY_DEFAULT;
return TPM_RC_SUCCESS;
}

int TPM2_TIS_GetInfo(TPM2_CTX* ctx)
{
(void)ctx;
return TPM_RC_SUCCESS;
}

void TPM2_SwtpmClose(TPM2_CTX* ctx)
{
(void)ctx;
wt_tpm_psa_close();
}

int TPM2_SWTPM_SendCommand(TPM2_CTX* ctx, TPM2_Packet* packet)
#if defined(WOLFTPM_USER_TRANSPORT)
int wt_tpm_psa_io(TPM2_CTX* ctx, const BYTE* cmd, UINT32 cmd_sz, BYTE* rsp,
UINT32 rsp_max, UINT32* rsp_sz, void* user_ctx)
{
size_t rsp_len = 0U;
uint32_t declared;
size_t got = 0U;
int rc;

(void)ctx;
if (packet == NULL || packet->buf == NULL || packet->pos <= 0 ||
packet->size <= 0 || packet->pos > packet->size) {
return BAD_FUNC_ARG;
}
rc = wt_tpm_psa_transact(packet->buf, (size_t)packet->pos, packet->buf,
(size_t)packet->size, &rsp_len);
if (rc != 0 || rsp_len < (size_t)TPM2_HEADER_SIZE ||
rsp_len > (size_t)packet->size) {
return TPM_RC_FAILURE;
(void)user_ctx;
rc = wt_tpm_psa_transact(cmd, (size_t)cmd_sz, rsp, (size_t)rsp_max,
&got);
if (rc == 0) {
*rsp_sz = (UINT32)got;
}
/* The response header declares its own total length; reject a mediated
* frame whose body length disagrees rather than let the core parse stale
* buffer tail past the bytes actually received. */
declared = ((uint32_t)packet->buf[2] << 24) |
((uint32_t)packet->buf[3] << 16) |
((uint32_t)packet->buf[4] << 8) |
(uint32_t)packet->buf[5];
if (declared != (uint32_t)rsp_len) {
return TPM_RC_FAILURE;
}
/* Bind the parser to the bytes actually received. */
packet->size = (int)rsp_len;
return TPM_RC_SUCCESS;
return rc;
}
#endif /* WOLFTPM_SWTPM */
#endif /* WOLFTPM_USER_TRANSPORT */
6 changes: 3 additions & 3 deletions tests/firmware/interface-guest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ SRCS := main.c ../psa-guest/guest_libc.c $(ROOT)/src/client/psa_ffm_client.c
ifeq ($(WT_INTERFACE),tpm)
APP_NAME := tpm-guest0
CFLAGS += -I$(WOLFTPM_DIR) -DWOLFTPM_USER_SETTINGS -DWT_TPM_ROLE_APP \
-DWOLFTPM_SWTPM -DWOLF_CRYPTO_CB -DWOLFTPM_CRYPTOCB -DWOLFTPM_SMALL_STACK \
-DWOLFTPM_USER_TRANSPORT -DWOLF_CRYPTO_CB -DWOLFTPM_CRYPTOCB -DWOLFTPM_SMALL_STACK \
-DWT_INTERFACE_TPM -DWT_TPM_APP_NAME=\"$(APP_NAME)\" \
-DWOLFSSL_SP_ASM -DWOLFSSL_SP_ARM_CORTEX_M_ASM -DWOLFSSL_ARM_ARCH=8 \
-DNO_ERROR_STRINGS -DWOLFSSL_PUBLIC_MP -DWC_RSA_DIRECT \
Expand All @@ -100,8 +100,8 @@ SRCS += tpm_tests.c $(ROOT)/src/client/tpm_psa_transport.c \
$(addprefix $(WOLFSSL_DIR)/wolfcrypt/src/, aes.c cryptocb.c ecc.c \
random.c sha256.c asn.c coding.c error.c hash.c hmac.c memory.c sha.c \
sp_cortexm.c sp_int.c wolfmath.c wc_port.c sha512.c kdf.c pwdbased.c)
KEEP := -Wl,-u,wt_tpm_psa_transact,-u,TPM2_SWTPM_SendCommand
WIRED := wt_tpm_psa_transact TPM2_SWTPM_SendCommand wt_tpm_psa_random
KEEP := -Wl,-u,wt_tpm_psa_transact,-u,wt_tpm_psa_io
WIRED := wt_tpm_psa_transact wt_tpm_psa_io wt_tpm_psa_random
ABSENT := 'fwTPM|FWTPM_|WP11_|wh_Client|wt_crypto_native'
else
APP_NAME := pkcs11-guest0
Expand Down
3 changes: 2 additions & 1 deletion tests/firmware/interface-guest/tpm_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ int wt_tpm_run(void)
int rc = -1;

for (tries = 0; tries < 50 && rc != 0; tries++) {
rc = wolfTPM2_Init(&g_dev, NULL, NULL);
rc = wolfTPM2_Init(&g_dev, wt_tpm_psa_io, NULL);
if (rc != 0) {
wt_tpm_psa_close();
wt_tpm_wait();
Expand Down Expand Up @@ -510,6 +510,7 @@ int wt_tpm_run(void)
app_line("tpm lockout ok");
#endif
(void)wolfTPM2_Cleanup(&g_dev);
wt_tpm_psa_close();
app_line("tpm round trip ok");
return 0;
}
8 changes: 6 additions & 2 deletions tests/host/tpm_client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CC ?= cc
BUILD_DIR ?= build

CFLAGS := \
-DWOLFTPM_SWTPM \
-DWOLFTPM_USER_TRANSPORT \
-DWOLFTPM_USER_SETTINGS \
-DWOLFTPM2_NO_WOLFCRYPT \
-DSINGLE_THREADED \
Expand All @@ -46,10 +46,11 @@ endif
_SRCS := \
$(ROOT)/src/client/tpm_psa_transport.c \
$(WOLFTPM)/src/tpm2.c \
$(WOLFTPM)/src/tpm2_packet.c \
$(WOLFTPM)/src/tpm2_util.c \
main.c

_OBJS := $(addprefix $(BUILD_DIR)/,msg_client.o tpm_psa_transport.o tpm2.o tpm2_util.o main.o)
_OBJS := $(addprefix $(BUILD_DIR)/,msg_client.o tpm_psa_transport.o tpm2.o tpm2_packet.o tpm2_util.o main.o)
TEST_BIN := $(BUILD_DIR)/test_tpm_client

.PHONY: all run compilers sanitize valgrind clean
Expand All @@ -68,6 +69,9 @@ $(BUILD_DIR)/msg_client.o: $(ROOT)/src/client/msg_client.c | $(BUILD_DIR)
$(BUILD_DIR)/tpm2.o: $(WOLFTPM)/src/tpm2.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c -o $@ $<

$(BUILD_DIR)/tpm2_packet.o: $(WOLFTPM)/src/tpm2_packet.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c -o $@ $<

$(BUILD_DIR)/tpm2_util.o: $(WOLFTPM)/src/tpm2_util.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c -o $@ $<

Expand Down
99 changes: 34 additions & 65 deletions tests/host/tpm_client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
*/

/* TPM PSA client suite: a scripted PSA transport checks connection recovery,
* synchronous execution, response framing, and the SWTPM initialization contract. */
* synchronous execution, response framing, and the user transport contract. */

#include "psa/client.h"
#include "psa/error.h"
#include "wolftrust/tpm_psa_transport.h"

#include <wolftpm/tpm2.h>
#include <wolftpm/tpm2_packet.h>
#include <wolftpm/tpm2_swtpm.h>
#include <wolftpm/tpm2_tis.h>

#include <stdint.h>
Expand Down Expand Up @@ -189,12 +188,13 @@ static void set_success_response(size_t declared, size_t received)
g_response_len = received;
}

static void packet_init(TPM2_Packet* packet, uint8_t* frame, size_t size)
static TPM_RC send_startup(void)
{
(void)memset(frame, 0, size);
packet->buf = frame;
packet->size = (int)size;
packet->pos = TPM2_HEADER_SIZE;
Startup_In in;

(void)memset(&in, 0, sizeof(in));
in.startupType = TPM_SU_CLEAR;
return TPM2_Startup(&in);
}

static void test_arguments(void)
Expand Down Expand Up @@ -256,94 +256,64 @@ static void test_connection_recovery(void)
static void test_execution_and_framing(void)
{
TPM2_CTX ctx;
TPM2_Packet packet;
uint8_t frame[32];
int rc;

check(TPM2_Init(&ctx, wt_tpm_psa_io, NULL) == TPM_RC_SUCCESS,
"user transport initializes without a TIS startup");

mock_reset();
packet_init(&packet, frame, sizeof(frame));
set_success_response(TPM2_HEADER_SIZE, TPM2_HEADER_SIZE);
rc = TPM2_SWTPM_SendCommand(&ctx, &packet);
check(rc == TPM_RC_SUCCESS && g_calls == 1 &&
packet.size == TPM2_HEADER_SIZE,
check(send_startup() == TPM_RC_SUCCESS && g_calls == 1,
"one synchronous call returns the framed response");

mock_reset();
packet_init(&packet, frame, sizeof(frame));
g_call_status = PSA_ERROR_CONNECTION_BUSY;
rc = TPM2_SWTPM_SendCommand(&ctx, &packet);
check(rc == TPM_RC_FAILURE && g_calls == 1,
check(send_startup() == TPM_RC_FAILURE && g_calls == 1,
"a failed command is never implicitly replayed");

mock_reset();
packet_init(&packet, frame, sizeof(frame));
set_success_response(TPM2_HEADER_SIZE, TPM2_HEADER_SIZE - 1U);
check(TPM2_SWTPM_SendCommand(&ctx, &packet) == TPM_RC_FAILURE,
check(send_startup() == TPM_RC_FAILURE,
"a response shorter than the TPM header is refused");

mock_reset();
packet_init(&packet, frame, sizeof(frame));
set_success_response(TPM2_HEADER_SIZE + 1U, TPM2_HEADER_SIZE);
check(TPM2_SWTPM_SendCommand(&ctx, &packet) == TPM_RC_FAILURE,
check(send_startup() == TPM_RC_FAILURE,
"a response whose declared length disagrees is refused");

mock_reset();
packet_init(&packet, frame, sizeof(frame));
set_success_response(TPM2_HEADER_SIZE, TPM2_HEADER_SIZE);
g_forced_out_len = sizeof(frame) + 1U;
check(TPM2_SWTPM_SendCommand(&ctx, &packet) == TPM_RC_FAILURE,
g_forced_out_len = WT_TPM_FRAME_MAX + 1U;
check(send_startup() == TPM_RC_FAILURE,
"a response reported beyond the packet capacity is refused");
}

static void test_packet_bounds(void)
{
TPM2_CTX ctx;
TPM2_Packet packet;
uint8_t frame[32];

mock_reset();
packet_init(&packet, frame, sizeof(frame));
packet.size = 0;
check(TPM2_SWTPM_SendCommand(&ctx, &packet) == BAD_FUNC_ARG &&
g_connects == 0 && g_calls == 0,
"SWTPM transport rejects a zero packet capacity before I/O");

packet_init(&packet, frame, sizeof(frame));
packet.size = -1;
check(TPM2_SWTPM_SendCommand(&ctx, &packet) == BAD_FUNC_ARG &&
g_connects == 0 && g_calls == 0,
"SWTPM transport rejects a negative packet capacity before I/O");

packet_init(&packet, frame, sizeof(frame));
packet.pos = packet.size + 1;
check(TPM2_SWTPM_SendCommand(&ctx, &packet) == BAD_FUNC_ARG &&
g_connects == 0 && g_calls == 0,
"SWTPM transport rejects a command beyond packet capacity");
(void)TPM2_Cleanup(&ctx);
}

static void test_locality_contract(void)
static void test_init_contract(void)
{
TPM2_CTX ctx;
int rc;

mock_reset();
check(TPM2_TIS_RequestLocality(NULL, 1) == BAD_FUNC_ARG,
"locality request rejects a NULL context");
rc = TPM2_Init(&ctx, NULL, NULL);
check(TPM2_Init(&ctx, NULL, NULL) == BAD_FUNC_ARG && g_calls == 0,
"initialization without the PSA IO callback is refused");
rc = TPM2_Init(&ctx, wt_tpm_psa_io, NULL);
check(rc == TPM_RC_SUCCESS && ctx.locality == WOLFTPM_LOCALITY_DEFAULT &&
TPM2_GetActiveCtx() == &ctx,
"direct TPM initialization records the granted locality");
"direct TPM initialization uses the default locality");
set_success_response(TPM2_HEADER_SIZE, TPM2_HEADER_SIZE);
check(send_startup() == TPM_RC_SUCCESS && g_connects == 1,
"the first command opens the service connection");
check(TPM2_Cleanup(&ctx) == TPM_RC_SUCCESS &&
TPM2_GetActiveCtx() == NULL,
"direct TPM cleanup clears the active context");
TPM2_GetActiveCtx() == NULL && g_closes == 0,
"TPM cleanup leaves the service connection to the caller");
wt_tpm_psa_close();
check(g_closes == 1, "closing the transport closes the connection");

rc = TPM2_Init_ex(&ctx, NULL, NULL, 1);
check(rc == TPM_RC_SUCCESS && ctx.locality == WOLFTPM_LOCALITY_DEFAULT &&
TPM2_GetActiveCtx() == &ctx,
"explicit SWTPM startup records the granted locality");
check(TPM2_Cleanup(&ctx) == TPM_RC_SUCCESS &&
TPM2_GetActiveCtx() == NULL,
"explicit SWTPM startup remains cleanup-safe");
check(TPM2_Init_ex(&ctx, wt_tpm_psa_io, NULL, 1) == BAD_FUNC_ARG &&
g_calls == 1,
"a TIS chip startup is refused without I/O");
(void)TPM2_Cleanup(&ctx);
}

static int rng_stream_ok(const uint8_t* out, size_t len)
Expand Down Expand Up @@ -476,8 +446,7 @@ int main(void)
test_arguments();
test_connection_recovery();
test_execution_and_framing();
test_packet_bounds();
test_locality_contract();
test_init_contract();
test_random();

wt_tpm_psa_close();
Expand Down
Loading