nvcounter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: BSD-3-Clause Imports: 8 Imported by: 1 module, 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/nvcounter

CI Go Reference License

A monotonic counter on a TPM 2.0: a TPM_NT_COUNTER NV index (TCG TPM 2.0 Library Part 3, 31.8) that is created once, incremented under its own authValue and never goes back. A rollback guard for anything that must refuse an older copy of itself: theca binds a container to one.

import "github.com/go-tpm2/nvcounter"

c, err := nvcounter.Create(nvcounter.Options{Auth: secret}) // defines a free owner index
err = c.Increment()
v, err := c.Read()
id, err := c.Identity() // names THIS TPM: the digest of its ECC P-256 EK

c, err = nvcounter.Open(nvcounter.Options{Handle: c.Handle(), Auth: secret})
  • A Counter holds no connection: each operation opens the device and closes it.
  • Transports: /dev/tpmrm0 (Linux, the default), Windows TBS (tbs:, the Windows default, through devtpm), and unix:<socket> for a software TPM (swtpm socket --tpm2 --server type=unixio,path=SOCKET). macOS has no TPM: ErrUnsupported unless a device is named.
  • New counters carry TPMA_NV_NO_DA; a wrong authValue does not lock the TPM.
  • The Name of a counter changes once, at its first increment (it covers TPMA_NV_WRITTEN): Increment reads it again. An index nobody incremented cannot be read.
  • Identity names the TPM, it does not prove the TPM answering is that one: whoever controls a software TPM can present any EK. It needs the endorsement hierarchy password in Options.EndorsementAuth if one is set; a refusal is ErrEndorsementAuth and does not count toward the lockout.
  • Every answer from the TPM that becomes a value is bounded (authValues at most 32 bytes, the EK Name exactly 34 bytes starting 000b).

Built on tpm2 (NVDefineCounter, NVIncrementAuth, NVReadCounter, EKIdentityAuth). This package is where the transport lives, so tpm2 itself stays free of devtpm and x/sys. Salted HMAC sessions for NV authorization are tracked in tpm2#20; today the authValue travels in a password session, as for every other tpm2 command.

Tested against a real software TPM in CI (NVCOUNTER_REQUIRE_SWTPM=1 turns a missing swtpm into a failure) and against a fake TPM for every error path, with 100% statement coverage. This code was moved here from theca's tpmcounter package, unchanged apart from the name.

Performance

No operation does work of its own: each opens the device, sends a fixed number of TPM commands and closes it, so the time is the TPM's, and there is no C reference tool to compare with. Create: NV_DefineSpace plus one NV_ReadPublic probe per taken handle. Open: one NV_ReadPublic. Read: one NV_Read. Increment: one NV_Increment (an NV write) and one NV_ReadPublic. Destroy: one NV_UndefineSpace. Identity: one TPM2_CreatePrimary of the ECC P-256 EK and one TPM2_FlushContext. Handle and Name return stored values. Measured on swtpm under a 1-minute load of 13 (so the times are indicative, the command counts are exact): Identity 0.26 ms, Read 0.04 ms, Increment 0.33 ms. A hardware TPM was not measured.

Install

go get github.com/go-tpm2/nvcounter

License

BSD-3-Clause. See LICENSE.

Documentation

Overview

Package nvcounter is a rollback counter on a TPM 2.0: a TPM_NT_COUNTER NV index (TCG TPM 2.0 Library Part 3, 31.8), created once, incremented by its own authValue and never reset, over go-tpm2/tpm2. It is the counter theca binds a container to; nothing in it is specific to theca.

Counter.Identity names the TPM the counter lives on (the digest inside the Name of its ECC P-256 endorsement key), so a caller can pin a counter to one TPM and refuse the same handle and Name on another.

Transports: the Linux kernel resource manager (/dev/tpmrm0), Windows TPM Base Services ("tbs:", the Windows default, go-tpm2/devtpm OpenTBS), and any raw TPM 2.0 stream the caller names as "unix:<socket>" (a software TPM started with swtpm --server type=unixio). macOS has no TPM and returns ErrUnsupported unless a device is named explicitly; "tbs:" off Windows is an error too.

A Counter holds no open connection. Each operation opens the device and closes it, so no lifetime has to be managed and the kernel resource manager releases everything between operations.

Index

Constants

View Source
const (
	First uint32 = 0x01500000
	Span  uint32 = 0x1000
)

First and Span give the owner NV handles searched for a free one: the TCG registry assigns 0x01500000-0x01BFFFFF to the owner.

View Source
const DefaultLinuxDevice = devtpm.DefaultDevice

DefaultLinuxDevice is the kernel resource manager.

View Source
const IDSize = 32

IDSize is the size of an identity: a SHA-256 digest.

View Source
const TBSDevice = "tbs:"

TBSDevice names Windows TPM Base Services, the Windows default: one TBS context per operation, as for the other devices.

Variables

View Source
var (
	// ErrUnsupported: this platform has no TPM interface and no device was named.
	ErrUnsupported = errors.New("nvcounter: no TPM interface on this platform (name a device)")
	// ErrNotCounter: the NV index exists but is not a counter this package made.
	ErrNotCounter = errors.New("nvcounter: the NV index is not a rollback counter created by this package")
	// ErrNoCounter: the NV index is not defined on this TPM (it was undefined,
	// the TPM was cleared, or this is another TPM).
	ErrNoCounter = errors.New("nvcounter: the TPM has no NV index at the counter's handle")
	// ErrAuthTooLong: an authValue longer than a SHA-256 digest (Part 3 31.3.1).
	ErrAuthTooLong = errors.New("nvcounter: an authValue is at most 32 bytes")
	// ErrIdentity: the TPM could not name itself (no EK, a set endorsement
	// authValue) or answered with something that is not an EK Name.
	ErrIdentity = errors.New("nvcounter: cannot read this TPM's identity")
	// ErrNoFreeHandle: every handle in the search range is taken.
	ErrNoFreeHandle = errors.New("nvcounter: no free NV index handle in the owner range searched")
)

Errors.

View Source
var ErrEndorsementAuth = errors.New("nvcounter: the TPM refused the endorsement hierarchy password")

ErrEndorsementAuth: the TPM refused the endorsement hierarchy password (TPM_RC_BAD_AUTH or TPM_RC_AUTH_FAIL). The authValue of a hierarchy is not DA protected, so the refusal does not count toward the lockout.

Functions

This section is empty.

Types

type Counter

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

Counter is a TPM NV counter. It satisfies theca.Counter and theca.IdentifiedCounter.

func Create

func Create(o Options) (*Counter, error)

Create defines a new counter index and returns it. The index is not yet written: the first Increment writes it. If a later step fails, call Destroy.

func Open

func Open(o Options) (*Counter, error)

Open attaches to the existing counter o.Handle and reads its Name from the TPM. It refuses an index that is not a counter with the attributes Create sets. (The container pins the Name too: a different index fails there.)

func (*Counter) Destroy

func (c *Counter) Destroy() error

Destroy deletes the index under the owner password in the Options it was created with. Use it only to undo a Create.

func (*Counter) Handle

func (c *Counter) Handle() uint32

Handle is the NV index.

func (*Counter) Identity

func (c *Counter) Identity() ([]byte, error)

Identity is the identity of THIS TPM, the SHA-256 digest inside the Name of its endorsement key (the EK Credential Profile ECC P-256 template, created transiently and flushed, never made persistent). The same TPM gives the same identity on every call, another TPM another one. Needs the endorsement hierarchy's authValue to be empty, the default, or EndorsementAuth to be the password; otherwise it fails (with ErrEndorsementAuth if the TPM refused the password).

It names the TPM, it does not prove the TPM answering is that one: whoever controls a software TPM can present any EK.

func (*Counter) Increment

func (c *Counter) Increment() error

Increment adds one to the counter. It then reads the Name again: the first increment sets TPMA_NV_WRITTEN, which is part of the public area the Name is the digest of, so the Name of a counter that was just created changes once. (Measured on swtpm: a Name read before the first increment does not match the one read after it.)

func (*Counter) Name

func (c *Counter) Name() []byte

Name is the NV Name the TPM computed.

func (*Counter) Read

func (c *Counter) Read() (uint64, error)

Read returns the counter's value.

type Options

type Options struct {
	// Device is a character device path, "unix:<socket>", or empty for the
	// platform default (/dev/tpmrm0 on Linux).
	Device string
	// Handle is the NV index. Create picks a free one when it is 0; Open
	// requires it.
	Handle uint32
	// OwnerAuth authorizes Create and Destroy (the owner hierarchy password;
	// empty on a TPM with no owner password). Read from a file by the caller,
	// never from argv or the environment.
	OwnerAuth []byte
	// Auth is the counter's own authValue, used to increment and read it.
	Auth []byte
	// EndorsementAuth authorizes the creation of the endorsement key that names
	// the TPM (Identity): the endorsement hierarchy password, empty on a TPM
	// with none. A wrong one is ErrEndorsementAuth.
	EndorsementAuth []byte
}

Options select the TPM and the credentials.

Jump to

Keyboard shortcuts

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