Skip to content

stack-encrypt: a stored value cannot be tied to its row without changing its key — add labels to EQL payloads #1567

Open

Description

@coderdan

Background

Stack Encrypt (packages/stack-encrypt) is the Rust engine that every CipherStash language SDK runs on. Each ciphertext has a key tag. ZeroKMS, our key service, needs the tag to give back the value's data key.

A label is a fact about how a value was written, such as the row it belongs to or who wrote it. Adding a label replaces the tag with HMAC(tag, PAE(name, value)), and records the label in the payload. PAE is a fixed way to encode several fields into one byte string without ambiguity. Labelling needs no ZeroKMS call. To read the value back, ZeroKMS checks the labels against the tag.

The key policy document, merged in pull request #1365, has the design.

Problem

Access intelligence needs each value tied to its row and to the facts of its writing. Today the only way to tie a value to its row is a binding in its context. A binding changes the data key and the AAD (additional authenticated data). It also has to be known to every reader.

A label records the same facts without changing the context, the AAD, or any query. It does not restrict any reader. Often the row ID exists only after the insert, such as a generated primary key. So a database trigger must be able to add the label.

Proposal

The Stack Encrypt API plan lists the work:

  1. Add label(name, value) to every EQL type and to a JSON document. It computes the new tag, stores it as the tag, and appends the label to the payload's list.
  2. Add eql_v4.label_tag(payload, name, value), the same operation in SQL, for a trigger that labels with the generated primary key. The function reads and writes only the key header h (stack-encrypt refactor: add typed encoded ciphertext targets and tag access #1485).
  3. Add label and labels to the dynamic interface, so the Go SDK can label a value before it writes the row, and read the labels.
  4. Add labels() on a stored value, to read the list without decrypting.
  5. Add an optional labels list to every EQL payload type. A payload without labels stays in the rc.3 form.
  6. On decrypt, send the stored tag and the labels to ZeroKMS. Rebuild the AAD from the original tag that ZeroKMS returns.

EQL defines the standard label names, and an application can add its own. A bare Ciphertext never gets labels.

Dependency outside this repository

The read path needs a ZeroKMS change. The retrieve-key request must include the labels. ZeroKMS must return the original tag in its response. Labels written by ZeroKMS itself, and policy based on labels, are deferred to ZeroKMS version 2. The key policy document's Still to explore section records the thinking.

Acceptance

  • The example in the key policy document round-trips through a trigger, in Rust and in Go.
  • ZeroKMS refuses a changed label.
  • ZeroKMS refuses a changed tag.
  • The base tag is gone from a labelled row. Nobody can read the unlabelled tag back.
  • The conformance profile for EQL covers the labels list.
  • A payload with no labels has the same bytes as before.

Relationship to other work

Plans: the Stack Encrypt API plan and the key policy document.

Activity

  1. self-assigned this
    on Nov 8, 2026
  2. added
    enhancement
    Rust
    on Nov 8, 2026
  3. coderdan commented on Nov 22, 2026

    @coderdan
    ContributorAuthor

    The ZeroKMS dependency is confirmed and now tracked internally as CIP-5509.

    Today ZeroKMS recomputes a value's tag on key retrieval and compares it byte for byte with the stored tag. A labelled tag never matches, and the retrieve request has no field that could carry labels. So the read path does need a ZeroKMS change, as the "Dependency outside this repository" section says.

    ZeroKMS context items don't cover this case. A context item has to be known when the key is generated. A label refines a key that already exists, for example in a trigger that learns the row ID after the insert.

    The proposed direction is a macaroon whose root key is the tag from generate-data-key. Each label is a caveat, so a label can be added without a ZeroKMS call, including from a PL/pgSQL function. The retrieve request sends the whole encoded macaroon, and ZeroKMS replays the chain from the base tag.

    One open question affects this issue directly. If ZeroKMS returns the base tag so the client can rebuild the AAD, a reader can store the bare tag again, which conflicts with "Nobody can read the unlabelled tag back". The AAD may need to bind a one-way value derived from the base tag instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

RustSDKenhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions