Skip to content

Add OP-TEE trusted application support for the fwTPM - #659

Merged
mattia-moffa merged 1 commit into
wolfSSL:masterfrom
dgarske:optee_port
Nov 7, 2026
Merged

mattia-moffa merged 1 commit into
wolfSSL:masterfrom
dgarske:optee_port

Conversation

@dgarske

@dgarske dgarske commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Description

The fwTPM as an OP-TEE trusted application: TPM 2.0 command processing in the secure world, client in the normal world. Needs only the TEE Internal Core API and OP-TEE secure storage, so nothing in it is specific to an SoC.

Builds on two wolfSSL changes: wolfSSL/wolfssl#11919 added the WOLFSSL_OPTEE build environment, and wolfSSL/wolfssl#11927 maps the wolfCrypt heap, RNG and time onto the TEE. That is why this port carries no platform shims of its own.

FWTPM_ProcessCommand has the shape of TA_InvokeCommandEntryPoint, so one invocation carries one whole TPM command and no IO HAL or TIS layer is involved. Both server loops block, which a trusted application cannot do, so fwtpm_io.c, fwtpm_tis.c, fwtpm_tis_shm.c and fwtpm_main.c are left out of a TA build.

Adds

  • src/fwtpm/ports/optee/ - the TA_*EntryPoint functions and an NV journal on OP-TEE secure storage, keyed from the hardware unique key through the system PTA
  • wolftpm/fwtpm/fwtpm_optee.h - the trusted application's UUID and the command IDs the client shares
  • src/tpm2_optee.c, wolftpm/tpm2_optee.h - the WOLFTPM_OPTEE libteec client transport, following the WOLFTPM_SWTPM whole-command pattern rather than emulating TIS registers
  • --enable-optee, and WOLFTPM_INTERFACE=OPTEE in CMake, which finds and links libteec

Touches shared code

  • src/tpm2.c, hal/tpm_io.c, hal/tpm_io.h, wolftpm/tpm2_types.h, src/tpm2_wrap.c, src/tpm2_spdm.c - add WOLFTPM_OPTEE to the existing "no bus, so no TIS" gates beside WOLFTPM_LINUX_DEV and WOLFTPM_SWTPM
  • src/fwtpm/fwtpm_command.c, wolftpm/fwtpm/fwtpm.h, wolftpm/fwtpm/fwtpm_command.h - FWTPM_SetCommandClient and FWTPM_FlushCommandClient. Transient objects, sessions, sequences and saved session contexts record the client that created them, so a closing TEE session flushes only its own while others stay open. Ownership does not restrict access between clients. Other transports stay on client 0 and keep FWTPM_ResetCommandClient
  • wolftpm/tpm2_types.h - a WOLFSSL_OPTEE arm for XTPM_GET_TIMEMS on TEE_GetSystemTime. A trusted application has no CLOCK_MONOTONIC, so the POSIX branch left WOLFTPM_HAVE_MONOTONIC_MS unset and command timeouts fell back to a counter. Not TEE_GetREETime, because the normal world owns that clock and can move it backwards. Applies with or without wolfCrypt
  • wolftpm/tpm2_types.h - and where wolfCrypt is not compiled in, map that path's own XMALLOC and XFREE onto TEE_Malloc and TEE_Free. WOLFTPM2_NO_HEAP still wins and leaves both undefined
  • src/fwtpm/fwtpm_nv.c - a storage read error while loading the journal now fails FWTPM_NV_Init instead of regenerating, so a transient fault cannot replace the seeds. Only invalid content still regenerates. The default file backend reports a missing file, and the region past its end, as erased NV, which first boot relies on
  • src/fwtpm/fwtpm_nv.c - print the NV version mismatch with printf under WOLFSSL_OPTEE only, since a trusted application cannot link fprintf. Every other build keeps stderr
  • src/fwtpm/ports/optee/fwtpm_optee_ta.c - accept TEE_PARAM_TYPE_MEMREF_INOUT for the response buffer as well as MEMREF_OUTPUT. Linux's tpm_ftpm_tee carries both memrefs in one registered shared memory and so passes the response as INOUT; without this it gets TEE_ERROR_NOT_SUPPORTED. Nothing is read out of that buffer either way, and it is what lets an in-kernel TPM driver bind to this trusted application and present a /dev/tpm device

Testing

Validated on an NVIDIA Jetson Orin Nano (L4T R36.4.4): unmodified caps, getrandom, native_test, wrap_test and the four pqc/ examples pass against the trusted application, and NV persists across separate client processes. The same examples pass with a tpm_ftpm_tee-derived driver bound, holding its own session alongside the libteec clients.

Multi-config build sweep green with WOLFTPM_OPTEE undefined, including the --enable-st33 and --enable-advio TIS paths the gate changes affect, --disable-wrapper, --enable-devtpm, --enable-autodetect and --enable-fwtpm --enable-v185. make check green against both the fwTPM server and ibmswtpm2.

@mattia-moffa mattia-moffa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running this:

./autogen.sh
./configure --enable-optee --disable-swtpm
make

fails with:

In file included from tests/fwtpm_hal_unit_tests.c:90:
tests/../hal/tpm_io_fwtpm.c:322:5: error: no previous prototype for ‘TPM2_IoCb_FwTPM’ [-Werror=missing-prototypes]
  322 | int TPM2_IoCb_FwTPM(TPM2_CTX* ctx, int isRead, word32 addr,
      |     ^~~~~~~~~~~~~~~

This is fixed by adding "$enable_optee" to the list at configure.ac:259.

Comment thread src/tpm2_wrap.c Resolved
Comment thread src/fwtpm/ports/optee/fwtpm_optee_nv.c Resolved
Comment thread src/fwtpm/ports/optee/fwtpm_optee_ta.c Resolved
Comment thread src/fwtpm/ports/optee/fwtpm_optee_ta.c Resolved
Comment thread src/include.am Resolved
Copilot AI balanced review requested due to automatic review settings November 6, 2026 16:26
@dgarske dgarske assigned mattia-moffa and wolfSSL-Bot and unassigned dgarske Nov 6, 2026
@dgarske
dgarske requested a review from mattia-moffa November 6, 2026 16:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A transient NV read failure can erase existing TPM state, while sensitive buffers remain uncleared and the OP-TEE transport lacks required CMake selection and linkage.

4 open issues
What changed in this PR

Adds OP-TEE support so wolfTPM’s firmware TPM can process commands in the secure world, with a normal-world client using libteec.

Changes:

  • Adds trusted-application entry points, secure-storage NV support, and a whole-command client transport.
  • Tracks transient-state ownership so closing one client releases its resources without flushing other clients.
  • Updates transport gates, platform mappings, build integration, documentation, and unit tests.
File Description
wolftpm/​tpm2_types.h Adds OP-TEE transport gates, heap mappings, and timing support.
wolftpm/​tpm2_optee.h Declares client transport and cleanup functions.
wolftpm/​include.am Installs the new OP-TEE headers.
wolftpm/​fwtpm/​fwtpm.h Adds client ownership fields and clarifies the integrity-key callback contract.
wolftpm/​fwtpm/​fwtpm_optee.h Defines the shared UUID, command IDs, and secure-world interfaces.
wolftpm/​fwtpm/​fwtpm_nv.h Corrects NV HAL lifetime documentation.
wolftpm/​fwtpm/​fwtpm_command.h Declares per-client ownership and cleanup APIs.
tests/​fwtpm_unit_tests.c Tests selective cleanup and loaded-session ownership.
src/​tpm2.c Integrates OP-TEE command dispatch and cleanup.
src/​tpm2_wrap.c Updates initialization, locality, and firmware transport gates.
src/​tpm2_spdm.c Excludes OP-TEE from TIS-specific SPDM paths.
src/​tpm2_optee.c Implements the libteec client transport.
src/​include.am Adds conditional compilation of the client transport.
src/​fwtpm/​README.md Updates the documented build-option list.
src/​fwtpm/​ports/​optee/​README.md Documents architecture, storage, sessions, and port requirements.
src/​fwtpm/​ports/​optee/​fwtpm_optee_ta.c Implements trusted-application lifecycle and command handling.
src/​fwtpm/​ports/​optee/​fwtpm_optee_nv.c Implements secure-storage NV, integrity-key handling, and the clock HAL.
src/​fwtpm/​include.am Includes OP-TEE port sources and documentation in distributions.
src/​fwtpm/​fwtpm_nv.c Uses TA-compatible output for NV version warnings.
src/​fwtpm/​fwtpm_command.c Records client ownership and selectively flushes transient state.
hal/​tpm_io.h Makes the OP-TEE I/O callback unnecessary.
hal/​tpm_io.c Excludes OP-TEE from bus callback implementation.
configure.ac Adds --enable-optee and checks client dependencies.
CMakeLists.txt Adds the OP-TEE client source to the source list.

Review effort: Balanced

Comment thread src/fwtpm/ports/optee/fwtpm_optee_nv.c Resolved
Comment thread CMakeLists.txt Resolved
Comment thread src/fwtpm/ports/optee/fwtpm_optee_ta.c Resolved
Comment thread src/tpm2_optee.c Resolved

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #659

Scan targets checked: wolftpm-src, wolftpm-bugs
Not reviewed (no scan target covers them): CMakeLists.txt, configure.ac, hal/tpm_io.h, src/fwtpm/README.md, src/fwtpm/include.am, src/fwtpm/ports/optee/README.md, src/include.am, wolftpm/include.am
Coverage: 13 of 16 in-scope changed file(s) opened by the reviewer; not opened: wolftpm/fwtpm/fwtpm_command.h, wolftpm/fwtpm/fwtpm_nv.h, wolftpm/tpm2_optee.h

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Max

Comment thread src/tpm2_optee.c Resolved
@mattia-moffa
mattia-moffa merged commit 1362e1e into wolfSSL:master Nov 7, 2026
249 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants