crb

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2026 License: BSD-3-Clause Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-tpm2/crb

go-tpm2/crb

CI Go Reference Coverage License

A pure-Go TPM 2.0 Command Response Buffer (CRB) MMIO transport. v0.1.0.

crb implements github.com/go-tpm2/common's Transport interface over its Regs MMIO accessor. The platform owns the register mapping (a physical MMIO window, an mmap of /dev/mem, or a test stub) and exposes it through common.Regs; this package drives the CRB register handshake defined by the TCG PC Client Platform TPM Profile (PTP) Specification, clause Command Response Buffer Interface.

Sibling repos: common (interfaces + codec), tis (the FIFO transport alternative), tpm2 (the command layer that rides on this Transport), and validate (live swtpm validation).

Install

go get github.com/go-tpm2/crb

Usage

import (
    "github.com/go-tpm2/common"
    "github.com/go-tpm2/crb"
)

// r is a platform-provided common.Regs over the CRB control area base.
dev, err := crb.Open(r)
if err != nil {
    // not a CRB interface, or the interface never reported valid
}

// *crb.CRB satisfies common.Transport, so it plugs straight into the
// go-tpm2/tpm2 command layer:
//   tpm := tpm2.New(dev)
//   tpm.Startup(uint16(common.SUClear))
//   rnd, _ := tpm.GetRandom(20)

// …or drive raw command buffers directly:
cmd := common.BuildCommand(uint16(common.TagNoSessions),
    uint32(common.CCGetRandom), []byte{0x00, 0x02})
rsp, err := dev.Send(cmd) // common.Transport.Send

Send state machine (TCG PTP)

  1. Request locality — LOC_CTRL.requestAccess, wait LOC_STS.Granted + LOC_STATE.locAssigned.
  2. Request command-ready — CTRL_REQ.cmdReady, poll until the TPM leaves Idle (CTRL_STS.tpmIdle clear) with no error and cmdReady self-cleared.
  3. Write command into the data buffer (common.WriteBytes).
  4. Start — CTRL_START.start = 1.
  5. Wait for the TPM to clear CTRL_START.start (bounded spin); on expiry drive CTRL_CANCEL and return ErrTimeout.
  6. Check CTRL_STS.Error.
  7. Read the 10-byte response header for responseSize, bounds-check, then read the whole response.
  8. Release — CTRL_REQ.goIdle.

Endianness

The TPM 2.0 wire protocol (the command/response byte stream) is big-endian and handled by common's codec. The CRB control registers are little-endian MMIO; common.Regs.Read32/Write32 access them in the platform's native width, so this package never byte-swaps register values — it only moves the opaque command/response byte streams through the data buffer.

Conventions

Pure Go, CGO_ENABLED=0, no assembly, big-endian TPM wire (via common), BSD-3-Clause, 100% statement coverage (GOWORK=off go test -cover), GOWORK=off.

Register offsets, bit positions, and handshake steps are cited inline to the TCG PTP specification. A few values that the spec leaves implementation-defined (the data-buffer base offset, the CTRL_CANCEL write value, the spin bound, the buffer size) are marked // INFERRED: and verified by the validate harness against a real swtpm 0.10.1 under QEMU -device tpm-crb.

Specifications

  • TCG PC Client Platform TPM Profile (PTP) — Command Response Buffer Interface.
  • TCG TPM 2.0 Library, Parts 1–4 (wire format, via common).

License

BSD-3-Clause. See LICENSE.

Documentation

Overview

Package crb implements the TPM 2.0 Command Response Buffer (CRB) interface as a github.com/go-tpm2/common.Transport over the platform-provided common.Regs MMIO accessor.

The CRB interface and its register block are defined by the TCG "PC Client Platform TPM Profile (PTP) Specification" (Family 2.0), in the clause "Command Response Buffer Interface" and its register tables ("Register Space" / "CRB Interface Definition"). Unless noted with an INFERRED marker, every register offset, bit position, and handshake step below is taken from that specification.

Endianness note: the TPM 2.0 *wire* protocol (the command/response byte stream this transport carries) is BIG-ENDIAN and is encoded by github.com/go-tpm2/common's codec. The CRB *control registers* themselves are LITTLE-ENDIAN MMIO; common.Regs.Read32/Write32 access them in the platform's native register width, so this package never byte-swaps register values — it only ever moves the opaque command and response byte streams through the data buffer with common.WriteBytes / common.ReadBytes.

Conventions: pure Go, CGO_ENABLED=0, no architecture-specific assembly, BSD-3-Clause on every file, 100% statement coverage, and GOWORK=off.

Index

Constants

View Source
const (
	// ErrNotCRB is returned by Open when INTERFACE_ID does not advertise
	// the CRB interface type.
	ErrNotCRB = common.Error("crb: interface is not CRB")
	// ErrLocality is returned when the locality cannot be granted.
	ErrLocality = common.Error("crb: locality not granted")
	// ErrNotReady is returned when the TPM never reaches command-ready.
	ErrNotReady = common.Error("crb: command-ready not granted")
	// ErrTPM is returned when CTRL_STS reports a fatal TPM error.
	ErrTPM = common.Error("crb: TPM reported a fatal error")
	// ErrTimeout is returned when the TPM never clears CTRL_START.start.
	ErrTimeout = common.Error("crb: timed out waiting for command completion")
	// ErrShortResponse is returned when the response is smaller than a
	// TPM 2.0 header.
	ErrShortResponse = common.Error("crb: response shorter than header")
	// ErrResponseTooLarge is returned when the declared response size
	// exceeds the data buffer.
	ErrResponseTooLarge = common.Error("crb: response larger than data buffer")
)

Error sentinels for the CRB transport, typed as common.Error so callers may compare with ==.

Variables

This section is empty.

Functions

This section is empty.

Types

type CRB

type CRB struct {
	// contains filtered or unexported fields
}

CRB is a TPM 2.0 Command Response Buffer transport bound to a single locality's control area, reached through a common.Regs accessor. It satisfies common.Transport.

func Open

func Open(r common.Regs) (*CRB, error)

Open binds a CRB transport to the register window r and validates that the window actually exposes a CRB interface.

Validation follows TCG PTP: it reads INTERFACE_ID and checks that its InterfaceType nibble names the CRB interface ("TPM_INTERFACE_ID_x", field "InterfaceType"), then reads LOC_STATE and waits for tpmRegValidSts ("TPM_LOC_STATE_x", field "tpmRegValidSts") so the state register may be trusted by the caller.

func (*CRB) Send

func (c *CRB) Send(cmd []byte) (rsp []byte, err error)

Send transmits one fully-marshaled TPM 2.0 command buffer through the CRB interface and returns the full response buffer (header + params). It satisfies common.Transport.

State machine (TCG PTP, clause "Command Response Buffer Interface", "CRB Interface State Transitions" and the per-register field tables):

  1. Request the locality: write requestAccess to LOC_CTRL, then wait for LOC_STS.Granted with LOC_STATE.locAssigned. (TCG PTP, "TPM_LOC_CTRL_x"/"TPM_LOC_STS_x"/"TPM_LOC_STATE_x".)
  2. Request command-ready: write cmdReady to CTRL_REQ and poll until the TPM leaves Idle and is not in error — CTRL_STS with tpmIdle clear and Error clear, and CTRL_REQ.cmdReady self-clearing once the transition completes. (TCG PTP, "CRB_CTRL_REQ_x"/"CRB_CTRL_STS_x".)
  3. Write the command into the data buffer (common.WriteBytes).
  4. Set CTRL_START.start to hand the command to the TPM. (TCG PTP, "CRB_CTRL_START_x".)
  5. Poll CTRL_START.start until the TPM clears it (command complete), bounded by maxSpin; on expiry write CTRL_CANCEL and return ErrTimeout. (TCG PTP, "CRB_CTRL_START_x"/"CRB_CTRL_CANCEL_x".)
  6. Check CTRL_STS for the Error bit. (TCG PTP, "CRB_CTRL_STS_x".)
  7. Read the 10-byte response header to learn responseSize, validate it against the buffer, then read the whole response.
  8. Release the interface: write goIdle to CTRL_REQ. (TCG PTP, "CRB_CTRL_REQ_x" field "goIdle".)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址