Documentation
¶
Overview ¶
Package theca is an encrypted container format: authenticated sectors (XChaCha20 xor AES-256-CTR, HMAC-SHA-256), a Merkle tree, a write-ahead journal, no magic, and keyslots opened by a password, a recovery code and hardware factors. See docs/DESIGN.md.
This package is a library over a Backing (a file or block device); it does not mount anything.
Index ¶
- Constants
- Variables
- func DefaultJournal(sectors uint64) uint32
- func Device(c *Container) volume.Borrowed
- func KeyPairs(ctx context.Context, dev FIDO2, partners ...Factor) ([][]Factor, error)
- func ShortID(id []byte) string
- func WipeSets(sets [][]Factor)
- type Backing
- type Container
- func (c *Container) AddSlot(fs ...Factor) error
- func (c *Container) Close() error
- func (c *Container) EnrollFIDO2(ctx context.Context, dev FIDO2, partners ...Factor) error
- func (c *Container) Info() Info
- func (c *Container) ReadAt(p []byte, off int64) (int, error)
- func (c *Container) RemoveFactorSets(remove, keep [][]Factor) (int, error)
- func (c *Container) RemoveFactors(remove, keep []Factor) (int, error)
- func (c *Container) RemoveSlot(i int) error
- func (c *Container) Size() int64
- func (c *Container) Slots() []int
- func (c *Container) SlotsOf(fs ...Factor) ([]int, error)
- func (c *Container) Sync() error
- func (c *Container) Verify() (uint64, error)
- func (c *Container) WriteAt(p []byte, off int64) (int, error)
- type Counter
- type CreateOptions
- type FIDO2
- type Factor
- type FileBacking
- type IdentifiedCounter
- type Info
- type OpenOptions
- type Policy
- type ResizeInfo
- type ResizeOptions
- type Unlock
Constants ¶
const DefaultCacheBytes = 64 << 20
DefaultCacheBytes is the cache budget when none is given: 64 MiB, which holds 8192 metadata blocks (each covering 64 sectors: 2 GiB of volume) and 8192 tree nodes.
const DefaultJournalEntries = 64
DefaultJournalEntries is the smallest default journal, used when CreateOptions leaves JournalEntries zero for a small container.
const IDSize = format.IDSize
IDSize is the size of a TPM identity.
const MaxDefaultJournalEntries = 4096
MaxDefaultJournalEntries caps the default journal: 4096 entries are 17 MiB on disk and at most 16 MiB of pending sectors in memory.
const MaxFIDO2Credentials = 128
MaxFIDO2Credentials bounds how many Theca credentials an authenticator may report in one answer (a YubiKey 5.7 holds at most 100 discoverable credentials). A larger answer is refused, not processed.
const MaxFIDO2Salts = 2
MaxFIDO2Salts is how many salts one hmac-secret request carries (salt1 and salt2, CTAP 2.1 §12.5), hence how many partner factors one key pairs with.
const SectorSize = ta1.SectorSize
SectorSize is the logical sector size.
Variables ¶
var ( // ErrNoMatch is the single answer to "nothing unlocked this": a wrong // password, a wrong key, a corrupted slot area and a file that is not a // container are indistinguishable (DESIGN 13). ErrNoMatch = errors.New("theca: no factor opened a keyslot") // ErrCorrupt: the container unlocked but a structure failed to // authenticate (both header copies, a tree node, a metadata block). ErrCorrupt = errors.New("theca: container is corrupted or was tampered with") // ErrSector names a data sector whose tag failed: an I/O error, never // silently zero data. ErrSector = errors.New("theca: sector failed authentication") // ErrRollback: the header is older than the hardware counter allows. ErrRollback = errors.New("theca: container is older than the rollback counter (use AcceptRollback to rebind)") // ErrCounter: the counter given does not match the one the header names. ErrCounter = errors.New("theca: rollback counter missing or not the one recorded in the header") // ErrCounterRequired: a counter was asked for (OpenOptions.Counter, // CounterFor or CounterDerived) and the header names none: the container // was made without one, or an older copy from before `counter rebind` was // restored. Asking for a counter means requiring it. ErrCounterRequired = errors.New("theca: this container has no rollback counter, but one was asked: an older copy may have been restored") ErrRange = errors.New("theca: offset or length out of range") ErrBroken = errors.New("theca: container failed an earlier write and must be reopened") ErrClosed = errors.New("theca: container is closed") ErrNoSlot = errors.New("theca: no free keyslot") ErrOption = errors.New("theca: invalid option") // ErrNoCredential is returned by a FIDO2 provider whose key holds no // Theca credential. It is routine during unlock and not an error. ErrNoCredential = errors.New("theca: authenticator holds no theca credential") // ErrAuthenticator: a FIDO2 provider answered something malformed. ErrAuthenticator = errors.New("theca: malformed answer from the authenticator") )
Errors.
var ErrCounterIdentity = errors.New("theca: the rollback counter is not on the TPM this container is bound to")
ErrCounterIdentity: the counter is not on the TPM the header pins (DESIGN 5.4.1), or cannot say which TPM it is on.
var ErrLastWay = errors.New("theca: refusing to remove the last way in: the factors to keep would not open any other slot")
ErrLastWay is returned by RemoveFactors when no factor set the caller presented as the ones to keep would still open the container afterwards.
var ErrResizeSame = errors.New("theca: the container already has that journal size")
ErrResizeSame: the container already has the journal size asked for.
Functions ¶
func DefaultJournal ¶ added in v0.26.1
DefaultJournal is the journal size Create uses for a container of that many sectors when JournalEntries is zero: one entry per 16 sectors (6.25 % of the data, clamped to [DefaultJournalEntries, MaxDefaultJournalEntries]).
The journal bounds a transaction, and every transaction costs about four flushes whatever its size (go-theca/theca#73): a bigger journal is fewer flushes per byte written. A 64-entry journal measured 11.7 MiB/s for 64 MiB on macOS (F_FULLFSYNC), a 1024-entry one 94 MiB/s. The size is fixed at creation: an existing container keeps its own.
func Device ¶ added in v0.35.0
Device lends c as a volume.Borrowed: the borrower reads and writes the logical volume, Sync commits the container, and nothing it does can close c (a Borrowed has no Close: the lender owns the keys and the wiping). It is how a share serves the filesystem inside c (go-fileshare/localshare).
func KeyPairs ¶ added in v0.17.0
KeyPairs asks the key ONCE (one touch) for the pairs it forms with the partner factors: for every partner and every Theca credential the key holds, the factor set {partner, key}, which is what opens a key slot. They are what RemoveFactorSets and SlotsOf take to remove a key slot, or to prove, before a removal, that the key still opens the container.
The pairs are ordered by credential, then by partner. A key holding no Theca credential is ErrNoCredential: there is nothing to prove with. The caller wipes the pairs with WipeSets (they hold copies of the partners' keys).
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container is an open container. It is safe for concurrent use.
func Create ¶
func Create(f Backing, o CreateOptions) (c *Container, printed []byte, err error)
Create formats a new container in f. It returns the container and, for both policies, the printed recovery code (shown once, never stored). The code is bytes, not a string, so the caller can wipe it after showing it; it is nil when err is not.
func (*Container) Close ¶
Close commits, wipes the keys and refuses further use. The backing is not closed: it belongs to the caller.
func (*Container) EnrollFIDO2 ¶
EnrollFIDO2 enrols a hardware key paired with each of the given partner factors (one or two: the hmac-secret salts of one request). The key makes a new discoverable credential and answers one output per partner, for the salt derived from that partner (kdf.FIDO2Salt); each output becomes the pairwise slot {partner, key}. Nothing else is stored, and all the slots are recorded by ONE commit. Enrolment needs the master secret, so the container must already be open. The container and its free slots are checked before the key is asked, so a touch is never spent on an enrolment that cannot be stored (review of PR #4, finding 6).
func (*Container) RemoveFactorSets ¶ added in v0.17.0
RemoveFactorSets is RemoveFactors for a factor set that stands for SEVERAL slots: "the password with the key" is one pair per Theca credential the key holds (KeyPairs). A slot is removed when ANY set in `remove` opens it, and the container must still open under ANY set in `keep` through a slot that is not among those removed, else ErrLastWay. Nothing is written before both are known, and the writes are RemoveFactors's.
func (*Container) RemoveFactors ¶ added in v0.6.0
RemoveFactors removes the slot(s) the factor set `remove` opens ("the recovery code", "this key and this password"), and returns how many it removed. It does not need an index.
`keep` is a DIFFERENT factor set that must still open the container afterwards: a slot holds nothing that says what opens it, so the container cannot count the ways in that would remain, and asks the caller to prove one. The proof is checked before anything is written. With none (no slot opens under `remove`) the answer is ErrNoMatch; with a `keep` that opens no slot outside the ones to remove, ErrLastWay. A power cut keeps RemoveSlot's order (overwrite first, then commit).
Like RemoveSlot it only stops access to the CURRENT container: anyone with an older copy can still open it with the removed factor (DESIGN 2, R7).
func (*Container) RemoveSlot ¶
RemoveSlot overwrites slot i with random bytes, then records it as free. Any index 0..15 is accepted, recorded or not, so that a working slot the header does not record (left by a crash in an older version, or written by another tool) can still be destroyed. The overwrite comes first: a crash in between leaves a recorded slot holding random bytes, never a working slot marked free (review of PR #4, finding 3).
It only stops access to the CURRENT container: anyone holding an older copy can still open that copy with the revoked factor, because the master secret does not change (DESIGN 2, review finding R7). Callers must tell the user.
func (*Container) Slots ¶ added in v0.6.0
Slots returns the indexes of the slots the header records, in order. A slot holds nothing that says what opens it, so this is all the container knows; SlotsOf answers "which slots does this factor set open".
func (*Container) SlotsOf ¶ added in v0.6.0
SlotsOf returns the indexes of the slots (recorded or not) that the factor set opens: one factor, or two that pair. It trial-opens all 16, as unlock does.
type Counter ¶
Counter is a hardware monotonic counter (DESIGN 5.4): in practice a TPM NV counter. Handle and Name identify the index; the container pins both.
type CreateOptions ¶
type CreateOptions struct {
Sectors uint64
JournalEntries uint32 // 0: DefaultJournal(Sectors) (raised to the minimum)
Hint int // Argon2id profile (D2)
Password []byte
Policy Policy
Counter Counter // optional hardware counter
// NewCounter, when Counter is nil, makes the counter AFTER the master secret
// exists, and is given the authValue derived from it (DESIGN 5.4.2): the
// container records that the counter's authValue is derived, so a mount asks
// OpenOptions.CounterDerived, not CounterFor. The slice is wiped when
// NewCounter returns: copy what is kept.
NewCounter func(auth []byte) (Counter, error)
Rand io.Reader // nil: crypto/rand
CacheBytes int // see OpenOptions.CacheBytes
}
CreateOptions configures Create.
type FIDO2 ¶
type FIDO2 interface {
// Enroll creates a new discoverable credential (rk, credProtect 3) and
// returns its outputs for each salt, in order (one or two salts).
Enroll(ctx context.Context, salts [][32]byte) ([][]byte, error)
// Derive asks for the outputs of EVERY Theca credential on the key, for
// each salt (getAssertion with an empty allowList, then
// getNextAssertion): result[i][j] is credential i's output for salts[j].
// It returns ErrNoCredential if the key holds none.
Derive(ctx context.Context, salts [][32]byte) ([][][]byte, error)
}
FIDO2 is a hmac-secret authenticator holding DISCOVERABLE credentials for the rp id "theca" (decision H2, revised: DESIGN 19, I21). Nothing about the key is stored in the container: the salts are derived from the partner factors (kdf.FIDO2Salt), and the key is asked about all of its Theca credentials at once. Each call is one touch and one user verification. Every output is the raw 32-byte hmac-secret value.
type Factor ¶
Factor is one access factor reduced to a key-encryption key.
func FIDO2Factor ¶
FIDO2Factor turns an hmac-secret output into a factor.
func PasswordFactor ¶
PasswordFactor runs Argon2id (one evaluation, D2) with the hinted profile.
func RecoveryFactor ¶
RecoveryFactor parses a printed recovery code. The bytes are the caller's to wipe.
type FileBacking ¶
FileBacking adapts an *os.File.
type IdentifiedCounter ¶ added in v0.18.0
IdentifiedCounter is a Counter that can name the TPM it lives on: a digest of that TPM's endorsement key Name, IDSize bytes. A container made with one records the identity in its authenticated header and refuses, at every mount, a counter that reports another.
The identity names a TPM, it does not prove the TPM answering is that one: whoever controls a software TPM, or the bus to a TPM, can report any identity (DESIGN 5.4.1).
type Info ¶
type Info struct {
Sectors uint64
Size uint64
Epoch uint64
SlotsUsed int
RollbackProtected bool
CounterHandle uint32 // the counter's handle; 0 when RollbackProtected is false
CounterTPM []byte // identity of the TPM the counter is pinned to; nil when RollbackProtected is false or the container predates the pin
CounterDerivedAuth bool // the counter's authValue is derived from the master secret (DESIGN 5.4.2); false when RollbackProtected is false
JournalEntries uint32
ReplayedTransaction bool
}
Info describes an open container.
type OpenOptions ¶
type OpenOptions struct {
Counter Counter
// CounterFor, when Counter is nil, is asked for the counter once the header
// has been authenticated and names one (its handle and NV Name are only
// known then: the header has no plaintext fields). An error it returns
// refuses the open.
CounterFor func(handle uint32, name []byte) (Counter, error)
// CounterDerived is CounterFor for a counter whose authValue is derived from
// the master secret (the header says so): it is given that authValue, which
// only someone who unlocked the container has. The slice is wiped when it
// returns. A container with such a counter and no CounterDerived is refused
// with ErrCounter.
CounterDerived func(handle uint32, name, auth []byte) (Counter, error)
// RebindDerived is RebindFor for a NEW counter with a derived authValue
// (the migration from a stored one). RebindFor wins if both are set.
RebindDerived func(handle uint32, name, auth []byte) (Counter, error)
// RebindFor is the recovery path for a counter that is gone (the index was
// undefined, the TPM was cleared) or that the container should move away
// from. It is asked, once the header has authenticated, for a NEW counter:
// the handle and Name the header records (0 and nil if it records none) are
// passed so a counter can be made at the same handle. The mount protocol
// does not run against the old counter: this mount increments the new one,
// reads its value and binds the header to it. Rollback protection restarts
// from that moment: nothing is checked against the old counter. Needs the
// unlocking factors, like any open; the decision to use it is the caller's.
RebindFor func(handle uint32, name []byte) (Counter, error)
AcceptRollback bool
Rand io.Reader
// CacheBytes bounds the memory the container keeps for verified, clean
// metadata blocks and tree nodes (half each, 4 KiB per entry): 0 is
// DefaultCacheBytes. Entries that are part of a transaction not yet
// committed are not counted: the journal budget bounds those. An evicted
// entry is wiped, and read and authenticated again when it is needed.
CacheBytes int
}
OpenOptions configures Open.
type ResizeInfo ¶ added in v0.27.0
type ResizeInfo struct {
OldEntries uint32
Entries uint32
Sectors uint64
// Epoch is the source's epoch after the unlock, which the copy carries.
Epoch uint64
// Size is the size of the new file in bytes.
Size uint64
// Hash is the SHA-256 of the whole volume, read through the source and,
// again, through the reopened copy: Resize reports success only when both
// agree.
Hash [32]byte
}
ResizeInfo describes a finished resize.
func Resize ¶ added in v0.27.0
func Resize(ctx context.Context, src Backing, u Unlock, o OpenOptions, dst Backing, ro ResizeOptions) (ResizeInfo, error)
Resize writes a copy of the container in src, with a journal of a different size, to the empty backing dst. The journal size is part of the header and the layout is derived from it, so every area after the journal moves; the sectors, the tree and the key slots are copied byte for byte (a sector is sealed under the container id and its INDEX, never its offset), and the header is sealed again under the same master secret.
src is unlocked like any mount: the factors open it, the counter is consulted and advanced, a committed transaction in its journal is replayed. That is the only thing written to it, and the copy is never written to src. dst is synced before the verification. The verification reopens dst with the counter's increments swallowed, checks the journal size, authenticates every sector, and compares the SHA-256 of the volume read through each.
The copy carries the counter binding unchanged (handle, name, TPM identity, derived authValue, bound value, epoch). The first real mount of dst finishes the increment the verification left unfinished and advances the counter, and from then on src is refused as a rollback, like any older copy of the container.
type ResizeOptions ¶ added in v0.27.0
type ResizeOptions struct {
// Entries is the new journal size in entries (the most one commit holds).
// 0 means DefaultJournal(sectors). A value below what one sector write needs
// is raised to that minimum, as in Create.
Entries uint32
}
ResizeOptions chooses the new journal size.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
bench
module
|
|
|
cmd
|
|
|
theca
command
Command theca creates, inspects and copies data in and out of Theca containers (docs/DESIGN.md).
|
Command theca creates, inspects and copies data in and out of Theca containers (docs/DESIGN.md). |
|
Package fido2 is Theca's FIDO2 factor on a real authenticator: it implements theca.FIDO2 over go-authn/fido (CTAP 2.1) and, on macOS, the go-macos/fido transport.
|
Package fido2 is Theca's FIDO2 factor on a real authenticator: it implements theca.FIDO2 over go-authn/fido (CTAP 2.1) and, on macOS, the go-macos/fido transport. |
|
internal
|
|
|
format
Package format holds the on-disk layout of a Theca container: the geometry derived from the authenticated header, the header body, the keyslots and the recovery code (DESIGN 9).
|
Package format holds the on-disk layout of a Theca container: the geometry derived from the authenticated header, the header body, the keyslots and the recovery code (DESIGN 9). |
|
journal
Package journal is Theca's write-ahead journal (DESIGN 5.3, review finding R1).
|
Package journal is Theca's write-ahead journal (DESIGN 5.3, review finding R1). |
|
kdf
Package kdf holds Theca's key derivation: Argon2id with parameters from a fixed compiled-in table (DESIGN 6.1, decision D2) and HKDF-SHA-256 at the wrapping layer (DESIGN 2).
|
Package kdf holds Theca's key derivation: Argon2id with parameters from a fixed compiled-in table (DESIGN 6.1, decision D2) and HKDF-SHA-256 at the wrapping layer (DESIGN 2). |
|
lru
Package lru is a bounded cache keyed by uint64 for the verified clean entries of an open container (go-theca/theca#6).
|
Package lru is a bounded cache keyed by uint64 for the verified clean entries of an open container (go-theca/theca#6). |
|
merkle
Package merkle implements Theca's integrity tree (DESIGN 5.2).
|
Package merkle implements Theca's integrity tree (DESIGN 5.2). |
|
pin
Package pin is theca's policy over go-authn/harden/memlock: what a refusal to pin the pages that hold key material means (go-theca/theca#68).
|
Package pin is theca's policy over go-authn/harden/memlock: what a refusal to pin the pages that hold key material means (go-theca/theca#68). |
|
ta1
Package ta1 implements the Theca-A sector construction and the sealed object format of suite TA1 (docs/DESIGN.md sections 2, 4.3 and 9.3).
|
Package ta1 implements the Theca-A sector construction and the sealed object format of suite TA1 (docs/DESIGN.md sections 2, 4.3 and 9.3). |
|
tools/covgaps
command
Command covgaps reads a Go cover profile and prints every uncovered block.
|
Command covgaps reads a Go cover profile and prints every uncovered block. |
|
Package profile is the file of named containers: containers.toml, in the per-user configuration directory, mode 0600.
|
Package profile is the file of named containers: containers.toml, in the per-user configuration directory, mode 0600. |
|
Package session is the conversation between `theca close` / `theca status` (or a GUI) and the background process that holds an unlocked container.
|
Package session is the conversation between `theca close` / `theca status` (or a GUI) and the background process that holds an unlocked container. |
|
Package share serves the exFAT filesystem inside an unlocked container over SMB on a loopback address, and optionally mounts it in the file manager.
|
Package share serves the exFAT filesystem inside an unlocked container over SMB on a loopback address, and optionally mounts it in the file manager. |