Skip to content

Add Go Bindings for TPM 2.0 Reference Implementation#2

Open
josephlr wants to merge 3 commits into
backportsfrom
bindings
Open

Add Go Bindings for TPM 2.0 Reference Implementation#2
josephlr wants to merge 3 commits into
backportsfrom
bindings

Conversation

@josephlr
Copy link
Copy Markdown
Member

@josephlr josephlr commented May 14, 2026

Depends on #1, so we temporarily use backports as the base branch (to make the changes easier to see). Will switch back to main once #1 is merged.

This PR adds Go bindings to the C code, replacing the terrible hacks in https://github.com/google/go-tpm-tools/tree/main/simulator/internal.

As with the old solution, we compile the entire TPM reference code using Go's CGO build system. To maintain an explicit list of source files without copying their contents, we use wrapper C files that #include the original C files in the repository. This avoids issues with symlinks not being handled correctly by fetched Go modules.

The platform interface required by the TPM reference code is implemented in Go, providing flexibility via callbacks and interfaces (rather than using https://github.com/google/go-tpm-tools/tree/main/simulator/ms-tpm-20-ref/Samples/Google). For example, the Storage struct uses a fixed array of size C.NV_MEMORY_SIZE and supports optional callbacks for OnEnable, OnReady, and OnCommit.

The entire repository is now the Go module (github.com/google/TPM), which ensures that the C source files in TPMCmd are included when the module is fetched by GOPROXY.

Commit Breakdown

  1. Add scaffolding for Go bindings

    • ./bindings/c/1build.go: to handle CGO compilation flags and define C wrapper functions.
    • ./bindings/README.md: instructions on building, testing, and publishing.
    • ./bindings/regenerate_c.py: automate the creation of wrapper C files.
    • ./go.mod: Must be at the repository root.
  2. Run ./bindings/regenerate_c.py

    • Contains the 200+ wrapper C files that include the source files needed for the build, located in ./bindings/c/ and pointing directly to the files in ./TPMCmd.
  3. Platform and Entrypoints code

    • ./bindings/go/platform/: implements the Go platform callbacks (storage, timer, system, act).
    • ./bindings/go/entrypoints/ : provides entrypoints for Manufacture, Init, and ExecuteCommand. Note that Manufacture takes no arguments and always forces manufacturing.

Verification

Build and run everything in this module from the repository root:

go build ./bindings/go/...
go test ./bindings/go/entrypoints/...

The bindings were verified by integrating them into go-tpm and running the full tpm2/test suite. All tests passed.

@josephlr josephlr force-pushed the bindings branch 6 times, most recently from 58c08bb to e4e1412 Compare May 14, 2026 17:05
@josephlr josephlr changed the base branch from main to backports May 14, 2026 17:06
josephlr added 3 commits May 14, 2026 18:17
Just the documentation, wrapper generation script, and bare-bones module.
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.

1 participant