diff --git a/.changeset/eql-bindings-optional-storage-only.md b/.changeset/eql-bindings-optional-storage-only.md index 17b2f3f17e..77555112ef 100644 --- a/.changeset/eql-bindings-optional-storage-only.md +++ b/.changeset/eql-bindings-optional-storage-only.md @@ -2,4 +2,4 @@ "@cipherstash/eql": patch --- -`eql-bindings` (the Rust crate released with this package): an EQL v4 type whose terms a null has, a storage-only type (`Date`, `Integer`, `Text`, ...) or one whose only term is equality's (`DateEq`, `IntegerEq`, `TextEq`, ...), now also implements `EncryptFrom` and `DecryptInto` for an `Option` of its plaintext, and its query twin `EncryptFrom` for one. For a type of fixed width a `None` seals as a typed null as long as a value (vitaminc 0.8), so an optional column does not show which rows are absent. For text it seals as a null that is not padded, since text's ciphertext is as long as the text, so its length may show that the value is absent. Either way it opens as `None`; its `hm` is the one term every null shares, and the query of a `None` finds the stored nulls with `=`. A value seals exactly as before. The by-name dispatch (`encryption::targets`) reads, opens and queries these types through the `Option`; a type with an order or match term still refuses a null. eql-bindings' own `Date`, `Timestamp` and `Decimal` plaintexts declare their fixed width to vitaminc, so their `None` is padded too. The SQL bundle and the TypeScript types do not change. +`eql-bindings` (the Rust crate released with this package): an EQL v4 type whose terms a null has, a storage-only type (`Date`, `Integer`, `Text`, ...) or one whose only term is equality's (`DateEq`, `IntegerEq`, `TextEq`, ...), now also implements `EncryptTarget` and `DecryptSource` for an `Option` of its plaintext, and its query twin `EncryptTarget` for one. For a type of fixed width a `None` seals as a typed null as long as a value (vitaminc 0.8), so an optional column does not show which rows are absent. For text it seals as a null that is not padded, since text's ciphertext is as long as the text, so its length may show that the value is absent. Either way it opens as `None`; its `hm` is the one term every null shares, and the query of a `None` finds the stored nulls with `=`. A value seals exactly as before. The by-name dispatch (`encryption::targets`) reads, opens and queries these types through the `Option`; a type with an order or match term still refuses a null. eql-bindings' own `Date`, `Timestamp` and `Decimal` plaintexts declare their fixed width to vitaminc, so their `None` is padded too. The SQL bundle and the TypeScript types do not change. diff --git a/.changeset/eql-json-tree-domain.md b/.changeset/eql-json-tree-domain.md index 16e5a2ca12..5089199504 100644 --- a/.changeset/eql-json-tree-domain.md +++ b/.changeset/eql-json-tree-domain.md @@ -2,6 +2,6 @@ "@cipherstash/eql": minor --- -**EQL v4 gains the `json_tree` domain family: `public.eql_v4_json_search`, an encrypted JSON document stored as one entry per node, with `@>` and `<@` that agree with PostgreSQL's own `jsonb @>` across array elements.** The family is new beside `public.eql_v4_json` and is not a port of the v3 SteVec surface: it is what Stack Encrypt's JSON index writes. A document's entries each carry a path selector `s` and a value selector `vs` (containment is a set comparison over the pair), the node's own ciphertext `c` (its label, kind and scalar value; a document's ciphertext is linear in its size, where a SteVec document repeats every subtree at every level), and a token chain (`p`, `w`, `o`) that lets core PostgreSQL walk the subtree under a node from one client-derived token: `eql_v4.json_tree_subtree(doc, token)`, `json_tree_subtree_at(doc, token, selector)`, `json_tree_children` and `json_tree_child_count`. By default the root is sealed, so no query can hand the server a token for the whole column. Containment projects a stored document into an element-nested shape from its group tags (`n`, `g`), `eql_v4.json_tree_query(doc)`, and compares it with the needle (`eql_v4.query_json`) by native `jsonb @>`, so each needle array element must fit inside one stored element, which closes the cross-element false positives of selector-set containment (#1231) for this family; a GIN index on `(eql_v4.json_tree_query(col)::jsonb) jsonb_path_ops` serves it. `->` by selector returns an entry that opens on its own (`public.eql_v4_json_entry`, with the header and ordinal grafted), and `eql_v4.query_json_leaf` orders an extracted number or string leaf with `<`, `<=`, `>`, `>=`. Every other native jsonb operator on the column is blocked, as on the other EQL domains. EQL v4 only (`-- ONLY EQL VERSION: 4`); the v3 bundle's SQL is unchanged. The `eql-bindings` crate carries the hand-written `v4::json_tree` types with Stack Encrypt's `EncryptFrom` / `DecryptInto` for the document and the entry. +**EQL v4 gains the `json_tree` domain family: `public.eql_v4_json_search`, an encrypted JSON document stored as one entry per node, with `@>` and `<@` that agree with PostgreSQL's own `jsonb @>` across array elements.** The family is new beside `public.eql_v4_json` and is not a port of the v3 SteVec surface: it is what Stack Encrypt's JSON index writes. A document's entries each carry a path selector `s` and a value selector `vs` (containment is a set comparison over the pair), the node's own ciphertext `c` (its label, kind and scalar value; a document's ciphertext is linear in its size, where a SteVec document repeats every subtree at every level), and a token chain (`p`, `w`, `o`) that lets core PostgreSQL walk the subtree under a node from one client-derived token: `eql_v4.json_tree_subtree(doc, token)`, `json_tree_subtree_at(doc, token, selector)`, `json_tree_children` and `json_tree_child_count`. By default the root is sealed, so no query can hand the server a token for the whole column. Containment projects a stored document into an element-nested shape from its group tags (`n`, `g`), `eql_v4.json_tree_query(doc)`, and compares it with the needle (`eql_v4.query_json`) by native `jsonb @>`, so each needle array element must fit inside one stored element, which closes the cross-element false positives of selector-set containment (#1231) for this family; a GIN index on `(eql_v4.json_tree_query(col)::jsonb) jsonb_path_ops` serves it. `->` by selector returns an entry that opens on its own (`public.eql_v4_json_entry`, with the header and ordinal grafted), and `eql_v4.query_json_leaf` orders an extracted number or string leaf with `<`, `<=`, `>`, `>=`. Every other native jsonb operator on the column is blocked, as on the other EQL domains. EQL v4 only (`-- ONLY EQL VERSION: 4`); the v3 bundle's SQL is unchanged. The `eql-bindings` crate carries the hand-written `v4::json_tree` types with Stack Encrypt's `EncryptTarget` for the document and `DecryptSource` for the document and the entry. Why. Stack Encrypt had no JSON index: the operators (`Contains`, `ContainedBy`, `AtPath`) were declared and answered by nothing, and the v4 JSON wire format was unconstrained by anything shipped. This is Phase 1 and 2 of the `json_tree` index's phased plan (#1249), implemented against `docs/plans/2026-10-17-json-tree-index.md`, whose "Implementation notes" section records where the code deviates from the design. diff --git a/.changeset/eql-json-tree-walk-and-projection.md b/.changeset/eql-json-tree-walk-and-projection.md index eb03d448ff..e9dcc6e2be 100644 --- a/.changeset/eql-json-tree-walk-and-projection.md +++ b/.changeset/eql-json-tree-walk-and-projection.md @@ -12,4 +12,4 @@ Also: `eql_v4.json_tree_path_query_first`, the function `->` and the ordering co The `eql-bindings` crate's `JsonTreeDocument` gains the `q` field and `JsonTreeSubtree` becomes a struct of the rows and the key that keyed the walk; `stack-encrypt`'s `JsonDocument` writes `q` on the wire and ignores it on the way in, `JsonPath::select` evaluates a path over a value, and `JsonSubtreeKey::path` and `open`, `JsonSubtree::path` and `is_sealed_root` carry the fallback. EQL v4 only; the v3 bundle is unchanged. -**The leaf ordering term `op` is opt-in per path.** A stored leaf carried the order-preserving term on every number and string, about 80 bytes each and the largest field of a nested document, whether or not anything ordered by it. Now the JSON index's options name the paths that order (`{"json": {"ordered": ["$.age", "$.orders[*].total"]}}` on the data plan; `JsonOptions::ordering([...])` in Rust), a wildcard covering every element of an array, and no path is ordered by default. A position in an ordered path (`$.scores[2]`), or in the path an ordering asks at, is refused: the comparison extracts by selector, which reads a position as the wildcard and reaches the first element, never the one named. An ordering query at a path the index does not order is refused before any key loads rather than silently matching no row. The SQL is unchanged: `eql_v4.ord_term` was already NULL for a leaf without a term. `eql-bindings` gains `v4::JsonTree`, the `json_tree` layout under options, and `stack-encrypt` gains `target::Layout` and `Plan::value().encrypt_with_layout(layout)`, a plan laid out by a value whose queries run under its options. +**The leaf ordering term `op` is opt-in per path.** A stored leaf carried the order-preserving term on every number and string, about 80 bytes each and the largest field of a nested document, whether or not anything ordered by it. Now the JSON index's options name the paths that order (`{"json": {"ordered": ["$.age", "$.orders[*].total"]}}` on the data plan; `JsonOptions::ordering([...])` in Rust), a wildcard covering every element of an array, and no path is ordered by default. A position in an ordered path (`$.scores[2]`), or in the path an ordering asks at, is refused: the comparison extracts by selector, which reads a position as the wildcard and reaches the first element, never the one named. An ordering query at a path the index does not order is refused before any key loads rather than silently matching no row. The SQL is unchanged: `eql_v4.ord_term` was already NULL for a leaf without a term. `eql-bindings` gains `v4::JsonTree`, which describes the `json_tree` encryption under options: `JsonTree::new(options).encryption()`. diff --git a/.changeset/eql-plan-field-targets.md b/.changeset/eql-plan-field-targets.md index 18b37b5909..6615a79bdd 100644 --- a/.changeset/eql-plan-field-targets.md +++ b/.changeset/eql-plan-field-targets.md @@ -2,4 +2,4 @@ '@cipherstash/eql': minor --- -**The `eql-bindings` crate resolves an EQL type named as a string to its own Stack Encrypt plan** (`targets` feature, which turns on `stack-encrypt`). `eql_bindings::encryption::targets` carries a catalog-generated table of every EQL type a data plan may name as a field target — its name across languages, family and suffix, the plaintext `ValueKind` it takes, the indexes it carries, its query twin, and whether the engine can produce it today with the reason when not — and `encrypt` / `decrypt` / `query` entry points that dispatch on the name and run the type's derived `EncryptFrom` / `DecryptInto`, resolving to the EQL value's JSON bytes through the engine's `Pending` so a guest batches it with the rest of a plan. This is the EQL half of EQL types as plan field targets (cipherstash/stack#1062): a Go data plan names `TextEq` and the guest returns the finished EQL value instead of assembling one. Every target is an `eql_v4_*` domain. `TextEq` is the only producible type; every other name is refused with the plan's reason. The SQL surface and the TypeScript package are unchanged. +**The `eql-bindings` crate resolves an EQL type named as a string to its own Stack Encrypt plan** (`targets` feature, which turns on `stack-encrypt`). `eql_bindings::encryption::targets` carries a catalog-generated table of every EQL type a data plan may name as a field target — its name across languages, family and suffix, the plaintext `ValueKind` it takes, the indexes it carries, its query twin, and whether the engine can produce it today with the reason when not — and `encrypt` / `decrypt` / `query` entry points that dispatch on the name and run the type's derived `EncryptTarget` / `DecryptSource`, resolving to the EQL value's JSON bytes through the engine's `Pending` so a guest batches it with the rest of a plan. This is the EQL half of EQL types as plan field targets (cipherstash/stack#1062): a Go data plan names `TextEq` and the guest returns the finished EQL value instead of assembling one. Every target is an `eql_v4_*` domain. `TextEq` is the only producible type; every other name is refused with the plan's reason. The SQL surface and the TypeScript package are unchanged. diff --git a/.changeset/eql-remove-structural-traits.md b/.changeset/eql-remove-structural-traits.md new file mode 100644 index 0000000000..a2fef48279 --- /dev/null +++ b/.changeset/eql-remove-structural-traits.md @@ -0,0 +1,12 @@ +--- +'@cipherstash/eql': minor +--- + +`eql-bindings`, the Rust crate released with this package: the EQL v4 types no longer implement Stack Encrypt's older traits `EncryptFrom` and `DecryptInto`. Stack Encrypt removed them, with `encrypt_as`, `decrypt_as`, the plan builder and the query chain. + +- Every type keeps `EncryptTarget` and `DecryptSource`. Write with `value.encrypt_into_with_context(&keyset, column)` and read with `P::decrypt_from_with_context(stored, &keyset, column)`. +- A typed query is a field marker of a derived record, such as `EncryptedMember::EMAIL.eq("bob").encrypt(&keyset)`. +- A JSON document is a `JsonPlaintext`, not a `serde_json::Value`. A typed JSON query has no replacement yet: derive the operand through a data plan that names `JsonSearch`. +- `JsonTree` no longer implements `stack_encrypt::target::Layout`. `encryption()`, `index()` and `specs()` are its own methods. + +The stored format, the SQL bundle, and the TypeScript types do not change. diff --git a/.changeset/eql-v4-bindings.md b/.changeset/eql-v4-bindings.md index e92cee2ee9..ace868e334 100644 --- a/.changeset/eql-v4-bindings.md +++ b/.changeset/eql-v4-bindings.md @@ -2,7 +2,7 @@ '@cipherstash/eql': minor --- -**The `eql-bindings` crate has an `eql_bindings::v4` module, with the `stack-encrypt` feature: payload types for the EQL v4 domains.** EQL v4 is the v3 SQL installed under a second name (ADR-0002), for values written by Stack Encrypt, so the module exists only with that feature and its types derive Stack Encrypt's traits without a further gate. The module is generated from the same catalog as `v3`, with `eql_v4_*` domain names and `"v": 4`. `SchemaVersion` is now generic over the version it admits: `v3::SchemaVersion` is `SchemaVersion<3>` and `v4::SchemaVersion` is `SchemaVersion<4>`, each refusing the other's payloads, and plain `SchemaVersion` still means v3. Only v4 types derive Stack Encrypt's `EncryptFrom` and `DecryptInto`, and the Stack Encrypt target table (now `eql_bindings::v4::targets`) names v4 domains. Besides `TextEq`, Stack Encrypt now produces the storage-only types `Text`, `Integer`, `Bigint`, `Boolean`, `Real` and `Double`, and `TextMatch` (3-grams, `k = 6`, a 2048-bit filter, pinned as `eql_bindings::encryption::EqlMatch`; text shorter than three characters is refused, stored or queried, and only `stored @@ query` matches until #1171 is fixed), from the Rust primitives `i32`, `i64`, `bool`, `f32` and `f64`; the Go SDK's `encrypt/eql` table marks them producible. Stack Encrypt also produces the number equality types `IntegerEq`, `BigintEq`, `RealEq` and `DoubleEq`, whose `=` compares as PostgreSQL compares the plain types: an integer has one term whatever its width, and for floats `-0.0 = 0.0` and `NaN = NaN` (ADR-0002's semantic baseline). Every type it cannot produce yet names the issue that tracks its blocker. A Rust caller queries those types through Stack Encrypt's typed query chain: `cipher.query(op::Eq, v).on(&plan)` on a plan for `TextEq` or a number equality type returns the type's query twin, and `cipher.query(op::Matches, text).on(&plan)` on a plan for `TextMatch` returns a `MatchTwin` with `verify` and `target_limit`, the same bytes and answers as the data path the Go SDK uses (`eql_bindings::encryption::query`; an operator a type does not answer does not compile). The by-name target entry points (`eql_bindings::encryption::targets::encrypt`, `decrypt` and `query`) exchange the plaintext as Stack Encrypt's `dynamic::ValueFrame`, one value as its transport frame, rather than a value tree. They, and the generated `v4::targets` table, need the new `targets` feature, which turns on `stack-encrypt` and Stack Encrypt's `dynamic` feature. `stack-encrypt` alone no longer builds Stack Encrypt's `dynamic` layer, so a Rust program using the typed v4 types through `encrypt_as` doesn't compile it. +**The `eql-bindings` crate has an `eql_bindings::v4` module, with the `stack-encrypt` feature: payload types for the EQL v4 domains.** EQL v4 is the v3 SQL installed under a second name (ADR-0002), for values written by Stack Encrypt, so the module exists only with that feature and its types derive Stack Encrypt's traits without a further gate. The module is generated from the same catalog as `v3`, with `eql_v4_*` domain names and `"v": 4`. `SchemaVersion` is now generic over the version it admits: `v3::SchemaVersion` is `SchemaVersion<3>` and `v4::SchemaVersion` is `SchemaVersion<4>`, each refusing the other's payloads, and plain `SchemaVersion` still means v3. Only v4 types derive Stack Encrypt's `EncryptTarget` and `DecryptSource`, and the Stack Encrypt target table (now `eql_bindings::v4::targets`) names v4 domains. Besides `TextEq`, Stack Encrypt now produces the storage-only types `Text`, `Integer`, `Bigint`, `Boolean`, `Real` and `Double`, and `TextMatch` (3-grams, `k = 6`, a 2048-bit filter, pinned as `eql_bindings::encryption::EqlMatch`; text shorter than three characters is refused, stored or queried, and only `stored @@ query` matches until #1171 is fixed), from the Rust primitives `i32`, `i64`, `bool`, `f32` and `f64`; the Go SDK's `encrypt/eql` table marks them producible. Stack Encrypt also produces the number equality types `IntegerEq`, `BigintEq`, `RealEq` and `DoubleEq`, whose `=` compares as PostgreSQL compares the plain types: an integer has one term whatever its width, and for floats `-0.0 = 0.0` and `NaN = NaN` (ADR-0002's semantic baseline). Every type it cannot produce yet names the issue that tracks its blocker. A Rust caller queries those types through a field marker of a derived record: `EncryptedMember::EMAIL.eq(v).encrypt(&keyset)` on a field typed `TextEq` or a number equality type returns the type's query twin, and `.matches(text)` on a field typed `TextMatch` returns a `MatchTwin` with `verify` and `target_limit`, the same bytes and answers as the data path the Go SDK uses (`eql_bindings::encryption::query`; an operator a type does not answer does not compile). The by-name target entry points (`eql_bindings::encryption::targets::encrypt`, `decrypt` and `query`) exchange the plaintext as Stack Encrypt's `dynamic::ValueFrame`, one value as its transport frame, rather than a value tree. They, and the generated `v4::targets` table, need the new `targets` feature, which turns on `stack-encrypt` and Stack Encrypt's `dynamic` feature. `stack-encrypt` alone no longer builds Stack Encrypt's `dynamic` layer, so a Rust program using the typed v4 types through `encrypt_into` doesn't compile it. **The EQL v4 SQL bundle has range query domains and `eql_v4.between`, so a `BETWEEN` query is one operand.** The v4 bundle (`cipherstash-encrypt-v4.sql`) is built from this source and reaches users as the `eql-v4-install.sql` attached to each Go release; neither the npm package nor the crate ships it, and the v3 SQL they ship does not change. Every ordered family has one range query domain per order term, beside its single-bound ones: `eql_v4.query_integer_range_ore` beside `eql_v4.query_integer_ord_ore`, `eql_v4.query_integer_range_ope` beside `eql_v4.query_integer_ord_ope`, and the same for `smallint`, `bigint`, `date`, `timestamp`, `numeric`, `text`, `real` and `double`. A range is `{"v": 4, "i": …, "lo": , "hi": }`: each bound is the single-bound domain's `ob` (ORE) or `op` (OPE) value, in a `{v, i, lo, hi}` envelope. Stack Encrypt does not build that envelope yet; it will need to when the ordered types become producible. `eql_v4.between(column, range)` is true when the column's order term lies between the bounds, both included, and has an overload for every ordered column domain with the range of the term it stores (`_ord_ore` and `_search_ore` take the ORE range; `_ord`, `_ord_ope` and `_search` the OPE one). It is an inlinable SQL function, so a btree on the column's order term (`eql_v4.ord_term_ore(col)` or `eql_v4.ord_term(col)`) serves it as one index range scan. Write it schema-qualified (`BETWEEN` is an SQL keyword, so a bare `between(...)` does not parse), and cast the range to its exact domain (`$1::eql_v4.query_integer_range_ore`): an uncast `$1`, a `jsonb` parameter, or a range of another term or family fails with SQLSTATE 42725 ("function … is not unique"), unlike the single-bound operators, which resolve an untyped `$1`. An ORE range scan needs the `ore_block_256` btree operator class, which the installer creates only as a superuser; on an install without it (Supabase and other non-superuser roles), the ORE range domains are disabled like the single-bound ORE domains, and a value cast to one raises `feature_not_supported` naming the family's OPE range. In the crate, `eql_bindings::v4` (`stack-encrypt` feature) has a type for each range domain (`IntegerRangeOreQuery`, `TextRangeOpeQuery`, …, listed by `v4::all_range_query()`). EQL v3 has no range domains: its SQL and types are unchanged (ADR-0009, decision 7). diff --git a/.changeset/eql-v4-owned-targets.md b/.changeset/eql-v4-owned-targets.md index 746c681051..75e92d59f2 100644 --- a/.changeset/eql-v4-owned-targets.md +++ b/.changeset/eql-v4-owned-targets.md @@ -6,9 +6,7 @@ - Every EQL v4 type that Stack Encrypt produces implements `EncryptTarget` and `DecryptSource`. Every derived query type implements `EncryptTarget`. - A Rust program can write `email.encrypt_into_with_context(&keyset, column)` and `String::decrypt_from(stored, &keyset)`. The owned traits take the plaintext by value and wipe it. -- The same derives still implement the older `EncryptFrom` and `DecryptInto`. So `encrypt_as`, plans, and the by-name dispatch work as before. -- The derives no longer generate a `plan()` function on each type. To build a plan for a type, use `Plan::value::

().encrypt_into::()`. -- Both APIs write the same terms. Each API opens what the other API wrote. +- The by-name dispatch (`encryption::targets`) runs each type through these traits. - An `Option` of an EQL type stores a database NULL for `None`. - A type encrypted from an `Option` of its plaintext still seals a null, as before. diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml index dd18c169c1..4bcbcef417 100644 --- a/.github/workflows/fuzz.yml +++ b/.github/workflows/fuzz.yml @@ -113,7 +113,6 @@ jobs: - { task: "fuzz:sealed-value", slug: sealed-value } - { task: "fuzz:term-decode", slug: term-decode } - { task: "fuzz:check-record", slug: check-record } - - { task: "fuzz:plan-build", slug: plan-build } - { task: "fuzz:json-wire", slug: json-wire } - { task: "fuzz:json-text", slug: json-text } steps: @@ -163,7 +162,6 @@ jobs: - { task: "fuzz:sealed-value", slug: sealed-value, dir: packages/stack-encrypt, target: sealed_value_decode } - { task: "fuzz:term-decode", slug: term-decode, dir: packages/stack-encrypt, target: term_decode } - { task: "fuzz:check-record", slug: check-record, dir: packages/stack-encrypt, target: check_record } - - { task: "fuzz:plan-build", slug: plan-build, dir: packages/stack-encrypt, target: plan_build } - { task: "fuzz:json-wire", slug: json-wire, dir: packages/stack-encrypt, target: json_wire_decode } - { task: "fuzz:json-text", slug: json-text, dir: packages/stack-encrypt, target: json_text } steps: diff --git a/conformance/stack-encrypt/README.md b/conformance/stack-encrypt/README.md index d25e63ebf3..9e73f4c8cb 100644 --- a/conformance/stack-encrypt/README.md +++ b/conformance/stack-encrypt/README.md @@ -154,8 +154,8 @@ the Go-only host conversions the host refuses before the engine. The `record-query-refusals-v1` in `v1/record-query-refusals-v1.cases.json`: one plan, every scalar operator against every field capability that does not answer it, the range shapes, other-kind values and nulls the contract lists, each refusal's -class and diagnostic, no key I/O, and the stage at which the typed Rust -chain refuses the same query. The [raw-frame contract](raw-frames-contract.md) +class and diagnostic, no key I/O, and the stage at which a typed Rust +query refuses the same query. The [raw-frame contract](raw-frames-contract.md) defines `record-raw-frames-v1` in `v1/record-raw-frames-v1.cases.json`: stored batches as transport bytes, so repeated keys and malformed leaves reach both decoders, and the table of error classes (transport, leaf, shape, authenticated, diff --git a/conformance/stack-encrypt/context-contract.md b/conformance/stack-encrypt/context-contract.md index 1868fa8e54..592053b1c6 100644 --- a/conformance/stack-encrypt/context-contract.md +++ b/conformance/stack-encrypt/context-contract.md @@ -10,9 +10,8 @@ are added; their absence is not conformance evidence. A field's base is a plain label: the record's declared label followed by the field's identity. A context-field record obtains the record label from each -row's stored field. A Rust plan built without a context can instead take that -label from the call. Go generated types declare either a literal context or a -context field; they do not expose Rust's contextless saved-plan builder. +row's stored field. Go generated types declare either a literal context or a +context field. Caller input has two distinct purposes: @@ -80,9 +79,9 @@ the public all-zero deterministic-provider test seed. `v1/record-contexts-v1.expected.rust-native.json` is keyed by exactly those case IDs. It pins the resolved context frame, the field context frame sent in a plan, descriptor, AAD, PRF bytes, one Rust ciphertext and equality term. Rust checks actual descriptors sent to the key provider, fresh typed and -dynamic writes, queries, opens, and frozen ciphertexts. The typed chain calls -`extend` repeatedly, including a final no-op, so replacement behavior cannot -regress unnoticed. +dynamic writes, queries, opens, and frozen ciphertexts. The typed side runs +the engine's combinators under a declared context with one part for each +part of the case, so a later part cannot replace an earlier one unnoticed. Go observes the actual plan buffer at the `se_encrypt_record` WASM boundary through a test-only function wrapper. It compares the field context in those @@ -109,14 +108,16 @@ Cases cover unrestricted reads, matching expectations, late mismatches, empty batches, changed stored labels with and without matching expectations, and missing/empty stored contexts late in a batch. A two-row ciphertext-tampering case reaches AEAD after retrieval in both languages and returns no partial -plaintext. Typed Rust opening and generated Go opening exercise the boundary -cases too, except that an absent member cannot be represented by a generated Go -struct and is tested at the raw-tree boundary. +plaintext. Generated Go opening exercises the boundary cases too, except that +an absent member cannot be represented by a generated Go struct and is tested +at the raw-tree boundary. Typed Rust opening, through the combinators, holds +no expected context, so it runs the cases that need none: the unrestricted +read and the three tampering cases. The separate `queries` array specifies a caller `context`, an `outcome` and whether the query `matches` the stored term for `tenants/acme`. Missing and invalid contexts refuse; another tenant produces a valid nonmatching query. -Rust typed query chains and generated Go query methods execute these cases. +Rust data-plan queries and generated Go query methods execute these cases. Key-independent refusals must make no retrieval. An empty valid batch also makes none. Changing the stored label does not confer permission to open the ciphertext even if the caller's expectation matches the replacement label. @@ -132,8 +133,10 @@ The earlier read profile continues to cover context storage shape and exact structured diagnostics. The enrolled `record-context-fields-v1` fixture retains generated Go expectation, query, tampering and single-field update checks and Rust typed/dynamic cross-opening. The required native `plan_grammar` -binary additionally covers missing caller bases, conflicting sources, invalid -labels, caller-derived queries and empty-batch validation before keyset loading. +binary additionally covers, through the data plan, a missing context (a +query on a context-field plan that names none, and an empty caller scope), +conflicting sources, invalid labels, caller-derived queries and an empty +scope on an empty batch. Each refusal is made before any key is requested. ## Bindings diff --git a/conformance/stack-encrypt/coverage.md b/conformance/stack-encrypt/coverage.md index 729ff10640..10377adab7 100644 --- a/conformance/stack-encrypt/coverage.md +++ b/conformance/stack-encrypt/coverage.md @@ -10,7 +10,7 @@ Paths are relative to the repository root. New profile cases run from `v1/`. | Owned opaque ciphertext and storage envelope | `packages/stack-encrypt/tests/scalar.rs`, `frozen_bytes.rs`; initial cross-opening cases; `owned_records.rs`, which tests the owned record derives: passthrough fields are moved, sealed fields are wiped, nested and renamed fields work, a record can store its context, and a column makes one batch. Whole-value blobs: `packages/stack-encrypt/tests/opaque_blob.rs` (round trips, the bytes leaf shared with the structural writer, a pinned storage blob and a pinned sealed ciphertext, refusals by class after authentication, source wipe) and `languages/golang/encrypt/gensupport/gensupport_internal_test.go::TestOpaqueReadsARustWrittenBlob`; the storage profile itself is pinned upstream by Vitamin C's golden corpus; fresh sealed blobs exchanged in both directions by the [exchange profile](exchange-contract.md). JSON documents as one ciphertext: `packages/stack-encrypt/tests/json_plaintext.rs` (round trips, the text leaf shared with a sealed string, refusals after authentication, a generated-document property) and, as the EQL type `Json`, `packages/eql/tests/encryption/tests/v4_types.rs` with the Go-sealed rows `v4_json_rows.json` and the frozen `stored_v4_json_rows_v1.json`, opened in Rust and Go | Encoded targets; supported version matrix | | Record declaration and lowering | [Declaration grammar](declaration-grammar.md); `v1/record-declarations-v1.cases.json` shared by native Rust and Go; native normalization observations; existing record lowering suites; `packages/stack-encrypt/tests/owned_equivalence.rs`, which seals one mixed, searchable record three ways: through a hand-written owned target, the owned derive, and the data plan. All three use one provider that records its requests. The test compares the descriptors and the term bytes, and checks that each way decrypts the records of the others | EQL-resolver profile; SDK-to-declaration mappings; runtime source/read shapes | | Context binding and composition | `packages/stack-encrypt/tests/target.rs`, `descriptor.rs`, `record_lowering.rs`; `languages/golang/encrypt/context_field_test.go`; initial wrong-context cases | Shared composition vectors; expected-context and required-input matrix | -| Query terms and diagnostics | [Query-operator requirements](fixture-contract.md#query-operator-requirements), existing Rust/Go runners, initial equality/query-error cases and the [query-refusal profile](query-refusals-contract.md) (`v1/record-query-refusals-v1.cases.json`: every scalar operator against every field capability that does not answer it, the listed range shapes, other-kind values and nulls, by class, with the typed chain's refusal stage) | JSON operator and EQL target refusals; moving the typed chain's value refusals before the keyset | +| Query terms and diagnostics | [Query-operator requirements](fixture-contract.md#query-operator-requirements), existing Rust/Go runners, initial equality/query-error cases and the [query-refusal profile](query-refusals-contract.md) (`v1/record-query-refusals-v1.cases.json`: every scalar operator against every field capability that does not answer it, the listed range shapes, other-kind values and nulls, by class, with the typed query's refusal stage) | JSON operator and EQL target refusals | | Missing, SQL NULL and sealed null | `packages/stack-encrypt/src/dynamic/record.rs` null tests; `languages/golang/encrypt/null_test.go` | Explicit language mappings and common missing/null fixtures | | Fresh cross-language storage | `.github/workflows/tests-golang.yml` exports fresh Go EQL rows for Rust, the storage-only `Json` documents among them; initial scalar artifact exchange; fixed storage-blob vectors read by both Rust (`opaque_blob.rs`) and Go (`TestOpaqueReadsARustWrittenBlob`); the [exchange profile](exchange-contract.md): values, records, blobs and EQL rows written fresh and opened in both Rust/Go directions, with producer, engine, backend and storage versions in the report | Python/TypeScript pairs and backends when their runners exist; supported released versions once the compatibility policy is defined; fresh exchange of searchable JSON documents, and of `Json` documents from Rust to Go; a SQL lane storing one language's fresh rows through the other | | JSON queries, extraction and tampering | `packages/stack-encrypt/tests/json_tree_vectors.rs`; `packages/eql/tests/encryption/tests/json_tree.rs`; `languages/golang/encrypt/json_tree_test.go`, `json_postgres_test.go` | Formal operation profile; JSON vectors consumed by all runners | @@ -100,9 +100,10 @@ adding a language does not create another copy of the expected behavior. The [context contract](context-contract.md) adds 18 shared composition cases and eleven shared read boundary cases and four query cases, plus enrollment of the existing record-lowering -context-field fixture. Rust typed write/query/open chains and Go generated -methods execute the composition cases. Required native plan-grammar tests cover -caller-provided bases and empty-batch source validation. Existing context tests +context-field fixture. Rust typed writes, queries and opens (the engine's +combinators) and Go generated methods execute the composition cases. Required +native plan-grammar tests cover a missing context, conflicting sources and an +empty scope on an empty batch, through the data plan. Existing context tests remain: no mixed-purpose Rust or Go test is removed. Go proves canonical AAD indirectly through its actual guest plan frame and native @@ -139,14 +140,12 @@ Each runs in native Rust and Go WASI under `test:conformance`. |---|---|---| | `value_kinds.json` and its runners | `i8-*`, `u8-*`, `i16-*`, `u16-*`, `u32-*`, `i128-*`, `u128-*`, `f32-*`, `f64-*`, `bytes-*`, `decimal-*`, `date-*`, `timestamp-*`, `sealed-*`, `sql-*`, `*-given-*` | Typed Rust term comparisons and generated Go methods | | Native `dynamic/query.rs` refusal tests, Go `TestVerifyAndTargetLimitRefuseWhatIsNotAMatchQuery` | Every `record-query-refusals-v1` case | Tree-notation inputs, the hand-built match options, generated query methods | -| Native typed refusal tests in `tests/op.rs` | The `typed` entries | Case-sensitivity mismatch, batch and `via` forms, which the API plan removes (its step 3) | +| Native typed refusal tests in `tests/op.rs` | The `typed` entries | A refusal in a batch of two queries, and the refusal on a case-sensitive field | | Native `a_stored_record_that_does_not_fit`, the read profile | `shape-*`, `authenticated-*`, `success-*` | Mutation vocabulary over real ciphertext | | Guest `test_frame` and `last_error` tests | `transport-*`, `leaf-*` | Plan-frame boundary cases, ABI status mapping | Unsupported combinations the corpora record rather than skip: the order -index on `bytes` stores a term no operator reads; the typed chain refuses -equality, order and block ORE value refusals only after its keyset loads, -where the binding refuses before any; a repeated key in a stored record is +index on `bytes` stores a term no operator reads; a repeated key in a stored record is a transport refusal in both decoders, and the engine's own `field_repeated` and `repeated_key` guards are reached natively alone; the Go host refuses the zero `Date`, a decimal scale above 28, a timestamp outside the @@ -257,7 +256,7 @@ Considered and kept, with the reason: - `tests/op.rs` typed refusals (`a_match_query_with_no_tokens_is_refused_before_any_term_or_keyset`, `a_match_query_is_cut_in_nfc_and_refuses_text_with_no_normal_form`, `an_inverted_range_is_a_query_like_any_other`): the corpus's `typed` entries - record the typed chain's refusal stage, not its `via`, batch, case-sensitive + record the typed query's refusal stage, not its batch, case-sensitive and bound-order assertions. - Go `TestPlanCheckAnswersWithoutACipher`, `TestQueryTermRefusesWhatTheFieldDoesNotAnswerBeforeState`, `TestGuestRefusesMalformedInputsBeforeState`, `TestCheckerRefusesAnIndexedUntypedField`: diff --git a/conformance/stack-encrypt/fixture-contract.md b/conformance/stack-encrypt/fixture-contract.md index 55a26bbe8b..8cb20f9229 100644 --- a/conformance/stack-encrypt/fixture-contract.md +++ b/conformance/stack-encrypt/fixture-contract.md @@ -61,7 +61,7 @@ the fixture. It does not claim every operator for every kind or index. | `queries.match-verification` | Verify the complete page in input order, including matches, nonmatches and null; results agree with the frozen boolean vector. | `verify` | | `queries.fetch-limits` | Given the frozen match term, requested count and statistics, return the recorded bounded row count or explicit unbounded result. | Every `target_limit` entry | -Rust compares the dynamic path with typed query chains, `MatchQuery::verify` +Rust compares the dynamic path with typed queries from field markers, `MatchQuery::verify` and typed fetch sizing, and checks that the fixture contains both verification outcomes and both bounded/unbounded limits. Go exercises the record interface and generated query methods. Generated string-page verification omits null, diff --git a/conformance/stack-encrypt/lifecycle-contract.md b/conformance/stack-encrypt/lifecycle-contract.md index 8016314731..7c4cef53f5 100644 --- a/conformance/stack-encrypt/lifecycle-contract.md +++ b/conformance/stack-encrypt/lifecycle-contract.md @@ -12,7 +12,7 @@ process. Python and TypeScript are explicitly not enrolled. |---|---|---| | `lifecycle-ownership-v1` | Actual record operations and owned `Vec` to `Vec` conversions release fulfilment captures exactly once on success, provider failure and unpolled drop; an unpolled encrypt or decrypt makes no key request. Zipped fulfilments do not share key material. An owned `OpaqueBlob` source is wiped before its pending is returned, on success and on an encoding refusal. A decrypted `OpaqueBlob` is wiped once on drop, and the `Zeroizing` its `into_inner` returns is wiped once when it is dropped, not before. | Client keys are consumed/redacted; explicit close and unreachable-client cleanup release guest memory; calls on a closed client fail. GC cleanup is eventual, so callers should close explicitly. | | `lifecycle-cancellation-v1` | Dropping an encrypt future paused in key generation, or a decrypt future paused in key retrieval, drops the provider future and operation captures, for a record operation or an owned collection; subsequent work on the cipher succeeds. Guest ABI abort spends the handle and drops the operation. | Cancellation before a call sends no request; cancellation between steps or during token fetch leaves unrelated operations usable. A guest step that overruns cancellation closes the client. Discarded late output is wiped. | -| `lifecycle-partial-output-v1` | A batch with a valid first row and a later authenticated type mismatch releases its decoded prefix without marked heap residue. Tests check this for the dynamic record path and for the owned `Vec` to `Vec` conversion. For owned collections, drop witnesses are test values that record whether they were wiped. They show that the prefix is wiped, not leaked, after a late failure or an element's panic. The same holds for a structural `Vec` that decrypts through `DecryptInto`, and for a column field of a structural record. For an owned record, a drop witness shows that a field opened before a later field is refused is wiped. Existing context cases reject late authentication failures without returning partial output. An `OpaqueBlob` refused before its first field is built leaves no marked residue; a refusal after some fields are built is the `native-partial-decode-fields` limit below. | Generated and raw batch APIs return no partial rows on failure; interrupted HTTP bodies fail; cancelled output is wiped. SDK conversion diagnostics contain no decrypted values. This does not assert erasure of partially populated caller-owned Go objects. | +| `lifecycle-partial-output-v1` | A batch with a valid first row and a later authenticated type mismatch releases its decoded prefix without marked heap residue. Tests check this for the dynamic record path and for the owned `Vec` to `Vec` conversion. For owned collections, drop witnesses are test values that record whether they were wiped. They show that the prefix is wiped, not leaked, after a late failure or an element's panic. The same holds for a column that is a field of a record. For an owned record, a drop witness shows that a field opened before a later field is refused is wiped. Existing context cases reject late authentication failures without returning partial output. An `OpaqueBlob` refused before its first field is built leaves no marked residue; a refusal after some fields are built is the `native-partial-decode-fields` limit below. | Generated and raw batch APIs return no partial rows on failure; interrupted HTTP bodies fail; cancelled output is wiped. SDK conversion diagnostics contain no decrypted values. This does not assert erasure of partially populated caller-owned Go objects. | | `lifecycle-buffer-cleanup-v1` | The existing allocator observer checks selected write/read/JSON parse paths and has a negative control. It also checks an `OpaqueBlob`'s encoded buffer (which regrows while it holds plaintext), its planned leaf and its opened buffer, and for those blob paths that no marked block allocated during the operation is still live at its end (a leak the free-time scan cannot see), with its own negative control. The partial-decode limit is pinned separately and is not enrolled here (see below). One of those paths encrypts an owned `Vec` collection through its guard. Drop witnesses show that an owned collection's source is wiped before its pending request is returned. That includes a refused context, and a context conversion that panics. It also includes the elements after an element whose declaration panics. For an owned record, drop witnesses show that the record wipes the fields it seals, also when the context is refused. When a context is refused, no copy of a sealed plaintext is dropped unwiped. Witnesses check this for a `struct` record, a nested record, a `plaintext` record, a record with a stored context, a column, a `StackCipherText`, a `Vec`, and an `Encrypted`. They also show that the record moves its passthrough fields without copying them, and that a record decrypted into a `Protected` is wiped when the `Protected` is dropped. Guest ABI tests check buffer-registry cleanup, error-buffer release and protected text drop. | Request bodies survive until their owner closes them, then wipe; refused requests wipe; heap growth wipes abandoned backing arrays; guest runtime close releases memory. Post-encryption memory scans are supplementary observations. | | `lifecycle-diagnostics-v1` | Guest errors discard untrusted foreign cause messages; failed/successful exports do not hand over stale diagnostics. | Structured errors remain available after failures; SDK errors and redacted display/logging expose types or fields rather than plaintext values. | | `lifecycle-validation-before-io-v1` | Existing runtime/read/context cases count provider calls. Wrong shape, independently detectable context mismatch and oversized descriptors fail before key I/O; lazy assembly stops after local failure. An `OpaqueBlob` whose `Serialize` refuses requests no key, and an oversized context is refused before the value is encoded; on decrypt the leaf authenticates (one retrieval) before any blob byte is read, and tampering is `Aead` however the bytes would decode. | Host refusals happen before entering the guest, and shared runtime/read cases exercise the uninitialised guest's preflight boundary. Go does not yet expose native provider counters. | diff --git a/conformance/stack-encrypt/query-refusals-contract.md b/conformance/stack-encrypt/query-refusals-contract.md index b816a68c9d..f772ed61b2 100644 --- a/conformance/stack-encrypt/query-refusals-contract.md +++ b/conformance/stack-encrypt/query-refusals-contract.md @@ -78,30 +78,30 @@ The operator vocabulary is closed in Rust (`dynamic::Op`), so the unknown operator case is observed at the binding boundary alone; the Rust runner checks that the case expects exactly that and continues. -## The typed chain +## The typed query -`typed` records how the typed Rust query chain answers the same query, for -the queries it can spell (text on the text fields, a `uint32` range under -ORE and an `int128` range under OPE): `before_keyset`, `after_keyset` or -`none`. The Rust runner builds a one-field typed plan with the index that -answers the operator, runs the chain under a keyset named for the case, and -counts the keyset load. A refusal before the load is `before_keyset`; one -after it is `after_keyset`. The error code must equal the binding's. +`typed` records how a typed Rust query answers the same query, for the +queries it can spell (text on the text fields, a `uint32` range under ORE +and an `int128` range under OPE): `before_keyset`, `after_keyset` or +`none`. A typed query is a query from the field marker of a derived record. +The Rust runner has one derived record whose fields have the corpus's +labels, runs the marker's query for the case, and counts the keyset loads +and the key requests. The error code must equal the binding's. The two paths refuse the same inputs with the same codes at the same -stage: before any keyset is resolved. Every `typed` entry of a refused case -is `before_keyset`. Both paths call the one set of rules in the engine's -`sem::query` module: the typed chain through each index's +stage: before any keyset is resolved and before any key is requested. Every +`typed` entry of a refused case is `before_keyset`, and the runner refuses +an `after_keyset` entry. Both paths call the one set of rules in the +engine's `sem::query` module: the typed query through each index's `Supports::prepare`, and the binding through `check_query`, `query_term` and `verify`. A rule written there is a rule on both paths, and a rule written anywhere else is a drift this profile fails on. -Until the API plan's step 2 ("The engine's rules, and the order of the -work", `docs/plans/2026-10-24-stack-encrypt-api-plan.md`) the typed chain -refused equality text with no normal form, order text with no normal form -and block ORE text over its limit only after the keyset was loaded, and the -`typed` entries of those rows said `after_keyset`. They were flipped on -purpose with that step. +A typed query takes a keyset that is already loaded, so it cannot refuse +after a keyset load. The value `after_keyset` stays in the vocabulary for +the corpus's history: until the API plan's step 2 ("The engine's rules, and +the order of the work", `docs/plans/2026-10-24-stack-encrypt-api-plan.md`) +some rows said `after_keyset`. ## Scope and remaining work diff --git a/conformance/stack-encrypt/v1/eql-json-v1.enrollment.json b/conformance/stack-encrypt/v1/eql-json-v1.enrollment.json index 7d3e392b9e..dc955c3127 100644 --- a/conformance/stack-encrypt/v1/eql-json-v1.enrollment.json +++ b/conformance/stack-encrypt/v1/eql-json-v1.enrollment.json @@ -117,7 +117,7 @@ "evidence": { "eql-record": [ "a_document_field_answers_the_json_operators", - "every_operand_is_the_typed_chains", + "every_operand_is_in_the_eql_wire_form_and_finds_what_it_should", "a_json_operator_refuses_what_it_cannot_answer", "a_document_has_no_one_plaintext_query" ], diff --git a/conformance/stack-encrypt/v1/lifecycle-v1.enrollment.json b/conformance/stack-encrypt/v1/lifecycle-v1.enrollment.json index ff325f7029..e2c248adaa 100644 --- a/conformance/stack-encrypt/v1/lifecycle-v1.enrollment.json +++ b/conformance/stack-encrypt/v1/lifecycle-v1.enrollment.json @@ -169,8 +169,7 @@ "collections": [ "a_late_element_failure_wipes_the_plaintext_recovered_before_it", "a_panicking_element_conversion_wipes_the_plaintext_recovered_before_it", - "a_late_element_failure_in_a_column_field_wipes_the_recovered_prefix", - "a_late_element_failure_wipes_the_recovered_prefix_on_the_structural_path_too" + "a_late_element_failure_in_a_column_field_wipes_the_recovered_prefix" ], "owned-records": [ "a_field_opened_before_a_later_field_is_refused_is_wiped" diff --git a/docs/fuzzing.md b/docs/fuzzing.md index d946e179d5..4ae31eb7f0 100644 --- a/docs/fuzzing.md +++ b/docs/fuzzing.md @@ -27,7 +27,6 @@ Current targets: | `fuzz:sealed-value` | `stack-encrypt` | `sealed_value_decode` | `SealedValue::from_bytes` (frozen v1 leaf); accepted input must re-encode to itself | | `fuzz:term-decode` | `stack-encrypt` | `term_decode` | the SEM term decoders (`EqualityTerm`, `MatchTerms`, `OreTerm`, `OpeTerm` `from_bytes`) | | `fuzz:check-record` | `stack-encrypt` | `check_record` | `dynamic::record::check_record` over an `Arbitrary`-derived plan and tree, against a model of the record rules (structure-aware) | -| `fuzz:plan-build` | `stack-encrypt` | `plan_build` | `plan::FieldsBuilder::build` over `Arbitrary`-derived untrusted names (context, fields, identities); a built field's label must render and parse back, and passthrough names are never refused (structure-aware) | | `fuzz:json-text` | `stack-encrypt` | `json_text` | `json::parse_text`, the boundary parser for a language SDK's JSON document, against serde_json: it reads what serde_json reads to the same value, refuses beyond it only repeated members and out-of-range integers, and what it reads writes back to itself | | `fuzz:json-wire` | `stack-encrypt` | `json_wire_decode` | the JSON index's wire decoders (`JsonDocument`, `JsonNode`, `JsonSelectorSet`, `JsonLeafQuery` from JSON text; `JsonHeader::from_bytes`), over base64 tags and base85 ciphertexts; an accepted document or node must round-trip through its wire form | diff --git a/docs/plans/2026-10-24-stack-encrypt-api-plan.md b/docs/plans/2026-10-24-stack-encrypt-api-plan.md index c516a44e27..89ff2f9d72 100644 --- a/docs/plans/2026-10-24-stack-encrypt-api-plan.md +++ b/docs/plans/2026-10-24-stack-encrypt-api-plan.md @@ -2,7 +2,9 @@ Date: 8 November 2026. Checked against `next` at commit `e4990e49b`. Revised 9 November 2026: the derive emits the combinators, not the plan -builder, and `Planned` is a target (ADR-0007, amended). +builder, and `Planned` is a target (ADR-0007, amended). Tracking lines +refreshed on 15 November 2026, against `next` at `360530276` and the open +stack of pull requests #1613, #1659, #1664 and #1666. Stack Encrypt encrypts each value under its own data key from ZeroKMS, CipherStash's key service. It also makes the search terms that let @@ -1179,8 +1181,10 @@ NEW.body := eql_v4.label_tag(NEW.body, 'row_id', NEW.id::text); ## Remaining work Everything in this section is work to implement. Each item ends with the -issues and pull requests that track it, checked on 8 November 2026. -`next` was still at `e4990e49b`, the plan's last check. +issues and pull requests that track it, checked on 15 November 2026, +against `next` at `360530276`. Every issue named in this section is in +the milestone "Stable Stack Encrypt Rust release". An item marked +**Done** is kept so that its decisions stay on record. An issue that a merged pull request says it closes can stay open, because GitHub closes issues only on the default branch. So an open @@ -1208,10 +1212,12 @@ release leaves behind. - stored reads and context; - queries; - EQL and JSON; - - the lifecycle of values and keys. The profiles are experimental. They do not certify a whole SDK. Three pull requests are in review. Pull request #1505 adds value, - refusal, and raw-frame corpora. Pull request #1506 adds a fresh Rust - and Go artifact exchange. Pull request #1507 consolidates the tests. -- **Tracked in.** Issues #1477, #1478, #1479, #1481, #1490, and #1314 are open. Issue #1512 is open for cross-language queries through PostgreSQL. Issues #1471 to #1476 and #1480 are closed. Pull requests #1505, #1506, and #1507 are open, stacked on #1503. Pull request #1506 adds `conformance/stack-encrypt/exchange-contract.md` and a placeholder for the released-version matrix. + - the lifecycle of values and keys. The profiles are experimental. They do not certify a whole SDK. Pull request #1505 added value, + refusal, and raw-frame corpora. Pull request #1506 added a fresh Rust + and Go artifact exchange, with `conformance/stack-encrypt/exchange-contract.md` + and a placeholder for the released-version matrix. Pull request #1507 + consolidated the tests. All three are merged. +- **Tracked in.** Issue #1678 is open for the JSON Schemas and the protocol version identifiers. Issue #1490 is open for the stable gates and the version matrix. Issue #1314 is open for the compatibility policy, #1479 for a TypeScript runner, and #1512 for cross-language queries through PostgreSQL. Issues #1471 to #1478, #1480 and #1481 are closed. - **Finished when** these are all true: - the profiles are no longer experimental; - the schemas exist; @@ -1231,7 +1237,7 @@ release leaves behind. see one child. That transfers more data and reveals a different set of tokens. - **Done already.** Nothing. -- **Tracked in.** No issue. Pull request #1353, the Go JSON plan document, lists it as remaining. +- **Tracked in.** Issue #1677 is open. Pull request #1353, the Go JSON plan document, lists it as remaining. - **Finished when** Go can open one child, and the `eql-json-v1` conformance profile covers it. @@ -1244,7 +1250,7 @@ release leaves behind. has the design. - **Why.** Today Go uses only the default match options. - **Done already.** Words of two letters can be searched (#1397). -- **Tracked in.** Issue #1112 is open. Issues #1398 and #1399 are open for match-index follow-ups. Issue #1299 is an open question on short substrings. Issue #1396 is closed. Pull request #1397 is merged. +- **Tracked in.** Issue #1112 is open for the generator. Issue #1673 is open for the analyzer that defines the options. Issues #1398 and #1399 are open for match-index follow-ups. Issue #1299 is an open question on short substrings. Issue #1396 is closed. Pull request #1397 is merged. - **Finished when** a Go field can declare match options, and its queries use the same options as its stored terms. @@ -1258,7 +1264,7 @@ release leaves behind. frozen: `stored_rows_v1.json` since 18 October 2026, and the Go rows that `eql_v4_test.go` never regenerates. Both predate the rc.3 tag of 2 November 2026, so neither is labelled rc.3. -- **Tracked in.** No dedicated issue. Issue #1119 is closed. It froze `stored_rows_v1.json` on 18 October 2026. Issue #1490 is open. It covers stable gates and supported conformance versions. +- **Tracked in.** Issue #1490 is open, and lists this item as a task. Issue #1119 is closed. It froze `stored_rows_v1.json` on 18 October 2026. - **Finished when** CI fails on any change to the stored bytes of rc.3 data. @@ -1268,7 +1274,7 @@ release leaves behind. with released module tags. - **Why.** A final Go release cannot depend on unreleased commits. - **Done already.** Prereleases can keep the pins, so rc.3 uses them. -- **Tracked in.** No direct issue. Pull request #1369, merged, introduced the pin. Issue #1253 is open. It would bring the frame codec into the Go module and remove the dependency instead. +- **Tracked in.** Issue #1679 is open. Pull request #1369, merged, introduced the pin. Issue #1253 is open. It would bring the frame codec into the Go module and remove the dependency instead. - **Finished when** the release workflow accepts a final version. ### 2. The stored format @@ -1287,7 +1293,7 @@ format version beside it. | Configurable match options | The terms of columns written with the rc.3 defaults | | Context parts as key request items | The descriptor a key is derived under, and so the data key itself. A new envelope version marks the new form | -- **Tracked in.** Draft pull request #1524 defines a version 2 leaf header of its own. The next item records the conflict. +- **Tracked in.** Each change has its own item below. Draft pull request #1524, which defined a version 2 leaf header of its own, was closed on 12 November 2026. #### Context parts as key request items @@ -1305,19 +1311,17 @@ has the design. logs it and cannot parse it. Items are structured. They have no size limit. Claims use the same channel, so a scope, a binding, and a claim travel the same way. -- **Conflict.** Draft pull request #1524, from the key-vendor work on - `main`, defines its own version 2 leaf. It starts with `0x02`, then the - key backend, the key mode, the keyset ID, the IV, the tag, and the - ciphertext. Two - changes cannot both own `0x02`. Either they merge into one version 2 - header that carries both, or one takes the next byte. Decide with the - key-vendor work before either lands. +- **No conflict over `0x02` now.** Draft pull request #1524, from the + key-vendor work, defined its own version 2 leaf. It was closed on + 12 November 2026, when the key-vendor work dropped the version 2 leaf: + every leaf stays version 1, and the key router sends a leaf to its + backend by its keyset ID. So this item can take `0x02`. - **Stored format.** Version 1 values keep the rc.3 descriptor rendering. That rendering stays in the engine until the last such value is gone. The change is additive: nothing written by rc.3 is re-encrypted. - **Done already.** `SealedValue` has the version byte, bound into the leaf AAD, and the rendering of a context into a descriptor is frozen and documented in `packages/stack-encrypt/src/descriptor.rs`. -- **Tracked in.** No issue. Related: issue #1067, open, says Stack Encrypt never sends key request items to ZeroKMS. Issue #1487 is open. Draft pull request #1524 claims the same version byte, `0x02`. +- **Tracked in.** Issue #1566 is open. Related: issue #1067, open, says Stack Encrypt never sends key request items to ZeroKMS. Issue #1487 is closed. - **Finished when** these are all true: - a version 2 value round-trips, and a version 1 value from the rc.3 vectors still decrypts; @@ -1353,9 +1357,10 @@ has the design. already a dependency, for its exact guarantees and whether any of them apply to the blob codec. Its guarantees for encoding bytes do not extend to arbitrary objects. -- **Done already.** Go writes and reads the format (#1369, #1370). #1503 - adds `OpaqueBlob`, and is in review. -- **Tracked in.** Issue #1484 is open. It closes when #1503 merges. Issues #1367 and #1113 are closed. Pull request #1503 is open. Pull requests #1369 and #1370 are merged. Pull request #1506 adds the Rust and Go exchange of sealed blobs. +- **Done.** Go writes and reads the format (#1369, #1370). `OpaqueBlob` + is merged (#1503), and the Rust and Go exchange of sealed blobs is + merged (#1506). +- **Tracked in.** Issues #1484, #1367 and #1113 are closed. Pull requests #1369, #1370, #1503 and #1506 are merged. - **Finished when** Rust and Go each open blobs that the other wrote, and the conformance profiles cover it. @@ -1392,7 +1397,7 @@ labels](2026-11-08-stack-encrypt-key-policy.md#labels) has the design. section records the thinking. - **Done already.** `Ciphertext::key_tag` reads the tag in Rust (#1404). The leaf AAD already binds the original tag. -- **Tracked in.** No issue. Related: issue #1067, open. +- **Tracked in.** Issue #1567 is open. Related: issue #1067, open, and issue #1485, the key header. - **Finished when** these are all true: - the example in the key policy document round-trips through a trigger, in Rust and in Go; @@ -1467,11 +1472,12 @@ Base64. - **Why.** The derives give the [record example](#encrypt-a-record). The encrypted struct is what goes in the row, so a developer reads the stored shape, and adds `Serialize` or `sqlx` derives, in one place. -- **Done already.** One value (#1404) and collections (#1501). #1504 adds - the record derives, and is in review. `#[stash(from = "..")]` already - renames the source field of an encrypted field +- **Done.** One value (#1404), collections (#1501), and the record + derives (#1504), which emit the combinators (ADR-0007, amended + 9 November 2026) and land `scope = ".."` and `DeclaredScopes`. + `#[stash(from = "..")]` renames the source field of an encrypted field (`stack-encrypt-derive/src/attrs.rs`). -- **Tracked in.** Issue #1483 is open. It closes when #1504 merges. Issue #1482 is still open although #1501 merged with "Closes #1482". It needs a manual close. Pull request #1504 is open. Pull requests #1404 and #1501 are merged. Pull request #1504 lands `scope = ".."` and `DeclaredScopes`, the names the naming review chose. Its record derives emit the combinators (ADR-0007, amended 9 November 2026). +- **Tracked in.** Issues #1482 and #1483 are closed. Pull requests #1404, #1501 and #1504 are merged. - **Finished when** these are both true: - a mixed record, with passthrough and encrypted fields, and a searchable record round-trip as tests; @@ -1511,16 +1517,16 @@ Base64. - `JsonTreeDocument` implements `EncryptTarget` and `DecryptSource`. - A query from a field's marker works for the equality and match - types, with the record's scope. A plan's query for an EQL type takes - a scope too, through the plan chain that #1488 removes. The order - types do not implement `Answers` yet, so a marker cannot query them. + types, with the record's scope. A data plan's query for an EQL type + takes a scope too. The order types do not implement `Answers` yet, so + a marker cannot query them (#1668). - A data plan that names an EQL type takes a scope and a binding too. `TargetResolver` receives them after the field's label, and `TargetError::Extended` is removed. Under the same scope and binding, a data plan and a derived record write the same terms, and each opens the other's values. A Go struct with an `encrypt_into` field seals through `cipher.Scope(..).Bind(..)`. -- **Tracked in.** Issue #1658. `eql-bindings` builds against the crates.io `stack-encrypt`, so a `stack-encrypt` release with `ContextFromField` must come before the `eql-bindings` release that uses it. +- **Tracked in.** Issue #1658 is open. Pull request #1659, open, closes it. Issue #1668 is open for order queries from a marker. `eql-bindings` builds against the crates.io `stack-encrypt`, so a `stack-encrypt` release with `ContextFromField` must come before the `eql-bindings` release that uses it. - **Finished when** these are all true: - a Rust record with EQL fields encrypts, decrypts, and queries through the new API, including order queries; @@ -1538,8 +1544,10 @@ Base64. - **Done already.** The functions it calls exist in `eql-bindings`: `encrypt_with`, `decrypt`, and `query` in `v4::targets` and `encryption::targets`. -- **Tracked in.** #1571. Done: the resolver is - `eql_bindings::encryption::EqlTargets`. +- **Done.** The resolver is `eql_bindings::encryption::EqlTargets`, on + `next`. The EQL tests and the Go guest use it, and no other + `TargetResolver` impl remains. +- **Tracked in.** Issue #1571 is still open, and can be closed. - **Finished when** the tests and the Go guest use the resolver from `eql-bindings`, and their own copies are deleted. @@ -1560,10 +1568,12 @@ Base64. needs the dynamic interface for element match, predicates, and children. Go already has typed methods for these. - **Done already.** The input types and their parsing rules, as private - types in `stack_encrypt::dynamic::json_query`. Typed containment and - comparison at a path, through a plan (`op::Contains`, `op::AtPath`). The - EQL query types exist in `eql-bindings`. -- **Tracked in.** Issue #1459 is open. It is exactly this item. Issue #1486 is open. + types in `stack_encrypt::dynamic::json_query`. The EQL query types + exist in `eql-bindings`. Typed containment and comparison at a path + went through the chain (`op::Contains`, `op::AtPath`), which #1666 + removes, so until this item is built every JSON query from Rust goes + through a query plan. +- **Tracked in.** Issue #1459 is open. It is exactly this item. Issue #1677 is open for opening one child. Issue #1486 is closed. - **Finished when** these are both true: - the [JSON query example](#query-a-json-document) compiles; - an order query on a path the options do not order is refused before @@ -1594,8 +1604,9 @@ review. `tests/ui/marker_*.rs`). Pull request #1599 gives a field of several indexes, a nullable field and a record field their operator methods, and a field its query type (`#[stash(query = ..)]`). The chain query - `cipher.query(op, value).on(&plan)` still exists until #1488 removes - it. + `cipher.query(op, value).on(&plan)` is removed (#1662). It was the only + typed JSON query, so until the JSON marker methods exist (#1459) a JSON + query goes through a query plan. - **No hand-built plan.** The review of 7 November 2026 gave a hand-built plan the same markers, through `Plan::::scope(..)` with `pick!`. That is withdrawn. No Rust program builds a plan by hand. @@ -1604,12 +1615,12 @@ review. - **Cannot fail.** The derive sees every field, so it refuses a repeated field name or a shared name scope at compile time. A typed index set that repeats an index, `(Equality, Equality)`, is refused at compile - time too. Today the builder refuses it at run time, so that check - lands with the builder's removal. The dynamic grammar keeps its - run-time checks. -- **Tracked in.** Issue #1486 is open; pull request #1595 closes it. - Issue #1596, the operator methods the markers lacked, is closed by - pull request #1599. + time too. The builder refused it at run time. With the builder gone + (#1662), `Encrypted` and a derived `plaintext` record refuse it + at run time, before any key is requested. The compile-time check is + not built (#1670). The dynamic grammar keeps its run-time checks. +- **Tracked in.** Issues #1486 and #1596 are closed: pull requests #1595 + and #1599 are merged. Issue #1670 is open for the compile-time check. - **Finished when** these are all true: - the [marker example](#query-a-record-field) compiles, with the index's query term as the output type; the EQL query types come @@ -1652,16 +1663,16 @@ Decided on 7 November 2026, as area one of the query design review. form, because its value is plaintext. It is never logged or written to disk. Its shape is not part of the stored format. - **Done already.** `Planned`, the query plan, the field handle, - `QueryTerm` and `contexts()` on a `Plan` and a `Planned` are on pull - request #1595 (`src/dynamic/planned.rs`, `tests/planned.rs`). `Planned` calls `encrypt_with`, `query_term` and - `decrypt_with` in `dynamic::record` and `dynamic::query`, under the - plan extended by the caller's parts. The guest still sends the engine - a plan frame, then a value frame for each call. The serialised - `Planned` names and groups what those functions already receive, and - is not built yet. -- **Tracked in.** Issue #1486 is open, in part. Issue #1594 is the - serialised `Planned`, the Go module sending it, and the corpus - rewrite, split from pull request #1595. On the Go side, issue #1116 is open to hide or rename `record.Plan`. Issue #1307 is open for a Go plan-building API, for types `stashgen` cannot tag. It builds a data plan, so the Rust builder's removal does not affect it. + `QueryTerm` and `contexts()` on a `Plan` and a `Planned` (#1595, + merged: `src/dynamic/planned.rs`, `tests/planned.rs`). The chain and + the builder are removed (#1666). `Planned` calls `encrypt_with`, + `query_term` and `decrypt_with` in `dynamic::record` and + `dynamic::query`, under the plan extended by the caller's parts; the + reverse, those functions building a `Planned`, is not done (#1669). + The guest still sends the engine a plan frame, then a value frame for + each call. The serialised `Planned` names and groups what those + functions already receive, and is not built yet. +- **Tracked in.** Issue #1594 is open: the serialised `Planned`, the Go module sending it, and the corpus rewrite. Issue #1669 is open. Issue #1486 is closed. On the Go side, issue #1116 is open to hide or rename `record.Plan`. Issue #1307 is open for a Go plan-building API, for types `stashgen` cannot tag. It builds a data plan, so the Rust builder's removal does not affect it. - **Finished when** these are all true: - every case in the conformance corpus is a serialised `Plan` or `Planned` with the engine's expected answer beside it; @@ -1698,9 +1709,10 @@ Decided on 7 November 2026, as area one of the query design review. could open without the plaintext. - **Done already.** The order in which parts are joined is fixed and tested in Rust and Go (#1491). The context inputs `RequiredContext`, - `NoCallerContext`, and `OptionalContext` exist (#1404). `ExpectedContext` exists on `next`, in `target/context.rs`. #1504 adds - `DeclaredScopes`, makes `ExpectedContext` a context input, and is in - review. + `NoCallerContext`, and `OptionalContext` exist (#1404). #1504 added + `DeclaredScopes` and made `ExpectedContext` a context input, and #1612 + hid `ExpectedContext` from the API. Scopes and bindings are separate + kinds of part (#1487). The naming review of 8 November 2026 decided that `ContextExtension` goes: the `Scope::new(..).bind(..)` chain is the input. The plan builder's `.context(..)` and `.context_field(..)` and the derive's @@ -1712,7 +1724,7 @@ Decided on 7 November 2026, as area one of the query design review. default stays "any", and the Go generator requires a declared part at compile time. `RequiredContext`, `OptionalContext`, and `NoCallerContext` (#1404) are the three states this keeps. -- **Tracked in.** Issue #1487 is open. Issue #1120 is open: a second `.extend()` replaces the first. It closes when the chain goes. Issue #1308 is open for Go identity independent of the column. Issue #1475 is closed. Pull request #1491 is merged. Pull request #1504 is open. +- **Tracked in.** Issue #1308 is open for Go identity independent of the column. Issues #1120, #1475 and #1487 are closed. Pull requests #1491 and #1504 are merged. - **Finished when** these are both true: - all eight scenarios run as tests in Rust and Go, on both paths: the derive, and `Planned`; @@ -1735,8 +1747,10 @@ Decided on 7 November 2026, as area one of the query design review. to Go. The context contract and the conformance README say "scope" instead of "extension". The frozen vectors do not change, because no key on the wire names the operation. -- **Done already.** Nothing. Today every part is in both contexts. -- **Tracked in.** Issue #1487 is open. It is exactly this item. Its pull request adds the binding kind to the derive, the dynamic grammar, and Go, and the `record-bindings-v1` conformance profile. Issue #1067 is open and related. +- **Done.** Issue #1487 added the binding kind to the derive, the + dynamic grammar, and Go, and the `record-bindings-v1` conformance + profile. +- **Tracked in.** Issue #1487 is closed. Issue #1067 is open and related. - **Finished when** these are all true, in Rust and in Go: - a record with a binding row ID encrypts; - a column query finds it; @@ -1760,8 +1774,9 @@ records only the work. does today. It stops being a public type. That last step is done (#1612): the type is hidden from the documentation and from compile errors, and is public only so that code the derives emit can name it. - A caller passes a `NonEmpty` or nothing. The rest of this item is - not done. + A caller passes a `NonEmpty` or nothing. The chain is removed + (#1662): a data plan runs as `Planned`, and a typed record runs through + the derives and its field markers. - **Why.** The typed API already pairs its entry points (#1404). A value for the context reads the same on both layers. A builder state for the context, and a part added later in a chain, are the mistakes the context @@ -1780,7 +1795,7 @@ records only the work. the Go guest builds one from its keyset selector. A `Keyset` reference refuses a foreign leaf with `ForeignKeyset`. A `Cipher` reference opens leaves from any keyset the client has. Done, in the second pull request for #1488. -- **Tracked in.** Issue #1488 is open. It is the chain removal. Issue #1120 is closed. Pull request #1504 is merged and added the record pairs. +- **Tracked in.** Issue #1488 is open. Its sub-issue #1662 is the chain removal, done in pull request #1666. Issue #1120 is closed. Pull request #1504 is merged and added the record pairs. - **Finished when** these are all true: - the examples in [Target state](#target-state) compile; - the compile-fail cases in `tests/ui` cover a binding in a query, and @@ -1817,14 +1832,10 @@ Decided on 7 November 2026. context matches nothing and gives no error. A developer who cannot see the context cannot find the mistake. A test that cannot state the context cannot catch a change to it. -- **Done already.** `Descriptor` renders a context as text, and - `ContextMismatch` reports the stored descriptor. Neither shows the - types, and neither is reachable from a plan or a call. -- **Tracked in.** Issue #1568, with the two items below. Its first pull - request lands `contexts()`, `context()`, `assert_context!`, and the Go - `encrypttest` package for the declarations that exist today. The - scenarios that need `scope(..)` and `bind(..)` land with #1487, and the - field-marker form with #1486. +- **Done.** `contexts()`, `context()`, `assert_context!`, and the Go + `encrypttest` package (#1568), the scenarios that need `scope(..)` and + `bind(..)` (#1487), and the field-marker form (#1486). +- **Tracked in.** Issues #1568, #1487 and #1486 are closed. - **Finished when** these are all true: - a test pins the output of `contexts()` for each of the [context scenarios](2026-10-24-stack-encrypt-context-examples.md); @@ -1851,7 +1862,7 @@ not change the stored format. Renaming Vitamin C's `IntoAad` trait is a follow-up in Vitamin C. The Go SDK and the dynamic interface have no public parameter named `aad`. -- **Tracked in.** Issue #1568. +- **Done** (#1568, closed). #### Pass context by reference @@ -1865,10 +1876,10 @@ parameter named `aad`. the old context path type that [Apply the naming review](#apply-the-naming-review) removes. The context inputs, such as `RequiredContext`, still need an owned value. -- **Tracked in.** Issue #1568. A stored EQL type still takes its - `NonEmpty` by value: Rust's orphan rule keeps +- **Tracked in.** Issue #1568 is closed. A stored EQL type still takes + its `NonEmpty` by value: Rust's orphan rule keeps `From<&NonEmpty>` for `NonEmpty` out of this repository, so that - impl is a follow-up in Vitamin C. + impl is a follow-up in Vitamin C, tracked in issue #1684. - **Finished when** the examples in this plan pass a context by reference, without moving it, and compile. The parts are cloned into the call. @@ -1885,7 +1896,7 @@ parameter named `aad`. `'static`, so `&str` does not meet it. A borrowed value needs its own bound, and the request borrows the value for as long as it runs. - **Done already.** Owned single values (#1404). -- **Tracked in.** #1569. Issue #1468, open, a Vitamin C buffer type, is adjacent. +- **Tracked in.** Issue #1569 is open. It also covers the newtype derive below. Issue #1468, a Vitamin C buffer type, is closed. - **Finished when** `keyset.encrypt("secret")` compiles, and a test shows that Stack Encrypt wipes its own copies of a borrowed value. @@ -1903,13 +1914,13 @@ parameter named `aad`. stops this repository adding that, so a wrapper is needed. A program that already has a `serde_json::Value` must not have to print it and parse it again. -- **Done already.** `stack_encrypt::dynamic::json::JsonPlaintext` wraps a - `serde_json::Value`, and wipes it when it is dropped. It is part of the - dynamic interface. It has only `value()`. The strict parser exists as - `stack_encrypt::json::parse_text`. Every JSON target encrypts from a - `JsonPlaintext` or decrypts to one through the new traits: the - document, one entry of it, and the rows of a read (#1658, #1660). -- **Tracked in.** No issue. Pull request #1504 names `serde_json::Value` not being `Zeroize` as what blocks configured nested targets. This item unblocks them. +- **Done already.** `stack_encrypt::json::JsonPlaintext` is public on + `next` (#1636), with `From`, `parse(&str)` and + `value()`, and wipes the value when it is dropped. Every JSON target + encrypts from a `JsonPlaintext` or decrypts to one through the new + traits: the document, one entry of it, and the rows of a read (#1658, + #1660). Encrypting from a borrowed `&serde_json::Value` is not built. +- **Tracked in.** Issue #1636 is closed. Issue #1569 is open for borrowed values, including `&serde_json::Value`. - **Finished when** the JSON examples in this plan compile, and a test shows that a dropped `JsonPlaintext` leaves no plaintext behind. @@ -1932,7 +1943,7 @@ parameter named `aad`. - **Done already.** Today's `StackCipher::keyset` takes `impl Into`, and `IdentifiedBy` implements `From` and `From`. -- **Tracked in.** No GitHub issue. The `cipherstash-suite` repository has issues disabled. It is where `zerokms-protocol` lives. +- **Tracked in.** Issue #1675 is open. The `cipherstash-suite` repository, where `zerokms-protocol` lives, has issues disabled, so the issue is here. - **Finished when** a released `zerokms-protocol` has both constructors, and the examples in this plan compile. @@ -1947,7 +1958,7 @@ parameter named `aad`. sequence whose order matters, such as the steps of a workflow. ADR-0010 says this is opt-in, by type. It does not exist yet. - **Done already.** Nothing. The default collection (#1501). -- **Tracked in.** No issue. +- **Tracked in.** Issue #1570 is open. - **Finished when** a test shows that each of swapping, dropping, and adding an element fails to open, in Rust and in Go. @@ -1965,7 +1976,7 @@ parameter named `aad`. - **Done already.** `OpaqueEncode` and `OpaqueDecode` already accept any type that meets those bounds. Vitamin C documents a hand-written `Blob(Vec)` newtype. -- **Tracked in.** No issue. +- **Tracked in.** Issue #1569 is open. It covers the newtype case. - **Finished when** the [byte example](#encrypt-a-byte-string) compiles with the derive and no hand-written implementation. @@ -1998,9 +2009,12 @@ table. - **Why.** The plan used placeholders so that the review could change a name without changing a design. The review is done, so the code can follow. -- **Done already.** Nothing. Every name above is still the old one on - `next`. -- **Tracked in.** Issue #1489 is open for naming and packaging. Issues #1116 and #1307 are open for the Go names. Pull request #1504 is open. It lands `scope = ".."` and `DeclaredScopes`, as the review decided. +- **Done already.** `OpSpec` is `Op`. `CipherScope` and `dynamic::Scope` + are `CipherRef`. `scope = ".."` replaced `context = ".."` (#1504). + `ContextExtension` is gone. The rest are still the old names: + `StackCipher`, `KeysetCipher`, the Go `Client` and `Cipher`, `Label`, + a public `Request`, `op::QueryTerm` and `LabelledTarget`. +- **Tracked in.** Issue #1489 is open for naming and packaging. Issue #1663 is open for `Label`, and on hold: the context naming work may give the declared path, which `Label` holds today, a new name and mode, so it is not renamed or removed until that settles. Issues #1116 and #1307 are open for the Go names. Pull request #1504 is merged. - **Finished when** no old name remains in the public API, the examples in [Target state](#target-state) compile under the new names, and the packaging glossary matches. @@ -2021,27 +2035,32 @@ table. The removal of `Label` is also listed in [Apply the naming review](#apply-the-naming-review); it happens once, here. - **Why.** Two APIs for the same job make the right one hard to find. -- **Done already.** The new API exists beside the old one. The first +- **Done already.** The first pull request for #1488 makes `Planned` the one lowering of a data plan: `dynamic::planned::compose` and `opening` build each field's combinator from the parsed plan, and `encrypt_with`, `decrypt_with`, `contexts()` and the checks read them. The dynamic path no longer uses the plan builder. Those functions do not yet build a `Planned`: they run the - lowering themselves. That inversion, the builder, the chain, the old - traits and `Label` are still to be removed. The helpers in - `target::legacy` are removed, in the fourth pull request for #1488. - Their callers use `encrypt_as` and `decrypt_as` until those go too. + lowering themselves. That inversion and `Label` are still to be + removed. The helpers in `target::legacy` are removed, in the fourth pull + request for #1488. The builder, the chain, `EncryptFrom`, `DecryptInto`, + their derives, `encrypt_as` and `decrypt_as` are removed (#1661, #1662). + EQL, the Go guest, the examples and every test use the owned API, field + markers or `Planned`. Every JSON target has the new traits over `JsonPlaintext` (#1658 for `JsonTreeDocument`, #1660 for the other five), so no target needs the old traits any more. - **Tracked in.** Issue #1488 is open. Its first pull request is the lowering. Its second replaces `CipherScope` and `dynamic::Scope` with - `CipherRef`. Its third hides `ExpectedContext`. Its fourth removes - `target::legacy`. The other removals are its sub-issues, in this - order: #1660 gives the remaining JSON targets the new traits, #1662 - removes the chain and the builder, #1661 removes `EncryptFrom`, - `DecryptInto`, `encrypt_as` and `decrypt_as`, and #1663 removes `Label` - and `IntoLabel`. + `CipherRef`. Its third hides `ExpectedContext` (#1612, merged). Its + fourth removes `target::legacy` (#1613, open). The other removals are + its sub-issues, in this order: #1660 gives the remaining JSON targets + the new traits (#1664, open); #1662 removes the chain and the builder, + and #1661 removes `EncryptFrom`, `DecryptInto`, `encrypt_as` and + `decrypt_as` (both in #1666, open); #1663 removes `Label` and + `IntoLabel`, on hold until the context naming work settles what the + declared path is called; and #1669 makes `encrypt_with` and `decrypt_with` build a + `Planned`. - **Finished when** the old API is gone, and every consumer uses the new API. @@ -2053,7 +2072,7 @@ table. - **Why.** Plaintext must not stay in freed memory. - **Done already.** Collections wipe the values decoded before a failure. Cancellation and drop are tested (#1493, #1501). -- **Tracked in.** Issue #1468 is open: a Vitamin C buffer type. Issue #1458 is open: data keys by reference. Issue #1480 is closed. Pull request #1503 is open. A decrypted `OpaqueBlob` wipes its value when it is dropped, and its `into_inner` returns a `Zeroizing`. The pull request records the partial Serde decode as a known gap. A drop-wiping adapter for a single value, such as `Zeroizing`, needs Vitamin C to implement `FromScalar` for `Zeroizing`. Pull requests #1493 and #1501 are merged. +- **Tracked in.** Issue #1676 is open for the partial Serde decode, which pull request #1503 recorded as a known gap. Issue #1684 is open for the drop-wiping adapter for a single value, such as `Zeroizing`, which needs Vitamin C to implement `FromScalar` for `Zeroizing`. Issue #1458 is open: data keys by reference. Issues #1468 and #1480 are closed. Pull requests #1493, #1501 and #1503 are merged; a decrypted `OpaqueBlob` wipes its value when it is dropped, and its `into_inner` returns a `Zeroizing`. - **Finished when** the conformance profile for the lifecycle covers both cases. ### 4. The query engine @@ -2080,7 +2099,7 @@ Decided on 7 November 2026, as area two of the query design review. does not know which bound the caller meant. The `InvertedRange` error asked for in #1279 predates that commit and is dropped. - **Stored format and wire.** Unchanged. A query sends the operator's wire name. That name does not change. -- **Tracked in.** The pull request for #1279, #1318, #1317, #1321 and #1233 does this area's `Op` enum and the `Matches` case change. Issues #1319 and #1321 were open; #1321 closes with that pull request. Issue #1320, the inverted-range rule, is closed: this plan's decision superseded it. +- **Tracked in.** Issues #1279, #1318, #1317 and #1233 are closed: their pull request made `Op` and removed `Matches`'s case parameter. Issue #1321 is still open and may already be fixed by it. Issue #1319 is open. Issue #1320, the inverted-range rule, is closed: this plan's decision superseded it. - **Finished when** these are all true: - the dynamic enum is generated from the `op` types; - `Matches` has no type parameter; @@ -2135,7 +2154,7 @@ design is the query artifact's; the adjustments below are the plan's. 7 November 2026. - **Breaking.** Every hand-written `MatchConfig` changes shape. This lands as a `!` commit with a changelog entry. -- **Tracked in.** Issues #1319, #1112, #1398, #1399, and #1299 are open. Issue #1309 is closed. Pull request #1397 is merged. +- **Tracked in.** Issue #1673 is open for the analyzer. Issues #1319, #1112, #1398, #1399, and #1299 are open. Issue #1309 is closed. Pull request #1397 is merged. - **Finished when** these are all true: - the conformance case for the rc.3 mapping passes in Rust and Go; - a `tests/ui` case shows a stemmer after n-grams fails to compile; @@ -2209,7 +2228,7 @@ review. The rules are the query artifact's. `.via`. The `MatchConfig` documentation says a field and its query agree "by construction". That is true only for one-value plans today. Both are amended, not contradicted in silence. -- **Tracked in.** Steps 1 and 2 are one pull request, which closes #1279, #1318, #1317, #1321 and #1233 (rule 2, the second order index). It brings `prepare`, the `sem::query` rules and `Op`, and drops `Matches`'s case parameter; the analyzer of "Match configuration: one analyzer" is its own work (#1319). Steps 3 and 4 are #1486 and #1488. +- **Tracked in.** Steps 1 and 2 are done: #1279, #1318, #1317 and #1233 are closed. They brought `prepare`, the `sem::query` rules and `Op`, and dropped `Matches`'s case parameter. The analyzer of "Match configuration: one analyzer" is its own work (#1673). Step 3 is done apart from its clean-up: the markers and `Planned` (#1486, closed), and the removal of `DefaultIndex` and `.via` (#1666). Issue #1671 is open to remove `PlanError::IndexOptions` and the other variants nothing raises, and to amend ADR-0009 and the `MatchConfig` documentation. Step 4 is issue #1672, open. - **Finished when** these are all true: - every index's operators appear in one declaration, and a test shows the typed and dynamic lookups agree on every index; @@ -2242,7 +2261,7 @@ internal adapter in some other way. Decide this separately from the [retirement of `AsyncCipher`](2026-10-25-stack-encrypt-packaging.md#retire-asynccipher) in the packaging document. -- **Tracked in.** No issue. Draft pull request #1522 reworks the stack-kms key source traits. It is adjacent. +- **Tracked in.** Issue #1489 is open, and asks how to split it. Draft pull request #1522, which reworked the stack-kms key source traits, was closed. #### Choose the keyset from the data @@ -2280,7 +2299,7 @@ Rules for the proposal: - If the caller passes a keyset and the record selects one, the two must match, or the call is refused. Neither wins silently. -- **Tracked in.** No Rust issue. Related open issues: #1048, the Go eager keyset; #735, a keyset mismatch is silently unsearchable; #561, a per-operation keyset in TypeScript; #1312, Go control-plane keysets. Issue #832 is closed. Pull request #1407, open, is the Go control SDK. +- **Tracked in.** Issue #1572 is open. Related open issues: #1048, the Go eager keyset; #735, a keyset mismatch is silently unsearchable; #561, a per-operation keyset in TypeScript; #1312, Go control-plane keysets. Issue #832 is closed. Pull request #1407, open, is the Go control SDK. #### Claims the key provider resolves @@ -2320,7 +2339,7 @@ let encrypted: EncryptedNote = note.encrypt_into(&keyset).await?; let note = Note::decrypt_from(encrypted, &keyset).await?; ``` -- **Tracked in.** Issue #1565 is open. Issue #1067 is open: Stack Encrypt never sends key request items to ZeroKMS. That is the prerequisite. Issue #1045 is closed. +- **Tracked in.** Issue #1683 is open. Issue #1566, context parts as key request items, is the prerequisite, with issue #1067. Issue #1565 is open for the TypeScript entry. Issue #1045 is closed. ## Open questions and decisions to be made @@ -2331,7 +2350,7 @@ The lead who approves this plan decides each question. | For each change that would alter the stored format, should it keep the rc.3 format or add a new version? | Keep rc.3 data readable and searchable. Or add a version beside it | Decide for each change when it is designed | | Should a record choose its keyset from a plaintext field? | The declaration in [Choose the keyset from the data](#choose-the-keyset-from-the-data). Or the caller keeps resolving the keyset by hand | The declaration | | Should this plan ask ZeroKMS version 2 to derive the data key over the key request items, not only the descriptor? | Yes, so a bind or a claim is enforced by derivation. Or keep the tag check as the only enforcement. The version 2 work decides | Yes. The [key policy document](2026-11-08-stack-encrypt-key-policy.md#what-zerokms-must-do) asks for it | -| Which change owns envelope version `0x02`: this plan's key request items, or #1524's key backend header? | One version 2 header that carries both, shared with the EQL key header `h`. Or separate bytes | One header. Decide with the key-vendor work before either lands | +| Which change owns envelope version `0x02`: this plan's key request items, or #1524's key backend header? | One version 2 header that carries both, shared with the EQL key header `h`. Or separate bytes | Settled: #1524 was closed on 12 November 2026 and the key-vendor work keeps every leaf at version 1, so key request items (#1566) take `0x02` | | What privacy claim can be made for match terms of short words? | Accept what they reveal, and document it. Or change how short words are stored | No recommendation yet. Merging #1397 did not show that they reveal nothing new | | Which Vitamin C crate should own `Base64Text` and `Base85Text`? | A new Vitamin C types crate that also holds key types. Or a small encoding crate | Vitamin C owns them, decided by Dan on 8 November 2026. The crate is to be confirmed, probably the types crate | | What crate layout should the final API use? | The proposal in the packaging document | Review the layout proposal when the crate moves begin. The names are decided | diff --git a/docs/target-directed-encryption.md b/docs/target-directed-encryption.md index b60f9359b6..a96a5aed0e 100644 --- a/docs/target-directed-encryption.md +++ b/docs/target-directed-encryption.md @@ -1,6 +1,7 @@ # Target-directed encryption **Status:** design record, reconciled with the shipped API on 2026-08-28 and again on 2026-09-04 (contexts are vitaminc's `NonEmpty`; a caller's context *extends* a field's own; `row = ..` became `struct = ..`, and `from` exists only there). The snippets below are the API as it ships in `stack-encrypt` (cipherstash-suite #2146, #2147); the async shape is specified by [RFC 0002](rfcs/0002-async-shape-for-target-directed-encryption.md). +**Superseded in part:** the snippets record the design as it first shipped. Since then `EncryptFrom`, `DecryptInto`, `encrypt_as`, `decrypt_as`, the plan builder and the call chain were removed. The API that ships now is `EncryptTarget` and `DecryptSource` with `encrypt_into` and `decrypt_from`, field markers for queries, and `dynamic::Planned` for a declaration that is data. See the crate docs and `packages/stack-encrypt/CHANGELOG.md`. **Date:** 2026-08-21 **Scope:** vitaminc (primitives), stack-encrypt (the new trait + batching), eql-bindings (one class of targets) diff --git a/languages/golang/encrypt/context_field_test.go b/languages/golang/encrypt/context_field_test.go index e9d2ee34ae..e8e2b0e65f 100644 --- a/languages/golang/encrypt/context_field_test.go +++ b/languages/golang/encrypt/context_field_test.go @@ -150,7 +150,7 @@ func TestContextFieldQueriesNameTheContext(t *testing.T) { } } -// The context-field case of the Rust record fixture: the typed chain and +// The context-field case of the Rust record fixture: the typed API and // the data-plan lowering each sealed a Note under the deterministic source, // and the generated code opens both under the context they store, refuses // both under another, and derives the same term. diff --git a/languages/golang/encrypt/guest/src/abi.rs b/languages/golang/encrypt/guest/src/abi.rs index 9abc6e1648..09fa1f37ca 100644 --- a/languages/golang/encrypt/guest/src/abi.rs +++ b/languages/golang/encrypt/guest/src/abi.rs @@ -431,7 +431,7 @@ pub unsafe extern "C" fn se_term( } /// Encrypt a record (or a batch) per a plan under the keyset `opts` selects -/// — the runtime form of `#[derive(EncryptFrom)]`; see +/// — the runtime form of `#[derive(EncryptTarget)]`; see /// [`ops::encrypt_record`] for the source, plan, and result encodings. All /// rows and fields seal from **one** batched key request regardless of row /// count — dispatched as one `generate-data-key` call per 500 keyed leaves, diff --git a/languages/golang/encrypt/guest/src/ops.rs b/languages/golang/encrypt/guest/src/ops.rs index 0fc5d2200c..14ffe2e751 100644 --- a/languages/golang/encrypt/guest/src/ops.rs +++ b/languages/golang/encrypt/guest/src/ops.rs @@ -170,7 +170,7 @@ fn parse_term(value: Frame<'_>, kind: u32) -> Result { /// result is a codec-encoded ciphertext tree — per record a map of /// `field → { output-key → node }`. /// -/// The plan is lowered into the engine's plan builder and run there +/// The plan is lowered into the engine's combinators and run there /// (ADR-0007): [`dynamic::record::encrypt`] hands back the plan's `Pending` /// with every key request queued and nothing sent, and awaiting it here is /// the one batched `generate_keys` for all rows and fields. That batch diff --git a/languages/golang/encrypt/query_refusal_conformance_test.go b/languages/golang/encrypt/query_refusal_conformance_test.go index 8d15d13481..08980b71e0 100644 --- a/languages/golang/encrypt/query_refusal_conformance_test.go +++ b/languages/golang/encrypt/query_refusal_conformance_test.go @@ -121,11 +121,11 @@ func TestQueryRefusalConformance(t *testing.T) { switch c.Typed.Refusal { case "none": if c.ExpectedError != nil { - t.Fatal("the typed chain accepts only what the binding accepts") + t.Fatal("the typed query accepts only what the binding accepts") } case "before_keyset", "after_keyset": if c.ExpectedError == nil || c.ExpectedError.Class != "term" { - t.Fatal("the typed chain's stage is recorded for term refusals alone") + t.Fatal("the typed query's stage is recorded for term refusals alone") } default: t.Fatalf("unknown typed stage %q", c.Typed.Refusal) diff --git a/languages/golang/encrypt/query_test.go b/languages/golang/encrypt/query_test.go index ab5328b31c..345fdafcc2 100644 --- a/languages/golang/encrypt/query_test.go +++ b/languages/golang/encrypt/query_test.go @@ -26,7 +26,7 @@ import ( // The query fixture holds the guest's query exports to the Rust results: // packages/stack-encrypt/tests/fixtures/query_operators.json records, for one // declaration, the term each query by operator derives (each equal to the -// typed Rust query chain's), a page of verified values and a few fetch +// typed Rust query's), a page of verified values and a few fetch // limits. These tests derive, verify and size the same queries through the // deterministic test build of the guest and compare. A missing build skips // ordinary tests, but fails when STACK_CONFORMANCE_REQUIRED=1. diff --git a/packages/eql/AGENTS.md b/packages/eql/AGENTS.md index 2cde32564a..b3c5340dc0 100644 --- a/packages/eql/AGENTS.md +++ b/packages/eql/AGENTS.md @@ -94,7 +94,7 @@ This is the **Encrypt Query Language (EQL)** - a PostgreSQL extension for search Adding a scalar encrypted-domain type is one row in the Rust catalog `eql-domains::CATALOG` (`crates/eql-domains/src/lib.rs`): a `DomainFamily` giving the type `name` (e.g. `bigint`), its `ScalarKind` (the `kind` field), the `Domain`s mapping each generated (bare) domain name to its fixed index `Term`s (`eq => [Hm]`, `ord`/`ord_ore => [Ore]`), and the `Fixture` value list. Term capabilities are fixed in the `Term` enum's `impl` methods (with unit tests): `Hm` provides equality, and `Ore` provides equality plus ordering. There is no TOML manifest and no Python — the catalog is the source of truth, validated by the compiler (an undefined term or unknown scalar is a compile error) plus catalog `#[test]`s. `mise run build` runs `cargo run -p eql-codegen`, which regenerates the scalar SQL surface into `src/v3/scalars//` from `CATALOG` at the start of every build; that surface includes supported comparison wrappers plus blockers for native `json` operators that would otherwise be reachable through domain fallback. `cargo run -p eql-codegen` regenerates every type at once (the same call `mise run build` uses; there is no per-type codegen task). The generated `*_types.sql` / `*_functions.sql` / `*_operators.sql` / `*_aggregates.sql` files are **committed in place** under `src/v3/scalars//` and drift-gated by `mise run codegen:parity` (regenerate in place + `git diff` + untracked check — the same regenerate-and-diff pattern `types:check` uses for the committed bindings). They are still machine-generated: change the catalog and rebuild, never hand-edit (CI fails on drift). The per-type plaintext fixture lists the SQLx matrix consumes are **not** a generated file — they are materialised from each `CATALOG` row at compile time as `eql_domains::INT4_VALUES` / `INT2_VALUES` (the `int_values!` macro) and read directly by `ScalarType::FIXTURE_VALUES`; a Rust source of truth no longer round-trips through a committed generated `.rs`. Generated SQL carries a `-- AUTOMATICALLY GENERATED FILE` header (the project-wide marker `docs:validate` greps on); change the catalog and rebuild, never hand-edit. Hand-written SQL beyond the fixed surface goes in `src/v3/scalars//_extensions.sql` with no auto-generated header and explicit `-- REQUIRE:` edges — that file IS committed. `json`'s **SteVec** domains never enter this pipeline, but its **scalar storage domain does**: `generate_all` iterates `eql_domains::families_with_scalar_domains()` and renders each family's scalar domains (so `public.eql_v3_json` is generated into `src/v3/scalars/json/`), while `list-types` and the SQLx matrix still iterate `eql_domains::scalar_families()`, which excludes the whole non-scalar `json` family — so `json` stays out of the operator/coverage matrix by construction (a storage-only domain carries no operator matrix), even though its ciphertext-only storage domain now has a generated SQL surface. -**EQL v4 bindings:** the generator writes the scalar domains a second time into `crates/eql-bindings/src/v4/` (`eql_v4_*` names, `v: 4` through `SchemaVersion<4>`, an `inventory.rs`), plus the Stack Encrypt target table `v4/targets.rs`. **Only v4 structs derive Stack Encrypt's `EncryptTarget` and `DecryptSource`**: Stack Encrypt's output is not byte-for-byte compatible with cipherstash-client's, so `encryption_gap` refuses every v3 domain and a codegen test asserts no v3 struct derives either. The same derives also implement the older `EncryptFrom` and `DecryptInto`. v4 also has one range query struct per ordered family and order term (`IntegerRangeOreQuery`, `TextRangeOpeQuery`, …: each bound the single-bound domain's `ob` or `op` value, typed as that twin's term newtype, in a `{v, i, lo, hi}` envelope that Stack Encrypt will need to build once the ordered types are producible), listed by `v4::all_range_query()`; v3 has none, matching the SQL. v4 has no SteVec bindings or SQL (its searchable JSON document is the JSON tree family, the target table's `JsonSearch` row), and no payload enums, TypeScript export or published JSON Schema yet. The hand-written `v4/mod.rs` re-exports v3's term newtypes and `DomainType`, so a change to a term's validation in `v3/terms.rs` changes v4 too; the version boundary is the envelope's `SchemaVersion` (one generic type in `lib.rs`; `v3::SchemaVersion` and `v4::SchemaVersion` are `SchemaVersion<3>` and `SchemaVersion<4>`). +**EQL v4 bindings:** the generator writes the scalar domains a second time into `crates/eql-bindings/src/v4/` (`eql_v4_*` names, `v: 4` through `SchemaVersion<4>`, an `inventory.rs`), plus the Stack Encrypt target table `v4/targets.rs`. **Only v4 structs derive Stack Encrypt's `EncryptTarget` and `DecryptSource`**: Stack Encrypt's output is not byte-for-byte compatible with cipherstash-client's, so `encryption_gap` refuses every v3 domain and a codegen test asserts no v3 struct derives either. v4 also has one range query struct per ordered family and order term (`IntegerRangeOreQuery`, `TextRangeOpeQuery`, …: each bound the single-bound domain's `ob` or `op` value, typed as that twin's term newtype, in a `{v, i, lo, hi}` envelope that Stack Encrypt will need to build once the ordered types are producible), listed by `v4::all_range_query()`; v3 has none, matching the SQL. v4 has no SteVec bindings or SQL (its searchable JSON document is the JSON tree family, the target table's `JsonSearch` row), and no payload enums, TypeScript export or published JSON Schema yet. The hand-written `v4/mod.rs` re-exports v3's term newtypes and `DomainType`, so a change to a term's validation in `v3/terms.rs` changes v4 too; the version boundary is the envelope's `SchemaVersion` (one generic type in `lib.rs`; `v3::SchemaVersion` and `v4::SchemaVersion` are `SchemaVersion<3>` and `SchemaVersion<4>`). The same generator also emits the **Rust payload bindings** under `crates/eql-bindings/src/v3/.rs` (structs + `DomainType` impls) and the `inventory.rs` `all()` list, from the same `CATALOG` — committed with a `// @generated` header (the bindings must exist on a clean clone because `ts-rs`/`schemars` derive the committed TypeScript/JSON Schema off them; the SQL surface is now committed too, so both generated targets are handled consistently — committed in place, drift-gated by regenerate-and-`git diff`). The hand-written `DomainType` trait, the shared newtypes (`SchemaVersion`/`Identifier`/`Ciphertext`/`Hmac256`/`OreBlock256`/`BloomFilter`), the `PhantomData` plumbing, and the architectural module doc (including the non-derivable float-NaN and bool storage-only caveats) stay hand-written in `crates/eql-bindings/src/v3/{mod,domain_type,terms}.rs`. Generated structs carry a catalog-derived struct doc — a summary line (`` `eql_v3.` — . ``) plus a detail line listing the supported operators and required payload keys, all derived from data the catalog already holds (the capability label, `Term::operators_for_terms`, and `ENVELOPE_KEYS` ++ `Term::term_json_keys` — see `struct_doc_lines` in `crates/eql-codegen/src/bindings.rs`). The required-key list makes structural distinctions visible — e.g. `text_ord` lists `` `v` `i` `c` `hm` `ob` `` (dual-term) versus an integer `integer_ord`'s `` `v` `i` `c` `ob` ``. There are **no per-field docs**: per-field/term semantics live on the shared term newtypes (`terms.rs`, flowing into the TS term files and JSON Schema `$defs`), and non-derivable per-family caveats (float-NaN, bool storage-only) in `mod.rs`. Free-form prose belongs at the **struct level** (a future optional catalog `doc` field emitted as extra `#[doc]` lines), never as per-field docs. JSON Schemas are emitted by **schemars 1.x** as JSON Schema 2020-12. `mise run types:generate` regenerates the Rust bindings (via `eql-codegen bindings`) then the TS/JSON; `mise run types:check` is the committed-reference drift gate — it regenerates and `git diff`s the Rust bindings (`crates/eql-bindings/src/v3` and `src/v4`), `bindings/` and `schema/`, the same regenerate-and-diff pattern `codegen:parity` now uses for the committed SQL surface. Both gates run in CI; `mise run install-hooks` wires a pre-commit hook that runs both locally whenever a commit touches the catalog, the generator, or a committed generated surface. The `json` family's **SteVec** structs (`SteVecDocument`, `SteVecEntry`, `SteVecQuery`, and `SteVecQueryEntry`) are hand-written for the same reason as their SQL — in `crates/eql-bindings/src/v3/json.rs`, symmetric with `src/v3/json/`. Their direct optional fields and `deny_unknown_fields` validation keep the Rust, TypeScript, JSON Schema, and PostgreSQL entry contracts aligned. The family's scalar storage struct **is** generated (a flat `{v,i,c}` struct, `Json`), via `render_scalar_only_bindings(&JSON)` into `crates/eql-bindings/src/v3/json_storage.rs`; the hand-written `json.rs` re-exports it (`pub use crate::v3::json_storage::Json;`) so `super::json::Json` resolves for the generated inventory/payload. Only inventory membership and ordering are catalog-driven: `inventory.rs::all()` iterates the full `CATALOG` (not `scalar_families()`), branching on `Shape` to include the three SteVec structs plus the generated `Json` alongside the other scalar structs. diff --git a/packages/eql/crates/eql-bindings/CHANGELOG.md b/packages/eql/crates/eql-bindings/CHANGELOG.md index 9227ee70d4..e60b39ee13 100644 --- a/packages/eql/crates/eql-bindings/CHANGELOG.md +++ b/packages/eql/crates/eql-bindings/CHANGELOG.md @@ -18,10 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 opens one entry, and the value is wiped when it is dropped. - The context is required. Pass the document's column. For a document that is a field of a record with scopes or bindings, pass the column, - then those scopes and bindings. The structural `DecryptInto` takes the - column alone, so it cannot open such a document's parts. - - The answers and the refusals are those of the structural - `DecryptInto`, which stays for now. + then those scopes and bindings. - **EQL types as record fields** (#1658). An EQL v4 type can be a field of a Stack Encrypt `#[derive(EncryptTarget, DecryptSource)]` record @@ -61,9 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `DecryptSource`** (#1658). It encrypts from a `JsonPlaintext` under the default options, with the column as its context, and writes the search terms that `JsonTree::default()` writes - for the same document. It can be a record field too. It also - implements `DecryptInto`, the form a record field opens - through, as every other EQL type does. + for the same document. It can be a record field too. - **`Target::leaf_query_sql_domain`** (`targets` feature, #1350): the domain a document type's ordering at a path binds as, @@ -110,11 +105,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 entry carrying another selector (`stack_encrypt::Error::JsonWrongEntry`), and a read's rows through the key that keyed it, which checks each row against the path the same way; `JsonTreeEntry` does not open on its own. - The document is an `EncryptFrom` and `DecryptInto` target, - the extracted entry and the subtree open through `DecryptInto`, and the needle and + The document is an `EncryptTarget` and + `DecryptSource` target, the extracted entry and the + subtree open through `DecryptSource`, and the needle and the ordering operand convert from Stack Encrypt's `JsonQuery` and `JsonLeafQuery`. `JsonTree::new(options)` is the `json_tree` layout under - index options other than the defaults (a `Layout`, so a program + index options other than the defaults (a program seals with `keyset.run(JsonTree::new(options).encryption(), ..)`), such as the paths that carry an ordering term. The wire fields are typed apart from the v3 hex terms, and each names its text encoding: @@ -172,13 +168,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `eql_v4.query__range_` domains in the v4 SQL bundle. No range type derives Stack Encrypt's traits yet, because the ordered types they range over are not producible. There are no v3 range types. -- **Rust queries an EQL v4 field through Stack Encrypt's typed query chain** - (`stack-encrypt` feature, #1238). `cipher.query(op::Eq, v).on(&plan)` on a - plan for `TextEq`, `SmallintEq`, `IntegerEq`, `BigintEq`, `RealEq`, - `DoubleEq`, `DateEq`, `TimestampEq` or `NumericEq` - (`Plan::value::().context("table/column").encrypt_into::()`) - returns the type's query twin (`TextEqQuery`, …), and - `cipher.query(op::Matches, text).on(&plan)` on a plan for `TextMatch` +- **Rust queries an EQL v4 field through a field marker** + (`stack-encrypt` feature, #1238). On a derived record, the marker of a + field typed `TextEq`, `SmallintEq`, `IntegerEq`, `BigintEq`, `RealEq`, + `DoubleEq`, `DateEq`, `TimestampEq` or `NumericEq` has `eq`: + `EncryptedMember::EMAIL.eq(v).encrypt(&keyset)` returns the type's query + twin (`TextEqQuery`, …). The marker of a field typed `TextMatch` has + `matches`, which returns a `MatchTwin`: the twin, which it serializes as, with `verify` and `target_limit` under `EqlMatch`. The query is the bytes the data path (`stack_encrypt::dynamic::query_term`) derives for the same @@ -187,30 +183,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 no `dynamic` layer and so not the `targets` feature, from each type's indexes, by the rule the data path answers an EQL type's operators with. A producible type whose twin carries an order term (`IntegerOrd`, - `TextOrd`, …) answers nothing in the typed chain yet: the chain sends one - index's term as the twin, and those twins need an order term, or several - terms at once. The encryption tests hold that gap to exactly the ordered - types, so query them through the data path for now. An operator a type - does not answer, or another index named with `.via(..)`, does not + `TextOrd`, …) answers nothing from a marker yet: a marker's query sends + one index's term as the twin, and those twins need an order term, or + several terms at once. The encryption tests hold that gap to exactly the + ordered types, so query them through the data path for now. An operator + a type does not answer does not compile. Each answering type is an - `op::LabelledTarget`. The label must name the column, table then column, - or the awaited query is refused before any keyset is resolved, with a - `QueryLabelError` in `Error::Other`. `eql_bindings::encryption::query` holds `TwinIndex` + `op::LabelledTarget`. A marker's label names the column, table then + column. `eql_bindings::encryption::query` holds `TwinIndex` (Stack Encrypt's index, its query sent as the twin, for the twin's own plaintext only), `MatchTwin`, `QueryTwin` and `QueryLabelError`. - `Hmac256` and `BloomFilter` now declare their indexes (`EncryptFrom::indexes`), + `Hmac256` and `BloomFilter` now declare their indexes (`EncryptTarget::indexes`), so a type holding one declares the equality or match index, and `EqlMatch` implements `op::MatchCase` (case-insensitive). - **`eql_bindings::v4` needs the `stack-encrypt` feature.** Only Stack Encrypt produces an EQL v4 value (ADR-0002), so the module is compiled with - that feature alone, and its producible types derive `EncryptFrom` and - `DecryptInto` with no attribute of their own gated on it. + that feature alone, and its producible types derive `EncryptTarget` and + `DecryptSource` with no attribute of their own gated on it. - **A `targets` feature for the by-name dispatch.** `encryption::targets` and `v4::targets`, which name an EQL type at run time and take a `ValueFrame`, need `targets`, which turns on `stack-encrypt` and Stack Encrypt's `dynamic` feature. `stack-encrypt` alone builds Stack Encrypt with no default features and no `dynamic` layer, so a Rust program that encrypts - the v4 types through `encrypt_as` no longer compiles it. + the v4 types through `encrypt_into` no longer compiles it. - **`IntegerEq`, `BigintEq`, `RealEq` and `DoubleEq` are producible** (`stack-encrypt` feature), from `i32`, `i64`, `f32` and `f64`. Their equality term follows ADR-0002's semantic baseline, so `=` compares as PostgreSQL's own integer and `float8 =` do: @@ -245,8 +240,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Encrypt's OPE term over ADR-0002's order key, hex encoded, which EQL compares as bytes: floats order as PostgreSQL's `float8` does, and text by its fold (case and accents folded), so text order types keep `TextEq`'s - equality term for `=`. `OpeCllw` implements `EncryptFrom` for any - orderable plaintext. Their query twins carry one bound, so the engine + equality term for `=`. Their query twins carry one bound, so the engine answers `<`, `<=`, `>` and `>=` (and `=` on text) through them, not `BETWEEN`. - **`TextSearch` is producible** (`stack-encrypt` feature), from `String`. @@ -262,7 +256,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `TextMatch` and every query keep `BloomFilter`, which refuses it. Its query is built per index, through `TextSearchEqualityQuery`, `TextSearchOrderQuery` or - `TextSearchMatchQuery` (each `EncryptFrom` and + `TextSearchMatchQuery` (each `EncryptTarget` and `Into`), so it carries only the term its operator reads: `=` and `<` on two-letter text work, and a match query on it is refused. The by-name dispatch picks the projection for the index it is handed. @@ -271,8 +265,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `DoubleOrdOre` from `i32`, `i16`, `i64`, `f32` and `f64`, and `TextOrdOre` and `TextSearchOre` from `String`. Their `ob` term is Stack Encrypt's block ORE term (ore-rs's 6-bit fixed-width scheme for the numbers, its chained scheme for text): - one element, the hex of the whole ciphertext. `OreBlock256` implements - `EncryptFrom` for any orderable plaintext and never decrypts. This is not + one element, the hex of the whole ciphertext. This is not EQL v3's `ob` (cipherstash-client's 8-bit blocks, up to six per text value), and only EQL v4's comparator, `compare_ore_block_v2_term`, reads it. Every term carries a random nonce, so a query's `ob` never equals the @@ -298,7 +291,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 indexes, query twin, and whether the engine can produce it today with the reason when not — serializable as the guest's `se_targets` export, and `encrypt` / `decrypt` / `query` entry points that dispatch on the name and - run the type's own `EncryptFrom` / `DecryptInto` plan, resolving to the EQL + run the type's own `EncryptTarget` / `DecryptSource`, resolving to the EQL value's JSON bytes through the engine's `Pending`. The plaintext crosses as Stack Encrypt's `dynamic::ValueFrame`, one value as its transport frame: `encrypt` and `query` take one, `decrypt` resolves to one, and the @@ -327,6 +320,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Breaking +- **The EQL v4 types no longer implement `EncryptFrom` and `DecryptInto`** + (#1661, #1662). Stack Encrypt removed those traits, with `encrypt_as`, + `decrypt_as`, the plan builder and the query chain. Every type keeps + `EncryptTarget` and `DecryptSource`. + - Write with `value.encrypt_into_with_context(&keyset, column)` and read + with `P::decrypt_from_with_context(stored, &keyset, column)`, or + `P::decrypt_from(stored, &keyset)` to open under the stored column. + - A typed query is a field marker of a derived record: + `EncryptedMember::EMAIL.eq("bob").encrypt(&keyset)` gives the + `TextEqQuery`. `cipher.query(op::Eq, ..).on(&plan)` and `.via(..)` are + gone. + - A JSON document is a `JsonPlaintext`, not a `serde_json::Value`. + `JsonTreeDocument`, `JsonTreeExtracted` and `JsonTreeSubtree` open to + it. A typed JSON query has no replacement yet: derive the operand + through a data plan that names `JsonSearch` (`EqlTargets`). + - `JsonTree` no longer implements `stack_encrypt::target::Layout`, which + is removed. `JsonTree::encryption()`, `index()` and `specs()` are its + own methods, and `index()` returns the index by value. + - **`v4::text::TextSearchQuery` and `TextSearchOreQuery` hold each term as an `Option`**, left out of the JSON when `None`, to match EQL v4's `query_text_search` and `query_text_search_ore`, which now take any one diff --git a/packages/eql/crates/eql-bindings/Cargo.toml b/packages/eql/crates/eql-bindings/Cargo.toml index f35f84934b..70bf6398b6 100644 --- a/packages/eql/crates/eql-bindings/Cargo.toml +++ b/packages/eql/crates/eql-bindings/Cargo.toml @@ -62,8 +62,7 @@ hex = { version = "0.4", optional = true } default = [] # The EQL v4 types as Stack Encrypt targets: `eql_bindings::v4`. Every # producible type implements `EncryptTarget` and `DecryptSource`, for a Rust -# program that encrypts through `encrypt_into`. The older `EncryptFrom` and -# `DecryptInto` are implemented too. No `dynamic` layer. +# program that encrypts through `encrypt_into`. No `dynamic` layer. stack-encrypt = [ "dep:stack-encrypt", "dep:base64", diff --git a/packages/eql/crates/eql-bindings/README.md b/packages/eql/crates/eql-bindings/README.md index ed1eccfdd7..bdedc7575d 100644 --- a/packages/eql/crates/eql-bindings/README.md +++ b/packages/eql/crates/eql-bindings/README.md @@ -114,10 +114,8 @@ The optional `stack-encrypt` feature implements Stack Encrypt's `DecryptSource` for `v4::text::TextEq`. These owned traits take the plaintext by value and wipe it. -The same derives also implement the older `EncryptFrom` and `DecryptInto`. -Plans, the dynamic layer, and `encrypt_as` still use them, until Stack Encrypt -removes them. Stack Encrypt writes EQL v4 only, so no `v3` type implements -these traits. `eql-codegen` emits the derives. +Stack Encrypt writes EQL v4 only, so no `v3` type implements these traits. +`eql-codegen` emits the derives. Each Rust example in this file is a complete program, and the encryption test crate runs it as a doctest: @@ -352,16 +350,31 @@ escaped in the descriptor rather than read as two names. ### Querying an EQL v4 field from Rust -A query names an operator, and a plan for the column's EQL type finds the -index that answers it (Stack Encrypt's ADR-0009). The term that comes back -is the type's query twin, the value the `eql_v4.query_*` domain takes: +A typed query starts from a field of a derived record. The record derive +gives each field a marker, such as `EncryptedUser::EMAIL`, with a method for +each operator the field's EQL type answers. The term that comes back is the +type's query twin, the value the `eql_v4.query_*` domain takes: ```rust use eql_bindings::v4::text::{TextEq, TextMatch}; -use stack_encrypt::op::{self, Probabilistic, Stats}; -use stack_encrypt::{Plan, StackCipher}; +use stack_encrypt::op::{Probabilistic, Stats}; +use stack_encrypt::{EncryptInto, EncryptTarget, StackCipher}; use stack_kms::FakeDataKeySource; +#[derive(zeroize::Zeroize)] +struct User { + email: String, + name: String, +} + +// The record's scope names the table, and each field's name is its column. +#[derive(EncryptTarget)] +#[stash(struct = User, scope = "users")] +struct EncryptedUser { + email: TextEq, // i = {"t": "users", "c": "email"} + name: TextMatch, // i = {"t": "users", "c": "name"} +} + #[tokio::main(flavor = "current_thread")] async fn main() -> Result<(), Box> { // Local example only: these keys are held in memory, not in ZeroKMS. @@ -369,28 +382,26 @@ async fn main() -> Result<(), Box> { .kms(FakeDataKeySource::new()) .init() .await?; + let keyset = cipher.default_keyset(); + + let user = User { + email: "alice@example.com".into(), + name: "Alice Nguyen".into(), + }; + let stored: EncryptedUser = user.encrypt_into(&keyset).await?; - // A plan for the column's type, under the label that names the column: - // "users/email" is Identifier::for_column("users", "email"). - let email_plan = Plan::value::() - .context("users/email") - .encrypt_into::() - .build()?; // A `TextEqQuery`. - let query = cipher - .query(op::Eq, "alice@example.com") - .on(&email_plan) + let query = EncryptedUser::EMAIL + .eq("alice@example.com") + .encrypt(&keyset) .await?; // WHERE email = $1::eql_v4.query_text_eq, binding sqlx::types::Json(&query) assert_eq!(query.i.t, "users"); assert_eq!(query.i.c, "email"); + assert_eq!(query.hm, stored.email.hm); - let name_plan = Plan::value::() - .context("users/name") - .encrypt_into::() - .build()?; // A `MatchTwin`. - let query = cipher.query(op::Matches, "ali").on(&name_plan).await?; + let query = EncryptedUser::NAME.matches("ali").encrypt(&keyset).await?; // How many rows to fetch to expect 10 real matches in a million. let fetch = query .target_limit(10, &Stats::declared(1_000_000)) @@ -404,37 +415,39 @@ async fn main() -> Result<(), Box> { } ``` -| v4 type | Operator | Query term | -| --- | --- | --- | -| `TextEq` | `op::Eq` | `TextEqQuery` | -| `SmallintEq`, `IntegerEq`, `BigintEq`, `RealEq`, `DoubleEq` | `op::Eq` | `SmallintEqQuery`, `IntegerEqQuery`, `BigintEqQuery`, `RealEqQuery`, `DoubleEqQuery` | -| `DateEq`, `TimestampEq`, `NumericEq` | `op::Eq` | `DateEqQuery`, `TimestampEqQuery`, `NumericEqQuery` (from `v4::plaintext::Date`, `Timestamp`, `Decimal`) | -| `TextMatch` | `op::Matches` | `MatchTwin` | +| v4 type | Marker method | Operator | Query term | +| --- | --- | --- | --- | +| `TextEq` | `eq` | `op::Eq` | `TextEqQuery` | +| `SmallintEq`, `IntegerEq`, `BigintEq`, `RealEq`, `DoubleEq` | `eq` | `op::Eq` | `SmallintEqQuery`, `IntegerEqQuery`, `BigintEqQuery`, `RealEqQuery`, `DoubleEqQuery` | +| `DateEq`, `TimestampEq`, `NumericEq` | `eq` | `op::Eq` | `DateEqQuery`, `TimestampEqQuery`, `NumericEqQuery` (from `v4::plaintext::Date`, `Timestamp`, `Decimal`) | +| `TextMatch` | `matches` | `op::Matches` | `MatchTwin` | `MatchTwin` serializes as the twin and adds `verify` and `target_limit` under `EqlMatch`: a filter can match a row that does not hold the query's text, so keep only the decrypted rows that verify. An exact query has no -`verify`. An operator a type does not answer does not compile: `op::Lt` on -`TextEq`, or `op::Matches` on `IntegerEq`. Nor does naming another index -with `.via(..)`, such as the bare `Equality`, whose term the type's SQL does -not take. The ordered types (`IntegerOrd`, `TextOrd`, …) answer nothing in -the typed chain yet: their twins need an order term, or several terms at -once, and the chain sends one index's term. Query them through the data -path (`stack_encrypt::dynamic::query_term`) for now. - -The label must name the column the value was written under, as two -segments, table then column. A label of any other length is refused when -the query is awaited, before any keyset is resolved, with an -`encryption::query::QueryLabelError` in `Error::Other`. A label's segments are plain, so a table or column name -that is not a plain label segment cannot be named on a plan, and such a -column, which `Identifier::for_column` accepts, cannot be queried this way: -for instance a name that starts with a digit, `-` or `b64:`, or one holding -`/`, `(`, `)`, or a control or invisible format character. -The plan is for queries: write with -`value.encrypt_into_with_context(&keyset, column)` and open with -`P::decrypt_from_with_context`, since -an EQL type is sealed under its column rather than the plan's label. The -query is the same bytes the data path derives for a field that names the +`verify`. An operator a type does not answer does not compile: `.lt(..)` on +a `TextEq` field, or `.matches(..)` on an `IntegerEq` field. A marker names +its field's type and no index, so a query cannot send another index's term, +such as the bare `Equality` term, which the type's SQL does not take. The +ordered types (`IntegerOrd`, `TextOrd`, …) and the text search types answer +nothing from a marker yet: their twins need an order term, or several terms +at once, and a marker's query sends one index's term. Query them through +the data path (`stack_encrypt::dynamic::query_term`) for now. + +The query is derived under the marker's label: the record's scope, then the +field's name (or its `identity`). That is the column the field stores in +`i`, so the query finds the values the record wrote, and the values the +type wrote on its own with +`value.encrypt_into_with_context(&keyset, Identifier::for_column("users", "email")?)`. +A marker's label always has those two segments, so it always names a +column. The derive accepts only plain label segments. So a column whose +table or column name is not one, which `Identifier::for_column` accepts, +cannot be a record field, and cannot be queried this way: for instance a +name that starts with a digit, `-` or `b64:`, or one holding `/`, `(`, `)`, +or a control or invisible format character. +A record that lists a scope, such as a tenant, takes it on the query with +`.scope(..)`: see [EQL types as record fields](#eql-types-as-record-fields). +The query is the same bytes the data path derives for a field that names the type (`stack_encrypt::dynamic::query_term`), which is what the Go SDK sends, and `tests/encryption/tests/typed_queries.rs` checks that for every type and operator. The `op::Answers` impls are generated into `src/v4/answers.rs` @@ -681,15 +694,14 @@ field's label, then those parts, and the EQL value stores the column alone, as it does for a [record field](#eql-types-as-record-fields). A type is producible exactly when its generated struct carries the `stack-encrypt` derives (`ENCRYPTION_DOMAINS` in `eql-codegen`), because the dispatch runs the derived -plan; `targets::targets()` lists them, each with `producible` and, when it is +declaration; `targets::targets()` lists them, each with `producible` and, when it is `false`, the reason. ### Features - `stack-encrypt`: the EQL v4 types (`eql_bindings::v4`) with Stack Encrypt's `EncryptTarget` and `DecryptSource`, for a Rust program that - encrypts through `encrypt_into`. The older `EncryptFrom` and `DecryptInto` - are implemented too. It builds Stack Encrypt with no default + encrypts through `encrypt_into`. It builds Stack Encrypt with no default features and without its `dynamic` layer. - `targets`: `stack-encrypt`, plus the by-name dispatch a language binding needs (`encryption::targets`, `v4::targets`), where the EQL type is named at diff --git a/packages/eql/crates/eql-bindings/src/encryption.rs b/packages/eql/crates/eql-bindings/src/encryption.rs index 9e98f1f95b..050980b2b3 100644 --- a/packages/eql/crates/eql-bindings/src/encryption.rs +++ b/packages/eql/crates/eql-bindings/src/encryption.rs @@ -52,11 +52,11 @@ //! #![cfg_attr( feature = "targets", - doc = "A binding that holds no Rust type to name reaches the same plans by the type's *name* through [`targets`] (`targets` feature): the catalog-generated table of EQL types a data plan may name as a field target, and `encrypt` / `decrypt` / `query` dispatching on that name." + doc = "A binding that holds no Rust type to name runs the same encryption by the type's *name* through [`targets`] (`targets` feature): the catalog-generated table of EQL types a data plan may name as a field target, and `encrypt` / `decrypt` / `query` dispatching on that name." )] #![cfg_attr( not(feature = "targets"), - doc = "A binding that holds no Rust type to name reaches the same plans by the type's *name* through `targets`, with the `targets` feature: the catalog-generated table of EQL types a data plan may name as a field target, and `encrypt` / `decrypt` / `query` dispatching on that name." + doc = "A binding that holds no Rust type to name runs the same encryption by the type's *name* through `targets`, with the `targets` feature: the catalog-generated table of EQL types a data plan may name as a field target, and `encrypt` / `decrypt` / `query` dispatching on that name." )] //! //! Use the same table and column identifier for writes and queries. @@ -117,9 +117,9 @@ use stack_encrypt::target::{ RequiredContext, }; use stack_encrypt::{ - CipherText, ContextPiece, Decrypt, DecryptField, DecryptInto, Decryptable, Decryption, - Describe, Description, Encrypt, EncryptFrom, Encryption, Error, IntoContext, MaybeEmpty, - NonEmpty, SealedValue, StackCipherText, + CipherText, ContextPiece, Decrypt, DecryptField, Decryptable, Decryption, Describe, + Description, Encrypt, Encryption, Error, IntoContext, MaybeEmpty, NonEmpty, SealedValue, + StackCipherText, }; use zeroize::Zeroize; @@ -153,7 +153,7 @@ impl Identifier { /// The column a two-segment label names, table then column, or `None` /// for a label of any other length. One reading of a label for the /// by-name dispatch (`targets::Identifier::from_label`) and the typed - /// query chain ([`query`]). + /// query of a field marker ([`query`]). pub(crate) fn column_of(label: &stack_encrypt::Label) -> Option> { let mut segments = label.segments(); let (Some(table), Some(column), None) = (segments.next(), segments.next(), segments.next()) @@ -257,18 +257,9 @@ impl Reader for Ciphertext { } } -impl EncryptFrom for Ciphertext { - type Context = AeadContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - target::ciphertext().transcode() - } -} -// The owned declaration of the same leaf: the canonical ciphertext, read into -// EQL's `c`. The declaration requires the AEAD (authenticated encryption) -// half of the context, as Stack Encrypt's own `StackCipherText` does. +// The canonical ciphertext, read into EQL's `c`. The declaration requires the +// AEAD (authenticated encryption) half of the context, as Stack Encrypt's own +// `StackCipherText` does. impl EncryptTarget for Ciphertext { type ContextInput = RequiredContext; fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput> @@ -290,31 +281,25 @@ impl Visitor for NativeCiphertextVisitor { Ok(CipherText::Single(leaf)) } } -impl DecryptInto

for Ciphertext { - type Context = AeadContext; - fn decryption(self, context: Self::Context) -> Decryption { - match self.read(NativeCiphertextVisitor) { - Ok(native) => target::open(native, context), - Err(error) => Decryption::failed(error), - } - } -} impl DecryptSource

for Ciphertext { type ContextInput = RequiredContext; fn decryption(self, context: Self::ContextInput) -> Decryption { - DecryptInto::decryption(self, context.into_inner()) + match self.read(NativeCiphertextVisitor) { + Ok(native) => target::open(native, context.into_inner()), + Err(error) => Decryption::failed(error), + } } } impl Decryptable for Ciphertext { const DECRYPTABLE: bool = true; } -impl DecryptField for Ciphertext +impl DecryptField for Ciphertext where - Self: DecryptInto

, - Ctx: Into<>::Context>, + Self: DecryptSource

, + Ctx: Into<>::ContextInput>, { fn decryption_field(self, context: Ctx) -> Option> { - Some(DecryptInto::decryption(self, context.into())) + Some(DecryptSource::decryption(self, context.into())) } } @@ -339,20 +324,6 @@ impl Transcode for Hmac256 { EqualityVisitor } } -impl EncryptFrom for Hmac256 { - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - target::equality().transcode() - } - /// The equality index: a plan for a type holding an `hm` answers - /// `op::Eq` through it. - fn indexes() -> Vec { - vec![IndexSpec::Equality] - } -} impl EncryptTarget for Hmac256 { type ContextInput = RequiredContext; fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput> @@ -361,8 +332,10 @@ impl EncryptTarget for Hmac256 { { target::equality().accepting().transcode() } + /// The equality index: a type holding an `hm` answers `op::Eq` + /// through it. fn indexes() -> Vec { - >::indexes() + vec![IndexSpec::Equality] } } impl Decryptable for Hmac256 { @@ -467,35 +440,21 @@ impl Transcode for BloomFilter { MatchVisitor } } -impl> EncryptFrom for BloomFilter { - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - // The type annotation names the match configuration. Inference fills - // in the other type parameters of `matching`. - let terms: Encryption<'s, S, MatchTerms, K, CallerContext> = target::matching(); - terms.transcode() - } - /// The match index under [`EqlMatch`]: a plan for a type holding a - /// `bf` answers `op::Matches` through it. - fn indexes() -> Vec { - vec![IndexSpec::Match(EqlMatch::options())] - } -} impl + Zeroize> EncryptTarget for BloomFilter { type ContextInput = RequiredContext; fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput> where S: 's, { - // The annotation names the match configuration, as for `BloomFilter`. + // The type annotation names the match configuration. Inference fills + // in the other type parameters of `matching`. let terms: Encryption<'s, S, MatchTerms, K, CallerContext> = target::matching(); terms.accepting().transcode() } + /// The match index under [`EqlMatch`]: a type holding a `bf` answers + /// `op::Matches` through it. fn indexes() -> Vec { - >::indexes() + vec![IndexSpec::Match(EqlMatch::options())] } } impl Decryptable for BloomFilter { @@ -527,17 +486,6 @@ impl Transcode for SearchBloomFilter { SearchMatchVisitor } } -impl> EncryptFrom for SearchBloomFilter { - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - // The annotation names the match configuration, as for `BloomFilter`. - let terms: Encryption<'s, S, MatchTerms, K, CallerContext> = target::matching(); - terms.transcode() - } -} impl + Zeroize> EncryptTarget for SearchBloomFilter { type ContextInput = RequiredContext; fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput> diff --git a/packages/eql/crates/eql-bindings/src/encryption/query.rs b/packages/eql/crates/eql-bindings/src/encryption/query.rs index 0f31e62ae0..35e8fb15a0 100644 --- a/packages/eql/crates/eql-bindings/src/encryption/query.rs +++ b/packages/eql/crates/eql-bindings/src/encryption/query.rs @@ -1,55 +1,58 @@ -//! Querying an EQL v4 field through Stack Encrypt's typed query chain. +//! Querying an EQL v4 field from Rust, through a field marker. //! -//! A query names an operator, and the plan it runs on finds the index that -//! answers it (Stack Encrypt's ADR-0009): -//! `cipher.query(op::Eq, "bob@example.com").on(&plan)`. On a plan built for -//! an EQL v4 type, `Plan::value::().encrypt_into::()`, the -//! type says which operators it answers through its -//! [`op::Answers`] impls, and the term that comes back is the type's -//! *query twin*: the value of the `eql_v4.query_*` domain the SQL compares -//! against the column. +//! A typed query of an EQL type starts from a field of a derived record. +//! `#[derive(EncryptTarget)]` on a `struct = ..` record gives each field a +//! marker, an associated constant named after the field +//! ([`stack_encrypt::marker`]). The marker has a method for each operator +//! the field's type answers: +//! `EncryptedMember::EMAIL.eq("bob@example.com").encrypt(&keyset)`. An EQL +//! v4 type says which operators it answers through its [`op::Answers`] +//! impls, and the term that comes back is the type's *query twin*: the +//! value of the `eql_v4.query_*` domain the SQL compares against the +//! column. //! -//! | type | operator | query term | -//! |---|---|---| -//! | `TextEq` | [`op::Eq`](struct@op::Eq) | `TextEqQuery` | -//! | `IntegerEq` | [`op::Eq`](struct@op::Eq) | `IntegerEqQuery` | -//! | `BigintEq` | [`op::Eq`](struct@op::Eq) | `BigintEqQuery` | -//! | `RealEq` | [`op::Eq`](struct@op::Eq) | `RealEqQuery` | -//! | `DoubleEq` | [`op::Eq`](struct@op::Eq) | `DoubleEqQuery` | -//! | `TextMatch` | [`op::Matches`](struct@op::Matches) | [`MatchTwin`](MatchTwin) | +//! | type | marker method | operator | query term | +//! |---|---|---|---| +//! | `TextEq` | `eq` | [`op::Eq`](struct@op::Eq) | `TextEqQuery` | +//! | `SmallintEq` | `eq` | [`op::Eq`](struct@op::Eq) | `SmallintEqQuery` | +//! | `IntegerEq` | `eq` | [`op::Eq`](struct@op::Eq) | `IntegerEqQuery` | +//! | `BigintEq` | `eq` | [`op::Eq`](struct@op::Eq) | `BigintEqQuery` | +//! | `RealEq` | `eq` | [`op::Eq`](struct@op::Eq) | `RealEqQuery` | +//! | `DoubleEq` | `eq` | [`op::Eq`](struct@op::Eq) | `DoubleEqQuery` | +//! | `DateEq` | `eq` | [`op::Eq`](struct@op::Eq) | `DateEqQuery` | +//! | `TimestampEq` | `eq` | [`op::Eq`](struct@op::Eq) | `TimestampEqQuery` | +//! | `NumericEq` | `eq` | [`op::Eq`](struct@op::Eq) | `NumericEqQuery` | +//! | `TextMatch` | `matches` | [`op::Matches`](struct@op::Matches) | [`MatchTwin`](MatchTwin) | //! //! eql-codegen writes those impls from the catalog, by the rule Stack //! Encrypt's data path answers an EQL type's operators with: `Eq` through //! the equality index, `Matches` through the match index. Each type is also -//! an [`op::LabelledTarget`], which is what lets its plan be queried. The -//! ordered types (`IntegerOrdOre`, `TextOrd` and the rest) are not produced -//! yet, so they answer nothing. An operator a type does not answer does not -//! compile, and neither does naming another index with `.via(..)`: the -//! plan sends the type's twin or nothing. +//! an [`op::LabelledTarget`], which is what lets its marker be queried. The +//! ordered types (`IntegerOrd`, `TextOrdOre` and the rest) and the text +//! search types answer nothing from a marker yet: their twins need an order +//! term, or several terms at once, and a marker's query sends one index's +//! term. Query them through the data path +//! (`stack_encrypt::dynamic::query_term`). An operator a type does not +//! answer does not compile. A marker names its field's type and no index, +//! so a query cannot send another index's term in place of the type's twin. //! -//! The query is derived under the plan's label, which must name the column -//! the value was written under: a two-segment label, table then column, is -//! the [`Identifier`] the value stores, so -//! `"users/email"` queries values written with -//! `Identifier::for_column("users", "email")`. A label's segments are plain -//! ([`Label`]): none may start with a digit or `-`, for -//! instance, though `Identifier::for_column` takes such a name. A column -//! whose table or column name is not a plain segment can be written but -//! not named on a plan, so it cannot be queried this way. A label of any other length -//! names no column, so the awaited query refuses it, before any keyset is -//! resolved or term derived. +//! The query is derived under the marker's label: the record's scope, then +//! the field's name (or its `identity`). That is the column the field +//! stores in `i`, so a marker of the field `email` of a record with +//! `scope = "users"` queries the values written under +//! `Identifier::for_column("users", "email")`, by the record or by the +//! type on its own. A marker's label always has those two segments, so it +//! always names a column. The derive accepts only plain segments +//! ([`Label`]): none may start with a digit or `-`, for instance, though +//! `Identifier::for_column` takes such a name. A column whose table or +//! column name is not a plain segment can be written by the type on its +//! own, but it cannot be a record field, so it cannot be queried this way. //! -//! A query can carry a scope: `.scope(..)` on a query from a record -//! field's marker, or the chain's `.extend(..)` on a plan's query. Its term -//! is then derived under the column and the scope, and matches the values -//! a record stored under the same scope. The twin stores the column alone, -//! as the stored value does: a scope is never stored in `i`. -//! -//! The plan is for queries. Stack Encrypt runs a plan's encryption under -//! the label, and an EQL type is sealed under its column instead, so write -//! with `value.encrypt_into_with_context(&keyset, column)` and open with -//! `P::decrypt_from_with_context(stored, &keyset, column)`, as the -//! [encryption example](super#example) does. +//! A record can list a scope, such as a tenant. A query from its marker +//! then takes the scope with `.scope(..)` before it can be encrypted. Its +//! term is derived under the column and the scope, and matches the values +//! the record stored under the same scope. The twin stores the column +//! alone, as the stored value does: a scope is never stored in `i`. //! //! The query is the bytes Stack Encrypt's data path derives for a field //! that names the same type (`stack_encrypt::dynamic::query_term`), which @@ -59,72 +62,92 @@ //! //! The code below is executed by `cargo test -p eql-encryption-tests --test //! query_example`, for the reason the [encryption example](super#example) -//! gives. +//! gives. It derives `zeroize::Zeroize` for its plaintext struct, so it +//! also needs `zeroize` with its `derive` feature. #![doc = concat!("```rust,ignore\n", include_str!("query_example.rs"), "\n```")] //! //! # Operators a type does not answer //! -//! A plan for `TextEq` answers `Eq`: -//! ```no_run +//! A `TextEq` field answers `eq`: +//! ``` //! use eql_bindings::v4::text::TextEq; -//! use stack_encrypt::plan::{Typed, ValuePlan}; -//! use stack_encrypt::{op, StackCipher}; -//! fn query(cipher: &StackCipher, plan: &ValuePlan>) { -//! let _ = cipher.query(op::Eq, "bob").on(plan); +//! use stack_encrypt::EncryptTarget; +//! use zeroize::Zeroize; +//! +//! #[derive(Zeroize)] +//! struct User { +//! email: String, //! } +//! +//! #[derive(EncryptTarget)] +//! #[stash(struct = User, scope = "users")] +//! struct EncryptedUser { +//! email: TextEq, +//! } +//! +//! let _ = EncryptedUser::EMAIL.eq("bob"); //! ``` //! and no ordering, since `TextEq` carries no order term: //! ```compile_fail //! use eql_bindings::v4::text::TextEq; -//! use stack_encrypt::plan::{Typed, ValuePlan}; -//! use stack_encrypt::{op, StackCipher}; -//! fn query(cipher: &StackCipher, plan: &ValuePlan>) { -//! let _ = cipher.query(op::Lt, "bob").on(plan); +//! use stack_encrypt::EncryptTarget; +//! use zeroize::Zeroize; +//! +//! #[derive(Zeroize)] +//! struct User { +//! email: String, //! } +//! +//! #[derive(EncryptTarget)] +//! #[stash(struct = User, scope = "users")] +//! struct EncryptedUser { +//! email: TextEq, +//! } +//! +//! let _ = EncryptedUser::EMAIL.lt("bob"); +//! ``` +//! An `IntegerEq` field answers `eq`: //! ``` -//! A plan for `IntegerEq` answers `Eq`: -//! ```no_run //! use eql_bindings::v4::integer::IntegerEq; -//! use stack_encrypt::plan::{Typed, ValuePlan}; -//! use stack_encrypt::{op, StackCipher}; -//! fn query(cipher: &StackCipher, plan: &ValuePlan>) { -//! let _ = cipher.query(op::Eq, &42).on(plan); +//! use stack_encrypt::EncryptTarget; +//! use zeroize::Zeroize; +//! +//! #[derive(Zeroize)] +//! struct User { +//! age: i32, +//! } +//! +//! #[derive(EncryptTarget)] +//! #[stash(struct = User, scope = "users")] +//! struct EncryptedUser { +//! age: IntegerEq, //! } +//! +//! let _ = EncryptedUser::AGE.eq(42i32); //! ``` //! and no text match: //! ```compile_fail //! use eql_bindings::v4::integer::IntegerEq; -//! use stack_encrypt::plan::{Typed, ValuePlan}; -//! use stack_encrypt::{op, StackCipher}; -//! fn query(cipher: &StackCipher, plan: &ValuePlan>) { -//! let _ = cipher.query(op::Matches, &42).on(plan); +//! use stack_encrypt::EncryptTarget; +//! use zeroize::Zeroize; +//! +//! #[derive(Zeroize)] +//! struct User { +//! age: i32, //! } -//! ``` -//! Nor does a plan for `TextEq` take another index, whose term the type's -//! SQL would not take, such as the bare equality index: -//! ```compile_fail -//! use eql_bindings::v4::text::TextEq; -//! use stack_encrypt::plan::{Typed, ValuePlan}; -//! use stack_encrypt::{op, Equality, StackCipher}; -//! fn query(cipher: &StackCipher, plan: &ValuePlan>) { -//! let _ = cipher.query(op::Eq, "bob").via(Equality).on(plan); +//! +//! #[derive(EncryptTarget)] +//! #[stash(struct = User, scope = "users")] +//! struct EncryptedUser { +//! age: IntegerEq, //! } +//! +//! let _ = EncryptedUser::AGE.matches("42"); //! ``` -//! or another type's twin index, which answers that type's plaintext only: -//! ```compile_fail -//! use eql_bindings::encryption::query::TwinIndex; -//! use eql_bindings::v4::integer::IntegerEqQuery; -//! use eql_bindings::v4::text::TextEq; -//! use stack_encrypt::plan::{Typed, ValuePlan}; -//! use stack_encrypt::{op, Equality, StackCipher}; -//! fn query(cipher: &StackCipher, plan: &ValuePlan>) { -//! let index = TwinIndex::::default(); -//! let _ = cipher.query(op::Eq, "bob").via(index).on(plan); -//! } +//! The index a type answers through is a [`TwinIndex`]. It answers for its +//! twin's own plaintext only ([`QueryTwin::Plaintext`]): `TextEqQuery`'s +//! for a `String`, //! ``` -//! The twin index answers for its twin's own plaintext only -//! ([`QueryTwin::Plaintext`]): `TextEqQuery`'s for a `String`, -//! ```no_run //! use eql_bindings::encryption::query::TwinIndex; //! use eql_bindings::v4::text::TextEqQuery; //! use stack_encrypt::{op, Equality}; @@ -177,7 +200,7 @@ use crate::Identifier; pub trait QueryTwin: Sized { /// The plaintext the twin's type is produced from: `String` for /// `TextEqQuery`, `i32` for `IntegerEqQuery`. A [`TwinIndex`] answers - /// for this plaintext only, so no plan can send one type's twin for + /// for this plaintext only, so no query can send one type's twin for /// another type's value. type Plaintext; /// The twin of `term`, derived under `column`. @@ -187,10 +210,11 @@ pub trait QueryTwin: Sized { /// The index an EQL v4 type answers an operator through: Stack Encrypt's /// index `I`, whose query term is sent as the type's query twin `Q`. /// -/// It derives exactly what `I` derives, under the plan's label and any -/// scope the query carries, and checks and refuses what `I` refuses, as -/// well as a label that names no column; it then writes the term into the -/// twin, with the column the label names. It answers for the twin's own plaintext +/// It derives exactly what `I` derives, under the label of the field's +/// marker and any scope the query carries, and checks and refuses what `I` +/// refuses, as well as a label that names no column; it then writes the +/// term into the twin, with the column the label names. It answers for the +/// twin's own plaintext /// ([`QueryTwin::Plaintext`]) only. `TextEq` answers [`op::Eq`](struct@op::Eq) through /// `TwinIndex`, and `TextMatch` answers /// [`op::Matches`](struct@op::Matches) through @@ -227,8 +251,8 @@ impl fmt::Debug for TwinIndex { } } -/// The index `I`, unchanged: the same spec, so a plan holds it as it holds -/// `I`, and the same derivation, so its term is `I`'s. +/// The index `I`, unchanged: the same spec, so it is declared as `I` is, +/// and the same derivation, so its term is `I`'s. impl, Q> Index for TwinIndex { type Term = I::Term; fn spec(&self) -> IndexSpec { @@ -245,8 +269,8 @@ impl, Q> Index for TwinIndex { } /// Refuse a label no EQL query value can record: one that names no column -/// (it must be two segments, table then column). Before any keyset is -/// resolved or term derived. +/// (it must be two segments, table then column). Before any term is +/// derived. /// /// A scope the query carries is not refused. The twin stores the column /// alone, and so does a value a record stored under that scope: the scope @@ -261,8 +285,12 @@ fn check_column(label: &Label) -> Result<(), Error> { } /// Why a typed query on an EQL v4 type was refused for its context, before -/// any keyset was resolved. It arrives as [`Error::Other`]; match it with +/// any term was derived. It arrives as [`Error::Other`]; match it with /// `downcast_ref::()`. +/// +/// The label of a derived record's field marker is the record's scope and +/// the field's name, two segments, so a query from such a marker is never +/// refused this way. #[derive(Clone, Debug, PartialEq, Eq)] #[non_exhaustive] pub enum QueryLabelError { @@ -288,8 +316,8 @@ impl fmt::Display for QueryLabelError { impl std::error::Error for QueryLabelError {} -/// The column a query's label names. The chain calls `query` only after -/// `check_label` accepted the label (`Supports::query`), and every +/// The column a query's label names. A marker's query calls `query` only +/// after `check_label` accepted the label (`Supports::query`), and every /// `Supports` impl here delegates `check_label` to [`check_column`], so the /// label is two segments; a label that is not would be a broken invariant, /// and fails here rather than as a twin with an empty column. diff --git a/packages/eql/crates/eql-bindings/src/encryption/query_example.rs b/packages/eql/crates/eql-bindings/src/encryption/query_example.rs index 0a3ae1a39a..543f427015 100644 --- a/packages/eql/crates/eql-bindings/src/encryption/query_example.rs +++ b/packages/eql/crates/eql-bindings/src/encryption/query_example.rs @@ -1,10 +1,23 @@ -use eql_bindings::{ - v4::text::{TextEq, TextMatch}, - Identifier, -}; -use stack_encrypt::op::{self, Probabilistic, Stats}; -use stack_encrypt::{EncryptInto, Plan, StackCipher}; +use eql_bindings::v4::text::{TextEq, TextMatch}; +use stack_encrypt::op::{Probabilistic, Stats}; +use stack_encrypt::{EncryptInto, EncryptTarget, StackCipher}; use stack_kms::FakeDataKeySource; +use zeroize::Zeroize; + +#[derive(Zeroize)] +struct User { + email: String, + name: String, +} + +// The record's scope names the table, and each field's name is its column: +// `users.email` and `users.name`. +#[derive(EncryptTarget)] +#[stash(struct = User, scope = "users")] +struct EncryptedUser { + email: TextEq, + name: TextMatch, +} #[tokio::main(flavor = "current_thread")] async fn main() -> Result<(), Box> { @@ -15,33 +28,26 @@ async fn main() -> Result<(), Box> { .await?; let keyset = cipher.default_keyset(); - // Write a value to the `users.email` column. - let column = Identifier::for_column("users", "email")?; - let stored: TextEq = String::from("alice@example.com") - .encrypt_into_with_context(&keyset, column) - .await?; + // Write a row. + let user = User { + email: "alice@example.com".into(), + name: "Alice Nguyen".into(), + }; + let stored: EncryptedUser = user.encrypt_into(&keyset).await?; - // Query it: a plan for the column's type, under the label that names - // the same column, table then column. - let email_plan = Plan::value::() - .context("users/email") - .encrypt_into::() - .build()?; + // Query the `email` column from the field's marker. // A `TextEqQuery`: bind it as `$1::eql_v4.query_text_eq`. - let query = cipher - .query(op::Eq, "alice@example.com") - .on(&email_plan) + let query = EncryptedUser::EMAIL + .eq("alice@example.com") + .encrypt(&keyset) .await?; - assert_eq!(query.hm, stored.hm); - assert_eq!(query.i, stored.i); + assert_eq!(query.hm, stored.email.hm); + assert_eq!(query.i, stored.email.i); // A text match: bind `query.twin()` (or the query itself, which // serializes as the twin) as `$1::eql_v4.query_text_match`. - let name_plan = Plan::value::() - .context("users/name") - .encrypt_into::() - .build()?; - let query = cipher.query(op::Matches, "ali").on(&name_plan).await?; + let query = EncryptedUser::NAME.matches("ali").encrypt(&keyset).await?; + assert_eq!(query.twin().i, stored.name.i); // How many rows to fetch to expect 10 real matches in a million. let _limit = query.target_limit(10, &Stats::declared(1_000_000)); // The server can return rows that hold every bit of the filter without diff --git a/packages/eql/crates/eql-bindings/src/encryption/targets.rs b/packages/eql/crates/eql-bindings/src/encryption/targets.rs index 54cb2d952c..44e4e3ee4d 100644 --- a/packages/eql/crates/eql-bindings/src/encryption/targets.rs +++ b/packages/eql/crates/eql-bindings/src/encryption/targets.rs @@ -1,17 +1,18 @@ //! EQL types as plan field targets, by name. //! -//! A Rust caller names an EQL type as a type: `encrypt_as::` runs the -//! plan `TextEq`'s own `EncryptFrom` describes. A binding has no type to -//! name — its plan arrives as data, and a field of that plan names its target -//! as a string, `"TextEq"`. This module is where that string meets the type: -//! a catalog-generated table of every EQL type a plan may name +//! A Rust caller names an EQL type as a type: +//! `value.encrypt_into_with_context::(&keyset, column)` runs the +//! encryption `TextEq`'s own `EncryptTarget` declares. A binding has no type +//! to name — its plan arrives as data, and a field of that plan names its +//! target as a string, `"TextEq"`. This module is where that string meets +//! the type: a catalog-generated table of every EQL type a plan may name //! ([`targets`], [`Target`]) and three entry points ([`encrypt`], -//! [`decrypt`], [`query`]) that dispatch on the name and run *the same plan* -//! the typed call runs. Nothing here derives a term or seals a byte itself -//! (ADR-0007: one engine, entered through a plan); the dispatch is generated -//! from `eql-domains::CATALOG` by `eql-codegen` into -//! [`crate::v4::targets`], beside the v4 family modules, so a type cannot be in the -//! catalog and missing from the table. +//! [`decrypt`], [`query`]) that dispatch on the name and run *the same +//! declaration* the typed call runs. Nothing here derives a term or seals a +//! byte itself (ADR-0007: one engine, entered through a plan); the dispatch +//! is generated from `eql-domains::CATALOG` by `eql-codegen` into +//! [`crate::v4::targets`], beside the v4 family modules, so a type cannot be +//! in the catalog and missing from the table. //! //! This module needs the `targets` feature, which also turns on Stack //! Encrypt's `dynamic` feature. The `stack-encrypt` feature alone gives only @@ -99,11 +100,10 @@ use stack_encrypt::dynamic::{TargetOptions, ValueFrame, ValueKind}; use stack_encrypt::json::JsonOptions; use stack_encrypt::kms::MaybeSend; use stack_encrypt::target::{ - CallerContext, DeclaredContext, EncryptTarget, ExpectedContext, IndexSpec, StoredContext, -}; -use stack_encrypt::{ - Decrypt, DecryptInto, Encrypt, Error, KeysetCipher, Label, NonEmpty, Pending, StackCipher, + CallerContext, DeclaredContext, DecryptSource, EncryptTarget, ExpectedContext, IndexSpec, + StoredContext, }; +use stack_encrypt::{Decrypt, Encrypt, Error, KeysetCipher, Label, NonEmpty, Pending, StackCipher}; use crate::v4::targets::{decrypt_named, encrypt_named, query_named, TARGETS}; use crate::Identifier; @@ -539,25 +539,20 @@ fn decrypt_document<'a, K: 'static>( stored: &[u8], ) -> Result, TargetError> { use crate::v4::json_tree::JsonTreeDocument; + use stack_encrypt::json::JsonPlaintext; let value: JsonTreeDocument = serde_json::from_slice(stored).map_err(|source| TargetError::Stored { target: name, source, })?; let expected: ExpectedContext = context.into(); + let opening = DecryptSource::::decryption(value, expected); Ok(match opener { - Opener::Client(cipher) => { - cipher.decrypt_as::(value, expected) - } - Opener::Keyset(keyset) => { - keyset.decrypt_as::(value, expected) - } + Opener::Client(cipher) => cipher.run_decryption(opening), + Opener::Keyset(keyset) => keyset.run_decryption(opening), } - .try_map(|mut document| { - let frame = stack_encrypt::dynamic::json_frame(&document); - stack_encrypt::json::wipe_value(&mut document); - frame - })) + // The document is wiped when it is dropped, once its frame is written. + .try_map(|document| stack_encrypt::dynamic::json_frame(document.value()))) } /// Run the named type's query twin for one plaintext: the operand that @@ -923,7 +918,7 @@ where } /// Parse a stored EQL value as the target and describe opening it through -/// the target's own `DecryptInto`. The stored column must be the field's, +/// the target's own `DecryptSource`. The stored column must be the field's, /// and the value opens under the field's context: its column, then the /// scopes and the bindings. pub(crate) fn open_target<'a, T, S, K>( @@ -933,8 +928,8 @@ pub(crate) fn open_target<'a, T, S, K>( stored: &[u8], ) -> Result, TargetError> where - T: DeserializeOwned + DecryptInto> + 'static, - S: Plaintext, + T: DeserializeOwned + DecryptSource> + 'static, + S: Plaintext + zeroize::Zeroize, K: 'static, { let value: T = serde_json::from_slice(stored).map_err(|source| TargetError::Stored { @@ -942,9 +937,10 @@ where source, })?; let expected: ExpectedContext = context.into(); + let opening = value.decryption(expected); Ok(match opener { - Opener::Client(cipher) => cipher.decrypt_as::(value, expected), - Opener::Keyset(keyset) => keyset.decrypt_as::(value, expected), + Opener::Client(cipher) => cipher.run_decryption(opening), + Opener::Keyset(keyset) => keyset.run_decryption(opening), } .try_map(S::into_frame)) } diff --git a/packages/eql/crates/eql-bindings/src/lib.rs b/packages/eql/crates/eql-bindings/src/lib.rs index 09c36279cc..20024a8624 100644 --- a/packages/eql/crates/eql-bindings/src/lib.rs +++ b/packages/eql/crates/eql-bindings/src/lib.rs @@ -34,7 +34,7 @@ )] #![cfg_attr( feature = "targets", - doc = "A binding that names an EQL type as a string reaches the same plans through [`encryption::targets`] (`targets` feature): the catalog-generated table of types a data plan may name as a field target, and the by-name `encrypt` / `decrypt` / `query` dispatch." + doc = "A binding that names an EQL type as a string runs the same encryption through [`encryption::targets`] (`targets` feature): the catalog-generated table of types a data plan may name as a field target, and the by-name `encrypt` / `decrypt` / `query` dispatch." )] use schemars::JsonSchema; diff --git a/packages/eql/crates/eql-bindings/src/v4/answers.rs b/packages/eql/crates/eql-bindings/src/v4/answers.rs index 98d2959cad..2587e16c12 100644 --- a/packages/eql/crates/eql-bindings/src/v4/answers.rs +++ b/packages/eql/crates/eql-bindings/src/v4/answers.rs @@ -1,5 +1,5 @@ // @generated by eql-codegen from the eql-domains catalog — do not edit -//! How Stack Encrypt's typed query chain queries a producible EQL v4 type: per type that answers an operator, its `op::Answers` impls, its `op::LabelledTarget` impl and its query twin's assembly (`crate::encryption::query`). Generated from the catalog, by the rule the data path answers a field that names the type with. +//! How a Stack Encrypt typed query, from a field's marker, queries a producible EQL v4 type: per type that answers an operator, its `op::Answers` impls, its `op::LabelledTarget` impl and its query twin's assembly (`crate::encryption::query`). Generated from the catalog, by the rule the data path answers a field that names the type with. use crate::encryption::query::{QueryTwin, TwinIndex}; use crate::encryption::EqlMatch; use crate::v4::terms::{BloomFilter, Hmac256}; @@ -9,7 +9,7 @@ use stack_encrypt::op; use stack_encrypt::Equality; use stack_encrypt::Match; /// The operators each EQL type answers in Stack Encrypt's typed -/// query chain (`op::Answers`), by the wire names +/// query (`op::Answers`), by the wire names /// `stack_encrypt::dynamic::Op` gives them, in catalog order: /// one row per type that answers any. Generated from the rule the /// impls below are, so the encryption tests can hold the impls to @@ -28,7 +28,7 @@ pub const ANSWERS: &[(&str, &[&str])] = &[ ("RealEq", &["eq"]), ("DoubleEq", &["eq"]), ]; -/// `IntegerEq` is sealed under the column a two-segment label names, so its plan is queried under the plan's label. +/// `IntegerEq` is sealed under the column a two-segment label names, so a field of this type is queried under its marker's label. impl op::LabelledTarget for super::integer::IntegerEq {} /// `IntegerEqQuery` from the column it queries and its `hm` term, at version 4. impl QueryTwin for super::integer::IntegerEqQuery { @@ -47,7 +47,7 @@ impl op::QueryTerm for super::integer::IntegerEqQuery {} impl op::Answers for super::integer::IntegerEq { type Via = TwinIndex; } -/// `SmallintEq` is sealed under the column a two-segment label names, so its plan is queried under the plan's label. +/// `SmallintEq` is sealed under the column a two-segment label names, so a field of this type is queried under its marker's label. impl op::LabelledTarget for super::smallint::SmallintEq {} /// `SmallintEqQuery` from the column it queries and its `hm` term, at version 4. impl QueryTwin for super::smallint::SmallintEqQuery { @@ -66,7 +66,7 @@ impl op::QueryTerm for super::smallint::SmallintEqQuery {} impl op::Answers for super::smallint::SmallintEq { type Via = TwinIndex; } -/// `BigintEq` is sealed under the column a two-segment label names, so its plan is queried under the plan's label. +/// `BigintEq` is sealed under the column a two-segment label names, so a field of this type is queried under its marker's label. impl op::LabelledTarget for super::bigint::BigintEq {} /// `BigintEqQuery` from the column it queries and its `hm` term, at version 4. impl QueryTwin for super::bigint::BigintEqQuery { @@ -85,7 +85,7 @@ impl op::QueryTerm for super::bigint::BigintEqQuery {} impl op::Answers for super::bigint::BigintEq { type Via = TwinIndex; } -/// `DateEq` is sealed under the column a two-segment label names, so its plan is queried under the plan's label. +/// `DateEq` is sealed under the column a two-segment label names, so a field of this type is queried under its marker's label. impl op::LabelledTarget for super::date::DateEq {} /// `DateEqQuery` from the column it queries and its `hm` term, at version 4. impl QueryTwin for super::date::DateEqQuery { @@ -104,7 +104,7 @@ impl op::QueryTerm for super::date::DateEqQuery {} impl op::Answers for super::date::DateEq { type Via = TwinIndex; } -/// `TimestampEq` is sealed under the column a two-segment label names, so its plan is queried under the plan's label. +/// `TimestampEq` is sealed under the column a two-segment label names, so a field of this type is queried under its marker's label. impl op::LabelledTarget for super::timestamp::TimestampEq {} /// `TimestampEqQuery` from the column it queries and its `hm` term, at version 4. impl QueryTwin for super::timestamp::TimestampEqQuery { @@ -123,7 +123,7 @@ impl op::QueryTerm for super::timestamp::TimestampEqQuery {} impl op::Answers for super::timestamp::TimestampEq { type Via = TwinIndex; } -/// `NumericEq` is sealed under the column a two-segment label names, so its plan is queried under the plan's label. +/// `NumericEq` is sealed under the column a two-segment label names, so a field of this type is queried under its marker's label. impl op::LabelledTarget for super::numeric::NumericEq {} /// `NumericEqQuery` from the column it queries and its `hm` term, at version 4. impl QueryTwin for super::numeric::NumericEqQuery { @@ -142,7 +142,7 @@ impl op::QueryTerm for super::numeric::NumericEqQuery {} impl op::Answers for super::numeric::NumericEq { type Via = TwinIndex; } -/// `TextEq` is sealed under the column a two-segment label names, so its plan is queried under the plan's label. +/// `TextEq` is sealed under the column a two-segment label names, so a field of this type is queried under its marker's label. impl op::LabelledTarget for super::text::TextEq {} /// `TextEqQuery` from the column it queries and its `hm` term, at version 4. impl QueryTwin for super::text::TextEqQuery { @@ -161,7 +161,7 @@ impl op::QueryTerm for super::text::TextEqQuery {} impl op::Answers for super::text::TextEq { type Via = TwinIndex; } -/// `TextMatch` is sealed under the column a two-segment label names, so its plan is queried under the plan's label. +/// `TextMatch` is sealed under the column a two-segment label names, so a field of this type is queried under its marker's label. impl op::LabelledTarget for super::text::TextMatch {} /// `TextMatchQuery` from the column it queries and its `bf` term, at version 4. impl QueryTwin for super::text::TextMatchQuery { @@ -178,7 +178,7 @@ impl QueryTwin for super::text::TextMatchQuery { impl op::Answers for super::text::TextMatch { type Via = TwinIndex, super::text::TextMatchQuery>; } -/// `RealEq` is sealed under the column a two-segment label names, so its plan is queried under the plan's label. +/// `RealEq` is sealed under the column a two-segment label names, so a field of this type is queried under its marker's label. impl op::LabelledTarget for super::real::RealEq {} /// `RealEqQuery` from the column it queries and its `hm` term, at version 4. impl QueryTwin for super::real::RealEqQuery { @@ -197,7 +197,7 @@ impl op::QueryTerm for super::real::RealEqQuery {} impl op::Answers for super::real::RealEq { type Via = TwinIndex; } -/// `DoubleEq` is sealed under the column a two-segment label names, so its plan is queried under the plan's label. +/// `DoubleEq` is sealed under the column a two-segment label names, so a field of this type is queried under its marker's label. impl op::LabelledTarget for super::double::DoubleEq {} /// `DoubleEqQuery` from the column it queries and its `hm` term, at version 4. impl QueryTwin for super::double::DoubleEqQuery { diff --git a/packages/eql/crates/eql-bindings/src/v4/json_tree.rs b/packages/eql/crates/eql-bindings/src/v4/json_tree.rs index 3fde352900..36542fd979 100644 --- a/packages/eql/crates/eql-bindings/src/v4/json_tree.rs +++ b/packages/eql/crates/eql-bindings/src/v4/json_tree.rs @@ -9,13 +9,12 @@ //! document ([`stack_encrypt::json::JsonDocument`], whose wire form is the //! `h` and `sv` here) and the query operands; these types are the SQL //! contract around them, with the `v` and `i` envelope, and the -//! [`EncryptFrom`] / [`DecryptInto`] impls that let a Rust program name -//! [`JsonTreeDocument`] as a target: `encrypt_as::` under -//! an [`Identifier`]. A query derives through the engine's own document -//! target under the same context as a label, `Plan::value::() -//! .context("users/profile").encrypt_into::()`, and converts -//! into the operand types here; the identifier and the two-segment label -//! are one context, so the selectors agree (see [`crate::encryption`]). The +//! [`EncryptTarget`] / [`DecryptSource`] impls that let a Rust program name +//! [`JsonTreeDocument`] as a target: +//! `document.encrypt_into_with_context(&keyset, column)` under an +//! [`Identifier`]. A query derives through a data plan that names the type +//! (`JsonSearch`) under the same column, and its operand converts into the +//! operand types here (see [`crate::encryption`]). The //! design is `docs/plans/2026-10-17-json-tree-index.md` at //! the repository root. @@ -27,12 +26,10 @@ use stack_encrypt::json::{ JsonSelector, JsonSubtree, JsonSubtreeKey, }; use stack_encrypt::target::{ - AeadContext, DecryptSource, EncryptTarget, ExpectedContext, IndexSpec, Json, Layout, - RequiredContext, StoredContext, -}; -use stack_encrypt::{ - DecryptField, DecryptInto, Decryptable, Decryption, EncryptFrom, Encryption, Error, NonEmpty, + json_document, AeadContext, Borrowed, DecryptSource, EncryptTarget, ExpectedContext, Index, + IndexSpec, Json, RequiredContext, StoredContext, }; +use stack_encrypt::{DecryptField, Decryptable, Decryption, Encryption, Error, NonEmpty}; use ts_rs::TS; use zeroize::Zeroize; @@ -460,33 +457,45 @@ impl JsonTreeEntry { // ============================================================================= /// The `json_tree` layout under the JSON index's options: what seals a -/// value into a [`JsonTreeDocument`] under options other than the +/// document into a [`JsonTreeDocument`] under options other than the /// defaults, such as the paths that carry an ordering term -/// ([`JsonOptions::ordered`]). A [`Layout`], so a Rust program runs it as -/// `keyset.run(JsonTree::new(options).encryption(), &value, column)`, and -/// its index is the engine's [`Json`] under the same options, which a -/// query derives through. [`JsonTreeDocument`]'s own [`EncryptFrom`] is -/// this layout under the defaults. +/// ([`JsonOptions::ordered`]). A Rust program runs it as +/// `keyset.run(JsonTree::new(options).encryption(), &value, column)`. Its +/// index is the engine's [`Json`] under the same options, which a query +/// derives through. [`JsonTreeDocument`]'s own [`EncryptTarget`] is this +/// layout under the defaults. #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct JsonTree { - index: Json, + options: JsonOptions, } impl JsonTree { /// The layout under `options`. pub fn new(options: JsonOptions) -> Self { - Self { - index: Json::new(options), - } + Self { options } } /// The index the layout seals under, with its options. - pub fn index(&self) -> &Json { - &self.index + pub fn index(&self) -> Json { + Json::new(self.options.clone()) + } + + /// The index the layout declares, as data: the JSON index under its + /// options. + pub fn specs(&self) -> Vec { + vec![Index::::spec(&self.index())] + } + + /// Seal a document as the JSON index lays it out under these options, + /// under the column it is stored in. The [`Identifier`] is the context + /// the data key is requested under and every selector derives under. + /// It is written to `i`. + pub fn encryption<'s, K: 'static>( + &self, + ) -> Encryption<'s, Value, JsonTreeDocument, K, NonEmpty> { + self.sealing().accepting() } -} -impl JsonTree { /// Seal a document under these options, and write its column to `i`. /// /// On its own, the document stores the column the caller passes, and @@ -495,55 +504,12 @@ impl JsonTree { pub(crate) fn sealing<'s, K: 'static>( &self, ) -> Encryption<'s, Value, JsonTreeDocument, K, StoredContext> { - Layout::::encryption::(&self.index) + json_document::(self.options.clone()) .storing(|document, column: Identifier| (column, document)) .try_map(|(column, document)| JsonTreeDocument::envelope(column, &document)) } } -/// A document seals as the JSON index lays it out under the layout's -/// options, under the column it is stored in: the [`Identifier`] is the -/// context the data key is minted under and every selector derives -/// under, and is written to `i`. -impl Layout for JsonTree { - type Target = JsonTreeDocument; - type Index = Json; - fn encryption<'s, K: 'static>( - &self, - ) -> Encryption<'s, Value, JsonTreeDocument, K, NonEmpty> { - self.sealing().accepting() - } - fn index(&self) -> &Json { - &self.index - } -} - -/// A document seals as the JSON index lays it out under the default -/// options ([`JsonTree::default`]): no path ordered, the root sealed. -impl EncryptFrom for JsonTreeDocument { - type Context = NonEmpty; - fn encryption<'s, K: 'static>() -> Encryption<'s, Value, Self, K, Self::Context> { - JsonTree::default().encryption() - } - fn indexes() -> Vec { - JsonTree::default().specs() - } -} - -/// Decrypts to the plaintext document, under the stored identifier. -/// -/// - If you pass the column you expect, as a `NonEmpty`, the -/// stored identifier must equal it. The check runs before any key is -/// requested. -/// - If you pass nothing, the document decrypts under the stored -/// identifier, without that check. -impl DecryptInto for JsonTreeDocument { - type Context = ExpectedContext; - fn decryption(self, context: Self::Context) -> Decryption { - self.open(context) - } -} - impl JsonTreeDocument { /// Open the document, once its stored column has passed the caller's /// check. The check runs before any key is requested. @@ -551,13 +517,16 @@ impl JsonTreeDocument { /// On its own, the document opens under the stored column. As a field /// of a record, it opens under the scopes and bindings of the record /// too. - fn open(self, context: ExpectedContext) -> Decryption { + fn open( + self, + context: ExpectedContext, + ) -> Decryption { let context = match context.decryption_context(self.i.clone()) { Ok(context) => context, Err(error) => return Decryption::failed(error), }; match self.document() { - Ok(document) => DecryptInto::decryption(document, AeadContext::from(context)), + Ok(document) => DecryptSource::decryption(document, AeadContext::from(context).into()), Err(error) => Decryption::failed(error), } } @@ -600,52 +569,27 @@ impl EncryptTarget for JsonTreeDocument { impl DecryptSource for JsonTreeDocument { type ContextInput = ExpectedContext; fn decryption(self, context: Self::ContextInput) -> Decryption { - self.open(context).map(JsonPlaintext::from) - } -} - -/// The same as [`DecryptSource`]. A field of a derived -/// record opens through this trait, as every other EQL type does. -impl DecryptInto for JsonTreeDocument { - type Context = ExpectedContext; - fn decryption(self, context: Self::Context) -> Decryption { - self.open(context).map(JsonPlaintext::from) + self.open(context) } } impl Decryptable for JsonTreeDocument { const DECRYPTABLE: bool = true; } -impl DecryptField for JsonTreeDocument +impl DecryptField for JsonTreeDocument where - Self: DecryptInto

, - Ctx: Into<>::Context>, + Self: DecryptSource

, + Ctx: Into<>::ContextInput>, { fn decryption_field(self, context: Ctx) -> Option> { - Some(>::decryption(self, context.into())) - } -} - -/// An extracted entry opens to the scalar it holds, or to the whole -/// document for a sealed root asked for at `$`, under the column it was -/// extracted from, once it is shown to carry the selector asked for. A -/// container node holds no value of its own; its subtree opens through -/// [`JsonTreeSubtree`]. -impl DecryptInto for JsonTreeExtracted { - type Context = NonEmpty; - fn decryption(self, context: Self::Context) -> Decryption { - match self.entry.node() { - Ok(node) => { - DecryptInto::decryption(self.selector.open(node), AeadContext::from(context)) - } - Err(error) => Decryption::failed(error), - } + Some(>::decryption(self, context.into())) } } /// An extracted entry opens to a [`JsonPlaintext`], which is wiped when it /// is dropped: the scalar the entry holds, or the whole document for a -/// sealed root asked for at `$`. +/// sealed root asked for at `$`. A container node holds no value of its +/// own. Its subtree opens through [`JsonTreeSubtree`]. /// /// The context is required. It is the context the document was sealed /// under: @@ -667,42 +611,6 @@ impl DecryptSource for JsonTreeExtracted { } } -/// The rows of a read open to the value at its path; no node there is an -/// error, which `Option` reads as `None`. -impl DecryptInto for JsonTreeSubtree { - type Context = NonEmpty; - fn decryption(self, context: Self::Context) -> Decryption { - match self.subtree() { - Ok(subtree) => DecryptInto::decryption(subtree, AeadContext::from(context)), - Err(error) => Decryption::failed(error), - } - } -} - -/// The rows of a read open to the value at its path, or `None` where the -/// row has no node there: the four answers `jsonb #>` gives. -impl DecryptInto> for JsonTreeSubtree { - type Context = NonEmpty; - fn decryption(self, context: Self::Context) -> Decryption, K> { - match self.subtree() { - Ok(subtree) => DecryptInto::decryption(subtree, AeadContext::from(context)), - Err(error) => Decryption::failed(error), - } - } -} - -/// The rows of a read open to a list: one value per concrete path the key -/// named that the row has, in order. -impl DecryptInto> for JsonTreeSubtree { - type Context = NonEmpty; - fn decryption(self, context: Self::Context) -> Decryption, K> { - match self.subtree() { - Ok(subtree) => DecryptInto::decryption(subtree, AeadContext::from(context)), - Err(error) => Decryption::failed(error), - } - } -} - /// The rows of a read open to the value at its path, as a /// [`JsonPlaintext`], which is wiped when it is dropped. No node at the /// path is an error: open as an `Option` to read that as diff --git a/packages/eql/crates/eql-bindings/src/v4/mod.rs b/packages/eql/crates/eql-bindings/src/v4/mod.rs index fdefc0da32..375e2c43cb 100644 --- a/packages/eql/crates/eql-bindings/src/v4/mod.rs +++ b/packages/eql/crates/eql-bindings/src/v4/mod.rs @@ -6,8 +6,7 @@ //! [`crate::v3`], with `eql_v4_*` domain names and a `v` of `4`. //! //! **Only these structs derive Stack Encrypt's traits**: `EncryptTarget` and -//! `DecryptSource`. The same derives implement the older `EncryptFrom` and -//! `DecryptInto`. Only Stack Encrypt produces a v4 value, so this module +//! `DecryptSource`. Only Stack Encrypt produces a v4 value, so this module //! exists only with the `stack-encrypt` feature. Stack Encrypt's output //! is not byte-for-byte compatible with cipherstash-client's, so no `eql_v3` //! struct claims it. Which v4 types derive them is @@ -44,8 +43,8 @@ //! domain. What separates the two on the wire is the envelope's //! [`SchemaVersion`], which refuses the other generation's `v`. -/// Generated: the `op::Answers` impls through which Stack Encrypt's typed -/// query chain queries a producible type, each type's +/// Generated: the `op::Answers` impls through which a field marker of a +/// derived record queries a producible type, each type's /// `op::LabelledTarget` impl and its query twin's assembly /// ([`crate::encryption::query`]). Needs only the `stack-encrypt` feature. pub mod answers; diff --git a/packages/eql/crates/eql-codegen/src/bindings.rs b/packages/eql/crates/eql-codegen/src/bindings.rs index 0d76cec8af..b97b30d364 100644 --- a/packages/eql/crates/eql-codegen/src/bindings.rs +++ b/packages/eql/crates/eql-codegen/src/bindings.rs @@ -1537,7 +1537,7 @@ fn render_bindings(dir: &Path) -> Vec<(PathBuf, String)> { /// mixed family's scalar domains under its own name, since v4 has no /// hand-written SteVec module to sit beside), and the Stack Encrypt target /// table and dispatch (`targets.rs`, declared under the `targets` feature in -/// the hand-written `v4/mod.rs`), and the typed query chain's impls +/// the hand-written `v4/mod.rs`), and a typed query's impls /// (`answers.rs`, `stack-encrypt` alone). Stack Encrypt produces v4 only, so the /// target table lives here. fn render_v4_bindings(dir: &Path) -> Vec<(PathBuf, String)> { diff --git a/packages/eql/crates/eql-codegen/src/targets.rs b/packages/eql/crates/eql-codegen/src/targets.rs index 7d87083c52..2c099c6b53 100644 --- a/packages/eql/crates/eql-codegen/src/targets.rs +++ b/packages/eql/crates/eql-codegen/src/targets.rs @@ -14,18 +14,18 @@ //! not. The dispatch has an arm for exactly the producible types, which are //! exactly the domains carrying the `stack-encrypt` derives //! ([`bindings::ENCRYPTION_DOMAINS`](crate::bindings::ENCRYPTION_DOMAINS)): -//! an arm runs `>::encryption()`, which only exists with +//! an arm runs `>::encryption()`, which only exists with //! the derive, and [`target_gap`] is tested to agree with //! [`encryption_gap`](crate::bindings::encryption_gap) on every domain. //! //! The same rows give the operators each producible type answers in Stack -//! Encrypt's typed query chain ([`answers_for_indexes`]), rendered to +//! Encrypt's typed query ([`answers_for_indexes`]), rendered to //! `v4/answers.rs` ([`render_answers_rs`]) under the `stack-encrypt` feature //! alone: it implements `op::Answers` for each, with the type's query twin as //! the query term, and lists them in `ANSWERS` so the encryption tests can //! hold them to the operators the engine's data path answers for the same //! type. A producible type whose twin carries an order index, or more than -//! one index, answers nothing there yet ([`typed_gap`]): the chain sends one +//! one index, answers nothing there yet ([`typed_gap`]): a typed query sends one //! index's term as the twin, and those twins need an order term, or several //! terms at once. //! @@ -69,8 +69,8 @@ pub fn index_key(term: Term) -> &'static str { } } -/// An operator a producible EQL type answers in Stack Encrypt's typed query -/// chain: what the generated file implements `op::Answers` for. +/// An operator a producible EQL type answers in a Stack Encrypt typed +/// query: what the generated file implements `op::Answers` for. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum Answer { /// `op::Eq`, through the equality index. @@ -98,7 +98,7 @@ impl Answer { } } -/// Why the typed query chain has no answer yet for a type carrying these +/// Why a typed query has no answer yet for a type carrying these /// indexes, by [`index_key`] name, or `None` when it answers: a query twin /// is assembled from the one term its type's index derived, so a twin that /// needs an order term (`"ore"`, `"ore_block"` or `"ope"`), or more than one term, is not @@ -109,9 +109,9 @@ pub(crate) fn typed_gap(indexes: &[&str]) -> Option<&'static str> { .iter() .any(|key| matches!(*key, "ore" | "ore_block" | "ope")) { - Some("its twin carries an order term, which the typed query chain does not derive yet") + Some("its twin carries an order term, which a typed query does not derive yet") } else if indexes.len() > 1 { - Some("its twin carries more than one term, which the typed query chain does not assemble yet") + Some("its twin carries more than one term, which a typed query does not assemble yet") } else { None } @@ -121,14 +121,14 @@ pub(crate) fn typed_gap(indexes: &[&str]) -> Option<&'static str> { /// indexes it carries by [`index_key`] name, the same input the engine's /// table reads: `Eq` for the `"eq"` index and `Matches` for the `"match"` /// index, as `Op::answered_by` answers a field that names the type -/// (`TargetDescriptor::operators`). Empty for a type the chain has no +/// (`TargetDescriptor::operators`). Empty for a type a typed query has no /// answer for yet ([`typed_gap`]). The encryption tests compare the two, /// type by type, and hold the gap to exactly those types. /// /// # Panics /// /// On an index key the generator does not know: a new catalog term must say -/// what the typed query chain does with it. +/// what a typed query does with it. pub(crate) fn answers_for_indexes(name: &str, indexes: &[&str]) -> Vec { if typed_gap(indexes).is_some() { return Vec::new(); @@ -139,7 +139,7 @@ pub(crate) fn answers_for_indexes(name: &str, indexes: &[&str]) -> Vec { "eq" => Answer::Eq, "match" => Answer::Matches, other => panic!( - "{name}: the typed query chain has no rule for the {other} index; extend \ + "{name}: a typed query has no rule for the {other} index; extend \ eql-codegen's answers_for_indexes and typed_gap" ), }) @@ -179,7 +179,7 @@ pub(crate) struct Row { /// Empty for every other twin, whose plan derives all its terms. pub(crate) projections: Vec<(&'static str, String)>, pub(crate) reason: Option<&'static str>, - /// The operators the type answers in the typed query chain: none + /// The operators the type answers in a typed query: none /// unless it is producible and has a query twin ([`answers_for_indexes`]). pub(crate) answers: Vec, /// A document family's stored type (`eql_domains::DOCUMENT_FAMILIES`): @@ -300,7 +300,7 @@ fn row(family: &'static DomainFamily, domain: &'static Domain) -> Row { } } -/// The typed query chain's impls for one type that answers an operator: +/// A typed query's impls for one type that answers an operator: /// its query twin's assembly from the one term it carries, `QueryTerm` on /// the twin when an exact operator returns it as is, and one `op::Answers` /// per operator, through Stack Encrypt's index for it, sent as the twin. @@ -324,8 +324,8 @@ fn answer_impls(r: &Row) -> TokenStream { term.json_key() ); let labelled_doc = format!( - " `{}` is sealed under the column a two-segment label names, so its plan is \ - queried under the plan's label.", + " `{}` is sealed under the column a two-segment label names, so a field of this \ + type is queried under its marker's label.", r.name ); let mut out = quote! { @@ -628,13 +628,13 @@ pub fn render_answers_rs() -> String { render_answers_from(&rows()) } -/// Render the typed query chain's impls from the given rows. A file of its +/// Render a typed query's impls from the given rows. A file of its /// own, beside `targets.rs` rather than in it: it needs only the /// `stack-encrypt` feature, so a Rust program queries a v4 type without /// Stack Encrypt's `dynamic` layer, which `targets.rs` (the `targets` /// feature) is built on. fn render_answers_from(rows: &[Row]) -> String { - // The typed query chain: per type that answers an operator, its twin's + // A typed query: per type that answers an operator, its twin's // assembly from the one term it carries, and one `op::Answers` impl per // operator, through Stack Encrypt's index for it, sent as the twin. let answering: Vec<&Row> = rows.iter().filter(|r| !r.answers.is_empty()).collect(); @@ -681,7 +681,8 @@ fn render_answers_from(rows: &[Row]) -> String { }); } - let mod_doc = " How Stack Encrypt's typed query chain queries a producible EQL v4 \ + let mod_doc = + " How a Stack Encrypt typed query, from a field's marker, queries a producible EQL v4 \ type: per type that answers an operator, its `op::Answers` impls, \ its `op::LabelledTarget` impl and its query twin's assembly \ (`crate::encryption::query`). Generated from the catalog, by the \ @@ -693,7 +694,7 @@ fn render_answers_from(rows: &[Row]) -> String { #answer_imports /// The operators each EQL type answers in Stack Encrypt's typed - /// query chain (`op::Answers`), by the wire names + /// query (`op::Answers`), by the wire names /// `stack_encrypt::dynamic::Op` gives them, in catalog order: /// one row per type that answers any. Generated from the rule the /// impls below are, so the encryption tests can hold the impls to @@ -942,7 +943,7 @@ mod tests { ); } - /// A type answers operators in the typed query chain exactly when the + /// A type answers operators in a typed query exactly when the /// engine produces it and it has a query twin, and then by the engine's /// rule over its indexes: `Eq` for the equality index, `Matches` for /// the match index. Spelled out per type, so the table reads without @@ -951,8 +952,8 @@ mod tests { fn a_producible_type_with_a_twin_answers_what_its_indexes_answer() { for row in rows() { let answers: Vec<&str> = row.answers.iter().map(|a| a.name()).collect(); - // A document's operators are the JSON index's, answered in - // the typed chain by the engine's own document target. + // A document's operators are the JSON index's, answered + // through a data plan. if row.reason.is_some() || row.query.is_none() || row.document { assert!(answers.is_empty(), "{}: answers nothing", row.name); continue; @@ -1010,7 +1011,7 @@ mod tests { /// A producible type whose twin carries an order index, or more than /// one index, has no typed answer yet: the generator leaves it out of - /// the typed query chain and says why. + /// a typed query and says why. #[test] fn an_order_index_or_several_indexes_answer_nothing_yet() { for (name, indexes) in [ diff --git a/packages/eql/tests/encryption/tests/json_tree.rs b/packages/eql/tests/encryption/tests/json_tree.rs index e3c69c9a88..e1d7133b58 100644 --- a/packages/eql/tests/encryption/tests/json_tree.rs +++ b/packages/eql/tests/encryption/tests/json_tree.rs @@ -1,6 +1,11 @@ //! The JSON tree family end to end: a document sealed by Stack Encrypt's //! JSON index into `public.eql_v4_json_search`, queried with the v4 SQL, and -//! opened again. The unit tests need no database; the PostgreSQL test +//! opened again. A document seals and opens through the EQL types +//! (`JsonTreeDocument`, `JsonTreeExtracted`, `JsonTreeSubtree`), as a +//! `JsonPlaintext`. A query's operand derives through a data plan that +//! names the type `JsonSearch` under the same column and options +//! ([`contains`], [`contained_by`], [`gte_at`]), since an EQL type has no +//! typed JSON query. The unit tests need no database; the PostgreSQL test //! installs nothing itself (`mise run test:encryption:postgres` installs the //! v4 bundle built from the tree) and is the oracle the design asks for: //! `@>` and `<@` over the encrypted column against PostgreSQL's own @@ -12,18 +17,20 @@ mod common; +use eql_bindings::encryption::EqlTargets; use eql_bindings::v4::json_tree::{ JsonTree, JsonTreeDocument, JsonTreeEntry, JsonTreeExtracted, JsonTreeLeafQuery, JsonTreeQuery, JsonTreeSubtree, }; use eql_bindings::Identifier; use serde_json::{json, Map, Value}; +use stack_encrypt::dynamic::{self, record, Op, Operand, ValueFrame}; use stack_encrypt::json::{ - JsonDocument, JsonOptions, JsonPath, JsonSelector, JsonSubtreeKey, Segment, + JsonDocument, JsonLeafQuery, JsonOptions, JsonPath, JsonPlaintext, JsonQuery, JsonSelector, + JsonSubtreeKey, Segment, }; -use stack_encrypt::plan::{Laid, ValuePlan}; -use stack_encrypt::target::{Json, Layout}; -use stack_encrypt::{op, NonEmpty, Plan}; +use stack_encrypt::target::{json_document, Borrowed, CallerContext}; +use stack_encrypt::{AsProtectedRef as _, DecryptFrom, Label, NonEmpty}; fn column() -> NonEmpty { Identifier::for_column("users", "profile").unwrap() @@ -45,27 +52,148 @@ async fn seal(cipher: &Cipher, value: &Value) -> JsonTreeDocument { .unwrap() } -/// The plan a query derives through: the engine's own document target -/// under the label `users/profile`, which is the same context as the -/// identifier the EQL target stores (the same two parts, rendered -/// `users/profile`), so its selectors are the stored document's, laid -/// out by the JSON index with `$.hello` and `$.number` ordered, the paths -/// the ordering queries below ask at. An EQL type takes its context as -/// an identifier and runs through `encrypt_as`, not through the chain. -fn plan() -> ValuePlan> { - Plan::value::() - .context("users/profile") - .encrypt_with_layout(Json::new(options())) - .build() - .unwrap() +/// The paths that carry ordering terms: `$.hello` and `$.number`, the +/// paths the ordering queries below ask at. [`options`] seals under them, +/// and [`plan`] names them, so a query derives under the options its +/// document was sealed under. +const ORDERED: [&str; 2] = ["$.hello", "$.number"]; + +/// The plan a query derives through: a data plan with one `json` field, +/// `profile`, that names the EQL type `JsonSearch` under `users/profile`. +/// That context is the identifier [`seal`] stores (the same two parts), so +/// a query's selectors are the stored document's. An EQL type has no typed +/// JSON query: a Rust program derives one through a data plan, as a +/// language SDK does. +fn plan() -> record::Plan { + let wire = json!({ + "profile": { + "context": ["users", "profile"], + "type": "json", + "target": {"name": "JsonSearch", "options": {"ordered": ORDERED}} + } + }); + record::plan_with( + dynamic::frame::serde::to_vec(&wire) + .unwrap() + .as_protected_ref(), + &EqlTargets, + ) + .unwrap() } -/// The index options the corpus is sealed under: `$.hello` and -/// `$.number` carry ordering terms. +/// The index options the corpus is sealed under: the [`ORDERED`] paths +/// carry ordering terms. fn options() -> JsonOptions { - JsonOptions::default() - .ordering(["$.hello", "$.number"]) - .unwrap() + JsonOptions::default().ordering(ORDERED).unwrap() +} + +/// The operand of one JSON operator on [`plan`]'s field, with the named +/// inputs a binding sends: the bytes the SQL binds. +async fn operand(cipher: &Cipher, operator: Op, inputs: Value) -> Vec { + let inputs = ValueFrame::from_bytes( + dynamic::frame::serde::to_vec(&inputs) + .unwrap() + .as_protected_ref(), + ) + .unwrap(); + let term = dynamic::query_term( + &cipher.default_keyset(), + &plan(), + "profile", + operator, + inputs, + &EqlTargets, + ) + .unwrap() + .await + .unwrap(); + let Operand::One(bytes) = term.operand() else { + panic!("{}: one operand", term.operator()); + }; + bytes.clone() +} + +/// The needle of `col @> needle`: the `eql_v4.query_json` operand. +async fn contains(cipher: &Cipher, needle: &Value) -> JsonTreeQuery { + let bytes = operand(cipher, Op::Contains, json!({"json": needle.to_string()})).await; + serde_json::from_slice(&bytes).unwrap() +} + +/// The needle of `col <@ needle`: the `eql_v4.query_json` operand. +async fn contained_by(cipher: &Cipher, needle: &Value) -> JsonTreeQuery { + let bytes = operand(cipher, Op::ContainedBy, json!({"json": needle.to_string()})).await; + serde_json::from_slice(&bytes).unwrap() +} + +/// The bound of `(col -> selector) >= bound` at `path`: the +/// `eql_v4.query_json_leaf` operand. +async fn gte_at(cipher: &Cipher, path: &str, value: Value) -> JsonTreeLeafQuery { + let bytes = operand( + cipher, + Op::GteAt, + json!({"path": path, "json": value.to_string()}), + ) + .await; + serde_json::from_slice(&bytes).unwrap() +} + +/// The scalar an extracted entry opens to under the column, through the +/// selector that asked for it. +async fn extract( + cipher: &Cipher, + asked: &JsonSelector, + entry: JsonTreeEntry, +) -> Result { + JsonPlaintext::decrypt_from_with_context( + JsonTreeExtracted::from_selector(asked, entry), + cipher, + column(), + ) + .await + .map(|opened| opened.value().clone()) +} + +/// The value the rows of a read open to under the column, through the key +/// that keyed the read. No node at the path is an error. +async fn read_one( + cipher: &Cipher, + key: &JsonSubtreeKey, + rows: Vec, +) -> Result { + JsonPlaintext::decrypt_from_with_context(JsonTreeSubtree::from_key(key, rows), cipher, column()) + .await + .map(|opened| opened.value().clone()) +} + +/// [`read_one`], with `None` where the row has no node at the path. +async fn read_optional( + cipher: &Cipher, + key: &JsonSubtreeKey, + rows: Vec, +) -> Result, stack_encrypt::Error> { + Option::::decrypt_from_with_context( + JsonTreeSubtree::from_key(key, rows), + cipher, + column(), + ) + .await + .map(|opened| opened.map(|value| value.value().clone())) +} + +/// The rows of a read as a list: one value per concrete path the key named +/// that the row has. +async fn read_list( + cipher: &Cipher, + key: &JsonSubtreeKey, + rows: Vec, +) -> Result, stack_encrypt::Error> { + Vec::::decrypt_from_with_context( + JsonTreeSubtree::from_key(key, rows), + cipher, + column(), + ) + .await + .map(|opened| opened.iter().map(|value| value.value().clone()).collect()) } /// Padded standard base64, as the wire spells a selector. @@ -109,21 +237,17 @@ async fn a_document_round_trips_through_the_typed_target() { .collect(); assert_eq!(keys, ["h", "i", "q", "sv", "v"]); - let opened: Value = cipher - .decrypt_as(stored.clone(), Default::default()) + let opened = JsonPlaintext::decrypt_from(stored.clone(), &cipher) .await .unwrap(); - assert_eq!(opened, fixture()); - let opened: Value = cipher - .decrypt_as(stored.clone(), column().into()) + assert_eq!(opened.value(), &fixture()); + let opened = JsonPlaintext::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); - assert_eq!(opened, fixture()); + assert_eq!(opened.value(), &fixture()); let wrong = Identifier::for_column("users", "bio").unwrap(); - let refused = cipher - .decrypt_as::(stored.clone(), wrong.into()) - .await; + let refused = JsonPlaintext::decrypt_from_with_context(stored.clone(), &cipher, wrong).await; assert!( matches!(refused, Err(stack_encrypt::Error::ContextMismatch { .. })), "{refused:?}" @@ -156,21 +280,13 @@ async fn a_document_round_trips_through_the_typed_target() { entry.j = Some(at as u64); entry.i = Some(stored.i.clone()); entry.v = Some(stored.v); - let scalar: Value = cipher - .decrypt_as( - JsonTreeExtracted::from_selector(&hello, entry.clone()), - column(), - ) - .await - .unwrap(); + let scalar = extract(&cipher, &hello, entry.clone()).await.unwrap(); assert_eq!(scalar, json!("world-1")); let asked = JsonTreeExtracted::from_selector(&hello, entry.clone()); assert_eq!(asked.entry(), &entry); assert_eq!(asked.selector(), &hello); let number = selector("$.number").await; - let refused = cipher - .decrypt_as::(JsonTreeExtracted::from_selector(&number, entry), column()) - .await; + let refused = extract(&cipher, &number, entry).await; assert!( matches!( refused, @@ -181,45 +297,56 @@ async fn a_document_round_trips_through_the_typed_target() { "another path's selector refuses the entry: {refused:?}" ); let bare = stored.sv[at].clone(); - assert!(cipher - .decrypt_as::(JsonTreeExtracted::from_selector(&hello, bare), column()) - .await - .is_err()); + assert!(extract(&cipher, &hello, bare).await.is_err()); } #[tokio::test] async fn the_query_operands_are_the_engines_terms_in_the_sql_contract() { let (cipher, _) = common::cipher().await; - let plan = plan(); - let query = cipher - .query(op::Contains, &json!({"nested": {"list": [{"x": 2}]}})) - .on(&plan) - .await - .unwrap(); - let operand = JsonTreeQuery::try_from(&query).unwrap(); + let keyset = cipher.default_keyset(); + + // A containment operand is the engine's query term, and reads as the + // SQL contract's type without changing. + let bytes = operand( + &cipher, + Op::Contains, + json!({"json": json!({"nested": {"list": [{"x": 2}]}}).to_string()}), + ) + .await; + let query: JsonQuery = serde_json::from_slice(&bytes).unwrap(); + let operand_json: Value = serde_json::from_slice(&bytes).unwrap(); + let needle = JsonTreeQuery::try_from(&query).unwrap(); assert_eq!( - serde_json::to_value(&operand).unwrap(), - serde_json::to_value(&query).unwrap() + needle, + serde_json::from_slice::(&bytes).unwrap() ); - assert_eq!(operand.el.len(), 1); + assert_eq!(serde_json::to_value(&needle).unwrap(), operand_json); + assert_eq!(needle.el.len(), 1); - let bound = cipher - .query(op::AtPath(op::Gte), &("$.number", 1)) - .on(&plan) - .await - .unwrap(); + // An ordering operand is the engine's leaf query, the same way. + let bytes = operand(&cipher, Op::GteAt, json!({"path": "$.number", "json": "1"})).await; + let bound: JsonLeafQuery = serde_json::from_slice(&bytes).unwrap(); let leaf = JsonTreeLeafQuery::try_from(&bound).unwrap(); assert_eq!( serde_json::to_value(&leaf).unwrap(), - serde_json::to_value(&bound).unwrap() + serde_json::from_slice::(&bytes).unwrap() ); + assert_eq!(leaf, gte_at(&cipher, "$.number", json!(1)).await); assert_eq!(leaf.s.0.as_str(), bound.selector().to_string()); // The engine's document under the label and the EQL document under the // identifier carry the same selectors: one context, two spellings. Each // is stored in its own random order, so they compare as multisets, and // the projections as the sets `jsonb @>` reads. - let engine = cipher.encrypt(&fixture()).using(&plan).await.unwrap(); + let label: Label = "users/profile".parse().unwrap(); + let engine: JsonDocument = keyset + .run( + json_document::<_, Borrowed>(options()), + &fixture(), + CallerContext::from(NonEmpty::from(label.clone())), + ) + .await + .unwrap(); let typed = seal(&cipher, &fixture()).await; assert_eq!(engine.entries().len(), typed.sv.len()); let mut a: Vec = engine @@ -236,16 +363,20 @@ async fn the_query_operands_are_the_engines_terms_in_the_sql_contract() { as_set(&serde_json::to_value(&typed.q).unwrap()), "and the same projection" ); - let opened: Value = cipher.open(engine).using(&plan).await.unwrap(); - assert_eq!(opened, fixture()); + // The query finds the document: the needle's set is inside the projection. + assert!(query.matches(engine.selector_set())); + let opened = JsonPlaintext::decrypt_from_with_context(engine, &cipher, NonEmpty::from(label)) + .await + .unwrap(); + assert_eq!(opened.value(), &fixture()); } // ============================================================================= // PostgreSQL // ============================================================================= -/// Settle a chain or a pending on the test's runtime: both are -/// `IntoFuture`, not `Future`. +/// Settle a pending or one of the async helpers above on the test's +/// runtime: a pending is `IntoFuture`, not `Future`. fn wait(runtime: &tokio::runtime::Runtime, f: F) -> F::Output { runtime.block_on(f.into_future()) } @@ -385,7 +516,6 @@ fn postgres_containment_over_the_encrypted_column_is_jsonb_containment() { .build() .unwrap(); let (cipher, _) = wait(&runtime, common::cipher()); - let plan = plan(); let mut db = connect(); db.batch_execute( "CREATE TEMP TABLE json_tree_docs (id int PRIMARY KEY, plain jsonb NOT NULL, doc public.eql_v4_json_search NOT NULL); @@ -413,8 +543,7 @@ fn postgres_containment_over_the_encrypted_column_is_jsonb_containment() { }; let mut checked = 0; for needle in needles() { - let query = wait(&runtime, cipher.query(op::Contains, &needle).on(&plan)).unwrap(); - let operand = serde_json::to_string(&JsonTreeQuery::try_from(&query).unwrap()).unwrap(); + let operand = serde_json::to_string(&wait(&runtime, contains(&cipher, &needle))).unwrap(); let plain = needle.to_string(); let expected = ids( &mut db, @@ -436,8 +565,8 @@ fn postgres_containment_over_the_encrypted_column_is_jsonb_containment() { // `<@`: a stored root scalar inside a needle root array is the one // case the SQL cannot answer (a stored document carries no `alt`). - let reverse = wait(&runtime, cipher.query(op::ContainedBy, &needle).on(&plan)).unwrap(); - let reverse = serde_json::to_string(&JsonTreeQuery::try_from(&reverse).unwrap()).unwrap(); + let reverse = + serde_json::to_string(&wait(&runtime, contained_by(&cipher, &needle))).unwrap(); let mut expected = ids( &mut db, "SELECT id FROM json_tree_docs WHERE plain <@ $1::text::jsonb ORDER BY id", @@ -465,14 +594,8 @@ fn postgres_containment_over_the_encrypted_column_is_jsonb_containment() { // The GIN index serves `@>`: with sequential scans discouraged, the // plan is a bitmap scan over the index. db.batch_execute("SET enable_seqscan = off").unwrap(); - let needle = wait( - &runtime, - cipher - .query(op::Contains, &json!({"a": [{"x": 1}]})) - .on(&plan), - ) - .unwrap(); - let operand = serde_json::to_string(&JsonTreeQuery::try_from(&needle).unwrap()).unwrap(); + let needle = wait(&runtime, contains(&cipher, &json!({"a": [{"x": 1}]}))); + let operand = serde_json::to_string(&needle).unwrap(); let explain: Vec = db .query( "EXPLAIN SELECT id FROM json_tree_docs WHERE doc @> $1::text::jsonb::eql_v4.query_json", @@ -538,7 +661,6 @@ fn postgres_extracts_walks_and_orders_by_path() { .unwrap(); let (cipher, _) = wait(&runtime, common::cipher()); let keyset = cipher.default_keyset(); - let plan = plan(); let mut db = connect(); db.batch_execute( "CREATE TEMP TABLE json_tree_rows (id int PRIMARY KEY, doc public.eql_v4_json_search NOT NULL)", @@ -584,11 +706,7 @@ fn postgres_extracts_walks_and_orders_by_path() { serde_json::from_str(&row.get::<_, String>(0)).unwrap() }; let open = |asked: &JsonSelector, e: JsonTreeEntry| -> Value { - wait( - &runtime, - cipher.decrypt_as(JsonTreeExtracted::from_selector(asked, e), column()), - ) - .unwrap() + wait(&runtime, extract(&cipher, asked, e)).unwrap() }; // `->` by selector opens to the leaf; a missing path is NULL. @@ -684,11 +802,7 @@ fn postgres_extracts_walks_and_orders_by_path() { assert!(walked .iter() .all(|e| e.d.is_some() && e.j.is_some() && e.h.is_some() && e.v.is_some())); - let value: Option = wait( - &runtime, - cipher.decrypt_as(JsonTreeSubtree::from_key(&nested, walked), column()), - ) - .unwrap(); + let value: Option = wait(&runtime, read_optional(&cipher, &nested, walked)).unwrap(); assert_eq!(value, expected, "row {id}"); } // The default budget, as a caller who names none gets it. @@ -726,11 +840,7 @@ fn postgres_extracts_walks_and_orders_by_path() { let root = read(&mut db, &nested, 1, 6); assert_eq!(root.len(), 1, "past the budget, the root alone"); assert_eq!((root[0].j, root[0].d), (Some(0), Some(0))); - let value: Value = wait( - &runtime, - cipher.decrypt_as(JsonTreeSubtree::from_key(&nested, root), column()), - ) - .unwrap(); + let value: Value = wait(&runtime, read_one(&cipher, &nested, root)).unwrap(); assert_eq!(value, fixture()["nested"]); let garbage: i64 = db .query_one( @@ -764,11 +874,7 @@ fn postgres_extracts_walks_and_orders_by_path() { assert_eq!(xs.tokens().len(), 65, "64 positions and the probe"); for budget in [0, 1, 64] { let rows = read(&mut db, &xs, 1, budget); - let values: Vec = wait( - &runtime, - cipher.decrypt_as(JsonTreeSubtree::from_key(&xs, rows), column()), - ) - .unwrap(); + let values: Vec = wait(&runtime, read_list(&cipher, &xs, rows)).unwrap(); assert_eq!(values, vec![json!(1), json!(2)], "budget {budget}"); } @@ -776,10 +882,7 @@ fn postgres_extracts_walks_and_orders_by_path() { let second = subtree_key("$.nested.list[1]"); let value: Value = wait( &runtime, - cipher.decrypt_as( - JsonTreeSubtree::from_key(&second, read(&mut db, &second, 1, 0)), - column(), - ), + read_one(&cipher, &second, read(&mut db, &second, 1, 0)), ) .unwrap(); assert_eq!(value, json!({"x": 2})); @@ -848,15 +951,8 @@ fn postgres_extracts_walks_and_orders_by_path() { // Orderings at a path: the rows whose `$.number` is in range, and // sorted by the leaf's term. - let at_least_one = wait( - &runtime, - cipher - .query(op::AtPath(op::Gte), &("$.number", 1)) - .on(&plan), - ) - .unwrap(); - let at_least_one = - serde_json::to_string(&JsonTreeLeafQuery::try_from(&at_least_one).unwrap()).unwrap(); + let at_least_one = wait(&runtime, gte_at(&cipher, "$.number", json!(1))); + let at_least_one = serde_json::to_string(&at_least_one).unwrap(); let number = selector("$.number"); let ids: Vec = db .query( @@ -998,7 +1094,6 @@ fn postgres_the_planner_picks_the_indexes_without_help() { .build() .unwrap(); let (cipher, _) = wait(&runtime, common::cipher()); - let plan = plan(); let mut db = connect(); let version: i32 = db @@ -1010,27 +1105,15 @@ fn postgres_the_planner_picks_the_indexes_without_help() { // The operands: a containment needle per shape, and one range bound at // `$.number`, whose selector the functional index is built on. - let bound = wait( - &runtime, - cipher - .query(op::AtPath(op::Gte), &("$.number", 1)) - .on(&plan), - ) - .unwrap(); - let number = bound.selector().to_string(); + let bound = wait(&runtime, gte_at(&cipher, "$.number", json!(1))); + let number = bound.s.0.as_str().to_owned(); let leaf = |value: i64| { - let query = wait( - &runtime, - cipher - .query(op::AtPath(op::Gte), &("$.number", value)) - .on(&plan), - ) - .unwrap(); - serde_json::to_string(&JsonTreeLeafQuery::try_from(&query).unwrap()).unwrap() + let query = wait(&runtime, gte_at(&cipher, "$.number", json!(value))); + serde_json::to_string(&query).unwrap() }; let needle = |value: Value| { - let query = wait(&runtime, cipher.query(op::Contains, &value).on(&plan)).unwrap(); - serde_json::to_string(&JsonTreeQuery::try_from(&query).unwrap()).unwrap() + let query = wait(&runtime, contains(&cipher, &value)); + serde_json::to_string(&query).unwrap() }; let explain = @@ -1619,26 +1702,18 @@ fn postgres_every_subtree_read_answers_as_jsonb_does() { "{at}: read in full" ); } - let opened: Option = wait( - &runtime, - cipher.decrypt_as(JsonTreeSubtree::from_key(&key, rows.clone()), column()), - ) - .unwrap_or_else(|error| panic!("{at}, budget {budget}: {error}")); + let opened: Option = + wait(&runtime, read_optional(&cipher, &key, rows.clone())) + .unwrap_or_else(|error| panic!("{at}, budget {budget}: {error}")); assert_eq!(opened, expected, "{at}, budget {budget}, as Option"); - let list: Vec = wait( - &runtime, - cipher.decrypt_as(JsonTreeSubtree::from_key(&key, rows.clone()), column()), - ) - .unwrap(); + let list: Vec = + wait(&runtime, read_list(&cipher, &key, rows.clone())).unwrap(); assert_eq!( list, expected.clone().into_iter().collect::>(), "{at}, as Vec" ); - let one: Result = wait( - &runtime, - cipher.decrypt_as(JsonTreeSubtree::from_key(&key, rows), column()), - ); + let one: Result = wait(&runtime, read_one(&cipher, &key, rows)); match (&expected, one) { (Some(expected), Ok(one)) => assert_eq!(&one, expected, "{at}, as Value"), (None, Err(stack_encrypt::Error::OpenedType { expected })) => { @@ -1699,14 +1774,7 @@ fn postgres_every_subtree_read_answers_as_jsonb_does() { .unwrap() .get(0); let entry: JsonTreeEntry = serde_json::from_str(&arrow.unwrap()).unwrap(); - let value: Value = wait( - &runtime, - cipher.decrypt_as( - JsonTreeExtracted::from_selector(&selector, entry), - column(), - ), - ) - .unwrap(); + let value: Value = wait(&runtime, extract(&cipher, &selector, entry)).unwrap(); assert_eq!(Some(value), expected, "{at}: `->` and the read agree"); } } @@ -1816,17 +1884,10 @@ fn postgres_wildcard_reads_answer_as_jsonb_path_query() { for budget in [0, 1, 64] { let at = format!("{table} {pattern}, row {id}, budget {budget}"); let rows = subtree_rows(&mut db, &table, id, &key, budget); - let list: Result, stack_encrypt::Error> = wait( - &runtime, - cipher.decrypt_as( - JsonTreeSubtree::from_key(&key, rows.clone()), - column(), - ), - ); - let one: Result, stack_encrypt::Error> = wait( - &runtime, - cipher.decrypt_as(JsonTreeSubtree::from_key(&key, rows), column()), - ); + let list: Result, stack_encrypt::Error> = + wait(&runtime, read_list(&cipher, &key, rows.clone())); + let one: Result, stack_encrypt::Error> = + wait(&runtime, read_optional(&cipher, &key, rows)); if too_long { for result in [list.map(|_| ()), one.map(|_| ())] { match result { @@ -1904,11 +1965,8 @@ fn postgres_a_position_reaches_array_elements_only() { if expected.is_none() { assert!(walked.is_empty(), "{path}, row {id}: jsonb has no node"); } - let value: Option = wait( - &runtime, - cipher.decrypt_as(JsonTreeSubtree::from_key(&key, walked), column()), - ) - .unwrap(); + let value: Option = + wait(&runtime, read_optional(&cipher, &key, walked)).unwrap(); assert_eq!(value, expected, "{path}, row {id}"); } let counts: Vec<(i32, Option)> = db @@ -1997,16 +2055,10 @@ fn postgres_hostile_answers_are_refused_by_the_client() { subtree_rows(db, "json_tree_hostile", id, key, 0) }; let extracted = |asked: &JsonSelector, entry: JsonTreeEntry| -> Result { - wait( - &runtime, - cipher.decrypt_as(JsonTreeExtracted::from_selector(asked, entry), column()), - ) + wait(&runtime, extract(&cipher, asked, entry)) }; let opened = |key: &JsonSubtreeKey, rows: Vec| -> Result { - wait( - &runtime, - cipher.decrypt_as(JsonTreeSubtree::from_key(key, rows), column()), - ) + wait(&runtime, read_one(&cipher, key, rows)) }; let wrong = |result: Result, what: &str| { assert!( @@ -2384,10 +2436,8 @@ fn postgres_reads_never_answer_wrongly_under_tampering() { if truncate_rows && rows.len() > 1 { let _ = rows.pop(); } - let opened: Result, stack_encrypt::Error> = wait( - &runtime, - cipher.decrypt_as(JsonTreeSubtree::from_key(&key, rows), column()), - ); + let opened: Result, stack_encrypt::Error> = + wait(&runtime, read_optional(&cipher, &key, rows)); let outcome = match opened { Err(_) => "refused", Ok(found) if found == expected => "the right answer", diff --git a/packages/eql/tests/encryption/tests/json_tree/elements.rs b/packages/eql/tests/encryption/tests/json_tree/elements.rs index b373802e58..b5b410876e 100644 --- a/packages/eql/tests/encryption/tests/json_tree/elements.rs +++ b/packages/eql/tests/encryption/tests/json_tree/elements.rs @@ -22,7 +22,6 @@ //! included. use super::*; -use stack_encrypt::AsProtectedRef as _; use eql_bindings::encryption::EqlTargets; use stack_encrypt::dynamic::{self, record, Op, Operand, ValueFrame}; diff --git a/packages/eql/tests/encryption/tests/json_tree/generated.rs b/packages/eql/tests/encryption/tests/json_tree/generated.rs index 006e5be228..214326e0d7 100644 --- a/packages/eql/tests/encryption/tests/json_tree/generated.rs +++ b/packages/eql/tests/encryption/tests/json_tree/generated.rs @@ -485,7 +485,6 @@ impl Containment { struct Scenario { runtime: tokio::runtime::Runtime, cipher: Cipher, - plan: ValuePlan>, db: RefCell, } @@ -507,7 +506,6 @@ fn scenario() -> Scenario { Scenario { runtime, cipher, - plan: plan(), db: RefCell::new(db), } } @@ -543,21 +541,14 @@ impl Scenario { .iter() .map(|needle| { let query = match containment { - Containment::Contains => wait( - &self.runtime, - self.cipher - .query(op::Contains, &needle.value) - .on(&self.plan), - ), - Containment::ContainedBy => wait( - &self.runtime, - self.cipher - .query(op::ContainedBy, &needle.value) - .on(&self.plan), - ), - } - .unwrap(); - serde_json::to_string(&JsonTreeQuery::try_from(&query).unwrap()).unwrap() + Containment::Contains => { + wait(&self.runtime, contains(&self.cipher, &needle.value)) + } + Containment::ContainedBy => { + wait(&self.runtime, contained_by(&self.cipher, &needle.value)) + } + }; + serde_json::to_string(&query).unwrap() }) .collect() } @@ -836,10 +827,7 @@ mod given_generated_documents_in_postgres { let entry: JsonTreeEntry = serde_json::from_str(&got).unwrap(); let value = wait( &scenario.runtime, - scenario.cipher.decrypt_as::( - JsonTreeExtracted::from_selector(&asked[i], entry), - column(), - ), + extract(&scenario.cipher, &asked[i], entry), ) .map_err(|e| { TestCaseError::fail(format!( diff --git a/packages/eql/tests/encryption/tests/json_tree/predicate.rs b/packages/eql/tests/encryption/tests/json_tree/predicate.rs index 2d7b0e43d9..b26cc257a6 100644 --- a/packages/eql/tests/encryption/tests/json_tree/predicate.rs +++ b/packages/eql/tests/encryption/tests/json_tree/predicate.rs @@ -9,7 +9,6 @@ use super::elements::{derive, eql_text, frame_of}; use super::*; use eql_bindings::encryption::EqlTargets; -use stack_encrypt::AsProtectedRef as _; use stack_encrypt::dynamic::{self, record, Op, Operand}; use stack_encrypt::KeysetCipher; diff --git a/packages/eql/tests/encryption/tests/json_tree_data_plan.rs b/packages/eql/tests/encryption/tests/json_tree_data_plan.rs index 7bb373e986..95a7f22e68 100644 --- a/packages/eql/tests/encryption/tests/json_tree_data_plan.rs +++ b/packages/eql/tests/encryption/tests/json_tree_data_plan.rs @@ -1,6 +1,8 @@ //! An encrypted JSON document through a data plan: the path a language SDK -//! takes (the Go guest installs the same resolver), against the typed Rust -//! path it must agree with. +//! takes (the Go guest installs the same resolver), and the path a Rust +//! program queries a document by, since an EQL type has no typed JSON +//! query. Sealing is held to the typed Rust path, `JsonTree::new(options)`, +//! which it must agree with. //! //! A data-plan field names the target `JsonSearch`, declares the `json` //! type, and hands its document over as JSON text. The engine parses it by @@ -15,6 +17,11 @@ //! document under the same column, and each opens what the other sealed; //! - text that is not a document is refused before any key is requested, //! naming the field and a code, never a byte of the text; +//! - each operator's operand reads as the EQL type the SQL binds it as +//! (`JsonTreeQuery`, `JsonTreeLeafQuery`, a selector, a list of tokens), +//! is bound as the domain the target names for it, and finds the stored +//! documents it should, decided here with no database; +//! - rows open against the request that prepared them; //! - a document sealed under a scope and a binding is found by a query //! under the scope, and read under the scope and the binding. @@ -25,13 +32,12 @@ use std::collections::BTreeMap; use eql_bindings::encryption::targets as eql_targets; use eql_bindings::encryption::EqlTargets; -use eql_bindings::v4::json_tree::{JsonTree, JsonTreeDocument}; +use eql_bindings::v4::json_tree::{JsonTree, JsonTreeDocument, JsonTreeLeafQuery, JsonTreeQuery}; use eql_bindings::Identifier; use serde_json::{json, Value}; use stack_encrypt::dynamic::{self, record, TargetError, ValueFrame}; -use stack_encrypt::json::{JsonDocument, JsonOptions, JsonQuery}; -use stack_encrypt::target::Layout; -use stack_encrypt::{CipherText, Encrypt, KeysetCipher, Label, NonEmpty}; +use stack_encrypt::json::{JsonDocument, JsonOptions, JsonPlaintext, JsonQuery}; +use stack_encrypt::{CipherText, DecryptFrom, Encrypt, KeysetCipher, Label, NonEmpty}; /// The options the documents are sealed under on both paths. fn options() -> JsonOptions { @@ -211,11 +217,7 @@ async fn the_data_plan_and_the_typed_layout_agree() { .unwrap(); let data = stored(&sealed); let typed: JsonTreeDocument = keyset - .run( - Layout::::encryption::(&JsonTree::new(options())), - &document, - column(), - ) + .run(JsonTree::new(options()).encryption(), &document, column()) .await .unwrap(); assert_eq!(data.i, typed.i, "{text}: the column"); @@ -232,11 +234,10 @@ async fn the_data_plan_and_the_typed_layout_agree() { ); // The typed path opens the data plan's document... - let opened: Value = cipher - .decrypt_as::(data, column().into()) + let opened = JsonPlaintext::decrypt_from_with_context(data, &cipher, column()) .await .unwrap(); - assert_eq!(opened, document); + assert_eq!(opened.value(), &document); // ...and the data plan opens the typed path's. let record = CipherText::Map(vec![( "profile".to_owned(), @@ -404,23 +405,9 @@ async fn text_that_is_not_a_document_is_refused_before_any_key() { } // ============================================================================= -// Operators: the data path's operands against the typed chain's +// Operators: each operand's shape, and what it finds // ============================================================================= -/// A typed plan over the same column and options, as a Rust program -/// queries a document: the engine's document target under the label -/// `users/profile`, the same context as the EQL identifier. -fn typed_plan() -> stack_encrypt::plan::ValuePlan< - Value, - stack_encrypt::plan::Laid, -> { - stack_encrypt::Plan::value::() - .context("users/profile") - .encrypt_with_layout(stack_encrypt::target::Json::new(options())) - .build() - .unwrap() -} - /// An operator's input as a binding writes it: an object of named inputs. fn input(entries: &[(&str, Value)]) -> ValueFrame { let object: serde_json::Map = entries @@ -531,47 +518,119 @@ fn a_document_field_answers_the_json_operators() { ); } -/// Every operand the data path derives is the typed chain's, in the EQL -/// wire form, bound as the domain the target names for it. A containment -/// or equality operand is the same selector set, in its own random order. +/// A document sealed through the plan, as the engine's document: what an +/// operand is decided against here, with no database. +async fn sealed( + keyset: &KeysetCipher<'_, common::Observed>, + plan: &record::Plan, + document: &Value, +) -> JsonDocument { + let text = document.to_string(); + let row = record::encrypt_with( + keyset, + frame(BTreeMap::from([("profile", text.as_str())])), + plan, + &EqlTargets, + ) + .unwrap() + .await + .unwrap(); + engine_document(&stored(&row)) +} + +/// The ordering term the document stores at a selector, as the SQL's +/// `col -> selector` reads it: bytes, which order as the plaintexts do. +fn stored_ordering_term(document: &JsonDocument, selector: &str) -> Vec { + use base64::Engine; + let selector: [u8; 16] = base64::engine::general_purpose::STANDARD + .decode(selector) + .unwrap() + .try_into() + .unwrap(); + document + .entries() + .iter() + .find(|entry| entry.selector() == &selector) + .expect("the document has a node at the selector") + .ordering_term() + .expect("the path is ordered") + .to_vec() +} + +/// Every operand the data path derives is in the EQL wire form: it reads +/// as the type the SQL binds it as, and is bound as the domain the target +/// names for it. Each finds the stored documents it should: a containment +/// or equality operand as `jsonb @>` decides it over the stored selector +/// sets, and an ordering operand as the SQL compares ordering terms. #[tokio::test] -async fn every_operand_is_the_typed_chains() { - use stack_encrypt::op; +async fn every_operand_is_in_the_eql_wire_form_and_finds_what_it_should() { + use base64::Engine; let (cipher, calls) = common::cipher().await; let keyset = cipher.default_keyset(); let plan = plan(); - let typed = typed_plan(); let document = json!({"visits": [{"at": 3}], "name": "Alice"}); + // Three stored documents: one that holds `document` and more, one that + // is part of it, and one that differs from it. + let more = sealed( + &keyset, + &plan, + &json!({"visits": [{"at": 3}, {"at": 1}], "name": "Alice", "age": 34}), + ) + .await; + let part = sealed(&keyset, &plan, &json!({"name": "Alice"})).await; + let other = sealed( + &keyset, + &plan, + &json!({"visits": [{"at": 4}], "name": "Bob", "age": 52}), + ) + .await; + calls.lock().unwrap().generate.clear(); - for (operator, typed_op) in [("contains", true), ("contained_by", false)] { - let term = data_term( - &keyset, - &plan, - operator, - &[("json", json!(document.to_string()))], - ) - .await; - let typed = if typed_op { - cipher - .query(op::Contains, &document) - .on(&typed) - .await - .unwrap() - } else { - cipher - .query(op::ContainedBy, &document) - .on(&typed) - .await - .unwrap() - }; + // A needle reads as the EQL type, and writes back as the operand. + let needle = |term: &dynamic::QueryTerm| -> JsonQuery { + let operand = one(term); + let typed: JsonTreeQuery = serde_json::from_value(operand.clone()).unwrap(); assert_eq!( - containment(one(&term)), - containment(serde_json::to_value(&typed).unwrap()), - "{operator}" + serde_json::to_value(&typed).unwrap(), + operand, + "{}", + term.operator() ); assert_eq!(term.domain(), Some("eql_v4.query_json")); assert_eq!(term.encoding(), "eql"); - } + containment(operand) + }; + + let contains = needle( + &data_term( + &keyset, + &plan, + "contains", + &[("json", json!(document.to_string()))], + ) + .await, + ); + assert!(contains.matches(more.selector_set()), "@> a superset"); + assert!(!contains.matches(part.selector_set()), "@> a part"); + assert!(!contains.matches(other.selector_set()), "@> another"); + + // `<@` sends the same needle, and the SQL reads it the other way: the + // stored document's selector set is inside the needle's. + let contained_by = needle( + &data_term( + &keyset, + &plan, + "contained_by", + &[("json", json!(document.to_string()))], + ) + .await, + ); + assert_eq!(contained_by, contains, "one needle for both directions"); + let within = + |stored: &JsonDocument| contained_by.selector_set().contains(stored.selector_set()); + assert!(within(&part), "<@ a part"); + assert!(!within(&more), "<@ a superset"); + assert!(!within(&other), "<@ another"); let eq = data_term( &keyset, @@ -580,16 +639,10 @@ async fn every_operand_is_the_typed_chains() { &[("path", json!("$.name")), ("json", json!("\"Alice\""))], ) .await; - let typed_eq = cipher - .query(op::AtPath(op::Eq), &("$.name", "Alice")) - .on(&typed) - .await - .unwrap(); - assert_eq!( - containment(one(&eq)), - containment(serde_json::to_value(&typed_eq).unwrap()) - ); - assert_eq!(eq.domain(), Some("eql_v4.query_json")); + let eq = needle(&eq); + assert!(eq.matches(more.selector_set()), "$.name == Alice"); + assert!(eq.matches(part.selector_set()), "$.name == Alice"); + assert!(!eq.matches(other.selector_set()), "$.name == Alice"); let lt = data_term( &keyset, @@ -598,13 +651,26 @@ async fn every_operand_is_the_typed_chains() { &[("path", json!("$.age")), ("json", json!("40"))], ) .await; - let typed_lt = cipher - .query(op::AtPath(op::Lt), &("$.age", 40)) - .on(&typed) + assert_eq!(lt.domain(), Some("eql_v4.query_json_leaf")); + assert_eq!(lt.encoding(), "eql"); + // A bound reads as the EQL type: the path's selector, which `->` takes, + // and the value's ordering term. + let bound: JsonTreeLeafQuery = serde_json::from_value(one(<)).unwrap(); + assert_eq!(serde_json::to_value(&bound).unwrap(), one(<)); + let age = keyset + .json_selector( + &stack_encrypt::json::JsonPath::parse("$.age").unwrap(), + column(), + ) .await + .unwrap() + .to_string(); + assert_eq!(bound.s.0.as_str(), age); + let forty = base64::engine::general_purpose::STANDARD + .decode(bound.op.0.as_str()) .unwrap(); - assert_eq!(one(<), serde_json::to_value(&typed_lt).unwrap()); - assert_eq!(lt.domain(), Some("eql_v4.query_json_leaf")); + assert!(stored_ordering_term(&more, &age) < forty, "34 < 40"); + assert!(stored_ordering_term(&other, &age) > forty, "52 > 40"); let between = data_term( &keyset, @@ -729,8 +795,8 @@ fn a_json_operator_refuses_what_it_cannot_answer() { let error = check(operator, entries).unwrap_err(); assert!(!format!("{error}").contains("secret"), "{error}"); match error.reason() { - // An input refusal names the field; the engine's own (a term - // error) is the same refusal the typed chain makes. + // An input refusal names the field and a reason; the engine's + // own refusal is a term error, which carries no reason. Some(reason) => { assert_eq!(error.field(), Some("profile"), "{error}"); reason.as_str() diff --git a/packages/eql/tests/encryption/tests/record_fields.rs b/packages/eql/tests/encryption/tests/record_fields.rs index 022e7950f4..f1f6d8025d 100644 --- a/packages/eql/tests/encryption/tests/record_fields.rs +++ b/packages/eql/tests/encryption/tests/record_fields.rs @@ -7,7 +7,8 @@ //! for a field that names the type. A data plan is the path the Go module //! takes. Here: //! -//! - a record with EQL fields encrypts and decrypts through the new API; +//! - a record with EQL fields encrypts with `encrypt_into` and decrypts +//! with `decrypt_from`; //! - each field writes the terms the type writes on its own, and each value //! opens through the other path; //! - a query from the field's marker gives the stored term; @@ -27,8 +28,9 @@ //! - a renamed field keeps its column with `identity`, and reads the rows //! written under its old name. With no `identity` they are refused; //! - a JSON document, `JsonTreeDocument`, encrypts from a `JsonPlaintext` -//! through the new API, writes the search terms the old path writes, and -//! is a record field too. +//! through `encrypt_into_with_context`, writes the search terms that +//! `JsonTree`'s layout writes when it is run directly, and is a record +//! field too. mod common; @@ -42,7 +44,7 @@ use serde_json::{json, Value}; use stack_encrypt::dynamic::{self, record, Op, Operand, Planned, QueryTerm, ValueFrame}; use stack_encrypt::json::JsonPlaintext; use stack_encrypt::target::transcode::Reader as _; -use stack_encrypt::target::{AeadContext, Bind, Layout, Parts, Scope}; +use stack_encrypt::target::{AeadContext, Bind, Parts, Scope}; use stack_encrypt::testing::{bind, scope, Expect}; use stack_encrypt::{ assert_context, nonempty, AsProtectedRef as _, CipherText, Controlled as _, DecryptFrom, @@ -433,7 +435,9 @@ async fn an_eql_field_stores_its_column_and_not_the_scope_or_the_binding() { // row: it opens as a Stack Encrypt leaf under exactly that context. let leaf = stored.email.c.clone().read(NativeLeaf).unwrap(); let context = AeadContext::from(tenant_and_row()).extend(nonempty!("members").with("email")); - let opened: String = cipher.decrypt_as(leaf, context).await.unwrap(); + let opened = String::decrypt_from_with_context(leaf, &cipher, context) + .await + .unwrap(); assert_eq!(opened, "ada@example.com"); // The term is derived under the column, then the tenant. A query from @@ -961,26 +965,26 @@ async fn a_json_document_encrypts_from_a_json_plaintext() { .encrypt_into_with_context(&keyset, column()) .await .unwrap(); - let old: JsonTreeDocument = keyset + let direct: JsonTreeDocument = keyset .run(JsonTree::default().encryption(), &profile(), column()) .await .unwrap(); assert_eq!( document_terms(&stored), - document_terms(&old), + document_terms(&direct), "the same envelope and search terms" ); - // Each path opens what the other sealed. - let opened = JsonPlaintext::decrypt_from_with_context(old, &keyset, column()) + // Each document opens under its column, through the keyset or the + // cipher. + let opened = JsonPlaintext::decrypt_from_with_context(direct, &keyset, column()) .await .unwrap(); assert_eq!(opened.value(), &profile()); - let opened: Value = cipher - .decrypt_as(stored.clone(), column().into()) + let opened = JsonPlaintext::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); - assert_eq!(opened, profile()); + assert_eq!(opened.value(), &profile()); // Without a column, the document opens under the one it stores. let opened = JsonPlaintext::decrypt_from(stored.clone(), &keyset) @@ -1030,7 +1034,7 @@ async fn a_json_document_is_a_record_field() { }, "the field's column is the record's scope and the field's name" ); - let old: JsonTreeDocument = keyset + let direct: JsonTreeDocument = keyset .run( JsonTree::default().encryption(), &profile(), @@ -1038,7 +1042,7 @@ async fn a_json_document_is_a_record_field() { ) .await .unwrap(); - assert_eq!(document_terms(&stored.profile), document_terms(&old)); + assert_eq!(document_terms(&stored.profile), document_terms(&direct)); let opened = Customer::decrypt_from(stored, &keyset).await.unwrap(); assert_eq!(opened.id, 3); diff --git a/packages/eql/tests/encryption/tests/targets.rs b/packages/eql/tests/encryption/tests/targets.rs index 6e7c6b4e89..f85d2dd8c1 100644 --- a/packages/eql/tests/encryption/tests/targets.rs +++ b/packages/eql/tests/encryption/tests/targets.rs @@ -1,7 +1,8 @@ //! EQL types as plan field targets, reached by name: the dispatch in -//! `eql_bindings::encryption::targets` must run the same plan the typed -//! `encrypt_as::` call runs, refuse what the table says it cannot -//! produce, and the table must be the catalog. +//! `eql_bindings::encryption::targets` must run the same encryption the +//! typed call runs (`value.encrypt_into_with_context::(..)`), +//! refuse what the table says it cannot produce, and the table must be the +//! catalog. mod common; @@ -10,7 +11,7 @@ use eql_bindings::v4::text::{TextEq, TextEqQuery}; use eql_bindings::{v4, Identifier}; use eql_domains::{Term, CATALOG}; use stack_encrypt::dynamic::{ValueFrame, ValueKind}; -use stack_encrypt::{Decrypt, Encrypt, Label}; +use stack_encrypt::{Decrypt, DecryptFrom, Encrypt, EncryptInto, Label}; /// A Rust value as the engine's runtime plaintext: its frame. fn frame(value: T) -> ValueFrame { @@ -49,12 +50,12 @@ fn refused(result: Result) -> TargetError { mod given_text_eq { use super::*; - /// The dispatch and the typed call are the same plan: same identifier, - /// same equality term, and each side's value opens through the other. - /// The ciphertexts differ because every write seals under a fresh key, - /// so byte identity is asserted on everything but `c`. + /// The dispatch and the typed call run the same encryption: same + /// identifier, same equality term, and each side's value opens through + /// the other. The ciphertexts differ because every write seals under a + /// fresh key, so byte identity is asserted on everything but `c`. #[tokio::test] - async fn produces_what_encrypt_as_produces() { + async fn produces_what_the_typed_call_produces() { let (cipher, calls) = common::cipher().await; let keyset = cipher.default_keyset(); for value in ["alice@example.com", "", "雪☃", "cafe\u{301}"] { @@ -63,8 +64,9 @@ mod given_text_eq { .await .unwrap(); let by_name: TextEq = serde_json::from_slice(&by_name).unwrap(); - let typed: TextEq = keyset - .encrypt_as(&value.to_owned(), column()) + let typed: TextEq = value + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); assert_eq!(by_name.v, typed.v, "{value:?}: same envelope version"); @@ -78,10 +80,10 @@ mod given_text_eq { "{value:?}: each write seals under a fresh key" ); - let typed_opened: String = cipher - .decrypt_as(by_name.clone(), column().into()) - .await - .unwrap(); + let typed_opened = + String::decrypt_from_with_context(by_name.clone(), &cipher, column()) + .await + .unwrap(); assert_eq!( typed_opened, value, "{value:?}: the typed path opens the named value" @@ -194,8 +196,9 @@ mod given_text_eq { .unwrap() .await .unwrap(); - let typed: TextEqQuery = keyset - .encrypt_as(&"alice@example.com".to_owned(), column()) + let typed: TextEqQuery = "alice@example.com" + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); assert_eq!( @@ -782,8 +785,9 @@ mod the_cross_language_fixture { committed, rendered, "the committed fixture is what the dispatch derives today" ); - let typed: TextEqQuery = keyset - .encrypt_as(&PLAINTEXT.to_owned(), column()) + let typed: TextEqQuery = PLAINTEXT + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); assert_eq!( diff --git a/packages/eql/tests/encryption/tests/text_eq.rs b/packages/eql/tests/encryption/tests/text_eq.rs index d3526e38d3..0d246d0189 100644 --- a/packages/eql/tests/encryption/tests/text_eq.rs +++ b/packages/eql/tests/encryption/tests/text_eq.rs @@ -7,8 +7,9 @@ use eql_bindings::v4::text::{TextEq, TextEqQuery}; use eql_bindings::v4::SchemaVersion; use eql_bindings::Identifier; use stack_encrypt::target::transcode::{Reader, Transcode}; -use stack_encrypt::{Encrypt, NonEmpty}; +use stack_encrypt::{DecryptFrom, Encrypt, EncryptInto, NonEmpty}; use vitaminc_aead::plaintext::Sink; +use zeroize::Zeroize; fn column() -> NonEmpty { Identifier::for_column("users", "email").unwrap() @@ -27,7 +28,11 @@ async fn text_round_trips_through_json_and_both_canonical_directions() { "a\0b", ] { let value = text.to_owned(); - let stored: TextEq = keyset.encrypt_as(&value, column()).await.unwrap(); + let stored: TextEq = value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let json = serde_json::to_value(&stored).unwrap(); assert_eq!(json["v"], 4, "Stack Encrypt writes EQL v4"); assert_eq!(json["i"], serde_json::json!({"t":"users", "c":"email"})); @@ -49,7 +54,7 @@ async fn text_round_trips_through_json_and_both_canonical_directions() { opened, value, "EQL adds no plaintext encoding or envelope-field AAD" ); - let opened: String = cipher.decrypt_as(stored, Default::default()).await.unwrap(); + let opened = String::decrypt_from(stored, &cipher).await.unwrap(); assert_eq!(opened, value); let native = keyset.encrypt_tree(value.clone(), column()).await.unwrap(); @@ -64,7 +69,9 @@ async fn text_round_trips_through_json_and_both_canonical_directions() { .read(Hmac256::visitor()) .unwrap(), }; - let opened: String = cipher.decrypt_as(stored, column().into()).await.unwrap(); + let opened = String::decrypt_from_with_context(stored, &cipher, column()) + .await + .unwrap(); assert_eq!( opened, value, "native ciphertext opens through the EQL target" @@ -82,12 +89,22 @@ async fn repeated_text_encryption_uses_distinct_ciphertexts_and_one_equality_ter let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); let value = "alice@example.com".to_owned(); - let first: TextEq = keyset.encrypt_as(&value, column()).await.unwrap(); - let second: TextEq = keyset.encrypt_as(&value, column()).await.unwrap(); + let first: TextEq = value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let second: TextEq = value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert_ne!(first.c, second.c, "each write seals under a fresh data key"); assert_eq!(first.hm, second.hm, "equality search matches through hm"); for record in [first, second] { - let opened: String = cipher.decrypt_as(record, column().into()).await.unwrap(); + let opened = String::decrypt_from_with_context(record, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, value); } } @@ -97,14 +114,14 @@ async fn independent_queries_match_native_terms_without_requesting_data_keys() { let (writer, _) = common::cipher().await; let (reader, calls) = common::cipher().await; let value = "café".to_owned(); - let stored: TextEq = writer - .default_keyset() - .encrypt_as(&value, column()) + let stored: TextEq = value + .clone() + .encrypt_into_with_context(&writer.default_keyset(), column()) .await .unwrap(); - let query: TextEqQuery = reader - .default_keyset() - .encrypt_as(&value, column()) + let query: TextEqQuery = value + .clone() + .encrypt_into_with_context(&reader.default_keyset(), column()) .await .unwrap(); assert_eq!(query.hm, stored.hm); @@ -125,7 +142,10 @@ async fn independent_queries_match_native_terms_without_requesting_data_keys() { let text = text.to_owned(); let keyset = reader.default_keyset(); async move { - let probe: TextEqQuery = keyset.encrypt_as(&text, column()).await.unwrap(); + let probe: TextEqQuery = text + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); probe.hm } }; @@ -162,8 +182,9 @@ async fn identifier_components_are_nonempty_and_keep_their_boundaries() { ("users", "name"), ("other", "email"), ] { - let stored: TextEq = keyset - .encrypt_as(&value, Identifier::for_column(t, c).unwrap()) + let stored: TextEq = value + .clone() + .encrypt_into_with_context(&keyset, Identifier::for_column(t, c).unwrap()) .await .unwrap(); for previous in &outputs { @@ -183,16 +204,17 @@ async fn identifier_components_are_nonempty_and_keep_their_boundaries() { #[tokio::test] async fn context_and_encoding_failures_are_rejected_before_retrieving_keys() { let (cipher, calls) = common::cipher().await; - let stored: TextEq = cipher - .default_keyset() - .encrypt_as(&"secret".to_owned(), column()) + let stored: TextEq = "secret" + .to_owned() + .encrypt_into_with_context(&cipher.default_keyset(), column()) .await .unwrap(); let wrong = Identifier::for_column("users", "name").unwrap(); - assert!(cipher - .decrypt_as::(stored.clone(), wrong.clone().into()) - .await - .is_err()); + assert!( + String::decrypt_from_with_context(stored.clone(), &cipher, wrong.clone()) + .await + .is_err() + ); for empty_table in [true, false] { let mut invalid = stored.clone(); if empty_table { @@ -200,10 +222,7 @@ async fn context_and_encoding_failures_are_rejected_before_retrieving_keys() { } else { invalid.i.c.clear(); } - assert!(cipher - .decrypt_as::(invalid, Default::default()) - .await - .is_err()); + assert!(String::decrypt_from(invalid, &cipher).await.is_err()); } // A payload the TypeScript stack wrote carries no marker. The README says // decryption rejects it with an error that names the producer; if the @@ -211,10 +230,7 @@ async fn context_and_encoding_failures_are_rejected_before_retrieving_keys() { // "invalid base64" instead, and `is_err()` alone would not notice. let mut legacy = stored.clone(); legacy.c = Ciphertext("legacy-ciphertext".into()); - let error = cipher - .decrypt_as::(legacy, Default::default()) - .await - .unwrap_err(); + let error = String::decrypt_from(legacy, &cipher).await.unwrap_err(); assert!( error .to_string() @@ -231,19 +247,13 @@ async fn context_and_encoding_failures_are_rejected_before_retrieving_keys() { ] { let mut invalid = stored.clone(); invalid.c = Ciphertext(encoded.into()); - assert!(cipher - .decrypt_as::(invalid, Default::default()) - .await - .is_err()); + assert!(String::decrypt_from(invalid, &cipher).await.is_err()); } assert!(calls.lock().unwrap().retrieve.is_empty()); let mut moved = stored; moved.i = wrong.into_inner(); assert!( - cipher - .decrypt_as::(moved, Default::default()) - .await - .is_err(), + String::decrypt_from(moved, &cipher).await.is_err(), "a different nonempty stored identifier fails authentication" ); assert_eq!(calls.lock().unwrap().retrieve.len(), 1); @@ -258,9 +268,9 @@ async fn one_changed_byte_in_a_valid_body_fails_authentication() { // A transcoding that truncated or reordered them would surface as a wrong // plaintext or a decode error in a customer database, not here. let (cipher, calls) = common::cipher().await; - let stored: TextEq = cipher - .default_keyset() - .encrypt_as(&"secret".to_owned(), column()) + let stored: TextEq = "secret" + .to_owned() + .encrypt_into_with_context(&cipher.default_keyset(), column()) .await .unwrap(); let body = stored.c.0.strip_prefix("stack-encrypt:1:").unwrap(); @@ -269,16 +279,19 @@ async fn one_changed_byte_in_a_valid_body_fails_authentication() { bytes[last] ^= 1; let mut tampered = stored.clone(); tampered.c = Ciphertext(format!("stack-encrypt:1:{}", STANDARD.encode(&bytes))); - assert!(cipher - .decrypt_as::(tampered, column().into()) - .await - .is_err()); + assert!( + String::decrypt_from_with_context(tampered, &cipher, column()) + .await + .is_err() + ); assert_eq!( calls.lock().unwrap().retrieve.len(), 1, "the tampered body was decoded and parsed; the failure is authentication" ); - let opened: String = cipher.decrypt_as(stored, column().into()).await.unwrap(); + let opened = String::decrypt_from_with_context(stored, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, "secret", "the untouched record still opens"); } @@ -286,14 +299,16 @@ async fn one_changed_byte_in_a_valid_body_fails_authentication() { async fn a_column_batches_generation_and_retrieval_under_the_same_descriptors() { let (cipher, calls) = common::cipher().await; let values = vec!["one".to_owned(), "two".to_owned(), "three".to_owned()]; - let records: Vec = cipher - .default_keyset() - .encrypt_as(&values, column()) + let records: Vec = values + .clone() + .encrypt_into_with_context(&cipher.default_keyset(), column()) .await .unwrap(); let json = serde_json::to_string(&records).unwrap(); let records: Vec = serde_json::from_str(&json).unwrap(); - let opened: Vec = cipher.decrypt_as(records, column().into()).await.unwrap(); + let opened = Vec::::decrypt_from_with_context(records, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, values); let calls = calls.lock().unwrap(); assert_eq!(calls.generate.len(), 1); @@ -302,7 +317,7 @@ async fn a_column_batches_generation_and_retrieval_under_the_same_descriptors() assert_eq!(calls.generate, calls.retrieve); } -#[derive(Clone)] +#[derive(Clone, Zeroize)] struct WithoutSerde(String); impl Encrypt for WithoutSerde { fn visit(self, sink: S) -> Result { @@ -318,14 +333,16 @@ impl Encrypt for &WithoutSerde { async fn leaf_transcoding_requires_no_serde_and_refuses_container_shapes() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); - let leaf: Ciphertext = keyset - .encrypt_as(&WithoutSerde("native".into()), column().into()) + let leaf: Ciphertext = WithoutSerde("native".into()) + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let text = String::decrypt_from_with_context(leaf, &cipher, column()) .await .unwrap(); - let text: String = cipher.decrypt_as(leaf, column().into()).await.unwrap(); assert_eq!(text, "native"); - assert!(keyset - .encrypt_as::<_, Ciphertext>(&vec!["one".to_owned()], column().into()) + assert!(vec!["one".to_owned()] + .encrypt_into_with_context::(&keyset, column()) .await .is_err()); } @@ -357,7 +374,12 @@ fn postgres_stores_queries_indexes_and_returns_decryptable_text() { (4, "ALICE@example.com"), ] { let record: TextEq = runtime - .block_on(async { keyset.encrypt_as(&value.to_owned(), column()).await }) + .block_on(async { + value + .to_owned() + .encrypt_into_with_context(&keyset, column()) + .await + }) .unwrap(); let json = serde_json::to_string(&record).unwrap(); db.execute( @@ -369,9 +391,9 @@ fn postgres_stores_queries_indexes_and_returns_decryptable_text() { let (query_cipher, _) = runtime.block_on(common::cipher()); let query: TextEqQuery = runtime .block_on(async { - query_cipher - .default_keyset() - .encrypt_as(&"alice@example.com".to_owned(), column()) + "alice@example.com" + .to_owned() + .encrypt_into_with_context(&query_cipher.default_keyset(), column()) .await }) .unwrap(); @@ -383,8 +405,8 @@ fn postgres_stores_queries_indexes_and_returns_decryptable_text() { ); for row in rows { let record: TextEq = serde_json::from_str(row.get::<_, &str>(1)).unwrap(); - let value: String = runtime - .block_on(async { cipher.decrypt_as(record, column().into()).await }) + let value = runtime + .block_on(async { String::decrypt_from_with_context(record, &cipher, column()).await }) .unwrap(); assert_eq!(value, "alice@example.com"); } diff --git a/packages/eql/tests/encryption/tests/typed_queries.rs b/packages/eql/tests/encryption/tests/typed_queries.rs index b2b71e177e..7dbb2e026d 100644 --- a/packages/eql/tests/encryption/tests/typed_queries.rs +++ b/packages/eql/tests/encryption/tests/typed_queries.rs @@ -1,16 +1,18 @@ -//! Rust queries an EQL v4 field through Stack Encrypt's typed query chain -//! (ADR-0009): `cipher.query(op::Eq, v).on(&plan)` on a plan for the type, -//! answered through the `op::Answers` impls eql-codegen writes, with the -//! type's query twin as the term. +//! Rust queries an EQL v4 field through the field's marker on a derived +//! record: `EncryptedUser::EMAIL.eq(v).encrypt(&keyset)`, answered through +//! the `op::Answers` impls eql-codegen writes, with the type's query twin +//! as the term. A record that lists a scope takes it with `.scope(..)`. //! //! The typed path must agree with the data path a binding takes //! (`stack_encrypt::dynamic::query_term`, which the Go SDK runs) on three //! things: which operators each type answers, the bytes of each query, and //! for `TextMatch`, how a decrypted row verifies and how many rows to fetch. +//! A marker's query must also be the term of the row its record stored, +//! under the same scope. mod common; -use eql_bindings::encryption::query::{MatchTwin, QueryLabelError}; +use eql_bindings::encryption::query::MatchTwin; use eql_bindings::encryption::targets as eql_targets; use eql_bindings::encryption::EqlTargets; use eql_bindings::v4::answers::ANSWERS; @@ -22,13 +24,14 @@ use eql_bindings::v4::{ use eql_bindings::Identifier; use serde::Serialize; use stack_encrypt::dynamic::{self, record, FieldPlan, Op, Operand, TargetDescriptor, ValueFrame}; -use stack_encrypt::op::{self, Probabilistic, Stats}; +use stack_encrypt::op::{Probabilistic, Stats}; use stack_encrypt::target::Scope; -use stack_encrypt::{ContextPiece, Encrypt, Error, NonEmpty, Plan, StackCipher}; +use stack_encrypt::{ContextPiece, Encrypt, Error, KeysetCipher, NonEmpty, StackCipher}; use stack_encrypt::{DecryptSource, EncryptInto, EncryptTarget}; use zeroize::Zeroize; type Cipher = StackCipher; +type Keyset<'k> = KeysetCipher<'k, common::Observed>; /// A Rust value as the engine's runtime plaintext: its frame. fn frame(value: T) -> ValueFrame { @@ -81,153 +84,137 @@ fn typed_bytes(query: &Q) -> (Vec, String) { ) } -/// A typed plan for the EQL type `$ty` over `$plaintext`, under -/// `users/`, and the term of `cipher.query($op, $value)` on it. +/// One plaintext for each EQL type that answers an operator. Each field is +/// named after its type, and the name is its column. +#[derive(Clone, Zeroize)] +struct Columns { + text_eq: String, + integer_eq: i32, + smallint_eq: i16, + date_eq: Date, + timestamp_eq: Timestamp, + numeric_eq: Decimal, + bigint_eq: i64, + real_eq: f32, + double_eq: f64, + text_match: String, +} + +/// A record with a field of each EQL type that answers an operator, in the +/// table `users`. Its markers are the typed queries under test. +#[derive(EncryptTarget)] +#[stash(struct = Columns, scope = "users")] +struct EncryptedColumns { + text_eq: TextEq, + integer_eq: integer::IntegerEq, + smallint_eq: smallint::SmallintEq, + date_eq: date::DateEq, + timestamp_eq: timestamp::TimestampEq, + numeric_eq: numeric::NumericEq, + bigint_eq: bigint::BigintEq, + real_eq: real::RealEq, + double_eq: double::DoubleEq, + text_match: TextMatch, +} + +/// The row each query below asks for. The float values are the ones whose +/// terms the semantic baseline folds (`-0.0`, NaN), and the timestamp has +/// nanoseconds below a microsecond, which both paths truncate alike. +fn columns() -> Columns { + Columns { + text_eq: "bob@example.com".into(), + integer_eq: -42, + smallint_eq: -7, + date_eq: Date::from_days_since_epoch(19_000).expect("a day in range"), + timestamp_eq: Timestamp::new(1_700_000_000, 123_456_789).expect("an instant in range"), + numeric_eq: Decimal::new(-1_234_567, 4).expect("a decimal in range"), + bigint_eq: i64::MIN + 1, + real_eq: -0.0, + double_eq: f64::NAN, + text_match: "alice".into(), + } +} + +/// A marker's query for one operator, as what the tests compare: the bytes +/// it binds with its query domain, the column the marker names, and the +/// input the data path is given for the same query. +struct Typed { + bytes: (Vec, String), + column: &'static str, + input: ValueFrame, +} + +/// The query `$marker.$operator($value)` under `$keyset`, with `$value` as +/// the data path's input. macro_rules! typed { - ($cipher:expr, $column:expr, $ty:ty, $plaintext:ty, $op:expr, $value:expr) => {{ - let plan = Plan::value::<$plaintext>() - .context(format!("users/{}", $column)) - .encrypt_into::<$ty>() - .build() + ($keyset:expr, $marker:expr, $operator:ident, $value:expr) => {{ + let marker = $marker; + let value = $value; + let query = marker + .$operator(value.clone()) + .encrypt($keyset) + .await .unwrap(); - $cipher.query($op, $value).on(&plan).await.unwrap() + Typed { + bytes: typed_bytes(&query), + column: marker.name(), + input: frame(value), + } }}; } -/// The typed query for each operator a type answers, with the input the -/// data path is given for the same query: one arm per row of `ANSWERS`, so -/// a type that starts answering an operator fails here until it has one, -/// and an arm compiles only if the type implements `op::Answers` for it. -async fn typed_and_input( - cipher: &Cipher, - name: &str, - operator: &str, - column: &str, -) -> ((Vec, String), ValueFrame) { +/// The marker query for each operator a type answers, for the value the +/// row `columns()` holds: one arm per row of `ANSWERS`, so a type that +/// starts answering an operator fails here until it has one, and an arm +/// compiles only if the type implements `op::Answers` for it. +async fn typed_and_input(keyset: &Keyset<'_>, name: &str, operator: &str) -> Typed { + let row = columns(); match (name, operator) { - ("TextEq", "eq") => ( - typed_bytes(&typed!( - cipher, - column, - TextEq, - String, - op::Eq, - "bob@example.com" - )), - frame("bob@example.com"), - ), - ("IntegerEq", "eq") => ( - typed_bytes(&typed!( - cipher, - column, - integer::IntegerEq, - i32, - op::Eq, - &-42i32 - )), - frame(-42i32), - ), - ("SmallintEq", "eq") => ( - typed_bytes(&typed!( - cipher, - column, - smallint::SmallintEq, - i16, - op::Eq, - &-7i16 - )), - frame(-7i16), - ), - ("DateEq", "eq") => { - let day = || Date::from_days_since_epoch(19_000).expect("a day in range"); - ( - typed_bytes(&typed!(cipher, column, date::DateEq, Date, op::Eq, &day())), - frame(day()), - ) - } + ("TextEq", "eq") => typed!(keyset, EncryptedColumns::TEXT_EQ, eq, row.text_eq), + ("IntegerEq", "eq") => typed!(keyset, EncryptedColumns::INTEGER_EQ, eq, row.integer_eq), + ("SmallintEq", "eq") => typed!(keyset, EncryptedColumns::SMALLINT_EQ, eq, row.smallint_eq), + ("DateEq", "eq") => typed!(keyset, EncryptedColumns::DATE_EQ, eq, row.date_eq), ("TimestampEq", "eq") => { - // Sub-microsecond nanoseconds: both paths truncate them alike. - let instant = - || Timestamp::new(1_700_000_000, 123_456_789).expect("an instant in range"); - ( - typed_bytes(&typed!( - cipher, - column, - timestamp::TimestampEq, - Timestamp, - op::Eq, - &instant() - )), - frame(instant()), - ) + typed!(keyset, EncryptedColumns::TIMESTAMP_EQ, eq, row.timestamp_eq) } - ("NumericEq", "eq") => { - let amount = || Decimal::new(-1_234_567, 4).expect("a decimal in range"); - ( - typed_bytes(&typed!( - cipher, - column, - numeric::NumericEq, - Decimal, - op::Eq, - &amount() - )), - frame(amount()), - ) - } - ("BigintEq", "eq") => ( - typed_bytes(&typed!( - cipher, - column, - bigint::BigintEq, - i64, - op::Eq, - &(i64::MIN + 1) - )), - frame(i64::MIN + 1), - ), - ("RealEq", "eq") => ( - typed_bytes(&typed!(cipher, column, real::RealEq, f32, op::Eq, &-0.0f32)), - frame(-0.0f32), - ), - ("DoubleEq", "eq") => ( - typed_bytes(&typed!( - cipher, - column, - double::DoubleEq, - f64, - op::Eq, - &f64::NAN - )), - frame(f64::NAN), - ), + ("NumericEq", "eq") => typed!(keyset, EncryptedColumns::NUMERIC_EQ, eq, row.numeric_eq), + ("BigintEq", "eq") => typed!(keyset, EncryptedColumns::BIGINT_EQ, eq, row.bigint_eq), + ("RealEq", "eq") => typed!(keyset, EncryptedColumns::REAL_EQ, eq, row.real_eq), + ("DoubleEq", "eq") => typed!(keyset, EncryptedColumns::DOUBLE_EQ, eq, row.double_eq), ("TextMatch", "matches") => { - let query: MatchTwin = - typed!(cipher, column, TextMatch, String, op::Matches, "alice"); - (typed_bytes(query.twin()), frame("alice")) + let marker = EncryptedColumns::TEXT_MATCH; + let query: MatchTwin = marker + .matches(row.text_match.clone()) + .encrypt(keyset) + .await + .unwrap(); + Typed { + bytes: typed_bytes(query.twin()), + column: marker.name(), + input: frame(row.text_match), + } } _ => panic!( - "{name} answers `{operator}` in the typed query chain: give it an arm here, with the \ - input the data path is given" + "{name} answers `{operator}` from a field marker: give it a field in \ + `EncryptedColumns` and an arm here, with the input the data path is given" ), } } -/// The operators each type answers in the typed chain are exactly the ones -/// the data path answers for a field that names it, type by type, in order, -/// for every type whose twin the chain assembles: one term, from an equality -/// or a match index. A producible type whose twin needs an order term, or -/// several terms, is the chain's one gap: the data path answers it and the -/// typed chain answers nothing, and this holds the gap to exactly those -/// types, so a type with an equality or match twin cannot drop out unseen. -/// The typed impls and `ANSWERS` are generated from one rule, so this is -/// what holds that rule to the engine's. +/// The operators each type answers from a field marker are exactly the +/// ones the data path answers for a field that names it, type by type, in +/// order, for every type whose twin a marker's query assembles: one term, +/// from an equality or a match index. A producible type whose twin needs +/// an order term, or several terms, is the one gap: the data path answers +/// it and a marker answers nothing, and this holds the gap to exactly +/// those types, so a type with an equality or match twin cannot drop out +/// unseen. The typed impls and `ANSWERS` are generated from one rule, so +/// this is what holds that rule to the engine's. #[test] -fn the_typed_chain_answers_what_the_data_path_answers() { +fn a_marker_answers_what_the_data_path_answers() { let mut gap = Vec::new(); - // A document's typed chain is the engine's own document target, not - // `op::Answers` on the EQL type: tests/json_tree_data_plan.rs holds the - // two to the same operands. + // A document has no marker query: it is queried through a data plan, + // which tests/json_tree_data_plan.rs covers. for target in eql_targets::targets() .iter() .filter(|t| t.plaintext != Some("json")) @@ -252,7 +239,7 @@ fn the_typed_chain_answers_what_the_data_path_answers() { } else { assert!( typed.is_empty(), - "{}: the typed chain answers nothing yet", + "{}: a marker answers nothing yet", target.name ); if !data.is_empty() { @@ -262,9 +249,8 @@ fn the_typed_chain_answers_what_the_data_path_answers() { } // The gap today: every producible ordered type (OPE and block ORE), and // text search, whose twins carry an order term among their three. A type - // leaves this list - // when the typed chain assembles its twin, and its answers are then - // held to the data path's above. + // leaves this list when a marker's query assembles its twin, and its + // answers are then held to the data path's above. assert_eq!( gap, [ @@ -305,54 +291,131 @@ fn the_typed_chain_answers_what_the_data_path_answers() { assert!(!ANSWERS.is_empty()); } -/// Every typed query binds the bytes the data path derives for the same -/// field, operator and value, under the same query domain. The float -/// values are the ones whose terms the semantic baseline folds (`-0.0`, -/// NaN). +/// Every marker query binds the bytes the data path derives for the same +/// column, operator and value, under the same query domain. The twin +/// names the column the marker names: the record's scope, then the field. #[tokio::test] -async fn every_typed_query_is_the_data_paths_query() { +async fn every_marker_query_is_the_data_paths_query() { let (cipher, _) = common::cipher().await; + let keyset = cipher.default_keyset(); for (name, operators) in ANSWERS { for operator in *operators { - let column = "col"; - let (typed, input) = typed_and_input(&cipher, name, operator, column).await; + let typed = typed_and_input(&keyset, name, operator).await; let op = Op::parse(operator).unwrap(); - let data = data_term(&cipher, name, column, op, input).await; - assert_eq!(typed, data, "{name} {operator}"); - let json: serde_json::Value = serde_json::from_slice(&typed.0).unwrap(); + let data = data_term(&cipher, name, typed.column, op, typed.input).await; + assert_eq!(typed.bytes, data, "{name} {operator}"); + let json: serde_json::Value = serde_json::from_slice(&typed.bytes.0).unwrap(); assert_eq!(json["v"], 4, "{name}"); - assert_eq!(json["i"], serde_json::json!({"t": "users", "c": column})); + assert_eq!( + json["i"], + serde_json::json!({"t": "users", "c": typed.column}) + ); } } } -/// The typed `TextEq` query is the stored value's term under the same -/// column, as the data path's is. +/// Every marker query carries the term of the row its record stored: the +/// `hm` of an exact type's field, and a filter the stored `bf` of a match +/// type's field contains. #[tokio::test] -async fn a_typed_eq_query_matches_the_stored_term() { +async fn every_marker_query_is_the_term_of_the_row_its_record_stored() { + let (cipher, _) = common::cipher().await; + let keyset = cipher.default_keyset(); + let stored: EncryptedColumns = columns().encrypt_into(&keyset).await.unwrap(); + let field = |column: &str| { + match column { + "text_eq" => serde_json::to_value(&stored.text_eq), + "integer_eq" => serde_json::to_value(&stored.integer_eq), + "smallint_eq" => serde_json::to_value(&stored.smallint_eq), + "date_eq" => serde_json::to_value(&stored.date_eq), + "timestamp_eq" => serde_json::to_value(&stored.timestamp_eq), + "numeric_eq" => serde_json::to_value(&stored.numeric_eq), + "bigint_eq" => serde_json::to_value(&stored.bigint_eq), + "real_eq" => serde_json::to_value(&stored.real_eq), + "double_eq" => serde_json::to_value(&stored.double_eq), + "text_match" => serde_json::to_value(&stored.text_match), + other => panic!("`EncryptedColumns` has no field {other}"), + } + .unwrap() + }; + for (name, operators) in ANSWERS { + for operator in *operators { + let typed = typed_and_input(&keyset, name, operator).await; + let query: serde_json::Value = serde_json::from_slice(&typed.bytes.0).unwrap(); + let row = field(typed.column); + assert_eq!(query["i"], row["i"], "{name}: the same column"); + match *operator { + "eq" => assert_eq!(query["hm"], row["hm"], "{name}: the stored term"), + "matches" => { + let stored = row["bf"].as_array().unwrap(); + let asked = query["bf"].as_array().unwrap(); + assert!(!asked.is_empty(), "{name}: the query sets a position"); + assert!( + asked.iter().all(|position| stored.contains(position)), + "{name}: the stored filter contains the query's" + ); + } + other => panic!("{name} answers `{other}`: say what its query finds here"), + } + } + } +} + +#[derive(Clone, Zeroize)] +struct User { + email: String, + name: String, +} + +/// A record with an exact field and a match field, in the table `users`, +/// that lists no scope. +#[derive(EncryptTarget, DecryptSource)] +#[stash(struct = User, scope = "users")] +struct EncryptedUser { + email: TextEq, + name: TextMatch, +} + +/// The marker's `TextEq` query is the term of a value the type stored on +/// its own under the same column, as the data path's is. +#[tokio::test] +async fn a_marker_eq_query_matches_the_stored_term() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); let column = Identifier::for_column("users", "email").unwrap(); - let stored: TextEq = keyset - .encrypt_as(&"bob@example.com".to_owned(), column) + let stored: TextEq = "bob@example.com" + .to_owned() + .encrypt_into_with_context(&keyset, column) + .await + .unwrap(); + let query = EncryptedUser::EMAIL + .eq("bob@example.com") + .encrypt(&keyset) .await .unwrap(); - let query = typed!(cipher, "email", TextEq, String, op::Eq, "bob@example.com"); assert_eq!(query.hm, stored.hm); assert_eq!(query.i, stored.i); - let other = typed!(cipher, "email", TextEq, String, op::Eq, "alice@example.com"); + let other = EncryptedUser::EMAIL + .eq("alice@example.com") + .encrypt(&keyset) + .await + .unwrap(); assert_ne!(other.hm, stored.hm); } -/// `verify` and `target_limit` on the typed `TextMatch` query answer as +/// `verify` and `target_limit` on the marker's `TextMatch` query answer as /// the data path's `verify` and `target_limit` do for the same query. #[tokio::test] -async fn a_typed_match_query_verifies_and_sizes_as_the_data_path_does() { +async fn a_marker_match_query_verifies_and_sizes_as_the_data_path_does() { let (cipher, _) = common::cipher().await; + let keyset = cipher.default_keyset(); let plan = data_plan("TextMatch", "name"); for needle in ["alice", "ali", "ALICE SMITH"] { - let query: MatchTwin = - typed!(cipher, "name", TextMatch, String, op::Matches, needle); + let query: MatchTwin = EncryptedUser::NAME + .matches(needle) + .encrypt(&keyset) + .await + .unwrap(); let input = frame(needle); let page = ["Alice Smith", "alicia", "Bob", "malice", "ALICE", "al", ""]; @@ -398,23 +461,17 @@ async fn a_typed_match_query_verifies_and_sizes_as_the_data_path_does() { } } -/// The query's refusals are the data path's: match text that cuts no token -/// is refused before any term exists, and a label that names no column is -/// refused, since the twin stores the column. Both come before the named -/// keyset is loaded. +/// A marker query refuses what the data path refuses: match text that cuts +/// no token is no query, by either path. +/// +/// A marker's label is its record's scope and its field's name, so it +/// always names a column: the refusal of a label that names no column is +/// the data path's alone (tests/targets.rs). #[tokio::test] -async fn a_typed_query_refuses_what_the_data_path_refuses() { - let (cipher, calls) = common::cipher().await; - let plan = Plan::value::() - .context("users/name") - .encrypt_into::() - .build() - .unwrap(); - let short = cipher - .query(op::Matches, "a") - .keyset("tenant") - .on(&plan) - .await; +async fn a_marker_query_refuses_what_the_data_path_refuses() { + let (cipher, _) = common::cipher().await; + let keyset = cipher.default_keyset(); + let short = EncryptedUser::NAME.matches("a").encrypt(&keyset).await; assert!( matches!( short, @@ -422,26 +479,23 @@ async fn a_typed_query_refuses_what_the_data_path_refuses() { ), "{short:?}" ); - - for label in ["users", "public/users/email"] { - let plan = Plan::value::() - .context(label) - .encrypt_into::() - .build() - .unwrap(); - let refused = cipher.query(op::Eq, "bob").keyset("tenant").on(&plan).await; - let Err(Error::Other(reason)) = &refused else { - panic!("{label}: {refused:?}"); - }; - assert_eq!( - reason.downcast_ref::(), - Some(&QueryLabelError::NotAColumn { - label: label.to_owned() - }), - "{label}: {reason}" - ); - } - assert_eq!(calls.lock().unwrap().loads, 0, "refused before any keyset"); + let data = dynamic::query_term( + &keyset, + &data_plan("TextMatch", "name"), + "value", + Op::Matches, + frame("a"), + &EqlTargets, + ); + assert!( + matches!( + data, + Err(dynamic::Error::Cipher(Error::Term( + stack_encrypt::sem::TermError::EmptyTermText + ))) + ), + "the data path refuses the same text, before it derives a term" + ); } #[derive(Clone, Zeroize)] @@ -459,13 +513,13 @@ struct EncryptedPerson { name: TextMatch, } -/// A query extended with `.extend(..)` derives its term under the column -/// and the extension. A record stores its fields the same way under a -/// scope. So the extended query is the term of the row stored under that +/// A marker query given a scope with `.scope(..)` derives its term under +/// the column and the scope. A record stores its fields the same way under +/// a scope. So the scoped query is the term of the row stored under that /// scope, and the twin stores the column alone, as the row does. For an /// exact type and for `TextMatch`. #[tokio::test] -async fn an_extended_typed_query_finds_a_value_stored_under_the_same_scope() { +async fn a_scoped_marker_query_finds_a_value_stored_under_the_same_scope() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); let stored: EncryptedPerson = Person { @@ -476,45 +530,36 @@ async fn an_extended_typed_query_finds_a_value_stored_under_the_same_scope() { .await .unwrap(); - let email = Plan::value::() - .context("users/email") - .encrypt_into::() - .build() - .unwrap(); - let query = cipher - .query(op::Eq, "bob@example.com") - .extend(7i64) - .on(&email) + let query = EncryptedPerson::EMAIL + .eq("bob@example.com") + .scope(Scope::new(7i64)) + .encrypt(&keyset) .await .unwrap(); assert_eq!(query.i, stored.email.i, "the twin stores the column alone"); assert_eq!(query.hm, stored.email.hm, "the term of the stored row"); - // Another scope, or none, gives another term. - let other = cipher - .query(op::Eq, "bob@example.com") - .extend(8i64) - .on(&email) + // Another scope, or none, gives another term. The query with no scope + // is from the marker of a record that lists none, for the same column. + let other = EncryptedPerson::EMAIL + .eq("bob@example.com") + .scope(Scope::new(8i64)) + .encrypt(&keyset) .await .unwrap(); assert_ne!(other.hm, stored.email.hm); - let unscoped = cipher - .query(op::Eq, "bob@example.com") - .on(&email) + let unscoped = EncryptedUser::EMAIL + .eq("bob@example.com") + .encrypt(&keyset) .await .unwrap(); assert_eq!(unscoped.i, stored.email.i); assert_ne!(unscoped.hm, stored.email.hm); - let name = Plan::value::() - .context("users/name") - .encrypt_into::() - .build() - .unwrap(); - let query = cipher - .query(op::Matches, "jones") - .extend(7i64) - .on(&name) + let query = EncryptedPerson::NAME + .matches("jones") + .scope(Scope::new(7i64)) + .encrypt(&keyset) .await .unwrap(); assert_eq!(query.twin().i, stored.name.i); @@ -528,10 +573,10 @@ async fn an_extended_typed_query_finds_a_value_stored_under_the_same_scope() { .all(|position| stored.name.bf.0.contains(position)), "the stored filter contains the query's" ); - let other = cipher - .query(op::Matches, "jones") - .extend(8i64) - .on(&name) + let other = EncryptedPerson::NAME + .matches("jones") + .scope(Scope::new(8i64)) + .encrypt(&keyset) .await .unwrap(); assert!( @@ -546,7 +591,7 @@ async fn an_extended_typed_query_finds_a_value_stored_under_the_same_scope() { } /// A record field naming a storage-only EQL type may store a null as the -/// plan says (#1298), through the resolver the Go guest installs: +/// plan says, through the resolver the Go guest installs: /// `"sealed"` stores an EQL value whose `c` is a typed null, as long as a /// value's, and `"sql"` stores nothing for the field. Both open as a null. #[tokio::test] diff --git a/packages/eql/tests/encryption/tests/v4_date_time_numeric.rs b/packages/eql/tests/encryption/tests/v4_date_time_numeric.rs index f8bf583db8..de39ea93cf 100644 --- a/packages/eql/tests/encryption/tests/v4_date_time_numeric.rs +++ b/packages/eql/tests/encryption/tests/v4_date_time_numeric.rs @@ -28,7 +28,7 @@ use eql_bindings::Identifier; use serde::de::DeserializeOwned; use stack_encrypt::dynamic::{ValueFrame, ValueKind}; use stack_encrypt::target::transcode::Reader; -use stack_encrypt::{Encrypt, Label, NonEmpty}; +use stack_encrypt::{DecryptFrom, Encrypt, EncryptInto, Label, NonEmpty}; fn frame(value: T) -> ValueFrame { ValueFrame::of(value).unwrap() @@ -108,9 +108,12 @@ async fn dates_round_trip_and_seal_the_leaf_a_naive_date_seals() { for text in ["2024-02-29", "1969-12-31", "0001-01-01", "1970-01-01"] { let theirs = day(text); let value = Date::from(theirs); - let stored: date::Date = keyset.encrypt_as(&value, column()).await.unwrap(); - let opened: Date = cipher - .decrypt_as(stored.clone(), column().into()) + let stored: date::Date = value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let opened = Date::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!(NaiveDate::from(&opened), theirs, "{text}"); @@ -119,7 +122,11 @@ async fn dates_round_trip_and_seal_the_leaf_a_naive_date_seals() { "{text}: the leaf a NaiveDate seals" ); - let searchable: date::DateEq = keyset.encrypt_as(&value, column()).await.unwrap(); + let searchable: date::DateEq = value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let named: date::DateEq = by_name(&keyset, "DateEq", frame(theirs)).await; assert_eq!(searchable.hm, named.hm, "{text}: one term on both paths"); let opened = targets::decrypt( @@ -134,11 +141,13 @@ async fn dates_round_trip_and_seal_the_leaf_a_naive_date_seals() { assert!(same(&opened, &frame(theirs)), "{text}: by name"); } for edge in [NaiveDate::MIN, NaiveDate::MAX] { - let stored: date::DateEq = keyset - .encrypt_as(&Date::from(edge), column()) + let stored: date::DateEq = Date::from(edge) + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let opened = Date::decrypt_from_with_context(stored, &cipher, column()) .await .unwrap(); - let opened: Date = cipher.decrypt_as(stored, column().into()).await.unwrap(); assert_eq!(NaiveDate::from(&opened), edge); } } @@ -154,9 +163,12 @@ async fn timestamps_round_trip_to_the_nanosecond_and_seal_the_leaf_a_date_time_s ] { let theirs = instant(text); let value = Timestamp::try_from(theirs).unwrap(); - let stored: timestamp::Timestamp = keyset.encrypt_as(&value, column()).await.unwrap(); - let opened: Timestamp = cipher - .decrypt_as(stored.clone(), column().into()) + let stored: timestamp::Timestamp = value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let opened = Timestamp::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!(DateTime::::from(&opened), theirs, "{text}"); @@ -165,11 +177,14 @@ async fn timestamps_round_trip_to_the_nanosecond_and_seal_the_leaf_a_date_time_s "{text}: the leaf a DateTime seals" ); - let searchable: timestamp::TimestampEq = keyset.encrypt_as(&value, column()).await.unwrap(); + let searchable: timestamp::TimestampEq = value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let named: timestamp::TimestampEq = by_name(&keyset, "TimestampEq", frame(theirs)).await; assert_eq!(searchable.hm, named.hm, "{text}: one term on both paths"); - let opened: Timestamp = cipher - .decrypt_as(searchable, column().into()) + let opened = Timestamp::decrypt_from_with_context(searchable, &cipher, column()) .await .unwrap(); assert_eq!( @@ -182,11 +197,14 @@ async fn timestamps_round_trip_to_the_nanosecond_and_seal_the_leaf_a_date_time_s chrono::NaiveDateTime::MIN.and_utc(), chrono::NaiveDateTime::MAX.and_utc(), ] { - let stored: timestamp::TimestampEq = keyset - .encrypt_as(&Timestamp::try_from(edge).unwrap(), column()) + let stored: timestamp::TimestampEq = Timestamp::try_from(edge) + .unwrap() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let opened = Timestamp::decrypt_from_with_context(stored, &cipher, column()) .await .unwrap(); - let opened: Timestamp = cipher.decrypt_as(stored, column().into()).await.unwrap(); assert_eq!(DateTime::::from(&opened), edge); } } @@ -204,9 +222,12 @@ async fn decimals_round_trip_with_their_scale_and_seal_the_leaf_rust_decimal_sea ] { let theirs = number(text); let value = Decimal::from(theirs); - let stored: numeric::Numeric = keyset.encrypt_as(&value, column()).await.unwrap(); - let opened: Decimal = cipher - .decrypt_as(stored.clone(), column().into()) + let stored: numeric::Numeric = value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let opened = Decimal::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!( @@ -219,7 +240,11 @@ async fn decimals_round_trip_with_their_scale_and_seal_the_leaf_rust_decimal_sea "{text}: the leaf a rust_decimal Decimal seals" ); - let searchable: numeric::NumericEq = keyset.encrypt_as(&value, column()).await.unwrap(); + let searchable: numeric::NumericEq = value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let named: numeric::NumericEq = by_name(&keyset, "NumericEq", frame(theirs)).await; assert_eq!(searchable.hm, named.hm, "{text}: one term on both paths"); let opened = targets::decrypt( @@ -337,8 +362,8 @@ async fn a_value_of_another_kind_is_refused_not_converted() { } // A stored date moved into a timestamp is not read as one. - let stored: date::Date = keyset - .encrypt_as(&Date::from(day("2024-02-29")), column()) + let stored: date::Date = Date::from(day("2024-02-29")) + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); let moved = timestamp::Timestamp { @@ -346,7 +371,7 @@ async fn a_value_of_another_kind_is_refused_not_converted() { i: stored.i, c: stored.c, }; - let opened: Result = cipher.decrypt_as(moved, column().into()).await; + let opened = Timestamp::decrypt_from_with_context(moved, &cipher, column()).await; assert!(opened.is_err(), "a date is not a timestamp"); } @@ -456,38 +481,38 @@ fn postgres_stores_and_finds_date_timestamp_and_numeric_rows() { let n = Decimal::from(number(n)); ( json( - &keyset - .encrypt_as::<_, date::Date>(&d, column()) + &d.clone() + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ), json( - &keyset - .encrypt_as::<_, date::DateEq>(&d, column()) + &d.clone() + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ), json( - &keyset - .encrypt_as::<_, timestamp::Timestamp>(&t, column()) + &t.clone() + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ), json( - &keyset - .encrypt_as::<_, timestamp::TimestampEq>(&t, column()) + &t.clone() + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ), json( - &keyset - .encrypt_as::<_, numeric::Numeric>(&n, column()) + &n.clone() + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ), json( - &keyset - .encrypt_as::<_, numeric::NumericEq>(&n, column()) + &n.clone() + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ), @@ -521,8 +546,9 @@ fn postgres_stores_and_finds_date_timestamp_and_numeric_rows() { let value = Date::from(day(text)); runtime.block_on(async { json( - &keyset - .encrypt_as::<_, date::DateEqQuery>(&value, column()) + &value + .clone() + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ) @@ -532,8 +558,9 @@ fn postgres_stores_and_finds_date_timestamp_and_numeric_rows() { let value = Timestamp::try_from(instant(text)).unwrap(); runtime.block_on(async { json( - &keyset - .encrypt_as::<_, timestamp::TimestampEqQuery>(&value, column()) + &value + .clone() + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ) @@ -543,8 +570,9 @@ fn postgres_stores_and_finds_date_timestamp_and_numeric_rows() { let value = Decimal::from(number(text)); runtime.block_on(async { json( - &keyset - .encrypt_as::<_, numeric::NumericEqQuery>(&value, column()) + &value + .clone() + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ) @@ -599,29 +627,29 @@ fn postgres_stores_and_finds_date_timestamp_and_numeric_rows() { .unwrap(); let (opened, created, total): (String, String, String) = (row.get(0), row.get(1), row.get(2)); runtime.block_on(async { - let opened: Date = cipher - .decrypt_as( - serde_json::from_str::(&opened).unwrap(), - column().into(), - ) - .await - .unwrap(); + let opened = Date::decrypt_from_with_context( + serde_json::from_str::(&opened).unwrap(), + &cipher, + column(), + ) + .await + .unwrap(); assert_eq!(NaiveDate::from(&opened), day("2024-02-29")); - let created: Timestamp = cipher - .decrypt_as( - serde_json::from_str::(&created).unwrap(), - column().into(), - ) - .await - .unwrap(); + let created = Timestamp::decrypt_from_with_context( + serde_json::from_str::(&created).unwrap(), + &cipher, + column(), + ) + .await + .unwrap(); assert_eq!(created.nanoseconds(), 123_456_999); - let total: Decimal = cipher - .decrypt_as( - serde_json::from_str::(&total).unwrap(), - column().into(), - ) - .await - .unwrap(); + let total = Decimal::decrypt_from_with_context( + serde_json::from_str::(&total).unwrap(), + &cipher, + column(), + ) + .await + .unwrap(); assert_eq!(rust_decimal::Decimal::from(&total).to_string(), "1.50"); }); @@ -709,59 +737,65 @@ mod go_ledger { let column = |name: &str| Identifier::for_column("profiles", name).unwrap(); let p = &row.plaintext; let id = row.id; - let opened: Date = cipher - .decrypt_as(parsed::(row, "opened"), column("opened").into()) - .await - .unwrap(); + let opened = Date::decrypt_from_with_context( + parsed::(row, "opened"), + cipher, + column("opened"), + ) + .await + .unwrap(); assert_eq!(NaiveDate::from(&opened), day(&p.opened), "row {id} opened"); - let due: Date = cipher - .decrypt_as(parsed::(row, "due"), column("due").into()) - .await - .unwrap(); + let due = Date::decrypt_from_with_context( + parsed::(row, "due"), + cipher, + column("due"), + ) + .await + .unwrap(); assert_eq!(NaiveDate::from(&due), day(&p.due), "row {id} due"); - let created: Timestamp = cipher - .decrypt_as( - parsed::(row, "created"), - column("created").into(), - ) - .await - .unwrap(); + let created = Timestamp::decrypt_from_with_context( + parsed::(row, "created"), + cipher, + column("created"), + ) + .await + .unwrap(); assert_eq!( DateTime::::from(&created), instant(&p.created), "row {id} created" ); - let posted: Timestamp = cipher - .decrypt_as( - parsed::(row, "posted"), - column("posted").into(), - ) - .await - .unwrap(); + let posted = Timestamp::decrypt_from_with_context( + parsed::(row, "posted"), + cipher, + column("posted"), + ) + .await + .unwrap(); assert_eq!( DateTime::::from(&posted), instant(&p.posted), "row {id} posted" ); - let total: Decimal = cipher - .decrypt_as( - parsed::(row, "total"), - column("total").into(), - ) - .await - .unwrap(); + let total = Decimal::decrypt_from_with_context( + parsed::(row, "total"), + cipher, + column("total"), + ) + .await + .unwrap(); assert_eq!( rust_decimal::Decimal::from(&total).to_string(), p.total, "row {id} total keeps its scale" ); - let amount: Decimal = cipher - .decrypt_as( - parsed::(row, "amount"), - column("amount").into(), - ) - .await - .unwrap(); + let amount = Decimal::decrypt_from_with_context( + parsed::(row, "amount"), + cipher, + column("amount"), + ) + .await + .unwrap(); assert_eq!( rust_decimal::Decimal::from(&amount).to_string(), p.amount, @@ -908,32 +942,42 @@ async fn a_storage_only_type_stores_a_none_as_long_as_a_value() { let keyset = cipher.default_keyset(); let value = Date::from(day("2024-02-29")); - let present: date::Date = keyset.encrypt_as(&value, column()).await.unwrap(); - let absent: date::Date = keyset.encrypt_as(&None::, column()).await.unwrap(); + let present: date::Date = value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let absent: date::Date = None:: + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert_eq!( leaf_len(&absent.c), leaf_len(&present.c), "as long as a value" ); - let opened: Option = cipher - .decrypt_as(absent.clone(), column().into()) + let opened = Option::::decrypt_from_with_context(absent.clone(), &cipher, column()) .await .unwrap(); assert!(opened.is_none()); - let refused: Result = cipher.decrypt_as(absent.clone(), column().into()).await; + let refused = Date::decrypt_from_with_context(absent.clone(), &cipher, column()).await; assert!(refused.is_err(), "a None does not open as a value"); - let some: date::Date = keyset - .encrypt_as(&Some(value.clone()), column()) + let some: date::Date = Some(value.clone()) + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let opened = Date::decrypt_from_with_context(some, &cipher, column()) .await .unwrap(); - let opened: Date = cipher.decrypt_as(some, column().into()).await.unwrap(); assert_eq!( NaiveDate::from(&opened), day("2024-02-29"), "Some seals the plaintext's own leaf" ); - let opened: Option = cipher.decrypt_as(present, column().into()).await.unwrap(); + let opened = Option::::decrypt_from_with_context(present, &cipher, column()) + .await + .unwrap(); assert_eq!( opened.as_ref().map(NaiveDate::from), Some(day("2024-02-29")), @@ -1025,8 +1069,16 @@ fn postgres_stores_a_sealed_null_in_a_storage_only_column() { for (id, value) in &rows { let (entry, due): (date::Date, date::DateEq) = runtime.block_on(async { ( - keyset.encrypt_as(value, column()).await.unwrap(), - keyset.encrypt_as(value, column()).await.unwrap(), + value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(), + value + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(), ) }); db.execute( @@ -1045,14 +1097,20 @@ fn postgres_stores_a_sealed_null_in_a_storage_only_column() { .unwrap() .get(0); let stored: date::Date = serde_json::from_str(&stored).unwrap(); - let opened: Option = runtime - .block_on(async { cipher.decrypt_as(stored, column().into()).await }) + let opened = runtime + .block_on(async { + Option::::decrypt_from_with_context(stored, &cipher, column()).await + }) .unwrap(); assert!(opened.is_none(), "the stored null opens as None"); // `=` with the query of a null finds the absent rows, and only them. let null_query: date::DateEqQuery = runtime - .block_on(async { keyset.encrypt_as(&None::, column()).await }) + .block_on(async { + None:: + .encrypt_into_with_context(&keyset, column()) + .await + }) .unwrap(); let found: Vec = db .query( diff --git a/packages/eql/tests/encryption/tests/v4_order.rs b/packages/eql/tests/encryption/tests/v4_order.rs index cdc39dfcdc..c81fc27649 100644 --- a/packages/eql/tests/encryption/tests/v4_order.rs +++ b/packages/eql/tests/encryption/tests/v4_order.rs @@ -19,7 +19,7 @@ use eql_bindings::v4::{bigint, double, integer, real, text}; use eql_bindings::Identifier; use stack_encrypt::dynamic::ValueFrame; use stack_encrypt::value::{Date, Decimal, Timestamp}; -use stack_encrypt::{Encrypt, Label, NonEmpty}; +use stack_encrypt::{DecryptFrom, Encrypt, EncryptInto, Label, NonEmpty}; fn frame(value: T) -> ValueFrame { ValueFrame::of(value).unwrap() @@ -413,7 +413,10 @@ async fn text_with_no_fold_is_refused() { }; assert!(queried.is_err(), "{name}: queried text with no fold"); } - let typed: Result = keyset.encrypt_as(&unassigned.to_owned(), column()).await; + let typed: Result = unassigned + .to_owned() + .encrypt_into_with_context(&keyset, column()) + .await; assert!(typed.is_err(), "the typed entry point refuses it too"); } @@ -424,36 +427,53 @@ async fn the_typed_entry_point_stores_the_dispatchs_terms() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); - let typed: integer::IntegerOrd = keyset.encrypt_as(&-9i32, column()).await.unwrap(); + let typed: integer::IntegerOrd = (-9i32) + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let named = stored(&keyset, "IntegerOrd", frame(-9i32)).await; assert_eq!(serde_json::to_value(&typed.op).unwrap(), named["op"]); - let opened: i32 = cipher.decrypt_as(typed, column().into()).await.unwrap(); + let opened = i32::decrypt_from_with_context(typed, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, -9); - let typed: bigint::BigintOrdOpeQuery = keyset.encrypt_as(&7i64, column()).await.unwrap(); + let typed: bigint::BigintOrdOpeQuery = 7i64 + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let named = query(&keyset, "BigintOrdOpe", frame(7i64)).await; assert_eq!(serde_json::to_value(&typed.op).unwrap(), named["op"]); - let typed: real::RealOrd = keyset.encrypt_as(&-0.0f32, column()).await.unwrap(); + let typed: real::RealOrd = (-0.0f32) + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let named = stored(&keyset, "RealOrd", frame(0.0f32)).await; assert_eq!( serde_json::to_value(&typed.op).unwrap(), named["op"], "-0.0 ties 0.0 in the term" ); - let opened: f32 = cipher.decrypt_as(typed, column().into()).await.unwrap(); + let opened = f32::decrypt_from_with_context(typed, &cipher, column()) + .await + .unwrap(); assert_eq!( opened.to_bits(), (-0.0f32).to_bits(), "and keeps its sign in c" ); - let typed: double::DoubleOrdOpe = keyset.encrypt_as(&f64::NAN, column()).await.unwrap(); + let typed: double::DoubleOrdOpe = f64::NAN + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let named = stored(&keyset, "DoubleOrdOpe", frame(f64::NAN)).await; assert_eq!(serde_json::to_value(&typed.op).unwrap(), named["op"]); - let typed: text::TextOrdQuery = keyset - .encrypt_as(&"Café".to_owned(), column()) + let typed: text::TextOrdQuery = "Café" + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); let named = query(&keyset, "TextOrd", frame("Café")).await; @@ -579,11 +599,13 @@ mod go_rows { let p = &row.plaintext; macro_rules! open { ($ty:ty, $plain:ty, $column:literal) => {{ - let opened: $plain = cipher - .decrypt_as(parsed::<$ty>(row, $column), column($column).into()) - .await - .unwrap(); - opened + <$plain>::decrypt_from_with_context( + parsed::<$ty>(row, $column), + cipher, + column($column), + ) + .await + .unwrap() }}; } assert_eq!(open!(integer::IntegerOrd, i32, "level"), p.level); @@ -809,14 +831,14 @@ fn postgres_compares_and_orders_ope_rows() { .query_one("SELECT eql_v4.max(level)::text FROM v4_ordered", &[]) .map(|r| r.get(0)) .unwrap_or_else(|e| panic!("max(level): {e}")); - let opened: i32 = runtime + let opened = runtime .block_on(async { - cipher - .decrypt_as( - serde_json::from_str::(&back).unwrap(), - column().into(), - ) - .await + i32::decrypt_from_with_context( + serde_json::from_str::(&back).unwrap(), + &cipher, + column(), + ) + .await }) .unwrap(); assert_eq!(opened, i32::MAX); diff --git a/packages/eql/tests/encryption/tests/v4_owned.rs b/packages/eql/tests/encryption/tests/v4_owned.rs index 732e76e379..d75565fb7f 100644 --- a/packages/eql/tests/encryption/tests/v4_owned.rs +++ b/packages/eql/tests/encryption/tests/v4_owned.rs @@ -1,16 +1,19 @@ -//! The EQL v4 types through Stack Encrypt's owned API: `EncryptTarget` and +//! The EQL v4 types through Stack Encrypt's API: `EncryptTarget` and //! `DecryptSource`, called as `value.encrypt_into_with_context(..)` and //! `P::decrypt_from(..)`. //! -//! Data written through either API goes into the same columns, and is queried -//! by the same terms. So the two APIs must agree. `v4_terms.rs` checks the -//! bytes that each type writes against known answers. This file checks what a -//! known answer cannot, for every stored type and every derived query type: +//! `v4_terms.rs` checks the bytes that each type writes against known answers. +//! This file checks what a known answer cannot, for every stored type and +//! every derived query type: //! -//! - The ciphertext `c` written by each API opens through the other API. `c` is -//! sealed under a new nonce each time, so its bytes differ every time. +//! - The ciphertext `c` opens, under the column the value stores and under the +//! column the reader expects. `c` is sealed under a new nonce each time, so +//! its bytes differ every time. //! - The block ORE (order-revealing encryption) term `ob` also contains a -//! nonce. The test compares it by order, not by its bytes. +//! nonce. Two terms of one value have different bytes. The test checks that +//! they still order as equal. +//! - Every other term is the same each time a value is written, and so is +//! every query operand. //! - Both forms of null behave as before. A type encrypted from an `Option` //! seals a null. An `Option` of a type stores a database NULL for `None`. //! - A value is refused under another column before any key is requested. @@ -54,23 +57,23 @@ fn terms_of(stored: &T) -> serde_json::Value { /// The right half of the term contains a random nonce, as the ciphertext /// does. So one value gives different bytes each time. Two terms of one value /// still compare as equal. -fn assert_same_terms(owned: &T, structural: &T, name: &str) { - let mut owned = terms_of(owned); - let mut structural = terms_of(structural); +fn assert_same_terms(first: &T, second: &T, name: &str) { + let mut first = terms_of(first); + let mut second = terms_of(second); let ob = |json: &mut serde_json::Value| { json.as_object_mut() .unwrap() .remove("ob") .map(|ob| serde_json::from_value::>(ob).unwrap()) }; - match (ob(&mut owned), ob(&mut structural)) { + match (ob(&mut first), ob(&mut second)) { (Some(a), Some(b)) => assert!(a == b, "{name}: the block ORE terms order equal"), (None, None) => {} - _ => panic!("{name}: only one path wrote a block ORE term"), + _ => panic!("{name}: only one write has a block ORE term"), } assert_eq!( - owned, structural, - "{name}: the owned API derives the terms the structural API does" + first, second, + "{name}: one value under one column derives the same terms each time" ); } @@ -130,50 +133,53 @@ impl Seen for DecimalValue { } } -/// Writes each stored type through both APIs, from the same plaintext under -/// the same column. Checks that the terms match, and that each ciphertext -/// opens through the other API. -macro_rules! stored_agrees { - ($cipher:expr, $keyset:expr, $plaintext:ty, $value:expr, [$($ty:ty),+ $(,)?]) => {$({ +/// Writes each stored type twice, from the same plaintext under the same +/// column. Checks that the terms match, and that each ciphertext opens: under +/// the column it stores, and under the column the reader expects. +macro_rules! stored_round_trips { + ($keyset:expr, $plaintext:ty, $value:expr, [$($ty:ty),+ $(,)?]) => {$({ let value: $plaintext = $value; - let structural: $ty = $keyset.encrypt_as(&value, column()).await.unwrap(); - let owned: $ty = value + let first: $ty = value .clone() .encrypt_into_with_context(&$keyset, column()) .await .unwrap(); - assert_same_terms(&owned, &structural, stringify!($ty)); - - let opened = <$plaintext>::decrypt_from(structural, &$keyset).await.unwrap(); - assert_eq!(opened.view(), value.view(), "{}: the owned API opens structural output", stringify!($ty)); - let opened: $plaintext = $cipher - .decrypt_as(owned.clone(), column().into()) + let second: $ty = value + .clone() + .encrypt_into_with_context(&$keyset, column()) .await .unwrap(); - assert_eq!(opened.view(), value.view(), "{}: the structural API opens owned output", stringify!($ty)); - let opened = <$plaintext>::decrypt_from_with_context(owned, &$keyset, column()) + assert_same_terms(&first, &second, stringify!($ty)); + + let opened = <$plaintext>::decrypt_from(first, &$keyset).await.unwrap(); + assert_eq!(opened.view(), value.view(), "{}: opens under the stored column", stringify!($ty)); + let opened = <$plaintext>::decrypt_from_with_context(second, &$keyset, column()) .await .unwrap(); assert_eq!(opened.view(), value.view(), "{}: the stored column is the one expected", stringify!($ty)); })+}; } -/// Builds each query type through both APIs, and checks that the operands -/// match. A query operand contains no ciphertext, so the whole payload must -/// match. -macro_rules! query_agrees { +/// Builds each query type twice, and checks that the operands match. A query +/// operand contains no ciphertext, so the whole payload must match. A block +/// ORE operand is compared by the ORE scheme, as the type's `PartialEq` does. +macro_rules! query_is_stable { ($keyset:expr, $plaintext:ty, $value:expr, [$($ty:ty),+ $(,)?]) => {$({ let value: $plaintext = $value; - let structural: $ty = $keyset.encrypt_as(&value, column()).await.unwrap(); - let owned: $ty = value + let first: $ty = value + .clone() + .encrypt_into_with_context(&$keyset, column()) + .await + .unwrap(); + let second: $ty = value .clone() .encrypt_into_with_context(&$keyset, column()) .await .unwrap(); assert_eq!( - owned, - structural, - "{}: the owned API builds the operand the structural API does", + first, + second, + "{}: one value under one column builds the same operand each time", stringify!($ty) ); })+}; @@ -187,8 +193,7 @@ mod every_stored_type { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); use text::*; - stored_agrees!( - cipher, + stored_round_trips!( keyset, String, "Ada Lovelace".to_owned(), @@ -211,8 +216,7 @@ mod every_stored_type { let keyset = cipher.default_keyset(); { use integer::*; - stored_agrees!( - cipher, + stored_round_trips!( keyset, i32, -42, @@ -221,8 +225,7 @@ mod every_stored_type { } { use smallint::*; - stored_agrees!( - cipher, + stored_round_trips!( keyset, i16, 7, @@ -237,8 +240,7 @@ mod every_stored_type { } { use bigint::*; - stored_agrees!( - cipher, + stored_round_trips!( keyset, i64, 1 << 40, @@ -253,8 +255,7 @@ mod every_stored_type { let keyset = cipher.default_keyset(); { use double::*; - stored_agrees!( - cipher, + stored_round_trips!( keyset, f64, -2.5, @@ -263,15 +264,14 @@ mod every_stored_type { } { use real::*; - stored_agrees!( - cipher, + stored_round_trips!( keyset, f32, 0.25, [Real, RealEq, RealOrdOre, RealOrd, RealOrdOpe] ); } - stored_agrees!(cipher, keyset, bool, true, [boolean::Boolean]); + stored_round_trips!(keyset, bool, true, [boolean::Boolean]); } #[tokio::test] @@ -280,8 +280,7 @@ mod every_stored_type { let keyset = cipher.default_keyset(); { use date::*; - stored_agrees!( - cipher, + stored_round_trips!( keyset, DateValue, date("2024-02-29"), @@ -290,8 +289,7 @@ mod every_stored_type { } { use timestamp::*; - stored_agrees!( - cipher, + stored_round_trips!( keyset, TimestampValue, timestamp("2024-02-29T12:34:56.123456789Z"), @@ -306,8 +304,7 @@ mod every_stored_type { } { use numeric::*; - stored_agrees!( - cipher, + stored_round_trips!( keyset, DecimalValue, decimal("-1234.5600"), @@ -325,7 +322,7 @@ mod every_query_type { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); use text::*; - query_agrees!( + query_is_stable!( keyset, String, "Ada Lovelace".to_owned(), @@ -351,7 +348,7 @@ mod every_query_type { let keyset = cipher.default_keyset(); { use integer::*; - query_agrees!( + query_is_stable!( keyset, i32, -42, @@ -365,7 +362,7 @@ mod every_query_type { } { use smallint::*; - query_agrees!( + query_is_stable!( keyset, i16, 7, @@ -379,7 +376,7 @@ mod every_query_type { } { use bigint::*; - query_agrees!( + query_is_stable!( keyset, i64, 1 << 40, @@ -393,7 +390,7 @@ mod every_query_type { } { use double::*; - query_agrees!( + query_is_stable!( keyset, f64, -2.5, @@ -407,7 +404,7 @@ mod every_query_type { } { use real::*; - query_agrees!( + query_is_stable!( keyset, f32, 0.25, @@ -422,7 +419,7 @@ mod every_query_type { let keyset = cipher.default_keyset(); { use date::*; - query_agrees!( + query_is_stable!( keyset, DateValue, date("2024-02-29"), @@ -431,7 +428,7 @@ mod every_query_type { } { use timestamp::*; - query_agrees!( + query_is_stable!( keyset, TimestampValue, timestamp("2024-02-29T12:34:56.123456789Z"), @@ -445,7 +442,7 @@ mod every_query_type { } { use numeric::*; - query_agrees!( + query_is_stable!( keyset, DecimalValue, decimal("-1234.5600"), @@ -466,87 +463,63 @@ mod a_null { // A type encrypted from an `Option` seals a null: a stored value, under a // key, with the terms of a null. #[tokio::test] - async fn sealed_by_a_type_over_an_option_matches_the_structural_api() { + async fn sealed_by_a_type_over_an_option_has_stable_terms_and_opens_as_none() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); - stored_agrees!( - cipher, - keyset, - Option, - None, - [text::Text, text::TextEq] - ); - stored_agrees!( - cipher, + stored_round_trips!(keyset, Option, None, [text::Text, text::TextEq]); + stored_round_trips!( keyset, Option, None, [integer::Integer, integer::IntegerEq] ); - stored_agrees!( - cipher, + stored_round_trips!( keyset, Option, None, [smallint::Smallint, smallint::SmallintEq] ); - stored_agrees!( - cipher, + stored_round_trips!( keyset, Option, None, [bigint::Bigint, bigint::BigintEq] ); - stored_agrees!( - cipher, + stored_round_trips!( keyset, Option, None, [double::Double, double::DoubleEq] ); - stored_agrees!( - cipher, - keyset, - Option, - None, - [real::Real, real::RealEq] - ); - stored_agrees!(cipher, keyset, Option, None, [boolean::Boolean]); - stored_agrees!( - cipher, - keyset, - Option, - None, - [date::Date, date::DateEq] - ); - stored_agrees!( - cipher, + stored_round_trips!(keyset, Option, None, [real::Real, real::RealEq]); + stored_round_trips!(keyset, Option, None, [boolean::Boolean]); + stored_round_trips!(keyset, Option, None, [date::Date, date::DateEq]); + stored_round_trips!( keyset, Option, None, [timestamp::Timestamp, timestamp::TimestampEq] ); - stored_agrees!( - cipher, + stored_round_trips!( keyset, Option, None, [numeric::Numeric, numeric::NumericEq] ); - query_agrees!(keyset, Option, None, [text::TextEqQuery]); - query_agrees!(keyset, Option, None, [integer::IntegerEqQuery]); - query_agrees!(keyset, Option, None, [smallint::SmallintEqQuery]); - query_agrees!(keyset, Option, None, [bigint::BigintEqQuery]); - query_agrees!(keyset, Option, None, [double::DoubleEqQuery]); - query_agrees!(keyset, Option, None, [real::RealEqQuery]); - query_agrees!(keyset, Option, None, [date::DateEqQuery]); - query_agrees!( + query_is_stable!(keyset, Option, None, [text::TextEqQuery]); + query_is_stable!(keyset, Option, None, [integer::IntegerEqQuery]); + query_is_stable!(keyset, Option, None, [smallint::SmallintEqQuery]); + query_is_stable!(keyset, Option, None, [bigint::BigintEqQuery]); + query_is_stable!(keyset, Option, None, [double::DoubleEqQuery]); + query_is_stable!(keyset, Option, None, [real::RealEqQuery]); + query_is_stable!(keyset, Option, None, [date::DateEqQuery]); + query_is_stable!( keyset, Option, None, [timestamp::TimestampEqQuery] ); - query_agrees!( + query_is_stable!( keyset, Option, None, diff --git a/packages/eql/tests/encryption/tests/v4_terms.rs b/packages/eql/tests/encryption/tests/v4_terms.rs index 5c473ca0a2..16d7181f6a 100644 --- a/packages/eql/tests/encryption/tests/v4_terms.rs +++ b/packages/eql/tests/encryption/tests/v4_terms.rs @@ -8,9 +8,9 @@ //! A change to any of these bytes is a change to the stored format. After such //! a change, a new query no longer finds the rows written before it. //! -//! The fixture contains the output of the structural API (`encrypt_as`). This -//! test writes through the owned API (`encrypt_into_with_context`). So the test -//! also checks that the owned API writes what the structural API wrote. +//! The fixture was recorded from the API that `encrypt_into_with_context` +//! replaced. This test writes through `encrypt_into_with_context`. So the test +//! also checks that a new value has the terms of the values stored before. //! //! The test leaves out two fields. Each contains a random nonce, so its bytes //! differ every time: diff --git a/packages/eql/tests/encryption/tests/v4_text_search.rs b/packages/eql/tests/encryption/tests/v4_text_search.rs index 586324f592..9fd0f84e17 100644 --- a/packages/eql/tests/encryption/tests/v4_text_search.rs +++ b/packages/eql/tests/encryption/tests/v4_text_search.rs @@ -24,7 +24,7 @@ use eql_bindings::Identifier; use stack_encrypt::dynamic::ValueFrame; use stack_encrypt::sem::TermError; use stack_encrypt::target::IndexSpec; -use stack_encrypt::{Encrypt, Label, NonEmpty}; +use stack_encrypt::{DecryptFrom, Encrypt, EncryptInto, Label, NonEmpty}; fn frame(value: T) -> ValueFrame { ValueFrame::of(value).unwrap() @@ -222,8 +222,16 @@ async fn the_typed_projections_are_what_the_dispatch_writes() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); let text = "A".to_owned(); - let eq: TextSearchEqualityQuery = keyset.encrypt_as(&text, column()).await.unwrap(); - let ord: TextSearchOrderQuery = keyset.encrypt_as(&text, column()).await.unwrap(); + let eq: TextSearchEqualityQuery = text + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let ord: TextSearchOrderQuery = text + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); for (typed, key) in [ (TextSearchQuery::from(eq), "eq"), (TextSearchQuery::from(ord), "ope"), @@ -234,8 +242,9 @@ async fn the_typed_projections_are_what_the_dispatch_writes() { "{key}" ); } - let matched: TextSearchMatchQuery = keyset - .encrypt_as(&"Alice".to_owned(), column()) + let matched: TextSearchMatchQuery = "Alice" + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); let matched = TextSearchQuery::from(matched); @@ -253,21 +262,30 @@ async fn the_typed_projections_are_what_the_dispatch_writes() { assert!(bare.hm.is_none() && bare.op.is_none() && bare.bf.is_none()); // The stored value round-trips through the typed path too. - let stored: TextSearch = keyset - .encrypt_as(&"Alice".to_owned(), column()) + let stored: TextSearch = "Alice" + .to_owned() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let opened = String::decrypt_from_with_context(stored, &cipher, column()) .await .unwrap(); - let opened: String = cipher.decrypt_as(stored, column().into()).await.unwrap(); assert_eq!(opened, "Alice"); // And short text through the typed path stores the empty filter, as the // dispatch does. - let short: TextSearch = keyset.encrypt_as(&text, column()).await.unwrap(); + let short: TextSearch = text + .clone() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert!(short.bf.0 .0.is_empty()); assert_eq!( serde_json::to_value(&short.bf).unwrap(), serde_json::json!([]) ); - let opened: String = cipher.decrypt_as(short, column().into()).await.unwrap(); + let opened = String::decrypt_from_with_context(short, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, "A"); } diff --git a/packages/eql/tests/encryption/tests/v4_types.rs b/packages/eql/tests/encryption/tests/v4_types.rs index 415132eeca..0380477f05 100644 --- a/packages/eql/tests/encryption/tests/v4_types.rs +++ b/packages/eql/tests/encryption/tests/v4_types.rs @@ -3,10 +3,10 @@ //! types, the integer and float equality types, and `TextMatch`. //! //! Each type must round-trip through both entry points (the typed -//! `encrypt_as` and the by-name dispatch a Go plan reaches), seal exactly the -//! leaf the same value's frame seals as (what the Go SDK writes for a plain -//! field of that kind) and open the leaf a frame sealed, and refuse a value -//! of another kind. +//! `encrypt_into_with_context` and the by-name dispatch a Go plan reaches), +//! seal exactly the leaf the same value's frame seals as (what the Go SDK +//! writes for a plain field of that kind) and open the leaf a frame sealed, +//! and refuse a value of another kind. mod common; @@ -135,11 +135,13 @@ async fn integers_round_trip_and_seal_the_leaf_their_frame_seals() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); for value in [0, 42, -7, i32::MIN, i32::MAX] { - let stored: integer::Integer = keyset.encrypt_as(&value, column()).await.unwrap(); + let stored: integer::Integer = value + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c"]); - let opened: i32 = cipher - .decrypt_as(stored.clone(), column().into()) + let opened = i32::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!(opened, value); @@ -163,10 +165,12 @@ async fn integers_round_trip_and_seal_the_leaf_their_frame_seals() { } for value in [0i16, -1, i16::MIN, i16::MAX] { - let stored: smallint::Smallint = keyset.encrypt_as(&value, column()).await.unwrap(); + let stored: smallint::Smallint = value + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c"]); - let opened: i16 = cipher - .decrypt_as(stored.clone(), column().into()) + let opened = i16::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!(opened, value); @@ -188,10 +192,12 @@ async fn integers_round_trip_and_seal_the_leaf_their_frame_seals() { } for value in [0i64, -1, i64::MIN, i64::MAX] { - let stored: bigint::Bigint = keyset.encrypt_as(&value, column()).await.unwrap(); + let stored: bigint::Bigint = value + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c"]); - let opened: i64 = cipher - .decrypt_as(stored.clone(), column().into()) + let opened = i64::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!(opened, value); @@ -297,33 +303,58 @@ async fn number_equality_types_round_trip_through_the_typed_entry_point() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); - let stored: integer::IntegerEq = keyset.encrypt_as(&-7i32, column()).await.unwrap(); + let stored: integer::IntegerEq = (-7i32) + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c", "hm"]); let named: serde_json::Value = by_name(&keyset, "IntegerEq", frame::(-7)).await; assert_eq!(serde_json::to_value(&stored).unwrap()["hm"], named["hm"]); - let opened: i32 = cipher.decrypt_as(stored, column().into()).await.unwrap(); + let opened = i32::decrypt_from_with_context(stored, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, -7); - let stored: smallint::SmallintEq = keyset.encrypt_as(&i16::MIN, column()).await.unwrap(); + let stored: smallint::SmallintEq = i16::MIN + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c", "hm"]); let named: serde_json::Value = by_name(&keyset, "SmallintEq", frame::(i16::MIN)).await; assert_eq!(serde_json::to_value(&stored).unwrap()["hm"], named["hm"]); - let opened: i16 = cipher.decrypt_as(stored, column().into()).await.unwrap(); + let opened = i16::decrypt_from_with_context(stored, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, i16::MIN); - let stored: bigint::BigintEq = keyset.encrypt_as(&i64::MIN, column()).await.unwrap(); + let stored: bigint::BigintEq = i64::MIN + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c", "hm"]); - let opened: i64 = cipher.decrypt_as(stored, column().into()).await.unwrap(); + let opened = i64::decrypt_from_with_context(stored, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, i64::MIN); - let stored: real::RealEq = keyset.encrypt_as(&-0.0f32, column()).await.unwrap(); + let stored: real::RealEq = (-0.0f32) + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c", "hm"]); - let opened: f32 = cipher.decrypt_as(stored, column().into()).await.unwrap(); + let opened = f32::decrypt_from_with_context(stored, &cipher, column()) + .await + .unwrap(); assert_eq!(opened.to_bits(), (-0.0f32).to_bits()); - let stored: double::DoubleEq = keyset.encrypt_as(&f64::NAN, column()).await.unwrap(); + let stored: double::DoubleEq = f64::NAN + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c", "hm"]); - let opened: f64 = cipher.decrypt_as(stored, column().into()).await.unwrap(); + let opened = f64::decrypt_from_with_context(stored, &cipher, column()) + .await + .unwrap(); assert_eq!(opened.to_bits(), f64::NAN.to_bits()); } @@ -332,10 +363,12 @@ async fn booleans_and_floats_round_trip_as_storage_only_types() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); for value in [true, false] { - let stored: boolean::Boolean = keyset.encrypt_as(&value, column()).await.unwrap(); + let stored: boolean::Boolean = value + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c"]); - let opened: bool = cipher - .decrypt_as(stored.clone(), column().into()) + let opened = bool::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!(opened, value); @@ -343,18 +376,22 @@ async fn booleans_and_floats_round_trip_as_storage_only_types() { } // Floats seal their bit pattern: -0.0 and a NaN's payload survive. for value in [1.5f32, -0.0, f32::NAN, f32::INFINITY] { - let stored: real::Real = keyset.encrypt_as(&value, column()).await.unwrap(); - let opened: f32 = cipher - .decrypt_as(stored.clone(), column().into()) + let stored: real::Real = value + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let opened = f32::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!(opened.to_bits(), value.to_bits()); assert!(same(&as_frame(&cipher, stored.c).await, &frame(value))); } for value in [2.25f64, -0.0, f64::NAN] { - let stored: double::Double = keyset.encrypt_as(&value, column()).await.unwrap(); - let opened: f64 = cipher - .decrypt_as(stored.clone(), column().into()) + let stored: double::Double = value + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let opened = f64::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!(opened.to_bits(), value.to_bits()); @@ -408,13 +445,12 @@ async fn a_json_document_round_trips_as_a_storage_only_type() { serde_json::json!([]), serde_json::json!({}), ] { - let stored: json::Json = keyset - .encrypt_as(&JsonPlaintext::from(document.clone()), column()) + let stored: json::Json = JsonPlaintext::from(document.clone()) + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c"]); - let opened: JsonPlaintext = cipher - .decrypt_as(stored.clone(), column().into()) + let opened = JsonPlaintext::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!(opened.value(), &document); @@ -514,8 +550,7 @@ async fn a_json_document_dispatches_by_name() { &frame(profile_text().as_str()) )); // The typed call opens what the dispatch sealed. - let opened: JsonPlaintext = cipher - .decrypt_as(stored.clone(), column().into()) + let opened = JsonPlaintext::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!(opened.value(), &profile()); @@ -536,8 +571,8 @@ async fn a_json_document_dispatches_by_name() { } } // The dispatch opens what the typed call sealed. - let typed: json::Json = keyset - .encrypt_as(&JsonPlaintext::from(profile()), column()) + let typed: json::Json = JsonPlaintext::from(profile()) + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); let opened = targets::decrypt( @@ -622,10 +657,10 @@ async fn a_stored_value_that_is_not_a_json_document_is_refused() { // JSON text a plain text field sealed is a document: `c` is the same // leaf either way. let c = sealed_from_frame(&keyset, frame(profile_text().as_str())).await; - let opened: JsonPlaintext = cipher - .decrypt_as(stored_as::(c), column().into()) - .await - .unwrap(); + let opened = + JsonPlaintext::decrypt_from_with_context(stored_as::(c), &cipher, column()) + .await + .unwrap(); assert_eq!(opened.value(), &profile()); // Text that is not JSON, the bytes of JSON text, and a number are not. for value in [ @@ -636,10 +671,10 @@ async fn a_stored_value_that_is_not_a_json_document_is_refused() { frame(None::), ] { let c = sealed_from_frame(&keyset, value).await; - let error = cipher - .decrypt_as::(stored_as::(c), column().into()) - .await - .unwrap_err(); + let error = + JsonPlaintext::decrypt_from_with_context(stored_as::(c), &cipher, column()) + .await + .unwrap_err(); assert!( matches!( error, @@ -669,36 +704,33 @@ async fn a_leaf_sealed_from_a_frame_opens_as_the_typed_plaintext() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); let c = sealed_from_frame(&keyset, frame::(-7)).await; - let opened: i32 = cipher - .decrypt_as(stored_as::(c), column().into()) - .await - .unwrap(); + let opened = + i32::decrypt_from_with_context(stored_as::(c), &cipher, column()) + .await + .unwrap(); assert_eq!(opened, -7); let c = sealed_from_frame(&keyset, frame::(i64::MAX)).await; - let opened: i64 = cipher - .decrypt_as(stored_as::(c), column().into()) + let opened = i64::decrypt_from_with_context(stored_as::(c), &cipher, column()) .await .unwrap(); assert_eq!(opened, i64::MAX); let c = sealed_from_frame(&keyset, frame::(true)).await; - let opened: bool = cipher - .decrypt_as(stored_as::(c), column().into()) - .await - .unwrap(); + let opened = + bool::decrypt_from_with_context(stored_as::(c), &cipher, column()) + .await + .unwrap(); assert!(opened); let c = sealed_from_frame(&keyset, frame::(-0.0)).await; - let opened: f32 = cipher - .decrypt_as(stored_as::(c), column().into()) + let opened = f32::decrypt_from_with_context(stored_as::(c), &cipher, column()) .await .unwrap(); assert_eq!(opened.to_bits(), (-0.0f32).to_bits()); let c = sealed_from_frame(&keyset, frame::(f64::NAN)).await; - let opened: f64 = cipher - .decrypt_as(stored_as::(c), column().into()) + let opened = f64::decrypt_from_with_context(stored_as::(c), &cipher, column()) .await .unwrap(); assert_eq!(opened.to_bits(), f64::NAN.to_bits()); @@ -816,16 +848,21 @@ async fn every_producible_type_dispatches_by_name() { async fn storage_only_text_round_trips_through_both_entry_points() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); - let stored: text::Text = keyset - .encrypt_as(&"a private note".to_owned(), column()) + let stored: text::Text = "a private note" + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c"]); - let opened: String = cipher.decrypt_as(stored, column().into()).await.unwrap(); + let opened = String::decrypt_from_with_context(stored, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, "a private note"); let named: text::Text = by_name(&keyset, "Text", frame("note")).await; - let opened: String = cipher.decrypt_as(named, column().into()).await.unwrap(); + let opened = String::decrypt_from_with_context(named, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, "note"); // A storage-only type answers no query. let error = refused(targets::query( @@ -850,8 +887,9 @@ fn positions(filter: &eql_bindings::v3::terms::BloomFilter) -> std::collections: async fn text_match_filters_contain_their_substrings() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); - let stored: text::TextMatch = keyset - .encrypt_as(&"Alice@Example.com".to_owned(), column()) + let stored: text::TextMatch = "Alice@Example.com" + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); assert_v4_envelope(&stored, &["v", "i", "c", "bf"]); @@ -859,8 +897,7 @@ async fn text_match_filters_contain_their_substrings() { stored.bf.0.iter().all(|&p| (0..2048).contains(&p)), "a 2048-bit filter" ); - let opened: String = cipher - .decrypt_as(stored.clone(), column().into()) + let opened = String::decrypt_from_with_context(stored.clone(), &cipher, column()) .await .unwrap(); assert_eq!(opened, "Alice@Example.com"); @@ -877,8 +914,9 @@ async fn text_match_filters_contain_their_substrings() { ("ice@ex", true), ("bob", false), ] { - let query: text::TextMatchQuery = keyset - .encrypt_as(&needle.to_owned(), column()) + let query: text::TextMatchQuery = needle + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); assert_eq!( @@ -897,11 +935,15 @@ async fn text_match_filters_contain_their_substrings() { // filter type builds both, and an empty filter as a query would match // every row. A two-letter word is a token of its own, so it stores. for short in ["a", "", " "] { - let stored: Result = - keyset.encrypt_as(&short.to_owned(), column()).await; + let stored: Result = short + .to_owned() + .encrypt_into_with_context(&keyset, column()) + .await; assert!(stored.is_err(), "{short:?} cuts no token"); - let query: Result = - keyset.encrypt_as(&short.to_owned(), column()).await; + let query: Result = short + .to_owned() + .encrypt_into_with_context(&keyset, column()) + .await; assert!( query.is_err(), "{short:?}: a query with no token would match every row" @@ -918,29 +960,31 @@ async fn two_letter_surnames_store_and_are_found() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); for &surname in SURNAMES { - let bare: text::TextMatch = keyset - .encrypt_as(&surname.to_owned(), column()) + let bare: text::TextMatch = surname + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap_or_else(|error| panic!("{surname:?} stores: {error}")); assert!(!bare.bf.0.is_empty(), "{surname:?} stores a token"); - let query: text::TextMatchQuery = keyset - .encrypt_as(&surname.to_lowercase(), column()) + let query: text::TextMatchQuery = surname + .to_lowercase() + .encrypt_into_with_context(&keyset, column()) .await .unwrap_or_else(|error| panic!("{surname:?} is a query: {error}")); assert_eq!( query.bf, bare.bf, "{surname:?}: the query's filter is the stored one's" ); - let full: text::TextMatch = keyset - .encrypt_as(&format!("Mike {surname}"), column()) + let full: text::TextMatch = format!("Mike {surname}") + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); assert!( positions(&query.bf).is_subset(&positions(&full.bf)), "{surname:?} is found in \"Mike {surname}\"" ); - let longer: text::TextMatch = keyset - .encrypt_as(&format!("Mike {surname}ward"), column()) + let longer: text::TextMatch = format!("Mike {surname}ward") + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); assert!( @@ -968,8 +1012,9 @@ const SURNAMES: &[&str] = &[ async fn text_match_filter_positions_are_pinned() { let (cipher, _) = common::cipher().await; let keyset = cipher.default_keyset(); - let query: text::TextMatchQuery = keyset - .encrypt_as(&"alice".to_owned(), column()) + let query: text::TextMatchQuery = "alice" + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); // 3 tokens ("ali", "lic", "ice") at 6 positions each, de-duplicated. @@ -1004,22 +1049,30 @@ async fn a_value_of_another_kind_is_refused_not_converted() { // plaintext as vitaminc reads it: an integer of another width that fits // opens unchanged, one that does not fit is refused, and a value of // another kind is refused. - let narrow: integer::Integer = keyset.encrypt_as(&42i32, column()).await.unwrap(); + let narrow: integer::Integer = 42i32 + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let moved = bigint::Bigint { v: narrow.v, i: narrow.i.clone(), c: narrow.c.clone(), }; - let opened: i64 = cipher.decrypt_as(moved, column().into()).await.unwrap(); + let opened = i64::decrypt_from_with_context(moved, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, 42); - let wide: bigint::Bigint = keyset.encrypt_as(&i64::MAX, column()).await.unwrap(); + let wide: bigint::Bigint = i64::MAX + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let moved = integer::Integer { v: narrow.v, i: wide.i, c: wide.c, }; - let opened: Result = cipher.decrypt_as(moved, column().into()).await; + let opened = i32::decrypt_from_with_context(moved, &cipher, column()).await; assert!(opened.is_err(), "i64::MAX does not fit an i32"); let moved = smallint::Smallint { @@ -1027,24 +1080,32 @@ async fn a_value_of_another_kind_is_refused_not_converted() { i: narrow.i.clone(), c: narrow.c.clone(), }; - let opened: i16 = cipher.decrypt_as(moved, column().into()).await.unwrap(); + let opened = i16::decrypt_from_with_context(moved, &cipher, column()) + .await + .unwrap(); assert_eq!(opened, 42, "an i32 that fits opens as an i16"); - let big: integer::Integer = keyset.encrypt_as(&70_000i32, column()).await.unwrap(); + let big: integer::Integer = 70_000i32 + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let moved = smallint::Smallint { v: narrow.v, i: big.i, c: big.c, }; - let opened: Result = cipher.decrypt_as(moved, column().into()).await; + let opened = i16::decrypt_from_with_context(moved, &cipher, column()).await; assert!(opened.is_err(), "70000 does not fit an i16"); - let flag: boolean::Boolean = keyset.encrypt_as(&true, column()).await.unwrap(); + let flag: boolean::Boolean = true + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); let moved = integer::Integer { v: narrow.v, i: flag.i, c: flag.c, }; - let opened: Result = cipher.decrypt_as(moved, column().into()).await; + let opened = i32::decrypt_from_with_context(moved, &cipher, column()).await; assert!(opened.is_err(), "a bool is not an integer"); } @@ -1084,10 +1145,16 @@ fn postgres_finds_two_letter_surnames() { ]; for (id, name) in names { let (matched, search) = runtime.block_on(async { - let matched: text::TextMatch = - keyset.encrypt_as(&name.to_owned(), column()).await.unwrap(); - let search: text::TextSearch = - keyset.encrypt_as(&name.to_owned(), column()).await.unwrap(); + let matched: text::TextMatch = name + .to_owned() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); + let search: text::TextSearch = name + .to_owned() + .encrypt_into_with_context(&keyset, column()) + .await + .unwrap(); (matched, search) }); db.execute( @@ -1104,12 +1171,14 @@ fn postgres_finds_two_letter_surnames() { } let mut found = |needle: &str| -> (Vec, Vec) { let (matched, search) = runtime.block_on(async { - let matched: text::TextMatchQuery = keyset - .encrypt_as(&needle.to_owned(), column()) + let matched: text::TextMatchQuery = needle + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); - let search: text::TextSearchMatchQuery = keyset - .encrypt_as(&needle.to_owned(), column()) + let search: text::TextSearchMatchQuery = needle + .to_owned() + .encrypt_into_with_context(&keyset, column()) .await .unwrap(); (matched, text::TextSearchQuery::from(search)) @@ -1179,36 +1248,36 @@ fn postgres_stores_and_queries_every_producible_v4_type() { for (id, age, balance) in [(1, 42, 10i64), (2, 7, 20), (3, 42, 30)] { let row = runtime.block_on(async { ( - keyset - .encrypt_as::<_, integer::Integer>(&age, column()) + age.encrypt_into_with_context::(&keyset, column()) .await .unwrap(), - keyset - .encrypt_as::<_, bigint::Bigint>(&balance, column()) + balance + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), - keyset - .encrypt_as::<_, boolean::Boolean>(&(id % 2 == 0), column()) + (id % 2 == 0) + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), - keyset - .encrypt_as::<_, real::Real>(&0.5f32, column()) + 0.5f32 + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), - keyset - .encrypt_as::<_, double::Double>(&0.25f64, column()) + 0.25f64 + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), - keyset - .encrypt_as::<_, text::Text>(&"note".to_owned(), column()) + "note" + .to_owned() + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), - keyset - .encrypt_as::<_, text::TextMatch>(&format!("user{id}@example.com"), column()) + format!("user{id}@example.com") + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), - keyset - .encrypt_as::<_, json::Json>(&JsonPlaintext::from(profile()), column()) + JsonPlaintext::from(profile()) + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ) @@ -1240,7 +1309,12 @@ fn postgres_stores_and_queries_every_producible_v4_type() { let mut matching = |needle: &str| -> Vec { let probe: text::TextMatchQuery = runtime - .block_on(async { keyset.encrypt_as(&needle.to_owned(), column()).await }) + .block_on(async { + needle + .to_owned() + .encrypt_into_with_context(&keyset, column()) + .await + }) .unwrap(); db.query( "SELECT id FROM v4_types WHERE name @@ $1::text::jsonb::eql_v4.query_text_match ORDER BY id", @@ -1260,7 +1334,12 @@ fn postgres_stores_and_queries_every_producible_v4_type() { // stored value's, so that operand order finds nothing. Only // `stored @@ query` works until then; the README says so. let probe: text::TextMatchQuery = runtime - .block_on(async { keyset.encrypt_as(&"user2".to_owned(), column()).await }) + .block_on(async { + "user2" + .to_owned() + .encrypt_into_with_context(&keyset, column()) + .await + }) .unwrap(); let reversed: Vec = db .query( @@ -1288,61 +1367,61 @@ fn postgres_stores_and_queries_every_producible_v4_type() { .unwrap(); let open = |i: usize| -> String { row.get(i) }; runtime.block_on(async { - let opened: i32 = cipher - .decrypt_as( - serde_json::from_str::(&open(0)).unwrap(), - column().into(), - ) - .await - .unwrap(); + let opened = i32::decrypt_from_with_context( + serde_json::from_str::(&open(0)).unwrap(), + &cipher, + column(), + ) + .await + .unwrap(); assert_eq!(opened, 7); - let opened: i64 = cipher - .decrypt_as( - serde_json::from_str::(&open(1)).unwrap(), - column().into(), - ) - .await - .unwrap(); + let opened = i64::decrypt_from_with_context( + serde_json::from_str::(&open(1)).unwrap(), + &cipher, + column(), + ) + .await + .unwrap(); assert_eq!(opened, 20); - let opened: bool = cipher - .decrypt_as( - serde_json::from_str::(&open(2)).unwrap(), - column().into(), - ) - .await - .unwrap(); + let opened = bool::decrypt_from_with_context( + serde_json::from_str::(&open(2)).unwrap(), + &cipher, + column(), + ) + .await + .unwrap(); assert!(opened); - let opened: f32 = cipher - .decrypt_as( - serde_json::from_str::(&open(3)).unwrap(), - column().into(), - ) - .await - .unwrap(); + let opened = f32::decrypt_from_with_context( + serde_json::from_str::(&open(3)).unwrap(), + &cipher, + column(), + ) + .await + .unwrap(); assert_eq!(opened, 0.5); - let opened: f64 = cipher - .decrypt_as( - serde_json::from_str::(&open(4)).unwrap(), - column().into(), - ) - .await - .unwrap(); + let opened = f64::decrypt_from_with_context( + serde_json::from_str::(&open(4)).unwrap(), + &cipher, + column(), + ) + .await + .unwrap(); assert_eq!(opened, 0.25); - let opened: String = cipher - .decrypt_as( - serde_json::from_str::(&open(5)).unwrap(), - column().into(), - ) - .await - .unwrap(); + let opened = String::decrypt_from_with_context( + serde_json::from_str::(&open(5)).unwrap(), + &cipher, + column(), + ) + .await + .unwrap(); assert_eq!(opened, "note"); - let opened: JsonPlaintext = cipher - .decrypt_as( - serde_json::from_str::(&open(6)).unwrap(), - column().into(), - ) - .await - .unwrap(); + let opened = JsonPlaintext::decrypt_from_with_context( + serde_json::from_str::(&open(6)).unwrap(), + &cipher, + column(), + ) + .await + .unwrap(); assert_eq!(opened.value(), &profile()); }); @@ -1424,20 +1503,19 @@ fn postgres_finds_number_equality_rows() { for (id, age, balance, ratio, score) in rows { let stored = runtime.block_on(async { ( - keyset - .encrypt_as::<_, integer::IntegerEq>(&age, column()) + age.encrypt_into_with_context::(&keyset, column()) .await .unwrap(), - keyset - .encrypt_as::<_, bigint::BigintEq>(&balance, column()) + balance + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), - keyset - .encrypt_as::<_, real::RealEq>(&ratio, column()) + ratio + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), - keyset - .encrypt_as::<_, double::DoubleEq>(&score, column()) + score + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ) @@ -1474,32 +1552,28 @@ fn postgres_finds_number_equality_rows() { let q = |stored: Result| stored.unwrap(); let age = |v: i32| { q(runtime.block_on(async { - keyset - .encrypt_as::<_, integer::IntegerEqQuery>(&v, column()) + v.encrypt_into_with_context::(&keyset, column()) .await .map(|q| json(&q)) })) }; let balance = |v: i64| { q(runtime.block_on(async { - keyset - .encrypt_as::<_, bigint::BigintEqQuery>(&v, column()) + v.encrypt_into_with_context::(&keyset, column()) .await .map(|q| json(&q)) })) }; let ratio = |v: f32| { q(runtime.block_on(async { - keyset - .encrypt_as::<_, real::RealEqQuery>(&v, column()) + v.encrypt_into_with_context::(&keyset, column()) .await .map(|q| json(&q)) })) }; let score = |v: f64| { q(runtime.block_on(async { - keyset - .encrypt_as::<_, double::DoubleEqQuery>(&v, column()) + v.encrypt_into_with_context::(&keyset, column()) .await .map(|q| json(&q)) })) @@ -1563,12 +1637,12 @@ fn postgres_stores_and_finds_smallint_rows() { for (id, value) in [(1, i16::MIN), (2, 7i16), (3, i16::MAX), (4, 7)] { let (plain, searchable) = runtime.block_on(async { ( - keyset - .encrypt_as::<_, smallint::Smallint>(&value, column()) + value + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), - keyset - .encrypt_as::<_, smallint::SmallintEq>(&value, column()) + value + .encrypt_into_with_context::(&keyset, column()) .await .unwrap(), ) @@ -1583,7 +1657,7 @@ fn postgres_stores_and_finds_smallint_rows() { } let mut ids = |value: i16| -> Vec { let query: smallint::SmallintEqQuery = runtime - .block_on(async { keyset.encrypt_as(&value, column()).await }) + .block_on(async { value.encrypt_into_with_context(&keyset, column()).await }) .unwrap(); db.query( "SELECT id FROM v4_smallint WHERE searchable = $1::text::jsonb::eql_v4.query_smallint_eq ORDER BY id", @@ -1603,14 +1677,14 @@ fn postgres_stores_and_finds_smallint_rows() { .query_one("SELECT plain::text FROM v4_smallint WHERE id = 1", &[]) .unwrap() .get(0); - let opened: i16 = runtime + let opened = runtime .block_on(async { - cipher - .decrypt_as( - serde_json::from_str::(&back).unwrap(), - column().into(), - ) - .await + i16::decrypt_from_with_context( + serde_json::from_str::(&back).unwrap(), + &cipher, + column(), + ) + .await }) .unwrap(); assert_eq!(opened, i16::MIN); @@ -1679,29 +1753,29 @@ mod go_tally { ) { let column = |name: &str| Identifier::for_column("profiles", name).unwrap(); let parsed = |name: &str| row.stored[name].clone(); - let small: i16 = cipher - .decrypt_as( - serde_json::from_value::(parsed("small")).unwrap(), - column("small").into(), - ) - .await - .unwrap(); + let small = i16::decrypt_from_with_context( + serde_json::from_value::(parsed("small")).unwrap(), + cipher, + column("small"), + ) + .await + .unwrap(); assert_eq!(small, row.plaintext.small, "row {} small", row.id); - let tally: i16 = cipher - .decrypt_as( - serde_json::from_value::(parsed("tally")).unwrap(), - column("tally").into(), - ) - .await - .unwrap(); + let tally = i16::decrypt_from_with_context( + serde_json::from_value::(parsed("tally")).unwrap(), + cipher, + column("tally"), + ) + .await + .unwrap(); assert_eq!(tally, row.plaintext.tally, "row {} tally", row.id); - let wide: i16 = cipher - .decrypt_as( - serde_json::from_value::(parsed("wide")).unwrap(), - column("wide").into(), - ) - .await - .unwrap(); + let wide = i16::decrypt_from_with_context( + serde_json::from_value::(parsed("wide")).unwrap(), + cipher, + column("wide"), + ) + .await + .unwrap(); assert_eq!(i32::from(wide), row.plaintext.wide, "row {} wide", row.id); } } @@ -1788,7 +1862,9 @@ mod go_settings { let stored: json::Json = serde_json::from_value(stored.clone()) .unwrap_or_else(|error| panic!("row {} {name}: {error}", row.id)); let column = Identifier::for_column("settings", name.as_str()).unwrap(); - let opened: JsonPlaintext = cipher.decrypt_as(stored, column.into()).await.unwrap(); + let opened = JsonPlaintext::decrypt_from_with_context(stored, cipher, column) + .await + .unwrap(); assert_eq!( opened.value(), &row.plaintext[name], @@ -2079,8 +2155,8 @@ mod go_rows { let mut stored = BTreeMap::new(); macro_rules! seal { ($name:literal, $ty:ty, $value:expr) => {{ - let sealed: $ty = keyset - .encrypt_as(&$value, column($name)) + let sealed: $ty = $value + .encrypt_into_with_context(&keyset, column($name)) .await .unwrap_or_else(|e| panic!("row {id} {}: {e}", $name)); stored.insert($name.to_owned(), serde_json::to_value(&sealed).unwrap()); @@ -2116,8 +2192,8 @@ mod go_rows { let p = &row.plaintext; macro_rules! eq { ($name:literal, $query:ty, $value:expr) => {{ - let query: $query = keyset - .encrypt_as(&$value, column($name)) + let query: $query = $value + .encrypt_into_with_context(&keyset, column($name)) .await .unwrap_or_else(|e| panic!("row {} {}: {e}", row.id, $name)); assert_eq!( @@ -2173,92 +2249,101 @@ mod go_rows { row: &Row, ) { let p = &row.plaintext; - let visits: i32 = cipher - .decrypt_as( - parsed::(row, "visits"), - column("visits").into(), - ) - .await - .unwrap(); + let visits = i32::decrypt_from_with_context( + parsed::(row, "visits"), + cipher, + column("visits"), + ) + .await + .unwrap(); assert_eq!(visits, p.visits); - let balance: i64 = cipher - .decrypt_as( - parsed::(row, "balance"), - column("balance").into(), - ) - .await - .unwrap(); + let balance = i64::decrypt_from_with_context( + parsed::(row, "balance"), + cipher, + column("balance"), + ) + .await + .unwrap(); assert_eq!(balance, p.balance); - let active: bool = cipher - .decrypt_as( - parsed::(row, "active"), - column("active").into(), - ) - .await - .unwrap(); + let active = bool::decrypt_from_with_context( + parsed::(row, "active"), + cipher, + column("active"), + ) + .await + .unwrap(); assert_eq!(active, p.active); - let ratio: f32 = cipher - .decrypt_as(parsed::(row, "ratio"), column("ratio").into()) - .await - .unwrap(); + let ratio = f32::decrypt_from_with_context( + parsed::(row, "ratio"), + cipher, + column("ratio"), + ) + .await + .unwrap(); assert_eq!(ratio.to_bits(), p.ratio.to_bits()); - let score: f64 = cipher - .decrypt_as( - parsed::(row, "score"), - column("score").into(), - ) - .await - .unwrap(); + let score = f64::decrypt_from_with_context( + parsed::(row, "score"), + cipher, + column("score"), + ) + .await + .unwrap(); assert_eq!(score.to_bits(), p.score.to_bits()); - let note: String = cipher - .decrypt_as(parsed::(row, "note"), column("note").into()) - .await - .unwrap(); + let note = String::decrypt_from_with_context( + parsed::(row, "note"), + cipher, + column("note"), + ) + .await + .unwrap(); assert_eq!(note, p.note); - let name: String = cipher - .decrypt_as( - parsed::(row, "name"), - column("name").into(), - ) - .await - .unwrap(); + let name = String::decrypt_from_with_context( + parsed::(row, "name"), + cipher, + column("name"), + ) + .await + .unwrap(); assert_eq!(name, p.name); - let age: i32 = cipher - .decrypt_as( - parsed::(row, "age"), - column("age").into(), - ) - .await - .unwrap(); + let age = i32::decrypt_from_with_context( + parsed::(row, "age"), + cipher, + column("age"), + ) + .await + .unwrap(); assert_eq!(age, p.age); - let rank: i64 = cipher - .decrypt_as( - parsed::(row, "rank"), - column("rank").into(), - ) - .await - .unwrap(); + let rank = i64::decrypt_from_with_context( + parsed::(row, "rank"), + cipher, + column("rank"), + ) + .await + .unwrap(); assert_eq!(rank, p.rank); - let weight: f32 = cipher - .decrypt_as( - parsed::(row, "weight"), - column("weight").into(), - ) - .await - .unwrap(); + let weight = f32::decrypt_from_with_context( + parsed::(row, "weight"), + cipher, + column("weight"), + ) + .await + .unwrap(); assert_eq!(weight.to_bits(), p.weight.to_bits()); - let price: f64 = cipher - .decrypt_as( - parsed::(row, "price"), - column("price").into(), - ) - .await - .unwrap(); + let price = f64::decrypt_from_with_context( + parsed::(row, "price"), + cipher, + column("price"), + ) + .await + .unwrap(); assert_eq!(price.to_bits(), p.price.to_bits()); - let email: String = cipher - .decrypt_as(parsed::(row, "email"), column("email").into()) - .await - .unwrap(); + let email = String::decrypt_from_with_context( + parsed::(row, "email"), + cipher, + column("email"), + ) + .await + .unwrap(); assert_eq!(email, p.email); } } diff --git a/packages/stack-encrypt-derive/docs/attributes.md b/packages/stack-encrypt-derive/docs/attributes.md index 7e3f0ba70f..196395122a 100644 --- a/packages/stack-encrypt-derive/docs/attributes.md +++ b/packages/stack-encrypt-derive/docs/attributes.md @@ -1,24 +1,12 @@ # Attributes -All attributes are written inside `#[stash(...)]`. There are two pairs of -derives, and they read the same attributes. - -**Use `EncryptTarget` and `DecryptSource`.** With these, encryption takes -the plaintext by value. The call `user.encrypt_into(&keyset)` moves `user` -into the encryption, so the caller has no copy left. The encryption then -wipes the plaintext: it overwrites the memory that held each encrypted -field. Only this pair accepts `passthrough`. - -`EncryptFrom` and `DecryptInto` are the older pair. They encrypt a borrowed -plaintext, `&user`. The caller keeps the original, and must wipe it. Plans -and the dynamic engine still use them. The older pair will be removed once -plans and the dynamic engine use the new pair (#1488). - -Derive one pair or the other on a record. `EncryptTarget` and -`DecryptSource` already implement `EncryptFrom` and `DecryptInto` for the -record, so an owned record also works where the older pair is expected. -If you derive both pairs, those traits are implemented twice. That does not -compile. +All attributes are written inside `#[stash(...)]`. The two derives, +`EncryptTarget` and `DecryptSource`, read the same attributes. + +Encryption takes the plaintext by value. The call +`user.encrypt_into(&keyset)` moves `user` into the encryption, so the +caller has no copy left. The encryption then wipes the plaintext: it +overwrites the memory that held each encrypted field. ## On the struct @@ -49,7 +37,7 @@ field from the whole `User`. To encrypt a struct field by field, use | Attribute | Effect | |---|---| | `context_field` | This field stores the record's context, and decryption reads it back. At most one per record. Not with any other field attribute. | -| `identity = "..."` | With `struct` only. The field is derived under `("", "")`, in place of the plaintext field's name. One plain segment. The plan builder's `.identity(segment)` does the same. | +| `identity = "..."` | With `struct` only. The field is derived under `("", "")`, in place of the plaintext field's name. One plain segment. | | `from = field` or `from = 0` | With `struct` only. Derive this field from `plaintext.field`, or `plaintext.0` for a tuple struct, when the names differ. Each plaintext field has one output, so two fields `from` the same plaintext field are refused. A ciphertext with search terms is one `Encrypted` field. | | `default` or `default = expr` | The field is not derived. It is filled with `Default::default()` or `expr`. It is not encrypted and not authenticated. | | `decrypt` | Decryption opens this field. Use it only when the field types do not decide it. See [Which field decryption opens](#which-field-decryption-opens). | @@ -58,8 +46,8 @@ field from the whole `User`. To encrypt a struct field by field, use A field cannot have a `context = ".."` of its own, on any derive. Every field of a `plaintext` record shares the caller's context. Every field of a `struct` record sits under the record's scope, and `identity` changes only -its second part. To write one value under two contexts, use a plan that -picks the same source twice. +its second part. To write one value under two contexts, encrypt it twice, +once under each context. **Upgrading from 0.2.0.** Version 0.2.0 accepted a field `context = ".."` and `#[stash(nested)]`. Each change below derives the field under a @@ -210,9 +198,9 @@ change it, and the encrypted fields beside it still decrypt. So: - If a value must be readable and also detect changes, it belongs in the record's context. A `struct` record cannot take a caller's context yet. -The field's type must implement `Clone`. The owned conversion never clones -it, but the older `EncryptFrom` form of the record does, and plans run that -form. +The field's type must implement `Clone`. Encrypting the record by value +never clones it. The borrowed form of the record's declaration does, and a +record that is a field of another record can run that form. ### Wiping @@ -260,19 +248,18 @@ compile error at the term, because a term needs a `CallerContext`. A call that leaves out a required context is a compile error at the call (`AllowsNoContext`), even if the context type implements `Default`. -### The structural traits +### A record as a field of another record -Each owned derive also implements the structural traits: +Each derive also implements the trait that lets a record be a field of +another record: -- `EncryptTarget` also implements `EncryptFrom` and `Decryptable`. - `EncryptFrom` converts the record's structural context into the owned - input. The structural context is a `DeclaredContext`, a `CallerContext`, - or a `NonEmpty`. -- `DecryptSource` also implements `DecryptInto` and `DecryptField`. +- `EncryptTarget` also implements `Decryptable`, which says whether the + record holds a ciphertext that decryption can open. +- `DecryptSource` also implements `DecryptField`, which opens the record + through its own `DecryptSource`, under the context the outer record + gives the field. -So an owned record can be a field of a `#[derive(EncryptFrom)]` record, or a -typed field of a plan (`encrypt_into::`), with no second derive. -A hand-written owned target that will be a record field must implement +A hand-written target that will be a record field must implement `Decryptable` and `DecryptField`, as well as `DecryptSource`. ### Example @@ -421,9 +408,9 @@ record's fields. A `struct = ..` record takes no context from the caller. A `plaintext = ..` record and a record with a `context_field` do take one. See [The context a -call takes](#the-context-a-call-takes). A plan that contains a `struct = ..` -record can still extend each field's context. Under a plan extension `7u64`, the `age` field -is derived under `(("users", "age"), 7u64)`. +call takes](#the-context-a-call-takes). A record that lists a scope, such +as `scope(tenant: u64)`, extends each field's context with it. Under the +tenant `7u64`, the `age` field is derived under `(("users", "age"), 7u64)`. ### Why the scope is required diff --git a/packages/stack-encrypt-derive/src/attrs.rs b/packages/stack-encrypt-derive/src/attrs.rs index ee3a176b8f..246f8b2272 100644 --- a/packages/stack-encrypt-derive/src/attrs.rs +++ b/packages/stack-encrypt-derive/src/attrs.rs @@ -194,14 +194,14 @@ impl ContainerAttrs { let plaintext: Type = meta.value()?.parse()?; // The type is spliced into the impl header as // written, where a reference has no lifetime to - // name. The generic impl (no `plaintext` at all) - // already accepts `&str` and friends. + // name. A plaintext is also passed by value and wiped, + // which a reference cannot be. if let Type::Reference(_) = plaintext { return Err(syn::Error::new_spanned( &plaintext, "`plaintext` must be an owned type: a reference plaintext has no \ - lifetime the generated impl can name. Omit `plaintext` for an impl \ - generic over the source, which accepts references too.", + lifetime the generated impl can name, and a plaintext is passed by \ + value and wiped. Name the owned type, such as `String` for `&str`.", )); } if plaintexts.contains(&plaintext) { @@ -321,8 +321,7 @@ pub(crate) struct FieldAttrs { pub(crate) context_field: bool, /// `#[stash(identity = "...")]`: with `struct = ..`, the label segment /// this field is keyed under, in place of the plaintext field's name: - /// the field is derived under `("", "")`. The plan - /// builder's `.identity(segment)`. + /// the field is derived under `("", "")`. pub(crate) identity: Option, /// `#[stash(from = field)]` / `#[stash(from = 0)]`: with `struct = ..`, /// derive this field from a plaintext field whose name differs from its @@ -359,9 +358,8 @@ impl FieldAttrs { return Ok(()); } // Removed rather than unknown, so the message can say what - // to write instead. A plan has exactly one context source, - // and every field sits under it: a field sealed under a - // literal of its own has no plan form. + // to write instead. A record has exactly one context source, + // and every field sits under it. if meta.path.is_ident("context") { return Err(meta.error( "a field-level `context = \"..\"` is no longer accepted (0.2.0 accepted \ @@ -370,9 +368,9 @@ impl FieldAttrs { record's context, so key it under another segment with \ `identity = \"..\"`. Either replacement is a different context from the \ old literal: data written with the literal does not decrypt under it and \ - its terms do not match, so re-encrypt that data. Writing one value under \ - two contexts (a dual write) is a fields plan that picks the same source \ - twice, not a derive", + its terms do not match, so re-encrypt that data. To write one value \ + under two contexts (a dual write), encrypt it twice, once under each \ + context", )); } // Each of these is singular by meaning, so a repeat is a diff --git a/packages/stack-encrypt-derive/src/decrypt.rs b/packages/stack-encrypt-derive/src/decrypt.rs deleted file mode 100644 index 47d32dce82..0000000000 --- a/packages/stack-encrypt-derive/src/decrypt.rs +++ /dev/null @@ -1,293 +0,0 @@ -//! Emit ciphertext inspection and core-owned opening descriptions. -use crate::shape::{trait_impl, zip, Field, Record}; -use proc_macro2::{Span, TokenStream}; -use quote::{quote, quote_spanned, ToTokens}; -use syn::spanned::Spanned; -use syn::{parse_quote, DeriveInput, Ident, LitStr, Member, Path, PathArguments, Result, Type}; - -pub(crate) fn derive(input: DeriveInput) -> Result { - let record = Record::parse(&input)?; - let krate = &record.krate; - let name = &input.ident; - let explicit = record.fields.iter().any(|f| f.decrypt); - let groups = if explicit { - match Mode::classify(record.fields.iter().filter(|f| f.decrypt).collect(), name)? { - Mode::Whole(field) => vec![(None, vec![field])], - Mode::ByField(fields) => fields.into_iter().map(|f| (f.from(), vec![f])).collect(), - } - } else { - match Auto::classify(&record) { - Auto::Whole(fields) => vec![(None, fields)], - Auto::ByField(fields) => fields - .into_iter() - .map(|(from, field)| (Some(from), vec![field])) - .collect(), - } - }; - let checks = if explicit { - TokenStream::new() - } else { - let checks = groups - .iter() - .map(|(from, fields)| check(krate, name, *from, fields)); - quote!(#(#checks)*) - }; - let (definition_check, body_check) = if input.generics.params.is_empty() { - (quote!(const _: () = { #checks };), TokenStream::new()) - } else { - (TokenStream::new(), quote!(let () = const { #checks };)) - }; - let context = record.context_type(true); - let (plaintexts, generic) = record.sources(parse_quote!(__P)); - let mut impls = Vec::new(); - for plaintext in plaintexts { - let mut generics = input.generics.clone(); - if generic { - generics.params.push(parse_quote!(__P: 'static)); - } - generics - .make_where_clause() - .predicates - .push(parse_quote!(Self: 'static)); - let mut operations = Vec::new(); - for (index, (from, fields)) in groups.iter().enumerate() { - let output = if from.is_some() { - parse_quote!(_) - } else { - plaintext.clone() - }; - if from.is_none() { - for field in fields { - let ty = &field.ty; - let ctx = record.field_context_type(field); - let predicates = &mut generics.make_where_clause().predicates; - if explicit { - predicates.push(parse_quote!(#ty: #krate::target::DecryptInto<#output>)); - predicates.push(parse_quote!(#ctx: Into<<#ty as #krate::target::DecryptInto<#output>>::Context>)); - } else { - predicates - .push(parse_quote!(#ty: #krate::target::DecryptField<#output, #ctx>)); - } - } - } - let operation = if explicit { - let field = fields[0]; - let ty = &field.ty; - let member = &field.member; - let ctx = record.context_expr(field); - quote_spanned!(ty.span()=> <#ty as #krate::target::DecryptInto<#output>>::decryption::<__K>(self.#member, #ctx.into())) - } else { - let calls: Vec<_> = fields.iter().map(|field| { - let ty = &field.ty; let member = &field.member; - let ctx = record.context_expr(field); - let ctx_ty = record.field_context_type(field); - quote_spanned!(ty.span()=> <#ty as #krate::target::DecryptField<#output, #ctx_ty>>::decryption_field::<__K>(self.#member, #ctx)) - }).collect(); - // Evaluate every inspection before chaining: a context error belongs - // to the whole declaration, not to an Option::or_else closure. - let locals: Vec<_> = (0..calls.len()) - .map(|n| Ident::new(&format!("__candidate_{n}"), Span::call_site())) - .collect(); - let bindings = calls - .iter() - .zip(&locals) - .map(|(call, local)| quote!(let #local = #call;)); - let first = &locals[0]; - let rest = &locals[1..]; - quote!({ #(#bindings)* #first #(.or(#rest))* .unwrap_or_else(|| #krate::target::Decryption::failed(#krate::Error::NotOpened)) }) - }; - operations.push(( - operation, - Ident::new(&format!("__group_{index}"), Span::call_site()), - )); - } - let body = if groups[0].0.is_none() { - operations.remove(0).0 - } else { - let literal = struct_literal_path(&plaintext)?; - let assignments = groups - .iter() - .zip(&operations) - .map(|((from, _), (_, local))| quote!(#from: #local)); - let output = quote!(#literal { #(#assignments),* }); - zip(operations, output) - }; - let stored = record.context_field().map(|field| { - let member = &field.member; - quote!(let __context = match __context.validate(self.#member) { - Ok(context) => context, Err(error) => return #krate::target::Decryption::failed(error), - };) - }); - impls.push(trait_impl(&input, &generics, quote!(#krate::target::DecryptInto<#plaintext>), quote! { - type Context = #context; - fn decryption<__K: 'static>(self, __context: Self::Context) -> #krate::target::Decryption<#plaintext, __K> { - #body_check #stored #body - } - })); - } - let mut generics = input.generics.clone(); - generics.params.push(parse_quote!(__P)); - generics.params.push(parse_quote!(__Ctx)); - generics - .make_where_clause() - .predicates - .push(parse_quote!(Self: #krate::target::DecryptInto<__P>)); - generics - .make_where_clause() - .predicates - .push(parse_quote!(__Ctx: Into<>::Context>)); - let field = trait_impl( - &input, - &generics, - quote!(#krate::target::DecryptField<__P, __Ctx>), - quote! { - fn decryption_field<__K: 'static>(self, context: __Ctx) -> Option<#krate::target::Decryption<__P, __K>> { - Some(>::decryption(self, context.into())) - } - }, - ); - Ok(quote!(#(#impls)* #field #definition_check)) -} -pub(crate) fn struct_literal_path(plaintext: &Type) -> Result { - let Type::Path(type_path) = plaintext else { - return Err(syn::Error::new_spanned( - plaintext, - "field-by-field decryption rebuilds the plaintext as a struct literal, so \ - `plaintext` must name a struct", - )); - }; - if type_path.qself.is_some() { - return Err(syn::Error::new_spanned( - plaintext, - "field-by-field decryption rebuilds the plaintext as a struct literal, so \ - `plaintext` must name a struct directly, not through a qualified path", - )); - } - let mut path = type_path.path.clone(); - for segment in &mut path.segments { - if let PathArguments::AngleBracketed(args) = &mut segment.arguments { - args.colon2_token = Some(Default::default()); - } - } - Ok(path) -} - -// ============================================================================= -// Automatic mode: the type system picks the field -// ============================================================================= - -/// The shape of a record with no `decrypt` attribute, from its `from`s. -enum Auto<'a> { - /// No derived field has a `from`: one of them is the whole plaintext's - /// ciphertext. - Whole(Vec<&'a Field>), - /// Every derived field has a `from`, and each plaintext field is - /// recovered by the one field derived from it: `Record::parse` refuses a - /// second (`one_output_per_plaintext_field`), so there is no choice to - /// make and nothing to group. - ByField(Vec<(&'a Member, &'a Field)>), -} - -impl<'a> Auto<'a> { - fn classify(record: &'a Record) -> Self { - let candidates = record.derived(); - if !record.by_field { - return Auto::Whole(candidates); - } - - Auto::ByField( - candidates - .into_iter() - .filter_map(|field| Some((field.from()?, field))) - .collect(), - ) - } -} - -pub(crate) fn check( - krate: &Path, - name: &Ident, - from: Option<&Member>, - fields: &[&Field], -) -> TokenStream { - let terms = fields.iter().map(|field| { - let ty = &field.ty; - // Spanned at the field type: a type that is not `Decryptable` is - // reported there, not at the derive. - quote_spanned!(ty.span()=> + (<#ty as #krate::target::Decryptable>::DECRYPTABLE as usize)) - }); - // A plaintext field has one output in a `struct` derive, so only the - // whole-value shape can hold several candidates. - let several = match from { - None => { - let several = LitStr::new( - &format!( - "`{name}` has several decryptable fields: mark the one decryption opens \ - `#[stash(decrypt)]`" - ), - Span::call_site(), - ); - quote!(::core::assert!(__decryptable <= 1, #several);) - } - Some(_) => TokenStream::new(), - }; - let none = match from { - None => format!( - "`{name}` has no decryptable field: every derived field is a one-way index term, so \ - there is nothing for DecryptInto to open" - ), - Some(from) => format!( - "no field of `{name}` can recover the plaintext field `{}`: the field derived from \ - it is a one-way index term. To store its ciphertext with the term, make it \ - `Encrypted`", - from.to_token_stream() - ), - }; - let none = LitStr::new(&none, Span::call_site()); - // A `let`, not a nested `const` item: an item could not see the - // record's generics from inside an inline `const`. - quote! { - { - let __decryptable: usize = 0 #(#terms)*; - ::core::assert!(__decryptable >= 1, #none); - #several - } - } -} - -enum Mode<'a> { - /// One field is the whole plaintext's ciphertext: decrypting the record is - /// decrypting that field. - Whole(&'a Field), - /// Each opened field recovers one field of the source, which is rebuilt - /// by name. - ByField(Vec<&'a Field>), -} - -impl<'a> Mode<'a> { - /// Which of the two shapes the `decrypt` fields describe; an error if - /// they describe neither. - fn classify(opened: Vec<&'a Field>, name: &Ident) -> Result { - let by_field = opened.iter().filter(|f| f.from().is_some()).count(); - if by_field == 0 { - if opened.len() == 1 { - return Ok(Mode::Whole(opened[0])); - } - return Err(syn::Error::new_spanned( - name, - "several fields are marked `decrypt` but the record is one value: one plaintext \ - cannot be recovered from two fields. Mark only the ciphertext field, or encrypt \ - a struct field by field with `#[stash(struct = ..)]`.", - )); - } - debug_assert_eq!( - by_field, - opened.len(), - "every field of a `struct` derive has a `from`, and no other field does" - ); - // Two `decrypt` fields cannot recover one plaintext field: a - // `struct` derive has one field per plaintext field - // (`Record::parse`). - Ok(Mode::ByField(opened)) - } -} diff --git a/packages/stack-encrypt-derive/src/encrypt.rs b/packages/stack-encrypt-derive/src/encrypt.rs deleted file mode 100644 index 0766e174a2..0000000000 --- a/packages/stack-encrypt-derive/src/encrypt.rs +++ /dev/null @@ -1,304 +0,0 @@ -//! Emit the record's plan, and an `EncryptFrom` that runs it. The derive -//! names plan verbs only; how fields are zipped, which context each is -//! handed and how the outputs are reassembled is the plan's business, so a -//! derived record and a hand-written plan cannot drift apart. -use crate::shape::{fresh_lifetime, outputs, trait_impl, ByField, Field, Kind, OwnContext, Record}; -use proc_macro2::TokenStream; -use quote::{quote, quote_spanned}; -use syn::spanned::Spanned; -use syn::{parse_quote, DeriveInput, LitStr, Member, Result, Type}; - -pub(crate) fn derive(input: DeriveInput) -> Result { - let record = Record::parse(&input)?; - let fields = record.derived(); - let decryptable = decryptable_impl(&input, &record, &fields); - let emitted = if record.by_field { - struct_derive(&input, &record, &fields) - } else { - value_derive(&input, &record, &fields) - }; - Ok(quote!(#emitted #decryptable)) -} - -/// The struct's own fields, filled from what the plan produced: a derived -/// field from `value(field)`, a `default` field with its default, and a -/// context field from `stored`. -fn assignments<'a>( - record: &'a Record, - value: impl Fn(&Field) -> TokenStream + 'a, - stored: TokenStream, -) -> impl Iterator + 'a { - record.fields.iter().map(move |field| { - let member = &field.member; - let value = match &field.kind { - Kind::Derived { .. } => value(field), - Kind::Default(Some(expr)) => quote!(#expr), - Kind::Default(None) => quote!(::core::default::Default::default()), - Kind::Context => stored.clone(), - Kind::Passthrough { .. } => { - unreachable!("the structural derive refuses a passthrough field") - } - }; - quote!(#member: #value) - }) -} - -/// The plan field name of a `struct` derive's field: the plaintext field it -/// is derived from (`email`, or `0` for a tuple struct). -fn plan_name(from: &Member) -> LitStr { - let name = match from { - Member::Named(ident) => ident.to_string(), - Member::Unnamed(index) => index.index.to_string(), - }; - LitStr::new(&name, from.span()) -} - -/// `#[stash(struct = User, scope = "users")]`: a fields plan under the -/// record's context, one typed field per derived field, each read by a -/// picker and laid out by its own type (`encrypt_into`). -fn struct_derive(input: &DeriveInput, record: &Record, fields: &[&Field]) -> TokenStream { - let krate = &record.krate; - let name = &input.ident; - let source = &record.plaintexts[0]; - let mut prefix = None; - let verbs: Vec = fields - .iter() - .map(|field| { - let ty = &field.ty; - let Kind::Derived { - by_field: - Some(ByField { - from, - context: OwnContext { prefix: own, field: segment }, - .. - }), - } = &field.kind - else { - unreachable!("every field of a `struct` derive has a `from` and the context it infers") - }; - prefix = Some(own.clone()); - let field_name = plan_name(from); - // Pinned only where it differs from the name: the plan keys a - // field under its name by default. - let identity = (segment.value() != field_name.value()) - .then(|| quote_spanned!(ty.span()=> .identity(#segment))); - // Spanned at the field type: what the field's type refuses is - // reported there, not at the derive. - quote_spanned! {ty.span()=> - .encrypt_into::<#ty, _>(#krate::plan::pick(#field_name, |__source: &#source| &__source.#from)) - #identity - } - }) - .collect(); - let prefix = prefix.unwrap_or_else(|| unreachable!("a record has a derived field")); - let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); - let plan = quote! { - #[automatically_derived] - impl #impl_generics #name #ty_generics #where_clause { - /// The plan this record's `EncryptFrom` runs: a fields plan - /// under the record's context, each field laid out by its own - /// type. Emitted by `#[derive(EncryptFrom)]`. - /// - /// # Errors - /// - /// The plan's own build errors, in `Error::Plan`: a field whose - /// type declares one index twice (`PlanError::DuplicateIndex`, - /// `Encrypted<(EqualityTerm, EqualityTerm)>`). The derive sees a - /// field's type, not the indexes it declares, so this fails - /// when the plan is built, not at compile time; every other - /// input whose plan would not build does not compile. - pub fn plan<__K: 'static>() -> ::core::result::Result<#krate::Plan<#source, __K>, #krate::Error> { - #krate::Plan::context(#prefix) - .fields::<#source, __K>() - #(#verbs)* - .build() - } - } - }; - let values = assignments( - record, - |field| { - let from = field.from().unwrap_or_else(|| unreachable!("a `from`")); - let field_name = plan_name(from); - quote!(__values.take(#field_name)?) - }, - quote!(::core::unreachable!()), - ); - let lifetime = fresh_lifetime(&input.generics, "__source"); - let mut generics = input.generics.clone(); - generics - .make_where_clause() - .predicates - .push(parse_quote!(Self: 'static)); - let encrypt_from = trait_impl( - input, - &generics, - quote!(#krate::target::EncryptFrom<#source>), - quote! { - type Context = #krate::target::DeclaredContext; - fn encryption<#lifetime, __K: 'static>() -> #krate::target::Encryption<#lifetime, #source, Self, __K, Self::Context> where #source: #lifetime { - match Self::plan::<__K>() { - ::core::result::Result::Ok(__plan) => __plan - .encryption(::core::option::Option::None) - .try_map(move |mut __values| ::core::result::Result::Ok(Self { #(#values),* })), - ::core::result::Result::Err(__error) => #krate::target::Encryption::failed(__error), - } - } - }, - ); - quote!(#plan #encrypt_from) -} - -/// `#[stash(plaintext = T)]` (or none): a one-value plan with no context -/// of its own, laid out by the tuple of the derived fields' types, run under -/// the context the caller hands over. A record with a `context_field` -/// carries that context out beside the tuple. -fn value_derive(input: &DeriveInput, record: &Record, fields: &[&Field]) -> TokenStream { - let krate = &record.krate; - let name = &input.ident; - let (tuple, pattern) = outputs(fields); - let stored = record.context_field().map(|field| field.ty.clone()); - let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); - let plan = match &stored { - None => quote! { - /// The plan this record's `EncryptFrom` runs: a one-value plan - /// over `__S` with no context of its own, laid out by the tuple - /// of the record's derived field types, so every output shares - /// the caller's context. Emitted by `#[derive(EncryptFrom)]`. - /// - /// # Errors - /// - /// The plan's own build errors, in `Error::Plan`: two outputs - /// that declare the same index (`PlanError::DuplicateIndex`). - /// The derive sees the outputs' types, not the indexes they - /// declare, so this fails when the plan is built, not at - /// compile time. - pub fn plan<__S>() -> ::core::result::Result<#krate::plan::ValuePlan<__S, #krate::plan::Typed<#tuple>>, #krate::Error> - where - #tuple: #krate::target::EncryptFrom<__S>, - { - #krate::Plan::value::<__S>().encrypt_into::<#tuple>().build() - } - }, - Some(stored) => quote! { - /// The plan this record's `EncryptFrom` runs: a one-value plan - /// over `__S` whose context is the caller's, carried out beside - /// the outputs as the record's context field, and laid out by the - /// tuple of the record's derived field types. Emitted by - /// `#[derive(EncryptFrom)]`. - /// - /// # Errors - /// - /// The plan's own build errors, in `Error::Plan`: two outputs - /// that declare the same index (`PlanError::DuplicateIndex`). - /// The derive sees the outputs' types, not the indexes they - /// declare, so this fails when the plan is built, not at - /// compile time. - pub fn plan<__S>() -> ::core::result::Result<#krate::plan::ValuePlan<__S, #krate::plan::Stored<#stored, #tuple>>, #krate::Error> - where - #tuple: #krate::target::EncryptFrom<__S>, - { - #krate::Plan::value::<__S>().context_field::<#stored>().encrypt_into::<#tuple>().build() - } - }, - }; - let plan = quote! { - #[automatically_derived] - impl #impl_generics #name #ty_generics #where_clause { - #plan - } - }; - - let context = record.context_type(false); - let (sources, generic) = record.sources(parse_quote!(__S)); - let lifetime = fresh_lifetime(&input.generics, "__source"); - let values = assignments( - record, - |field| { - let local = &field.local; - quote!(#local) - }, - quote!(__stored), - ) - .collect::>(); - let mut impls = Vec::new(); - for source in sources { - let mut generics = input.generics.clone(); - if generic { - generics.params.push(parse_quote!(__S)); - } - let predicates = &mut generics.make_where_clause().predicates; - predicates.push(parse_quote!(Self: 'static)); - // Per field first, spanned at the field: what a field's type refuses - // is reported there. Then what the plan asks of the tuple. - for field in fields { - predicates.extend(record.field_bounds(field, &source)); - } - let tuple_context: Type = - parse_quote!(<#tuple as #krate::target::EncryptFrom<#source>>::Context); - predicates.push(parse_quote!(#tuple: #krate::target::EncryptFrom<#source>)); - let body = match &stored { - None => { - predicates.push(parse_quote!(#context: Into<#tuple_context>)); - quote! { - __plan - .encryption_with_context::<__K, Self::Context>() - .map(move |#pattern| Self { #(#values),* }) - } - } - Some(_) => { - predicates.push(parse_quote!(#krate::target::CallerContext: Into<#tuple_context>)); - quote! { - __plan - .encryption_with_context::<__K>() - .map(move |(__stored, #pattern)| Self { #(#values),* }) - } - } - }; - impls.push(trait_impl( - input, - &generics, - quote!(#krate::target::EncryptFrom<#source>), - quote! { - type Context = #context; - fn encryption<#lifetime, __K: 'static>() -> #krate::target::Encryption<#lifetime, #source, Self, __K, Self::Context> where #source: #lifetime { - match Self::plan::<#source>() { - ::core::result::Result::Ok(__plan) => #body, - ::core::result::Result::Err(__error) => #krate::target::Encryption::failed(__error), - } - } - fn indexes() -> ::std::vec::Vec<#krate::target::IndexSpec> { - <#tuple as #krate::target::EncryptFrom<#source>>::indexes() - } - }, - )); - } - quote!(#plan #(#impls)*) -} - -pub(crate) fn decryptable_impl( - input: &DeriveInput, - record: &Record, - derived: &[&Field], -) -> TokenStream { - let krate = &record.krate; - let name = &input.ident; - let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); - let value = if record.fields.iter().any(|f| f.decrypt) { - quote!(true) - } else { - let terms = derived.iter().map(|field| { - let ty = &field.ty; - // Spanned at the field type: a type that is not `Decryptable` - // is reported there, not at the derive. - quote_spanned!(ty.span()=> || <#ty as #krate::target::Decryptable>::DECRYPTABLE) - }); - quote!(false #(#terms)*) - }; - quote! { - #[automatically_derived] - impl #impl_generics #krate::target::Decryptable for #name #ty_generics #where_clause { - const DECRYPTABLE: bool = #value; - } - } -} diff --git a/packages/stack-encrypt-derive/src/lib.rs b/packages/stack-encrypt-derive/src/lib.rs index 5a00095275..53142a7fe0 100644 --- a/packages/stack-encrypt-derive/src/lib.rs +++ b/packages/stack-encrypt-derive/src/lib.rs @@ -8,12 +8,6 @@ //! - `Protected::

::decrypt_from(record, &cipher)` decrypts the record into //! a `Protected`, which wipes the plaintext when it is dropped. //! -//! `EncryptFrom

` and `DecryptInto

` are an older pair of derives over a -//! borrowed plaintext. They take the same attributes. Plans and the dynamic -//! engine still use them. The older pair will be removed once plans and the -//! dynamic engine use the new pair (#1488). See -//! [Owned records](#owned-records). -//! //! The generated code describes the encryption. Stack Encrypt runs it. The //! generated code is never given a cipher. //! @@ -159,52 +153,42 @@ //! //! Ciphertext and term operations compose before awaiting, preserving batched //! key requests. Terms alone need only their respective PRF or ordering trait. -//! Query-only targets derive `EncryptFrom` alone. -//! -//! # The record's plan -//! -//! `#[derive(EncryptFrom)]` emits the record's plan, `Record::plan()`, and -//! its `EncryptFrom::encryption()` runs that plan and maps the outputs into -//! the struct. On the encrypt side the derive names plan verbs and nothing -//! beneath them, so a derived record and the same plan written by hand -//! cannot drift apart (see `stack_encrypt::plan`, "The derive emits a plan", -//! for a worked example): -//! -//! - a `struct = User, scope = "users"` record emits -//! `Plan::context("users").fields()` with -//! `.encrypt_into::(pick("field", |u: &User| &u.field))` per -//! derived field, and `.identity("..")` where one is pinned; -//! - a `plaintext = T` record emits -//! `Plan::value::().encrypt_into::<(A, B, ..)>()` over the derived -//! fields' types, with no context of its own (every output shares the -//! caller's), plus `.context_field::()` when a field stores the context. -//! Its `EncryptFrom::indexes()` is the outputs' indexes, so a derived -//! record used as a plan field answers the queries its terms support. -//! -//! A plan seals and opens each field, so every field type of a `struct` -//! record implements `DecryptField` for the plaintext -//! field `F` it is derived from, even when the record derives `EncryptFrom` -//! alone: every target in `stack_encrypt` and every record deriving -//! `DecryptInto` does. That is refused at compile time, at the field: a -//! plaintext field that is borrowed (`&'static str`) cannot be opened into, -//! so derive from an owned field (`String`), or encrypt the borrowed value -//! through a `plaintext` record. -//! -//! Every other input whose plan would not build is refused at compile time -//! too, except one the derive cannot see: an index declared twice. The -//! derive sees field types by name, not the indexes they declare, so a -//! `plaintext` record with two outputs of one index (`EqualityTerm` twice) -//! and a `struct` record whose field type declares one index twice -//! (`Encrypted<(EqualityTerm, EqualityTerm)>`) compile, and `Record::plan()` -//! returns `Error::Plan(PlanError::DuplicateIndex { .. })`; encrypting -//! through the record fails with that error, before any key is requested. -//! -//! On the decrypt side, `DecryptInto` opens a `plaintext = T` record through -//! its one decryptable output (or the one `#[stash(decrypt)]` names) under -//! the record's context, and a `struct` record opens each field through its -//! own `DecryptField` under `/`, extended by the caller's -//! context. Neither goes through the plan: each is written out by the -//! derive, and the crate's tests open what one writes through the other. +//! A record of terms alone, such as a query value, derives `EncryptTarget` +//! alone. +//! +//! # What the derives emit +//! +//! `#[derive(EncryptTarget)]` composes the declarations of the record's +//! fields, from the combinators in `stack_encrypt::target`: +//! +//! - A `struct = User, scope = "users"` record runs each field's own target +//! over the plaintext field it is derived from, under the field's +//! context: `("users", "")`, or `("users", "")` where +//! `identity = ".."` names another segment. +//! - A `plaintext = T` record runs every field's target over the whole +//! value, under the one context the caller supplies. With a +//! `#[stash(context_field)]`, the record stores that context. Its +//! `EncryptTarget::indexes()` is the fields' indexes. +//! +//! `#[derive(DecryptSource)]` opens a `plaintext = T` record through its one +//! decryptable field, or the one `#[stash(decrypt)]` names, under the +//! record's context. It opens each field of a `struct` record through the +//! field type's `DecryptField`, under `/`, extended by the +//! record's scopes and bindings. So every field type of a `struct` record +//! that derives `DecryptSource` implements `DecryptField` +//! for the plaintext field `F` it is derived from. Every target in +//! `stack_encrypt` does, and so does every record that derives +//! `DecryptSource`. A field whose type does not is refused at compile time, +//! at the field. +//! +//! The derive sees field types by name, not the indexes they declare. So +//! one mistake is refused when the record runs, before any key is +//! requested, and not at compile time: two indexes that answer one +//! operator. A `plaintext` record with two fields of one index +//! (`EqualityTerm` twice), and a field whose type declares one index twice +//! (`Encrypted<(EqualityTerm, EqualityTerm)>`), fail with +//! `Error::Plan(PlanError::DuplicateIndex { .. })`. An ORE term beside an +//! OPE term fails with `PlanError::IndexOverlap`. //! //! # A field in your own storage format //! @@ -292,9 +276,7 @@ //! To decrypt the plaintext through the record, the field type also //! implements `DecryptSource` and `DecryptField`, and the record derives //! `DecryptSource`. The crate's `transcode` integration test shows the full -//! set. A field of a `struct = ..` record needs `DecryptField` even -//! when the record derives `EncryptFrom` alone, because the record's plan -//! opens each field (see "The record's plan"). +//! set. //! #![doc = include_str!("../docs/attributes.md")] #![doc(html_favicon_url = "https://cipherstash.com/favicon.ico")] @@ -318,44 +300,13 @@ use proc_macro::TokenStream; use syn::{parse_macro_input, DeriveInput}; mod attrs; -mod decrypt; -mod encrypt; mod owned_decrypt; mod owned_encrypt; mod shape; -/// Derive `EncryptFrom` for a record struct. See the [crate -/// documentation](crate) for what the derive emits; the attributes it accepts -/// are reproduced below. -#[doc = include_str!("../docs/attributes.md")] -#[proc_macro_derive(EncryptFrom, attributes(stash))] -pub fn derive_encrypt_from(input: TokenStream) -> TokenStream { - let input = parse_macro_input!(input as DeriveInput); - encrypt::derive(input) - .unwrap_or_else(syn::Error::into_compile_error) - .into() -} - -/// Derive `DecryptInto

` for a record struct, one impl per -/// `plaintext` type. See the [crate documentation](crate); the attributes it -/// accepts are reproduced below. -#[doc = include_str!("../docs/attributes.md")] -#[proc_macro_derive(DecryptInto, attributes(stash))] -pub fn derive_decrypt_into(input: TokenStream) -> TokenStream { - let input = parse_macro_input!(input as DeriveInput); - decrypt::derive(input) - .unwrap_or_else(syn::Error::into_compile_error) - .into() -} - -/// Derive the owned `EncryptTarget` declaration for a record struct. The -/// derive also implements the structural `EncryptFrom`, so the record works -/// where a structural record is expected. -/// -/// The derive takes the same attributes as `EncryptFrom`. It also takes -/// `#[stash(passthrough)]` on a field of a `struct = ..` record. A -/// passthrough field is not encrypted and not authenticated. See -/// [Owned records](crate#owned-records). +/// Derive the owned `EncryptTarget` declaration for a record struct. See +/// the [crate documentation](crate) for what the derive emits. The attributes +/// it accepts are reproduced below. #[doc = include_str!("../docs/attributes.md")] #[proc_macro_derive(EncryptTarget, attributes(stash))] pub fn derive_encrypt_target(input: TokenStream) -> TokenStream { @@ -367,8 +318,8 @@ pub fn derive_encrypt_target(input: TokenStream) -> TokenStream { /// Derive the owned `DecryptSource<Plaintext>` declaration for a record /// struct, with one impl for each `plaintext` type. The derive also -/// implements the structural `DecryptInto` and `DecryptField`, so the record -/// works where a structural record is expected. +/// implements `DecryptField`, so the record can be a field of another +/// record. /// /// The derive also implements `DecryptSource<Protected<Plaintext>>` for each /// listed plaintext, so `Protected::<User>::decrypt_from(record, &cipher)` diff --git a/packages/stack-encrypt-derive/src/owned_decrypt.rs b/packages/stack-encrypt-derive/src/owned_decrypt.rs index 11a7882c29..768ba5c205 100644 --- a/packages/stack-encrypt-derive/src/owned_decrypt.rs +++ b/packages/stack-encrypt-derive/src/owned_decrypt.rs @@ -1,6 +1,5 @@ -//! Emit an owned `DecryptSource` declaration. Also emit the structural -//! `DecryptInto` and `DecryptField`, so the record works where a structural -//! record is expected. +//! Emit an owned `DecryptSource` declaration, and `DecryptField`, so the +//! record can be a field of another record. //! //! Decryption follows the same layout as the owned encryption: //! @@ -11,16 +10,15 @@ //! - A `plaintext = T` record opens its one recoverable field under the //! context the caller supplies. A `context_field` record opens it under the //! context it stores, once that context has passed the caller's check. -use crate::decrypt::check; use crate::owned_encrypt::{encrypt_input, spanned_predicate, threaded_input}; -use crate::shape::{trait_impl, zip_wiping, Field, Flavor, Record}; +use crate::shape::{check, struct_literal_path, trait_impl, zip_wiping, Field, Record}; use proc_macro2::{Span, TokenStream}; use quote::{quote, quote_spanned}; use syn::spanned::Spanned; use syn::{parse_quote, DeriveInput, Ident, Member, Result, Type}; pub(crate) fn derive(input: DeriveInput) -> Result<TokenStream> { - let record = Record::parse_as(&input, Flavor::Owned)?; + let record = Record::parse(&input)?; let krate = &record.krate; let name = &input.ident; let explicit = record.fields.iter().any(|f| f.decrypt); @@ -100,26 +98,31 @@ pub(crate) fn derive(input: DeriveInput) -> Result<TokenStream> { } }, )); - impls.push(bridge(&input, &record, &plaintext, generic)); if !generic { impls.push(protected(&input, &record, &plaintext)); } } - // `DecryptField`, emitted the same way as the structural derive emits it. + // `DecryptField`: as a field of another record, this record opens + // through its own `DecryptSource`, under the context that record gives + // the field. let mut generics = input.generics.clone(); - generics.params.push(parse_quote!(__P)); + generics + .params + .push(parse_quote!(__P: #krate::target::Zeroize)); generics.params.push(parse_quote!(__Ctx)); let predicates = &mut generics.make_where_clause().predicates; - predicates.push(parse_quote!(Self: #krate::target::DecryptInto<__P>)); - predicates.push(parse_quote!(__Ctx: Into<<Self as #krate::target::DecryptInto<__P>>::Context>)); + predicates.push(parse_quote!(Self: #krate::target::DecryptSource<__P>)); + predicates.push( + parse_quote!(__Ctx: Into<<Self as #krate::target::DecryptSource<__P>>::ContextInput>), + ); let field = trait_impl( &input, &generics, quote!(#krate::target::DecryptField<__P, __Ctx>), quote! { fn decryption_field<__K: 'static>(self, context: __Ctx) -> Option<#krate::target::Decryption<__P, __K>> { - Some(<Self as #krate::target::DecryptInto<__P>>::decryption(self, context.into())) + Some(<Self as #krate::target::DecryptSource<__P>>::decryption(self, context.into())) } }, ); @@ -158,7 +161,7 @@ fn struct_body( explicit: bool, ) -> Result<TokenStream> { let krate = &record.krate; - let literal = crate::decrypt::struct_literal_path(plaintext)?; + let literal = struct_literal_path(plaintext)?; let mut operations = Vec::new(); for (index, (_, fields)) in groups.iter().enumerate() { let operation = if explicit { @@ -301,35 +304,6 @@ fn value_body( quote!(#stored #operation) } -/// The structural `DecryptInto`. The record's structural context is converted -/// into its owned input. -fn bridge(input: &DeriveInput, record: &Record, plaintext: &Type, generic: bool) -> TokenStream { - let krate = &record.krate; - let legacy = record.context_type(true); - let mut generics = input.generics.clone(); - if generic { - generics - .params - .push(parse_quote!(__P: #krate::target::Zeroize + 'static)); - } - let predicates = &mut generics.make_where_clause().predicates; - predicates.push(parse_quote!(Self: #krate::target::DecryptSource<#plaintext>)); - predicates.push(parse_quote!( - #legacy: ::core::convert::Into<<Self as #krate::target::DecryptSource<#plaintext>>::ContextInput> - )); - trait_impl( - input, - &generics, - quote!(#krate::target::DecryptInto<#plaintext>), - quote! { - type Context = #legacy; - fn decryption<__K: 'static>(self, __context: Self::Context) -> #krate::target::Decryption<#plaintext, __K> { - <Self as #krate::target::DecryptSource<#plaintext>>::decryption::<__K>(self, ::core::convert::Into::into(__context)) - } - }, - ) -} - /// `DecryptSource<Protected<P>>`, so a caller can decrypt straight into a /// `Protected` that wipes the plaintext when it is dropped: /// `Protected::<User>::decrypt_from(record, &cipher)`. It takes the same diff --git a/packages/stack-encrypt-derive/src/owned_encrypt.rs b/packages/stack-encrypt-derive/src/owned_encrypt.rs index b91efdc264..1062d9fe51 100644 --- a/packages/stack-encrypt-derive/src/owned_encrypt.rs +++ b/packages/stack-encrypt-derive/src/owned_encrypt.rs @@ -1,6 +1,4 @@ -//! Emit an owned `EncryptTarget` declaration. Also emit the structural -//! `EncryptFrom`. Then the record can be a field of a plan or of a structural -//! record, as well as an owned target. +//! Emit an owned `EncryptTarget` declaration. //! //! The derive composes the declarations of the fields: //! @@ -13,16 +11,19 @@ //! The field target's `ContextInput` decides which context the field can be //! given. So a target that cannot take the record's context is a compile error //! at that field. -use crate::shape::{fresh_lifetime, trait_impl, ByField, Field, Flavor, Kind, OwnContext, Record}; +use crate::shape::{ + decryptable_impl, fresh_lifetime, struct_literal_path, trait_impl, ByField, Field, Kind, + OwnContext, Record, +}; use proc_macro2::{Span, TokenStream}; use quote::{quote, quote_spanned}; use syn::spanned::Spanned; use syn::{parse_quote, DeriveInput, Ident, Result, Type, WherePredicate}; pub(crate) fn derive(input: DeriveInput) -> Result<TokenStream> { - let record = Record::parse_as(&input, Flavor::Owned)?; + let record = Record::parse(&input)?; let derived = record.derived(); - let decryptable = crate::encrypt::decryptable_impl(&input, &record, &derived); + let decryptable = decryptable_impl(&input, &record, &derived); let emitted = if record.by_field { struct_derive(&input, &record) } else { @@ -401,7 +402,7 @@ fn struct_derive(input: &DeriveInput, record: &Record) -> TokenStream { let nested = nested_tuple(&locals); // A pattern writes generic arguments with a turbofish, as a struct // literal does: `Pt::<u64> { .. }`, not `Pt<u64> { .. }`. - let literal_path = match crate::decrypt::struct_literal_path(source) { + let literal_path = match struct_literal_path(source) { Ok(path) => path, Err(error) => return error.to_compile_error(), }; @@ -443,7 +444,7 @@ fn struct_derive(input: &DeriveInput, record: &Record) -> TokenStream { .make_where_clause() .predicates .push(parse_quote!(Self: 'static)); - let encrypt_target = trait_impl( + trait_impl( input, &generics, quote!(#krate::target::EncryptTarget<#source>), @@ -456,9 +457,7 @@ fn struct_derive(input: &DeriveInput, record: &Record) -> TokenStream { #owned_body } }, - ); - let bridge = bridge(input, record, source, record.context_type(false), false); - quote!(#encrypt_target #bridge) + ) } /// The `wipe` function that a `split` is given for the parts still waiting. @@ -588,6 +587,15 @@ fn value_derive(input: &DeriveInput, record: &Record, fields: &[&Field]) -> Toke quote! { type ContextInput = #input_type; fn encryption<#lifetime, __K: 'static>() -> #krate::target::Encryption<#lifetime, #source, Self, __K, Self::ContextInput> where #source: #lifetime { + // The derive sees the fields' types, not the indexes + // they declare. Two fields that declare one index, or + // two indexes that answer one operator, are refused + // here, before any key is requested. + if let ::core::result::Result::Err(__error) = #krate::target::check_indexes( + &<Self as #krate::target::EncryptTarget<#source>>::indexes(), + ) { + return #krate::target::Encryption::failed(__error); + } #(#descriptions)* #body } @@ -598,48 +606,6 @@ fn value_derive(input: &DeriveInput, record: &Record, fields: &[&Field]) -> Toke } }, )); - let legacy = record.context_type(false); - impls.push(bridge(input, record, &source, legacy, generic)); } quote!(#(#impls)*) } - -/// The structural `EncryptFrom`. The record's structural context is converted -/// into its owned input at the root. This impl is emitted beside every owned -/// declaration. So an owned record can be a field of a plan or of a structural -/// record, with no second derive. -fn bridge( - input: &DeriveInput, - record: &Record, - source: &Type, - legacy: Type, - generic: bool, -) -> TokenStream { - let krate = &record.krate; - let lifetime = fresh_lifetime(&input.generics, "__source"); - let mut generics = input.generics.clone(); - if generic { - generics - .params - .push(parse_quote!(__S: #krate::target::Zeroize)); - } - let predicates = &mut generics.make_where_clause().predicates; - predicates.push(parse_quote!(Self: #krate::target::EncryptTarget<#source>)); - predicates.push(parse_quote!( - #legacy: ::core::convert::Into<<Self as #krate::target::EncryptTarget<#source>>::ContextInput> - )); - trait_impl( - input, - &generics, - quote!(#krate::target::EncryptFrom<#source>), - quote! { - type Context = #legacy; - fn encryption<#lifetime, __K: 'static>() -> #krate::target::Encryption<#lifetime, #source, Self, __K, Self::Context> where #source: #lifetime { - <Self as #krate::target::EncryptTarget<#source>>::encryption::<__K>().accepting::<Self::Context>() - } - fn indexes() -> ::std::vec::Vec<#krate::target::IndexSpec> { - <Self as #krate::target::EncryptTarget<#source>>::indexes() - } - }, - ) -} diff --git a/packages/stack-encrypt-derive/src/shape.rs b/packages/stack-encrypt-derive/src/shape.rs index 19f69fc25c..8fb05e2dad 100644 --- a/packages/stack-encrypt-derive/src/shape.rs +++ b/packages/stack-encrypt-derive/src/shape.rs @@ -1,11 +1,11 @@ //! Classification of the derive input into the record it describes. use proc_macro2::{Span, TokenStream}; -use quote::{quote, ToTokens}; +use quote::{quote, quote_spanned, ToTokens}; use syn::spanned::Spanned; use syn::{ parse_quote, Data, DeriveInput, Expr, Fields, Generics, Ident, Lifetime, LitStr, Member, Path, - Result, Type, WherePredicate, + PathArguments, Result, Type, }; use crate::attrs::{ContainerAttrs, FieldAttrs}; @@ -40,7 +40,7 @@ pub(crate) struct Field { /// How a field gets its value when the record is encrypted. #[cfg_attr(test, derive(Debug))] pub(crate) enum Kind { - /// Derived from the source through the field type's own `EncryptFrom`. + /// Derived from the source through the field type's own `EncryptTarget`. Derived { /// With `struct = ..`: the plaintext field this one is derived from /// and the context it is keyed under, which come together. `None` @@ -104,7 +104,7 @@ impl Field { /// `()`, and under it *extended* with the caller's (`(("users", "age"), id)`) /// when the caller passes a `NonEmpty<_>`. Every field of a `plaintext` /// record has none and is handed the caller's context as it is (a - /// one-value plan has one context source), and its type decides what + /// record of one value has one context source), and its type decides what /// that means: a record field composes it with its own contexts; a leaf /// accepts it only as a `NonEmpty<_>`. /// @@ -154,18 +154,6 @@ pub(crate) enum FieldContext<'a> { Caller, } -/// Which pair of declaration traits a derive emits. -#[derive(Clone, Copy, PartialEq, Eq)] -#[cfg_attr(test, derive(Debug))] -pub(crate) enum Flavor { - /// `EncryptFrom` and `DecryptInto`: the structural declarations. They run - /// over a borrowed plaintext. - Structural, - /// `EncryptTarget` and `DecryptSource`: the owned declarations. These also - /// emit the structural pair. - Owned, -} - /// The record a derive input describes. #[cfg_attr(test, derive(Debug))] pub(crate) struct Record { @@ -268,17 +256,13 @@ impl Record { } pub(crate) fn parse(input: &DeriveInput) -> Result<Self> { - Self::parse_as(input, Flavor::Structural) - } - - pub(crate) fn parse_as(input: &DeriveInput, flavor: Flavor) -> Result<Self> { let attrs = ContainerAttrs::parse(&input.attrs)?; let data = match &input.data { Data::Struct(data) => data, Data::Enum(_) => return Err(syn::Error::new_spanned( &input.ident, - "EncryptFrom/DecryptInto cannot be derived for enums: a record is a fixed set of \ + "EncryptTarget/DecryptSource cannot be derived for enums: a record is a fixed set of \ fields derived from one source, and a variant choice has no field to be \ derived into. Model the choice explicitly instead, e.g. as a struct of \ `Option` fields.", @@ -286,7 +270,7 @@ impl Record { Data::Union(_) => { return Err(syn::Error::new_spanned( &input.ident, - "EncryptFrom/DecryptInto cannot be derived for unions", + "EncryptTarget/DecryptSource cannot be derived for unions", )) } }; @@ -294,16 +278,6 @@ impl Record { // `ContainerAttrs::parse` has established that `scope` is present // exactly when `struct` is. let fields = collect(&data.fields, attrs.scope.as_ref())?; - if flavor == Flavor::Structural { - if let Some(field) = fields.iter().find(|f| f.is_passthrough()) { - return Err(syn::Error::new_spanned( - &field.ty, - "`passthrough` is a field of an owned record: derive `EncryptTarget` and \ - `DecryptSource`, which move the plaintext field into the record and back. \ - The structural `EncryptFrom` / `DecryptInto` derives have no form for it", - )); - } - } if fields .iter() .filter(|f| matches!(f.kind, Kind::Context)) @@ -337,19 +311,6 @@ impl Record { )); } - if flavor == Flavor::Structural - && (attrs.caller_scope.is_some() - || attrs.stored_scope.is_some() - || attrs.caller_bind.is_some() - || attrs.stored_bind.is_some()) - { - return Err(syn::Error::new_spanned( - &input.ident, - "`scope(..)` and `bind(..)` are parts of an owned record's context: derive \ - `EncryptTarget` and `DecryptSource`. The structural `EncryptFrom` / \ - `DecryptInto` derives have no form for them", - )); - } let parts = Parts { stored_scope: attrs .stored_scope @@ -380,7 +341,7 @@ impl Record { if record.context_field().is_some() { return Err(syn::Error::new_spanned( context_type, - "`context_field` supplies the complete context, so the record's `Context` is \ + "`context_field` supplies the complete context, so the caller passes a \ `NonEmpty<T>` of that field's type; `context_type` does not apply", )); } @@ -389,7 +350,7 @@ impl Record { } } -/// A `struct` derive's fields, as a plan's: each plaintext field has one +/// A `struct` derive's fields: each plaintext field has one /// output, and each output one segment. A second output from one plaintext /// field (a term beside its ciphertext) is one field of type /// `Encrypted<Terms>` instead; two fields keyed under one segment would @@ -512,8 +473,8 @@ fn collect(fields: &Fields, prefix: Option<&LitStr>) -> Result<Vec<Field>> { )); } } - // An identity is one label segment, as the plan builder's is: - // non-empty, and plain so the descriptor names the field. + // An identity is one label segment: non-empty, and plain so the + // descriptor names the field. if let Some(identity) = &attrs.identity { if !crate::attrs::is_plain_segment(&identity.value()) { return Err(syn::Error::new( @@ -631,7 +592,8 @@ fn collect(fields: &Fields, prefix: Option<&LitStr>) -> Result<Vec<Field>> { } } // Every output of a `plaintext` record takes the - // caller's context: a one-value plan has one source. + // caller's context: a record of one value has one + // source. None => Kind::Derived { by_field: None }, }, } @@ -651,35 +613,6 @@ impl Record { pub(crate) fn context_field(&self) -> Option<&Field> { self.fields.iter().find(|f| matches!(f.kind, Kind::Context)) } - /// Whether the record takes `DeclaredContext`: exactly when it is a - /// `struct` derive, whose fields all carry a context of their own. A - /// `plaintext` record's fields have none. - pub(crate) fn declared_contexts(&self) -> bool { - self.by_field - } - pub(crate) fn context_type(&self, decrypt: bool) -> Type { - let krate = &self.krate; - // On the encrypt side a record whose parts are all stored in the row - // takes nothing from the caller, so it keeps the declared context. - // On the decrypt side the caller may pass a stored part to check it. - if self.parts.any() && (decrypt || self.parts.caller_any()) { - return self.parts_type(decrypt); - } - if let Some(field) = self.context_field() { - let ty = &field.ty; - if decrypt { - parse_quote!(#krate::target::ExpectedContext<#ty>) - } else { - parse_quote!(#krate::NonEmpty<#ty>) - } - } else if self.declared_contexts() { - parse_quote!(#krate::target::DeclaredContext) - } else if let Some(context_type) = &self.context_type { - context_type.clone() - } else { - parse_quote!(#krate::target::CallerContext) - } - } /// The `Parts<S, B>` a record with `scope(..)` or `bind(..)` takes from /// its caller. On the encrypt side a stored part is read from the /// plaintext, so its slot is empty. On the decrypt side a stored part @@ -705,58 +638,6 @@ impl Record { parse_quote!(#krate::target::Parts<#scope, #bind>) } - /// The context a field is handed on the decrypt side: the caller's as it - /// is, or — for a field with a context of its own, which only a `struct` - /// derive has — the `CallerContext` that `context_expr` builds from the - /// record's `DeclaredContext` with `under`. - pub(crate) fn field_context_type(&self, field: &Field) -> Type { - let krate = &self.krate; - match field.field_context() { - FieldContext::Own(_) => parse_quote!(#krate::target::CallerContext), - FieldContext::Caller => self.context_type(false), - } - } - /// The context type the record's declaration tree carries on the - /// encrypt side: a `DeclaredContext` when every field has a - /// context of its own, so the caller's is optional; otherwise the - /// caller's — the `context_type` named, or `CallerContext`. It is the - /// record's own `Context` except for a record that stores its context, - /// which declares the `NonEmpty<T>` it stores and converts it into this - /// once, at the root. - pub(crate) fn threaded_context(&self) -> Type { - let krate = &self.krate; - if self.context_field().is_some() { - parse_quote!(#krate::target::CallerContext) - } else { - self.context_type(false) - } - } - - /// What a `plaintext` record asks of a field's type, as the impl's - /// where-clause: that it is a target of `source`, and that the context - /// the record takes converts into the one it declares. Stated per field, - /// beside what the plan asks of the tuple of them, so what a field's - /// type refuses is reported at that field. - /// Only for a field whose source the derive can name — a `from` field's - /// obligation is checked in the body, against a plaintext field's type - /// the derive cannot name. - pub(crate) fn field_bounds(&self, field: &Field, source: &Type) -> [WherePredicate; 2] { - let krate = &self.krate; - let ty = &field.ty; - let context = quote!(<#ty as #krate::target::EncryptFrom<#source>>::Context); - let threading = match field.field_context() { - FieldContext::Own(_) => parse_quote!(#context: From<#krate::target::CallerContext>), - FieldContext::Caller => { - let threaded = self.threaded_context(); - parse_quote!(#threaded: Into<#context>) - } - }; - [ - parse_quote!(#ty: #krate::target::EncryptFrom<#source>), - threading, - ] - } - /// The context a field is opened under, from the record's `__context`, /// on the decrypt side: the caller's as it is, or the field's own /// extended by it. @@ -778,47 +659,6 @@ impl Record { } } } -/// The outputs of a `plaintext` record's derived `fields`, as the plan's -/// one-value target sees them: the tuple of their types (the type itself -/// for one field), and the pattern binding each to its local. A tuple of -/// targets is a target for up to five elements; a record of more than four -/// outputs nests the rest in its fourth: `(A, B, C, (D, E, ..))`. -pub(crate) fn outputs(fields: &[&Field]) -> (TokenStream, TokenStream) { - match fields { - [field] => { - let ty = &field.ty; - let local = &field.local; - (quote!(#ty), quote!(#local)) - } - _ => { - let (flat, rest) = if fields.len() <= 4 { - (fields, None) - } else { - (&fields[..3], Some(outputs(&fields[3..]))) - }; - let mut types: Vec<TokenStream> = flat - .iter() - .map(|field| { - let ty = &field.ty; - quote!(#ty) - }) - .collect(); - let mut patterns: Vec<TokenStream> = flat - .iter() - .map(|field| { - let local = &field.local; - quote!(#local) - }) - .collect(); - if let Some((ty, pattern)) = rest { - types.push(ty); - patterns.push(pattern); - } - (quote!((#(#types),*)), quote!((#(#patterns),*))) - } - } -} - /// The chain zipping `operations` into one description, and the nested /// tuple pattern that binds each operation's output to its local in the /// closure that maps the chain's output. @@ -838,15 +678,9 @@ fn zip_chain(operations: Vec<(TokenStream, Ident)>, method: &str) -> (TokenStrea (chain, pattern) } -/// The zipped `operations`, mapped to `result`. -pub(crate) fn zip(operations: Vec<(TokenStream, Ident)>, result: TokenStream) -> TokenStream { - let (chain, pattern) = zip_chain(operations, "zip"); - quote!(#chain.map(move |#pattern| #result)) -} - -/// [`zip`] over recovered plaintext. Each field opened before a later field -/// fails is wiped, not dropped as it is (`Decryption::zip_wiping`). An owned -/// record's fields open through this. +/// The zipped `operations` over recovered plaintext, mapped to `result`. +/// Each field opened before a later field fails is wiped, not dropped as it +/// is (`Decryption::zip_wiping`). A record's fields open through this. pub(crate) fn zip_wiping( operations: Vec<(TokenStream, Ident)>, result: TokenStream, @@ -855,6 +689,115 @@ pub(crate) fn zip_wiping( quote!(#chain.map(move |#pattern| #result)) } +/// `Decryptable` for the record: whether one of its derived fields holds +/// recoverable ciphertext. A record that marks a field `decrypt` is +/// decryptable. +pub(crate) fn decryptable_impl( + input: &DeriveInput, + record: &Record, + derived: &[&Field], +) -> TokenStream { + let krate = &record.krate; + let name = &input.ident; + let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); + let value = if record.fields.iter().any(|f| f.decrypt) { + quote!(true) + } else { + let terms = derived.iter().map(|field| { + let ty = &field.ty; + // Spanned at the field type: a type that is not `Decryptable` + // is reported there, not at the derive. + quote_spanned!(ty.span()=> || <#ty as #krate::target::Decryptable>::DECRYPTABLE) + }); + quote!(false #(#terms)*) + }; + quote! { + #[automatically_derived] + impl #impl_generics #krate::target::Decryptable for #name #ty_generics #where_clause { + const DECRYPTABLE: bool = #value; + } + } +} + +/// The plaintext struct's path as a struct literal and a pattern write it, +/// with a turbofish on its generic arguments. +pub(crate) fn struct_literal_path(plaintext: &Type) -> Result<Path> { + let Type::Path(type_path) = plaintext else { + return Err(syn::Error::new_spanned( + plaintext, + "field-by-field decryption rebuilds the plaintext as a struct literal, so \ + `plaintext` must name a struct", + )); + }; + if type_path.qself.is_some() { + return Err(syn::Error::new_spanned( + plaintext, + "field-by-field decryption rebuilds the plaintext as a struct literal, so \ + `plaintext` must name a struct directly, not through a qualified path", + )); + } + let mut path = type_path.path.clone(); + for segment in &mut path.segments { + if let PathArguments::AngleBracketed(args) = &mut segment.arguments { + args.colon2_token = Some(Default::default()); + } + } + Ok(path) +} + +/// The compile-time check that a record, or one plaintext field of it, has +/// exactly one field that decryption opens. +pub(crate) fn check( + krate: &Path, + name: &Ident, + from: Option<&Member>, + fields: &[&Field], +) -> TokenStream { + let terms = fields.iter().map(|field| { + let ty = &field.ty; + // Spanned at the field type: a type that is not `Decryptable` is + // reported there, not at the derive. + quote_spanned!(ty.span()=> + (<#ty as #krate::target::Decryptable>::DECRYPTABLE as usize)) + }); + // A plaintext field has one output in a `struct` derive, so only the + // whole-value shape can hold several candidates. + let several = match from { + None => { + let several = LitStr::new( + &format!( + "`{name}` has several decryptable fields: mark the one decryption opens \ + `#[stash(decrypt)]`" + ), + Span::call_site(), + ); + quote!(::core::assert!(__decryptable <= 1, #several);) + } + Some(_) => TokenStream::new(), + }; + let none = match from { + None => format!( + "`{name}` has no decryptable field: every derived field is a one-way index term, so \ + there is nothing for DecryptSource to open" + ), + Some(from) => format!( + "no field of `{name}` can recover the plaintext field `{}`: the field derived from \ + it is a one-way index term. To store its ciphertext with the term, make it \ + `Encrypted<Terms>`", + from.to_token_stream() + ), + }; + let none = LitStr::new(&none, Span::call_site()); + // A `let`, not a nested `const` item: an item could not see the + // record's generics from inside an inline `const`. + quote! { + { + let __decryptable: usize = 0 #(#terms)*; + ::core::assert!(__decryptable >= 1, #none); + #several + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -1035,10 +978,11 @@ mod tests { #[test] fn a_field_context_is_refused_on_every_derive_form() { - // A one-value plan has one context source, the caller's, and a - // fields plan's fields all sit under the plan's: a field sealed under - // a literal of its own has no plan form, whatever the literal, so the - // attribute itself is the mistake. Even an empty or non-plain one. + // A record of one value has one context source, the caller's, and + // a `struct` record's fields all sit under the record's scope. So a + // field cannot be sealed under a literal of its own, whatever the + // literal: the attribute itself is the mistake. Even an empty or + // non-plain one. for context in ["nickname", "users/nickname", ""] { let inputs: [DeriveInput; 3] = [ parse_quote! { @@ -1083,7 +1027,7 @@ mod tests { assert!(message.contains("re-encrypt that data"), "{message}"); assert!(message.contains("identity = \"..\""), "{message}"); assert!( - message.contains("a fields plan that picks the same source twice"), + message.contains("encrypt it twice, once under each context"), "{message}" ); } @@ -1549,17 +1493,17 @@ mod tests { } }) .unwrap(); - let ty = |ty: &Type| quote!(#ty).to_string(); - assert_eq!(ty(&record.context_type(false)), "AeadContext"); - assert_eq!(ty(&record.context_type(true)), "AeadContext"); - // Both fields are handed the caller's type as it is. + // The type as text, without the spaces `quote` puts between tokens. + let ty = |ty: &Type| quote!(#ty).to_string().replace(' ', ""); + // The record requires the caller's type, and every field is handed + // that type as it is. assert_eq!( - ty(&record.field_context_type(&record.fields[0])), - "AeadContext" + ty(&crate::owned_encrypt::encrypt_input(&record)), + "::stack_encrypt::target::RequiredContext<AeadContext>" ); assert_eq!( - ty(&record.field_context_type(&record.fields[1])), - "AeadContext" + ty(&crate::owned_encrypt::threaded_input(&record)), + "::stack_encrypt::target::RequiredContext<AeadContext>" ); let record = parse(parse_quote! { @@ -1570,8 +1514,8 @@ mod tests { .unwrap(); assert!(record.context_type.is_none()); assert_eq!( - ty(&record.context_type(false)), - ":: stack_encrypt :: target :: CallerContext" + ty(&crate::owned_encrypt::encrypt_input(&record)), + "::stack_encrypt::target::RequiredContext<::stack_encrypt::target::CallerContext>" ); } @@ -1635,8 +1579,8 @@ mod tests { assert_eq!(record.fields.len(), 3); // Nothing is derived from a field of the plaintext. assert!(record.fields.iter().all(|f| f.from().is_none())); - // Every derived field takes the caller's context: a one-value plan - // has one source. + // Every derived field takes the caller's context: a record of one + // value has one source. assert!(record.fields[0].decrypt); assert!(record.fields[1].is_derived()); for field in &record.fields[..2] { diff --git a/packages/stack-encrypt/CHANGELOG.md b/packages/stack-encrypt/CHANGELOG.md index d9608b42f5..2a260bdc82 100644 --- a/packages/stack-encrypt/CHANGELOG.md +++ b/packages/stack-encrypt/CHANGELOG.md @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- **Two more records derive** (#1661). The owned derives did not accept + these shapes, and the removed derives did: + - A `plaintext = T` record can have a `struct = ..` record as a field. + The caller's context extends each of the inner record's fields. + - A record with `context_type = NonEmpty<T>` can have a ciphertext or a + term as a field. - **The JSON targets implement the owned traits** (#1660). The plaintext is `json::JsonPlaintext`, which holds a `serde_json::Value` and wipes it when it is dropped. @@ -22,14 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `DecryptSource<JsonPlaintext>`. - `JsonSubtree`, the rows of a read, implements `DecryptSource` for `JsonPlaintext`, `Option<JsonPlaintext>` and `Vec<JsonPlaintext>`. - - The context is required in each case. The answers and the refusals - are those of the structural traits (`EncryptFrom<Value>` and - `DecryptInto<Value>`), which stay for now. The stored bytes do not - change: each API opens what the other sealed. - - These three types now have a `decryption` method from two traits. Code - that imports both `DecryptInto` and `DecryptSource` and calls - `.decryption(..)` on one of them must name the trait: - `DecryptInto::decryption(document, context)`. + - The context is required in each case. The stored bytes do not + change. - **A field type that stores its context can be a record field** (#1658). A derived `struct = ..` record gives each field a context of its own: @@ -131,10 +131,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 refused, or dropped. `assert_context!` takes a marker, and `Marker::contexts()` is the record's contexts narrowed to the field. A tuple field has no marker. -- **Tuples of terms answer the query chain too** (#1596). A query of - `op::Eq` or `op::Matches` on a plan of - `Encrypted<(EqualityTerm, MatchTerms)>` needs no `.via(index)`. The - query uses the one term that answers the operator. `OreBlockTerm` implements `LabelledTarget`, so a block ORE field +- **Tuples of terms answer a query too** (#1596). The marker of a field + typed `Encrypted<(EqualityTerm, MatchTerms)>` answers `eq` and + `matches`. The query uses the one term that answers the operator. `OreBlockTerm` implements `LabelledTarget`, so a block ORE field is queried under its label. - **`dynamic::Planned`, a value under a plan that is data** (#1486). A record under a `Plan`, or a query value under a `QueryPlan`, each a @@ -165,6 +164,75 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Breaking +- **The structural API is removed** (#1661, #1662). Stack Encrypt had two + ways to encrypt a record and two ways to query one. The owned API stays: + `EncryptTarget`, `DecryptSource`, `EncryptInto`, `DecryptFrom`, field + markers, and `dynamic::Planned` for a plan that is data. These are gone: + - The traits `EncryptFrom` and `DecryptInto`, the derives + `#[derive(EncryptFrom)]` and `#[derive(DecryptInto)]`, and the calls + `encrypt_as` and `decrypt_as`. + - The plan builder and the chain: `Plan`, `Plan::context`, `Plan::fields`, + `Plan::value`, `build()`, `plan::pick`, `FieldRef`, `FieldsBuilder`, + `FieldValues`, `Fields`, `Field`, `FieldSchema`, `ValuePlan`, `Typed`, + `Stored`, `Laid`, `Runs`, `Opens`, `cipher.encrypt(&value)` with + `.context`, `.with`, `.fields`, `.using`, `.extend` and `.keyset`, + `cipher.query(op, value)` with `.on` and `.via`, `cipher.open(record)`, + and `plan::all`. The `plan` module keeps `PlanError` and `IntoLabel`. + - `target::Layout`, `target::JoinContext`, and tuples of targets such as + `(StackCipherText, EqualityTerm)`. + - `op::DefaultIndex`. Only the chain read it. A field's target type + names the index that answers an operator (`op::Answers`), and a plan + that is data reads `dynamic::Op::answered_by`. + - The JSON targets no longer open to a `serde_json::Value`. They open to + a `json::JsonPlaintext`, which is wiped when it is dropped. + + To upgrade: + + | Removed | Replacement | + | --- | --- | + | `keyset.encrypt_as(&value, context)` | `value.encrypt_into_with_context(&keyset, context)`. The plaintext is passed by value and must be `Zeroize`, so a `&str` becomes a `String`. | + | `cipher.decrypt_as(stored, context)` | `P::decrypt_from_with_context(stored, &cipher, context)` | + | `#[derive(EncryptFrom, DecryptInto)]` | `#[derive(EncryptTarget, DecryptSource)]`, with the same `#[stash(..)]` attributes. The plaintext type derives `Zeroize`. A `struct = ..` record seals every plaintext field or marks it `#[stash(passthrough)]`. | + | `cipher.encrypt(&value).context("a/b")` | `keyset.encrypt_tree(&value, &Label::parse("a/b")?)` | + | `cipher.encrypt(&value).context(c).with((Equality, Ore))` | `value.encrypt_into_with_context::<Encrypted<(EqualityTerm, OreTerm<u32>)>, _>(&keyset, c)` | + | `Plan::context("users").fields()..build()` and `.using(&plan)` | A derived record, `#[stash(struct = User, scope = "users")]`. A program whose declaration is data parses it with `dynamic::record::plan` and runs `dynamic::Planned::new(&plan, value)`. | + | `cipher.query(op::Eq, value).on(&plan)` | The field's marker: `EncryptedUser::EMAIL.eq(value).encrypt(&keyset)` | + | `cipher.open(record).using(&plan)` | `P::decrypt_from(record, &keyset)` | + | `plan::all((a, b))` | `a.zip(b)` on two `Pending` values, or `Pending::all` | + | A hand-written `impl EncryptFrom<S> for T` | `impl EncryptTarget<S> for T` with `type ContextInput = RequiredContext<CallerContext>`, and `DecryptSource` in place of `DecryptInto` | + + What has no replacement yet: + - **A typed JSON query.** `cipher.query(op::Contains, ..)` and + `op::AtPath(..)` on a plan were the only typed way to query a JSON + document. Until the document's field marker has methods for them, a + JSON query goes through a plan that is data (`dynamic::query_term`, or + `Planned` with a query plan). + - **Naming another index for a query** (`.via(index)`). A marker + answers through the index its field's type declares. + - **A keyset chosen by name on a call** (`.keyset("tenant")`). Load the + keyset first, with `cipher.keyset(..)`. + + Notes for a hand-written target and for two kinds of record: + - `DecryptField` and `Decryptable` stay. A hand-written target that is a + record field implements `DecryptField` through `DecryptSource`. + - A record that is generic over a lifetime needs `'a: 'static`, because + `EncryptTarget` requires `Self: 'static`. + - A field whose type is a derived `plaintext = T` record has no marker + method, because the derive writes no `op::Answers` for the record. + Implement `op::Answers` and `op::LabelledTarget` for the record by + hand to query it. + +- **A set of terms holds one index for each operator.** + `Encrypted<Terms>` and a derived `plaintext = T` record now refuse a set + that names one index twice, such as `Encrypted<(EqualityTerm, + EqualityTerm)>`, or two indexes that answer one operator, such as + `Encrypted<(OreTerm<u32>, OpeTerm<u32>)>`. The refusal is + `PlanError::DuplicateIndex` or `PlanError::IndexOverlap`, when the value + is encrypted and before any key is requested. The plan builder refused + these sets, a plan that is data still does, and a marker has no method + for an operator that two indexes answer. Used directly as a target, + such a set was accepted before. + - **A data plan field that names an EQL type takes a scope and a binding** (#1658). `dynamic::TargetResolver` now receives them: `encrypt` and `decrypt` take `parts: &DeclaredContext` after the label, @@ -190,38 +258,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **`target::legacy` is removed** (#1488). It held two traits, implemented for every type, that gave the structural API a second - call syntax: `value.encrypt_into(&keyset)`, - `value.encrypt_into_with_context(&keyset, context)`, - `Target::encrypt_from(&value, &keyset, context)`, - `stored.decrypt_into(&cipher, context)`, `P::decrypt_from(stored, &cipher)` - and `P::decrypt_from_with_context(stored, &cipher, context)`. Their - names were the same as the owned traits' `EncryptInto` and - `DecryptFrom`, but they borrowed the plaintext. - - To upgrade a type that implements `EncryptTarget` or - `DecryptSource`, use the owned traits. They take the plaintext by - value. - - To upgrade a type that implements only `EncryptFrom` or - `DecryptInto`, call the cipher. The context is `T::Context`, so - convert it with `into()`: - - | Removed call | Replacement | - | --- | --- | - | `value.encrypt_into(&keyset)` | `keyset.encrypt_as(&value, Default::default())` | - | `value.encrypt_into_with_context(&keyset, context)` | `keyset.encrypt_as(&value, context.into())` | - | `Target::encrypt_from(&value, &keyset, context)` | `keyset.encrypt_as::<_, Target>(&value, context.into())` | - | `stored.decrypt_into(&cipher, context)` | `cipher.decrypt_as(stored, context.into())` | - | `P::decrypt_from(stored, &cipher)` | `cipher.decrypt_as::<P, _>(stored, Default::default())` | - | `P::decrypt_from_with_context(stored, &cipher, context)` | `cipher.decrypt_as::<P, _>(stored, context.into())` | - - `encrypt_as` does not name the target, so name it with a type - annotation on the result or with the turbofish shown. - - If `value` is already a reference, pass it as it is: - `keyset.encrypt_as(value, ..)`. The removed methods took the value - through method-call autoref, so a `&String` receiver encrypted a - `String`. `encrypt_as(&value, ..)` on a `&String` encrypts a - `&String` instead. Into a derived record that is a compile error. - Into `StackCipherText` it compiles, because that target accepts any - `Encrypt` source. + call syntax. Their names were the same as the owned traits' + `EncryptInto` and `DecryptFrom`, but they borrowed the plaintext. Use + the owned traits, which take the plaintext by value. The entry "The + structural API is removed", above, has the upgrade table. - **`target::ExpectedContext` is no longer part of the API** (#1488). It is the check that a `#[stash(context_field)]` record runs on decrypt: @@ -234,12 +274,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - To upgrade, do not name the type. For a record whose context field has type `C` and whose plaintext has type `P`, pass the `NonEmpty<C>` you expect, or `()` for no check, and let `Into` - convert it: `cipher.decrypt_as(record, expected.into())`, or - `P::decrypt_from_with_context(record, &cipher, expected)`. For no - check, `P::decrypt_from(record, &cipher)` also works. - - `ValuePlan::decryption_with_context` takes an `Option<NonEmpty<C>>` - in place of an `ExpectedContext<C>`. `None` checks only that the - stored context is not empty. + convert it: `P::decrypt_from_with_context(record, &cipher, expected)`. + For no check, `P::decrypt_from(record, &cipher)` also works. - **`CipherRef` replaces `target::CipherScope` and `dynamic::Scope`** (#1488). A `CipherRef` is one borrowed value: a connection, and the @@ -248,8 +284,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Every entry point that took `impl CipherScope` takes `impl Into<CipherRef>`. A call that passes `&cipher` or `&keyset` compiles unchanged. These are `DecryptFrom::decrypt_from` and - `decrypt_from_with_context`, the legacy `decrypt_into`, `decrypt_from` - and `decrypt_from_with_context`, and `Pending::ready`, `failed`, + `decrypt_from_with_context`, and `Pending::ready`, `failed`, `request` and `all`. - A function of your own that took `impl CipherScope<'a, K>` takes `impl Into<CipherRef<'a, K>>` instead. @@ -283,7 +318,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 the three. `Operator::OP` and `Leaf::AT` are new, with `None` defaults, so an operator outside this crate needs nothing more. - What a query is refused for is written once, in the `sem` module, for - the typed chain and the dynamic path alike. So the typed chain now + a typed query and the dynamic path alike. So a typed query now refuses an equality or order query of text with no normal form, and block ORE text over its limit, before the keyset is resolved (#1321); the dynamic path already did. `Supports<Eq, S> for Equality` @@ -300,11 +335,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 as `PlanError::IndexOverlap` on the typed path and `overlapping_outputs` on the dynamic one, before any key is requested; two match indexes were already one index twice (`DuplicateIndex`). `(Equality, Ore)` answer - different operators and are allowed. The rule is a declaration's. A - record derived with `EncryptFrom` builds a plan, so a field of - `(OreTerm, OpeTerm)` on it is refused the same way. `indexed(..)` and a - hand-written `EncryptFrom` with two order indexes still run, since no - plan holds them and no query reads the second term. Each index declares the operators it answers once (`Equality::OPERATORS`, + different operators and are allowed. The rule is a declaration's. + `Encrypted<(OreTerm, OpeTerm)>` as a target, and a derived + `plaintext = T` record with those two fields, are refused the same way + (see "A set of terms holds one index for each operator", above). The + combinator `indexed(..)` composed by hand with two order indexes still + runs, since no query reads the second term. Each index declares the operators it answers once (`Equality::OPERATORS`, `Ore::OPERATORS`, …; `IndexSpec::operators` and `IndexSpec::answers` read them), and `Op::answered_by` has no priority between order indexes. A field that stored both order terms wrote the OPE one for nothing, and @@ -360,22 +396,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 bound. The change lets `OpaqueBlob<T>` implement `OpaqueDecode`: a blob opens as a bytes leaf and is decoded afterwards, so it cannot implement `Decrypt`. -- **The record attribute `context = ".."` is now `scope = ".."`**, on the - `EncryptFrom` and `DecryptInto` derives as on the owned ones. The value is +- **The record attribute `context = ".."` is now `scope = ".."`**. The + value is the record's name, the first scope of each field's context, and the stored data does not change. Write `#[stash(struct = User, scope = "users")]`. The old spelling is a compile error that names the new one. - The plan builder's `Plan::context(..)` and the Go `context=` tag are not - renamed yet. -- **The structural `Vec<T>: DecryptInto<Vec<P>>` and `DecryptField<Vec<P>, _>` - impls require `P: Zeroize`** (#1483). These impls used to decrypt through - `Decryption::all`. When an element failed late, the elements recovered - before it were dropped without being wiped. The impls now decrypt through - the same wiping path as the owned `Vec<T>: DecryptSource<Vec<P>>`, so - those elements are wiped before the error is returned. - - If a structural record decrypts a column into a struct plaintext, derive - `Zeroize` on that struct. - - Every scalar the engine decrypts into already implements `Zeroize`. + The Go `context=` tag is not renamed yet. - Repeated plan-chain `.extend(..)` calls now append parts in order, matching Go's `Cipher.Extend`, instead of replacing earlier extensions. `.extend(())` adds nothing, and grouped `NonEmpty` parts retain their grouping. @@ -463,8 +489,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 data. A database NULL is not a sealed null. A sealed null is a stored value that - means "null". An EQL type encrypted from an `Option<String>` writes one. The - structural `EncryptFrom` and `DecryptInto` already supported `Option<T>`. + means "null". An EQL type encrypted from an `Option<String>` writes one. - **Owned records: `#[derive(EncryptTarget, DecryptSource)]`** (#1483). A struct now encrypts field by field into a record type that declares its own layout. The crate docs' "Records" section and the derive crate's @@ -509,10 +534,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `User`, which the caller must wipe. - If a later field is refused during decryption, the fields already decrypted are wiped. - - Both derives also implement the structural `EncryptFrom`, - `DecryptInto`, and `DecryptField`. So an owned record can be a field of a - `#[derive(EncryptFrom)]` record, or a typed plan field, with no second - derive. Derive one pair on a record, not both. + - `#[derive(DecryptSource)]` also implements `DecryptField`, so a + record can be a field of another record. - **A `Ciphertext` decrypts into a `Protected`.** `OpaqueDecode` now covers every `Decrypt + Zeroize` type, not only the `FromScalar` ones. So `keyset.decrypt::<Protected<String>>(ct)` and @@ -566,9 +589,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 contexts, `DeclaredContext` and `CallerContext`. So do the `RequiredContext` wrappers of the core contexts. This is how a record field is given its context. -- **`FieldsBuilder::target::<T, _>()`** adds a plan field laid out by an - owned target that has no structural declaration, such as a `Ciphertext`. - It lowers to `<T as EncryptTarget<F>>::encryption().accepting::<CallerContext>()`. - **Owned collections: `EncryptInto` encrypts a `Vec<S>` into a `Vec<T>`, and `DecryptFrom` decrypts it back** (#1482). `Vec<T>` implements `EncryptTarget<Vec<S>>` and `DecryptSource<Vec<P>>` for any owned element @@ -728,18 +748,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 rule itself. It no longer names design records or plans, since those are not published with the crates. -### Deprecated - -- **The structural derives `#[derive(EncryptFrom, DecryptInto)]` and the - `target::legacy` conversion traits.** New records derive `EncryptTarget` - and `DecryptSource`. These derives also implement the structural traits. The - structural API stays for now, because the plans and the dynamic engine - run it. It is where a migration starts, not where it ends. - - The structural API will be removed once every consumer has an owned - equivalent. That removal is tracked in #1488. - - No `#[deprecated]` attribute is applied yet, because the crate's own - plan and dynamic modules still implement these traits. - ### Fixed - **A derived record wipes an owned plaintext when a caller's part is @@ -755,8 +763,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 (#1504). `target::ciphertext()` cloned a borrowed plaintext before it checked the context. When the context was too long for a ZeroKMS descriptor, the clone was dropped without being wiped. This affected - `encrypt_as`, plans, and every owned target with a `StackCipherText` or - `Encrypted<Terms>` field. + every target with a `StackCipherText` or `Encrypted<Terms>` field. - `ciphertext()` now checks the context before it takes the plaintext. So a refused context makes no copy. - In `Owned` mode, a refused context still drops the value as it is, diff --git a/packages/stack-encrypt/CONTEXT.md b/packages/stack-encrypt/CONTEXT.md index ba9b2844b1..ec56845116 100644 --- a/packages/stack-encrypt/CONTEXT.md +++ b/packages/stack-encrypt/CONTEXT.md @@ -21,7 +21,7 @@ _Avoid_: raw path, low-level path Encryption driven by the output type: the type being produced (a ciphertext, a term, a record) declares what it is derived from, which operations produce it, and which context it demands; execution belongs to the cipher. Rust-only: a -hand-written `EncryptFrom` type, the combinators, and the derive. With +hand-written `EncryptTarget` type, the combinators, and the derive. With cipher-directed, the preferred entry point for a native Rust caller. A binding reaches indexes and field-by-field encryption only through a plan lowered from data, which runs the same engine. @@ -68,7 +68,7 @@ mapping, config, target (a plan may name a target type for a field) **Target**: A type that decides a field's layout, and so the queries it answers, through -its own `EncryptFrom`: an EQL type, `Encrypted<Terms>`, or a type of the +its own `EncryptTarget`: an EQL type, `Encrypted<Terms>`, or a type of the consumer's. A plan field is either plain verbs (encrypt, index, passthrough) or one target, never both. Rust-only, since it names a Rust type; a binding's counterpart is generated code (in Go, the generator's output), which diff --git a/packages/stack-encrypt/Cargo.toml b/packages/stack-encrypt/Cargo.toml index 1bdcd9c793..73f06a3d4d 100644 --- a/packages/stack-encrypt/Cargo.toml +++ b/packages/stack-encrypt/Cargo.toml @@ -21,7 +21,7 @@ stack-kms = { path = "../stack-kms", version = "0.2.0", default-features = false # `StackCipher::new()` builds a ZeroKMS client from the environment, so its # return type names the auto-detected auth strategy. Only needed with `http`. stack-auth = { workspace = true, optional = true } -# `#[derive(EncryptFrom)]` / `#[derive(DecryptInto)]`, re-exported from `target`. +# `#[derive(EncryptTarget)]` / `#[derive(DecryptSource)]`, re-exported from `target`. stack-encrypt-derive = { path = "../stack-encrypt-derive", version = "0.3.0" } # The workspace vitaminc: one canonical context encoding shared by diff --git a/packages/stack-encrypt/docs/adr/0012-owned-record-declarations.md b/packages/stack-encrypt/docs/adr/0012-owned-record-declarations.md index 10ba9726b8..d04fcabcf2 100644 --- a/packages/stack-encrypt/docs/adr/0012-owned-record-declarations.md +++ b/packages/stack-encrypt/docs/adr/0012-owned-record-declarations.md @@ -199,10 +199,13 @@ All three give the same descriptors and the same search term bytes, and each decrypts the records of the other two. The plan removes the structural traits and the `encrypt_as` and -`decrypt_as` calls. They stay until plans, EQL, Go, and the guest have moved -to the owned API. The helpers in `target::legacy` were removed first. They -were only a second way to call `encrypt_as` and `decrypt_as`, so their -callers now call those methods. +`decrypt_as` calls. The helpers in `target::legacy` were removed first. They +were only a second way to call `encrypt_as` and `decrypt_as`. The traits +`EncryptFrom` and `DecryptInto`, their derives, `encrypt_as` and +`decrypt_as` were removed next, with the plan chain and builder, once every +target had the owned traits. The owned derives no longer emit the +structural traits. `DecryptField`, which lets a record be a field of +another record, stays, and is implemented through `DecryptSource`. ### 6. The structural `Vec` decrypts through the wiping path diff --git a/packages/stack-encrypt/fuzz/Cargo.toml b/packages/stack-encrypt/fuzz/Cargo.toml index dfbe55b4a8..0f5c38ed95 100644 --- a/packages/stack-encrypt/fuzz/Cargo.toml +++ b/packages/stack-encrypt/fuzz/Cargo.toml @@ -21,8 +21,8 @@ vitaminc-protected = "0.9.1" # The sinks the record harness writes its plans and passthroughs through. vitaminc-aead = "0.9.0" libfuzzer-sys = "0.4" -# Structure-aware inputs for `check_record` and `plan_build`: the mirror -# types derive `Arbitrary`, so the fuzzer mutates trees and plans, not bytes. +# Structure-aware inputs for `check_record`: the mirror types derive +# `Arbitrary`, so the fuzzer mutates trees and plans, not bytes. arbitrary = { version = "1", features = ["derive"] } # The fixed leaf the record harness fills its sealed nodes with. uuid = "1.8" @@ -58,12 +58,6 @@ test = false doc = false bench = false -[[bin]] -name = "plan_build" -path = "fuzz_targets/plan_build.rs" -test = false -doc = false -bench = false [[bin]] name = "json_wire_decode" diff --git a/packages/stack-encrypt/fuzz/corpus/plan_build/valid-duplicate-field-beside-passthrough-refused b/packages/stack-encrypt/fuzz/corpus/plan_build/valid-duplicate-field-beside-passthrough-refused deleted file mode 100644 index 37e9e4c035..0000000000 Binary files a/packages/stack-encrypt/fuzz/corpus/plan_build/valid-duplicate-field-beside-passthrough-refused and /dev/null differ diff --git a/packages/stack-encrypt/fuzz/corpus/plan_build/valid-passthrough-under-two-segment-context b/packages/stack-encrypt/fuzz/corpus/plan_build/valid-passthrough-under-two-segment-context deleted file mode 100644 index 718b5c2895..0000000000 --- a/packages/stack-encrypt/fuzz/corpus/plan_build/valid-passthrough-under-two-segment-context +++ /dev/null @@ -1 +0,0 @@ -Ú'šÿÿÿÿÿÿKKK \ No newline at end of file diff --git a/packages/stack-encrypt/fuzz/corpus/plan_build/valid-sealed-beside-odd-passthrough-names b/packages/stack-encrypt/fuzz/corpus/plan_build/valid-sealed-beside-odd-passthrough-names deleted file mode 100644 index 4adddeeb81..0000000000 Binary files a/packages/stack-encrypt/fuzz/corpus/plan_build/valid-sealed-beside-odd-passthrough-names and /dev/null differ diff --git a/packages/stack-encrypt/fuzz/corpus/plan_build/valid-sealed-name-not-a-segment-refused b/packages/stack-encrypt/fuzz/corpus/plan_build/valid-sealed-name-not-a-segment-refused deleted file mode 100644 index 9631013e01..0000000000 Binary files a/packages/stack-encrypt/fuzz/corpus/plan_build/valid-sealed-name-not-a-segment-refused and /dev/null differ diff --git a/packages/stack-encrypt/fuzz/fuzz_targets/check_record.rs b/packages/stack-encrypt/fuzz/fuzz_targets/check_record.rs index 7ab1d5fc71..99d5daf6c2 100644 --- a/packages/stack-encrypt/fuzz/fuzz_targets/check_record.rs +++ b/packages/stack-encrypt/fuzz/fuzz_targets/check_record.rs @@ -5,7 +5,7 @@ //! //! This is the walk a binding's decrypt runs before any key is requested: //! it decides whether a stored tree fits its plan, and it is the only thing -//! standing between a tree an attacker rewrote and `decrypt_as`, which +//! standing between a tree an attacker rewrote and the decrypt, which //! opens no AEAD for a passthrough and would hand forged plaintext back as //! a successful decrypt. So the input is not bytes but a *structure*: an //! `Arbitrary`-derived mirror of a plan value and of a ciphertext tree, diff --git a/packages/stack-encrypt/fuzz/fuzz_targets/plan_build.rs b/packages/stack-encrypt/fuzz/fuzz_targets/plan_build.rs deleted file mode 100644 index 3d039ea678..0000000000 --- a/packages/stack-encrypt/fuzz/fuzz_targets/plan_build.rs +++ /dev/null @@ -1,219 +0,0 @@ -#![no_main] - -//! A fields plan built from untrusted names: the context, each field's -//! name, the identity pinned on it and the verb that declares it. -//! -//! A binding builds plans from names its caller supplies (a record's keys, -//! a column list), so `FieldsBuilder::build` is a parser of untrusted text -//! as much as the byte decoders are. Names come either from a small -//! alphabet, so duplicates, shared identities and reserved forms collide -//! often, or as free text. -//! -//! The plan may be built with its context, or without one (the call names -//! it), and a field may be declared as the plan's context field, so a -//! context can also be given twice. -//! -//! Five invariants. Building and rendering never panic, whatever the -//! names. A plan built with its context gives every sealed or indexed field -//! a well-formed label: `<context>/<identity>`, which renders and parses -//! back to itself; a plan without one gives none, and keys each such field -//! under a plain identity segment. A passthrough or context field is under -//! no label, so a plan of passthrough fields with distinct names, under a -//! plain context or none, builds whatever text the names are. And a plan -//! whose context is given twice is refused with `TwoContextSources`. -//! And a built plan checks an empty batch's call context as it would one -//! record's: run with none and no context of its own is `NoContext`, run -//! with one beside its own is `TwoContextSources`, and opened likewise, -//! except that a context field's expected value waits for a record. - -use std::collections::HashSet; - -use arbitrary::Arbitrary; -use libfuzzer_sys::fuzz_target; -use stack_encrypt::plan::{FieldKind, FieldValues, Opens, PlanError, Runs}; -use stack_encrypt::{Equality, Error, Label, Plan}; - -/// A name: one of a few that collide or sit on a rule's edge, or free text. -#[derive(Arbitrary, Debug)] -enum Name { - Email, - Id, - /// Begins with a digit: not a plain segment. - TwoFactor, - /// A reserved character. - Parenthesised, - /// Two segments, not one. - Slashed, - Empty, - Free(String), -} - -impl Name { - fn as_str(&self) -> &str { - match self { - Name::Email => "email", - Name::Id => "id", - Name::TwoFactor => "2fa_enabled", - Name::Parenthesised => "created(utc)", - Name::Slashed => "users/email", - Name::Empty => "", - Name::Free(text) => text, - } - } -} - -#[derive(Arbitrary, Debug, Clone, Copy, PartialEq, Eq)] -enum Verb { - Encrypt, - EncryptIndex, - Index, - Passthrough, - ContextField, -} - -#[derive(Arbitrary, Debug)] -struct Declared { - verb: Verb, - name: Name, - identity: Option<Name>, -} - -#[derive(Arbitrary, Debug)] -struct Input { - /// `None`: a plan built without a context. - context: Option<Name>, - fields: Vec<Declared>, -} - -fuzz_target!(|input: Input| { - let mut builder = match &input.context { - Some(context) => Plan::context(context.as_str()).fields::<FieldValues, ()>(), - None => Plan::fields::<FieldValues, ()>(), - }; - for field in &input.fields { - let name = field.name.as_str(); - builder = match field.verb { - Verb::Encrypt => builder.encrypt::<String>(name), - Verb::EncryptIndex => builder.encrypt_index::<String>(name, Equality), - Verb::Index => builder.index::<String>(name, Equality), - Verb::Passthrough => builder.passthrough::<String>(name), - Verb::ContextField => builder.context_field::<String>(name), - }; - if let Some(identity) = &field.identity { - builder = builder.identity(identity.as_str()); - } - } - let _ = format!("{builder:?}"); - - let context = input.context.as_ref().map(|c| Label::parse(c.as_str())); - let context_fields = input - .fields - .iter() - .filter(|f| f.verb == Verb::ContextField) - .count(); - let sources = usize::from(context.is_some()) + context_fields; - let names: HashSet<&str> = input.fields.iter().map(|f| f.name.as_str()).collect(); - let passthrough_only = !matches!(context, Some(Err(_))) - && names.len() == input.fields.len() - && input.fields.iter().all(|f| f.verb == Verb::Passthrough); - - match builder.build() { - Ok(plan) => { - assert!(sources <= 1, "a context given {sources} times built"); - let context = context.map(|c| c.expect("a plan that builds has a plain context")); - assert_eq!(plan.label(), context.as_ref()); - if let Some(context) = &context { - assert_eq!(Label::parse(&context.to_string()).as_ref(), Ok(context)); - } - assert_eq!( - plan.context_field().is_some(), - context_fields == 1, - "the plan names its context field" - ); - assert_eq!(plan.field_plans().len(), input.fields.len()); - for (built, declared) in plan.field_plans().zip(&input.fields) { - assert_eq!(built.name(), declared.name.as_str()); - let identity = declared - .identity - .as_ref() - .unwrap_or(&declared.name) - .as_str(); - assert_eq!(built.identity(), identity); - let keys_nothing = matches!( - built.kind(), - FieldKind::Passthrough | FieldKind::ContextField - ); - match (built.label(), &context) { - (None, Some(_)) => assert!(keys_nothing), - (None, None) => { - if !keys_nothing { - assert_eq!( - Label::parse(identity).map(|l| l.segments().count()), - Ok(1), - "a sealed field is keyed under one plain segment" - ); - } - } - (Some(_), None) => panic!("a plan without a context labelled a field"), - (Some(label), Some(context)) => { - assert!(!keys_nothing); - assert_eq!(Label::parse(&label.to_string()).as_ref(), Ok(label)); - let mut expected: Vec<&str> = context.segments().collect(); - expected.push(identity); - assert!(label.segments().eq(expected)); - } - } - assert!(plan.field(built.name()).is_ok()); - } - let _ = format!("{plan:?}"); - - let has_own = context.is_some() || context_fields == 1; - let call = Label::parse("users").expect("a plain label"); - for call in [None, Some(&call)] { - let expected = match (has_own, call.is_some()) { - (false, false) => Some(PlanError::NoContext), - (true, true) => Some(PlanError::TwoContextSources { - first: if context_fields == 1 { - "a context field" - } else { - "the plan" - }, - second: "the call", - }), - _ => None, - }; - let run = Runs::<Vec<FieldValues>, ()>::check(&plan, &Vec::new(), call); - match (&expected, run) { - (None, Ok(())) => {} - (Some(expected), Err(Error::Plan(error))) => assert_eq!(&error, expected), - (expected, run) => panic!("an empty run checked {run:?}, not {expected:?}"), - } - let opened = Opens::<Vec<FieldValues>, ()>::check(&plan, &Vec::new(), call); - match (&expected, opened) { - (_, Ok(())) if context_fields == 1 => {} - (None, Ok(())) => {} - (Some(expected), Err(Error::Plan(error))) if context_fields == 0 => { - assert_eq!(&error, expected) - } - (expected, opened) => { - panic!("an empty opening checked {opened:?}, not {expected:?}") - } - } - } - } - Err(Error::Plan(error)) => { - assert!( - !passthrough_only, - "passthrough names are under no label, yet refused: {error}" - ); - if sources > 1 && !matches!(context, Some(Err(_))) { - assert!( - matches!(error, PlanError::TwoContextSources { .. }), - "a context given twice is refused as that, got {error}" - ); - } - let _ = error.to_string(); - } - Err(other) => panic!("a plan is refused only with a plan error, got {other:?}"), - } -}); diff --git a/packages/stack-encrypt/src/blob.rs b/packages/stack-encrypt/src/blob.rs index 9bc15acdc3..ff7503ad36 100644 --- a/packages/stack-encrypt/src/blob.rs +++ b/packages/stack-encrypt/src/blob.rs @@ -56,8 +56,8 @@ //! A blob is opaque. You cannot query anything inside it, and you decrypt //! all of it or none of it. Use something else for these needs: //! -//! - To encrypt each field of a struct as its own ciphertext, use the -//! structural API ([`KeysetCipher::encrypt_tree`] and the derives). +//! - To encrypt each field of a struct as its own ciphertext, use a +//! derived record, or [`KeysetCipher::encrypt_tree`]. //! - To encrypt many values in one request, use a batch //! ([`Pending::all`](crate::Pending::all)) or a collection (see //! [Collections](#collections)). diff --git a/packages/stack-encrypt/src/cipher.rs b/packages/stack-encrypt/src/cipher.rs index 10630396ed..46fe0ba7af 100644 --- a/packages/stack-encrypt/src/cipher.rs +++ b/packages/stack-encrypt/src/cipher.rs @@ -277,8 +277,8 @@ pub enum Error { #[error(transparent)] #[diagnostic(transparent)] Term(#[from] crate::sem::TermError), - /// A third-party [`EncryptFrom`](crate::target::EncryptFrom) / - /// [`DecryptInto`](crate::target::DecryptInto) implementation failed + /// A third-party [`EncryptTarget`](crate::target::EncryptTarget) / + /// [`DecryptSource`](crate::target::DecryptSource) implementation failed /// for a reason of its own. /// /// Its message is the implementation's, shown as given: an @@ -318,7 +318,7 @@ pub enum Error { /// A [`Pending`](crate::target::Pending) fulfilment's requests and /// responses did not line up: it drew more responses — or a different /// kind — than its requests asked for, or left some of them unconsumed. - /// Always a composition bug in an `EncryptFrom`/`DecryptInto` + /// Always a composition bug in an `EncryptTarget`/`DecryptSource` /// implementation, never a data error. #[error("a pending fulfilment's responses did not match its requests")] #[diagnostic(code(stack_encrypt::response_shape))] @@ -376,7 +376,7 @@ pub enum Error { /// A data key was requested through a [`StackCipher`] rather than a /// [`KeysetCipher`]: a [`Request::generate_data_key`] needs a keyset /// to mint under, and only a keyset-scoped pending has one. Always a - /// composition bug in a hand-written `EncryptFrom`, caught before any + /// composition bug in a hand-written `EncryptTarget`, caught before any /// I/O. /// /// [`Request::generate_data_key`]: crate::target::Request::generate_data_key @@ -394,10 +394,8 @@ pub enum Error { #[diagnostic(code(stack_encrypt::not_opened))] NotOpened, /// A [plan](crate::plan) was refused: it did not validate when it was - /// built, or the value, record or query it was run with does not match - /// it. A refusal at build or when a plan runs is raised before any key - /// is requested; [`FieldValues::take`](crate::plan::FieldValues::take) - /// also returns one for a record already in hand. + /// parsed, or the value, record or query it was run with does not match + /// it. A refusal is raised before any key is requested. #[error(transparent)] #[diagnostic(transparent)] Plan(#[from] crate::plan::PlanError), @@ -1593,7 +1591,7 @@ impl PendingStackCipherText { /// `Aes256Cipher`, where AAD-less sealing is a legitimate AEAD use, /// opened symmetrically by [`StackCipher::decrypt`]. But a tree that /// will be opened through the target layer's - /// [`decrypt_into`](crate::target::DecryptInto) — a per-field record + /// [`DecryptSource`](crate::target::DecryptSource) — a per-field record /// assembly in an FFI front-end, say — is bound by that layer's rule: it /// opens only under a [`NonEmpty`](crate::NonEmpty) context, so seal /// under one here (a `NonEmpty<T>` is an [`IntoContext`] like any other, and diff --git a/packages/stack-encrypt/src/contexts.rs b/packages/stack-encrypt/src/contexts.rs index b20ebfdcc0..844fee620a 100644 --- a/packages/stack-encrypt/src/contexts.rs +++ b/packages/stack-encrypt/src/contexts.rs @@ -19,7 +19,7 @@ //! It lists the contexts of each field of a declaration. A part the caller //! supplies when the declaration runs appears as a typed //! [`Placeholder`]. The derives emit `contexts()` on every record, and a -//! [`Plan`](crate::Plan) has it too. +//! plan that is data (`dynamic::record::Plan`) has it too. //! - [`CallContexts`], from [`Pending::context`](crate::Pending::context). //! It lists the exact contexts a prepared call will use, with the caller's //! values filled in. No key has been requested yet. @@ -739,8 +739,8 @@ impl CallerShape { /// What the caller supplies to a declaration whose context input is this /// type: the placeholders that stand for its parts in [`Contexts`]. /// -/// Implemented for the context inputs of the crate. The derives and the -/// plan builder use it to render the root of a declaration. Sealed: a +/// Implemented for the context inputs of the crate. The derives use it to +/// render the root of a declaration. Sealed: a /// context input is one of this crate's types. pub trait SuppliedContext: sealed::Sealed { /// The placeholders for the caller's parts. @@ -949,8 +949,9 @@ pub(crate) enum Shape { /// `own`, extended by `by`: what `DeclaredContext::under` and /// `Extends::extend` build. Under { own: Box<Shape>, by: Box<Shape> }, - /// A list of parts, some of them not known yet: a label the call names - /// joined to a field's identity. + /// A list of parts, some of them not known yet: a label a field of the + /// record holds, joined to a field's identity. + #[cfg_attr(not(feature = "dynamic"), allow(dead_code))] List(Vec<Shape>), /// Known caller parts, applied one after another: what a data plan /// carries as its extension and its bindings. @@ -1035,6 +1036,7 @@ impl Shape { /// The shape of a subtree run under a label that is not known yet, /// joined to `identity` when the field has one, and extended by the /// context the subtree is handed. + #[cfg(any(test, feature = "dynamic"))] pub(crate) fn under_label(placeholder: Placeholder, identity: Option<&str>) -> Shape { let own = match identity { None => Shape::Placeholder(placeholder), diff --git a/packages/stack-encrypt/src/descriptor.rs b/packages/stack-encrypt/src/descriptor.rs index b998b9e3e2..c5adfae056 100644 --- a/packages/stack-encrypt/src/descriptor.rs +++ b/packages/stack-encrypt/src/descriptor.rs @@ -622,8 +622,8 @@ impl std::str::FromStr for Label { /// position, counting from zero. /// /// A label is not always schema: a plan's context and its fields' names -/// are, but [`context_field`](crate::plan::FieldsBuilder::context_field) -/// builds the context from a record's own field, which is customer data. +/// are, but a plan whose context is a field of the record (`context_field`) +/// builds the context from that field's value, which is customer data. /// So no message or payload quotes a segment or the character it refused; /// they name the segment by position. A caller in the same process can /// still read [`Reserved`](Self::Reserved)'s `found`. diff --git a/packages/stack-encrypt/src/dynamic/context.rs b/packages/stack-encrypt/src/dynamic/context.rs index 079675b84e..0bc0694b5a 100644 --- a/packages/stack-encrypt/src/dynamic/context.rs +++ b/packages/stack-encrypt/src/dynamic/context.rs @@ -20,7 +20,7 @@ use crate::{ContextPiece, NonEmpty}; /// x.into_prf_context() == x.into_context().into_prf_context() /// ``` /// -/// So a `#[derive(EncryptFrom)]` row sealed with +/// So a `#[derive(EncryptTarget)]` row sealed with /// `encrypt_into_with_context(row, 7u64)`, which binds each field under /// `(("users", "age"), 7u64)` — the inferred pair, extended — and a binding /// that spells the same context as `[["users", "age"], 7u64]` agree byte for diff --git a/packages/stack-encrypt/src/dynamic/json_query.rs b/packages/stack-encrypt/src/dynamic/json_query.rs index 4672501d24..f5b3928cd8 100644 --- a/packages/stack-encrypt/src/dynamic/json_query.rs +++ b/packages/stack-encrypt/src/dynamic/json_query.rs @@ -205,12 +205,12 @@ use zeroize::Zeroizing; use super::query::{Op, Operand}; use super::{Error, Reason, ValueFrame}; use crate::json::{ - parse_text, wipe_value, JsonExtracted, JsonLeafQuery, JsonNode, JsonOptions, JsonPath, - JsonQuery, JsonSubtree, JsonSubtreeKey, + parse_text, wipe_value, JsonLeafQuery, JsonNode, JsonOptions, JsonPath, JsonPlaintext, + JsonQuery, JsonSubtreeKey, }; use crate::sem::TermError; use crate::target::{AeadContext, CallerContext, DeclaredContext, Index, Json, Owned}; -use crate::{ContextPiece, KeysetCipher, Label, NonEmpty, Pending}; +use crate::{ContextPiece, DecryptFrom, KeysetCipher, Label, NonEmpty, Pending}; /// The SQL type a JSON operand binds as. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -1070,45 +1070,50 @@ pub(super) fn open<'a, K: 'static>( // keyset's, so a document sealed under another cannot answer it. let opener_cipher = keyset.cipher(); let confine = keyset.keyset_id(); - let confined = |pending: Pending<'a, Vec<Value>, K>| pending.scoped_to(confine); - let mut pendings: Vec<Pending<'a, Vec<Value>, K>> = Vec::with_capacity(groups.len()); + let confined = |pending: Pending<'a, Vec<JsonPlaintext>, K>| pending.scoped_to(confine); + let mut pendings: Vec<Pending<'a, Vec<JsonPlaintext>, K>> = Vec::with_capacity(groups.len()); for nodes in groups { pendings.push(match &opener { Opener::Selector(selector) => match <[JsonNode; 1]>::try_from(nodes) { Ok([node]) => confined( - opener_cipher - .decrypt_as::<Value, JsonExtracted>(selector.open(node), context()) - .map(|value| vec![value]), + JsonPlaintext::decrypt_from_with_context( + selector.open(node), + opener_cipher, + context(), + ) + .map(|value| vec![value]), ), // `read_open` allows a selector group one entry or none. Err(_) => Pending::ready(keyset, Ok(Vec::new())), }, - Opener::Subtree(key) => confined( - opener_cipher.decrypt_as::<Vec<Value>, JsonSubtree>(key.open(nodes), context()), - ), + Opener::Subtree(key) => confined(Vec::<JsonPlaintext>::decrypt_from_with_context( + key.open(nodes), + opener_cipher, + context(), + )), }); } Ok(Pending::all(keyset, pendings).try_map(opened_frame)) } /// The opened groups as a frame: a list per group of each value's JSON -/// text, as bytes. Each value is wiped once its text is written, and each -/// text once the frame holds it. -fn opened_frame(mut groups: Vec<Vec<Value>>) -> Result<ValueFrame, crate::Error> { +/// text, as bytes. Each value is wiped when it is dropped, here, once its +/// text is written, and each text once the frame holds it. +fn opened_frame(groups: Vec<Vec<JsonPlaintext>>) -> Result<ValueFrame, crate::Error> { let texts: Result<Vec<Vec<Protected<Vec<u8>>>>, crate::Error> = groups .iter() .map(|group| { group .iter() .map(|value| { - crate::json::to_text(value) + crate::json::to_text(value.value()) .map(Protected::new) .map_err(|error| crate::Error::Other(Box::new(error))) }) .collect() }) .collect(); - groups.iter_mut().flatten().for_each(wipe_value); + drop(groups); ValueFrame::of(&texts?).map_err(|error| crate::Error::Other(Box::new(error))) } diff --git a/packages/stack-encrypt/src/dynamic/mod.rs b/packages/stack-encrypt/src/dynamic/mod.rs index 26d6e15c18..ddcccd52a6 100644 --- a/packages/stack-encrypt/src/dynamic/mod.rs +++ b/packages/stack-encrypt/src/dynamic/mod.rs @@ -32,10 +32,10 @@ //! only at run time, and the value model it holds. //! * [`term`](term()) — one index term for a value, derived at the one //! scalar it is walked to. -//! * [`record`] — a fields plan spelled as data, *lowered* into the plan -//! builder ([`Plan`](crate::Plan)) and run by the engine: the same code a -//! Rust chain and the derive run, so a record written from any language -//! is the same bytes. The one step that stays dynamic is +//! * [`record`] — a record's declaration as data, *lowered* into the +//! combinators in [`target`](crate::target) and run by the engine: the +//! same combinators the derive composes, so a record written from any +//! language is the same bytes. The one step that stays dynamic is //! dispatching a value whose type is known only at run time to the typed //! term operation, which [`IndexSpec`]'s `Index` impls do. //! * [`read_json`] and [`json_frame`] — a `json` field's document, from @@ -61,7 +61,8 @@ //! implements `Encrypt` already, so `keyset.encrypt_tree(value, aad)` is the whole //! of it and needs nothing from this module. Nor is a second executor: nothing here calls //! the term functions or the seal path to produce a record. A capability a -//! data plan needs and the builder lacks is added to the builder, once. +//! data plan needs and the combinators lack is added to the combinators, +//! once. //! //! # Stability //! @@ -211,8 +212,8 @@ pub enum Error { /// missing or duplicating an output, carrying a key that is not /// `"context"`, `"outputs"`, `"target"` or `"type"`, naming a type that /// is not one, asking for an index its declared type is not defined - /// for, giving a field a context that is not a label a fields plan can - /// seal it under, or declaring what the plan builder refuses (two fields + /// for, giving a field a context that is not a label a plan can + /// seal it under, or declaring what no plan allows (two fields /// under one identity, fields under different contexts). #[error("record plan is malformed: {reason}{}", in_field_text(field))] #[diagnostic(code(stack_encrypt::dynamic_plan))] @@ -590,9 +591,8 @@ reasons! { /// bindings, a list of scalar context parts: it is not a field name, /// and its value is nothing else, so a grouped part is refused. Bind => "bind", r#""bind" is reserved for the caller's bindings: not a field name, and its value is a list of scalar context parts"#; - /// The plan builder refused the plan for a reason none of the above - /// names. - Refused => "refused", "the plan builder refused it"; + /// The plan was refused for a reason none of the above names. + Refused => "refused", "the plan was refused"; /// A field the plan names is not there. FieldMissing => "field_missing", "a field the plan names is missing"; /// A field is there twice. diff --git a/packages/stack-encrypt/src/dynamic/query.rs b/packages/stack-encrypt/src/dynamic/query.rs index e3c3e3cf1f..e4c95333ec 100644 --- a/packages/stack-encrypt/src/dynamic/query.rs +++ b/packages/stack-encrypt/src/dynamic/query.rs @@ -1,15 +1,15 @@ //! Queries named by operator, for a binding. //! -//! A Rust caller names an operator as a type, `cipher.query(op::Matches, -//! "ali").on(&plan)`, and the compiler finds the index that answers it. A +//! A Rust caller queries a field through its marker, +//! `EncryptedUser::NAME.matches("ali")`, and the compiler finds the index +//! that answers the operator. A //! binding has no type to name: its plan arrives as data and its operator //! as a string. This module is the runtime form of that query, written once //! here rather than once per language binding: //! //! - [`Op`] names an operator, as [`IndexSpec`] names an index, and -//! [`Op::answered_by`] is the table the typed path's `DefaultIndex` -//! impls state: which declared index answers the operator for a field of -//! a kind, if any does. +//! [`Op::answered_by`] is the table of which declared index answers the +//! operator for a field of a kind, if any does. //! - [`query_term`] derives the query term for a plan field, an operator and //! its input: the field's own index term for the input, or, for a field //! that names an EQL type, that type's query value. It refuses an operator @@ -37,8 +37,8 @@ //! `between`) through an ORE index over an integer of 16 bits or wider, a //! float, text, a date, a timestamp or a decimal, else a block ORE index, //! else an OPE index. The typed -//! path's default for an ordering is ORE, and block ORE and OPE are the -//! typed path's `.via(OreBlock)` and `.via(Ope)`; here a field that +//! path's field names its order term by type (`OreTerm`, `OreBlockTerm` or +//! `OpeTerm`); here a field that //! declares only one of them is answered through it. Every order term is //! derived over the value's order key: a float's ties `-0.0` with //! `0.0` and sorts every NaN last, and text's is its fold. An ordering over @@ -699,8 +699,8 @@ fn not_a_range(field: &str) -> Error { } /// What a query of `input` through `index` is refused for before any term -/// exists, and what it keeps: the `sem` module's rules, which the typed -/// chain's [`Supports::prepare`](crate::op::Supports::prepare) applies to +/// exists, and what it keeps: the `sem` module's rules, which a typed +/// query's [`Supports::prepare`](crate::op::Supports::prepare) applies to /// the same effect. A binding asks at its boundary ([`check_query`]), and /// [`query_term`] and [`verify`] ask before they derive anything, so the /// three refuse exactly the same inputs. A match query keeps its tokens. @@ -773,7 +773,7 @@ fn query_tokens(value: &ValueFrame, options: &MatchOptions) -> Result<QueryToken value .visit(OneScalar::new( |ty: Option<Logical>, value: Physical<'_>| match (ty, value) { - // The query chain's rule, from the same function. + // The typed query's rule, from the same function. (None, Physical::Text(text)) => Ok(crate::sem::query::match_tokens(text, options) .map_err(|error| Error::Cipher(error.into()))), _ => Err(None), @@ -1035,7 +1035,22 @@ where /// ``` /// use stack_encrypt::dynamic::{query_term, record, NoTargets, Op, Operand}; /// use stack_encrypt::kms::FakeDataKeySource; -/// use stack_encrypt::{op, AsProtectedRef, Ore, Plan, StackCipher}; +/// use stack_encrypt::sem::OreTerm; +/// use stack_encrypt::target::EncryptTarget; +/// use stack_encrypt::{AsProtectedRef, Encrypted, StackCipher}; +/// use zeroize::Zeroize; +/// +/// #[derive(Zeroize)] +/// struct User { +/// age: u32, +/// } +/// +/// // The typed record that declares the same field. +/// #[derive(EncryptTarget)] +/// #[stash(struct = User, scope = "users")] +/// struct EncryptedUser { +/// age: Encrypted<OreTerm<u32>>, +/// } /// /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async { /// let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; @@ -1047,9 +1062,8 @@ where /// /// let term = query_term(&keyset, &plan, "age", Op::Lt, 40u32, &NoTargets)?.await?; /// -/// // The typed path's term for the same query. -/// let typed_plan = Plan::context("users/age").with::<u32, _>(Ore).build()?; -/// let typed = cipher.query(op::Lt, &40u32).on(&typed_plan).await?; +/// // The typed query of the same field, from its marker, is the same term. +/// let typed = EncryptedUser::AGE.lt(40u32).encrypt(&keyset).await?; /// assert_eq!(term.operand(), &Operand::One(typed.to_bytes())); /// # Ok::<(), Box<dyn std::error::Error>>(()) /// # }).unwrap(); @@ -1382,11 +1396,22 @@ mod tests { ) } use crate::dynamic::{record, NoTargets, TargetError}; - use crate::op::{self, Probabilistic as _}; - use crate::sem::MatchTerms; - use crate::{Equality, Label, Match, Ope, Ore, StackCipher}; + use crate::marker::{Marker, Root}; + use crate::op::Probabilistic as _; + use crate::sem::{EqualityTerm, MatchTerms, OpeTerm, OreTerm}; + use crate::{Label, StackCipher}; use stack_kms::FakeDataKeySource; + /// The marker of the field `name` of a record with `scope = "users"`, + /// whose target is `T`, as the derive emits it. A typed query starts + /// from it, and derives under the label `users/<name>`, which is the + /// label the data plan of [`users`] gives the field. + fn marker<T>(name: &'static str) -> Marker<Root<()>, T> { + Marker::new(name, "users", name, || { + unreachable!("these tests do not read a marker's contexts") + }) + } + async fn cipher() -> StackCipher<FakeDataKeySource> { StackCipher::builder() .kms(FakeDataKeySource::new()) @@ -1615,13 +1640,9 @@ mod tests { let term = query(&keyset, "email", Op::Eq, text("bob@example.com")) .await .unwrap(); - let plan = crate::Plan::context("users/email") - .with::<String, _>(Equality) - .build() - .unwrap(); - let typed = cipher - .query(op::Eq, "bob@example.com") - .on(&plan) + let typed = marker::<EqualityTerm>("email") + .eq("bob@example.com") + .encrypt(&keyset) .await .unwrap(); assert_eq!(term.operand(), &Operand::One(typed.to_bytes())); @@ -1635,11 +1656,11 @@ mod tests { async fn an_ordering_derives_the_typed_paths_ore_term_and_reads_the_input_as_the_fields_kind() { let cipher = cipher().await; let keyset = cipher.default_keyset(); - let plan = crate::Plan::context("users/age") - .with::<u32, _>(Ore) - .build() + let typed = marker::<OreTerm<u32>>("age") + .lt(40u32) + .encrypt(&keyset) + .await .unwrap(); - let typed = cipher.query(op::Lt, &40u32).on(&plan).await.unwrap(); for input in [Tree::UInt32(40), Tree::Float64(40.0), Tree::Int64(40)] { let term = query(&keyset, "age", Op::Lt, input).await.unwrap(); assert_eq!(term.operand(), &Operand::One(typed.to_bytes())); @@ -1651,13 +1672,9 @@ mod tests { async fn between_derives_both_bounds_under_one_context_as_the_typed_range() { let cipher = cipher().await; let keyset = cipher.default_keyset(); - let plan = crate::Plan::context("users/age") - .with::<u32, _>(Ore) - .build() - .unwrap(); - let typed = cipher - .query(op::Between, &(18u32, 65)) - .on(&plan) + let typed = marker::<OreTerm<u32>>("age") + .between(18u32, 65) + .encrypt(&keyset) .await .unwrap(); let term = query( @@ -1684,14 +1701,9 @@ mod tests { .await; assert!(one.is_ok(), "(v, v) is one value: {one:?}"); - let plan = crate::Plan::context("users/score") - .with::<i64, _>(Ope) - .build() - .unwrap(); - let typed = cipher - .query(op::Between, &(-5i64, 5)) - .via(Ope) - .on(&plan) + let typed = marker::<OpeTerm<i64>>("score") + .between(-5i64, 5) + .encrypt(&keyset) .await .unwrap(); let term = query( @@ -1720,11 +1732,11 @@ mod tests { async fn matches_derives_the_typed_paths_filter() { let cipher = cipher().await; let keyset = cipher.default_keyset(); - let plan = crate::Plan::context("users/name") - .with::<String, _>(Match::default()) - .build() + let typed = marker::<MatchTerms>("name") + .matches("alice") + .encrypt(&keyset) + .await .unwrap(); - let typed = cipher.query(op::Matches, "alice").on(&plan).await.unwrap(); let term = query(&keyset, "name", Op::Matches, text("alice")) .await .unwrap(); @@ -1854,11 +1866,12 @@ mod tests { #[tokio::test] async fn verify_answers_as_the_typed_match_query() { let cipher = cipher().await; - let plan = crate::Plan::context("users/name") - .with::<String, _>(Match::default()) - .build() + let keyset = cipher.default_keyset(); + let typed = marker::<MatchTerms>("name") + .matches("alice") + .encrypt(&keyset) + .await .unwrap(); - let typed = cipher.query(op::Matches, "alice").on(&plan).await.unwrap(); let page = ["Alice Smith", "alicia", "Bob", "MALICE", "", "ali ce"]; let values: Vec<Tree> = page.iter().map(|v| text(v)).collect(); let verified = verify( @@ -1961,7 +1974,7 @@ mod tests { } /// Match options on a field plan built by hand are not checked when the - /// plan is, so the query chain refuses them before it cuts a token: an + /// plan is, so the query refuses them before it cuts a token: an /// n-gram length of zero would otherwise panic in the tokenizer. #[test] fn invalid_match_options_are_refused_before_tokenising() { @@ -2009,11 +2022,11 @@ mod tests { async fn target_limit_answers_as_the_typed_match_query() { let cipher = cipher().await; let keyset = cipher.default_keyset(); - let plan = crate::Plan::context("users/name") - .with::<String, _>(Match::default()) - .build() + let typed = marker::<MatchTerms>("name") + .matches("ali") + .encrypt(&keyset) + .await .unwrap(); - let typed = cipher.query(op::Matches, "ali").on(&plan).await.unwrap(); let term = query(&keyset, "name", Op::Matches, text("ali")) .await .unwrap(); @@ -2217,11 +2230,11 @@ mod tests { } }; - let tiny = crate::Plan::context("users/tiny") - .with::<i16, _>(Ore) - .build() + let typed = marker::<OreTerm<i16>>("tiny") + .lt(-8i16) + .encrypt(&keyset) + .await .unwrap(); - let typed = cipher.query(op::Lt, &-8i16).on(&tiny).await.unwrap(); for input in [ int(Int::I8(-8)), vf(&Tree::Int64(-8)), @@ -2236,14 +2249,9 @@ mod tests { "70000 is no int16: {refused:?}" ); - let huge = crate::Plan::context("users/huge") - .with::<i128, _>(Ope) - .build() - .unwrap(); - let typed = cipher - .query(op::Between, &(i128::MIN, i128::MAX)) - .via(Ope) - .on(&huge) + let typed = marker::<OpeTerm<i128>>("huge") + .between(i128::MIN, i128::MAX) + .encrypt(&keyset) .await .unwrap(); let bounds = ValueFrame::of(vec![ diff --git a/packages/stack-encrypt/src/dynamic/record.rs b/packages/stack-encrypt/src/dynamic/record.rs index 05dfd0f32f..3fcc49c3d5 100644 --- a/packages/stack-encrypt/src/dynamic/record.rs +++ b/packages/stack-encrypt/src/dynamic/record.rs @@ -30,14 +30,14 @@ //! has at least two segments. A field's label may be extended by the //! caller's parts, nested to the left as a binding extends one part at a //! time (`[["users", "age"], 7]`, then `[[["users", "age"], 7], "eu"]`), and -//! every field must carry the same extension: it becomes the call's -//! `.extend(..)`. A context that is not a label (one text part `"users/age"`, +//! every field must carry the same extension: it extends the context of +//! each field. A context that is not a label (one text part `"users/age"`, //! an integer, bytes) is refused as a plan a fields plan cannot express. //! //! # A plan whose context is a field of the record //! -//! The typed plan builder's third context source, `context_field`, has a -//! data form too: a plan-level key, `"context_field": "<field name>"`, beside +//! A plan can take its context from a field of each record: a plan-level +//! key, `"context_field": "<field name>"`, beside //! the field specs. The named field's value in each record is then the //! context every other field is sealed under — a tenant label such as //! `"tenants/acme"`, which must parse as a plain [`Label`] — and the field @@ -110,7 +110,7 @@ //! kind, once checked — by the typed path's derivation, so they are the //! bytes a Rust `u32` or `String` field derives under the same label. //! -//! The ciphertext is where a data plan and a Rust chain meet too. Every +//! The ciphertext is where a data plan and typed Rust code meet too. Every //! leaf is tagged with its type, a typed Rust value's as much as //! a [`ValueFrame`]'s, so a Rust `u32` field and a `uint32` data field seal //! the same leaf under the same context, and each opens the other's. A reader @@ -707,9 +707,8 @@ pub struct Plan { fields: Vec<FieldPlan>, } -/// Where a data plan takes its context from: the builder's first and third -/// sources. The second, the call, has no data form: a binding names the -/// context in the declaration it sends. +/// Where a data plan takes its context from: the plan, or a field of each +/// record. A binding names the context in the declaration it sends. #[derive(Clone, Debug)] pub(super) enum Context { /// The plan's own label, which every field's label extends. @@ -839,8 +838,7 @@ impl Plan { } /// A plan over `fields` that takes its context from the field named - /// `context_field`: the plan builder's - /// [`context_field`](crate::plan::FieldsBuilder::context_field), as data. That + /// `context_field`. That /// field's value in each record is the context every other field is /// sealed under, and the field is carried as a passthrough of type /// string. Each field's label is its identity alone, a one-segment @@ -981,8 +979,7 @@ impl Plan { /// The plan's one context: the label every field's label extends. /// `None` for a plan that takes its context from a field - /// ([`context_field`](Self::context_field)), as the builder's - /// [`Plan::label`](crate::Plan::label) is. + /// ([`context_field`](Self::context_field)). pub fn label(&self) -> Option<&Label> { match &self.context { Context::Label(label) => Some(label), @@ -990,8 +987,7 @@ impl Plan { } } - /// The field the plan takes its context from, if it has one: the - /// builder's [`Plan::context_field`](crate::Plan::context_field). + /// The field the plan takes its context from, if it has one. pub fn context_field(&self) -> Option<&str> { match &self.context { Context::Field(name) => Some(name), @@ -1008,9 +1004,8 @@ impl Plan { /// The contexts this plan's fields are sealed and indexed under, with /// the plan's extension applied and a placeholder for a context read /// from a field of each record. No key is requested. The same view as - /// [`Plan::contexts`](crate::Plan::contexts) for a typed plan, so a - /// binding can show a record's contexts, and a test in that language - /// can pin them. + /// a derived record's `contexts()`, so a binding can show a record's + /// contexts, and a test in that language can pin them. /// /// # Errors /// @@ -1148,9 +1143,8 @@ impl Plan { )) } - /// The scopes every field's label is run under: the extension, as the - /// chain's `.extend(..)` would carry it. A query runs under these - /// alone. + /// The scopes every field's label is run under: the extension. A + /// query runs under these alone. fn scopes(&self) -> DeclaredContext { self.extension .iter() @@ -2050,14 +2044,12 @@ fn name_target(error: TargetError, field: &str) -> TargetError { /// against `expected` or leaves the sealed fields unable to open. /// /// `expected` is the context the caller expects, for a plan with a -/// [context field](Plan::with_context_field): the chain's -/// `open(record).context(expected)`. Each record's stored context field is +/// [context field](Plan::with_context_field). Each record's stored context field is /// checked against it before any key is requested, and a record that /// names another context fails the pending with /// [`Error::ContextMismatch`](crate::Error::ContextMismatch). `None` /// opens each record under whatever context it stores. For a plan with a -/// context of its own there is nothing to expect: `Some` is refused as the -/// chain refuses it, with +/// context of its own there is nothing to expect: `Some` is refused with /// [`PlanError::TwoContextSources`](crate::PlanError::TwoContextSources). /// /// # Errors @@ -2903,10 +2895,10 @@ mod tests { use vitaminc_protected::Controlled; use crate::dynamic::term; - use crate::plan::pick; + use crate::plan::join; use crate::sem::EqualityTerm; - use crate::target::{AeadContext, DecryptInto, EncryptFrom}; - use crate::{nonempty, Equality, PlanError, StackCipher}; + use crate::target::{ciphertext, indexed, AeadContext, Borrowed, DecryptSource, EncryptTarget}; + use crate::{nonempty, DecryptFrom, EncryptInto, Equality, Indexes, PlanError, StackCipher}; /// `FakeDataKeySource` with call counters, so the batching contract — /// one key request per invocation, none for a refused call — is @@ -3350,8 +3342,8 @@ mod tests { /// A field's context is its label, optionally extended, and nothing /// else: what a fields plan can give a field. The shapes refused here - /// are contexts (`dynamic::context` reads them) that no `.fields()` - /// chain could spell. + /// are contexts (`dynamic::context` reads them) that no field of a + /// record can have. #[test] fn refuses_a_field_context_that_is_not_a_label() { // The text, one-segment, malformed-segment and nested-extension @@ -3426,7 +3418,7 @@ mod tests { } /// Two sealed fields keyed under one identity would have one context - /// and interchangeable terms: the builder refuses it, so the plan does. + /// and interchangeable terms, so the plan refuses it. #[test] fn refuses_two_fields_keyed_under_one_identity() { let parsed = plan(obj(vec![ @@ -4182,41 +4174,46 @@ mod tests { assert_eq!(text_of(&email[1].1), "b@x"); } - /// The lowering is the plan builder: the record a data plan seals is - /// the record the chain seals under the same declaration over the - /// same plaintext type, a [`ValueFrame`] per field — the same terms, - /// and ciphertexts each side opens. + /// The lowering is the combinators: each field of the record a data + /// plan seals is what the combinator for its outputs seals over the + /// same plaintext type, a [`ValueFrame`], under the field's label. + /// The terms are the same, and each side opens the other's + /// ciphertext. #[tokio::test] - async fn seals_what_the_chain_seals_under_the_same_declaration() { - struct User { - age: ValueFrame, - email: ValueFrame, - nick: ValueFrame, - id: u64, - } + async fn seals_what_the_combinators_seal_under_the_same_declaration() { let cipher = cipher().await; let keyset = cipher.default_keyset(); let frame = |value: Tree| ValueFrame::of(&value).expect("a value"); - let user = User { - age: frame(Tree::UInt32(34)), - email: frame(s("a@x")), - nick: frame(s("al smith")), - id: 7, - }; let default_match = IndexSpec::Match(crate::sem::MatchOptions::default()); - let mut chain = cipher - .encrypt(&user) - .context("users") - .fields() - .encrypt_index( - pick("age", |u: &User| &u.age), - (IndexSpec::Equality, IndexSpec::Ore), + // `c`, `eq` and `ore`: a ciphertext with its terms beside it. + let age: Encrypted<(TermBytes, TermBytes)> = keyset + .run( + indexed::<_, _, Borrowed, _>((IndexSpec::Equality, IndexSpec::Ore)) + .under(nonempty!("users").with("age")), + &frame(Tree::UInt32(34)), + DeclaredContext::default(), + ) + .await + .expect("age"); + // `c` alone: a ciphertext. + let email: StackCipherText = keyset + .run( + ciphertext::<_, _, Borrowed>().under(nonempty!("users").with("email")), + &frame(s("a@x")), + DeclaredContext::default(), ) - .encrypt(pick("email", |u: &User| &u.email)) - .index(pick("nick", |u: &User| &u.nick), default_match) - .passthrough(pick("id", |u: &User| &u.id)) .await - .expect("the chain"); + .expect("email"); + // `match` alone: the terms, with no ciphertext. + let nick: TermBytes = keyset + .run( + Indexes::<ValueFrame>::operations::<_, Borrowed>(&default_match) + .under(nonempty!("users").with("nick")), + &frame(s("al smith")), + DeclaredContext::default(), + ) + .await + .expect("nick"); let plan = plan(obj(vec![ ( "age", @@ -4229,7 +4226,6 @@ mod tests { .expect("plan"); let mut fields = map(seal(&keyset, row(34), &plan).await); - let age: Encrypted<(TermBytes, TermBytes)> = chain.take("age").expect("age"); let mut lowered_age = map(node(&mut fields, "age")); assert_eq!( term_bytes(&node(&mut lowered_age, "eq")), @@ -4241,7 +4237,6 @@ mod tests { age.terms.1.as_bytes(), "the same ore term" ); - let nick: TermBytes = chain.take("nick").expect("nick"); let mut lowered_nick = map(node(&mut fields, "nick")); assert_eq!( term_bytes(&node(&mut lowered_nick, "match")), @@ -4250,15 +4245,14 @@ mod tests { ); // Each side's ciphertext opens through the other. - let chain_opened: Tree = cipher + let typed_opened: Tree = cipher .decrypt( node(&mut lowered_age, "c"), Label::parse("users/age").expect("label"), ) .await - .expect("the chain's reader opens the lowering's leaf"); - assert_eq!(u32_of(&chain_opened), 34); - let email: StackCipherText = chain.take("email").expect("email"); + .expect("the cipher's reader opens the lowering's leaf"); + assert_eq!(u32_of(&typed_opened), 34); let stored = CipherText::Map(vec![ ( "age".to_string(), @@ -4278,7 +4272,7 @@ mod tests { assert_eq!( u32_of(&opened[0].1), 34, - "the lowering opens the chain's leaf" + "the lowering opens the combinator's leaf" ); assert_eq!(text_of(&opened[1].1), "a@x", "and its string"); assert_eq!(u64_of(&opened[2].1), 7); @@ -4306,15 +4300,11 @@ mod tests { } } - /// A field's extension lowers to the chain's `.extend(..)`: the - /// record is the typed chain's under the same extension, and the - /// stored terms are the probes under the extended context, not the - /// flat one. + /// A field's extension extends the field's context: the stored term + /// is the term the combinator derives under the same extension, and + /// the probe under the extended context, not the flat one. #[tokio::test] - async fn a_one_part_extension_is_the_chains_extend() { - struct Age { - age: u32, - } + async fn a_one_part_extension_extends_the_fields_context() { let cipher = cipher().await; let keyset = cipher.default_keyset(); let parts = [Tree::UInt64(7)]; @@ -4327,20 +4317,19 @@ mod tests { let mut fields = map(seal(&keyset, obj(vec![("age", Tree::UInt32(34))]), &plan).await); let mut age = map(node(&mut fields, "age")); - let mut typed_record = cipher - .encrypt(&Age { age: 34 }) - .context("users") - .fields() - .encrypt_index(pick("age", |a: &Age| &a.age), Equality) - .extend(7u64) + let typed_age: Encrypted<EqualityTerm> = keyset + .run( + indexed::<_, _, Borrowed, _>(Equality).under(nonempty!("users").with("age")), + &34u32, + DeclaredContext::from(CallerContext::from(7u64)), + ) .await - .expect("typed chain"); - let typed_age: Encrypted<EqualityTerm> = typed_record.take("age").expect("age"); + .expect("the combinator"); let stored = term_bytes(&node(&mut age, "eq")); assert_eq!( stored, typed_age.terms.to_bytes(), - "the same term as the chain extended by the same part" + "the same term as the combinator extended by the same part" ); let native = nonempty!("users").with("age").with(7u64); let probe = keyset.equality_term(34u32, native).await.expect("probe"); @@ -4379,19 +4368,15 @@ mod tests { let mut age = map(node(&mut fields, "age")); let stored = term_bytes(&node(&mut age, "eq")); - struct Age { - age: u32, - } - let mut typed = cipher - .encrypt(&Age { age: 34 }) - .context("users") - .fields() - .encrypt_index(pick("age", |a: &Age| &a.age), Equality) - .extend(7u64) - .extend(nonempty!("eu")) + let typed_age: Encrypted<EqualityTerm> = keyset + .run( + indexed::<_, _, Borrowed, _>(Equality).under(nonempty!("users").with("age")), + &34u32, + DeclaredContext::from(CallerContext::from(7u64)) + .with(CallerContext::from(nonempty!("eu"))), + ) .await - .expect("typed chain"); - let typed_age: Encrypted<EqualityTerm> = typed.take("age").expect("age"); + .expect("the combinator"); assert_eq!(stored, typed_age.terms.to_bytes()); let nested = nonempty!("users").with("age").with(7u64).with("eu"); @@ -4880,32 +4865,30 @@ mod tests { plan.fields().iter().map(FieldPlan::name).collect() } - struct Age { - age: u32, - } - - /// The equality term the typed chain stores for `age` under + /// The equality term the combinator stores for `age` under /// `<context>/age`, extended by `parts`: what the lowering must /// derive when the context comes from the record. - async fn chain_term( + async fn typed_term( cipher: &StackCipher<Counting>, context: &str, age: u32, parts: &[u64], ) -> Vec<u8> { - let value = Age { age }; - let chain = cipher - .encrypt(&value) - .context(context) - .fields() - .encrypt_index(pick("age", |a: &Age| &a.age), Equality); - let mut record = match parts { - [] => chain.await, - [part] => chain.extend(*part).await, + let label = join(&Label::parse(context).expect("a label"), "age").expect("a label"); + let extension = match parts { + [] => DeclaredContext::default(), + [part] => DeclaredContext::from(CallerContext::from(*part)), _ => panic!("one part at most"), - } - .expect("typed chain"); - let typed: Encrypted<EqualityTerm> = record.take("age").expect("age"); + }; + let typed: Encrypted<EqualityTerm> = cipher + .default_keyset() + .run( + indexed::<_, _, Borrowed, _>(Equality).under(NonEmpty::from(label)), + &age, + extension, + ) + .await + .expect("the combinator"); typed.terms.to_bytes() } @@ -5139,13 +5122,13 @@ mod tests { let globex_term = term_bytes(&node(&mut map(node(&mut globex, "age")), "eq")); assert_eq!( acme_term, - chain_term(&cipher, "tenants/acme", 34, &[]).await, - "the chain's term under tenants/acme" + typed_term(&cipher, "tenants/acme", 34, &[]).await, + "the typed term under tenants/acme" ); assert_eq!( globex_term, - chain_term(&cipher, "tenants/globex", 34, &[]).await, - "the chain's term under tenants/globex" + typed_term(&cipher, "tenants/globex", 34, &[]).await, + "the typed term under tenants/globex" ); assert_ne!( acme_term, globex_term, @@ -5184,8 +5167,7 @@ mod tests { } /// The caller's expected context is checked against each record's - /// stored one before any key is requested: the chain's - /// `open(record).context(expected)`. + /// stored one before any key is requested. #[tokio::test] async fn an_expected_context_refuses_a_record_stored_under_another() { let cipher = cipher().await; @@ -5336,7 +5318,7 @@ mod tests { /// An expected context is for a plan that takes its context from a /// field. For one with a context of its own it is a second source, - /// refused as the chain refuses it. + /// and it is refused. #[tokio::test] async fn an_expected_context_on_a_plan_with_its_own_is_two_sources() { let cipher = cipher().await; @@ -5357,7 +5339,7 @@ mod tests { )); } - /// `.extend(..)` extends the context the field supplies, as it + /// An extension extends the context the field supplies, as it /// extends a plan's own: the term is the probe under /// `<tenant>/age` then the part. #[tokio::test] @@ -5385,8 +5367,8 @@ mod tests { let stored = term_bytes(&node(&mut map(node(&mut fields, "age")), "eq")); assert_eq!( stored, - chain_term(&cipher, "tenants/acme", 34, &[7]).await, - "the chain's term under tenants/acme, extended by 7" + typed_term(&cipher, "tenants/acme", 34, &[7]).await, + "the typed term under tenants/acme, extended by 7" ); let sealed = seal(&keyset, source(), &plan).await; let opened = object(open(&cipher, sealed, &plan).await); @@ -5399,7 +5381,7 @@ mod tests { /// The context field may be declared after the fields it keys: the /// lowering reads it by name wherever the plan puts it, the terms - /// are the chain's, and the record comes back in plan order. + /// are the typed terms, and the record comes back in plan order. #[tokio::test] async fn the_context_field_may_be_declared_last() { let cipher = cipher().await; @@ -5429,8 +5411,8 @@ mod tests { let stored = term_bytes(&node(&mut map(node(&mut fields, "age")), "eq")); assert_eq!( stored, - chain_term(&cipher, "tenants/acme", 34, &[]).await, - "the chain's term under tenants/acme" + typed_term(&cipher, "tenants/acme", 34, &[]).await, + "the typed term under tenants/acme" ); let sealed = seal(&keyset, source(), &plan).await; @@ -6622,8 +6604,8 @@ mod tests { let column = column(name, label)?; let stored_label = label.to_string(); let context = AeadContext::from(parts.clone().under(column)); - Ok(keyset - .encrypt_as::<String, StackCipherText>(&text, context) + Ok(text + .encrypt_into_with_context::<StackCipherText, K>(keyset, context) .try_map(move |sealed| { let CipherText::Single(leaf) = sealed else { return Err(crate::Error::UnsupportedShape); @@ -6689,8 +6671,11 @@ mod tests { let text = text_of_value(name, plaintext)?; let column = column(name, label)?; let stored_label = label.to_string(); - Ok(keyset - .encrypt_as::<String, EqualityTerm>(&text, scopes.clone().under(column)) + Ok(text + .encrypt_into_with_context::<EqualityTerm, K>( + keyset, + scopes.clone().under(column), + ) .try_map(move |term| { serde_json::to_vec(&serde_json::json!({ "v": 3, @@ -7643,8 +7628,9 @@ mod tests { CallerContext::from(crate::target::Scope::new(part)) .extend(NonEmpty::from(Label::parse("users/email").unwrap())) }; - let direct = keyset - .encrypt_as::<String, EqualityTerm>(&"ada@example.com".to_owned(), under(7)) + let direct: EqualityTerm = "ada@example.com" + .to_owned() + .encrypt_into_with_context(&keyset, under(7)) .await .unwrap(); let seven = term(7).await; @@ -7948,7 +7934,7 @@ mod tests { mod given_the_leaf_encoding { use super::*; - #[derive(EncryptFrom, DecryptInto)] + #[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32, crate = "crate")] struct Age { c: StackCipherText, @@ -8065,8 +8051,11 @@ mod tests { let cipher = cipher().await; let keyset = cipher.default_keyset(); let label_ = Label::parse("users/age").expect("label"); - let derived: Age = keyset - .encrypt_as(&34u32, CallerContext::from(NonEmpty::from(label_.clone()))) + let derived: Age = 34u32 + .encrypt_into_with_context( + &keyset, + CallerContext::from(NonEmpty::from(label_.clone())), + ) .await .expect("derive"); @@ -8083,13 +8072,13 @@ mod tests { "the same equality term" ); - let as_u32: u32 = keyset - .decrypt_as( - node(&mut age, "c"), - AeadContext::from(NonEmpty::from(label_.clone())), - ) - .await - .expect("the derive opens the lowering's leaf"); + let as_u32 = u32::decrypt_from_with_context( + node(&mut age, "c"), + &keyset, + AeadContext::from(NonEmpty::from(label_.clone())), + ) + .await + .expect("the derive opens the lowering's leaf"); assert_eq!(as_u32, 34); let stored = CipherText::Map(vec![( "age".to_string(), @@ -8460,7 +8449,7 @@ mod tests { } } - /// Each refusal from the plan builder maps to the field it names and + /// Each plan refusal maps to the field it names and /// the reason a binding reports: the lowering checks most of these /// first, so not every one is reachable through [`plan`]. #[test] diff --git a/packages/stack-encrypt/src/dynamic/target.rs b/packages/stack-encrypt/src/dynamic/target.rs index 2809bc4818..2ea1d5beff 100644 --- a/packages/stack-encrypt/src/dynamic/target.rs +++ b/packages/stack-encrypt/src/dynamic/target.rs @@ -9,8 +9,8 @@ //! target field, and [`NoTargets`], the resolver of a build without EQL //! types, which refuses every name. //! -//! The resolver runs the named type's own plan (its `EncryptFrom` / -//! `DecryptInto`) and hands back the engine's [`Pending`], so a target field +//! The resolver runs the named type's own declaration (its `EncryptTarget` / +//! `DecryptSource`) and hands back the engine's [`Pending`], so a target field //! settles in the same ZeroKMS request as the rest of the record: the //! lowering zips it in. Nothing here derives a term or seals a byte; the //! resolver reaches the engine through the typed call, as any Rust caller diff --git a/packages/stack-encrypt/src/dynamic/term.rs b/packages/stack-encrypt/src/dynamic/term.rs index 351b763041..295f30aab1 100644 --- a/packages/stack-encrypt/src/dynamic/term.rs +++ b/packages/stack-encrypt/src/dynamic/term.rs @@ -869,9 +869,10 @@ fn lifted(error: Error) -> crate::Error { /// `operation` is [`term`]'s derivation wrapped as a description, which /// walks the frame in place to its one scalar, so a field lowered from data /// runs through the same `indexed()` and `zip` every other field does, and -/// its term is a [`TermBytes`]. A Rust chain over `ValueFrame` fields names -/// its indexes this way too (`(IndexSpec::Equality, IndexSpec::Ore)`), and is -/// then the same declaration as a data plan's. +/// its term is a [`TermBytes`]. Rust code that runs the combinators over a +/// `ValueFrame` names its indexes this way too +/// (`indexed((IndexSpec::Equality, IndexSpec::Ore))`), and is then the same +/// declaration as a data plan's. /// /// A value the index does not take (a container, a boolean under ORE) /// fails the description when it runs, with the dynamic [`Error::Term`] diff --git a/packages/stack-encrypt/src/dynamic/value_frame.rs b/packages/stack-encrypt/src/dynamic/value_frame.rs index 7bb0e83da4..a8099510b8 100644 --- a/packages/stack-encrypt/src/dynamic/value_frame.rs +++ b/packages/stack-encrypt/src/dynamic/value_frame.rs @@ -404,8 +404,8 @@ impl PartialEq for ValueFrame { } /// A text value read as a plan's context: what a data plan's -/// `"context_field"` holds (`"tenants/acme"`), parsed as a Rust chain's -/// `String` context field is ([`Label::parse`]). Anything else is +/// `"context_field"` holds (`"tenants/acme"`), parsed as a label +/// ([`Label::parse`]). Anything else is /// [`LabelError::NotText`]. The frame is read in place: a record's context /// field is read once per sealed field, and nothing is copied for it. impl IntoLabel for &ValueFrame { diff --git a/packages/stack-encrypt/src/json/derive.rs b/packages/stack-encrypt/src/json/derive.rs index 5144df0552..cba5e0eb67 100644 --- a/packages/stack-encrypt/src/json/derive.rs +++ b/packages/stack-encrypt/src/json/derive.rs @@ -36,7 +36,7 @@ use crate::keyset::KeysetState; use crate::sem::TermError; use crate::target::{AeadContext, Decryption, Pending, Request, RequiredContext}; use crate::ContextPiece; -use crate::{DecryptInto, DecryptSource, Descriptor, Error, KeysetCipher, NonEmpty, StackCipher}; +use crate::{DecryptSource, Descriptor, Error, KeysetCipher, NonEmpty, StackCipher}; /// PAE domain of the path selector `s`. const SELECTOR_DOMAIN: &[u8] = b"stack-encrypt/json/selector/v1"; @@ -1216,8 +1216,8 @@ fn take_label(mut node: Opened) -> (Label, Opened) { /// The value the caller asked for, out of the `Protected` that held it: /// the one place an opened JSON plaintext is unwrapped. -fn hand_over(value: Protected<PlainJson>) -> Value { - value.risky_unwrap().into_value() +fn hand_over(value: Protected<PlainJson>) -> JsonPlaintext { + JsonPlaintext::from(value.risky_unwrap().into_value()) } /// What opening a document's entries under the caller's context needs @@ -1256,7 +1256,7 @@ fn open_document<'a, K>( doc: JsonDocument, cipher: &'a StackCipher<K>, context: AeadContext, -) -> Pending<'a, Value, K> { +) -> Pending<'a, JsonPlaintext, K> { // The key and the AAD derive under the stored context, the scopes then // the bindings; the token chain and the selectors under the query // context, the scopes alone, as they were sealed (`seal_pending`). So a @@ -1295,7 +1295,7 @@ fn open_sealed_root( doc: &JsonDocument, aes: &Aes256Cipher, aad: &Context<'_>, -) -> Result<Value, Error> { +) -> Result<JsonPlaintext, Error> { let root = doc .entries .first() @@ -1313,7 +1313,7 @@ fn open_whole_chain( aes: &Aes256Cipher, aad: &Context<'_>, selectors: Selectors, -) -> Result<Value, Error> { +) -> Result<JsonPlaintext, Error> { let root = JsonPath::root(); let token = prf16( &selectors.keyset.prf, @@ -1365,13 +1365,6 @@ fn open_whole_chain( .ok_or_else(|| shape("a document whose chain reaches its root")) } -impl DecryptInto<Value> for JsonDocument { - type Context = AeadContext; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<Value, K> { - Decryption::from_open(move |cipher| open_document(self, cipher, context)) - } -} - /// The whole document, as a [`JsonPlaintext`], which is wiped when it is /// dropped. The context is required: it is the context the document was /// sealed under. @@ -1379,9 +1372,7 @@ impl DecryptSource<JsonPlaintext> for JsonDocument { type ContextInput = RequiredContext<AeadContext>; fn decryption<K: 'static>(self, context: Self::ContextInput) -> Decryption<JsonPlaintext, K> { let context = context.into_inner(); - Decryption::from_open(move |cipher| { - open_document(self, cipher, context).map(JsonPlaintext::from) - }) + Decryption::from_open(move |cipher| open_document(self, cipher, context)) } } @@ -1417,62 +1408,42 @@ fn wrong(expected: &'static str) -> Error { /// sel)`. Every element of a wildcard path is reached through all its /// matches instead (`json_tree_path_query`, or a [`JsonSubtree`] read, /// which opens them in document order by their labels). -impl DecryptInto<Value> for JsonExtracted { - type Context = AeadContext; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<Value, K> { - open_extracted(self, context) - } -} - -/// The scalar the entry holds, or the whole document for a sealed root -/// asked for at `$`, as a [`JsonPlaintext`], which is wiped when it is -/// dropped. The refusals are those of [`DecryptInto<Value>`], above. The -/// context is required: it is the context the document was sealed under. impl DecryptSource<JsonPlaintext> for JsonExtracted { type ContextInput = RequiredContext<AeadContext>; fn decryption<K: 'static>(self, context: Self::ContextInput) -> Decryption<JsonPlaintext, K> { - open_extracted(self, context.into_inner()).map(JsonPlaintext::from) - } -} - -/// Open the entry `->` returned, once it is shown to carry the selector -/// asked for. Shared by the two openings above, so they agree on every -/// refusal. -fn open_extracted<K: 'static>( - extracted: JsonExtracted, - context: AeadContext, -) -> Decryption<Value, K> { - let (node, selector) = extracted.into_parts(); - if node.entry.s != selector { - return Decryption::failed(wrong("the entry at the path asked for")); - } - Decryption::from_open(move |cipher| { - let (request, aad) = match key_retrieval(&node.header, context.into_context()) { - Ok(parts) => parts, - Err(error) => return Pending::failed(cipher, error), - }; - Pending::request(cipher, vec![request], move |responses| { - let key = responses.next_retrieved_key()?; - let aes = leaf_cipher(&key)?; - let opened = open_entry( - &aes, - &node.header, - &aad, - &node.entry, - node.slot, - &cascade::no_children(), - ) - .map_err(|error| match error { - Error::Aead => Error::JsonNotALeaf, - other => other, - })?; - opened - .whole - .or(opened.scalar) - .map(hand_over) - .ok_or_else(|| shape("a JSON scalar, or a sealed root")) + let context = context.into_inner(); + let (node, selector) = self.into_parts(); + if node.entry.s != selector { + return Decryption::failed(wrong("the entry at the path asked for")); + } + Decryption::from_open(move |cipher| { + let (request, aad) = match key_retrieval(&node.header, context.into_context()) { + Ok(parts) => parts, + Err(error) => return Pending::failed(cipher, error), + }; + Pending::request(cipher, vec![request], move |responses| { + let key = responses.next_retrieved_key()?; + let aes = leaf_cipher(&key)?; + let opened = open_entry( + &aes, + &node.header, + &aad, + &node.entry, + node.slot, + &cascade::no_children(), + ) + .map_err(|error| match error { + Error::Aead => Error::JsonNotALeaf, + other => other, + })?; + opened + .whole + .or(opened.scalar) + .map(hand_over) + .ok_or_else(|| shape("a JSON scalar, or a sealed root")) + }) }) - }) + } } /// Open the rows of a read into the values of the nodes its tokens named, @@ -1513,10 +1484,21 @@ fn one_reached( /// [`Error::JsonWildcardTruncated`], never a partial list. Every row is /// checked against the key first ([`JsonSubtreeKey::open`]); a row that /// does not fit is [`Error::JsonWrongEntry`]. -impl DecryptInto<Vec<Value>> for JsonSubtree { - type Context = AeadContext; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<Vec<Value>, K> { - open_every(self, context) +/// +/// Each value is a [`JsonPlaintext`], which is wiped when it is dropped. +/// The context is required: it is the context the document was sealed +/// under. +impl DecryptSource<Vec<JsonPlaintext>> for JsonSubtree { + type ContextInput = RequiredContext<AeadContext>; + fn decryption<K: 'static>( + self, + context: Self::ContextInput, + ) -> Decryption<Vec<JsonPlaintext>, K> { + let context = context.into_inner(); + Decryption::from_open(move |cipher| { + open_reads(self, cipher, context) + .map(|values| values.into_iter().map(hand_over).collect()) + }) } } @@ -1527,94 +1509,38 @@ impl DecryptInto<Vec<Value>> for JsonSubtree { /// than one node (a wildcard that reached several) is /// [`Error::OpenedType`]; a probe that named a node is /// [`Error::JsonWildcardTruncated`]. -impl DecryptInto<Option<Value>> for JsonSubtree { - type Context = AeadContext; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<Option<Value>, K> { - open_at_most_one(self, context) - } -} - -/// Open the rows of a read into the value at the path: an object, an array -/// or a scalar, empty containers included, as `jsonb` holds them there. No -/// node at the path is [`Error::OpenedType`]; open as `Option<Value>` to -/// read that as `None`. More than one node is [`Error::OpenedType`] too, -/// and opens through [`DecryptInto<Vec<Value>>`] instead; a probe that -/// named a node is [`Error::JsonWildcardTruncated`]. -impl DecryptInto<Value> for JsonSubtree { - type Context = AeadContext; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<Value, K> { - open_one(self, context) - } -} - -/// The rows of a read as a list of [`JsonPlaintext`]s, each wiped when it -/// is dropped: one value per concrete path the key named that the row has, -/// in document order. The answers and the refusals are those of -/// [`DecryptInto<Vec<Value>>`], above. The context is required: it is the -/// context the document was sealed under. -impl DecryptSource<Vec<JsonPlaintext>> for JsonSubtree { - type ContextInput = RequiredContext<AeadContext>; - fn decryption<K: 'static>( - self, - context: Self::ContextInput, - ) -> Decryption<Vec<JsonPlaintext>, K> { - open_every(self, context.into_inner()) - .map(|values| values.into_iter().map(JsonPlaintext::from).collect()) - } -} - -/// The value at the path as a [`JsonPlaintext`], or `None` where the row -/// has no node there. The answers and the refusals are those of -/// [`DecryptInto<Option<Value>>`], above. impl DecryptSource<Option<JsonPlaintext>> for JsonSubtree { type ContextInput = RequiredContext<AeadContext>; fn decryption<K: 'static>( self, context: Self::ContextInput, ) -> Decryption<Option<JsonPlaintext>, K> { - open_at_most_one(self, context.into_inner()).map(|value| value.map(JsonPlaintext::from)) + let context = context.into_inner(); + Decryption::from_open(move |cipher| { + open_reads(self, cipher, context) + .try_map(|values| Ok(one_reached(values)?.map(hand_over))) + }) } } -/// The value at the path as a [`JsonPlaintext`], which is wiped when it is -/// dropped. No node at the path is [`Error::OpenedType`]: open as an -/// `Option<JsonPlaintext>` to read that as `None`. The answers and the -/// refusals are those of [`DecryptInto<Value>`], above. +/// Open the rows of a read into the value at the path: an object, an array +/// or a scalar, empty containers included, as `jsonb` holds them there. No +/// node at the path is [`Error::OpenedType`]; open as an +/// `Option<JsonPlaintext>` to read that as `None`. More than one node is +/// [`Error::OpenedType`] too, and opens as a `Vec<JsonPlaintext>` instead; a +/// probe that named a node is [`Error::JsonWildcardTruncated`]. impl DecryptSource<JsonPlaintext> for JsonSubtree { type ContextInput = RequiredContext<AeadContext>; fn decryption<K: 'static>(self, context: Self::ContextInput) -> Decryption<JsonPlaintext, K> { - open_one(self, context.into_inner()).map(JsonPlaintext::from) - } -} - -/// Every value the read reached, in the key's order. -fn open_every<K: 'static>(subtree: JsonSubtree, context: AeadContext) -> Decryption<Vec<Value>, K> { - Decryption::from_open(move |cipher| { - open_reads(subtree, cipher, context) - .map(|values| values.into_iter().map(hand_over).collect()) - }) -} - -/// The one value the read reached, or none. -fn open_at_most_one<K: 'static>( - subtree: JsonSubtree, - context: AeadContext, -) -> Decryption<Option<Value>, K> { - Decryption::from_open(move |cipher| { - open_reads(subtree, cipher, context) - .try_map(|values| Ok(one_reached(values)?.map(hand_over))) - }) -} - -/// The one value the read reached. None is a refusal. -fn open_one<K: 'static>(subtree: JsonSubtree, context: AeadContext) -> Decryption<Value, K> { - Decryption::from_open(move |cipher| { - open_reads(subtree, cipher, context).try_map(|values| { - one_reached(values)? - .map(hand_over) - .ok_or_else(|| shape("a node at the walked path")) + let context = context.into_inner(); + Decryption::from_open(move |cipher| { + open_reads(self, cipher, context).try_map(|values| { + one_reached(values)? + .map(hand_over) + .ok_or_else(|| shape("a node at the walked path")) + }) }) - }) + } } /// One row of a read as the server returned it, not yet opened: the entry, @@ -3132,7 +3058,14 @@ mod tests { let opened = decode_node(&plain).unwrap(); assert!(matches!(opened.label, Label::Root)); assert_eq!(opened.kind, Kind::Object); - assert_eq!(opened.whole.map(hand_over), Some(value)); + assert_eq!( + opened + .whole + .map(hand_over) + .as_ref() + .map(JsonPlaintext::value), + Some(&value) + ); } #[test] @@ -3316,6 +3249,7 @@ mod tests { ) .unwrap(); open_whole_chain(doc, &aes, &context, Selectors::for_tests()) + .map(|opened| opened.value().clone()) } #[test] diff --git a/packages/stack-encrypt/src/json/mod.rs b/packages/stack-encrypt/src/json/mod.rs index 624691ec0d..e4d4cfec50 100644 --- a/packages/stack-encrypt/src/json/mod.rs +++ b/packages/stack-encrypt/src/json/mod.rs @@ -53,7 +53,7 @@ //! selectors, tokens and grouping), which a query is built from. Sealing is //! a separate operation, [`json_document`](crate::target::json_document), //! whose output is the stored [`JsonDocument`]; `JsonDocument` is itself an -//! [`EncryptFrom<Value>`](crate::EncryptFrom) target, so a plan or a record +//! [`EncryptTarget<JsonPlaintext>`](crate::EncryptTarget), so a record //! names it by type. The query terms are [`JsonQuery`] (containment and //! `AtPath<Eq>`), [`JsonLeafQuery`] (ordering at a path), and the two //! extraction keys a caller derives through the keyset cipher, @@ -129,7 +129,7 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; use vitaminc_protected::OpaqueDebug; -use crate::target::{DecryptField, DecryptInto, Decryptable, Decryption}; +use crate::target::{DecryptField, DecryptSource, Decryptable, Decryption}; mod cascade; mod derive; @@ -458,10 +458,9 @@ impl JsonEntry { /// A stored JSON document: its [`JsonHeader`] and its entries, one per /// node, in a random order drawn for the document when it was sealed, with /// the root first. What -/// [`json_document`](crate::target::json_document) produces and -/// [`DecryptInto<Value>`](crate::DecryptInto) opens. +/// [`json_document`](crate::target::json_document) produces. /// -/// With the owned API, a [`JsonPlaintext`] encrypts into a document +/// A [`JsonPlaintext`] encrypts into a document /// ([`EncryptTarget<JsonPlaintext>`](crate::EncryptTarget)), and the /// document decrypts to one /// ([`DecryptSource<JsonPlaintext>`](crate::DecryptSource)). A @@ -630,10 +629,10 @@ impl JsonDocument { impl Decryptable for JsonDocument { const DECRYPTABLE: bool = true; } -impl<P: 'static, Ctx> DecryptField<P, Ctx> for JsonDocument +impl<P: zeroize::Zeroize + 'static, Ctx> DecryptField<P, Ctx> for JsonDocument where - Self: crate::DecryptInto<P>, - Ctx: Into<<Self as crate::DecryptInto<P>>::Context>, + Self: DecryptSource<P>, + Ctx: Into<<Self as DecryptSource<P>>::ContextInput>, { fn decryption_field<K: 'static>(self, context: Ctx) -> Option<Decryption<P, K>> { Some(self.decryption(context.into())) @@ -684,16 +683,14 @@ impl JsonNode { } /// The entry `->` returned, with the selector the client asked for: what -/// [`DecryptInto<Value>`](crate::DecryptInto) opens to the node's scalar, -/// or to the whole document for a sealed root asked for at `$`. Built only +/// [`DecryptSource<JsonPlaintext>`](crate::DecryptSource) opens to the +/// node's scalar, or to the whole document for a sealed root asked for at +/// `$`. The value is wiped when it is dropped. Built only /// by [`JsonSelector::open`], so it always carries the selector the entry /// is checked against; an entry carrying another is refused with /// [`Error::JsonWrongEntry`](crate::Error::JsonWrongEntry) before any key /// is retrieved. /// -/// With the owned API, the entry decrypts to a [`JsonPlaintext`] -/// ([`DecryptSource<JsonPlaintext>`](crate::DecryptSource)), which is wiped -/// when it is dropped. #[derive(Clone, Debug, PartialEq, Eq)] pub struct JsonExtracted { node: JsonNode, @@ -723,10 +720,10 @@ impl JsonExtracted { impl Decryptable for JsonExtracted { const DECRYPTABLE: bool = true; } -impl<P: 'static, Ctx> DecryptField<P, Ctx> for JsonExtracted +impl<P: zeroize::Zeroize + 'static, Ctx> DecryptField<P, Ctx> for JsonExtracted where - Self: crate::DecryptInto<P>, - Ctx: Into<<Self as crate::DecryptInto<P>>::Context>, + Self: DecryptSource<P>, + Ctx: Into<<Self as DecryptSource<P>>::ContextInput>, { fn decryption_field<K: 'static>(self, context: Ctx) -> Option<Decryption<P, K>> { Some(self.decryption(context.into())) @@ -735,18 +732,14 @@ where /// The entries the server returned for one subtree read, ready to open: /// what `json_tree_subtree(doc, key.tokens(), budget)` yields, collected, -/// and what [`DecryptInto<Value>`](crate::DecryptInto), -/// `DecryptInto<Option<Value>>` and `DecryptInto<Vec<Value>>` open into the -/// value at the read's path. Built only by the key that keyed the read +/// and what [`DecryptSource`] opens into the value at +/// the read's path: a [`JsonPlaintext`], an `Option<JsonPlaintext>` or a +/// `Vec<JsonPlaintext>`, each wiped when it is dropped. Built only by the +/// key that keyed the read /// ([`JsonSubtreeKey::open`]), so it always carries the key, and what /// checks every row against it when the rows are opened (see /// [`JsonSubtreeKey::open`] for the checks). /// -/// With the owned API, the rows decrypt to a [`JsonPlaintext`], an -/// `Option<JsonPlaintext>` or a `Vec<JsonPlaintext>` -/// ([`DecryptSource`](crate::DecryptSource)), with the same answers. Each -/// `JsonPlaintext` is wiped when it is dropped. -/// /// The rows are, for each token of the key that names a node, the node at /// depth `0` and everything beneath it, so the four answers `jsonb #>` /// gives are four answers here: an empty object's entry, an empty array's @@ -799,10 +792,10 @@ impl JsonSubtree { impl Decryptable for JsonSubtree { const DECRYPTABLE: bool = true; } -impl<P: 'static, Ctx> DecryptField<P, Ctx> for JsonSubtree +impl<P: zeroize::Zeroize + 'static, Ctx> DecryptField<P, Ctx> for JsonSubtree where - Self: crate::DecryptInto<P>, - Ctx: Into<<Self as crate::DecryptInto<P>>::Context>, + Self: DecryptSource<P>, + Ctx: Into<<Self as DecryptSource<P>>::ContextInput>, { fn decryption_field<K: 'static>(self, context: Ctx) -> Option<Decryption<P, K>> { Some(self.decryption(context.into())) diff --git a/packages/stack-encrypt/src/json/query.rs b/packages/stack-encrypt/src/json/query.rs index fd011731c5..565ec40715 100644 --- a/packages/stack-encrypt/src/json/query.rs +++ b/packages/stack-encrypt/src/json/query.rs @@ -17,8 +17,8 @@ use super::path::JsonPath; use super::plain::PlainJson; use super::{base64, JsonDocument, JsonExtracted, JsonNode, JsonOptions, JsonSubtree, Tag}; use crate::op::{ - Answers, AtPath, Between, ContainedBy, Contains, DefaultIndex, Eq, Gt, Gte, Input, - LabelledTarget, Lt, Lte, One, Op, Pair, Pathed, QueryTerm, QueryToken, Supports, + Answers, AtPath, Between, ContainedBy, Contains, Eq, Gt, Gte, Input, LabelledTarget, Lt, Lte, + One, Op, Pair, Pathed, QueryTerm, QueryToken, Supports, }; use crate::sem::{RangeQuery, TermError}; use crate::target::{DeclaredContext, Index, Json, Owned, Pending}; @@ -396,7 +396,7 @@ impl JsonSelector { } /// The entry `col -> selector` returned, ready to open: the only way to - /// open one. Opening it ([`DecryptInto<Value>`](crate::DecryptInto)) + /// open one. Opening it ([`DecryptSource<JsonPlaintext>`](crate::DecryptSource)) /// first checks that the entry carries this selector, and refuses any /// other with [`Error::JsonWrongEntry`] before a key is retrieved: an /// entry's ciphertext is bound to the selector it carries, so a @@ -739,9 +739,6 @@ macro_rules! containment { impl Answers<$op, Value> for JsonDocument { type Via = Json; } - impl DefaultIndex<Value> for $op { - type Via = Json; - } )+}; } containment!(Contains, ContainedBy); @@ -775,13 +772,6 @@ where { type Via = Json; } -impl<P, V> DefaultIndex<(P, V)> for AtPath<Eq> -where - P: AsRef<str> + 'static, - V: Into<Value> + Clone + 'static, -{ - type Via = Json; -} /// The copy of the caller's plaintext a check reads: wiped when it drops. fn copy_of<V: Into<Value> + Clone>(value: &V) -> Protected<PlainJson> { @@ -851,13 +841,6 @@ macro_rules! orderings { { type Via = Json; } - impl<P, V> DefaultIndex<(P, V)> for AtPath<$op> - where - P: AsRef<str> + 'static, - V: Into<Value> + Clone + 'static, - { - type Via = Json; - } )+}; } orderings!(Lt, Lte, Gt, Gte); @@ -891,13 +874,6 @@ where { type Via = Json; } -impl<P, V> DefaultIndex<(P, (V, V))> for AtPath<Between> -where - P: AsRef<str> + 'static, - V: Into<Value> + Clone + 'static, -{ - type Via = Json; -} /// The options a query derives under must be the stored document's; a /// plan that declares the index carries them, and a query through it takes diff --git a/packages/stack-encrypt/src/lib.rs b/packages/stack-encrypt/src/lib.rs index 469300937c..eee05dfb39 100644 --- a/packages/stack-encrypt/src/lib.rs +++ b/packages/stack-encrypt/src/lib.rs @@ -213,7 +213,8 @@ endpoint — are `StackKmsBuilder`'s, and the two keyset-cache knobs are //! A wrong context loses data silently: a query under the wrong context //! matches nothing, and gives no error. So every declaration and every call //! can show the context it uses, before any key is requested. A derived -//! record has `contexts()`, a [`Plan`] has [`Plan::contexts`], and a +//! record has `contexts()`, a plan that is data has +//! `dynamic::record::Plan::contexts` (with the `dynamic` feature), and a //! prepared call has [`Pending::context`](target::Pending::context). The //! `testing` feature adds `assert_context!`, which pins a context in a //! test. See [`contexts`] for the rendering. A field with a binding shows @@ -521,19 +522,13 @@ endpoint — are `StackKmsBuilder`'s, and the two keyset-cache knobs are //! attribute. The [`target`] module describes the traits a hand-written //! target implements, and how the context of a record field is built. //! -//! The older structural plans and derives (`#[derive(EncryptFrom, -//! DecryptInto)]`) encrypt a borrowed plaintext into a [`StackCipherText`]. -//! A `StackCipherText` is a tree of sealed leaves. The low-level entry -//! points of the structural API are [`KeysetCipher::encrypt_tree`] and -//! [`KeysetCipher::decrypt_tree`]. A declaration runs through -//! [`KeysetCipher::encrypt_as`] and [`KeysetCipher::decrypt_as`]. The -//! structural API is separate from the owned scalar API. +//! A [`StackCipherText`] is a tree of sealed leaves, one for each field of +//! the value. [`KeysetCipher::encrypt_tree`] seals a whole value as a tree, +//! under one context, and [`KeysetCipher::decrypt_tree`] opens it. A derived +//! record's ciphertext fields are trees of the same leaves. //! -//! The owned derives also implement the structural traits. So an owned -//! record can be a field of a structural record, or a typed field of a plan. -//! -//! A struct sealed through the structural API, or as an owned record, is one -//! ciphertext per field. A struct in an [`OpaqueBlob`] is one ciphertext. +//! A struct sealed as a tree, or as a derived record, is one ciphertext per +//! field. A struct in an [`OpaqueBlob`] is one ciphertext. //! //! # What is authenticated //! @@ -557,7 +552,7 @@ endpoint — are `StackKmsBuilder`'s, and the two keyset-cache knobs are //! # Relationship to vitaminc //! //! A `KeysetCipher` seals through a vitaminc `Cipher`; everything vitaminc -//! can encrypt remains available through the structural engine. Its data model, AAD derivations and +//! can encrypt remains available through [`KeysetCipher::encrypt_tree`]. Its data model, AAD derivations and //! the leaf wire format are vitaminc's (`vitaminc_encrypt::Aes256Cipher`, AES-256-GCM under a random //! per-leaf nonce vitaminc generates itself). The ZeroKMS `iv` a [`SealedValue`] //! carries is *not* that nonce: it identifies the data key, and is sent back to @@ -614,7 +609,7 @@ pub use contexts::{ pub use descriptor::{Describe, Description, Descriptor, Label, LabelError}; pub use keyset::KeysetCipher; pub use marker::{BetweenQuery, MarkedQuery, Marker, Needs, QueryInput, Ready, ScopeInput}; -pub use plan::{all, Plan, PlanError}; +pub use plan::PlanError; // stack-kms is a public dependency: `StackCipher` is generic over its // `DataKeySource`, and `StackCipherBuilder::kms` takes its `StackKms`. It is // versioned on its own (release-plz.toml), so a caller reaches it through @@ -627,10 +622,9 @@ pub use stack_kms as kms; /// crate, so a binding encodes an error from any of them the same way. pub use stack_kms::{diagnostic, ErrorPayload}; pub use target::{ - CallerContext, CipherRef, DecryptField, DecryptFrom, DecryptInto, DecryptSource, Decryptable, - Decryption, EncryptFrom, EncryptInto, EncryptTarget, Encrypted, Encryption, Equality, Index, - IndexSpec, Indexes, Json, Match, Ope, Ore, OreBlock, Pending, PendingFuture, Request, - Responses, TermSet, + CallerContext, CipherRef, DecryptField, DecryptFrom, DecryptSource, Decryptable, Decryption, + EncryptInto, EncryptTarget, Encrypted, Encryption, Equality, Index, IndexSpec, Indexes, Json, + Match, Ope, Ore, OreBlock, Pending, PendingFuture, Request, Responses, TermSet, }; // Re-export the vitaminc AEAD surface callers need to drive the cipher, so they @@ -654,11 +648,10 @@ pub use vitaminc_prf::IntoPrfContext; #[allow(deprecated)] pub use vitaminc_prf::PrfContext; -// The nonempty proof used by searchable targets and legacy declarations, -// built with `nonempty!` +// The nonempty proof used by searchable targets, built with `nonempty!` // (a literal, checked at compile time) or `NonEmpty::new` (a runtime value, // checked once); `MaybeEmpty` is what a context type of your own implements -// to be wrapped. `#[derive(EncryptFrom)]` names these through this crate. +// to be wrapped. `#[derive(EncryptTarget)]` names these through this crate. pub use vitaminc_protected::{nonempty, nonempty_bytes, EmptyError, MaybeEmpty, NonEmpty}; // And the wrapper plaintext comes back in (`ValueFrame::into_inner`, a diff --git a/packages/stack-encrypt/src/op/mod.rs b/packages/stack-encrypt/src/op/mod.rs index c66bee90ed..42d31d0069 100644 --- a/packages/stack-encrypt/src/op/mod.rs +++ b/packages/stack-encrypt/src/op/mod.rs @@ -3,28 +3,45 @@ //! A query names an *operator* ([`Eq`](struct@Eq), [`Lt`], [`Lte`], //! [`Gt`], [`Gte`], [`Between`], [`Matches`](struct@Matches), and the //! JSON operators [`Contains`], [`ContainedBy`] and [`AtPath`]), and the -//! plan it runs on finds the index that answers it. The operator is a unit -//! type passed by value, as an index is (`.via(Ore)`), and it decides what -//! the query takes: one value for most, a pair of bounds for `Between`, a -//! path and the leaf operator's input for `AtPath`. The JSON operators are -//! answered by the [`Json`](struct@crate::target::Json) index, over a -//! `serde_json::Value`. +//! field it runs on names the index that answers it. The operator decides +//! what the query takes: one value for most, a pair of bounds for +//! `Between`, a path and the leaf operator's input for `AtPath`. The JSON +//! operators are answered by the [`Json`](struct@crate::target::Json) +//! index, over a `serde_json::Value`. +//! +//! A typed query is a method of a field's [`Marker`](crate::Marker), one +//! method for each operator the field's indexes answer. A query whose +//! declaration is data names the operator by its wire name ([`Op`]), with +//! the `dynamic` feature. //! //! ``` //! # async fn example() -> Result<(), stack_encrypt::Error> { //! use stack_encrypt::kms::FakeDataKeySource; -//! use stack_encrypt::{op, Equality, Match, Ore, Plan, StackCipher}; +//! use stack_encrypt::sem::{EqualityTerm, MatchTerms, OreTerm}; +//! use stack_encrypt::target::EncryptTarget; +//! use stack_encrypt::{Encrypted, StackCipher}; +//! use zeroize::Zeroize; +//! +//! #[derive(Zeroize)] +//! struct User { +//! email: String, +//! age: u32, +//! } +//! +//! #[derive(EncryptTarget)] +//! #[stash(struct = User, scope = "users")] +//! struct EncryptedUser { +//! email: Encrypted<(EqualityTerm, MatchTerms)>, +//! age: Encrypted<OreTerm<u32>>, +//! } //! //! let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; -//! let email_plan = Plan::context("users/email") -//! .with::<String, _>((Equality, Match)) -//! .build()?; -//! let age_plan = Plan::context("users/age").with::<u32, _>(Ore).build()?; +//! let keyset = cipher.default_keyset(); //! -//! let exact = cipher.query(op::Eq, "bob@example.com").on(&email_plan).await?; -//! let search = cipher.query(op::Matches, "bob").on(&email_plan).await?; -//! let under = cipher.query(op::Lt, &40u32).on(&age_plan).await?; -//! let range = cipher.query(op::Between, &(18u32, 65)).on(&age_plan).await?; +//! let exact = EncryptedUser::EMAIL.eq("bob@example.com").encrypt(&keyset).await?; +//! let search = EncryptedUser::EMAIL.matches("bob").encrypt(&keyset).await?; +//! let under = EncryptedUser::AGE.lt(40u32).encrypt(&keyset).await?; +//! let range = EncryptedUser::AGE.between(18u32, 65).encrypt(&keyset).await?; //! # let _ = (exact, search, under, range); //! # Ok(()) //! # } @@ -34,11 +51,15 @@ //! Every operator is declared once, with its type, its arity and its wire //! name ([`Op`], the dynamic vocabulary): `op::Eq`, `Op::Eq` and `"eq"` //! are three faces of one operator. Every index declares the operators it -//! answers once as well (`Equality::OPERATORS`, `Ore::OPERATORS`, …), and -//! a plan reads that declaration: a field declares at most one index per -//! operator, so `(Equality, Ore)` is allowed and `(Ore, Ope)` is refused -//! when the plan is built ([`PlanError::IndexOverlap`](crate::PlanError::IndexOverlap); -//! two match indexes are one index twice, [`PlanError::DuplicateIndex`](crate::PlanError::DuplicateIndex)). +//! answers once as well (`Equality::OPERATORS`, `Ore::OPERATORS`, …). A +//! field declares at most one index per operator, so `(Equality, Ore)` is +//! allowed and `(Ore, Ope)` is not. A typed field that declares two is +//! refused when it is encrypted, before any key is requested +//! ([`PlanError::IndexOverlap`](crate::PlanError::IndexOverlap); two match +//! indexes are one index twice, +//! [`PlanError::DuplicateIndex`](crate::PlanError::DuplicateIndex)). Its +//! marker has no method for the operator. A plan that is data refuses such +//! a field when it is parsed. //! //! Three traits connect an operator to an index: //! @@ -54,24 +75,21 @@ //! - [`Answers<Op, In>`] is on a target type, a domain type such as an EQL //! type, and names the index that answers `Op` for it. It is written by //! hand per type, because the choice decides what the query term reveals. -//! - [`DefaultIndex<In>`] is on an operator, and names the index a plan that -//! declares its indexes, rather than a target type, answers it with. //! //! What a query takes is the operator's [`Arity`]: [`One`] plaintext, a //! [`Pair`] of them as a tuple, or for an accessor a path and its leaf //! operator's input ([`Pathed<A>`](Pathed), where `A` is the leaf's arity). //! [`Input<A>`] says, for the value a query is given, which plaintext the -//! index is declared over (`S` for `(S, S)`), so the plan's type is checked +//! index is declared over (`S` for `(S, S)`), so the field's type is checked //! against it. An input of a `Pathed` arity is a path and the leaf's input, //! `("$.address.city", "Sydney")`, over the JSON index's document. //! //! What the types can see is checked at compile time: a match query on an -//! integer, an ordering on bytes, an operator a target type does not answer, -//! and, on a one-value plan, an index the plan does not hold. A field of a -//! fields plan is data, so there an index the field does not declare, or -//! declares with other options, is refused when the query is awaited, +//! integer, an ordering on bytes, and an operator a target type does not +//! answer. A field of a plan that is data is data, so there an operator no +//! index of the field answers is refused when the query plan is made, //! before any key is requested. So are the values a query refuses, which -//! the `sem` module states once for the typed chain and the dynamic path: +//! the `sem` module states once for the typed query and the dynamic path: //! match text that cuts no token under the index's tokenizer, whose filter //! would match every row ([`TermError::EmptyTermText`]), and text an //! equality or order index cannot normalize. The stored side has no @@ -147,7 +165,7 @@ pub struct Gt; pub struct Gte; /// A range: rows whose value is between the query's two bounds, inclusive -/// at both ends. It takes a [`Pair`], `cipher.query(op::Between, &(lo, hi))`, +/// at both ends. It takes a [`Pair`], as a marker's `between(lo, hi)` does, /// derives both bounds under one context, and sends them as one term /// ([`RangeQuery`]), so no statement can pair the /// low bound of one query with the high bound of another. One value is @@ -435,7 +453,7 @@ impl fmt::Display for Op { /// How many plaintexts a query for an operator takes: [`One`] or a /// [`Pair`], or for an accessor a path and its leaf's input ([`Pathed`]). -/// Sealed: the chain derives a term per plaintext and joins them as the +/// Sealed: a query derives a term per plaintext and joins them as the /// arity says, so there are exactly these. pub trait Arity: sealed::Sealed + 'static {} @@ -468,13 +486,13 @@ impl LeafArity for Pair {} /// What a query of arity `A` is given, and the plaintext its index is /// declared over: an `S` is an input of [`One`] over `S`, and an `(S, S)` -/// an input of [`Pair`] over `S`. The chain's value type is the input, and -/// the plan is checked against the plaintext. +/// an input of [`Pair`] over `S`. A query's value type is the input, and +/// the field's index is checked against the plaintext. /// /// A `(P, V)` is an input of [`Pathed<One>`] over a `serde_json::Value`, /// and a `(P, (V, V))` of [`Pathed<Pair>`]: the path and the value become /// the document that holds the value at the path, and the JSON index's -/// terms of it. The arity decides how the chain derives terms from the +/// terms of it. The arity decides how a query derives terms from the /// input, which is what [`derive`](Self::derive) does. #[diagnostic::on_unimplemented( message = "`{Self}` is not what a query of arity `{A}` takes", @@ -488,7 +506,7 @@ pub trait Input<A: Arity>: Sized + 'static { type Terms<T: 'static>: 'static; /// Derive the index's term for each plaintext of the input under - /// `label`, as one pending. Only a query chain calls this. + /// `label`, as one pending. Only a field marker's query calls this. #[doc(hidden)] fn derive<'a, 'p, I, K>( self, @@ -755,8 +773,8 @@ pub trait Supports<Op: Operator, In: Input<Op::Arity>>: Index<In::Plaintext> { /// before the input is consumed; an index that refuses nothing and /// keeps nothing returns `Ok(())`. /// - /// A query chain calls this once, when the query is awaited, before any - /// keyset is resolved, and keeps the verifier until the term is + /// A field marker's query calls this once, when it is encrypted, before + /// any key is requested, and keeps the verifier until the term is /// derived. So a verifier exists only for an input the index accepted, /// and a refused query costs no key request. The refusals are the /// `sem` module's, one function per index, which the dynamic path @@ -776,14 +794,14 @@ pub trait Supports<Op: Operator, In: Input<Op::Arity>>: Index<In::Plaintext> { Ok(()) } /// The query term, from the verifier and the terms the index derived - /// for the input's plaintexts. Only a query chain calls this: + /// for the input's plaintexts. Only a field marker's query calls this: /// `QueryToken` cannot be made outside this crate, so no caller can /// pair one query's tokens with another value's filter, or pass a /// stored term off as a query. /// /// The token carries the context the terms were derived under /// ([`QueryToken::label`], [`QueryToken::is_extended`]), for a query - /// term that records it beside its terms. The chain calls this only + /// term that records it beside its terms. The query calls this only /// after [`check_label`](Self::check_label) accepted that context, so a /// term built from it need not refuse it again. fn query( @@ -793,7 +811,7 @@ pub trait Supports<Op: Operator, In: Input<Op::Arity>>: Index<In::Plaintext> { ) -> Self::Query; } -/// Proof that [`Supports::query`] is called by a query chain, and the +/// Proof that [`Supports::query`] is called by a field marker's query, and the /// context the query's terms were derived under. Only this crate can make /// one, so an index in another crate can implement [`Supports`] but no /// caller can turn a term into a query term by hand, or name a context its @@ -924,21 +942,6 @@ macro_rules! orders { } orders!(Ore => OreTerm, block: false, Ope => OpeTerm, block: false, OreBlock => OreBlockTerm, block: true); -/// The ordering operators default to the ORE index; an OPE or block ORE -/// index is named with `.via(Ope)` or `.via(OreBlock)`. -macro_rules! default_ore { - ($($op:ident),+ $(,)?) => {$( - impl<S: Ordered> DefaultIndex<S> for $op { - type Via = Ore; - } - )+}; -} -default_ore!(Lt, Lte, Gt, Gte); - -impl<S: Ordered> DefaultIndex<(S, S)> for Between { - type Via = Ore; -} - /// A target type that answers the operator `Op` for the input `In`, and /// the index it answers it with. /// @@ -950,13 +953,13 @@ impl<S: Ordered> DefaultIndex<(S, S)> for Between { /// the one term that answers the operator ([`OperatorAnswer`]), and an /// `Option` answers what a present value answers. /// -/// To be queried on a typed plan (`Plan::value::<S>().encrypt_into::<T>()`), -/// the type must also implement [`LabelledTarget`], whatever context it is -/// sealed under: an `Answers` impl alone answers no query. +/// To be queried from a field's marker, the type must also implement +/// [`LabelledTarget`], whatever context it is sealed under: an `Answers` +/// impl alone answers no query. #[diagnostic::on_unimplemented( message = "`{Self}` does not answer `{Op}` given `{In}`", label = "this target type does not answer the operator", - note = "a target answers the operators its type implements `Answers` for, and a tuple of terms answers each operator exactly one of its terms answers; on a plan's query chain, `.via(index)` names an index the target declares" + note = "a target answers the operators its type implements `Answers` for, and a tuple of terms answers each operator exactly one of its terms answers" )] pub trait Answers<Op: Operator, In: Input<Op::Arity>> { /// The index that answers `Op` for this type. An index is a unit type, @@ -965,9 +968,8 @@ pub trait Answers<Op: Operator, In: Input<Op::Arity>> { } /// A target type whose stored terms were derived under the context a -/// query's label names, so a plan for it -/// (`Plan::value::<S>().encrypt_into::<T>()`) answers the operators its -/// [`Answers`] impls name, with each query derived under the plan's label. +/// query's label names, so a field of that type answers the operators its +/// [`Answers`] impls name, with each query derived under the field's label. /// /// Implementing it is a claim about the stored values: a query term derived /// under the label equals the stored term only if the stored term was @@ -979,17 +981,17 @@ pub trait Answers<Op: Operator, In: Input<Op::Arity>> { /// v4 type is sealed under the column it stores, which a two-segment label /// names, and its index refuses any other label /// ([`Supports::check_label`]). A type sealed under a context no label -/// names, such as an integer tenant id, must not implement it: a query on -/// its plan would match nothing, with no error. +/// names, such as an integer tenant id, must not implement it: a query of +/// a field of that type would match nothing, with no error. /// -/// A query on such a plan derives through the index alone and never runs -/// the target's own encryption, so the target need not accept the label as -/// its context: an EQL type, sealed under a column identifier, cannot be -/// encrypted through its plan and is queried through it all the same. +/// A query derives through the index alone and never runs the target's own +/// encryption, so the target need not accept the label as its context: an +/// EQL type, sealed under a column identifier, is queried under the label +/// of the same two names. #[diagnostic::on_unimplemented( - message = "`{Self}` is not queried under a plan's label", - label = "a typed plan for this target type answers no query", - note = "a target type answers queries on its plan when it implements `op::LabelledTarget`: its stored terms were derived under the context the plan's label names" + message = "`{Self}` is not queried under a field's label", + label = "a field of this target type answers no query", + note = "a target type answers queries when it implements `op::LabelledTarget`: its stored terms were derived under the context the field's label names" )] pub trait LabelledTarget {} @@ -1064,8 +1066,9 @@ impl<T: LabelledTarget> LabelledTarget for Option<T> {} /// /// When no term of a tuple answers `Op`, the tuple does not answer it. /// When two do, it does not either: which one a query used would decide -/// what the query reveals. A plan refuses such a field -/// ([`PlanError::IndexOverlap`](crate::PlanError::IndexOverlap)). +/// what the query reveals. Such a field is refused when it is encrypted +/// ([`PlanError::IndexOverlap`](crate::PlanError::IndexOverlap)), and a +/// plan that is data refuses it when it is parsed. /// /// Implemented for this crate's terms and the operators [`Eq`](struct@Eq), /// [`Lt`], [`Lte`], [`Gt`], [`Gte`], [`Between`] and @@ -1197,44 +1200,14 @@ mod term_tuples { } } -/// The index a plan that declares its indexes answers an operator with: -/// a field of a fields plan, or a one-value plan built `with` indexes. -/// -/// A plan that declares a different index for the operator, such as a -/// match index with its own configuration, names it with `.via(index)`. -/// So does a typed field of a fields plan (`encrypt_into::<T, _>(name)`): -/// the field's type is not known where the query is written, so its -/// [`Answers`] impl is not consulted, and a field whose type answers -/// through another index needs `via`. -#[diagnostic::on_unimplemented( - message = "`{Self}` has no default index given `{In}`", - label = "no default index answers this operator here", - note = "`op::Eq` defaults to `Equality`; `op::Matches` defaults to `Match` with the default configuration, which is case-insensitive and over text only; `op::Lt`, `Lte`, `Gt`, `Gte` and `Between` default to `Ore` over an integer, and `Between` takes a pair; name another index, such as `Ope`, with `.via(index)`; `op::Contains`, `op::ContainedBy` and `op::AtPath` default to `Json` over a `serde_json::Value`" -)] -pub trait DefaultIndex<In: Input<Self::Arity>>: Operator { - /// The index. An index is a unit type, so its `Default` is the index. - type Via: Supports<Self, In> + Default; -} - -impl<S> DefaultIndex<S> for Eq -where - S: crate::Encrypt + Clone + 'static, -{ - type Via = Equality; -} - -impl<S: AsRef<str> + 'static> DefaultIndex<S> for Matches { - type Via = Match; -} - #[cfg(test)] mod tests { use super::*; use crate::sem::DefaultMatch; /// A match index's verifier is the query's tokens: a query built from - /// it verifies a row that holds them and no other. A query chain takes - /// them through `prepare`, once. + /// it verifies a row that holds them and no other. A field marker's + /// query takes them through `prepare`, once. #[test] fn a_match_verifier_is_the_query_tokens() { let index = Match::<DefaultMatch>::default(); diff --git a/packages/stack-encrypt/src/plan/build.rs b/packages/stack-encrypt/src/plan/build.rs deleted file mode 100644 index 2559e0227b..0000000000 --- a/packages/stack-encrypt/src/plan/build.rs +++ /dev/null @@ -1,1840 +0,0 @@ -//! Saved fields plans: the chain without the value, validated once at -//! `build()`, and how each lowers to the combinators in -//! [`target`](crate::target). -use std::any::{type_name, TypeId}; -use std::fmt; -use std::marker::PhantomData; -use std::sync::Arc; - -use super::field_ref::{resolve, Access, FieldRef, FieldsOf, Reader}; -use super::values::Slot; -use super::{FieldValues, PlanError}; -use crate::target::{ - ciphertext, indexed, inspect, open, passthrough, AeadContext, Borrowed, CallerContext, - DeclaredContext, DecryptField, Decryptable, Decryption, EncryptFrom, EncryptTarget, Encryption, - ExpectedContext, IndexSpec, Indexes, Pending, -}; -use crate::{Error, KeysetCipher, Label, LabelError, NonEmpty, StackCipherText}; - -/// A context a plan is declared under: a [`Label`], or text parsed as one. -/// -/// A plan's context is a name, so it is a label of plain segments: -/// `"users"`, or `"documents/v2/body"`, which is three segments and renders -/// in the ZeroKMS log exactly as written. Text that is not a plain label is -/// kept and refused when the plan is built or run -/// ([`PlanError::ContextLabel`]), so a chain reads straight through and -/// fails in one place. -pub trait IntoLabel { - /// The label, or why the text is not one. - fn into_label(self) -> Result<Label, LabelError>; -} -impl IntoLabel for Label { - fn into_label(self) -> Result<Label, LabelError> { - Ok(self) - } -} -impl IntoLabel for &Label { - fn into_label(self) -> Result<Label, LabelError> { - Ok(self.clone()) - } -} -impl IntoLabel for &str { - fn into_label(self) -> Result<Label, LabelError> { - Label::parse(self) - } -} -impl IntoLabel for String { - fn into_label(self) -> Result<Label, LabelError> { - Label::parse(&self) - } -} -/// A context held in a `String` (a config value, say), borrowed: -/// `Plan::context(&name)`. -impl IntoLabel for &String { - fn into_label(self) -> Result<Label, LabelError> { - Label::parse(self) - } -} - -/// Who supplied a context, for [`PlanError::TwoContextSources`]. -pub(crate) const FROM_PLAN: &str = "the plan"; -pub(crate) const FROM_CALL: &str = "the call"; -pub(crate) const FROM_FIELD: &str = "a context field"; - -/// The label a field is sealed under: the context, then its identity. -pub(crate) fn join(context: &Label, identity: &str) -> Result<Label, LabelError> { - Label::new(context.segments().chain([identity])) -} - -/// The one context of a plan that takes it from the plan or the call: -/// exactly one of the two. -pub(crate) fn resolve_context( - plan: Option<&Label>, - call: Option<Label>, -) -> Result<Label, PlanError> { - match (plan, call) { - (Some(plan), None) => Ok(plan.clone()), - (None, Some(call)) => Ok(call), - (Some(_), Some(_)) => Err(PlanError::TwoContextSources { - first: FROM_PLAN, - second: FROM_CALL, - }), - (None, None) => Err(PlanError::NoContext), - } -} - -/// What a field of a fields plan does with its value. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -#[non_exhaustive] -pub enum FieldKind { - /// Sealed, with no index: [`encrypt`](FieldsBuilder::encrypt). - Encrypt, - /// Sealed, with indexes beside the ciphertext: - /// [`encrypt_index`](FieldsBuilder::encrypt_index). - EncryptIndex, - /// Indexes alone, no ciphertext: [`index`](FieldsBuilder::index). - Index, - /// Carried unsealed and unauthenticated: - /// [`passthrough`](FieldsBuilder::passthrough). - Passthrough, - /// Laid out by a target type's own `EncryptFrom`: - /// [`encrypt_into`](FieldsBuilder::encrypt_into). - EncryptInto, - /// The plan's context, carried unsealed: - /// [`context_field`](FieldsBuilder::context_field). - ContextField, -} - -/// One field of a built [`Plan`]: its name, the label it is sealed and -/// indexed under, and what it declares. What a query takes -/// ([`Plan::field`]), so a query is derived under exactly the label the -/// write used, and asking for an index the field never declared is refused. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct FieldPlan { - name: Arc<str>, - identity: Arc<str>, - label: Option<Label>, - kind: FieldKind, - indexes: Vec<IndexSpec>, - type_id: TypeId, - type_name: &'static str, -} - -impl FieldPlan { - /// The field's name in the value and in the record. - pub fn name(&self) -> &str { - &self.name - } - - /// The label segment the field is keyed under: its name, unless one - /// was pinned with [`identity`](FieldsBuilder::identity). - pub fn identity(&self) -> &str { - &self.identity - } - - /// The label the field is sealed and indexed under, - /// `<context>/<identity>`, when the plan was built with its context. - /// `None` when the context comes from the call or from a context field - /// (a query then names it with `.context(..)`), and for a passthrough or - /// context field, which is neither sealed nor indexed, so its name need - /// not be a label segment. - pub fn label(&self) -> Option<&Label> { - self.label.as_ref() - } - - /// What the field does with its value. - pub fn kind(&self) -> FieldKind { - self.kind - } - - /// The indexes the field declares, in order; empty for `encrypt` and - /// `passthrough`. A typed field's are its target's - /// ([`EncryptFrom::indexes`]). - pub fn indexes(&self) -> &[IndexSpec] { - &self.indexes - } - - /// The field's plaintext type, by name. - pub fn type_name(&self) -> &'static str { - self.type_name - } - - /// The label a query of `F` through `index` is derived under, given the - /// context the query call names (if any), or why the field cannot - /// answer it: the query's plaintext is not the field's type, the field - /// never declared that index (or declared it with other options), or the - /// context is missing or given twice. - pub(crate) fn query_label<F: 'static>( - &self, - index: &IndexSpec, - call: Option<Label>, - ) -> Result<Label, PlanError> { - if TypeId::of::<F>() != self.type_id { - return Err(PlanError::FieldType { - field: self.name.to_string(), - expected: self.type_name, - }); - } - check_declared(&self.name, &self.indexes, index)?; - match &self.label { - Some(label) => resolve_context(Some(label), call), - None => { - let context = call.ok_or(PlanError::NoContext)?; - join(&context, &self.identity).map_err(|source| PlanError::FieldLabel { - field: self.name.to_string(), - source, - }) - } - } - } -} - -/// `index` is one of `declared`, or the error naming how it is not. -pub(crate) fn check_declared( - at: &str, - declared: &[IndexSpec], - index: &IndexSpec, -) -> Result<(), PlanError> { - if declared.contains(index) { - return Ok(()); - } - match declared.iter().find(|have| have.key() == index.key()) { - Some(have) => Err(PlanError::IndexOptions { - field: at.to_owned(), - declared: have.clone(), - asked: index.clone(), - }), - None => Err(PlanError::IndexNotDeclared { - field: at.to_owned(), - index: index.key(), - }), - } -} - -/// The start of a plan, and the namespace of a built fields plan. -/// -/// A plan starts one of two ways, each with an optional context: -/// -/// - [`Plan::fields`] (or [`Plan::context`]`(c).fields()`): field by field; -/// - [`Plan::value`] (or [`Plan::context`]`(c).with(..)`): one value. -/// -/// A built `Plan<S, K>` is a **fields plan** over values of type `S`, for a -/// cipher over the data-key source `K` (both are usually inferred from where -/// the plan is run). It is data: `Clone`, `Debug`, reusable, and each run -/// lowers it to a fresh [`Encryption`] (see [`encryption`](Self::encryption)). -/// A one-value plan is a [`ValuePlan`](super::ValuePlan). -/// -/// # Where the context comes from -/// -/// Exactly one of three places: -/// -/// 1. the plan, when it is built: `.context(c)`; -/// 2. the call that runs it, for a plan built without one: -/// `cipher.encrypt(&v).context(c).using(&plan)`; -/// 3. a field of the value: [`context_field`](FieldsBuilder::context_field). -/// -/// Two of them is [`PlanError::TwoContextSources`], when the plan is built -/// or when it is run; none is [`PlanError::NoContext`], when it is run -/// (before any key is requested), since only the call can say. Every field -/// is sealed under `<context>/<identity>`, and `.extend(parts)` extends -/// whichever context the plan has. -/// -/// ``` -/// # use stack_encrypt::plan::pick; -/// # struct User { email: String, id: u64 } -/// # async fn example() -> Result<(), stack_encrypt::Error> { -/// use stack_encrypt::kms::FakeDataKeySource; -/// use stack_encrypt::{op, Equality, Plan, StackCipher}; -/// -/// let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; -/// let user = User { email: "bob@example.com".into(), id: 42 }; -/// -/// // Built without a context: each call names its own. -/// let users_plan = Plan::fields() -/// .encrypt_index(pick("email", |u: &User| &u.email), Equality) -/// .passthrough(pick("id", |u: &User| &u.id)) -/// .build()?; -/// let record = cipher.encrypt(&user).context("tenants/acme/users").using(&users_plan).await?; -/// -/// // A query and a read name the same context. -/// let email_plan = users_plan.field("email")?; -/// let query_value = cipher -/// .query(op::Eq, "bob@example.com") -/// .context("tenants/acme/users") -/// .on(&email_plan) -/// .await?; -/// # let _ = query_value; -/// let back = cipher.open(record).context("tenants/acme/users").using(&users_plan).await?; -/// assert_eq!(back.get::<String>("email").map(String::as_str), Some("bob@example.com")); -/// -/// // Without one, the call is refused before any key is requested. -/// let refused = cipher.encrypt(&user).using(&users_plan).await; -/// assert!(matches!( -/// refused, -/// Err(stack_encrypt::Error::Plan(stack_encrypt::PlanError::NoContext)) -/// )); -/// # Ok(()) -/// # } -/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); -/// ``` -pub struct Plan<S: 'static, K: 'static> { - inner: Arc<PlanInner<S, K>>, -} - -struct PlanInner<S: 'static, K: 'static> { - source: Source<S>, - names: Arc<[Arc<str>]>, - fields: Vec<Built<S, K>>, - fields_of: Option<FieldsOf<S>>, -} - -/// Where a built fields plan takes its context from. -enum Source<S> { - Plan(Label), - Call, - Field(ContextField<S>), -} - -impl<S> Source<S> { - /// The name of the context field, for a plan whose context is one. - fn field_name(&self) -> Option<&str> { - match self { - Source::Field(field) => Some(&field.name), - Source::Plan(_) | Source::Call => None, - } - } - fn what(&self) -> &'static str { - match self { - Source::Plan(_) => FROM_PLAN, - Source::Call => FROM_CALL, - Source::Field(_) => FROM_FIELD, - } - } -} - -/// Read the context out of a value. -type OfValue<S> = Arc<dyn for<'b> Fn(&'b S) -> Result<Label, Error> + Send + Sync>; -/// Read the context out of a stored record's context field. -type OfStored = Arc<dyn Fn(&Slot) -> Result<Label, PlanError> + Send + Sync>; - -/// A field of the value whose value is the plan's context: read from the -/// value to seal, and from the stored record to open. -struct ContextField<S> { - name: Arc<str>, - of_value: OfValue<S>, - of_stored: OfStored, -} - -impl<S> Clone for ContextField<S> { - fn clone(&self) -> Self { - Self { - name: Arc::clone(&self.name), - of_value: Arc::clone(&self.of_value), - of_stored: Arc::clone(&self.of_stored), - } - } -} - -impl<S> ContextField<S> { - /// The context a stored record was sealed under, read from its context - /// field and checked against what the caller expects, if anything, - /// before any key is requested. - fn stored(&self, record: &FieldValues, expected: Option<Label>) -> Result<Label, Error> { - let slot = record - .slot(&self.name) - .ok_or_else(|| PlanError::NotInValue { - field: self.name.to_string(), - })?; - let stored = (self.of_stored)(slot)?; - let expected = match expected { - Some(expected) => ExpectedContext::from(NonEmpty::from(expected)), - None => ExpectedContext::default(), - }; - expected.validate(stored).map(NonEmpty::into_inner) - } -} - -struct Built<S: 'static, K: 'static> { - plan: FieldPlan, - lower: Lower<S, K>, - probe: Probe<S>, - open: Option<FieldOpener<K>>, -} - -/// A field's description, produced afresh for each run: a description is -/// single-use, a plan is not. It runs under the field's whole context (its -/// label, extended by the call's parts); the plan supplies that. The phantom -/// argument binds the lifetime the description borrows its source for. -type Lower<S, K> = Arc< - dyn for<'s> Fn(PhantomData<&'s ()>) -> Encryption<'s, S, Slot, K, CallerContext> + Send + Sync, ->; -/// A field's opening, from the stored value and the whole context it was -/// sealed under. -type Opener<K> = - Arc<dyn Fn(&str, Slot, CallerContext) -> Result<Decryption<Slot, K>, PlanError> + Send + Sync>; -/// Whether a value produces a field at the plan's type: what the field's -/// lowering will find in it, asked before any keyset is loaded. -type Probe<S> = Arc<dyn Fn(&S) -> Result<(), Error> + Send + Sync>; -/// Whether a stored value is one a field's opening reads, or the type it -/// expects: the opening's own type check, asked before any keyset is -/// loaded. -type Holds = fn(&Slot) -> Result<(), &'static str>; - -/// How a field comes back from a stored record. A field that does not come -/// back (terms alone, which are one-way) has none. -struct FieldOpener<K> { - holds: Holds, - open: Opener<K>, -} - -impl<K> Clone for FieldOpener<K> { - fn clone(&self) -> Self { - Self { - holds: self.holds, - open: Arc::clone(&self.open), - } - } -} - -/// A stored value read as exactly a `T`. -fn holds<T: 'static>(slot: &Slot) -> Result<(), &'static str> { - if slot.is::<T>() { - Ok(()) - } else { - Err(type_name::<T>()) - } -} - -impl<S: 'static, K: 'static> Clone for Plan<S, K> { - fn clone(&self) -> Self { - Self { - inner: Arc::clone(&self.inner), - } - } -} - -impl<S: 'static, K: 'static> fmt::Debug for Plan<S, K> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let context = match &self.inner.source { - Source::Plan(label) => label.to_string(), - Source::Call => String::from("<from the call>"), - Source::Field(field) => format!("<from field {:?}>", field.name), - }; - f.debug_struct("Plan") - .field("context", &context) - .field( - "fields", - &self - .inner - .fields - .iter() - .map(|field| &field.plan) - .collect::<Vec<_>>(), - ) - .finish() - } -} - -impl Plan<(), ()> { - /// Start a plan under `context`, with no value: the saved form of - /// `cipher.encrypt(&value).context(..)`. Follow it with - /// [`fields`](PlanContext::fields) or [`with`](PlanContext::with), then - /// `build()`. The common spelling of `Plan::fields().context(c)`. - /// - /// Anchored on `Plan<(), ()>` only so `Plan::context` resolves without - /// naming a type; that plan is never built. - pub fn context(context: impl IntoLabel) -> PlanContext { - PlanContext { - context: context.into_label(), - } - } - - /// Start a fields plan with no context yet: give it one with - /// [`context`](FieldsBuilder::context) or - /// [`context_field`](FieldsBuilder::context_field), or leave it for the - /// call that runs it. - pub fn fields<S: 'static, K: 'static>() -> FieldsBuilder<S, K> { - FieldsBuilder { - sources: Vec::new(), - fields: Vec::new(), - identity_without_field: false, - fields_of: None, - } - } -} - -/// A plan with its context and nothing else yet: choose one value -/// ([`with`](Self::with)) or field by field ([`fields`](Self::fields)). -#[derive(Clone, Debug)] -pub struct PlanContext { - context: Result<Label, LabelError>, -} - -impl PlanContext { - /// A context already parsed, for a chain that started from a value. - pub(super) fn from_result(context: Result<Label, LabelError>) -> Self { - Self { context } - } - - pub(super) fn into_result(self) -> Result<Label, LabelError> { - self.context - } - - /// Seal each top-level field of the value on its own, under - /// `<context>/<field>`, as the field verbs that follow declare. - pub fn fields<S: 'static, K: 'static>(self) -> FieldsBuilder<S, K> { - let mut fields = Plan::fields(); - fields.sources.push(Declare::Plan(self.context)); - fields - } -} - -/// A context source as declared, before `build()` checks there is one. -enum Declare<S> { - Plan(Result<Label, LabelError>), - Field(ContextField<S>), -} - -impl<S> fmt::Debug for Declare<S> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Declare::Plan(label) => label.fmt(f), - Declare::Field(field) => f.debug_tuple("Field").field(&field.name).finish(), - } - } -} - -impl<S> Declare<S> { - fn what(&self) -> &'static str { - match self { - Declare::Plan(_) => FROM_PLAN, - Declare::Field(_) => FROM_FIELD, - } - } -} - -/// A fields plan under construction: the field verbs, then -/// [`build`](Self::build). -/// -/// Each verb names its field with a [`FieldRef`]: a name, read through the -/// value's [`Field<F>`](super::Field), or a picker, a name with an accessor -/// ([`pick`](super::pick)). With a name, Rust cannot learn the field's type, -/// so where the value has fields of several types the type is named: -/// `.encrypt_index::<u32>("age", (Equality, Ore))`; a picker's accessor -/// names it. An index that is not defined over `F` (`Match` on a `u32`) does -/// not compile, and neither does an empty index set. -/// -/// A field is either the data verbs (`encrypt`, `encrypt_index`, `index`, -/// `passthrough`) or one typed target (`encrypt_into`), never both. -pub struct FieldsBuilder<S: 'static, K: 'static> { - sources: Vec<Declare<S>>, - fields: Vec<Declared<S, K>>, - identity_without_field: bool, - fields_of: Option<FieldsOf<S>>, -} - -struct Declared<S: 'static, K: 'static> { - name: Arc<str>, - identity: Option<Arc<str>>, - kind: FieldKind, - indexes: Vec<IndexSpec>, - type_id: TypeId, - type_name: &'static str, - lower: Lower<S, K>, - probe: Probe<S>, - open: Option<FieldOpener<K>>, -} - -impl<S: 'static, K: 'static> fmt::Debug for FieldsBuilder<S, K> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("FieldsBuilder") - .field("context", &self.sources) - .field( - "fields", - &self - .fields - .iter() - .map(|field| (&*field.name, field.kind, &field.indexes)) - .collect::<Vec<_>>(), - ) - .finish() - } -} - -/// A description of a field `F`, lifted to read it out of the value `S` -/// and to hold its output in a record slot. -fn lifted<'s, S, F, T, K>( - operation: Encryption<'s, F, T, K, CallerContext>, - name: &Arc<str>, - read: &Reader<S, F>, -) -> Encryption<'s, S, Slot, K, CallerContext> -where - S: 's, - F: 'static, - T: std::any::Any + Send + 'static, - K: 'static, -{ - let name = Arc::clone(name); - let read = Arc::clone(read); - operation - .project_by(move |source: &S| picked(&read, source, &name)) - .map(Slot::new) -} - -/// The field `name` of `source` as an `F`. A plan that reads fields by name -/// has already checked that the value names every field it declares -/// (`check_value`, zipped first, whose error wins), so a field that does -/// not come back here is one the value cannot produce at the plan's type. -fn picked<'b, S, F: 'static>( - read: &Reader<S, F>, - source: &'b S, - name: &str, -) -> Result<&'b F, Error> { - read(source).ok_or_else(|| { - PlanError::FieldType { - field: name.to_owned(), - expected: type_name::<F>(), - } - .into() - }) -} - -/// A stored value as `T`, or the plan's error naming the field. -fn stored<T: 'static>(field: &str, slot: Slot) -> Result<T, PlanError> { - slot.downcast::<T>().map_err(|_| PlanError::FieldType { - field: field.to_owned(), - expected: type_name::<T>(), - }) -} - -/// Open a sealed field's ciphertext as an `F`. -fn open_sealed<F, K>(ciphertext: StackCipherText, context: CallerContext) -> Decryption<Slot, K> -where - F: crate::Decrypt + Send + 'static, - K: 'static, -{ - open::<F, K>(ciphertext, AeadContext::from(context)).map(Slot::new) -} - -impl<S: 'static, K: 'static> FieldsBuilder<S, K> { - fn declare<F: 'static>( - mut self, - name: Arc<str>, - access: &Access<S, F>, - kind: FieldKind, - indexes: Vec<IndexSpec>, - lower: Lower<S, K>, - open: Option<FieldOpener<K>>, - ) -> Self { - if let Some(fields) = access.fields { - self.fields_of = Some(fields); - } - let read = Arc::clone(&access.read); - let field = Arc::clone(&name); - let probe: Probe<S> = Arc::new(move |source: &S| picked(&read, source, &field).map(drop)); - self.fields.push(Declared { - name, - identity: None, - kind, - indexes, - type_id: TypeId::of::<F>(), - type_name: type_name::<F>(), - lower, - probe, - open, - }); - self - } - - /// Take the plan's context from `context`, given when the plan is - /// built: every field is sealed under `<context>/<identity>`. - /// `Plan::context(c).fields()` is the same. - pub fn context(mut self, context: impl IntoLabel) -> Self { - self.sources.push(Declare::Plan(context.into_label())); - self - } - - /// Take the plan's context from the field `field` of each value: its - /// value is the context, and every other field is sealed under - /// `<that value>/<identity>`. - /// - /// The field is carried into the record as it is, as a passthrough is - /// (so **unsealed and unauthenticated**), so the record can be opened - /// without being told its context. When it is opened, the stored value - /// is the context each field is opened under, checked first against the - /// context the caller expects, if it names one: - /// `cipher.open(record).context(expected)` refuses a record whose - /// context field says otherwise with - /// [`Error::ContextMismatch`](crate::Error::ContextMismatch), before any - /// key is requested. That is the derive's `#[stash(context_field)]` - /// check. A context field changed in storage - /// opens nothing either way: every field was sealed under the original. - /// - /// The field's value must be a plain label (`"tenants/acme"`), read - /// through [`IntoLabel`]; one that is not is - /// [`PlanError::ContextLabel`] when the plan runs. - /// - /// ``` - /// # use stack_encrypt::plan::pick; - /// # struct Note { tenant: String, text: String } - /// # async fn example() -> Result<(), stack_encrypt::Error> { - /// use stack_encrypt::kms::FakeDataKeySource; - /// use stack_encrypt::{Error, Plan, StackCipher}; - /// - /// let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; - /// let notes_plan = Plan::fields() - /// .context_field(pick("tenant", |n: &Note| &n.tenant)) - /// .encrypt(pick("text", |n: &Note| &n.text)) - /// .build()?; - /// - /// // `text` is sealed under `tenants/acme/text`. - /// let note = Note { tenant: "tenants/acme".into(), text: "hello".into() }; - /// let record = cipher.encrypt(&note).using(&notes_plan).await?; - /// - /// // Opening checks the stored context against the one expected. - /// let refused = cipher.open(record).context("tenants/globex").using(&notes_plan).await; - /// assert!(matches!(refused, Err(Error::ContextMismatch { .. }))); - /// # Ok(()) - /// # } - /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); - /// ``` - pub fn context_field<F>(self, field: impl FieldRef<S, F>) -> Self - where - F: IntoLabel + Clone + Send + 'static, - { - let (name, access) = resolve(field); - let read = Arc::clone(&access.read); - let field_name = Arc::clone(&name); - let stored_name = Arc::clone(&name); - let context = ContextField { - name: Arc::clone(&name), - of_value: Arc::new(move |source: &S| { - picked(&read, source, &field_name)? - .clone() - .into_label() - .map_err(|error| PlanError::ContextLabel(error).into()) - }), - of_stored: Arc::new(move |slot: &Slot| { - slot.downcast_ref::<F>() - .ok_or_else(|| PlanError::FieldType { - field: stored_name.to_string(), - expected: type_name::<F>(), - })? - .clone() - .into_label() - .map_err(PlanError::ContextLabel) - }), - }; - let mut plan = self.passthrough_as(name, access, FieldKind::ContextField); - plan.sources.push(Declare::Field(context)); - plan - } - - /// Seal the field `field` with no index: its ciphertext alone, under - /// `<context>/<identity>`. Lowers to [`ciphertext`]`().under(label)`. - pub fn encrypt<F>(self, field: impl FieldRef<S, F>) -> Self - where - F: crate::Encrypt + crate::Decrypt + Clone + Send + 'static, - { - let (name, access) = resolve(field); - let read = Arc::clone(&access.read); - let field = Arc::clone(&name); - self.declare::<F>( - name, - &access, - FieldKind::Encrypt, - Vec::new(), - Arc::new(move |_| { - lifted( - ciphertext::<F, K, Borrowed>().accepting::<CallerContext>(), - &field, - &read, - ) - }), - Some(FieldOpener { - holds: holds::<StackCipherText>, - open: Arc::new(|field, slot, context| { - Ok(open_sealed::<F, K>(stored(field, slot)?, context)) - }), - }), - ) - } - - /// Seal the field `field` with `indexes` beside its ciphertext, all - /// under `<context>/<identity>`: one index, or a tuple of two to five. - /// The record holds an [`Encrypted<Terms>`](crate::Encrypted). Lowers to - /// [`indexed`]`(indexes).under(label)`, which is also what - /// `encrypt_into::<Encrypted<Terms>, _>` lowers to. - pub fn encrypt_index<F>( - self, - field: impl FieldRef<S, F>, - indexes: impl Indexes<F, Terms: Send> + Clone + Send + Sync + 'static, - ) -> Self - where - F: crate::Encrypt + crate::Decrypt + Clone + Send + 'static, - { - self.encrypt_index_of(field, indexes) - } - - fn encrypt_index_of<F, X>(self, field: impl FieldRef<S, F>, indexes: X) -> Self - where - F: crate::Encrypt + crate::Decrypt + Clone + Send + 'static, - X: Indexes<F> + Clone + Send + Sync + 'static, - X::Terms: Send, - { - let (name, access) = resolve(field); - let read = Arc::clone(&access.read); - let field = Arc::clone(&name); - let specs = indexes.specs(); - self.declare::<F>( - name, - &access, - FieldKind::EncryptIndex, - specs, - Arc::new(move |_| lifted(indexed::<F, K, Borrowed, X>(indexes.clone()), &field, &read)), - // A stored record may hold the whole `Encrypted<Terms>`, or only - // its ciphertext: the terms are not needed to decrypt. - Some(FieldOpener { - holds: |slot| match slot.is::<crate::Encrypted<X::Terms>>() { - true => Ok(()), - false => holds::<StackCipherText>(slot), - }, - open: Arc::new(|field, slot, context| { - let ciphertext = match slot.downcast::<crate::Encrypted<X::Terms>>() { - Ok(encrypted) => encrypted.ciphertext, - Err(slot) => stored::<StackCipherText>(field, slot)?, - }; - Ok(open_sealed::<F, K>(ciphertext, context)) - }), - }), - ) - } - - /// Derive `indexes` from the field `field` with no ciphertext beside - /// them, under `<context>/<identity>`. The field is written and - /// searched, and does not come back from decrypt: a term is one-way. - /// The record holds the terms. Lowers to - /// [`Indexes::operations`]`().under(label)`. - pub fn index<F>( - self, - field: impl FieldRef<S, F>, - indexes: impl Indexes<F, Terms: Send> + Clone + Send + Sync + 'static, - ) -> Self - where - F: Clone + Send + 'static, - { - self.index_of(field, indexes) - } - - fn index_of<F, X>(self, field: impl FieldRef<S, F>, indexes: X) -> Self - where - F: Clone + Send + 'static, - X: Indexes<F> + Clone + Send + Sync + 'static, - X::Terms: Send, - { - let (name, access) = resolve(field); - let read = Arc::clone(&access.read); - let field = Arc::clone(&name); - let specs = indexes.specs(); - self.declare::<F>( - name, - &access, - FieldKind::Index, - specs, - Arc::new(move |_| lifted(indexes.operations::<K, Borrowed>(), &field, &read)), - None, - ) - } - - /// Carry the field `field` into the record as it is. - /// - /// A passthrough field is **not encrypted and not authenticated**: - /// nothing binds it to the sealed fields beside it, so whoever can write - /// the stored record can change it undetected. A field that must stay - /// readable but be tamper-evident is not a passthrough: seal it with an - /// [`Equality`](crate::Equality) index beside it. Lowers to - /// [`passthrough`]`()`, which ignores the context. - /// - /// A passthrough field is under no label, so its name may be any text - /// (`"2fa_enabled"`, `"created(utc)"`): it is only the record key. An - /// [`identity`](Self::identity) pinned on it keys nothing. - pub fn passthrough<F>(self, field: impl FieldRef<S, F>) -> Self - where - F: Clone + Send + 'static, - { - let (name, access) = resolve(field); - self.passthrough_as(name, access, FieldKind::Passthrough) - } - - fn passthrough_as<F>(self, name: Arc<str>, access: Access<S, F>, kind: FieldKind) -> Self - where - F: Clone + Send + 'static, - { - let read = Arc::clone(&access.read); - let field = Arc::clone(&name); - self.declare::<F>( - name, - &access, - kind, - Vec::new(), - Arc::new(move |_| { - lifted( - passthrough::<F, K, Borrowed, CallerContext>(), - &field, - &read, - ) - }), - Some(FieldOpener { - holds: holds::<F>, - open: Arc::new(|field, slot, _| { - Ok(Decryption::ready(Slot::new(stored::<F>(field, slot)?))) - }), - }), - ) - } - - /// Lay the field `field` out as the target `T` does: `T`'s own - /// [`EncryptFrom<F>`] decides what is sealed and which terms sit beside - /// it, all under `<context>/<identity>`, and the record holds a `T`. - /// Lowers to `<T as EncryptFrom<F>>::encryption().under(label)`, as the - /// derive composes a field of a record type. - /// - /// The target says which queries the field answers - /// ([`EncryptFrom::indexes`]); any other is refused. It opens through - /// `T`'s own decryption ([`DecryptField`], which a ciphertext, an - /// [`Encrypted<Terms>`](crate::Encrypted), a tuple holding one and a - /// derived record implement); a target with nothing to open - /// ([`Decryptable::DECRYPTABLE`] false: terms alone) does not come back - /// from `open`, like an [`index`](Self::index) field. - /// - /// Name the target and let the field's type be inferred, or name both: - /// `.encrypt_into::<TextEq, _>("email")`, - /// `.encrypt_into::<Encrypted<(EqualityTerm, MatchTerms)>, String>("email")`. - /// With a picker the field's type is the accessor's. - /// - /// A field is either data verbs or one target: declaring the same field - /// with both is [`PlanError::TargetWithVerbs`]. - /// - /// ``` - /// # use stack_encrypt::plan::pick; - /// # struct User { email: String } - /// # async fn example() -> Result<(), stack_encrypt::Error> { - /// use stack_encrypt::kms::FakeDataKeySource; - /// use stack_encrypt::sem::{EqualityTerm, MatchTerms}; - /// use stack_encrypt::{op, Encrypted, Plan, StackCipher}; - /// - /// let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; - /// let users_plan = Plan::context("users") - /// .fields() - /// .encrypt_into::<Encrypted<(EqualityTerm, MatchTerms)>, _>(pick("email", |u: &User| &u.email)) - /// .build()?; - /// - /// let user = User { email: "bob@example.com".into() }; - /// let mut record = cipher.encrypt(&user).using(&users_plan).await?; - /// let email: Encrypted<(EqualityTerm, MatchTerms)> = record.take("email")?; - /// - /// // The target declares an equality index, so the field answers it. - /// let query_value = cipher.query(op::Eq, "bob@example.com").on(&users_plan.field("email")?).await?; - /// assert_eq!(query_value, email.terms.0); - /// # Ok(()) - /// # } - /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); - /// ``` - pub fn encrypt_into<T, F>(self, field: impl FieldRef<S, F>) -> Self - where - F: Send + 'static, - T: EncryptFrom<F> + Decryptable + DecryptField<F, CallerContext> + Send, - CallerContext: Into<T::Context>, - { - let (name, access) = resolve(field); - let read = Arc::clone(&access.read); - let field = Arc::clone(&name); - let open: Option<FieldOpener<K>> = if T::DECRYPTABLE { - Some(FieldOpener { - holds: holds::<T>, - open: Arc::new(|field, slot, context| { - Ok(stored::<T>(field, slot)? - .decryption_field::<K>(context) - .map_or_else( - || Decryption::failed(Error::NotOpened), - |d| d.map(Slot::new), - )) - }), - }) - } else { - None - }; - self.declare::<F>( - name, - &access, - FieldKind::EncryptInto, - T::indexes(), - Arc::new(move |_| { - lifted( - T::encryption::<K>().accepting::<CallerContext>(), - &field, - &read, - ) - }), - open, - ) - } - - /// Lay out the field `field` as the owned target `T` lays it out. `T`'s - /// own [`EncryptTarget<F>`] decides what is sealed and which terms are - /// stored beside it, all under `<context>/<identity>`. The record - /// contains a `T`. - /// - /// This is the owned form of [`encrypt_into`](Self::encrypt_into), for a - /// target that implements only `EncryptTarget`. Examples are an opaque - /// [`Ciphertext`](crate::Ciphertext), or a record that derives - /// `EncryptTarget`. A derived record also works with `encrypt_into`, - /// because the derive implements the structural traits too. - /// - /// The field lowers to - /// `<T as EncryptTarget<F>>::encryption().accepting::<CallerContext>()`. - /// So the field's context reaches the target as its caller context, and - /// the target's `ContextInput` must take a `CallerContext`. A target - /// that accepts no caller context - /// ([`NoCallerContext`](crate::target::NoCallerContext)), or that - /// requires a context of another type, is a compile error at the call. - /// - /// The plan runs the target over a borrow of the value, as it does for - /// every field. So the value stays the caller's, and this method wipes - /// nothing. The owned conversions consume the value and wipe it. They are - /// in [`EncryptInto`](crate::EncryptInto). - /// - /// ``` - /// # use stack_encrypt::plan::pick; - /// # struct User { age: u32 } - /// # async fn example() -> Result<(), stack_encrypt::Error> { - /// use stack_encrypt::{Ciphertext, Plan, StackCipher}; - /// - /// // With no `.kms(..)`, the cipher uses ZeroKMS. - /// let cipher = StackCipher::builder() - /// # .kms(stack_encrypt::kms::FakeDataKeySource::new()) - /// .init() - /// .await?; - /// let users_plan = Plan::context("users") - /// .fields() - /// .target::<Ciphertext, _>(pick("age", |u: &User| &u.age)) - /// .build()?; - /// - /// let user = User { age: 34 }; - /// let mut record = cipher.encrypt(&user).using(&users_plan).await?; - /// let age: Ciphertext = record.take("age")?; - /// // An ordinary opaque ciphertext, under the field's context. - /// assert_eq!(cipher.default_keyset().decrypt_with_context::<u32>(age, ("users", "age")).await?, 34); - /// # Ok(()) - /// # } - /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); - /// ``` - pub fn target<T, F>(self, field: impl FieldRef<S, F>) -> Self - where - F: Send + zeroize::Zeroize + 'static, - T: EncryptTarget<F> + Decryptable + DecryptField<F, CallerContext> + Send, - T::ContextInput: From<CallerContext>, - { - let (name, access) = resolve(field); - let read = Arc::clone(&access.read); - let field = Arc::clone(&name); - let open: Option<FieldOpener<K>> = if T::DECRYPTABLE { - Some(FieldOpener { - holds: holds::<T>, - open: Arc::new(|field, slot, context| { - Ok(stored::<T>(field, slot)? - .decryption_field::<K>(context) - .map_or_else( - || Decryption::failed(Error::NotOpened), - |d| d.map(Slot::new), - )) - }), - }) - } else { - None - }; - self.declare::<F>( - name, - &access, - FieldKind::EncryptInto, - <T as EncryptTarget<F>>::indexes(), - Arc::new(move |_| { - lifted( - <T as EncryptTarget<F>>::encryption::<K>().accepting::<CallerContext>(), - &field, - &read, - ) - }), - open, - ) - } - - /// Key the field just declared under `identity` rather than its name. - /// - /// A field's identity is the label segment its data is keyed under, and - /// it never changes once data is written. It defaults to the field's - /// name; pin it when a stored field is renamed, so the name moves and the - /// key does not. Once pinned, the name is only the field's key in the - /// value and the record, so it need not be a plain label segment - /// (`"0"`, a tuple index, or `"2fa_secret"` keyed as `totp_secret`). - pub fn identity(mut self, identity: &str) -> Self { - match self.fields.last_mut() { - Some(field) => field.identity = Some(Arc::from(identity)), - None => self.identity_without_field = true, - } - self - } - - /// Validate the whole plan and freeze it. - /// - /// # Errors - /// - /// In [`Error::Plan`], before any key is touched: - /// - /// - [`PlanError::TwoContextSources`]: the context is given twice - /// (`context` and `context_field`, or either twice); - /// - [`PlanError::ContextLabel`]: the context is not a plain label; - /// - [`PlanError::IdentityWithoutField`]: `identity` came before any field; - /// - [`PlanError::FieldLabel`]: the identity of a sealed or indexed - /// field (its name, unless one is pinned) is not a plain segment (a - /// passthrough field's name may be any text: it is under no label); - /// - [`PlanError::DuplicateIndex`]: a field names one index twice; - /// - [`PlanError::TargetWithVerbs`]: a field is both a typed target and - /// data verbs; - /// - [`PlanError::PassthroughIndexed`]: a field is both passthrough and - /// indexed; - /// - [`PlanError::DuplicateField`]: a field is named twice; - /// - [`PlanError::SharedIdentity`]: two sealed or indexed fields share - /// one identity; - /// - with a type [`schema`](super::Fields::schema), when a field is named - /// by name: [`PlanError::NotInValue`] for a field the type does not - /// have, [`PlanError::FieldType`] for one declared at the wrong type, - /// and [`PlanError::NotInPlan`] for a field of the type the plan leaves - /// unnamed. - /// - /// A plan with no context at all builds: the call that runs it names - /// one. A value's fields are checked against the plan again whenever it - /// runs, so a type with no schema is refused then. - pub fn build(self) -> Result<Plan<S, K>, Error> { - self.freeze() - } - - /// Validate the plan as [`build`](Self::build) does, without keeping - /// it, and `source` against it as running it would: what a chain asks - /// before it loads a keyset. - pub(super) fn check(&self, source: &S) -> Result<(), Error> { - Runs::<S, K>::check(&self.freeze()?, source, None) - } - - fn freeze(&self) -> Result<Plan<S, K>, Error> { - let mut sources = self.sources.iter(); - let source = match (sources.next(), sources.next()) { - (None, _) => Source::Call, - (Some(Declare::Plan(label)), None) => { - Source::Plan(label.clone().map_err(PlanError::ContextLabel)?) - } - (Some(Declare::Field(field)), None) => Source::Field(field.clone()), - (Some(first), Some(second)) => { - return Err(PlanError::TwoContextSources { - first: first.what(), - second: second.what(), - } - .into()) - } - }; - if self.identity_without_field { - return Err(PlanError::IdentityWithoutField.into()); - } - let mut fields = Vec::with_capacity(self.fields.len()); - for (at, field) in self.fields.iter().enumerate() { - let identity = field.identity.as_ref().unwrap_or(&field.name); - check_indexes(&field.name, &field.indexes)?; - let label = match (&source, keys_nothing(field.kind)) { - // Carried as it is, under no label: its name is only the - // record key, so any text will do. - (_, true) => Ok(None), - (Source::Plan(context), false) => join(context, identity).map(Some), - (_, false) => Label::new([&**identity]).map(|_| None), - } - .map_err(|source| PlanError::FieldLabel { - field: field.name.to_string(), - source, - })?; - for earlier in &self.fields[..at] { - check_pair(earlier, field)?; - } - fields.push(Built { - plan: FieldPlan { - name: Arc::clone(&field.name), - identity: Arc::clone(identity), - label, - kind: field.kind, - indexes: field.indexes.clone(), - type_id: field.type_id, - type_name: field.type_name, - }, - lower: Arc::clone(&field.lower), - probe: Arc::clone(&field.probe), - open: field.open.clone(), - }); - } - if let Some(schema) = self.fields_of.and_then(|fields| (fields.schema)()) { - check_schema(&schema, &fields)?; - } - Ok(Plan { - inner: Arc::new(PlanInner { - source, - names: fields.iter().map(|f| Arc::clone(&f.plan.name)).collect(), - fields, - fields_of: self.fields_of, - }), - }) - } -} - -/// No index named twice in one field or one-value plan, and no two indexes -/// that answer one operator. Two match indexes with different options are -/// still one index twice: a field has one term per index kind. `(Ore, -/// Ope)` are two indexes that both answer the orderings, so a query for -/// them would have to choose; the plan refuses them instead -/// ([`PlanError::IndexOverlap`]), and the dynamic path refuses the same -/// declaration (`overlapping_outputs`). -pub(crate) fn check_indexes(at: &str, indexes: &[IndexSpec]) -> Result<(), PlanError> { - for (i, index) in indexes.iter().enumerate() { - if indexes[..i] - .iter() - .any(|earlier| earlier.key() == index.key()) - { - return Err(PlanError::DuplicateIndex { - at: at.to_owned(), - index: index.key(), - }); - } - } - if let Some((first, second, operator)) = crate::target::index::overlapping(indexes) { - return Err(PlanError::IndexOverlap { - at: at.to_owned(), - first: first.key(), - second: second.key(), - operator, - }); - } - Ok(()) -} - -/// The rules between two fields: one name each, and one identity each. A -/// name declared once as a target and once with data verbs is reported as -/// that, and one declared once passthrough and once indexed likewise, since -/// those are the likelier mistakes. -fn check_pair<S, K>(earlier: &Declared<S, K>, field: &Declared<S, K>) -> Result<(), PlanError> { - if earlier.name == field.name { - let target = |f: &Declared<S, K>| matches!(f.kind, FieldKind::EncryptInto); - if target(earlier) != target(field) { - return Err(PlanError::TargetWithVerbs { - field: field.name.to_string(), - }); - } - let passthrough = |f: &Declared<S, K>| f.kind == FieldKind::Passthrough; - let indexed = |f: &Declared<S, K>| !f.indexes.is_empty(); - if (passthrough(earlier) && indexed(field)) || (indexed(earlier) && passthrough(field)) { - return Err(PlanError::PassthroughIndexed { - field: field.name.to_string(), - }); - } - return Err(PlanError::DuplicateField { - field: field.name.to_string(), - }); - } - // A passthrough or context field keys nothing, so it shares no - // identity. - if keys_nothing(earlier.kind) || keys_nothing(field.kind) { - return Ok(()); - } - let identity = - |f: &Declared<S, K>| -> Arc<str> { Arc::clone(f.identity.as_ref().unwrap_or(&f.name)) }; - if identity(earlier) == identity(field) { - return Err(PlanError::SharedIdentity { - identity: identity(field).to_string(), - first: earlier.name.to_string(), - second: field.name.to_string(), - }); - } - Ok(()) -} - -/// A plan against a type that fixes its fields: every plan field is one of -/// the type's, at the type the plan declares, and every field of the type -/// is named. -fn check_schema<S, K>( - schema: &[super::FieldSchema], - fields: &[Built<S, K>], -) -> Result<(), PlanError> { - for field in fields { - let Some(declared) = schema.iter().find(|s| s.name() == field.plan.name()) else { - return Err(PlanError::NotInValue { - field: field.plan.name().to_owned(), - }); - }; - if declared.type_id() != field.plan.type_id { - return Err(PlanError::FieldType { - field: field.plan.name().to_owned(), - expected: field.plan.type_name, - }); - } - } - match schema - .iter() - .find(|s| !fields.iter().any(|f| f.plan.name() == s.name())) - { - Some(unnamed) => Err(PlanError::NotInPlan { - field: unnamed.name().to_owned(), - }), - None => Ok(()), - } -} - -/// The value's fields against the plan's, both ways: what a fields plan -/// that reads any field by name checks every time it runs. -fn check_value(have: &[&str], names: &[Arc<str>]) -> Result<(), Error> { - if let Some(extra) = have.iter().find(|n| !names.iter().any(|p| &**p == **n)) { - return Err(PlanError::NotInPlan { - field: (*extra).to_owned(), - } - .into()); - } - if let Some(missing) = names.iter().find(|p| !have.contains(&&***p)) { - return Err(PlanError::NotInValue { - field: missing.to_string(), - } - .into()); - } - Ok(()) -} - -/// The base context every field's label extends, for one run. -enum Base<S> { - Label(Label), - Field(OfValue<S>), - /// The call names the context, and no call has been made: what - /// [`Plan::contexts`] lowers a plan built without a context under. - Call, -} - -impl<S: 'static, K: 'static> Plan<S, K> { - /// The plan's context, when it was built with one. - pub fn label(&self) -> Option<&Label> { - match &self.inner.source { - Source::Plan(label) => Some(label), - _ => None, - } - } - - /// The field the plan takes its context from, if it has one. - pub fn context_field(&self) -> Option<&str> { - match &self.inner.source { - Source::Field(field) => Some(&field.name), - _ => None, - } - } - - /// The fields, in the order they were declared. - pub fn field_plans(&self) -> impl ExactSizeIterator<Item = &FieldPlan> + '_ { - self.inner.fields.iter().map(|field| &field.plan) - } - - /// One field, for a query: `users_plan.field("email")?`. - /// - /// # Errors - /// - /// [`PlanError::NoSuchField`] if the plan has no field of that name. - pub fn field(&self, name: &str) -> Result<FieldPlan, Error> { - self.field_plans() - .find(|field| field.name() == name) - .cloned() - .ok_or_else(|| { - PlanError::NoSuchField { - field: name.to_owned(), - } - .into() - }) - } - - /// The description one run of this plan executes over an `S`, given - /// the context the call names (for a plan built without one; `None` - /// otherwise): a check that the value's fields are the plan's (when it - /// reads any by name), then each field's description, picked out of the - /// value under its own label, zipped so every key request settles in - /// one batch, and the record assembled. A context missing or given twice - /// is a description that fails without I/O. - /// - /// This is the lowering, not a second executor: run it with - /// [`KeysetCipher::run`], or let the chain do so. - pub fn encryption<'s>( - &self, - context: Option<Label>, - ) -> Encryption<'s, S, FieldValues, K, DeclaredContext> { - let base = match self.base(context) { - Ok(base) => base, - Err(error) => return Encryption::failed(error.into()), - }; - self.lowering(base) - } - - /// The contexts this plan's fields are sealed and indexed under, with a - /// placeholder for each part that is not known until the plan runs: the - /// context a call names, a context read from a field of the value, and - /// the extension a call may add. No key is requested. See - /// [`Contexts`](crate::Contexts). - /// - /// ``` - /// # use stack_encrypt::plan::{Field, Fields}; - /// # struct User { email: String, id: u64 } - /// # impl Fields for User { - /// # fn field_names(&self) -> Vec<&str> { vec!["email", "id"] } - /// # } - /// # impl Field<String> for User { - /// # fn field(&self, name: &str) -> Option<&String> { (name == "email").then_some(&self.email) } - /// # } - /// # impl Field<u64> for User { - /// # fn field(&self, name: &str) -> Option<&u64> { (name == "id").then_some(&self.id) } - /// # } - /// use stack_encrypt::kms::FakeDataKeySource; - /// use stack_encrypt::{Equality, Plan}; - /// - /// let plan: Plan<User, FakeDataKeySource> = Plan::context("users") - /// .fields() - /// .encrypt_index::<String>("email", Equality) - /// .passthrough::<u64>("id") - /// .build()?; - /// println!("{}", plan.contexts()); - /// // email - /// // context: (users / email) / {scope?: CallerContext} - /// # #[cfg(feature = "testing")] - /// stack_encrypt::assert_context!( - /// plan.contexts(), - /// email = [["users", "email"], stack_encrypt::testing::caller_scope()], - /// ); - /// # Ok::<(), stack_encrypt::Error>(()) - /// ``` - pub fn contexts(&self) -> crate::Contexts { - let base = match &self.inner.source { - Source::Plan(label) => Base::Label(label.clone()), - Source::Field(field) => Base::Field(Arc::clone(&field.of_value)), - Source::Call => Base::Call, - }; - self.lowering(base).contexts() - } - - /// [`encryption`](Self::encryption), once the base context is known. - fn lowering<'s>(&self, base: Base<S>) -> Encryption<'s, S, FieldValues, K, DeclaredContext> { - let names = Arc::clone(&self.inner.names); - let fields_of = self.inner.fields_of; - let mut record = inspect::<S, K, DeclaredContext, _>(move |source| match fields_of { - Some(fields) => check_value(&(fields.names)(source), &names), - None => Ok(()), - }) - .map(|()| Vec::new()); - for field in &self.inner.fields { - let operation = (field.lower)(PhantomData); - let identity = Arc::clone(&field.plan.identity); - let name = Arc::clone(&field.plan.name); - let operation = match &base { - // Under no label of its own: it ignores the context, so it - // is handed the base one. - Base::Label(context) if keys_nothing(field.plan.kind) => { - operation.under(NonEmpty::from(context.clone())) - } - Base::Label(context) => match join(context, &identity) { - Ok(label) => operation.under(NonEmpty::from(label)), - Err(source) => Encryption::failed(field_label(&name, source)), - }, - Base::Field(of_value) => { - let of_value = Arc::clone(of_value); - let name = Arc::clone(&name); - let keys_nothing = keys_nothing(field.plan.kind); - let context_field = self.inner.source.field_name().unwrap_or("context"); - let joined: Option<String> = (!keys_nothing).then(|| identity.to_string()); - operation.under_by(context_field, joined.as_deref(), move |source: &S| { - let context = of_value(source)?; - match keys_nothing { - true => Ok(context), - false => join(&context, &identity).map_err(|e| field_label(&name, e)), - } - }) - } - // The call names the context. Only `contexts()` lowers a - // plan this way: a run has the call's label by then. - Base::Call => { - let joined = (!keys_nothing(field.plan.kind)).then_some(&*identity); - operation.under_call(joined, PlanError::NoContext.into()) - } - } - .named(&name); - record = record.zip(operation).map(move |(mut slots, slot)| { - slots.push((name, slot)); - slots - }); - } - record.map(FieldValues::from_slots) - } - - /// The base context one run extends, from the plan's source and the - /// context the call names: exactly one of them. - fn base(&self, call: Option<Label>) -> Result<Base<S>, PlanError> { - match (&self.inner.source, call) { - (Source::Plan(label), None) => Ok(Base::Label(label.clone())), - (Source::Call, Some(label)) => Ok(Base::Label(label)), - (Source::Field(field), None) => Ok(Base::Field(Arc::clone(&field.of_value))), - (Source::Call, None) => Err(PlanError::NoContext), - (source, Some(_)) => Err(PlanError::TwoContextSources { - first: source.what(), - second: FROM_CALL, - }), - } - } - - /// The context the call names against the plan's own source, apart - /// from any value: exactly one of them. A batch asks this once, so an - /// empty one is refused as one value would be. - fn check_call(&self, call: Option<&Label>) -> Result<(), Error> { - Ok(self.base(call.cloned()).map(drop)?) - } - - /// A value against the plan, as running it checks it under the context - /// the call names: one context source, the value's fields are the - /// plan's (when it reads any by name), each is of the type the plan - /// declares, and a context field holds a plain label. - fn check_value(&self, source: &S, call: Option<&Label>) -> Result<(), Error> { - let base = self.base(call.cloned())?; - if let Some(fields) = self.inner.fields_of { - check_value(&(fields.names)(source), &self.inner.names)?; - } - self.inner - .fields - .iter() - .try_for_each(|field| (field.probe)(source))?; - match base { - Base::Label(_) => Ok(()), - Base::Field(of_value) => of_value(source).map(drop), - // `base(call)` never yields it: a run has the call's label. - Base::Call => Err(PlanError::NoContext.into()), - } - } - - /// A stored record against the plan, as opening it checks it under the - /// context the call names: the record's fields (see - /// [`check_record`](Self::check_record)), then the context it opens - /// under, a context field's checked against what the caller expects. - fn check_opening(&self, record: &FieldValues, call: Option<&Label>) -> Result<(), Error> { - self.check_record(record)?; - self.opening_base(record, call.cloned()).map(drop) - } - - /// The context the call names against the plan's own source, as - /// opening checks it apart from any record: exactly one, for a plan - /// that does not read its context from a field. For one that does, the - /// call names what the record's field should hold, which only a record - /// can answer. A batch asks this once, so an empty one is refused as - /// one record would be. - fn check_opening_call(&self, call: Option<&Label>) -> Result<(), Error> { - match &self.inner.source { - Source::Field(_) => Ok(()), - Source::Plan(label) => Ok(resolve_context(Some(label), call.cloned()).map(drop)?), - Source::Call => Ok(resolve_context(None, call.cloned()).map(drop)?), - } - } - - /// The context a stored record opens under. - fn opening_base(&self, record: &FieldValues, call: Option<Label>) -> Result<Label, Error> { - match (&self.inner.source, call) { - (Source::Field(field), expected) => field.stored(record, expected), - (Source::Plan(label), call) => Ok(resolve_context(Some(label), call)?), - (Source::Call, call) => Ok(resolve_context(None, call)?), - } - } - - /// A stored record against the plan, as opening it checks it: every - /// field of the record is one the plan names, and every field the plan - /// can return is in it, of a type its opening reads. - fn check_record(&self, record: &FieldValues) -> Result<(), PlanError> { - if let Some(extra) = record - .names() - .find(|name| !self.inner.names.iter().any(|p| &**p == *name)) - { - return Err(PlanError::NotInPlan { - field: extra.to_owned(), - }); - } - for field in &self.inner.fields { - let Some(reader) = &field.open else { - continue; - }; - let name = field.plan.name(); - let Some(slot) = record.slot(name) else { - return Err(PlanError::NotInValue { - field: name.to_owned(), - }); - }; - (reader.holds)(slot).map_err(|expected| PlanError::FieldType { - field: name.to_owned(), - expected, - })?; - } - Ok(()) - } - - /// The opening of a stored record: each sealed field opened under the - /// context it was sealed under (its label, extended by `extend`), - /// passthrough fields carried back, fields that are terms alone left - /// out. Every field of the record must be one the plan names, and every - /// field the plan can return must be in it. - /// - /// `context` is what the call names: the context, for a plan built - /// without one; for a plan with a context field, the context the caller - /// expects the record's field to hold, checked before any key is - /// requested; and nothing for a plan built with one. - pub fn decryption( - &self, - mut record: FieldValues, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<FieldValues, K> { - if let Err(error) = self.check_record(&record) { - return Decryption::failed(error.into()); - } - let base = match self.opening_base(&record, context) { - Ok(base) => base, - Err(error) => return Decryption::failed(error), - }; - let mut opened = Decryption::ready(Vec::new()); - for field in &self.inner.fields { - let slot = record.take_slot(field.plan.name()); - let Some(FieldOpener { open, .. }) = &field.open else { - continue; - }; - let label = match keys_nothing(field.plan.kind) { - // Under no label of its own: its opening ignores the - // context, so it is handed the base one. - true => Ok(base.clone()), - false => join(&base, &field.plan.identity), - }; - let opening = match (slot, label) { - (None, _) => Decryption::failed( - PlanError::NotInValue { - field: field.plan.name().to_owned(), - } - .into(), - ), - (Some(_), Err(source)) => Decryption::failed(field_label(&field.plan.name, source)), - (Some(slot), Ok(label)) => { - let sealed_under = extend.clone().under(NonEmpty::from(label)); - open(field.plan.name(), slot, sealed_under) - .unwrap_or_else(|error| Decryption::failed(error.into())) - } - }; - let name = Arc::clone(&field.plan.name); - opened = opened.zip(opening).map(move |(mut slots, slot)| { - slots.push((name, slot)); - slots - }); - } - opened.map(FieldValues::from_slots) - } -} - -/// Whether a field is carried under no label of its own: a passthrough or -/// a context field, whose name is only its record key. -fn keys_nothing(kind: FieldKind) -> bool { - matches!(kind, FieldKind::Passthrough | FieldKind::ContextField) -} - -pub(crate) fn field_label(field: &str, source: LabelError) -> Error { - PlanError::FieldLabel { - field: field.to_owned(), - source, - } - .into() -} - -/// A plan that can be run over a source of type `Src`, producing `Output`: -/// what [`using`](crate::plan::EncryptBuilder::using) asks of its plan. -/// -/// Implemented for a fields plan over one value, a slice and a `Vec` of -/// them, and for a one-value plan likewise. A slice or a `Vec` runs one -/// description per item and settles all of them in one batch: one key -/// request for the whole collection. -pub trait Runs<Src: ?Sized, K> { - /// What one run produces. - type Output: 'static; - /// Lower the plan and run it over `source`, under the context the call - /// names (if any) extended by `extend`: the [`Pending`], with every term - /// already derived and every key request queued, and nothing sent. - fn pending<'p>( - &self, - keyset: &'p KeysetCipher<'_, K>, - source: &Src, - context: Option<Label>, - extend: DeclaredContext, - ) -> Pending<'p, Self::Output, K>; - - /// Whether `source` is one the plan can run over under the context the - /// call names (if any): the refusals [`pending`](Self::pending) would - /// raise about the value and the context, asked before a chain loads a - /// keyset. A plan with nothing to check keeps this default. - /// - /// # Errors - /// - /// The [`Error::Plan`] that running the plan over `source` would raise. - fn check(&self, _source: &Src, _context: Option<&Label>) -> Result<(), Error> { - Ok(()) - } -} - -/// A plan that can open a stored `R`, producing `Output`: what -/// [`using`](crate::plan::OpenBuilder::using) asks of its plan. -pub trait Opens<R, K> { - /// What opening produces. - type Output: 'static; - /// The opening of `record`, under the context the call names (if any; - /// see [`Plan::decryption`]) extended by `extend`. - fn decryption( - &self, - record: R, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<Self::Output, K>; - - /// Whether `record` is one the plan can open under the context the - /// call names (if any): the refusals - /// [`decryption`](Self::decryption) would raise about the record and - /// the context, asked before a chain loads a keyset. A plan with - /// nothing to check keeps this default. - /// - /// # Errors - /// - /// The [`Error::Plan`] (or [`Error::ContextMismatch`]) that opening - /// `record` would raise before any key request. - fn check(&self, _record: &R, _context: Option<&Label>) -> Result<(), Error> { - Ok(()) - } -} - -impl<S: 'static, K: 'static> Runs<S, K> for Plan<S, K> { - type Output = FieldValues; - fn pending<'p>( - &self, - keyset: &'p KeysetCipher<'_, K>, - source: &S, - context: Option<Label>, - extend: DeclaredContext, - ) -> Pending<'p, FieldValues, K> { - keyset.run(self.encryption(context), source, extend) - } - - fn check(&self, source: &S, context: Option<&Label>) -> Result<(), Error> { - self.check_value(source, context) - } -} - -/// A collection of sources runs one description per item, merged into one -/// batch. The context the call names is checked once, by the plan's -/// `$check_call`, before any item: an empty collection runs no item, and -/// would otherwise accept a context missing or given twice. -macro_rules! runs_over_collections { - ($([$($generics:tt)*] $plan:ty => $item:ty where [$($bounds:tt)*] $check_call:ident;)+) => {$( - impl<$($generics)*> Runs<[$item], K> for $plan where $($bounds)* { - type Output = Vec<<Self as Runs<$item, K>>::Output>; - fn pending<'p>( - &self, - keyset: &'p KeysetCipher<'_, K>, - source: &[$item], - context: Option<Label>, - extend: DeclaredContext, - ) -> Pending<'p, Self::Output, K> { - if let Err(error) = self.$check_call(context.as_ref()) { - return Pending::failed(keyset, error); - } - Pending::all( - keyset, - source - .iter() - .map(|item| { - Runs::<$item, K>::pending( - self, - keyset, - item, - context.clone(), - extend.clone(), - ) - }) - .collect(), - ) - } - fn check(&self, source: &[$item], context: Option<&Label>) -> Result<(), Error> { - self.$check_call(context)?; - source - .iter() - .try_for_each(|item| Runs::<$item, K>::check(self, item, context)) - } - } - impl<$($generics)*> Runs<Vec<$item>, K> for $plan where $($bounds)* { - type Output = Vec<<Self as Runs<$item, K>>::Output>; - fn pending<'p>( - &self, - keyset: &'p KeysetCipher<'_, K>, - source: &Vec<$item>, - context: Option<Label>, - extend: DeclaredContext, - ) -> Pending<'p, Self::Output, K> { - Runs::<[$item], K>::pending(self, keyset, source.as_slice(), context, extend) - } - fn check(&self, source: &Vec<$item>, context: Option<&Label>) -> Result<(), Error> { - Runs::<[$item], K>::check(self, source.as_slice(), context) - } - } - )+}; -} -pub(crate) use runs_over_collections; -runs_over_collections! { - [S, K] Plan<S, K> => S where [S: 'static, K: 'static] check_call; -} - -impl<S: 'static, K: 'static> Opens<FieldValues, K> for Plan<S, K> { - type Output = FieldValues; - fn decryption( - &self, - record: FieldValues, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<FieldValues, K> { - Plan::decryption(self, record, context, extend) - } - fn check(&self, record: &FieldValues, context: Option<&Label>) -> Result<(), Error> { - self.check_opening(record, context) - } -} - -impl<S: 'static, K: 'static> Opens<Vec<FieldValues>, K> for Plan<S, K> { - type Output = Vec<FieldValues>; - fn decryption( - &self, - records: Vec<FieldValues>, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<Vec<FieldValues>, K> { - if let Err(error) = self.check_opening_call(context.as_ref()) { - return Decryption::failed(error); - } - Decryption::all( - records - .into_iter() - .map(|record| Plan::decryption(self, record, context.clone(), extend.clone())), - ) - } - fn check(&self, records: &Vec<FieldValues>, context: Option<&Label>) -> Result<(), Error> { - self.check_opening_call(context)?; - records - .iter() - .try_for_each(|record| self.check_opening(record, context)) - } -} diff --git a/packages/stack-encrypt/src/plan/chain.rs b/packages/stack-encrypt/src/plan/chain.rs deleted file mode 100644 index 5dc4b1843d..0000000000 --- a/packages/stack-encrypt/src/plan/chain.rs +++ /dev/null @@ -1,1270 +0,0 @@ -//! The chain on the cipher: `cipher.encrypt(&value)`, `cipher.query(&value)` -//! and `cipher.open(record)`, each finished by `.await`, and [`all`] for -//! several at once. -use std::future::IntoFuture; -use std::marker::PhantomData; - -use stack_kms::{DataKeySource, IdentifiedBy, IndexKeySource, MaybeSend}; -use uuid::Uuid; - -use super::build::{FieldPlan, FieldsBuilder, IntoLabel, Opens, PlanContext, Runs}; -use super::field_ref::FieldRef; -use super::value::{Indexed, Laid, Typed, ValueLayout, ValuePlan, ValueShape}; -use super::PlanError; -use crate::op::{Answers, DefaultIndex, Input, LabelledTarget, Operator, QueryToken, Supports}; -use crate::target::{ - ciphertext, Borrowed, CallerContext, DeclaredContext, DecryptField, Decryptable, EncryptFrom, - Index, Indexes, Layout, Pending, PendingFuture, Select, Whole, -}; -use crate::{Error, KeysetCipher, Label, LabelError, NonEmpty, StackCipher, StackCipherText}; - -/// What a cipher's data-key source must offer for a chain to run: data keys -/// to seal and open, index keys to load a keyset named in the chain, and, -/// on native targets, `Sync`, because the future holds the cipher across an -/// await (as awaiting a [`Pending`] does). -#[cfg(not(target_arch = "wasm32"))] -pub trait PlanKms: DataKeySource + IndexKeySource + Sync + 'static {} -#[cfg(not(target_arch = "wasm32"))] -impl<K: DataKeySource + IndexKeySource + Sync + 'static> PlanKms for K {} -/// See the native definition; the same minus `Sync`. -#[cfg(target_arch = "wasm32")] -pub trait PlanKms: DataKeySource + IndexKeySource + 'static {} -#[cfg(target_arch = "wasm32")] -impl<K: DataKeySource + IndexKeySource + 'static> PlanKms for K {} - -/// The keyset a chain names with `.keyset(..)`: a [`KeysetCipher`] already -/// in hand, or a keyset by id or name, loaded when the chain is awaited (as -/// [`StackCipher::keyset`] would load it). -/// -/// A chain that names no keyset encrypts under the client's default keyset, -/// and opens leaves from any keyset the client may use. Inside [`all`], a -/// chain that names none encrypts under the keyset the batch's other chains -/// name (or the default), and still opens leaves from any keyset. -pub enum KeysetChoice<'a, K> { - /// A keyset already selected. - Handle(KeysetCipher<'a, K>), - /// A keyset to select when the chain runs. - Named(IdentifiedBy), -} - -impl<K> std::fmt::Debug for KeysetChoice<'_, K> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - KeysetChoice::Handle(keyset) => f.debug_tuple("Handle").field(keyset).finish(), - KeysetChoice::Named(keyset) => f.debug_tuple("Named").field(keyset).finish(), - } - } -} - -impl<'a, K> From<KeysetCipher<'a, K>> for KeysetChoice<'a, K> { - fn from(keyset: KeysetCipher<'a, K>) -> Self { - KeysetChoice::Handle(keyset) - } -} -impl<'a, K> From<&KeysetCipher<'a, K>> for KeysetChoice<'a, K> { - fn from(keyset: &KeysetCipher<'a, K>) -> Self { - KeysetChoice::Handle(keyset.clone()) - } -} -impl<K> From<IdentifiedBy> for KeysetChoice<'_, K> { - fn from(keyset: IdentifiedBy) -> Self { - KeysetChoice::Named(keyset) - } -} -impl<K> From<Uuid> for KeysetChoice<'_, K> { - fn from(keyset: Uuid) -> Self { - KeysetChoice::Named(IdentifiedBy::Uuid(keyset)) - } -} -impl<K> From<&str> for KeysetChoice<'_, K> { - fn from(name: &str) -> Self { - KeysetChoice::Named(IdentifiedBy::Name(name.to_owned().into())) - } -} -impl<K> From<String> for KeysetChoice<'_, K> { - fn from(name: String) -> Self { - KeysetChoice::Named(IdentifiedBy::Name(name.into())) - } -} -impl<K> From<&String> for KeysetChoice<'_, K> { - fn from(name: &String) -> Self { - Self::from(name.as_str()) - } -} - -/// What every chain carries besides its value: the cipher, the keyset it -/// names, the context it names for a plan built without one, and the -/// caller's extension of the plan's contexts. -struct Common<'a, K> { - cipher: &'a StackCipher<K>, - keyset: Option<KeysetChoice<'a, K>>, - context: Option<Result<Label, LabelError>>, - extend: DeclaredContext, -} - -impl<'a, K> Common<'a, K> { - fn new(cipher: &'a StackCipher<K>) -> Self { - Self { - cipher, - keyset: None, - context: None, - extend: DeclaredContext::default(), - } - } - - /// The context the call named, parsed, left in place: what a chain's - /// [`check`](Operation::check) reads. - fn peek_context(&self) -> Result<Option<&Label>, PlanError> { - match &self.context { - None => Ok(None), - Some(Ok(label)) => Ok(Some(label)), - Some(Err(error)) => Err(PlanError::ContextLabel(*error)), - } - } - - /// The context the call named, parsed. - fn call_context(&mut self) -> Result<Option<Label>, PlanError> { - self.context - .take() - .transpose() - .map_err(PlanError::ContextLabel) - } -} - -mod sealed { - pub trait Sealed {} -} - -/// One finished chain: something `.await` settles, and [`all`] batches. -/// -/// Sealed: the chains in this module are the operations. What the trait -/// shows is the contract: a chain is checked with no I/O -/// ([`check`](Self::check)), then yields a [`Pending`] synchronously -/// ([`prepare`](Self::prepare)), with every term derived and every key -/// request queued, and only awaiting it reaches ZeroKMS. -/// -/// Awaiting a chain checks it before it loads a keyset it names, so a plan -/// that does not hold up is refused before any request to ZeroKMS, the -/// keyset lookup included. -pub trait Operation<'a, K: 'static>: sealed::Sealed + Sized { - /// What the chain produces. - type Output: MaybeSend + 'static; - /// The cipher the chain was started on. - fn cipher(&self) -> &'a StackCipher<K>; - /// The keyset the chain names, if any, taken out of it. - fn take_keyset(&mut self) -> Option<KeysetChoice<'a, K>>; - /// The chain's plan, and the value or record it runs over, checked - /// with no I/O and no keyset: the [`Error::Plan`] refusals - /// [`prepare`](Self::prepare) would raise. A query chain also prepares - /// its query here ([`Supports::prepare`]), once, and keeps what the - /// query keeps for [`prepare`](Self::prepare). - /// - /// # Errors - /// - /// The first refusal, as [`prepare`](Self::prepare) would report it. - fn check(&mut self) -> Result<(), Error>; - /// Lower the chain and build its [`Pending`] under `keyset`, with no - /// I/O. `scoped` says whether this chain named `keyset`: an opening is - /// then confined to it, and otherwise opens leaves from any keyset. - fn prepare<'p>( - self, - keyset: &'p KeysetCipher<'a, K>, - scoped: bool, - ) -> Pending<'p, Self::Output, K> - where - 'a: 'p; -} - -/// The keyset a set of chains runs under: the one they name (all the same -/// one), or the client's default when none names one. The `bool` says -/// whether one was named. -async fn resolve<'a, K: PlanKms>( - cipher: &'a StackCipher<K>, - choices: Vec<Option<KeysetChoice<'a, K>>>, -) -> Result<(KeysetCipher<'a, K>, bool), Error> { - let mut chosen: Option<KeysetCipher<'a, K>> = None; - for choice in choices.into_iter().flatten() { - let keyset = match choice { - KeysetChoice::Handle(keyset) => keyset, - KeysetChoice::Named(keyset) => cipher.keyset(keyset).await?, - }; - match &chosen { - Some(first) if first.keyset_id() != keyset.keyset_id() => { - return Err(Error::KeysetMismatch { - left: first.keyset_id(), - right: keyset.keyset_id(), - }); - } - Some(_) => {} - None => chosen = Some(keyset), - } - } - Ok(match chosen { - Some(keyset) => (keyset, true), - None => (cipher.default_keyset(), false), - }) -} - -/// Await one chain: resolve its keyset, prepare it, settle it. -fn settle<'a, K, O>(mut operation: O) -> PendingFuture<'a, O::Output> -where - K: PlanKms, - O: Operation<'a, K> + MaybeSend + 'a, -{ - Box::pin(async move { - operation.check()?; - let choice = operation.take_keyset(); - let (keyset, scoped) = resolve(operation.cipher(), vec![choice]).await?; - operation.prepare(&keyset, scoped).settle().await - }) -} - -/// `.keyset(..)` and `.extend(..)`, on every chain. -macro_rules! chain_options { - ($([$($generics:tt)*] $chain:ty $(where [$($bounds:tt)*])?;)+) => {$( - impl<$($generics)*> $chain $(where $($bounds)*)? { - /// Run under this keyset: a [`KeysetCipher`] in hand, or a - /// keyset id or name, loaded when the chain is awaited. With - /// none, encryption uses the client's default keyset. - pub fn keyset(mut self, keyset: impl Into<KeysetChoice<'a, K>>) -> Self { - self.common.keyset = Some(keyset.into()); - self - } - /// Extend the plan's every context by `parts` for this call: - /// each field's `<context>/<field>` becomes - /// `(<context>/<field>)/<parts>`, as a derived record's caller - /// context does. The same parts must be given to read it back. - /// Further calls append in order, nesting to the left. A grouped - /// `NonEmpty` remains one part; `.extend(())` adds nothing. - pub fn extend(mut self, parts: impl Into<DeclaredContext>) -> Self { - self.common.extend.append(parts.into()); - self - } - } - impl<$($generics)*> sealed::Sealed for $chain $(where $($bounds)*)? {} - )+}; -} - -/// `.await` on a finished chain. -macro_rules! settles { - ($([$($generics:tt)*] $chain:ty $(where [$($bounds:tt)*])?;)+) => {$( - impl<$($generics)*> IntoFuture for $chain - where - $($($bounds)*,)? - Self: Operation<'a, K> + MaybeSend + 'a, - K: PlanKms, - { - type Output = Result<<Self as Operation<'a, K>>::Output, Error>; - type IntoFuture = PendingFuture<'a, <Self as Operation<'a, K>>::Output>; - fn into_future(self) -> Self::IntoFuture { - settle(self) - } - } - )+}; -} - -impl<K: 'static> StackCipher<K> { - /// Start a chain that encrypts `value`. - /// - /// Finish it with a context ([`context`](EncryptBuilder::context)) or a - /// saved plan ([`using`](EncryptBuilder::using)), then `.await`. Before - /// the await nothing has touched a key. With no `.keyset(..)` the - /// client's default keyset mints the data keys. - /// - /// This is the fluent spelling of the cipher-directed and - /// target-directed entry points on [`KeysetCipher`] - /// ([`encrypt_tree`](KeysetCipher::encrypt_tree), [`encrypt_as`](KeysetCipher::encrypt_as), - /// [`run`](KeysetCipher::run)), which stay; see the [`plan`](crate::plan) - /// module for the whole chain. - pub fn encrypt<'a, S: ?Sized>(&'a self, value: &'a S) -> EncryptBuilder<'a, S, K> { - EncryptBuilder { - common: Common::new(self), - source: value, - } - } - - /// Start a chain that derives the query term for the operator `op` over - /// `value`, to match terms a plan wrote. Name the plan with - /// [`on`](QueryBuilder::on): `cipher.query(op::Eq, "bob@example.com").on(&plan)`. - /// The plan chooses the index that answers the operator there; an - /// index the plan declares beside that one is named first, with - /// [`via`](QueryBuilder::via). - /// - /// The value is what the operator takes ([`Input`]): one plaintext, or - /// for `op::Between` a pair of them, `cipher.query(op::Between, &(18, 65))`. - /// It is copied once (`to_owned`), so `query(op::Eq, "bob@example.com")` - /// queries a `String` field. - pub fn query<'a, Op, Q>(&'a self, op: Op, value: &Q) -> QueryBuilder<'a, Op, F<Q>, K> - where - Op: Operator, - Q: ToOwned + ?Sized, - F<Q>: Input<Op::Arity>, - { - let _: Op = op; - QueryBuilder { - common: Common::new(self), - value: value.to_owned(), - op: PhantomData, - } - } - - /// Start a chain that decrypts `record`, a record a plan wrote. Name the - /// plan with [`using`](OpenBuilder::using), then `.await`. - /// - /// Named `open` rather than `decrypt`: [`StackCipher::decrypt`] is the - /// cipher-directed decrypt and keeps its two-argument form. - pub fn open<'a, R>(&'a self, record: R) -> OpenBuilder<'a, R, K> { - OpenBuilder { - common: Common::new(self), - record, - } - } -} - -/// A chain encrypting a value, before its context or plan is chosen. -pub struct EncryptBuilder<'a, S: ?Sized, K> { - common: Common<'a, K>, - source: &'a S, -} - -impl<'a, S: ?Sized, K: 'static> EncryptBuilder<'a, S, K> { - /// Encrypt under `context`: one tree, sealed under that label, as - /// `keyset.encrypt_tree(&value, label)` does. Text is parsed as a - /// [`Label`] (`"documents/v2/body"` is three segments), and text that - /// is not one is refused when the chain is awaited. - pub fn context(self, context: impl IntoLabel) -> EncryptWithContext<'a, S, K> { - EncryptWithContext { - common: self.common, - source: self.source, - context: context.into_label(), - } - } - - /// Encrypt as the saved `plan` declares: a fields plan ([`Plan`](super::Plan)) or a - /// one-value plan ([`ValuePlan`]), over one value, a slice or a `Vec` of - /// them. A collection settles every key request in one batch. The plan - /// must have a context of its own (built with one, or a context field); - /// one without is given its context by - /// [`context(..).using(..)`](EncryptWithContext::using). - pub fn using<P: Runs<S, K>>(self, plan: &'a P) -> EncryptUsing<'a, S, P, K> { - EncryptUsing { - common: self.common, - source: self.source, - plan, - } - } -} - -/// A chain encrypting a value under a context: awaited as it is, one tree; -/// or indexed ([`with`](Self::with)), or field by field -/// ([`fields`](Self::fields)). -pub struct EncryptWithContext<'a, S: ?Sized, K> { - common: Common<'a, K>, - source: &'a S, - context: Result<Label, LabelError>, -} - -impl<'a, S: ?Sized, K: 'static> EncryptWithContext<'a, S, K> { - /// Encrypt as the saved `plan` declares, under this context: for a plan - /// built without a context of its own, this call names it. A plan that - /// has one (built with it, or from a context field) is refused with - /// [`PlanError::TwoContextSources`] when awaited, before any key is - /// requested. - pub fn using<P: Runs<S, K>>(mut self, plan: &'a P) -> EncryptUsing<'a, S, P, K> { - self.common.context = Some(self.context); - EncryptUsing { - common: self.common, - source: self.source, - plan, - } - } - - /// Seal the value with `indexes` beside it: one index or a tuple of - /// two to five, each defined over the value's type. The output is an - /// [`Encrypted<Terms>`](crate::Encrypted), terms read by destructuring. - pub fn with<X: Indexes<S>>(self, indexes: X) -> EncryptIndexed<'a, S, X, K> - where - S: Sized, - { - EncryptIndexed { - common: self.common, - source: self.source, - plan: PlanContext::from_result(self.context).with(indexes), - } - } - - /// Seal each top-level field on its own, under `<context>/<field>`, as - /// the field verbs that follow declare. The output is a - /// [`FieldValues`](super::FieldValues). - pub fn fields(self) -> EncryptFields<'a, S, K> - where - S: Sized + 'static, - { - EncryptFields { - common: self.common, - source: self.source, - plan: PlanContext::from_result(self.context).fields(), - } - } -} - -impl<'a, S, K: 'static> Operation<'a, K> for EncryptWithContext<'a, S, K> -where - S: crate::Encrypt + Clone, -{ - type Output = StackCipherText; - fn cipher(&self) -> &'a StackCipher<K> { - self.common.cipher - } - fn take_keyset(&mut self) -> Option<KeysetChoice<'a, K>> { - self.common.keyset.take() - } - fn check(&mut self) -> Result<(), Error> { - match &self.context { - Ok(_) => Ok(()), - Err(error) => Err(PlanError::ContextLabel(*error).into()), - } - } - fn prepare<'p>(self, keyset: &'p KeysetCipher<'a, K>, _: bool) -> Pending<'p, Self::Output, K> - where - 'a: 'p, - { - match self.context { - Ok(label) => keyset.run( - ciphertext::<S, K, Borrowed>().under(NonEmpty::from(label)), - self.source, - self.common.extend, - ), - Err(error) => Pending::failed(keyset, PlanError::ContextLabel(error).into()), - } - } -} - -/// A chain encrypting one value with indexes beside it. -pub struct EncryptIndexed<'a, S, X, K> { - common: Common<'a, K>, - source: &'a S, - plan: super::value::ValuePlanBuilder<S, Indexed<X>>, -} - -impl<'a, S, X, K: 'static> Operation<'a, K> for EncryptIndexed<'a, S, X, K> -where - S: crate::Encrypt + Clone, - X: Indexes<S> + Clone, - X::Terms: MaybeSend, -{ - type Output = crate::Encrypted<X::Terms>; - fn cipher(&self) -> &'a StackCipher<K> { - self.common.cipher - } - fn take_keyset(&mut self) -> Option<KeysetChoice<'a, K>> { - self.common.keyset.take() - } - fn check(&mut self) -> Result<(), Error> { - self.plan.check() - } - fn prepare<'p>(self, keyset: &'p KeysetCipher<'a, K>, _: bool) -> Pending<'p, Self::Output, K> - where - 'a: 'p, - { - match self.plan.build() { - Ok(plan) => plan.pending(keyset, self.source, None, self.common.extend), - Err(error) => Pending::failed(keyset, error), - } - } -} - -/// A chain encrypting a value field by field: the field verbs of -/// [`FieldsBuilder`], then `.await`. The plan is validated when it is -/// awaited, with the same errors as [`FieldsBuilder::build`]. -pub struct EncryptFields<'a, S: 'static, K: 'static> { - common: Common<'a, K>, - source: &'a S, - plan: FieldsBuilder<S, K>, -} - -impl<'a, S: 'static, K: 'static> EncryptFields<'a, S, K> { - /// [`FieldsBuilder::encrypt`]. - pub fn encrypt<F>(mut self, field: impl FieldRef<S, F>) -> Self - where - F: crate::Encrypt + crate::Decrypt + Clone + Send + 'static, - { - self.plan = self.plan.encrypt::<F>(field); - self - } - /// [`FieldsBuilder::encrypt_index`]. - pub fn encrypt_index<F>( - mut self, - field: impl FieldRef<S, F>, - indexes: impl Indexes<F, Terms: Send> + Clone + Send + Sync + 'static, - ) -> Self - where - F: crate::Encrypt + crate::Decrypt + Clone + Send + 'static, - { - self.plan = self.plan.encrypt_index::<F>(field, indexes); - self - } - /// [`FieldsBuilder::index`]. - pub fn index<F>( - mut self, - field: impl FieldRef<S, F>, - indexes: impl Indexes<F, Terms: Send> + Clone + Send + Sync + 'static, - ) -> Self - where - F: Clone + Send + 'static, - { - self.plan = self.plan.index::<F>(field, indexes); - self - } - /// [`FieldsBuilder::passthrough`]: carried **unsealed and - /// unauthenticated**. - pub fn passthrough<F>(mut self, field: impl FieldRef<S, F>) -> Self - where - F: Clone + Send + 'static, - { - self.plan = self.plan.passthrough::<F>(field); - self - } - /// [`FieldsBuilder::encrypt_into`]. - pub fn encrypt_into<T, F>(mut self, field: impl FieldRef<S, F>) -> Self - where - F: Send + 'static, - T: EncryptFrom<F> + Decryptable + DecryptField<F, CallerContext> + Send, - CallerContext: Into<T::Context>, - { - self.plan = self.plan.encrypt_into::<T, F>(field); - self - } - /// [`FieldsBuilder::identity`]. - pub fn identity(mut self, identity: &str) -> Self { - self.plan = self.plan.identity(identity); - self - } -} - -impl<'a, S: 'static, K: 'static> Operation<'a, K> for EncryptFields<'a, S, K> { - type Output = super::FieldValues; - fn cipher(&self) -> &'a StackCipher<K> { - self.common.cipher - } - fn take_keyset(&mut self) -> Option<KeysetChoice<'a, K>> { - self.common.keyset.take() - } - fn check(&mut self) -> Result<(), Error> { - self.plan.check(self.source) - } - fn prepare<'p>(self, keyset: &'p KeysetCipher<'a, K>, _: bool) -> Pending<'p, Self::Output, K> - where - 'a: 'p, - { - match self.plan.build() { - Ok(plan) => plan.pending(keyset, self.source, None, self.common.extend), - Err(error) => Pending::failed(keyset, error), - } - } -} - -/// A chain encrypting a value, a slice or a `Vec` as a saved plan declares. -pub struct EncryptUsing<'a, S: ?Sized, P, K> { - common: Common<'a, K>, - source: &'a S, - plan: &'a P, -} - -impl<'a, S: ?Sized, P: Runs<S, K>, K: 'static> Operation<'a, K> for EncryptUsing<'a, S, P, K> -where - P::Output: MaybeSend, -{ - type Output = P::Output; - fn cipher(&self) -> &'a StackCipher<K> { - self.common.cipher - } - fn take_keyset(&mut self) -> Option<KeysetChoice<'a, K>> { - self.common.keyset.take() - } - fn check(&mut self) -> Result<(), Error> { - self.plan.check(self.source, self.common.peek_context()?) - } - fn prepare<'p>( - mut self, - keyset: &'p KeysetCipher<'a, K>, - _: bool, - ) -> Pending<'p, Self::Output, K> - where - 'a: 'p, - { - match self.common.call_context() { - Ok(context) => self - .plan - .pending(keyset, self.source, context, self.common.extend), - Err(error) => Pending::failed(keyset, error.into()), - } - } -} - -/// The owned form of a queried value: `String` for a `str` query. -type F<Q> = <Q as ToOwned>::Owned; - -/// A chain deriving the query term for an operator, before its plan is -/// named. -pub struct QueryBuilder<'a, Op, F, K> { - common: Common<'a, K>, - value: F, - op: PhantomData<fn() -> Op>, -} - -impl<'a, Op: Operator, F: Input<Op::Arity>, K: 'static> QueryBuilder<'a, Op, F, K> { - /// Name the context the term is derived under, for a plan built - /// without one: the context the write's call named, or for a plan with - /// a context field, the value that field held. A plan built with its - /// context refuses this ([`PlanError::TwoContextSources`]). - pub fn context(mut self, context: impl IntoLabel) -> Self { - self.common.context = Some(context.into_label()); - self - } - - /// Answer the operator through `index` rather than the index the plan - /// chooses for it ([`Chooses`]): a match index with its own - /// configuration, an OPE index where ORE is the default, or an index - /// of a target type that has no [`Answers`] impl for the operator. The - /// plan named next must declare it, as [`on`](Self::on) says. - pub fn via<I: Supports<Op, F>>(self, index: I) -> QueryVia<'a, Op, F, I, K> { - QueryVia { - common: self.common, - value: self.value, - op: self.op, - index, - } - } - - /// Query on `plan`: a field of a fields plan - /// ([`Plan::field`](super::Plan::field)), or a one-value plan - /// ([`ValuePlan`]). The plan chooses the index that answers the - /// operator ([`Chooses`]), and the term is derived under exactly the - /// label the write used. - /// - /// What the plan's type can see is checked here: an operator it does - /// not answer, a value that is not what the operator takes over the - /// plan's plaintext, or a one-value plan without the index, does not - /// compile. A field of a fields plan is data, so there an index the - /// field does not declare, or declares with other options, is refused - /// when the query is awaited ([`PlanError::IndexNotDeclared`], - /// [`PlanError::IndexOptions`]), before any key is requested. - pub fn on<P, At>(self, plan: &P) -> QueryOp<'a, F, Op, P::Via, K> - where - P: Chooses<Op, F, At>, - { - let QueryBuilder { common, value, op } = self; - QueryOp::resolve( - common, - value, - op, - (), - |()| P::Via::default(), - |(), call| plan.choose(call), - ) - } -} - -/// A chain deriving the query term for an operator through a named index, -/// before its plan is named. -pub struct QueryVia<'a, Op, F, I, K> { - common: Common<'a, K>, - value: F, - op: PhantomData<fn() -> Op>, - index: I, -} - -impl<'a, Op, F, I, K: 'static> QueryVia<'a, Op, F, I, K> -where - Op: Operator, - F: Input<Op::Arity>, - I: Supports<Op, F>, -{ - /// Name the context the term is derived under, as - /// [`QueryBuilder::context`], before or after the index. - pub fn context(mut self, context: impl IntoLabel) -> Self { - self.common.context = Some(context.into_label()); - self - } - - /// Query on `plan` through the named index, which the plan must - /// declare ([`Resolves`]): a one-value plan without it does not - /// compile; a field of a fields plan without it is refused when the - /// query is awaited. A one-value plan built `with` indexes uses its - /// own index of that type, so the named index says only which. - pub fn on<P, At>(self, plan: &P) -> QueryOp<'a, F, Op, I, K> - where - P: Resolves<F::Plaintext, I, At>, - { - let QueryVia { - common, - value, - op, - index, - } = self; - QueryOp::resolve( - common, - value, - op, - index, - |index| index, - |index, call| plan.resolve(index, call), - ) - } -} - -/// A plan a query for the operator `Op`, given `In`, runs on, and the -/// index it answers the operator with. Sealed: the three plan shapes -/// implement it. -/// -/// A field of a fields plan and a one-value plan built `with` indexes -/// answer through the operator's [`DefaultIndex`]; a one-value plan for a -/// target type answers through the type's [`Answers`] impl, when the type -/// is a [`LabelledTarget`], whose stored terms were derived under the -/// context the plan's label names. Such a plan is queried through the -/// indexes the type declares ([`ValueLayout`]) even when the type cannot be -/// encrypted through it, as an EQL type sealed under its column cannot. -/// The plan's -/// plaintext is the input's ([`Input::Plaintext`]): `S` for a query given -/// an `S`, and for a `Between` given an `(S, S)`. `At` is the position of -/// that index in a plan built `with` several, and is inferred. -#[diagnostic::on_unimplemented( - message = "`{Self}` does not answer `{Op}` given `{In}`", - label = "no index answers this operator on this plan", - note = "a field of a fields plan or a plan built `with` indexes answers an operator through its default index (`op::Eq`: `Equality`; `op::Matches`: `Match` with the default configuration, case-insensitive and over text only; `op::Lt`, `Lte`, `Gt`, `Gte` and `Between`: `Ore` over an integer, `Between` given a pair); a typed plan answers the operators its target type implements `Answers` for; `.via(index)` names an index the plan declares" -)] -pub trait Chooses<Op: Operator, In: Input<Op::Arity>, At>: plan_sealed::Sealed { - /// The index that answers `Op` on this plan. - type Via: Supports<Op, In> + Default; - /// The index the query runs through on this plan, and the label the - /// write used, or why the plan refuses the query, as - /// [`Resolves::resolve`] says. `call` is the context the call named, if - /// any. - fn choose(&self, call: Option<Label>) -> (Self::Via, Result<Label, PlanError>); -} - -impl<Op: DefaultIndex<In>, In: Input<Op::Arity>> Chooses<Op, In, Whole> for FieldPlan { - type Via = Op::Via; - fn choose(&self, call: Option<Label>) -> (Op::Via, Result<Label, PlanError>) { - Resolves::<In::Plaintext, Op::Via, Whole>::resolve(self, Op::Via::default(), call) - } -} - -impl<Op, S, In, X, At> Chooses<Op, In, At> for ValuePlan<S, Indexed<X>> -where - Op: DefaultIndex<In>, - Op::Via: Clone, - In: Input<Op::Arity, Plaintext = S>, - S: 'static, - X: Indexes<S> + Select<Op::Via, At>, -{ - type Via = Op::Via; - fn choose(&self, call: Option<Label>) -> (Op::Via, Result<Label, PlanError>) { - Resolves::<S, Op::Via, At>::resolve(self, Op::Via::default(), call) - } -} - -impl<Op, S, In, T> Chooses<Op, In, Whole> for ValuePlan<S, Typed<T>> -where - Op: Operator, - In: Input<Op::Arity, Plaintext = S>, - S: 'static, - T: Answers<Op, In> + LabelledTarget, - Typed<T>: ValueLayout<S>, -{ - type Via = T::Via; - fn choose(&self, call: Option<Label>) -> (T::Via, Result<Label, PlanError>) { - let index = T::Via::default(); - let label = self.query_label(&index.spec(), call); - (index, label) - } -} - -/// A plan laid out by value answers the operators its layout's index -/// supports, through that index, under the plan's options. -impl<Op, S, In, X, T> Chooses<Op, In, Whole> for ValuePlan<S, Laid<X, T>> -where - Op: Operator, - In: Input<Op::Arity, Plaintext = S>, - S: 'static, - X: Layout<S, Target = T>, - X::Index: Supports<Op, In> + Default, - Laid<X, T>: ValueShape<S>, -{ - type Via = X::Index; - fn choose(&self, call: Option<Label>) -> (X::Index, Result<Label, PlanError>) { - Resolves::<S, X::Index, Whole>::resolve(self, X::Index::default(), call) - } -} - -mod plan_sealed { - use super::{FieldPlan, Indexed, Laid, Typed, ValuePlan}; - pub trait Sealed {} - impl Sealed for FieldPlan {} - impl<F, X> Sealed for ValuePlan<F, Indexed<X>> {} - impl<F, T> Sealed for ValuePlan<F, Typed<T>> {} - impl<F, X, T> Sealed for ValuePlan<F, Laid<X, T>> {} -} - -/// A plan the index `I` over the plaintext `S` can be queried on. Sealed: -/// the three plan shapes implement it, and each finds the label the write -/// used. -/// -/// A field of a fields plan ([`Plan::field`](super::Plan::field)) and a -/// one-value plan for a target type take the index as given, and refuse -/// one the plan does not declare when the query is awaited -/// ([`PlanError::IndexNotDeclared`]). A one-value plan for a target type -/// takes a named index only when the type is sealed under the label -/// ([`ValueShape`]): a type that answers through its [`Answers`] impls -/// alone, as an EQL type does, cannot have another index named on its -/// plan, which would send a term the type's SQL does not take. A one-value plan built `with` -/// indexes selects its own index of that type ([`Indexes::select`]), at -/// the position `At`, which is inferred; a plan without that index does -/// not compile. -#[diagnostic::on_unimplemented( - message = "`{Self}` cannot be queried through `{I}` over `{S}`", - label = "this plan does not declare the index, or is not a plan a query runs on", - note = "query on a field of a fields plan (`plan.field(..)`) or on a one-value plan; a plan built `with` indexes must hold one of this type" -)] -pub trait Resolves<S, I, At>: plan_sealed::Sealed { - /// The index the query runs through on this plan, and the label the - /// write used, or why the plan refuses the query. `call` is the context - /// the call named, if any. - fn resolve(&self, index: I, call: Option<Label>) -> (I, Result<Label, PlanError>); -} - -impl<S: 'static, I: Index<S>> Resolves<S, I, Whole> for FieldPlan { - fn resolve(&self, index: I, call: Option<Label>) -> (I, Result<Label, PlanError>) { - let label = self.query_label::<S>(&index.spec(), call); - (index, label) - } -} - -impl<S: 'static, X, I, At> Resolves<S, I, At> for ValuePlan<S, Indexed<X>> -where - X: Indexes<S> + Select<I, At>, - I: Index<S> + Clone, -{ - fn resolve(&self, _: I, call: Option<Label>) -> (I, Result<Label, PlanError>) { - let index = self.indexes().select::<I, At>().clone(); - (index, self.resolve(call)) - } -} - -impl<S: 'static, T, I: Index<S>> Resolves<S, I, Whole> for ValuePlan<S, Typed<T>> -where - Typed<T>: ValueShape<S>, -{ - fn resolve(&self, index: I, call: Option<Label>) -> (I, Result<Label, PlanError>) { - let label = self.query_label(&index.spec(), call); - (index, label) - } -} - -/// A plan laid out by value uses its layout's own index, with its -/// options, so the index named says only which; as a plan built `with` -/// indexes does. -impl<S: 'static, X, T> Resolves<S, X::Index, Whole> for ValuePlan<S, Laid<X, T>> -where - X: Layout<S, Target = T>, - Laid<X, T>: ValueShape<S>, -{ - fn resolve(&self, _: X::Index, call: Option<Label>) -> (X::Index, Result<Label, PlanError>) { - (self.layout().index().clone(), self.resolve(call)) - } -} - -/// A chain deriving one query term for an operator, ready to await. It -/// resolves to the [`QueryTerm`](crate::op::QueryTerm) the index sends for -/// the operator ([`Supports::Query`]). -pub struct QueryOp<'a, F, Op, I, K> -where - Op: Operator, - F: Input<Op::Arity>, - I: Supports<Op, F>, -{ - common: Common<'a, K>, - value: F, - index: I, - label: Result<Label, PlanError>, - /// What the index kept when [`Operation::check`] prepared the query, - /// so the term derivation does not prepare it again. - verifier: Option<I::Verifier>, - op: PhantomData<fn() -> Op>, -} - -impl<'a, F, Op, I, K> QueryOp<'a, F, Op, I, K> -where - Op: Operator, - F: Input<Op::Arity>, - I: Supports<Op, F>, -{ - /// The chain for a query on a plan: the index the plan resolves for the - /// context the call named, and the label the write used, or why the - /// plan refuses it. A call context that is not a label is that refusal, - /// with `fallback`'s index, which is never used to derive a term. `seed` - /// is what either is built from: the named index for [`QueryVia::on`], - /// nothing for [`QueryBuilder::on`]. The one path from both. - fn resolve<X>( - mut common: Common<'a, K>, - value: F, - op: PhantomData<fn() -> Op>, - seed: X, - fallback: impl FnOnce(X) -> I, - resolve: impl FnOnce(X, Option<Label>) -> (I, Result<Label, PlanError>), - ) -> Self { - let (index, label) = match common.call_context() { - Ok(call) => resolve(seed, call), - Err(error) => (fallback(seed), Err(error)), - }; - QueryOp { - common, - value, - index, - label, - verifier: None, - op, - } - } -} - -impl<'a, F, Op, I, K: 'static> Operation<'a, K> for QueryOp<'a, F, Op, I, K> -where - Op: Operator, - F: Input<Op::Arity>, - I: Supports<Op, F> + 'static, - I::Term: MaybeSend, - F::Terms<I::Term>: MaybeSend, - I::Query: MaybeSend + 'static, -{ - type Output = I::Query; - fn cipher(&self) -> &'a StackCipher<K> { - self.common.cipher - } - fn take_keyset(&mut self) -> Option<KeysetChoice<'a, K>> { - self.common.keyset.take() - } - fn check(&mut self) -> Result<(), Error> { - if self.common.extend.has_bindings() { - return Err(PlanError::BindInQuery.into()); - } - let label = self.label.as_ref().map_err(|error| error.clone())?; - self.index - .check_label(label, self.common.extend.is_extended())?; - // Once per query: the verifier is kept for `prepare`, so a match - // query cuts its tokens here and never again. - self.verifier = Some(self.index.prepare(&self.value)?); - Ok(()) - } - fn prepare<'p>(self, keyset: &'p KeysetCipher<'a, K>, _: bool) -> Pending<'p, Self::Output, K> - where - 'a: 'p, - { - // A binding is in no query context. A `DeclaredContext` carries one - // through `CallerContext`, so the refusal is here, before any key, - // as the dynamic path's `bind_in_query` is. - if self.common.extend.has_bindings() { - return Pending::failed(keyset, PlanError::BindInQuery.into()); - } - let label = match self.label { - Ok(label) => label, - Err(error) => return Pending::failed(keyset, error.into()), - }; - // The context is checked here whatever `check` did: a chain can be - // extended between the two, and the label check is about the - // context. `check` ran before any keyset was resolved and kept the - // verifier, which is about the value alone. A chain prepared on its - // own prepares here instead: no term is derived under a context or - // from a value the index refuses, and no query is prepared twice. - let extended = self.common.extend.is_extended(); - if let Err(error) = self.index.check_label(&label, extended) { - return Pending::failed(keyset, error); - } - let verifier = match self.verifier { - Some(verifier) => verifier, - None => match self.index.prepare(&self.value) { - Ok(verifier) => verifier, - Err(error) => return Pending::failed(keyset, error), - }, - }; - let token = QueryToken::new(label.clone(), extended); - self.value - .derive(&self.index, keyset, label, self.common.extend) - .map(move |terms| I::query(verifier, terms, token)) - } -} - -/// A chain decrypting a stored record, before its plan is named. -pub struct OpenBuilder<'a, R, K> { - common: Common<'a, K>, - record: R, -} - -impl<'a, R, K: 'static> OpenBuilder<'a, R, K> { - /// Name the context: for a plan built without one, the context the - /// write's call named; for a plan with a context field, the context the - /// caller expects the record to hold, so a record whose field says - /// otherwise is [`Error::ContextMismatch`] before any key is requested. - /// A plan built with its context refuses this - /// ([`PlanError::TwoContextSources`]). - pub fn context(mut self, context: impl IntoLabel) -> Self { - self.common.context = Some(context.into_label()); - self - } - - /// Decrypt as `plan` declares: the fields that can come back (sealed - /// and passthrough fields, not index-only ones) for a fields plan, the - /// plaintext for a one-value plan. - pub fn using<P: Opens<R, K>>(self, plan: &'a P) -> OpenUsing<'a, R, P, K> { - OpenUsing { - common: self.common, - record: self.record, - plan, - } - } -} - -/// A chain decrypting a stored record through a plan, ready to await. -pub struct OpenUsing<'a, R, P, K> { - common: Common<'a, K>, - record: R, - plan: &'a P, -} - -impl<'a, R, P: Opens<R, K>, K: 'static> Operation<'a, K> for OpenUsing<'a, R, P, K> -where - P::Output: MaybeSend, -{ - type Output = P::Output; - fn cipher(&self) -> &'a StackCipher<K> { - self.common.cipher - } - fn take_keyset(&mut self) -> Option<KeysetChoice<'a, K>> { - self.common.keyset.take() - } - fn check(&mut self) -> Result<(), Error> { - self.plan.check(&self.record, self.common.peek_context()?) - } - fn prepare<'p>( - mut self, - keyset: &'p KeysetCipher<'a, K>, - scoped: bool, - ) -> Pending<'p, Self::Output, K> - where - 'a: 'p, - { - let opening = match self.common.call_context() { - Ok(context) => self - .plan - .decryption(self.record, context, self.common.extend), - Err(error) => crate::target::Decryption::failed(error.into()), - }; - if scoped { - keyset.run_decryption(opening) - } else { - keyset.cipher().run_decryption(opening) - } - } -} - -chain_options! { - ['a, S: ?Sized, K: 'static] EncryptBuilder<'a, S, K>; - ['a, S: ?Sized, K: 'static] EncryptWithContext<'a, S, K>; - ['a, S, X, K: 'static] EncryptIndexed<'a, S, X, K>; - ['a, S: 'static, K: 'static] EncryptFields<'a, S, K>; - ['a, S: ?Sized, P, K: 'static] EncryptUsing<'a, S, P, K>; - ['a, Op, F, K: 'static] QueryBuilder<'a, Op, F, K>; - ['a, Op, F, I, K: 'static] QueryVia<'a, Op, F, I, K>; - ['a, F, Op, I, K: 'static] QueryOp<'a, F, Op, I, K> where [Op: Operator, F: Input<Op::Arity>, I: Supports<Op, F>]; - ['a, R, K: 'static] OpenBuilder<'a, R, K>; - ['a, R, P, K: 'static] OpenUsing<'a, R, P, K>; -} - -settles! { - ['a, S: ?Sized, K] EncryptWithContext<'a, S, K>; - ['a, S, X, K] EncryptIndexed<'a, S, X, K>; - ['a, S: 'static, K: 'static] EncryptFields<'a, S, K>; - ['a, S: ?Sized, P, K] EncryptUsing<'a, S, P, K>; - ['a, F, Op, I, K] QueryOp<'a, F, Op, I, K> where [Op: Operator, F: Input<Op::Arity>, I: Supports<Op, F>]; - ['a, R, P, K] OpenUsing<'a, R, P, K>; -} - -/// Each chain's `Debug` names the chain and nothing it holds: the value -/// is plaintext. -macro_rules! opaque_debug { - ($([$($generics:tt)*] $chain:ty $(where [$($bounds:tt)*])?, $name:literal;)+) => {$( - impl<$($generics)*> std::fmt::Debug for $chain $(where $($bounds)*)? { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct($name).finish_non_exhaustive() - } - } - )+}; -} -opaque_debug! { - ['a, S: ?Sized, K] EncryptBuilder<'a, S, K>, "EncryptBuilder"; - ['a, S: ?Sized, K] EncryptWithContext<'a, S, K>, "EncryptWithContext"; - ['a, S, X, K] EncryptIndexed<'a, S, X, K>, "EncryptIndexed"; - ['a, S: 'static, K: 'static] EncryptFields<'a, S, K>, "EncryptFields"; - ['a, S: ?Sized, P, K] EncryptUsing<'a, S, P, K>, "EncryptUsing"; - ['a, Op, F, K] QueryBuilder<'a, Op, F, K>, "QueryBuilder"; - ['a, Op, F, I, K] QueryVia<'a, Op, F, I, K>, "QueryVia"; - ['a, F, Op, I, K] QueryOp<'a, F, Op, I, K> where [Op: Operator, F: Input<Op::Arity>, I: Supports<Op, F>], "QueryOp"; - ['a, R, K] OpenBuilder<'a, R, K>, "OpenBuilder"; - ['a, R, P, K] OpenUsing<'a, R, P, K>, "OpenUsing"; -} - -/// Several chains settled together: one ZeroKMS request per request kind -/// for all of them, under one keyset. Built by [`all`]. -pub struct All<'a, K, T> { - operations: T, - scope: PhantomData<Scope<'a, K>>, -} - -/// The lifetime and data-key source a batch runs under, carried only in -/// the type. -type Scope<'a, K> = (&'a (), fn() -> K); - -impl<K, T> std::fmt::Debug for All<'_, K, T> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("All").finish_non_exhaustive() - } -} - -/// Settle several chains in one ZeroKMS request: a tuple of two to four -/// chains, awaited together, resolving to the tuple of their outputs. -/// -/// Every chain is prepared under one keyset (the one they name, which must -/// be the same for all, or the client's default) and their [`Pending`]s -/// are zipped, so however many there are, awaiting makes one `generate` -/// call and one `retrieve` call per keyset at most. A chain that names no -/// keyset encrypts and queries under that batch keyset. An opening that -/// names none still opens leaves from any keyset, as it does alone: the -/// batch settles those openings beside it, in one more `retrieve` call per -/// keyset they read. -/// -/// Every chain is checked ([`Operation::check`]) before any keyset is -/// loaded, so one chain whose plan does not hold up fails the batch with no -/// request at all. Chains started on different ciphers are -/// [`PlanError::MixedCiphers`], and chains naming different keysets are -/// [`Error::KeysetMismatch`], both before any key request. -/// -/// ``` -/// # async fn example() -> Result<(), stack_encrypt::Error> { -/// use stack_encrypt::kms::FakeDataKeySource; -/// use stack_encrypt::{op, Equality, Plan, StackCipher}; -/// -/// let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; -/// let email_plan = Plan::context("users/email").with(Equality).build()?; -/// -/// let email = String::from("bob@example.com"); -/// let (written, query_value) = stack_encrypt::all(( -/// cipher.encrypt(&email).using(&email_plan), -/// cipher.query(op::Eq, "bob@example.com").on(&email_plan), -/// )) -/// .await?; -/// assert_eq!(written.terms, query_value); -/// # Ok(()) -/// # } -/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); -/// ``` -pub fn all<'a, K: 'static, T: Batch<'a, K>>(operations: T) -> All<'a, K, T> { - All { - operations, - scope: PhantomData, - } -} - -/// A tuple of two to four chains [`all`] can settle together. Sealed. -pub trait Batch<'a, K: 'static>: sealed::Sealed {} - -/// One chain's pending, routed to the group it settles in: the batch, -/// under the batch keyset, or, for an opening that named no keyset and has -/// not failed, the openings that read from any keyset. The other group -/// holds an empty place for it, so a failure always lands in the batch and -/// stops it before any I/O. -fn route<'p, T, K>( - cipher: &'p StackCipher<K>, - pending: Pending<'p, T, K>, -) -> (Pending<'p, Option<T>, K>, Pending<'p, Option<T>, K>) -where - T: MaybeSend + 'p, -{ - if pending.opens_any_keyset() { - (Pending::ready(cipher, Ok(None)), pending.map(Some)) - } else { - (pending.map(Some), Pending::ready(cipher, Ok(None))) - } -} - -/// A chain's output, from whichever group it was routed to. -fn placed<T>(batch: Option<T>, any: Option<T>) -> Result<T, Error> { - batch.or(any).ok_or(Error::ResponseShape) -} - -/// `all` over a tuple: each entry is the chain's type, its value, whether -/// it named a keyset, and its places in the batch and in the openings -/// group; the patterns undo the nested `zip` of each group. -macro_rules! all_of { - ($(( - $first:ident $fop:ident $fnamed:ident $fbatch:ident $fany:ident - $(, $rest:ident $rop:ident $rnamed:ident $rbatch:ident $rany:ident)+ - ) => ($batch:pat, $any:pat),)+) => {$( - impl<$first, $($rest),+> sealed::Sealed for ($first, $($rest),+) {} - impl<'a, K: 'static, $first: Operation<'a, K>, $($rest: Operation<'a, K>),+> Batch<'a, K> - for ($first, $($rest),+) - { - } - impl<'a, K: PlanKms, $first, $($rest),+> IntoFuture for All<'a, K, ($first, $($rest),+)> - where - $first: Operation<'a, K> + MaybeSend + 'a, - $($rest: Operation<'a, K> + MaybeSend + 'a,)+ - { - type Output = Result<($first::Output, $($rest::Output),+), Error>; - type IntoFuture = PendingFuture<'a, ($first::Output, $($rest::Output),+)>; - fn into_future(self) -> Self::IntoFuture { - let (mut $fop, $(mut $rop),+) = self.operations; - Box::pin(async move { - $fop.check()?; - $($rop.check()?;)+ - let cipher = $fop.cipher(); - if $(!std::ptr::eq($rop.cipher(), cipher))||+ { - return Err(PlanError::MixedCiphers.into()); - } - let ($fnamed, $($rnamed),+) = ($fop.take_keyset(), $($rop.take_keyset()),+); - let named = ($fnamed.is_some(), $($rnamed.is_some()),+); - let (keyset, _) = resolve(cipher, vec![$fnamed, $($rnamed),+]).await?; - let ($fnamed, $($rnamed),+) = named; - let ($fbatch, $fany) = route(cipher, $fop.prepare(&keyset, $fnamed)); - $(let ($rbatch, $rany) = route(cipher, $rop.prepare(&keyset, $rnamed));)+ - let ($fbatch, $($rbatch),+) = $fbatch - $(.zip($rbatch))+ - .map(|$batch| ($fbatch, $($rbatch),+)) - .settle() - .await?; - let ($fany, $($rany),+) = $fany - $(.zip($rany))+ - .map(|$any| ($fany, $($rany),+)) - .settle() - .await?; - Ok((placed($fbatch, $fany)?, $(placed($rbatch, $rany)?),+)) - }) - } - } - )+}; -} -all_of! { - (A a a_named a_batch a_any, B b b_named b_batch b_any) - => ((a_batch, b_batch), (a_any, b_any)), - (A a a_named a_batch a_any, B b b_named b_batch b_any, C c c_named c_batch c_any) - => (((a_batch, b_batch), c_batch), ((a_any, b_any), c_any)), - ( - A a a_named a_batch a_any, - B b b_named b_batch b_any, - C c c_named c_batch c_any, - D d d_named d_batch d_any - ) => ( - (((a_batch, b_batch), c_batch), d_batch), - (((a_any, b_any), c_any), d_any) - ), -} diff --git a/packages/stack-encrypt/src/plan/error.rs b/packages/stack-encrypt/src/plan/error.rs index 173c6d6f54..1364637909 100644 --- a/packages/stack-encrypt/src/plan/error.rs +++ b/packages/stack-encrypt/src/plan/error.rs @@ -3,14 +3,15 @@ use crate::op::Op; use crate::target::IndexSpec; use crate::LabelError; -/// Why a plan was refused: when it was built, or when it was run against a +/// Why a plan was refused: when it was parsed, or when it was run against a /// value, a stored record or a query that does not match it. /// -/// Every refusal of a plan, at build or when it runs, is raised before any -/// key is requested: a plan that does not hold up never reaches ZeroKMS. -/// [`FieldValues::take`](super::FieldValues::take) reuses two of these -/// ([`NotInValue`](Self::NotInValue), [`FieldType`](Self::FieldType)) for a -/// record already in hand. Carried in [`Error::Plan`]. +/// Every refusal of a plan is raised before any key is requested: a plan +/// that does not hold up never reaches ZeroKMS. Carried in [`Error::Plan`]. +/// +/// Some variants were raised only by the Rust plan builder, which is +/// removed, so nothing raises them now. They stay because each has an error +/// code that a binding's status table lists. /// /// [`Error::Plan`]: crate::Error::Plan /// @@ -88,11 +89,11 @@ pub enum PlanError { /// The field. field: String, }, - /// A field, or a one-value plan, names the same index twice. + /// A field, or a record of one value, names the same index twice. #[error("{at:?} names the {index} index twice")] #[diagnostic(code(stack_encrypt::plan_duplicate_index))] DuplicateIndex { - /// The field, or `the value` for a one-value plan. + /// The field, or `the value` for a typed declaration. at: String, /// The index named twice, as its key (`"eq"`, `"match"`, ...). index: &'static str, @@ -142,14 +143,14 @@ pub enum PlanError { /// The field asked for. field: String, }, - /// The chains given to one [`all`](crate::all) were started on - /// different ciphers. A batch settles through one client and resolves - /// every keyset it names there, so a chain from another cipher would - /// run under a keyset its own cipher never chose. - #[error("the chains in one batch were started on different ciphers")] + /// The operations given to one batch were started on different + /// ciphers. A batch settles through one client and resolves every keyset + /// it names there, so an operation from another cipher would run under a + /// keyset its own cipher never chose. + #[error("the operations in one batch were started on different ciphers")] #[diagnostic( code(stack_encrypt::plan_mixed_ciphers), - help("Start every chain in one batch on the same cipher.") + help("Start every operation in one batch on the same cipher.") )] MixedCiphers, /// A query asked a field for an index the field never declared, so it diff --git a/packages/stack-encrypt/src/plan/field_ref.rs b/packages/stack-encrypt/src/plan/field_ref.rs deleted file mode 100644 index 4388c6bfe2..0000000000 --- a/packages/stack-encrypt/src/plan/field_ref.rs +++ /dev/null @@ -1,139 +0,0 @@ -//! How a field verb names its field: by name, read through the value's -//! [`Field<F>`], or by a **picker**, a name with an accessor that reads the -//! field directly. -use std::sync::Arc; - -use super::values::{Field, FieldSchema, Fields}; - -/// A field verb's field argument: a name, or a picker. -/// -/// - `"email"`: the field is read through the value's [`Field<F>`] impl, -/// looked up by name, which is what a dynamic value (a binding's record) -/// provides. The value names its fields -/// ([`Fields::field_names`]), so a fields plan checks them against its own -/// in both directions every time it runs. -/// - `("email", accessor)`: a **picker**. The accessor reads the field out -/// of a borrowed value, and its return type is the field's type, so the -/// verb needs no turbofish and the value needs no `Fields` or `Field<F>` -/// impl. The type check is the compiler's. This is the form the derive -/// emits. -/// -/// The accessor is any `for<'b> Fn(&'b S) -> &'b F`: a function item, a -/// function pointer, or a closure whose signature is fixed where it is -/// written. Rust does not infer a higher-ranked signature for a closure -/// written straight into a tuple (`("email", |u: &User| &u.email)` is -/// refused with "lifetime may not live long enough"), so write the picker -/// with [`pick`], which fixes it: -/// -/// ``` -/// use stack_encrypt::plan::pick; -/// # struct User { email: String } -/// let email = pick("email", |u: &User| &u.email); -/// # let _ = email; -/// ``` -/// -/// A plan built only from pickers never asks the value for its field names: -/// the plan names exactly the fields its accessors read, and a value's -/// other fields are not its concern. A plan that names any field by name -/// checks the value against every field it declares. -/// -/// Sealed: the two forms are the whole grammar. -pub trait FieldRef<S, F>: sealed::Sealed<S, F> { - /// The field's name: its key in the record, and its identity unless one - /// is pinned. - fn name(&self) -> &str; -} - -/// A picker: the field `name`, read by `accessor`. The same as writing the -/// tuple `(name, accessor)`, with the accessor's signature fixed so a -/// closure borrowing from its argument compiles. -/// -/// ``` -/// # use stack_encrypt::plan::{pick, FieldRef}; -/// # struct User { email: String } -/// let email = pick("email", |u: &User| &u.email); -/// assert_eq!(FieldRef::<User, String>::name(&email), "email"); -/// ``` -pub fn pick<S, F, G>(name: &str, accessor: G) -> (&str, G) -where - G: for<'b> Fn(&'b S) -> &'b F, -{ - (name, accessor) -} - -/// Read a field out of a value: by name or by accessor. -pub(crate) type Reader<S, F> = Arc<dyn for<'b> Fn(&'b S) -> Option<&'b F> + Send + Sync>; - -/// A value's own account of its fields, captured where a verb named a field -/// by name (and so `S: Field<F>`, whose supertrait is [`Fields`]). -pub(crate) struct FieldsOf<S> { - pub(crate) names: for<'b> fn(&'b S) -> Vec<&'b str>, - pub(crate) schema: fn() -> Option<Vec<FieldSchema>>, -} - -impl<S> Clone for FieldsOf<S> { - fn clone(&self) -> Self { - *self - } -} -impl<S> Copy for FieldsOf<S> {} - -mod sealed { - use super::{FieldsOf, Reader}; - - /// What a field argument resolves to. Unnameable outside the crate. - pub struct Access<S, F> { - pub(crate) read: Reader<S, F>, - pub(crate) fields: Option<FieldsOf<S>>, - } - - pub trait Sealed<S, F> { - fn access(self) -> Access<S, F>; - } -} -pub(crate) use sealed::Access; - -impl<S: Field<F>, F: 'static> FieldRef<S, F> for &str { - fn name(&self) -> &str { - self - } -} -impl<S: Field<F>, F: 'static> sealed::Sealed<S, F> for &str { - fn access(self) -> Access<S, F> { - let name: Arc<str> = Arc::from(self); - Access { - read: Arc::new(move |source: &S| source.field(&name)), - fields: Some(FieldsOf { - names: <S as Fields>::field_names, - schema: <S as Fields>::schema, - }), - } - } -} - -impl<S, F, G> FieldRef<S, F> for (&str, G) -where - G: for<'b> Fn(&'b S) -> &'b F + Send + Sync + 'static, -{ - fn name(&self) -> &str { - self.0 - } -} -impl<S, F, G> sealed::Sealed<S, F> for (&str, G) -where - G: for<'b> Fn(&'b S) -> &'b F + Send + Sync + 'static, -{ - fn access(self) -> Access<S, F> { - let accessor = self.1; - Access { - read: Arc::new(move |source: &S| Some(accessor(source))), - fields: None, - } - } -} - -/// The name and the access of a field argument, taken apart. -pub(crate) fn resolve<S, F>(field: impl FieldRef<S, F>) -> (Arc<str>, Access<S, F>) { - let name = Arc::from(field.name()); - (name, sealed::Sealed::access(field)) -} diff --git a/packages/stack-encrypt/src/plan/label.rs b/packages/stack-encrypt/src/plan/label.rs new file mode 100644 index 0000000000..51a3be6373 --- /dev/null +++ b/packages/stack-encrypt/src/plan/label.rs @@ -0,0 +1,64 @@ +//! The label a plan's context is written as, and the label of each field +//! under it. +use crate::{Label, LabelError}; + +/// A context a plan is declared under: a [`Label`], or text parsed as one. +/// +/// A plan's context is a name, so it is a label of plain segments: +/// `"users"`, or `"documents/v2/body"`, which is three segments and renders +/// in the ZeroKMS log exactly as written. Text that is not a plain label is +/// refused when the plan is parsed or run +/// ([`PlanError::ContextLabel`](super::PlanError::ContextLabel)). +pub trait IntoLabel { + /// The label, or why the text is not one. + fn into_label(self) -> Result<Label, LabelError>; +} +impl IntoLabel for Label { + fn into_label(self) -> Result<Label, LabelError> { + Ok(self) + } +} +impl IntoLabel for &Label { + fn into_label(self) -> Result<Label, LabelError> { + Ok(self.clone()) + } +} +impl IntoLabel for &str { + fn into_label(self) -> Result<Label, LabelError> { + Label::parse(self) + } +} +impl IntoLabel for String { + fn into_label(self) -> Result<Label, LabelError> { + Label::parse(&self) + } +} +/// A context held in a `String` (a config value, say), borrowed. +impl IntoLabel for &String { + fn into_label(self) -> Result<Label, LabelError> { + Label::parse(self) + } +} + +/// Who supplied a context, for +/// [`PlanError::TwoContextSources`](super::PlanError::TwoContextSources). +#[cfg(feature = "dynamic")] +pub(crate) const FROM_PLAN: &str = "the plan"; +#[cfg(feature = "dynamic")] +pub(crate) const FROM_CALL: &str = "the call"; + +/// The label a field is sealed under: the context, then its identity. +#[cfg(feature = "dynamic")] +pub(crate) fn join(context: &Label, identity: &str) -> Result<Label, LabelError> { + Label::new(context.segments().chain([identity])) +} + +/// The refusal of a field whose label is not plain. +#[cfg(feature = "dynamic")] +pub(crate) fn field_label(field: &str, source: LabelError) -> crate::Error { + super::PlanError::FieldLabel { + field: field.to_owned(), + source, + } + .into() +} diff --git a/packages/stack-encrypt/src/plan/mod.rs b/packages/stack-encrypt/src/plan/mod.rs index c9e7ae2ff2..f24a93927d 100644 --- a/packages/stack-encrypt/src/plan/mod.rs +++ b/packages/stack-encrypt/src/plan/mod.rs @@ -1,331 +1,20 @@ -//! The plan builder: one chained front door for writes, queries and reads. -//! -//! A **plan** is the saved tail of an encrypt call with the value left out: -//! a context, and either the indexes of one value or, field by field, each -//! field's. The write, the query and the read all take the same plan, so -//! none of them can spell a context differently from the others, and a query -//! cannot silently match nothing because its label drifted from the write's. -//! -//! Every chain starts on a [`StackCipher`](crate::StackCipher) and ends with -//! `.await`. Before the await nothing has touched a key: a chain lowers to a -//! [`Pending`](crate::Pending) synchronously ([`Operation::prepare`]) and the -//! await is what reaches ZeroKMS. -//! -//! # One value -//! -//! ``` -//! # async fn example() -> Result<(), stack_encrypt::Error> { -//! use stack_encrypt::kms::FakeDataKeySource; -//! use stack_encrypt::sem::{EqualityTerm, OreTerm}; -//! use stack_encrypt::{Equality, Ore, StackCipher}; -//! -//! let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; -//! -//! // One tree, one context: the cipher-directed call, by name. -//! let doc = String::from("the body"); -//! let sealed = cipher.encrypt(&doc).context("documents/v2/body").await?; -//! -//! // One value with indexes beside it. The tuple types the terms. -//! let out = cipher.encrypt(&34u32).context("users/age").with((Equality, Ore)).await?; -//! let (eq, ore): (EqualityTerm, OreTerm<u32>) = out.terms; -//! # let _ = (sealed, eq, ore, out.ciphertext); -//! # Ok(()) -//! # } -//! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); -//! ``` -//! -//! # Field by field -//! -//! `.fields()` seals each top-level field of the value on its own, under -//! `<context>/<field>`, so a field can be stored, read, indexed and queried -//! without the rest. Each field is named once, with one verb: -//! -//! - `encrypt(name)`: its ciphertext alone; -//! - `encrypt_index(name, indexes)`: its ciphertext with a non-empty index -//! set beside it (one index or a tuple; `()` does not compile); -//! - `index(name, indexes)`: the indexes alone, no ciphertext, so the field -//! is searchable but does not come back from decrypt; -//! - `passthrough(name)`: carried as it is, **unsealed and unauthenticated**; -//! - `encrypt_into::<T, _>(name)`: laid out by the **target** type `T`, -//! whose own `EncryptFrom` decides what is sealed, which terms sit beside -//! it and which queries it answers. A field is either the data verbs above -//! or one target, never both. -//! -//! A verb names its field by name, read through the value's [`Fields`] and -//! [`Field<F>`] (Rust cannot learn a field's type from its name, so a field -//! verb names it where the value has fields of several types), or by a -//! **picker**, a name with an accessor ([`pick`]`("email", |u: &User| -//! &u.email)`), which reads the field directly and needs neither impl nor -//! turbofish. The output is a [`FieldValues`]. -//! -//! ``` -//! # use stack_encrypt::plan::{Field, Fields}; -//! # struct User { email: String, age: u32, notes: String, id: u64 } -//! # impl Fields for User { -//! # fn field_names(&self) -> Vec<&str> { vec!["email", "age", "notes", "id"] } -//! # } -//! # impl Field<String> for User { -//! # fn field(&self, name: &str) -> Option<&String> { -//! # match name { "email" => Some(&self.email), "notes" => Some(&self.notes), _ => None } -//! # } -//! # } -//! # impl Field<u32> for User { -//! # fn field(&self, name: &str) -> Option<&u32> { (name == "age").then_some(&self.age) } -//! # } -//! # impl Field<u64> for User { -//! # fn field(&self, name: &str) -> Option<&u64> { (name == "id").then_some(&self.id) } -//! # } -//! # async fn example() -> Result<(), stack_encrypt::Error> { -//! use stack_encrypt::kms::FakeDataKeySource; -//! use stack_encrypt::sem::{EqualityTerm, MatchTerms}; -//! use stack_encrypt::{Encrypted, Equality, Match, Ore, StackCipher}; -//! -//! let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; -//! let user = User { email: "bob@example.com".into(), age: 34, notes: "hi".into(), id: 42 }; -//! -//! let mut record = cipher -//! .encrypt(&user) -//! .context("users") -//! .fields() -//! .encrypt_index::<String>("email", (Equality, Match)) -//! .encrypt_index::<u32>("age", (Equality, Ore)) -//! .encrypt::<String>("notes") -//! .passthrough::<u64>("id") -//! .await?; -//! -//! let email: Encrypted<(EqualityTerm, MatchTerms)> = record.take("email")?; -//! assert_eq!(record.take::<u64>("id")?, 42); -//! # let _ = email; -//! # Ok(()) -//! # } -//! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); -//! ``` -//! -//! `.keyset(..)` names the keyset the data keys are minted under (a -//! [`KeysetCipher`](crate::KeysetCipher) in hand, or an id or name loaded -//! at the await), and `.extend(parts)` extends every field's context for one -//! call, as a derived record's caller context does. Both go anywhere in the -//! chain. -//! -//! # A saved plan, a query and a read -//! -//! A plan is the same chain without a value. It starts one of two ways, -//! [`Plan::fields`] or [`Plan::value`], each with an optional `.context(c)`; -//! [`Plan::context`]`(c).fields()` and `Plan::context(c).with(..)` are the -//! common spellings. Its context comes from exactly one place: the plan -//! (`.context(c)`), the call that runs it -//! (`cipher.encrypt(&v).context(c).using(&plan)`, for a plan built without -//! one), or a field of the value -//! ([`context_field`](FieldsBuilder::context_field)); see [`Plan`]. -//! -//! -//! [`build`](FieldsBuilder::build) validates the whole plan once: a field -//! named twice, a label that is not plain, two fields under one identity, -//! passthrough on an indexed field, a field both a target and data verbs, -//! a context given twice, an index named twice, and (where the type -//! declares a [`schema`](Fields::schema)) a field the type lacks, one it -//! leaves unnamed or one at the wrong type are all refused there. A plan is -//! data: `Clone`, `Debug`, reusable. -//! -//! The same plan then writes ([`using`](EncryptBuilder::using)), queries -//! ([`query`](crate::StackCipher::query)) and reads -//! ([`open`](crate::StackCipher::open)): -//! -//! ``` -//! # use stack_encrypt::plan::{Field, Fields}; -//! # struct User { email: String, age: u32, notes: String, id: u64 } -//! # impl Fields for User { -//! # fn field_names(&self) -> Vec<&str> { vec!["email", "age", "notes", "id"] } -//! # } -//! # impl Field<String> for User { -//! # fn field(&self, name: &str) -> Option<&String> { -//! # match name { "email" => Some(&self.email), "notes" => Some(&self.notes), _ => None } -//! # } -//! # } -//! # impl Field<u32> for User { -//! # fn field(&self, name: &str) -> Option<&u32> { (name == "age").then_some(&self.age) } -//! # } -//! # impl Field<u64> for User { -//! # fn field(&self, name: &str) -> Option<&u64> { (name == "id").then_some(&self.id) } -//! # } -//! # async fn example() -> Result<(), stack_encrypt::Error> { -//! use stack_encrypt::kms::FakeDataKeySource; -//! use stack_encrypt::sem::{EqualityTerm, MatchTerms}; -//! use stack_encrypt::{op, Encrypted, Equality, Match, Ore, Plan, StackCipher}; -//! -//! let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; -//! let user = User { email: "bob@example.com".into(), age: 34, notes: "hi".into(), id: 42 }; -//! -//! let users_plan: Plan<User, _> = Plan::context("users") -//! .fields() -//! .encrypt_index::<String>("email", (Equality, Match)) -//! .encrypt_index::<u32>("age", (Equality, Ore)) -//! .encrypt::<String>("notes") -//! .passthrough::<u64>("id") -//! .build()?; -//! -//! // Write. -//! let record = cipher.encrypt(&user).using(&users_plan).await?; -//! let stored: &Encrypted<(EqualityTerm, MatchTerms)> = record.get("email").unwrap(); -//! -//! // Query: derived under the label the write used, so it matches. -//! let email_plan = users_plan.field("email")?; -//! let query_value = cipher.query(op::Eq, "bob@example.com").on(&email_plan).await?; -//! assert_eq!(query_value, stored.terms.0); -//! -//! // Read: every field that can come back. -//! let back = cipher.open(record).using(&users_plan).await?; -//! assert_eq!(back.get::<String>("notes").map(String::as_str), Some("hi")); -//! assert_eq!(back.get::<u32>("age"), Some(&34)); -//! # Ok(()) -//! # } -//! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); -//! ``` -//! -//! A query against an index the field never declared is -//! [`PlanError::IndexNotDeclared`], and one whose plaintext is not the -//! field's type is [`PlanError::FieldType`]: never a term that quietly -//! matches nothing. A one-value plan given its indexes ([`ValuePlan`], from -//! `.with(..)`) selects its query index by type instead, so there an -//! undeclared index does not compile; it also carries its plaintext type, -//! so opening through it yields that type. -//! -//! [`all`] settles several chains in one ZeroKMS request, and a saved plan -//! over a slice or a `Vec` settles every record in one, both ways. -//! -//! # The derive emits a plan -//! -//! `#[derive(EncryptFrom)]` is a second author of the same grammar, not a -//! second executor. It emits `Record::plan()`, a chain built from the -//! record's attributes, and the record's `EncryptFrom::encryption()` is that -//! plan's description, mapped into the struct: -//! -//! - `#[stash(struct = User, scope = "users")]` emits -//! `Plan::context("users").fields()` with one -//! `encrypt_into::<FieldType, _>(pick("name", |u: &User| &u.name))` per -//! field (and `.identity(..)` where one is pinned). The field's own type -//! decides its layout. -//! - `#[stash(plaintext = T)]` emits -//! `Plan::value::<T>().encrypt_into::<(A, B, ..)>()`, the tuple of the -//! derived fields' types, with no context of its own: every output shares -//! the context the caller hands over -//! ([`ValuePlan::encryption_with_context`]). With a -//! `#[stash(context_field)]` it is -//! `Plan::value::<T>().context_field::<C>().encrypt_into::<(A, B, ..)>()`, -//! which carries that context out beside the outputs -//! ([`ValueStart::context_field`]). -//! -//! So the derive's record and the hand-written chain are the same plan: -//! -//! ``` -//! # async fn example() -> Result<(), stack_encrypt::Error> { -//! use stack_encrypt::kms::FakeDataKeySource; -//! use stack_encrypt::plan::pick; -//! use stack_encrypt::sem::EqualityTerm; -//! use stack_encrypt::{op, Encrypted, EncryptFrom, Plan, StackCipher, StackCipherText}; -//! -//! struct User { -//! email: String, -//! nickname: String, -//! } -//! -//! #[derive(EncryptFrom)] -//! #[stash(struct = User, scope = "users")] -//! struct EncryptedUser { -//! email: Encrypted<EqualityTerm>, -//! #[stash(identity = "handle")] -//! nickname: StackCipherText, -//! } -//! -//! let derived = EncryptedUser::plan::<FakeDataKeySource>()?; -//! let by_hand: Plan<User, FakeDataKeySource> = Plan::context("users") -//! .fields() -//! .encrypt_into::<Encrypted<EqualityTerm>, _>(pick("email", |u: &User| &u.email)) -//! .encrypt_into::<StackCipherText, _>(pick("nickname", |u: &User| &u.nickname)) -//! .identity("handle") -//! .build()?; -//! // The same fields, labels, layouts and indexes. -//! assert_eq!(format!("{derived:?}"), format!("{by_hand:?}")); -//! assert_eq!(derived.field("nickname")?.label().map(ToString::to_string).as_deref(), Some("users/handle")); -//! -//! // And the record's equality term is the one a query through the plan derives. -//! let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; -//! let user = User { email: "bob@example.com".into(), nickname: "bob".into() }; -//! let record: EncryptedUser = cipher.default_keyset().encrypt_as(&user, ().into()).await?; -//! let query_value = cipher.query(op::Eq, "bob@example.com").on(&by_hand.field("email")?).await?; -//! assert_eq!(record.email.terms, query_value); -//! # Ok(()) -//! # } -//! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); -//! ``` -//! -//! # A binding lowers data into the same builder -//! -//! A language binding has no types to name, so it declares a record as -//! data. With the `dynamic` feature, the `dynamic::record` module -//! reads that declaration and lowers it into this builder — one context, -//! given by the plan or taken from a field of each record -//! ([`context_field`](FieldsBuilder::context_field)), and `encrypt` / -//! `encrypt_index` / `index` / `passthrough` per field — and runs the -//! result through [`KeysetCipher::run`](crate::KeysetCipher::run) and the -//! plan's opener. It is the third author of this grammar, beside a Rust -//! chain and the derive, and not a second executor. Of the three context -//! sources only the call has no data form: a binding names the context in -//! the declaration it sends, or in the record. -//! -//! The engine is shared, and so is the stored leaf: every leaf is tagged -//! with its type, so a data plan's `uint32` field and a -//! Rust `u32` field seal the same leaf under the same context, and a reader -//! of another type refuses it rather than misreading it. -//! -//! # How a chain lowers -//! -//! The builder adds no cryptographic operation and no executor; every call -//! lowers to the combinators in [`target`](crate::target), and the bytes are -//! theirs: -//! -//! | chain | combinators | -//! |---|---| -//! | `context(c)` alone | [`ciphertext`](crate::target::ciphertext)`().under(c)` | -//! | `with(x)`, `encrypt_index(name, x)` | [`indexed`](crate::target::indexed)`(x).under(label)` | -//! | `encrypt_into::<T, _>(name)`, `encrypt_into::<T>()` | `<T as EncryptFrom<F>>::encryption().under(label)`; `Encrypted<Terms>`'s is `indexed(..)` | -//! | `encrypt(name)` | `ciphertext().under(label)` | -//! | `index(name, x)` | [`Indexes::operations`](crate::Indexes::operations)`().under(label)` | -//! | `passthrough(name)` | [`passthrough`](crate::target::passthrough)`()` | -//! | `context_field(name)` | `passthrough()`, and its value is the context every other field runs under | -//! | `fields()` | each field picked out of the value (by name, or by a picker's accessor as `project` does), `zip`ped | -//! | `extend(parts)` | the [`DeclaredContext`](crate::target::DeclaredContext) the description runs under | -//! | `using(&plan)` | [`KeysetCipher::run`](crate::KeysetCipher::run) of [`Plan::encryption`] | -//! | `query(op, v).on(..)` | the answering index's operation alone, `.under(label)`, once per plaintext the operator takes ([`Input`](crate::op::Input)), mapped to its query term ([`Supports::query`](crate::op::Supports::query)) | -//! | `open(record).using(&plan)` | [`open`](crate::target::open) per field, run by `run_decryption` | -//! | `.await`, [`all`] | `Pending::zip`, then one settle | -//! -//! The cipher-directed and target-directed entry points on -//! [`KeysetCipher`](crate::KeysetCipher) remain; the chain is their fluent -//! spelling. The chain decrypts with [`open`](crate::StackCipher::open) -//! because [`StackCipher::decrypt`](crate::StackCipher::decrypt) is the -//! cipher-directed decrypt, and keeps its form. -mod build; -mod chain; +//! What a plan that is data shares with the rest of the crate: the errors a +//! plan is refused with ([`PlanError`]), and the label a plan's context is +//! written as ([`IntoLabel`]). +//! +//! A program does not build a plan in Rust. A record with static types +//! derives `EncryptTarget` and `DecryptSource`, and queries a field through +//! its marker ([`Marker`](crate::Marker)). A program whose declaration is +//! data parses a plan with the `dynamic` feature +//! (`dynamic::record::plan`) and runs it as a `dynamic::Planned` value. mod error; -mod field_ref; -mod value; +mod label; +#[cfg(feature = "dynamic")] mod values; -#[cfg(feature = "dynamic")] -pub(crate) use build::{field_label, join, FROM_CALL, FROM_PLAN}; -pub use build::{FieldKind, FieldPlan, FieldsBuilder, IntoLabel, Opens, Plan, PlanContext, Runs}; -pub use chain::{ - all, All, Batch, Chooses, EncryptBuilder, EncryptFields, EncryptIndexed, EncryptUsing, - EncryptWithContext, KeysetChoice, OpenBuilder, OpenUsing, Operation, PlanKms, QueryBuilder, - QueryOp, QueryVia, Resolves, -}; pub use error::PlanError; -pub use field_ref::{pick, FieldRef}; -pub use value::{ - ContextFieldStart, Indexed, Laid, Stored, Typed, ValueLayout, ValuePlan, ValuePlanBuilder, - ValueShape, ValueStart, -}; +pub use label::IntoLabel; +#[cfg(feature = "dynamic")] +pub(crate) use label::{field_label, join, FROM_CALL, FROM_PLAN}; #[cfg(feature = "dynamic")] -pub(crate) use values::Slot; -pub use values::{Field, FieldSchema, FieldValues, Fields}; +pub(crate) use values::{FieldValues, Slot}; diff --git a/packages/stack-encrypt/src/plan/value.rs b/packages/stack-encrypt/src/plan/value.rs deleted file mode 100644 index b80a2a71c7..0000000000 --- a/packages/stack-encrypt/src/plan/value.rs +++ /dev/null @@ -1,922 +0,0 @@ -//! One-value plans: how to encrypt a single plaintext. Start one with -//! [`Plan::value`], then choose its output in one of three ways: -//! -//! - [`with`](ValueStart::with): a ciphertext with the indexes you name -//! beside it, as an [`Encrypted<Terms>`]. -//! - [`encrypt_into`](ValueStart::encrypt_into): a target type, laid out -//! as that type says. -//! - [`encrypt_with_layout`](ValueStart::encrypt_with_layout): a target laid -//! out by a value with options, such as the JSON index with the paths it -//! orders ([`Layout`]). -use std::fmt; -use std::marker::PhantomData; - -use super::build::{ - check_declared, check_indexes, resolve_context, runs_over_collections, IntoLabel, Opens, - PlanContext, Runs, FROM_CALL, FROM_FIELD, FROM_PLAN, -}; -use super::PlanError; -use crate::target::{ - indexed, Borrowed, CallerContext, DeclaredContext, DecryptInto, Decryption, EncryptFrom, - Encrypted, Encryption, ExpectedContext, IndexSpec, Indexes, Layout, Pending, -}; -use crate::{ - Error, IntoContext, KeysetCipher, Label, LabelError, MaybeEmpty, NonEmpty, Plan, - StackCipherText, -}; -use stack_kms::MaybeSend; - -/// What a one-value plan's errors name where a fields plan's name a field. -/// Never the context: a context can carry customer data (a tenant, a user), -/// so an error reports none of it. -const THE_VALUE: &str = "the value"; - -impl Plan<(), ()> { - /// Start a one-value plan over plaintext `S`, with no context yet: give - /// it one with [`context`](ValueStart::context), or leave it for the - /// call that runs it. Then [`with`](ValueStart::with) its indexes or - /// [`encrypt_into`](ValueStart::encrypt_into) a target, and `build()`. - /// - /// ``` - /// # async fn example() -> Result<(), stack_encrypt::Error> { - /// use stack_encrypt::kms::FakeDataKeySource; - /// use stack_encrypt::sem::EqualityTerm; - /// use stack_encrypt::{op, Equality, Ore, Plan, StackCipher, StackCipherText}; - /// - /// let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; - /// - /// // With its context and indexes. - /// let age_plan = Plan::value::<u32>().context("users/age").with((Equality, Ore)).build()?; - /// let age = cipher.encrypt(&34u32).using(&age_plan).await?; - /// let back: u32 = cipher.open(age).using(&age_plan).await?; - /// assert_eq!(back, 34); - /// - /// // Laid out by a tuple of targets, its context named by each call: - /// // a `plaintext = String` record without the struct. - /// let email_plan = Plan::value::<String>() - /// .encrypt_into::<(StackCipherText, EqualityTerm)>() - /// .build()?; - /// let email = String::from("bob@example.com"); - /// let (c, eq) = cipher.encrypt(&email).context("users/email").using(&email_plan).await?; - /// let query_value = cipher - /// .query(op::Eq, "bob@example.com") - /// .context("users/email") - /// .on(&email_plan) - /// .await?; - /// assert_eq!(query_value, eq); - /// let back = cipher.open((c, eq)).context("users/email").using(&email_plan).await?; - /// assert_eq!(back, email); - /// # Ok(()) - /// # } - /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); - /// ``` - pub fn value<S>() -> ValueStart<S> { - ValueStart { - sources: Vec::new(), - plaintext: PhantomData, - } - } -} - -/// A one-value plan before its layout is chosen; see [`Plan::value`]. -pub struct ValueStart<S> { - sources: Vec<Result<Label, LabelError>>, - plaintext: PhantomData<fn(&S)>, -} - -impl<S> fmt::Debug for ValueStart<S> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("ValueStart") - .field("context", &self.sources) - .finish() - } -} - -impl<S> ValueStart<S> { - /// Seal the value under `context`, given when the plan is built. - pub fn context(mut self, context: impl IntoLabel) -> Self { - self.sources.push(context.into_label()); - self - } - - /// Seal the value with `indexes` beside it: one index, or a tuple of - /// two to five. The output is an [`Encrypted<Terms>`]. The data spelling - /// of `encrypt_into::<Encrypted<Terms>>()`: both lower to - /// [`indexed`]. - pub fn with<X: Indexes<S>>(self, indexes: X) -> ValuePlanBuilder<S, Indexed<X>> { - ValuePlanBuilder { - sources: self.sources, - shape: Indexed(indexes), - plaintext: PhantomData, - } - } - - /// Lay the value out as the target `T` does: `T`'s own - /// [`EncryptFrom<S>`] decides what is sealed and which terms sit beside - /// it. A tuple of targets, `encrypt_into::<(StackCipherText, - /// EqualityTerm)>()`, derives each element from the value under the one - /// context, which is what a `plaintext = S` derive's record is. Lowers - /// to `<T as EncryptFrom<S>>::encryption().under(context)`. - /// - /// A tuple target holding no ciphertext (terms only) builds, but `open` - /// on it fails with [`Error::NotOpened`](crate::Error::NotOpened): a - /// target of terms alone is a query-only plan, as a record deriving - /// `EncryptFrom` alone is. - pub fn encrypt_into<T>(self) -> ValuePlanBuilder<S, Typed<T>> - where - T: EncryptFrom<S>, - { - ValuePlanBuilder { - sources: self.sources, - shape: Typed(PhantomData), - plaintext: PhantomData, - } - } - - /// Encrypt the value as `layout` does, with the layout's options. The - /// plan's output is the layout's target. - /// - /// ``` - /// # async fn example() -> Result<(), stack_encrypt::Error> { - /// use serde_json::json; - /// use stack_encrypt::json::{JsonDocument, JsonOptions}; - /// use stack_encrypt::kms::FakeDataKeySource; - /// use stack_encrypt::target::Json; - /// use stack_encrypt::{op, Plan, StackCipher}; - /// - /// let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; - /// - /// // The JSON index, ordering the leaves at `$.age`. - /// let options = JsonOptions::default().ordering(["$.age"])?; - /// let plan = Plan::value::<serde_json::Value>() - /// .context("users/profile") - /// .encrypt_with_layout(Json::new(options)) - /// .build()?; - /// - /// // The output is a `JsonDocument`, with an ordering term on `$.age`. - /// let doc: JsonDocument = cipher.encrypt(&json!({"age": 34, "name": "Bob"})).using(&plan).await?; - /// assert_eq!(doc.entries().iter().filter(|e| e.ordering_term().is_some()).count(), 1); - /// - /// // A query through the plan uses the same options, so `$.age` can be compared. - /// let _at_least_18 = cipher.query(op::AtPath(op::Gte), &("$.age", 18)).on(&plan).await?; - /// # Ok(()) - /// # } - /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); - /// ``` - /// - /// Use this when the target needs options that its type cannot carry, - /// such as the paths the JSON index orders. With - /// [`encrypt_into::<JsonDocument>()`](Self::encrypt_into) the same - /// document is sealed under the default options, which order no path. - /// [`Layout`] says more. - pub fn encrypt_with_layout<X>(self, layout: X) -> ValuePlanBuilder<S, Laid<X, X::Target>> - where - X: Layout<S>, - { - ValuePlanBuilder { - sources: self.sources, - shape: Laid(layout, PhantomData), - plaintext: PhantomData, - } - } -} - -impl<S> ValueStart<S> { - /// Take the plan's context from the call, and carry it out beside the - /// output, typed `C`: the one-value form of a fields plan's - /// [`context_field`](super::FieldsBuilder::context_field), and what a - /// `plaintext = S` derive with `#[stash(context_field)]` is. - /// - /// The call hands the context over as a `NonEmpty<C>`; the output is - /// `(C, T)`, the context as given beside the target's layout. The - /// stored context is **unsealed and unauthenticated**, as a passthrough - /// is: what protects the record is that the target was sealed under it. - /// When the record is opened - /// ([`decryption_with_context`](ValuePlan::decryption_with_context)), the - /// stored context is checked first against the one the caller expects, - /// if the caller names one: a mismatch is - /// [`Error::ContextMismatch`](crate::Error::ContextMismatch), before any - /// key is requested. - /// - /// A context field is the plan's one context source, so a build-time - /// [`context`](Self::context) beside it is - /// [`PlanError::TwoContextSources`] at `build()`. - /// - /// Unlike a fields plan's context field, the context here is a `C`, not - /// a [`Label`], so the plan does not run through the chain: - /// `cipher.encrypt(..).using`, `cipher.open(..).using` and - /// `cipher.query(..).on` do not take it (it is neither - /// [`Runs`](super::Runs) nor [`Opens`](super::Opens)), and it has no - /// query path. Run it as the derive does, with its two descriptions: - /// `keyset.run(plan.encryption_with_context(), &value, context)` and - /// `keyset.run_decryption(plan.decryption_with_context(record, - /// expected))`. - pub fn context_field<C>(self) -> ContextFieldStart<S, C> { - ContextFieldStart { - sources: self.sources, - plaintext: PhantomData, - } - } -} - -/// A one-value plan whose context is carried out beside its output, before -/// its layout is chosen; see [`ValueStart::context_field`]. -pub struct ContextFieldStart<S, C> { - sources: Vec<Result<Label, LabelError>>, - plaintext: PhantomData<fn(&S) -> C>, -} - -impl<S, C> fmt::Debug for ContextFieldStart<S, C> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("ContextFieldStart") - .field("context", &self.sources) - .field("stored", &std::any::type_name::<C>()) - .finish() - } -} - -impl<S, C> ContextFieldStart<S, C> { - /// Lay the value out as the target `T` does, under the call's context, - /// and carry that context out beside it: the output is `(C, T)`. - pub fn encrypt_into<T>(self) -> ValuePlanBuilder<S, Stored<C, T>> - where - T: EncryptFrom<S>, - { - ValuePlanBuilder { - sources: self.sources, - shape: Stored(PhantomData), - plaintext: PhantomData, - } - } -} - -/// A one-value plan's layout when its context is carried out beside its -/// target `T` ([`ValueStart::context_field`]): its output is `(C, T)`. Run -/// through [`ValuePlan::encryption_with_context`] and -/// [`ValuePlan::decryption_with_context`] only; see -/// [`context_field`](ValueStart::context_field). -pub struct Stored<C, T>(PhantomData<fn() -> (C, T)>); - -impl<C, T> Clone for Stored<C, T> { - fn clone(&self) -> Self { - Self(PhantomData) - } -} -impl<C, T> fmt::Debug for Stored<C, T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "Stored<{}, {}>", - std::any::type_name::<C>(), - std::any::type_name::<T>() - ) - } -} - -impl<S, C, T: EncryptFrom<S>> ValuePlanBuilder<S, Stored<C, T>> { - /// Validate the plan: no build-time context beside the context field, - /// and no index named twice. - /// - /// # Errors - /// - /// [`PlanError::TwoContextSources`] or [`PlanError::DuplicateIndex`], - /// in [`Error::Plan`]. - pub fn build(self) -> Result<ValuePlan<S, Stored<C, T>>, Error> { - if !self.sources.is_empty() { - return Err(PlanError::TwoContextSources { - first: FROM_PLAN, - second: FROM_FIELD, - } - .into()); - } - check_indexes(THE_VALUE, &T::indexes())?; - Ok(ValuePlan { - context: None, - shape: self.shape, - plaintext: PhantomData, - }) - } -} - -impl<S, C, T> ValuePlan<S, Stored<C, T>> { - /// The description of one run: `T`'s layout under the context the call - /// hands over, which is carried out beside it as the record's `C`. - /// Run it with [`KeysetCipher::run`] and a `NonEmpty<C>`. - pub fn encryption_with_context<'s, K: 'static>( - &self, - ) -> Encryption<'s, S, (C, T), K, NonEmpty<C>> - where - S: 's, - T: EncryptFrom<S>, - CallerContext: Into<T::Context>, - NonEmpty<C>: Into<CallerContext>, - C: Clone + MaybeSend + 'static, - { - T::encryption() - .accepting::<CallerContext>() - .accepting::<NonEmpty<C>>() - .map_with_context(|target, context: NonEmpty<C>| (context.into_inner(), target)) - } - - /// The opening of a stored `(C, T)`: the stored context checked against - /// `expected` (`None` leaves it unchecked beyond being nonempty), before - /// any key is requested, then `T` opened under it through its own - /// [`DecryptInto`]. - pub fn decryption_with_context<K: 'static>( - &self, - record: (C, T), - expected: Option<NonEmpty<C>>, - ) -> Decryption<S, K> - where - S: 'static, - T: DecryptInto<S>, - NonEmpty<C>: Into<T::Context>, - C: MaybeEmpty + PartialEq + IntoContext<'static>, - { - let (stored, target) = record; - let expected = expected.map_or_else(ExpectedContext::default, ExpectedContext::from); - match expected.validate(stored) { - Ok(context) => target.decryption(context.into()), - Err(error) => Decryption::failed(error), - } - } -} - -impl PlanContext { - /// One value, sealed under the context with `indexes` beside it: a - /// one-value plan, whose output is an [`Encrypted<Terms>`]. - pub fn with<S, X: Indexes<S>>(self, indexes: X) -> ValuePlanBuilder<S, Indexed<X>> { - ValuePlanBuilder { - sources: vec![self.into_result()], - shape: Indexed(indexes), - plaintext: PhantomData, - } - } -} - -/// A one-value plan's layout when it was given its indexes -/// ([`with`](ValueStart::with)): its output is an [`Encrypted<Terms>`], -/// and a query selects its index by type. -#[derive(Clone, Debug)] -pub struct Indexed<X>(X); - -/// A one-value plan's layout when it names a target -/// ([`encrypt_into`](ValueStart::encrypt_into)): its output is a `T`, and a -/// query may ask for the indexes `T` declares ([`EncryptFrom::indexes`]). -pub struct Typed<T>(PhantomData<fn() -> T>); - -impl<T> Clone for Typed<T> { - fn clone(&self) -> Self { - Self(PhantomData) - } -} -impl<T> fmt::Debug for Typed<T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Typed<{}>", std::any::type_name::<T>()) - } -} - -/// A one-value plan's layout when it was given one by value -/// ([`encrypt_with_layout`](ValueStart::encrypt_with_layout)): the layout -/// `X`, whose target `T` is its output, and a query runs through the -/// layout's own index, with its options ([`Layout::index`]). `T` is the -/// layout's [`Target`](Layout::Target), spelled out so that opening one -/// `T` and a `Vec<T>` are told apart as they are for a [`Typed`] plan. -pub struct Laid<X, T>(X, PhantomData<fn() -> T>); - -impl<X: Clone, T> Clone for Laid<X, T> { - fn clone(&self) -> Self { - Self(self.0.clone(), PhantomData) - } -} -impl<X: fmt::Debug, T> fmt::Debug for Laid<X, T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Laid<{}>({:?})", std::any::type_name::<T>(), self.0) - } -} - -mod sealed { - pub trait Sealed {} - impl<X> Sealed for super::Indexed<X> {} - impl<T> Sealed for super::Typed<T> {} - impl<X, T> Sealed for super::Laid<X, T> {} -} - -/// What a one-value plan's layout says before it runs: what one run -/// produces, and the indexes it declares. Enough to build the plan -/// ([`ValuePlanBuilder::build`]), which checks those indexes; running it -/// under a [`Label`] asks for [`ValueShape`]. Sealed. -/// -/// A [`Typed`] layout is one for every target, whatever context the target -/// takes; it is a [`ValueShape`] only when a [`CallerContext`] converts -/// into that context. A target whose context is, say, a `NonEmpty<u64>` -/// builds, and runs through -/// [`encryption_with_context`](ValuePlan::encryption_with_context). -/// -/// A query through a [`Typed`] plan whose target is a -/// [`LabelledTarget`](crate::op::LabelledTarget) needs only this: the query -/// derives through the index the target's `Answers` impl names, under the -/// plan's label, and never runs the target's encryption. So an EQL type, -/// sealed under its column rather than the label, is queried through its -/// plan though it cannot be encrypted through it. Naming another index with -/// `.via(..)` needs [`ValueShape`]. -pub trait ValueLayout<S>: sealed::Sealed + Clone { - /// What one run produces. - type Output: 'static; - /// The indexes the value is searchable by, as data. - fn specs(&self) -> Vec<IndexSpec>; -} - -/// How a one-value plan lays its value out under a [`Label`]: [`Indexed`] -/// or [`Typed`]. What [`ValuePlan::encryption`], `cipher.encrypt(..).using` -/// and a query that names its index with `.via(..)` ask. A query that lets -/// a [`Typed`] plan choose the index asks only [`ValueLayout`], when the -/// target is a [`LabelledTarget`](crate::op::LabelledTarget). Sealed. -pub trait ValueShape<S>: ValueLayout<S> { - /// The description of one value, run under its whole context. - fn lower<'s, K: 'static>(&self) -> Encryption<'s, S, Self::Output, K, CallerContext> - where - S: 's; -} - -impl<S, X> ValueLayout<S> for Indexed<X> -where - S: crate::Encrypt + Clone, - X: Indexes<S> + Clone, -{ - type Output = Encrypted<X::Terms>; - fn specs(&self) -> Vec<IndexSpec> { - self.0.specs() - } -} - -impl<S, X> ValueShape<S> for Indexed<X> -where - S: crate::Encrypt + Clone, - X: Indexes<S> + Clone, -{ - fn lower<'s, K: 'static>(&self) -> Encryption<'s, S, Self::Output, K, CallerContext> - where - S: 's, - { - indexed::<S, K, Borrowed, X>(self.0.clone()) - } -} - -impl<S, T> ValueLayout<S> for Typed<T> -where - T: EncryptFrom<S>, -{ - type Output = T; - fn specs(&self) -> Vec<IndexSpec> { - T::indexes() - } -} - -impl<S, T> ValueShape<S> for Typed<T> -where - T: EncryptFrom<S>, - CallerContext: Into<T::Context>, -{ - fn lower<'s, K: 'static>(&self) -> Encryption<'s, S, T, K, CallerContext> - where - S: 's, - { - T::encryption().accepting::<CallerContext>() - } -} - -impl<S, X, T> ValueLayout<S> for Laid<X, T> -where - X: Layout<S, Target = T>, - T: EncryptFrom<S>, -{ - type Output = T; - fn specs(&self) -> Vec<IndexSpec> { - self.0.specs() - } -} - -impl<S, X, T> ValueShape<S> for Laid<X, T> -where - X: Layout<S, Target = T>, - T: EncryptFrom<S>, - CallerContext: Into<T::Context>, -{ - fn lower<'s, K: 'static>(&self) -> Encryption<'s, S, T, K, CallerContext> - where - S: 's, - { - self.0.encryption().accepting::<CallerContext>() - } -} - -/// A one-value plan under construction; see [`ValuePlan`]. -pub struct ValuePlanBuilder<S, X> { - sources: Vec<Result<Label, LabelError>>, - shape: X, - plaintext: PhantomData<fn(&S)>, -} - -impl<S, X: fmt::Debug> fmt::Debug for ValuePlanBuilder<S, X> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("ValuePlanBuilder") - .field("context", &self.sources) - .field("shape", &self.shape) - .finish() - } -} - -impl<S, X: ValueLayout<S>> ValuePlanBuilder<S, X> { - /// Validate the plan: at most one context, a plain label, and no index - /// named twice. A plan with no context builds; each call names one. - /// - /// # Errors - /// - /// [`PlanError::TwoContextSources`], [`PlanError::ContextLabel`] or - /// [`PlanError::DuplicateIndex`], in [`Error::Plan`]. - pub fn build(self) -> Result<ValuePlan<S, X>, Error> { - let context = self.checked_context()?; - Ok(ValuePlan { - context, - shape: self.shape, - plaintext: PhantomData, - }) - } - - /// Validate the plan as [`build`](Self::build) does, without building - /// it: what a chain asks before it loads a keyset. - pub(super) fn check(&self) -> Result<(), Error> { - self.checked_context().map(drop) - } - - /// The context, if any, once the plan has passed - /// [`build`](Self::build)'s checks. - fn checked_context(&self) -> Result<Option<Label>, Error> { - let mut sources = self.sources.iter().cloned(); - let context = match (sources.next(), sources.next()) { - (None, _) => None, - (Some(context), None) => Some(context.map_err(PlanError::ContextLabel)?), - (Some(_), Some(_)) => { - return Err(PlanError::TwoContextSources { - first: FROM_PLAN, - second: FROM_PLAN, - } - .into()) - } - }; - check_indexes(THE_VALUE, &self.shape.specs())?; - Ok(context) - } -} - -/// A one-value plan: a context (or none, for the call to name), and how the -/// value is laid out: its indexes ([`Indexed`], from `with`) or a target -/// ([`Typed`], from `encrypt_into`). Built from [`Plan::value`] or -/// `Plan::context(..).with(..)`. -/// -/// It carries its plaintext type, so decrypting through it yields an `S`. -/// A query through an [`Indexed`] plan selects its index by type -/// ([`Indexes::select`]): asking for one it does not hold does not compile. -/// Through a [`Typed`] plan, asking for an index the target does not declare -/// is [`PlanError::IndexNotDeclared`]. -pub struct ValuePlan<S, X> { - context: Option<Label>, - shape: X, - plaintext: PhantomData<fn(&S)>, -} - -impl<S, X: Clone> Clone for ValuePlan<S, X> { - fn clone(&self) -> Self { - Self { - context: self.context.clone(), - shape: self.shape.clone(), - plaintext: PhantomData, - } - } -} - -impl<S, X: fmt::Debug> fmt::Debug for ValuePlan<S, X> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("ValuePlan") - .field("context", &self.context.as_ref().map(Label::to_string)) - .field("shape", &self.shape) - .finish() - } -} - -impl<S, X> ValuePlan<S, X> { - /// The label the value is sealed and indexed under, when the plan was - /// built with one. - pub fn label(&self) -> Option<&Label> { - self.context.as_ref() - } - - /// The label one run uses, given the context the call names. - pub(crate) fn resolve(&self, call: Option<Label>) -> Result<Label, PlanError> { - resolve_context(self.context.as_ref(), call) - } - - /// The context the call names against the plan's own: exactly one of - /// them, which is all a one-value plan checks before a run or an - /// opening. - fn check_call(&self, call: Option<&Label>) -> Result<(), Error> { - Ok(self.resolve(call.cloned()).map(drop)?) - } - - /// The description one run of this plan executes, given the context the - /// call names (for a plan built without one; `None` otherwise): - /// the layout `.under(context)`. A context missing or given twice is a - /// description that fails without I/O. - pub fn encryption<'s, K: 'static>( - &self, - context: Option<Label>, - ) -> Encryption<'s, S, X::Output, K, DeclaredContext> - where - S: 's, - X: ValueShape<S>, - { - match self.resolve(context) { - Ok(label) => self.shape.lower().under(NonEmpty::from(label)), - Err(error) => Encryption::failed(error.into()), - } - } -} - -impl<S, X> ValuePlan<S, Indexed<X>> { - /// The index set. - pub fn indexes(&self) -> &X { - &self.shape.0 - } -} - -impl<S, T> ValuePlan<S, Typed<T>> -where - Typed<T>: ValueLayout<S>, -{ - /// The label a query through `index` is derived under, or why the plan - /// cannot answer it. It reads only the indexes `T` declares - /// ([`ValueLayout`]); which plans may be queried at all is the query - /// chain's (`Chooses`, `Resolves`). - pub(crate) fn query_label( - &self, - index: &IndexSpec, - call: Option<Label>, - ) -> Result<Label, PlanError> { - let label = self.resolve(call)?; - check_declared(THE_VALUE, &self.shape.specs(), index)?; - Ok(label) - } -} - -impl<S, X, T> ValuePlan<S, Laid<X, T>> -where - X: Layout<S, Target = T>, -{ - /// The layout: the index a query runs through, with its options. - pub fn layout(&self) -> &X { - &self.shape.0 - } -} - -impl<S, X: ValueLayout<S>> ValuePlan<S, X> { - /// The indexes the plan declares, as data: what a query through it may - /// ask for ([`ValueLayout::specs`]). - pub fn specs(&self) -> Vec<IndexSpec> { - self.shape.specs() - } -} - -impl<S, T> ValuePlan<S, Typed<T>> { - /// The description of one run under a context the caller hands over - /// whole, of any type `C` the target accepts: `T`'s layout, - /// `.accepting::<C>()`. This is how a derived `plaintext = S` record - /// runs its plan, so a caller's context reaches every output exactly as - /// it was given (a `NonEmpty<_>` of any context type, an integer, or an - /// [`AeadContext`](crate::target::AeadContext) for a target of - /// ciphertexts alone), not parsed into a [`Label`] first. - /// - /// For a plan built without a context; one built with its own fails - /// with [`PlanError::TwoContextSources`], without I/O. - pub fn encryption_with_context<'s, K: 'static, C>(&self) -> Encryption<'s, S, T, K, C> - where - S: 's, - T: EncryptFrom<S>, - C: Into<T::Context> + 's, - { - match self.context { - None => T::encryption().accepting::<C>(), - Some(_) => Encryption::failed( - PlanError::TwoContextSources { - first: FROM_PLAN, - second: FROM_CALL, - } - .into(), - ), - } - } -} - -impl<S, X, K> Runs<S, K> for ValuePlan<S, X> -where - X: ValueShape<S>, - K: 'static, -{ - type Output = X::Output; - fn pending<'p>( - &self, - keyset: &'p KeysetCipher<'_, K>, - source: &S, - context: Option<Label>, - extend: DeclaredContext, - ) -> Pending<'p, Self::Output, K> { - keyset.run(self.encryption(context), source, extend) - } - - fn check(&self, _source: &S, context: Option<&Label>) -> Result<(), Error> { - self.check_call(context) - } -} - -runs_over_collections! { - [S, X, K] ValuePlan<S, X> => S where [X: ValueShape<S>, K: 'static] check_call; -} - -impl<S, X> ValuePlan<S, X> { - /// Open one stored `R` under the plan's context (or the call's), - /// extended by `extend`. - fn open_one<R, K>( - &self, - record: R, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<S, K> - where - R: DecryptInto<S>, - CallerContext: Into<R::Context>, - S: 'static, - K: 'static, - { - match self.resolve(context) { - Ok(label) => record.decryption(extend.under(NonEmpty::from(label)).into()), - Err(error) => Decryption::failed(error.into()), - } - } - - /// Open every stored `R` in one batch: one key request. - fn open_all<R, K>( - &self, - records: Vec<R>, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<Vec<S>, K> - where - R: DecryptInto<S>, - CallerContext: Into<R::Context>, - S: 'static, - K: 'static, - { - Decryption::all( - records - .into_iter() - .map(|record| self.open_one(record, context.clone(), extend.clone())), - ) - } -} - -/// An [`Indexed`] plan opens what it wrote, an [`Encrypted<Terms>`], or its -/// ciphertext alone, one or a `Vec` of them. -macro_rules! indexed_opens { - ($([$($generics:tt)*] $record:ty;)+) => {$( - impl<S, X, K, $($generics)*> Opens<$record, K> for ValuePlan<S, Indexed<X>> - where - S: crate::Decrypt + Send + 'static, - K: 'static, - { - type Output = S; - fn decryption( - &self, - record: $record, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<S, K> { - self.open_one(record, context, extend) - } - fn check(&self, _: &$record, context: Option<&Label>) -> Result<(), Error> { - self.check_call(context) - } - } - impl<S, X, K, $($generics)*> Opens<Vec<$record>, K> for ValuePlan<S, Indexed<X>> - where - S: crate::Decrypt + Send + 'static, - K: 'static, - { - type Output = Vec<S>; - fn decryption( - &self, - records: Vec<$record>, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<Vec<S>, K> { - self.open_all(records, context, extend) - } - fn check(&self, _: &Vec<$record>, context: Option<&Label>) -> Result<(), Error> { - self.check_call(context) - } - } - )+}; -} -indexed_opens! { - [Terms] Encrypted<Terms>; - [] StackCipherText; -} - -/// A [`Typed`] plan opens its target, one or a `Vec` of them, through the -/// target's own [`DecryptInto`]. -impl<S, T, K> Opens<T, K> for ValuePlan<S, Typed<T>> -where - T: DecryptInto<S>, - CallerContext: Into<T::Context>, - S: 'static, - K: 'static, -{ - type Output = S; - fn decryption( - &self, - record: T, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<S, K> { - self.open_one(record, context, extend) - } - fn check(&self, _: &T, context: Option<&Label>) -> Result<(), Error> { - self.check_call(context) - } -} - -impl<S, T, K> Opens<Vec<T>, K> for ValuePlan<S, Typed<T>> -where - T: DecryptInto<S>, - CallerContext: Into<T::Context>, - S: 'static, - K: 'static, -{ - type Output = Vec<S>; - fn decryption( - &self, - records: Vec<T>, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<Vec<S>, K> { - self.open_all(records, context, extend) - } - fn check(&self, _: &Vec<T>, context: Option<&Label>) -> Result<(), Error> { - self.check_call(context) - } -} - -/// A [`Laid`] plan opens its layout's target, one or a `Vec` of them, -/// through the target's own [`DecryptInto`], as a [`Typed`] plan does. -impl<S, X, T, K> Opens<T, K> for ValuePlan<S, Laid<X, T>> -where - X: Layout<S, Target = T>, - T: DecryptInto<S>, - CallerContext: Into<T::Context>, - S: 'static, - K: 'static, -{ - type Output = S; - fn decryption( - &self, - record: T, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<S, K> { - self.open_one(record, context, extend) - } - fn check(&self, _: &T, context: Option<&Label>) -> Result<(), Error> { - self.check_call(context) - } -} - -impl<S, X, T, K> Opens<Vec<T>, K> for ValuePlan<S, Laid<X, T>> -where - X: Layout<S, Target = T>, - T: DecryptInto<S>, - CallerContext: Into<T::Context>, - S: 'static, - K: 'static, -{ - type Output = Vec<S>; - fn decryption( - &self, - records: Vec<T>, - context: Option<Label>, - extend: DeclaredContext, - ) -> Decryption<Vec<S>, K> { - self.open_all(records, context, extend) - } - fn check(&self, _: &Vec<T>, context: Option<&Label>) -> Result<(), Error> { - self.check_call(context) - } -} diff --git a/packages/stack-encrypt/src/plan/values.rs b/packages/stack-encrypt/src/plan/values.rs index 10f511bcaa..be2dc8d381 100644 --- a/packages/stack-encrypt/src/plan/values.rs +++ b/packages/stack-encrypt/src/plan/values.rs @@ -1,114 +1,12 @@ -//! How a value is taken apart field by field ([`Fields`], [`Field`]), and the -//! map-like record a fields plan produces and reads ([`FieldValues`]). -use std::any::{type_name, Any, TypeId}; +//! The record of named values a plan that is data produces and reads +//! ([`FieldValues`]). Internal to the dynamic path. +use std::any::{type_name, Any}; use std::fmt; use std::sync::Arc; use super::PlanError; use crate::Error; -/// A value a fields plan can take apart: it names its top-level fields. -/// -/// A fields plan is fail-closed in both directions: every field the value -/// names must be named by the plan, and every field the plan names must be -/// in the value. [`field_names`](Self::field_names) is what the plan checks -/// the value against when it runs; [`schema`](Self::schema), when a type -/// fixes its fields, lets [`build`](super::FieldsBuilder::build) make the -/// same check, and the type check, before there is a value at all. -/// -/// Each field is read through [`Field<F>`], one implementation per field -/// type. That is what `#[derive(EncryptFrom)]` is expected to emit for a -/// record, and what a binding's dynamic value implements with `F` its one -/// runtime value type. -/// -/// ``` -/// use stack_encrypt::plan::{Field, FieldSchema, Fields}; -/// -/// struct User { -/// email: String, -/// age: u32, -/// } -/// -/// impl Fields for User { -/// fn field_names(&self) -> Vec<&str> { -/// vec!["email", "age"] -/// } -/// fn schema() -> Option<Vec<FieldSchema>> { -/// Some(vec![FieldSchema::of::<String>("email"), FieldSchema::of::<u32>("age")]) -/// } -/// } -/// impl Field<String> for User { -/// fn field(&self, name: &str) -> Option<&String> { -/// (name == "email").then_some(&self.email) -/// } -/// } -/// impl Field<u32> for User { -/// fn field(&self, name: &str) -> Option<&u32> { -/// (name == "age").then_some(&self.age) -/// } -/// } -/// ``` -pub trait Fields { - /// The names of this value's top-level fields. - fn field_names(&self) -> Vec<&str>; - - /// The fields every value of this type has, with their types, when the - /// type fixes them; `None` (the default) when only a value knows, as for - /// a dynamic map. With a schema, a plan that names a field the type does - /// not have, leaves one of its fields unnamed, or names one at the wrong - /// type is refused by `build()` rather than when it first runs. - fn schema() -> Option<Vec<FieldSchema>> - where - Self: Sized, - { - None - } -} - -/// Read one field of a [`Fields`] value as `F`. -/// -/// Returns `None` when the value has no field of that name, or has one of -/// another type; a plan tells the two apart through -/// [`field_names`](Fields::field_names) and reports which. -pub trait Field<F>: Fields { - /// The field named `name`, if the value has one of type `F`. - fn field(&self, name: &str) -> Option<&F>; -} - -/// One field of a type's [`schema`](Fields::schema): its name and type. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct FieldSchema { - name: &'static str, - type_id: TypeId, - type_name: &'static str, -} - -impl FieldSchema { - /// The field `name`, of type `F`. - pub fn of<F: 'static>(name: &'static str) -> Self { - Self { - name, - type_id: TypeId::of::<F>(), - type_name: type_name::<F>(), - } - } - - /// The field's name. - pub fn name(&self) -> &'static str { - self.name - } - - /// The field's type, by id. - pub fn type_id(&self) -> TypeId { - self.type_id - } - - /// The field's type, by name, for messages. - pub fn type_name(&self) -> &'static str { - self.type_name - } -} - /// One value in a [`FieldValues`]: the value, and its type's name so the /// record can say what it holds without showing it. pub(crate) struct Slot { @@ -138,48 +36,26 @@ impl Slot { self.value.is::<T>() } - /// The value as a `&T`, if it is one. - pub(crate) fn downcast_ref<T: Any>(&self) -> Option<&T> { - self.value.downcast_ref::<T>() - } - pub(crate) fn type_name(&self) -> &'static str { self.type_name } } -/// A record of named fields: what a fields plan produces when it encrypts -/// a value, what it reads when it decrypts one, and what decrypting -/// produces. -/// -/// It is the plan's output shape, not a type the caller declares: a plan -/// is data, built at run time by a person, the derive or a binding, so its -/// output cannot be a Rust struct named in advance. Each field holds the -/// type its verb produces, and is read back by name and type with -/// [`take`](Self::take) or [`get`](Self::get): -/// -/// | field verb | encrypted field | decrypted field | -/// |---|---|---| -/// | `encrypt` | [`StackCipherText`](crate::StackCipherText) | the field's type | -/// | `encrypt_index` | [`Encrypted<Terms>`](crate::Encrypted) | the field's type | -/// | `index` | the index set's terms (a term, or a tuple of them) | absent: terms are one-way | -/// | `passthrough` | the field's type, unchanged | the field's type | -/// -/// A typed record is the derive's job: it converts this into a struct with -/// the same field names. A record read from storage is assembled with -/// [`insert`](Self::insert); an `encrypt_index` field may then hold its -/// `StackCipherText` alone, since decrypting needs only the ciphertext. +/// A record of named fields: what a plan that is data produces when it +/// encrypts a record, and what it reads when it decrypts one. Each field +/// holds the type its declaration produces, and is read back by name and +/// type. /// /// Its [`Debug`](fmt::Debug) shows each field's name and type, never a /// value: a decrypted record holds plaintext. #[derive(Default)] -pub struct FieldValues { +pub(crate) struct FieldValues { slots: Vec<(Arc<str>, Slot)>, } impl FieldValues { /// An empty record. - pub fn new() -> Self { + pub(crate) fn new() -> Self { Self::default() } @@ -188,7 +64,7 @@ impl FieldValues { } /// Set the field `name` to `value`, replacing any value it had. - pub fn insert<T: Any + Send>(&mut self, name: &str, value: T) -> &mut Self { + pub(crate) fn insert<T: Any + Send>(&mut self, name: &str, value: T) -> &mut Self { let slot = Slot::new(value); match self.slots.iter_mut().find(|(have, _)| &**have == name) { Some((_, existing)) => *existing = slot, @@ -198,7 +74,7 @@ impl FieldValues { } /// The field `name`, if the record has it and it is a `T`. - pub fn get<T: Any>(&self, name: &str) -> Option<&T> { + pub(crate) fn get<T: Any>(&self, name: &str) -> Option<&T> { self.slots .iter() .find(|(have, _)| &**have == name) @@ -212,7 +88,7 @@ impl FieldValues { /// [`PlanError::NotInValue`] if the record has no such field, and /// [`PlanError::FieldType`] if it is not a `T`, in which case the field /// stays in the record. - pub fn take<T: Any>(&mut self, name: &str) -> Result<T, Error> { + pub(crate) fn take<T: Any>(&mut self, name: &str) -> Result<T, Error> { let at = self .slots .iter() @@ -244,30 +120,15 @@ impl FieldValues { .map(|(_, slot)| slot) } - /// Remove the field `name`, whatever it holds; whether there was one. - pub fn remove(&mut self, name: &str) -> bool { - self.take_slot(name).is_some() - } - /// Whether the record has a field `name`. - pub fn contains(&self, name: &str) -> bool { + pub(crate) fn contains(&self, name: &str) -> bool { self.slots.iter().any(|(have, _)| &**have == name) } /// The field names, in order. - pub fn names(&self) -> impl ExactSizeIterator<Item = &str> + '_ { + pub(crate) fn names(&self) -> impl ExactSizeIterator<Item = &str> + '_ { self.slots.iter().map(|(name, _)| &**name) } - - /// How many fields the record has. - pub fn len(&self) -> usize { - self.slots.len() - } - - /// Whether the record has no fields. - pub fn is_empty(&self) -> bool { - self.slots.is_empty() - } } impl fmt::Debug for FieldValues { @@ -280,21 +141,6 @@ impl fmt::Debug for FieldValues { } } -/// A record is itself a value a plan can take apart: a binding that builds -/// records at run time, or a test, hands one to a fields plan directly. Every -/// field is read at whatever type the plan names, and refused at any other. -impl Fields for FieldValues { - fn field_names(&self) -> Vec<&str> { - self.names().collect() - } -} - -impl<F: Any> Field<F> for FieldValues { - fn field(&self, name: &str) -> Option<&F> { - self.get(name) - } -} - #[cfg(test)] mod tests { use super::*; @@ -305,9 +151,7 @@ mod tests { values.insert("a", 1u32).insert("b", 2u32).insert("a", 3u32); assert_eq!(values.names().collect::<Vec<_>>(), ["a", "b"]); assert_eq!(values.get::<u32>("a"), Some(&3)); - assert_eq!(values.len(), 2); - assert!(!values.is_empty()); - assert!(FieldValues::new().is_empty()); + assert_eq!(FieldValues::new().names().count(), 0); } #[test] @@ -330,12 +174,12 @@ mod tests { ["age", "email"], "a wrong-type take leaves the field where it was" ); - assert!(values.remove("email")); + assert!(values.take_slot("email").is_some()); assert_eq!(values.take::<u32>("age").unwrap(), 34); assert!(!values.contains("age"), "a take removes the field"); values.insert("age", 34u32); - assert!(values.remove("age")); - assert!(!values.remove("age"), "nothing left to remove"); + assert!(values.take_slot("age").is_some()); + assert!(values.take_slot("age").is_none(), "nothing left to take"); assert!(!values.contains("age")); } @@ -351,17 +195,7 @@ mod tests { fn a_record_reads_its_fields_at_the_type_asked_for() { let mut values = FieldValues::new(); values.insert("age", 34u32); - assert_eq!(values.field_names(), ["age"]); - assert_eq!(Field::<u32>::field(&values, "age"), Some(&34)); - assert_eq!(Field::<u64>::field(&values, "age"), None); - assert_eq!(FieldValues::schema(), None); - } - - #[test] - fn a_schema_entry_reports_its_name_and_type() { - let schema = FieldSchema::of::<u32>("age"); - assert_eq!(schema.name(), "age"); - assert_eq!(schema.type_id(), TypeId::of::<u32>()); - assert_eq!(schema.type_name(), "u32"); + assert_eq!(values.get::<u32>("age"), Some(&34)); + assert_eq!(values.get::<u64>("age"), None); } } diff --git a/packages/stack-encrypt/src/scalar.rs b/packages/stack-encrypt/src/scalar.rs index ae2ee9abbf..59a55aa56f 100644 --- a/packages/stack-encrypt/src/scalar.rs +++ b/packages/stack-encrypt/src/scalar.rs @@ -196,7 +196,7 @@ impl Ciphertext { } } } -/// The one sealed leaf, as the structural API holds it. The bytes are the +/// The one sealed leaf, as a tree of leaves holds it. The bytes are the /// same: this is how a caller holding a `CipherText::Single` from /// [`KeysetCipher::encrypt_tree`] opens it through the owned API. impl From<SealedValue> for Ciphertext { diff --git a/packages/stack-encrypt/src/sem/mod.rs b/packages/stack-encrypt/src/sem/mod.rs index 45eaf7eed8..f13e18d84e 100644 --- a/packages/stack-encrypt/src/sem/mod.rs +++ b/packages/stack-encrypt/src/sem/mod.rs @@ -3,7 +3,7 @@ //! Index terms are stored alongside a //! [`StackCipherText`](crate::StackCipherText) so encrypted values can be //! queried without decryption. Each term type implements -//! [`EncryptFrom`](crate::EncryptFrom), so the usual entry point is +//! [`EncryptTarget`](crate::EncryptTarget), so the usual entry point is //! target-directed: //! //! ```text @@ -53,7 +53,7 @@ //! [`HmacSha256Prf`] — keyed by the //! deterministic per-keyset [`IndexKey`](crate::kms::IndexKey) from //! [`kms::IndexKeySource`](crate::kms::IndexKeySource), loaded when the keyset is selected — so -//! every derivation completes with no I/O and an [`EncryptFrom`](crate::EncryptFrom) term +//! every derivation completes with no I/O and an [`EncryptTarget`](crate::EncryptTarget) term //! carries **no requests** in its [`Pending`]. That is the backend's //! property, not the API's: the term is a `Pending` either way. The next //! ZeroKMS release adds 2-party PRF generation; under @@ -282,8 +282,8 @@ pub enum TermError { /// A match query's text produced no tokens under the index's tokenizer: /// empty or separator-only text, or (for n-grams, as in the v1 match /// indexer) text shorter than the n-gram length. A query with no tokens - /// would match every row, so the query chain refuses it when the query - /// is awaited, before a term exists. A *stored* value with no tokens is + /// would match every row, so a query refuses it when it is encrypted, + /// before a term exists. A *stored* value with no tokens is /// not refused: it is stored as the empty filter, which no query /// matches, so a short or empty value is a valid thing to hold in a /// match column. The empty filter shows that the value cut no token @@ -865,7 +865,7 @@ impl<O> MatchTerms<O> { /// /// An empty `query` returns `false`: containment of zero positions is /// vacuously true, which would turn an empty query into a match-every-row - /// query. A query chain already refuses to build such a query + /// query. A field marker's query already refuses to build such a query /// ([`TermError::EmptyTermText`]); this guards any other (e.g. /// deserialized) source of an empty one. The stored side is the other /// way round: a value with no tokens is stored as the empty filter, and @@ -973,7 +973,7 @@ impl<O> Eq for MatchTerms<O> {} /// The two are different types because they are different things, even /// where their bytes share an encoding: a query is never stored, and a /// stored filter is never sent as a query. A -/// `MatchQuery` comes from a query chain ([`op::Matches`](struct@crate::op::Matches)), +/// `MatchQuery` comes from a field marker's query ([`op::Matches`](struct@crate::op::Matches)), /// never from a target. /// /// A Bloom filter can hold every bit of the query without the value holding @@ -1086,7 +1086,7 @@ impl<O> Eq for MatchQuery<O> {} /// term the index would send for that value alone, which is how a query /// domain that carries a range serialises it. /// -/// It comes only from a query chain: a range is never stored. Both ends +/// It comes only from a field marker's query: a range is never stored. Both ends /// are inclusive, and a range whose low bound is above its high bound is /// not refused: as SQL's `BETWEEN`, it matches nothing. #[derive(Clone, Debug, PartialEq, Eq)] @@ -1120,8 +1120,8 @@ impl<T> RangeQuery<T> { /// The query's tokens, as a match index cuts them from the query's value: /// a match query's verifier, which a [`MatchQuery`] keeps to verify a row. Plaintext, so it is /// zeroized on drop and has no `Debug`. -/// [`Supports::prepare`](crate::op::Supports::prepare) makes one; only a query -/// chain can join it to a filter +/// [`Supports::prepare`](crate::op::Supports::prepare) makes one; only a field +/// marker's query can join it to a filter /// ([`Supports::query`](crate::op::Supports::query) takes a token no caller /// can make), so one query's tokens never verify rows for another value's /// filter. @@ -1143,7 +1143,7 @@ impl QueryTokens { /// ([`TermError::InvalidOptions`]), checked before the text is /// tokenised, or text that cuts no token ([`TermError::EmptyTermText`]), /// whose filter would match every row. The one rule for a match query: - /// the query chain and the `dynamic` path both apply it, so they cannot + /// a field marker's query and the `dynamic` path both apply it, so they cannot /// disagree on what they refuse. The stored side keeps token-less text /// as the empty filter instead. pub(crate) fn of_query(text: &str, options: &MatchOptions) -> Result<Self, TermError> { @@ -1247,8 +1247,8 @@ impl<P: Send + 'static> PrfVisitor<[u8; 32], P> for BloomVisitor { /// no PRF call. Stored, /// it is a value no query matches, since a query always has at least one /// token and the empty filter holds none of its positions. This is the -/// *stored* side's answer; a query of such text is refused by the query -/// chain before it gets here ([`TermError::EmptyTermText`]). +/// *stored* side's answer; a query of such text is refused before it gets +/// here ([`TermError::EmptyTermText`]). fn match_term<O>( prf: &HmacSha256Prf, text: &str, @@ -2158,8 +2158,8 @@ impl<K> KeysetCipher<'_, K> { /// or separator-only text, or text shorter than the n-gram length; see /// [`Tokenizer::Ngram`]) is the empty filter, which no query matches, /// so a short value can be stored. A query is the other type, - /// [`MatchQuery`], and comes from the query chain - /// (`cipher.query(op::Matches, ..)`), which refuses a query of such + /// [`MatchQuery`], and comes from a query + /// (a marker's `matches(..)`), which refuses a query of such /// text ([`TermError::EmptyTermText`]) because its filter would match /// every row; this method does not know which it is deriving and /// refuses nothing. @@ -2268,7 +2268,7 @@ mod tests { #[test] fn a_match_query_with_no_tokens_verifies_nothing() { - // A query chain refuses token-less text before a term exists, so + // A query refuses token-less text before a term exists, so // this cannot reach a `MatchQuery` through the API; the guard is for // one built any other way, which must never match every row. let query = MatchQuery::<DefaultMatch>::new( diff --git a/packages/stack-encrypt/src/sem/query.rs b/packages/stack-encrypt/src/sem/query.rs index 39036007dc..2e7b6e99d8 100644 --- a/packages/stack-encrypt/src/sem/query.rs +++ b/packages/stack-encrypt/src/sem/query.rs @@ -1,10 +1,10 @@ //! What a query is refused for before any term exists, written once. //! -//! Both paths call these: the typed query chain through each index's +//! Both paths call these: the typed query through each index's //! [`Supports::prepare`](crate::op::Supports::prepare), and the dynamic //! path at its boundary (`dynamic::check_query`) and before it derives a //! term (`dynamic::query_term`, `dynamic::verify`). So what a binding -//! refuses is what the typed chain refuses, with the same error, and +//! refuses is what a typed query refuses, with the same error, and //! neither has a copy of the rule. Every refusal here needs no key, so a //! refused query costs no request. //! diff --git a/packages/stack-encrypt/src/sem/tokenize.rs b/packages/stack-encrypt/src/sem/tokenize.rs index f3120a6994..ebf2f4c8da 100644 --- a/packages/stack-encrypt/src/sem/tokenize.rs +++ b/packages/stack-encrypt/src/sem/tokenize.rs @@ -29,7 +29,7 @@ pub enum Tokenizer { /// Sliding character n-grams of the given length over the whole text /// (whitespace included). Text shorter than `length` yields **no tokens**, /// exactly like the v1 match indexer — so a query shorter than the gram - /// length is refused by the query chain + /// length is refused when it is encrypted /// ([`TermError::EmptyTermText`](crate::sem::TermError::EmptyTermText)) /// rather than silently never matching, and a stored value shorter than /// it is the empty filter, which no query matches. This is the default, diff --git a/packages/stack-encrypt/src/target/context.rs b/packages/stack-encrypt/src/target/context.rs index 289dc56eee..744249e8c1 100644 --- a/packages/stack-encrypt/src/target/context.rs +++ b/packages/stack-encrypt/src/target/context.rs @@ -865,8 +865,8 @@ impl<'a, T: Clone + IntoContext<'a>> From<&NonEmpty<T>> for DeclaredContext { } } /// A scope the call adds. A query takes a scope and no binding, so this is -/// the one conversion from a caller's part: a `Bind` does not convert, and -/// a query chain's `.extend(Bind::new(..))` does not compile. +/// the one conversion from a caller's part: a `Bind` does not convert, so +/// a query's `.scope(Bind::new(..))` does not compile. impl<'c, T: IntoContext<'c> + MaybeEmpty> From<Scope<T>> for DeclaredContext { fn from(scope: Scope<T>) -> Self { Self(vec![scope.into()]) @@ -886,6 +886,7 @@ impl DeclaredContext { /// Whether any part carries a binding. A query refuses such a context /// before any key is requested: a binding is in no query context. + #[cfg(any(test, feature = "dynamic"))] pub(crate) fn has_bindings(&self) -> bool { self.0.iter().any(CallerContext::has_bindings) } diff --git a/packages/stack-encrypt/src/target/index.rs b/packages/stack-encrypt/src/target/index.rs index c68d71faf9..c28386cdc1 100644 --- a/packages/stack-encrypt/src/target/index.rs +++ b/packages/stack-encrypt/src/target/index.rs @@ -44,9 +44,11 @@ //! # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); //! ``` //! -//! On the query side a field's indexes answer one at a time, with no -//! ciphertext: [`Indexes::select`] picks the index by type and -//! [`Index::operation`] is its term alone. +//! One term can be derived alone, with no ciphertext: [`Indexes::select`] +//! picks an index out of a set by type, and [`Index::operation`] is its +//! term alone. A field marker's query does not use `select`: the field's +//! target type names the index that answers each operator +//! ([`Answers`](crate::op::Answers)). //! //! For the FFI and for saved plans an index lowers to data, //! [`IndexSpec`], through [`Index::spec`]. Parameters live on the index @@ -61,8 +63,8 @@ use super::operations::{ }; use super::source::{ConsumeSource, Owned, ShareSource}; use super::{ - DecryptField, DecryptInto, DecryptSource, Decryptable, Decryption, EncryptFrom, EncryptTarget, - Encryption, RequiredContext, + DecryptField, DecryptSource, Decryptable, Decryption, EncryptTarget, Encryption, + RequiredContext, }; use crate::json::{JsonOptions, JsonTerms}; use crate::op::Op; @@ -180,6 +182,55 @@ pub(crate) fn overlapping(indexes: &[IndexSpec]) -> Option<(&IndexSpec, &IndexSp }) } +/// Refuse a set of indexes that one value cannot declare together: one +/// index named twice, or two indexes that answer one operator. +/// +/// Two match indexes with different options are still one index twice: a +/// value has one term for each kind of index. `Ore` and `Ope` both answer +/// the orderings, so a query for one would have to choose between them. +/// The dynamic path refuses the same declarations when it parses a plan. +/// +/// A target calls this when it is described, so the refusal comes before +/// any key is requested: [`Encrypted<Terms>`] used as a target checks the +/// indexes its terms name, and a derived `plaintext = T` record checks the +/// indexes of its fields. +/// +/// The combinator [`indexed`] does not call it. A description built by +/// hand from `indexed((Ore, Ope))` and run with +/// [`KeysetCipher::run`](crate::KeysetCipher::run) derives both terms. +/// +/// # Errors +/// +/// [`PlanError::DuplicateIndex`](crate::PlanError::DuplicateIndex) or +/// [`PlanError::IndexOverlap`](crate::PlanError::IndexOverlap), in +/// [`Error::Plan`](crate::Error::Plan). +#[doc(hidden)] +pub fn check_indexes(indexes: &[IndexSpec]) -> Result<(), crate::Error> { + const AT: &str = "the value"; + for (i, index) in indexes.iter().enumerate() { + if indexes[..i] + .iter() + .any(|earlier| earlier.key() == index.key()) + { + return Err(crate::PlanError::DuplicateIndex { + at: AT.to_owned(), + index: index.key(), + } + .into()); + } + } + if let Some((first, second, operator)) = overlapping(indexes) { + return Err(crate::PlanError::IndexOverlap { + at: AT.to_owned(), + first: first.key(), + second: second.key(), + operator, + } + .into()); + } + Ok(()) +} + impl fmt::Display for IndexSpec { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.key()) @@ -635,7 +686,7 @@ impl<S, I: Index<S>> Indexes<S> for Vec<I> { /// /// There are deliberately no named accessors (`out.ore()`): they would need /// a type-level search of the tuple to serve a case that barely exists. A -/// struct with named fields is what `#[derive(EncryptFrom)]` is for. +/// struct with named fields is what `#[derive(EncryptTarget)]` is for. /// /// Decrypting it opens the ciphertext: the terms are one-way. #[derive(Debug)] @@ -646,37 +697,11 @@ pub struct Encrypted<Terms> { pub terms: Terms, } -impl<P: crate::Decrypt + Send + 'static, Terms> DecryptInto<P> for Encrypted<Terms> { - type Context = AeadContext; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<P, K> { - open(self.ciphertext, context) - } -} - -/// `Encrypted<Terms>` is a target, so a record field, a typed verb or a -/// caller names a ciphertext with its terms by type alone: +/// `Encrypted<Terms>` is a target, so a record field or a caller names a +/// ciphertext with its terms by type alone: /// `email: Encrypted<(EqualityTerm, MatchTerms)>`. The terms name their -/// indexes ([`TermSet`]), and the description is [`indexed`] over them, so -/// the bytes are the data spelling's and the hand composition's. -impl<S, Terms> EncryptFrom<S> for Encrypted<Terms> -where - S: crate::Encrypt + Clone, - Terms: TermSet<S>, -{ - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - indexed(Terms::INDEXES) - } - fn indexes() -> Vec<IndexSpec> { - Terms::INDEXES.specs() - } -} - -/// The owned declaration of the same layout: the ciphertext with its terms, -/// under the one caller context that the record field or the caller supplies. +/// indexes ([`TermSet`]), and the description is [`indexed`] over them, under +/// the one caller context that the record field or the caller supplies. /// /// The owned form is handed the value. The ciphertext takes a copy, and the /// terms take the value. When the context is refused, the ciphertext wipes its @@ -691,12 +716,22 @@ where where S: 's, { + if let Err(error) = check_indexes(&Terms::INDEXES.specs()) { + return Encryption::failed(error); + } indexed(Terms::INDEXES).accepting() } fn owned_encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput, Owned> where S: 's, { + if let Err(error) = check_indexes(&Terms::INDEXES.specs()) { + // The refused value is the description's to wipe. + return Encryption::from_build(Vec::new(), move |mut source: S, cipher, _| { + zeroize::Zeroize::zeroize(&mut source); + super::Pending::failed(cipher, error) + }); + } indexed_beside(wiping_ciphertext::<S, K, Owned>(), Terms::INDEXES).accepting() } fn indexes() -> Vec<IndexSpec> { @@ -716,13 +751,13 @@ impl<P: crate::Decrypt + zeroize::Zeroize + Send + 'static, Terms> DecryptSource impl<Terms> Decryptable for Encrypted<Terms> { const DECRYPTABLE: bool = true; } -impl<P: 'static, Ctx, Terms> DecryptField<P, Ctx> for Encrypted<Terms> +impl<P: zeroize::Zeroize + 'static, Ctx, Terms> DecryptField<P, Ctx> for Encrypted<Terms> where - Self: DecryptInto<P>, - Ctx: Into<<Self as DecryptInto<P>>::Context>, + Self: DecryptSource<P>, + Ctx: Into<<Self as DecryptSource<P>>::ContextInput>, { fn decryption_field<K: 'static>(self, context: Ctx) -> Option<Decryption<P, K>> { - Some(<Self as DecryptInto<P>>::decryption(self, context.into())) + Some(<Self as DecryptSource<P>>::decryption(self, context.into())) } } @@ -812,6 +847,11 @@ mod term_tuples { /// /// The type parameters are in the order [`ciphertext`]'s are, with the /// indexes last: `indexed::<u32, _, Borrowed, _>((Equality, Ore))`. +/// +/// This does not check the set of indexes. A set that names one index +/// twice, or two indexes that answer one operator, derives every term it +/// names. The target [`Encrypted<Terms>`] refuses such a set before any +/// key is requested. pub fn indexed<'s, S, K, M, X>( indexes: X, ) -> Encryption<'s, S, Encrypted<X::Terms>, K, CallerContext, M> diff --git a/packages/stack-encrypt/src/target/input.rs b/packages/stack-encrypt/src/target/input.rs index 991ad6e659..ea4925c0cb 100644 --- a/packages/stack-encrypt/src/target/input.rs +++ b/packages/stack-encrypt/src/target/input.rs @@ -96,15 +96,13 @@ impl<C> AcceptsContext for OptionalContext<C> { /// The record's name is the first scope, and the field's name is the next. /// So the caller has nothing to add, and `encrypt_into_with_context` does not /// compile for the record. A part that the caller supplies, such as a tenant -/// scope or a row binding, must be declared on the record first. The -/// declaration attributes for those parts are not built yet. +/// scope or a row binding, must be declared on the record, with `scope(..)` +/// or `bind(..)`. Such a record takes a `RequiredContext` of its parts. /// /// Only the library passes this input a value: /// /// - A record that is a field of another record is given that field's /// context, which then extends each of its own fields' contexts. -/// - A plan or a structural record passes its declared context the same -/// way. #[derive(Clone, Debug, Default)] pub struct DeclaredScopes(DeclaredContext); impl sealed::Sealed for DeclaredScopes {} @@ -121,9 +119,8 @@ impl From<DeclaredScopes> for DeclaredContext { scopes.0 } } -// A structural caller with a `DeclaredContext`, such as a plan or a `struct = -// ..` structural derive, passes its context to an owned record field through -// this conversion. +// A caller with a `DeclaredContext`, such as a data plan's lowering, passes +// its context to a record field through this conversion. impl From<DeclaredContext> for DeclaredScopes { fn from(context: DeclaredContext) -> Self { Self(context) @@ -136,6 +133,29 @@ impl From<CallerContext> for DeclaredScopes { Self(context.into()) } } +// A record of one value hands each field the caller's context as it is. A +// field that is a `struct = ..` record takes it as the context its own +// fields extend. +impl<C: Into<CallerContext>> From<RequiredContext<C>> for DeclaredScopes { + fn from(context: RequiredContext<C>) -> Self { + Self(DeclaredContext::from(context.into_inner().into())) + } +} +// A record that names its context type as a `NonEmpty<T>` +// (`context_type = NonEmpty<u64>`) hands each field that context. A +// ciphertext takes its AEAD half, and a term takes the whole. +impl<'c, T: IntoContext<'c>> From<RequiredContext<NonEmpty<T>>> + for RequiredContext<super::AeadContext> +{ + fn from(context: RequiredContext<NonEmpty<T>>) -> Self { + Self::supplied(super::AeadContext::from(context.into_inner())) + } +} +impl<'c, T: IntoContext<'c>> From<RequiredContext<NonEmpty<T>>> for RequiredContext<CallerContext> { + fn from(context: RequiredContext<NonEmpty<T>>) -> Self { + Self::supplied(CallerContext::from(context.into_inner())) + } +} // A field whose target requires a caller context is given the record's context // for that field, extended by the caller's context. So a // `RequiredContext<CallerContext>` field or a `RequiredContext<AeadContext>` @@ -153,9 +173,8 @@ impl From<RequiredContext<CallerContext>> for RequiredContext<super::AeadContext Self::supplied(context.into_inner().into()) } } -// The contexts of the structural declarations, wrapped as the required input -// of the owned leaf that replaces each one. A plan or a structural record -// gives an owned field its context this way. +// An `AeadContext` in hand, wrapped as the required input of a ciphertext +// leaf. A data plan's lowering gives a field its context this way. impl From<super::AeadContext> for RequiredContext<super::AeadContext> { fn from(context: super::AeadContext) -> Self { Self::supplied(context) diff --git a/packages/stack-encrypt/src/target/mod.rs b/packages/stack-encrypt/src/target/mod.rs index c54307b6dd..7bf297bc4c 100644 --- a/packages/stack-encrypt/src/target/mod.rs +++ b/packages/stack-encrypt/src/target/mod.rs @@ -67,26 +67,11 @@ //! requires the `NonEmpty<T>` it stores. On decrypt the caller may pass that //! `NonEmpty<T>` again, and the stored value must then equal it. //! -//! Both derives also implement the structural traits ([`EncryptFrom`], -//! [`DecryptInto`] and [`DecryptField`]). So an owned record can be a field -//! of a structural record, or a typed field of a plan. A plan field uses -//! [`FieldsBuilder::encrypt_into`](crate::plan::FieldsBuilder::encrypt_into), -//! or [`target`](crate::plan::FieldsBuilder::target) for a target that is -//! only owned, such as a `Ciphertext`. A hand-written owned target that will -//! be a record field must implement [`Decryptable`] and [`DecryptField`], as -//! well as [`DecryptSource`]. A hand-written structural target does the -//! same. -//! -//! # Structural API during migration -//! -//! The structural derives, plans, and dynamic targets still use the -//! declarations below. -//! -//! [`EncryptFrom<S>`] describes ciphertext and/or term operations and has an -//! associated context type. It does not receive plaintext or a cipher. The -//! cipher's `encrypt_as` executes that description, returning the existing -//! batched [`Pending`]. [`DecryptInto<P>`] inspects stored output and describes -//! opening it through `P::Decrypt`. +//! The `DecryptSource` derive also implements [`DecryptField`], which is how +//! a record finds the field that decryption opens. A hand-written target +//! that will be a record field must implement [`Decryptable`] and +//! [`DecryptField`], as well as [`DecryptSource`]. A term cannot be +//! decrypted: it is not `Decryptable`, and its `DecryptField` returns `None`. //! //! # Records and context //! @@ -102,8 +87,7 @@ //! constructed from a nonempty context, including a borrowed one — an //! `AeadContext` from one with the AEAD encoding alone. Records that supply //! their own field contexts use [`DeclaredContext`], whose default leaves those -//! contexts unchanged and whose nonempty form extends them. Custom targets may -//! instead declare `Context = ()`. +//! contexts unchanged and whose nonempty form extends them. //! //! Within one target, every operation runs under the one context the target is //! handed: the context is a type parameter of [`Encryption`], zipped @@ -123,17 +107,23 @@ //! the final target's own storage remain normal allocations. //! //! ``` -//! use stack_encrypt::{EncryptFrom, Encryption}; -//! use stack_encrypt::target::{self, CallerContext}; //! use stack_encrypt::sem::EqualityTerm; +//! use stack_encrypt::target::{self, CallerContext, EncryptTarget, RequiredContext}; +//! use stack_encrypt::{Encrypt, Encryption}; +//! use zeroize::Zeroize; //! +//! /// An equality term, stored as its bytes. //! struct StoredEquality([u8; 32]); -//! impl<S> EncryptFrom<S> for StoredEquality -//! where EqualityTerm: EncryptFrom<S, Context = CallerContext> { -//! type Context = CallerContext; -//! fn encryption<'s,K:'static>()->Encryption<'s,S,Self,K,Self::Context> -//! where S:'s { -//! <EqualityTerm as EncryptFrom<S>>::encryption().map(|term| Self(term.into_bytes())) +//! +//! impl<S: Encrypt + Clone + Zeroize> EncryptTarget<S> for StoredEquality { +//! type ContextInput = RequiredContext<CallerContext>; +//! fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput> +//! where +//! S: 's, +//! { +//! target::equality() +//! .accepting() +//! .map(|term: EqualityTerm| Self(term.into_bytes())) //! } //! } //! ``` @@ -141,7 +131,7 @@ //! Ciphertext operations require Vitamin C's `Encrypt`/`Decrypt` on the plaintext; //! there is no Serde fallback. Terms require only their PRF or ordering capability. //! New cryptographic operations belong in core; output adapters cannot install an -//! execution callback. The separate cipher-directed API remains public. +//! execution callback. //! //! Those schemes consume the plaintext. A description is handed it by //! reference by default, and an operation clones it before consuming it, so a @@ -153,22 +143,14 @@ //! # Collections and authentication //! //! `Vec<Target>` describes rows that are each encrypted on their own, under -//! the same context. That rule applies on the owned path, where a `Vec<S>` encrypts -//! into a `Vec` of [`EncryptTarget`]s, and on the structural path. Encrypting a plaintext `Vec<T>` into `StackCipherText` instead -//! follows Vitamin C's native sequence model, including its authenticated empty +//! the same context: a `Vec<S>` encrypts into a `Vec` of [`EncryptTarget`]s. +//! Encrypting a plaintext `Vec<T>` into `StackCipherText` instead follows +//! Vitamin C's native sequence model, including its authenticated empty //! marker. The same distinction applies to `Option<Target>` versus a native //! encrypted option. //! Readers preserve marker ciphertext and map keys; changing a key changes the //! authenticated context when opening. Passthrough metadata and query terms are //! not presented as AEAD-authenticated values. -//! -//! # Migration from the unpublished execution traits -//! -//! Implement `encryption`/`decryption`, returning core-owned descriptions, in place -//! of `encrypt_from`/`decrypt_into` methods that took a cipher. The derives do this -//! automatically. Call `keyset.encrypt_as(&value, context)` and -//! `cipher.decrypt_as(record, context)`. New scalar targets implement -//! [`EncryptTarget`] and [`DecryptSource`]. mod collections; mod context; mod conversion; @@ -180,7 +162,6 @@ mod pending; mod request; mod source; pub mod transcode; -mod tuples; pub(crate) use self::core::{bind_pending, seal_pending}; pub use context::{ @@ -197,6 +178,8 @@ pub use context::{ #[doc(hidden)] pub use context::{ExpectedContext, StoredContext}; pub use conversion::{DecryptFrom, DecryptSource, EncryptInto, EncryptTarget}; +#[doc(hidden)] +pub use index::check_indexes; pub use index::{ indexed, At, Encrypted, Equality, Index, IndexSpec, Indexes, Json, Match, Ope, Ore, OreBlock, Select, TermSet, Whole, @@ -206,19 +189,15 @@ pub use input::{ OptionalContext, RequiredContext, ScalarContext, }; #[cfg(feature = "dynamic")] -pub(crate) use operations::chosen; -pub(crate) use operations::inspect; +pub(crate) use operations::{chosen, inspect}; pub use operations::{ ciphertext, equality, json_document, json_terms, matching, ope, open, ore, ore_block, - passthrough, scalar_ciphertext, DecryptField, DecryptInto, Decryptable, Decryption, - EncryptFrom, Encryption, Layout, + passthrough, scalar_ciphertext, DecryptField, Decryptable, Decryption, Encryption, }; pub use pending::{CipherRef, Pending, PendingFuture}; pub use request::{Request, Responses}; pub use source::{Borrowed, ConsumeSource, Owned, ShareSource, SourceMode}; -pub use stack_encrypt_derive::{DecryptInto, EncryptFrom}; pub use stack_encrypt_derive::{DecryptSource, EncryptTarget}; -pub use tuples::JoinContext; /// The cleanup trait that every owned plaintext must implement. It is /// re-exported here so that the derives can name it, even when the caller's /// crate does not depend on `zeroize`. diff --git a/packages/stack-encrypt/src/target/operations.rs b/packages/stack-encrypt/src/target/operations.rs index 9188be471a..d78a5d2e36 100644 --- a/packages/stack-encrypt/src/target/operations.rs +++ b/packages/stack-encrypt/src/target/operations.rs @@ -19,7 +19,6 @@ use super::context::{ }; use super::conversion::{DecryptSource, EncryptTarget}; use super::core::{encrypt_native, open_native, Term}; -use super::index::{Index, IndexSpec, Json}; use super::input::{AcceptsContext, NoCallerContext, RequiredContext}; use super::source::{Borrowed, ConsumeSource, Owned, ShareSource, SourceMode}; use super::{CipherRef, Pending}; @@ -29,56 +28,6 @@ use crate::{Error, IntoContext, KeysetCipher, MaybeEmpty, NonEmpty, StackCipher, use stack_kms::MaybeSend; use std::fmt; -/// Declaration that an encrypted target is produced from `S`. -/// -/// The derive supplies it; a hand-written implementation composes the -/// constructors in this module ([`ciphertext`], [`equality`], [`matching`], -/// [`ore`], [`ope`], [`ore_block`]) and converts their output with [`Encryption::map`] or -/// [`Encryption::transcode`]. Nothing here receives the plaintext or a -/// cipher: the returned [`Encryption`]'s execution is private, so a target -/// can choose operations and build its output but cannot replace encryption. -pub trait EncryptFrom<S>: Sized + 'static { - /// The context this target still needs when it is run — what a caller - /// supplies alongside the plaintext: a [`CallerContext`] for a target - /// that derives terms, an [`AeadContext`] for one that only seals, a - /// `NonEmpty<T>` for a record that stores its identifier, or a - /// [`DeclaredContext`] — which `()` satisfies — for a record whose fields - /// name their own. - type Context; - /// The description the cipher executes for one value of `S`. The context - /// is supplied when the description is run, not here, so every operation - /// beneath it receives the same one. - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's; - /// The indexes this target's terms answer queries by, as data, under - /// the context the target is run under: what a plan field typed with - /// this target ([`encrypt_into`](crate::plan::FieldsBuilder::encrypt_into)) - /// lets a query ask for. A query for any other index is refused rather - /// than derived, since it would match nothing. - /// - /// The default is none, which is right for a ciphertext and for a record - /// whose fields sit under contexts of their own; a term, an - /// [`Encrypted<Terms>`](super::Encrypted) and a tuple of targets name - /// theirs. - fn indexes() -> Vec<super::IndexSpec> { - vec![] - } -} -/// Declaration of how a stored target recovers `P`. -/// -/// Inspection sees no cipher: the implementation selects the recoverable -/// ciphertext and its context, and [`open`] describes the rest. A query-only -/// target (terms alone) has no implementation. -pub trait DecryptInto<P>: Sized { - /// The context that a caller passes to decrypt the target. For a - /// record that stores its context, the caller passes `()`, or the - /// `NonEmpty<T>` that the caller expects the record to store. - type Context; - /// The description the cipher executes to recover `P`. - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<P, K>; -} - // What the description is handed is `M::Source`: `&'s S` in the default // `Borrowed` mode, `S` itself in `Owned` mode (see `SourceMode`). #[cfg(not(target_arch = "wasm32"))] @@ -109,8 +58,9 @@ type Open<T, K> = Box<dyn for<'a> FnOnce(&'a StackCipher<K>) -> Pending<'a, T, K /// `Ctx` it is handed when it runs. /// /// Built from the constructors in this module and the combinators below; -/// executed only by [`KeysetCipher::encrypt_as`]. Nothing runs, and no key is -/// requested, until then. +/// executed only by a keyset cipher: [`EncryptInto`](super::EncryptInto) runs +/// a target's declaration, and [`KeysetCipher::run`] runs a description held +/// in a variable. Nothing runs, and no key is requested, until then. /// /// `Ctx` is the context this description still needs. It is a type parameter, /// not a stored value, and that is what makes two rules hold at compile time @@ -135,9 +85,9 @@ type Open<T, K> = Box<dyn for<'a> FnOnce(&'a StackCipher<K>) -> Pending<'a, T, K /// the plaintext. /// /// `M` is how the description is handed its plaintext ([`SourceMode`]): -/// by reference in the default [`Borrowed`] mode, which every -/// [`EncryptFrom`] declaration runs in, or by value in -/// [`Owned`] mode, run with [`KeysetCipher::run`]. In owned mode +/// by reference in the default [`Borrowed`] mode, which a declaration that +/// takes a field out of a borrowed struct runs in, or by value in +/// [`Owned`] mode, which [`EncryptInto`](super::EncryptInto) runs. In owned mode /// a single operation consumes the plaintext without copying it, so `S` need /// not be `Clone`; [`zip`](Self::zip) is the one place that asks for it. #[must_use = "an encryption description does nothing until a keyset cipher executes it"] @@ -150,8 +100,10 @@ pub struct Encryption<'s, S: 's, T, K, Ctx, M: SourceMode<'s, S> = Borrowed> { } /// A composable description of how `T` is recovered from a stored target. /// -/// Built from [`open`] and the combinators below; executed only by -/// `decrypt_as`. Nothing runs, and no key is retrieved, until then. +/// Built from [`open`] and the combinators below; executed only by a +/// cipher: [`DecryptFrom`](super::DecryptFrom) runs a stored target's +/// declaration, and `run_decryption` runs a description held in a variable. +/// Nothing runs, and no key is retrieved, until then. #[must_use = "a decryption description does nothing until a cipher executes it"] pub struct Decryption<T, K> { inner: Opening<T, K>, @@ -684,12 +636,13 @@ impl<'s, S: 's, T: 'static, K: 'static, Ctx: 's> Encryption<'s, S, T, K, Ctx> { /// reads the label from the borrowed source, or fails, in which case the /// description yields that error without I/O. /// - /// Crate-internal: the plan builder and the data plan's lowering - /// (`dynamic::planned::compose`) are its callers. + /// Crate-internal: the data plan's lowering + /// (`dynamic::planned::compose`) is its caller. /// /// `field` names the field of the source the label is read from, and /// `identity` the segment `own` joins to it, for /// [`contexts`](Self::contexts). + #[cfg(feature = "dynamic")] pub(crate) fn under_by<G>( self, field: &str, @@ -718,37 +671,15 @@ impl<'s, S: 's, T: 'static, K: 'static, Ctx: 's> Encryption<'s, S, T, K, Ctx> { shapes, } } - /// [`under`](Encryption::under) for a context the call names, which a - /// plan built without one takes. The description fails with `error` - /// if it runs, and shows the call's context as a placeholder. - /// - /// Crate-internal: the plan builder is its caller, for - /// [`contexts`](Self::contexts) on such a plan. - pub(crate) fn under_call( - self, - identity: Option<&str>, - error: Error, - ) -> Encryption<'s, S, T, K, DeclaredContext> - where - Ctx: From<CallerContext>, - { - let shapes = contexts::substitute( - self.shapes, - &Shape::under_label(Placeholder::required("context", "Label"), identity), - ); - Encryption { - build: Box::new(move |_, cipher, _: DeclaredContext| Pending::failed(cipher, error)), - shapes, - } - } /// [`project`](Self::project) for a field picked by name at run time, as - /// a plan's `fields()` does: `select` may capture the name, and may fail + /// a data plan does: `select` may capture the name, and may fail /// (the value has no such field, or not of this type), in which case the /// description yields that error without I/O. /// - /// Crate-internal: the plan builder and the data plan's lowering are - /// its callers, and what `select` captures is a field name. The public combinator stays the + /// Crate-internal: the data plan's lowering is its caller, and what + /// `select` captures is a field name. The public combinator stays the /// capture-free `project`. + #[cfg(feature = "dynamic")] pub(crate) fn project_by<P: 's, G>(self, select: G) -> Encryption<'s, P, T, K, Ctx> where G: for<'b> FnOnce(&'b P) -> Result<&'b S, Error> + MaybeSend + 's, @@ -900,8 +831,10 @@ where /// A description that only checks the borrowed source, yielding `()` or the /// error without I/O. Zipped beside a record's fields, a failed check fails -/// the whole record before any key is requested. Crate-internal: the plan -/// builder uses it to refuse a value whose fields the plan does not match. +/// the whole record before any key is requested. Crate-internal: the data +/// plan's lowering uses it to refuse a value whose fields the plan does not +/// match. +#[cfg(feature = "dynamic")] pub(crate) fn inspect<'s, S: 's, K: 'static, Ctx: 's, C>(check: C) -> Encryption<'s, S, (), K, Ctx> where C: FnOnce(&S) -> Result<(), Error> + MaybeSend + 's, @@ -1174,7 +1107,7 @@ pub fn json_terms<'s, K: 'static, M: SourceMode<'s, serde_json::Value>>( /// The JSON index's one sealing operation: flatten the document, derive /// its terms, request one data key under the context's descriptor and seal /// every node under it, into a [`JsonDocument`]. The stored form of a JSON -/// field; what [`JsonDocument`]'s own [`EncryptFrom<Value>`] runs. The +/// field; what [`JsonDocument`]'s own [`EncryptTarget<JsonPlaintext>`] runs. The /// document is consumed, so in the default [`Borrowed`] mode it is cloned /// once, as [`ciphertext`] clones its plaintext. pub fn json_document<'s, K: 'static, M: ConsumeSource<'s, serde_json::Value>>( @@ -1202,121 +1135,6 @@ pub fn json_document<'s, K: 'static, M: ConsumeSource<'s, serde_json::Value>>( } } -/// A target's layout, chosen by value rather than by type. -/// -/// Most targets have one layout, fixed by their type: the type's -/// [`EncryptFrom`] says what is sealed and which terms sit beside it, and -/// a plan uses it with -/// [`encrypt_into::<T>()`](crate::plan::ValueStart::encrypt_into). A -/// target that takes options needs more than a type, and a layout is the -/// value that carries them. -/// -/// The JSON index is the layout this crate provides. `Json::new(options)` -/// seals the same [`JsonDocument`] as `encrypt_into::<JsonDocument>()`, -/// but under `options` instead of the defaults: for example, with the -/// leaves at `$.age` ordered, so that a query can compare them. -/// -/// A plan takes a layout with -/// [`encrypt_with_layout`](crate::plan::ValueStart::encrypt_with_layout). -/// A query through that plan uses the layout's index ([`Layout::index`]), -/// so it derives under the same options without naming them again. -/// -/// ``` -/// # async fn example() -> Result<(), stack_encrypt::Error> { -/// use serde_json::json; -/// use stack_encrypt::json::{JsonDocument, JsonOptions}; -/// use stack_encrypt::kms::FakeDataKeySource; -/// use stack_encrypt::sem::TermError; -/// use stack_encrypt::target::Json; -/// use stack_encrypt::{op, Error, Plan, StackCipher}; -/// -/// let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; -/// let profile = json!({"name": "Bob", "age": 34}); -/// -/// // By value: the JSON index with `$.age` ordered. -/// let options = JsonOptions::default().ordering(["$.age"])?; -/// let ordered = Plan::value::<serde_json::Value>() -/// .context("users/profile") -/// .encrypt_with_layout(Json::new(options)) -/// .build()?; -/// let doc: JsonDocument = cipher.encrypt(&profile).using(&ordered).await?; -/// // The leaf at `$.age` carries an ordering term... -/// assert_eq!(doc.entries().iter().filter(|e| e.ordering_term().is_some()).count(), 1); -/// // ...and a query through the plan derives under the same options. -/// let _adults = cipher.query(op::AtPath(op::Gte), &("$.age", 18)).on(&ordered).await?; -/// -/// // By type: the same target under the default options, which order no -/// // path, so the same query is refused. -/// let unordered = Plan::value::<serde_json::Value>() -/// .context("users/profile") -/// .encrypt_into::<JsonDocument>() -/// .build()?; -/// let refused = cipher.query(op::AtPath(op::Gte), &("$.age", 18)).on(&unordered).await; -/// assert!(matches!(refused, Err(Error::Term(TermError::JsonPathNotOrdered)))); -/// # Ok(()) -/// # } -/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); -/// ``` -/// -/// A layout belongs to a plan. The cipher's -/// [`encrypt_as`](crate::KeysetCipher::encrypt_as) is a different layer: -/// it seals one value as a target type's own `EncryptFrom` does, with no -/// plan and no options. -pub trait Layout<S>: Clone + fmt::Debug + 'static { - /// What a run produces: the target the layout seals a value into. - type Target: EncryptFrom<S>; - /// The index a query through the plan runs through: the layout - /// itself, for an index with options. - type Index: Index<S> + Clone; - /// The description the cipher executes for one value of `S`, under - /// this layout's options. - fn encryption<'s, K: 'static>( - &self, - ) -> Encryption<'s, S, Self::Target, K, <Self::Target as EncryptFrom<S>>::Context> - where - S: 's; - /// The index, with its options. - fn index(&self) -> &Self::Index; - /// The indexes the plan declares, as data: the index's. - fn specs(&self) -> Vec<IndexSpec> { - vec![self.index().spec()] - } -} - -/// The JSON index seals a [`JsonDocument`] under its options, and is the -/// index a query through the plan runs through. -impl Layout<serde_json::Value> for Json { - type Target = JsonDocument; - type Index = Json; - fn encryption<'s, K: 'static>( - &self, - ) -> Encryption<'s, serde_json::Value, JsonDocument, K, CallerContext> - where - serde_json::Value: 's, - { - json_document(self.options.clone()) - } - fn index(&self) -> &Json { - self - } -} - -/// A JSON document seals as the JSON index lays it out under the default -/// options, and declares that index: a plan typed with it answers -/// containment and the accessors through [`Json`](struct@super::Json). -impl EncryptFrom<serde_json::Value> for JsonDocument { - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, serde_json::Value, Self, K, Self::Context> - where - serde_json::Value: 's, - { - json_document(JsonOptions::default()) - } - fn indexes() -> Vec<super::IndexSpec> { - vec![super::IndexSpec::Json(JsonOptions::default())] - } -} - /// A [`JsonPlaintext`] seals into a document as the JSON index lays it out /// under the default options, and declares that index. /// @@ -1522,20 +1340,10 @@ pub fn open<P: crate::Decrypt + Send + 'static, K: 'static>( } impl<K: 'static> KeysetCipher<'_, K> { - /// Encrypt `source` into `T` under this keyset, as `T`'s declaration - /// describes. The returned [`Pending`] settles every key request the - /// declaration made in one batch. - pub fn encrypt_as<'a, S, T>(&'a self, source: &S, context: T::Context) -> Pending<'a, T, K> - where - T: EncryptFrom<S>, - { - (T::encryption().build)(source, self, context) - } /// Run a description held in a variable over `source`, under `context`. /// /// `source` is what the description's mode hands its operations: `&S` - /// for a [`Borrowed`]-mode description (the default, and what - /// [`encrypt_as`](Self::encrypt_as) runs for a type), or `S` itself for + /// for a [`Borrowed`]-mode description (the default), or `S` itself for /// an [`Owned`]-mode one. Owned mode is how a plaintext /// that is not `Clone` reaches an operation: a single operation consumes /// it without a copy. @@ -1588,7 +1396,7 @@ impl<K: 'static> KeysetCipher<'_, K> { } /// Run a [`Decryption`] held in a variable under this keyset: the /// counterpart of [`run`](Self::run), and what - /// [`decrypt_as`](Self::decrypt_as) does with a type's declaration. A + /// [`DecryptFrom`](super::DecryptFrom) does with a type's declaration. A /// leaf sealed under another keyset is refused /// ([`Error::ForeignKeyset`]) before any key is retrieved. /// @@ -1596,12 +1404,15 @@ impl<K: 'static> KeysetCipher<'_, K> { /// # async fn example() -> Result<(), stack_encrypt::Error> { /// use stack_encrypt::kms::FakeDataKeySource; /// use stack_encrypt::target::{self, AeadContext, Decryption}; - /// use stack_encrypt::{nonempty, StackCipher, StackCipherText}; + /// use stack_encrypt::{nonempty, EncryptInto, StackCipher, StackCipherText}; /// /// let cipher = StackCipher::builder().kms(FakeDataKeySource::new()).init().await?; /// let keyset = cipher.default_keyset(); /// let context = || AeadContext::from(nonempty!("users/email")); - /// let sealed: StackCipherText = keyset.encrypt_as(&"bob@example.com".to_string(), context()).await?; + /// let sealed: StackCipherText = "bob@example.com" + /// .to_string() + /// .encrypt_into_with_context(&keyset, context()) + /// .await?; /// /// let opening: Decryption<String, _> = target::open(sealed, context()); /// assert_eq!(keyset.run_decryption(opening).await?, "bob@example.com"); @@ -1615,19 +1426,6 @@ impl<K: 'static> KeysetCipher<'_, K> { ) -> Pending<'a, P, K> { decryption.open_in(self) } - /// Recover `P` from `source`, as its declaration describes. A leaf sealed - /// under another keyset is refused ([`Error::ForeignKeyset`]) before any - /// key is retrieved. - pub fn decrypt_as<'a, P: 'static, T>( - &'a self, - source: T, - context: T::Context, - ) -> Pending<'a, P, K> - where - T: DecryptInto<P>, - { - source.decryption(context).open_in(self) - } } impl<K: 'static> StackCipher<K> { /// Run a [`Decryption`] held in a variable through the client: leaves @@ -1639,111 +1437,6 @@ impl<K: 'static> StackCipher<K> { ) -> Pending<'a, P, K> { decryption.open_in(self) } - /// Recover `P` from `source`, as its declaration describes. Leaves from - /// any of the client's keysets open here. - pub fn decrypt_as<'a, P: 'static, T>( - &'a self, - source: T, - context: T::Context, - ) -> Pending<'a, P, K> - where - T: DecryptInto<P>, - { - source.decryption(context).open_in(self) - } -} - -// The leaf declarations below keep `Clone` on purpose. An `EncryptFrom` -// declaration runs in the `Borrowed` mode (`encrypt_as` hands it `&S`), and an -// operation that consumes a borrowed plaintext must clone it: the bound is -// `Borrowed: ConsumeSource<'s, S>`, spelled out. The constructors themselves -// (`ciphertext`, `equality`, `ore`, `ope`) ask only for the scheme's own -// capability, and run a non-`Clone` plaintext in `Owned` mode. -impl<S: crate::Encrypt + Clone> EncryptFrom<S> for StackCipherText { - type Context = AeadContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - ciphertext() - } -} -impl<P: crate::Decrypt + Send + 'static> DecryptInto<P> for StackCipherText { - type Context = AeadContext; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<P, K> { - open(self, context) - } -} -impl<S: crate::Encrypt + Clone> EncryptFrom<S> for crate::sem::EqualityTerm { - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - equality() - } - fn indexes() -> Vec<super::IndexSpec> { - vec![super::IndexSpec::Equality] - } -} -impl<S: AsRef<str>, O: crate::sem::MatchConfig + 'static> EncryptFrom<S> - for crate::sem::MatchTerms<O> -{ - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - matching() - } - fn indexes() -> Vec<super::IndexSpec> { - vec![super::IndexSpec::Match(O::options())] - } -} -impl<S> EncryptFrom<S> for crate::sem::OreTerm<S> -where - S: crate::sem::Orderable + Clone, -{ - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - ore() - } - fn indexes() -> Vec<super::IndexSpec> { - vec![super::IndexSpec::Ore] - } -} -impl<S> EncryptFrom<S> for crate::sem::OpeTerm<S> -where - S: crate::sem::Orderable + Clone, -{ - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - ope() - } - fn indexes() -> Vec<super::IndexSpec> { - vec![super::IndexSpec::Ope] - } -} -impl<S> EncryptFrom<S> for crate::sem::OreBlockTerm<S> -where - S: crate::sem::Orderable + Clone, -{ - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - ore_block() - } - fn indexes() -> Vec<super::IndexSpec> { - vec![super::IndexSpec::OreBlock] - } } // The owned declarations of the same leaves. Each one requires the context its @@ -1791,7 +1484,7 @@ impl<S: crate::Encrypt + Clone + zeroize::Zeroize> EncryptTarget<S> for crate::s equality().accepting() } fn indexes() -> Vec<super::IndexSpec> { - <Self as EncryptFrom<S>>::indexes() + vec![super::IndexSpec::Equality] } } impl<S: AsRef<str> + zeroize::Zeroize, O: crate::sem::MatchConfig + 'static> EncryptTarget<S> @@ -1805,11 +1498,11 @@ impl<S: AsRef<str> + zeroize::Zeroize, O: crate::sem::MatchConfig + 'static> Enc matching().accepting() } fn indexes() -> Vec<super::IndexSpec> { - <Self as EncryptFrom<S>>::indexes() + vec![super::IndexSpec::Match(O::options())] } } macro_rules! owned_order_term { - ($($ty:ident, $operation:ident;)+) => {$( + ($($ty:ident, $operation:ident, $spec:ident;)+) => {$( impl<S: crate::sem::Orderable + Clone + zeroize::Zeroize> EncryptTarget<S> for crate::sem::$ty<S> { type ContextInput = RequiredContext<CallerContext>; fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput> @@ -1819,83 +1512,15 @@ macro_rules! owned_order_term { $operation().accepting() } fn indexes() -> Vec<super::IndexSpec> { - <Self as EncryptFrom<S>>::indexes() + vec![super::IndexSpec::$spec] } } )+}; } owned_order_term! { - OreTerm, ore; - OpeTerm, ope; - OreBlockTerm, ore_block; -} - -// A `Vec<Target>` is a row per item, each encrypted independently under the -// same context and settled in one batch; an `Option<Target>` is one row or -// nothing, without I/O. (A plaintext `Vec`/`Option` sealed into a -// `StackCipherText` is a different thing: Vitamin C's native sequence and -// option, with their authenticated markers.) -impl<S, T: EncryptFrom<S>> EncryptFrom<Vec<S>> for Vec<T> -where - T::Context: Clone + 'static + MaybeSend, -{ - type Context = T::Context; - fn encryption<'s, K: 'static>() -> Encryption<'s, Vec<S>, Self, K, Self::Context> - where - S: 's, - { - Encryption { - shapes: T::encryption::<K>().shapes(), - build: Box::new(move |source, cipher, cx: T::Context| { - Pending::collect( - cipher, - source - .iter() - .map(|item| cipher.encrypt_as(item, cx.clone())), - ) - }), - } - } -} -impl<S, T: EncryptFrom<S> + MaybeSend> EncryptFrom<Option<S>> for Option<T> -where - T::Context: 'static + MaybeSend, -{ - type Context = T::Context; - fn encryption<'s, K: 'static>() -> Encryption<'s, Option<S>, Self, K, Self::Context> - where - S: 's, - { - Encryption { - shapes: T::encryption::<K>().shapes(), - build: Box::new(move |source, cipher, cx: T::Context| match source { - Some(item) => cipher.encrypt_as(item, cx).map(Some), - None => Pending::ready(cipher, Ok(None)), - }), - } - } -} -// Opened through `all_wiping`, as the owned `Vec<T>` is. If an element fails -// after its key arrives, the column fails, and the elements recovered before -// it are wiped. That is why `P` must implement `Zeroize`. Every plaintext the -// engine decrypts into already implements it. -impl<P: zeroize::Zeroize + 'static, T: DecryptInto<P> + 'static> DecryptInto<Vec<P>> for Vec<T> -where - T::Context: Clone + 'static, -{ - type Context = T::Context; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<Vec<P>, K> { - Decryption::all_wiping( - self.into_iter() - .map(|item| item.decryption(context.clone())), - ) - } -} -impl<P: 'static + MaybeSend, T: DecryptInto<P> + 'static> DecryptInto<Option<P>> for Option<T> { - type Context = T::Context; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<Option<P>, K> { - Decryption::optional(self.map(|item| item.decryption(context))) - } + OreTerm, ore, Ore; + OpeTerm, ope, Ope; + OreBlockTerm, ore_block, OreBlock; } /// How a derive finds the one recoverable field of a record among its terms. @@ -1908,13 +1533,13 @@ pub trait DecryptField<P, Ctx>: Sized { /// The opening description, if this field holds recoverable ciphertext. fn decryption_field<K: 'static>(self, context: Ctx) -> Option<Decryption<P, K>>; } -impl<P: 'static, Ctx> DecryptField<P, Ctx> for StackCipherText +impl<P: zeroize::Zeroize + 'static, Ctx> DecryptField<P, Ctx> for StackCipherText where - Self: DecryptInto<P>, - Ctx: Into<<Self as DecryptInto<P>>::Context>, + Self: DecryptSource<P>, + Ctx: Into<<Self as DecryptSource<P>>::ContextInput>, { fn decryption_field<K: 'static>(self, context: Ctx) -> Option<Decryption<P, K>> { - Some(<Self as DecryptInto<P>>::decryption(self, context.into())) + Some(<Self as DecryptSource<P>>::decryption(self, context.into())) } } impl< @@ -1966,7 +1591,7 @@ impl<T: Decryptable> Decryptable for Option<T> { #[cfg(test)] mod tests { use super::*; - use crate::{nonempty, sem::EqualityTerm}; + use crate::{nonempty, sem::EqualityTerm, EncryptInto}; use stack_kms::FakeDataKeySource; #[tokio::test] @@ -1981,7 +1606,13 @@ mod tests { for expected in [Some("secret nickname".to_string()), None] { let sealed: Option<StackCipherText> = match &expected { - Some(value) => Some(keyset.encrypt_as(value, context()).await.unwrap()), + Some(value) => Some( + value + .clone() + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(), + ), None => None, }; let opening: Decryption<Option<String>, FakeDataKeySource> = sealed diff --git a/packages/stack-encrypt/src/target/pending.rs b/packages/stack-encrypt/src/target/pending.rs index f267c243f1..5d8547c974 100644 --- a/packages/stack-encrypt/src/target/pending.rs +++ b/packages/stack-encrypt/src/target/pending.rs @@ -1,5 +1,5 @@ //! [`Pending`]: the request carrier [`StackCipher`] hands back from -//! [`EncryptFrom`](super::EncryptFrom) / [`DecryptInto`](super::DecryptInto), +//! [`EncryptTarget`](super::EncryptTarget) / [`DecryptSource`](super::DecryptSource), //! and the single place the target layer talks to ZeroKMS. //! //! A `Pending` is built synchronously and settled once. Combining pendings @@ -227,7 +227,7 @@ impl<'a, T: 'a, K> Pending<'a, T, K> { /// type `T` is ever produced), and any assembly it is merged into fails /// without I/O — see the `failed` field. /// - /// Public because a hand-written `EncryptFrom` / `DecryptInto` that + /// Public because a hand-written `EncryptTarget` / `DecryptSource` that /// works with a cipher scope directly reaches for it when a contract is /// broken — e.g. [`Error::NotOpened`] for a field whose type declared /// [`DECRYPTABLE`](super::Decryptable::DECRYPTABLE) but was passed over. @@ -383,14 +383,6 @@ impl<'a, T: 'a, K> Pending<'a, T, K> { } } - /// Whether this pending opens leaves from any keyset: it was built - /// through the [`StackCipher`] (no scope) and has not failed. Such a - /// pending cannot be merged with a scoped one without being confined to - /// that scope, so a batch settles it on its own. - pub(crate) fn opens_any_keyset(&self) -> bool { - self.keyset.is_none() && self.failed.is_none() - } - /// Merge two pendings into one resolving to the pair. Their requests /// concatenate — awaiting the result is still one batched call per /// request kind. diff --git a/packages/stack-encrypt/src/target/source.rs b/packages/stack-encrypt/src/target/source.rs index a3a3421664..94514bdfc7 100644 --- a/packages/stack-encrypt/src/target/source.rs +++ b/packages/stack-encrypt/src/target/source.rs @@ -3,8 +3,8 @@ //! module is private and its items are re-exported from `target`. /// A description's plaintext is handed to it by reference: the default mode, -/// and the one every [`EncryptFrom`](super::EncryptFrom) declaration runs in. -/// An operation that consumes the plaintext clones it. +/// and the one a record runs each field in when it takes the field out of a +/// borrowed struct. An operation that consumes the plaintext clones it. /// /// This is a mode, not a value: the enum is uninhabited, so no `Borrowed` /// ever exists and it never holds a plaintext. Its `Debug` can print nothing. @@ -34,9 +34,9 @@ mod sealed { /// description can be handed it two ways, and because the mode is a type /// parameter the difference is settled at compile time: /// -/// - [`Borrowed`], the default: the description is handed `&S`. This is what -/// [`encrypt_as`](crate::KeysetCipher::encrypt_as) does for every -/// [`EncryptFrom`](super::EncryptFrom) declaration, and what lets a record +/// - [`Borrowed`], the default: the description is handed `&S`. This is +/// what [`EncryptTarget::encryption`](super::EncryptTarget::encryption) +/// describes, and what lets a record /// pick its fields out of a borrowed struct. An operation that consumes the /// plaintext clones it first ([`ConsumeSource`]), so here, and only here, /// `S` must be `Clone`. diff --git a/packages/stack-encrypt/src/target/tuples.rs b/packages/stack-encrypt/src/target/tuples.rs deleted file mode 100644 index 0edb123ada..0000000000 --- a/packages/stack-encrypt/src/target/tuples.rs +++ /dev/null @@ -1,160 +0,0 @@ -//! A tuple of targets is a target: each element derived from the same -//! plaintext, under the one context the tuple is handed, settled in one -//! batch. This is a `plaintext = T` record without the struct, and what a -//! one-value plan's [`encrypt_into`](crate::plan::ValueStart::encrypt_into) -//! names: `encrypt_into::<(StackCipherText, EqualityTerm)>()`. -//! -//! The context is the one every element accepts ([`JoinContext`]): an -//! [`AeadContext`] for a tuple of ciphertexts, a [`CallerContext`] as soon as -//! one element derives a term. A ciphertext element takes its AEAD half, a -//! term the whole ([`Encryption::accepting`]), so the bytes are the derive's. -use super::context::{AeadContext, CallerContext, DeclaredContext}; -use super::operations::{ - DecryptField, DecryptInto, Decryptable, Decryption, EncryptFrom, Encryption, -}; -use super::IndexSpec; -use crate::Error; -use stack_kms::MaybeSend; - -/// The one context two targets can be handed together: what a tuple of -/// targets takes, so each element receives the same value. -/// -/// Two targets that need the same context take that context; a ciphertext -/// ([`AeadContext`]) beside a term or a record ([`CallerContext`], -/// [`DeclaredContext`]) takes a [`CallerContext`], of which the ciphertext's -/// context is the AEAD half. A tuple of ciphertexts therefore takes an -/// [`AeadContext`], and accepts every context a lone ciphertext accepts: -/// what a `plaintext = T` record declared `context_type = AeadContext` is. -/// -/// `Output` is a context every element's own context is made from (`Into` -/// each of them): the tuple's impl asks that, so a join can only ever widen -/// what an element is handed, never drop part of it. -/// -/// Sealed: the pairs are those above, and a context paired with itself. -/// Targets whose contexts differ otherwise do not form a tuple (nor a -/// `plaintext = T` record, whose outputs are one), so a target of your own -/// that sits beside a ciphertext or a term declares one of this crate's -/// contexts, usually [`CallerContext`], and takes what it needs from it. -/// Sealing keeps the joins this crate's to extend: a pair implemented -/// outside it would conflict with any join added here later, which would -/// make adding one a breaking change. -pub trait JoinContext<Other>: sealed::Sealed<Other> { - /// The context both are handed. - type Output: Clone + MaybeSend + 'static; -} -mod sealed { - pub trait Sealed<Other> {} -} -impl<C: Clone + MaybeSend + 'static> sealed::Sealed<C> for C {} -impl<C: Clone + MaybeSend + 'static> JoinContext<C> for C { - type Output = C; -} -macro_rules! join_to_caller { - ($($left:ty, $right:ty;)+) => {$( - impl sealed::Sealed<$right> for $left {} - impl JoinContext<$right> for $left { - type Output = CallerContext; - } - )+}; -} -join_to_caller! { - AeadContext, CallerContext; - CallerContext, AeadContext; - AeadContext, DeclaredContext; - DeclaredContext, AeadContext; - CallerContext, DeclaredContext; - DeclaredContext, CallerContext; -} - -/// The joined context of a list of targets, left to right. -macro_rules! joined { - ($s:ident; $first:ident) => { <$first as EncryptFrom<$s>>::Context }; - ($s:ident; $first:ident, $second:ident $(, $rest:ident)*) => { - joined!(@acc $s; <<$first as EncryptFrom<$s>>::Context as JoinContext<<$second as EncryptFrom<$s>>::Context>>::Output $(, $rest)*) - }; - (@acc $s:ident; $acc:ty) => { $acc }; - (@acc $s:ident; $acc:ty, $next:ident $(, $rest:ident)*) => { - joined!(@acc $s; <$acc as JoinContext<<$next as EncryptFrom<$s>>::Context>>::Output $(, $rest)*) - }; -} - -macro_rules! tuple_of_targets { - ($(($first:ident $(, $rest:ident)+) => $nested:pat, joins $([$($pre:ident),+; $next:ident])+;)+) => {$( - impl<S, $first, $($rest),+> EncryptFrom<S> for ($first, $($rest),+) - where - $first: EncryptFrom<S>, - $($rest: EncryptFrom<S>,)+ - $(joined!(S; $($pre),+): JoinContext<<$next as EncryptFrom<S>>::Context>,)+ - joined!(S; $first $(, $rest)+): Into<$first::Context> $(+ Into<$rest::Context>)+, - { - type Context = joined!(S; $first $(, $rest)+); - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> - where - S: 's, - { - $first::encryption() - .accepting::<Self::Context>() - $(.zip($rest::encryption().accepting::<Self::Context>()))+ - .map(|$nested| ($first, $($rest),+)) - } - fn indexes() -> Vec<IndexSpec> { - let mut indexes = $first::indexes(); - $(indexes.extend($rest::indexes());)+ - indexes - } - } - - /// The tuple is decryptable when one of its elements is. - impl<$first: Decryptable, $($rest: Decryptable),+> Decryptable for ($first, $($rest),+) { - const DECRYPTABLE: bool = $first::DECRYPTABLE $(|| $rest::DECRYPTABLE)+; - } - - /// Opened through its first element that holds recoverable - /// ciphertext ([`DecryptField`]), under the tuple's one context, as - /// a derived record opens its one decryptable field. A tuple of terms - /// alone does not open: [`Error::NotOpened`]. - impl<P: 'static, $first, $($rest),+> DecryptInto<P> for ($first, $($rest),+) - where - $first: DecryptField<P, CallerContext>, - $($rest: DecryptField<P, CallerContext>,)+ - { - type Context = CallerContext; - fn decryption<K: 'static>(self, context: CallerContext) -> Decryption<P, K> { - let ($first, $($rest),+) = self; - $first - .decryption_field(context.clone()) - $(.or_else(|| $rest.decryption_field(context.clone())))+ - .unwrap_or_else(|| Decryption::failed(Error::NotOpened)) - } - } - - /// As a field, a tuple holding no recoverable ciphertext is - /// `None`, as a term is: the search for the field that opens the - /// record moves on to the ciphertext beside it, rather than - /// stopping at a tuple that cannot open. - impl<P: 'static, Ctx, $first, $($rest),+> DecryptField<P, Ctx> for ($first, $($rest),+) - where - Self: DecryptInto<P> + Decryptable, - Ctx: Into<<Self as DecryptInto<P>>::Context>, - { - fn decryption_field<K: 'static>(self, context: Ctx) -> Option<Decryption<P, K>> { - if !<Self as Decryptable>::DECRYPTABLE { - return None; - } - Some(self.decryption(context.into())) - } - } - )+}; -} - -// The closure's pattern re-binds each output under its target's type name. -#[allow(non_snake_case)] -mod impls { - use super::*; - tuple_of_targets! { - (A, B) => (A, B), joins [A; B]; - (A, B, C) => ((A, B), C), joins [A; B] [A, B; C]; - (A, B, C, D) => (((A, B), C), D), joins [A; B] [A, B; C] [A, B, C; D]; - (A, B, C, D, E) => ((((A, B), C), D), E), joins [A; B] [A, B; C] [A, B, C; D] [A, B, C, D; E]; - } -} diff --git a/packages/stack-encrypt/tasks.toml b/packages/stack-encrypt/tasks.toml index 02913222b2..5a01cc620e 100644 --- a/packages/stack-encrypt/tasks.toml +++ b/packages/stack-encrypt/tasks.toml @@ -51,17 +51,6 @@ description = "Fuzz stack-encrypt's stored-record preflight against a model of i dir = "{{config_root}}/packages/stack-encrypt" run = "mise x --env test -- cargo +nightly fuzz run check_record --sanitizer none --target $(rustc -vV | sed -n 's/^host: //p') -- -max_total_time=60" -# Fuzz fields-plan building (`FieldsBuilder::build`) over untrusted names -# with structure-aware inputs: the context, field names, pinned identities -# and verbs a binding takes from its caller. Building and rendering must -# never panic, a sealed or indexed field that builds has a well-formed -# `<context>/<identity>` label, and passthrough names, which are under no -# label, are never refused for their text. -["fuzz:plan-build"] -description = "Fuzz stack-encrypt's fields-plan build over untrusted names (structure-aware, libFuzzer, nightly, 60s default)" -dir = "{{config_root}}/packages/stack-encrypt" -run = "mise x --env test -- cargo +nightly fuzz run plan_build --sanitizer none --target $(rustc -vV | sed -n 's/^host: //p') -- -max_total_time=60" - # Fuzz the JSON index's wire decoders: a stored `json_tree` document and the # entries `->` and the walks return come back through them before anything is # authenticated. The base64 tags, the base85 ciphertexts (SIMD-decoded on diff --git a/packages/stack-encrypt/tests/collections.rs b/packages/stack-encrypt/tests/collections.rs index 499ab9610f..f036bb6782 100644 --- a/packages/stack-encrypt/tests/collections.rs +++ b/packages/stack-encrypt/tests/collections.rs @@ -526,59 +526,6 @@ fn a_panicking_element_conversion_wipes_the_plaintext_recovered_before_it() { ); } -// --- The structural `Vec` declarations wipe what they recovered too -------- - -/// The same column, declared the structural way (`DecryptInto`). So the test -/// covers the structural `Vec<T>: DecryptInto<Vec<P>>` path. -impl stack_encrypt::DecryptInto<Tracked> for TrackedCiphertext { - type Context = stack_encrypt::target::AeadContext; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<Tracked, K> { - let wiped = self.wiped; - self.ciphertext - .decryption(OptionalContext::<ScalarContext>::supplied(context.into())) - .map(move |value: String| Tracked { value, wiped }) - } -} - -#[tokio::test] -async fn a_late_element_failure_wipes_the_recovered_prefix_on_the_structural_path_too() { - let (cipher, _, retrieves) = common::counting_cipher().await; - let keyset = cipher.default_keyset(); - let context = stack_encrypt::nonempty!("rows"); - let first = keyset - .encrypt_with_context("recovered first".to_owned(), context) - .await - .unwrap(); - // This element is sealed under another context. It fails - // authentication only once its key arrives, and by then the first - // element has been recovered. - let second = keyset - .encrypt_with_context("never recovered".to_owned(), "elsewhere") - .await - .unwrap(); - let column = vec![ - TrackedCiphertext::new(first), - TrackedCiphertext::new(second), - ]; - let wiped: Vec<_> = column.iter().map(|t| t.wiped.clone()).collect(); - // `Tracked::drop` fails the test if the recovered elements are dropped - // without being wiped. The structural path did that before it decrypted - // through the wiping collection. - let result = cipher - .decrypt_as::<Vec<Tracked>, _>(column, stack_encrypt::target::AeadContext::from(context)) - .await; - assert!(matches!(result, Err(Error::Aead)), "{:?}", result.err()); - assert!( - wiped[0].load(Ordering::SeqCst), - "the recovered prefix is wiped" - ); - assert!( - !wiped[1].load(Ordering::SeqCst), - "nothing was recovered to wipe" - ); - assert_eq!(retrieves.load(Ordering::SeqCst), 1, "both keys in one call"); -} - /// Seals a `Flagged`, and panics while declaring the element whose /// plaintext is `"panics"`. // The declaration panics before a ciphertext exists, so the ciphertext is @@ -619,16 +566,21 @@ async fn a_panicking_element_declaration_wipes_the_elements_after_it() { ); } -// --- A structural record with a column field wipes its recovered prefix ---- +// --- A record with a column field wipes its recovered prefix --------------- +// What a hand-written target needs to be a field of a derived record: it +// says that it holds ciphertext, and it opens under the field's context. impl stack_encrypt::target::Decryptable for TrackedCiphertext { const DECRYPTABLE: bool = true; } -impl<Ctx: Into<stack_encrypt::target::AeadContext>> - stack_encrypt::target::DecryptField<Tracked, Ctx> for TrackedCiphertext +impl<Ctx: Into<ScalarContext>> stack_encrypt::target::DecryptField<Tracked, Ctx> + for TrackedCiphertext { fn decryption_field<K: 'static>(self, context: Ctx) -> Option<Decryption<Tracked, K>> { - Some(stack_encrypt::DecryptInto::decryption(self, context.into())) + Some(DecryptSource::decryption( + self, + OptionalContext::<ScalarContext>::supplied(context.into()), + )) } } @@ -637,9 +589,14 @@ struct Rows { #[allow(dead_code)] items: Vec<Tracked>, } -/// A column as a field. The structural `Vec<T>: DecryptField<Vec<P>, _>` impl -/// decrypts `items`. -#[derive(stack_encrypt::DecryptInto)] +impl zeroize::Zeroize for Rows { + fn zeroize(&mut self) { + self.items.zeroize(); + } +} +/// A column as a field. The `Vec<T>: DecryptField<Vec<P>, _>` impl decrypts +/// `items`. +#[derive(DecryptSource)] #[stash(struct = Rows, scope = "rows")] struct StoredRows { items: Vec<TrackedCiphertext>, @@ -669,9 +626,7 @@ async fn a_late_element_failure_in_a_column_field_wipes_the_recovered_prefix() { let wiped: Vec<_> = stored.items.iter().map(|t| t.wiped.clone()).collect(); // `Tracked::drop` fails the test if the recovered elements are dropped // without being wiped. - let result = cipher - .decrypt_as::<Rows, _>(stored, stack_encrypt::target::DeclaredContext::default()) - .await; + let result = Rows::decrypt_from(stored, &cipher).await; assert!(matches!(result, Err(Error::Aead)), "{:?}", result.err()); assert!( wiped[0].load(Ordering::SeqCst), diff --git a/packages/stack-encrypt/tests/combinators.rs b/packages/stack-encrypt/tests/combinators.rs new file mode 100644 index 0000000000..433737b87d --- /dev/null +++ b/packages/stack-encrypt/tests/combinators.rs @@ -0,0 +1,1196 @@ +//! The combinators of `stack_encrypt::target` are the one engine every +//! declaration runs through, and the bytes are theirs. +//! +//! A derived record, a target named by its type, and a query from a field +//! marker each write what the combinators write when a test composes them +//! by hand: `indexed(..).under(label)` for a sealed field with terms, +//! `ciphertext().under(label)` for a sealed field, an index's own operation +//! for a term alone, `passthrough()` for a field carried as it is, and +//! `project` and `zip` to make a record of them. The tests run the +//! hand-composed description with `KeysetCipher::run` beside each +//! declaration and compare the descriptors sent to ZeroKMS, the terms byte +//! for byte, and that each side opens what the other sealed. +//! +//! The rest of the file holds what follows from one engine: a caller's +//! context extends every field, `Pending::zip` and `Pending::all` settle +//! several operations in one ZeroKMS request, nothing is requested before +//! an await, and every operation runs under the keyset it is given. +mod common; + +use std::fmt::Debug; +use std::sync::atomic::Ordering; + +use common::{counting_cipher, recording_cipher, stack_cipher}; +use stack_encrypt::kms::{FakeDataKeySource, IdentifiedBy}; +use stack_encrypt::sem::{ + EqualityTerm, MatchConfig, MatchOptions, MatchTerms, OpeTerm, OreBlockTerm, OreTerm, Tokenizer, +}; +use stack_encrypt::target::{ + self, equality, indexed, matching, open, passthrough, AeadContext, AllowsNoContext, Borrowed, + CallerContext, DeclaredContext, DecryptField, DecryptSource, Decryptable, Decryption, + EncryptTarget, Encrypted, Encryption, IndexSpec, Indexes, RequiredContext, Scope, TermSet, +}; +use stack_encrypt::{ + nonempty, CipherText, Decrypt, DecryptFrom, Encrypt, EncryptInto, Equality, Error, + KeysetCipher, Label, Match, NonEmpty, Ope, Ore, Pending, PlanError, StackCipherText, +}; +use zeroize::Zeroize; + +/// The record most tests encrypt: two indexed fields, one sealed alone, +/// one carried through. +#[derive(Clone, Debug, PartialEq, Zeroize)] +struct User { + email: String, + age: u32, + notes: String, + id: u64, +} + +fn user() -> User { + User { + email: "bob@example.com".into(), + age: 34, + notes: "likes cheese".into(), + id: 42, + } +} + +type EmailTerms = (EqualityTerm, MatchTerms); +type AgeTerms = (EqualityTerm, OreTerm<u32>); + +/// The derived record: each sealed field under `users/<field>`. +#[derive(EncryptTarget, DecryptSource)] +#[stash(struct = User, scope = "users")] +struct EncryptedUser { + email: Encrypted<EmailTerms>, + age: Encrypted<AgeTerms>, + notes: StackCipherText, + #[stash(passthrough)] + id: u64, +} + +/// The same record, whose caller supplies a tenant on every call. +#[derive(EncryptTarget, DecryptSource)] +#[stash(struct = User, scope = "users", scope(tenant: u64))] +struct TenantUser { + email: Encrypted<EmailTerms>, + age: Encrypted<AgeTerms>, + notes: StackCipherText, + #[stash(passthrough)] + id: u64, +} + +fn plan_error<T: Debug>(result: Result<T, Error>) -> PlanError { + match result { + Err(Error::Plan(error)) => error, + other => panic!("expected a plan error, got {other:?}"), + } +} + +fn leaf_keyset(ciphertext: &StackCipherText) -> uuid::Uuid { + match ciphertext { + CipherText::Single(leaf) => leaf.keyset_id(), + other => panic!("expected a single leaf, got {other:?}"), + } +} + +/// The hand-composed combinator spelling of the four-field record, each +/// field under the derive's spelling of its context. +type HandOut = ( + ( + (Encrypted<EmailTerms>, Encrypted<AgeTerms>), + StackCipherText, + ), + u64, +); +fn hand_composed<'s, K: 'static>() -> Encryption<'s, User, HandOut, K, DeclaredContext> { + indexed::<String, _, Borrowed, _>((Equality, Match)) + .under(nonempty!("users").with("email")) + .project(|u: &User| &u.email) + .zip( + indexed::<u32, _, Borrowed, _>((Equality, Ore)) + .under(nonempty!("users").with("age")) + .project(|u: &User| &u.age), + ) + .zip( + target::ciphertext::<String, _, Borrowed>() + .under(nonempty!("users").with("notes")) + .project(|u: &User| &u.notes), + ) + .zip(passthrough::<u64, _, Borrowed, DeclaredContext>().project(|u: &User| &u.id)) +} + +/// One document sealed alone under `docs`, as a pending operation. +fn seal_doc<'a, K: 'static>(keyset: &'a KeysetCipher<'_, K>) -> Pending<'a, StackCipherText, K> { + String::from("doc").encrypt_into_with_context(keyset, nonempty!("docs")) +} + +// --- A derived record is the combinators --------------------------------------- + +#[tokio::test] +async fn a_derived_record_is_the_hand_composed_combinators_field_for_field() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + let user = user(); + + let derived: EncryptedUser = user.clone().encrypt_into(&keyset).await.unwrap(); + let derived_sent = sent.lock().unwrap().generated(); + sent.lock().unwrap().generate.clear(); + + let (((email, age), notes), id) = keyset + .run(hand_composed(), &user, DeclaredContext::default()) + .await + .unwrap(); + let hand_sent = sent.lock().unwrap().generated(); + + assert_eq!( + derived_sent, hand_sent, + "every data key under the same descriptor" + ); + assert_eq!( + derived_sent, + ["users/email", "users/age", "users/notes"], + "one key per sealed field, under <scope>/<field>" + ); + assert_eq!(derived.id, id, "passthrough is the value"); + assert_eq!(derived.email.terms.0, email.terms.0, "email equality term"); + assert_eq!( + derived.email.terms.1.to_bytes(), + email.terms.1.to_bytes(), + "email match terms" + ); + assert_eq!(derived.age.terms.0, age.terms.0, "age equality term"); + assert_eq!( + derived.age.terms.1.to_bytes(), + age.terms.1.to_bytes(), + "age ORE term" + ); + + // Ciphertexts are randomised, so they are compared by opening each + // side's with the other's reader. The derived record's fields open + // through the cipher under the labels the combinators sealed under. + let cipher = &cipher; + let open = |ct: StackCipherText, field: &str| { + let context = nonempty!("users").with(field.to_owned()); + async move { cipher.decrypt::<String, _>(ct, context).await } + }; + assert_eq!( + open(derived.email.ciphertext, "email").await.unwrap(), + user.email + ); + assert_eq!(open(derived.notes, "notes").await.unwrap(), user.notes); + let derived_age: u32 = cipher + .decrypt(derived.age.ciphertext, nonempty!("users").with("age")) + .await + .unwrap(); + assert_eq!(derived_age, user.age); + + // And the hand-composed fields open as the derived record. + let hand_row = EncryptedUser { + email, + age, + notes, + id, + }; + let back = User::decrypt_from(hand_row, cipher).await.unwrap(); + assert_eq!(back, user); +} + +#[tokio::test] +async fn a_marker_query_derives_under_the_label_the_write_used() { + let cipher = stack_cipher().await; + let keyset = cipher.default_keyset(); + // The stored terms are the combinators', so a query that matches them + // is derived as the combinators derive. + let (((email, age), _), _) = keyset + .run(hand_composed(), &user(), DeclaredContext::default()) + .await + .unwrap(); + + let eq: EqualityTerm = EncryptedUser::EMAIL + .eq("bob@example.com") + .encrypt(&keyset) + .await + .unwrap(); + assert_eq!( + eq, email.terms.0, + "equality query bytes equal the stored term" + ); + let matching = EncryptedUser::EMAIL + .matches("bob@example.com") + .encrypt(&keyset) + .await + .unwrap(); + assert_eq!(matching.to_bytes(), email.terms.1.to_bytes()); + let ore: OreTerm<u32> = EncryptedUser::AGE.lt(34u32).encrypt(&keyset).await.unwrap(); + assert_eq!(ore.to_bytes(), age.terms.1.to_bytes()); + let by_age: EqualityTerm = EncryptedUser::AGE.eq(34u32).encrypt(&keyset).await.unwrap(); + assert_eq!(by_age, age.terms.0, "each operator through its own index"); + + // A different value is a different term: the query is not a constant. + let other = EncryptedUser::EMAIL + .eq("eve@example.com") + .encrypt(&keyset) + .await + .unwrap(); + assert_ne!(other, email.terms.0); +} + +/// A record whose `email` is its terms alone: searchable, and not stored. +#[derive(EncryptTarget)] +#[stash(struct = User, scope = "users")] +struct IndexedUser { + email: EqualityTerm, + age: StackCipherText, + notes: StackCipherText, + #[stash(passthrough)] + id: u64, +} + +/// A field of terms alone asks for no data key, a query of the field +/// matches the stored term, and the term is the bytes a sealed field +/// stores beside its ciphertext. +#[tokio::test] +async fn a_field_of_terms_alone_matches_its_query_and_mints_no_data_key() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + let row: IndexedUser = user().encrypt_into(&keyset).await.unwrap(); + assert_eq!( + sent.lock().unwrap().generated(), + ["users/age", "users/notes"], + "a field of terms alone asks for no data key" + ); + assert_eq!(row.id, 42); + let age: u32 = cipher + .decrypt(row.age, nonempty!("users").with("age")) + .await + .unwrap(); + assert_eq!(age, 34, "the fields beside it are sealed as before"); + let notes: String = cipher + .decrypt(row.notes, nonempty!("users").with("notes")) + .await + .unwrap(); + assert_eq!(notes, "likes cheese"); + + let query = IndexedUser::EMAIL + .eq("bob@example.com") + .encrypt(&keyset) + .await + .unwrap(); + assert_eq!( + query, row.email, + "the query derives under the write's label" + ); + + // Byte-identical to the term the combinators derive alone, and to the + // terms a sealed field writes for the same field. + sent.lock().unwrap().generate.clear(); + let (eq, matched): EmailTerms = keyset + .run( + Indexes::<String>::operations::<_, Borrowed>(&(Equality, Match)) + .under(nonempty!("users").with("email")), + &user().email, + DeclaredContext::default(), + ) + .await + .unwrap(); + assert!( + sent.lock().unwrap().generated().is_empty(), + "the indexes' operations ask for no data key" + ); + assert_eq!(eq, row.email); + let sealed: EncryptedUser = user().encrypt_into(&keyset).await.unwrap(); + assert_eq!(eq, sealed.email.terms.0); + assert_eq!(matched.to_bytes(), sealed.email.terms.1.to_bytes()); +} + +/// A record with a field keyed under another segment than its name. +#[derive(Clone, Zeroize)] +struct Secret { + totp: String, + attempts: u32, +} +#[derive(EncryptTarget, DecryptSource)] +#[stash(struct = Secret, scope = "users")] +struct EncryptedSecret { + #[stash(identity = "totp_secret")] + totp: StackCipherText, + #[stash(identity = "value")] + attempts: Encrypted<EqualityTerm>, +} + +/// A pinned identity keys the field: the write, the query and the read all +/// use `<scope>/<identity>`, never the field's name. +#[tokio::test] +async fn a_pinned_identity_keys_the_field_for_the_write_the_query_and_the_read() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + assert_eq!( + EncryptedSecret::TOTP.label().to_string(), + "users/totp_secret" + ); + assert_eq!(EncryptedSecret::TOTP.name(), "totp"); + let secret = Secret { + totp: "s".into(), + attempts: 21, + }; + let record: EncryptedSecret = secret.encrypt_into(&keyset).await.unwrap(); + assert_eq!( + sent.lock().unwrap().generated(), + ["users/totp_secret", "users/value"] + ); + + // The same bytes as the combinators under the pinned label. + let by_hand: Encrypted<EqualityTerm> = keyset + .run( + indexed::<u32, _, Borrowed, _>(Equality).under(nonempty!("users").with("value")), + &21u32, + DeclaredContext::default(), + ) + .await + .unwrap(); + assert_eq!(record.attempts.terms, by_hand.terms); + let query = EncryptedSecret::ATTEMPTS + .eq(21u32) + .encrypt(&keyset) + .await + .unwrap(); + assert_eq!(query, record.attempts.terms); + let opened: u32 = cipher + .decrypt(record.attempts.ciphertext, nonempty!("users").with("value")) + .await + .unwrap(); + assert_eq!(opened, 21); + let totp: String = cipher + .decrypt(record.totp, nonempty!("users").with("totp_secret")) + .await + .unwrap(); + assert_eq!(totp, "s"); +} + +/// A target that says it holds ciphertext and then opens nothing. +#[allow(dead_code)] // never read: it only says it holds ciphertext +struct Lying(StackCipherText); +impl EncryptTarget<u32> for Lying { + type ContextInput = RequiredContext<AeadContext>; + fn encryption<'s, K: 'static>() -> Encryption<'s, u32, Self, K, Self::ContextInput> { + <StackCipherText as EncryptTarget<u32>>::encryption().map(Lying) + } +} +impl Decryptable for Lying { + const DECRYPTABLE: bool = true; +} +impl<P, Ctx> DecryptField<P, Ctx> for Lying { + fn decryption_field<K: 'static>(self, _: Ctx) -> Option<Decryption<P, K>> { + None + } +} + +#[derive(Clone, Zeroize)] +struct Counter { + count: u32, +} +#[derive(EncryptTarget, DecryptSource)] +#[stash(struct = Counter, scope = "counters")] +struct LyingCounter { + count: Lying, +} + +#[tokio::test] +async fn a_field_that_opens_nothing_is_not_opened() { + let cipher = stack_cipher().await; + let keyset = cipher.default_keyset(); + let record: LyingCounter = Counter { count: 7 }.encrypt_into(&keyset).await.unwrap(); + let result = Counter::decrypt_from(record, &keyset).await; + assert!( + matches!(result.as_ref().map(|c| c.count), Err(Error::NotOpened)), + "the record is refused, not opened without its field" + ); +} + +// --- A record of one plaintext is the combinators --------------------------------- + +/// One value, sealed with its equality term beside it, under the one +/// context the caller supplies. +#[derive(EncryptTarget, DecryptSource)] +#[stash(plaintext = String)] +struct DerivedEmail { + c: StackCipherText, + hm: EqualityTerm, +} + +#[tokio::test] +async fn a_plaintext_record_is_the_combinators_bytes() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + let context = || CallerContext::from(nonempty!("users").with("email")); + let email = String::from("bob@example.com"); + + // The ciphertext takes the AEAD half of the context its term takes. + let (c, hm): (StackCipherText, EqualityTerm) = keyset + .run( + target::ciphertext::<String, _, Borrowed>() + .accepting::<CallerContext>() + .zip(equality()), + &email, + context(), + ) + .await + .unwrap(); + let derived: DerivedEmail = email + .clone() + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + assert_eq!( + sent.lock().unwrap().generated(), + ["users/email", "users/email"] + ); + assert_eq!(hm, derived.hm, "the same equality term"); + + // The record answers the query its term answers: the term alone, + // under the same context. + let query: EqualityTerm = email + .clone() + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + assert_eq!(query, derived.hm); + + // Each ciphertext opens through the other's reader. + let opened = + String::decrypt_from_with_context(DerivedEmail { c, hm: hm.clone() }, &cipher, context()) + .await + .unwrap(); + assert_eq!(opened, email); + let back: String = cipher + .run_decryption(open(derived.c, context())) + .await + .unwrap(); + assert_eq!(back, email); +} + +/// A record whose terms come before its ciphertext. +#[derive(EncryptTarget, DecryptSource)] +#[stash(plaintext = u32)] +struct TermsFirst { + hm: EqualityTerm, + ore: OreTerm<u32>, + c: StackCipherText, +} + +/// A term is no field to open a record through: it yields to the +/// ciphertext beside it, wherever the ciphertext sits. +#[tokio::test] +async fn a_record_opens_through_its_ciphertext_wherever_it_sits() { + let cipher = stack_cipher().await; + let keyset = cipher.default_keyset(); + let context = || CallerContext::from(nonempty!("t")); + + let record: TermsFirst = 7u32 + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + let by_hand: Encrypted<(EqualityTerm, OreTerm<u32>)> = keyset + .run( + indexed::<u32, _, Borrowed, _>((Equality, Ore)), + &7u32, + context(), + ) + .await + .unwrap(); + assert_eq!(record.hm, by_hand.terms.0); + assert_eq!(record.ore.to_bytes(), by_hand.terms.1.to_bytes()); + let opened = u32::decrypt_from_with_context(record, &cipher, context()) + .await + .unwrap(); + assert_eq!(opened, 7); + + // A term opens nothing, and the ciphertext opens under the context. + let term: Option<Decryption<u32, FakeDataKeySource>> = + by_hand.terms.0.clone().decryption_field(context()); + assert!(term.is_none(), "a term alone opens nothing"); + let opening: Option<Decryption<u32, FakeDataKeySource>> = by_hand.decryption_field(context()); + let opened = cipher.run_decryption(opening.unwrap()).await.unwrap(); + assert_eq!(opened, 7); +} + +// --- One value --------------------------------------------------------------- + +#[tokio::test] +async fn one_value_with_indexes_is_the_indexed_combinator_and_reads_back() { + let cipher = stack_cipher().await; + let keyset = cipher.default_keyset(); + let context = || nonempty!("users").with("age"); + let out: Encrypted<AgeTerms> = 34u32 + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + + // Byte-identical to the combinator, under the same label. + let hand = keyset + .run( + indexed::<u32, _, Borrowed, _>((Equality, Ore)), + &34u32, + CallerContext::from(NonEmpty::from(Label::parse("users/age").unwrap())), + ) + .await + .unwrap(); + assert_eq!(hand.terms.0, out.terms.0); + assert_eq!(hand.terms.1.to_bytes(), out.terms.1.to_bytes()); + + // A term alone, under the same context, is the term stored beside the + // ciphertext: what a query for the value sends. + let eq: EqualityTerm = 34u32 + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + assert_eq!(eq, out.terms.0); + let ore: OreTerm<u32> = 34u32 + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + assert_eq!(ore.to_bytes(), out.terms.1.to_bytes()); + + let age = u32::decrypt_from_with_context(out, &cipher, context()) + .await + .unwrap(); + assert_eq!(age, 34); + let age = u32::decrypt_from_with_context(hand.ciphertext, &cipher, context()) + .await + .unwrap(); + assert_eq!(age, 34, "a bare ciphertext opens too"); +} + +#[tokio::test] +async fn one_tree_under_a_label_is_the_ciphertext_combinator_under_it() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + let body = String::from("the document"); + let label = Label::parse("documents/v2/body").unwrap(); + + let tree = keyset.encrypt_tree(&body, &label).await.unwrap(); + let by_hand: StackCipherText = keyset + .run( + target::ciphertext::<String, _, Borrowed>().under(NonEmpty::from(label.clone())), + &body, + DeclaredContext::default(), + ) + .await + .unwrap(); + assert_eq!( + sent.lock().unwrap().generated(), + ["documents/v2/body", "documents/v2/body"] + ); + for ciphertext in [tree, by_hand] { + let back: String = cipher.decrypt(ciphertext, &label).await.unwrap(); + assert_eq!(back, body); + } + assert!( + Label::parse("not//plain").is_err(), + "a label is plain segments" + ); +} + +// --- A target named by its type is the indexes named as values ----------------- + +/// `Encrypted<Terms>` as a target beside `indexed(indexes)`: the same +/// descriptors, the same terms, and each opens through the other's reader. +async fn same_field_bytes<F, Terms, X>(value: F, indexes: X) +where + F: Encrypt + Decrypt + Clone + Zeroize + Send + Sync + PartialEq + Debug + 'static, + Terms: TermSet<F> + PartialEq + Debug + Send, + X: Indexes<F, Terms = Terms> + Send + Sync + 'static, +{ + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + let context = || nonempty!("t").with("v"); + assert_eq!( + <Encrypted<Terms> as EncryptTarget<F>>::indexes(), + indexes.specs(), + "the target declares the indexes the values name" + ); + + let typed: Encrypted<Terms> = value + .clone() + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + let data: Encrypted<Terms> = keyset + .run( + indexed::<F, _, Borrowed, _>(indexes).under(context()), + &value, + DeclaredContext::default(), + ) + .await + .unwrap(); + assert_eq!(sent.lock().unwrap().generated(), ["t/v", "t/v"]); + assert_eq!(typed.terms, data.terms, "the same terms"); + + let back: F = cipher.decrypt(typed.ciphertext, context()).await.unwrap(); + assert_eq!( + back, value, + "the cipher opens the typed target's ciphertext" + ); + let back = F::decrypt_from_with_context(data, &keyset, context()) + .await + .unwrap(); + assert_eq!( + back, value, + "the typed reader opens the combinator's output" + ); +} + +/// A configuration other than the default. +struct Words; +impl MatchConfig for Words { + fn options() -> MatchOptions { + MatchOptions { + tokenizer: Tokenizer::Standard, + downcase: false, + short_words: false, + k: 4, + m: 512, + } + } +} + +type M = MatchTerms; +type W = MatchTerms<Words>; + +#[tokio::test] +async fn a_typed_text_target_is_the_indexes_bytes_for_every_index_set() { + let text = || String::from("Hello World"); + let ore = Ore; + let ope = Ope; + let eq = Equality; + let m = Match::default; + same_field_bytes::<_, EqualityTerm, _>(text(), eq).await; + same_field_bytes::<_, M, _>(text(), m()).await; + same_field_bytes::<_, W, _>(text(), Match::<Words>::new()).await; + same_field_bytes::<_, OreTerm<String>, _>(text(), ore).await; + same_field_bytes::<_, OpeTerm<String>, _>(text(), ope).await; + same_field_bytes::<_, (EqualityTerm, M), _>(text(), (eq, m())).await; + same_field_bytes::<_, (EqualityTerm, OreTerm<String>), _>(text(), (eq, ore)).await; + same_field_bytes::<_, (EqualityTerm, OpeTerm<String>), _>(text(), (eq, ope)).await; + same_field_bytes::<_, (M, OreTerm<String>), _>(text(), (m(), ore)).await; + same_field_bytes::<_, (M, OpeTerm<String>), _>(text(), (m(), ope)).await; + same_field_bytes::<_, (EqualityTerm, M, OreTerm<String>), _>(text(), (eq, m(), ore)).await; + same_field_bytes::<_, (EqualityTerm, M, OpeTerm<String>), _>(text(), (eq, m(), ope)).await; +} + +#[tokio::test] +async fn a_typed_integer_target_is_the_indexes_bytes_for_every_index_set() { + same_field_bytes::<_, EqualityTerm, _>(7u32, Equality).await; + same_field_bytes::<_, OreTerm<u32>, _>(7u32, Ore).await; + same_field_bytes::<_, OpeTerm<u32>, _>(7u32, Ope).await; + same_field_bytes::<_, (EqualityTerm, OreTerm<u32>), _>(7u32, (Equality, Ore)).await; + same_field_bytes::<_, (EqualityTerm, OpeTerm<u32>), _>(7u32, (Equality, Ope)).await; + same_field_bytes::<_, (OreTerm<u32>, EqualityTerm), _>(7u32, (Ore, Equality)).await; +} + +#[test] +fn every_target_declares_the_indexes_its_terms_answer() { + assert_eq!( + <EqualityTerm as EncryptTarget<String>>::indexes(), + [IndexSpec::Equality] + ); + assert_eq!( + <MatchTerms<Words> as EncryptTarget<String>>::indexes(), + [IndexSpec::Match(Words::options())] + ); + assert_eq!( + <OreTerm<u32> as EncryptTarget<u32>>::indexes(), + [IndexSpec::Ore] + ); + assert_eq!( + <OpeTerm<u32> as EncryptTarget<u32>>::indexes(), + [IndexSpec::Ope] + ); + assert_eq!( + <OreBlockTerm<u32> as EncryptTarget<u32>>::indexes(), + [IndexSpec::OreBlock] + ); + assert!(<StackCipherText as EncryptTarget<u32>>::indexes().is_empty()); + assert_eq!( + <Encrypted<(OreTerm<u32>, EqualityTerm)> as EncryptTarget<u32>>::indexes(), + [IndexSpec::Ore, IndexSpec::Equality] + ); + assert_eq!( + <DerivedEmail as EncryptTarget<String>>::indexes(), + [IndexSpec::Equality], + "a record of one plaintext declares the indexes of its terms" + ); + assert_eq!( + <TermsFirst as EncryptTarget<u32>>::indexes(), + [IndexSpec::Equality, IndexSpec::Ore] + ); +} + +/// Five terms over text: equality, two match terms under different options, +/// ORE and OPE. +type FiveTerms = (EqualityTerm, M, OreTerm<String>, OpeTerm<String>, W); + +/// A record whose one field names the equality index twice. +#[derive(EncryptTarget)] +#[stash(struct = Counter, scope = "counters")] +#[allow(dead_code)] // never read: its declaration is refused +struct TwiceCounter { + count: Encrypted<(EqualityTerm, EqualityTerm)>, +} + +/// A target holds one term for each kind of index, so a target that names +/// an index twice is refused when it runs, before any key is requested: +/// alone, or as a field of a derived record. Two match indexes under +/// different options are still the match index twice. +#[tokio::test] +async fn a_target_that_names_an_index_twice_is_refused_before_any_key_request() { + let (cipher, generates, _) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let context = || nonempty!("t").with("v"); + + let five: Result<Encrypted<FiveTerms>, _> = String::from("Hello World") + .encrypt_into_with_context(&keyset, context()) + .await; + assert_eq!( + plan_error(five), + PlanError::DuplicateIndex { + at: "the value".into(), + index: "match" + } + ); + let twice: Result<Encrypted<(EqualityTerm, EqualityTerm)>, _> = + 7u32.encrypt_into_with_context(&keyset, context()).await; + assert_eq!( + plan_error(twice), + PlanError::DuplicateIndex { + at: "the value".into(), + index: "eq" + } + ); + let in_a_record: Result<TwiceCounter, _> = Counter { count: 7 }.encrypt_into(&keyset).await; + assert!(matches!( + plan_error(in_a_record.map(drop)), + PlanError::DuplicateIndex { index: "eq", .. } + )); + assert_eq!(generates.load(Ordering::SeqCst), 0, "no key requested"); +} + +// --- The caller's context ------------------------------------------------------- + +/// The context a field of the record is sealed under when the caller +/// supplies `parts`: the field's own label, extended by them. +fn extended(field: &'static str, parts: DeclaredContext) -> CallerContext { + parts.under(nonempty!("users").with(field)) +} + +#[tokio::test] +async fn a_callers_part_extends_every_field_and_reading_back_needs_it() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + let user = user(); + let (((email, _), _), _) = keyset + .run(hand_composed(), &user, DeclaredContext::from(7u64)) + .await + .unwrap(); + assert_eq!( + sent.lock().unwrap().generated(), + [ + "(users/email)/7u64", + "(users/age)/7u64", + "(users/notes)/7u64" + ] + ); + // A derived record whose caller supplies the part sends the same. + sent.lock().unwrap().generate.clear(); + let derived: TenantUser = user + .clone() + .encrypt_into_with_context(&keyset, Scope::new(7u64)) + .await + .unwrap(); + assert_eq!( + sent.lock().unwrap().generated(), + [ + "(users/email)/7u64", + "(users/age)/7u64", + "(users/notes)/7u64" + ] + ); + assert_eq!(derived.email.terms.0, email.terms.0); + + let back: String = cipher + .run_decryption(open( + derived.email.ciphertext, + extended("email", DeclaredContext::from(7u64)), + )) + .await + .unwrap(); + assert_eq!(back, "bob@example.com"); + + let unextended: Result<String, _> = cipher + .run_decryption(open( + email.ciphertext, + extended("email", DeclaredContext::default()), + )) + .await; + assert!( + matches!(unextended, Err(Error::Aead)), + "the fake key source ignores descriptors, so a missing extension reaches the AEAD" + ); + let row: TenantUser = user + .encrypt_into_with_context(&keyset, Scope::new(7u64)) + .await + .unwrap(); + let other = User::decrypt_from_with_context(row, &keyset, Scope::new(8u64)).await; + assert!(matches!(other, Err(Error::Aead)), "{:?}", other.err()); +} + +/// Repeated parts append in order, and opening needs both. +#[tokio::test] +async fn a_second_part_appends_to_the_first() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + let parts = || DeclaredContext::from(1u64).with(7u64); + let (((email, _), _), _) = keyset.run(hand_composed(), &user(), parts()).await.unwrap(); + assert_eq!( + sent.lock().unwrap().generated(), + [ + "((users/email)/1u64)/7u64", + "((users/age)/1u64)/7u64", + "((users/notes)/1u64)/7u64" + ] + ); + let back: Result<String, _> = cipher + .run_decryption(open(email.ciphertext, extended("email", parts()))) + .await; + assert!(back.is_ok(), "{back:?}"); +} + +/// An extension reaches the write, the query and the open of one value: +/// the data key is asked for under the extended label, a term matches only +/// under the same extension, and the value opens only with it. +#[tokio::test] +async fn one_value_extended_on_write_needs_the_extension_to_query_and_open() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + let label = || NonEmpty::from(Label::parse("users/age").unwrap()); + let write = || { + keyset.run( + indexed::<u32, _, Borrowed, _>(Equality).under(label()), + &34u32, + DeclaredContext::from(7u64), + ) + }; + + let out = write().await.unwrap(); + assert_eq!(sent.lock().unwrap().generated(), ["(users/age)/7u64"]); + + let query = |parts: Option<u64>| { + keyset.run( + equality::<u32, _, Borrowed>().under(label()), + &34u32, + parts.map_or_else(DeclaredContext::default, DeclaredContext::from), + ) + }; + assert_eq!(query(Some(7)).await.unwrap(), out.terms); + assert_ne!(query(None).await.unwrap(), out.terms); + assert_ne!(query(Some(8)).await.unwrap(), out.terms); + + let opening = |sealed: Encrypted<EqualityTerm>, parts: DeclaredContext| { + cipher.run_decryption(open::<u32, _>(sealed.ciphertext, parts.under(label()))) + }; + let back = opening(out, DeclaredContext::from(7u64)).await.unwrap(); + assert_eq!(back, 34); + let unextended = opening(write().await.unwrap(), DeclaredContext::default()).await; + assert!(matches!(unextended, Err(Error::Aead)), "{unextended:?}"); + let other = opening(write().await.unwrap(), DeclaredContext::from(8u64)).await; + assert!(matches!(other, Err(Error::Aead)), "{other:?}"); +} + +#[tokio::test] +async fn a_query_extended_like_the_write_matches_it() { + let cipher = stack_cipher().await; + let keyset = cipher.default_keyset(); + let row: TenantUser = user() + .encrypt_into_with_context(&keyset, Scope::new(7u64)) + .await + .unwrap(); + let query = |tenant: u64| { + TenantUser::EMAIL + .eq("bob@example.com") + .scope(Scope::new(tenant)) + .encrypt(&keyset) + }; + assert_eq!(query(7).await.unwrap(), row.email.terms.0); + assert_ne!(query(8).await.unwrap(), row.email.terms.0); + // A query that names no tenant derives the term of a record that has + // none, which is another term. + let unextended = EncryptedUser::EMAIL + .eq("bob@example.com") + .encrypt(&keyset) + .await + .unwrap(); + assert_ne!(unextended, row.email.terms.0); +} + +// --- One request for several operations ------------------------------------------ + +#[tokio::test] +async fn zip_settles_several_operations_in_one_request() { + let (cipher, generates, retrieves) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let user = user(); + let write = || user.clone().encrypt_into::<EncryptedUser, _>(&keyset); + + let (row, probe) = write() + .zip(EncryptedUser::EMAIL.eq("bob@example.com").encrypt(&keyset)) + .await + .unwrap(); + assert_eq!(generates.load(Ordering::SeqCst), 1, "one generate call"); + assert_eq!(row.email.terms.0, probe); + + let ((a, b), c) = seal_doc(&keyset) + .zip(write()) + .zip(User::decrypt_from(row, &keyset)) + .await + .unwrap(); + assert_eq!( + generates.load(Ordering::SeqCst), + 2, + "one more generate call" + ); + assert_eq!(retrieves.load(Ordering::SeqCst), 1, "one retrieve call"); + assert_eq!(c.notes, "likes cheese"); + let (((_, _), opened), d) = seal_doc(&keyset) + .zip(seal_doc(&keyset)) + .zip(String::decrypt_from_with_context( + a, + &keyset, + nonempty!("docs"), + )) + .zip(write()) + .await + .unwrap(); + assert_eq!(generates.load(Ordering::SeqCst), 3); + assert_eq!(retrieves.load(Ordering::SeqCst), 2); + assert_eq!(opened, "doc"); + assert_eq!(d.id, 42); + let _ = b; +} + +#[tokio::test] +async fn zip_runs_under_one_keyset_and_refuses_two() { + let (cipher, generates, _) = counting_cipher().await; + let tenant = cipher + .keyset(IdentifiedBy::Name("tenant".to_string().into())) + .await + .unwrap(); + let default = cipher.default_keyset(); + + let (a, b) = seal_doc(&tenant).zip(seal_doc(&tenant)).await.unwrap(); + assert_eq!(leaf_keyset(&a), tenant.keyset_id()); + assert_eq!(leaf_keyset(&b), tenant.keyset_id()); + generates.store(0, Ordering::SeqCst); + + let refused = seal_doc(&tenant).zip(seal_doc(&default)).await; + assert!(matches!(refused, Err(Error::KeysetMismatch { .. }))); + assert_eq!( + generates.load(Ordering::SeqCst), + 0, + "refused before any request" + ); +} + +/// One operation that is refused fails the whole batch before any key is +/// requested, however valid the operations beside it are. +#[tokio::test] +async fn zip_refuses_a_failed_operation_beside_a_good_one_before_any_request() { + let (cipher, generates, retrieves) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let row: EncryptedUser = user().encrypt_into(&keyset).await.unwrap(); + generates.store(0, Ordering::SeqCst); + let result = seal_doc(&keyset) + .zip(User::decrypt_from(row, &keyset)) + .zip( + // Text with no normal form: no term exists for it. + EncryptedUser::EMAIL + .eq("bob\u{0378}@example.com") + .encrypt(&keyset), + ) + .await; + assert!(matches!(result, Err(Error::Term(_)))); + assert_eq!(generates.load(Ordering::SeqCst), 0); + assert_eq!(retrieves.load(Ordering::SeqCst), 0); +} + +#[tokio::test] +async fn a_collection_runs_in_one_request() { + let (cipher, generates, retrieves) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let users = vec![user(), user(), user()]; + + let rows: Vec<EncryptedUser> = users.clone().encrypt_into(&keyset).await.unwrap(); + assert_eq!(rows.len(), 3); + assert_eq!( + generates.load(Ordering::SeqCst), + 1, + "one generate for every row" + ); + // A slice: each record's own description, merged into one request. + let rows: Vec<EncryptedUser> = Pending::all( + &keyset, + users[..2] + .iter() + .map(|user| { + keyset.run( + <EncryptedUser as EncryptTarget<User>>::encryption(), + user, + AllowsNoContext::omitted(), + ) + }) + .collect(), + ) + .await + .unwrap(); + assert_eq!(rows.len(), 2); + assert_eq!(generates.load(Ordering::SeqCst), 2); + + let back = Vec::<User>::decrypt_from(rows, &keyset).await.unwrap(); + assert_eq!(back.len(), 2); + assert_eq!( + retrieves.load(Ordering::SeqCst), + 1, + "one retrieve for every row" + ); + assert_eq!(back[1].age, 34); + + let ages = vec![30u32, 31, 32]; + let context = || nonempty!("users").with("age"); + let written: Vec<Encrypted<EqualityTerm>> = ages + .clone() + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + assert_eq!(written.len(), 3); + assert_eq!(generates.load(Ordering::SeqCst), 3); + let back = Vec::<u32>::decrypt_from_with_context(written, &keyset, context()) + .await + .unwrap(); + assert_eq!(back, ages); + assert_eq!( + retrieves.load(Ordering::SeqCst), + 2, + "one retrieve for the Vec" + ); + + let written: Vec<Encrypted<EqualityTerm>> = ages + .clone() + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + let ciphertexts: Vec<StackCipherText> = written.into_iter().map(|e| e.ciphertext).collect(); + let back = Vec::<u32>::decrypt_from_with_context(ciphertexts, &keyset, context()) + .await + .unwrap(); + assert_eq!(back, ages, "a Vec of bare ciphertexts opens too"); + assert_eq!(retrieves.load(Ordering::SeqCst), 3); +} + +#[tokio::test] +async fn nothing_touches_a_key_before_the_await() { + let (cipher, generates, retrieves) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let user = user(); + + let dropped = user.clone().encrypt_into::<EncryptedUser, _>(&keyset); + drop(dropped); + assert_eq!(generates.load(Ordering::SeqCst), 0); + + // An operation yields its Pending synchronously: terms derived, key + // requests queued, nothing sent until it is awaited. + let pending = user.encrypt_into::<EncryptedUser, _>(&keyset); + assert_eq!(generates.load(Ordering::SeqCst), 0, "prepared, not sent"); + let row = pending.await.unwrap(); + assert_eq!(generates.load(Ordering::SeqCst), 1); + let pending = User::decrypt_from(row, &keyset); + assert_eq!(retrieves.load(Ordering::SeqCst), 0); + assert!(pending.await.is_ok()); + assert_eq!(retrieves.load(Ordering::SeqCst), 1); +} + +/// A declaration is a description held by value: `KeysetCipher::run` runs +/// the encryption over a source, and `run_decryption` runs the decryption +/// a stored record describes. +#[tokio::test] +async fn a_declaration_is_an_encryption_and_a_decryption_held_by_value() { + let cipher = stack_cipher().await; + let keyset = cipher.default_keyset(); + let encryption = <EncryptedUser as EncryptTarget<User>>::encryption(); + let row = keyset + .run(encryption, &user(), AllowsNoContext::omitted()) + .await + .unwrap(); + let decryption: Decryption<User, _> = row.decryption(AllowsNoContext::omitted()); + let back = keyset.run_decryption(decryption).await.unwrap(); + assert_eq!(back.id, 42); + + // The combinators describe a term alone the same way. + let term = keyset + .run( + matching::<String, _, Borrowed, stack_encrypt::sem::DefaultMatch>() + .under(nonempty!("users").with("email")), + &user().email, + DeclaredContext::default(), + ) + .await + .unwrap(); + let sealed: EncryptedUser = user().encrypt_into(&keyset).await.unwrap(); + assert_eq!(term.to_bytes(), sealed.email.terms.1.to_bytes()); +} + +// --- Keysets --------------------------------------------------------------------- + +#[tokio::test] +async fn every_operation_honours_the_keyset_it_runs_through() { + let cipher = stack_cipher().await; + let tenant = cipher + .keyset(IdentifiedBy::Name("tenant".to_string().into())) + .await + .unwrap(); + let default = cipher.default_keyset(); + let id = tenant.keyset_id(); + assert_ne!(id, default.keyset_id()); + let user = user(); + + let indexed: Encrypted<EqualityTerm> = 34u32 + .encrypt_into_with_context(&tenant, nonempty!("users").with("age")) + .await + .unwrap(); + assert_eq!(leaf_keyset(&indexed.ciphertext), id); + + let (((_, _), notes), _) = tenant + .run(hand_composed(), &user, DeclaredContext::default()) + .await + .unwrap(); + assert_eq!(leaf_keyset(&notes), id); + + let row: EncryptedUser = user.clone().encrypt_into(&tenant).await.unwrap(); + assert_eq!(leaf_keyset(&row.notes), id); + + // A term is keyed by the keyset's index key: a query under the tenant + // matches the tenant's term, not the default keyset's. + let query = |keyset| EncryptedUser::EMAIL.eq("bob@example.com").encrypt(keyset); + assert_eq!(query(&tenant).await.unwrap(), row.email.terms.0); + assert_ne!(query(&default).await.unwrap(), row.email.terms.0); + + // The record opens through its own keyset, and through the client, + // which opens a leaf from any keyset. Another keyset refuses it. + let row: EncryptedUser = user.clone().encrypt_into(&tenant).await.unwrap(); + assert_eq!(User::decrypt_from(row, &cipher).await.unwrap(), user); + let row: EncryptedUser = user.clone().encrypt_into(&tenant).await.unwrap(); + let refused = User::decrypt_from(row, &default).await; + assert!( + matches!(refused, Err(Error::ForeignKeyset { .. })), + "limited to a keyset, a leaf from another is refused: {refused:?}" + ); +} diff --git a/packages/stack-encrypt/tests/context_conformance.rs b/packages/stack-encrypt/tests/context_conformance.rs index dbb0f6d596..3af544f6bf 100644 --- a/packages/stack-encrypt/tests/context_conformance.rs +++ b/packages/stack-encrypt/tests/context_conformance.rs @@ -1,4 +1,12 @@ -//! Shared context composition, typed chains and dynamic lowering. +//! Shared context composition: the data-plan lowering, and the combinators +//! of `stack_encrypt::target` composed by hand. +//! +//! The lowering runs every case of the shared corpora. The combinators run +//! what they can state: a field sealed and indexed under a label, extended +//! one part at a time, the term a query derives under the same context, and +//! the opening under it. They compare no stored context with an expected +//! one and check no record's shape. Those checks are a plan's, so the cases +//! that need them run through the lowering alone. #![cfg(feature = "dynamic")] mod common; @@ -8,12 +16,13 @@ use common::{hex, unhex}; use miette::Diagnostic; use serde::{Deserialize, Serialize}; use stack_encrypt::dynamic::{self, record, ValueFrame}; -use stack_encrypt::plan::pick; use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::target::{DeclaredContext, Encrypted}; +use stack_encrypt::target::{ + equality, indexed, open, Borrowed, CallerContext, DeclaredContext, Decryption, Encrypted, +}; use stack_encrypt::AsProtectedRef as _; use stack_encrypt::{ - CipherText, Descriptor, Equality, IntoAad, IntoPrfContext, Label, NonEmpty, Plan, SealedValue, + CipherText, Descriptor, Equality, IntoAad, IntoPrfContext, Label, NonEmpty, SealedValue, StackCipherText, }; use std::collections::{BTreeMap, BTreeSet}; @@ -66,19 +75,28 @@ fn part(input: &Input) -> ValueFrame { value(input) } } -fn extension(input: &Input) -> DeclaredContext { +/// One part of an extension, as the combinators take it. +fn extension(input: &Input) -> CallerContext { // An empty scalar is a valid extension of an already nonempty label. // CallerContext's constructors enforce nonemptiness on each part, so // the empty-part case is exercised through lowering only. match input.kind.as_str() { - "string" => DeclaredContext::from(NonEmpty::new(input.text.clone()).unwrap()), - "bytes" => DeclaredContext::from(NonEmpty::new(unhex(&input.text)).unwrap()), + "string" => CallerContext::from(NonEmpty::new(input.text.clone()).unwrap()), + "bytes" => CallerContext::from(NonEmpty::new(unhex(&input.text)).unwrap()), "int32" => input.text.parse::<i32>().unwrap().into(), "int64" => input.text.parse::<i64>().unwrap().into(), "uint64" => input.text.parse::<u64>().unwrap().into(), _ => panic!("unsupported context part"), } } +/// The parts of an extension, added one at a time, in order. +fn extended(parts: &[Input]) -> DeclaredContext { + parts + .iter() + .fold(DeclaredContext::default(), |context, part| { + context.with(extension(part)) + }) +} fn nest(head: ValueFrame, parts: &[Input]) -> ValueFrame { parts .iter() @@ -164,9 +182,50 @@ fn stored(case: &Case, vector: &Vector) -> StackCipherText { )); CipherText::Map(fields) } -struct Note { - tenant: String, - text: String, +/// The outputs of the field `text` of one stored row. +fn text_outputs(row: &StackCipherText) -> &[(String, StackCipherText)] { + let CipherText::Map(fields) = row else { + panic!("record") + }; + let (_, CipherText::Map(outputs)) = fields.iter().find(|(k, _)| k == "text").unwrap() else { + panic!("outputs") + }; + outputs +} +/// The equality term a stored row holds for `text`. +fn equality_term(row: &StackCipherText) -> Vec<u8> { + let (_, CipherText::Passthrough(term)) = + text_outputs(row).iter().find(|(k, _)| k == "eq").unwrap() + else { + panic!("term") + }; + vitaminc_aead_frame::serde::from_slice::<&[u8]>(&term.0) + .unwrap() + .to_vec() +} +/// The context a stored row names and its sealed `text`, as the +/// combinators open it: under `<tenant>/text`. +fn opening(row: &StackCipherText) -> Decryption<String, common::CountingSource> { + let CipherText::Map(fields) = row else { + panic!("record") + }; + let (_, CipherText::Map(tenant)) = fields.iter().find(|(k, _)| k == "tenant").unwrap() else { + panic!("tenant") + }; + let (_, CipherText::Passthrough(tenant)) = + tenant.iter().find(|(k, _)| k == "passthrough").unwrap() + else { + panic!("passthrough") + }; + let tenant: String = vitaminc_aead_frame::serde::from_slice(&tenant.0).unwrap(); + let (_, CipherText::Single(leaf)) = text_outputs(row).iter().find(|(k, _)| k == "c").unwrap() + else { + panic!("leaf") + }; + open( + CipherText::Single(SealedValue::from_bytes(&leaf.to_bytes()).unwrap()), + NonEmpty::from(Label::parse(&format!("{tenant}/text")).unwrap()), + ) } #[tokio::test] @@ -301,52 +360,40 @@ async fn shared_context_composition() { .unwrap(); assert_eq!(plain, "hello"); - // Native public chains: repeated calls, not one preassembled extension. + // The combinators, composed by hand: one `with` for each part, not + // one preassembled extension. The label is `<label>/text` for both + // bases. Where a label comes from is the plan's concern. if case.parts.iter().any(|p| p.text.is_empty()) { assert!( NonEmpty::new(String::new()).is_err(), "typed empty extension must refuse" ); } else { - let note = Note { - tenant: case.label.clone(), - text: "hello".into(), - }; - let fields = if case.base == "field" { - Plan::fields().context_field(pick("tenant", |n: &Note| &n.tenant)) - } else { - Plan::context(case.label.as_str()).fields() - }; - let typed = fields - .encrypt_index(pick("text", |n: &Note| &n.text), Equality) - .build() + let label = || NonEmpty::from(Label::parse(&format!("{}/text", case.label)).unwrap()); + let text = String::from("hello"); + let query: EqualityTerm = keyset + .run( + equality::<String, _, Borrowed>().under(label()), + &text, + extended(&case.parts), + ) + .await .unwrap(); - let mut chain = cipher.encrypt(&note).using(&typed); - for p in &case.parts { - chain = chain.extend(extension(p)); - } - // A no-op extension must not clear earlier calls. - let mut row = chain.extend(()).await.unwrap(); - let field_plan = typed.field("text").unwrap(); - let mut query = cipher.query(stack_encrypt::op::Eq, "hello"); - if case.base == "field" { - query = query.context(case.label.as_str()); - } - let mut query = query.on(&field_plan); - for p in &case.parts { - query = query.extend(extension(p)); - } - let query: EqualityTerm = query.extend(()).await.unwrap(); assert_eq!(hex(&query.to_bytes()), expected.equality); - let field: Encrypted<EqualityTerm> = row.take("text").unwrap(); + let field: Encrypted<EqualityTerm> = keyset + .run( + indexed::<String, _, Borrowed, _>(Equality).under(label()), + &text, + extended(&case.parts), + ) + .await + .unwrap(); assert_eq!(hex(&field.terms.to_bytes()), expected.equality); - row.insert("text", field); - let mut open = cipher.open(row).using(&typed); - for p in &case.parts { - open = open.extend(extension(p)); - } - let mut opened = open.extend(()).await.unwrap(); - assert_eq!(opened.take::<String>("text").unwrap(), "hello"); + let opened: String = cipher + .run_decryption(open(field.ciphertext, extended(&case.parts).under(label()))) + .await + .unwrap(); + assert_eq!(opened, "hello"); } vectors.insert(case.id, vector); } @@ -502,71 +549,33 @@ async fn shared_context_boundaries() { } _ => panic!("unknown mutation"), } - let typed_rows = batch - .iter() - .map(|row| { - let CipherText::Map(fields) = row else { - panic!("row") - }; - let mut values = stack_encrypt::plan::FieldValues::new(); - for (name, node) in fields { - let CipherText::Map(outputs) = node else { - panic!("outputs") - }; - for (output, node) in outputs { - match (output.as_str(), node) { - ("passthrough", CipherText::Passthrough(payload)) => { - values.insert( - name, - vitaminc_aead_frame::serde::from_slice::<String>(&payload.0) - .unwrap(), - ); - } - ("c", CipherText::Single(leaf)) => { - values.insert( - name, - StackCipherText::Single( - SealedValue::from_bytes(&leaf.to_bytes()).unwrap(), - ), - ); - } - _ => {} - } - } - } - values - }) - .collect::<Vec<_>>(); - let typed = Plan::fields() - .context_field(pick("tenant", |n: &Note| &n.tenant)) - .encrypt_index(pick("text", |n: &Note| &n.text), Equality) - .build() - .unwrap(); - retrieves.store(0, Ordering::SeqCst); - let mut open = cipher.open(typed_rows); - if !case.expected.is_empty() { - open = open.context(case.expected.as_str()); - } - let typed_result = open.using(&typed).await; - assert_eq!( - typed_result.is_ok(), - case.outcome == "success", - "typed {}", - case.id - ); - if let Ok(opened) = typed_result { - assert_eq!(opened.len(), case.tenants.len()); - for (row, tenant) in opened.iter().zip(&case.tenants) { - assert_eq!(row.get::<String>("text").unwrap(), "hello"); - assert_eq!(row.get::<String>("tenant").unwrap(), tenant); + // The combinators open each row under the context it stores, all + // in one request. They hold no expected context and read no shape, + // so they run the cases with no expectation whose rows all store a + // label. + if case.expected.is_empty() + && ["none", "tamper", "ciphertext"].contains(&case.mutation.as_str()) + { + retrieves.store(0, Ordering::SeqCst); + let opened = cipher + .run_decryption(Decryption::all(batch.iter().map(opening))) + .await; + assert_eq!( + opened.is_ok(), + case.outcome == "success", + "combinators {}", + case.id + ); + if let Ok(opened) = opened { + assert_eq!(opened, vec!["hello"; case.tenants.len()]); } + assert_eq!( + retrieves.load(Ordering::SeqCst), + case.native_retrievals, + "combinators {}", + case.id + ); } - assert_eq!( - retrieves.load(Ordering::SeqCst), - case.native_retrievals, - "typed {}", - case.id - ); generates.store(0, Ordering::SeqCst); retrieves.store(0, Ordering::SeqCst); let expected = if case.expected.is_empty() { @@ -600,34 +609,69 @@ async fn shared_context_boundaries() { ); assert_eq!(generates.load(Ordering::SeqCst), 0); } + // A plan that reads its context from the record has none for a query, + // so a query names it: the plan under that label, with no context + // field, as a binding sends it. assert!(!corpus.queries.is_empty()); for query in corpus.queries { assert!(!query.id.is_empty() && ids.insert(query.id.clone())); + eprintln!("context boundary: {}", query.id); let (cipher, generates, retrieves) = common::counting_cipher().await; - let typed = Plan::fields() - .context_field(pick("tenant", |n: &Note| &n.tenant)) - .encrypt_index(pick("text", |n: &Note| &n.text), Equality) - .build() - .unwrap(); - let note = Note { - tenant: "tenants/acme".into(), - text: "hello".into(), - }; - let mut stored = cipher.encrypt(&note).using(&typed).await.unwrap(); - let field: Encrypted<EqualityTerm> = stored.take("text").unwrap(); + let keyset = cipher.default_keyset(); + let row = object(vec![ + ("tenant", frame("tenants/acme")), + ("text", frame("hello")), + ]); + let stored = record::encrypt(&keyset, row, &plan).unwrap().await.unwrap(); generates.store(0, Ordering::SeqCst); - let field_plan = typed.field("text").unwrap(); - let mut call = cipher.query(stack_encrypt::op::Eq, "hello"); - if !query.context.is_empty() { - call = call.context(query.context.as_str()); - } - let result: Result<EqualityTerm, _> = call.on(&field_plan).await; + let named = if query.context.is_empty() { + Ok(plan.clone()) + } else { + let mut label = query + .context + .split('/') + .map(str::to_owned) + .collect::<Vec<_>>(); + label.push("text".into()); + record::plan( + object(vec![( + "text", + object(vec![ + ("context", frame(label)), + ("outputs", frame(vec!["c", "eq"])), + ("type", frame("string")), + ]), + )]) + .as_protected_ref(), + ) + }; + let pending = named.and_then(|named| { + dynamic::query_term( + &keyset, + &named, + "text", + dynamic::Op::Eq, + "hello", + &dynamic::NoTargets, + ) + }); match query.outcome.as_str() { "refused" => { - assert!(result.is_err()); + assert!(pending.is_err(), "{}", query.id); assert!(!query.matches); } - "success" => assert_eq!(result.unwrap() == field.terms, query.matches), + "success" => { + let term = pending.unwrap().await.unwrap(); + let dynamic::Operand::One(bytes) = term.operand() else { + panic!("one term") + }; + assert_eq!( + bytes == equality_term(&stored).as_slice(), + query.matches, + "{}", + query.id + ); + } _ => panic!("unknown query outcome"), } assert_eq!(generates.load(Ordering::SeqCst), 0); @@ -638,35 +682,33 @@ async fn shared_context_boundaries() { #[tokio::test] async fn a_grouped_extension_remains_one_part_when_appended() { let (cipher, sent) = common::recording_cipher().await; - let plan = Plan::context("users/age") - .with::<u32, _>(Equality) - .build() - .unwrap(); + let keyset = cipher.default_keyset(); + let label = || NonEmpty::from(Label::parse("users/age").unwrap()); let grouped = || NonEmpty::from(7u64).with("eu"); - let out = cipher - .encrypt(&34u32) - .using(&plan) - .extend(grouped()) - .extend(9u64) + let extended = || DeclaredContext::default().with(grouped()).with(9u64); + let out: Encrypted<EqualityTerm> = keyset + .run( + indexed::<u32, _, Borrowed, _>(Equality).under(label()), + &34u32, + extended(), + ) .await .unwrap(); assert_eq!( sent.lock().unwrap().generated(), ["((users/age)/(7u64/eu))/9u64"] ); - let query = cipher - .query(stack_encrypt::op::Eq, &34u32) - .on(&plan) - .extend(grouped()) - .extend(9u64) + let query: EqualityTerm = keyset + .run( + equality::<u32, _, Borrowed>().under(label()), + &34u32, + extended(), + ) .await .unwrap(); assert_eq!(out.terms, query); let back: u32 = cipher - .open(out) - .using(&plan) - .extend(grouped()) - .extend(9u64) + .run_decryption(open(out.ciphertext, extended().under(label()))) .await .unwrap(); assert_eq!(back, 34); @@ -688,37 +730,39 @@ async fn a_grouped_extension_remains_one_part_when_appended() { #[tokio::test] async fn replacement_context_records_keep_their_original_read_and_query_context() { let cipher = common::deterministic_cipher([0; 32]).await; - let plan = Plan::context("users/age") - .with::<u32, _>(Equality) - .build() - .unwrap(); - // The old chain discarded the first extension before sealing. Writing with - // its surviving part exactly reproduces that context without an old API. - let old = cipher - .encrypt(&34u32) - .using(&plan) - .extend(7u64) - .await - .unwrap(); - let old_term = old.terms.clone(); - let current = cipher - .query(stack_encrypt::op::Eq, &34u32) - .on(&plan) - .extend(1u64) - .extend(7u64) + let keyset = cipher.default_keyset(); + let label = || NonEmpty::from(Label::parse("users/age").unwrap()); + let query = |extension: DeclaredContext| { + keyset.run( + equality::<u32, _, Borrowed>().under(label()), + &34u32, + extension, + ) + }; + // The removed chain of an earlier release replaced its extension on each + // `extend`, so only the last part reached the seal. Writing with that + // surviving part reproduces the context of the records it stored. + let old: Encrypted<EqualityTerm> = keyset + .run( + indexed::<u32, _, Borrowed, _>(Equality).under(label()), + &34u32, + DeclaredContext::from(7u64), + ) .await .unwrap(); + let current = query(DeclaredContext::from(1u64).with(7u64)).await.unwrap(); assert_ne!( - current, old_term, + current, old.terms, "the changed query succeeds but silently misses old rows" ); - let compatible = cipher - .query(stack_encrypt::op::Eq, &34u32) - .on(&plan) - .extend(7u64) + let compatible = query(DeclaredContext::from(7u64)).await.unwrap(); + assert_eq!(compatible, old.terms); + let value: u32 = cipher + .run_decryption(open( + old.ciphertext, + DeclaredContext::from(7u64).under(label()), + )) .await .unwrap(); - assert_eq!(compatible, old_term); - let value: u32 = cipher.open(old).using(&plan).extend(7u64).await.unwrap(); assert_eq!(value, 34); } diff --git a/packages/stack-encrypt/tests/contexts.rs b/packages/stack-encrypt/tests/contexts.rs index ee343b0953..61488a15b9 100644 --- a/packages/stack-encrypt/tests/contexts.rs +++ b/packages/stack-encrypt/tests/contexts.rs @@ -1,25 +1,24 @@ //! The contexts a declaration or a call uses, shown before any key is -//! requested (#1568). +//! requested. //! //! Each test states the contexts of one shape of declaration with //! `assert_context!`, and the contexts of a call that runs it with //! `assert_contexts!`. The text the two views print is pinned by the unit -//! tests of `contexts.rs`, since the Go module compares it. The shapes are the -//! context scenarios of the Stack Encrypt API plan that exist on `next`: -//! a static context (scenario 1), a context stored in the row, a nested -//! record, a target that takes the caller's context, a collection, and a -//! plan with each of its three context sources. The scenarios that need the -//! `scope(..)` and `bind(..)` attributes land with #1487. +//! tests of `contexts.rs`, since the Go module compares it. The shapes are: +//! a static context (scenario 1 of the Stack Encrypt API plan), a context +//! stored in the row, a nested record, a target that takes the caller's +//! context, a collection, and a plan that is data, with its own context and +//! with a context read from a field of each record. The scenarios with a +//! `scope(..)` or a `bind(..)` attribute are in `scopes_and_bindings.rs`. #![cfg(feature = "testing")] use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::plan::{Field, Fields, Operation as _}; use stack_encrypt::sem::EqualityTerm; use stack_encrypt::target::{DecryptSource, EncryptTarget, Encryption, RequiredContext}; -use stack_encrypt::testing::{caller_scope, from_plaintext, required, Expect}; +use stack_encrypt::testing::required; use stack_encrypt::{ assert_context, assert_contexts, nonempty, ContextTree, DecryptFrom, EncryptInto, Encrypted, - Equality, IntoContext as _, LeafContext, NonEmpty, Ore, Plan, StackCipher, StackCipherText, + Equality, IntoContext as _, LeafContext, NonEmpty, StackCipher, StackCipherText, }; use zeroize::Zeroize; @@ -273,116 +272,131 @@ mod a_collection { } } -struct User { - email: String, - age: u32, - id: u64, - tenant: String, -} -impl Fields for User { - fn field_names(&self) -> Vec<&str> { - vec!["email", "age", "id", "tenant"] - } -} -impl Field<String> for User { - fn field(&self, name: &str) -> Option<&String> { - match name { - "email" => Some(&self.email), - "tenant" => Some(&self.tenant), - _ => None, - } - } -} -impl Field<u32> for User { - fn field(&self, name: &str) -> Option<&u32> { - (name == "age").then_some(&self.age) +/// A plan that is data: a record declaration a binding sends. A plan names +/// its own context, or reads it from a field of each record. The caller of +/// a run may add a scope and a binding. +#[cfg(feature = "dynamic")] +mod a_data_plan { + use super::*; + use stack_encrypt::dynamic::record::{self, FieldPlan, Output, Plan as DataPlan}; + use stack_encrypt::dynamic::{Planned, ValueFrame, ValueKind}; + use stack_encrypt::target::IndexSpec; + use stack_encrypt::testing::{caller_scope, from_plaintext, Expect}; + use stack_encrypt::{AsProtectedRef as _, Describe as _, Label, Placeholder}; + use vitaminc_aead_frame::serde::to_vec; + + /// A data plan read from its JSON declaration. + fn plan_of(declaration: serde_json::Value) -> DataPlan { + record::plan(to_vec(&declaration).unwrap().as_protected_ref()).unwrap() } -} -impl Field<u64> for User { - fn field(&self, name: &str) -> Option<&u64> { - (name == "id").then_some(&self.id) + + /// One record, as a binding sends it. + fn ada() -> ValueFrame { + let record = serde_json::json!({ + "email": "ada@example.com", + "age": 36, + "id": 7, + "tenant": "acme", + }); + ValueFrame::from_bytes(to_vec(&record).unwrap().as_protected_ref()).unwrap() } -} -mod a_plan { - use super::*; + /// The binding the caller of a run may add: `{bind?: CallerContext}`. + fn caller_bind() -> Placeholder { + Placeholder::optional("bind", "CallerContext").as_bind() + } #[test] - fn with_its_own_context_shows_the_extension_a_run_may_add() { - let plan: Plan<User, FakeDataKeySource> = Plan::context("users") - .fields() - .encrypt_index::<String>("email", Equality) - .encrypt_index::<u32>("age", (Equality, Ore)) - .passthrough::<u64>("id") - .build() - .unwrap(); - assert_context!( - plan.contexts(), - email = [["users", "email"], caller_scope()], - age = [["users", "age"], caller_scope()], - ); + fn shows_each_field_under_its_own_context() { + let label = NonEmpty::new(Label::parse("users/email").unwrap().to_context()).unwrap(); + let email = FieldPlan::new( + "email", + label, + vec![Output::Ciphertext, Output::Term(IndexSpec::Equality)], + ) + .unwrap() + .with_type(ValueKind::String) + .unwrap(); + let plan = DataPlan::new(vec![email]).unwrap(); + assert_context!(plan.contexts().unwrap(), email = ["users", "email"]); } #[test] - fn built_without_a_context_shows_the_label_the_call_names() { - let plan: Plan<User, FakeDataKeySource> = Plan::fields() - .encrypt_index::<String>("email", Equality) - .build() - .unwrap(); + fn with_its_own_context_shows_the_parts_a_run_may_add() { + let plan = plan_of(serde_json::json!({ + "email": { "context": ["users", "email"], "outputs": ["c", "eq"], "type": "string" }, + "age": { "context": ["users", "age"], "outputs": ["c", "eq", "ore"], "type": "uint32" }, + "id": { "context": ["users", "id"], "outputs": ["passthrough"] }, + "tenant": { "context": ["users", "tenant"], "outputs": ["passthrough"] }, + })); assert_context!( - plan.contexts(), - email = [ - [ - Expect::from(required::<stack_encrypt::Label>()), - Expect::from("email"), + Planned::new(&plan, ada()).contexts().unwrap(), + email = { + query = [["users", "email"], caller_scope()], + stored = [ + [ + Expect::from(["users", "email"]), + Expect::from(caller_scope()), + ], + caller_bind(), ], - caller_scope(), - ], + }, + age = { + query = [["users", "age"], caller_scope()], + stored = [ + [ + Expect::from(["users", "age"]), + Expect::from(caller_scope()), + ], + caller_bind(), + ], + }, ); } #[test] fn with_a_context_field_shows_where_the_context_is_read_from() { - let plan: Plan<User, FakeDataKeySource> = Plan::fields() - .context_field::<String>("tenant") - .encrypt_index::<String>("email", Equality) - .build() - .unwrap(); + let plan = plan_of(serde_json::json!({ + "context_field": "tenant", + "tenant": { "context": ["tenant"], "outputs": ["passthrough"] }, + "email": { "context": ["email"], "outputs": ["c", "eq"], "type": "string" }, + })); assert_context!( - plan.contexts(), - email = [ - [ - Expect::from(from_plaintext::<stack_encrypt::Label>("tenant")), - Expect::from("email"), + Planned::new(&plan, ada()).contexts().unwrap(), + email = { + query = [ + [ + Expect::from(from_plaintext::<Label>("tenant")), + Expect::from("email"), + ], + caller_scope(), ], - caller_scope(), - ], + stored = [ + [ + Expect::from([ + Expect::from(from_plaintext::<Label>("tenant")), + Expect::from("email"), + ]), + Expect::from(caller_scope()), + ], + caller_bind(), + ], + }, ); } #[tokio::test] - async fn a_run_fills_the_extension_in() { + async fn a_run_fills_the_callers_scope_in() { let cipher = cipher().await; - let plan: Plan<User, FakeDataKeySource> = Plan::context("users") - .fields() - .encrypt_index::<String>("email", Equality) - .passthrough::<u32>("age") - .passthrough::<u64>("id") - .passthrough::<String>("tenant") - .build() - .unwrap(); - let user = User { - email: "ada@example.com".into(), - age: 36, - id: 7, - tenant: "acme".into(), - }; + let plan = plan_of(serde_json::json!({ + "email": { "context": ["users", "email"], "outputs": ["c", "eq"], "type": "string" }, + "age": { "context": ["users", "age"], "outputs": ["passthrough"] }, + "id": { "context": ["users", "id"], "outputs": ["passthrough"] }, + "tenant": { "context": ["users", "tenant"], "outputs": ["passthrough"] }, + })); let keyset = cipher.default_keyset(); - let call = cipher - .encrypt(&user) - .using(&plan) - .extend(nonempty!("acme")) - .prepare(&keyset, false); + let call = Planned::new(&plan, ada()) + .encrypt_into_with_context::<StackCipherText, _>(&keyset, nonempty!("acme")); let shown = call.context(); let record = call.await; assert!(record.is_ok(), "{record:?}"); @@ -390,30 +404,6 @@ mod a_plan { } } -#[cfg(feature = "dynamic")] -mod a_data_plan { - use super::*; - use stack_encrypt::dynamic::record::{FieldPlan, Output, Plan as DataPlan}; - use stack_encrypt::dynamic::ValueKind; - use stack_encrypt::target::IndexSpec; - use stack_encrypt::{Describe as _, Label}; - - #[test] - fn shows_the_same_contexts_as_the_typed_plan_it_lowers_to() { - let label = NonEmpty::new(Label::parse("users/email").unwrap().to_context()).unwrap(); - let email = FieldPlan::new( - "email", - label, - vec![Output::Ciphertext, Output::Term(IndexSpec::Equality)], - ) - .unwrap() - .with_type(ValueKind::String) - .unwrap(); - let plan = DataPlan::new(vec![email]).unwrap(); - assert_context!(plan.contexts().unwrap(), email = ["users", "email"]); - } -} - /// A target extended with `extend` shows its own context, then the caller's /// required one, as the call does. mod a_target_that_extends_its_own_context { @@ -538,8 +528,8 @@ async fn a_failed_call_says_it_failed() { /// does. mod a_json_document { use super::*; - use stack_encrypt::json::JsonDocument; - use stack_encrypt::target::{CallerContext, DecryptInto, EncryptFrom}; + use stack_encrypt::json::{JsonDocument, JsonPlaintext}; + use stack_encrypt::target::CallerContext; #[tokio::test] async fn shows_its_context_when_sealed_and_when_opened() { @@ -547,22 +537,14 @@ mod a_json_document { let keyset = cipher.default_keyset(); let value = serde_json::json!({"name": "ada", "age": 36}); let column = CallerContext::from(nonempty!("users").with("profile")); - let sealing = keyset.run( - <JsonDocument as EncryptFrom<serde_json::Value>>::encryption::<FakeDataKeySource>(), - &value, - column.clone(), - ); + let sealing = JsonPlaintext::from(value) + .encrypt_into_with_context::<JsonDocument, _>(&keyset, column.clone()); assert_context!(sealing.context(), ["users", "profile"]); let document = sealing.await.unwrap(); - let opening = keyset.run_decryption( - <JsonDocument as DecryptInto<serde_json::Value>>::decryption::<FakeDataKeySource>( - document, - column.into(), - ), - ); + let opening = JsonPlaintext::decrypt_from_with_context(document, &keyset, column); assert_context!(opening.context(), ["users", "profile"]); - let opened: serde_json::Value = opening.await.unwrap(); - assert_eq!(opened["name"], "ada"); + let opened = opening.await.unwrap(); + assert_eq!(opened.value()["name"], "ada"); } } diff --git a/packages/stack-encrypt/tests/derive.rs b/packages/stack-encrypt/tests/derive.rs index 6ae81e228a..54370a5671 100644 --- a/packages/stack-encrypt/tests/derive.rs +++ b/packages/stack-encrypt/tests/derive.rs @@ -1,8 +1,12 @@ -//! `#[derive(EncryptFrom)]` / `#[derive(DecryptInto)]`: the derived impls are the -//! hand-written composite in `target.rs`, emitted — same terms, same decrypt -//! mirror, same one-batched-call settlement — plus what only a derive makes -//! cheap: sources listed or left generic, structs encrypted field by field, -//! and fields that are not derived at all. +//! `#[derive(EncryptTarget)]` / `#[derive(DecryptSource)]`: a derived record +//! writes what its leaves write on their own. It has the same terms, it +//! decrypts through its one ciphertext, and it settles in one batched key +//! request. The tests also cover what only a derive makes cheap: sources +//! listed or left generic, structs encrypted field by field, and fields that +//! are not derived at all. +//! +//! The plaintext is passed by value (`value.encrypt_into(&keyset)`), so each +//! test clones or rebuilds the value it compares against. mod common; @@ -11,17 +15,21 @@ use std::sync::atomic::Ordering as AtomicOrdering; use common::{counting_cipher, recording_cipher, stack_cipher}; use stack_encrypt::sem::Orderable; use stack_encrypt::sem::{EqualityTerm, MatchConfig, MatchOptions, MatchTerms, OreTerm, TermError}; -use stack_encrypt::target::{AeadContext, Encrypted}; +use stack_encrypt::target::{ + AeadContext, CallerContext, DecryptSource, EncryptTarget, Encrypted, Encryption, + RequiredContext, Scope, +}; use stack_encrypt::{ - nonempty, ContextPiece, DecryptField, DecryptInto, Decryptable, EncryptFrom, Error, - IntoContext, MaybeEmpty, NonEmpty, StackCipherText, + nonempty, ContextPiece, DecryptField, DecryptFrom, Decryptable, EncryptInto, Error, + IntoContext, Label, MaybeEmpty, NonEmpty, StackCipherText, }; +use zeroize::Zeroize; // --- Records: every field from one plaintext, under one context ------------- /// The hand-written record in `target.rs`, derived: an encrypted `u32` /// stored as its ciphertext plus an equality term and an ORE term. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32)] struct EncryptedAge { c: StackCipherText, @@ -36,27 +44,26 @@ async fn a_derived_record_is_the_hand_written_one() { let generator = stack_cipher().await; let generator = generator.default_keyset(); - let record: EncryptedAge = keyset - .encrypt_as(&42u32, nonempty!("users/age").into()) + let record: EncryptedAge = 42u32 + .encrypt_into_with_context(&keyset, nonempty!("users/age")) .await .unwrap(); // Each term is what the leaf derives on its own, so query terms built // leaf-by-leaf find records encrypted as composites. - let hm: EqualityTerm = generator - .encrypt_as(&42u32, nonempty!("users/age").into()) + let hm: EqualityTerm = 42u32 + .encrypt_into_with_context(&generator, nonempty!("users/age")) .await .unwrap(); - let ob: OreTerm<u32> = generator - .encrypt_as(&42u32, nonempty!("users/age").into()) + let ob: OreTerm<u32> = 42u32 + .encrypt_into_with_context(&generator, nonempty!("users/age")) .await .unwrap(); assert_eq!(record.hm, hm); assert_eq!(record.ob, ob); - // And the decrypt mirror opens the ciphertext field. - let age: u32 = cipher - .decrypt_as(record, nonempty!("users/age").into()) + // And decryption opens the ciphertext field. + let age = u32::decrypt_from_with_context(record, &cipher, nonempty!("users/age")) .await .unwrap(); assert_eq!(age, 42); @@ -65,7 +72,7 @@ async fn a_derived_record_is_the_hand_written_one() { /// No `plaintext`: one impl generic over it, accepting whatever every leaf /// accepts — here any text type, since `MatchTerms` wants `AsRef<str>` — and /// decrypting to whatever the ciphertext field opens to. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] struct SearchableText { c: StackCipherText, hm: EqualityTerm, @@ -73,15 +80,16 @@ struct SearchableText { } /// Tuple structs assign by index. -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] struct Pair(StackCipherText, EqualityTerm); -/// A record may declare `'__k` itself; the declaration API no longer adds -/// a keyset lifetime. Compiling is the test. -#[derive(EncryptFrom)] +/// A record may declare `'__k` itself: the declaration adds no keyset +/// lifetime. A target is `'static`, so a lifetime the record declares must +/// be too. Compiling is the test. +#[derive(EncryptTarget)] #[stash(plaintext = u32)] #[allow(dead_code)] -struct Borrowed<'__k> { +struct Borrowed<'__k: 'static> { c: StackCipherText, #[stash(default)] label: Option<&'__k str>, @@ -89,10 +97,10 @@ struct Borrowed<'__k> { /// The declaration's source lifetime steps aside for the record's own, /// including when the first fallback name is also taken. -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(plaintext = u32)] #[allow(dead_code)] -struct BorrowedSource<'__source, '__source_> { +struct BorrowedSource<'__source: 'static, '__source_: 'static> { c: StackCipherText, #[stash(default)] label: Option<&'__source str>, @@ -104,7 +112,7 @@ struct BorrowedSource<'__source, '__source_> { /// where clause makes `Tagged<T>` accept exactly `T` — the ORE term is typed /// by its source. A generic record's one-ciphertext check runs when the /// record is first used rather than where it is defined. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] struct Tagged<T: Orderable> { c: StackCipherText, ob: OreTerm<T>, @@ -117,52 +125,54 @@ async fn a_generic_plaintext_record_accepts_what_its_leaves_accept() { let generator = stack_cipher().await; let generator = generator.default_keyset(); - let record: SearchableText = keyset - .encrypt_as(&"alice".to_string(), nonempty!("users/name").into()) + let record: SearchableText = "alice" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/name")) .await .unwrap(); - let hm: EqualityTerm = generator - .encrypt_as(&"alice".to_string(), nonempty!("users/name").into()) + let hm: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/name")) .await .unwrap(); - let m: MatchTerms = generator - .encrypt_as(&"alice".to_string(), nonempty!("users/name").into()) + let m: MatchTerms = "alice" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/name")) .await .unwrap(); assert_eq!(record.hm, hm); assert_eq!(record.m, m); - let name: String = cipher - .decrypt_as(record, nonempty!("users/name").into()) + let name = String::decrypt_from_with_context(record, &cipher, nonempty!("users/name")) .await .unwrap(); assert_eq!(name, "alice"); - let pair: Pair = keyset - .encrypt_as(&"bob", nonempty!("users/name").into()) + let pair: Pair = "bob" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/name")) .await .unwrap(); - let hm: EqualityTerm = generator - .encrypt_as(&"bob", nonempty!("users/name").into()) + let hm: EqualityTerm = "bob" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/name")) .await .unwrap(); assert_eq!(pair.1, hm); - let name: String = cipher - .decrypt_as(pair.0, nonempty!("users/name").into()) + let name = String::decrypt_from_with_context(pair.0, &cipher, nonempty!("users/name")) .await .unwrap(); assert_eq!(name, "bob"); - let tagged: Tagged<u32> = keyset - .encrypt_as(&7u32, nonempty!("users/score").into()) + let tagged: Tagged<u32> = 7u32 + .encrypt_into_with_context(&keyset, nonempty!("users/score")) .await .unwrap(); - let ob: OreTerm<u32> = generator - .encrypt_as(&7u32, nonempty!("users/score").into()) + let ob: OreTerm<u32> = 7u32 + .encrypt_into_with_context(&generator, nonempty!("users/score")) .await .unwrap(); assert_eq!(tagged.ob, ob); - let score: u32 = cipher - .decrypt_as(tagged, nonempty!("users/score").into()) + let score = u32::decrypt_from_with_context(tagged, &cipher, nonempty!("users/score")) .await .unwrap(); assert_eq!(score, 7); @@ -172,7 +182,7 @@ async fn a_generic_plaintext_record_accepts_what_its_leaves_accept() { /// output of a `plaintext` record shares it): the type system cannot pick, /// so `#[stash(decrypt)]` does. Only marked fields are considered, and the /// others need not be `Decryptable` at all. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32)] struct Doubled { #[stash(decrypt)] @@ -182,7 +192,7 @@ struct Doubled { /// Fields that are collections or optional follow their content: a record /// of a `Vec<u32>` has one decryptable field, its `Vec<StackCipherText>`. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = Vec<u32>)] struct Numbers { c: Vec<StackCipherText>, @@ -194,61 +204,59 @@ async fn decrypt_marks_the_field_when_the_types_cannot_choose() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let doubled: Doubled = keyset - .encrypt_as(&9u32, nonempty!("doubled").into()) + let doubled: Doubled = 9u32 + .encrypt_into_with_context(&keyset, nonempty!("doubled")) .await .unwrap(); - let opened: u32 = cipher - .decrypt_as(doubled, nonempty!("doubled").into()) + let opened = u32::decrypt_from_with_context(doubled, &cipher, nonempty!("doubled")) .await .unwrap(); assert_eq!(opened, 9); // The unmarked ciphertext is still a ciphertext, just not the record's: // a second, independent seal of the same value under the same context. - let doubled: Doubled = keyset - .encrypt_as(&9u32, nonempty!("doubled").into()) + let doubled: Doubled = 9u32 + .encrypt_into_with_context(&keyset, nonempty!("doubled")) .await .unwrap(); - let shadow: u32 = cipher - .decrypt_as(doubled.shadow, nonempty!("doubled").into()) + let shadow = u32::decrypt_from_with_context(doubled.shadow, &cipher, nonempty!("doubled")) .await .unwrap(); assert_eq!(shadow, 9); - let numbers: Numbers = keyset - .encrypt_as(&vec![1u32, 2, 3], nonempty!("numbers").into()) + let numbers: Numbers = vec![1u32, 2, 3] + .encrypt_into_with_context(&keyset, nonempty!("numbers")) .await .unwrap(); assert_eq!(numbers.hm.len(), 3); - let opened: Vec<u32> = cipher - .decrypt_as(numbers, nonempty!("numbers").into()) + let opened = Vec::<u32>::decrypt_from_with_context(numbers, &cipher, nonempty!("numbers")) .await .unwrap(); assert_eq!(opened, vec![1, 2, 3]); } -/// An index term type from outside this crate that predates `Decryptable`: -/// it implements `EncryptFrom` only, wrapping a term of ours. +/// An index term type from outside this crate that does not say whether it +/// is recoverable: it implements `EncryptTarget` only, wrapping a term of +/// ours. #[derive(PartialEq)] struct OpaqueTerm(EqualityTerm); -impl<S> EncryptFrom<S> for OpaqueTerm +impl<S: Zeroize> EncryptTarget<S> for OpaqueTerm where - EqualityTerm: EncryptFrom<S>, + EqualityTerm: EncryptTarget<S, ContextInput = RequiredContext<CallerContext>>, { - type Context = <EqualityTerm as EncryptFrom<S>>::Context; - fn encryption<'s, K: 'static>() -> stack_encrypt::Encryption<'s, S, Self, K, Self::Context> + type ContextInput = RequiredContext<CallerContext>; + fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput> where S: 's, { - <EqualityTerm as EncryptFrom<S>>::encryption().map(OpaqueTerm) + <EqualityTerm as EncryptTarget<S>>::encryption().map(OpaqueTerm) } } /// The documented explicit-mode shape: `#[stash(decrypt)]` frees the *other* /// field types from `Decryptable`, so the paired derive must compile with an -/// opaque field — including the `Decryptable` impl `EncryptFrom` emits. -#[derive(EncryptFrom, DecryptInto)] +/// opaque field — including the `Decryptable` impl `EncryptTarget` emits. +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32)] struct WithOpaque { #[stash(decrypt)] @@ -267,25 +275,24 @@ async fn explicit_mode_supports_opaque_fields_in_the_paired_derive() { let generator = stack_cipher().await; let generator = generator.default_keyset(); - let record: WithOpaque = keyset - .encrypt_as(&5u32, nonempty!("opaque").into()) + let record: WithOpaque = 5u32 + .encrypt_into_with_context(&keyset, nonempty!("opaque")) .await .unwrap(); - let hm: EqualityTerm = generator - .encrypt_as(&5u32, nonempty!("opaque").into()) + let hm: EqualityTerm = 5u32 + .encrypt_into_with_context(&generator, nonempty!("opaque")) .await .unwrap(); assert!(record.o == OpaqueTerm(hm)); - let opened: u32 = cipher - .decrypt_as(record, nonempty!("opaque").into()) + let opened = u32::decrypt_from_with_context(record, &cipher, nonempty!("opaque")) .await .unwrap(); assert_eq!(opened, 5); } /// A third-party field type that breaks the `DecryptField` contract: -/// `DECRYPTABLE` says decryption opens it, but `decrypt_field` passes it +/// `DECRYPTABLE` says decryption opens it, but `decryption_field` passes it /// over anyway. struct Lying; @@ -302,17 +309,17 @@ impl<P, Ctx> DecryptField<P, Ctx> for Lying { } } -#[derive(DecryptInto)] +#[derive(DecryptSource)] struct LyingRecord { l: Lying, } -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Zeroize)] struct Held { value: u32, } -#[derive(DecryptInto)] +#[derive(DecryptSource)] #[stash(struct = Held, scope = "held")] struct LyingHeld { value: Lying, @@ -325,19 +332,16 @@ async fn a_broken_decrypt_field_contract_is_not_opened_never_a_panic() { // The compile-time check accepted `Lying` (its `DECRYPTABLE` is `true`), // so the broken contract only shows at decrypt time: `Error::NotOpened` // as a failed pending, for the record and for the row alike. - let result: Result<u32, _> = cipher - .decrypt_as(LyingRecord { l: Lying }, nonempty!("l").into()) - .await; + let result = + u32::decrypt_from_with_context(LyingRecord { l: Lying }, &cipher, nonempty!("l")).await; assert!(matches!(result, Err(Error::NotOpened))); - let result: Result<Held, _> = cipher - .decrypt_as(LyingHeld { value: Lying }, ().into()) - .await; + let result = Held::decrypt_from(LyingHeld { value: Lying }, &cipher).await; assert!(matches!(result, Err(Error::NotOpened))); } /// Listed plaintexts: one impl each, and nothing else is accepted. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32, plaintext = String)] struct EncryptedValue { c: StackCipherText, @@ -349,26 +353,25 @@ async fn listed_plaintexts_each_get_their_own_impl() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let number: EncryptedValue = keyset - .encrypt_as(&7u32, nonempty!("t/n").into()) + let number: EncryptedValue = 7u32 + .encrypt_into_with_context(&keyset, nonempty!("t/n")) .await .unwrap(); - let text: EncryptedValue = keyset - .encrypt_as(&"seven".to_string(), nonempty!("t/t").into()) + let text: EncryptedValue = "seven" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("t/t")) .await .unwrap(); - let hm: EqualityTerm = keyset - .encrypt_as(&7u32, nonempty!("t/n").into()) + let hm: EqualityTerm = 7u32 + .encrypt_into_with_context(&keyset, nonempty!("t/n")) .await .unwrap(); assert_eq!(number.hm, hm); - let number: u32 = cipher - .decrypt_as(number, nonempty!("t/n").into()) + let number = u32::decrypt_from_with_context(number, &cipher, nonempty!("t/n")) .await .unwrap(); - let text: String = cipher - .decrypt_as(text, nonempty!("t/t").into()) + let text = String::decrypt_from_with_context(text, &cipher, nonempty!("t/t")) .await .unwrap(); assert_eq!((number, text.as_str()), (7, "seven")); @@ -393,7 +396,7 @@ impl<'a> IntoContext<'a> for Tenant { /// Ciphertext only, so it declares the ciphertext operation's own context /// type rather than the default `CallerContext`, which would demand a PRF /// encoding no field here uses. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String, context_type = AeadContext)] struct SealedName { c: StackCipherText, @@ -411,21 +414,28 @@ async fn a_ciphertext_only_record_accepts_an_aead_only_context_like_the_leaf_doe // The derived record and the canonical leaf accept the same context // and produce interchangeable ciphertext: each opens the other's. - let record: SealedName = keyset.encrypt_as(&name, tenant().into()).await.unwrap(); + let record: SealedName = name + .clone() + .encrypt_into_with_context(&keyset, tenant()) + .await + .unwrap(); let opened: String = cipher.decrypt(record.c, tenant()).await.unwrap(); assert_eq!(opened, name); let leaf = keyset.encrypt_tree(name.clone(), tenant()).await.unwrap(); - let opened: String = cipher - .decrypt_as(SealedName { c: leaf }, tenant().into()) + let opened = String::decrypt_from_with_context(SealedName { c: leaf }, &cipher, tenant()) .await .unwrap(); assert_eq!(opened, name); // Bound to the context like any other leaf. - let record: SealedName = keyset.encrypt_as(&name, tenant().into()).await.unwrap(); + let record: SealedName = name + .clone() + .encrypt_into_with_context(&keyset, tenant()) + .await + .unwrap(); let other = NonEmpty::new(Tenant("other".into())).unwrap(); - let result: Result<String, _> = cipher.decrypt_as(record, other.into()).await; + let result = String::decrypt_from_with_context(record, &cipher, other).await; assert!(matches!(result, Err(Error::Aead)), "{result:?}"); } @@ -441,7 +451,7 @@ impl MatchConfig for TooManyHashes { } } -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] struct BadlyIndexedText { c: StackCipherText, m: MatchTerms<TooManyHashes>, @@ -455,8 +465,9 @@ async fn a_failed_field_fails_the_derived_record_before_any_io() { // Invalid match options fail that leaf during the synchronous build; // the derived record is the zip of its fields, so it fails the same way // and never mints the data key its ciphertext field would have wanted. - let result: Result<BadlyIndexedText, _> = keyset - .encrypt_as(&"alice".to_string(), nonempty!("users/name").into()) + let result: Result<BadlyIndexedText, _> = "alice" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/name")) .await; assert!(matches!( result, @@ -466,8 +477,8 @@ async fn a_failed_field_fails_the_derived_record_before_any_io() { // Text with no match tokens is not a failure: the leaf is the empty // filter, and the record seals as any other. - let stored: SearchableText = keyset - .encrypt_as(&String::new(), nonempty!("users/name").into()) + let stored: SearchableText = String::new() + .encrypt_into_with_context(&keyset, nonempty!("users/name")) .await .unwrap(); assert!(stored.m.positions().is_empty()); @@ -476,9 +487,10 @@ async fn a_failed_field_fails_the_derived_record_before_any_io() { // --- Structs: each field from one field of the plaintext, under its own context -// `Zeroize` is required: a column of these decrypts through the wiping -// collection path. That path wipes the rows recovered before a late failure. -#[derive(Debug, Clone, PartialEq, Eq, zeroize::Zeroize)] +// `Zeroize` is required: the conversion takes the struct by value and wipes +// each field it seals, and a column of these decrypts through the wiping +// collection path, which wipes the rows recovered before a late failure. +#[derive(Debug, Clone, PartialEq, Eq, Zeroize)] struct User { age: u32, email: String, @@ -490,7 +502,7 @@ struct User { /// prefix names the stored data, explicitly: it is part of its identity, so /// it is never inferred from the type's name. `from` is the override for a /// field name that differs; the context then follows the plaintext field. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(struct = User, scope = "user")] struct EncryptedUser { /// A record inside a struct: recursion, not a second mechanism. @@ -519,10 +531,7 @@ async fn a_struct_is_one_batched_call_and_rebuilds_its_plaintext() { // Every field has its own context, so the struct needs none from the // caller: the context-free forms are the whole call, both ways. - let row: EncryptedUser = keyset - .encrypt_as(&user(), Default::default()) - .await - .unwrap(); + let row: EncryptedUser = user().encrypt_into(&keyset).await.unwrap(); assert_eq!( generates.load(AtomicOrdering::SeqCst), 1, @@ -532,26 +541,32 @@ async fn a_struct_is_one_batched_call_and_rebuilds_its_plaintext() { // Each field's terms are what a query site derives under the field's // inferred context: the prefix and the plaintext field's name. - let age_hm: EqualityTerm = generator - .encrypt_as(&42u32, nonempty!("user").with("age").into()) + let age_hm: EqualityTerm = 42u32 + .encrypt_into_with_context(&generator, nonempty!("user").with("age")) .await .unwrap(); assert_eq!(row.age.hm, age_hm); - let email_hm: EqualityTerm = generator - .encrypt_as(&user().email, nonempty!("user").with("email").into()) + let email_hm: EqualityTerm = user() + .email + .encrypt_into_with_context(&generator, nonempty!("user").with("email")) .await .unwrap(); - let email_match: MatchTerms = generator - .encrypt_as(&user().email, nonempty!("user").with("email").into()) + let email_match: MatchTerms = user() + .email + .encrypt_into_with_context(&generator, nonempty!("user").with("email")) .await .unwrap(); assert_eq!(row.email.terms, (email_hm, email_match)); - - // Decryption rebuilds the plaintext field by field: one batched call. - let recovered = cipher - .decrypt_as::<User, _>(row, Default::default()) + // The field's marker is that query site: it derives the stored term. + let by_marker = EncryptedUser::EMAIL + .eq(user().email) + .encrypt(&generator) .await .unwrap(); + assert_eq!(by_marker, row.email.terms.0); + + // Decryption rebuilds the plaintext field by field: one batched call. + let recovered = User::decrypt_from(row, &cipher).await.unwrap(); assert_eq!(recovered, user()); assert_eq!( retrieves.load(AtomicOrdering::SeqCst), @@ -572,18 +587,25 @@ async fn a_column_of_structs_is_still_one_call_each_way() { }) .collect(); - let rows: Vec<EncryptedUser> = keyset.encrypt_as(&users, Default::default()).await.unwrap(); + let rows: Vec<EncryptedUser> = users.clone().encrypt_into(&keyset).await.unwrap(); assert_eq!(rows.len(), 4); assert_eq!(generates.load(AtomicOrdering::SeqCst), 1); - let recovered = cipher - .decrypt_as::<Vec<User>, _>(rows, Default::default()) - .await - .unwrap(); + let recovered = Vec::<User>::decrypt_from(rows, &cipher).await.unwrap(); assert_eq!(recovered, users); assert_eq!(retrieves.load(AtomicOrdering::SeqCst), 1); } +/// The same struct, with a part the caller supplies on every call. A +/// `struct` record takes no context it does not declare, so the row's id is +/// declared as a scope. +#[derive(EncryptTarget, DecryptSource)] +#[stash(struct = User, scope = "user", scope(id: u64))] +struct EncryptedUserOfRow { + age: EncryptedAge, + email: Encrypted<(EqualityTerm, MatchTerms)>, +} + #[tokio::test] async fn a_struct_extends_its_contexts_with_the_callers() { let (cipher, generates, retrieves) = counting_cipher().await; @@ -591,50 +613,41 @@ async fn a_struct_extends_its_contexts_with_the_callers() { let generator = stack_cipher().await; let generator = generator.default_keyset(); - // The caller's context — the record's id — extends every inferred one: + // The caller's scope — the record's id — extends every inferred context: // `age` is derived under `("user/age", 7u64)`, still in one batched // call, and a query site probes it under the same pair. - let row: EncryptedUser = keyset.encrypt_as(&user(), 7u64.into()).await.unwrap(); + let row: EncryptedUserOfRow = user() + .encrypt_into_with_context(&keyset, Scope::new(7u64)) + .await + .unwrap(); assert_eq!(generates.load(AtomicOrdering::SeqCst), 1); - let age_hm: EqualityTerm = generator - .encrypt_as(&42u32, nonempty!("user").with("age").with(7u64).into()) + let age_hm: EqualityTerm = 42u32 + .encrypt_into_with_context(&generator, nonempty!("user").with("age").with(7u64)) .await .unwrap(); assert_eq!(row.age.hm, age_hm); - let unextended: EqualityTerm = generator - .encrypt_as(&42u32, nonempty!("user").with("age").into()) + let unextended: EqualityTerm = 42u32 + .encrypt_into_with_context(&generator, nonempty!("user").with("age")) .await .unwrap(); assert_ne!(row.age.hm, unextended); - // Opens under the same extension, in one call — and under no other. - let recovered = cipher - .decrypt_as::<User, _>(row, 7u64.into()) + // Opens under the same scope, in one call — and under no other. + let recovered = User::decrypt_from_with_context(row, &cipher, Scope::new(7u64)) .await .unwrap(); assert_eq!(recovered, user()); assert_eq!(retrieves.load(AtomicOrdering::SeqCst), 1); - let row: EncryptedUser = keyset.encrypt_as(&user(), 7u64.into()).await.unwrap(); + let row: EncryptedUserOfRow = user() + .encrypt_into_with_context(&keyset, Scope::new(7u64)) + .await + .unwrap(); // The fake key source ignores descriptors, so the AEAD is what refuses // a wrong context here. ZeroKMS refuses the key retrieval itself first - // (`Error::Kms`) — `examples/encrypted_record.rs` shows that live. - let other_row = cipher.decrypt_as::<User, _>(row, 8u64.into()).await; + // (`Error::Kms`). + let other_row = User::decrypt_from_with_context(row, &cipher, Scope::new(8u64)).await; assert!(matches!(other_row, Err(Error::Aead))); - let row: EncryptedUser = keyset.encrypt_as(&user(), 7u64.into()).await.unwrap(); - let no_row = cipher.decrypt_as::<User, _>(row, Default::default()).await; - assert!(matches!(no_row, Err(Error::Aead))); - - // Any context does: a string, a pair, an `Option`. - let row: EncryptedUser = keyset - .encrypt_as(&user(), nonempty!("tenant/acme").into()) - .await - .unwrap(); - let recovered = cipher - .decrypt_as::<User, _>(row, nonempty!("tenant/acme").into()) - .await - .unwrap(); - assert_eq!(recovered, user()); } #[tokio::test] @@ -642,21 +655,17 @@ async fn a_struct_field_opened_under_the_wrong_context_fails() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let row: EncryptedUser = keyset - .encrypt_as(&user(), Default::default()) - .await - .unwrap(); + let row: EncryptedUser = user().encrypt_into(&keyset).await.unwrap(); // The literal contexts are baked into the impl, so a transplanted field // is caught exactly as for a leaf: by the AAD against the fake key // source, by ZeroKMS's descriptor check (`Error::Kms`) before that in // production. - let transplanted: Result<u32, _> = cipher - .decrypt_as(row.age.c, nonempty!("user").with("height").into()) - .await; + let transplanted = + u32::decrypt_from_with_context(row.age.c, &cipher, nonempty!("user").with("height")).await; assert!(matches!(transplanted, Err(Error::Aead))); } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Zeroize)] struct Account { user: User, plan: String, @@ -666,13 +675,28 @@ struct Account { /// derived under the inferred pair `("accounts", "user")` like any other, /// and the inner struct's own fields sit under that: their contexts are /// extended by it, as by any context the inner struct is handed. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(struct = Account, scope = "accounts")] struct EncryptedAccount { user: EncryptedUser, plan: StackCipherText, } +/// The same nesting, with a scope the caller supplies on the outer record. +#[derive(EncryptTarget, DecryptSource)] +#[stash(struct = Account, scope = "accounts", scope(id: u64))] +struct EncryptedAccountOfRow { + user: EncryptedUser, + plan: StackCipherText, +} + +fn account() -> Account { + Account { + user: user(), + plan: "pro".to_string(), + } +} + #[tokio::test] async fn a_struct_nests_in_a_struct_as_an_ordinary_field() { let (cipher, generates, retrieves) = counting_cipher().await; @@ -680,89 +704,82 @@ async fn a_struct_nests_in_a_struct_as_an_ordinary_field() { let generator = stack_cipher().await; let generator = generator.default_keyset(); - let account = Account { - user: user(), - plan: "pro".to_string(), - }; - let row: EncryptedAccount = keyset - .encrypt_as(&account, Default::default()) - .await - .unwrap(); + let row: EncryptedAccount = account().encrypt_into(&keyset).await.unwrap(); assert_eq!(generates.load(AtomicOrdering::SeqCst), 1); // The inner struct's fields are under their own contexts, extended by // the outer field's pair. - let age_hm: EqualityTerm = generator - .encrypt_as( - &42u32, + let age_hm: EqualityTerm = 42u32 + .encrypt_into_with_context( + &generator, nonempty!("user") .with("age") - .with(nonempty!("accounts").with("user")) - .into(), + .with(nonempty!("accounts").with("user")), ) .await .unwrap(); assert_eq!(row.user.age.hm, age_hm); - let recovered = cipher - .decrypt_as::<Account, _>(row, Default::default()) - .await - .unwrap(); - assert_eq!(recovered, account); + let recovered = Account::decrypt_from(row, &cipher).await.unwrap(); + assert_eq!(recovered, account()); assert_eq!(retrieves.load(AtomicOrdering::SeqCst), 1); // The outer's plan is under the inferred `"accounts/plan"`. Decrypting // the row consumed it, so mint a fresh one to open the field alone. - let row: EncryptedAccount = keyset - .encrypt_as(&account, Default::default()) - .await - .unwrap(); - let plan: String = cipher - .decrypt_as(row.plan, nonempty!("accounts").with("plan").into()) - .await - .unwrap(); + let row: EncryptedAccount = account().encrypt_into(&keyset).await.unwrap(); + let plan = + String::decrypt_from_with_context(row.plan, &cipher, nonempty!("accounts").with("plan")) + .await + .unwrap(); assert_eq!(plan, "pro"); - // An extension extends the outer field's pair, which the inner struct - // composes with its own contexts: the inner `age` is under + // A scope the caller supplies extends the outer field's pair, which the + // inner struct composes with its own contexts: the inner `age` is under // `("user/age", ("accounts/user", id))`, the outer `plan` under // `("accounts/plan", id)`. - let row: EncryptedAccount = keyset.encrypt_as(&account, 9u64.into()).await.unwrap(); - let age_hm: EqualityTerm = generator - .encrypt_as( - &42u32, + let row: EncryptedAccountOfRow = account() + .encrypt_into_with_context(&keyset, Scope::new(9u64)) + .await + .unwrap(); + let age_hm: EqualityTerm = 42u32 + .encrypt_into_with_context( + &generator, nonempty!("user") .with("age") - .with(nonempty!("accounts").with("user").with(9u64)) - .into(), + .with(nonempty!("accounts").with("user").with(9u64)), ) .await .unwrap(); assert_eq!(row.user.age.hm, age_hm); - let plan: String = cipher - .decrypt_as( - row.plan, - nonempty!("accounts").with("plan").with(9u64).into(), - ) - .await - .unwrap(); + let plan = String::decrypt_from_with_context( + row.plan, + &cipher, + nonempty!("accounts").with("plan").with(9u64), + ) + .await + .unwrap(); assert_eq!(plan, "pro"); // The whole record opens under the same id: the decrypt side hands the // inner record the outer field's pair extended by the id, as the // encrypt side did. - let row: EncryptedAccount = keyset.encrypt_as(&account, 9u64.into()).await.unwrap(); - let recovered = cipher - .decrypt_as::<Account, _>(row, 9u64.into()) + let row: EncryptedAccountOfRow = account() + .encrypt_into_with_context(&keyset, Scope::new(9u64)) .await .unwrap(); - assert_eq!(recovered, account); + let recovered = Account::decrypt_from_with_context(row, &cipher, Scope::new(9u64)) + .await + .unwrap(); + assert_eq!(recovered, account()); // And not under another. The fake key source ignores descriptors, so the // AEAD refuses; ZeroKMS would refuse the key retrieval first // (`Error::Kms`). - let row: EncryptedAccount = keyset.encrypt_as(&account, 9u64.into()).await.unwrap(); - let other = cipher.decrypt_as::<Account, _>(row, 8u64.into()).await; + let row: EncryptedAccountOfRow = account() + .encrypt_into_with_context(&keyset, Scope::new(9u64)) + .await + .unwrap(); + let other = Account::decrypt_from_with_context(row, &cipher, Scope::new(8u64)).await; assert!(matches!(other, Err(Error::Aead)), "{other:?}"); } @@ -772,10 +789,10 @@ async fn a_struct_nests_in_a_struct_as_an_ordinary_field() { /// descriptor reserves), so each field under `struct = ..` names its segment /// with `identity = ".."`, keyed under `("reading", "<identity>")`; the /// derive refuses one that does not (`tests/ui`). -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Zeroize)] struct Reading(u32, String); -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(struct = Reading, scope = "reading")] struct EncryptedReading( #[stash(identity = "value")] EncryptedAge, @@ -783,7 +800,7 @@ struct EncryptedReading( ); /// The same with named fields: `from` by index, and the segment given. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(struct = Reading, scope = "reading")] struct NamedReading { #[stash(from = 0, identity = "value")] @@ -800,32 +817,23 @@ async fn a_tuple_plaintext_is_reached_and_rebuilt_by_index() { let generator = generator.default_keyset(); let reading = Reading(21, "celsius".into()); - let row: EncryptedReading = keyset - .encrypt_as(&reading, Default::default()) - .await - .unwrap(); + let row: EncryptedReading = reading.clone().encrypt_into(&keyset).await.unwrap(); - let hm: EqualityTerm = generator - .encrypt_as(&21u32, nonempty!("reading").with("value").into()) + let hm: EqualityTerm = 21u32 + .encrypt_into_with_context(&generator, nonempty!("reading").with("value")) .await .unwrap(); assert_eq!(row.0.hm, hm); - let recovered = cipher - .decrypt_as::<Reading, _>(row, Default::default()) - .await - .unwrap(); + let recovered = Reading::decrypt_from(row, &cipher).await.unwrap(); assert_eq!(recovered, reading); - let named: NamedReading = keyset - .encrypt_as(&reading, Default::default()) - .await - .unwrap(); + let named: NamedReading = reading.clone().encrypt_into(&keyset).await.unwrap(); assert_eq!(named.value.hm, hm, "from = 0 with the same identity"); - let unit: String = cipher - .decrypt_as(named.unit, nonempty!("reading").with("unit").into()) - .await - .unwrap(); + let unit = + String::decrypt_from_with_context(named.unit, &cipher, nonempty!("reading").with("unit")) + .await + .unwrap(); assert_eq!(unit, "celsius"); } @@ -833,9 +841,9 @@ async fn a_tuple_plaintext_is_reached_and_rebuilt_by_index() { /// A `plaintext` record wrapping a struct record that carries contexts of /// its own: the caller's context reaches the inner record as it is, and -/// extends its contexts — `("user/age", "wrapped")`. The derive is told +/// extends its contexts, `("user/age", "wrapped")`. The derive is told /// nothing about the inner record's context type. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = User)] struct WrappedUser { user: EncryptedUser, @@ -848,279 +856,106 @@ async fn a_plaintext_record_field_may_be_a_record_with_declared_contexts() { let generator = stack_cipher().await; let generator = generator.default_keyset(); - let row: WrappedUser = keyset - .encrypt_as(&user(), nonempty!("wrapped").into()) + let row: WrappedUser = user() + .encrypt_into_with_context(&keyset, nonempty!("wrapped")) .await .unwrap(); - let age_hm: EqualityTerm = generator - .encrypt_as( - &42u32, - nonempty!("user") - .with("age") - .with(nonempty!("wrapped")) - .into(), + let age_hm: EqualityTerm = 42u32 + .encrypt_into_with_context( + &generator, + nonempty!("user").with("age").with(nonempty!("wrapped")), ) .await .unwrap(); assert_eq!(row.user.age.hm, age_hm); - let recovered = cipher - .decrypt_as::<User, _>(row, nonempty!("wrapped").into()) + let recovered = User::decrypt_from_with_context(row, &cipher, nonempty!("wrapped")) .await .unwrap(); assert_eq!(recovered, user()); } -// --- The plan writes what the derive writes ------------------------------------ -// -// A plan field typed with a derived record (`encrypt_into`) composes that -// record exactly as the derive composes a record-typed field: the record's -// own contexts extended by the field's `<context>/<field>` pair. The derive -// will emit these plans; until it does, these tests hold the two together. - -mod the_plan_writes_what_the_derive_writes { - use super::*; - use stack_encrypt::plan::{pick, FieldValues}; - use stack_encrypt::{Label, Plan}; - - fn account() -> Account { - Account { - user: user(), - plan: "pro".to_string(), - } - } - - fn accounts_plan<K: 'static>() -> Plan<Account, K> { - Plan::context("accounts") - .fields() - .encrypt_into::<EncryptedUser, _>(pick("user", |a: &Account| &a.user)) - .encrypt(pick("plan", |a: &Account| &a.plan)) - .build() - .unwrap() - } - - #[tokio::test] - async fn a_record_typed_field_is_the_derives_nested_struct() { - let (cipher, sent) = recording_cipher().await; - let keyset = cipher.default_keyset(); - let accounts_plan = accounts_plan(); - - let mut planned = cipher - .encrypt(&account()) - .using(&accounts_plan) - .await - .unwrap(); - let plan_sent = sent.lock().unwrap().generated(); - sent.lock().unwrap().generate.clear(); - let derived: EncryptedAccount = keyset - .encrypt_as(&account(), Default::default()) - .await - .unwrap(); - assert_eq!( - plan_sent, - sent.lock().unwrap().generated(), - "every data key under the same descriptor" - ); - - let planned_user: EncryptedUser = planned.take("user").unwrap(); - assert_eq!(planned_user.age.hm, derived.user.age.hm); - assert_eq!(planned_user.age.ob, derived.user.age.ob); - assert_eq!(planned_user.email.terms, derived.user.email.terms); - let planned_plan: StackCipherText = planned.take("plan").unwrap(); - - // Each record opens through the other spelling. - let recovered = cipher - .decrypt_as::<Account, _>( - EncryptedAccount { - user: planned_user, - plan: planned_plan, - }, - Default::default(), - ) - .await - .unwrap(); - assert_eq!(recovered, account()); - let mut stored = FieldValues::new(); - stored - .insert("user", derived.user) - .insert("plan", derived.plan); - let back = cipher.open(stored).using(&accounts_plan).await.unwrap(); - assert_eq!(back.get::<User>("user"), Some(&user())); - assert_eq!(back.get::<String>("plan").map(String::as_str), Some("pro")); - } - - #[tokio::test] - async fn an_extension_reaches_a_record_typed_field_as_the_derives() { - let cipher = stack_cipher().await; - let keyset = cipher.default_keyset(); - let accounts_plan = accounts_plan(); - let mut planned = cipher - .encrypt(&account()) - .using(&accounts_plan) - .extend(9u64) - .await - .unwrap(); - let derived: EncryptedAccount = keyset.encrypt_as(&account(), 9u64.into()).await.unwrap(); - let planned_user: EncryptedUser = planned.take("user").unwrap(); - assert_eq!(planned_user.age.hm, derived.user.age.hm); - } - - #[tokio::test] - async fn a_struct_of_targets_is_the_derives_struct() { - let (cipher, sent) = recording_cipher().await; - let keyset = cipher.default_keyset(); - let users_plan: Plan<User, _> = Plan::context("user") - .fields() - .encrypt_into::<EncryptedAge, _>(pick("age", |u: &User| &u.age)) - .encrypt_into::<Encrypted<(EqualityTerm, MatchTerms)>, _>(pick("email", |u: &User| { - &u.email - })) - .build() - .unwrap(); - let mut planned = cipher.encrypt(&user()).using(&users_plan).await.unwrap(); - let plan_sent = sent.lock().unwrap().generated(); - sent.lock().unwrap().generate.clear(); - let derived: EncryptedUser = keyset - .encrypt_as(&user(), Default::default()) - .await - .unwrap(); - assert_eq!(plan_sent, sent.lock().unwrap().generated()); - let age: EncryptedAge = planned.take("age").unwrap(); - let email: Encrypted<(EqualityTerm, MatchTerms)> = planned.take("email").unwrap(); - assert_eq!(age.hm, derived.age.hm); - assert_eq!(age.ob, derived.age.ob); - assert_eq!(email.terms, derived.email.terms); - let recovered = cipher - .decrypt_as::<User, _>( - EncryptedUser { - age, - email, - version: 3, - }, - Default::default(), - ) - .await - .unwrap(); - assert_eq!(recovered, user()); - } - - /// The derive's `context_field`: the record stores the context it was - /// sealed under, and opening checks it against the caller's. - #[derive(EncryptFrom, DecryptInto)] - #[stash(plaintext = String)] - struct ScopedEmail { - #[stash(context_field)] - scope: Label, - c: StackCipherText, - hm: EqualityTerm, - } +// --- A record that stores its context ---------------------------------------- - #[derive(Clone)] - struct TenantRecord { - tenant: String, - email: String, - } +/// `context_field`: the record stores the context it was sealed under, and +/// opening checks it against the context the caller expects. +#[derive(EncryptTarget, DecryptSource)] +#[stash(plaintext = String)] +struct ScopedEmail { + #[stash(context_field)] + scope: Label, + c: StackCipherText, + hm: EqualityTerm, +} - fn tenant_record() -> TenantRecord { - TenantRecord { - tenant: "tenants/acme".into(), - email: "bob@example.com".into(), - } - } +fn scope() -> NonEmpty<Label> { + NonEmpty::from(Label::parse("tenants/acme/email").unwrap()) +} - fn records_plan<K: 'static>() -> Plan<TenantRecord, K> { - Plan::fields() - .context_field(pick("tenant", |r: &TenantRecord| &r.tenant)) - .encrypt_into::<(StackCipherText, EqualityTerm), _>(pick( - "email", - |r: &TenantRecord| &r.email, - )) - .build() - .unwrap() - } +fn email() -> String { + "bob@example.com".to_string() +} - fn scope() -> NonEmpty<Label> { - NonEmpty::from(Label::parse("tenants/acme/email").unwrap()) - } +#[tokio::test] +async fn a_context_field_record_is_sealed_under_the_context_it_stores() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); - #[tokio::test] - async fn a_context_field_writes_the_derives_context_field_bytes() { - let (cipher, sent) = recording_cipher().await; - let keyset = cipher.default_keyset(); - let records_plan = records_plan(); + let derived: ScopedEmail = email() + .encrypt_into_with_context(&keyset, scope()) + .await + .unwrap(); + assert_eq!( + sent.lock().unwrap().generated(), + ["tenants/acme/email"], + "the field is sealed under the context the derive is handed" + ); + assert_eq!(derived.scope, scope().into_inner()); + // The term is the leaf's under the same context. + let hm: EqualityTerm = email() + .encrypt_into_with_context(&keyset, scope()) + .await + .unwrap(); + assert_eq!(derived.hm, hm); - let mut planned = cipher - .encrypt(&tenant_record()) - .using(&records_plan) - .await - .unwrap(); - let derived: ScopedEmail = keyset - .encrypt_as(&tenant_record().email, scope()) - .await - .unwrap(); - assert_eq!( - sent.lock().unwrap().generated(), - ["tenants/acme/email", "tenants/acme/email"], - "the field is sealed under the context the derive is handed" - ); - let (c, hm): (StackCipherText, EqualityTerm) = planned.take("email").unwrap(); - assert_eq!(hm, derived.hm); - - // The derive's reader opens the plan's ciphertext, and the plan's - // reader the derive's. - let opened: String = cipher - .decrypt_as( - ScopedEmail { - scope: scope().into_inner(), - c, - hm, - }, - scope().into(), - ) - .await - .unwrap(); - assert_eq!(opened, tenant_record().email); - planned.insert("email", (derived.c, derived.hm)); - let back = cipher - .open(planned) - .context("tenants/acme") - .using(&records_plan) - .await - .unwrap(); - assert_eq!(back.get::<String>("email"), Some(&tenant_record().email)); - } + // A record rebuilt from its stored parts opens under the context the + // caller expects, and the leaf's ciphertext opens through the record. + let leaf: StackCipherText = email() + .encrypt_into_with_context(&keyset, scope()) + .await + .unwrap(); + let opened = String::decrypt_from_with_context( + ScopedEmail { + scope: scope().into_inner(), + c: leaf, + hm, + }, + &cipher, + scope(), + ) + .await + .unwrap(); + assert_eq!(opened, email()); + let opened = String::decrypt_from_with_context(derived, &cipher, scope()) + .await + .unwrap(); + assert_eq!(opened, email()); +} - #[tokio::test] - async fn both_refuse_a_mismatched_expected_context_before_any_key_request() { - let (cipher, _, retrieves) = counting_cipher().await; - let keyset = cipher.default_keyset(); - let records_plan = records_plan(); +#[tokio::test] +async fn a_context_field_record_refuses_a_mismatched_expected_context_before_any_key_request() { + let (cipher, _, retrieves) = counting_cipher().await; + let keyset = cipher.default_keyset(); - let planned = cipher - .encrypt(&tenant_record()) - .using(&records_plan) - .await - .unwrap(); - let refused = cipher - .open(planned) - .context("tenants/globex") - .using(&records_plan) - .await; - assert!( - matches!(refused, Err(Error::ContextMismatch { .. })), - "{refused:?}" - ); - - let derived: ScopedEmail = keyset - .encrypt_as(&tenant_record().email, scope()) - .await - .unwrap(); - let other = NonEmpty::from(Label::parse("tenants/globex/email").unwrap()); - let refused: Result<String, _> = cipher.decrypt_as(derived, other.into()).await; - assert!( - matches!(refused, Err(Error::ContextMismatch { .. })), - "{refused:?}" - ); - assert_eq!(retrieves.load(AtomicOrdering::SeqCst), 0); - } + let derived: ScopedEmail = email() + .encrypt_into_with_context(&keyset, scope()) + .await + .unwrap(); + let other = NonEmpty::from(Label::parse("tenants/globex/email").unwrap()); + let refused = String::decrypt_from_with_context(derived, &cipher, other).await; + assert!( + matches!(refused, Err(Error::ContextMismatch { .. })), + "{refused:?}" + ); + assert_eq!(retrieves.load(AtomicOrdering::SeqCst), 0); } diff --git a/packages/stack-encrypt/tests/derive_plan.rs b/packages/stack-encrypt/tests/derive_plan.rs index 076414b10c..47390a7997 100644 --- a/packages/stack-encrypt/tests/derive_plan.rs +++ b/packages/stack-encrypt/tests/derive_plan.rs @@ -1,62 +1,59 @@ -//! The derive emits the plan: what `#[derive(EncryptFrom)]` generates now -//! runs `Record::plan()`, and it must write exactly what the combinator -//! chain it used to generate wrote. Each `Legacy*` type below is that old -//! generated code, written out by hand; each test runs it beside the derived -//! record and compares what ZeroKMS is sent, the terms, and that each -//! record's ciphertext opens through the other. +//! The derive writes what the combinators write: what `#[derive(EncryptTarget)]` +//! generates must write exactly what the same declaration written out by hand +//! with the combinators of `stack_encrypt::target` writes. Each `Legacy*` type +//! below is such a declaration. Each test runs it beside the derived record +//! and compares what ZeroKMS is sent and the terms, and opens the hand-written +//! record's ciphertext through the derived record. mod common; use std::sync::atomic::Ordering as AtomicOrdering; use common::{counting_cipher, recording_cipher, stack_cipher}; -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::op; -use stack_encrypt::plan::{pick, FieldValues}; -use stack_encrypt::sem::{EqualityTerm, MatchTerms, OreTerm}; +use stack_encrypt::sem::{EqualityTerm, MatchTerms, OpeTerm, OreTerm}; use stack_encrypt::target::{ - AeadContext, CallerContext, DeclaredContext, DecryptField, Decryptable, Decryption, Encrypted, - Encryption, IndexSpec, + AeadContext, CallerContext, DeclaredContext, DeclaredScopes, DecryptField, DecryptSource, + Decryptable, Decryption, EncryptTarget, Encrypted, Encryption, IndexSpec, RequiredContext, }; use stack_encrypt::{ - nonempty, DecryptInto, EmptyError, EncryptFrom, Error, NonEmpty, Plan, PlanError, - StackCipherText, + nonempty, DecryptFrom, EmptyError, EncryptInto, Error, NonEmpty, PlanError, StackCipherText, }; +use zeroize::Zeroize; // --- A `plaintext = T` record ------------------------------------------------ -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String)] struct Email { c: StackCipherText, hm: EqualityTerm, } -/// What the derive generated for `Email` before it emitted the plan. +/// `Email`'s declaration, written by hand. struct LegacyEmail { c: StackCipherText, hm: EqualityTerm, } -impl EncryptFrom<String> for LegacyEmail { - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> +impl EncryptTarget<String> for LegacyEmail { + type ContextInput = RequiredContext<CallerContext>; + fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::ContextInput> where String: 's, { - <StackCipherText as EncryptFrom<String>>::encryption::<K>() + <StackCipherText as EncryptTarget<String>>::encryption::<K>() .accepting::<CallerContext>() .zip( - <EqualityTerm as EncryptFrom<String>>::encryption::<K>() + <EqualityTerm as EncryptTarget<String>>::encryption::<K>() .accepting::<CallerContext>(), ) - .accepting::<Self::Context>() + .accepting::<Self::ContextInput>() .map(move |(c, hm)| Self { c, hm }) } } #[tokio::test] -async fn a_plaintext_record_writes_what_the_old_chain_wrote() { +async fn a_plaintext_record_writes_what_the_hand_written_combinators_write() { // A context of one escaped part, a two-part one and an integer: the // caller's context must reach the outputs as given, never as a label. for (n, context) in [ @@ -71,8 +68,16 @@ async fn a_plaintext_record_writes_what_the_old_chain_wrote() { let keyset = cipher.default_keyset(); let value = format!("alice{n}@example.com"); - let derived: Email = keyset.encrypt_as(&value, context.clone()).await.unwrap(); - let legacy: LegacyEmail = keyset.encrypt_as(&value, context.clone()).await.unwrap(); + let derived: Email = value + .clone() + .encrypt_into_with_context(&keyset, context.clone()) + .await + .unwrap(); + let legacy: LegacyEmail = value + .clone() + .encrypt_into_with_context(&keyset, context.clone()) + .await + .unwrap(); assert_eq!(derived.hm, legacy.hm, "the same term"); { let sent = sent.lock().unwrap(); @@ -82,62 +87,37 @@ async fn a_plaintext_record_writes_what_the_old_chain_wrote() { assert_eq!(generated[0], generated[1], "the same descriptor"); } - // Each record opens through the other's reader. + // The hand-written record opens through the derived record's reader, + // and the derived record's ciphertext opens as a leaf. let crossed = Email { c: legacy.c, hm: legacy.hm, }; - let opened: String = cipher.decrypt_as(crossed, context.clone()).await.unwrap(); - assert_eq!(opened, value); - let opened: String = keyset - .decrypt_as(derived.c, AeadContext::from(context)) + let opened = String::decrypt_from_with_context(crossed, &cipher, context.clone()) .await .unwrap(); assert_eq!(opened, value); + let opened = + String::decrypt_from_with_context(derived.c, &keyset, AeadContext::from(context)) + .await + .unwrap(); + assert_eq!(opened, value); } } #[test] -fn a_plaintext_record_plan_is_a_one_value_plan_over_its_outputs() { - let plan = Email::plan::<String>().unwrap(); - // No context of its own: every output shares the caller's. - assert_eq!(plan.label(), None); - // The record answers the queries its terms answer. +fn a_plaintext_record_answers_the_queries_its_terms_answer() { assert_eq!( - <Email as EncryptFrom<String>>::indexes(), + <Email as EncryptTarget<String>>::indexes(), vec![IndexSpec::Equality] ); + // A record used as a field names what its type declares: `Age` names ORE. + assert_eq!(<Age as EncryptTarget<u32>>::indexes(), vec![IndexSpec::Ore]); } -/// A derived record used as a plan field answers its terms' queries, through -/// the `indexes()` the derive now emits. -#[tokio::test] -async fn a_derived_record_as_a_typed_field_answers_its_queries() { - struct User { - email: String, - } - let cipher = stack_cipher().await; - let users_plan: Plan<User, FakeDataKeySource> = Plan::context("users") - .fields() - .encrypt_into::<Email, _>(pick("email", |u: &User| &u.email)) - .build() - .unwrap(); - let user = User { - email: "bob@example.com".into(), - }; - let mut record = cipher.encrypt(&user).using(&users_plan).await.unwrap(); - let email: Email = record.take("email").unwrap(); - let query_value = cipher - .query(op::Eq, "bob@example.com") - .on(&users_plan.field("email").unwrap()) - .await - .unwrap(); - assert_eq!(query_value, email.hm); -} - -// --- A record with more outputs than one tuple holds ------------------------- +// --- A record with five outputs ---------------------------------------------- -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32)] struct Wide { #[stash(decrypt)] @@ -154,62 +134,46 @@ async fn five_outputs_nest_and_each_is_derived_under_the_one_context() { let keyset = cipher.default_keyset(); let context = || nonempty!("readings"); - let wide: Wide = keyset.encrypt_as(&9u32, context().into()).await.unwrap(); - let hm: EqualityTerm = keyset.encrypt_as(&9u32, context().into()).await.unwrap(); - let ob: OreTerm<u32> = keyset.encrypt_as(&9u32, context().into()).await.unwrap(); + let wide: Wide = 9u32 + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + let hm: EqualityTerm = 9u32 + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + let ob: OreTerm<u32> = 9u32 + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); assert_eq!(wide.hm, hm); assert_eq!(wide.ob, ob); assert_eq!( - <Wide as EncryptFrom<u32>>::indexes().len(), + <Wide as EncryptTarget<u32>>::indexes().len(), 2, "equality and ORE: a record declares one order index" ); - let shadow: u32 = cipher - .decrypt_as(wide.shadow, context().into()) + let shadow = u32::decrypt_from_with_context(wide.shadow, &cipher, context()) .await .unwrap(); assert_eq!(shadow, 9); - let twin: u32 = cipher - .decrypt_as(wide.twin, context().into()) + let twin = u32::decrypt_from_with_context(wide.twin, &cipher, context()) .await .unwrap(); assert_eq!(twin, 9); - let wide: Wide = keyset.encrypt_as(&9u32, context().into()).await.unwrap(); - let opened: u32 = cipher.decrypt_as(wide, context().into()).await.unwrap(); + let wide: Wide = 9u32 + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + let opened = u32::decrypt_from_with_context(wide, &cipher, context()) + .await + .unwrap(); assert_eq!(opened, 9); } -// --- Two outputs that declare the same index --------------------------------- - -#[allow(dead_code)] // never built: refusing it is the point -#[derive(EncryptFrom)] -#[stash(plaintext = String)] -struct TwoEqualities { - hm: EqualityTerm, - again: EqualityTerm, -} - -/// The one plan refusal the derive cannot know at compile time: it sees the -/// outputs' type names, not the indexes they declare. Refused when run, -/// before any key is requested. -#[tokio::test] -async fn two_outputs_declaring_one_index_are_refused_before_any_request() { - let (cipher, generates, _) = counting_cipher().await; - let keyset = cipher.default_keyset(); - let result: Result<TwoEqualities, _> = keyset - .encrypt_as(&"x".to_string(), nonempty!("x").into()) - .await; - assert!( - matches!(result, Err(Error::Plan(PlanError::DuplicateIndex { .. }))), - "{:?}", - result.err() - ); - assert_eq!(generates.load(AtomicOrdering::SeqCst), 0); -} - // --- A record that stores its context ----------------------------------------- -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String)] struct Scoped { #[stash(context_field)] @@ -220,28 +184,29 @@ struct Scoped { v: u8, } -/// What the derive generated for `Scoped` before it emitted the plan. +/// `Scoped`'s declaration, written by hand: the outputs run under the +/// caller's context, and the record keeps a copy of it. struct LegacyScoped { i: String, c: StackCipherText, hm: EqualityTerm, } -impl EncryptFrom<String> for LegacyScoped { - type Context = NonEmpty<String>; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> +impl EncryptTarget<String> for LegacyScoped { + type ContextInput = RequiredContext<NonEmpty<String>>; + fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::ContextInput> where String: 's, { - <StackCipherText as EncryptFrom<String>>::encryption::<K>() + <StackCipherText as EncryptTarget<String>>::encryption::<K>() .accepting::<CallerContext>() .zip( - <EqualityTerm as EncryptFrom<String>>::encryption::<K>() + <EqualityTerm as EncryptTarget<String>>::encryption::<K>() .accepting::<CallerContext>(), ) - .accepting::<Self::Context>() + .accepting::<Self::ContextInput>() .map_with_context(move |(c, hm), context| Self { - i: context.clone().into_inner(), + i: context.into_inner().into_inner(), c, hm, }) @@ -253,13 +218,21 @@ fn tenant() -> NonEmpty<String> { } #[tokio::test] -async fn a_context_field_record_writes_what_the_old_chain_wrote() { +async fn a_context_field_record_writes_what_the_hand_written_combinators_write() { let (cipher, sent) = recording_cipher().await; let keyset = cipher.default_keyset(); let value = "bob@example.com".to_string(); - let derived: Scoped = keyset.encrypt_as(&value, tenant()).await.unwrap(); - let legacy: LegacyScoped = keyset.encrypt_as(&value, tenant()).await.unwrap(); + let derived: Scoped = value + .clone() + .encrypt_into_with_context(&keyset, tenant()) + .await + .unwrap(); + let legacy: LegacyScoped = value + .clone() + .encrypt_into_with_context(&keyset, tenant()) + .await + .unwrap(); assert_eq!(derived.i, legacy.i); assert_eq!(derived.i, "tenants/acme/email"); assert_eq!(derived.v, 3); @@ -277,103 +250,40 @@ async fn a_context_field_record_writes_what_the_old_chain_wrote() { hm: legacy.hm, v: 3, }; - let opened: String = cipher.decrypt_as(crossed, tenant().into()).await.unwrap(); + let opened = String::decrypt_from_with_context(crossed, &cipher, tenant()) + .await + .unwrap(); assert_eq!(opened, value); } #[tokio::test] -async fn a_context_field_plan_carries_and_checks_the_context() { +async fn a_context_field_record_refuses_an_empty_stored_context_before_any_request() { let (cipher, _, retrieves) = counting_cipher().await; let keyset = cipher.default_keyset(); let value = "bob@example.com".to_string(); - let plan = Scoped::plan::<String>().unwrap(); - - let seal = || keyset.run(plan.encryption_with_context(), &value, tenant()); - - let (stored, (_, hm)) = seal().await.unwrap(); - assert_eq!(stored, "tenants/acme/email"); - let derived: Scoped = keyset.encrypt_as(&value, tenant()).await.unwrap(); - assert_eq!(hm, derived.hm, "the plan's term is the record's"); - - // Another destination is refused before any key is retrieved. - let other = NonEmpty::new("tenants/globex/email".to_string()).unwrap(); - let record = seal().await.unwrap(); - let refused = keyset - .run_decryption(plan.decryption_with_context(record, Some(other))) - .await; - assert!( - matches!(refused, Err(Error::ContextMismatch { .. })), - "{:?}", - refused.err() - ); - assert_eq!(retrieves.load(AtomicOrdering::SeqCst), 0); - - // The expected one, and an unchecked one, open it. - let record = seal().await.unwrap(); - let opened = keyset - .run_decryption(plan.decryption_with_context(record, Some(tenant()))) + let sealed: Scoped = value + .encrypt_into_with_context(&keyset, tenant()) .await .unwrap(); - assert_eq!(opened, value); - let record = seal().await.unwrap(); - let opened = keyset - .run_decryption(plan.decryption_with_context(record, None)) - .await - .unwrap(); - assert_eq!(opened, value); -} -#[test] -fn a_context_field_plan_has_one_context_source() { - let refused = Plan::value::<String>() - .context("users/email") - .context_field::<String>() - .encrypt_into::<(StackCipherText, EqualityTerm)>() - .build(); - assert!(matches!( - refused, - Err(Error::Plan(PlanError::TwoContextSources { .. })) - )); - let refused = Plan::value::<String>() - .context_field::<String>() - .encrypt_into::<(EqualityTerm, EqualityTerm)>() - .build(); - assert!(matches!( - refused, - Err(Error::Plan(PlanError::DuplicateIndex { .. })) - )); -} - -#[tokio::test] -async fn a_one_value_plan_with_its_own_context_refuses_the_callers() { - let (cipher, generates, _) = counting_cipher().await; - let keyset = cipher.default_keyset(); - let plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<(StackCipherText, EqualityTerm)>() - .build() - .unwrap(); - let refused = keyset - .run( - plan.encryption_with_context::<_, CallerContext>(), - &"x".to_string(), - nonempty!("users/email").into(), - ) - .await; + // The stored context is not authenticated: a row can hold an empty one. + let emptied = Scoped { + i: String::new(), + ..sealed + }; + let refused = String::decrypt_from(emptied, &keyset).await; + // The empty-context refusal, not a mismatch: with no expected context + // there is nothing for the stored one to differ from. assert!( - matches!( - refused, - Err(Error::Plan(PlanError::TwoContextSources { .. })) - ), - "{:?}", - refused.err() + matches!(&refused, Err(Error::Other(error)) if error.is::<EmptyError>()), + "{refused:?}" ); - assert_eq!(generates.load(AtomicOrdering::SeqCst), 0); + assert_eq!(retrieves.load(AtomicOrdering::SeqCst), 0); } // --- A record of ciphertexts alone takes an AEAD-only context ----------------- -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String, context_type = AeadContext)] struct Shadowed { #[stash(decrypt)] @@ -382,44 +292,45 @@ struct Shadowed { } #[tokio::test] -async fn a_tuple_of_ciphertexts_takes_an_aead_context() { +async fn a_record_of_ciphertexts_takes_an_aead_context() { let (cipher, sent) = recording_cipher().await; let keyset = cipher.default_keyset(); let value = "x".to_string(); let context = || AeadContext::from(nonempty!("names")); - let record: Shadowed = keyset.encrypt_as(&value, context()).await.unwrap(); - let pair: (StackCipherText, StackCipherText) = - keyset.encrypt_as(&value, context()).await.unwrap(); - assert_eq!( - sent.lock().unwrap().generated(), - ["names", "names", "names", "names"] - ); - let opened: String = keyset.decrypt_as(record.shadow, context()).await.unwrap(); - assert_eq!(opened, value); - let opened: String = keyset.decrypt_as(pair.1, context()).await.unwrap(); + let record: Shadowed = value + .clone() + .encrypt_into_with_context(&keyset, context()) + .await + .unwrap(); + assert_eq!(sent.lock().unwrap().generated(), ["names", "names"]); + let opened = String::decrypt_from_with_context(record.shadow, &keyset, context()) + .await + .unwrap(); assert_eq!(opened, value); - let opened: String = cipher.decrypt_as(record.c, context()).await.unwrap(); + let opened = String::decrypt_from_with_context(record.c, &cipher, context()) + .await + .unwrap(); assert_eq!(opened, value); } // --- A `struct` record --------------------------------------------------------- -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Zeroize)] struct User { email: String, name: String, age: u32, } -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32)] struct Age { c: StackCipherText, ob: OreTerm<u32>, } -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(struct = User, scope = "users")] struct EncryptedUser { email: Encrypted<EqualityTerm>, @@ -430,33 +341,34 @@ struct EncryptedUser { v: u8, } -/// What the derive generated for `EncryptedUser` before it emitted the plan. +/// `EncryptedUser`'s declaration, written by hand: each field's target over +/// its plaintext field, under the field's own context. struct LegacyUser { email: Encrypted<EqualityTerm>, name: StackCipherText, age: Age, } -impl EncryptFrom<User> for LegacyUser { - type Context = DeclaredContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, User, Self, K, Self::Context> +impl EncryptTarget<User> for LegacyUser { + type ContextInput = DeclaredScopes; + fn encryption<'s, K: 'static>() -> Encryption<'s, User, Self, K, Self::ContextInput> where User: 's, { - <Encrypted<EqualityTerm> as EncryptFrom<String>>::encryption::<K>() + <Encrypted<EqualityTerm> as EncryptTarget<String>>::encryption::<K>() .project(|u: &User| &u.email) .under(nonempty!("users").with("email")) .zip( - <StackCipherText as EncryptFrom<String>>::encryption::<K>() + <StackCipherText as EncryptTarget<String>>::encryption::<K>() .project(|u: &User| &u.name) .under(nonempty!("users").with("nickname")), ) .zip( - <Age as EncryptFrom<u32>>::encryption::<K>() + <Age as EncryptTarget<u32>>::encryption::<K>() .project(|u: &User| &u.age) .under(nonempty!("users").with("age")), ) - .accepting::<Self::Context>() + .accepting::<Self::ContextInput>() .map(move |((email, name), age)| Self { email, name, age }) } } @@ -469,14 +381,67 @@ fn user() -> User { } } +/// The record as a caller runs it: the plaintext by value, and no context. #[tokio::test] -async fn a_struct_record_writes_what_the_old_chain_wrote() { +async fn a_struct_record_writes_what_the_hand_written_combinators_write() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + + let derived: EncryptedUser = user().encrypt_into(&keyset).await.unwrap(); + let legacy: LegacyUser = user().encrypt_into(&keyset).await.unwrap(); + assert_eq!(derived.v, 3); + assert_eq!(derived.email.terms, legacy.email.terms); + assert_eq!(derived.age.ob, legacy.age.ob); + { + let sent = sent.lock().unwrap(); + assert_eq!(sent.generate.len(), 2, "one request per record"); + assert_eq!( + sent.generate[0], sent.generate[1], + "the same descriptors, in the same order" + ); + // Each field under the record's scope and its own segment, the + // pinned identity in place of the field's name. + assert_eq!( + sent.generate[0], + ["users/email", "users/nickname", "users/age"] + ); + } + + let crossed = EncryptedUser { + email: legacy.email, + name: legacy.name, + age: legacy.age, + v: 3, + }; + let opened = User::decrypt_from(crossed, &cipher).await.unwrap(); + assert_eq!(opened, user()); +} + +/// The record as a record that contains it runs it: the plaintext borrowed, +/// and each field's context extended by the context it is handed. +#[tokio::test] +async fn a_struct_record_handed_a_context_writes_what_the_hand_written_combinators_write() { for extend in [DeclaredContext::from(()), DeclaredContext::from(7u64)] { let (cipher, sent) = recording_cipher().await; let keyset = cipher.default_keyset(); + let plaintext = user(); - let derived: EncryptedUser = keyset.encrypt_as(&user(), extend.clone()).await.unwrap(); - let legacy: LegacyUser = keyset.encrypt_as(&user(), extend.clone()).await.unwrap(); + let derived: EncryptedUser = keyset + .run( + <EncryptedUser as EncryptTarget<User>>::encryption(), + &plaintext, + DeclaredScopes::from(extend.clone()), + ) + .await + .unwrap(); + let legacy: LegacyUser = keyset + .run( + <LegacyUser as EncryptTarget<User>>::encryption(), + &plaintext, + DeclaredScopes::from(extend.clone()), + ) + .await + .unwrap(); assert_eq!(derived.v, 3); assert_eq!(derived.email.terms, legacy.email.terms); assert_eq!(derived.age.ob, legacy.age.ob); @@ -495,178 +460,19 @@ async fn a_struct_record_writes_what_the_old_chain_wrote() { age: legacy.age, v: 3, }; - let opened = cipher.decrypt_as::<User, _>(crossed, extend).await.unwrap(); - assert_eq!(opened, user()); - } -} - -/// `EncryptedUser`'s plan, written by hand. -fn users_plan_by_hand<K: 'static>() -> Plan<User, K> { - Plan::context("users") - .fields() - .encrypt_into::<Encrypted<EqualityTerm>, _>(pick("email", |u: &User| &u.email)) - .encrypt_into::<StackCipherText, _>(pick("name", |u: &User| &u.name)) - .identity("nickname") - .encrypt_into::<Age, _>(pick("age", |u: &User| &u.age)) - .build() - .unwrap() -} - -#[test] -fn a_struct_record_plan_is_the_hand_written_chain() { - let derived = EncryptedUser::plan::<FakeDataKeySource>().unwrap(); - let by_hand: Plan<User, FakeDataKeySource> = users_plan_by_hand(); - assert_eq!(format!("{derived:?}"), format!("{by_hand:?}")); - let labels: Vec<_> = derived - .field_plans() - .map(|field| field.label().map(ToString::to_string)) - .collect(); - assert_eq!( - labels, - [ - Some("users/email".to_string()), - Some("users/nickname".to_string()), - Some("users/age".to_string()) - ] - ); - // A typed field answers what its type declares: `Age` names ORE. - assert_eq!(derived.field("age").unwrap().indexes(), [IndexSpec::Ore]); -} - -/// A `struct` record whose field type declares one index twice: the other -/// plan refusal the derive cannot see at compile time. -#[allow(dead_code)] // never built: refusing it is the point -#[derive(EncryptFrom)] -#[stash(struct = User, scope = "users")] -struct TwoEqualitiesInAField { - email: Encrypted<(EqualityTerm, EqualityTerm)>, -} - -#[tokio::test] -async fn a_field_declaring_one_index_twice_is_refused_before_any_request() { - let refused = TwoEqualitiesInAField::plan::<FakeDataKeySource>(); - assert!( - matches!( - &refused, - Err(Error::Plan(PlanError::DuplicateIndex { at, index: "eq" })) if at == "email" - ), - "{:?}", - refused.err() - ); - let (cipher, generates, _) = counting_cipher().await; - let keyset = cipher.default_keyset(); - let result = keyset - .encrypt_as::<_, TwoEqualitiesInAField>(&user(), DeclaredContext::default()) - .await; - assert!( - matches!(result, Err(Error::Plan(PlanError::DuplicateIndex { .. }))), - "{:?}", - result.err() - ); - assert_eq!(generates.load(AtomicOrdering::SeqCst), 0); -} - -/// Every plan the derive emits builds, except where an output's type -/// declares an index another declares too: the derive sees types, not the -/// indexes they declare, and refused every other input at compile time. -#[test] -fn every_derived_plan_builds() { - assert!(EncryptedUser::plan::<FakeDataKeySource>().is_ok()); - assert!(Email::plan::<String>().is_ok()); - assert!(Age::plan::<u32>().is_ok()); - assert!(Wide::plan::<u32>().is_ok()); - assert!(Scoped::plan::<String>().is_ok()); - assert!(Shadowed::plan::<String>().is_ok()); - assert!( - TwoEqualities::plan::<String>().is_err(), - "two outputs of one index" - ); - assert!( - TwoEqualitiesInAField::plan::<FakeDataKeySource>().is_err(), - "a field type of one index twice" - ); -} - -#[test] -fn a_context_field_plan_debugs_its_stored_context_and_target() { - let start = Plan::value::<String>().context_field::<String>(); - let start = format!("{start:?}"); - assert!(start.contains("ContextFieldStart"), "{start}"); - assert!(start.contains("String"), "{start}"); - let plan = Scoped::plan::<String>().unwrap(); - let plan = format!("{plan:?}"); - assert!(plan.contains("Stored<alloc::string::String, "), "{plan}"); - assert!(plan.contains("EqualityTerm"), "{plan}"); -} - -// --- The derived `struct` record and the same plan written by hand ----------- - -/// The derive's `struct` opener (written out by the derive) and the plan's -/// opening (`Plan::decryption`) each encode `<context>/<identity>` extended -/// by the caller's context. Each opens what the other wrote, for every field -/// kind the record has (an index field, an identity-pinned field, a nested -/// record) and with and without an extension; and the derived record sends -/// ZeroKMS exactly what the hand-written plan sends, in one request each. -#[tokio::test] -async fn a_struct_record_and_its_plan_written_by_hand_open_each_other() { - for extend in [DeclaredContext::from(()), DeclaredContext::from(7u64)] { - let (cipher, sent) = recording_cipher().await; - let keyset = cipher.default_keyset(); - let by_hand = users_plan_by_hand(); - - let derived: EncryptedUser = keyset.encrypt_as(&user(), extend.clone()).await.unwrap(); - let mut written = cipher - .encrypt(&user()) - .using(&by_hand) - .extend(extend.clone()) - .await - .unwrap(); - { - let sent = sent.lock().unwrap(); - assert_eq!(sent.generate.len(), 2, "one request per record"); - assert_eq!(sent.generate[0], sent.generate[1], "the same descriptors"); - } - - // The plan's output, opened through the derived record. - let email: Encrypted<EqualityTerm> = written.take("email").unwrap(); - assert_eq!(email.terms, derived.email.terms, "the same term"); - let crossed = EncryptedUser { - email, - name: written.take("name").unwrap(), - age: written.take("age").unwrap(), - v: 3, - }; - let opened = cipher - .decrypt_as::<User, _>(crossed, extend.clone()) + let opened: User = cipher + .run_decryption(crossed.decryption(DeclaredScopes::from(extend))) .await .unwrap(); assert_eq!(opened, user()); - - // The derived record, opened through the plan. - let mut values = FieldValues::new(); - values - .insert("email", derived.email) - .insert("name", derived.name) - .insert("age", derived.age); - let opened = cipher - .open(values) - .using(&by_hand) - .extend(extend) - .await - .unwrap(); - assert_eq!(opened.get::<String>("email"), Some(&user().email)); - assert_eq!(opened.get::<String>("name"), Some(&user().name)); - assert_eq!(opened.get::<u32>("age"), Some(&user().age)); } } // --- A record whose ciphertext is last ----------------------------------------- -/// Five outputs, its one ciphertext last: a tuple of targets holds up to -/// five, so the plan's target nests the tail, `(A, B, C, (D, E))`, and the -/// ciphertext sits in the nested tuple. +/// Five outputs, its one ciphertext last. #[allow(dead_code)] // only `hm` and the opening are read -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String)] struct CiphertextLast { hm: EqualityTerm, @@ -678,98 +484,34 @@ struct CiphertextLast { } #[tokio::test] -async fn the_one_decryptable_output_opens_from_the_nested_tail() { +async fn the_one_decryptable_output_opens_when_it_is_the_last_field() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); let context = || nonempty!("names"); let value = "alice".to_string(); - // Through the derived record. - let record: CiphertextLast = keyset.encrypt_as(&value, context().into()).await.unwrap(); - let hm: EqualityTerm = keyset.encrypt_as(&value, context().into()).await.unwrap(); - assert_eq!(record.hm, hm); - let opened: String = cipher.decrypt_as(record, context().into()).await.unwrap(); - assert_eq!(opened, value); - - // Through the plan, whose output is the nested tuple and which opens it - // through the tuple's own `DecryptInto`. - let plan = CiphertextLast::plan::<String>().unwrap(); - let outputs = cipher - .encrypt(&value) - .context("names") - .using(&plan) - .await - .unwrap(); - let (plan_hm, _, _, (_, _)) = &outputs; - assert_eq!(*plan_hm, hm); - let opened = cipher - .open(outputs) - .context("names") - .using(&plan) + let record: CiphertextLast = value + .clone() + .encrypt_into_with_context(&keyset, context()) .await .unwrap(); - assert_eq!(opened, value); -} - -// --- A record's context field, through its plan ---------------------------------- - -#[tokio::test] -async fn a_context_field_plan_refuses_an_empty_stored_context_before_any_request() { - let (cipher, _, retrieves) = counting_cipher().await; - let keyset = cipher.default_keyset(); - let value = "bob@example.com".to_string(); - let plan = Scoped::plan::<String>().unwrap(); - let (_, sealed) = keyset - .run(plan.encryption_with_context(), &value, tenant()) + let hm: EqualityTerm = value + .clone() + .encrypt_into_with_context(&keyset, context()) .await .unwrap(); - - // The stored context is not authenticated: a row can hold an empty one. - let refused = keyset - .run_decryption(plan.decryption_with_context((String::new(), sealed), None)) - .await; - // The empty-context refusal, not a mismatch: with no expected context - // there is nothing for the stored one to differ from. - assert!( - matches!(&refused, Err(Error::Other(error)) if error.is::<EmptyError>()), - "{refused:?}" - ); - assert_eq!(retrieves.load(AtomicOrdering::SeqCst), 0); -} - -/// The derived `context_field` record's opener (written out by the derive) -/// and `ValuePlan::decryption_with_context` each check the stored context -/// and open under it: each opens what the other wrote. -#[tokio::test] -async fn a_context_field_record_and_its_plan_open_each_other() { - let cipher = stack_cipher().await; - let keyset = cipher.default_keyset(); - let value = "bob@example.com".to_string(); - let plan = Scoped::plan::<String>().unwrap(); - - // The derived record, opened through the plan. - let derived: Scoped = keyset.encrypt_as(&value, tenant()).await.unwrap(); - let opened = keyset - .run_decryption( - plan.decryption_with_context((derived.i, (derived.c, derived.hm)), Some(tenant())), - ) - .await - .unwrap(); - assert_eq!(opened, value); - - // The plan's output, opened through the derived record. - let (i, (c, hm)) = keyset - .run(plan.encryption_with_context(), &value, tenant()) + assert_eq!(record.hm, hm); + let opened = String::decrypt_from_with_context(record, &cipher, context()) .await .unwrap(); - let crossed = Scoped { i, c, hm, v: 3 }; - let opened: String = cipher.decrypt_as(crossed, tenant().into()).await.unwrap(); assert_eq!(opened, value); } // --- A record that declares its context type ---------------------------------- -#[derive(EncryptFrom, DecryptInto)] +/// A ciphertext alone, under the record's `NonEmpty<u64>`: the ciphertext +/// takes the AEAD half of that context. +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String, context_type = NonEmpty<u64>)] struct Tenanted { c: StackCipherText, @@ -778,11 +520,12 @@ struct Tenanted { /// A term whose context is a `NonEmpty<u64>`, not a `CallerContext`. #[derive(Debug, PartialEq)] struct TenantTag(EqualityTerm); -impl EncryptFrom<String> for TenantTag { - type Context = NonEmpty<u64>; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> { - <EqualityTerm as EncryptFrom<String>>::encryption() - .accepting::<NonEmpty<u64>>() +impl EncryptTarget<String> for TenantTag { + type ContextInput = RequiredContext<NonEmpty<u64>>; + fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::ContextInput> { + <EqualityTerm as EncryptTarget<String>>::encryption() + .accepting::<CallerContext>() + .accepting::<Self::ContextInput>() .map(TenantTag) } fn indexes() -> Vec<IndexSpec> { @@ -799,14 +542,14 @@ impl<P, Ctx> DecryptField<P, Ctx> for TenantTag { } /// One output, whose context is the record's `NonEmpty<u64>`. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String, context_type = NonEmpty<u64>)] struct WrappedTenanted { inner: Tenanted, } /// Two outputs sharing the record's `NonEmpty<u64>`, one of them decryptable. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String, context_type = NonEmpty<u64>)] struct TenantedPair { inner: Tenanted, @@ -823,14 +566,137 @@ async fn a_record_keeps_its_declared_context_type() { let value = "bob@example.com".to_string(); let tenant = || NonEmpty::from(7u64); - let wrapped: WrappedTenanted = keyset.encrypt_as(&value, tenant()).await.unwrap(); - let opened: String = cipher.decrypt_as(wrapped, tenant()).await.unwrap(); + let wrapped: WrappedTenanted = value + .clone() + .encrypt_into_with_context(&keyset, tenant()) + .await + .unwrap(); + let opened = String::decrypt_from_with_context(wrapped, &cipher, tenant()) + .await + .unwrap(); assert_eq!(opened, value); - let pair: TenantedPair = keyset.encrypt_as(&value, tenant()).await.unwrap(); - let tag: TenantTag = keyset.encrypt_as(&value, tenant()).await.unwrap(); + let pair: TenantedPair = value + .clone() + .encrypt_into_with_context(&keyset, tenant()) + .await + .unwrap(); + let tag: TenantTag = value + .clone() + .encrypt_into_with_context(&keyset, tenant()) + .await + .unwrap(); assert_eq!(pair.tag, tag, "the record's term is the target's"); - let opened: String = cipher.decrypt_as(pair, tenant()).await.unwrap(); + let opened = String::decrypt_from_with_context(pair, &cipher, tenant()) + .await + .unwrap(); assert_eq!(opened, value); - assert!(TenantedPair::plan::<String>().is_ok()); +} + +// --- Two outputs that declare the same index --------------------------------- + +/// A `plaintext` record whose two fields declare the equality index. +#[allow(dead_code)] // never built: refusing it is the point +#[derive(EncryptTarget)] +#[stash(plaintext = String)] +struct TwoEqualities { + hm: EqualityTerm, + again: EqualityTerm, +} + +/// The derive sees the fields' type names, not the indexes they declare. So +/// two fields that declare one index are refused when the record runs, +/// before any key is requested. +#[tokio::test] +async fn two_outputs_declaring_one_index_are_refused_before_any_request() { + let (cipher, generates, _) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let result: Result<TwoEqualities, _> = "x" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("x")) + .await; + assert!( + matches!( + &result, + Err(Error::Plan(PlanError::DuplicateIndex { index: "eq", .. })) + ), + "{:?}", + result.err() + ); + assert_eq!(generates.load(AtomicOrdering::SeqCst), 0); +} + +/// A `plaintext` record with an ORE term and an OPE term: two indexes that +/// answer the orderings. +#[allow(dead_code)] // never built: refusing it is the point +#[derive(EncryptTarget)] +#[stash(plaintext = u32)] +struct TwoOrders { + ore: OreTerm<u32>, + ope: OpeTerm<u32>, +} + +#[tokio::test] +async fn two_outputs_that_answer_one_operator_are_refused_before_any_request() { + let (cipher, generates, _) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let result: Result<TwoOrders, _> = 7u32 + .encrypt_into_with_context(&keyset, nonempty!("x")) + .await; + assert!( + matches!( + &result, + Err(Error::Plan(PlanError::IndexOverlap { + first: "ore", + second: "ope", + .. + })) + ), + "{:?}", + result.err() + ); + assert_eq!(generates.load(AtomicOrdering::SeqCst), 0); +} + +/// A `struct` record whose field type declares one index twice. The field's +/// type refuses it, when the record runs. +#[allow(dead_code)] // never built: refusing it is the point +#[derive(EncryptTarget)] +#[stash(struct = Named, scope = "users")] +struct TwoEqualitiesInAField { + email: Encrypted<(EqualityTerm, EqualityTerm)>, +} + +#[derive(Clone, Zeroize)] +struct Named { + email: String, +} + +#[tokio::test] +async fn a_field_declaring_one_index_twice_is_refused_before_any_request() { + let (cipher, generates, _) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let named = Named { + email: "bob@example.com".to_string(), + }; + // By value, and through a borrow: the two forms of the declaration. + let owned: Result<TwoEqualitiesInAField, _> = named.clone().encrypt_into(&keyset).await; + let borrowed: Result<TwoEqualitiesInAField, _> = keyset + .run( + <TwoEqualitiesInAField as EncryptTarget<Named>>::encryption(), + &named, + DeclaredScopes::from(DeclaredContext::default()), + ) + .await; + for result in [owned, borrowed] { + assert!( + matches!( + &result, + Err(Error::Plan(PlanError::DuplicateIndex { index: "eq", .. })) + ), + "{:?}", + result.err() + ); + } + assert_eq!(generates.load(AtomicOrdering::SeqCst), 0); } diff --git a/packages/stack-encrypt/tests/descriptor.rs b/packages/stack-encrypt/tests/descriptor.rs index f007377c99..9c58a7700b 100644 --- a/packages/stack-encrypt/tests/descriptor.rs +++ b/packages/stack-encrypt/tests/descriptor.rs @@ -10,26 +10,27 @@ mod common; use common::recording_cipher; use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::target::Encrypted; +use stack_encrypt::target::{DecryptSource, EncryptTarget, Encrypted, Scope}; use stack_encrypt::{ - nonempty, DecryptInto, Describe, Descriptor, EncryptFrom, Error, Label, NonEmpty, + nonempty, DecryptFrom, Describe, Descriptor, EncryptInto, Error, Label, NonEmpty, StackCipherText, }; +use zeroize::Zeroize; -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32)] struct EncryptedAge { c: StackCipherText, } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Zeroize)] struct User { email: String, name: String, age: u32, } -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(struct = User, scope = "users")] struct EncryptedUser { email: Encrypted<EqualityTerm>, @@ -38,6 +39,16 @@ struct EncryptedUser { age: EncryptedAge, } +/// The same record, with a scope that its caller supplies: a tenant. +#[derive(EncryptTarget, DecryptSource)] +#[stash(struct = User, scope = "users", scope(tenant: u64))] +struct TenantUser { + email: Encrypted<EqualityTerm>, + #[stash(identity = "nickname")] + name: StackCipherText, + age: EncryptedAge, +} + fn user() -> User { User { email: "alice@example.com".into(), @@ -51,12 +62,12 @@ async fn a_leaf_sends_its_context_as_the_descriptor_both_ways() -> Result<(), Er let (cipher, sent) = recording_cipher().await; let keyset = cipher.default_keyset(); - let ct: StackCipherText = keyset - .encrypt_as(&"alice", nonempty!("users").with("email").into()) - .await?; - let _: String = cipher - .decrypt_as(ct, nonempty!("users").with("email").into()) + let ct: StackCipherText = "alice" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users").with("email")) .await?; + let _: String = + String::decrypt_from_with_context(ct, &cipher, nonempty!("users").with("email")).await?; let sent = sent.lock().expect("lock").clone(); assert_eq!(sent.generated(), ["users/email"]); @@ -73,19 +84,22 @@ async fn a_label_is_sealed_under_the_descriptor_it_spells() -> Result<(), Error> // what ZeroKMS binds and logs, and the pair-form context opens it. let email = Label::parse("users/email").expect("a plain label"); assert_eq!(email.descriptor().as_str(), "users/email"); - let ct: StackCipherText = keyset - .encrypt_as(&"alice", NonEmpty::from(email.clone()).into()) - .await?; - let _: String = cipher - .decrypt_as(ct, nonempty!("users").with("email").into()) + let ct: StackCipherText = "alice" + .to_string() + .encrypt_into_with_context(&keyset, NonEmpty::from(email.clone())) .await?; + let _: String = + String::decrypt_from_with_context(ct, &cipher, nonempty!("users").with("email")).await?; // Extended by a row id like any context head. let row = NonEmpty::from(email).with(7u64); - let ct: StackCipherText = keyset.encrypt_as(&"alice", row.into()).await?; - let _: String = cipher - .decrypt_as(ct, nonempty!("users").with("email").with(7u64).into()) + let ct: StackCipherText = "alice" + .to_string() + .encrypt_into_with_context(&keyset, row) .await?; + let _: String = + String::decrypt_from_with_context(ct, &cipher, nonempty!("users").with("email").with(7u64)) + .await?; let sent = sent.lock().expect("lock").clone(); assert_eq!(sent.generated(), ["users/email", "(users/email)/7u64"]); @@ -98,10 +112,8 @@ async fn a_struct_sends_one_descriptor_per_field_context() -> Result<(), Error> let (cipher, sent) = recording_cipher().await; let keyset = cipher.default_keyset(); - let row: EncryptedUser = keyset.encrypt_as(&user(), Default::default()).await?; - let back = cipher - .decrypt_as::<User, _>(row, Default::default()) - .await?; + let row: EncryptedUser = user().encrypt_into(&keyset).await?; + let back = User::decrypt_from(row, &cipher).await?; assert_eq!(back, user()); // The inferred pair `users/email`, the pair `users/nickname` the @@ -126,8 +138,12 @@ async fn a_callers_context_extends_every_fields_descriptor() -> Result<(), Error let (cipher, sent) = recording_cipher().await; let keyset = cipher.default_keyset(); - let row: EncryptedUser = keyset.encrypt_as(&user(), 7u64.into()).await?; - let back = cipher.decrypt_as::<User, _>(row, 7u64.into()).await?; + // The record lists a scope that its caller supplies. The caller's scope + // extends the context of every field. + let row: TenantUser = user() + .encrypt_into_with_context(&keyset, Scope::new(7u64)) + .await?; + let back = User::decrypt_from_with_context(row, &cipher, Scope::new(7u64)).await?; assert_eq!(back, user()); // The extended contexts are composites, rendered part by part — the @@ -160,12 +176,12 @@ async fn every_leaf_of_a_tree_shares_the_root_descriptor() -> Result<(), Error> let (cipher, sent) = recording_cipher().await; let keyset = cipher.default_keyset(); - let column: Vec<StackCipherText> = keyset - .encrypt_as(&vec![1u32, 2, 3], nonempty!("users").with("age").into()) - .await?; - let _: Vec<u32> = cipher - .decrypt_as(column, nonempty!("users").with("age").into()) + let column: Vec<StackCipherText> = vec![1u32, 2, 3] + .encrypt_into_with_context(&keyset, nonempty!("users").with("age")) .await?; + let _: Vec<u32> = + Vec::<u32>::decrypt_from_with_context(column, &cipher, nonempty!("users").with("age")) + .await?; // Per-element AAD derivation is vitaminc's and stays inside the AEAD; // ZeroKMS sees the field, not the element. @@ -223,8 +239,9 @@ async fn a_column_renders_an_over_long_context_once() -> Result<(), Error> { let context = stack_encrypt::NonEmpty::new(Counted(renders.clone())).unwrap(); let values: Vec<u32> = (0..10_000).collect(); - let result: Result<Vec<StackCipherText>, Error> = - keyset.encrypt_as(&values, context.clone().into()).await; + let result: Result<Vec<StackCipherText>, Error> = values + .encrypt_into_with_context(&keyset, context.clone()) + .await; assert!( matches!(result, Err(Error::DescriptorTooLong { .. })), "{result:?}" @@ -235,10 +252,11 @@ async fn a_column_renders_an_over_long_context_once() -> Result<(), Error> { "one rendering on encrypt" ); - let column: Vec<StackCipherText> = keyset - .encrypt_as(&vec![1u32, 2, 3], nonempty!("users").with("age").into()) + let column: Vec<StackCipherText> = vec![1u32, 2, 3] + .encrypt_into_with_context(&keyset, nonempty!("users").with("age")) .await?; - let opened: Result<Vec<u32>, Error> = cipher.decrypt_as(column, context.into()).await; + let opened: Result<Vec<u32>, Error> = + Vec::<u32>::decrypt_from_with_context(column, &cipher, context).await; assert!( matches!(opened, Err(Error::DescriptorTooLong { .. })), "{opened:?}" @@ -269,13 +287,17 @@ async fn a_column_with_nothing_to_bind_takes_any_context() -> Result<(), Error> let long = stack_encrypt::NonEmpty::new("a".repeat(Descriptor::MAX_LEN + 1)).unwrap(); let names = vec!["alice".to_string(), "bob".to_string()]; - let terms: Vec<EqualityTerm> = keyset.encrypt_as(&names, long.clone().into()).await?; + let terms: Vec<EqualityTerm> = names + .encrypt_into_with_context(&keyset, long.clone()) + .await?; assert_eq!(terms.len(), 2); let none: Vec<u32> = Vec::new(); - let sealed: Vec<StackCipherText> = keyset.encrypt_as(&none, long.clone().into()).await?; + let sealed: Vec<StackCipherText> = none + .encrypt_into_with_context(&keyset, long.clone()) + .await?; assert!(sealed.is_empty()); - let opened: Vec<u32> = cipher.decrypt_as(sealed, long.into()).await?; + let opened: Vec<u32> = Vec::<u32>::decrypt_from_with_context(sealed, &cipher, long).await?; assert!(opened.is_empty()); let sent = sent.lock().expect("lock").clone(); @@ -293,17 +315,18 @@ async fn an_over_long_context_is_refused_before_any_request_on_either_path() -> let long = stack_encrypt::NonEmpty::new("a".repeat(Descriptor::MAX_LEN + 1)).unwrap(); let values: Vec<u32> = (0..10_000).collect(); - let result: Result<Vec<StackCipherText>, Error> = - keyset.encrypt_as(&values, long.clone().into()).await; + let result: Result<Vec<StackCipherText>, Error> = values + .encrypt_into_with_context(&keyset, long.clone()) + .await; assert!( matches!(result, Err(Error::DescriptorTooLong { len }) if len == Descriptor::MAX_LEN + 1), "{result:?}" ); - let sealed: StackCipherText = keyset - .encrypt_as(&7u32, nonempty!("users").with("age").into()) + let sealed: StackCipherText = 7u32 + .encrypt_into_with_context(&keyset, nonempty!("users").with("age")) .await?; - let opened: Result<u32, Error> = cipher.decrypt_as(sealed, long.into()).await; + let opened: Result<u32, Error> = u32::decrypt_from_with_context(sealed, &cipher, long).await; assert!( matches!(opened, Err(Error::DescriptorTooLong { .. })), "{opened:?}" diff --git a/packages/stack-encrypt/tests/fixtures/README.md b/packages/stack-encrypt/tests/fixtures/README.md index 038d5f8b9c..42bba91785 100644 --- a/packages/stack-encrypt/tests/fixtures/README.md +++ b/packages/stack-encrypt/tests/fixtures/README.md @@ -1,18 +1,20 @@ # Record fixture: `record_lowering.json` -One declaration, two authors, one engine. A Rust chain -(`cipher.encrypt(&user).context("users").fields()…`, over fields of type +One declaration, two authors, one engine. Typed Rust code (the engine's +combinators, `indexed(..).under(label)` and the rest, run over fields of type `stack_encrypt::dynamic::ValueFrame`, the frame a binding's values arrive as) and the data-plan lowering (`stack_encrypt::dynamic::record`, what a language binding calls) each sealed the same plaintext under the same -declaration. The fixture holds both records, with their term bytes, sealed +declaration. The fixture's key for the first author is `typed_chain`: it was +first written by the plan chain, which is removed, and the combinators the +chain lowered to open and reproduce the same record. The fixture holds both records, with their term bytes, sealed under a deterministic key source so they open in any process built from the same seed. `tests/record_lowering.rs` opens each record with the other author and checks that both derive the terms it holds. That cross-opening -is the proof that `dynamic::record` is a lowering into the plan builder and +is the proof that `dynamic::record` is a lowering into the combinators and not a second executor (ADR-0007, amended 2026-10-06). A Go test reads the -same file later, once the Go SDK's generated code is the third author. A -chain over bare `u32` or `String` fields seals the same leaves too: every +same file later, once the Go SDK's generated code is the third author. +Typed code over bare `u32` or `String` fields seals the same leaves too: every leaf is tagged with its type (vitaminc ADR 0001), whoever wrote it. Regenerate with `STACK_ENCRYPT_UPDATE_FIXTURES=1 cargo test -p @@ -46,9 +48,8 @@ by hand. ``` - **`context_field`.** A second case under the same key source and keyset: - a plan that takes its context from a field of the record (the Rust - chain's `FieldsBuilder::context_field`, the data grammar's plan-level - `"context_field"` key). Its `plan` names the context field, each field's + a plan that takes its context from a field of the record (the data + grammar's plan-level `"context_field"` key). Its `plan` names the context field, each field's `context` is its identity alone, and the context field's node in each record is `"passthrough"` carrying the label the record was sealed under (`"tenants/acme"`); the sealed field's term is derived under @@ -70,9 +71,9 @@ by hand. - **`plan`.** The declaration in the data grammar `dynamic::record::plan` parses: per field its label (a list of plain segments), its outputs (`"c"`, `"passthrough"`, or an index key `"eq"`, `"match"`, `"ore"`, `"ope"`) and its - `"type"` (a vitaminc `ValueKind` name). The Rust chain writes the same - declaration as `Plan::context("users").fields()` with one verb per field, - its indexes named as `IndexSpec`s over `Value` fields. + `"type"` (a vitaminc `ValueKind` name). The typed author writes the same + declaration as one combinator per field under the label `users/<field>`, + its indexes named as `IndexSpec`s over `ValueFrame` fields. - **`plaintext`.** The value both authors sealed, each field as JSON at the kind `plan` declares for it. - **`records`.** Each record in the stored shape, field by field, output by @@ -91,8 +92,8 @@ for a plain label segment, read by the Rust, derive and Go tests. Queries named by operator through the data path (`stack_encrypt::dynamic::query_term`, what a language binding calls), each -term equal to the one the typed query chain (`cipher.query(op::Lt, -&40u32).on(&plan)`) derives for the same query, with a page `verify` +term equal to the one a typed query from a field marker +(`EncryptedUser::AGE.lt(40u32)`) derives for the same query, with a page `verify` answered and fetch limits `target_limit` sized (ADR-0009; ADR-0007, amended 2026-10-16). `tests/query_operators.rs` derives every entry and checks it against the typed path; the Go SDK's `encrypt` tests derive, verify and size diff --git a/packages/stack-encrypt/tests/frozen_bytes.rs b/packages/stack-encrypt/tests/frozen_bytes.rs index 6657196cef..9a22b5656b 100644 --- a/packages/stack-encrypt/tests/frozen_bytes.rs +++ b/packages/stack-encrypt/tests/frozen_bytes.rs @@ -26,7 +26,7 @@ use stack_encrypt::sem::{ DefaultMatch, EqualityTerm, MatchTerms, OpeTerm, OreTerm, TermBytesError, }; -use stack_encrypt::{CipherText, Error, LeafBytesError, SealedValue, StackCipher}; +use stack_encrypt::{CipherText, EncryptInto, Error, LeafBytesError, SealedValue, StackCipher}; use stack_kms::{ DataKey, DataKeySource, DataKeyWithTag, FakeDataKeySource, GenerateKeyPayload, IdentifiedBy, IndexKey, IndexKeySource, RetrieveKeyPayload, UnverifiedContext, @@ -459,8 +459,8 @@ fn match_term_from_bytes_rejects_positions_outside_the_filter() { async fn ore_term_encoding_is_the_raw_cllw_bytes() { let cipher = cipher().await; let keyset = cipher.default_keyset(); - let term: OreTerm<u32> = keyset - .encrypt_as(&42u32, nonempty!("users/age").into()) + let term: OreTerm<u32> = 42u32 + .encrypt_into_with_context(&keyset, nonempty!("users/age")) .await .expect("ore term"); @@ -489,8 +489,8 @@ async fn ore_term_encoding_is_the_raw_cllw_bytes() { async fn ope_term_encoding_is_the_raw_cllw_bytes() { let cipher = cipher().await; let keyset = cipher.default_keyset(); - let term: OpeTerm<u32> = keyset - .encrypt_as(&42u32, nonempty!("users/age").into()) + let term: OpeTerm<u32> = 42u32 + .encrypt_into_with_context(&keyset, nonempty!("users/age")) .await .expect("ope term"); @@ -535,8 +535,9 @@ async fn variable_length_ore_and_ope_terms_decode() { // byte. The decode path is the length-validating TryFrom in clww-ore. let cipher = cipher().await; let keyset = cipher.default_keyset(); - let ore: OreTerm<String> = keyset - .encrypt_as(&"alice".to_string(), nonempty!("users/name").into()) + let ore: OreTerm<String> = "alice" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/name")) .await .expect("ore term"); assert_eq!(ore.as_bytes().len(), 6 * 8); @@ -549,8 +550,9 @@ async fn variable_length_ore_and_ope_terms_decode() { Err(TermBytesError::MalformedCllwCiphertext(6 * 8 - 1)) ); - let ope: OpeTerm<String> = keyset - .encrypt_as(&"alice".to_string(), nonempty!("users/name").into()) + let ope: OpeTerm<String> = "alice" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/name")) .await .expect("ope term"); assert_eq!(ope.as_bytes().len(), 6 * 8 + 1); diff --git a/packages/stack-encrypt/tests/heap_hygiene.rs b/packages/stack-encrypt/tests/heap_hygiene.rs index 8f292fb51a..132df22a98 100644 --- a/packages/stack-encrypt/tests/heap_hygiene.rs +++ b/packages/stack-encrypt/tests/heap_hygiene.rs @@ -4,10 +4,11 @@ //! //! The JSON index's tests ([`json_index`]) cover sealing under either root //! option, opening a document whole, a subtree read, an extraction and the -//! containment and equality needles. One thing they leave out: the path a -//! caller hands `AtPath` as an owned `String` is cloned by the query -//! builder and dropped by the plan chain unwiped, since the chain only knows -//! `P: AsRef<str>`; the test passes a `&'static str`. +//! containment and equality needles. A needle is derived as a binding +//! derives it, through a data plan: the needle's text and the path of an +//! equality are the input's, and both carry the marker. The engine parses +//! them into a document and a path of its own and wipes each, so the path +//! is held to the rule as the needle is. //! //! This binary installs its own global allocator: a spy that, while a test //! watches its own thread, scans every block freed on that thread for a @@ -563,28 +564,57 @@ fn a_query_term_leaves_no_plaintext_on_the_heap() { } } -/// The typed chain prepares an equality query before any key is requested -/// by normalizing a copy of the value (`Supports<Eq, S>` is `S: Clone`). -/// The copy is wiped as it is read, so nothing of the query reaches freed -/// memory. A typed `Matches` query is not watched here: `matching` views -/// its owned text and drops it unwiped, which #1593 tracks. +/// A record with one field that answers equality, for the marker query +/// below: `EncryptedAccount::EMAIL`, under `users/email`. +#[derive(Clone, zeroize::Zeroize)] +struct Account { + email: String, +} + +#[derive(stack_encrypt::target::EncryptTarget)] +#[stash(struct = Account, scope = "users")] +struct EncryptedAccount { + email: stack_encrypt::Encrypted<stack_encrypt::sem::EqualityTerm>, +} + +/// A query from a field's marker copies the value it is handed by +/// reference and owns the copy. The equality index prepares the query +/// before any key is requested, by normalizing a copy of its own, and then +/// derives the term from the marker's. Each copy is wiped, so nothing of +/// the query reaches freed memory. A `matches` query from a marker is not +/// watched here: `matching` views the copy the marker hands it and drops +/// it unwiped, which is a known gap in the engine. #[test] fn a_typed_query_leaves_no_plaintext_on_the_heap() { - use stack_encrypt::{op, Equality, Plan}; + use stack_encrypt::EncryptInto; let rt = runtime(); let cipher = rt.block_on(common::stack_cipher()); - let plan = Plan::context("users/email") - .with::<String, _>(Equality) - .build() + let keyset = cipher.default_keyset(); + // A stored record, sealed before the watch, to hold the query to. + let account = Account { + email: marked("query"), + }; + let stored: EncryptedAccount = rt + .block_on(async { account.encrypt_into(&keyset).await }) .unwrap(); - // The caller's own value stays alive across the watch: the chain works + // The caller's own value stays alive across the watch: the marker works // on its copy, and what the test owns is the test's residue, not the // engine's. let query = marked("query"); - watched("typed query (Eq)", || { - rt.block_on(async { cipher.query(op::Eq, &query).on(&plan).await.unwrap() }); + let term = watched("marker query (Eq)", || { + rt.block_on(async { + EncryptedAccount::EMAIL + .eq(&query) + .encrypt(&keyset) + .await + .unwrap() + }) }); drop(query); + assert_eq!( + term, stored.email.terms, + "the watched query derived the term the field stores" + ); } /// An order term takes its plaintext by value, so the engine owns the @@ -1606,10 +1636,10 @@ mod json_index { use serde_json::{json, Value}; use stack_encrypt::json::{ - JsonDocument, JsonNode, JsonOptions, JsonPath, JsonQuery, JsonSubtree, + JsonDocument, JsonNode, JsonOptions, JsonPath, JsonPlaintext, JsonQuery, }; use stack_encrypt::target::{json_document, AeadContext, Borrowed, CallerContext}; - use stack_encrypt::{op, Plan, StackCipher}; + use stack_encrypt::{DecryptFrom, StackCipher}; use stack_kms::{ DataKey, DataKeySource, DataKeyWithTag, DeterministicSource, GenerateKeyPayload, IdentifiedBy, IndexKey, IndexKeySource, RetrieveKeyPayload, UnverifiedContext, @@ -1741,10 +1771,14 @@ mod json_index { let value = document(); for sealed_root in [true, false] { let doc = sealed(&rt, &cipher, &value, sealed_root); - let opened: Value = watched(&format!("open (sealed_root: {sealed_root})"), || { - rt.block_on(async { cipher.decrypt_as(doc.clone(), aead()).await.unwrap() }) + let opened = watched(&format!("open (sealed_root: {sealed_root})"), || { + rt.block_on(async { + JsonPlaintext::decrypt_from_with_context(doc.clone(), &cipher, aead()) + .await + .unwrap() + }) }); - assert_eq!(opened, value); + assert_eq!(opened.value(), &value); } *KEY.lock().unwrap() = None; } @@ -1780,18 +1814,17 @@ mod json_index { }) .collect(); let read = key.open(rows); - let opened: Value = watched( + let opened = watched( &format!("subtree read (sealed_root: {sealed_root})"), || { rt.block_on(async { - cipher - .decrypt_as::<Value, JsonSubtree>(read, aead()) + JsonPlaintext::decrypt_from_with_context(read, &cipher, aead()) .await .unwrap() }) }, ); - assert_eq!(opened, json!(marked("short"))); + assert_eq!(opened.value(), &json!(marked("short"))); let selector = rt .block_on(async { keyset @@ -1801,15 +1834,14 @@ mod json_index { .unwrap(); let (j, _) = doc.find(&key.token_bytes()[0]).unwrap(); let node = selector.open(doc.node(j).unwrap()); - let opened: Value = watched(&format!("-> (sealed_root: {sealed_root})"), || { + let opened = watched(&format!("-> (sealed_root: {sealed_root})"), || { rt.block_on(async { - cipher - .decrypt_as::<Value, stack_encrypt::json::JsonExtracted>(node, aead()) + JsonPlaintext::decrypt_from_with_context(node, &cipher, aead()) .await .unwrap() }) }); - assert_eq!(opened, json!(marked("short"))); + assert_eq!(opened.value(), &json!(marked("short"))); } *KEY.lock().unwrap() = None; } @@ -1818,32 +1850,53 @@ mod json_index { fn a_containment_needle_leaves_no_plaintext_on_the_heap() { let rt = runtime(); let cipher = rt.block_on(cipher()); - let plan = Plan::value::<Value>() - .context("users/profile") - .encrypt_into::<JsonDocument>() - .build() - .unwrap(); - let needle = document(); - let query: JsonQuery = watched("Contains needle", || { - rt.block_on(async { cipher.query(op::Contains, &needle).on(&plan).await.unwrap() }) - }); - drop(query); - // The path is a `&'static str`: an owned path `String` is cloned by - // the query builder and dropped by the plan chain, and the chain's - // `P: AsRef<str>` cannot wipe it (see the module's JSON notes). - let path: &'static str = - Box::leak(format!("$[\"{}\"].inner", marked("key")).into_boxed_str()); - let at = (path, marked("short")); - let query: JsonQuery = watched("AtPath(Eq) needle", || { - rt.block_on(async { - cipher - .query(op::AtPath(op::Eq), &at) - .on(&plan) - .await - .unwrap() - }) + let keyset = cipher.default_keyset(); + // The field as a binding's plan names it, under the index's default + // options. + let wire = json!({ + "profile": {"context": ["users", "profile"], "type": "json", "null": "sql", + "target": "JsonSearch"} }); - drop(query); + let plan = record::plan_with( + stack_encrypt::dynamic::frame::serde::to_vec(&wire) + .unwrap() + .as_protected_ref(), + &Documents, + ) + .unwrap(); + // The needle's text, the path and the value at it are the test's + // input, built and framed before the watch and alive across it. The + // path names the marked key, in quotes, and is an owned string. + let needle = document().to_string(); + let path = format!("$[\"{}\"].inner", marked("key")); + let short = json!(marked("short")).to_string(); + let inputs = [ + (Op::Contains, object(vec![("json", frame(needle.as_str()))])), + ( + Op::EqAt, + object(vec![ + ("path", frame(path.as_str())), + ("json", frame(short.as_str())), + ]), + ), + ]; + for (operator, input) in &inputs { + let term = watched(&format!("{operator:?} needle"), || { + rt.block_on(async { + query_term(&keyset, &plan, "profile", *operator, input, &Documents) + .expect("a document operator") + .await + .expect("its needle") + }) + }); + // The operand is the needle the database is sent. + let stack_encrypt::dynamic::Operand::One(operand) = term.operand() else { + panic!("{operator:?}: one operand") + }; + let query: JsonQuery = serde_json::from_slice(operand).expect("a needle"); + drop(query); + } + drop((needle, path, short)); } /// A document field's operators through the data plan diff --git a/packages/stack-encrypt/tests/index.rs b/packages/stack-encrypt/tests/index.rs index 60521e4b4e..61019f17ef 100644 --- a/packages/stack-encrypt/tests/index.rs +++ b/packages/stack-encrypt/tests/index.rs @@ -1,6 +1,7 @@ -//! The engine pieces the plan builder lowers onto: indexes as types -//! (`Index`, `Indexes`, `indexed`, `Encrypted<Terms>`), `passthrough`, and -//! running a description held in a variable (`run`, `run_decryption`). +//! The engine pieces that the targets and the data plans are composed of: +//! indexes as types (`Index`, `Indexes`, `indexed`, `Encrypted<Terms>`), +//! `passthrough`, and running a description held in a variable (`run`, +//! `run_decryption`). //! //! The load-bearing claim is byte identity: `indexed` is the hand //! composition `ciphertext().accepting().zip(..)` and what the derive emits, @@ -17,10 +18,10 @@ use stack_encrypt::sem::{ }; use stack_encrypt::target::{ ciphertext, equality, indexed, matching, ope, ore, passthrough, AeadContext, Borrowed, - CallerContext, Decryption, Encrypted, Encryption, Equality, Index, IndexSpec, Indexes, Match, - Ope, Ore, Owned, + CallerContext, DecryptSource, Decryption, EncryptTarget, Encrypted, Encryption, Equality, + Index, IndexSpec, Indexes, Match, Ope, Ore, Owned, }; -use stack_encrypt::{nonempty, DecryptInto, EncryptFrom, Error, StackCipherText}; +use stack_encrypt::{nonempty, DecryptFrom, EncryptInto, Error, StackCipherText}; use stack_kms::{FakeDataKeySource, IdentifiedBy}; use vitaminc_protected::{Controlled, Protected}; @@ -114,7 +115,7 @@ fn aead() -> AeadContext { /// What the derive emits today for a field with equality and ORE beside its /// ciphertext: the struct `indexed::<String>((Equality, Ore))` replaces. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String)] struct DerivedEmail { c: StackCipherText, @@ -146,7 +147,11 @@ mod given_equality_and_ore_over_a_string { .zip(ore::<String, _, Borrowed>()); let ((hand_c, hand_eq), hand_ore) = keyset.run(hand, &email, caller()).await.expect("hand"); - let derived: DerivedEmail = keyset.encrypt_as(&email, caller()).await.expect("derived"); + let derived: DerivedEmail = email + .clone() + .encrypt_into_with_context(&keyset, caller()) + .await + .expect("derived"); let (eq, ore) = out.terms; assert_eq!(eq, hand_eq, "equality term matches the hand composition"); @@ -162,24 +167,22 @@ mod given_equality_and_ore_over_a_string { hm: eq, ob: ore, }; - let opened: String = cipher - .decrypt_as(as_derived, caller()) + let opened: String = String::decrypt_from_with_context(as_derived, &cipher, caller()) .await .expect("the derive's reader opens indexed's ciphertext"); assert_eq!(opened, email); - let opened: String = cipher - .decrypt_as( - Encrypted { - ciphertext: derived.c, - terms: (), - }, - aead(), - ) - .await - .expect("Encrypted's reader opens the derive's ciphertext"); + let opened: String = String::decrypt_from_with_context( + Encrypted { + ciphertext: derived.c, + terms: (), + }, + &cipher, + aead(), + ) + .await + .expect("Encrypted's reader opens the derive's ciphertext"); assert_eq!(opened, email); - let opened: String = keyset - .decrypt_as(hand_c, aead()) + let opened: String = String::decrypt_from_with_context(hand_c, &keyset, aead()) .await .expect("the hand composition's ciphertext opens"); assert_eq!(opened, email); @@ -213,7 +216,9 @@ mod given_equality_and_ore_over_a_string { owned.terms, borrowed.terms, "the mode does not change a term" ); - let opened: String = cipher.decrypt_as(owned, aead()).await.expect("open"); + let opened: String = String::decrypt_from_with_context(owned, &cipher, aead()) + .await + .expect("open"); assert_eq!(opened, email); } } @@ -390,7 +395,9 @@ mod given_a_set_of_indexes { .unwrap(); let (m, o, p, e) = four.terms; assert_eq!((m, o, p, e), (words, ore_t, ope_t, eq)); - let opened: String = cipher.decrypt_as(four.ciphertext, aead()).await.unwrap(); + let opened: String = String::decrypt_from_with_context(four.ciphertext, &cipher, aead()) + .await + .unwrap(); assert_eq!(opened, text); } @@ -539,7 +546,9 @@ mod given_a_passthrough { 1, "one batch, for the ciphertext" ); - let opened: String = keyset.decrypt_as(sealed, aead()).await.expect("open"); + let opened: String = String::decrypt_from_with_context(sealed, &keyset, aead()) + .await + .expect("open"); assert_eq!(opened, value); } } @@ -551,12 +560,14 @@ mod given_a_decryption_held_in_a_variable { async fn run_decryption_opens_it_through_a_keyset_or_the_client() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let sealed: StackCipherText = keyset - .encrypt_as(&"secret".to_string(), aead()) + let sealed: StackCipherText = "secret" + .to_string() + .encrypt_into_with_context(&keyset, aead()) .await .unwrap(); - let again: StackCipherText = keyset - .encrypt_as(&"secret".to_string(), aead()) + let again: StackCipherText = "secret" + .to_string() + .encrypt_into_with_context(&keyset, aead()) .await .unwrap(); @@ -579,12 +590,14 @@ mod given_a_decryption_held_in_a_variable { .keyset(IdentifiedBy::Name("globex".to_string().into())) .await .unwrap(); - let sealed: StackCipherText = acme - .encrypt_as(&"secret".to_string(), aead()) + let sealed: StackCipherText = "secret" + .to_string() + .encrypt_into_with_context(&acme, aead()) .await .unwrap(); - let again: StackCipherText = acme - .encrypt_as(&"secret".to_string(), aead()) + let again: StackCipherText = "secret" + .to_string() + .encrypt_into_with_context(&acme, aead()) .await .unwrap(); @@ -629,6 +642,7 @@ mod given_encrypted_terms_as_a_target { where S: stack_encrypt::Encrypt + stack_encrypt::Decrypt + + zeroize::Zeroize + Send + Clone + PartialEq @@ -640,7 +654,11 @@ mod given_encrypted_terms_as_a_target { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let typed: Encrypted<Terms> = keyset.encrypt_as(&value, caller()).await.expect("typed"); + let typed: Encrypted<Terms> = value + .clone() + .encrypt_into_with_context(&keyset, caller()) + .await + .expect("typed"); let data = keyset .run(indexed::<S, _, Borrowed, _>(indexes), &value, caller()) .await @@ -650,18 +668,20 @@ mod given_encrypted_terms_as_a_target { "the typed and data spellings agree" ); - let opened: S = cipher - .decrypt_as( - Encrypted { - ciphertext: typed.ciphertext, - terms: (), - }, - aead(), - ) - .await - .expect("the typed ciphertext opens"); + let opened: S = S::decrypt_from_with_context( + Encrypted { + ciphertext: typed.ciphertext, + terms: (), + }, + &cipher, + aead(), + ) + .await + .expect("the typed ciphertext opens"); assert_eq!(opened, value); - let opened: S = cipher.decrypt_as(data, aead()).await.expect("data opens"); + let opened: S = S::decrypt_from_with_context(data, &cipher, aead()) + .await + .expect("data opens"); assert_eq!(opened, value); } @@ -675,40 +695,23 @@ mod given_encrypted_terms_as_a_target { same_bytes::<u32, OpeTerm<u32>, _>(7, Ope).await; } + /// A set holds at most one index for each operator, so the largest set + /// over text is three: equality, match and one order index. A set that + /// names an index twice, or two order indexes, is refused + /// (`combinators.rs`). #[tokio::test] async fn a_tuple_of_terms_lowers_to_the_tuple_of_their_indexes_in_order() { same_bytes::<u32, (EqualityTerm, OreTerm<u32>), _>(7, (Equality, Ore)).await; same_bytes::<u32, (OreTerm<u32>, EqualityTerm), _>(7, (Ore, Equality)).await; - same_bytes::<u32, (OpeTerm<u32>, OreTerm<u32>, EqualityTerm), _>(7, (Ope, Ore, Equality)) - .await; - same_bytes::< - String, - ( - MatchTerms<Words>, - OpeTerm<String>, - OreTerm<String>, - EqualityTerm, - ), - _, - >( + same_bytes::<u32, (OpeTerm<u32>, EqualityTerm), _>(7, (Ope, Equality)).await; + same_bytes::<String, (MatchTerms<Words>, OpeTerm<String>, EqualityTerm), _>( "Hello World".into(), - (Match::<Words>::new(), Ope, Ore, Equality), + (Match::<Words>::new(), Ope, Equality), ) .await; - // Five: every index once, and a second match index under other options. - same_bytes::< - String, - ( - EqualityTerm, - MatchTerms, - OreTerm<String>, - OpeTerm<String>, - MatchTerms<Words>, - ), - _, - >( + same_bytes::<String, (EqualityTerm, MatchTerms, OreTerm<String>), _>( "Hello World".into(), - (Equality, Match, Ore, Ope, Match::<Words>::new()), + (Equality, Match, Ore), ) .await; } @@ -719,8 +722,11 @@ mod given_encrypted_terms_as_a_target { let keyset = cipher.default_keyset(); let email = "bob@example.com".to_string(); - let typed: Encrypted<(EqualityTerm, MatchTerms)> = - keyset.encrypt_as(&email, caller()).await.expect("typed"); + let typed: Encrypted<(EqualityTerm, MatchTerms)> = email + .clone() + .encrypt_into_with_context(&keyset, caller()) + .await + .expect("typed"); let hand = ciphertext::<String, _, Borrowed>() .accepting::<CallerContext>() .zip(equality::<String, _, Borrowed>()) @@ -728,9 +734,13 @@ mod given_encrypted_terms_as_a_target { let ((hand_c, hand_eq), hand_match) = keyset.run(hand, &email, caller()).await.expect("hand"); assert_eq!(typed.terms, (hand_eq, hand_match)); - let opened: String = keyset.decrypt_as(hand_c, aead()).await.expect("hand opens"); + let opened: String = String::decrypt_from_with_context(hand_c, &keyset, aead()) + .await + .expect("hand opens"); assert_eq!(opened, email); - let opened: String = cipher.decrypt_as(typed, aead()).await.expect("typed opens"); + let opened: String = String::decrypt_from_with_context(typed, &cipher, aead()) + .await + .expect("typed opens"); assert_eq!(opened, email); } @@ -759,15 +769,15 @@ mod given_encrypted_terms_as_a_target { /// (`dynamic::admits`): refused when the term is derived, before any key /// is minted. (It has no order term either, which is a compile error: /// `bool` is not `sem::Orderable`, so `OreTerm<bool>`, `OpeTerm<bool>` and -/// a plan `.with::<bool, _>(Ore)` do not build.) +/// `indexed::<bool, _, _, _>(Ore)` do not build.) #[tokio::test] async fn a_boolean_has_no_equality_term() { use stack_encrypt::sem::TermError; let (cipher, generates, _) = counting_cipher().await; let keyset = cipher.default_keyset(); - let equality_term: Result<EqualityTerm, _> = keyset - .encrypt_as(&true, nonempty!("users/flag").into()) + let equality_term: Result<EqualityTerm, _> = true + .encrypt_into_with_context(&keyset, nonempty!("users/flag")) .await; assert!(matches!( equality_term, diff --git a/packages/stack-encrypt/tests/json_index.rs b/packages/stack-encrypt/tests/json_index.rs index eb4278fe24..c5a5284af4 100644 --- a/packages/stack-encrypt/tests/json_index.rs +++ b/packages/stack-encrypt/tests/json_index.rs @@ -8,6 +8,16 @@ //! rules PostgreSQL documents (the live oracle against PostgreSQL itself is //! `packages/eql/tests/encryption/tests/json_tree.rs`). //! +//! A document is sealed from a [`JsonPlaintext`], or by the +//! [`json_document`](stack_encrypt::target::json_document) operation where +//! a test sets the index's options, and every stored form opens to a +//! `JsonPlaintext`. A query is derived through a data plan (the `query` +//! module): the plan's one field is the column the documents are sealed +//! under, with the options they are sealed with, and the operand it derives +//! is read back into the typed [`JsonQuery`] or `JsonLeafQuery`, so the +//! checks here run over the bytes a binding sends. The data plan is behind +//! the `dynamic` feature, and so is every test that derives a query. +//! //! Each `given_<scenario>` module is one situation, and each test in it is //! named for what it expects. Shared setup is [`scenario`] and the helpers //! below it; repeated checks are in [`assertions`]. The generated cases are @@ -21,16 +31,17 @@ mod common; use common::stack_cipher; use serde_json::{json, Map, Value}; +#[cfg(feature = "dynamic")] +use stack_encrypt::json::JsonQuery; use stack_encrypt::json::{ - JsonDocument, JsonExtracted, JsonNode, JsonOptions, JsonPath, JsonQuery, JsonSelector, + JsonDocument, JsonExtracted, JsonNode, JsonOptions, JsonPath, JsonPlaintext, JsonSelector, JsonSubtree, JsonSubtreeKey, Tag, }; -use stack_encrypt::op; use stack_encrypt::sem::TermError; use stack_encrypt::target::{ AeadContext, CallerContext, DecryptField, Decryption, IndexSpec, Json, }; -use stack_encrypt::{nonempty, Error, Plan, PlanError, StackCipher}; +use stack_encrypt::{nonempty, DecryptFrom, EncryptInto, Error, StackCipher}; use stack_kms::FakeDataKeySource; // ============================================================================= @@ -39,21 +50,19 @@ use stack_kms::FakeDataKeySource; type Cipher = StackCipher<FakeDataKeySource>; -type JsonPlan = stack_encrypt::plan::ValuePlan<Value, stack_encrypt::plan::Typed<JsonDocument>>; - -type OrderedPlan = - stack_encrypt::plan::ValuePlan<Value, stack_encrypt::plan::Laid<Json, JsonDocument>>; - -/// What most tests start from: a cipher over the fake key source and the -/// default plan for a JSON column under `users/profile`. +/// What most tests start from: a cipher over the fake key source and, for +/// the tests that derive a query, the data plan of a JSON column under +/// `users/profile` with the default options. struct Scenario { cipher: Cipher, - plan: JsonPlan, + #[cfg(feature = "dynamic")] + plan: query::Plan, } async fn scenario() -> Scenario { Scenario { cipher: stack_cipher().await, + #[cfg(feature = "dynamic")] plan: plan(), } } @@ -66,22 +75,23 @@ fn aead() -> AeadContext { AeadContext::from(nonempty!("users").with("profile")) } -fn plan() -> JsonPlan { - Plan::value::<Value>() - .context("users/profile") - .encrypt_into::<JsonDocument>() - .build() - .unwrap() +/// The column's data plan under the default options, the ones [`seal`] +/// seals with. +#[cfg(feature = "dynamic")] +fn plan() -> query::Plan { + query::plan(&JsonOptions::default()) } -/// The plan laid out by the JSON index with `$.hello`, `$.number` and -/// `$.flag` ordered. -fn ordered_plan() -> OrderedPlan { - Plan::value::<Value>() - .context("users/profile") - .encrypt_with_layout(Json::new(ordering(&["$.hello", "$.number", "$.flag"]))) - .build() - .unwrap() +/// The options with `$.hello`, `$.number` and `$.flag` ordered. +fn ordered() -> JsonOptions { + ordering(&["$.hello", "$.number", "$.flag"]) +} + +/// The column's data plan under [`ordered`]: it answers queries on +/// documents sealed with those options. +#[cfg(feature = "dynamic")] +fn ordered_plan() -> query::Plan { + query::plan(&ordered()) } /// The options with the root open. @@ -114,11 +124,10 @@ fn nodes(value: &Value) -> usize { } } -/// Seal through the typed target, as `encrypt_as` does. +/// Seal a [`JsonPlaintext`] into a document, under the default options. async fn seal(cipher: &Cipher, value: &Value) -> JsonDocument { - cipher - .default_keyset() - .encrypt_as::<Value, JsonDocument>(value, context()) + JsonPlaintext::from(value.clone()) + .encrypt_into_with_context(&cipher.default_keyset(), context()) .await .expect("seals") } @@ -145,8 +154,55 @@ async fn try_seal( .await } +/// The document a plaintext holds. An opened plaintext is wiped when it is +/// dropped and is not compared directly, so the helpers below hand a test +/// the document itself. +fn held(opened: JsonPlaintext) -> Value { + opened.value().clone() +} + +/// A stored document, opened under the suite's column. async fn open(cipher: &Cipher, doc: JsonDocument) -> Result<Value, Error> { - cipher.decrypt_as::<Value, JsonDocument>(doc, aead()).await + open_under(cipher, doc, aead()).await +} + +/// A stored document, opened under `context`. +async fn open_under( + cipher: &Cipher, + doc: JsonDocument, + context: AeadContext, +) -> Result<Value, Error> { + JsonPlaintext::decrypt_from_with_context(doc, cipher, context) + .await + .map(held) +} + +/// The entry `->` returned, opened to its value. +async fn open_extracted(cipher: &Cipher, entry: JsonExtracted) -> Result<Value, Error> { + JsonPlaintext::decrypt_from_with_context(entry, cipher, aead()) + .await + .map(held) +} + +/// The rows of a read, opened as the one node at its path. +async fn open_one(cipher: &Cipher, rows: JsonSubtree) -> Result<Value, Error> { + JsonPlaintext::decrypt_from_with_context(rows, cipher, aead()) + .await + .map(held) +} + +/// The rows of a read, opened as the node at its path, or none. +async fn open_option(cipher: &Cipher, rows: JsonSubtree) -> Result<Option<Value>, Error> { + Option::<JsonPlaintext>::decrypt_from_with_context(rows, cipher, aead()) + .await + .map(|opened| opened.map(held)) +} + +/// The rows of a read, opened as a list: one value per node its path names. +async fn open_list(cipher: &Cipher, rows: JsonSubtree) -> Result<Vec<Value>, Error> { + Vec::<JsonPlaintext>::decrypt_from_with_context(rows, cipher, aead()) + .await + .map(|opened| opened.into_iter().map(held).collect()) } async fn subtree_key( @@ -219,6 +275,7 @@ async fn slots_of(cipher: &Cipher, doc: &JsonDocument, paths: &[&str]) -> Vec<us /// A projection as a set: every group's pairs and element groups sorted, so /// two projections that hold the same pairs in the same groups compare /// equal whatever their stored order (`jsonb @>` reads them as sets). +#[cfg(feature = "dynamic")] fn as_set(q: &Value) -> Value { let mut sv = q["sv"].as_array().cloned().unwrap_or_default(); sv.sort_by_key(|pair| pair.to_string()); @@ -285,10 +342,308 @@ fn pg_contains(stored: &Value, needle: &Value, top: bool) -> bool { /// array: a stored document carries no alternative pair, so the SQL answers /// false where `jsonb` answers true, as the design's leakage table records /// for v3's `@>`. +#[cfg(feature = "dynamic")] fn scalar_in_array(stored: &Value, needle: &Value) -> bool { !stored.is_object() && !stored.is_array() && needle.is_array() } +// ============================================================================= +// Queries through a data plan +// ============================================================================= + +/// Queries on the suite's column. There is no typed JSON query: a caller +/// derives one through a data plan, which names the column's field, the +/// operator and its input, and gets the operand to bind. Each helper here +/// derives one operator's operand and reads it back into the type that +/// serializes to it, [`JsonQuery`] or [`JsonLeafQuery`], so a test checks +/// the query a binding sends. +#[cfg(feature = "dynamic")] +mod query { + use stack_encrypt::dynamic::{ + self, record, Operand, QueryTerm, TargetDescriptor, TargetError, TargetOptions, + TargetResolver, ValueFrame, ValueKind, + }; + use stack_encrypt::json::JsonLeafQuery; + use stack_encrypt::target::DeclaredContext; + use stack_encrypt::{AsProtectedRef as _, KeysetCipher, Label, Pending}; + use stack_kms::{DataKeySource, IndexKeySource}; + + use super::*; + + pub(super) use stack_encrypt::dynamic::record::Plan; + pub(super) use stack_encrypt::dynamic::Op; + + /// The plan's one field: the column. + const FIELD: &str = "profile"; + + /// What the helpers run over: any key source a test builds its cipher + /// on. + pub(super) trait Source: DataKeySource + IndexKeySource + Send + Sync + 'static {} + impl<K: DataKeySource + IndexKeySource + Send + Sync + 'static> Source for K {} + + /// A resolver holding one type, `JsonSearch`, which stores a document + /// and so answers the JSON operators. The engine derives those + /// operators itself, and this suite seals and opens its documents + /// without a plan, so the resolver is asked for the type's description + /// and nothing else. + struct Documents; + + impl TargetResolver for Documents { + fn targets(&self) -> Vec<TargetDescriptor> { + vec![TargetDescriptor::new( + "JsonSearch", + "json", + "", + Some(ValueKind::Json), + "public.eql_v4_json_search", + vec!["json".to_string()], + Some("JsonSearchQuery".to_string()), + Some("eql_v4.query_json".to_string()), + true, + None, + ) + .with_leaf_query_sql_domain("eql_v4.query_json_leaf")] + } + + fn encrypt<'a, K: 'static>( + &self, + _: &str, + _: Option<&TargetOptions>, + _: &'a KeysetCipher<'_, K>, + _: &Label, + _: &DeclaredContext, + _: ValueFrame, + ) -> Result<Pending<'a, Vec<u8>, K>, TargetError> { + unreachable!("these tests seal a document without a plan") + } + + fn decrypt<'a, K: 'static>( + &self, + _: &str, + _: &'a StackCipher<K>, + _: &Label, + _: &DeclaredContext, + _: &[u8], + ) -> Result<Pending<'a, ValueFrame, K>, TargetError> { + unreachable!("these tests open a document without a plan") + } + + fn query<'a, K: 'static>( + &self, + _: &str, + _: &IndexSpec, + _: &'a KeysetCipher<'_, K>, + _: &Label, + _: &DeclaredContext, + _: ValueFrame, + ) -> Result<Pending<'a, Vec<u8>, K>, TargetError> { + unreachable!("the JSON operators are the engine's") + } + } + + /// The column as a data plan: one field under `users/profile`, the + /// context the documents are sealed under ([`context`]), storing a + /// document under `options`. A query derives the selectors a document + /// sealed with the same options stores, so a test seals with the + /// options it builds its plan from. + pub(super) fn plan(options: &JsonOptions) -> Plan { + let ordered: Vec<String> = options.ordered.iter().map(ToString::to_string).collect(); + let wire = json!({ + FIELD: { + "context": ["users", "profile"], + "type": "json", + "null": "sql", + "target": { + "name": "JsonSearch", + "options": {"sealed_root": options.sealed_root, "ordered": ordered} + } + } + }); + let plan = record::plan_with( + dynamic::frame::serde::to_vec(&wire) + .unwrap() + .as_protected_ref(), + &Documents, + ) + .unwrap(); + assert_eq!( + plan.fields()[0].target_options(), + Some(&TargetOptions::Json(options.clone())), + "the plan resolved the options it was built from" + ); + plan + } + + /// A JSON value as a frame: what a binding sends as an operator's + /// input. + fn frame(input: &Value) -> ValueFrame { + ValueFrame::from_bytes( + dynamic::frame::serde::to_vec(input) + .unwrap() + .as_protected_ref(), + ) + .unwrap() + } + + /// The input of an operator that takes a document. + fn document(value: &Value) -> Value { + json!({"json": value.to_string()}) + } + + /// The input of an operator that takes a value at a path. + fn at(path: &str, value: &Value) -> Value { + json!({"path": path, "json": value.to_string()}) + } + + /// The input of `between_at`: the two bounds at a path. + fn within(path: &str, low: &Value, high: &Value) -> Value { + json!({"path": path, "low": low.to_string(), "high": high.to_string()}) + } + + /// The term `operator` derives for `input`, or the refusal: the data + /// plan's own, or the index's as [`dynamic::Error::Cipher`]. + async fn term<K: Source>( + keyset: &KeysetCipher<'_, K>, + plan: &Plan, + operator: Op, + input: &Value, + ) -> Result<QueryTerm, dynamic::Error> { + dynamic::query_term(keyset, plan, FIELD, operator, frame(input), &Documents)? + .await + .map_err(dynamic::Error::Cipher) + } + + /// One EQL operand's JSON, read back into the query type that + /// serializes to it. The type writes the same JSON again, so it holds + /// everything the operand does. + fn read<T>(bytes: &[u8]) -> T + where + T: serde::Serialize + serde::de::DeserializeOwned, + { + let wire: Value = serde_json::from_slice(bytes).unwrap(); + let query: T = serde_json::from_value(wire.clone()).unwrap(); + assert_eq!( + serde_json::to_value(&query).unwrap(), + wire, + "the typed query is the operand" + ); + query + } + + /// A containment needle: the one operand of `contains`, `contained_by` + /// and `eq_at`. + async fn needle<K: Source>( + keyset: &KeysetCipher<'_, K>, + plan: &Plan, + operator: Op, + input: &Value, + ) -> Result<JsonQuery, dynamic::Error> { + let term = term(keyset, plan, operator, input).await?; + assert_eq!(term.domain(), Some("eql_v4.query_json"), "{operator:?}"); + match term.operand() { + Operand::One(bytes) => Ok(read(bytes)), + other => panic!("{operator:?} derives one needle, got {other:?}"), + } + } + + /// `column @> needle`. + pub(super) async fn contains<K: Source>( + keyset: &KeysetCipher<'_, K>, + plan: &Plan, + value: &Value, + ) -> Result<JsonQuery, dynamic::Error> { + needle(keyset, plan, Op::Contains, &document(value)).await + } + + /// `column <@ needle`. + pub(super) async fn contained_by<K: Source>( + keyset: &KeysetCipher<'_, K>, + plan: &Plan, + value: &Value, + ) -> Result<JsonQuery, dynamic::Error> { + needle(keyset, plan, Op::ContainedBy, &document(value)).await + } + + /// Equality with the scalar `value` at `path`. + pub(super) async fn eq_at<K: Source>( + keyset: &KeysetCipher<'_, K>, + plan: &Plan, + path: &str, + value: &Value, + ) -> Result<JsonQuery, dynamic::Error> { + needle(keyset, plan, Op::EqAt, &at(path, value)).await + } + + /// An ordering (`Op::LtAt`, `Op::LteAt`, `Op::GtAt` or `Op::GteAt`) + /// against `value` at `path`: the leaf operand the SQL compares with + /// the stored leaf's. + pub(super) async fn ordering_at<K: Source>( + keyset: &KeysetCipher<'_, K>, + plan: &Plan, + operator: Op, + path: &str, + value: &Value, + ) -> Result<JsonLeafQuery, dynamic::Error> { + assert!( + matches!(operator, Op::LtAt | Op::LteAt | Op::GtAt | Op::GteAt), + "{operator:?} is not an ordering at a path" + ); + let term = term(keyset, plan, operator, &at(path, value)).await?; + assert_eq!( + term.domain(), + Some("eql_v4.query_json_leaf"), + "{operator:?}" + ); + match term.operand() { + Operand::One(bytes) => Ok(read(bytes)), + other => panic!("{operator:?} derives one leaf operand, got {other:?}"), + } + } + + /// `between_at`: the leaf operands of the low bound and the high. + pub(super) async fn between_at<K: Source>( + keyset: &KeysetCipher<'_, K>, + plan: &Plan, + path: &str, + low: &Value, + high: &Value, + ) -> Result<(JsonLeafQuery, JsonLeafQuery), dynamic::Error> { + let term = term(keyset, plan, Op::BetweenAt, &within(path, low, high)).await?; + assert_eq!(term.domain(), Some("eql_v4.query_json_leaf"), "between_at"); + match term.operand() { + Operand::Range { lo, hi } => Ok((read(lo), read(hi))), + other => panic!("between_at derives a range, got {other:?}"), + } + } + + /// What a caller checks before it has a keyset: everything a query at + /// a path is refused for, with no cipher. + pub(super) fn check_at( + plan: &Plan, + operator: Op, + path: &str, + value: &Value, + ) -> Result<(), dynamic::Error> { + dynamic::check_query(plan, FIELD, operator, frame(&at(path, value)), &Documents) + } + + /// [`check_at`] for `between_at`. + pub(super) fn check_between_at( + plan: &Plan, + path: &str, + low: &Value, + high: &Value, + ) -> Result<(), dynamic::Error> { + dynamic::check_query( + plan, + FIELD, + Op::BetweenAt, + frame(&within(path, low, high)), + &Documents, + ) + } +} + // ============================================================================= // Round trips // ============================================================================= @@ -420,8 +775,9 @@ mod given_the_fixture_document { #[tokio::test] async fn stores_the_wire_form_the_sql_domain_checks() { - let Scenario { cipher, plan } = scenario().await; - let doc = seal(&cipher, &fixture()).await; + let scenario = scenario().await; + let cipher = &scenario.cipher; + let doc = seal(cipher, &fixture()).await; let wire = serde_json::to_value(&doc).unwrap(); let keys: Vec<&str> = wire .as_object() @@ -431,19 +787,20 @@ mod given_the_fixture_document { .collect(); assert_eq!(keys, ["h", "q", "sv"], "the document's wire keys"); // `q` is the document's own projection, written once: the needle a - // `Contains` of the whole document derives has the same pairs in the + // `contains` of the whole document derives has the same pairs in the // same groups, and nothing else. Stored, its lists are shuffled; as // sets, which is how `jsonb @>` reads them, the two are one. - let needle = cipher - .query(op::Contains, &fixture()) - .on(&plan) - .await - .unwrap(); - assert_eq!( - as_set(&wire["q"]), - as_set(&serde_json::to_value(&needle).unwrap()), - "`q` is the needle a `Contains` of the whole document derives" - ); + #[cfg(feature = "dynamic")] + { + let needle = query::contains(&cipher.default_keyset(), &scenario.plan, &fixture()) + .await + .unwrap(); + assert_eq!( + as_set(&wire["q"]), + as_set(&serde_json::to_value(&needle).unwrap()), + "`q` is the needle a `contains` of the whole document derives" + ); + } assert!( serde_json::from_value::<JsonDocument>(json!({"h": wire["h"], "sv": wire["sv"]})).is_err(), "a document without `q` is not a document: the projection is the only place the value selectors live" @@ -788,6 +1145,27 @@ mod given_ordered_paths { "a position set on the field refuses the seal", ); } + + /// A document sealed with ordered paths opens as any other does, one + /// document or a batch. + #[tokio::test] + async fn opens_what_it_wrote_one_document_or_a_batch() { + let Scenario { cipher, .. } = scenario().await; + let doc = seal_with(&cipher, &fixture(), &ordered()).await; + let back = open(&cipher, doc.clone()).await.unwrap(); + assert_eq!(back, fixture(), "one document"); + let docs: Vec<Value> = Vec::<JsonPlaintext>::decrypt_from_with_context( + vec![doc.clone(), doc], + &cipher, + aead(), + ) + .await + .unwrap() + .into_iter() + .map(held) + .collect(); + assert_eq!(docs, [fixture(), fixture()], "a batch"); + } } // ============================================================================= @@ -1003,9 +1381,7 @@ mod given_an_extracted_node { let doc = &doc; async move { let node = doc.node(at(cipher, doc, path).await).unwrap(); - cipher - .decrypt_as::<Value, JsonExtracted>(extracted(cipher, path, node).await, aead()) - .await + open_extracted(cipher, extracted(cipher, path, node).await).await } }; assert_eq!( @@ -1032,19 +1408,13 @@ mod given_an_extracted_node { .await .unwrap(); let rows = read_rows(&doc, &key); - let nested = cipher - .decrypt_as::<Value, JsonSubtree>(key.open(rows.clone()), aead()) - .await - .unwrap(); + let nested = open_one(&cipher, key.open(rows.clone())).await.unwrap(); assert_eq!( nested, fixture()["nested"], "the walk under $.nested opens to its value" ); - let forest = cipher - .decrypt_as::<Vec<Value>, JsonSubtree>(key.open(rows), aead()) - .await - .unwrap(); + let forest = open_list(&cipher, key.open(rows)).await.unwrap(); assert_eq!( forest, vec![fixture()["nested"].clone()], @@ -1056,10 +1426,7 @@ mod given_an_extracted_node { .await .unwrap(); let rows = read_rows(&doc, &list); - let elements = cipher - .decrypt_as::<Value, JsonSubtree>(list.open(rows), aead()) - .await - .unwrap(); + let elements = open_one(&cipher, list.open(rows)).await.unwrap(); assert_eq!( elements, json!([{"x": 1}, {"x": 2}]), @@ -1080,13 +1447,12 @@ mod given_an_extracted_node { let back: JsonNode = serde_json::from_value(wire).unwrap(); assert_eq!(back.slot(), x as u64, "the node keeps its slot"); assert_eq!( - cipher - .decrypt_as::<Value, JsonExtracted>( - extracted(&cipher, "$.nested.list[*].x", back).await, - aead() - ) - .await - .unwrap(), + open_extracted( + &cipher, + extracted(&cipher, "$.nested.list[*].x", back).await + ) + .await + .unwrap(), json!(1), "the grafted node opens to its scalar" ); @@ -1138,15 +1504,10 @@ mod given_wildcard_rows { assert_eq!(xs.probes(), [64]); let rows = read_rows(&doc, &xs); assert_eq!(rows.len(), 2, "both x"); - let values = cipher - .decrypt_as::<Vec<Value>, JsonSubtree>(xs.open(rows.clone()), aead()) - .await - .unwrap(); + let values = open_list(&cipher, xs.open(rows.clone())).await.unwrap(); assert_eq!(values, vec![json!(1), json!(2)], "one value per element"); assertions::refused_as_opened_type( - &cipher - .decrypt_as::<Value, JsonSubtree>(xs.open(rows), aead()) - .await, + &open_one(&cipher, xs.open(rows)).await, Some("one node at the walked path"), "two elements are not one value", ); @@ -1170,12 +1531,8 @@ mod given_wildcard_rows { "the first x, and the second element with its x" ); for opened in [ - cipher - .decrypt_as::<Vec<Value>, JsonSubtree>(one.open(rows.clone()), aead()) - .await - .map(|_| ()), - cipher - .decrypt_as::<Option<Value>, JsonSubtree>(one.open(rows.clone()), aead()) + open_list(&cipher, one.open(rows.clone())).await.map(|_| ()), + open_option(&cipher, one.open(rows.clone())) .await .map(|_| ()), ] { @@ -1195,8 +1552,7 @@ mod given_wildcard_rows { ) .await .unwrap(); - let values = cipher - .decrypt_as::<Vec<Value>, JsonSubtree>(two.open(read_rows(&doc, &two)), aead()) + let values = open_list(&cipher, two.open(read_rows(&doc, &two))) .await .unwrap(); assert_eq!( @@ -1235,25 +1591,31 @@ mod given_a_derived_record { "the list, both elements and both leaves" ); - let opening: Option<Decryption<Value, FakeDataKeySource>> = doc.decryption_field(aead()); + let opening: Option<Decryption<JsonPlaintext, FakeDataKeySource>> = + doc.decryption_field(aead()); let opened = cipher .run_decryption(opening.expect("a document opens")) .await .unwrap(); - assert_eq!(opened, fixture(), "the document field"); - let opening: Option<Decryption<Value, FakeDataKeySource>> = node.decryption_field(aead()); + assert_eq!(opened.value(), &fixture(), "the document field"); + let opening: Option<Decryption<JsonPlaintext, FakeDataKeySource>> = + node.decryption_field(aead()); let opened = cipher .run_decryption(opening.expect("a node opens")) .await .unwrap(); - assert_eq!(opened, json!("world-1"), "the node field"); - let opening: Option<Decryption<Value, FakeDataKeySource>> = + assert_eq!(opened.value(), &json!("world-1"), "the node field"); + let opening: Option<Decryption<JsonPlaintext, FakeDataKeySource>> = subtree.decryption_field(aead()); let opened = cipher .run_decryption(opening.expect("a subtree opens")) .await .unwrap(); - assert_eq!(opened, json!([{"x": 1}, {"x": 2}]), "the subtree field"); + assert_eq!( + opened.value(), + &json!([{"x": 1}, {"x": 2}]), + "the subtree field" + ); } } @@ -1284,28 +1646,20 @@ mod given_a_fallback_root { "$.nested", "the fallback carries the key's path" ); - let nested = cipher - .decrypt_as::<Value, JsonSubtree>(rows, aead()) - .await - .unwrap(); + let nested = open_one(&cipher, rows).await.unwrap(); assert_eq!(nested, fixture()["nested"], "the value at the path"); - let xs = cipher - .decrypt_as::<Vec<Value>, JsonSubtree>( - keyed(&cipher, "$.nested.list[*].x", vec![root.clone()]).await, - aead(), - ) - .await - .unwrap(); + let xs = open_list( + &cipher, + keyed(&cipher, "$.nested.list[*].x", vec![root.clone()]).await, + ) + .await + .unwrap(); assert_eq!( xs, vec![json!(1), json!(2)], "a wildcard path selects every node over the document" ); - let none = cipher - .decrypt_as::<Vec<Value>, JsonSubtree>( - keyed(&cipher, "$.missing[*]", vec![root]).await, - aead(), - ) + let none = open_list(&cipher, keyed(&cipher, "$.missing[*]", vec![root]).await) .await .unwrap(); assert!( @@ -1326,12 +1680,7 @@ mod given_a_fallback_root { ("$.missing", "a node at the walked path"), ("$.nested.list[*].x", "one node at the walked path"), ] { - let refused = cipher - .decrypt_as::<Value, JsonSubtree>( - keyed(&cipher, path, vec![root.clone()]).await, - aead(), - ) - .await; + let refused = open_one(&cipher, keyed(&cipher, path, vec![root.clone()]).await).await; assertions::refused_as_opened_type(&refused, Some(expected), path); } } @@ -1351,7 +1700,7 @@ mod given_a_fallback_root { "the root at depth 1 is a walk's row" ); assertions::refused_as_wrong_entry( - &cipher.decrypt_as::<Value, JsonSubtree>(rows, aead()).await, + &open_one(&cipher, rows).await, "the root at depth 1 is not a row of the walk", ); let open_doc = seal_with(&cipher, &fixture(), &open_root()).await; @@ -1414,23 +1763,13 @@ mod given_a_fallback_root { let walked = read_rows(&doc, &key); let fallen_back = key.open(vec![root.clone()]); assert!(fallen_back.is_sealed_root(), "{text}: the fallback"); - let as_walked = cipher - .decrypt_as::<Vec<Value>, JsonSubtree>(key.open(walked.clone()), aead()) - .await - .unwrap(); - let as_fallen = cipher - .decrypt_as::<Vec<Value>, JsonSubtree>(fallen_back.clone(), aead()) - .await - .unwrap(); + let as_walked = open_list(&cipher, key.open(walked.clone())).await.unwrap(); + let as_fallen = open_list(&cipher, fallen_back.clone()).await.unwrap(); let expected: Vec<Value> = key.path().select(&document).into_iter().cloned().collect(); assert_eq!(as_walked, expected, "{text}, walked, as a list"); assert_eq!(as_fallen, expected, "{text}, fallen back, as a list"); - let one_walked = cipher - .decrypt_as::<Option<Value>, JsonSubtree>(key.open(walked), aead()) - .await; - let one_fallen = cipher - .decrypt_as::<Option<Value>, JsonSubtree>(fallen_back, aead()) - .await; + let one_walked = open_option(&cipher, key.open(walked)).await; + let one_fallen = open_option(&cipher, fallen_back).await; match (one_walked, one_fallen) { (Ok(a), Ok(b)) => assert_eq!(a, b, "{text}, as one value"), (Err(Error::OpenedType { .. }), Err(Error::OpenedType { .. })) => { @@ -1461,12 +1800,7 @@ mod given_rows_the_server_chose { let doc = seal(&cipher, &fixture()).await; let node = |j: usize| doc.node(j).unwrap(); let repeated = keyed(&cipher, "$.nested", vec![node(1), node(1)]).await; - assertions::refused_as_wrong_entry( - &cipher - .decrypt_as::<Vec<Value>, JsonSubtree>(repeated, aead()) - .await, - "a repeated row", - ); + assertions::refused_as_wrong_entry(&open_list(&cipher, repeated).await, "a repeated row"); // A wide document of one-member objects: its objects, returned as rows // with the last leaf after them, each take the next as their member, @@ -1487,12 +1821,7 @@ mod given_rows_the_server_chose { containers.push(doc.node(at(&cipher, &doc, "$.k199.x").await).unwrap()); assert_eq!(containers.len(), 201, "200 objects and the last leaf"); assertions::refused_as_tampered( - &cipher - .decrypt_as::<Vec<Value>, JsonSubtree>( - keyed(&cipher, "$.k000", containers).await, - aead(), - ) - .await, + &open_list(&cipher, keyed(&cipher, "$.k000", containers).await).await, "a line of 200 objects", ); @@ -1518,12 +1847,7 @@ mod given_rows_the_server_chose { "the outer array, 200 inner and the last leaf" ); assertions::refused_as_tampered( - &cipher - .decrypt_as::<Vec<Value>, JsonSubtree>( - keyed(&cipher, "$.k000", arrays).await, - aead(), - ) - .await, + &open_list(&cipher, keyed(&cipher, "$.k000", arrays).await).await, "a line of 202 arrays", ); } @@ -1569,12 +1893,12 @@ mod given_a_tampered_document { }); assertions::refused_as_tampered(&open(&cipher, flipped).await, "a changed byte"); - let wrong_context = cipher - .decrypt_as::<Value, JsonDocument>( - doc, - AeadContext::from(nonempty!("users").with("bio")), - ) - .await; + let wrong_context = open_under( + &cipher, + doc, + AeadContext::from(nonempty!("users").with("bio")), + ) + .await; assertions::refused_as_tampered(&wrong_context, "another column"); // A node lifted out of its document fails under another slot. Through @@ -1586,9 +1910,7 @@ mod given_a_tampered_document { lifted["j"] = json!(3 - a); let lifted: JsonNode = serde_json::from_value(lifted).unwrap(); assertions::refused_as_not_a_leaf( - &cipher - .decrypt_as::<Value, JsonExtracted>(extracted(&cipher, "$.a", lifted).await, aead()) - .await, + &open_extracted(&cipher, extracted(&cipher, "$.a", lifted).await).await, "a node under another slot", ); } @@ -1656,6 +1978,7 @@ mod given_the_containment_model { } } +#[cfg(feature = "dynamic")] mod given_the_design_corpus { use super::*; @@ -1753,12 +2076,13 @@ mod given_the_design_corpus { #[tokio::test] async fn sends_each_needle_in_an_order_of_its_own() { let Scenario { cipher, plan } = scenario().await; + let keyset = cipher.default_keyset(); let value: Value = (0..11) .map(|i| (format!("k{i}"), json!(i))) .collect::<Map<String, Value>>() .into(); - let one: JsonQuery = cipher.query(op::Contains, &value).on(&plan).await.unwrap(); - let two: JsonQuery = cipher.query(op::Contains, &value).on(&plan).await.unwrap(); + let one = query::contains(&keyset, &plan, &value).await.unwrap(); + let two = query::contains(&keyset, &plan, &value).await.unwrap(); let (a, b) = ( serde_json::to_value(&one).unwrap(), serde_json::to_value(&two).unwrap(), @@ -1771,12 +2095,10 @@ mod given_the_design_corpus { assert_ne!(a, b, "two orders"); assert_eq!(as_set(&a), as_set(&b), "one set"); assert_eq!(one, two, "equal as sets"); - let at: JsonQuery = cipher - .query(op::AtPath(op::Eq), &("$.k3", 3)) - .on(&plan) + let at = query::eq_at(&keyset, &plan, "$.k3", &json!(3)) .await .unwrap(); - let doc = cipher.encrypt(&value).using(&plan).await.unwrap(); + let doc = seal(&cipher, &value).await; assert!(one.matches(doc.selector_set()) && two.matches(doc.selector_set())); assert!(at.matches(doc.selector_set()), "an equality needle too"); } @@ -1787,14 +2109,11 @@ mod given_the_design_corpus { #[tokio::test] async fn answers_containment_both_ways_as_jsonb_does() { let Scenario { cipher, plan } = scenario().await; + let keyset = cipher.default_keyset(); for (stored, needle) in corpus() { - let doc = cipher.encrypt(&stored).using(&plan).await.unwrap(); - let contains: JsonQuery = cipher.query(op::Contains, &needle).on(&plan).await.unwrap(); - let contained_by: JsonQuery = cipher - .query(op::ContainedBy, &needle) - .on(&plan) - .await - .unwrap(); + let doc = seal(&cipher, &stored).await; + let contains = query::contains(&keyset, &plan, &needle).await.unwrap(); + let contained_by = query::contained_by(&keyset, &plan, &needle).await.unwrap(); assertions::contains_as_jsonb(&contains, &doc, &stored, &needle); assertions::is_contained_by_as_jsonb(&contained_by, &doc, &stored, &needle); } @@ -1811,17 +2130,22 @@ mod given_generated_documents { //! later run replays first. The strategies mirror the EQL live oracle's //! (`packages/eql/tests/encryption/tests/json_tree/generated.rs`), which //! sits in another Cargo workspace. + //! + //! The two properties derive their queries through the data plan, so + //! they are behind the `dynamic` feature. Without it the cases and the + //! needle strategies are compiled and not run. + #![cfg_attr(not(feature = "dynamic"), allow(dead_code))] - use std::cell::Cell; use std::fmt; - use std::future::IntoFuture; use proptest::collection::vec as vec_of; use proptest::prelude::*; use proptest::sample::{select, Index}; use proptest::strategy::ValueTree; + #[cfg(feature = "dynamic")] + use proptest::test_runner::TestCaseError; use proptest::test_runner::{ - Config, FileFailurePersistence, RngAlgorithm, RngSeed, TestCaseError, TestRunner, + Config, FileFailurePersistence, RngAlgorithm, RngSeed, TestRunner, }; use super::*; @@ -2157,6 +2481,7 @@ mod given_generated_documents { /// Run `body` over a whole proptest run on one runtime and cipher, and /// fail with the minimal case and the way to replay it. + #[cfg(feature = "dynamic")] fn run( name: &str, body: impl Fn(&Scenario, &tokio::runtime::Runtime, &Case) -> Result<(), TestCaseError>, @@ -2180,36 +2505,26 @@ mod given_generated_documents { } } - /// The stored document, sealed through the plan. + /// The stored document, sealed under the options the scenario's plan + /// queries under. + #[cfg(feature = "dynamic")] fn sealed( scenario: &Scenario, runtime: &tokio::runtime::Runtime, stored: &Value, ) -> JsonDocument { - runtime - .block_on( - scenario - .cipher - .encrypt(stored) - .using(&scenario.plan) - .into_future(), - ) - .unwrap() + runtime.block_on(seal(&scenario.cipher, stored)) } + #[cfg(feature = "dynamic")] #[test] fn contains_as_jsonb_contains() { run("@>", |scenario, runtime, case| { let (needle, contained) = case.needle(); let doc = sealed(scenario, runtime, &case.stored); - let query: JsonQuery = runtime - .block_on( - scenario - .cipher - .query(op::Contains, &needle) - .on(&scenario.plan) - .into_future(), - ) + let keyset = scenario.cipher.default_keyset(); + let query = runtime + .block_on(query::contains(&keyset, &scenario.plan, &needle)) .unwrap(); prop_assert!( !contained || pg_contains(&case.stored, &needle, true), @@ -2226,14 +2541,8 @@ mod given_generated_documents { // Every query sends its needle in a random order of its own: a // second derivation is the same set of pairs and groups, and // matches exactly what the first did. - let again: JsonQuery = runtime - .block_on( - scenario - .cipher - .query(op::Contains, &needle) - .on(&scenario.plan) - .into_future(), - ) + let again = runtime + .block_on(query::contains(&keyset, &scenario.plan, &needle)) .unwrap(); prop_assert_eq!(&again, &query, "one needle, as sets"); prop_assert_eq!( @@ -2247,20 +2556,16 @@ mod given_generated_documents { }); } + #[cfg(feature = "dynamic")] #[test] fn is_contained_by_as_jsonb_except_a_root_scalar_in_a_root_array() { - let excluded = Cell::new(0); + let excluded = std::cell::Cell::new(0); run("<@", |scenario, runtime, case| { let (needle, _) = case.needle(); let doc = sealed(scenario, runtime, &case.stored); - let query: JsonQuery = runtime - .block_on( - scenario - .cipher - .query(op::ContainedBy, &needle) - .on(&scenario.plan) - .into_future(), - ) + let keyset = scenario.cipher.default_keyset(); + let query = runtime + .block_on(query::contained_by(&keyset, &scenario.plan, &needle)) .unwrap(); let jsonb = pg_contains(&needle, &case.stored, true); if jsonb && scalar_in_array(&case.stored, &needle) { @@ -2345,21 +2650,13 @@ mod given_a_float_at_full_precision { ), ( "the extracted element, as `->` returns it", - cipher - .decrypt_as::<Value, JsonExtracted>( - extracted(cipher, "$.list[*]", element).await, - aead(), - ) + open_extracted(cipher, extracted(cipher, "$.list[*]", element).await) .await .unwrap(), ), ( "the walk under $.list", - cipher - .decrypt_as::<Value, JsonSubtree>(key.open(rows), aead()) - .await - .unwrap()[0] - .take(), + open_one(cipher, key.open(rows)).await.unwrap()[0].take(), ), ] } @@ -2427,16 +2724,16 @@ mod given_a_float_at_full_precision { // Queries // ============================================================================= +#[cfg(feature = "dynamic")] mod given_a_containment_query { use super::*; #[tokio::test] async fn serializes_as_the_needle_operand_and_derives_no_key() { let (cipher, generates, _) = common::counting_cipher().await; + let keyset = cipher.default_keyset(); let plan = plan(); - let query = cipher - .query(op::Contains, &json!({"a": [{"x": 1}]})) - .on(&plan) + let query = query::contains(&keyset, &plan, &json!({"a": [{"x": 1}]})) .await .unwrap(); let wire = serde_json::to_value(&query).unwrap(); @@ -2465,11 +2762,7 @@ mod given_a_containment_query { "a query mints no key" ); - let scalar = cipher - .query(op::Contains, &json!("a")) - .on(&plan) - .await - .unwrap(); + let scalar = query::contains(&keyset, &plan, &json!("a")).await.unwrap(); let wire = serde_json::to_value(&scalar).unwrap(); assert!( wire.get("alt").is_some(), @@ -2478,34 +2771,28 @@ mod given_a_containment_query { } } +#[cfg(feature = "dynamic")] mod given_equality_at_the_root { use super::*; - /// At the root, equality is of the root value: `("$", 1)` matches a + /// At the root, equality is of the root value: `1` at `$` matches a /// stored `1` and not a stored `[1]`, which containment of the scalar /// `1` would. #[tokio::test] async fn matches_the_root_value_and_not_an_array_holding_it() { let Scenario { cipher, plan } = scenario().await; - let at_root: JsonQuery = cipher - .query(op::AtPath(op::Eq), &("$", 1)) - .on(&plan) - .await - .unwrap(); - let contains: JsonQuery = cipher - .query(op::Contains, &json!(1)) - .on(&plan) - .await - .unwrap(); - let scalar = cipher.encrypt(&json!(1)).using(&plan).await.unwrap(); - let array = cipher.encrypt(&json!([1])).using(&plan).await.unwrap(); + let keyset = cipher.default_keyset(); + let at_root = query::eq_at(&keyset, &plan, "$", &json!(1)).await.unwrap(); + let contains = query::contains(&keyset, &plan, &json!(1)).await.unwrap(); + let scalar = seal(&cipher, &json!(1)).await; + let array = seal(&cipher, &json!([1])).await; assert!( at_root.matches(scalar.selector_set()), - "(\"$\", 1) matches a stored 1" + "1 at `$` matches a stored 1" ); assert!( !at_root.matches(array.selector_set()), - "(\"$\", 1) does not match a stored [1]" + "1 at `$` does not match a stored [1]" ); assert!(contains.matches(array.selector_set()), "jsonb: [1] @> 1"); assert!( @@ -2515,13 +2802,16 @@ mod given_equality_at_the_root { } } +#[cfg(feature = "dynamic")] mod given_an_accessor { + use super::query::Op; use super::*; #[tokio::test] async fn matches_the_value_at_its_path() { let Scenario { cipher, plan } = scenario().await; - let doc = cipher.encrypt(&fixture()).using(&plan).await.unwrap(); + let keyset = cipher.default_keyset(); + let doc = seal(&cipher, &fixture()).await; let projection = doc.selector_set(); for (path, value, matches, what) in [ ("$.nested.list[*].x", json!(2), true, "an element's x is 2"), @@ -2529,28 +2819,24 @@ mod given_an_accessor { ("$.nested.deep", json!("constant"), true, "the deep string"), ("$.number", json!("1"), false, "a string is not the number"), ] { - let query: JsonQuery = cipher - .query(op::AtPath(op::Eq), &(path, value)) - .on(&plan) - .await - .unwrap(); + let query = query::eq_at(&keyset, &plan, path, &value).await.unwrap(); assert_eq!(query.matches(projection), matches, "{what}"); } } /// An ordering at a path: the path's selector and a leaf term that /// orders as the stored leaf's does, under native byte comparison. The - /// plan declares the paths it orders, and the query runs under them. + /// plan declares the paths it orders, the document is sealed with them, + /// and the query runs under them. #[tokio::test] async fn orders_as_the_stored_leaf_at_an_ordered_path() { let Scenario { cipher, .. } = scenario().await; + let keyset = cipher.default_keyset(); let plan = ordered_plan(); - let doc = cipher.encrypt(&fixture()).using(&plan).await.unwrap(); + let doc = seal_with(&cipher, &fixture(), &ordered()).await; let number = &doc.entries()[at(&cipher, &doc, "$.number").await]; let hello = &doc.entries()[at(&cipher, &doc, "$.hello").await]; - let bound = cipher - .query(op::AtPath(op::Gte), &("$.number", 0)) - .on(&plan) + let bound = query::ordering_at(&keyset, &plan, Op::GteAt, "$.number", &json!(0)) .await .unwrap(); assert_eq!( @@ -2562,39 +2848,33 @@ mod given_an_accessor { bound.ordering_term() < number.ordering_term().unwrap(), "0 < 1" ); - let above = cipher - .query(op::AtPath(op::Lt), &("$.number", 5)) - .on(&plan) + let above = query::ordering_at(&keyset, &plan, Op::LtAt, "$.number", &json!(5)) .await .unwrap(); assert!( above.ordering_term() > number.ordering_term().unwrap(), "5 > 1" ); - let text = cipher - .query(op::AtPath(op::Gt), &("$.hello", "world-0")) - .on(&plan) + let text = query::ordering_at(&keyset, &plan, Op::GtAt, "$.hello", &json!("world-0")) .await .unwrap(); assert!( text.ordering_term() < hello.ordering_term().unwrap(), "world-0 < world-1" ); - let range = cipher - .query(op::AtPath(op::Between), &("$.number", (0, 5))) - .on(&plan) + let (lo, hi) = query::between_at(&keyset, &plan, "$.number", &json!(0), &json!(5)) .await .unwrap(); assert!( - range.lo().ordering_term() < range.hi().ordering_term(), + lo.ordering_term() < hi.ordering_term(), "the range's bounds are in order" ); assert_eq!( - range.lo().selector().as_bytes(), + lo.selector().as_bytes(), number.selector(), "the range asks at $.number" ); - let wire = serde_json::to_value(range.lo()).unwrap(); + let wire = serde_json::to_value(&lo).unwrap(); assert_eq!( wire.as_object().unwrap().keys().collect::<Vec<_>>(), ["op", "s"], @@ -2605,50 +2885,39 @@ mod given_an_accessor { /// Refusals, before any term is derived: a text that is not a path, a /// leaf kind that has no ordering, and a path the plan does not order /// (no stored leaf there carries a term, so the comparison would match - /// no row; under the default plan that is every path). + /// no row). A plan that orders no path, as the default plan does, + /// answers no ordering at all, and the data plan says so with a reason + /// of its own. #[tokio::test] async fn refuses_a_bad_path_an_unordered_leaf_or_a_path_the_plan_does_not_order() { let Scenario { cipher, plan: default_plan, } = scenario().await; + let keyset = cipher.default_keyset(); let plan = ordered_plan(); - let bad_path = cipher - .query(op::AtPath(op::Eq), &("address.city", "Sydney")) - .on(&plan) - .await; - assertions::refused_by_term( + let bad_path = query::eq_at(&keyset, &plan, "address.city", &json!("Sydney")).await; + assertions::query_refused_by_term( &bad_path, |e| matches!(e, TermError::JsonPath(_)), "a text that is not a path", ); - let unordered = cipher - .query(op::AtPath(op::Gte), &("$.flag", true)) - .on(&plan) - .await; - assertions::refused_by_term( + let unordered = query::ordering_at(&keyset, &plan, Op::GteAt, "$.flag", &json!(true)).await; + assertions::query_refused_by_term( &unordered, |e| matches!(e, TermError::JsonLeafNotOrdered), "a boolean has no ordering", ); - let not_ordered = cipher - .query(op::AtPath(op::Lt), &("$.nested.deep", "z")) - .on(&plan) - .await; - assertions::refused_by_term( + let not_ordered = + query::ordering_at(&keyset, &plan, Op::LtAt, "$.nested.deep", &json!("z")).await; + assertions::query_refused_by_term( &not_ordered, |e| matches!(e, TermError::JsonPathNotOrdered), "a path the plan does not order", ); - let not_ordered = cipher - .query(op::AtPath(op::Between), &("$.number", (0, 5))) - .on(&default_plan) - .await; - assertions::refused_by_term( - &not_ordered, - |e| matches!(e, TermError::JsonPathNotOrdered), - "the default plan orders no path", - ); + let not_answered = + query::between_at(&keyset, &default_plan, "$.number", &json!(0), &json!(5)).await; + assertions::query_not_answered(&not_answered, "the default plan orders no path"); } /// An ordering at a wildcard path is covered by the wildcard. One at a @@ -2658,53 +2927,46 @@ mod given_an_accessor { #[tokio::test] async fn orders_at_a_wildcard_and_refuses_a_position() { let Scenario { cipher, .. } = scenario().await; - let by_element = Plan::value::<Value>() - .context("users/profile") - .encrypt_with_layout(Json::new(ordering(&["$.nested.list[*].x"]))) - .build() - .unwrap(); - let wildcard = cipher - .query(op::AtPath(op::Gt), &("$.nested.list[*].x", 0)) - .on(&by_element) - .await; + let keyset = cipher.default_keyset(); + let by_element = query::plan(&ordering(&["$.nested.list[*].x"])); + let wildcard = query::ordering_at( + &keyset, + &by_element, + Op::GtAt, + "$.nested.list[*].x", + &json!(0), + ) + .await; assert!( wildcard.is_ok(), "the wildcard the plan orders: {wildcard:?}" ); for path in ["$.nested.list[1].x", "$.nested.list[0].x"] { - let refused = cipher - .query(op::AtPath(op::Gt), &(path, 0)) - .on(&by_element) - .await; - assertions::refused_by_term( + let refused = query::ordering_at(&keyset, &by_element, Op::GtAt, path, &json!(0)).await; + assertions::query_refused_by_term( &refused, |e| matches!(e, TermError::JsonOrderingAtPosition), - &format!("{path} with Gt"), + &format!("{path} with gt_at"), ); - let refused = cipher - .query(op::AtPath(op::Between), &(path, (0, 5))) - .on(&by_element) - .await; - assertions::refused_by_term( + let refused = query::between_at(&keyset, &by_element, path, &json!(0), &json!(5)).await; + assertions::query_refused_by_term( &refused, |e| matches!(e, TermError::JsonOrderingAtPosition), - &format!("{path} with Between"), + &format!("{path} with between_at"), ); } } /// Equality at a path compares one value: an object or an array would - /// be containment, which `op::Contains` asks for. + /// be containment, which `contains` asks for. #[tokio::test] async fn refuses_a_container_for_equality() { let Scenario { cipher, .. } = scenario().await; + let keyset = cipher.default_keyset(); let plan = ordered_plan(); for container in [json!({"deep": "constant"}), json!([]), json!([{"x": 1}])] { - let refused = cipher - .query(op::AtPath(op::Eq), &("$.nested", container.clone())) - .on(&plan) - .await; - assertions::refused_by_term( + let refused = query::eq_at(&keyset, &plan, "$.nested", &container).await; + assertions::query_refused_by_term( &refused, |e| matches!(e, TermError::JsonValueNotScalar), &container.to_string(), @@ -2712,88 +2974,56 @@ mod given_an_accessor { } } - /// An accessor whose path or bounds the index refuses is refused when - /// the query is awaited, before the keyset it names is loaded; so is an - /// open that names a context the plan already has. + /// An accessor whose path or bounds the index refuses is refused by + /// the check a caller runs before it has a keyset, which takes no + /// cipher: the keyset the query is for is not loaded for it. #[tokio::test] async fn is_refused_before_its_keyset_loads() { let (cipher, loads) = common::loads_counting_cipher().await; let plan = ordered_plan(); - let eq = cipher - .query(op::AtPath(op::Eq), &("address.city", "Sydney")) - .on(&plan) - .keyset("tenant") - .await; - assertions::refused_by_term( + let eq = query::check_at(&plan, Op::EqAt, "address.city", &json!("Sydney")); + assertions::query_refused_by_term( &eq, |e| matches!(e, TermError::JsonPath(_)), - "Eq at a bad path", + "eq_at at a bad path", ); - let between = cipher - .query(op::AtPath(op::Between), &("address.age", (1, 2))) - .on(&plan) - .keyset("tenant") - .await; - assertions::refused_by_term( + let between = query::check_between_at(&plan, "address.age", &json!(1), &json!(2)); + assertions::query_refused_by_term( &between, |e| matches!(e, TermError::JsonPath(_)), - "Between at a bad path", + "between_at at a bad path", ); - let unordered = cipher - .query(op::AtPath(op::Between), &("$.flag", (false, true))) - .on(&plan) - .keyset("tenant") - .await; - assertions::refused_by_term( + let unordered = query::check_between_at(&plan, "$.flag", &json!(false), &json!(true)); + assertions::query_refused_by_term( &unordered, |e| matches!(e, TermError::JsonLeafNotOrdered), - "Between over booleans", + "between_at over booleans", ); - let not_ordered = cipher - .query(op::AtPath(op::Lt), &("$.nested.deep", "z")) - .on(&plan) - .keyset("tenant") - .await; - assertions::refused_by_term( + let not_ordered = query::check_at(&plan, Op::LtAt, "$.nested.deep", &json!("z")); + assertions::query_refused_by_term( &not_ordered, |e| matches!(e, TermError::JsonPathNotOrdered), "a path the plan does not order", ); - let at_position = cipher - .query(op::AtPath(op::Gte), &("$.nested.list[0].x", 1)) - .on(&plan) - .keyset("tenant") - .await; - assertions::refused_by_term( + let at_position = query::check_at(&plan, Op::GteAt, "$.nested.list[0].x", &json!(1)); + assertions::query_refused_by_term( &at_position, |e| matches!(e, TermError::JsonOrderingAtPosition), "an ordering at a position", ); - let doc = seal(&stack_cipher().await, &fixture()).await; - let clash = cipher - .open(doc.clone()) - .context("users/other") - .using(&plan) - .keyset("tenant") - .await; - assertions::refused_for_two_contexts(&clash, "one document"); - let clash = cipher - .open(vec![doc]) - .context("users/other") - .using(&plan) - .keyset("tenant") - .await; - assertions::refused_for_two_contexts(&clash, "a batch"); assert_eq!( loads.load(std::sync::atomic::Ordering::SeqCst), 0, - "no keyset was loaded for a refused query or open" + "no keyset was loaded for a refused query" ); - // The same query with a path the index takes does load it. - let _ = cipher - .query(op::AtPath(op::Eq), &("$.address.city", "Sydney")) - .on(&plan) - .keyset("tenant") + // The same query with a path the index takes passes the check, and + // deriving it loads the keyset. + query::check_at(&plan, Op::EqAt, "$.address.city", &json!("Sydney")).unwrap(); + let tenant = cipher + .keyset(stack_kms::IdentifiedBy::Name("tenant".to_string().into())) + .await + .unwrap(); + let _ = query::eq_at(&tenant, &plan, "$.address.city", &json!("Sydney")) .await .unwrap(); assert_eq!( @@ -2805,126 +3035,31 @@ mod given_an_accessor { } // ============================================================================= -// Plans +// The index as data // ============================================================================= -mod given_a_plan { - use super::*; +mod given_the_json_index { + use stack_encrypt::target::Index; - #[tokio::test] - async fn refuses_a_query_under_other_options() { - let Scenario { cipher, plan } = scenario().await; - let refused = cipher - .query(op::Contains, &json!({})) - .via(Json::new(open_root())) - .on(&plan) - .await; - assert!( - matches!(refused, Err(Error::Plan(PlanError::IndexOptions { .. }))), - "a query under other options is refused: {refused:?}" - ); - assert_eq!( - <JsonDocument as stack_encrypt::EncryptFrom<Value>>::indexes(), - [IndexSpec::Json(JsonOptions::default())], - "a JSON document's index is the default JSON index" - ); - assert_eq!(Json, Json::default(), "the value is the default index"); - } - - #[tokio::test] - async fn opens_what_it_wrote_as_the_typed_open_does() { - let Scenario { cipher, plan } = scenario().await; - let doc = cipher.encrypt(&fixture()).using(&plan).await.unwrap(); - let typed = open(&cipher, doc.clone()).await.unwrap(); - let back: Value = cipher.open(doc).using(&plan).await.unwrap(); - assert_eq!(back, fixture(), "the plan opens what it wrote"); - assert_eq!(back, typed, "and the typed open agrees"); - } -} - -mod given_a_laid_out_plan { use super::*; + /// The index names its options in its spec, the data form a plan + /// carries, and the value `Json` is the index under the defaults. #[test] - fn reports_its_layout() { - let plan = ordered_plan(); - let options = ordering(&["$.hello", "$.number", "$.flag"]); - assert_eq!(plan.layout().options(), &options, "the layout's options"); + fn names_its_options_in_its_spec() { + let options = ordered(); + let index = Json::new(options.clone()); + assert_eq!(index.options(), &options, "the index's options"); assert_eq!( - plan.specs(), - [IndexSpec::Json(options.clone())], - "the plan's index specs" - ); - assert_eq!( - stack_encrypt::target::Layout::<Value>::specs(&Json::new(options.clone())), - [IndexSpec::Json(options)], - "the layout's index specs" - ); - assert!( - format!("{plan:?}").contains("Laid<stack_encrypt::json::JsonDocument>(Json"), - "the plan's Debug names its layout: {plan:?}" + Index::<Value>::spec(&index), + IndexSpec::Json(options), + "the index's spec" ); - } - - /// A plan laid out by the index opens what it wrote, one document or a - /// batch. - #[tokio::test] - async fn opens_what_it_wrote_one_document_or_a_batch() { - let Scenario { cipher, .. } = scenario().await; - let plan = ordered_plan(); - let doc = cipher.encrypt(&fixture()).using(&plan).await.unwrap(); - let back: Value = cipher.open(doc.clone()).using(&plan).await.unwrap(); - assert_eq!(back, fixture(), "one document"); - let docs: Vec<Value> = cipher - .open(vec![doc.clone(), doc]) - .using(&plan) - .await - .unwrap(); - assert_eq!(docs, [fixture(), fixture()], "a batch"); - } - - /// The plan was built with its context, so an open that names one is - /// refused, one document or a batch, before any key is requested. - #[tokio::test] - async fn refuses_an_open_that_names_another_context() { - let Scenario { cipher, .. } = scenario().await; - let plan = ordered_plan(); - let doc = cipher.encrypt(&fixture()).using(&plan).await.unwrap(); - let clash = cipher - .open(doc.clone()) - .context("users/other") - .using(&plan) - .await; - assertions::refused_for_two_contexts(&clash, "one document"); - let clash = cipher - .open(vec![doc]) - .context("users/other") - .using(&plan) - .await; - assertions::refused_for_two_contexts(&clash, "a batch"); - } - - /// The index named on the query says only which: the plan's own, with - /// its options, answers. - #[tokio::test] - async fn answers_a_named_index_with_its_own_options() { - let Scenario { cipher, .. } = scenario().await; - let plan = ordered_plan(); - let through_default = cipher - .query(op::AtPath(op::Gte), &("$.number", 0)) - .via(Json) - .on(&plan) - .await - .unwrap(); - let chosen = cipher - .query(op::AtPath(op::Gte), &("$.number", 0)) - .on(&plan) - .await - .unwrap(); + assert_eq!(Json, Json::default(), "the value is the default index"); assert_eq!( - through_default.selector(), - chosen.selector(), - "the default index named on the query answers as the plan's own" + Index::<Value>::spec(&Json), + IndexSpec::Json(JsonOptions::default()), + "the default index's spec" ); } } @@ -3087,18 +3222,40 @@ mod assertions { ); } - /// `result` is the refusal of an open that names a second context. - pub(super) fn refused_for_two_contexts<T: Debug>(result: &Result<T, Error>, what: &str) { + /// `result` is a query the index refused with the term error `is` + /// picks out, which the data plan hands on as its cipher error. + #[cfg(feature = "dynamic")] + pub(super) fn query_refused_by_term<T: Debug>( + result: &Result<T, stack_encrypt::dynamic::Error>, + is: impl Fn(&TermError) -> bool, + what: &str, + ) { assert!( matches!( result, - Err(Error::Plan(PlanError::TwoContextSources { .. })) + Err(stack_encrypt::dynamic::Error::Cipher(Error::Term(error))) if is(error) ), - "{what}: an open naming another context should be refused, got {result:?}" + "{what}: refused with the wrong outcome: {result:?}" + ); + } + + /// `result` is a query the data plan refused because the field does + /// not answer its operator. + #[cfg(feature = "dynamic")] + pub(super) fn query_not_answered<T: Debug>( + result: &Result<T, stack_encrypt::dynamic::Error>, + what: &str, + ) { + let reason = result.as_ref().err().and_then(|error| error.reason()); + assert_eq!( + reason, + Some(stack_encrypt::dynamic::Reason::OperatorNotAnswered), + "{what}: should not be answered, got {result:?}" ); } /// `@>` over the projections is the model's `jsonb @>`. + #[cfg(feature = "dynamic")] pub(super) fn contains_as_jsonb( query: &JsonQuery, doc: &JsonDocument, @@ -3114,6 +3271,7 @@ mod assertions { /// `<@` is the model's `jsonb <@`, but for a stored root scalar in a /// needle root array. + #[cfg(feature = "dynamic")] pub(super) fn is_contained_by_as_jsonb( query: &JsonQuery, doc: &JsonDocument, diff --git a/packages/stack-encrypt/tests/json_leakage.rs b/packages/stack-encrypt/tests/json_leakage.rs index c67a511c0c..4ae1ec5b2c 100644 --- a/packages/stack-encrypt/tests/json_leakage.rs +++ b/packages/stack-encrypt/tests/json_leakage.rs @@ -1,5 +1,10 @@ //! An executable leakage profile for the JSON index (EQL v4 `json_tree`). //! See `json_leakage/mod.rs` for what it is and how to read it. +//! +//! The real side derives its containment, equality and ordering queries +//! through a data plan, as a binding does, so the binary needs the +//! `dynamic` feature. +#![cfg(feature = "dynamic")] mod common; #[path = "json_leakage/mod.rs"] diff --git a/packages/stack-encrypt/tests/json_leakage/corpus.rs b/packages/stack-encrypt/tests/json_leakage/corpus.rs index f34f1983ba..a10c9b8bff 100644 --- a/packages/stack-encrypt/tests/json_leakage/corpus.rs +++ b/packages/stack-encrypt/tests/json_leakage/corpus.rs @@ -432,7 +432,10 @@ fn query(kind: Ordered) -> impl Strategy<Value = Query> { object(prop::collection::vec(object(leaf(), 1..2), 1..2).prop_map(Value::Array), 1..2), ] .prop_map(move |v| Query::Contains(kind.fix(v))), - 1 => (prop::sample::select(vec!["$.a", "$.tags[0]", "$.list[*].b"]), + // Equality at a path takes no position: its operand is a selector + // set, which reads a position as the wildcard, so the query is + // refused rather than answered for whichever element is found. + 1 => (prop::sample::select(vec!["$.a", "$.tags[*]", "$.list[*].b"]), prop_oneof![(-3i64..6).prop_map(Value::from), prop::sample::select(vec!["x", "ab"]).prop_map(Value::from)]) .prop_map(|(p, v)| Query::Equals(p.into(), v)), 1 => kind.query(), diff --git a/packages/stack-encrypt/tests/json_leakage/observe.rs b/packages/stack-encrypt/tests/json_leakage/observe.rs index edf2b94087..aaa2afc1ac 100644 --- a/packages/stack-encrypt/tests/json_leakage/observe.rs +++ b/packages/stack-encrypt/tests/json_leakage/observe.rs @@ -86,9 +86,9 @@ pub enum QueryKind { Subtree, /// `@>`: a containment needle, with its `alt`. Contains, - /// `AtPath<Eq>`: a containment needle with no `alt`. + /// `eq_at`: a containment needle with no `alt`. Equals, - /// `AtPath<Gte>`: a selector and an ordering term. + /// `gte_at`: a selector and an ordering term. Order, } diff --git a/packages/stack-encrypt/tests/json_leakage/profile.rs b/packages/stack-encrypt/tests/json_leakage/profile.rs index 7cb547e020..4801bcbb7c 100644 --- a/packages/stack-encrypt/tests/json_leakage/profile.rs +++ b/packages/stack-encrypt/tests/json_leakage/profile.rs @@ -87,9 +87,11 @@ pub enum Query { }, /// Containment, `@>`, of a needle document. Contains(Value), - /// Equality at a path, `AtPath<Eq>`, of a scalar. + /// Equality at a path, the data plan's `eq_at`, of a scalar. The path + /// names no position. Equals(String, Value), - /// An ordering bound at a path, `AtPath<Gte>`, of a number or string. + /// An ordering bound at a path, the data plan's `gte_at`, of a number + /// or string. Order(String, Value), } diff --git a/packages/stack-encrypt/tests/json_leakage/real.rs b/packages/stack-encrypt/tests/json_leakage/real.rs index 691f02842c..77bbe15779 100644 --- a/packages/stack-encrypt/tests/json_leakage/real.rs +++ b/packages/stack-encrypt/tests/json_leakage/real.rs @@ -6,20 +6,30 @@ //! Deliberately thin: it says which API each [`Query`] goes through and //! nothing about what the result reveals. When the format changes, this file //! changes only where the API does. +//! +//! A document is sealed by the JSON index's own sealing operation +//! ([`json_document`]), under the column's options. A path's selector and a +//! subtree read's tokens come from the keyset. Containment, equality at a +//! path and an ordering at a path are derived as a binding derives them, +//! through a data plan ([`dynamic::query_term`]): the operand is the JSON +//! the database is sent, and it is read here as the type it serializes +//! ([`JsonQuery`], [`JsonLeafQuery`]). -use serde_json::Value; -use stack_encrypt::json::{JsonDocument, JsonOptions, JsonPath}; -use stack_encrypt::op; -use stack_encrypt::target::Json; -use stack_encrypt::{nonempty, Plan, StackCipher}; +use serde::de::DeserializeOwned; +use serde::Serialize; +use serde_json::{json, Value}; +use stack_encrypt::dynamic::{ + self, record, Op, Operand, TargetDescriptor, TargetError, TargetOptions, TargetResolver, + ValueFrame, ValueKind, +}; +use stack_encrypt::json::{JsonDocument, JsonLeafQuery, JsonOptions, JsonPath, JsonQuery}; +use stack_encrypt::target::{json_document, Borrowed, CallerContext, DeclaredContext, IndexSpec}; +use stack_encrypt::{nonempty, AsProtectedRef as _, KeysetCipher, Label, Pending, StackCipher}; use stack_kms::FakeDataKeySource; use super::observe::QueryKind; use super::profile::{Column, Query}; -type JsonPlan = - stack_encrypt::plan::ValuePlan<Value, stack_encrypt::plan::Laid<Json, JsonDocument>>; - /// The crate's options for a column. pub fn options(column: &Column) -> JsonOptions { JsonOptions { @@ -28,12 +38,120 @@ pub fn options(column: &Column) -> JsonOptions { } } -fn plan(column: &Column) -> JsonPlan { - Plan::value::<Value>() - .context("users/profile") - .encrypt_with_layout(Json::new(options(column))) - .build() - .expect("the plan builds") +/// A resolver that names one type, `JsonSearch`, so that a plan can name +/// it. The queries here are the JSON operators, which the engine derives; +/// the documents are sealed by [`json_document`], not through the plan. So +/// nothing here is asked to seal, open or derive. +struct Documents; + +impl TargetResolver for Documents { + fn targets(&self) -> Vec<TargetDescriptor> { + vec![TargetDescriptor::new( + "JsonSearch", + "json", + "", + Some(ValueKind::Json), + "public.eql_v4_json_search", + vec!["json".to_string()], + Some("JsonSearchQuery".to_string()), + Some("eql_v4.query_json".to_string()), + true, + None, + ) + .with_leaf_query_sql_domain("eql_v4.query_json_leaf")] + } + + fn encrypt<'a, K: 'static>( + &self, + _: &str, + _: Option<&TargetOptions>, + _: &'a KeysetCipher<'_, K>, + _: &Label, + _: &DeclaredContext, + _: ValueFrame, + ) -> Result<Pending<'a, Vec<u8>, K>, TargetError> { + unreachable!("the documents are sealed by the JSON index's own operation") + } + + fn decrypt<'a, K: 'static>( + &self, + _: &str, + _: &'a StackCipher<K>, + _: &Label, + _: &DeclaredContext, + _: &[u8], + ) -> Result<Pending<'a, ValueFrame, K>, TargetError> { + unreachable!("nothing is opened here") + } + + fn query<'a, K: 'static>( + &self, + _: &str, + _: &IndexSpec, + _: &'a KeysetCipher<'_, K>, + _: &Label, + _: &DeclaredContext, + _: ValueFrame, + ) -> Result<Pending<'a, Vec<u8>, K>, TargetError> { + unreachable!("the JSON operators are the engine's") + } +} + +/// The column as a binding's plan names it: one `json` field, `profile`, +/// under `users/profile`, storing `JsonSearch` with the column's options. +fn plan(column: &Column) -> record::Plan { + let wire = json!({ + "profile": { + "context": ["users", "profile"], + "type": "json", + "null": "sql", + "target": { + "name": "JsonSearch", + "options": {"sealed_root": column.sealed_root, "ordered": column.ordered} + } + } + }); + record::plan_with( + dynamic::frame::serde::to_vec(&wire) + .expect("the plan is a frame") + .as_protected_ref(), + &Documents, + ) + .expect("the plan builds") +} + +/// The operand of `operator` for `input`, as the data plan derives it: the +/// JSON bytes the database is sent, read as `T`, the type that serializes +/// to them, and serialized again. The two are checked to be the same JSON, +/// so what is returned is what was sent, lists in the order sent. +async fn operand<T: DeserializeOwned + Serialize>( + keyset: &KeysetCipher<'_, FakeDataKeySource>, + plan: &record::Plan, + operator: Op, + input: &Value, +) -> Value { + let input = ValueFrame::from_bytes( + dynamic::frame::serde::to_vec(input) + .expect("the input is a frame") + .as_protected_ref(), + ) + .expect("the input is a value"); + let term = dynamic::query_term(keyset, plan, "profile", operator, input, &Documents) + .expect("the field answers the operator") + .await + .expect("derives"); + let Operand::One(bytes) = term.operand() else { + panic!("{}: one operand, got {:?}", operator.name(), term.operand()) + }; + let typed: T = serde_json::from_slice(bytes).expect("the operand is its type's JSON"); + let wire = serde_json::to_value(&typed).expect("serializes"); + assert_eq!( + serde_json::from_slice::<Value>(bytes).expect("the operand is JSON"), + wire, + "{}: the typed operand is the JSON sent", + operator.name() + ); + wire } /// The rows and query terms the database would hold and receive. @@ -44,12 +162,19 @@ pub async fn world( queries: &[Query], ) -> (Vec<Value>, Vec<(QueryKind, Value)>) { let plan = plan(column); + let keyset = cipher.default_keyset(); let mut rows = Vec::with_capacity(docs.len()); for doc in docs { - let sealed = cipher.encrypt(doc).using(&plan).await.expect("seals"); + let sealed: JsonDocument = keyset + .run( + json_document::<_, Borrowed>(options(column)), + doc, + CallerContext::from(nonempty!("users").with("profile")), + ) + .await + .expect("seals"); rows.push(serde_json::to_value(&sealed).expect("serializes")); } - let keyset = cipher.default_keyset(); let mut sent = Vec::with_capacity(queries.len()); for query in queries { sent.push(match query { @@ -84,30 +209,36 @@ pub async fn world( wire["budget"] = serde_json::json!(budget); (QueryKind::Subtree, wire) } - Query::Contains(needle) => { - let term = cipher - .query(op::Contains, needle) - .on(&plan) - .await - .expect("derives"); - (QueryKind::Contains, serde_json::to_value(&term).unwrap()) - } - Query::Equals(path, value) => { - let term = cipher - .query(op::AtPath(op::Eq), &(path.clone(), value.clone())) - .on(&plan) - .await - .expect("derives"); - (QueryKind::Equals, serde_json::to_value(&term).unwrap()) - } - Query::Order(path, value) => { - let term = cipher - .query(op::AtPath(op::Gte), &(path.clone(), value.clone())) - .on(&plan) - .await - .expect("derives"); - (QueryKind::Order, serde_json::to_value(&term).unwrap()) - } + Query::Contains(needle) => ( + QueryKind::Contains, + operand::<JsonQuery>( + &keyset, + &plan, + Op::Contains, + &json!({"json": needle.to_string()}), + ) + .await, + ), + Query::Equals(path, value) => ( + QueryKind::Equals, + operand::<JsonQuery>( + &keyset, + &plan, + Op::EqAt, + &json!({"path": path, "json": value.to_string()}), + ) + .await, + ), + Query::Order(path, value) => ( + QueryKind::Order, + operand::<JsonLeafQuery>( + &keyset, + &plan, + Op::GteAt, + &json!({"path": path, "json": value.to_string()}), + ) + .await, + ), }); } (rows, sent) diff --git a/packages/stack-encrypt/tests/json_operators.rs b/packages/stack-encrypt/tests/json_operators.rs index dd75878b7d..01cbc3ba9e 100644 --- a/packages/stack-encrypt/tests/json_operators.rs +++ b/packages/stack-encrypt/tests/json_operators.rs @@ -7,8 +7,7 @@ //! //! The resolver here stores the engine's `JsonDocument` as JSON, so the //! whole path runs inside this crate. `packages/eql/tests/encryption/tests/ -//! json_tree_data_plan.rs` runs the same path through eql-bindings' table -//! and holds every operand to the typed chain's. +//! json_tree_data_plan.rs` runs the same path through eql-bindings' table. #![cfg(feature = "dynamic")] mod common; @@ -513,8 +512,8 @@ fn malformed_inputs_are_refused_with_their_reason() { ( Op::LtAt, json!({"path": "$.visits[0].at", "json": "1"}), - // A position on an ordering is the index's own refusal, the - // one the typed ordering gives. + // A position on an ordering is the index's own refusal, named + // by its term error. "JsonOrderingAtPosition", ), ( diff --git a/packages/stack-encrypt/tests/json_owned.rs b/packages/stack-encrypt/tests/json_owned.rs index af2ac81ec5..c4e947d8ed 100644 --- a/packages/stack-encrypt/tests/json_owned.rs +++ b/packages/stack-encrypt/tests/json_owned.rs @@ -1,12 +1,13 @@ -//! The JSON targets through the owned API, with no database: a +//! The JSON targets through the owned traits, with no database: a //! [`JsonPlaintext`] seals into a [`JsonDocument`], and the document, one //! entry of it ([`JsonExtracted`]) and the rows of a read ([`JsonSubtree`]) //! each open to a `JsonPlaintext`, which is wiped when it is dropped. //! //! The rows here are built as the SQL returns them, from a sealed document, //! as `json_read_checks.rs` builds them. That file holds the checks a read -//! makes on its rows. This one shows that the owned API reaches the same -//! answers and the same refusals. +//! makes on its rows. This one holds the forms a caller opens with (one +//! `JsonPlaintext`, an `Option` of one and a `Vec` of them), the answer +//! each gives and the refusals each makes. mod common; @@ -158,31 +159,6 @@ mod given_a_document { vec![IndexSpec::Json(JsonOptions::default())], ); } - - /// The owned API and the structural API write the same document: each - /// opens what the other sealed. This holds until the structural API is - /// removed. - #[tokio::test] - async fn the_owned_api_and_the_structural_api_open_each_others_documents() { - let (cipher, _) = cipher().await; - let owned = seal(&cipher, fixture()).await; - let opened: Value = cipher.decrypt_as(owned, aead()).await.expect("opens"); - assert_eq!(opened, fixture(), "sealed owned, opened structural"); - - let structural: JsonDocument = cipher - .default_keyset() - .encrypt_as(&fixture(), column()) - .await - .expect("seals"); - let opened = JsonPlaintext::decrypt_from_with_context(structural, &cipher, aead()) - .await - .expect("opens"); - assert_eq!( - opened.value(), - &fixture(), - "sealed structural, opened owned" - ); - } } mod given_the_entry_arrow_returned { diff --git a/packages/stack-encrypt/tests/json_properties.rs b/packages/stack-encrypt/tests/json_properties.rs index a7402c34cc..3b5e9807e2 100644 --- a/packages/stack-encrypt/tests/json_properties.rs +++ b/packages/stack-encrypt/tests/json_properties.rs @@ -30,10 +30,10 @@ use proptest::prelude::*; use proptest::test_runner::TestCaseError; use serde_json::{json, Map, Value}; use stack_encrypt::json::{ - JsonDocument, JsonExtracted, JsonNode, JsonOptions, JsonPath, JsonSubtree, Segment, + JsonDocument, JsonNode, JsonOptions, JsonPath, JsonPlaintext, JsonSubtree, Segment, }; use stack_encrypt::target::{AeadContext, CallerContext}; -use stack_encrypt::{nonempty, Error, StackCipher}; +use stack_encrypt::{nonempty, DecryptFrom, Error, StackCipher}; use stack_kms::FakeDataKeySource; type Cipher = StackCipher<FakeDataKeySource>; @@ -100,7 +100,28 @@ fn with_depth(doc: &JsonDocument, j: usize, d: u32) -> JsonNode { serde_json::from_value(wire).unwrap() } -/// The read of `path` in `doc`, as `Option<Value>`. +/// The document a plaintext holds. An opened plaintext is wiped when it is +/// dropped and is not compared directly, so the helpers below hand a +/// property the document itself. +fn held(opened: JsonPlaintext) -> Value { + opened.value().clone() +} + +/// The rows of a read opened as one node or none. +async fn open_option(cipher: &Cipher, rows: JsonSubtree) -> Result<Option<Value>, Error> { + Option::<JsonPlaintext>::decrypt_from_with_context(rows, cipher, aead()) + .await + .map(|opened| opened.map(held)) +} + +/// The rows of a read opened as a list, one value per node the path names. +async fn open_list(cipher: &Cipher, rows: JsonSubtree) -> Result<Vec<Value>, Error> { + Vec::<JsonPlaintext>::decrypt_from_with_context(rows, cipher, aead()) + .await + .map(|opened| opened.into_iter().map(held).collect()) +} + +/// The read of `path` in `doc`, as one node or none. async fn read( cipher: &Cipher, doc: &JsonDocument, @@ -108,8 +129,7 @@ async fn read( options: &JsonOptions, ) -> Option<Value> { let key = key(cipher, path, options, 4).await; - cipher - .decrypt_as::<Option<Value>, JsonSubtree>(key.open(rows(doc, &key)), aead()) + open_option(cipher, key.open(rows(doc, &key))) .await .unwrap_or_else(|error| panic!("{path}: {error}")) } @@ -333,9 +353,10 @@ proptest! { .await .unwrap(); let j = doc.entries().iter().position(|e| e.selector() == selector.as_bytes()).unwrap(); - let arrow = cipher - .decrypt_as::<Value, JsonExtracted>(selector.open(doc.node(j).unwrap()), aead()) + let entry = selector.open(doc.node(j).unwrap()); + let arrow = JsonPlaintext::decrypt_from_with_context(entry, &cipher, aead()) .await + .map(held) .unwrap(); prop_assert_eq!(Some(arrow), read(&cipher, &doc, &path, &options).await, "{}", path); } @@ -435,8 +456,7 @@ proptest! { let path = &paths[pick.index(paths.len())]; let k = key(&cipher, path, &options, 4).await; let shuffled = permuted(rows(&doc, &k), seed); - let opened = cipher - .decrypt_as::<Option<Value>, JsonSubtree>(k.open(shuffled), aead()) + let opened = open_option(&cipher, k.open(shuffled)) .await .unwrap_or_else(|error| panic!("{path}: {error}")); prop_assert_eq!(opened, path.select(&value).first().cloned().cloned(), "{}", path); @@ -504,9 +524,7 @@ proptest! { "a row at the depth the chain gives it" } }; - let opened = cipher - .decrypt_as::<Option<Value>, JsonSubtree>(k.open(rows), aead()) - .await; + let opened = open_option(&cipher, k.open(rows)).await; match (expected, opened) { ("aead", Err(Error::Aead)) | ("none", Ok(None)) => {} (expected, Err(Error::JsonWrongEntry { expected: got })) if got == expected => {} @@ -647,9 +665,7 @@ async fn a_wildcard_read_fails_past_its_bound_and_only_there() { let doc = seal(&cipher, &value, &options(true)).await; let root = with_depth(&doc, 0, 0); for rows in [rows(&doc, &k), vec![root]] { - let opened = cipher - .decrypt_as::<Vec<Value>, JsonSubtree>(k.open(rows), aead()) - .await; + let opened = open_list(&cipher, k.open(rows)).await; if len > bound as usize { assert!( matches!(opened, Err(Error::JsonWildcardTruncated { bound: b }) if b == bound), diff --git a/packages/stack-encrypt/tests/json_read_checks.rs b/packages/stack-encrypt/tests/json_read_checks.rs index 91ebf15e7f..e9cfe5af66 100644 --- a/packages/stack-encrypt/tests/json_read_checks.rs +++ b/packages/stack-encrypt/tests/json_read_checks.rs @@ -23,11 +23,11 @@ use std::sync::atomic::Ordering; use common::CountingSource; use serde_json::{json, Value}; use stack_encrypt::json::{ - JsonDocument, JsonExtracted, JsonNode, JsonOptions, JsonPath, JsonSelector, JsonSubtree, - JsonSubtreeKey, + JsonDocument, JsonExtracted, JsonNode, JsonOptions, JsonPath, JsonPlaintext, JsonSelector, + JsonSubtree, JsonSubtreeKey, }; use stack_encrypt::target::{AeadContext, CallerContext}; -use stack_encrypt::{nonempty, Error, StackCipher}; +use stack_encrypt::{nonempty, DecryptFrom, EncryptInto, Error, StackCipher}; type Cipher = StackCipher<CountingSource>; @@ -53,10 +53,9 @@ fn fixture() -> Value { } async fn seal(cipher: &Cipher, value: &Value) -> JsonDocument { - cipher - .default_keyset() - .encrypt_as::<Value, JsonDocument>( - value, + JsonPlaintext::from(value.clone()) + .encrypt_into_with_context( + &cipher.default_keyset(), CallerContext::from(nonempty!("users").with("profile")), ) .await @@ -155,20 +154,43 @@ fn base64(bytes: &[u8]) -> String { Base64::encode_string(bytes) } +/// The document a plaintext holds. An opened plaintext is wiped when it is +/// dropped and is not compared directly, so the helpers below hand a test +/// the document itself. +fn document(opened: JsonPlaintext) -> Value { + opened.value().clone() +} + async fn open_extracted(cipher: &Cipher, extracted: JsonExtracted) -> Result<Value, Error> { - cipher - .decrypt_as::<Value, JsonExtracted>(extracted, aead()) + JsonPlaintext::decrypt_from_with_context(extracted, cipher, aead()) .await + .map(document) } async fn open_value(cipher: &Cipher, rows: JsonSubtree) -> Result<Value, Error> { - cipher.decrypt_as::<Value, JsonSubtree>(rows, aead()).await + open_value_under(cipher, rows, aead()).await +} + +async fn open_value_under( + cipher: &Cipher, + rows: JsonSubtree, + context: AeadContext, +) -> Result<Value, Error> { + JsonPlaintext::decrypt_from_with_context(rows, cipher, context) + .await + .map(document) +} + +async fn open_option(cipher: &Cipher, rows: JsonSubtree) -> Result<Option<Value>, Error> { + Option::<JsonPlaintext>::decrypt_from_with_context(rows, cipher, aead()) + .await + .map(|opened| opened.map(document)) } async fn open_list(cipher: &Cipher, rows: JsonSubtree) -> Result<Vec<Value>, Error> { - cipher - .decrypt_as::<Vec<Value>, JsonSubtree>(rows, aead()) + Vec::<JsonPlaintext>::decrypt_from_with_context(rows, cipher, aead()) .await + .map(|opened| opened.into_iter().map(document).collect()) } mod assertions { @@ -731,12 +753,12 @@ mod given_the_rows_of_a_read { let (cipher, _) = cipher().await; let doc = seal(&cipher, &fixture()).await; let nested = key(&cipher, "$.nested").await; - let opened = cipher - .decrypt_as::<Value, JsonSubtree>( - nested.open(walk(&doc, &nested)), - AeadContext::from(nonempty!("users").with("other")), - ) - .await; + let opened = open_value_under( + &cipher, + nested.open(walk(&doc, &nested)), + AeadContext::from(nonempty!("users").with("other")), + ) + .await; assertions::aead(opened); } @@ -836,9 +858,7 @@ mod given_answers_the_check_cannot_tell_apart { async fn rows_withheld_whole_read_as_no_node() { let (cipher, _) = cipher().await; let nested = key(&cipher, "$.nested").await; - let opened = cipher - .decrypt_as::<Option<Value>, JsonSubtree>(nested.open(Vec::new()), aead()) - .await; + let opened = open_option(&cipher, nested.open(Vec::new())).await; assert_eq!( opened.unwrap(), None, @@ -849,7 +869,7 @@ mod given_answers_the_check_cannot_tell_apart { /// The four answers a read at a path with no wildcard gives, as `jsonb #>` /// gives them: an empty object, an empty array, a scalar and no node, as a -/// `Value`, an `Option<Value>` and a `Vec<Value>`. +/// `JsonPlaintext`, an `Option<JsonPlaintext>` and a `Vec<JsonPlaintext>`. mod given_a_read_of_one_node { use super::*; @@ -869,19 +889,16 @@ mod given_a_read_of_one_node { ] { let read = key(&cipher, path).await; let rows = walk(&doc, &read); - let option = cipher - .decrypt_as::<Option<Value>, JsonSubtree>(read.open(rows.clone()), aead()) - .await - .unwrap(); - assert_eq!(option, expected, "{path} as Option<Value>"); + let option = open_option(&cipher, read.open(rows.clone())).await.unwrap(); + assert_eq!(option, expected, "{path} as an Option"); let list = open_list(&cipher, read.open(rows.clone())).await.unwrap(); assert_eq!( list, expected.clone().into_iter().collect::<Vec<_>>(), - "{path} as Vec<Value>" + "{path} as a Vec" ); match (open_value(&cipher, read.open(rows)).await, expected) { - (Ok(value), Some(expected)) => assert_eq!(value, expected, "{path} as Value"), + (Ok(value), Some(expected)) => assert_eq!(value, expected, "{path} as one value"), (Err(Error::OpenedType { expected }), None) => { assert_eq!(expected, "a node at the walked path", "{path}") } diff --git a/packages/stack-encrypt/tests/keysets.rs b/packages/stack-encrypt/tests/keysets.rs index b0c8346de9..6724f2a9fc 100644 --- a/packages/stack-encrypt/tests/keysets.rs +++ b/packages/stack-encrypt/tests/keysets.rs @@ -8,7 +8,10 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::Mutex; use std::time::Duration; -use stack_encrypt::{nonempty, CipherText, Error, SealedValue, StackCipher, StackCipherText}; +use stack_encrypt::{ + nonempty, CipherText, DecryptFrom, EncryptInto, Error, SealedValue, StackCipher, + StackCipherText, +}; use stack_kms::{ DataKey, DataKeySource, DataKeyWithTag, FakeDataKeySource, GenerateKeyPayload, IdentifiedBy, IndexKey, IndexKeySource, LoadKeysetError, RetrieveKeyPayload, UnverifiedContext, @@ -623,28 +626,25 @@ async fn the_target_path_through_a_keyset_handle_is_constrained_too() { let globex = cipher.keyset(name("globex")).await.expect("globex"); // A ciphertext tree is not `Clone`; seal three, one per path. let seal = || async { - let sealed: StackCipherText = acme - .encrypt_as(&34u32, nonempty!("users/age").into()) + let sealed: StackCipherText = 34u32 + .encrypt_into_with_context(&acme, nonempty!("users/age")) .await .expect("seal"); sealed }; - let opened: u32 = acme - .decrypt_as(seal().await, nonempty!("users/age").into()) + let opened: u32 = u32::decrypt_from_with_context(seal().await, &acme, nonempty!("users/age")) .await .expect("own keyset opens"); assert_eq!(opened, 34, "the sealing keyset opens its own leaf"); - let opened: u32 = cipher - .decrypt_as(seal().await, nonempty!("users/age").into()) + let opened: u32 = u32::decrypt_from_with_context(seal().await, &cipher, nonempty!("users/age")) .await .expect("the client opens"); assert_eq!(opened, 34, "and so does the client it belongs to"); - let result: Result<u32, _> = globex - .decrypt_as(seal().await, nonempty!("users/age").into()) - .await; + let result: Result<u32, _> = + u32::decrypt_from_with_context(seal().await, &globex, nonempty!("users/age")).await; assert!( matches!(result, Err(Error::ForeignKeyset { .. })), "{result:?}" @@ -660,17 +660,17 @@ async fn a_mixed_keyset_column_opens_through_the_client_in_one_call_per_keyset() // A column whose rows belong to two tenants, interleaved. let mut column: Vec<StackCipherText> = Vec::new(); for (i, tenant) in [(1u32, &acme), (2, &globex), (3, &acme)] { - let sealed: StackCipherText = tenant - .encrypt_as(&i, nonempty!("users/age").into()) + let sealed: StackCipherText = i + .encrypt_into_with_context(tenant, nonempty!("users/age")) .await .expect("seal"); column.push(sealed); } - let opened: Vec<u32> = cipher - .decrypt_as(column, nonempty!("users/age").into()) - .await - .expect("open"); + let opened: Vec<u32> = + Vec::<u32>::decrypt_from_with_context(column, &cipher, nonempty!("users/age")) + .await + .expect("open"); assert_eq!( opened, vec![1, 2, 3], @@ -690,14 +690,15 @@ async fn a_mixed_keyset_column_does_not_open_through_a_keyset_handle() { let globex = cipher.keyset(name("globex")).await.expect("globex"); let mut column: Vec<StackCipherText> = Vec::new(); for tenant in [&acme, &globex] { - let sealed: StackCipherText = tenant - .encrypt_as(&1u32, nonempty!("users/age").into()) + let sealed: StackCipherText = 1u32 + .encrypt_into_with_context(tenant, nonempty!("users/age")) .await .expect("seal"); column.push(sealed); } - let result: Result<Vec<u32>, _> = acme.decrypt_as(column, nonempty!("users/age").into()).await; + let result: Result<Vec<u32>, _> = + Vec::<u32>::decrypt_from_with_context(column, &acme, nonempty!("users/age")).await; assert!( matches!(result, Err(Error::ForeignKeyset { .. })), "{result:?}" @@ -783,7 +784,7 @@ async fn reader() -> StackCipher<Reader> { #[tokio::test] async fn opening_an_open_root_json_document_loads_its_keyset_once() { use serde_json::json; - use stack_encrypt::json::{JsonDocument, JsonOptions}; + use stack_encrypt::json::{JsonDocument, JsonOptions, JsonPlaintext}; use stack_encrypt::target::{json_document, AeadContext, Borrowed, CallerContext}; let value = json!({"a": [1, {"b": "two"}], "c": null}); @@ -810,20 +811,18 @@ async fn opening_an_open_root_json_document_loads_its_keyset_once() { let reader = reader().await; assert_eq!(reader.kms().loads(), 1, "the reader has its default only"); let sealed: JsonDocument = seal(true).await; - let opened: serde_json::Value = reader - .decrypt_as(sealed, aead()) + let opened = JsonPlaintext::decrypt_from_with_context(sealed, &reader, aead()) .await .expect("a sealed root opens"); - assert_eq!(opened, value); + assert_eq!(opened.value(), &value); assert_eq!(reader.kms().loads(), 1, "a sealed root needs no index key"); let open: JsonDocument = seal(false).await; for _ in 0..2 { - let opened: serde_json::Value = reader - .decrypt_as(open.clone(), aead()) + let opened = JsonPlaintext::decrypt_from_with_context(open.clone(), &reader, aead()) .await .expect("an open root opens through a cipher that had not loaded its keyset"); - assert_eq!(opened, value); + assert_eq!(opened.value(), &value); } assert_eq!( reader.kms().loads(), @@ -832,9 +831,8 @@ async fn opening_an_open_root_json_document_loads_its_keyset_once() { ); let acme = reader.keyset(name("acme")).await.expect("select"); assert_eq!(acme.keyset_id(), open.header().keyset_id()); - let opened: serde_json::Value = acme - .decrypt_as(open.clone(), aead()) + let opened = JsonPlaintext::decrypt_from_with_context(open.clone(), &acme, aead()) .await .expect("and through the keyset's own cipher"); - assert_eq!(opened, value); + assert_eq!(opened.value(), &value); } diff --git a/packages/stack-encrypt/tests/match_short_words.rs b/packages/stack-encrypt/tests/match_short_words.rs index 6d277b27fd..b12ae88991 100644 --- a/packages/stack-encrypt/tests/match_short_words.rs +++ b/packages/stack-encrypt/tests/match_short_words.rs @@ -4,9 +4,9 @@ //! //! - **Round trip.** "Ng" stored is found by the query "Ng"; "Mike Ng" is //! found by "Ng" and by "Mike Ng"; "Nguyen" is not found by "Ng". -//! - **Identity.** The option is part of the index: a query asked under -//! other options than the field declares is refused, and options the -//! index cannot use are refused on both sides. +//! - **Identity.** The option is part of the index, which is the field's +//! type: a field is stored and queried under the options its type +//! declares, and options the index cannot use are refused on both sides. //! - **Containment.** For random text, the stored filter holds the filter of //! any run of its whole words. //! - **Calibration.** A one-word query sets as many bits as a one-trigram @@ -19,14 +19,11 @@ use proptest::prelude::*; use proptest::strategy::ValueTree; use proptest::test_runner::{Config, RngAlgorithm, RngSeed, TestRng, TestRunner}; use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::op::{self, Probabilistic}; -use stack_encrypt::plan::{Field, FieldSchema, Fields, Indexed, PlanError, ValuePlan}; +use stack_encrypt::op::Probabilistic; use stack_encrypt::sem::{MatchConfig, MatchOptions, MatchQuery, MatchTerms, TermError, Tokenizer}; -use stack_encrypt::target::{Encrypted, IndexSpec}; -use stack_encrypt::{nonempty, Error, Match, Plan, StackCipher}; - -/// A one-value plan of text under one match index of the configuration `C`. -type MatchPlan<C> = ValuePlan<String, Indexed<Match<C>>>; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::{nonempty, EncryptInto, Encrypted, Error, KeysetCipher}; +use zeroize::Zeroize; /// 3-grams, downcased, with short-word tokens: the default index with the /// option on. @@ -53,46 +50,32 @@ impl MatchConfig for StandardShortWords { } } -type Cipher = StackCipher<FakeDataKeySource>; +type Keyset<'a> = KeysetCipher<'a, FakeDataKeySource>; -fn plan() -> MatchPlan<ShortWords> { - Plan::context("people/name") - .with::<String, _>(Match::<ShortWords>::new()) - .build() - .expect("a valid plan") +#[derive(Clone, Zeroize)] +struct Person { + name: String, } -async fn store( - cipher: &Cipher, - plan: &MatchPlan<ShortWords>, - text: &str, -) -> MatchTerms<ShortWords> { - let stored: Encrypted<MatchTerms<ShortWords>> = cipher - .encrypt(&text.to_owned()) - .using(plan) - .await - .unwrap_or_else(|error| panic!("{text:?} stores: {error}")); - stored.terms +/// A person whose name is under the short-word match index. +#[derive(EncryptTarget)] +#[stash(struct = Person, scope = "people")] +struct EncryptedPerson { + name: Encrypted<MatchTerms<ShortWords>>, } -async fn query( - cipher: &Cipher, - plan: &MatchPlan<ShortWords>, - text: &str, -) -> Result<MatchQuery<ShortWords>, Error> { - cipher - .query(op::Matches, text) - .via(Match::<ShortWords>::new()) - .on(plan) +/// The stored filter of `text` as a person's name. +async fn store(keyset: &Keyset<'_>, text: &str) -> MatchTerms<ShortWords> { + let stored: EncryptedPerson = Person { name: text.into() } + .encrypt_into(keyset) .await + .unwrap_or_else(|error| panic!("{text:?} stores: {error}")); + stored.name.terms } -fn plan_error<T>(result: Result<T, Error>) -> PlanError { - match result { - Err(Error::Plan(error)) => error, - Err(other) => panic!("expected a plan error, got {other:?}"), - Ok(_) => panic!("expected a plan error, got a value"), - } +/// A match query of `text` on a person's name, from the field's marker. +async fn query(keyset: &Keyset<'_>, text: &str) -> Result<MatchQuery<ShortWords>, Error> { + EncryptedPerson::NAME.matches(text).encrypt(keyset).await } mod given_short_words { @@ -101,15 +84,13 @@ mod given_short_words { #[tokio::test] async fn a_two_letter_value_stores_one_token_and_the_two_letter_query_finds_it() { let cipher = stack_cipher().await; - let plan = plan(); - let stored = store(&cipher, &plan, "Ng").await; + let keyset = cipher.default_keyset(); + let stored = store(&keyset, "Ng").await; assert!( !stored.positions().is_empty(), "\"Ng\" stores a token, not the empty filter" ); - let ng = query(&cipher, &plan, "Ng") - .await - .expect("\"Ng\" is a query"); + let ng = query(&keyset, "Ng").await.expect("\"Ng\" is a query"); assert!(stored.matches(&ng), "the query \"Ng\" finds \"Ng\""); assert!(ng.verify("Ng"), "and the row verifies"); assert_eq!( @@ -122,8 +103,8 @@ mod given_short_words { #[tokio::test] async fn a_two_letter_query_finds_the_word_and_not_a_longer_word_it_begins() { let cipher = stack_cipher().await; - let plan = plan(); - let ng = query(&cipher, &plan, "ng").await.expect("a query"); + let keyset = cipher.default_keyset(); + let ng = query(&keyset, "ng").await.expect("a query"); for (text, found) in [ ("Ng", true), ("Mike Ng", true), @@ -133,7 +114,7 @@ mod given_short_words { ("Mike Nguyen", false), ("Long", false), ] { - let stored = store(&cipher, &plan, text).await; + let stored = store(&keyset, text).await; // The filter alone: a row without the word could hold its bits // by chance, but under the test key and these rows none does, // so a stored side that set the word's bits for a longer word @@ -154,12 +135,12 @@ mod given_short_words { #[tokio::test] async fn a_query_of_words_and_ngrams_needs_every_short_word_whole() { let cipher = stack_cipher().await; - let plan = plan(); - let mike_ng = query(&cipher, &plan, "Mike Ng").await.expect("a query"); - let stored = store(&cipher, &plan, "Mike Ng").await; + let keyset = cipher.default_keyset(); + let mike_ng = query(&keyset, "Mike Ng").await.expect("a query"); + let stored = store(&keyset, "Mike Ng").await; assert!(stored.matches(&mike_ng), "\"Mike Ng\" finds itself"); assert!(mike_ng.verify("mike ng"), "case folded"); - let longer = store(&cipher, &plan, "Mike Nguyen").await; + let longer = store(&keyset, "Mike Nguyen").await; assert!( !mike_ng.verify("Mike Nguyen"), "the query's short word must be a whole word of the row" @@ -168,7 +149,7 @@ mod given_short_words { !longer.matches(&mike_ng), "and the filter says so: \"Mike Nguyen\" has no word token ng" ); - let mike = query(&cipher, &plan, "Mike").await.expect("a query"); + let mike = query(&keyset, "Mike").await.expect("a query"); assert!( longer.matches(&mike) && stored.matches(&mike), "a query of trigrams alone finds both" @@ -178,15 +159,15 @@ mod given_short_words { #[tokio::test] async fn a_query_that_cuts_nothing_is_still_refused() { let cipher = stack_cipher().await; - let plan = plan(); + let keyset = cipher.default_keyset(); for text in ["N", "", " ", "n "] { - let refused = query(&cipher, &plan, text).await; + let refused = query(&keyset, text).await; assert!( matches!(refused, Err(Error::Term(TermError::EmptyTermText))), "{text:?}: {refused:?}" ); } - let stored = store(&cipher, &plan, "N").await; + let stored = store(&keyset, "N").await; assert!( stored.positions().is_empty(), "a one-letter value is still the empty filter" @@ -195,103 +176,82 @@ mod given_short_words { /// A person with one name under each index: the short-word index on /// `name`, the default on `alias`. - struct Person { + #[derive(Clone, Zeroize)] + struct Aliased { name: String, alias: String, } - impl Fields for Person { - fn field_names(&self) -> Vec<&str> { - vec!["name", "alias"] - } - fn schema() -> Option<Vec<FieldSchema>> { - Some(vec![ - FieldSchema::of::<String>("name"), - FieldSchema::of::<String>("alias"), - ]) - } - } - impl Field<String> for Person { - fn field(&self, name: &str) -> Option<&String> { - match name { - "name" => Some(&self.name), - "alias" => Some(&self.alias), - _ => None, - } - } + + #[derive(EncryptTarget)] + #[stash(struct = Aliased, scope = "aliased")] + struct EncryptedAliased { + name: Encrypted<MatchTerms<ShortWords>>, + alias: Encrypted<MatchTerms>, } - /// The option is part of the index: asking a field for a match index - /// under the other setting is refused, as a mismatched `k` is. + /// The option is part of the index, and a field's index is its type: + /// each field is stored and queried under its own setting. The + /// two-letter word is a token of the short-word field, and cuts nothing + /// under the default field. #[tokio::test] - async fn a_query_under_other_options_than_the_field_declares_is_refused() { + async fn a_field_is_queried_under_the_options_its_type_declares() { let cipher = stack_cipher().await; - let plan: Plan<Person, FakeDataKeySource> = Plan::context("people") - .fields() - .encrypt_index::<String>("name", Match::<ShortWords>::new()) - .encrypt_index::<String>("alias", Match) - .build() - .expect("a valid plan"); - let name = plan.field("name").expect("declared"); - let alias = plan.field("alias").expect("declared"); - assert_eq!( - plan_error(cipher.query(op::Matches, "Mike").on(&name).await), - PlanError::IndexOptions { - field: "name".into(), - declared: IndexSpec::Match(ShortWords::options()), - asked: IndexSpec::Match(MatchOptions::default()), - }, - "the default index is not the field's" - ); - assert_eq!( - plan_error( - cipher - .query(op::Matches, "Mike") - .via(Match::<ShortWords>::new()) - .on(&alias) - .await - ), - PlanError::IndexOptions { - field: "alias".into(), - declared: IndexSpec::Match(MatchOptions::default()), - asked: IndexSpec::Match(ShortWords::options()), - }, - "nor is the short-word index a default field's" - ); - let found: MatchQuery<ShortWords> = cipher - .query(op::Matches, "Ng") - .via(Match::<ShortWords>::new()) - .on(&name) + let keyset = cipher.default_keyset(); + let stored: EncryptedAliased = Aliased { + name: "Mike Ng".into(), + alias: "Mike Ng".into(), + } + .encrypt_into(&keyset) + .await + .expect("stores"); + + let found: MatchQuery<ShortWords> = EncryptedAliased::NAME + .matches("Ng") + .encrypt(&keyset) .await .expect("the field's own options answer"); - let mut row = cipher - .encrypt(&Person { - name: "Mike Ng".into(), - alias: "Mike Ng".into(), - }) - .using(&plan) + assert!(stored.name.terms.matches(&found), "and find the row"); + + let refused = EncryptedAliased::ALIAS.matches("Ng").encrypt(&keyset).await; + assert!( + matches!(refused, Err(Error::Term(TermError::EmptyTermText))), + "the default index has no token for a two-letter word: {refused:?}" + ); + let mike: MatchQuery = EncryptedAliased::ALIAS + .matches("Mike") + .encrypt(&keyset) .await - .expect("stores"); - let stored: Encrypted<MatchTerms<ShortWords>> = row.take("name").expect("a name"); - assert!(stored.terms.matches(&found), "and find the row"); + .expect("a trigram query"); + assert!(stored.alias.terms.matches(&mike)); + assert!( + mike.verify("Mike Ng") && !mike.verify("Ng"), + "the default index verifies by trigrams alone" + ); + } + + /// A person whose name is under options the match index cannot use. + #[derive(EncryptTarget)] + #[stash(struct = Person, scope = "people")] + #[allow(dead_code)] + struct Unusable { + name: Encrypted<MatchTerms<StandardShortWords>>, } #[tokio::test] async fn options_the_index_cannot_use_are_refused_stored_and_queried() { let cipher = stack_cipher().await; - let plan: MatchPlan<StandardShortWords> = Plan::context("people/name") - .with::<String, _>(Match::<StandardShortWords>::new()) - .build() - .expect("the plan does not read the options"); - let stored = cipher.encrypt(&"Mike Ng".to_owned()).using(&plan).await; + let keyset = cipher.default_keyset(); + let stored: Result<Unusable, Error> = Person { + name: "Mike Ng".into(), + } + .encrypt_into(&keyset) + .await; assert!( matches!(stored, Err(Error::Term(TermError::InvalidOptions(_)))), - "stored: {stored:?}" + "stored: {:?}", + stored.err() ); - let queried = cipher - .query(op::Matches, "Mike Ng") - .via(Match::<StandardShortWords>::new()) - .on(&plan) - .await; + let queried = Unusable::NAME.matches("Mike Ng").encrypt(&keyset).await; assert!( matches!(queried, Err(Error::Term(TermError::InvalidOptions(_)))), "queried: {queried:?}" @@ -368,9 +328,9 @@ mod given_random_text { runtime().block_on(async { let cipher = stack_cipher().await; - let plan = plan(); - let stored = store(&cipher, &plan, &text).await; - match query(&cipher, &plan, asked).await { + let keyset = cipher.default_keyset(); + let stored = store(&keyset, &text).await; + match query(&keyset, asked).await { Ok(query) => { prop_assert!( stored.matches(&query), diff --git a/packages/stack-encrypt/tests/op.rs b/packages/stack-encrypt/tests/op.rs index 26a63850c6..5d9b691b45 100644 --- a/packages/stack-encrypt/tests/op.rs +++ b/packages/stack-encrypt/tests/op.rs @@ -1,21 +1,35 @@ //! Operator-typed queries (`stack_encrypt::op`, ADR-0009): a query names an -//! operator, the plan finds the index that answers it, and the term that -//! comes back is the query term for that operator, which for a match index -//! is not the stored term. +//! operator through the marker of a record field, the field's target type +//! names the index that answers it, and the term that comes back is the +//! query term for that operator, which for a match index is not the stored +//! term. +//! +//! The records at the top of the file give the fields the tests query: a +//! field's type says which indexes it stores, and so which operators its +//! marker has. `tests/markers.rs` states what a marker is and how it takes +//! its scope; this file states what each operator sends, what it refuses, +//! and how a probabilistic term is verified and sized. mod common; -use common::{loads_counting_cipher, stack_cipher}; -use stack_encrypt::kms::FakeDataKeySource; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::Arc; + +use common::{stack_cipher, CountingSource}; +use stack_encrypt::kms::IdentifiedBy; +use stack_encrypt::marker::Root; use stack_encrypt::op::{self, Limit, Probabilistic, Stats}; -use stack_encrypt::plan::{Field, FieldSchema, Fields, PlanError}; use stack_encrypt::sem::{ EqualityTerm, MatchConfig, MatchOptions, MatchQuery, MatchTerms, OpeTerm, OreTerm, RangeQuery, TermError, Tokenizer, }; use stack_encrypt::target::{ - CallerContext, ConsumeSource, EncryptFrom, Encrypted, Encryption, Index, IndexSpec, + CallerContext, ConsumeSource, DecryptSource, EncryptTarget, Encryption, Index, IndexSpec, + NoScope, Scope, +}; +use stack_encrypt::{ + nonempty, DecryptFrom, EncryptInto, Encrypted, Equality, Error, Label, Marker, StackCipher, }; -use stack_encrypt::{Equality, Error, Label, Match, NonEmpty, Ope, Ore, OreBlock, Plan}; +use zeroize::Zeroize; /// A match configuration that keeps case: its index cuts stored text and /// queries as written, so `Nguyen` and `nguyen` are different tokens. @@ -40,31 +54,50 @@ impl MatchConfig for ZeroGram { } } -#[derive(Clone)] -struct Person { - email: String, - name: String, +/// A match configuration with the smallest filter allowed, so a long value +/// sets nearly every bit and false positives are easy to find. +struct Tiny; +impl MatchConfig for Tiny { + fn options() -> MatchOptions { + MatchOptions { + m: 32, + ..Default::default() + } + } } -impl Fields for Person { - fn field_names(&self) -> Vec<&str> { - vec!["email", "name"] - } - fn schema() -> Option<Vec<FieldSchema>> { - Some(vec![ - FieldSchema::of::<String>("email"), - FieldSchema::of::<String>("name"), - ]) +/// Whole words, as the v1 match indexer's standard tokenizer splits them. +struct Words; +impl MatchConfig for Words { + fn options() -> MatchOptions { + MatchOptions { + tokenizer: stack_encrypt::sem::Tokenizer::Standard, + ..Default::default() + } } } -impl Field<String> for Person { - fn field(&self, name: &str) -> Option<&String> { - match name { - "email" => Some(&self.email), - "name" => Some(&self.name), - _ => None, + +/// EQL v4's filter size and slices (`k = 6`, `m = 2048`; its match types +/// also turn on short words, which this prior's arithmetic does not need), +/// with a notes column's prior. +struct Notes; +impl MatchConfig for Notes { + fn options() -> MatchOptions { + MatchOptions { + k: 6, + m: 2048, + ..Default::default() } } + fn expected_tokens() -> Option<u32> { + Some(200) + } +} + +#[derive(Clone, Zeroize)] +struct Person { + email: String, + name: String, } fn person() -> Person { @@ -74,50 +107,214 @@ fn person() -> Person { } } -fn people_plan() -> Plan<Person, FakeDataKeySource> { - Plan::context("people") - .fields() - .encrypt_index::<String>("email", (Equality, Match)) - .encrypt_index::<String>("name", Match::<Exact>::new()) - .build() - .expect("a valid plan") +/// A person: the email under the equality index and the default match +/// index, the name under a match index that keeps case. +#[derive(EncryptTarget, DecryptSource)] +#[stash(struct = Person, scope = "people")] +struct EncryptedPerson { + email: Encrypted<(EqualityTerm, MatchTerms)>, + name: Encrypted<MatchTerms<Exact>>, +} + +/// The same person under the same labels, in a record whose caller +/// supplies a tenant scope on every call. +#[derive(EncryptTarget)] +#[stash(struct = Person, scope = "people", scope(tenant: u64))] +#[allow(dead_code)] +struct TenantPerson { + email: Encrypted<(EqualityTerm, MatchTerms)>, + name: Encrypted<MatchTerms<Exact>>, +} + +#[derive(Clone, Zeroize)] +struct Contact { + email: String, + bio: String, + phone: String, } -fn plan_error<T>(result: Result<T, Error>) -> PlanError { - match result { - Err(Error::Plan(error)) => error, - Err(other) => panic!("expected a plan error, got {other:?}"), - Ok(_) => panic!("expected a plan error, got a value"), +fn contact() -> Contact { + Contact { + email: "bob@example.com".into(), + bio: "bob builds bridges".into(), + phone: "555 0100".into(), } } +/// A record whose fields are each one term: two terms alone, with no +/// ciphertext, and one beside its ciphertext. +#[derive(EncryptTarget)] +#[stash(struct = Contact, scope = "contacts")] +struct EncryptedContact { + email: EqualityTerm, + bio: MatchTerms, + phone: Encrypted<EqualityTerm>, +} + +#[derive(Clone, Zeroize)] +struct Note { + body: String, +} + +fn note(body: &str) -> Note { + Note { body: body.into() } +} + +/// A note under a 32-bit filter. +#[derive(EncryptTarget)] +#[stash(struct = Note, scope = "notes")] +struct TinyNote { + body: Encrypted<MatchTerms<Tiny>>, +} + +/// A note cut into whole words. +#[derive(EncryptTarget)] +#[stash(struct = Note, scope = "notes")] +struct WordNote { + body: Encrypted<MatchTerms<Words>>, +} + +/// A note whose index declares a prior. +#[derive(EncryptTarget)] +#[stash(struct = Note, scope = "notes")] +#[allow(dead_code)] +struct PriorNote { + body: Encrypted<MatchTerms<Notes>>, +} + +/// A note under options the match index cannot use. +#[derive(EncryptTarget)] +#[stash(struct = Note, scope = "notes")] +#[allow(dead_code)] +struct ZeroGramNote { + body: Encrypted<MatchTerms<ZeroGram>>, +} + +#[derive(Clone, Zeroize)] +struct Profile { + age: u32, + surname: String, + score: f64, +} + +fn profile(age: u32) -> Profile { + Profile { + age, + surname: "Nguyen".into(), + score: 1.5, + } +} + +/// A profile whose order index is ORE: an integer beside an equality +/// term, text and a float. +#[derive(EncryptTarget)] +#[stash(struct = Profile, scope = "profiles")] +#[allow(dead_code)] +struct OreProfile { + age: Encrypted<(EqualityTerm, OreTerm<u32>)>, + surname: Encrypted<OreTerm<String>>, + score: Encrypted<OreTerm<f64>>, +} + +/// The same profile under the same labels, whose order index is OPE. +#[derive(EncryptTarget)] +#[stash(struct = Profile, scope = "profiles")] +#[allow(dead_code)] +struct OpeProfile { + age: Encrypted<(EqualityTerm, OpeTerm<u32>)>, + surname: Encrypted<OpeTerm<String>>, + score: Encrypted<OpeTerm<f64>>, +} + +#[derive(Clone, Zeroize)] +struct Member { + age: u32, + balance: i64, +} + +/// A member whose fields are each one order term: an OPE term beside its +/// ciphertext, and an ORE term alone, of a signed integer. +#[derive(EncryptTarget)] +#[stash(struct = Member, scope = "members")] +struct EncryptedMember { + age: Encrypted<OpeTerm<u32>>, + balance: OreTerm<i64>, +} + +/// The requests a cipher has sent its key source. +struct KeyRequests { + loads: Arc<AtomicUsize>, + generates: Arc<AtomicUsize>, + retrieves: Arc<AtomicUsize>, +} + +impl KeyRequests { + /// Keyset loads, data keys generated and data keys retrieved. + fn total(&self) -> usize { + [&self.loads, &self.generates, &self.retrieves] + .into_iter() + .map(|count| count.load(Ordering::SeqCst)) + .sum() + } +} + +/// A cipher that counts the requests it sends its key source. The count +/// starts after the cipher has loaded its default keyset. +async fn counted_cipher() -> (StackCipher<CountingSource>, KeyRequests) { + let source = CountingSource::new(); + let loads = source.loads(); + let (generates, retrieves) = source.counters(); + let cipher = StackCipher::builder() + .kms(source) + .init() + .await + .expect("build cipher"); + loads.store(0, Ordering::SeqCst); + let requests = KeyRequests { + loads, + generates, + retrieves, + }; + (cipher, requests) +} + +/// A marker written by hand for the label `scope/identity`, of the target +/// type `T` and the scope type `C`. No record declares it, so it states +/// nothing about a stored value: the tests that use it are about what the +/// index of `T` is told of the query's context. +fn marker_of<T, C>(scope: &'static str, identity: &'static str) -> Marker<Root<()>, T, C> { + Marker::new(identity, scope, identity, || { + unreachable!("a query does not read its record's contexts") + }) +} + #[tokio::test] async fn an_eq_query_is_the_stored_equality_term() { let cipher = stack_cipher().await; - let plan = people_plan(); - let mut row = cipher.encrypt(&person()).using(&plan).await.unwrap(); - let email: Encrypted<(EqualityTerm, MatchTerms)> = row.take("email").unwrap(); + let keyset = cipher.default_keyset(); + let row: EncryptedPerson = person().encrypt_into(&keyset).await.unwrap(); - let query: EqualityTerm = cipher - .query(op::Eq, "bob@example.com") - .on(&plan.field("email").unwrap()) + let query: EqualityTerm = EncryptedPerson::EMAIL + .eq("bob@example.com") + .encrypt(&keyset) .await .unwrap(); - assert_eq!(query, email.terms.0, "HMAC equality sends what it stores"); + assert_eq!( + query, row.email.terms.0, + "HMAC equality sends what it stores" + ); } #[tokio::test] async fn a_matches_query_is_a_filter_over_the_query_value_that_the_stored_filter_holds() { let cipher = stack_cipher().await; - let plan = people_plan(); - let mut row = cipher.encrypt(&person()).using(&plan).await.unwrap(); - let email: Encrypted<(EqualityTerm, MatchTerms)> = row.take("email").unwrap(); - let stored = email.terms.1; - let email_plan = plan.field("email").unwrap(); - - let query: MatchQuery = cipher - .query(op::Matches, "BOB") - .on(&email_plan) + let keyset = cipher.default_keyset(); + let row: EncryptedPerson = person().encrypt_into(&keyset).await.unwrap(); + let stored = row.email.terms.1; + + let query: MatchQuery = EncryptedPerson::EMAIL + .matches("BOB") + .encrypt(&keyset) .await .unwrap(); assert!(stored.matches(&query), "a case-folding index matches BOB"); @@ -128,251 +325,120 @@ async fn a_matches_query_is_a_filter_over_the_query_value_that_the_stored_filter // The query's filter is the filter the index would store for the same // text, under the same label. - let mut same_text = cipher - .encrypt(&Person { - email: "BOB".into(), - name: "x".into(), - }) - .using(&plan) - .await - .unwrap(); - let as_stored: Encrypted<(EqualityTerm, MatchTerms)> = same_text.take("email").unwrap(); - assert_eq!(query.to_bytes(), as_stored.terms.1.to_bytes()); - assert_eq!(query.positions(), as_stored.terms.1.positions()); + let same_text: EncryptedPerson = Person { + email: "BOB".into(), + name: "x".into(), + } + .encrypt_into(&keyset) + .await + .unwrap(); + let as_stored = same_text.email.terms.1; + assert_eq!(query.to_bytes(), as_stored.to_bytes()); + assert_eq!(query.positions(), as_stored.positions()); assert_eq!(query.clone(), query); - let elsewhere: MatchQuery = cipher - .query(op::Matches, "zzz") - .on(&email_plan) + let elsewhere: MatchQuery = EncryptedPerson::EMAIL + .matches("zzz") + .encrypt(&keyset) .await .unwrap(); assert!(!stored.matches(&elsewhere)); assert_ne!(elsewhere, query); } +/// A field's match index is its type's, so a query on the field is cut +/// under the field's own configuration and no other. #[tokio::test] -async fn a_match_index_with_its_own_configuration_is_named_with_via() { +async fn a_match_field_with_its_own_configuration_is_queried_under_it() { let cipher = stack_cipher().await; - let plan = people_plan(); - let mut row = cipher.encrypt(&person()).using(&plan).await.unwrap(); - let name: Encrypted<MatchTerms<Exact>> = row.take("name").unwrap(); - let name_plan = plan.field("name").unwrap(); - - let exact = |value: &'static str| { - cipher - .query(op::Matches, value) - .via(Match::<Exact>::new()) - .on(&name_plan) - }; - let hit: MatchQuery<Exact> = exact("Nguyen").await.unwrap(); - assert!(name.terms.matches(&hit)); - let miss: MatchQuery<Exact> = exact("nguyen").await.unwrap(); - assert!( - !name.terms.matches(&miss), - "a case-sensitive index does not fold the query" - ); - - // The operator's default index is the default configuration, which - // this field does not declare. - assert_eq!( - plan_error(cipher.query(op::Matches, "Nguyen").on(&name_plan).await), - PlanError::IndexOptions { - field: "name".into(), - declared: IndexSpec::Match(Exact::options()), - asked: IndexSpec::Match(MatchOptions::default()), - } - ); -} + let keyset = cipher.default_keyset(); + let row: EncryptedPerson = person().encrypt_into(&keyset).await.unwrap(); + let name = row.name.terms; -#[tokio::test] -async fn an_operator_the_field_has_no_index_for_is_refused_when_awaited() { - let cipher = stack_cipher().await; - let plan = people_plan(); - assert_eq!( - plan_error( - cipher - .query(op::Eq, "Alice") - .on(&plan.field("name").unwrap()) - .await - ), - PlanError::IndexNotDeclared { - field: "name".into(), - index: "eq" - } - ); -} - -#[tokio::test] -async fn a_one_value_plan_answers_from_its_indexes_by_type() { - let cipher = stack_cipher().await; - let plan = Plan::context("users/email") - .with::<String, _>((Equality, Match)) - .build() - .unwrap(); - let stored: Encrypted<(EqualityTerm, MatchTerms)> = cipher - .encrypt(&"bob@example.com".to_owned()) - .using(&plan) - .await - .unwrap(); - - let eq = cipher - .query(op::Eq, "bob@example.com") - .on(&plan) - .await - .unwrap(); - assert_eq!(eq, stored.terms.0); - let matches = cipher - .query(op::Matches, "example") - .on(&plan) - .await - .unwrap(); - assert!(stored.terms.1.matches(&matches)); - - let exact_plan = Plan::context("users/name") - .with::<String, _>(Match::<Exact>::new()) - .build() - .unwrap(); - let name: Encrypted<MatchTerms<Exact>> = cipher - .encrypt(&"Alice".to_owned()) - .using(&exact_plan) + let hit: MatchQuery<Exact> = EncryptedPerson::NAME + .matches("Nguyen") + .encrypt(&keyset) .await .unwrap(); - let query = cipher - .query(op::Matches, "Ali") - .via(Match::<Exact>::new()) - .on(&exact_plan) + assert!(name.matches(&hit)); + let miss: MatchQuery<Exact> = EncryptedPerson::NAME + .matches("nguyen") + .encrypt(&keyset) .await .unwrap(); - assert!(name.terms.matches(&query)); + assert!( + !name.matches(&miss), + "a case-sensitive index does not fold the query" + ); } #[tokio::test] async fn a_target_answers_an_operator_through_the_index_it_names() { let cipher = stack_cipher().await; - let eq_plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<EqualityTerm>() - .build() - .unwrap(); - let stored: EqualityTerm = cipher - .encrypt(&"bob@example.com".to_owned()) - .using(&eq_plan) - .await - .unwrap(); - let query = cipher - .query(op::Eq, "bob@example.com") - .on(&eq_plan) - .await - .unwrap(); - assert_eq!(query, stored); + let keyset = cipher.default_keyset(); + let stored: EncryptedContact = contact().encrypt_into(&keyset).await.unwrap(); - let match_plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<MatchTerms>() - .build() - .unwrap(); - let stored: MatchTerms = cipher - .encrypt(&"bob@example.com".to_owned()) - .using(&match_plan) + let query = EncryptedContact::EMAIL + .eq("bob@example.com") + .encrypt(&keyset) .await .unwrap(); - let query = cipher - .query(op::Matches, "bob") - .on(&match_plan) + assert_eq!(query, stored.email); + + let query = EncryptedContact::BIO + .matches("bob") + .encrypt(&keyset) .await .unwrap(); - assert!(stored.matches(&query)); + assert!(stored.bio.matches(&query)); } #[tokio::test] async fn a_target_with_several_terms_answers_through_the_term_that_answers() { let cipher = stack_cipher().await; - let plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<Encrypted<(EqualityTerm, MatchTerms)>>() - .build() - .unwrap(); - let stored = cipher - .encrypt(&"bob@example.com".to_owned()) - .using(&plan) + let keyset = cipher.default_keyset(); + let stored: EncryptedPerson = person().encrypt_into(&keyset).await.unwrap(); + let (equality, matching) = &stored.email.terms; + + // Each operator has one term that answers it. + let eq = EncryptedPerson::EMAIL + .eq("bob@example.com") + .encrypt(&keyset) .await .unwrap(); - let eq = cipher - .query(op::Eq, "bob@example.com") - .via(Equality) - .on(&plan) + assert_eq!(&eq, equality); + let matches = EncryptedPerson::EMAIL + .matches("bob") + .encrypt(&keyset) .await .unwrap(); - assert_eq!(eq, stored.terms.0); - let matches = cipher - .query(op::Matches, "bob") - .via(Match) - .on(&plan) - .await - .unwrap(); - assert!(stored.terms.1.matches(&matches)); + assert!(matching.matches(&matches)); - // Each operator has one term that answers it, so `via` is optional. - let eq = cipher - .query(op::Eq, "bob@example.com") - .on(&plan) + // One term answers what the term answers. + let one_term: EncryptedContact = contact().encrypt_into(&keyset).await.unwrap(); + let eq = EncryptedContact::PHONE + .eq("555 0100") + .encrypt(&keyset) .await .unwrap(); - assert_eq!(eq, stored.terms.0); - let matches = cipher.query(op::Matches, "bob").on(&plan).await.unwrap(); - assert!(stored.terms.1.matches(&matches)); - - // An index the target does not declare is refused when awaited. - let one_term = Plan::value::<String>() - .context("users/email") - .encrypt_into::<Encrypted<EqualityTerm>>() - .build() - .unwrap(); - assert_eq!( - plan_error( - cipher - .query(op::Matches, "bob") - .via(Match) - .on(&one_term) - .await - ), - PlanError::IndexNotDeclared { - field: "the value".into(), - index: "match" - } - ); - let eq = cipher.query(op::Eq, "bob").on(&one_term).await; - assert!(eq.is_ok(), "one term answers what the term answers"); + assert_eq!(eq, one_term.phone.terms); } -/// A target sealed under a context of its own, a `NonEmpty<Label>` rather -/// than the caller's context, as an EQL type is sealed under its column. The -/// label names that context, so the type opts in to queries on its plan -/// (`op::LabelledTarget`); it accepts no `CallerContext`, so no plan can -/// encrypt into it. +/// A target type that no record here encrypts into, queried through a +/// marker written by hand, as an EQL type is queried under its column. It +/// states that its terms are derived under the marker's label +/// (`op::LabelledTarget`) and names the index that answers `Eq` for it. A +/// query derives through that index alone and never runs the target's own +/// encryption. struct Tagged; -impl EncryptFrom<String> for Tagged { - type Context = NonEmpty<Label>; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> - where - String: 's, - { - <EqualityTerm as EncryptFrom<String>>::encryption() - .accepting::<NonEmpty<Label>>() - .map(|_| Tagged) - } - fn indexes() -> Vec<IndexSpec> { - vec![IndexSpec::Equality] - } -} - impl op::LabelledTarget for Tagged {} /// The equality index, its query term sent with the context it was derived /// under, as an EQL query value stores its column: what a query term that -/// records its context reads from the token. A label of other than two -/// segments names no column, and is refused before any term exists. +/// records its context reads from the token. The index serves the table +/// `users` alone. A label under another table names none of its columns, +/// and is refused before any term exists. #[derive(Clone, Copy, Debug, Default)] struct Labelled; @@ -404,7 +470,7 @@ impl op::Supports<op::Eq, String> for Labelled { <Equality as op::Supports<op::Eq, String>>::prepare(&Equality, value) } fn check_label(&self, label: &Label, _: bool) -> Result<(), Error> { - if label.segments().len() == 2 { + if label.segments().next() == Some("users") { Ok(()) } else { Err(Error::Other("not a column".into())) @@ -434,21 +500,6 @@ fn refused_as_not_a_column<T: std::fmt::Debug>(result: Result<T, Error>) { /// is, stored under its table and column alone. struct Column; -impl EncryptFrom<String> for Column { - type Context = NonEmpty<Label>; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> - where - String: 's, - { - <EqualityTerm as EncryptFrom<String>>::encryption() - .accepting::<NonEmpty<Label>>() - .map(|_| Column) - } - fn indexes() -> Vec<IndexSpec> { - vec![IndexSpec::Equality] - } -} - impl op::LabelledTarget for Column {} /// The equality index that refuses an extended context. @@ -489,57 +540,53 @@ impl op::Answers<op::Eq, String> for Column { type Via = Unextendable; } -/// A chain checked and then extended is checked again when it is prepared: -/// the label check is about the context, which a later `.extend(..)` -/// changes, so a kept verifier does not carry a stale acceptance. +/// The label check is about the whole context of the query. A scope +/// extends that context, so an index that takes no extension refuses a +/// query under a scope, before any key is requested, and derives its term +/// for the same label with no scope. #[tokio::test] -async fn a_chain_extended_after_its_check_is_checked_again() { - use stack_encrypt::plan::Operation; - let cipher = stack_cipher().await; - let plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<Column>() - .build() - .unwrap(); - let mut query = cipher.query(op::Eq, "bob@example.com").on(&plan); - query.check().unwrap(); - let refused = query.extend(7u64).await; +async fn an_index_that_takes_no_extension_refuses_a_query_under_a_scope() { + let (cipher, requests) = counted_cipher().await; + let keyset = cipher.default_keyset(); + let refused = marker_of::<Column, Scope<u64>>("users", "email") + .eq("bob@example.com") + .scope(Scope::new(7u64)) + .encrypt(&keyset) + .await; assert!( matches!(&refused, Err(Error::Other(reason)) if reason.to_string() == "no extension"), "{refused:?}" ); - // Unextended, the checked chain derives its term. - let mut query = cipher.query(op::Eq, "bob@example.com").on(&plan); - query.check().unwrap(); - assert!(query.await.is_ok()); + assert_eq!(requests.total(), 0, "refused before any key is requested"); + + let term = marker_of::<Column, NoScope>("users", "email") + .eq("bob@example.com") + .encrypt(&keyset) + .await + .unwrap(); + let stored: EqualityTerm = String::from("bob@example.com") + .encrypt_into_with_context(&keyset, nonempty!("users").with("email")) + .await + .unwrap(); + assert_eq!(term, stored); } -/// A `LabelledTarget` is queried through its plan though nothing can be -/// encrypted through it, and its query term reads the context its terms -/// were derived under from the token: the plan's label or the call's, and -/// whether the call extended it. +/// A `LabelledTarget` is queried through a marker though nothing is +/// encrypted into it, and its query term reads the context its terms were +/// derived under from the token: the marker's label, and whether a scope +/// extended it. #[tokio::test] -async fn a_labelled_target_is_queried_under_the_plans_label() { +async fn a_labelled_target_is_queried_under_the_markers_label() { let cipher = stack_cipher().await; - let eq_plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<EqualityTerm>() - .build() - .unwrap(); - let stored: EqualityTerm = cipher - .encrypt(&"bob@example.com".to_owned()) - .using(&eq_plan) + let keyset = cipher.default_keyset(); + let stored: EqualityTerm = String::from("bob@example.com") + .encrypt_into_with_context(&keyset, nonempty!("users").with("email")) .await .unwrap(); - let plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<Tagged>() - .build() - .unwrap(); - let query = cipher - .query(op::Eq, "bob@example.com") - .on(&plan) + let query = marker_of::<Tagged, NoScope>("users", "email") + .eq("bob@example.com") + .encrypt(&keyset) .await .unwrap(); assert_eq!( @@ -551,265 +598,125 @@ async fn a_labelled_target_is_queried_under_the_plans_label() { } ); - let unlabelled = Plan::value::<String>() - .encrypt_into::<Tagged>() - .build() - .unwrap(); - let query = cipher - .query(op::Eq, "bob@example.com") - .context("users/name") - .on(&unlabelled) + let query = marker_of::<Tagged, NoScope>("users", "name") + .eq("bob@example.com") + .encrypt(&keyset) .await .unwrap(); assert_eq!(query.label, "users/name"); - // Extended, the terms are derived under the label and the extension, and - // the token says so: a term that records only the label must refuse it. - let extended = cipher - .query(op::Eq, "bob@example.com") - .extend(7u64) - .on(&plan) + // Under a scope, the terms are derived under the label and the scope, + // and the token says so: a term that records only the label must + // refuse it. + let extended = marker_of::<Tagged, Scope<u64>>("users", "email") + .eq("bob@example.com") + .scope(Scope::new(7u64)) + .encrypt(&keyset) .await .unwrap(); - let stored_extended: EqualityTerm = cipher - .encrypt(&"bob@example.com".to_owned()) - .extend(7u64) - .using(&eq_plan) + let stored_extended: EqualityTerm = String::from("bob@example.com") + .encrypt_into_with_context(&keyset, nonempty!("users").with("email").with(7u64)) .await .unwrap(); assert!(extended.extended); - assert_eq!( - extended.label, "users/email", - "the label, before the extension" - ); + assert_eq!(extended.label, "users/email", "the label, before the scope"); assert_eq!(extended.term, stored_extended); } -/// A context the index refuses is refused when the query is awaited, -/// before the named keyset is loaded or any term is derived. +/// A context the index refuses is refused when the query is encrypted, +/// before any keyset or key is requested and before any term is derived. #[tokio::test] async fn a_label_the_index_refuses_is_refused_before_any_keyset_or_term() { - let (cipher, loads) = loads_counting_cipher().await; - let plan = Plan::value::<String>() - .context("users") - .encrypt_into::<Tagged>() - .build() - .unwrap(); + let (cipher, requests) = counted_cipher().await; + let keyset = cipher.default_keyset(); refused_as_not_a_column( - cipher - .query(op::Eq, "bob@example.com") - .keyset("tenant") - .on(&plan) + marker_of::<Tagged, NoScope>("orders", "email") + .eq("bob@example.com") + .encrypt(&keyset) .await, ); - assert_eq!( - loads.load(std::sync::atomic::Ordering::SeqCst), - 0, - "refused before the named keyset is loaded" - ); - // A label the index takes loads the keyset, so the count above is the - // refusal's. - let plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<Tagged>() - .build() - .unwrap(); - cipher - .query(op::Eq, "bob@example.com") - .keyset("tenant") - .on(&plan) - .await - .unwrap(); - assert_eq!(loads.load(std::sync::atomic::Ordering::SeqCst), 1); -} + assert_eq!(requests.total(), 0, "refused before any key is requested"); -#[tokio::test] -async fn operator_queries_settle_together_and_extend_like_the_write() { - let cipher = stack_cipher().await; - let plan = people_plan(); - let mut row = cipher - .encrypt(&person()) - .using(&plan) - .extend(7u64) + // A label the index takes derives the term, so the refusal above is + // the label's. + let query = marker_of::<Tagged, NoScope>("users", "email") + .eq("bob@example.com") + .encrypt(&keyset) .await .unwrap(); - let email: Encrypted<(EqualityTerm, MatchTerms)> = row.take("email").unwrap(); - let email_plan = plan.field("email").unwrap(); - - let (eq, matches) = stack_encrypt::all(( - cipher - .query(op::Eq, "bob@example.com") - .on(&email_plan) - .extend(7u64), - cipher - .query(op::Matches, "bob") - .on(&email_plan) - .extend(7u64), - )) - .await - .unwrap(); - assert_eq!(eq, email.terms.0); - assert!(email.terms.1.matches(&matches)); - - let unextended = cipher - .query(op::Eq, "bob@example.com") - .on(&email_plan) - .await - .unwrap(); - assert_ne!( - unextended, email.terms.0, - "the query is derived under the extended label" - ); -} - -#[tokio::test] -async fn a_query_chain_debugs_without_its_value() { - let cipher = stack_cipher().await; - let plan = people_plan(); - let email_plan = plan.field("email").unwrap(); - let secret = "bob@example.com"; - let shown = format!("{:?}", cipher.query(op::Eq, secret).on(&email_plan)); - assert_eq!(shown, "QueryOp { .. }"); -} - -/// A match index the field declares under other options is refused by -/// the plan before the query's text is cut, and a plan with no context is -/// refused before the index is looked at. -#[tokio::test] -async fn an_undeclared_index_is_refused_before_the_query_is_prepared() { - let cipher = stack_cipher().await; - let plan = people_plan(); - let refused = plan_error( - cipher - .query(op::Matches, "Bob") - .via(Match::<Exact>::new()) - .on(&plan.field("email").unwrap()) - .await, - ); - assert!( - matches!(refused, PlanError::IndexOptions { ref field, .. } if field == "email"), - "the plan's refusal: {refused:?}" - ); - - let no_context: Plan<Person, FakeDataKeySource> = Plan::fields() - .encrypt::<String>("email") - .encrypt_index::<String>("name", Match::<Exact>::new()) - .build() - .unwrap(); - let refused = plan_error( - cipher - .query(op::Matches, "Bob") - .via(Match::<Exact>::new()) - .on(&no_context.field("name").unwrap()) - .await, - ); - assert_eq!(refused, PlanError::NoContext); + assert_eq!(query.label, "users/email"); } #[tokio::test] -async fn a_query_names_its_context_before_or_after_the_index() { +async fn operator_queries_settle_together_under_the_scope_of_the_write() { let cipher = stack_cipher().await; - let plan: Plan<Person, FakeDataKeySource> = Plan::fields() - .encrypt_index::<String>("email", Match) - .encrypt::<String>("name") - .build() - .unwrap(); - let email_plan = plan.field("email").unwrap(); - let before = cipher - .query(op::Matches, "bob") - .context("people") - .via(Match) - .on(&email_plan) + let keyset = cipher.default_keyset(); + let row: TenantPerson = person() + .encrypt_into_with_context(&keyset, Scope::new(7u64)) .await .unwrap(); - let after = cipher - .query(op::Matches, "bob") - .via(Match) - .context("people") - .on(&email_plan) + let (equality, matching) = &row.email.terms; + + let (eq, matches) = TenantPerson::EMAIL + .eq("bob@example.com") + .scope(Scope::new(7u64)) + .encrypt(&keyset) + .zip( + TenantPerson::EMAIL + .matches("bob") + .scope(Scope::new(7u64)) + .encrypt(&keyset), + ) .await .unwrap(); - assert_eq!(before, after); -} + assert_eq!(&eq, equality); + assert!(matching.matches(&matches)); -#[tokio::test] -async fn a_refused_operator_query_loads_no_keyset() { - let (cipher, loads) = loads_counting_cipher().await; - let plan: Plan<Person, common::CountingSource> = Plan::context("people") - .fields() - .encrypt::<String>("email") - .encrypt_index::<String>("name", Match::<Exact>::new()) - .build() + // The same label with no scope is another context. + let unscoped = EncryptedPerson::EMAIL + .eq("bob@example.com") + .encrypt(&keyset) + .await .unwrap(); - let refused = cipher - .query(op::Eq, "Alice") - .on(&plan.field("name").unwrap()) - .keyset("tenant") - .await; - assert_eq!( - plan_error(refused), - PlanError::IndexNotDeclared { - field: "name".into(), - index: "eq" - } - ); - assert_eq!( - loads.load(std::sync::atomic::Ordering::SeqCst), - 0, - "refused before the named keyset is loaded" + assert_ne!( + &unscoped, equality, + "the query is derived under the label and the scope" ); } #[tokio::test] -async fn an_operator_query_runs_under_the_keyset_it_names() { +async fn an_operator_query_runs_under_the_keyset_it_is_given() { let cipher = stack_cipher().await; - let plan = Plan::context("users/email") - .with::<String, _>(Equality) - .build() - .unwrap(); - let stored: Encrypted<EqualityTerm> = cipher - .encrypt(&"bob@example.com".to_owned()) - .using(&plan) - .keyset("tenant") + let tenant = cipher + .keyset(IdentifiedBy::Name("tenant".to_string().into())) .await .unwrap(); - let under_tenant = cipher - .query(op::Eq, "bob@example.com") - .on(&plan) - .keyset("tenant") + let stored: EncryptedPerson = person().encrypt_into(&tenant).await.unwrap(); + let under_tenant = EncryptedPerson::EMAIL + .eq("bob@example.com") + .encrypt(&tenant) .await .unwrap(); - assert_eq!(under_tenant, stored.terms); - let under_default = cipher - .query(op::Eq, "bob@example.com") - .on(&plan) + assert_eq!(under_tenant, stored.email.terms.0); + let under_default = EncryptedPerson::EMAIL + .eq("bob@example.com") + .encrypt(&cipher.default_keyset()) .await .unwrap(); - assert_ne!(under_default, stored.terms, "another keyset, another key"); -} - -/// A match configuration with the smallest filter allowed, so a long value -/// sets nearly every bit and false positives are easy to find. -struct Tiny; -impl MatchConfig for Tiny { - fn options() -> MatchOptions { - MatchOptions { - m: 32, - ..Default::default() - } - } + assert_ne!( + under_default, stored.email.terms.0, + "another keyset, another key" + ); } #[tokio::test] async fn verify_keeps_every_real_match_and_folds_case_as_the_index_does() { let cipher = stack_cipher().await; - let plan = people_plan(); - let email_plan = plan.field("email").unwrap(); - let name_plan = plan.field("name").unwrap(); + let keyset = cipher.default_keyset(); - let query = cipher - .query(op::Matches, "BOB@Example") - .on(&email_plan) + let query = EncryptedPerson::EMAIL + .matches("BOB@Example") + .encrypt(&keyset) .await .unwrap(); assert!(query.verify("bob@example.com")); @@ -817,10 +724,9 @@ async fn verify_keeps_every_real_match_and_folds_case_as_the_index_does() { assert!(!query.verify("alice@example.com")); assert!(query.clone().verify("bob@example.com")); - let exact = cipher - .query(op::Matches, "Nguyen") - .via(Match::<Exact>::new()) - .on(&name_plan) + let exact = EncryptedPerson::NAME + .matches("Nguyen") + .encrypt(&keyset) .await .unwrap(); assert!(exact.verify("Alice Nguyen")); @@ -837,17 +743,16 @@ async fn verify_keeps_every_real_match_and_folds_case_as_the_index_does() { #[tokio::test] async fn a_match_query_is_cut_in_nfc_and_refuses_text_with_no_normal_form() { let cipher = stack_cipher().await; - let plan = people_plan(); - let email_plan = plan.field("email").unwrap(); + let keyset = cipher.default_keyset(); - let query = cipher - .query(op::Matches, "Rene\u{301}e") - .on(&email_plan) + let query = EncryptedPerson::EMAIL + .matches("Rene\u{301}e") + .encrypt(&keyset) .await .unwrap(); - let precomposed = cipher - .query(op::Matches, "Ren\u{e9}e") - .on(&email_plan) + let precomposed = EncryptedPerson::EMAIL + .matches("Ren\u{e9}e") + .encrypt(&keyset) .await .unwrap(); assert_eq!(query, precomposed, "one string, one filter"); @@ -858,9 +763,9 @@ async fn a_match_query_is_cut_in_nfc_and_refuses_text_with_no_normal_form() { "a value with no normal form verifies against nothing" ); - let refused = cipher - .query(op::Matches, "Ren\u{378}e") - .on(&email_plan) + let refused = EncryptedPerson::EMAIL + .matches("Ren\u{378}e") + .encrypt(&keyset) .await; assert!( matches!( @@ -876,37 +781,29 @@ async fn a_match_query_is_cut_in_nfc_and_refuses_text_with_no_normal_form() { #[tokio::test] async fn verify_drops_a_row_the_filter_matched_by_chance() { let cipher = stack_cipher().await; - let plan = Plan::context("notes/body") - .with::<String, _>(Match::<Tiny>::new()) - .build() - .unwrap(); + let keyset = cipher.default_keyset(); let value = "the quick brown fox jumps over the lazy dog and keeps running"; - let stored: Encrypted<MatchTerms<Tiny>> = cipher - .encrypt(&value.to_owned()) - .using(&plan) - .await - .unwrap(); + let stored: TinyNote = note(value).encrypt_into(&keyset).await.unwrap(); + let stored = stored.body.terms; - let real = cipher - .query(op::Matches, "lazy dog") - .via(Match::<Tiny>::new()) - .on(&plan) + let real = TinyNote::BODY + .matches("lazy dog") + .encrypt(&keyset) .await .unwrap(); - assert!(stored.terms.matches(&real) && real.verify(value)); + assert!(stored.matches(&real) && real.verify(value)); // The value holds no digit, so no query with one is in it; a 32-bit // filter this full holds some of their filters anyway. let mut false_positive = None; for i in 0..1000 { let digits = format!("{i:04}"); - let query = cipher - .query(op::Matches, &digits) - .via(Match::<Tiny>::new()) - .on(&plan) + let query = TinyNote::BODY + .matches(&digits) + .encrypt(&keyset) .await .unwrap(); - if stored.terms.matches(&query) { + if stored.matches(&query) { false_positive = Some(query); break; } @@ -921,10 +818,10 @@ async fn verify_drops_a_row_the_filter_matched_by_chance() { #[tokio::test] async fn a_match_query_debugs_without_its_tokens() { let cipher = stack_cipher().await; - let plan = people_plan(); - let query = cipher - .query(op::Matches, "needle") - .on(&plan.field("email").unwrap()) + let keyset = cipher.default_keyset(); + let query = EncryptedPerson::EMAIL + .matches("needle") + .encrypt(&keyset) .await .unwrap(); let shown = format!("{query:?}"); @@ -937,48 +834,31 @@ async fn a_match_query_debugs_without_its_tokens() { } } -/// Whole words, as the v1 match indexer's standard tokenizer splits them. -struct Words; -impl MatchConfig for Words { - fn options() -> MatchOptions { - MatchOptions { - tokenizer: stack_encrypt::sem::Tokenizer::Standard, - ..Default::default() - } - } -} - /// A Bloom match asks for the query's tokens, in any order and anywhere; /// it is not a substring test, and `verify` must not become one, or it /// would drop rows the index rightly returned. #[tokio::test] async fn verify_is_a_token_test_not_a_substring_test() { let cipher = stack_cipher().await; - let plan = people_plan(); - let query = cipher - .query(op::Matches, "abcd") - .on(&plan.field("email").unwrap()) + let keyset = cipher.default_keyset(); + let query = EncryptedPerson::EMAIL + .matches("abcd") + .encrypt(&keyset) .await .unwrap(); assert!(query.verify("abc bcd"), "both 3-grams, never adjacent"); assert!(!query.verify("abc"), "one 3-gram of two"); - let words = Plan::context("notes/body") - .with::<String, _>(Match::<Words>::new()) - .build() - .unwrap(); - let stored: Encrypted<MatchTerms<Words>> = cipher - .encrypt(&"Hello, big World!".to_owned()) - .using(&words) + let stored: WordNote = note("Hello, big World!") + .encrypt_into(&keyset) .await .unwrap(); - let query = cipher - .query(op::Matches, "world hello") - .via(Match::<Words>::new()) - .on(&words) + let query = WordNote::BODY + .matches("world hello") + .encrypt(&keyset) .await .unwrap(); - assert!(stored.terms.matches(&query)); + assert!(stored.body.terms.matches(&query)); assert!(query.verify("Hello, big World!")); assert!( !query.verify("worldly hello"), @@ -986,36 +866,11 @@ async fn verify_is_a_token_test_not_a_substring_test() { ); } -/// EQL v4's filter size and slices (`k = 6`, `m = 2048`; its match types -/// also turn on short words, which this prior's arithmetic does not need), -/// with a notes column's prior. -struct Notes; -impl MatchConfig for Notes { - fn options() -> MatchOptions { - MatchOptions { - k: 6, - m: 2048, - ..Default::default() - } - } - fn expected_tokens() -> Option<u32> { - Some(200) - } -} - #[tokio::test] async fn target_limit_sizes_the_fetch_from_the_query_and_the_prior() { let cipher = stack_cipher().await; - let plan = Plan::context("notes/body") - .with::<String, _>(Match::<Notes>::new()) - .build() - .unwrap(); - let query = |text: &'static str| { - cipher - .query(op::Matches, text) - .via(Match::<Notes>::new()) - .on(&plan) - }; + let keyset = cipher.default_keyset(); + let query = |text: &'static str| PriorNote::BODY.matches(text).encrypt(&keyset); let stats = Stats::declared(1_000_000); let short = query("ali").await.unwrap(); @@ -1041,11 +896,10 @@ async fn target_limit_sizes_the_fetch_from_the_query_and_the_prior() { #[tokio::test] async fn target_limit_without_a_prior_needs_a_measured_column() { let cipher = stack_cipher().await; - let plan = people_plan(); - let email_plan = plan.field("email").unwrap(); - let query = cipher - .query(op::Matches, "bob") - .on(&email_plan) + let keyset = cipher.default_keyset(); + let query = EncryptedPerson::EMAIL + .matches("bob") + .encrypt(&keyset) .await .unwrap(); @@ -1062,17 +916,15 @@ async fn target_limit_without_a_prior_needs_a_measured_column() { "alice@example.org", "carol.smith@example.net", ] { - let mut row = cipher - .encrypt(&Person { - email: email.into(), - name: "x".repeat(4), - }) - .using(&plan) - .await - .unwrap(); - let stored: Encrypted<(EqualityTerm, MatchTerms)> = row.take("email").unwrap(); + let row: EncryptedPerson = Person { + email: email.into(), + name: "x".repeat(4), + } + .encrypt_into(&keyset) + .await + .unwrap(); *histogram - .entry(stored.terms.1.positions().len() as u32) + .entry(row.email.terms.1.positions().len() as u32) .or_default() += 1; } // The pass rate the histogram gives this query, over a large table. @@ -1102,102 +954,96 @@ async fn target_limit_without_a_prior_needs_a_measured_column() { #[tokio::test] async fn a_short_match_value_stores_as_the_empty_filter_and_no_query_finds_it() { let cipher = stack_cipher().await; - let plan = Plan::context("users/name") - .with::<String, _>((Equality, Match)) - .build() + let keyset = cipher.default_keyset(); + let query = EncryptedPerson::EMAIL + .matches("alice") + .encrypt(&keyset) + .await .unwrap(); - let query = cipher.query(op::Matches, "alice").on(&plan).await.unwrap(); + let short_person = |email: &str| Person { + email: email.into(), + name: "Alice Nguyen".into(), + }; for short in ["Al", "", " "] { - let stored: Encrypted<(EqualityTerm, MatchTerms)> = cipher - .encrypt(&short.to_owned()) - .using(&plan) + let stored: EncryptedPerson = short_person(short) + .encrypt_into(&keyset) .await .unwrap_or_else(|error| panic!("{short:?} stores: {error}")); + let filter = &stored.email.terms.1; assert!( - stored.terms.1.positions().is_empty(), + filter.positions().is_empty(), "{short:?} is stored as the empty filter" ); assert!( - !stored.terms.1.matches(&query), + !filter.matches(&query), "{short:?}: the empty filter holds none of a query's positions" ); assert!(!query.verify(short)); - let back: String = cipher.open(stored).using(&plan).await.unwrap(); - assert_eq!(back, short, "{short:?} opens as written"); + let back = Person::decrypt_from(stored, &keyset).await.unwrap(); + assert_eq!(back.email, short, "{short:?} opens as written"); } // The equality term still finds it: only the match term is empty. - let stored: Encrypted<(EqualityTerm, MatchTerms)> = - cipher.encrypt(&"Al".to_owned()).using(&plan).await.unwrap(); - let eq = cipher.query(op::Eq, "Al").on(&plan).await.unwrap(); - assert_eq!(eq, stored.terms.0); + let stored: EncryptedPerson = short_person("Al").encrypt_into(&keyset).await.unwrap(); + let eq = EncryptedPerson::EMAIL + .eq("Al") + .encrypt(&keyset) + .await + .unwrap(); + assert_eq!(eq, stored.email.terms.0); } /// A query with no tokens would match every row, so it is the one query -/// value the library refuses: when the query is awaited, before a term -/// exists and before any keyset is loaded. +/// value the library refuses: when the query is encrypted, before a term +/// exists and before any keyset or key is requested. #[tokio::test] async fn a_match_query_with_no_tokens_is_refused_before_any_term_or_keyset() { - let (cipher, loads) = loads_counting_cipher().await; - let plan: Plan<Person, common::CountingSource> = Plan::context("people") - .fields() - .encrypt_index::<String>("email", (Equality, Match)) - .encrypt_index::<String>("name", Match::<Exact>::new()) - .build() - .unwrap(); - let email_plan = plan.field("email").unwrap(); + let (cipher, requests) = counted_cipher().await; + let keyset = cipher.default_keyset(); for text in ["", " ", "bo"] { - let refused = cipher - .query(op::Matches, text) - .on(&email_plan) - .keyset("tenant") - .await; + let refused = EncryptedPerson::EMAIL.matches(text).encrypt(&keyset).await; assert!( matches!(refused, Err(Error::Term(TermError::EmptyTermText))), "{text:?}: {refused:?}" ); } - // Through `via`, under another configuration, and in a batch. - let refused = cipher - .query(op::Matches, "Al") - .via(Match::<Exact>::new()) - .on(&plan.field("name").unwrap()) - .await; + // Under another configuration, and in a batch. + let refused = EncryptedPerson::NAME.matches("Al").encrypt(&keyset).await; assert!(matches!( refused, Err(Error::Term(TermError::EmptyTermText)) )); - let refused = stack_encrypt::all(( - cipher.query(op::Eq, "bob@example.com").on(&email_plan), - cipher.query(op::Matches, "bo").on(&email_plan), - )) - .await; + let refused = EncryptedPerson::EMAIL + .eq("bob@example.com") + .encrypt(&keyset) + .zip(EncryptedPerson::EMAIL.matches("bo").encrypt(&keyset)) + .await; assert!(matches!( refused, Err(Error::Term(TermError::EmptyTermText)) )); - assert_eq!( - loads.load(std::sync::atomic::Ordering::SeqCst), - 0, - "refused before the named keyset is loaded" - ); + assert_eq!(requests.total(), 0, "refused before any key is requested"); assert_eq!( TermError::EmptyTermText.to_string(), "the query's text produces no match tokens (empty, separator-only, or shorter than the n-gram length)" ); // The same text is a fine equality query, and a fine stored value. - assert!(cipher.query(op::Eq, "").on(&email_plan).await.is_ok()); - let row = cipher - .encrypt(&Person { - email: "bo".into(), - name: "Al".into(), - }) - .using(&plan) - .await; - assert!(row.is_ok(), "short values store: {row:?}"); + assert!(EncryptedPerson::EMAIL.eq("").encrypt(&keyset).await.is_ok()); + let row: Result<EncryptedPerson, Error> = Person { + email: "bo".into(), + name: "Al".into(), + } + .encrypt_into(&keyset) + .await; + assert!(row.is_ok(), "short values store: {:?}", row.err()); + assert_eq!( + requests.total(), + 1, + "storing a row asks for its data keys, so the count above is the refusals'" + ); } /// A match query under options the index cannot use is refused with @@ -1206,126 +1052,69 @@ async fn a_match_query_with_no_tokens_is_refused_before_any_term_or_keyset() { #[tokio::test] async fn a_match_query_under_invalid_options_is_refused_not_tokenised() { let cipher = stack_cipher().await; - let plan = Plan::context("users/name") - .with::<String, _>(Match::<ZeroGram>::new()) - .build() - .unwrap(); - let refused = cipher - .query(op::Matches, "alice") - .via(Match::<ZeroGram>::new()) - .on(&plan) - .await; + let keyset = cipher.default_keyset(); + let refused = ZeroGramNote::BODY.matches("alice").encrypt(&keyset).await; assert!( matches!(refused, Err(Error::Term(TermError::InvalidOptions(_)))), "{refused:?}" ); - let stored = cipher.encrypt(&"alice".to_owned()).using(&plan).await; + let stored: Result<ZeroGramNote, Error> = note("alice").encrypt_into(&keyset).await; assert!( matches!(stored, Err(Error::Term(TermError::InvalidOptions(_)))), - "{stored:?}" + "{:?}", + stored.err() ); } /// An ordering query sends the index's own term for the value: this /// crate's ORE is CLLW, whose ciphertext has no left half to send alone, -/// so what is sent is what is stored, as for an HMAC. The ordering -/// operators default to ORE; OPE is named with `via`, on a plan that -/// declares it (a plan declares one order index). +/// so what is sent is what is stored, as for an HMAC. The field's type +/// names its order index: an `OreTerm` field sends an ORE term and an +/// `OpeTerm` field an OPE term. #[tokio::test] async fn an_ordering_query_is_the_index_term_of_the_value() { let cipher = stack_cipher().await; - let plan = Plan::context("users/age") - .with::<u32, _>((Equality, Ore)) - .build() - .unwrap(); - let stored: Encrypted<(EqualityTerm, OreTerm<u32>)> = - cipher.encrypt(&34u32).using(&plan).await.unwrap(); - - let lt: OreTerm<u32> = cipher.query(op::Lt, &34u32).on(&plan).await.unwrap(); - let lte = cipher.query(op::Lte, &34u32).on(&plan).await.unwrap(); - let gt = cipher.query(op::Gt, &34u32).on(&plan).await.unwrap(); - let gte = cipher.query(op::Gte, &34u32).on(&plan).await.unwrap(); + let keyset = cipher.default_keyset(); + let stored: OreProfile = profile(34).encrypt_into(&keyset).await.unwrap(); + let stored = &stored.age.terms.1; + + let lt: OreTerm<u32> = OreProfile::AGE.lt(34u32).encrypt(&keyset).await.unwrap(); + let lte = OreProfile::AGE.lte(34u32).encrypt(&keyset).await.unwrap(); + let gt = OreProfile::AGE.gt(34u32).encrypt(&keyset).await.unwrap(); + let gte = OreProfile::AGE.gte(34u32).encrypt(&keyset).await.unwrap(); for (name, term) in [("Lt", &lt), ("Lte", &lte), ("Gt", &gt), ("Gte", &gte)] { - assert_eq!(term, &stored.terms.1, "{name} sends the ORE term"); + assert_eq!(term, stored, "{name} sends the ORE term"); } - let above = cipher.query(op::Lt, &35u32).on(&plan).await.unwrap(); - let below = cipher.query(op::Lt, &33u32).on(&plan).await.unwrap(); + let above = OreProfile::AGE.lt(35u32).encrypt(&keyset).await.unwrap(); + let below = OreProfile::AGE.lt(33u32).encrypt(&keyset).await.unwrap(); assert!( - below < stored.terms.1 && stored.terms.1 < above, + &below < stored && stored < &above, "ORE terms order as the values" ); - let ope_plan = Plan::context("users/age") - .with::<u32, _>((Equality, Ope)) - .build() - .unwrap(); - let stored_ope: Encrypted<(EqualityTerm, OpeTerm<u32>)> = - cipher.encrypt(&34u32).using(&ope_plan).await.unwrap(); - let ope: OpeTerm<u32> = cipher - .query(op::Gte, &34u32) - .via(Ope) - .on(&ope_plan) - .await - .unwrap(); - assert_eq!(ope, stored_ope.terms.1, "OPE is named with via"); - let ope_above = cipher - .query(op::Gt, &35u32) - .via(Ope) - .on(&ope_plan) - .await - .unwrap(); + let stored_ope: OpeProfile = profile(34).encrypt_into(&keyset).await.unwrap(); + let ope: OpeTerm<u32> = OpeProfile::AGE.gte(34u32).encrypt(&keyset).await.unwrap(); + assert_eq!( + ope, stored_ope.age.terms.1, + "an OPE field sends the OPE term" + ); + let ope_above = OpeProfile::AGE.gt(35u32).encrypt(&keyset).await.unwrap(); assert!( ope.as_bytes() < ope_above.as_bytes(), "OPE terms order as bytes" ); - // A typed target answers the orderings through the index it names. - let typed = Plan::value::<u32>() - .context("users/balance") - .encrypt_into::<Encrypted<OpeTerm<u32>>>() - .build() + // A field of one term answers the orderings through the index it names. + let stored: EncryptedMember = Member { age: 5, balance: 0 } + .encrypt_into(&keyset) + .await .unwrap(); - let stored = cipher.encrypt(&5u32).using(&typed).await.unwrap(); - let query = cipher.query(op::Lte, &5u32).on(&typed).await.unwrap(); - assert_eq!(query, stored.terms); -} - -/// A field declares at most one index per operator (#1233): `(Ore, Ope)` -/// would store a second order term that no query uses, and that tells -/// anyone who reads the column how its values order. Both spellings of a -/// plan refuse it when the plan is built, before any key is requested, and -/// so does a typed target that declares both. `(Equality, Ore)` answer -/// different operators and are allowed. -#[test] -fn a_field_declares_one_index_per_operator() { - let overlap = |at: &str, first: &'static str, second: &'static str| PlanError::IndexOverlap { - at: at.into(), - first, - second, - operator: stack_encrypt::op::Op::Lt, - }; - let with = Plan::context("users/age") - .with::<u32, _>((Ore, Ope)) - .build(); - assert_eq!(plan_error(with), overlap("the value", "ore", "ope")); - let typed = Plan::value::<u32>() - .context("users/age") - .encrypt_into::<Encrypted<(OreTerm<u32>, OpeTerm<u32>)>>() - .build(); - assert_eq!(plan_error(typed), overlap("the value", "ore", "ope")); - let field: Result<Plan<Person, FakeDataKeySource>, _> = Plan::context("users") - .fields() - .encrypt_index::<String>("name", (Equality, Ore, OreBlock)) - .build(); - assert_eq!(plan_error(field), overlap("name", "ore", "ore_block")); - assert_eq!( - overlap("name", "ore", "ore_block").to_string(), - "field \"name\" declares two indexes that answer lt: ore and ore_block" - ); - assert!(Plan::context("users/age") - .with::<u32, _>((Equality, Ore)) - .build() - .is_ok()); + let query = EncryptedMember::AGE + .lte(5u32) + .encrypt(&keyset) + .await + .unwrap(); + assert_eq!(query, stored.age.terms); } /// `Between` is one term carrying both bounds, inclusive, derived under @@ -1334,62 +1123,49 @@ fn a_field_declares_one_index_per_operator() { #[tokio::test] async fn a_between_query_is_one_term_carrying_both_bounds() { let cipher = stack_cipher().await; - let plan = Plan::context("users/age") - .with::<u32, _>(Ore) - .build() - .unwrap(); + let keyset = cipher.default_keyset(); - let range: RangeQuery<OreTerm<u32>> = cipher - .query(op::Between, &(18u32, 65)) - .on(&plan) + let range: RangeQuery<OreTerm<u32>> = OreProfile::AGE + .between(18u32, 65) + .encrypt(&keyset) .await .unwrap(); - let lo = cipher.query(op::Gte, &18u32).on(&plan).await.unwrap(); - let hi = cipher.query(op::Lte, &65u32).on(&plan).await.unwrap(); + let lo = OreProfile::AGE.gte(18u32).encrypt(&keyset).await.unwrap(); + let hi = OreProfile::AGE.lte(65u32).encrypt(&keyset).await.unwrap(); assert_eq!(range.lo(), &lo); assert_eq!(range.hi(), &hi); assert_eq!(range.clone().into_bounds(), (lo, hi)); // A stored value inside the range orders between the bounds. - let inside: Encrypted<OreTerm<u32>> = cipher.encrypt(&30u32).using(&plan).await.unwrap(); - let outside: Encrypted<OreTerm<u32>> = cipher.encrypt(&70u32).using(&plan).await.unwrap(); - assert!(range.lo() <= &inside.terms && &inside.terms <= range.hi()); - assert!(&outside.terms > range.hi()); + let inside: OreProfile = profile(30).encrypt_into(&keyset).await.unwrap(); + let outside: OreProfile = profile(70).encrypt_into(&keyset).await.unwrap(); + let (inside, outside) = (&inside.age.terms.1, &outside.age.terms.1); + assert!(range.lo() <= inside && inside <= range.hi()); + assert!(outside > range.hi()); // Inclusive at both ends: one value is `(v, v)`. - let one = cipher - .query(op::Between, &(40u32, 40)) - .on(&plan) + let one = OreProfile::AGE + .between(40u32, 40) + .encrypt(&keyset) .await .unwrap(); assert_eq!(one.lo(), one.hi()); assert_ne!(one, range); - // OPE is a plan's own index, named with via, and a typed target answers - // through its index. A plan declares one order index: `(Ore, Ope)` is - // refused when it is built (`a_field_declares_one_index_per_operator`). - let ope_plan = Plan::context("users/age") - .with::<u32, _>(Ope) - .build() - .unwrap(); - let ope: RangeQuery<OpeTerm<u32>> = cipher - .query(op::Between, &(18u32, 65)) - .via(Ope) - .on(&ope_plan) + // An OPE field's range carries OPE terms, and a field of one term + // answers a range through the index it names. + let ope: RangeQuery<OpeTerm<u32>> = OpeProfile::AGE + .between(18u32, 65) + .encrypt(&keyset) .await .unwrap(); assert!(ope.lo().as_bytes() < ope.hi().as_bytes()); - let typed = Plan::value::<u32>() - .context("users/age") - .encrypt_into::<Encrypted<OreTerm<u32>>>() - .build() - .unwrap(); - let typed_range = cipher - .query(op::Between, &(1u32, 2)) - .on(&typed) + let alone = EncryptedMember::BALANCE + .between(1i64, 2) + .encrypt(&keyset) .await .unwrap(); - assert!(typed_range.lo() < typed_range.hi()); + assert!(alone.lo() < alone.hi()); } /// A range whose low bound is above its high bound is not refused, as @@ -1398,137 +1174,70 @@ async fn a_between_query_is_one_term_carrying_both_bounds() { #[tokio::test] async fn an_inverted_range_is_a_query_like_any_other() { let cipher = stack_cipher().await; - let plan = Plan::context("users/age") - .with::<u32, _>(Ore) - .build() - .unwrap(); - let inverted = cipher - .query(op::Between, &(65u32, 18)) - .on(&plan) + let keyset = cipher.default_keyset(); + let inverted = OreProfile::AGE + .between(65u32, 18) + .encrypt(&keyset) .await .unwrap(); assert!(inverted.lo() > inverted.hi(), "the bounds as given"); - let one = cipher - .query(op::Between, &(18u32, 18)) - .on(&plan) + let one = OreProfile::AGE + .between(18u32, 18) + .encrypt(&keyset) .await .unwrap(); assert_eq!(one.lo(), one.hi()); } -/// A field of a fields plan answers the orderings over its declared index, -/// under the field's own label, and refuses an index it does not declare. -/// An index-only field needs no sealing, so it can hold the integer types -/// the cipher does not seal on its own. +/// A field answers the orderings through the index its type names, under +/// the field's own label. A field that is a term alone needs no sealing, +/// so it can hold the integer types the cipher does not seal on its own. #[tokio::test] async fn a_field_answers_the_orderings_through_its_declared_index() { - #[derive(Clone)] - struct Member { - age: u32, - balance: i64, - } - impl Fields for Member { - fn field_names(&self) -> Vec<&str> { - vec!["age", "balance"] - } - fn schema() -> Option<Vec<FieldSchema>> { - Some(vec![ - FieldSchema::of::<u32>("age"), - FieldSchema::of::<i64>("balance"), - ]) - } - } - impl Field<u32> for Member { - fn field(&self, name: &str) -> Option<&u32> { - (name == "age").then_some(&self.age) - } - } - impl Field<i64> for Member { - fn field(&self, name: &str) -> Option<&i64> { - (name == "balance").then_some(&self.balance) - } - } let cipher = stack_cipher().await; - let plan: Plan<Member, FakeDataKeySource> = Plan::context("members") - .fields() - .encrypt_index::<u32>("age", Ope) - .index::<i64>("balance", Ore) - .build() - .unwrap(); - let mut row = cipher - .encrypt(&Member { - age: 34, - balance: -250, - }) - .using(&plan) - .await - .unwrap(); - let age: Encrypted<OpeTerm<u32>> = row.take("age").unwrap(); - let balance: OreTerm<i64> = row.take("balance").unwrap(); - let age_plan = plan.field("age").unwrap(); - let balance_plan = plan.field("balance").unwrap(); - - let query = cipher - .query(op::Lte, &34u32) - .via(Ope) - .on(&age_plan) + let keyset = cipher.default_keyset(); + let row: EncryptedMember = Member { + age: 34, + balance: -250, + } + .encrypt_into(&keyset) + .await + .unwrap(); + let age = &row.age.terms; + let balance = &row.balance; + + let query = EncryptedMember::AGE + .lte(34u32) + .encrypt(&keyset) .await .unwrap(); - assert_eq!(query, age.terms); - let range = cipher - .query(op::Between, &(30u32, 40)) - .via(Ope) - .on(&age_plan) + assert_eq!(&query, age); + let range = EncryptedMember::AGE + .between(30u32, 40) + .encrypt(&keyset) .await .unwrap(); - assert!( - range.lo().as_bytes() <= age.terms.as_bytes() - && age.terms.as_bytes() <= range.hi().as_bytes() - ); + assert!(range.lo().as_bytes() <= age.as_bytes() && age.as_bytes() <= range.hi().as_bytes()); // A signed value, ordered as a signed value. - let owed = cipher - .query(op::Lt, &-250i64) - .on(&balance_plan) + let owed = EncryptedMember::BALANCE + .lt(-250i64) + .encrypt(&keyset) .await .unwrap(); - assert_eq!(owed, balance); - let zero = cipher - .query(op::Gte, &0i64) - .on(&balance_plan) + assert_eq!(&owed, balance); + let zero = EncryptedMember::BALANCE + .gte(0i64) + .encrypt(&keyset) .await .unwrap(); - assert!(balance < zero, "a negative balance is below zero"); - let overdrawn = cipher - .query(op::Between, &(-1_000i64, -1)) - .on(&balance_plan) + assert!(balance < &zero, "a negative balance is below zero"); + let overdrawn = EncryptedMember::BALANCE + .between(-1_000i64, -1) + .encrypt(&keyset) .await .unwrap(); - assert!(overdrawn.lo() <= &balance && &balance <= overdrawn.hi()); - - // The default index for an ordering is ORE, which the age field lacks. - assert_eq!( - plan_error(cipher.query(op::Lt, &34u32).on(&age_plan).await), - PlanError::IndexNotDeclared { - field: "age".into(), - index: "ore" - } - ); - assert_eq!( - plan_error(cipher.query(op::Between, &(1u32, 2)).on(&age_plan).await), - PlanError::IndexNotDeclared { - field: "age".into(), - index: "ore" - } - ); - // And a value of another type than the field declares. - assert_eq!( - plan_error(cipher.query(op::Lt, &34u64).via(Ope).on(&age_plan).await), - PlanError::FieldType { - field: "age".into(), - expected: "u32" - } - ); + assert!(overdrawn.lo() <= balance && balance <= overdrawn.hi()); } /// Text orders by its order key, ADR-0002's fold: the orderings are @@ -1537,16 +1246,9 @@ async fn a_field_answers_the_orderings_through_its_declared_index() { #[tokio::test] async fn text_orders_by_its_folded_key() { let cipher = stack_cipher().await; - let plan = Plan::context("users/surname") - .with::<String, _>(Ore) - .build() - .unwrap(); - let ope_plan = Plan::context("users/surname") - .with::<String, _>(Ope) - .build() - .unwrap(); - let lt = |text: &str| cipher.query(op::Lt, text).on(&plan); - let ope = |text: &str| cipher.query(op::Lt, text).via(Ope).on(&ope_plan); + let keyset = cipher.default_keyset(); + let lt = |text: &str| OreProfile::SURNAME.lt(text).encrypt(&keyset); + let ope = |text: &str| OpeProfile::SURNAME.lt(text).encrypt(&keyset); let cafe: OreTerm<String> = lt("cafe").await.unwrap(); for text in ["Caf\u{e9}", "CAF\u{c9}", "cafe\u{301}"] { assert_eq!(lt(text).await.unwrap(), cafe, "{text:?}"); @@ -1565,11 +1267,7 @@ async fn text_orders_by_its_folded_key() { let zoe: OreTerm<String> = lt("Zoe").await.unwrap(); assert!(alice < zoe); - let between = |lo: &str, hi: &str| { - cipher - .query(op::Between, &(lo.to_owned(), hi.to_owned())) - .on(&plan) - }; + let between = |lo: &str, hi: &str| OreProfile::SURNAME.between(lo, hi).encrypt(&keyset); let tie = between("A", "a").await.unwrap(); assert_eq!(tie.lo(), tie.hi(), "one folded value"); assert!(between("a", "B").await.is_ok()); @@ -1588,16 +1286,9 @@ async fn text_orders_by_its_folded_key() { #[tokio::test] async fn floats_order_by_their_order_key() { let cipher = stack_cipher().await; - let plan = Plan::context("users/score") - .with::<f64, _>(Ore) - .build() - .unwrap(); - let ope_plan = Plan::context("users/score") - .with::<f64, _>(Ope) - .build() - .unwrap(); - let lt = |value: f64| cipher.query(op::Lt, &value).on(&plan); - let ope = |value: f64| cipher.query(op::Lt, &value).via(Ope).on(&ope_plan); + let keyset = cipher.default_keyset(); + let lt = |value: f64| OreProfile::SCORE.lt(value).encrypt(&keyset); + let ope = |value: f64| OpeProfile::SCORE.lt(value).encrypt(&keyset); let zero: OreTerm<f64> = lt(0.0).await.unwrap(); assert_eq!(lt(-0.0).await.unwrap(), zero, "-0.0 is 0.0"); let nan: OreTerm<f64> = lt(f64::NAN).await.unwrap(); @@ -1621,7 +1312,7 @@ async fn floats_order_by_their_order_key() { let low: OreTerm<f64> = lt(-1.5).await.unwrap(); assert!(low < zero && zero < nan); - let between = |lo: f64, hi: f64| cipher.query(op::Between, &(lo, hi)).on(&plan); + let between = |lo: f64, hi: f64| OreProfile::SCORE.between(lo, hi).encrypt(&keyset); let from_nan = between(f64::NAN, f64::INFINITY).await.unwrap(); assert!(from_nan.lo() > from_nan.hi(), "NaN sorts last"); let one = between(0.0, -0.0).await.unwrap(); diff --git a/packages/stack-encrypt/tests/owned_records.rs b/packages/stack-encrypt/tests/owned_records.rs index 1cf16006b3..945a17fbde 100644 --- a/packages/stack-encrypt/tests/owned_records.rs +++ b/packages/stack-encrypt/tests/owned_records.rs @@ -560,6 +560,63 @@ async fn a_searchable_ciphertext_is_not_leaked_when_the_context_is_refused() { .await; } +/// A record of one plaintext whose two fields name the equality index. +#[allow(dead_code)] // never built: its declaration is refused +#[derive(EncryptTarget)] +#[stash(plaintext = Probe)] +struct TwiceIndexedProbe { + first: EqualityTerm, + second: EqualityTerm, +} + +/// A set of terms that names one index twice is refused before any key is +/// requested. The refusal is handed the plaintext, so it must wipe it: the +/// target alone, and a record of one plaintext. +#[tokio::test] +async fn a_plaintext_is_not_leaked_when_its_indexes_are_refused() { + let (cipher, generates, _) = common::counting_cipher().await; + let keyset = cipher.default_keyset(); + let context = || nonempty!("users").with("email"); + let leaked = Arc::new(AtomicBool::new(false)); + + let alone = probe(&leaked) + .encrypt_into_with_context::<Encrypted<(EqualityTerm, EqualityTerm)>, _>(&keyset, context()) + .await; + assert!( + matches!( + alone.map(drop), + Err(Error::Plan(stack_encrypt::PlanError::DuplicateIndex { + index: "eq", + .. + })) + ), + "the target names the equality index twice" + ); + assert!( + !leaked.load(Ordering::SeqCst), + "the refused target dropped the plaintext unwiped" + ); + + let in_a_record = probe(&leaked) + .encrypt_into_with_context::<TwiceIndexedProbe, _>(&keyset, context()) + .await; + assert!( + matches!( + in_a_record.map(drop), + Err(Error::Plan(stack_encrypt::PlanError::DuplicateIndex { + index: "eq", + .. + })) + ), + "the record names the equality index twice" + ); + assert!( + !leaked.load(Ordering::SeqCst), + "the refused record dropped the plaintext unwiped" + ); + assert_eq!(generates.load(Ordering::SeqCst), 0, "no key requested"); +} + #[derive(Zeroize)] struct Pair { first: Secret, @@ -1116,18 +1173,16 @@ impl DecryptSource<i32> for FixedInteger { } #[tokio::test] -async fn an_owned_record_is_a_field_of_a_structural_record_and_a_plan() { - use stack_encrypt::plan::pick; - use stack_encrypt::{EncryptFrom, Plan}; - - #[derive(Clone)] +async fn an_owned_record_is_a_field_of_another_record() { + #[derive(Clone, Zeroize)] struct Profile { user: User, age: u32, } - /// A structural record with an owned record as a field. The owned derive - /// also implements `EncryptFrom` with a `DeclaredContext`, so this works. - #[derive(EncryptFrom)] + /// A record whose fields are records: a `struct` record with passthrough + /// fields, and a `plaintext` record of the structural leaves. Each is + /// given its field's context. + #[derive(EncryptTarget)] #[stash(struct = Profile, scope = "profiles")] struct EncryptedProfile { user: EncryptedUser, @@ -1141,10 +1196,7 @@ async fn an_owned_record_is_a_field_of_a_structural_record_and_a_plan() { user: alice(), age: 34, }; - let encrypted: EncryptedProfile = keyset - .encrypt_as(&profile, Default::default()) - .await - .unwrap(); + let encrypted: EncryptedProfile = profile.encrypt_into(&keyset).await.unwrap(); assert_eq!(encrypted.user.id, 42); assert_eq!( sent.lock().unwrap().generated(), @@ -1154,30 +1206,6 @@ async fn an_owned_record_is_a_field_of_a_structural_record_and_a_plan() { "profiles/age" ] ); - - // The owned record also works as a typed field of a plan, for the same - // reason. - let plan = Plan::context("profiles") - .fields::<Profile, _>() - .encrypt_into::<EncryptedUser, _>(pick("user", |p: &Profile| &p.user)) - .target::<Ciphertext, _>(pick("age", |p: &Profile| &p.age)) - .build() - .unwrap(); - let profile = Profile { - user: bob(), - age: 51, - }; - let mut values = cipher.encrypt(&profile).using(&plan).await.unwrap(); - let user: EncryptedUser = values.take("user").unwrap(); - assert_eq!(user.id, 43); - let age: Ciphertext = values.take("age").unwrap(); - assert_eq!( - keyset - .decrypt_with_context::<u32>(age, ("profiles", "age")) - .await - .unwrap(), - 51 - ); let _ = ( FixedInteger::encryption::<common::CountingSource>, RequiredContext::<u64>::into_inner, diff --git a/packages/stack-encrypt/tests/plan.rs b/packages/stack-encrypt/tests/plan.rs deleted file mode 100644 index a0e77bb147..0000000000 --- a/packages/stack-encrypt/tests/plan.rs +++ /dev/null @@ -1,1872 +0,0 @@ -//! The plan builder (`stack_encrypt::plan`): the chain lowers to the -//! combinators and produces the same bytes they do, a saved plan is the same -//! chain run later, queries derive under the write's labels, and every way a -//! plan can be refused is refused before any key is requested. -mod common; - -use std::sync::atomic::Ordering; - -use common::{counting_cipher, loads_counting_cipher, recording_cipher, stack_cipher}; -use stack_encrypt::kms::{FakeDataKeySource, IdentifiedBy}; -use stack_encrypt::op; -use stack_encrypt::plan::{ - Field, FieldKind, FieldSchema, FieldValues, Fields, Operation, PlanError, -}; -use stack_encrypt::sem::{EqualityTerm, MatchTerms, OreTerm}; -use stack_encrypt::target::{ - self, indexed, passthrough, Borrowed, DeclaredContext, Encrypted, Encryption, IndexSpec, -}; -use stack_encrypt::{ - nonempty, CipherText, Equality, Error, Label, Match, NonEmpty, Ope, Ore, Plan, StackCipher, - StackCipherText, -}; - -/// The record every test encrypts: two indexed fields, one sealed alone, -/// one carried through. -#[derive(Clone, Debug, PartialEq)] -struct User { - email: String, - age: u32, - notes: String, - id: u64, -} - -fn user() -> User { - User { - email: "bob@example.com".into(), - age: 34, - notes: "likes cheese".into(), - id: 42, - } -} - -impl Fields for User { - fn field_names(&self) -> Vec<&str> { - vec!["email", "age", "notes", "id"] - } - fn schema() -> Option<Vec<FieldSchema>> { - Some(vec![ - FieldSchema::of::<String>("email"), - FieldSchema::of::<u32>("age"), - FieldSchema::of::<String>("notes"), - FieldSchema::of::<u64>("id"), - ]) - } -} -impl Field<String> for User { - fn field(&self, name: &str) -> Option<&String> { - match name { - "email" => Some(&self.email), - "notes" => Some(&self.notes), - _ => None, - } - } -} -impl Field<u32> for User { - fn field(&self, name: &str) -> Option<&u32> { - (name == "age").then_some(&self.age) - } -} -impl Field<u64> for User { - fn field(&self, name: &str) -> Option<&u64> { - (name == "id").then_some(&self.id) - } -} - -/// The same record shape with no schema, and whose field names can be -/// set: a value whose fields only it knows, as a binding's is. -struct Loose { - names: Vec<&'static str>, - email: String, - age: u32, -} -impl Fields for Loose { - fn field_names(&self) -> Vec<&str> { - self.names.clone() - } -} -impl Field<String> for Loose { - fn field(&self, name: &str) -> Option<&String> { - (name == "email").then_some(&self.email) - } -} -impl Field<u32> for Loose { - fn field(&self, name: &str) -> Option<&u32> { - (name == "age").then_some(&self.age) - } -} - -type EmailTerms = (EqualityTerm, MatchTerms); -type AgeTerms = (EqualityTerm, OreTerm<u32>); - -fn users_plan<K: 'static>() -> Plan<User, K> { - Plan::context("users") - .fields() - .encrypt_index::<String>("email", (Equality, Match)) - .encrypt_index::<u32>("age", (Equality, Ore)) - .encrypt::<String>("notes") - .passthrough::<u64>("id") - .build() - .expect("a valid plan") -} - -fn plan_error<T: std::fmt::Debug>(result: Result<T, Error>) -> PlanError { - match result { - Err(Error::Plan(error)) => error, - other => panic!("expected a plan error, got {other:?}"), - } -} - -fn leaf_keyset(ciphertext: &StackCipherText) -> uuid::Uuid { - match ciphertext { - CipherText::Single(leaf) => leaf.keyset_id(), - other => panic!("expected a single leaf, got {other:?}"), - } -} - -/// The hand-composed combinator spelling of the four-field record, each -/// field under the derive's spelling of its context. -type HandOut = ( - ( - (Encrypted<EmailTerms>, Encrypted<AgeTerms>), - StackCipherText, - ), - u64, -); -fn hand_composed<'s, K: 'static>() -> Encryption<'s, User, HandOut, K, DeclaredContext> { - indexed::<String, _, Borrowed, _>((Equality, Match)) - .under(nonempty!("users").with("email")) - .project(|u: &User| &u.email) - .zip( - indexed::<u32, _, Borrowed, _>((Equality, Ore)) - .under(nonempty!("users").with("age")) - .project(|u: &User| &u.age), - ) - .zip( - target::ciphertext::<String, _, Borrowed>() - .under(nonempty!("users").with("notes")) - .project(|u: &User| &u.notes), - ) - .zip(passthrough::<u64, _, Borrowed, DeclaredContext>().project(|u: &User| &u.id)) -} - -#[tokio::test] -async fn the_chain_is_the_hand_composed_combinators_field_for_field() { - let (cipher, sent) = recording_cipher().await; - let user = user(); - - let mut row = cipher - .encrypt(&user) - .context("users") - .fields() - .encrypt_index::<String>("email", (Equality, Match)) - .encrypt_index::<u32>("age", (Equality, Ore)) - .encrypt::<String>("notes") - .passthrough::<u64>("id") - .await - .unwrap(); - let chain_sent = sent.lock().unwrap().generated(); - sent.lock().unwrap().generate.clear(); - - let keyset = cipher.default_keyset(); - let (((email, age), notes), id) = keyset - .run(hand_composed(), &user, DeclaredContext::default()) - .await - .unwrap(); - let hand_sent = sent.lock().unwrap().generated(); - - assert_eq!( - row.names().collect::<Vec<_>>(), - ["email", "age", "notes", "id"], - "the record has the plan's fields, in order" - ); - assert_eq!( - chain_sent, hand_sent, - "every data key under the same descriptor" - ); - assert_eq!( - chain_sent, - ["users/email", "users/age", "users/notes"], - "one key per sealed field, under <context>/<field>" - ); - - let chain_email: Encrypted<EmailTerms> = row.take("email").unwrap(); - let chain_age: Encrypted<AgeTerms> = row.take("age").unwrap(); - let chain_notes: StackCipherText = row.take("notes").unwrap(); - assert_eq!( - row.take::<u64>("id").unwrap(), - id, - "passthrough is the value" - ); - assert_eq!(chain_email.terms.0, email.terms.0, "email equality term"); - assert_eq!( - chain_email.terms.1.to_bytes(), - email.terms.1.to_bytes(), - "email match terms" - ); - assert_eq!(chain_age.terms.0, age.terms.0, "age equality term"); - assert_eq!( - chain_age.terms.1.to_bytes(), - age.terms.1.to_bytes(), - "age ORE term" - ); - - // Ciphertexts are randomised, so they are compared by opening each - // spelling's under the other's context. - let cipher = &cipher; - let open = |ct: StackCipherText, field: &str| { - let context = nonempty!("users").with(field.to_owned()); - async move { cipher.decrypt::<String, _>(ct, context).await } - }; - assert_eq!( - open(chain_email.ciphertext, "email").await.unwrap(), - user.email - ); - assert_eq!(open(chain_notes, "notes").await.unwrap(), user.notes); - let chain_age_plain: u32 = cipher - .decrypt(chain_age.ciphertext, nonempty!("users").with("age")) - .await - .unwrap(); - assert_eq!(chain_age_plain, user.age); - - let mut hand_row = FieldValues::new(); - hand_row - .insert("email", email) - .insert("age", age) - .insert("notes", notes) - .insert("id", id); - let users_plan = users_plan(); - let mut back = cipher.open(hand_row).using(&users_plan).await.unwrap(); - assert_eq!(back.take::<String>("email").unwrap(), user.email); - assert_eq!(back.take::<u32>("age").unwrap(), user.age); - assert_eq!(back.take::<String>("notes").unwrap(), user.notes); - assert_eq!(back.take::<u64>("id").unwrap(), user.id); - assert!(back.is_empty()); -} - -#[tokio::test] -async fn a_saved_plan_is_the_same_chain_and_decrypts_back() { - let cipher = stack_cipher().await; - let user = user(); - let users_plan = users_plan(); - - let mut saved = cipher.encrypt(&user).using(&users_plan).await.unwrap(); - let mut chained = cipher - .encrypt(&user) - .context("users") - .fields() - .encrypt_index::<String>("email", (Equality, Match)) - .encrypt_index::<u32>("age", (Equality, Ore)) - .encrypt::<String>("notes") - .passthrough::<u64>("id") - .await - .unwrap(); - let a: Encrypted<EmailTerms> = saved.take("email").unwrap(); - let b: Encrypted<EmailTerms> = chained.take("email").unwrap(); - assert_eq!(a.terms.0, b.terms.0); - saved.insert("email", a); - - let mut back = cipher.open(saved).using(&users_plan).await.unwrap(); - assert_eq!(back.take::<String>("email").unwrap(), user.email); - assert_eq!(back.take::<u32>("age").unwrap(), user.age); - assert_eq!(back.take::<String>("notes").unwrap(), user.notes); - assert_eq!(back.take::<u64>("id").unwrap(), user.id); - - // A plan is reusable: every run lowers it afresh. - let again = cipher.encrypt(&user).using(&users_plan).await.unwrap(); - assert_eq!(again.len(), 4); - let cloned = users_plan.clone(); - assert!(cipher.encrypt(&user).using(&cloned).await.is_ok()); -} - -#[tokio::test] -async fn a_query_derives_under_the_label_the_write_used() { - let cipher = stack_cipher().await; - let users_plan = users_plan(); - let mut row = cipher.encrypt(&user()).using(&users_plan).await.unwrap(); - let email: Encrypted<EmailTerms> = row.take("email").unwrap(); - let age: Encrypted<AgeTerms> = row.take("age").unwrap(); - - let email_plan = users_plan.field("email").unwrap(); - let age_plan = users_plan.field("age").unwrap(); - let eq: EqualityTerm = cipher - .query(op::Eq, "bob@example.com") - .on(&email_plan) - .await - .unwrap(); - assert_eq!( - eq, email.terms.0, - "equality query bytes equal the stored term" - ); - let matching = cipher - .query(op::Matches, "bob@example.com") - .on(&email_plan) - .await - .unwrap(); - assert_eq!(matching.to_bytes(), email.terms.1.to_bytes()); - let ore: OreTerm<u32> = cipher.query(op::Lt, &34u32).on(&age_plan).await.unwrap(); - assert_eq!(ore.to_bytes(), age.terms.1.to_bytes()); - - // A different value is a different term: the query is not a constant. - let other = cipher - .query(op::Eq, "eve@example.com") - .on(&email_plan) - .await - .unwrap(); - assert_ne!(other, email.terms.0); -} - -#[tokio::test] -async fn a_query_extended_like_the_write_matches_it() { - let cipher = stack_cipher().await; - let users_plan = users_plan(); - let mut row = cipher - .encrypt(&user()) - .using(&users_plan) - .extend(7u64) - .await - .unwrap(); - let email: Encrypted<EmailTerms> = row.take("email").unwrap(); - let email_plan = users_plan.field("email").unwrap(); - let query = |extend: Option<u64>| { - let chain = cipher.query(op::Eq, "bob@example.com"); - let chain = match extend { - Some(parts) => chain.extend(parts), - None => chain, - }; - chain.on(&email_plan) - }; - assert_eq!(query(Some(7)).await.unwrap(), email.terms.0); - assert_ne!(query(None).await.unwrap(), email.terms.0); - assert_ne!(query(Some(8)).await.unwrap(), email.terms.0); -} - -#[tokio::test] -async fn a_query_against_an_undeclared_index_or_type_is_refused() { - let cipher = stack_cipher().await; - let users_plan: Plan<User, FakeDataKeySource> = users_plan(); - let age_plan = users_plan.field("age").unwrap(); - let notes_plan = users_plan.field("notes").unwrap(); - - assert_eq!( - plan_error(cipher.query(op::Lt, &34u32).via(Ope).on(&age_plan).await), - PlanError::IndexNotDeclared { - field: "age".into(), - index: "ope" - } - ); - assert_eq!( - plan_error(cipher.query(op::Eq, "x").on(&notes_plan).await), - PlanError::IndexNotDeclared { - field: "notes".into(), - index: "eq" - } - ); - assert_eq!( - plan_error(cipher.query(op::Eq, &34u64).on(&age_plan).await), - PlanError::FieldType { - field: "age".into(), - expected: "u32" - } - ); - assert_eq!( - plan_error(users_plan.field("nope")), - PlanError::NoSuchField { - field: "nope".into() - } - ); -} - -#[tokio::test] -async fn a_one_value_plan_writes_queries_by_type_and_reads_back() { - let cipher = stack_cipher().await; - let age_plan = Plan::context("users/age") - .with((Equality, Ore)) - .build() - .unwrap(); - let out: Encrypted<AgeTerms> = cipher.encrypt(&34u32).using(&age_plan).await.unwrap(); - - let chained = cipher - .encrypt(&34u32) - .context("users/age") - .with((Equality, Ore)) - .await - .unwrap(); - assert_eq!(chained.terms.0, out.terms.0); - assert_eq!(chained.terms.1.to_bytes(), out.terms.1.to_bytes()); - - let eq = cipher.query(op::Eq, &34u32).on(&age_plan).await.unwrap(); - assert_eq!(eq, out.terms.0); - let ore = cipher.query(op::Gte, &34u32).on(&age_plan).await.unwrap(); - assert_eq!(ore.to_bytes(), out.terms.1.to_bytes()); - - // Byte-identical to the combinator, under the same label. - let hand = cipher - .default_keyset() - .run( - indexed::<u32, _, Borrowed, _>((Equality, Ore)), - &34u32, - target::CallerContext::from(NonEmpty::from(Label::parse("users/age").unwrap())), - ) - .await - .unwrap(); - assert_eq!(hand.terms.0, out.terms.0); - - let age: u32 = cipher.open(out).using(&age_plan).await.unwrap(); - assert_eq!(age, 34); - let age: u32 = cipher.open(hand.ciphertext).using(&age_plan).await.unwrap(); - assert_eq!(age, 34, "a bare ciphertext opens too"); - assert_eq!(age_plan.label().unwrap().to_string(), "users/age"); - assert_eq!(age_plan.indexes(), &(Equality, Ore)); -} - -#[tokio::test] -async fn the_one_tree_chain_is_the_cipher_directed_call_under_the_label() { - let (cipher, sent) = recording_cipher().await; - let body = String::from("the document"); - let ct = cipher - .encrypt(&body) - .context("documents/v2/body") - .await - .unwrap(); - assert_eq!(sent.lock().unwrap().generated(), ["documents/v2/body"]); - let label = Label::parse("documents/v2/body").unwrap(); - let back: String = cipher.decrypt(ct, label.clone()).await.unwrap(); - assert_eq!(back, body); - - let refused = cipher.encrypt(&body).context("not//plain").await; - assert!(matches!(plan_error(refused), PlanError::ContextLabel(_))); -} - -#[tokio::test] -async fn extend_extends_every_field_and_reading_back_needs_it() { - let (cipher, sent) = recording_cipher().await; - let users_plan = users_plan(); - let row = cipher - .encrypt(&user()) - .using(&users_plan) - .extend(7u64) - .await - .unwrap(); - assert_eq!( - sent.lock().unwrap().generated(), - [ - "(users/email)/7u64", - "(users/age)/7u64", - "(users/notes)/7u64" - ] - ); - let back = cipher - .open(row) - .using(&users_plan) - .extend(7u64) - .await - .unwrap(); - assert_eq!(back.get::<String>("email").unwrap(), "bob@example.com"); - - let row = cipher - .encrypt(&user()) - .using(&users_plan) - .extend(7u64) - .await - .unwrap(); - assert!( - matches!(cipher.open(row).using(&users_plan).await, Err(Error::Aead)), - "the fake key source ignores descriptors, so a missing extension reaches the AEAD" - ); -} - -/// `.extend(..)` through a one-value plan reaches the write, the query and -/// the open: the data key is asked for under the extended label, a query -/// term matches only under the same extension, and the value opens only -/// with it. -#[tokio::test] -async fn a_one_value_plan_extended_on_write_needs_the_extension_to_query_and_open() { - let (cipher, sent) = recording_cipher().await; - let age_plan = Plan::context("users/age") - .with::<u32, _>(Equality) - .build() - .unwrap(); - let write = || cipher.encrypt(&34u32).using(&age_plan).extend(7u64); - - let out = write().await.unwrap(); - assert_eq!(sent.lock().unwrap().generated(), ["(users/age)/7u64"]); - - let query = |parts: Option<u64>| { - let chain = cipher.query(op::Eq, &34u32).on(&age_plan); - match parts { - Some(parts) => chain.extend(parts), - None => chain, - } - }; - assert_eq!(query(Some(7)).await.unwrap(), out.terms); - assert_ne!(query(None).await.unwrap(), out.terms); - assert_ne!(query(Some(8)).await.unwrap(), out.terms); - - let back: u32 = cipher - .open(out) - .using(&age_plan) - .extend(7u64) - .await - .unwrap(); - assert_eq!(back, 34); - let unextended = cipher.open(write().await.unwrap()).using(&age_plan).await; - assert!(matches!(unextended, Err(Error::Aead)), "{unextended:?}"); - let other = cipher - .open(write().await.unwrap()) - .using(&age_plan) - .extend(8u64) - .await; - assert!(matches!(other, Err(Error::Aead)), "{other:?}"); -} - -/// The `index` verb writes terms alone: no data key is asked for, the -/// query of the field matches the stored term, and the terms are the bytes -/// `encrypt_index` writes for the same field. -#[tokio::test] -async fn an_index_only_field_matches_its_query_and_mints_no_data_key() { - let (cipher, sent) = recording_cipher().await; - let tokens_plan: Plan<User, _> = Plan::context("users") - .fields() - .index::<String>("email", (Equality, Match)) - .encrypt::<u32>("age") - .encrypt::<String>("notes") - .passthrough::<u64>("id") - .build() - .unwrap(); - let mut row = cipher.encrypt(&user()).using(&tokens_plan).await.unwrap(); - assert_eq!( - sent.lock().unwrap().generated(), - ["users/age", "users/notes"], - "an index-only field asks for no data key" - ); - let (eq, matching): EmailTerms = row.take("email").unwrap(); - - let email_plan = tokens_plan.field("email").unwrap(); - let query = cipher - .query(op::Eq, "bob@example.com") - .on(&email_plan) - .await - .unwrap(); - assert_eq!(query, eq, "the query derives under the write's label"); - - // Byte-identical to the terms `encrypt_index` writes for the same field. - let mut sealed = cipher.encrypt(&user()).using(&users_plan()).await.unwrap(); - let sealed: Encrypted<EmailTerms> = sealed.take("email").unwrap(); - assert_eq!(eq, sealed.terms.0); - assert_eq!(matching.to_bytes(), sealed.terms.1.to_bytes()); -} - -/// Repeated extensions append in order, and opening needs both parts. -#[tokio::test] -async fn a_second_extend_appends_to_the_first() { - let (cipher, sent) = recording_cipher().await; - let users_plan: Plan<User, _> = users_plan(); - let row = cipher - .encrypt(&user()) - .using(&users_plan) - .extend(1u64) - .extend(7u64) - .await - .unwrap(); - assert_eq!( - sent.lock().unwrap().generated(), - [ - "((users/email)/1u64)/7u64", - "((users/age)/1u64)/7u64", - "((users/notes)/1u64)/7u64" - ] - ); - let back = cipher - .open(row) - .using(&users_plan) - .extend(1u64) - .extend(7u64) - .await; - assert!(back.is_ok(), "{back:?}"); -} - -#[tokio::test] -async fn decrypt_returns_what_can_come_back_and_refuses_a_mismatched_row() { - let cipher = stack_cipher().await; - let tokens_plan: Plan<User, _> = Plan::context("users") - .fields() - .index::<String>("email", Equality) - .encrypt_index::<u32>("age", Ore) - .encrypt::<String>("notes") - .passthrough::<u64>("id") - .build() - .unwrap(); - let row = cipher.encrypt(&user()).using(&tokens_plan).await.unwrap(); - assert!( - row.get::<EqualityTerm>("email").is_some(), - "index alone: the term" - ); - - let back = cipher.open(row).using(&tokens_plan).await.unwrap(); - assert_eq!( - back.names().collect::<Vec<_>>(), - ["age", "notes", "id"], - "an index-only field does not come back" - ); - - // A row with an encrypt_index field stored as its bare ciphertext. - let mut row = cipher.encrypt(&user()).using(&tokens_plan).await.unwrap(); - let age: Encrypted<OreTerm<u32>> = row.take("age").unwrap(); - row.insert("age", age.ciphertext); - let back = cipher.open(row).using(&tokens_plan).await.unwrap(); - assert_eq!(back.get::<u32>("age"), Some(&34)); - - // An index-only field may be absent from a stored row. - let mut row = cipher.encrypt(&user()).using(&tokens_plan).await.unwrap(); - let _ = row.take::<EqualityTerm>("email").unwrap(); - assert!(cipher.open(row).using(&tokens_plan).await.is_ok()); - - let fresh = || async { cipher.encrypt(&user()).using(&tokens_plan).await.unwrap() }; - - let mut extra = fresh().await; - extra.insert("rogue", 1u8); - assert_eq!( - plan_error(cipher.open(extra).using(&tokens_plan).await), - PlanError::NotInPlan { - field: "rogue".into() - } - ); - for missing in ["notes", "id"] { - let mut row = fresh().await; - assert!(row.remove(missing)); - assert_eq!( - plan_error(cipher.open(row).using(&tokens_plan).await), - PlanError::NotInValue { - field: missing.into() - } - ); - } - for (field, expected) in [ - ("notes", std::any::type_name::<StackCipherText>()), - ("age", std::any::type_name::<StackCipherText>()), - ("id", "u64"), - ] { - let mut row = fresh().await; - row.insert(field, "wrong".to_string()); - assert_eq!( - plan_error(cipher.open(row).using(&tokens_plan).await), - PlanError::FieldType { - field: field.into(), - expected - } - ); - } -} - -#[tokio::test] -async fn a_value_that_does_not_match_the_plan_is_refused_without_a_key_request() { - let (cipher, generates, _) = counting_cipher().await; - let loose_plan: Plan<Loose, _> = Plan::context("users") - .fields() - .encrypt_index::<String>("email", Equality) - .encrypt::<u32>("age") - .build() - .unwrap(); - let loose = |names: Vec<&'static str>| Loose { - names, - email: "bob@example.com".into(), - age: 34, - }; - - assert!(cipher - .encrypt(&loose(vec!["email", "age"])) - .using(&loose_plan) - .await - .is_ok()); - generates.store(0, Ordering::SeqCst); - - assert_eq!( - plan_error( - cipher - .encrypt(&loose(vec!["email", "age", "rogue"])) - .using(&loose_plan) - .await - ), - PlanError::NotInPlan { - field: "rogue".into() - } - ); - assert_eq!( - plan_error( - cipher - .encrypt(&loose(vec!["email"])) - .using(&loose_plan) - .await - ), - PlanError::NotInValue { - field: "age".into() - } - ); - assert_eq!(generates.load(Ordering::SeqCst), 0, "no key requested"); - - // A field the value names but cannot produce at the plan's type. - let wrong_type: Plan<Loose, _> = Plan::context("users") - .fields() - .encrypt::<String>("email") - .encrypt::<String>("age") - .build() - .unwrap(); - assert_eq!( - plan_error( - cipher - .encrypt(&loose(vec!["email", "age"])) - .using(&wrong_type) - .await - ), - PlanError::FieldType { - field: "age".into(), - expected: "alloc::string::String" - } - ); - // A field the plan names that the value neither names nor produces. - let ghost: Plan<Loose, _> = Plan::context("users") - .fields() - .encrypt::<String>("email") - .encrypt::<u32>("age") - .encrypt::<String>("ghost") - .build() - .unwrap(); - assert_eq!( - plan_error( - cipher - .encrypt(&loose(vec!["email", "age"])) - .using(&ghost) - .await - ), - PlanError::NotInValue { - field: "ghost".into() - } - ); - // A field the value names but cannot produce at all. - assert_eq!( - plan_error( - cipher - .encrypt(&loose(vec!["email", "age", "ghost"])) - .using(&ghost) - .await - ), - PlanError::FieldType { - field: "ghost".into(), - expected: "alloc::string::String" - } - ); - assert_eq!(generates.load(Ordering::SeqCst), 0, "no key requested"); -} - -/// A chain naming a keyset is checked before the keyset is loaded: a plan -/// that does not hold up asks ZeroKMS nothing, the keyset lookup included, -/// and its own refusal is what comes back. -#[tokio::test] -async fn a_refused_chain_never_loads_the_keyset_it_names() { - let (cipher, loads) = loads_counting_cipher().await; - let users_plan: Plan<User, _> = users_plan(); - let age_plan = users_plan.field("age").unwrap(); - let doc = String::from("doc"); - let loose_plan: Plan<Loose, _> = Plan::context("users") - .fields() - .encrypt::<String>("email") - .encrypt::<String>("age") - .build() - .unwrap(); - let loose = |names: Vec<&'static str>| Loose { - names, - email: "bob@example.com".into(), - age: 34, - }; - let row = || async { cipher.encrypt(&user()).using(&users_plan).await.unwrap() }; - let mut rogue = row().await; - rogue.insert("rogue", 1u8); - let mut retyped = row().await; - retyped.insert("id", "wrong".to_string()); - assert_eq!( - loads.load(Ordering::SeqCst), - 0, - "the default keyset is in hand" - ); - - assert!(matches!( - plan_error( - cipher - .encrypt(&doc) - .context("docs//x") - .keyset("tenant") - .await - ), - PlanError::ContextLabel(_) - )); - assert!(matches!( - plan_error( - cipher - .encrypt(&34u32) - .context("users/age") - .with((Equality, Equality)) - .keyset("tenant") - .await - ), - PlanError::DuplicateIndex { .. } - )); - assert!(matches!( - plan_error( - cipher - .encrypt(&user()) - .context("users") - .fields() - .encrypt::<String>("email") - .encrypt::<String>("email") - .keyset("tenant") - .await - ), - PlanError::DuplicateField { .. } - )); - assert!(matches!( - plan_error( - cipher - .encrypt(&loose(vec!["email", "age", "rogue"])) - .using(&loose_plan) - .keyset("tenant") - .await - ), - PlanError::NotInPlan { field } if field == "rogue" - )); - assert!(matches!( - plan_error( - cipher - .encrypt(&vec![loose(vec!["email", "age"])]) - .using(&loose_plan) - .keyset("tenant") - .await - ), - PlanError::FieldType { field, .. } if field == "age" - )); - assert!(matches!( - plan_error( - cipher - .query(op::Gt, &34u32) - .via(Ope) - .on(&age_plan) - .keyset("tenant") - .await - ), - PlanError::IndexNotDeclared { .. } - )); - assert!(matches!( - plan_error(cipher.open(rogue).using(&users_plan).keyset("tenant").await), - PlanError::NotInPlan { field } if field == "rogue" - )); - assert!(matches!( - plan_error( - cipher - .open(vec![retyped]) - .using(&users_plan) - .keyset("tenant") - .await - ), - PlanError::FieldType { field, expected: "u64" } if field == "id" - )); - assert_eq!(loads.load(Ordering::SeqCst), 0, "no keyset was loaded"); - - let refused = stack_encrypt::all(( - cipher.encrypt(&doc).context("docs").keyset("tenant"), - cipher.encrypt(&doc).context("docs//x"), - )) - .await; - assert!(matches!(plan_error(refused), PlanError::ContextLabel(_))); - assert_eq!(loads.load(Ordering::SeqCst), 0, "nor in a batch"); - - assert!(cipher - .encrypt(&doc) - .context("docs") - .keyset("tenant") - .await - .is_ok()); - assert_eq!(loads.load(Ordering::SeqCst), 1, "a valid chain loads it"); -} - -/// A record whose stored column names are not label segments: a digit -/// first, a reserved character. -struct Odd { - user_id: String, - two_factor: u64, - created: u64, - id: u64, -} -impl Fields for Odd { - fn field_names(&self) -> Vec<&str> { - vec!["user_id", "2fa_enabled", "created(utc)", "id"] - } -} -impl Field<String> for Odd { - fn field(&self, name: &str) -> Option<&String> { - (name == "user_id").then_some(&self.user_id) - } -} -impl Field<u64> for Odd { - fn field(&self, name: &str) -> Option<&u64> { - match name { - "2fa_enabled" => Some(&self.two_factor), - "created(utc)" => Some(&self.created), - "id" => Some(&self.id), - _ => None, - } - } -} - -/// A passthrough field is under no label, so its name is only the record -/// key: any text builds, round-trips, and shares no identity with a sealed -/// field. A sealed or indexed field's name is still a label segment. -#[tokio::test] -async fn a_passthrough_name_need_not_be_a_label_segment() { - let cipher = stack_cipher().await; - let odd_plan: Plan<Odd, _> = Plan::context("users") - .fields() - .encrypt::<String>("user_id") - .identity("id") - .passthrough::<u64>("2fa_enabled") - .passthrough::<u64>("created(utc)") - .passthrough::<u64>("id") - .build() - .unwrap(); - assert_eq!(odd_plan.field("2fa_enabled").unwrap().label(), None); - - let value = Odd { - user_id: "u-1".into(), - two_factor: 1, - created: 1_700_000_000, - id: 42, - }; - let row = cipher.encrypt(&value).using(&odd_plan).await.unwrap(); - assert_eq!(row.get::<u64>("created(utc)"), Some(&1_700_000_000)); - let back = cipher.open(row).using(&odd_plan).await.unwrap(); - assert_eq!(back.get::<String>("user_id").unwrap(), "u-1"); - assert_eq!(back.get::<u64>("2fa_enabled"), Some(&1)); - assert_eq!(back.get::<u64>("id"), Some(&42)); - - let passthrough_plan = odd_plan.field("2fa_enabled").unwrap(); - assert_eq!( - plan_error(cipher.query(op::Eq, &1u64).on(&passthrough_plan).await), - PlanError::IndexNotDeclared { - field: "2fa_enabled".into(), - index: "eq" - }, - "a passthrough field answers no query" - ); - - let sealed = Plan::context("users") - .fields::<Odd, FakeDataKeySource>() - .encrypt::<String>("2fa_enabled") - .build(); - assert!(matches!( - plan_error(sealed), - PlanError::FieldLabel { field, .. } if field == "2fa_enabled" - )); - let indexed = Plan::context("users") - .fields::<Odd, FakeDataKeySource>() - .index::<u64>("created(utc)", Equality) - .build(); - assert!(matches!( - plan_error(indexed), - PlanError::FieldLabel { field, .. } if field == "created(utc)" - )); -} - -#[tokio::test] -async fn every_build_error_is_its_own() { - fn fields() -> stack_encrypt::plan::FieldsBuilder<User, FakeDataKeySource> { - Plan::context("users").fields() - } - let build = - |b: stack_encrypt::plan::FieldsBuilder<User, FakeDataKeySource>| plan_error(b.build()); - - assert!(matches!( - plan_error( - Plan::context("users//x") - .fields::<User, FakeDataKeySource>() - .build() - ), - PlanError::ContextLabel(_) - )); - assert_eq!( - build(fields().identity("email")), - PlanError::IdentityWithoutField - ); - assert!(matches!( - build(fields().encrypt::<String>("e/mail")), - PlanError::FieldLabel { field, .. } if field == "e/mail" - )); - assert!(matches!( - build(fields().encrypt::<String>("email").identity("1email")), - PlanError::FieldLabel { field, .. } if field == "email" - )); - assert_eq!( - build(fields().encrypt_index::<u32>("age", (Equality, Equality))), - PlanError::DuplicateIndex { - at: "age".into(), - index: "eq" - } - ); - assert_eq!( - build(fields().encrypt_index::<u32>("age", (Ore, Equality, Ore))), - PlanError::DuplicateIndex { - at: "age".into(), - index: "ore" - } - ); - assert_eq!( - build( - fields() - .encrypt_index::<String>("email", Equality) - .passthrough::<String>("email") - ), - PlanError::PassthroughIndexed { - field: "email".into() - } - ); - assert_eq!( - build( - fields() - .passthrough::<String>("email") - .index::<String>("email", Equality) - ), - PlanError::PassthroughIndexed { - field: "email".into() - } - ); - assert_eq!( - build( - fields() - .passthrough::<String>("email") - .encrypt::<String>("email") - ), - PlanError::DuplicateField { - field: "email".into() - }, - "passthrough beside a sealed field with no index is a field named twice" - ); - assert_eq!( - build( - fields() - .encrypt_index::<String>("email", Equality) - .encrypt::<String>("email") - ), - PlanError::DuplicateField { - field: "email".into() - }, - "an indexed field named again without passthrough is a field named twice" - ); - assert_eq!( - build( - fields() - .passthrough::<String>("email") - .passthrough::<String>("email") - ), - PlanError::DuplicateField { - field: "email".into() - }, - "passthrough twice, with no index, is a field named twice" - ); - assert_eq!( - build( - fields() - .encrypt::<String>("notes") - .encrypt::<String>("notes") - ), - PlanError::DuplicateField { - field: "notes".into() - } - ); - assert_eq!( - build( - fields() - .encrypt::<String>("email") - .encrypt::<String>("notes") - .identity("email") - ), - PlanError::SharedIdentity { - identity: "email".into(), - first: "email".into(), - second: "notes".into() - } - ); - assert_eq!( - build( - fields() - .encrypt::<String>("email") - .identity("contact") - .encrypt::<String>("notes") - .identity("contact") - ), - PlanError::SharedIdentity { - identity: "contact".into(), - first: "email".into(), - second: "notes".into() - } - ); - // The schema checks: a field the type lacks, one at the wrong type, and - // one of the type's the plan leaves unnamed. - let complete = |b: stack_encrypt::plan::FieldsBuilder<User, FakeDataKeySource>| { - b.encrypt_index::<String>("email", Equality) - .encrypt::<u32>("age") - .encrypt::<String>("notes") - .passthrough::<u64>("id") - }; - assert!(complete(fields()).build().is_ok()); - assert_eq!( - build(complete(fields()).encrypt::<String>("ghost")), - PlanError::NotInValue { - field: "ghost".into() - } - ); - assert_eq!( - build( - fields() - .encrypt::<String>("email") - .encrypt::<String>("age") - .encrypt::<String>("notes") - .passthrough::<u64>("id") - ), - PlanError::FieldType { - field: "age".into(), - expected: "alloc::string::String" - } - ); - assert_eq!( - build( - fields() - .encrypt::<String>("email") - .encrypt::<u32>("age") - .encrypt::<String>("notes") - ), - PlanError::NotInPlan { field: "id".into() } - ); - - // The one-value plan's own two. - assert!(matches!( - plan_error(Plan::context("a b/").with::<u32, _>(Equality).build()), - PlanError::ContextLabel(_) - )); - assert_eq!( - plan_error( - Plan::context("users/age") - .with::<u32, _>((Ore, Ore)) - .build() - ), - PlanError::DuplicateIndex { - at: "the value".into(), - index: "ore" - } - ); - // The chain reports the same errors when awaited. - let cipher = stack_cipher().await; - assert_eq!( - plan_error( - cipher - .encrypt(&34u32) - .context("users/age") - .with((Equality, Equality)) - .await - ), - PlanError::DuplicateIndex { - at: "the value".into(), - index: "eq" - } - ); - assert_eq!( - plan_error( - cipher - .encrypt(&user()) - .context("users") - .fields() - .encrypt::<String>("email") - .await - ), - PlanError::NotInPlan { - field: "age".into() - } - ); -} - -#[tokio::test] -async fn match_options_must_agree_for_a_query_to_be_declared() { - #![allow(clippy::needless_update)] - use stack_encrypt::sem::MatchConfig; - struct Shingles; - impl MatchConfig for Shingles { - fn options() -> stack_encrypt::sem::MatchOptions { - stack_encrypt::sem::MatchOptions { - downcase: false, - ..Default::default() - } - } - } - let cipher = stack_cipher().await; - let users_plan: Plan<User, FakeDataKeySource> = users_plan(); - let email_plan = users_plan.field("email").unwrap(); - let refused = plan_error( - cipher - .query(op::Matches, "bob") - .via(Match::<Shingles>::new()) - .on(&email_plan) - .await, - ); - assert_eq!( - refused, - PlanError::IndexOptions { - field: "email".into(), - declared: IndexSpec::Match(stack_encrypt::sem::MatchOptions::default()), - asked: IndexSpec::Match(Shingles::options()), - } - ); - let message = refused.to_string(); - assert!( - message.starts_with(r#"field "email" declares a match index with other options"#), - "the message points at the options, not a missing index: {message}" - ); - assert!(message.contains("downcase: false"), "{message}"); -} - -#[tokio::test] -async fn all_settles_several_chains_in_one_request() { - let (cipher, generates, retrieves) = counting_cipher().await; - let users_plan: Plan<User, _> = users_plan(); - let email_plan = users_plan.field("email").unwrap(); - let user = user(); - - let (row, probe) = stack_encrypt::all(( - cipher.encrypt(&user).using(&users_plan), - cipher.query(op::Eq, "bob@example.com").on(&email_plan), - )) - .await - .unwrap(); - assert_eq!(generates.load(Ordering::SeqCst), 1, "one generate call"); - let email: &Encrypted<EmailTerms> = row.get("email").unwrap(); - assert_eq!(email.terms.0, probe); - - let doc = String::from("doc"); - let (a, b, c) = stack_encrypt::all(( - cipher.encrypt(&doc).context("docs"), - cipher.encrypt(&user).using(&users_plan), - cipher.open(row).using(&users_plan), - )) - .await - .unwrap(); - assert_eq!( - generates.load(Ordering::SeqCst), - 2, - "one more generate call" - ); - assert_eq!(retrieves.load(Ordering::SeqCst), 1, "one retrieve call"); - assert_eq!(c.get::<String>("notes").unwrap(), "likes cheese"); - let (_, _, _, d) = stack_encrypt::all(( - cipher.encrypt(&doc).context("docs"), - cipher.encrypt(&doc).context("docs"), - cipher.open(a).using( - &Plan::context("docs") - .with::<String, _>(Equality) - .build() - .unwrap(), - ), - cipher.encrypt(&user).using(&users_plan), - )) - .await - .unwrap(); - assert_eq!(generates.load(Ordering::SeqCst), 3); - assert_eq!(retrieves.load(Ordering::SeqCst), 2); - assert_eq!(d.len(), 4); - let _ = b; -} - -#[tokio::test] -async fn all_runs_under_one_named_keyset_and_refuses_two() { - let (cipher, generates, _) = counting_cipher().await; - let tenant = cipher - .keyset(IdentifiedBy::Name("tenant".to_string().into())) - .await - .unwrap(); - let doc = String::from("doc"); - - let (a, b) = stack_encrypt::all(( - cipher.encrypt(&doc).context("docs").keyset("tenant"), - cipher.encrypt(&doc).context("docs").keyset(&tenant), - )) - .await - .unwrap(); - assert_eq!(leaf_keyset(&a), tenant.keyset_id()); - assert_eq!(leaf_keyset(&b), tenant.keyset_id()); - let (a, _) = stack_encrypt::all(( - cipher.encrypt(&doc).context("docs"), - cipher.encrypt(&doc).context("docs").keyset(&tenant), - )) - .await - .unwrap(); - assert_eq!( - leaf_keyset(&a), - tenant.keyset_id(), - "one chain names it for all" - ); - generates.store(0, Ordering::SeqCst); - - let refused = stack_encrypt::all(( - cipher.encrypt(&doc).context("docs").keyset("tenant"), - cipher.encrypt(&doc).context("docs").keyset("other"), - )) - .await; - assert!(matches!(refused, Err(Error::KeysetMismatch { .. }))); - assert_eq!( - generates.load(Ordering::SeqCst), - 0, - "refused before any request" - ); -} - -/// A batch settles through one client and resolves every keyset it names -/// there, so chains started on two ciphers are refused, before any keyset -/// is loaded or key requested on either. -#[tokio::test] -async fn all_refuses_chains_from_two_ciphers() { - let (one, one_loads) = loads_counting_cipher().await; - let (two, two_loads) = loads_counting_cipher().await; - let doc = String::from("doc"); - - for refused in [ - stack_encrypt::all(( - one.encrypt(&doc).context("docs"), - two.encrypt(&doc).context("docs").keyset("tenant"), - )) - .await, - stack_encrypt::all(( - one.encrypt(&doc).context("docs").keyset("tenant"), - two.encrypt(&doc).context("docs"), - )) - .await, - ] { - assert_eq!(plan_error(refused), PlanError::MixedCiphers); - } - let refused = stack_encrypt::all(( - one.encrypt(&doc).context("docs"), - one.encrypt(&doc).context("docs"), - two.encrypt(&doc).context("docs"), - )) - .await; - assert_eq!(plan_error(refused), PlanError::MixedCiphers, "any position"); - assert_eq!(one_loads.load(Ordering::SeqCst), 0); - assert_eq!(two_loads.load(Ordering::SeqCst), 0); - - let (a, b, c, d) = stack_encrypt::all(( - one.encrypt(&doc).context("docs"), - one.encrypt(&doc).context("docs"), - one.encrypt(&doc).context("docs"), - one.encrypt(&doc).context("docs").keyset("tenant"), - )) - .await - .unwrap(); - assert_eq!(leaf_keyset(&a), leaf_keyset(&d), "one cipher is one batch"); - assert_eq!(leaf_keyset(&b), leaf_keyset(&c)); - assert_eq!( - one_loads.load(Ordering::SeqCst), - 1, - "the named keyset, once" - ); -} - -/// An opening that names no keyset reads a leaf from any keyset inside a -/// batch, as it does alone: it is not confined to the keyset the batch's -/// other chains mint under. -#[tokio::test] -async fn all_lets_an_unnamed_opening_read_any_keyset() { - let (cipher, generates, retrieves) = counting_cipher().await; - let tenant = cipher - .keyset(IdentifiedBy::Name("tenant".to_string().into())) - .await - .unwrap(); - let default = cipher.default_keyset().keyset_id(); - let docs_plan = Plan::context("docs") - .with::<String, _>(Equality) - .build() - .unwrap(); - let doc = String::from("doc"); - let tenant_row = || async { - cipher - .encrypt(&doc) - .using(&docs_plan) - .keyset("tenant") - .await - .unwrap() - }; - let (first, second, third) = (tenant_row().await, tenant_row().await, tenant_row().await); - let default_row = cipher.encrypt(&doc).using(&docs_plan).await.unwrap(); - generates.store(0, Ordering::SeqCst); - - let (written, opened) = stack_encrypt::all(( - cipher.encrypt(&doc).context("docs"), - cipher.open(first).using(&docs_plan), - )) - .await - .unwrap(); - assert_eq!(leaf_keyset(&written), default, "minted under the default"); - assert_eq!(opened, "doc", "a tenant row opens beside a default write"); - assert_eq!(generates.load(Ordering::SeqCst), 1); - assert_eq!(retrieves.load(Ordering::SeqCst), 1); - - let (written, opened, also) = stack_encrypt::all(( - cipher.encrypt(&doc).context("docs").keyset("tenant"), - cipher.open(default_row).using(&docs_plan), - cipher.open(second).using(&docs_plan), - )) - .await - .unwrap(); - assert_eq!(leaf_keyset(&written), tenant.keyset_id()); - assert_eq!(opened, "doc", "a default row opens beside a tenant write"); - assert_eq!(also, "doc"); - assert_eq!( - retrieves.load(Ordering::SeqCst), - 3, - "the unnamed openings: one retrieve per keyset they read" - ); - - // A named opening is confined to the keyset it names, and an unnamed - // write beside it mints there too. - let (written, opened) = stack_encrypt::all(( - cipher.encrypt(&doc).context("docs"), - cipher.open(third).using(&docs_plan).keyset(&tenant), - )) - .await - .unwrap(); - assert_eq!(leaf_keyset(&written), tenant.keyset_id()); - assert_eq!(opened, "doc"); - let other_default = cipher.encrypt(&doc).using(&docs_plan).await.unwrap(); - let refused = stack_encrypt::all(( - cipher.encrypt(&doc).context("docs"), - cipher.open(other_default).using(&docs_plan).keyset(&tenant), - )) - .await; - assert!( - matches!(refused, Err(Error::ForeignKeyset { .. })), - "{refused:?}" - ); -} - -/// One chain whose plan does not hold up fails the whole batch before any -/// key is requested, however valid its siblings are. -#[tokio::test] -async fn all_refuses_a_bad_chain_beside_a_good_one_before_any_request() { - let (cipher, generates, retrieves) = counting_cipher().await; - let users_plan: Plan<User, _> = users_plan(); - let row = cipher.encrypt(&user()).using(&users_plan).await.unwrap(); - generates.store(0, Ordering::SeqCst); - let document = String::from("doc"); - let result = stack_encrypt::all(( - cipher.encrypt(&document).context("docs"), - cipher.open(row).using(&users_plan), - cipher - .encrypt(&user()) - .context("users") - .fields() - .encrypt::<String>("email"), - )) - .await; - assert!(matches!( - plan_error(result), - PlanError::NotInPlan { field } if field == "age" - )); - assert_eq!(generates.load(Ordering::SeqCst), 0); - assert_eq!(retrieves.load(Ordering::SeqCst), 0); -} - -#[tokio::test] -async fn keyset_selects_the_minting_keyset_and_scopes_an_opening() { - let cipher = stack_cipher().await; - let tenant = cipher - .keyset(IdentifiedBy::Name("tenant".to_string().into())) - .await - .unwrap(); - let default = cipher.default_keyset().keyset_id(); - let doc = String::from("doc"); - - let plain = cipher.encrypt(&doc).context("docs").await.unwrap(); - assert_eq!(leaf_keyset(&plain), default); - let by_name = cipher - .encrypt(&doc) - .context("docs") - .keyset("tenant") - .await - .unwrap(); - assert_eq!(leaf_keyset(&by_name), tenant.keyset_id()); - let by_id = cipher - .encrypt(&doc) - .context("docs") - .keyset(tenant.keyset_id()) - .await - .unwrap(); - assert_eq!(leaf_keyset(&by_id), tenant.keyset_id()); - let by_handle = cipher - .encrypt(&doc) - .context("docs") - .keyset(tenant.clone()) - .await - .unwrap(); - assert_eq!(leaf_keyset(&by_handle), tenant.keyset_id()); - let by_string = cipher - .encrypt(&doc) - .context("docs") - .keyset("tenant".to_string()) - .await - .unwrap(); - assert_eq!(leaf_keyset(&by_string), tenant.keyset_id()); - - let docs_plan = Plan::context("docs") - .with::<String, _>(Equality) - .build() - .unwrap(); - let opened: String = cipher.open(by_name).using(&docs_plan).await.unwrap(); - assert_eq!(opened, "doc", "unscoped, a leaf from any keyset opens"); - let refused = cipher.open(plain).using(&docs_plan).keyset(&tenant).await; - assert!( - matches!(refused, Err(Error::ForeignKeyset { .. })), - "scoped to a keyset, a leaf from another is refused: {refused:?}" - ); -} - -#[tokio::test] -async fn a_collection_runs_in_one_request() { - let (cipher, generates, retrieves) = counting_cipher().await; - let users_plan: Plan<User, _> = users_plan(); - let users = vec![user(), user(), user()]; - - let rows = cipher.encrypt(&users).using(&users_plan).await.unwrap(); - assert_eq!(rows.len(), 3); - assert_eq!( - generates.load(Ordering::SeqCst), - 1, - "one generate for every row" - ); - let rows = cipher - .encrypt(&users[..2]) - .using(&users_plan) - .await - .unwrap(); - assert_eq!(rows.len(), 2); - assert_eq!(generates.load(Ordering::SeqCst), 2); - - let back = cipher.open(rows).using(&users_plan).await.unwrap(); - assert_eq!(back.len(), 2); - assert_eq!( - retrieves.load(Ordering::SeqCst), - 1, - "one retrieve for every row" - ); - assert_eq!(back[1].get::<u32>("age"), Some(&34)); - - let ages = vec![1u32, 2, 3]; - let age_plan = Plan::context("users/age") - .with::<u32, _>(Equality) - .build() - .unwrap(); - let out = cipher.encrypt(&ages).using(&age_plan).await.unwrap(); - assert_eq!(out.len(), 3); - let out = cipher.encrypt(&ages[1..]).using(&age_plan).await.unwrap(); - assert_eq!(out.len(), 2); - assert_eq!(generates.load(Ordering::SeqCst), 4); -} - -#[tokio::test] -async fn nothing_touches_a_key_before_the_await() { - let (cipher, generates, retrieves) = counting_cipher().await; - let users_plan: Plan<User, _> = users_plan(); - let user = user(); - - let chain = cipher.encrypt(&user).using(&users_plan).keyset("tenant"); - drop(chain); - assert_eq!(generates.load(Ordering::SeqCst), 0); - - // A chain yields its Pending synchronously: terms derived, key requests - // queued, nothing sent until it is awaited. - let keyset = cipher.default_keyset(); - let pending = cipher - .encrypt(&user) - .using(&users_plan) - .prepare(&keyset, false); - assert_eq!(generates.load(Ordering::SeqCst), 0, "prepared, not sent"); - let row = pending.await.unwrap(); - assert_eq!(generates.load(Ordering::SeqCst), 1); - let pending = cipher.open(row).using(&users_plan).prepare(&keyset, true); - assert_eq!(retrieves.load(Ordering::SeqCst), 0); - assert!(pending.await.is_ok()); - assert_eq!(retrieves.load(Ordering::SeqCst), 1); -} - -#[tokio::test] -async fn a_plan_lowers_to_an_encryption_and_a_decryption_held_by_value() { - let cipher = stack_cipher().await; - let users_plan: Plan<User, _> = users_plan(); - let keyset = cipher.default_keyset(); - let row = keyset - .run( - users_plan.encryption(None), - &user(), - DeclaredContext::default(), - ) - .await - .unwrap(); - let back = keyset - .run_decryption(users_plan.decryption(row, None, DeclaredContext::default())) - .await - .unwrap(); - assert_eq!(back.get::<u64>("id"), Some(&42)); - - // A record built at run time is a value a plan takes apart too. - let mut dynamic = FieldValues::new(); - dynamic - .insert("email", "a@b.c".to_string()) - .insert("age", 7u32); - let dynamic_plan: Plan<FieldValues, _> = Plan::context("rows") - .fields() - .encrypt_index::<String>("email", Equality) - .encrypt::<u32>("age") - .build() - .unwrap(); - let row = keyset - .run( - dynamic_plan.encryption(None), - &dynamic, - DeclaredContext::default(), - ) - .await - .unwrap(); - let back = keyset - .run_decryption(dynamic_plan.decryption(row, None, DeclaredContext::default())) - .await - .unwrap(); - assert_eq!(back.get::<u32>("age"), Some(&7)); -} - -#[test] -fn a_plan_is_data_its_fields_say_what_they_declare() { - let users_plan: Plan<User, FakeDataKeySource> = users_plan(); - assert_eq!(users_plan.label().unwrap().to_string(), "users"); - let fields: Vec<_> = users_plan.field_plans().collect(); - assert_eq!(fields.len(), 4); - assert_eq!(fields[0].name(), "email"); - assert_eq!(fields[0].label().unwrap().to_string(), "users/email"); - assert_eq!(fields[0].kind(), FieldKind::EncryptIndex); - assert_eq!(fields[0].indexes()[0], IndexSpec::Equality); - assert_eq!(fields[0].indexes()[1].key(), "match"); - assert_eq!(fields[0].type_name(), "alloc::string::String"); - assert_eq!(fields[1].indexes(), [IndexSpec::Equality, IndexSpec::Ore]); - assert_eq!(fields[2].kind(), FieldKind::Encrypt); - assert!(fields[2].indexes().is_empty()); - assert_eq!(fields[3].kind(), FieldKind::Passthrough); - assert_eq!(fields[3].label(), None, "a passthrough field has no label"); - - let renamed: Plan<User, FakeDataKeySource> = Plan::context("users") - .fields() - .encrypt::<String>("email") - .identity("contact") - .encrypt::<u32>("age") - .encrypt::<String>("notes") - .passthrough::<u64>("id") - .build() - .unwrap(); - assert_eq!( - renamed.field("email").unwrap().label().unwrap().to_string(), - "users/contact", - "a pinned identity keys the field, not its name" - ); - let index_only: Plan<User, FakeDataKeySource> = Plan::context("users") - .fields() - .index::<String>("email", Equality) - .encrypt::<u32>("age") - .encrypt::<String>("notes") - .passthrough::<u64>("id") - .build() - .unwrap(); - assert_eq!(index_only.field("email").unwrap().kind(), FieldKind::Index); - - let shown = format!("{users_plan:?}"); - assert!(shown.starts_with(r#"Plan { context: "users", fields: [FieldPlan { name: "email""#)); - let builder = Plan::context("users") - .fields::<User, FakeDataKeySource>() - .encrypt::<u32>("age"); - assert_eq!( - format!("{builder:?}"), - r#"FieldsBuilder { context: [Ok(Label(["users"]))], fields: [("age", Encrypt, [])] }"# - ); - let value_plan = Plan::context("a").with::<u32, _>(Equality); - assert_eq!( - format!("{value_plan:?}"), - r#"ValuePlanBuilder { context: [Ok(Label(["a"]))], shape: Indexed(Equality) }"# - ); - let built = value_plan.build().unwrap(); - assert_eq!( - format!("{:?}", built.clone()), - r#"ValuePlan { context: Some("a"), shape: Indexed(Equality) }"# - ); - assert_eq!( - format!("{:?}", Plan::context("x")), - r#"PlanContext { context: Ok(Label(["x"])) }"# - ); -} - -#[tokio::test] -async fn a_chain_shows_no_value() { - let cipher = stack_cipher().await; - let secret = String::from("hunter2"); - let users_plan: Plan<User, _> = users_plan(); - let email_plan = users_plan.field("email").unwrap(); - let shown = [ - format!("{:?}", cipher.encrypt(&secret)), - format!("{:?}", cipher.encrypt(&secret).context("x")), - format!("{:?}", cipher.encrypt(&secret).context("x").with(Equality)), - format!("{:?}", cipher.encrypt(&user()).context("x").fields()), - format!("{:?}", cipher.encrypt(&user()).using(&users_plan)), - format!("{:?}", cipher.query(op::Eq, &secret)), - format!("{:?}", cipher.query(op::Eq, &secret).via(Equality)), - format!("{:?}", cipher.query(op::Eq, &secret).on(&email_plan)), - format!( - "{:?}", - cipher.query(op::Between, &(secret.clone(), secret.clone())) - ), - format!("{:?}", cipher.open(secret.clone())), - format!("{:?}", cipher.open(FieldValues::new()).using(&users_plan)), - format!( - "{:?}", - stack_encrypt::all(( - cipher.encrypt(&secret).context("x"), - cipher.encrypt(&secret).context("y") - )) - ), - ]; - for chain in shown { - assert!(!chain.contains("hunter2"), "{chain}"); - assert!(chain.ends_with(" { .. }"), "{chain}"); - } -} - -#[tokio::test] -async fn a_keyset_choice_names_what_it_holds() { - let cipher = stack_cipher().await; - let handle: stack_encrypt::plan::KeysetChoice<'_, _> = (&cipher.default_keyset()).into(); - assert!(format!("{handle:?}").starts_with("Handle(KeysetCipher")); - let named: stack_encrypt::plan::KeysetChoice<'_, FakeDataKeySource> = "tenant".into(); - assert_eq!( - format!("{named:?}"), - r#"Named(Name(Name { inner: "tenant" }))"# - ); -} - -/// A context or keyset name held in a `String` is passed by reference, as -/// a config value would be. -#[tokio::test] -async fn a_borrowed_string_names_a_context_and_a_keyset() { - let cipher = stack_cipher().await; - let tenant = cipher - .keyset(IdentifiedBy::Name("tenant".to_string().into())) - .await - .unwrap(); - let context = String::from("users/age"); - let tenant_name = String::from("tenant"); - - let age_plan = Plan::context(&context) - .with::<u32, _>(Equality) - .build() - .unwrap(); - assert_eq!( - age_plan.label().map(ToString::to_string).as_deref(), - Some("users/age") - ); - let sealed = cipher - .encrypt(&34u32) - .context(&context) - .keyset(&tenant_name) - .await - .unwrap(); - assert_eq!(leaf_keyset(&sealed), tenant.keyset_id()); - let back: u32 = cipher - .open(sealed) - .using(&age_plan) - .keyset(&tenant_name) - .await - .unwrap(); - assert_eq!(back, 34); - let not_plain = String::from("users//age"); - assert!(matches!( - plan_error(Plan::context(&not_plain).with::<u32, _>(Equality).build()), - PlanError::ContextLabel(_) - )); -} - -#[tokio::test] -async fn every_chain_honours_its_named_keyset() { - let cipher = stack_cipher().await; - let tenant = cipher - .keyset(IdentifiedBy::Name("tenant".to_string().into())) - .await - .unwrap(); - let id = tenant.keyset_id(); - assert_ne!(id, cipher.default_keyset().keyset_id()); - let user = user(); - - let indexed = cipher - .encrypt(&34u32) - .context("users/age") - .with(Equality) - .keyset("tenant") - .await - .unwrap(); - assert_eq!(leaf_keyset(&indexed.ciphertext), id); - - let mut fields = cipher - .encrypt(&user) - .context("users") - .fields() - .encrypt::<String>("email") - .encrypt::<u32>("age") - .encrypt::<String>("notes") - .passthrough::<u64>("id") - .keyset("tenant") - .await - .unwrap(); - assert_eq!( - leaf_keyset(&fields.take::<StackCipherText>("notes").unwrap()), - id - ); - - let users_plan: Plan<User, _> = users_plan(); - let mut row = cipher - .encrypt(&user) - .using(&users_plan) - .keyset("tenant") - .await - .unwrap(); - assert_eq!( - leaf_keyset(&row.take::<StackCipherText>("notes").unwrap()), - id - ); - - // A term is keyed by the keyset's index key: a query under the tenant - // matches the tenant's term, not the default keyset's. - let email: Encrypted<EmailTerms> = row.take("email").unwrap(); - let email_plan = users_plan.field("email").unwrap(); - let under_tenant = cipher - .query(op::Eq, "bob@example.com") - .on(&email_plan) - .keyset("tenant") - .await - .unwrap(); - let under_default = cipher - .query(op::Eq, "bob@example.com") - .on(&email_plan) - .await - .unwrap(); - assert_eq!(under_tenant, email.terms.0); - assert_ne!(under_default, email.terms.0); -} - -// `StackCipher` must stay nameable in this binary's imports. -#[allow(dead_code)] -fn _cipher_type(_: &StackCipher<FakeDataKeySource>) {} diff --git a/packages/stack-encrypt/tests/plan_grammar.rs b/packages/stack-encrypt/tests/plan_grammar.rs index 44977bcf46..36a764acff 100644 --- a/packages/stack-encrypt/tests/plan_grammar.rs +++ b/packages/stack-encrypt/tests/plan_grammar.rs @@ -1,29 +1,41 @@ -//! The widened plan grammar: two starts, three context sources (exactly -//! one), the typed verb `encrypt_into` in both positions, and the picker. +//! Where a plan that is data gets its context, and what it refuses before +//! any key is requested. //! -//! The load-bearing claim is again byte identity: a typed field lowers to -//! the same bytes as the data verbs it replaces, for every index set -//! `Encrypted<Terms>` names, and a one-value typed plan is the `plaintext = -//! T` derive's record. +//! A plan (`dynamic::record::Plan`) takes the context of its fields from +//! one of two places: the declaration, where each field names its label, or +//! a field of each record (`context_field`), which holds the label the +//! record's other fields run under. A caller adds a scope to either with +//! `Planned`. A reader of a plan with a context field can name the context +//! it expects each record to store. +//! +//! These tests hold the rules that follow from that, through the public +//! entry points, with a key source that counts its calls: +//! +//! - a query on a plan that reads its context from the record names the +//! context, and is refused when it names none; +//! - a context given by two sources is refused; +//! - a context that is not a plain label is refused; +//! - a query under the context or the scope the caller names derives the +//! stored term, and another derives another term; +//! - an empty batch has the scope of its call checked, as a batch of +//! records has. +//! +//! Each refusal is made before any key is requested. A caller holds its +//! keyset before it runs a plan, so no test here observes a keyset load. +#![cfg(feature = "dynamic")] mod common; use std::fmt::Debug; use std::sync::atomic::Ordering; -use common::{counting_cipher, loads_counting_cipher, recording_cipher, stack_cipher}; -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::op; -use stack_encrypt::plan::{pick, Field, FieldKind, FieldValues, Fields, Opens, PlanError, Runs}; -use stack_encrypt::sem::{ - EqualityTerm, MatchConfig, MatchOptions, MatchTerms, OpeTerm, OreBlockTerm, OreTerm, Tokenizer, -}; -use stack_encrypt::target::{ - CallerContext, DeclaredContext, DecryptField, DecryptInto, Decryptable, Decryption, - EncryptFrom, Encrypted, Encryption, IndexSpec, Indexes, TermSet, -}; +use common::frames::{frame, object}; +use common::{counting_cipher, recording_cipher}; +use stack_encrypt::dynamic::{self, record, NoTargets, Op, Operand, Planned, ValueFrame}; +use stack_encrypt::plan::IntoLabel; +use stack_encrypt::target::Scope; use stack_encrypt::{ - nonempty, Decrypt, Encrypt, Equality, Error, ErrorPayload, Label, Match, NonEmpty, Ope, Ore, - Plan, StackCipherText, + AsProtectedRef as _, CipherText, DecryptFrom, EncryptInto, Error, Label, PlanError, + StackCipherText, }; fn plan_error<T: Debug>(result: Result<T, Error>) -> PlanError { @@ -37,1824 +49,765 @@ fn two(first: &'static str, second: &'static str) -> PlanError { PlanError::TwoContextSources { first, second } } -/// A value with no `Fields` impl at all: only pickers can read it. -#[derive(Clone, Debug, PartialEq)] -struct User { - email: String, - age: u32, +/// One field's declaration: its label, its outputs and its type. +fn field(context: &[&str], outputs: &[&str], kind: &str) -> ValueFrame { + object(vec![ + ("context", frame(context.to_vec())), + ("outputs", frame(outputs.to_vec())), + ("type", frame(kind)), + ]) } -fn user() -> User { - User { - email: "bob@example.com".into(), - age: 34, - } +fn plan_of(declaration: Vec<(&str, ValueFrame)>) -> Result<record::Plan, dynamic::Error> { + record::plan(object(declaration).as_protected_ref()) } -fn email(u: &User) -> &String { - &u.email +/// A plan whose context is in the declaration: `email` sealed and indexed +/// under `users/email`, `age` sealed under `users/age`. +fn users_plan() -> record::Plan { + plan_of(vec![ + ("email", field(&["users", "email"], &["c", "eq"], "string")), + ("age", field(&["users", "age"], &["c"], "uint32")), + ]) + .expect("the users plan parses") } -fn age(u: &User) -> &u32 { - &u.age + +fn user() -> ValueFrame { + object(vec![ + ("email", frame("bob@example.com")), + ("age", frame(34u32)), + ]) } -/// The picker fields plan every context-source test runs, without a -/// context of its own. -fn contextless_plan<K: 'static>() -> Plan<User, K> { - Plan::fields() - .encrypt_index(("email", email), Equality) - .encrypt(("age", age)) - .build() - .unwrap() +/// A plan that reads its context from the record: `tenant` holds the +/// label, and `email` is sealed and indexed under `<tenant>/email`. +fn records_plan() -> record::Plan { + plan_of(vec![ + ("context_field", frame("tenant")), + ("tenant", field(&["tenant"], &["passthrough"], "string")), + ("email", field(&["email"], &["c", "eq"], "string")), + ]) + .expect("the records plan parses") } -// --- Two starts -------------------------------------------------------------- +fn tenant_record(tenant: &str) -> ValueFrame { + object(vec![ + ("tenant", frame(tenant)), + ("email", frame("bob@example.com")), + ]) +} -#[tokio::test] -async fn both_starts_build_the_plan_the_context_start_builds() { - let (cipher, sent) = recording_cipher().await; - let fields_first = Plan::fields() - .context("users") - .encrypt_index(("email", email), Equality) - .encrypt(("age", age)) - .build() - .unwrap(); - let context_first = Plan::context("users") - .fields() - .encrypt_index(("email", email), Equality) - .encrypt(("age", age)) - .build() - .unwrap(); - assert_eq!(fields_first.label(), context_first.label()); - assert_eq!( - fields_first.field_plans().collect::<Vec<_>>(), - context_first.field_plans().collect::<Vec<_>>() - ); +/// The records plan under the context a call names: `email` under +/// `<context>/email`, with no context field. A binding sends this plan for +/// a query, because a query has no record to read the context from. +fn under(context: &str) -> Result<record::Plan, dynamic::Error> { + let mut label = context.split('/').collect::<Vec<_>>(); + label.push("email"); + plan_of(vec![("email", field(&label, &["c", "eq"], "string"))]) +} - let mut a = cipher.encrypt(&user()).using(&fields_first).await.unwrap(); - let mut b = cipher.encrypt(&user()).using(&context_first).await.unwrap(); - assert_eq!( - sent.lock().unwrap().generated(), - ["users/email", "users/age", "users/email", "users/age"] - ); - let a: Encrypted<EqualityTerm> = a.take("email").unwrap(); - let b: Encrypted<EqualityTerm> = b.take("email").unwrap(); - assert_eq!(a.terms, b.terms); +fn label(text: &str) -> Label { + Label::parse(text).expect("a label") +} - let value_first = Plan::value::<u32>() - .context("users/age") - .with((Equality, Ore)) - .build() - .unwrap(); - let context_first = Plan::context("users/age") - .with::<u32, _>((Equality, Ore)) - .build() - .unwrap(); - assert_eq!(value_first.label(), context_first.label()); - let a = cipher.encrypt(&34u32).using(&value_first).await.unwrap(); - let b = cipher.encrypt(&34u32).using(&context_first).await.unwrap(); - assert_eq!(a.terms, b.terms); +/// The outputs of the field `name` of one stored record. +fn outputs<'t>(record: &'t StackCipherText, name: &str) -> &'t [(String, StackCipherText)] { + let CipherText::Map(fields) = record else { + panic!("a record is a map of fields") + }; + let (_, CipherText::Map(outputs)) = fields + .iter() + .find(|(key, _)| key == name) + .expect("the field") + else { + panic!("a field is a map of outputs") + }; + outputs +} + +/// The equality term a stored record holds for `email`. +fn stored_term(record: &StackCipherText) -> Vec<u8> { + let (_, CipherText::Passthrough(term)) = outputs(record, "email") + .iter() + .find(|(key, _)| key == "eq") + .expect("the term") + else { + panic!("a term is a passthrough") + }; + vitaminc_aead_frame::serde::from_slice::<&[u8]>(&term.0) + .expect("a term is bytes") + .to_vec() +} + +/// A stored record with its `tenant` replaced, or removed for `None`, as +/// someone with write access to the row would leave it. +fn with_stored_tenant(record: StackCipherText, tenant: Option<&str>) -> StackCipherText { + let CipherText::Map(mut fields) = record else { + panic!("a record is a map of fields") + }; + fields.retain(|(key, _)| key != "tenant"); + if let Some(tenant) = tenant { + fields.push(( + "tenant".into(), + CipherText::Map(vec![( + "passthrough".into(), + CipherText::Passthrough(common::passthrough(tenant)), + )]), + )); + } + CipherText::Map(fields) +} + +/// The equality term a query for `bob@example.com` derives under `plan`. +async fn query<K>(keyset: &stack_encrypt::KeysetCipher<'_, K>, plan: &record::Plan) -> Vec<u8> +where + K: stack_encrypt::kms::DataKeySource + stack_encrypt::kms::IndexKeySource + Sync + 'static, +{ + let term = dynamic::query_term(keyset, plan, "email", Op::Eq, "bob@example.com", &NoTargets) + .expect("the plan answers the query") + .await + .expect("the term"); + match term.operand() { + Operand::One(bytes) => bytes.clone(), + other => panic!("one term, got {other:?}"), + } } -// --- The context, from the call ------------------------------------------------ +// --- The context, from a field of the record ----------------------------------- #[tokio::test] -async fn a_plan_built_without_a_context_takes_the_calls() { +async fn a_context_field_is_the_context_of_every_other_field() { let (cipher, sent) = recording_cipher().await; - let users_plan = contextless_plan(); - assert_eq!(users_plan.label(), None); - assert_eq!(users_plan.context_field(), None); - let email_plan = users_plan.field("email").unwrap(); - assert_eq!(email_plan.label(), None, "the label is known only per call"); - assert_eq!(email_plan.identity(), "email"); + let keyset = cipher.default_keyset(); + let plan = records_plan(); + assert_eq!(plan.context_field(), Some("tenant")); + assert_eq!(plan.label(), None, "the plan has no context of its own"); - let mut record = cipher - .encrypt(&user()) - .context("tenants/acme") - .using(&users_plan) + let record = record::encrypt(&keyset, tenant_record("tenants/acme"), &plan) + .unwrap() .await .unwrap(); assert_eq!( sent.lock().unwrap().generated(), - ["tenants/acme/email", "tenants/acme/age"], - "every field under <call context>/<identity>" + ["tenants/acme/email"], + "the field is sealed under <context field>/<identity>" ); - let stored: Encrypted<EqualityTerm> = record.take("email").unwrap(); - - let query_value = cipher - .query(op::Eq, "bob@example.com") - .context("tenants/acme") - .on(&email_plan) - .await - .unwrap(); + let (_, CipherText::Passthrough(tenant)) = &outputs(&record, "tenant")[0] else { + panic!("the context field is a passthrough") + }; assert_eq!( - query_value, stored.terms, - "a query naming the same context matches" + tenant.0, + common::passthrough("tenants/acme").0, + "the context field is carried as it is" ); - let elsewhere = cipher - .query(op::Eq, "bob@example.com") - .context("tenants/globex") - .on(&email_plan) - .await - .unwrap(); - assert_ne!(elsewhere, stored.terms); - record.insert("email", stored); - let back = cipher - .open(record) - .context("tenants/acme") - .using(&users_plan) + // The same bytes as a plan given that context in its declaration. + let fixed = under("tenants/acme").unwrap(); + let fixed_record = record::encrypt( + &keyset, + object(vec![("email", frame("bob@example.com"))]), + &fixed, + ) + .unwrap() + .await + .unwrap(); + assert_eq!(stored_term(&record), stored_term(&fixed_record)); + + // A query names the context field's value. + assert_eq!(query(&keyset, &fixed).await, stored_term(&record)); + + // Opens with no expectation, under the stored context. + let back = record::decrypt(&cipher, record, &plan, None) + .unwrap() .await .unwrap(); - assert_eq!(back.get::<String>("email"), Some(&user().email)); - assert_eq!(back.get::<u32>("age"), Some(&34)); + assert_eq!(back, tenant_record("tenants/acme")); } +/// A batch through a plan with a context field seals each record under its +/// own tenant, and a context the reader expects is checked against every +/// record of it. #[tokio::test] -async fn a_plan_run_with_no_context_is_refused_before_any_key_request() { - let (cipher, generates, retrieves) = counting_cipher().await; - let users_plan = Plan::fields() - .encrypt_index(("email", email), Equality) - .build() +async fn a_batch_through_a_context_field_plan_seals_each_record_under_its_own_tenant() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + let plan = records_plan(); + let records = || { + frame(vec![ + tenant_record("tenants/acme"), + tenant_record("tenants/globex"), + ]) + }; + let written = record::encrypt(&keyset, records(), &plan) + .unwrap() + .await .unwrap(); assert_eq!( - plan_error(cipher.encrypt(&user()).using(&users_plan).await), - PlanError::NoContext - ); - assert_eq!( - plan_error( - cipher - .query(op::Eq, "bob@example.com") - .on(&users_plan.field("email").unwrap()) - .await - ), - PlanError::NoContext + sent.lock().unwrap().generated(), + ["tenants/acme/email", "tenants/globex/email"], ); - let record = cipher - .encrypt(&user()) - .context("users") - .using(&users_plan) + let back = record::decrypt(&cipher, written, &plan, None) + .unwrap() .await .unwrap(); - assert_eq!( - plan_error(cipher.open(record).using(&users_plan).await), - PlanError::NoContext - ); + assert_eq!(back, records()); - let age_plan = Plan::value::<u32>().with(Equality).build().unwrap(); - assert_eq!(age_plan.label(), None); - assert_eq!( - plan_error(cipher.encrypt(&34u32).using(&age_plan).await), - PlanError::NoContext - ); - assert_eq!( - plan_error(cipher.query(op::Eq, &34u32).on(&age_plan).await), - PlanError::NoContext - ); - let age = cipher - .encrypt(&34u32) - .context("users/age") - .using(&age_plan) + let written = record::encrypt(&keyset, records(), &plan) + .unwrap() .await .unwrap(); - assert_eq!( - plan_error(cipher.open(age).using(&age_plan).await), - PlanError::NoContext - ); - assert_eq!( - generates.load(Ordering::SeqCst), - 2, - "only the two good writes" + let refused = record::decrypt(&cipher, written, &plan, Some(label("tenants/acme"))) + .unwrap() + .await; + assert!( + matches!(refused, Err(Error::ContextMismatch { .. })), + "{refused:?}" ); - assert_eq!(retrieves.load(Ordering::SeqCst), 0); - assert!(PlanError::NoContext - .to_string() - .starts_with("the plan has no context")); } #[tokio::test] -async fn a_context_given_twice_is_refused_at_build() { - let both = Plan::context("users") - .fields::<User, FakeDataKeySource>() - .context("people") - .encrypt(("age", age)) - .build(); - assert_eq!(plan_error(both), two("the plan", "the plan")); - - let with_field = Plan::context("users") - .fields::<TenantRecord, FakeDataKeySource>() - .context_field(("tenant", tenant)) - .build(); - assert_eq!(plan_error(with_field), two("the plan", "a context field")); - - let field_first = Plan::fields::<TenantRecord, FakeDataKeySource>() - .context_field(("tenant", tenant)) - .context("users") - .build(); - assert_eq!(plan_error(field_first), two("a context field", "the plan")); +async fn opening_checks_the_context_field_against_the_expected_context() { + let (cipher, _, retrieves) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let plan = records_plan(); + let sealed = || async { + record::encrypt(&keyset, tenant_record("tenants/acme"), &plan) + .unwrap() + .await + .unwrap() + }; - let value = Plan::value::<u32>() - .context("a") - .context("b") - .with(Equality) - .build(); - assert_eq!(plan_error(value), two("the plan", "the plan")); + let back = record::decrypt(&cipher, sealed().await, &plan, Some(label("tenants/acme"))) + .unwrap() + .await + .unwrap(); + assert_eq!(back, tenant_record("tenants/acme")); + let before = retrieves.load(Ordering::SeqCst); + let refused = record::decrypt( + &cipher, + sealed().await, + &plan, + Some(label("tenants/globex")), + ) + .unwrap() + .await; + assert!( + matches!(refused, Err(Error::ContextMismatch { .. })), + "{refused:?}" + ); assert_eq!( - two("the plan", "the call").to_string(), - "the plan's context is given twice: by the plan and by the call" + retrieves.load(Ordering::SeqCst), + before, + "refused before any key request" ); + + // A context field changed in storage opens nothing: every field was + // sealed under the original. + let moved = with_stored_tenant(sealed().await, Some("tenants/globex")); + let refused = record::decrypt(&cipher, moved, &plan, None).unwrap().await; + assert!(matches!(refused, Err(Error::Aead)), "{refused:?}"); } +// --- A context that is missing ----------------------------------------------- + +/// A plan that reads its context from the record has none for a query: a +/// query holds no record. The query is refused until the call names the +/// context, by the check that needs no key and by the derivation alike. #[tokio::test] -async fn a_context_given_twice_is_refused_at_the_call_before_any_key_request() { +async fn a_query_that_names_no_context_is_refused_before_any_key_request() { let (cipher, generates, retrieves) = counting_cipher().await; - let users_plan: Plan<User, _> = Plan::context("users") - .fields() - .encrypt_index(("email", email), Equality) - .build() - .unwrap(); - assert_eq!( - plan_error( - cipher - .encrypt(&user()) - .context("users") - .using(&users_plan) - .await - ), - two("the plan", "the call") - ); - assert_eq!( - plan_error( - cipher - .query(op::Eq, "bob@example.com") - .context("users") - .on(&users_plan.field("email").unwrap()) - .await - ), - two("the plan", "the call") - ); - let record = cipher.encrypt(&user()).using(&users_plan).await.unwrap(); - assert_eq!( - plan_error( - cipher - .open(record) - .context("users") - .using(&users_plan) - .await - ), - two("the plan", "the call") - ); + let keyset = cipher.default_keyset(); + let plan = records_plan(); - let records_plan = records_plan(); - assert_eq!( - plan_error( - cipher - .encrypt(&tenant_record()) - .context("x") - .using(&records_plan) - .await - ), - two("a context field", "the call") + let checked = dynamic::check_query(&plan, "email", Op::Eq, "bob@example.com", &NoTargets); + assert!( + matches!(checked, Err(dynamic::Error::Plan { .. })), + "{checked:?}" ); - - let age_plan = Plan::value::<u32>() - .context("users/age") - .with(Equality) - .build() - .unwrap(); - assert_eq!( - plan_error( - cipher - .encrypt(&34u32) - .context("users/age") - .using(&age_plan) - .await - ), - two("the plan", "the call") + let derived = dynamic::query_term( + &keyset, + &plan, + "email", + Op::Eq, + "bob@example.com", + &NoTargets, ); - assert_eq!( - plan_error( - cipher - .query(op::Eq, &34u32) - .context("users/age") - .on(&age_plan) - .await - ), - two("the plan", "the call") + assert!( + matches!(derived.as_ref().err(), Some(dynamic::Error::Plan { .. })), + "a query on a context field plan names its context" ); - assert_eq!(generates.load(Ordering::SeqCst), 1, "only the good write"); + assert_eq!(generates.load(Ordering::SeqCst), 0); assert_eq!(retrieves.load(Ordering::SeqCst), 0); } +/// A scope the caller passes empty names nothing. The write, the query and +/// the read each refuse it before any key is requested. #[tokio::test] -async fn a_call_context_that_is_not_a_label_is_refused() { - let cipher = stack_cipher().await; - let users_plan = contextless_plan(); - let bad = |error: PlanError| matches!(error, PlanError::ContextLabel(_)); - assert!(bad(plan_error( - cipher - .encrypt(&user()) - .context("a//b") - .using(&users_plan) - .await - ))); - assert!(bad(plan_error( - cipher - .query(op::Eq, "bob@example.com") - .context("a//b") - .on(&users_plan.field("email").unwrap()) - .await - ))); - assert!(bad(plan_error( - cipher - .open(FieldValues::new()) - .context("a//b") - .using(&users_plan) - .await - ))); -} - -// --- The context, from a field of the value ----------------------------------- +async fn an_empty_scope_is_refused_before_any_key_request() { + let (cipher, generates, retrieves) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let plan = users_plan(); + let stored: StackCipherText = Planned::new(&plan, user()) + .encrypt_into_with_context(&keyset, Scope::new("acme")) + .await + .unwrap(); + generates.store(0, Ordering::SeqCst); -#[derive(Clone, Debug, PartialEq)] -struct TenantRecord { - tenant: String, - email: String, + let written = Planned::new(&plan, user()) + .encrypt_into_with_context::<StackCipherText, _>(&keyset, Scope::new("")) + .await; + assert_eq!(plan_error(written), PlanError::EmptyPart); + let queried = plan + .field("email") + .unwrap() + .eq(frame("bob@example.com")) + .unwrap() + .scope(Scope::new("")) + .encrypt(&keyset) + .await; + assert!(queried.is_err(), "{queried:?}"); + let opened = + ValueFrame::decrypt_from_with_context(Planned::new(&plan, stored), &keyset, Scope::new("")) + .await; + assert_eq!(plan_error(opened), PlanError::EmptyPart); + assert_eq!(generates.load(Ordering::SeqCst), 0); + assert_eq!(retrieves.load(Ordering::SeqCst), 0); } -fn tenant_record() -> TenantRecord { - TenantRecord { - tenant: "tenants/acme".into(), - email: "bob@example.com".into(), +// --- A context given twice ----------------------------------------------------- + +/// A plan names one source for its context. A declaration that names a +/// context field and gives a field a label of its own is refused when it +/// is parsed. +#[test] +fn a_context_given_twice_is_refused_when_the_plan_is_parsed() { + let refused = [ + ( + "a field under a label of its own beside the context field", + vec![ + ("context_field", frame("tenant")), + ("tenant", field(&["tenant"], &["passthrough"], "string")), + ("email", field(&["users", "email"], &["c", "eq"], "string")), + ], + ), + ( + "the context field itself under a label", + vec![ + ("context_field", frame("tenant")), + ( + "tenant", + field(&["users", "tenant"], &["passthrough"], "string"), + ), + ("email", field(&["email"], &["c", "eq"], "string")), + ], + ), + ]; + for (what, declaration) in refused { + let result = plan_of(declaration); + assert!( + matches!(result, Err(dynamic::Error::Plan { .. })), + "{what}: {result:?}" + ); } } -fn tenant(r: &TenantRecord) -> &String { - &r.tenant -} -fn record_email(r: &TenantRecord) -> &String { - &r.email -} +/// A context the reader expects is for a plan that reads its context from +/// the record. A plan with a context of its own already has one, so the +/// reader's is a second source. +#[tokio::test] +async fn a_context_given_twice_is_refused_at_the_call_before_any_key_request() { + let (cipher, generates, retrieves) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let plan = users_plan(); + let sealed = || async { + record::encrypt(&keyset, user(), &plan) + .unwrap() + .await + .unwrap() + }; -fn records_plan<K: 'static>() -> Plan<TenantRecord, K> { - Plan::fields() - .context_field(("tenant", tenant)) - .encrypt_index(("email", record_email), Equality) - .build() + let checked = record::check_record(sealed().await, &plan, Some(&label("users"))); + assert!( + matches!(checked, Err(dynamic::Error::Record { .. })), + "{checked:?}" + ); + let refused = record::decrypt(&cipher, sealed().await, &plan, Some(label("users"))) .unwrap() -} + .await; + assert_eq!(plan_error(refused), two("the plan", "the call")); -/// A context refused at the call (missing, given twice, not a label, or a -/// context field that disagrees with the one expected) is refused before -/// the keyset the chain names is loaded, for every plan kind and for a -/// `Vec` as for one value, and for a query whether the plan resolves its -/// index or the call names one with `.via(..)`. -#[tokio::test] -async fn a_context_refused_at_the_call_never_loads_the_keyset_it_names() { - let (cipher, loads) = loads_counting_cipher().await; - let users_plan = contextless_plan(); - let records_plan = records_plan(); - let age_plan = Plan::value::<u32>().with(Equality).build().unwrap(); - let pair_plan = Plan::value::<u32>() - .encrypt_into::<(StackCipherText, EqualityTerm)>() - .build() - .unwrap(); - let in_hand = |context: &'static str| { - let cipher = &cipher; - let pair_plan = &pair_plan; - async move { - cipher - .encrypt(&7u32) - .context(context) - .using(pair_plan) - .await - .unwrap() - } - }; - let record = cipher - .encrypt(&tenant_record()) - .using(&records_plan) - .await - .unwrap(); - let sealed_age = cipher - .encrypt(&7u32) - .context("ages") - .using(&age_plan) + // A batch of them is refused the same way. + let batch = record::encrypt(&keyset, frame(vec![user(), user()]), &plan) + .unwrap() .await .unwrap(); - let pair = in_hand("ages").await; - let pairs = vec![in_hand("ages").await]; + let refused = record::decrypt(&cipher, batch, &plan, Some(label("users"))) + .unwrap() + .await; + assert_eq!(plan_error(refused), two("the plan", "the call")); + + assert_eq!(generates.load(Ordering::SeqCst), 3, "only the good writes"); + assert_eq!(retrieves.load(Ordering::SeqCst), 0); assert_eq!( - loads.load(Ordering::SeqCst), - 0, - "the default keyset is in hand" + two("the plan", "the call").to_string(), + "the plan's context is given twice: by the plan and by the call" ); +} - let refused = cipher - .encrypt(&user()) - .using(&users_plan) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), PlanError::NoContext); - let refused = cipher - .encrypt(&vec![user()]) - .context("users//x") - .using(&users_plan) - .keyset("tenant") - .await; - assert!(matches!(plan_error(refused), PlanError::ContextLabel(_))); - let refused = cipher - .encrypt(&tenant_record()) - .context("users") - .using(&records_plan) - .keyset("tenant") - .await; - assert!(matches!( - plan_error(refused), - PlanError::TwoContextSources { .. } - )); - let bad_tenant = TenantRecord { - tenant: "tenants//acme".into(), - ..tenant_record() +// --- A context that is not a label --------------------------------------------- + +/// The value of a context field is read as a label when the plan runs. +/// Text that is not a plain label is refused, in the record a caller +/// writes and in the record a store returns, before any key is requested. +#[tokio::test] +async fn a_context_field_that_is_not_a_label_or_missing_is_refused() { + let (cipher, generates, retrieves) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let plan = records_plan(); + for not_a_label in ["acme corp/(x)", "tenants//acme", ""] { + let refused = record::encrypt(&keyset, tenant_record(not_a_label), &plan) + .expect("the record has the plan's shape") + .await; + assert!( + matches!(plan_error(refused), PlanError::ContextLabel(_)), + "{not_a_label:?}" + ); + } + assert_eq!(generates.load(Ordering::SeqCst), 0, "nothing was sealed"); + + let sealed = || async { + record::encrypt(&keyset, tenant_record("tenants/acme"), &plan) + .unwrap() + .await + .unwrap() }; - let refused = cipher - .encrypt(&bad_tenant) - .using(&records_plan) - .keyset("tenant") + let relabelled = with_stored_tenant(sealed().await, Some("a//b")); + let refused = record::decrypt(&cipher, relabelled, &plan, None) + .expect("the record has the plan's shape") .await; assert!(matches!(plan_error(refused), PlanError::ContextLabel(_))); - let refused = cipher - .open(record) - .context("tenants/globex") - .using(&records_plan) - .keyset("tenant") - .await; + // A stored context that is not text is not the plan's shape. + let CipherText::Map(mut fields) = with_stored_tenant(sealed().await, None) else { + panic!("a record is a map of fields") + }; + fields.push(( + "tenant".into(), + CipherText::Map(vec![( + "passthrough".into(), + CipherText::Passthrough(common::passthrough(7u32)), + )]), + )); + let refused = record::decrypt(&cipher, CipherText::Map(fields), &plan, None); assert!( - matches!(refused, Err(Error::ContextMismatch { .. })), - "{refused:?}" + matches!( + refused.as_ref().err(), + Some(dynamic::Error::Record { field: Some(field), .. }) if field == "tenant" + ), + "a context field of another type does not fit the plan" ); - - let refused = cipher - .encrypt(&7u32) - .using(&age_plan) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), PlanError::NoContext); - let refused = cipher - .encrypt(&vec![7u32]) - .using(&pair_plan) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), PlanError::NoContext); - let refused = cipher - .open(sealed_age) - .using(&age_plan) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), PlanError::NoContext); - let refused = cipher.open(pair).using(&pair_plan).keyset("tenant").await; - assert_eq!(plan_error(refused), PlanError::NoContext); - let refused = cipher.open(pairs).using(&pair_plan).keyset("tenant").await; - assert_eq!(plan_error(refused), PlanError::NoContext); - - // A query: the call context that is not a label is the refusal, whether - // the plan resolves the index or the call names it, and neither derives - // a term under it. - let refused = cipher - .query(op::Eq, &7u32) - .context("ages//x") - .on(&age_plan) - .keyset("tenant") - .await; - assert!(matches!(plan_error(refused), PlanError::ContextLabel(_))); - let refused = cipher - .query(op::Eq, &7u32) - .via(Equality) - .context("ages//x") - .on(&age_plan) - .keyset("tenant") - .await; - assert!(matches!(plan_error(refused), PlanError::ContextLabel(_))); - assert_eq!(loads.load(Ordering::SeqCst), 0, "no keyset was loaded"); + let missing = with_stored_tenant(sealed().await, None); + let refused = record::decrypt(&cipher, missing, &plan, None); + assert!( + matches!(refused.as_ref().err(), Some(dynamic::Error::Record { .. })), + "a record with no context field does not fit the plan" + ); + assert_eq!(generates.load(Ordering::SeqCst), 3, "the three good writes"); + assert_eq!(retrieves.load(Ordering::SeqCst), 0); } -/// A batch checks the context its call names once, apart from its items: -/// an empty slice or `Vec` runs no item, and is still refused a context -/// missing or given twice before the keyset the chain names is loaded, -/// for a fields plan and a one-value plan, run or opened. -#[tokio::test] -async fn an_empty_batch_has_its_context_refused_before_the_keyset_loads() { - let (cipher, loads) = loads_counting_cipher().await; - let users_plan = contextless_plan(); - let fixed_users = Plan::context("users") - .fields() - .encrypt_index(("email", email), Equality) - .encrypt(("age", age)) - .build() - .unwrap(); - let records_plan = records_plan(); - let age_plan = Plan::value::<u32>().with(Equality).build().unwrap(); - let fixed_age = Plan::context("ages") - .with::<u32, _>(Equality) - .build() - .unwrap(); - let no_users: Vec<User> = Vec::new(); - let no_records: Vec<TenantRecord> = Vec::new(); - let no_ages: Vec<u32> = Vec::new(); - - let refused = cipher - .encrypt(&no_users) - .using(&users_plan) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), PlanError::NoContext); - let refused = cipher - .encrypt(no_users.as_slice()) - .using(&users_plan) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), PlanError::NoContext); - let refused = cipher - .encrypt(&no_users) - .context("users") - .using(&fixed_users) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), two("the plan", "the call")); - let refused = cipher - .encrypt(no_users.as_slice()) - .context("users") - .using(&fixed_users) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), two("the plan", "the call")); - let refused = cipher - .encrypt(&no_records) - .context("tenants/acme") - .using(&records_plan) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), two("a context field", "the call")); - - let refused = cipher - .encrypt(&no_ages) - .using(&age_plan) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), PlanError::NoContext); - let refused = cipher - .encrypt(no_ages.as_slice()) - .context("ages") - .using(&fixed_age) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), two("the plan", "the call")); +/// The context a query names is a label too: one that is not plain makes +/// no plan, so no term is derived under it. +#[test] +fn a_query_context_that_is_not_a_label_is_refused() { + for not_a_label in ["a//b", "tenants/(acme)", "tenants/2fa"] { + let result = under(not_a_label); + assert!( + matches!(result, Err(dynamic::Error::Plan { .. })), + "{not_a_label:?}: {result:?}" + ); + } + assert!(under("tenants/acme").is_ok()); +} - let refused = cipher - .open(Vec::<FieldValues>::new()) - .using(&users_plan) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), PlanError::NoContext); - let refused = cipher - .open(Vec::<FieldValues>::new()) - .context("users") - .using(&fixed_users) - .keyset("tenant") - .await; - assert_eq!(plan_error(refused), two("the plan", "the call")); - assert_eq!(loads.load(Ordering::SeqCst), 0, "no keyset was loaded"); +// --- Queries under the context the caller names ---------------------------------- - // A context field's expected value is checked against each record, so - // an empty batch of them has nothing to refuse. - let opened = cipher - .open(Vec::<FieldValues>::new()) - .context("tenants/acme") - .using(&records_plan) - .await - .unwrap(); - assert!(opened.is_empty()); - let sealed = cipher - .encrypt(&no_users) - .context("users") - .using(&users_plan) +/// A query on a plan with a context field derives under the context the +/// call names: the stored term for the record's own tenant, and another +/// term for another tenant. No key is requested for either. +#[tokio::test] +async fn a_query_under_the_context_the_call_names_matches_the_stored_term() { + let (cipher, generates, retrieves) = counting_cipher().await; + let keyset = cipher.default_keyset(); + let plan = records_plan(); + let record = record::encrypt(&keyset, tenant_record("tenants/acme"), &plan) + .unwrap() .await .unwrap(); - assert!(sealed.is_empty()); + generates.store(0, Ordering::SeqCst); + + let acme = query(&keyset, &under("tenants/acme").unwrap()).await; + assert_eq!( + acme, + stored_term(&record), + "a query naming the same context matches" + ); + let globex = query(&keyset, &under("tenants/globex").unwrap()).await; + assert_ne!(globex, stored_term(&record)); + assert_eq!(generates.load(Ordering::SeqCst), 0); + assert_eq!(retrieves.load(Ordering::SeqCst), 0); } -/// Run or opened without a chain, an empty batch's description fails on -/// the same context a chain refuses, so the two never disagree. +/// A scope the caller passes extends every field of a plan whose context +/// is in its declaration. A query that names the same scope derives the +/// stored term, and the record opens only under that scope. #[tokio::test] -async fn an_empty_batch_description_fails_on_the_context_a_chain_refuses() { - let cipher = stack_cipher().await; +async fn a_callers_scope_extends_every_field_and_a_query_names_it() { + let (cipher, sent) = recording_cipher().await; let keyset = cipher.default_keyset(); - let users_plan = contextless_plan(); - let fixed_users = Plan::context("users") - .fields() - .encrypt(("age", age)) - .build() - .unwrap(); - let age_plan = Plan::value::<u32>().with(Equality).build().unwrap(); - let users = || Some(Label::parse("users").unwrap()); - let no_users: Vec<User> = Vec::new(); - - let refused = Runs::<Vec<User>, _>::pending( - &users_plan, - &keyset, - &no_users, - None, - DeclaredContext::default(), - ) - .await; - assert_eq!(plan_error(refused), PlanError::NoContext); - let refused = Runs::<[User], _>::pending( - &fixed_users, - &keyset, - &no_users, - users(), - DeclaredContext::default(), - ) - .await; - assert_eq!(plan_error(refused), two("the plan", "the call")); - let refused = Runs::<Vec<u32>, _>::pending( - &age_plan, - &keyset, - &Vec::new(), - None, - DeclaredContext::default(), - ) - .await; - assert_eq!(plan_error(refused), PlanError::NoContext); - let run = Runs::<Vec<User>, _>::pending( - &users_plan, - &keyset, - &no_users, - users(), - DeclaredContext::default(), - ) - .await; - assert!(run.unwrap().is_empty()); - - let opening = Opens::<Vec<FieldValues>, _>::decryption( - &users_plan, - Vec::new(), - None, - DeclaredContext::default(), - ); - assert_eq!( - plan_error(keyset.run_decryption(opening).await), - PlanError::NoContext - ); - let opening = Opens::<Vec<FieldValues>, _>::decryption( - &fixed_users, - Vec::new(), - users(), - DeclaredContext::default(), - ); - assert_eq!( - plan_error(keyset.run_decryption(opening).await), - two("the plan", "the call") - ); - let opening = Opens::<Vec<FieldValues>, _>::decryption( - &fixed_users, - Vec::new(), - None, - DeclaredContext::default(), - ); - assert!(keyset.run_decryption(opening).await.unwrap().is_empty()); -} - -/// `.extend(parts)` reaches every field of a plan whose context is read -/// from the value, and the same parts are needed to open it. -#[tokio::test] -async fn an_extension_reaches_every_field_of_a_context_field_plan() { - let (cipher, sent) = recording_cipher().await; - let records_plan = records_plan(); - let record = cipher - .encrypt(&tenant_record()) - .using(&records_plan) - .extend(7u64) - .await - .unwrap(); + let plan = users_plan(); + let write = || { + Planned::new(&plan, user()) + .encrypt_into_with_context::<StackCipherText, _>(&keyset, Scope::new("acme")) + }; + let record = write().await.unwrap(); assert_eq!( sent.lock().unwrap().generated(), - ["(tenants/acme/email)/7u64"] + ["(users/email)/acme", "(users/age)/acme"], + "every field under its own label, then the scope" ); - let back = cipher - .open(record) - .using(&records_plan) - .extend(7u64) - .await - .unwrap(); - assert_eq!(back.get::<String>("email"), Some(&tenant_record().email)); - let record = cipher - .encrypt(&tenant_record()) - .using(&records_plan) - .extend(7u64) - .await - .unwrap(); - assert!(matches!( - cipher.open(record).using(&records_plan).await, - Err(Error::Aead) - )); -} - -/// A `Vec` through a context-field plan seals each record under its own -/// tenant, and a context the caller expects is checked against every -/// record of it. -#[tokio::test] -async fn a_vec_through_a_context_field_plan_seals_each_record_under_its_own_tenant() { - let (cipher, sent) = recording_cipher().await; - let records_plan = records_plan(); - let globex = TenantRecord { - tenant: "tenants/globex".into(), - ..tenant_record() + let by_email = |scope: &'static str| { + plan.field("email") + .unwrap() + .eq(frame("bob@example.com")) + .unwrap() + .scope(Scope::new(scope)) + .encrypt(&keyset) }; - let records = vec![tenant_record(), globex]; - let written = cipher.encrypt(&records).using(&records_plan).await.unwrap(); - assert_eq!( - sent.lock().unwrap().generated(), - ["tenants/acme/email", "tenants/globex/email"], - ); - let back = cipher.open(written).using(&records_plan).await.unwrap(); + let named = by_email("acme").await.unwrap(); assert_eq!( - back[0].get::<String>("tenant").map(String::as_str), - Some("tenants/acme") + named.operand(), + &Operand::One(stored_term(&record)), + "a query naming the same scope matches" ); - assert_eq!( - back[1].get::<String>("tenant").map(String::as_str), - Some("tenants/globex") + let elsewhere = by_email("globex").await.unwrap(); + assert_ne!(elsewhere.operand(), &Operand::One(stored_term(&record))); + assert_ne!( + query(&keyset, &plan).await, + stored_term(&record), + "and so does a query that names none" ); - assert_eq!(back[1].get::<String>("email"), Some(&tenant_record().email)); - let written = cipher.encrypt(&records).using(&records_plan).await.unwrap(); - let refused = cipher - .open(written) - .context("tenants/acme") - .using(&records_plan) - .await; + let back = ValueFrame::decrypt_from_with_context( + Planned::new(&plan, record), + &keyset, + Scope::new("acme"), + ) + .await + .unwrap(); + assert_eq!(back, user()); + let unscoped = + ValueFrame::decrypt_from(Planned::new(&plan, write().await.unwrap()), &keyset).await; assert!( - matches!(refused, Err(Error::ContextMismatch { .. })), - "{refused:?}" + matches!(unscoped, Err(Error::Aead)), + "the fake key source ignores descriptors, so a missing scope reaches the AEAD" ); } +/// A scope reaches every field of a plan whose context is read from the +/// record, and the same scope is needed to open it. #[tokio::test] -async fn a_context_field_is_the_context_of_every_other_field() { +async fn a_scope_reaches_every_field_of_a_context_field_plan() { let (cipher, sent) = recording_cipher().await; - let records_plan: Plan<TenantRecord, _> = Plan::fields() - .context_field(("tenant", tenant)) - .encrypt_index(("email", record_email), Equality) - .build() - .unwrap(); - assert_eq!(records_plan.context_field(), Some("tenant")); - assert_eq!(records_plan.label(), None); - assert_eq!( - records_plan.field("tenant").unwrap().kind(), - FieldKind::ContextField - ); - - let mut record = cipher - .encrypt(&tenant_record()) - .using(&records_plan) - .await - .unwrap(); + let keyset = cipher.default_keyset(); + let plan = records_plan(); + let write = || { + Planned::new(&plan, tenant_record("tenants/acme")) + .encrypt_into_with_context::<StackCipherText, _>(&keyset, Scope::new(7u64)) + }; + let record = write().await.unwrap(); assert_eq!( sent.lock().unwrap().generated(), - ["tenants/acme/email"], - "the field is sealed under <context field>/<identity>" - ); - assert_eq!( - record.get::<String>("tenant"), - Some(&tenant_record().tenant), - "the context field is carried as it is" + ["(tenants/acme/email)/7u64"] ); + let back = ValueFrame::decrypt_from_with_context( + Planned::new(&plan, record), + &keyset, + Scope::new(7u64), + ) + .await + .unwrap(); + assert_eq!(back, tenant_record("tenants/acme")); - // The same bytes as a plan given that context when it is built. - let fixed: Plan<TenantRecord, _> = Plan::context("tenants/acme") - .fields() - .passthrough(("tenant", tenant)) - .encrypt_index(("email", record_email), Equality) - .build() - .unwrap(); - let mut fixed_record = cipher - .encrypt(&tenant_record()) - .using(&fixed) - .await - .unwrap(); - let stored: Encrypted<EqualityTerm> = record.take("email").unwrap(); - let fixed_email: Encrypted<EqualityTerm> = fixed_record.take("email").unwrap(); - assert_eq!(stored.terms, fixed_email.terms); - - // A query names the context field's value. - let query_value = cipher - .query(op::Eq, "bob@example.com") - .context("tenants/acme") - .on(&records_plan.field("email").unwrap()) - .await - .unwrap(); - assert_eq!(query_value, stored.terms); - - // Opens with no expectation, under the stored context. - record.insert("email", stored); - let back = cipher.open(record).using(&records_plan).await.unwrap(); - assert_eq!(back.get::<String>("email"), Some(&tenant_record().email)); - assert_eq!(back.get::<String>("tenant"), Some(&tenant_record().tenant)); + let unscoped = + ValueFrame::decrypt_from(Planned::new(&plan, write().await.unwrap()), &keyset).await; + assert!(matches!(unscoped, Err(Error::Aead)), "{unscoped:?}"); } +/// A field whose label ends in another segment than its name is keyed +/// under that segment: the write, the query and the read all use the +/// label, never the field's name. So a name that is not a plain segment, +/// such as a column that starts with a digit, is still a field. #[tokio::test] -async fn opening_checks_the_context_field_against_the_expected_context() { - let (cipher, _, retrieves) = counting_cipher().await; - let records_plan = records_plan(); - - let record = cipher - .encrypt(&tenant_record()) - .using(&records_plan) - .await - .unwrap(); - let back = cipher - .open(record) - .context("tenants/acme") - .using(&records_plan) - .await - .unwrap(); - assert_eq!(back.get::<String>("email"), Some(&tenant_record().email)); - let before = retrieves.load(Ordering::SeqCst); - - let record = cipher - .encrypt(&tenant_record()) - .using(&records_plan) +async fn a_pinned_identity_keys_the_field_for_the_write_the_query_and_the_read() { + let (cipher, sent) = recording_cipher().await; + let keyset = cipher.default_keyset(); + let plan = plan_of(vec![( + "2fa_secret", + field(&["users", "totp_secret"], &["c", "eq"], "string"), + )]) + .unwrap(); + assert_eq!(plan.fields()[0].identity(), "totp_secret"); + let source = || object(vec![("2fa_secret", frame("s3cret"))]); + let record = record::encrypt(&keyset, source(), &plan) + .unwrap() .await .unwrap(); - let refused = cipher - .open(record) - .context("tenants/globex") - .using(&records_plan) - .await; - assert!( - matches!(refused, Err(Error::ContextMismatch { .. })), - "{refused:?}" - ); - assert_eq!( - retrieves.load(Ordering::SeqCst), - before, - "refused before any key request" - ); + assert_eq!(sent.lock().unwrap().generated(), ["users/totp_secret"]); - // A context field changed in storage opens nothing: every field was - // sealed under the original. - let mut moved = cipher - .encrypt(&tenant_record()) - .using(&records_plan) + let term = dynamic::query_term(&keyset, &plan, "2fa_secret", Op::Eq, "s3cret", &NoTargets) + .unwrap() .await .unwrap(); - moved.insert("tenant", String::from("tenants/globex")); - let refused = cipher.open(moved).using(&records_plan).await; - assert!(matches!(refused, Err(Error::Aead)), "{refused:?}"); -} - -#[tokio::test] -async fn a_context_field_that_is_not_a_label_or_missing_is_refused() { - let (cipher, generates, retrieves) = counting_cipher().await; - let records_plan = records_plan(); - let bad = TenantRecord { - tenant: "acme corp/(x)".into(), - email: "bob@example.com".into(), + let (_, CipherText::Passthrough(stored)) = &outputs(&record, "2fa_secret")[1] else { + panic!("the term follows the ciphertext") }; - assert!(matches!( - plan_error(cipher.encrypt(&bad).using(&records_plan).await), - PlanError::ContextLabel(_) - )); - - let mut record = cipher - .encrypt(&tenant_record()) - .using(&records_plan) - .await - .unwrap(); - record.insert("tenant", 7u32); assert_eq!( - plan_error(cipher.open(record).using(&records_plan).await), - PlanError::FieldType { - field: "tenant".into(), - expected: "alloc::string::String" - } + term.operand(), + &Operand::One( + vitaminc_aead_frame::serde::from_slice::<&[u8]>(&stored.0) + .unwrap() + .to_vec() + ) ); - let mut record = cipher - .encrypt(&tenant_record()) - .using(&records_plan) - .await - .unwrap(); - record.insert("tenant", String::from("a//b")); - assert!(matches!( - plan_error(cipher.open(record).using(&records_plan).await), - PlanError::ContextLabel(_) - )); - let mut record = cipher - .encrypt(&tenant_record()) - .using(&records_plan) + let back = record::decrypt(&cipher, record, &plan, None) + .unwrap() .await .unwrap(); - assert!(record.remove("tenant")); - assert_eq!( - plan_error(cipher.open(record).using(&records_plan).await), - PlanError::NotInValue { - field: "tenant".into() - } - ); - assert_eq!(generates.load(Ordering::SeqCst), 3, "the three good writes"); - assert_eq!(retrieves.load(Ordering::SeqCst), 0); -} - -// --- encrypt_into: a typed field is the data verbs' bytes ---------------------- - -/// A record of one field, read by a picker. -struct One<F> { - v: F, -} - -/// `encrypt_into::<Encrypted<Terms>>` beside `encrypt_index(indexes)`: the -/// same descriptors, the same terms, and each record opens through the -/// other plan. -async fn same_field_bytes<F, Terms, X>(value: F, indexes: X) -where - F: Encrypt + Decrypt + Clone + Send + Sync + PartialEq + Debug + 'static, - Terms: TermSet<F> + PartialEq + Debug + Send, - X: Indexes<F, Terms = Terms> + Clone + Send + Sync + 'static, -{ - let (cipher, sent) = recording_cipher().await; - let typed: Plan<One<F>, _> = Plan::context("t") - .fields() - .encrypt_into::<Encrypted<Terms>, _>(pick("v", |o: &One<F>| &o.v)) - .build() - .unwrap(); - let data: Plan<One<F>, _> = Plan::context("t") - .fields() - .encrypt_index(pick("v", |o: &One<F>| &o.v), indexes.clone()) - .build() - .unwrap(); - let field = typed.field("v").unwrap(); - assert_eq!(field.kind(), FieldKind::EncryptInto); - assert_eq!( - field.indexes(), - data.field("v").unwrap().indexes(), - "the target declares the indexes the data verb names" - ); - assert_eq!(field.indexes(), indexes.specs()); - - let one = One { v: value.clone() }; - let mut typed_record = cipher.encrypt(&one).using(&typed).await.unwrap(); - let mut data_record = cipher.encrypt(&one).using(&data).await.unwrap(); - assert_eq!(sent.lock().unwrap().generated(), ["t/v", "t/v"]); - - let typed_out: Encrypted<Terms> = typed_record.take("v").unwrap(); - let data_out: Encrypted<Terms> = data_record.take("v").unwrap(); - assert_eq!(typed_out.terms, data_out.terms, "the same terms"); - - typed_record.insert("v", typed_out); - data_record.insert("v", data_out); - let back = cipher.open(typed_record).using(&data).await.unwrap(); - assert_eq!( - back.get::<F>("v"), - Some(&value), - "the data plan opens the typed record" - ); - let back = cipher.open(data_record).using(&typed).await.unwrap(); - assert_eq!( - back.get::<F>("v"), - Some(&value), - "the typed plan opens the data record" - ); -} - -/// A configuration other than the default. -struct Words; -impl MatchConfig for Words { - fn options() -> MatchOptions { - MatchOptions { - tokenizer: Tokenizer::Standard, - downcase: false, - short_words: false, - k: 4, - m: 512, - } - } -} - -type M = MatchTerms; -type W = MatchTerms<Words>; - -#[tokio::test] -async fn a_typed_text_field_is_the_data_verbs_bytes_for_every_index_set() { - let text = || String::from("Hello World"); - let ore = Ore; - let ope = Ope; - let eq = Equality; - let m = Match::default; - same_field_bytes::<_, EqualityTerm, _>(text(), eq).await; - same_field_bytes::<_, M, _>(text(), m()).await; - same_field_bytes::<_, W, _>(text(), Match::<Words>::new()).await; - same_field_bytes::<_, OreTerm<String>, _>(text(), ore).await; - same_field_bytes::<_, OpeTerm<String>, _>(text(), ope).await; - same_field_bytes::<_, (EqualityTerm, M), _>(text(), (eq, m())).await; - same_field_bytes::<_, (EqualityTerm, OreTerm<String>), _>(text(), (eq, ore)).await; - same_field_bytes::<_, (EqualityTerm, OpeTerm<String>), _>(text(), (eq, ope)).await; - same_field_bytes::<_, (M, OreTerm<String>), _>(text(), (m(), ore)).await; - same_field_bytes::<_, (M, OpeTerm<String>), _>(text(), (m(), ope)).await; - same_field_bytes::<_, (EqualityTerm, M, OreTerm<String>), _>(text(), (eq, m(), ore)).await; - same_field_bytes::<_, (EqualityTerm, M, OpeTerm<String>), _>(text(), (eq, m(), ope)).await; -} - -/// Five indexes compile (there are four index kinds, so the fifth is a -/// second match index under other options), but a field holds one term per -/// kind, so both spellings refuse the set when the plan is built. -#[test] -fn a_five_index_field_is_refused_by_both_spellings() { - let refused = || PlanError::DuplicateIndex { - at: "v".into(), - index: "match", - }; - let typed = Plan::context("t") - .fields::<One<String>, FakeDataKeySource>() - .encrypt_into::<Encrypted<(EqualityTerm, M, OreTerm<String>, OpeTerm<String>, W)>, _>(pick( - "v", - |o: &One<String>| &o.v, - )) - .build(); - assert_eq!(plan_error(typed), refused()); - let data = Plan::context("t") - .fields::<One<String>, FakeDataKeySource>() - .encrypt_index( - pick("v", |o: &One<String>| &o.v), - (Equality, Match, Ore, Ope, Match::<Words>::new()), - ) - .build(); - assert_eq!(plan_error(data), refused()); -} - -#[tokio::test] -async fn a_typed_integer_field_is_the_data_verbs_bytes_for_every_index_set() { - same_field_bytes::<_, EqualityTerm, _>(7u32, Equality).await; - same_field_bytes::<_, OreTerm<u32>, _>(7u32, Ore).await; - same_field_bytes::<_, OpeTerm<u32>, _>(7u32, Ope).await; - same_field_bytes::<_, (EqualityTerm, OreTerm<u32>), _>(7u32, (Equality, Ore)).await; - same_field_bytes::<_, (EqualityTerm, OpeTerm<u32>), _>(7u32, (Equality, Ope)).await; - same_field_bytes::<_, (OreTerm<u32>, EqualityTerm), _>(7u32, (Ore, Equality)).await; -} - -type EmailOut = Encrypted<(EqualityTerm, MatchTerms)>; - -#[tokio::test] -async fn a_typed_field_answers_the_queries_its_target_declares_and_no_other() { - let cipher = stack_cipher().await; - let users_plan: Plan<User, _> = Plan::context("users") - .fields() - .encrypt_into::<EmailOut, _>(("email", email)) - .encrypt_into::<Encrypted<OreTerm<u32>>, _>(("age", age)) - .build() - .unwrap(); - let mut record = cipher.encrypt(&user()).using(&users_plan).await.unwrap(); - let stored: EmailOut = record.take("email").unwrap(); + assert_eq!(back, source()); - let email_plan = users_plan.field("email").unwrap(); - let query_value = cipher - .query(op::Eq, "bob@example.com") - .on(&email_plan) - .await - .unwrap(); - assert_eq!(query_value, stored.terms.0); - let matched = cipher - .query(op::Matches, "bob@example.com") - .on(&email_plan) + // Under a context field the identity is the whole of the field's + // label, and the record's context comes before it. + let readings = plan_of(vec![ + ("context_field", frame("tenant")), + ("tenant", field(&["tenant"], &["passthrough"], "string")), + ("0", field(&["value"], &["c", "eq"], "uint32")), + ]) + .unwrap(); + sent.lock().unwrap().generate.clear(); + let reading = || object(vec![("tenant", frame("reading")), ("0", frame(21u32))]); + let record = record::encrypt(&keyset, reading(), &readings) + .unwrap() .await .unwrap(); - assert_eq!(matched.positions(), stored.terms.1.positions()); - assert_eq!( - plan_error( - cipher - .query(op::Lt, &34u32) - .via(Ope) - .on(&users_plan.field("age").unwrap()) - .await - ), - PlanError::IndexNotDeclared { - field: "age".into(), - index: "ope" - } - ); - assert_eq!( - plan_error( - cipher - .query(op::Eq, &34u32) - .on(&users_plan.field("age").unwrap()) - .await - ), - PlanError::IndexNotDeclared { - field: "age".into(), - index: "eq" - } - ); -} - -/// A target that opens nothing: its field does not come back from `open`, -/// like an index-only field, and the stored record need not carry it. -#[tokio::test] -async fn a_typed_field_of_terms_alone_does_not_come_back() { - let cipher = stack_cipher().await; - let users_plan: Plan<User, _> = Plan::context("users") - .fields() - .encrypt_into::<EqualityTerm, _>(("email", email)) - .encrypt_into::<StackCipherText, _>(("age", age)) - .build() - .unwrap(); - let mut record = cipher.encrypt(&user()).using(&users_plan).await.unwrap(); - let term: EqualityTerm = record.take("email").unwrap(); - let query_value = cipher - .query(op::Eq, "bob@example.com") - .on(&users_plan.field("email").unwrap()) + assert_eq!(sent.lock().unwrap().generated(), ["reading/value"]); + // A query names the context, and the field's label under it ends in + // the identity. + let named = plan_of(vec![( + "0", + field(&["reading", "value"], &["c", "eq"], "uint32"), + )]) + .unwrap(); + let term = dynamic::query_term(&keyset, &named, "0", Op::Eq, 21u32, &NoTargets) + .unwrap() .await .unwrap(); - assert_eq!(query_value, term); - let back = cipher.open(record).using(&users_plan).await.unwrap(); - assert_eq!(back.names().collect::<Vec<_>>(), ["age"]); - assert_eq!(back.get::<u32>("age"), Some(&34)); -} - -/// A target that says it holds ciphertext and then opens nothing. -#[allow(dead_code)] // never read: it only says it holds ciphertext -struct Lying(StackCipherText); -impl EncryptFrom<u32> for Lying { - type Context = stack_encrypt::target::AeadContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, u32, Self, K, Self::Context> { - <StackCipherText as EncryptFrom<u32>>::encryption().map(Lying) - } -} -impl Decryptable for Lying { - const DECRYPTABLE: bool = true; -} -impl<P, Ctx> DecryptField<P, Ctx> for Lying { - fn decryption_field<K: 'static>(self, _: Ctx) -> Option<Decryption<P, K>> { - None - } -} - -#[tokio::test] -async fn a_typed_field_that_opens_nothing_is_not_opened() { - let cipher = stack_cipher().await; - let plan: Plan<User, _> = Plan::context("users") - .fields() - .encrypt_into::<Lying, _>(("age", age)) - .build() - .unwrap(); - let record = cipher.encrypt(&user()).using(&plan).await.unwrap(); - let result = cipher.open(record).using(&plan).await; - assert!(matches!(result, Err(Error::NotOpened)), "{result:?}"); -} - -#[tokio::test] -async fn a_field_is_a_target_or_data_verbs_never_both() { - let target_first = Plan::context("users") - .fields::<User, FakeDataKeySource>() - .encrypt_into::<EmailOut, _>(("email", email)) - .encrypt(("email", email)) - .build(); - let verbs_first = Plan::context("users") - .fields::<User, FakeDataKeySource>() - .passthrough(("email", email)) - .encrypt_into::<EmailOut, _>(("email", email)) - .build(); - for refused in [target_first, verbs_first] { - assert_eq!( - plan_error(refused), - PlanError::TargetWithVerbs { - field: "email".into() - } - ); - } - let twice = Plan::context("users") - .fields::<User, FakeDataKeySource>() - .encrypt_into::<EmailOut, _>(("email", email)) - .encrypt_into::<EmailOut, _>(("email", email)) - .build(); - assert_eq!( - plan_error(twice), - PlanError::DuplicateField { - field: "email".into() - }, - "two targets on one field is a field named twice" - ); - let index_twice = Plan::context("users") - .fields::<User, FakeDataKeySource>() - .encrypt_into::<Encrypted<(EqualityTerm, EqualityTerm)>, _>(("email", email)) - .build(); + let (_, CipherText::Passthrough(stored)) = &outputs(&record, "0")[1] else { + panic!("the term follows the ciphertext") + }; assert_eq!( - plan_error(index_twice), - PlanError::DuplicateIndex { - at: "email".into(), - index: "eq" - } - ); -} - -// --- encrypt_into, one value: the `plaintext = T` record ----------------------- - -#[derive(stack_encrypt::EncryptFrom, stack_encrypt::DecryptInto)] -#[stash(plaintext = String)] -struct DerivedEmail { - c: StackCipherText, - hm: EqualityTerm, -} - -#[tokio::test] -async fn a_one_value_typed_plan_is_the_plaintext_records_bytes() { - let (cipher, sent) = recording_cipher().await; - let keyset = cipher.default_keyset(); - let email_plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<(StackCipherText, EqualityTerm)>() - .build() - .unwrap(); - let email = String::from("bob@example.com"); - - let (c, hm) = cipher.encrypt(&email).using(&email_plan).await.unwrap(); - let derived: DerivedEmail = keyset - .encrypt_as( - &email, - CallerContext::from(nonempty!("users").with("email")), + term.operand(), + &Operand::One( + vitaminc_aead_frame::serde::from_slice::<&[u8]>(&stored.0) + .unwrap() + .to_vec() ) - .await - .unwrap(); - assert_eq!( - sent.lock().unwrap().generated(), - ["users/email", "users/email"] ); - assert_eq!(hm, derived.hm, "the same equality term"); - - // Each ciphertext opens through the other's reader. - let opened: String = cipher - .decrypt_as( - DerivedEmail { c, hm: hm.clone() }, - CallerContext::from(nonempty!("users").with("email")), - ) - .await - .unwrap(); - assert_eq!(opened, email); - let back = cipher - .open((derived.c, derived.hm)) - .using(&email_plan) - .await - .unwrap(); - assert_eq!(back, email); -} - -#[tokio::test] -async fn a_one_value_typed_plan_answers_its_targets_queries_and_no_other() { - let cipher = stack_cipher().await; - let email_plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<(StackCipherText, EqualityTerm)>() - .build() - .unwrap(); - let email = String::from("bob@example.com"); - let (_, hm) = cipher.encrypt(&email).using(&email_plan).await.unwrap(); - let query_value = cipher - .query(op::Eq, "bob@example.com") - .on(&email_plan) + let back = record::decrypt(&cipher, record, &readings, Some(label("reading"))) + .unwrap() .await .unwrap(); - assert_eq!(query_value, hm); - assert_eq!( - plan_error( - cipher - .query(op::Matches, "bob") - .via(Match) - .on(&email_plan) - .await - ), - PlanError::IndexNotDeclared { - field: "the value".into(), - index: "match" - } - ); -} - -/// A context can carry customer data, so a one-value plan's refusal names -/// `the value`, never the label the call gave: not in the message, not in -/// the payload a binding hands its callers. -#[tokio::test] -async fn a_one_value_plan_quotes_no_call_context_in_its_refusals() { - let cipher = stack_cipher().await; - let plan = Plan::value::<String>() - .encrypt_into::<(StackCipherText, EqualityTerm)>() - .build() - .unwrap(); - let marker = "tenants/marker-tenant"; - - let error = plan_error( - cipher - .query(op::Matches, "bob") - .context(marker) - .via(Match) - .on(&plan) - .await, - ); - assert_eq!( - error, - PlanError::IndexNotDeclared { - field: "the value".into(), - index: "match" - } - ); - let message = error.to_string(); - let payload = serde_json::Value::Object(error.payload()).to_string(); - for shown in [&message, &payload] { - assert!(!shown.contains("marker-tenant"), "{shown}"); - } -} - -#[tokio::test] -async fn a_one_value_plan_names_the_value_in_its_errors_never_its_context() { - let refused = Plan::value::<u32>().with((Equality, Equality)).build(); - assert_eq!( - plan_error(refused), - PlanError::DuplicateIndex { - at: "the value".into(), - index: "eq" - } - ); - let refused = Plan::value::<u32>() - .context("a") - .with((Equality, Equality)) - .build(); - assert_eq!( - plan_error(refused), - PlanError::DuplicateIndex { - at: "the value".into(), - index: "eq" - } - ); -} - -/// A target whose context is neither a `CallerContext` nor converted from -/// one: a `NonEmpty<u64>`, as a `context_type = NonEmpty<u64>` record has. -/// It declares its one index as many times as it is told to. -struct Tenanted<const N: usize>(#[allow(dead_code)] EqualityTerm); -impl<const N: usize> EncryptFrom<String> for Tenanted<N> { - type Context = NonEmpty<u64>; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> { - <EqualityTerm as EncryptFrom<String>>::encryption() - .accepting::<NonEmpty<u64>>() - .map(Tenanted) - } - fn indexes() -> Vec<IndexSpec> { - vec![IndexSpec::Equality; N] - } + assert_eq!(back, reading()); } +/// A context is written as a label, or as text that parses as one: a +/// literal, a `String`, or a borrowed `String` such as a configuration +/// value. Text that is not a plain label is refused. #[test] -fn a_one_value_plan_builds_over_a_target_of_any_context() { - // Building asks only for the target's indexes, never for a - // `CallerContext` it could not take. - let built = Plan::value::<String>() - .encrypt_into::<Tenanted<1>>() - .build(); - assert!(built.is_ok(), "{:?}", built.err()); - // And those indexes are checked. - let refused = Plan::value::<String>() - .encrypt_into::<Tenanted<2>>() - .build(); - assert_eq!( - plan_error(refused), - PlanError::DuplicateIndex { - at: "the value".into(), - index: "eq" - } - ); -} - -// --- Collections through a one-value plan ------------------------------------- - +fn a_context_is_a_label_or_text_that_parses_as_one() { + let users_age = label("users/age"); + let held = String::from("users/age"); + assert_eq!("users/age".into_label().unwrap(), users_age); + assert_eq!(held.clone().into_label().unwrap(), users_age); + assert_eq!((&held).into_label().unwrap(), users_age); + assert_eq!((&users_age).into_label().unwrap(), users_age); + assert_eq!(users_age.clone().into_label().unwrap(), users_age); + let not_plain = String::from("users//age"); + assert!((&not_plain).into_label().is_err()); + assert!("a b/".into_label().is_err()); +} + +// --- An empty batch ------------------------------------------------------------ + +/// An empty batch runs no record. The scope its call names is checked +/// once for the call, apart from the records, so a scope passed empty is +/// refused for an empty batch too, before any key is requested. A context +/// the reader expects is compared with each record, so an empty batch of a +/// plan with a context field has none to refuse. #[tokio::test] -async fn a_vec_written_through_a_one_value_plan_opens_through_it_in_one_request() { +async fn an_empty_batch_has_its_context_checked_before_any_key_request() { let (cipher, generates, retrieves) = counting_cipher().await; - let ages = vec![30u32, 31, 32]; - - let age_plan = Plan::context("users/age") - .with::<u32, _>(Equality) - .build() - .unwrap(); - let written = cipher.encrypt(&ages).using(&age_plan).await.unwrap(); - let back: Vec<u32> = cipher.open(written).using(&age_plan).await.unwrap(); - assert_eq!(back, ages); - assert_eq!(generates.load(Ordering::SeqCst), 1); - assert_eq!( - retrieves.load(Ordering::SeqCst), - 1, - "one retrieve for the Vec" - ); - - let written = cipher.encrypt(&ages).using(&age_plan).await.unwrap(); - let ciphertexts: Vec<StackCipherText> = written.into_iter().map(|e| e.ciphertext).collect(); - let back: Vec<u32> = cipher.open(ciphertexts).using(&age_plan).await.unwrap(); - assert_eq!(back, ages, "a Vec of bare ciphertexts opens too"); - let one = cipher.encrypt(&30u32).using(&age_plan).await.unwrap(); - let back: u32 = cipher.open(one.ciphertext).using(&age_plan).await.unwrap(); - assert_eq!(back, 30); - - let typed_plan = Plan::value::<u32>() - .context("users/age") - .encrypt_into::<(StackCipherText, EqualityTerm)>() - .build() - .unwrap(); - let before = retrieves.load(Ordering::SeqCst); - let written = cipher.encrypt(&ages).using(&typed_plan).await.unwrap(); - let back: Vec<u32> = cipher.open(written).using(&typed_plan).await.unwrap(); - assert_eq!(back, ages); - assert_eq!(retrieves.load(Ordering::SeqCst), before + 1); -} - -// --- Pickers ----------------------------------------------------------------- - -#[tokio::test] -async fn a_picker_plan_needs_no_fields_impl_and_is_the_by_name_plans_bytes() { - /// The same record, readable by name. - #[derive(Clone)] - struct Named(User); - impl Fields for Named { - fn field_names(&self) -> Vec<&str> { - vec!["email", "age"] - } - } - impl Field<String> for Named { - fn field(&self, name: &str) -> Option<&String> { - (name == "email").then_some(&self.0.email) - } - } - impl Field<u32> for Named { - fn field(&self, name: &str) -> Option<&u32> { - (name == "age").then_some(&self.0.age) - } - } - - let (cipher, sent) = recording_cipher().await; - let picked: Plan<User, _> = Plan::context("users") - .fields() - .encrypt_index(pick("email", |u: &User| &u.email), (Equality, Ore)) - .encrypt(pick("age", |u: &User| &u.age)) - .build() - .unwrap(); - let named: Plan<Named, _> = Plan::context("users") - .fields() - .encrypt_index::<String>("email", (Equality, Ore)) - .encrypt::<u32>("age") - .build() - .unwrap(); - let mut a = cipher.encrypt(&user()).using(&picked).await.unwrap(); - let mut b = cipher.encrypt(&Named(user())).using(&named).await.unwrap(); - let sent = sent.lock().unwrap().generated(); - assert_eq!( - sent, - ["users/email", "users/age", "users/email", "users/age"] - ); - let a_email: Encrypted<(EqualityTerm, OreTerm<String>)> = a.take("email").unwrap(); - let b_email: Encrypted<(EqualityTerm, OreTerm<String>)> = b.take("email").unwrap(); - assert_eq!(a_email.terms, b_email.terms); - b.insert("email", a_email); - let back = cipher.open(b).using(&picked).await.unwrap(); - assert_eq!(back.get::<String>("email"), Some(&user().email)); -} - -#[tokio::test] -async fn a_plan_naming_any_field_by_name_checks_the_value_and_one_of_pickers_does_not() { - /// A value naming a field no plan declares. - struct Extra(User); - impl Fields for Extra { - fn field_names(&self) -> Vec<&str> { - vec!["email", "age", "extra"] - } - } - impl Field<String> for Extra { - fn field(&self, name: &str) -> Option<&String> { - (name == "email").then_some(&self.0.email) - } - } - - let cipher = stack_cipher().await; - let mixed: Plan<Extra, _> = Plan::context("users") - .fields() - .encrypt::<String>("email") - .encrypt(pick("age", |e: &Extra| &e.0.age)) - .build() - .unwrap(); - assert_eq!( - plan_error(cipher.encrypt(&Extra(user())).using(&mixed).await), - PlanError::NotInPlan { - field: "extra".into() - } - ); - let picked: Plan<Extra, _> = Plan::context("users") - .fields() - .encrypt(pick("email", |e: &Extra| &e.0.email)) - .build() - .unwrap(); - let record = cipher.encrypt(&Extra(user())).using(&picked).await.unwrap(); - assert_eq!(record.names().collect::<Vec<_>>(), ["email"]); -} - -// --- identity rescues a name that is not a plain segment ----------------------- - -#[tokio::test] -async fn a_pinned_identity_rescues_a_field_name_that_is_not_plain() { - #[derive(Clone)] - struct Secret { - totp: String, - } - #[derive(Clone)] - struct Reading(u32, String); - - let (cipher, sent) = recording_cipher().await; - let users_plan: Plan<Secret, _> = Plan::context("users") - .fields() - .encrypt(pick("2fa_secret", |s: &Secret| &s.totp)) - .identity("totp_secret") - .build() - .unwrap(); - assert_eq!( - users_plan - .field("2fa_secret") - .unwrap() - .label() - .unwrap() - .to_string(), - "users/totp_secret" - ); - let record = cipher - .encrypt(&Secret { totp: "s".into() }) - .using(&users_plan) - .await - .unwrap(); - assert_eq!(record.names().collect::<Vec<_>>(), ["2fa_secret"]); - - // A tuple record: fields named by index, keyed by their identities. - let readings_plan: Plan<Reading, _> = Plan::context("reading") - .fields() - .encrypt_index(pick("0", |r: &Reading| &r.0), Equality) - .identity("value") - .encrypt(pick("1", |r: &Reading| &r.1)) - .identity("unit") - .build() - .unwrap(); - let mut record = cipher - .encrypt(&Reading(21, "celsius".into())) - .using(&readings_plan) - .await - .unwrap(); - assert_eq!( - sent.lock().unwrap().generated(), - ["users/totp_secret", "reading/value", "reading/unit"] - ); - let value: Encrypted<EqualityTerm> = record.take("0").unwrap(); - let query_value = cipher - .query(op::Eq, &21u32) - .on(&readings_plan.field("0").unwrap()) - .await - .unwrap(); - assert_eq!(query_value, value.terms); - let opened: u32 = cipher - .decrypt(value.ciphertext, nonempty!("reading").with("value")) - .await - .unwrap(); - assert_eq!(opened, 21); + let keyset = cipher.default_keyset(); + let users = users_plan(); + let records = records_plan(); + let none = || frame(Vec::<ValueFrame>::new()); + let stored_none = || CipherText::Sequence(Vec::new()); - // Without an identity the name is the identity, and must be plain. - let refused = Plan::context("users") - .fields::<Secret, FakeDataKeySource>() - .encrypt(pick("2fa_secret", |s: &Secret| &s.totp)) - .build(); - assert!(matches!( - plan_error(refused), - PlanError::FieldLabel { field, .. } if field == "2fa_secret" - )); - // And in a plan whose context comes later, likewise. - let refused = Plan::fields::<Secret, FakeDataKeySource>() - .encrypt(pick("2fa_secret", |s: &Secret| &s.totp)) - .build(); - assert!(matches!( - plan_error(refused), - PlanError::FieldLabel { field, .. } if field == "2fa_secret" - )); -} + let refused = Planned::new(&users, none()) + .encrypt_into_with_context::<StackCipherText, _>(&keyset, Scope::new("")) + .await; + assert_eq!(plan_error(refused), PlanError::EmptyPart); + let refused = ValueFrame::decrypt_from_with_context( + Planned::new(&users, stored_none()), + &keyset, + Scope::new(""), + ) + .await; + assert_eq!(plan_error(refused), PlanError::EmptyPart); -/// In a plan built without a context, a pinned identity still keys the -/// field: the write, the query and the open all use `<call>/<identity>`, -/// never the field's name. -#[tokio::test] -async fn a_pinned_identity_keys_a_call_context_plan_for_query_and_open() { - #[derive(Clone)] - struct Reading(u32); - let (cipher, sent) = recording_cipher().await; - let readings_plan: Plan<Reading, _> = Plan::fields() - .encrypt_index(pick("0", |r: &Reading| &r.0), Equality) - .identity("value") - .build() - .unwrap(); - let mut record = cipher - .encrypt(&Reading(21)) - .context("reading") - .using(&readings_plan) - .await - .unwrap(); - assert_eq!(sent.lock().unwrap().generated(), ["reading/value"]); - let stored: Encrypted<EqualityTerm> = record.take("0").unwrap(); - let query_value = cipher - .query(op::Eq, &21u32) - .context("reading") - .on(&readings_plan.field("0").unwrap()) - .await - .unwrap(); - assert_eq!(query_value, stored.terms); - record.insert("0", stored); - let back = cipher - .open(record) - .context("reading") - .using(&readings_plan) + let opened = record::decrypt( + &cipher, + stored_none(), + &records, + Some(label("tenants/acme")), + ) + .unwrap() + .await + .unwrap(); + assert_eq!(opened, none()); + let sealed: StackCipherText = Planned::new(&users, none()) + .encrypt_into_with_context(&keyset, Scope::new("acme")) .await .unwrap(); - assert_eq!(back.get::<u32>("0"), Some(&21)); -} - -// --- Targets declare their indexes -------------------------------------------- - -#[test] -fn every_target_declares_the_indexes_its_terms_answer() { - assert_eq!( - <EqualityTerm as EncryptFrom<String>>::indexes(), - [IndexSpec::Equality] - ); - assert_eq!( - <MatchTerms<Words> as EncryptFrom<String>>::indexes(), - [IndexSpec::Match(Words::options())] - ); - assert_eq!( - <OreTerm<u32> as EncryptFrom<u32>>::indexes(), - [IndexSpec::Ore] - ); - assert_eq!( - <OpeTerm<u32> as EncryptFrom<u32>>::indexes(), - [IndexSpec::Ope] - ); - assert_eq!( - <OreBlockTerm<u32> as EncryptFrom<u32>>::indexes(), - [IndexSpec::OreBlock] - ); - assert!(<StackCipherText as EncryptFrom<u32>>::indexes().is_empty()); - assert_eq!( - <Encrypted<(OreTerm<u32>, EqualityTerm)> as EncryptFrom<u32>>::indexes(), - [IndexSpec::Ore, IndexSpec::Equality] - ); - assert_eq!( - <(StackCipherText, EqualityTerm, OreTerm<u32>, OpeTerm<u32>) as EncryptFrom<u32>>::indexes( - ), - [IndexSpec::Equality, IndexSpec::Ore, IndexSpec::Ope] - ); - assert_eq!( - <(OreTerm<u32>, StackCipherText, EqualityTerm) as EncryptFrom<u32>>::indexes(), - [IndexSpec::Ore, IndexSpec::Equality] - ); - assert_eq!( - <( - EqualityTerm, - OreTerm<u32>, - StackCipherText, - OpeTerm<u32>, - EqualityTerm, - ) as EncryptFrom<u32>>::indexes(), - [ - IndexSpec::Equality, - IndexSpec::Ore, - IndexSpec::Ope, - IndexSpec::Equality - ] - ); -} - -#[test] -#[allow(clippy::assertions_on_constants)] // evaluated at run time, so a mutant fails a test -fn a_tuple_is_decryptable_when_one_element_is() { - assert!(<(StackCipherText, EqualityTerm) as Decryptable>::DECRYPTABLE); - assert!(<(EqualityTerm, StackCipherText) as Decryptable>::DECRYPTABLE); - assert!(!<(EqualityTerm, OreTerm<u32>) as Decryptable>::DECRYPTABLE); - assert!(<(EqualityTerm, OreTerm<u32>, StackCipherText) as Decryptable>::DECRYPTABLE); - assert!( - !<(EqualityTerm, OreTerm<u32>, OpeTerm<u32>, EqualityTerm) as Decryptable>::DECRYPTABLE - ); - assert!( - <( - EqualityTerm, - OreTerm<u32>, - OpeTerm<u32>, - EqualityTerm, - StackCipherText - ) as Decryptable>::DECRYPTABLE - ); - assert!( - !<( - EqualityTerm, - OreTerm<u32>, - OpeTerm<u32>, - EqualityTerm, - OreTerm<u32> - ) as Decryptable>::DECRYPTABLE - ); -} - -#[tokio::test] -async fn a_tuple_opens_through_its_ciphertext_wherever_it_sits() { - let cipher = stack_cipher().await; - let keyset = cipher.default_keyset(); - let context = || CallerContext::from(nonempty!("t")); - let terms_first: (EqualityTerm, OreTerm<u32>, StackCipherText) = - keyset.encrypt_as(&7u32, context()).await.unwrap(); - let opened: u32 = cipher.decrypt_as(terms_first, context()).await.unwrap(); - assert_eq!(opened, 7); - - let terms_alone: (EqualityTerm, OreTerm<u32>) = - keyset.encrypt_as(&7u32, context()).await.unwrap(); - let result: Result<u32, _> = cipher.decrypt_as(terms_alone, context()).await; - assert!(matches!(result, Err(Error::NotOpened)), "{result:?}"); - - // Five elements, the ciphertext last. - let five: ( - EqualityTerm, - OreTerm<u32>, - OpeTerm<u32>, - EqualityTerm, - StackCipherText, - ) = keyset.encrypt_as(&7u32, context()).await.unwrap(); - let opened: u32 = cipher.decrypt_as(five, context()).await.unwrap(); - assert_eq!(opened, 7); - - // As a field of a derived record, through `DecryptField`. - let pair: (StackCipherText, EqualityTerm) = keyset.encrypt_as(&7u32, context()).await.unwrap(); - let opening: Option<Decryption<u32, FakeDataKeySource>> = pair.decryption_field(context()); - let opened = cipher.run_decryption(opening.unwrap()).await.unwrap(); - assert_eq!(opened, 7); - let _ = <(StackCipherText, EqualityTerm) as DecryptInto<u32>>::decryption::<FakeDataKeySource>; -} - -/// A record whose terms-only tuple comes before its ciphertext. -#[derive(stack_encrypt::EncryptFrom, stack_encrypt::DecryptInto)] -#[stash(plaintext = u32)] -struct TermsFirst { - terms: (EqualityTerm, OreTerm<u32>), - c: StackCipherText, -} - -/// A tuple of terms alone is no field to open a record through: it yields -/// to the ciphertext beside it, nested in a tuple or in a derived record, -/// rather than stopping the search with `NotOpened`. -#[tokio::test] -async fn a_terms_only_tuple_yields_to_the_ciphertext_beside_it() { - let cipher = stack_cipher().await; - let keyset = cipher.default_keyset(); - let context = || CallerContext::from(nonempty!("t")); - - let terms_alone: (EqualityTerm, OreTerm<u32>) = - keyset.encrypt_as(&7u32, context()).await.unwrap(); - let opening: Option<Decryption<u32, FakeDataKeySource>> = - terms_alone.decryption_field(context()); - assert!(opening.is_none(), "a tuple of terms alone opens nothing"); - - let nested: ((EqualityTerm, OreTerm<u32>), StackCipherText) = - keyset.encrypt_as(&7u32, context()).await.unwrap(); - let opened: u32 = cipher.decrypt_as(nested, context()).await.unwrap(); - assert_eq!(opened, 7); - - let record: TermsFirst = keyset.encrypt_as(&7u32, context()).await.unwrap(); - let opened: u32 = cipher.decrypt_as(record, context()).await.unwrap(); - assert_eq!(opened, 7); + assert!(matches!(sealed, CipherText::Sequence(rows) if rows.is_empty())); + assert_eq!(generates.load(Ordering::SeqCst), 0); + assert_eq!(retrieves.load(Ordering::SeqCst), 0); } +/// The declaration says where the context comes from, before any value is +/// in hand. #[test] fn a_plan_says_where_its_context_comes_from() { - let records_plan = records_plan::<FakeDataKeySource>(); - assert!(format!("{records_plan:?}").starts_with(r#"Plan { context: "<from field \"tenant\">""#)); - let users_plan = contextless_plan::<FakeDataKeySource>(); - assert!(format!("{users_plan:?}").starts_with(r#"Plan { context: "<from the call>""#)); - let builder = - Plan::fields::<TenantRecord, FakeDataKeySource>().context_field(("tenant", tenant)); - assert!(format!("{builder:?}").starts_with(r#"FieldsBuilder { context: [Field("tenant")]"#)); - let start = Plan::value::<u32>().context("a"); + let users = users_plan(); assert_eq!( - format!("{start:?}"), - r#"ValueStart { context: [Ok(Label(["a"]))] }"# + users.label().map(ToString::to_string).as_deref(), + Some("users") ); - let typed = Plan::value::<u32>() - .encrypt_into::<StackCipherText>() - .build() - .unwrap(); - assert!(format!("{typed:?}").contains("Typed<")); - let _ = Label::parse("a").unwrap(); + assert_eq!(users.context_field(), None); + let records = records_plan(); + assert_eq!(records.label(), None); + assert_eq!(records.context_field(), Some("tenant")); + let shown = records.contexts().unwrap().to_string(); + assert!(shown.contains("tenant"), "{shown}"); } diff --git a/packages/stack-encrypt/tests/query_operators.rs b/packages/stack-encrypt/tests/query_operators.rs index f6774bd6d2..98ba96c31d 100644 --- a/packages/stack-encrypt/tests/query_operators.rs +++ b/packages/stack-encrypt/tests/query_operators.rs @@ -1,11 +1,11 @@ //! The query fixture: a query a binding names by operator derives the term -//! the typed query chain derives, and a probabilistic term's two questions -//! answer as the typed `MatchQuery` answers them (ADR-0009; ADR-0007, -//! amended 2026-10-16). `tests/fixtures/query_operators.json` records each -//! query's term, a page of verified values and a few fetch limits, so the -//! Go SDK's tests can hold the guest's `se_query_term`, `se_verify` and -//! `se_target_limit` exports to the same results; its README gives the -//! schema. +//! the marker of the same field of a derived record derives, and a +//! probabilistic term's two questions answer as the typed `MatchQuery` +//! answers them (ADR-0009; ADR-0007, amended 2026-10-16). +//! `tests/fixtures/query_operators.json` records each query's term, a page +//! of verified values and a few fetch limits, so the Go SDK's tests can hold +//! the guest's `se_query_term`, `se_verify` and `se_target_limit` exports to +//! the same results; its README gives the schema. //! //! Every query derives under the deterministic key source the record //! fixture uses, whose index key is `FakeDataKeySource`'s, so nothing in the @@ -23,9 +23,12 @@ use common::frames::{frame, object}; use common::{deterministic_cipher, DeterministicSource}; use serde_json::{json, Value as Json}; use stack_encrypt::dynamic::{self, record, NoTargets, Op, Operand, QueryTerm, ValueFrame}; -use stack_encrypt::op::{self, Limit, Probabilistic, Stats}; -use stack_encrypt::{Equality, KeysetCipher, Match, Ore, Plan, StackCipher}; +use stack_encrypt::op::{Limit, Probabilistic, Stats}; +use stack_encrypt::sem::{EqualityTerm, MatchQuery, MatchTerms, OreTerm}; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::{Encrypted, KeysetCipher}; use vitaminc_aead::plaintext::{Physical, Raw}; +use zeroize::Zeroize; const SEED: [u8; 32] = *b"stack-encrypt record fixture v1 "; @@ -39,6 +42,27 @@ fn plan_json() -> Json { }) } +#[derive(Clone, Zeroize)] +struct User { + email: String, + age: u32, + name: String, + score: f64, +} + +/// The declaration every query runs on, as a derived record: the scope, +/// the field names and the indexes of [`plan_json`], so each field's +/// marker derives under the label the data plan gives the field. +#[derive(EncryptTarget)] +#[stash(struct = User, scope = "users")] +#[allow(dead_code)] +struct EncryptedUser { + email: Encrypted<EqualityTerm>, + age: Encrypted<(EqualityTerm, OreTerm<u32>)>, + name: Encrypted<MatchTerms>, + score: Encrypted<OreTerm<f64>>, +} + /// Each query: a field, an operator and its input, as a binding sends them. fn queries() -> Vec<(&'static str, Op, Json)> { vec![ @@ -127,68 +151,59 @@ fn limit_json(limit: Limit) -> Json { limit.rows().map_or(Json::Null, |rows| json!(rows)) } -/// The typed chain's term for the same query, as the operand's bytes: the -/// proof that the data path derives what a Rust caller does. +/// The term of the field's marker for the same query, as the operand's +/// bytes: the proof that the data path derives what a Rust caller does. async fn typed_operand( - cipher: &StackCipher<DeterministicSource>, + keyset: &KeysetCipher<'_, DeterministicSource>, field: &str, operator: Op, input: &Json, ) -> Operand { let int = |value: &Json| u32::try_from(value.as_u64().expect("an integer")).expect("a u32"); + let float = |value: &Json| value.as_f64().expect("a number"); let text = |value: &Json| value.as_str().expect("text").to_owned(); - let age = Plan::context("users/age") - .with::<u32, _>((Equality, Ore)) - .build() - .expect("plan"); let one = Operand::One; match (field, operator) { - ("email", Op::Eq) => { - let plan = Plan::context("users/email") - .with::<String, _>(Equality) - .build() - .expect("plan"); - one(cipher - .query(op::Eq, &text(input)) - .on(&plan) - .await - .expect("eq") - .to_bytes()) - } - ("age", Op::Eq) => one(cipher - .query(op::Eq, &int(input)) - .on(&age) + ("email", Op::Eq) => one(EncryptedUser::EMAIL + .eq(text(input)) + .encrypt(keyset) + .await + .expect("eq") + .to_bytes()), + ("age", Op::Eq) => one(EncryptedUser::AGE + .eq(int(input)) + .encrypt(keyset) .await .expect("eq") .to_bytes()), - ("age", Op::Lt) => one(cipher - .query(op::Lt, &int(input)) - .on(&age) + ("age", Op::Lt) => one(EncryptedUser::AGE + .lt(int(input)) + .encrypt(keyset) .await .expect("lt") .to_bytes()), - ("age", Op::Lte) => one(cipher - .query(op::Lte, &int(input)) - .on(&age) + ("age", Op::Lte) => one(EncryptedUser::AGE + .lte(int(input)) + .encrypt(keyset) .await .expect("lte") .to_bytes()), - ("age", Op::Gt) => one(cipher - .query(op::Gt, &int(input)) - .on(&age) + ("age", Op::Gt) => one(EncryptedUser::AGE + .gt(int(input)) + .encrypt(keyset) .await .expect("gt") .to_bytes()), - ("age", Op::Gte) => one(cipher - .query(op::Gte, &int(input)) - .on(&age) + ("age", Op::Gte) => one(EncryptedUser::AGE + .gte(int(input)) + .encrypt(keyset) .await .expect("gte") .to_bytes()), ("age", Op::Between) => { - let range = cipher - .query(op::Between, &(int(&input[0]), int(&input[1]))) - .on(&age) + let range = EncryptedUser::AGE + .between(int(&input[0]), int(&input[1])) + .encrypt(keyset) .await .expect("between"); Operand::Range { @@ -196,49 +211,32 @@ async fn typed_operand( hi: range.hi().to_bytes(), } } - ("name", Op::Matches) => one(typed_match(cipher, &text(input)).await.to_bytes()), - ("score", operator) => { - let float = |value: &Json| value.as_f64().expect("a number"); - let score = Plan::context("users/score") - .with::<f64, _>(Ore) - .build() - .expect("plan"); - match operator { - Op::Lt => one(cipher - .query(op::Lt, &float(input)) - .on(&score) - .await - .expect("lt") - .to_bytes()), - Op::Between => { - let range = cipher - .query(op::Between, &(float(&input[0]), float(&input[1]))) - .on(&score) - .await - .expect("between"); - Operand::Range { - lo: range.lo().to_bytes(), - hi: range.hi().to_bytes(), - } - } - other => panic!("no typed score query for {other}"), + ("name", Op::Matches) => one(typed_match(keyset, &text(input)).await.to_bytes()), + ("score", Op::Lt) => one(EncryptedUser::SCORE + .lt(float(input)) + .encrypt(keyset) + .await + .expect("lt") + .to_bytes()), + ("score", Op::Between) => { + let range = EncryptedUser::SCORE + .between(float(&input[0]), float(&input[1])) + .encrypt(keyset) + .await + .expect("between"); + Operand::Range { + lo: range.lo().to_bytes(), + hi: range.hi().to_bytes(), } } other => panic!("no typed query for {other:?}"), } } -async fn typed_match( - cipher: &StackCipher<DeterministicSource>, - input: &str, -) -> stack_encrypt::sem::MatchQuery { - let plan = Plan::context("users/name") - .with::<String, _>(Match::default()) - .build() - .expect("plan"); - cipher - .query(op::Matches, input) - .on(&plan) +async fn typed_match(keyset: &KeysetCipher<'_, DeterministicSource>, input: &str) -> MatchQuery { + EncryptedUser::NAME + .matches(input) + .encrypt(keyset) .await .expect("matches") } @@ -276,8 +274,8 @@ async fn derived() -> Json { let term = data_term(&keyset, &plan, field, operator, &input).await; assert_eq!( term.operand(), - &typed_operand(&cipher, field, operator, &input).await, - "{operator} on {field}: the data path derives the typed chain's term" + &typed_operand(&keyset, field, operator, &input).await, + "{operator} on {field}: the data path derives the field marker's term" ); terms.push(json!({ "field": field, @@ -287,7 +285,7 @@ async fn derived() -> Json { })); } - let typed = typed_match(&cipher, "alice").await; + let typed = typed_match(&keyset, "alice").await; let values: Vec<ValueFrame> = page() .as_array() .expect("a list") @@ -317,7 +315,7 @@ async fn derived() -> Json { let Operand::One(bytes) = term.operand() else { panic!("a match term is one term"); }; - let typed = typed_match(&cipher, input).await; + let typed = typed_match(&keyset, input).await; for (stats_json, stats) in stats_cases() { let read = Stats::from_frame(frame_of_value(&stats_json).as_protected_ref()) .expect("stats read"); @@ -342,6 +340,9 @@ async fn derived() -> Json { } } + // The comment is part of the committed fixture, and is compared with it + // as every other key is: it keeps the wording the fixture was written + // with. json!({ "_comment": "Query terms derived by operator through the data path (stack_encrypt::dynamic::query_term), each equal to the typed query chain's, with a verified page and fetch limits. Read by tests/query_operators.rs and the Go SDK's tests; the schema is in README.md beside this file. Regenerate with STACK_ENCRYPT_UPDATE_FIXTURES=1; do not edit by hand.", "key_source": { "kind": "deterministic-sha256", "seed": hex(&SEED) }, diff --git a/packages/stack-encrypt/tests/query_refusal_conformance.rs b/packages/stack-encrypt/tests/query_refusal_conformance.rs index bc0b246417..a4078b53b1 100644 --- a/packages/stack-encrypt/tests/query_refusal_conformance.rs +++ b/packages/stack-encrypt/tests/query_refusal_conformance.rs @@ -1,6 +1,7 @@ //! The shared query-refusal profile: what a query is refused for before any //! term exists, through the binding's check, the binding's derivation and, -//! where the typed chain can spell the same query, the typed chain. See +//! where a field marker of a derived record can spell the same query, the +//! typed query from that marker. See //! conformance/stack-encrypt/query-refusals-contract.md. #![cfg(feature = "dynamic")] mod common; @@ -14,10 +15,9 @@ use miette::Diagnostic; use serde::Deserialize; use serde_json::json; use stack_encrypt::dynamic::{self, record, Error, NoTargets, Op, ValueFrame}; -use stack_encrypt::sem::DefaultMatch; -use stack_encrypt::{ - op, AsProtectedRef as _, Equality, Match, Ope, Ore, OreBlock, Plan, StackCipher, -}; +use stack_encrypt::sem::{EqualityTerm, MatchTerms, OpeTerm, OreBlockTerm, OreTerm}; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::{AsProtectedRef as _, Encrypted, KeysetCipher, StackCipher}; use std::{ collections::{BTreeMap, BTreeSet}, sync::atomic::{AtomicUsize, Ordering}, @@ -52,9 +52,11 @@ struct Case { input: QueryInput, accepted: bool, expected_error: Option<ExpectedError>, - /// How the typed Rust chain answers the same query, for the queries it - /// can spell: refused before the named keyset is loaded, after it, or - /// not refused. + /// How the typed query answers the same query, for the queries a field + /// marker can spell: refused before a keyset is asked for anything, or + /// not refused. The corpus has a third value, for a refusal after the + /// keyset was loaded. A marker query runs through a keyset the caller + /// already holds, so it cannot be refused at that stage. typed: Option<Typed>, } @@ -146,17 +148,41 @@ fn check(id: &str, path: &str, result: Result<(), Error>, expected: Option<&Expe } } -/// The typed chain's answer: a `stack_encrypt::Error` code, or none. +/// The fields of the corpus plan that a typed query can name, each with +/// the plaintext type the corpus declares for it. +#[derive(Clone, zeroize::Zeroize)] +struct Conformance { + email: String, + name: String, + nickname: String, + title: String, + age: u32, + big: i128, +} + +/// The same fields as a derived record, each under `conformance/<field>` +/// with the indexes the corpus plan declares. Only its field markers are +/// used: no record is sealed, so no field is read. +#[derive(EncryptTarget)] +#[allow(dead_code)] +#[stash(struct = Conformance, scope = "conformance")] +struct Fields { + email: Encrypted<EqualityTerm>, + name: Encrypted<MatchTerms>, + nickname: Encrypted<OreTerm<String>>, + title: Encrypted<OreBlockTerm<String>>, + age: Encrypted<(EqualityTerm, OreTerm<u32>)>, + big: Encrypted<(EqualityTerm, OpeTerm<i128>)>, +} + +/// The typed query's answer: a `stack_encrypt::Error` code, or none. The +/// query starts from the marker of `field`. async fn typed( - cipher: &StackCipher<CountingSource>, - keyset: &str, + keyset: &KeysetCipher<'_, CountingSource>, field: &str, - kind: &str, - outputs: &[String], operator: Op, query: &QueryInput, ) -> Option<String> { - let context = format!("conformance/{field}"); let text = |query: &QueryInput| match query { QueryInput::Scalar(Input { kind, text }) if kind == "string" => text.clone(), other => panic!("{field}: not text: {other:?}"), @@ -186,97 +212,45 @@ async fn typed( hi.text.parse::<i128>().unwrap(), ) }; - // The index that answers the operator, as the plan declares it: the - // equality, match or order index, whichever the operator reads. - let answers: &[&str] = match operator { - Op::Eq => &["eq"], - Op::Matches => &["match"], - _ => &["ore", "ore_block", "ope"], - }; - let index = outputs - .iter() - .map(String::as_str) - .find(|output| answers.contains(output)) - .expect("an index that answers the operator"); + // The marker's label is the context the corpus plan gives the field, + // so the typed query and the binding's query are the same query. macro_rules! run { - ($plan:expr, $op:expr, $value:expr) => { - run!($plan, $op, $value, Ore) - }; - ($plan:expr, $op:expr, $value:expr, $via:expr) => { - cipher - .query($op, $value) - .via($via) - .on(&$plan) - .keyset(keyset) + ($marker:expr, $query:expr) => {{ + assert_eq!( + $marker.label().to_string(), + format!("conformance/{field}"), + "{field}: the marker's label" + ); + $query + .encrypt(keyset) .await .err() .map(|error| error.code().expect("a code").to_string()) - }; + }}; } - match (kind, index, operator) { - ("string", "eq", Op::Eq) => { - let plan = Plan::context(context) - .with::<String, _>(Equality) - .build() - .unwrap(); - run!(plan, op::Eq, &text(query), Equality) - } - ("string", "match", Op::Matches) => { - let plan = Plan::context(context) - .with::<String, _>(Match::<DefaultMatch>::default()) - .build() - .unwrap(); - run!( - plan, - op::Matches, - &text(query), - Match::<DefaultMatch>::default() - ) + match (field, operator) { + ("email", Op::Eq) => run!(Fields::EMAIL, Fields::EMAIL.eq(text(query))), + ("name", Op::Matches) => run!(Fields::NAME, Fields::NAME.matches(text(query))), + ("nickname", Op::Lt) => run!(Fields::NICKNAME, Fields::NICKNAME.lt(text(query))), + ("nickname", Op::Between) => { + let (lo, hi) = texts(query); + run!(Fields::NICKNAME, Fields::NICKNAME.between(lo, hi)) } - ("string", "ore", Op::Lt) => { - let plan = Plan::context(context) - .with::<String, _>(Ore) - .build() - .unwrap(); - run!(plan, op::Lt, &text(query)) + ("title", Op::Lt) => run!(Fields::TITLE, Fields::TITLE.lt(text(query))), + ("title", Op::Gt) => run!(Fields::TITLE, Fields::TITLE.gt(text(query))), + ("title", Op::Between) => { + let (lo, hi) = texts(query); + run!(Fields::TITLE, Fields::TITLE.between(lo, hi)) } - ("string", "ore", Op::Between) => { - let plan = Plan::context(context) - .with::<String, _>(Ore) - .build() - .unwrap(); - run!(plan, op::Between, &texts(query)) + ("age", Op::Between) => { + let (lo, hi) = u32s(query); + run!(Fields::AGE, Fields::AGE.between(lo, hi)) } - ("string", "ore_block", Op::Lt) => { - let plan = Plan::context(context) - .with::<String, _>(OreBlock) - .build() - .unwrap(); - run!(plan, op::Lt, &text(query), OreBlock) + ("big", Op::Between) => { + let (lo, hi) = i128s(query); + run!(Fields::BIG, Fields::BIG.between(lo, hi)) } - ("string", "ore_block", Op::Gt) => { - let plan = Plan::context(context) - .with::<String, _>(OreBlock) - .build() - .unwrap(); - run!(plan, op::Gt, &text(query), OreBlock) - } - ("string", "ore_block", Op::Between) => { - let plan = Plan::context(context) - .with::<String, _>(OreBlock) - .build() - .unwrap(); - run!(plan, op::Between, &texts(query), OreBlock) - } - ("uint32", "ore", Op::Between) => { - let plan = Plan::context(context).with::<u32, _>(Ore).build().unwrap(); - run!(plan, op::Between, &u32s(query)) - } - ("int128", "ope", Op::Between) => { - let plan = Plan::context(context).with::<i128, _>(Ope).build().unwrap(); - run!(plan, op::Between, &i128s(query), Ope) - } - other => panic!("{field}: the typed chain is not spelled for {other:?}"), + other => panic!("{field}: no typed query is spelled for {other:?}"), } } @@ -386,20 +360,11 @@ async fn shared_query_refusal_profile() { let Some(want) = &case.typed else { continue; }; - let spec = field.expect("a declared field"); - // A named keyset is loaded once and cached, so each typed query - // names its own: the load is the observation. - let code = typed( - &cipher, - &format!("keyset-{}", case.id), - &case.field, - &spec.kind, - &spec.outputs, - operator, - &case.input, - ) - .await; - let loads = counters[0].swap(0, Ordering::SeqCst); + assert!(field.is_some(), "{}: a declared field", case.id); + // A marker query runs through a keyset the caller holds, here the + // default one, so it has no keyset to load. The observation is that + // it asks the key source for nothing, refused or not. + let code = typed(&keyset, &case.field, operator, &case.input).await; match want.refusal { Refusal::None => { assert!( @@ -407,20 +372,23 @@ async fn shared_query_refusal_profile() { "{}: typed {code:?}", case.id ); - assert_eq!(loads, 1, "{}: the typed chain loaded its keyset", case.id); } - Refusal::BeforeKeyset | Refusal::AfterKeyset => { + Refusal::BeforeKeyset => { let expected = expected.expect("a refusal"); assert_eq!(expected.class, Class::Term, "{}: typed", case.id); assert_eq!(code.as_deref(), Some(expected.code.as_str()), "{}", case.id); - assert_eq!( - loads, - usize::from(want.refusal == Refusal::AfterKeyset), - "{}: typed refusal stage", - case.id - ); } + Refusal::AfterKeyset => panic!( + "{}: a marker query is prepared before its keyset is asked for anything", + case.id + ), } + assert_eq!( + counters[0].load(Ordering::SeqCst), + 0, + "{}: typed keyset lookup", + case.id + ); assert_eq!( counters[1].load(Ordering::SeqCst), 0, diff --git a/packages/stack-encrypt/tests/record_lowering.rs b/packages/stack-encrypt/tests/record_lowering.rs index ca2cc19649..f3a59d8aa7 100644 --- a/packages/stack-encrypt/tests/record_lowering.rs +++ b/packages/stack-encrypt/tests/record_lowering.rs @@ -1,19 +1,31 @@ //! The record fixture is the proof of the lowering (ADR-0007, amended -//! 2026-10-06): a Rust chain and the data-plan lowering each open the -//! records the other sealed, and both derive the same bytes for each term. -//! The chain's fields are `dynamic::ValueFrame`s, the plaintext type a binding's -//! values have, so the two authors write one declaration over one type; a -//! chain over bare `u32`/`String` fields seals the same tagged leaves (see +//! 2026-10-06): the combinators of `stack_encrypt::target`, composed by +//! hand, and the data-plan lowering each open the records the other sealed, +//! and both derive the same bytes for each term. The combinators run over +//! `dynamic::ValueFrame` fields, the plaintext type a binding's values have, +//! so the two authors write one declaration over one type; combinators over +//! bare `u32`/`String` fields seal the same tagged leaves (see //! `dynamic::record`). `tests/fixtures/record_lowering.json` holds one //! record from each author, sealed under a deterministic key source so the //! bytes open in any process, and its README gives the schema a Go test //! reads later. //! +//! The fixture names the first author `typed_chain`, and the tests here +//! say "the chain". The committed records of that author were sealed by the +//! plan builder's chain, which is removed. The chain added no operation of +//! its own: `encrypt_index` was `indexed(..).under(label)`, `encrypt` was +//! `ciphertext().under(label)`, `passthrough` was `passthrough()`, and an +//! open was `open` for each field. Those combinators remain, so they seal +//! and open in the chain's place. The fixture and the test names keep the +//! old word, because the Go module reads the file and the fixture +//! enrollment index lists the tests by name. +//! //! The fixture holds a second case under its `context_field` key: the same -//! proof for a plan that takes its context from a field of the record -//! (`FieldsBuilder::context_field`, the data grammar's `"context_field"`), -//! sealed by each author and opened by the other under the context the -//! record stores, and refused by both under another. +//! proof for a plan that takes its context from a field of the record (the +//! data grammar's `"context_field"`). The combinators read the context from +//! the record and run the sealed field under it. Each author seals the +//! record, and the other opens it under the context the record stores. A +//! reader that names another context opens neither author's record. //! //! Regenerate the fixture with `STACK_ENCRYPT_UPDATE_FIXTURES=1 cargo test //! --test record_lowering --all-features`; every other run reads it. Each @@ -32,10 +44,15 @@ use common::{deterministic_cipher, DeterministicSource}; use serde_json::{json, Map, Value as Json}; use stack_encrypt::dynamic::record::{self, Plan as DataPlan}; use stack_encrypt::dynamic::{TermBytes, ValueFrame, ValueKind}; -use stack_encrypt::plan::{pick, FieldValues}; +use stack_encrypt::plan::IntoLabel; use stack_encrypt::sem::MatchOptions; -use stack_encrypt::target::{Encrypted, IndexSpec}; -use stack_encrypt::{CipherText, Error, Label, Plan, SealedValue, StackCipher, StackCipherText}; +use stack_encrypt::target::{ + ciphertext, indexed, open, passthrough as carried, Borrowed, DeclaredContext, Encrypted, + Encryption, IndexSpec, +}; +use stack_encrypt::{ + nonempty, CipherText, Error, Label, NonEmpty, SealedValue, StackCipher, StackCipherText, +}; use vitaminc_aead_frame::serde::from_slice; const SEED: [u8; 32] = *b"stack-encrypt record fixture v1 "; @@ -56,25 +73,40 @@ fn user() -> User { } } -/// The one declaration, as the Rust chain writes it over `ValueFrame` fields. -fn typed_plan() -> Plan<User, DeterministicSource> { - Plan::context("users") - .fields() - .encrypt_index( - pick("age", |u: &User| &u.age), - (IndexSpec::Equality, IndexSpec::Ore), - ) - .encrypt_index( - pick("email", |u: &User| &u.email), - ( +/// What the combinators write for a user: `age` and `email` each sealed +/// with two terms beside it, `notes` sealed alone, and `id` as it is. +type TypedUser = ( + ( + ( + Encrypted<(TermBytes, TermBytes)>, + Encrypted<(TermBytes, TermBytes)>, + ), + StackCipherText, + ), + u32, +); + +/// The one declaration, as the combinators write it over `ValueFrame` +/// fields: each sealed field under `users/<field>`. +fn typed_declaration<'s>() -> Encryption<'s, User, TypedUser, DeterministicSource, DeclaredContext> +{ + indexed::<ValueFrame, _, Borrowed, _>((IndexSpec::Equality, IndexSpec::Ore)) + .under(nonempty!("users").with("age")) + .project(|u: &User| &u.age) + .zip( + indexed::<ValueFrame, _, Borrowed, _>(( IndexSpec::Equality, IndexSpec::Match(MatchOptions::default()), - ), + )) + .under(nonempty!("users").with("email")) + .project(|u: &User| &u.email), + ) + .zip( + ciphertext::<ValueFrame, _, Borrowed>() + .under(nonempty!("users").with("notes")) + .project(|u: &User| &u.notes), ) - .encrypt(pick("notes", |u: &User| &u.notes)) - .passthrough(pick("id", |u: &User| &u.id)) - .build() - .expect("the typed plan builds") + .zip(carried::<u32, _, Borrowed, DeclaredContext>().project(|u: &User| &u.id)) } /// The same declaration as a binding sends it: the fixture's `plan`. @@ -254,13 +286,9 @@ fn record_of_json(record: &Json) -> StackCipherText { ) } -/// The typed chain's record, shaped as the stored tree, by hand: what a Go +/// The combinators' record, shaped as the stored tree, by hand: what a Go /// program assembles from the engine's standard outputs. -fn record_of_typed(mut values: FieldValues) -> StackCipherText { - let age: Encrypted<(TermBytes, TermBytes)> = values.take("age").expect("age"); - let email: Encrypted<(TermBytes, TermBytes)> = values.take("email").expect("email"); - let notes: StackCipherText = values.take("notes").expect("notes"); - let id: u32 = values.take("id").expect("id"); +fn record_of_typed((((age, email), notes), id): TypedUser) -> StackCipherText { let term = |bytes: Vec<u8>| -> StackCipherText { passthrough(bytes) }; CipherText::Map(vec![ ( @@ -287,35 +315,66 @@ fn record_of_typed(mut values: FieldValues) -> StackCipherText { ]) } -/// A stored tree as the typed chain's record, for `open`: each sealed field -/// as its bare ciphertext, the passthrough as its value. -fn typed_of_record(tree: StackCipherText) -> FieldValues { - let CipherText::Map(fields) = tree else { - panic!("a record is a map"); - }; - let mut values = FieldValues::new(); - for (name, node) in fields { - let CipherText::Map(outputs) = node else { - panic!("a field is a map of outputs"); +/// What the combinators read of a stored tree: each sealed field's bare +/// ciphertext by name, and each passthrough's payload by name. The terms +/// are not read: a term opens nothing. +struct StoredFields { + sealed: BTreeMap<String, StackCipherText>, + carried: BTreeMap<String, Vec<u8>>, +} + +impl StoredFields { + fn of(tree: StackCipherText) -> Self { + let CipherText::Map(fields) = tree else { + panic!("a record is a map"); }; - for (key, node) in outputs { - match key.as_str() { - "c" => { - let _ = values.insert(&name, node); - } - "passthrough" => { - let CipherText::Passthrough(payload) = node else { - panic!("a passthrough node"); - }; - let id: u32 = from_slice(&payload.0).expect("a u32"); - assert_eq!(payload.0, common::frames::payload(id), "the id is a u32"); - let _ = values.insert(&name, id); + let mut stored = Self { + sealed: BTreeMap::new(), + carried: BTreeMap::new(), + }; + for (name, node) in fields { + let CipherText::Map(outputs) = node else { + panic!("a field is a map of outputs"); + }; + for (key, node) in outputs { + match key.as_str() { + "c" => { + let _ = stored.sealed.insert(name.clone(), node); + } + "passthrough" => { + let CipherText::Passthrough(payload) = node else { + panic!("a passthrough node"); + }; + let _ = stored.carried.insert(name.clone(), payload.0); + } + _ => {} } - _ => {} } } + stored + } + + /// The ciphertext of the sealed field `name`. + fn sealed(&mut self, name: &str) -> StackCipherText { + self.sealed.remove(name).expect(name) + } + + /// The passthrough field `name`, which must be a `u32` in its frame. + fn id(&mut self, name: &str) -> u32 { + let payload = self.carried.remove(name).expect(name); + let id: u32 = from_slice(&payload).expect("a u32"); + assert_eq!(payload, common::frames::payload(id), "the id is a u32"); + id + } + + /// The passthrough field `name`, which must be text in its frame. + fn text(&mut self, name: &str) -> ValueFrame { + let payload = self.carried.remove(name).expect(name); + let text = ValueFrame::from_bytes(Protected::new(payload).as_protected_ref().as_deref()) + .expect("a value"); + assert_eq!(text.kind(), Some(ValueKind::String), "{name} is text"); + text } - values } /// Every term of a record, by `field/key`, so two records' terms compare @@ -340,12 +399,12 @@ fn fixture_path() -> PathBuf { } async fn seal_typed(cipher: &StackCipher<DeterministicSource>) -> StackCipherText { - let values = cipher - .encrypt(&user()) - .using(&typed_plan()) + let sealed = cipher + .default_keyset() + .run(typed_declaration(), &user(), DeclaredContext::default()) .await - .expect("the typed chain seals"); - record_of_typed(values) + .expect("the combinators seal"); + record_of_typed(sealed) } async fn seal_lowered( @@ -362,19 +421,28 @@ async fn seal_lowered( .expect("the lowering seals") } +/// Open through the combinators: `open` for each sealed field under the +/// label the declaration seals it under, all in one request. async fn open_typed(cipher: &StackCipher<DeterministicSource>, tree: StackCipherText) -> Json { - let mut opened = cipher - .open(typed_of_record(tree)) - .using(&typed_plan()) + let mut stored = StoredFields::of(tree); + let opening = open::<ValueFrame, _>(stored.sealed("age"), nonempty!("users").with("age")) + .zip(open::<ValueFrame, _>( + stored.sealed("email"), + nonempty!("users").with("email"), + )) + .zip(open::<ValueFrame, _>( + stored.sealed("notes"), + nonempty!("users").with("notes"), + )); + let ((age, email), notes) = cipher + .run_decryption(opening) .await - .expect("the typed chain opens it"); - let mut value = |name: &str| json_of_opened(opened.take::<ValueFrame>(name).expect(name)); - let (age, email, notes) = (value("age"), value("email"), value("notes")); + .expect("the combinators open it"); json!({ - "age": age, - "email": email, - "notes": notes, - "id": opened.take::<u32>("id").expect("id"), + "age": json_of_opened(age), + "email": json_of_opened(email), + "notes": json_of_opened(notes), + "id": stored.id("id"), }) } @@ -391,7 +459,7 @@ async fn open_lowered( ) } -/// The typed chain and the lowering, run now under the same declaration: +/// The combinators and the lowering, run now under the same declaration: /// the same terms, and each opens the other's record. #[tokio::test] async fn the_chain_and_the_lowering_are_one_engine_today() { @@ -411,17 +479,19 @@ async fn the_chain_and_the_lowering_are_one_engine_today() { assert_eq!( open_lowered(&cipher, record_of_json(&typed), &plan).await, plaintext_json(), - "the lowering opens the chain's record" + "the lowering opens the combinators' record" ); assert_eq!( open_typed(&cipher, record_of_json(&lowered)).await, plaintext_json(), - "the chain opens the lowering's record" + "the combinators open the lowering's record" ); } /// The committed fixture: records each author sealed in an earlier run, -/// opened by the other now, their terms the ones derived now. With +/// opened by the other now, their terms the ones derived now. The +/// `typed_chain` record was sealed by the chain, and the combinators open +/// it; an update seals that record with the combinators. With /// `STACK_ENCRYPT_UPDATE_FIXTURES=1` the records are sealed afresh and /// written; the terms and the plaintext never change. Updates panic when /// `STACK_CONFORMANCE_REQUIRED=1`. @@ -436,6 +506,9 @@ async fn the_committed_fixture_opens_both_ways() { if update { let mut fixture = committed_fixture(&path); let entries = fixture.as_object_mut().expect("an object"); + // The comment and the `typed_chain` key are written as they are + // committed, so that an update changes the sealed bytes and nothing + // else in the file the Go module reads. let _ = entries.insert("_comment".into(), json!("Records sealed by the typed Rust chain and by the data-plan lowering under one declaration (ADR-0007). Read by tests/record_lowering.rs; the schema is in README.md beside this file. Regenerate with STACK_ENCRYPT_UPDATE_FIXTURES=1; do not edit by hand.")); let _ = entries.insert( "key_source".into(), @@ -494,12 +567,12 @@ async fn the_committed_fixture_opens_both_ways() { assert_eq!( open_typed(&cipher, record_of_json(lowered)).await, plaintext_json(), - "the chain opens the lowering's committed record" + "the combinators open the lowering's committed record" ); assert_eq!( open_typed(&cipher, record_of_json(typed)).await, plaintext_json(), - "and each opens its own" + "and each opens its own author's" ); assert_eq!( open_lowered(&cipher, record_of_json(lowered), &plan).await, @@ -542,15 +615,31 @@ fn note() -> Note { } } -/// The declaration as the Rust chain writes it: `context_field` in place -/// of `Plan::context(..)`. -fn note_plan() -> Plan<Note, DeterministicSource> { - Plan::fields() - .context_field(pick("tenant", |n: &Note| &n.tenant)) - .encrypt_index(pick("text", |n: &Note| &n.text), IndexSpec::Equality) - .passthrough(pick("id", |n: &Note| &n.id)) - .build() - .expect("the note plan builds") +/// What the combinators write for a note: the tenant as it is, `text` +/// sealed with its equality term beside it, and `id` as it is. +type TypedNote = ((ValueFrame, Encrypted<TermBytes>), u32); + +/// The label a note's `text` is sealed under: the context the note names, +/// then the field. +fn text_label(tenant: &ValueFrame) -> Label { + let context = tenant.into_label().expect("the tenant is a label"); + Label::new(context.segments().chain(["text"])).expect("a label") +} + +/// The declaration as the combinators write it for one note: the tenant +/// is carried as it is, and `text` runs under the label read from it. The +/// label is in the record, so the description is composed for each record. +fn note_declaration<'s>( + note: &Note, +) -> Encryption<'s, Note, TypedNote, DeterministicSource, DeclaredContext> { + carried::<ValueFrame, _, Borrowed, DeclaredContext>() + .project(|n: &Note| &n.tenant) + .zip( + indexed::<ValueFrame, _, Borrowed, _>(IndexSpec::Equality) + .under(NonEmpty::from(text_label(&note.tenant))) + .project(|n: &Note| &n.text), + ) + .zip(carried::<u32, _, Borrowed, DeclaredContext>().project(|n: &Note| &n.id)) } /// The same declaration as a binding sends it: the plan-level @@ -576,11 +665,8 @@ fn globex() -> Label { Label::parse("tenants/globex").expect("a label") } -/// The typed chain's note record, shaped as the stored tree. -fn record_of_note(mut values: FieldValues) -> StackCipherText { - let tenant: ValueFrame = values.take("tenant").expect("tenant"); - let text: Encrypted<TermBytes> = values.take("text").expect("text"); - let id: u32 = values.take("id").expect("id"); +/// The combinators' note record, shaped as the stored tree. +fn record_of_note(((tenant, text), id): TypedNote) -> StackCipherText { CipherText::Map(vec![ ( "tenant".into(), @@ -600,57 +686,14 @@ fn record_of_note(mut values: FieldValues) -> StackCipherText { ]) } -/// A stored tree as the typed chain's note record, for `open`. -fn note_of_record(tree: StackCipherText) -> FieldValues { - let CipherText::Map(fields) = tree else { - panic!("a record is a map"); - }; - let mut values = FieldValues::new(); - for (name, node) in fields { - let CipherText::Map(outputs) = node else { - panic!("a field is a map of outputs"); - }; - for (key, node) in outputs { - match key.as_str() { - "c" => { - let _ = values.insert(&name, node); - } - "passthrough" => { - let CipherText::Passthrough(payload) = node else { - panic!("a passthrough node"); - }; - // The id is a u32; the tenant is text, kept as its frame. - match from_slice::<u32>(&payload.0) { - Ok(id) => { - assert_eq!(payload.0, common::frames::payload(id), "the id is a u32"); - let _ = values.insert(&name, id); - } - Err(_) => { - let text = ValueFrame::from_bytes( - Protected::new(payload.0.clone()) - .as_protected_ref() - .as_deref(), - ) - .expect("a value"); - assert_eq!(text.kind(), Some(ValueKind::String), "the tenant is text"); - let _ = values.insert(&name, text); - } - } - } - _ => {} - } - } - } - values -} - async fn seal_note_typed(cipher: &StackCipher<DeterministicSource>) -> StackCipherText { + let note = note(); record_of_note( cipher - .encrypt(&note()) - .using(&note_plan()) + .default_keyset() + .run(note_declaration(&note), &note, DeclaredContext::default()) .await - .expect("the typed chain seals the note"), + .expect("the combinators seal the note"), ) } @@ -668,20 +711,33 @@ async fn seal_note_lowered( .expect("the lowering seals the note") } -/// Open through the typed chain, under the context `expected` if given. +/// Open through the combinators: `open` for `text`, under the context +/// `named` if given, or else under the context the record stores. The +/// combinators compare no stored context with an expected one, as a plan +/// does. A reader that names a context opens the field under it, and the +/// key source refuses a leaf that was sealed under another. async fn open_note_typed( cipher: &StackCipher<DeterministicSource>, tree: StackCipherText, - expected: Option<Label>, + named: Option<Label>, ) -> Result<Json, Error> { - let open = cipher.open(note_of_record(tree)); - let mut opened = match expected { - Some(expected) => open.context(expected).using(&note_plan()).await?, - None => open.using(&note_plan()).await?, + let mut stored = StoredFields::of(tree); + let tenant = stored.text("tenant"); + let label = match named { + Some(context) => Label::new(context.segments().chain(["text"])).expect("a label"), + None => text_label(&tenant), }; - let mut value = |name: &str| json_of_opened(opened.take::<ValueFrame>(name).expect(name)); - let (tenant, text) = (value("tenant"), value("text")); - Ok(json!({ "tenant": tenant, "text": text, "id": opened.take::<u32>("id").expect("id") })) + let text = cipher + .run_decryption(open::<ValueFrame, _>( + stored.sealed("text"), + NonEmpty::from(label), + )) + .await?; + Ok(json!({ + "tenant": json_of_opened(tenant), + "text": json_of_opened(text), + "id": stored.id("id"), + })) } async fn open_note_lowered( @@ -697,9 +753,12 @@ async fn open_note_lowered( )) } -/// The two authors under a context field, run now: the same term, each -/// opens the other's record under the context it stores, and each refuses -/// the other's record under another context before any key is requested. +/// The two authors under a context field, run now: the same term, and each +/// opens the other's record under the context it stores. Neither opens the +/// other's record under another context. The lowering compares the stored +/// context with the expected one and refuses before any key is requested. +/// The combinators ask for the key under the context the reader names, and +/// the key source refuses. #[tokio::test] async fn the_chain_and_the_lowering_agree_on_a_context_field_today() { let cipher = deterministic_cipher(SEED).await; @@ -724,13 +783,13 @@ async fn the_chain_and_the_lowering_agree_on_a_context_field_today() { assert_eq!( open_note_lowered(&cipher, record_of_json(&typed), &plan, expected.clone()) .await - .expect("the lowering opens the chain's note"), + .expect("the lowering opens the combinators' note"), note_json() ); assert_eq!( open_note_typed(&cipher, record_of_json(&lowered), expected) .await - .expect("the chain opens the lowering's note"), + .expect("the combinators open the lowering's note"), note_json() ); } @@ -740,12 +799,14 @@ async fn the_chain_and_the_lowering_agree_on_a_context_field_today() { )); assert!(matches!( open_note_typed(&cipher, record_of_json(&lowered), Some(globex())).await, - Err(Error::ContextMismatch { .. }) + Err(Error::Kms(_)) )); } /// The committed context-field case, under the fixture's `context_field` /// key: sealed by each author in an earlier run, opened by the other now. +/// The `typed_chain` note was sealed by the chain, and an update seals it +/// with the combinators. /// With `STACK_ENCRYPT_UPDATE_FIXTURES=1` this test rewrites that key and /// nothing else in the file. Updates panic when `STACK_CONFORMANCE_REQUIRED=1`. #[tokio::test] @@ -806,7 +867,7 @@ async fn the_committed_context_field_case_opens_both_ways() { assert_eq!( open_note_typed(&cipher, record_of_json(lowered), Some(acme())) .await - .expect("the chain opens the lowering's committed note"), + .expect("the combinators open the lowering's committed note"), note_json() ); assert!(matches!( @@ -815,7 +876,7 @@ async fn the_committed_context_field_case_opens_both_ways() { )); assert!(matches!( open_note_typed(&cipher, record_of_json(lowered), Some(globex())).await, - Err(Error::ContextMismatch { .. }) + Err(Error::Kms(_)) )); } diff --git a/packages/stack-encrypt/tests/scopes_and_bindings.rs b/packages/stack-encrypt/tests/scopes_and_bindings.rs index a750d5b043..3bd6e3a340 100644 --- a/packages/stack-encrypt/tests/scopes_and_bindings.rs +++ b/packages/stack-encrypt/tests/scopes_and_bindings.rs @@ -1,5 +1,5 @@ -//! Scopes and bindings on a derived record (#1487): the context scenarios -//! 2 to 7 of the Stack Encrypt API plan. +//! Scopes and bindings on a derived record: the context scenarios 2 to 7 +//! of the Stack Encrypt API plan. //! //! A scope is a part a query can supply too, such as a tenant. A binding //! ties a ciphertext to one row, such as a row ID. Search terms derive @@ -524,8 +524,8 @@ fn a_record_with_no_binding_has_one_context_per_field() { /// its binding and under nothing less. mod a_bound_json_document { use super::*; - use stack_encrypt::json::{JsonDocument, JsonPath}; - use stack_encrypt::target::{CallerContext, DecryptInto, EncryptFrom}; + use stack_encrypt::json::{JsonDocument, JsonPath, JsonPlaintext}; + use stack_encrypt::target::CallerContext; fn profile() -> stack_encrypt::NonEmpty<(&'static str, &'static str)> { nonempty!("users").with("profile") @@ -537,11 +537,8 @@ mod a_bound_json_document { let keyset = cipher.default_keyset(); let value = serde_json::json!({"hello": "world"}); let bound = CallerContext::from(Scope::new(42u64).bind(9u128)).extend(profile()); - let sealing = keyset.run( - <JsonDocument as EncryptFrom<serde_json::Value>>::encryption::<FakeDataKeySource>(), - &value, - bound.clone(), - ); + let sealing = JsonPlaintext::from(value.clone()) + .encrypt_into_with_context::<JsonDocument, _>(&keyset, bound.clone()); assert_eq!( sealing.context().to_string(), "query context: (users / profile) / 42u64\n\ @@ -575,25 +572,13 @@ mod a_bound_json_document { ); // The document opens under its binding, and not under the scopes. - let opened: serde_json::Value = keyset - .run_decryption( - <JsonDocument as DecryptInto<serde_json::Value>>::decryption::<FakeDataKeySource>( - document.clone(), - bound.into(), - ), - ) + let opened = JsonPlaintext::decrypt_from_with_context(document.clone(), &keyset, bound) .await .unwrap(); - assert_eq!(opened, value); + assert_eq!(opened.value(), &value); let scopes_alone = CallerContext::from(Scope::new(42u64)).extend(profile()); - let refused = keyset - .run_decryption( - <JsonDocument as DecryptInto<serde_json::Value>>::decryption::<FakeDataKeySource>( - document, - scopes_alone.into(), - ), - ) - .await; + let refused = + JsonPlaintext::decrypt_from_with_context(document, &keyset, scopes_alone).await; assert!(refused.is_err(), "the binding left out: {refused:?}"); } @@ -616,85 +601,96 @@ mod a_bound_json_document { .run(json_document::<_, Borrowed>(options), &value, bound.clone()) .await .unwrap(); - let opened: serde_json::Value = keyset - .run_decryption( - <JsonDocument as DecryptInto<serde_json::Value>>::decryption::<FakeDataKeySource>( - document.clone(), - bound.into(), - ), - ) + let opened = JsonPlaintext::decrypt_from_with_context(document.clone(), &keyset, bound) .await .unwrap(); - assert_eq!(opened, value); + assert_eq!(opened.value(), &value); let scopes_alone = CallerContext::from(Scope::new(42u64)).extend(profile()); - let refused = keyset - .run_decryption( - <JsonDocument as DecryptInto<serde_json::Value>>::decryption::<FakeDataKeySource>( - document, - scopes_alone.into(), - ), - ) - .await; + let refused = + JsonPlaintext::decrypt_from_with_context(document, &keyset, scopes_alone).await; assert!(refused.is_err(), "the binding left out: {refused:?}"); } } -/// A query carries no binding. A bare `Bind` does not compile -/// (`tests/ui/bind_in_query.rs`); a binding that reaches the chain inside -/// a `CallerContext` or a `DeclaredContext` is refused when the query is -/// awaited, before any key is requested, rather than dropped. -mod a_binding_in_a_query_chain { +/// A query carries no binding. A typed query cannot be given one: a marker +/// takes the scope its record lists and nothing else +/// (`tests/ui/marker_query_has_no_bind.rs`). A query from a plan that is +/// data takes its context as a value. A binding in that value is refused +/// when the query is awaited, before any key is requested, rather than +/// dropped. +#[cfg(feature = "dynamic")] +mod a_binding_in_a_query { use super::*; - use stack_encrypt::target::{CallerContext, DeclaredContext}; - use stack_encrypt::{op, Equality, Plan, PlanError}; + use common::frames::frame; + use stack_encrypt::dynamic::{record, Op, Operand, Planned, QueryTerm}; + use stack_encrypt::target::DeclaredContext; + use stack_encrypt::{AsProtectedRef, PlanError}; + use std::sync::atomic::Ordering; + + /// A record whose caller supplies its tenant and its row. + #[derive(EncryptTarget, DecryptSource)] + #[stash(struct = User, scope = "users", scope(tenant: u64), bind(row: u128))] + struct TenantRowBound { + email: Encrypted<EqualityTerm>, + } + + /// The data plan of the same column: `email` under `("users", "email")`, + /// with its ciphertext and an equality term. + fn plan() -> record::Plan { + let declaration = vitaminc_aead_frame::serde::to_vec(&serde_json::json!({ + "email": { "context": ["users", "email"], "outputs": ["c", "eq"], "type": "string" }, + })) + .unwrap(); + record::plan(declaration.as_protected_ref()).unwrap() + } #[tokio::test] async fn is_refused_before_any_key_is_requested() { - let cipher = cipher().await; - let plan = Plan::context("users/email") - .with::<String, _>(Equality) - .build() - .unwrap(); - let bound = || CallerContext::from(Scope::new(7u64).bind(9u128)); - let refused = cipher - .query(op::Eq, "ada@example.com") - .extend(bound()) - .on(&plan) + let (cipher, generates, retrieves) = common::counting_cipher().await; + let keyset = cipher.default_keyset(); + let plan = plan(); + let by_email = plan.query("email", Op::Eq).unwrap(); + let refused = Planned::new(&by_email, frame("ada@example.com")) + .encrypt_into_with_context::<QueryTerm, _>(&keyset, Scope::new(7u64).bind(9u128)) .await; assert!( matches!(refused, Err(Error::Plan(PlanError::BindInQuery))), "{refused:?}" ); - let refused = cipher - .query(op::Eq, "ada@example.com") - .extend(DeclaredContext::default().with(Bind::new(9u128))) - .on(&plan) + let refused = plan + .field("email") + .unwrap() + .eq(frame("ada@example.com")) + .unwrap() + .scope(DeclaredContext::default().with(Bind::new(9u128))) + .encrypt(&keyset) .await; assert!( matches!(refused, Err(Error::Plan(PlanError::BindInQuery))), "{refused:?}" ); + assert_eq!(generates.load(Ordering::SeqCst), 0); + assert_eq!(retrieves.load(Ordering::SeqCst), 0); assert_eq!( PlanError::BindInQuery.to_string(), "a query takes no binding: a binding is in the stored context only" ); - // The same context seals a write through the plan, and the scopes - // alone derive the stored term: the row is found by its tenant. - let keyset = cipher.default_keyset(); - let stored = cipher - .encrypt(&String::from("ada@example.com")) - .using(&plan) - .extend(bound()) - .await - .unwrap(); - let query = cipher - .query(op::Eq, "ada@example.com") - .extend(Scope::new(7u64)) - .on(&plan) + // The same context seals a write, and the scopes alone derive the + // stored term: the row is found by its tenant. + let stored: TenantRowBound = User { + email: "ada@example.com".into(), + } + .encrypt_into_with_context(&keyset, Scope::new(7u64).bind(9u128)) + .await + .unwrap(); + let query: QueryTerm = Planned::new(&by_email, frame("ada@example.com")) + .encrypt_into_with_context(&keyset, Scope::new(7u64)) .await .unwrap(); - assert_eq!(stored.terms, query); - let _ = keyset; + assert_eq!( + query.operand(), + &Operand::One(stored.email.terms.as_bytes().to_vec()) + ); } } diff --git a/packages/stack-encrypt/tests/source_mode.rs b/packages/stack-encrypt/tests/source_mode.rs index 1d7e3b2692..7eab41345f 100644 --- a/packages/stack-encrypt/tests/source_mode.rs +++ b/packages/stack-encrypt/tests/source_mode.rs @@ -1,10 +1,10 @@ //! How a description receives its plaintext (`target::{Borrowed, Owned}`). //! //! Vitamin C's by-value `Encrypt` walk and `PrfValue` consume their input. A description -//! handed a borrow (the default, and what every `EncryptFrom` declaration -//! runs in) clones before an operation consumes; one handed the value -//! (`Owned`) gives it to the operation. These tests hold the two claims that -//! make owned mode worth having: +//! handed a borrow (the default, and what every `EncryptTarget::encryption` +//! declaration runs in) clones before an operation consumes; one handed the +//! value (`Owned`) gives it to the operation. These tests hold the two claims +//! that make owned mode worth having: //! //! - a plaintext that is **not `Clone`** runs a single operation — a //! ciphertext alone, or one term alone — and the output is the same as the @@ -21,7 +21,7 @@ use stack_encrypt::sem::{EqualityTerm, MatchTerms}; use stack_encrypt::target::{ ciphertext, equality, matching, AeadContext, Borrowed, CallerContext, Encryption, Owned, }; -use stack_encrypt::{nonempty, Encrypt, NonEmpty, StackCipherText}; +use stack_encrypt::{nonempty, DecryptFrom, Encrypt, NonEmpty, StackCipherText}; use stack_kms::FakeDataKeySource; use vitaminc_aead::plaintext::Sink; use vitaminc_prf::{IntoPrfContext, Prf, PrfValue, PrfVisitor}; @@ -132,7 +132,9 @@ async fn a_plaintext_that_is_not_clone_seals_through_ciphertext_alone() { .await .unwrap(); - let opened: String = cipher.decrypt_as(sealed, aead()).await.unwrap(); + let opened: String = String::decrypt_from_with_context(sealed, &cipher, aead()) + .await + .unwrap(); assert_eq!( opened, NUMBER, "an owned, non-Clone plaintext should seal to a ciphertext that opens to its text" @@ -192,7 +194,9 @@ async fn a_value_frame_seals_by_value_through_ciphertext_alone() { .await .unwrap(); - let opened: String = cipher.decrypt_as(sealed, aead()).await.unwrap(); + let opened: String = String::decrypt_from_with_context(sealed, &cipher, aead()) + .await + .unwrap(); assert_eq!( opened, NUMBER, "a value frame should seal by value and open to the same text" @@ -248,7 +252,9 @@ async fn an_owned_zip_copies_for_every_side_but_the_last() { ); // And what each side made is what it would have made alone. - let opened: String = cipher.decrypt_as(sealed, aead()).await.unwrap(); + let opened: String = String::decrypt_from_with_context(sealed, &cipher, aead()) + .await + .unwrap(); assert_eq!(opened, NUMBER, "the zipped ciphertext opens to the text"); let expected = keyset.equality_term(NUMBER, context()).await.unwrap(); assert_eq!(term, expected, "the zipped term is the text's term"); @@ -275,7 +281,9 @@ async fn a_borrowed_operation_copies_once_for_each_operation_that_consumes() { .await .unwrap(); assert_eq!(clones.load(Ordering::SeqCst), 1, "a borrowed ciphertext"); - let opened: String = cipher.decrypt_as(sealed, aead()).await.unwrap(); + let opened: String = String::decrypt_from_with_context(sealed, &cipher, aead()) + .await + .unwrap(); assert_eq!(opened, NUMBER, "the borrowed ciphertext opens to the text"); let (value, clones) = Counted::new(NUMBER); @@ -385,7 +393,9 @@ async fn an_owned_plaintext_is_dropped_before_its_key_request_is_sent() { 1, "ciphertext: dropped exactly once" ); - let opened: String = cipher.decrypt_as(sealed, aead()).await.unwrap(); + let opened: String = String::decrypt_from_with_context(sealed, &cipher, aead()) + .await + .unwrap(); assert_eq!( opened, NUMBER, "the text was sealed before the husk dropped" diff --git a/packages/stack-encrypt/tests/target.rs b/packages/stack-encrypt/tests/target.rs index a60bce3e00..5c61a66dcb 100644 --- a/packages/stack-encrypt/tests/target.rs +++ b/packages/stack-encrypt/tests/target.rs @@ -1,8 +1,10 @@ -//! Target-directed encryption tests: leaf `EncryptFrom`/`DecryptInto` -//! implementations, a hand-written composite record (the shape a future -//! derive will emit), a "third-party" term type built on the public extension -//! surface only, and — the point of the design — proof that however large the -//! assembly, settling it is one batched ZeroKMS call per request kind. +//! Target-directed encryption tests, through the owned entry points +//! (`EncryptInto` and `DecryptFrom`): the leaf targets' `EncryptTarget` and +//! `DecryptSource` declarations, a hand-written composite record, a +//! "third-party" term type built on the public extension surface only, and +//! hand-written trees that give a subtree a context of its own. The point of +//! the design is proved here too: however large the assembly, settling it +//! is one batched ZeroKMS call per request kind. use std::cmp::Ordering; use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; @@ -10,12 +12,15 @@ use std::sync::Arc; use stack_encrypt::sem::{EqualityTerm, MatchConfig, MatchOptions, MatchTerms, OreTerm}; use stack_encrypt::target::{ - ciphertext, equality, CallerContext, DeclaredContext, DecryptInto, Decryption, EncryptFrom, - Encryption, Pending, Request, + ciphertext, equality, CallerContext, DeclaredContext, DecryptSource, Decryption, EncryptTarget, + Encryption, NoCallerContext, Pending, Request, RequiredContext, +}; +use stack_encrypt::{ + nonempty, DecryptFrom, EmptyError, EncryptInto, Error, NonEmpty, StackCipher, StackCipherText, }; -use stack_encrypt::{nonempty, EmptyError, Error, NonEmpty, StackCipher, StackCipherText}; use stack_kms::{FakeDataKeySource, IdentifiedBy, IndexKeySource}; use uuid::Uuid; +use zeroize::Zeroize; mod common; use common::{counting_cipher, stack_cipher}; @@ -35,8 +40,9 @@ async fn equality_leaf_agrees_with_the_descriptor_api() { let generator = generator().await; let generator = generator.default_keyset(); - let via_target: EqualityTerm = generator - .encrypt_as(&"alice", nonempty!("users/email").into()) + let via_target: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/email")) .await .unwrap(); let via_descriptor = generator @@ -60,22 +66,24 @@ async fn terms_agree_between_independently_built_ciphers() { let generator = generator().await; let generator = generator.default_keyset(); - let a: EqualityTerm = cipher - .encrypt_as(&"alice", nonempty!("users/email").into()) + let a: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&cipher, nonempty!("users/email")) .await .unwrap(); - let b: EqualityTerm = generator - .encrypt_as(&"alice", nonempty!("users/email").into()) + let b: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/email")) .await .unwrap(); assert_eq!(a, b); - let a: OreTerm<u64> = cipher - .encrypt_as(&7u64, nonempty!("users/n").into()) + let a: OreTerm<u64> = 7u64 + .encrypt_into_with_context(&cipher, nonempty!("users/n")) .await .unwrap(); - let b: OreTerm<u64> = generator - .encrypt_as(&7u64, nonempty!("users/n").into()) + let b: OreTerm<u64> = 7u64 + .encrypt_into_with_context(&generator, nonempty!("users/n")) .await .unwrap(); assert_eq!(a, b); @@ -86,12 +94,14 @@ async fn equality_leaf_binds_the_context() { let generator = generator().await; let generator = generator.default_keyset(); - let email: EqualityTerm = generator - .encrypt_as(&"alice", nonempty!("users/email").into()) + let email: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/email")) .await .unwrap(); - let name: EqualityTerm = generator - .encrypt_as(&"alice", nonempty!("users/name").into()) + let name: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/name")) .await .unwrap(); @@ -108,12 +118,13 @@ async fn term_derivation_makes_no_kms_calls() { let (cipher, generates, retrieves) = counting_cipher().await; let cipher = cipher.default_keyset(); - let _term: EqualityTerm = cipher - .encrypt_as(&"alice", nonempty!("users/email").into()) + let _term: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&cipher, nonempty!("users/email")) .await .unwrap(); - let _ore: OreTerm<u64> = cipher - .encrypt_as(&7u64, nonempty!("users/age").into()) + let _ore: OreTerm<u64> = 7u64 + .encrypt_into_with_context(&cipher, nonempty!("users/age")) .await .unwrap(); @@ -122,18 +133,19 @@ async fn term_derivation_makes_no_kms_calls() { } #[tokio::test] -async fn ciphertext_leaf_round_trips_via_decrypt_into() { +async fn ciphertext_leaf_round_trips_via_decrypt_from() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let ciphertext: StackCipherText = keyset - .encrypt_as(&"secret".to_string(), nonempty!("users/email").into()) - .await - .unwrap(); - let plaintext: String = cipher - .decrypt_as(ciphertext, nonempty!("users/email").into()) + let ciphertext: StackCipherText = "secret" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/email")) .await .unwrap(); + let plaintext: String = + String::decrypt_from_with_context(ciphertext, &cipher, nonempty!("users/email")) + .await + .unwrap(); assert_eq!(plaintext, "secret"); } @@ -143,14 +155,14 @@ async fn ciphertext_leaf_cannot_be_transplanted_to_another_context() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let ciphertext: StackCipherText = keyset - .encrypt_as(&"secret".to_string(), nonempty!("users/email").into()) + let ciphertext: StackCipherText = "secret" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/email")) .await .unwrap(); - let transplanted: Result<String, _> = cipher - .decrypt_as(ciphertext, nonempty!("users/name").into()) - .await; + let transplanted: Result<String, _> = + String::decrypt_from_with_context(ciphertext, &cipher, nonempty!("users/name")).await; assert!( transplanted.is_err(), "the context is bound into the AAD, so a ciphertext must not decrypt under another field's context" @@ -162,15 +174,14 @@ async fn match_leaf_supports_containment_queries() { let generator = generator().await; let generator = generator.default_keyset(); - let stored: MatchTerms = generator - .encrypt_as( - &"alice wonderland".to_string(), - nonempty!("users/bio").into(), - ) + let stored: MatchTerms = "alice wonderland" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/bio")) .await .unwrap(); - let query: MatchTerms = generator - .encrypt_as(&"wonder".to_string(), nonempty!("users/bio").into()) + let query: MatchTerms = "wonder" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/bio")) .await .unwrap(); @@ -207,22 +218,18 @@ async fn match_leaf_config_is_type_level() { let generator = generator().await; let generator = generator.default_keyset(); - let term: MatchTerms<SmallFilter> = generator - .encrypt_as( - &"a longer piece of text".to_string(), - nonempty!("users/bio").into(), - ) + let term: MatchTerms<SmallFilter> = "a longer piece of text" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/bio")) .await .unwrap(); assert!(term.positions().iter().all(|&p| u32::from(p) < 64)); // The same text under the default config is a different (larger-filter) // term — and a different type, so the two cannot be compared by mistake. - let default_term: MatchTerms = generator - .encrypt_as( - &"a longer piece of text".to_string(), - nonempty!("users/bio").into(), - ) + let default_term: MatchTerms = "a longer piece of text" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/bio")) .await .unwrap(); assert_ne!(term.positions(), default_term.positions()); @@ -233,20 +240,20 @@ async fn ore_leaf_preserves_order_and_binds_the_context() { let generator = generator().await; let generator = generator.default_keyset(); - let ten: OreTerm<u64> = generator - .encrypt_as(&10u64, nonempty!("users/age").into()) + let ten: OreTerm<u64> = 10u64 + .encrypt_into_with_context(&generator, nonempty!("users/age")) .await .unwrap(); - let ten_again: OreTerm<u64> = generator - .encrypt_as(&10u64, nonempty!("users/age").into()) + let ten_again: OreTerm<u64> = 10u64 + .encrypt_into_with_context(&generator, nonempty!("users/age")) .await .unwrap(); - let twenty: OreTerm<u64> = generator - .encrypt_as(&20u64, nonempty!("users/age").into()) + let twenty: OreTerm<u64> = 20u64 + .encrypt_into_with_context(&generator, nonempty!("users/age")) .await .unwrap(); - let other_field: OreTerm<u64> = generator - .encrypt_as(&10u64, nonempty!("users/height").into()) + let other_field: OreTerm<u64> = 10u64 + .encrypt_into_with_context(&generator, nonempty!("users/height")) .await .unwrap(); @@ -262,12 +269,12 @@ async fn ope_leaf_compares_with_plain_byte_order() { let generator = generator().await; let generator = generator.default_keyset(); - let ten: OpeTerm<u64> = generator - .encrypt_as(&10u64, nonempty!("users/age").into()) + let ten: OpeTerm<u64> = 10u64 + .encrypt_into_with_context(&generator, nonempty!("users/age")) .await .unwrap(); - let twenty: OpeTerm<u64> = generator - .encrypt_as(&20u64, nonempty!("users/age").into()) + let twenty: OpeTerm<u64> = 20u64 + .encrypt_into_with_context(&generator, nonempty!("users/age")) .await .unwrap(); @@ -283,8 +290,8 @@ async fn a_column_encrypts_in_one_batched_call() { let cipher = cipher.default_keyset(); let ages: Vec<u32> = vec![29, 34, 41, 34, 57]; - let sealed: Vec<StackCipherText> = cipher - .encrypt_as(&ages, nonempty!("users/age").into()) + let sealed: Vec<StackCipherText> = ages + .encrypt_into_with_context(&cipher, nonempty!("users/age")) .await .unwrap(); @@ -302,15 +309,16 @@ async fn a_column_decrypts_in_one_batched_call() { let keyset = cipher.default_keyset(); let ages: Vec<u32> = vec![29, 34, 41]; - let sealed: Vec<StackCipherText> = keyset - .encrypt_as(&ages, nonempty!("users/age").into()) + let sealed: Vec<StackCipherText> = ages + .clone() + .encrypt_into_with_context(&keyset, nonempty!("users/age")) .await .unwrap(); - let roundtrip: Vec<u32> = cipher - .decrypt_as(sealed, nonempty!("users/age").into()) - .await - .unwrap(); + let roundtrip: Vec<u32> = + Vec::<u32>::decrypt_from_with_context(sealed, &cipher, nonempty!("users/age")) + .await + .unwrap(); assert_eq!(roundtrip, ages); assert_eq!( @@ -325,33 +333,30 @@ async fn optional_fields_encrypt_and_decrypt_structurally() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let present: Option<StackCipherText> = keyset - .encrypt_as( - &Some("here".to_string()), - nonempty!("users/nickname").into(), - ) + let present: Option<StackCipherText> = Some("here".to_string()) + .encrypt_into_with_context(&keyset, nonempty!("users/nickname")) .await .unwrap(); - let absent: Option<StackCipherText> = keyset - .encrypt_as(&Option::<String>::None, nonempty!("users/nickname").into()) + let absent: Option<StackCipherText> = Option::<String>::None + .encrypt_into_with_context(&keyset, nonempty!("users/nickname")) .await .unwrap(); assert!(present.is_some()); assert!(absent.is_none()); - let roundtrip: Option<String> = cipher - .decrypt_as(present, nonempty!("users/nickname").into()) - .await - .unwrap(); + let roundtrip: Option<String> = + Option::<String>::decrypt_from_with_context(present, &cipher, nonempty!("users/nickname")) + .await + .unwrap(); assert_eq!(roundtrip.as_deref(), Some("here")); } // --- A hand-written composite record ---------------------------------------- // -// The shape a `#[derive(EncryptFrom)]` will emit: one impl, pendings combined -// with zip/map (never awaited), one context fanning out to every field, the -// caller seeing a single await — and a single batched call. +// The shape a `plaintext = T` derive emits: one declaration, operations +// combined with zip/map (never awaited), one context fanning out to every +// field, the caller seeing a single await — and a single batched call. /// "An encrypted `u32`, stored as its ciphertext plus an equality term and an /// ORE term" — an EQL `integer_ord_ore`-shaped record, minus the EQL. @@ -361,26 +366,29 @@ struct EncryptedAge { ob: OreTerm<u32>, } -impl EncryptFrom<u32> for EncryptedAge { - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, u32, Self, K, Self::Context> +impl EncryptTarget<u32> for EncryptedAge { + type ContextInput = RequiredContext<CallerContext>; + fn encryption<'s, K: 'static>() -> Encryption<'s, u32, Self, K, Self::ContextInput> where u32: 's, { // One context reaches all three; there is no second one to pass. // The ciphertext seals under the AEAD half of the one context the - // terms derive under: `accepting` lets it take theirs. + // terms derive under: `accepting` lets it take theirs. The last + // `accepting` takes that one context out of the caller's required + // input. stack_encrypt::target::ciphertext() - .accepting() + .accepting::<CallerContext>() .zip(stack_encrypt::target::equality()) .zip(stack_encrypt::target::ore()) .map(|((c, hm), ob)| Self { c, hm, ob }) + .accepting() } } -impl DecryptInto<u32> for EncryptedAge { - type Context = CallerContext; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<u32, K> { - stack_encrypt::target::open(self.c, context) +impl DecryptSource<u32> for EncryptedAge { + type ContextInput = RequiredContext<CallerContext>; + fn decryption<K: 'static>(self, context: Self::ContextInput) -> Decryption<u32, K> { + stack_encrypt::target::open(self.c, context.into_inner()) } } @@ -391,32 +399,31 @@ async fn composite_record_encrypts_every_field_from_one_source() { let generator = generator().await; let generator = generator.default_keyset(); - let record: EncryptedAge = keyset - .encrypt_as(&42u32, nonempty!("users/age").into()) + let record: EncryptedAge = 42u32 + .encrypt_into_with_context(&keyset, nonempty!("users/age")) .await .unwrap(); // The ciphertext round-trips through the decrypt mirror. - let plaintext: u32 = cipher - .decrypt_as(record, nonempty!("users/age").into()) + let plaintext: u32 = u32::decrypt_from_with_context(record, &cipher, nonempty!("users/age")) .await .unwrap(); assert_eq!(plaintext, 42); // Each term matches what the primitive would derive on its own, so query // terms generated leaf-by-leaf find records encrypted as composites. - let record: EncryptedAge = keyset - .encrypt_as(&42u32, nonempty!("users/age").into()) + let record: EncryptedAge = 42u32 + .encrypt_into_with_context(&keyset, nonempty!("users/age")) .await .unwrap(); - let hm: EqualityTerm = generator - .encrypt_as(&42u32, nonempty!("users/age").into()) + let hm: EqualityTerm = 42u32 + .encrypt_into_with_context(&generator, nonempty!("users/age")) .await .unwrap(); assert_eq!(record.hm, hm); - let ob: OreTerm<u32> = generator - .encrypt_as(&42u32, nonempty!("users/age").into()) + let ob: OreTerm<u32> = 42u32 + .encrypt_into_with_context(&generator, nonempty!("users/age")) .await .unwrap(); assert_eq!(record.ob, ob); @@ -427,8 +434,8 @@ async fn a_composite_record_is_one_batched_call() { let (cipher, generates, _) = counting_cipher().await; let cipher = cipher.default_keyset(); - let _record: EncryptedAge = cipher - .encrypt_as(&42u32, nonempty!("users/age").into()) + let _record: EncryptedAge = 42u32 + .encrypt_into_with_context(&cipher, nonempty!("users/age")) .await .unwrap(); assert_eq!( @@ -439,8 +446,8 @@ async fn a_composite_record_is_one_batched_call() { // A whole column of records: still one call. let ages: Vec<u32> = vec![10, 20, 30]; - let _column: Vec<EncryptedAge> = cipher - .encrypt_as(&ages, nonempty!("users/age").into()) + let _column: Vec<EncryptedAge> = ages + .encrypt_into_with_context(&cipher, nonempty!("users/age")) .await .unwrap(); assert_eq!( @@ -455,12 +462,12 @@ async fn composite_record_terms_preserve_order() { let cipher = stack_cipher().await; let cipher = cipher.default_keyset(); - let ten: EncryptedAge = cipher - .encrypt_as(&10u32, nonempty!("users/age").into()) + let ten: EncryptedAge = 10u32 + .encrypt_into_with_context(&cipher, nonempty!("users/age")) .await .unwrap(); - let twenty: EncryptedAge = cipher - .encrypt_as(&20u32, nonempty!("users/age").into()) + let twenty: EncryptedAge = 20u32 + .encrypt_into_with_context(&cipher, nonempty!("users/age")) .await .unwrap(); @@ -471,28 +478,30 @@ async fn composite_record_terms_preserve_order() { // cryptographic implementation. Prefix tokenization would need a core operation. #[derive(Debug, PartialEq, Eq)] struct StoredEquality([u8; 32]); -impl<S> EncryptFrom<S> for StoredEquality +impl<S: Zeroize> EncryptTarget<S> for StoredEquality where - EqualityTerm: EncryptFrom<S>, + EqualityTerm: EncryptTarget<S>, { - type Context = <EqualityTerm as EncryptFrom<S>>::Context; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> + type ContextInput = <EqualityTerm as EncryptTarget<S>>::ContextInput; + fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput> where S: 's, { - <EqualityTerm as EncryptFrom<S>>::encryption().map(|term| Self(term.into_bytes())) + <EqualityTerm as EncryptTarget<S>>::encryption().map(|term| Self(term.into_bytes())) } } #[tokio::test] async fn third_party_output_wraps_a_core_term() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let stored: StoredEquality = keyset - .encrypt_as(&"alice", nonempty!("users/name").into()) + let stored: StoredEquality = "alice" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/name")) .await .unwrap(); - let canonical: EqualityTerm = keyset - .encrypt_as(&"alice", nonempty!("users/name").into()) + let canonical: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/name")) .await .unwrap(); assert_eq!( @@ -588,62 +597,58 @@ async fn wrapped_and_extended_contexts_bind_like_plain_ones() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let sealed: StackCipherText = keyset - .encrypt_as( - &"secret".to_string(), - NonEmpty::new(Some("users/email")).unwrap().into(), - ) - .await - .unwrap(); - let opened: String = cipher - .decrypt_as(sealed, NonEmpty::new(Some("users/email")).unwrap().into()) + let sealed: StackCipherText = "secret" + .to_string() + .encrypt_into_with_context(&keyset, NonEmpty::new(Some("users/email")).unwrap()) .await .unwrap(); + let opened: String = String::decrypt_from_with_context( + sealed, + &cipher, + NonEmpty::new(Some("users/email")).unwrap(), + ) + .await + .unwrap(); assert_eq!(opened, "secret"); // Extended with a row id: opens under the same pair, and only there. // (Against the fake key source the AEAD refuses; ZeroKMS refuses the // key retrieval under the other descriptor first, as `Error::Kms`.) - let sealed: StackCipherText = keyset - .encrypt_as( - &"secret".to_string(), - nonempty!("users/email").with(42u64).into(), - ) + let sealed: StackCipherText = "secret" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/email").with(42u64)) .await .unwrap(); - let wrong_row: Result<String, _> = cipher - .decrypt_as(sealed, nonempty!("users/email").with(43u64).into()) - .await; + let wrong_row: Result<String, _> = + String::decrypt_from_with_context(sealed, &cipher, nonempty!("users/email").with(43u64)) + .await; assert!(matches!(wrong_row, Err(Error::Aead))); - let sealed: StackCipherText = keyset - .encrypt_as( - &"secret".to_string(), - nonempty!("users/email").with(42u64).into(), - ) + let sealed: StackCipherText = "secret" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/email").with(42u64)) .await .unwrap(); - let no_row: Result<String, _> = cipher - .decrypt_as(sealed, nonempty!("users/email").into()) - .await; + let no_row: Result<String, _> = + String::decrypt_from_with_context(sealed, &cipher, nonempty!("users/email")).await; assert!(matches!(no_row, Err(Error::Aead))); // The pair encodes as the bare tuple would: a term under the extended // context equals one under the plain pair, so a query site need not // hold a `NonEmpty` head to probe. - let extended: EqualityTerm = keyset - .encrypt_as(&"alice", nonempty!("users/email").with(42u64).into()) + let extended: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/email").with(42u64)) .await .unwrap(); - let plain: EqualityTerm = keyset - .encrypt_as( - &"alice", - NonEmpty::new(("users/email", 42u64)).unwrap().into(), - ) + let plain: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&keyset, NonEmpty::new(("users/email", 42u64)).unwrap()) .await .unwrap(); assert_eq!(extended, plain); - let unextended: EqualityTerm = keyset - .encrypt_as(&"alice", nonempty!("users/email").into()) + let unextended: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/email")) .await .unwrap(); assert_ne!(extended, unextended); @@ -656,44 +661,54 @@ async fn a_bare_integer_is_a_context() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let sealed: StackCipherText = keyset - .encrypt_as(&"secret".to_string(), 42u64.into()) + let sealed: StackCipherText = "secret" + .to_string() + .encrypt_into_with_context(&keyset, 42u64) .await .unwrap(); - let opened: String = cipher - .decrypt_as(sealed, NonEmpty::from(42u64).into()) + let opened: String = String::decrypt_from_with_context(sealed, &cipher, NonEmpty::from(42u64)) .await .unwrap(); assert_eq!(opened, "secret"); - let term: EqualityTerm = keyset.encrypt_as(&"alice", 7u32.into()).await.unwrap(); - let other: EqualityTerm = keyset.encrypt_as(&"alice", 8u32.into()).await.unwrap(); + let term: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&keyset, 7u32) + .await + .unwrap(); + let other: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&keyset, 8u32) + .await + .unwrap(); assert_ne!(term, other); } #[tokio::test] async fn containers_pass_the_context_through_to_their_leaves() { // `Vec` and `Option` hand the context on untouched, and so hand on the - // obligation: a column of leaves is `EncryptFrom<_, _, Ctx>` only for a - // `NonEmpty<_>` (`tests/ui/leaf_without_context.rs`), a column of - // derived rows — records whose fields carry their own contexts — for - // `()` as well. An empty container derives nothing either way. + // obligation: their context input is the element target's, so a column + // of leaves requires a context as one leaf does. An empty container + // derives nothing either way. let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let none: Option<StackCipherText> = keyset - .encrypt_as(&None::<String>, nonempty!("users/x").into()) + let none: Option<StackCipherText> = None::<String> + .encrypt_into_with_context(&keyset, nonempty!("users/x")) .await .unwrap(); assert!(none.is_none()); - let empty: Vec<StackCipherText> = keyset - .encrypt_as(&Vec::<String>::new(), nonempty!("users/x").into()) + let empty: Vec<StackCipherText> = Vec::<String>::new() + .encrypt_into_with_context(&keyset, nonempty!("users/x")) .await .unwrap(); assert!(empty.is_empty()); - let empty: Vec<String> = cipher - .decrypt_as(Vec::<StackCipherText>::new(), nonempty!("users/x").into()) - .await - .unwrap(); + let empty: Vec<String> = Vec::<String>::decrypt_from_with_context( + Vec::<StackCipherText>::new(), + &cipher, + nonempty!("users/x"), + ) + .await + .unwrap(); assert!(empty.is_empty()); } @@ -701,9 +716,9 @@ async fn containers_pass_the_context_through_to_their_leaves() { /// the shape of a derived record whose stored context fails validation. /// Counts how many times it was asked to declare. struct Refused(Arc<AtomicUsize>); -impl DecryptInto<u32> for Refused { - type Context = CallerContext; - fn decryption<K: 'static>(self, _: Self::Context) -> Decryption<u32, K> { +impl DecryptSource<u32> for Refused { + type ContextInput = RequiredContext<CallerContext>; + fn decryption<K: 'static>(self, _: Self::ContextInput) -> Decryption<u32, K> { self.0.fetch_add(1, AtomicOrdering::SeqCst); Decryption::failed(Error::NotOpened) } @@ -718,9 +733,12 @@ async fn a_column_stops_declaring_at_the_first_refused_row() { // `Vec<T>` declares its rows lazily and the collection stops at the // first refusal: the rows after it are never asked, and nothing is // retrieved. - let result: Result<Vec<u32>, _> = cipher - .decrypt_as(column, CallerContext::from(nonempty!("users/age"))) - .await; + let result: Result<Vec<u32>, _> = Vec::<u32>::decrypt_from_with_context( + column, + &cipher, + CallerContext::from(nonempty!("users/age")), + ) + .await; assert!(matches!(result, Err(Error::NotOpened)), "{result:?}"); assert_eq!(declared.load(AtomicOrdering::SeqCst), 1); assert_eq!(retrieves.load(AtomicOrdering::SeqCst), 0); @@ -735,16 +753,21 @@ async fn a_failed_field_fails_the_record_before_any_kms_call() { let cipher = cipher.default_keyset(); let b = "b".to_string(); - let zipped = cipher - .encrypt_as::<_, MatchTerms<TooManyHashes>>(&"abc", nonempty!("users/x").into()) - .zip(cipher.encrypt_as::<_, StackCipherText>(&b, nonempty!("users/x").into())) + let zipped = "abc" + .to_string() + .encrypt_into_with_context::<MatchTerms<TooManyHashes>, _>(&cipher, nonempty!("users/x")) + .zip( + b.clone() + .encrypt_into_with_context::<StackCipherText, _>(&cipher, nonempty!("users/x")), + ) .await; assert!(matches!(zipped, Err(Error::Term(_)))); let column = Pending::all( &cipher, vec![ - cipher.encrypt_as::<_, StackCipherText>(&b, nonempty!("users/x").into()), + b.clone() + .encrypt_into_with_context::<StackCipherText, _>(&cipher, nonempty!("users/x")), Pending::failed(&cipher, Error::NotOpened), ], ) @@ -778,9 +801,13 @@ async fn pendings_from_two_ciphers_over_the_same_keyset_merge() { let v = "v".to_string(); let w = "w".to_string(); - let zipped = cipher_a - .encrypt_as::<_, StackCipherText>(&v, nonempty!("users/x").into()) - .zip(cipher_b.encrypt_as::<_, StackCipherText>(&w, nonempty!("users/x").into())) + let zipped = v + .clone() + .encrypt_into_with_context::<StackCipherText, _>(&cipher_a, nonempty!("users/x")) + .zip( + w.clone() + .encrypt_into_with_context::<StackCipherText, _>(&cipher_b, nonempty!("users/x")), + ) .await; assert!( zipped.is_ok(), @@ -791,8 +818,10 @@ async fn pendings_from_two_ciphers_over_the_same_keyset_merge() { let column = Pending::all( &cipher_a, vec![ - cipher_a.encrypt_as::<_, StackCipherText>(&v, nonempty!("users/x").into()), - cipher_b.encrypt_as::<_, StackCipherText>(&w, nonempty!("users/x").into()), + v.clone() + .encrypt_into_with_context::<StackCipherText, _>(&cipher_a, nonempty!("users/x")), + w.clone() + .encrypt_into_with_context::<StackCipherText, _>(&cipher_b, nonempty!("users/x")), ], ) .await; @@ -826,9 +855,13 @@ async fn pendings_from_two_ciphers_over_different_keysets_refuse_to_merge() { let v = "v".to_string(); let w = "w".to_string(); - let zipped = acme - .encrypt_as::<_, StackCipherText>(&v, nonempty!("users/x").into()) - .zip(globex.encrypt_as::<_, StackCipherText>(&w, nonempty!("users/x").into())) + let zipped = v + .clone() + .encrypt_into_with_context::<StackCipherText, _>(&acme, nonempty!("users/x")) + .zip( + w.clone() + .encrypt_into_with_context::<StackCipherText, _>(&globex, nonempty!("users/x")), + ) .await; assert!( matches!(zipped, Err(Error::KeysetMismatch { left, right }) @@ -869,22 +902,22 @@ async fn terms_rehydrate_from_persisted_parts() { let generator = generator().await; let generator = generator.default_keyset(); - let eq: EqualityTerm = generator - .encrypt_as(&"alice", nonempty!("users/email").into()) + let eq: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/email")) .await .unwrap(); let rehydrated = EqualityTerm::from_bytes(eq.clone().into_bytes()); assert_eq!(eq, rehydrated); - let stored: MatchTerms = generator - .encrypt_as( - &"alice wonderland".to_string(), - nonempty!("users/bio").into(), - ) + let stored: MatchTerms = "alice wonderland" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/bio")) .await .unwrap(); - let query: MatchTerms = generator - .encrypt_as(&"wonder".to_string(), nonempty!("users/bio").into()) + let query: MatchTerms = "wonder" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/bio")) .await .unwrap(); // Rehydrate from unsorted positions: from_positions normalises (and @@ -904,8 +937,9 @@ async fn pending_futures_are_send() { let handle = tokio::spawn(async move { let generator = generator.default_keyset(); - let term: EqualityTerm = generator - .encrypt_as(&"alice", nonempty!("users/email").into()) + let term: EqualityTerm = "alice" + .to_string() + .encrypt_into_with_context(&generator, nonempty!("users/email")) .await .unwrap(); term @@ -955,15 +989,16 @@ async fn cipher_directed_and_target_directed_ciphertexts_are_interchangeable() { .encrypt_tree(value.clone(), "users/tags") .await .unwrap(); - let via_target: Vec<String> = cipher - .decrypt_as(ct, nonempty!("users/tags").into()) - .await - .unwrap(); + let via_target: Vec<String> = + Vec::<String>::decrypt_from_with_context(ct, &cipher, nonempty!("users/tags")) + .await + .unwrap(); assert_eq!(via_target, value); // Sealed by the target-directed API, opened by the cipher-directed one. - let ct: StackCipherText = keyset - .encrypt_as(&value, nonempty!("users/tags").into()) + let ct: StackCipherText = value + .clone() + .encrypt_into_with_context(&keyset, nonempty!("users/tags")) .await .unwrap(); let via_cipher: Vec<String> = cipher.decrypt(ct, "users/tags").await.unwrap(); @@ -974,8 +1009,9 @@ async fn cipher_directed_and_target_directed_ciphertexts_are_interchangeable() { async fn cipher_directed_decrypt_rejects_a_transplanted_ciphertext() { let cipher = stack_cipher().await; let keyset = cipher.default_keyset(); - let ct: StackCipherText = keyset - .encrypt_as(&"secret".to_string(), nonempty!("users/email").into()) + let ct: StackCipherText = "secret" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/email")) .await .unwrap(); let result: Result<String, Error> = cipher.decrypt(ct, "users/name").await; @@ -997,12 +1033,10 @@ struct OwnedEmail { hm: EqualityTerm, } -impl EncryptFrom<String> for OwnedEmail { - type Context = DeclaredContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> - where - String: 's, - { +impl OwnedEmail { + /// The tree. It runs under a `DeclaredContext`: `()` leaves the tree's + /// own context as it is, and a context extends it. + fn tree<'s, K: 'static>() -> Encryption<'s, String, Self, K, DeclaredContext> { ciphertext() .accepting() .zip(equality()) @@ -1011,6 +1045,17 @@ impl EncryptFrom<String> for OwnedEmail { } } +/// As a target, the tree takes no context from its caller: it has its own. +impl EncryptTarget<String> for OwnedEmail { + type ContextInput = NoCallerContext; + fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::ContextInput> + where + String: 's, + { + Self::tree().accepting() + } +} + #[tokio::test] async fn under_gives_a_subtree_its_own_context_which_the_callers_extends() { let cipher = stack_cipher().await; @@ -1019,31 +1064,37 @@ async fn under_gives_a_subtree_its_own_context_which_the_callers_extends() { let generator = generator.default_keyset(); let email = "alice@example.com".to_string(); - // Under `()`: the own context as it is, for the ciphertext and the term - // alike. - let record: OwnedEmail = keyset.encrypt_as(&email, Default::default()).await.unwrap(); - let probe: EqualityTerm = generator - .encrypt_as(&email, nonempty!("users/email").into()) + // With no caller context: the own context as it is, for the ciphertext + // and the term alike. + let record: OwnedEmail = email.clone().encrypt_into(&keyset).await.unwrap(); + let probe: EqualityTerm = email + .clone() + .encrypt_into_with_context(&generator, nonempty!("users/email")) .await .unwrap(); assert_eq!(record.hm, probe); - let opened: String = cipher - .decrypt_as(record.c, nonempty!("users/email").into()) - .await - .unwrap(); + let opened: String = + String::decrypt_from_with_context(record.c, &cipher, nonempty!("users/email")) + .await + .unwrap(); assert_eq!(opened, email); // Under a caller's context: the own context extended by it, for both. - let record: OwnedEmail = keyset.encrypt_as(&email, 7u64.into()).await.unwrap(); - let probe: EqualityTerm = generator - .encrypt_as(&email, nonempty!("users/email").with(7u64).into()) + // The target takes no caller context, so the tree itself is run. + let record: OwnedEmail = keyset + .run(OwnedEmail::tree(), &email, DeclaredContext::from(7u64)) .await .unwrap(); - assert_eq!(record.hm, probe); - let opened: String = cipher - .decrypt_as(record.c, nonempty!("users/email").with(7u64).into()) + let probe: EqualityTerm = email + .clone() + .encrypt_into_with_context(&generator, nonempty!("users/email").with(7u64)) .await .unwrap(); + assert_eq!(record.hm, probe); + let opened: String = + String::decrypt_from_with_context(record.c, &cipher, nonempty!("users/email").with(7u64)) + .await + .unwrap(); assert_eq!(opened, email); } @@ -1055,16 +1106,17 @@ struct ShadowedEmail { hm: EqualityTerm, } -impl EncryptFrom<String> for ShadowedEmail { - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> +impl EncryptTarget<String> for ShadowedEmail { + type ContextInput = RequiredContext<CallerContext>; + fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::ContextInput> where String: 's, { ciphertext() - .extend(nonempty!("users/shadow")) + .extend::<CallerContext>(nonempty!("users/shadow")) .zip(equality()) .map(|(shadow, hm)| Self { shadow, hm }) + .accepting() } } @@ -1076,24 +1128,26 @@ async fn extend_gives_a_subtree_its_own_context_and_still_requires_the_callers() let generator = generator.default_keyset(); let email = "alice@example.com".to_string(); - let record: ShadowedEmail = keyset - .encrypt_as(&email, nonempty!("users").into()) + let record: ShadowedEmail = email + .clone() + .encrypt_into_with_context(&keyset, nonempty!("users")) .await .unwrap(); // The term is under the caller's context as it is ... - let probe: EqualityTerm = generator - .encrypt_as(&email, nonempty!("users").into()) + let probe: EqualityTerm = email + .clone() + .encrypt_into_with_context(&generator, nonempty!("users")) .await .unwrap(); assert_eq!(record.hm, probe); // ... and the ciphertext under its own, extended by the caller's. - let opened: String = cipher - .decrypt_as( - record.shadow, - nonempty!("users/shadow").with(nonempty!("users")).into(), - ) - .await - .unwrap(); + let opened: String = String::decrypt_from_with_context( + record.shadow, + &cipher, + nonempty!("users/shadow").with(nonempty!("users")), + ) + .await + .unwrap(); assert_eq!(opened, email); } @@ -1106,17 +1160,17 @@ struct TenantEmail { tenant: u64, } -impl EncryptFrom<String> for TenantEmail { - type Context = NonEmpty<u64>; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> +impl EncryptTarget<String> for TenantEmail { + type ContextInput = RequiredContext<NonEmpty<u64>>; + fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::ContextInput> where String: 's, { equality() - .accepting::<NonEmpty<u64>>() + .accepting::<RequiredContext<NonEmpty<u64>>>() .map_with_context(|hm, tenant| Self { hm, - tenant: tenant.into_inner(), + tenant: tenant.into_inner().into_inner(), }) } } @@ -1129,11 +1183,15 @@ async fn accepting_converts_the_declared_context_once_and_map_with_context_keeps let generator = generator.default_keyset(); let email = "alice@example.com".to_string(); - let record: TenantEmail = keyset - .encrypt_as(&email, NonEmpty::from(7u64)) + let record: TenantEmail = email + .clone() + .encrypt_into_with_context(&keyset, NonEmpty::from(7u64)) .await .unwrap(); assert_eq!(record.tenant, 7); - let probe: EqualityTerm = generator.encrypt_as(&email, 7u64.into()).await.unwrap(); + let probe: EqualityTerm = email + .encrypt_into_with_context(&generator, 7u64) + .await + .unwrap(); assert_eq!(record.hm, probe); } diff --git a/packages/stack-encrypt/tests/term_serde.rs b/packages/stack-encrypt/tests/term_serde.rs index b8341200aa..961baef018 100644 --- a/packages/stack-encrypt/tests/term_serde.rs +++ b/packages/stack-encrypt/tests/term_serde.rs @@ -6,7 +6,7 @@ use serde_json::{json, Value}; use stack_encrypt::nonempty; use stack_encrypt::sem::{OpeTerm, Orderable, OreBlockTerm, OreTerm, TermBytesError}; -use stack_encrypt::{KeysetCipher, StackCipher}; +use stack_encrypt::{EncryptInto, KeysetCipher, StackCipher}; use stack_kms::FakeDataKeySource; async fn cipher() -> StackCipher<FakeDataKeySource> { @@ -37,12 +37,13 @@ async fn block<T: Orderable>( async fn an_order_term_is_its_bytes_in_lowercase_hex() { let cipher = cipher().await; let keyset = cipher.default_keyset(); - let ore: OreTerm<u32> = keyset - .encrypt_as(&42u32, nonempty!("users/age").into()) + let ore: OreTerm<u32> = 42u32 + .encrypt_into_with_context(&keyset, nonempty!("users/age")) .await .expect("ore term"); - let ope: OpeTerm<String> = keyset - .encrypt_as(&"alice".to_string(), nonempty!("users/name").into()) + let ope: OpeTerm<String> = "alice" + .to_string() + .encrypt_into_with_context(&keyset, nonempty!("users/name")) .await .expect("ope term"); let fixed = block(&keyset, 42i64).await; diff --git a/packages/stack-encrypt/tests/transcode.rs b/packages/stack-encrypt/tests/transcode.rs index fa3114f115..c2fb90f786 100644 --- a/packages/stack-encrypt/tests/transcode.rs +++ b/packages/stack-encrypt/tests/transcode.rs @@ -3,14 +3,18 @@ mod common; use stack_encrypt::sem::EqualityTerm; use stack_encrypt::target::transcode::{MapReader, Reader, SequenceReader, Transcode, Visitor}; -use stack_encrypt::target::{self, CallerContext}; +use stack_encrypt::target::{ + self, AcceptsContext, AeadContext, Borrowed, DecryptSource, EncryptTarget, NoCallerContext, + RequiredContext, +}; use stack_encrypt::{ - nonempty, CipherText, ContextPiece, DecryptField, DecryptInto, Decryptable, Decryption, - Encrypt, EncryptFrom, Encryption, Error, IntoContext, MaybeEmpty, NonEmpty, PassthroughFrame, + nonempty, CipherText, ContextPiece, DecryptField, DecryptFrom, Decryptable, Decryption, + Encrypt, EncryptInto, Encryption, Error, IntoContext, MaybeEmpty, NonEmpty, PassthroughFrame, SealedValue, StackCipherText, }; use std::sync::atomic::Ordering; use vitaminc_aead::plaintext::Sink; +use zeroize::Zeroize; #[derive(Clone, Debug, PartialEq)] struct Identifier { @@ -51,38 +55,42 @@ impl Transcode for StoredLeaf { LeafVisitor } } -impl<S: Encrypt + Clone> EncryptFrom<S> for StoredLeaf { - type Context = CallerContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> +// A ciphertext alone: it seals, and derives no term, so it requires the AEAD +// context, as the canonical `StackCipherText` does. +impl<S: Encrypt + Clone + Zeroize> EncryptTarget<S> for StoredLeaf { + type ContextInput = RequiredContext<AeadContext>; + fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput> where S: 's, { - target::ciphertext().accepting().transcode() + target::ciphertext().transcode().accepting() } } -impl<P: stack_encrypt::Decrypt + Send + 'static> DecryptInto<P> for StoredLeaf { - type Context = CallerContext; - fn decryption<K: 'static>(self, context: Self::Context) -> Decryption<P, K> { +impl<P: stack_encrypt::Decrypt + Zeroize + Send + 'static> DecryptSource<P> for StoredLeaf { + type ContextInput = RequiredContext<AeadContext>; + fn decryption<K: 'static>(self, context: Self::ContextInput) -> Decryption<P, K> { match SealedValue::from_bytes(&self.0) { - Ok(leaf) => target::open(CipherText::Single(leaf), context), + Ok(leaf) => target::open(CipherText::Single(leaf), context.into_inner()), Err(error) => Decryption::failed(Error::Other(Box::new(error))), } } } +// What makes the leaf a field of a derived record: it says that it holds +// ciphertext, and it opens under the context the record hands its fields. impl Decryptable for StoredLeaf { const DECRYPTABLE: bool = true; } -impl<P, Ctx> DecryptField<P, Ctx> for StoredLeaf +impl<P: Zeroize + 'static, Ctx> DecryptField<P, Ctx> for StoredLeaf where - Self: DecryptInto<P>, - Ctx: Into<<Self as DecryptInto<P>>::Context>, + Self: DecryptSource<P>, + Ctx: Into<<Self as DecryptSource<P>>::ContextInput>, { fn decryption_field<K: 'static>(self, ctx: Ctx) -> Option<Decryption<P, K>> { Some(self.decryption(ctx.into())) } } -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String)] struct TextEq { #[stash(context_field)] @@ -106,8 +114,9 @@ async fn stored_identifier_supplies_every_operations_context() { let (cipher, generates, _) = common::counting_cipher().await; let keyset = cipher.default_keyset(); let email = "alice@example.com".to_owned(); - let record: TextEq = keyset - .encrypt_as(&email, Identifier::email()) + let record: TextEq = email + .clone() + .encrypt_into_with_context(&keyset, Identifier::email()) .await .unwrap(); assert_eq!( @@ -162,7 +171,7 @@ async fn canonical_ciphertext_opens_through_the_record() { hm: probe, v: 3, }; - let opened: String = cipher.decrypt_as(record, ().into()).await.unwrap(); + let opened: String = String::decrypt_from(record, &cipher).await.unwrap(); assert_eq!( opened, email, "a record assembled from canonical output opens under its stored identifier" @@ -174,14 +183,13 @@ async fn expected_identifier_is_checked_before_any_key_is_retrieved() { let (cipher, _, retrieves) = common::counting_cipher().await; let keyset = cipher.default_keyset(); let email = "alice@example.com".to_owned(); - let record: TextEq = keyset - .encrypt_as(&email, Identifier::email()) + let record: TextEq = email + .clone() + .encrypt_into_with_context(&keyset, Identifier::email()) .await .unwrap(); let before = retrieves.load(Ordering::SeqCst); - let result = cipher - .decrypt_as::<String, _>(record, other_column().into()) - .await; + let result = String::decrypt_from_with_context(record, &cipher, other_column()).await; assert!( matches!(result, Err(Error::ContextMismatch { .. })), "a stored identifier that differs from the expected one is a mismatch, got {result:?}" @@ -198,15 +206,14 @@ async fn empty_stored_identifier_is_refused_before_any_key_is_retrieved() { let (cipher, _, retrieves) = common::counting_cipher().await; let keyset = cipher.default_keyset(); let email = "alice@example.com".to_owned(); - let mut record: TextEq = keyset - .encrypt_as(&email, Identifier::email()) + let mut record: TextEq = email + .clone() + .encrypt_into_with_context(&keyset, Identifier::email()) .await .unwrap(); record.i.column.clear(); let before = retrieves.load(Ordering::SeqCst); - let result = cipher - .decrypt_as::<String, _>(record, Default::default()) - .await; + let result = String::decrypt_from(record, &cipher).await; assert!( result.is_err(), "a stored identifier is data, not a proof: an empty one fails validation" @@ -220,6 +227,11 @@ async fn empty_stored_identifier_is_refused_before_any_key_is_retrieved() { #[derive(Clone)] struct WithoutSerde(String); +impl Zeroize for WithoutSerde { + fn zeroize(&mut self) { + self.0.zeroize(); + } +} impl Encrypt for WithoutSerde { fn visit<S: Sink>(self, sink: S) -> Result<S::Ok, S::Error> { self.0.visit(sink) @@ -234,8 +246,8 @@ impl Encrypt for &WithoutSerde { async fn ciphertext_uses_plaintexts_native_contract_without_serde() { let cipher = common::stack_cipher().await; let keyset = cipher.default_keyset(); - let result: StoredLeaf = keyset - .encrypt_as(&WithoutSerde("native".into()), nonempty!("value").into()) + let result: StoredLeaf = WithoutSerde("native".into()) + .encrypt_into_with_context(&keyset, nonempty!("value")) .await .unwrap(); let opened: String = cipher @@ -250,8 +262,14 @@ async fn ciphertext_uses_plaintexts_native_contract_without_serde() { "a plaintext without Serde seals through its own Plaintext impl and opens canonically" ); let text = String::from("borrowed"); + // A `&str` is not `Zeroize`, so it is not the source of a target. The + // operation the target is built on still seals one, run directly. let result: StoredLeaf = keyset - .encrypt_as(&text.as_str(), nonempty!("value").into()) + .run( + target::ciphertext::<&str, _, Borrowed>().transcode(), + &text.as_str(), + AeadContext::from(nonempty!("value")), + ) .await .unwrap(); let opened: String = cipher @@ -269,8 +287,8 @@ async fn scalar_destination_refuses_a_sequence() { let cipher = common::stack_cipher().await; let keyset = cipher.default_keyset(); // A scalar destination must refuse a sequence rather than flatten or serialize it. - let result = keyset - .encrypt_as::<_, StoredLeaf>(&vec![1u32, 2], nonempty!("value").into()) + let result = vec![1u32, 2] + .encrypt_into_with_context::<StoredLeaf, _>(&keyset, nonempty!("value")) .await; assert!( matches!(result, Err(Error::UnsupportedShape)), @@ -280,16 +298,16 @@ async fn scalar_destination_refuses_a_sequence() { } // A target whose declaration carries every context it needs asks its caller -// for none: `Context = ()`. +// for none: `ContextInput = NoCallerContext`. struct FixedLeaf(StoredLeaf); -impl<S: Encrypt + Clone> EncryptFrom<S> for FixedLeaf { - type Context = (); - fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::Context> +impl<S: Encrypt + Clone + Zeroize> EncryptTarget<S> for FixedLeaf { + type ContextInput = NoCallerContext; + fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput> where S: 's, { - // The declaration names its own context with `under`; `()` then - // satisfies the `DeclaredContext` that leaves. + // The declaration names its own context with `under`; no caller + // context then satisfies the `DeclaredContext` that leaves. target::ciphertext() .transcode() .map(Self) @@ -297,19 +315,20 @@ impl<S: Encrypt + Clone> EncryptFrom<S> for FixedLeaf { .accepting() } } -impl<P: stack_encrypt::Decrypt + Send + 'static> DecryptInto<P> for FixedLeaf { - type Context = (); - fn decryption<K: 'static>(self, (): ()) -> Decryption<P, K> { - self.0.decryption(nonempty!("fixed/leaf").into()) +impl<P: stack_encrypt::Decrypt + Zeroize + Send + 'static> DecryptSource<P> for FixedLeaf { + type ContextInput = NoCallerContext; + fn decryption<K: 'static>(self, _: NoCallerContext) -> Decryption<P, K> { + self.0 + .decryption(RequiredContext::supplied(nonempty!("fixed/leaf").into())) } } #[tokio::test] -async fn unit_context_target_supplies_its_own_context() { +async fn a_target_that_takes_no_caller_context_supplies_its_own() { let cipher = common::stack_cipher().await; let keyset = cipher.default_keyset(); let text = String::from("fixed"); - let stored: FixedLeaf = keyset.encrypt_as(&text, ()).await.unwrap(); + let stored: FixedLeaf = text.clone().encrypt_into(&keyset).await.unwrap(); let opened: String = cipher .decrypt( CipherText::Single(SealedValue::from_bytes(&stored.0 .0).unwrap()), @@ -321,8 +340,8 @@ async fn unit_context_target_supplies_its_own_context() { opened, text, "the leaf was sealed under the context the declaration named, not one the caller gave" ); - let stored: FixedLeaf = keyset.encrypt_as(&text, ()).await.unwrap(); - let opened: String = cipher.decrypt_as(stored, ()).await.unwrap(); + let stored: FixedLeaf = text.clone().encrypt_into(&keyset).await.unwrap(); + let opened: String = String::decrypt_from(stored, &cipher).await.unwrap(); assert_eq!( opened, text, "and it opens back through the declaration alone" diff --git a/packages/stack-encrypt/tests/ui/aead_context_with_term.rs b/packages/stack-encrypt/tests/ui/aead_context_with_term.rs deleted file mode 100644 index 6ae7236240..0000000000 --- a/packages/stack-encrypt/tests/ui/aead_context_with_term.rs +++ /dev/null @@ -1,15 +0,0 @@ -//! An `AeadContext` is the context of a ciphertext-only record: nothing -//! converts it into the `CallerContext` a term's declaration wants, so a -//! record declaring it cannot hold a term. -use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::target::AeadContext; -use stack_encrypt::{EncryptFrom, StackCipherText}; - -#[derive(EncryptFrom)] -#[stash(plaintext = u32, context_type = AeadContext)] -struct Indexed { - c: StackCipherText, - hm: EqualityTerm, -} - -fn main() {} diff --git a/packages/stack-encrypt/tests/ui/aead_context_with_term.stderr b/packages/stack-encrypt/tests/ui/aead_context_with_term.stderr deleted file mode 100644 index 305aab0a4e..0000000000 --- a/packages/stack-encrypt/tests/ui/aead_context_with_term.stderr +++ /dev/null @@ -1,19 +0,0 @@ -error[E0277]: the trait bound `CallerContext: From<AeadContext>` is not satisfied - --> tests/ui/aead_context_with_term.rs:8:10 - | -8 | #[derive(EncryptFrom)] - | ^^^^^^^^^^^ the trait `From<AeadContext>` is not implemented for `CallerContext` - | - = help: the following other types implement trait `From<T>`: - `CallerContext` implements `From<&NonEmpty<T>>` - `CallerContext` implements `From<NonEmpty<T>>` - `CallerContext` implements `From<Parts<S, B>>` - `CallerContext` implements `From<RequiredContext<C>>` - `CallerContext` implements `From<i128>` - `CallerContext` implements `From<i16>` - `CallerContext` implements `From<i32>` - `CallerContext` implements `From<i64>` - and $N others - = note: required for `AeadContext` to implement `Into<CallerContext>` - = help: see issue #48214 - = note: this error originates in the derive macro `EncryptFrom` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/packages/stack-encrypt/tests/ui/bare_context.rs b/packages/stack-encrypt/tests/ui/bare_context.rs index 77bfae75a8..888966b1ac 100644 --- a/packages/stack-encrypt/tests/ui/bare_context.rs +++ b/packages/stack-encrypt/tests/ui/bare_context.rs @@ -1,53 +1,60 @@ //! A context converts from a `NonEmpty<_>`: a `nonempty!(..)` literal, a //! `NonEmpty::new(..)?` value, or a bare integer. It does not convert from //! anything unchecked. A `&str` is not a context until it has been checked, -//! so `"..".into()` does not compile, and the value never reaches a leaf. -//! This holds on every path to a leaf: a term, a struct record, a -//! plaintext-derived record, and a leaf opened directly. So the check cannot -//! be skipped by passing the raw value. +//! so passing one does not compile, and the value never reaches a leaf. +//! This holds on every path to a leaf: a term, a `plaintext` record, a +//! record that stores its context, and a leaf sealed or opened directly. So +//! the check cannot be skipped by passing the raw value. use stack_encrypt::sem::EqualityTerm; - -use stack_encrypt::{DecryptInto, EncryptFrom, KeysetCipher, StackCipher, StackCipherText}; +use stack_encrypt::target::{DecryptSource, EncryptTarget}; +use stack_encrypt::{DecryptFrom, EncryptInto, KeysetCipher, StackCipher, StackCipherText}; use stack_kms::FakeDataKeySource; -struct User { - email: String, -} - -#[derive(EncryptFrom, DecryptInto)] -#[stash(struct = User, scope = "users")] -struct EncryptedUser { - email: StackCipherText, -} - -#[derive(EncryptFrom)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32)] struct EncryptedAge { c: StackCipherText, hm: EqualityTerm, } -async fn encrypt(cipher: &KeysetCipher<'_, FakeDataKeySource>, user: User) { - let _term: EqualityTerm = cipher.encrypt_as(&"alice", "users/email".into()) +#[derive(EncryptTarget, DecryptSource)] +#[stash(plaintext = String)] +struct StoredEmail { + #[stash(context_field)] + column: &'static str, + c: StackCipherText, +} + +async fn encrypt(keyset: &KeysetCipher<'_, FakeDataKeySource>) { + let _term: EqualityTerm = String::from("alice") + .encrypt_into_with_context(keyset, "users/email") + .await + .unwrap(); + let _record: EncryptedAge = 42u32 + .encrypt_into_with_context(keyset, "users/age") .await .unwrap(); - let _row: EncryptedUser = cipher.encrypt_as(&user, "tenant/acme".into()) + let _stored: StoredEmail = String::from("alice") + .encrypt_into_with_context(keyset, "users/email") .await .unwrap(); - let _record: EncryptedAge = cipher.encrypt_as(&42u32, "users/age".into()) + let _leaf: StackCipherText = 42u32 + .encrypt_into_with_context(keyset, "users/age") .await .unwrap(); } async fn decrypt( cipher: &StackCipher<FakeDataKeySource>, - row: EncryptedUser, + record: EncryptedAge, sealed: StackCipherText, ) { - let _user = cipher.decrypt_as::<User, _>(row, "tenant/acme".into()) + let _age = u32::decrypt_from_with_context(record, cipher, "users/age") + .await + .unwrap(); + let _age = u32::decrypt_from_with_context(sealed, cipher, "users/age") .await .unwrap(); - let _age: u32 = cipher.decrypt_as(sealed, "users/age".into()).await.unwrap(); } fn main() {} diff --git a/packages/stack-encrypt/tests/ui/bare_context.stderr b/packages/stack-encrypt/tests/ui/bare_context.stderr index 8b8d3c40b9..7b4a11fe10 100644 --- a/packages/stack-encrypt/tests/ui/bare_context.stderr +++ b/packages/stack-encrypt/tests/ui/bare_context.stderr @@ -1,8 +1,10 @@ error[E0277]: the trait bound `CallerContext: From<&str>` is not satisfied - --> tests/ui/bare_context.rs:31:73 + --> tests/ui/bare_context.rs:30:44 | -31 | let _term: EqualityTerm = cipher.encrypt_as(&"alice", "users/email".into()) - | ^^^^ the trait `From<&str>` is not implemented for `CallerContext` +30 | .encrypt_into_with_context(keyset, "users/email") + | ------------------------- ^^^^^^^^^^^^^ the trait `From<&str>` is not implemented for `CallerContext` + | | + | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: `CallerContext` implements `From<&NonEmpty<T>>` @@ -15,30 +17,22 @@ error[E0277]: the trait bound `CallerContext: From<&str>` is not satisfied `CallerContext` implements `From<i64>` and $N others = note: required for `&str` to implement `Into<CallerContext>` - -error[E0277]: the trait bound `DeclaredContext: From<&str>` is not satisfied - --> tests/ui/bare_context.rs:34:70 - | -34 | let _row: EncryptedUser = cipher.encrypt_as(&user, "tenant/acme".into()) - | ^^^^ the trait `From<&str>` is not implemented for `DeclaredContext` +note: required by a bound in `encrypt_into_with_context` + --> src/target/conversion.rs | - = help: the following other types implement trait `From<T>`: - `DeclaredContext` implements `From<&NonEmpty<T>>` - `DeclaredContext` implements `From<()>` - `DeclaredContext` implements `From<CallerContext>` - `DeclaredContext` implements `From<DeclaredScopes>` - `DeclaredContext` implements `From<NoCallerContext>` - `DeclaredContext` implements `From<NonEmpty<T>>` - `DeclaredContext` implements `From<RequiredContext<Parts<S, B>>>` - `DeclaredContext` implements `From<i128>` - and $N others - = note: required for `&str` to implement `Into<DeclaredContext>` + | fn encrypt_into_with_context<'a, T, K: 'static>( + | ------------------------- required by a bound in this associated function +... + | context: impl Into<<T::ContextInput as AcceptsContext>::Value>, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `EncryptInto::encrypt_into_with_context` error[E0277]: the trait bound `CallerContext: From<&str>` is not satisfied - --> tests/ui/bare_context.rs:37:71 + --> tests/ui/bare_context.rs:34:44 | -37 | let _record: EncryptedAge = cipher.encrypt_as(&42u32, "users/age".into()) - | ^^^^ the trait `From<&str>` is not implemented for `CallerContext` +34 | .encrypt_into_with_context(keyset, "users/age") + | ------------------------- ^^^^^^^^^^^ the trait `From<&str>` is not implemented for `CallerContext` + | | + | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: `CallerContext` implements `From<&NonEmpty<T>>` @@ -51,30 +45,52 @@ error[E0277]: the trait bound `CallerContext: From<&str>` is not satisfied `CallerContext` implements `From<i64>` and $N others = note: required for `&str` to implement `Into<CallerContext>` +note: required by a bound in `encrypt_into_with_context` + --> src/target/conversion.rs + | + | fn encrypt_into_with_context<'a, T, K: 'static>( + | ------------------------- required by a bound in this associated function +... + | context: impl Into<<T::ContextInput as AcceptsContext>::Value>, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `EncryptInto::encrypt_into_with_context` -error[E0277]: the trait bound `DeclaredContext: From<&str>` is not satisfied - --> tests/ui/bare_context.rs:47:65 +error[E0277]: the trait bound `NonEmpty<&'static str>: From<&str>` is not satisfied + --> tests/ui/bare_context.rs:38:44 | -47 | let _user = cipher.decrypt_as::<User, _>(row, "tenant/acme".into()) - | ^^^^ the trait `From<&str>` is not implemented for `DeclaredContext` +38 | .encrypt_into_with_context(keyset, "users/email") + | ------------------------- ^^^^^^^^^^^^^ the trait `From<&str>` is not implemented for `NonEmpty<&'static str>` + | | + | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: - `DeclaredContext` implements `From<&NonEmpty<T>>` - `DeclaredContext` implements `From<()>` - `DeclaredContext` implements `From<CallerContext>` - `DeclaredContext` implements `From<DeclaredScopes>` - `DeclaredContext` implements `From<NoCallerContext>` - `DeclaredContext` implements `From<NonEmpty<T>>` - `DeclaredContext` implements `From<RequiredContext<Parts<S, B>>>` - `DeclaredContext` implements `From<i128>` + `NonEmpty<Label>` implements `From<Label>` + `NonEmpty<i128>` implements `From<i128>` + `NonEmpty<i16>` implements `From<i16>` + `NonEmpty<i32>` implements `From<i32>` + `NonEmpty<i64>` implements `From<i64>` + `NonEmpty<i8>` implements `From<i8>` + `NonEmpty<u128>` implements `From<u128>` + `NonEmpty<u16>` implements `From<u16>` and $N others - = note: required for `&str` to implement `Into<DeclaredContext>` + = note: there are multiple different versions of crate `vitaminc_protected` in the dependency graph + = help: you can use `cargo tree` to explore your dependency tree + = note: required for `&str` to implement `Into<NonEmpty<&'static str>>` +note: required by a bound in `encrypt_into_with_context` + --> src/target/conversion.rs + | + | fn encrypt_into_with_context<'a, T, K: 'static>( + | ------------------------- required by a bound in this associated function +... + | context: impl Into<<T::ContextInput as AcceptsContext>::Value>, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `EncryptInto::encrypt_into_with_context` error[E0277]: the trait bound `AeadContext: From<&str>` is not satisfied - --> tests/ui/bare_context.rs:50:59 + --> tests/ui/bare_context.rs:42:44 | -50 | let _age: u32 = cipher.decrypt_as(sealed, "users/age".into()).await.unwrap(); - | ^^^^ the trait `From<&str>` is not implemented for `AeadContext` +42 | .encrypt_into_with_context(keyset, "users/age") + | ------------------------- ^^^^^^^^^^^ the trait `From<&str>` is not implemented for `AeadContext` + | | + | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: `AeadContext` implements `From<&NonEmpty<T>>` @@ -87,3 +103,67 @@ error[E0277]: the trait bound `AeadContext: From<&str>` is not satisfied `AeadContext` implements `From<i32>` and $N others = note: required for `&str` to implement `Into<AeadContext>` +note: required by a bound in `encrypt_into_with_context` + --> src/target/conversion.rs + | + | fn encrypt_into_with_context<'a, T, K: 'static>( + | ------------------------- required by a bound in this associated function +... + | context: impl Into<<T::ContextInput as AcceptsContext>::Value>, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `EncryptInto::encrypt_into_with_context` + +error[E0277]: the trait bound `CallerContext: From<&str>` is not satisfied + --> tests/ui/bare_context.rs:52:63 + | +52 | let _age = u32::decrypt_from_with_context(record, cipher, "users/age") + | ------------------------------ ^^^^^^^^^^^ the trait `From<&str>` is not implemented for `CallerContext` + | | + | required by a bound introduced by this call + | + = help: the following other types implement trait `From<T>`: + `CallerContext` implements `From<&NonEmpty<T>>` + `CallerContext` implements `From<NonEmpty<T>>` + `CallerContext` implements `From<Parts<S, B>>` + `CallerContext` implements `From<RequiredContext<C>>` + `CallerContext` implements `From<i128>` + `CallerContext` implements `From<i16>` + `CallerContext` implements `From<i32>` + `CallerContext` implements `From<i64>` + and $N others + = note: required for `&str` to implement `Into<CallerContext>` +note: required by a bound in `decrypt_from_with_context` + --> src/target/conversion.rs + | + | fn decrypt_from_with_context<'a, S, K: 'static>( + | ------------------------- required by a bound in this associated function +... + | context: impl Into<<S::ContextInput as AcceptsContext>::Value>, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `DecryptFrom::decrypt_from_with_context` + +error[E0277]: the trait bound `AeadContext: From<&str>` is not satisfied + --> tests/ui/bare_context.rs:55:63 + | +55 | let _age = u32::decrypt_from_with_context(sealed, cipher, "users/age") + | ------------------------------ ^^^^^^^^^^^ the trait `From<&str>` is not implemented for `AeadContext` + | | + | required by a bound introduced by this call + | + = help: the following other types implement trait `From<T>`: + `AeadContext` implements `From<&NonEmpty<T>>` + `AeadContext` implements `From<CallerContext>` + `AeadContext` implements `From<NonEmpty<T>>` + `AeadContext` implements `From<Parts<S, B>>` + `AeadContext` implements `From<RequiredContext<C>>` + `AeadContext` implements `From<i128>` + `AeadContext` implements `From<i16>` + `AeadContext` implements `From<i32>` + and $N others + = note: required for `&str` to implement `Into<AeadContext>` +note: required by a bound in `decrypt_from_with_context` + --> src/target/conversion.rs + | + | fn decrypt_from_with_context<'a, S, K: 'static>( + | ------------------------- required by a bound in this associated function +... + | context: impl Into<<S::ContextInput as AcceptsContext>::Value>, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `DecryptFrom::decrypt_from_with_context` diff --git a/packages/stack-encrypt/tests/ui/bind_in_query.rs b/packages/stack-encrypt/tests/ui/bind_in_query.rs deleted file mode 100644 index 892c6caeb4..0000000000 --- a/packages/stack-encrypt/tests/ui/bind_in_query.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! A query takes a scope and no binding: a binding is not in the query -//! context, so a query chain cannot be extended by a bare one. A binding -//! inside a `CallerContext` or a `DeclaredContext` converts, and is refused -//! when the query is awaited (`PlanError::BindInQuery`, -//! `tests/scopes_and_bindings.rs`). -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::target::{Bind, Scope}; -use stack_encrypt::{op, Equality, Plan, StackCipher}; - -struct User { - email: String, -} - -async fn query(cipher: &StackCipher<FakeDataKeySource>, plan: &Plan<User, FakeDataKeySource>) { - let field = plan.field("email").unwrap(); - // A scope extends the query's context. - let _ = cipher - .query(op::Eq, "ada@example.com") - .extend(Scope::new(42u64)) - .on(&field) - .await; - // A binding does not. - let _ = cipher - .query(op::Eq, "ada@example.com") - .extend(Bind::new(9u128)) - .on(&field) - .await; -} - -fn main() { - let _ = Equality; -} diff --git a/packages/stack-encrypt/tests/ui/bind_in_query.stderr b/packages/stack-encrypt/tests/ui/bind_in_query.stderr deleted file mode 100644 index 424a4a535a..0000000000 --- a/packages/stack-encrypt/tests/ui/bind_in_query.stderr +++ /dev/null @@ -1,34 +0,0 @@ -error[E0277]: the trait bound `DeclaredContext: From<stack_encrypt::target::Bind<u128>>` is not satisfied - --> tests/ui/bind_in_query.rs:25:17 - | -25 | .extend(Bind::new(9u128)) - | ------ ^^^^^^^^^^^^^^^^ the trait `From<stack_encrypt::target::Bind<u128>>` is not implemented for `DeclaredContext` - | | - | required by a bound introduced by this call - | - = help: the following other types implement trait `From<T>`: - `DeclaredContext` implements `From<&NonEmpty<T>>` - `DeclaredContext` implements `From<()>` - `DeclaredContext` implements `From<CallerContext>` - `DeclaredContext` implements `From<DeclaredScopes>` - `DeclaredContext` implements `From<NoCallerContext>` - `DeclaredContext` implements `From<NonEmpty<T>>` - `DeclaredContext` implements `From<RequiredContext<Parts<S, B>>>` - `DeclaredContext` implements `From<i128>` - and $N others - = note: required for `stack_encrypt::target::Bind<u128>` to implement `Into<DeclaredContext>` -note: required by a bound in `QueryBuilder::<'a, Op, F, K>::extend` - --> src/plan/chain.rs - | - | pub fn extend(mut self, parts: impl Into<DeclaredContext>) -> Self { - | ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `QueryBuilder::<'a, Op, F, K>::extend` -... - | / chain_options! { - | | ['a, S: ?Sized, K: 'static] EncryptBuilder<'a, S, K>; - | | ['a, S: ?Sized, K: 'static] EncryptWithContext<'a, S, K>; - | | ['a, S, X, K: 'static] EncryptIndexed<'a, S, X, K>; -... | - | | ['a, R, P, K: 'static] OpenUsing<'a, R, P, K>; - | | } - | |_- in this macro invocation - = note: this error originates in the macro `chain_options` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/packages/stack-encrypt/tests/ui/context_field_conflicts.rs b/packages/stack-encrypt/tests/ui/context_field_conflicts.rs index 81825f31a5..eb9f96df58 100644 --- a/packages/stack-encrypt/tests/ui/context_field_conflicts.rs +++ b/packages/stack-encrypt/tests/ui/context_field_conflicts.rs @@ -1,5 +1,9 @@ -use stack_encrypt::{EncryptFrom, StackCipherText}; -#[derive(EncryptFrom)] +//! A record stores its context in one field, and that field is metadata: it +//! is not also derived, defaulted or passed through. +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; + +#[derive(EncryptTarget)] struct Twice { #[stash(context_field)] one: String, @@ -7,10 +11,12 @@ struct Twice { two: String, c: StackCipherText, } -#[derive(EncryptFrom)] + +#[derive(EncryptTarget)] struct Defaulted { #[stash(context_field, default)] i: String, c: StackCipherText, } + fn main() {} diff --git a/packages/stack-encrypt/tests/ui/context_field_conflicts.stderr b/packages/stack-encrypt/tests/ui/context_field_conflicts.stderr index bcb6f092c4..a8d73b491b 100644 --- a/packages/stack-encrypt/tests/ui/context_field_conflicts.stderr +++ b/packages/stack-encrypt/tests/ui/context_field_conflicts.stderr @@ -1,11 +1,11 @@ error: a record has exactly one `context_field` - --> tests/ui/context_field_conflicts.rs:3:8 + --> tests/ui/context_field_conflicts.rs:7:8 | -3 | struct Twice { +7 | struct Twice { | ^^^^^ error: `context_field` is metadata and cannot also be derived, defaulted or passed through - --> tests/ui/context_field_conflicts.rs:13:8 + --> tests/ui/context_field_conflicts.rs:18:8 | -13 | i: String, +18 | i: String, | ^^^^^^ diff --git a/packages/stack-encrypt/tests/ui/context_type_conflicts.rs b/packages/stack-encrypt/tests/ui/context_type_conflicts.rs index bde8e9d150..f9c8748806 100644 --- a/packages/stack-encrypt/tests/ui/context_type_conflicts.rs +++ b/packages/stack-encrypt/tests/ui/context_type_conflicts.rs @@ -1,15 +1,16 @@ //! `context_type` names what the caller passes to a record whose fields take //! the caller's context. The two shapes that settle the context themselves //! refuse it, and like every other singular attribute it is given once. -use stack_encrypt::{EncryptFrom, StackCipherText}; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = User, scope = "users", context_type = stack_encrypt::target::AeadContext)] struct ByField { name: StackCipherText, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(plaintext = String, context_type = stack_encrypt::target::AeadContext)] struct Stored { #[stash(context_field)] @@ -17,7 +18,7 @@ struct Stored { c: StackCipherText, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(context_type = stack_encrypt::target::AeadContext, context_type = stack_encrypt::target::AeadContext)] struct Twice { c: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/context_type_conflicts.stderr b/packages/stack-encrypt/tests/ui/context_type_conflicts.stderr index 4c7a6d298b..cae9f6a600 100644 --- a/packages/stack-encrypt/tests/ui/context_type_conflicts.stderr +++ b/packages/stack-encrypt/tests/ui/context_type_conflicts.stderr @@ -1,23 +1,23 @@ error: `context_type` names what the caller passes to a record whose fields take the caller's context; a `struct` derive's fields carry their own contexts, so it does not apply - --> tests/ui/context_type_conflicts.rs:7:56 + --> tests/ui/context_type_conflicts.rs:8:56 | -7 | #[stash(struct = User, scope = "users", context_type = stack_encrypt::target::AeadContext)] +8 | #[stash(struct = User, scope = "users", context_type = stack_encrypt::target::AeadContext)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: `struct` given here - --> tests/ui/context_type_conflicts.rs:7:18 + --> tests/ui/context_type_conflicts.rs:8:18 | -7 | #[stash(struct = User, scope = "users", context_type = stack_encrypt::target::AeadContext)] +8 | #[stash(struct = User, scope = "users", context_type = stack_encrypt::target::AeadContext)] | ^^^^ -error: `context_field` supplies the complete context, so the record's `Context` is `NonEmpty<T>` of that field's type; `context_type` does not apply - --> tests/ui/context_type_conflicts.rs:13:44 +error: `context_field` supplies the complete context, so the caller passes a `NonEmpty<T>` of that field's type; `context_type` does not apply + --> tests/ui/context_type_conflicts.rs:14:44 | -13 | #[stash(plaintext = String, context_type = stack_encrypt::target::AeadContext)] +14 | #[stash(plaintext = String, context_type = stack_encrypt::target::AeadContext)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: `context_type` is given twice; a record has one associated context - --> tests/ui/context_type_conflicts.rs:21:60 + --> tests/ui/context_type_conflicts.rs:22:60 | -21 | #[stash(context_type = stack_encrypt::target::AeadContext, context_type = stack_encrypt::target::AeadContext)] +22 | #[stash(context_type = stack_encrypt::target::AeadContext, context_type = stack_encrypt::target::AeadContext)] | ^^^^^^^^^^^^ diff --git a/packages/stack-encrypt/tests/ui/decrypt_duplicate_from.rs b/packages/stack-encrypt/tests/ui/decrypt_duplicate_from.rs index 0b67f58900..6c0c716701 100644 --- a/packages/stack-encrypt/tests/ui/decrypt_duplicate_from.rs +++ b/packages/stack-encrypt/tests/ui/decrypt_duplicate_from.rs @@ -1,10 +1,15 @@ -use stack_encrypt::{DecryptInto, StackCipherText}; +//! A plaintext field has one output on the decrypt side too: two fields +//! marked `decrypt` that are derived from the same plaintext field are +//! refused. +use stack_encrypt::target::DecryptSource; +use stack_encrypt::StackCipherText; +#[derive(zeroize::Zeroize)] struct User { a: u32, } -#[derive(DecryptInto)] +#[derive(DecryptSource)] #[stash(struct = User, scope = "users")] struct Rec { #[stash(decrypt)] diff --git a/packages/stack-encrypt/tests/ui/decrypt_duplicate_from.stderr b/packages/stack-encrypt/tests/ui/decrypt_duplicate_from.stderr index 3f69d1bc4c..f86eef8f20 100644 --- a/packages/stack-encrypt/tests/ui/decrypt_duplicate_from.stderr +++ b/packages/stack-encrypt/tests/ui/decrypt_duplicate_from.stderr @@ -1,5 +1,5 @@ error: `a` and `b` are both derived from the plaintext field `a`: a plaintext field has one output. For a ciphertext with search terms beside it, make it one field of type `Encrypted<Terms>`, e.g. `a: Encrypted<(EqualityTerm, MatchTerms)>` - --> tests/ui/decrypt_duplicate_from.rs:12:29 + --> tests/ui/decrypt_duplicate_from.rs:17:29 | -12 | #[stash(decrypt, from = a)] +17 | #[stash(decrypt, from = a)] | ^ diff --git a/packages/stack-encrypt/tests/ui/decrypt_field_not_decryptable.rs b/packages/stack-encrypt/tests/ui/decrypt_field_not_decryptable.rs index 676b0e1400..b09a730aef 100644 --- a/packages/stack-encrypt/tests/ui/decrypt_field_not_decryptable.rs +++ b/packages/stack-encrypt/tests/ui/decrypt_field_not_decryptable.rs @@ -1,9 +1,13 @@ -use stack_encrypt::{DecryptInto, StackCipherText}; +//! Every field of a record that derives `DecryptSource` says whether it is +//! decryptable (`Decryptable`) and how it opens (`DecryptField`). A field +//! type that says neither is refused at the field. +use stack_encrypt::target::DecryptSource; +use stack_encrypt::StackCipherText; /// A hand-written leaf that says nothing about whether it is decryptable. struct Opaque; -#[derive(DecryptInto)] +#[derive(DecryptSource)] #[stash(plaintext = u32)] struct Rec { c: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/decrypt_field_not_decryptable.stderr b/packages/stack-encrypt/tests/ui/decrypt_field_not_decryptable.stderr index a3c6d675df..e784585c9e 100644 --- a/packages/stack-encrypt/tests/ui/decrypt_field_not_decryptable.stderr +++ b/packages/stack-encrypt/tests/ui/decrypt_field_not_decryptable.stderr @@ -1,45 +1,43 @@ -error[E0277]: the trait bound `Opaque: DecryptField<u32, CallerContext>` is not satisfied - --> tests/ui/decrypt_field_not_decryptable.rs:6:10 - | -6 | #[derive(DecryptInto)] - | ^^^^^^^^^^^ unsatisfied trait bound - | -help: the trait `DecryptField<u32, CallerContext>` is not implemented for `Opaque` - --> tests/ui/decrypt_field_not_decryptable.rs:4:1 - | -4 | struct Opaque; - | ^^^^^^^^^^^^^ - = help: the following other types implement trait `DecryptField<P, Ctx>`: - `(A, B)` implements `DecryptField<P, Ctx>` - `(A, B, C)` implements `DecryptField<P, Ctx>` - `(A, B, C, D)` implements `DecryptField<P, Ctx>` - `(A, B, C, D, E)` implements `DecryptField<P, Ctx>` - `CipherText<SealedValue, PassthroughFrame>` implements `DecryptField<P, Ctx>` - `Encrypted<Terms>` implements `DecryptField<P, Ctx>` - `EqualityTerm` implements `DecryptField<P, Ctx>` - `JsonDocument` implements `DecryptField<P, Ctx>` - and $N others - = help: see issue #48214 - = note: this error originates in the derive macro `DecryptInto` (in Nightly builds, run with -Z macro-backtrace for more info) +error[E0277]: the trait bound `Opaque: DecryptField<u32, RequiredContext<CallerContext>>` is not satisfied + --> tests/ui/decrypt_field_not_decryptable.rs:14:8 + | +14 | o: Opaque, + | ^^^^^^ unsatisfied trait bound + | +help: the trait `DecryptField<u32, RequiredContext<CallerContext>>` is not implemented for `Opaque` + --> tests/ui/decrypt_field_not_decryptable.rs:8:1 + | + 8 | struct Opaque; + | ^^^^^^^^^^^^^ + = help: the following other types implement trait `DecryptField<P, Ctx>`: + `CipherText<SealedValue, PassthroughFrame>` implements `DecryptField<P, Ctx>` + `Encrypted<Terms>` implements `DecryptField<P, Ctx>` + `EqualityTerm` implements `DecryptField<P, Ctx>` + `JsonDocument` implements `DecryptField<P, Ctx>` + `JsonExtracted` implements `DecryptField<P, Ctx>` + `JsonSubtree` implements `DecryptField<P, Ctx>` + `MatchTerms<O>` implements `DecryptField<P, Ctx>` + `OpeTerm<T>` implements `DecryptField<P, Ctx>` + and $N others error[E0277]: the trait bound `Opaque: Decryptable` is not satisfied - --> tests/ui/decrypt_field_not_decryptable.rs:10:8 + --> tests/ui/decrypt_field_not_decryptable.rs:14:8 | -10 | o: Opaque, +14 | o: Opaque, | ^^^^^^ unsatisfied trait bound | help: the trait `Decryptable` is not implemented for `Opaque` - --> tests/ui/decrypt_field_not_decryptable.rs:4:1 + --> tests/ui/decrypt_field_not_decryptable.rs:8:1 | - 4 | struct Opaque; + 8 | struct Opaque; | ^^^^^^^^^^^^^ = help: the following other types implement trait `Decryptable`: - (A, B) - (A, B, C) - (A, B, C, D) - (A, B, C, D, E) CipherText<SealedValue, PassthroughFrame> Encrypted<Terms> EqualityTerm JsonDocument + JsonExtracted + JsonSubtree + MatchTerms<O> + OpeTerm<T> and $N others diff --git a/packages/stack-encrypt/tests/ui/decrypt_no_decryptable_field.rs b/packages/stack-encrypt/tests/ui/decrypt_no_decryptable_field.rs index 86be6d1061..19702058ba 100644 --- a/packages/stack-encrypt/tests/ui/decrypt_no_decryptable_field.rs +++ b/packages/stack-encrypt/tests/ui/decrypt_no_decryptable_field.rs @@ -1,7 +1,9 @@ +//! A record of one-way terms alone holds no ciphertext, so it cannot derive +//! `DecryptSource`. use stack_encrypt::sem::{EqualityTerm, OreTerm}; -use stack_encrypt::DecryptInto; +use stack_encrypt::target::DecryptSource; -#[derive(DecryptInto)] +#[derive(DecryptSource)] #[stash(plaintext = u32)] struct Rec { hm: EqualityTerm, diff --git a/packages/stack-encrypt/tests/ui/decrypt_no_decryptable_field.stderr b/packages/stack-encrypt/tests/ui/decrypt_no_decryptable_field.stderr index 51acb56123..3a4daa6a9c 100644 --- a/packages/stack-encrypt/tests/ui/decrypt_no_decryptable_field.stderr +++ b/packages/stack-encrypt/tests/ui/decrypt_no_decryptable_field.stderr @@ -1,5 +1,5 @@ -error[E0080]: evaluation panicked: `Rec` has no decryptable field: every derived field is a one-way index term, so there is nothing for DecryptInto to open - --> tests/ui/decrypt_no_decryptable_field.rs:4:10 +error[E0080]: evaluation panicked: `Rec` has no decryptable field: every derived field is a one-way index term, so there is nothing for DecryptSource to open + --> tests/ui/decrypt_no_decryptable_field.rs:6:10 | -4 | #[derive(DecryptInto)] - | ^^^^^^^^^^^ evaluation of `_` failed here +6 | #[derive(DecryptSource)] + | ^^^^^^^^^^^^^ evaluation of `_` failed here diff --git a/packages/stack-encrypt/tests/ui/decrypt_several_decryptable_fields.rs b/packages/stack-encrypt/tests/ui/decrypt_several_decryptable_fields.rs index 09419eb565..25de2adbdd 100644 --- a/packages/stack-encrypt/tests/ui/decrypt_several_decryptable_fields.rs +++ b/packages/stack-encrypt/tests/ui/decrypt_several_decryptable_fields.rs @@ -1,6 +1,9 @@ -use stack_encrypt::{DecryptInto, StackCipherText}; +//! A `plaintext` record opens through one ciphertext. With two, and neither +//! marked `#[stash(decrypt)]`, the derive cannot choose. +use stack_encrypt::target::DecryptSource; +use stack_encrypt::StackCipherText; -#[derive(DecryptInto)] +#[derive(DecryptSource)] #[stash(plaintext = u32)] struct Rec { a: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/decrypt_several_decryptable_fields.stderr b/packages/stack-encrypt/tests/ui/decrypt_several_decryptable_fields.stderr index 4cd2aa2405..9a134da859 100644 --- a/packages/stack-encrypt/tests/ui/decrypt_several_decryptable_fields.stderr +++ b/packages/stack-encrypt/tests/ui/decrypt_several_decryptable_fields.stderr @@ -1,5 +1,5 @@ error[E0080]: evaluation panicked: `Rec` has several decryptable fields: mark the one decryption opens `#[stash(decrypt)]` - --> tests/ui/decrypt_several_decryptable_fields.rs:3:10 + --> tests/ui/decrypt_several_decryptable_fields.rs:6:10 | -3 | #[derive(DecryptInto)] - | ^^^^^^^^^^^ evaluation of `_` failed here +6 | #[derive(DecryptSource)] + | ^^^^^^^^^^^^^ evaluation of `_` failed here diff --git a/packages/stack-encrypt/tests/ui/decrypt_struct_field_not_recoverable.rs b/packages/stack-encrypt/tests/ui/decrypt_struct_field_not_recoverable.rs index 77e153456e..9367a84472 100644 --- a/packages/stack-encrypt/tests/ui/decrypt_struct_field_not_recoverable.rs +++ b/packages/stack-encrypt/tests/ui/decrypt_struct_field_not_recoverable.rs @@ -2,14 +2,16 @@ //! whose one output is a one-way term cannot be recovered. The refusal names //! the plaintext field and the replacement that stores its ciphertext. use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::{DecryptInto, StackCipherText}; +use stack_encrypt::target::DecryptSource; +use stack_encrypt::StackCipherText; +#[derive(zeroize::Zeroize)] struct User { email: String, name: String, } -#[derive(DecryptInto)] +#[derive(DecryptSource)] #[stash(struct = User, scope = "users")] struct Opened { email: EqualityTerm, diff --git a/packages/stack-encrypt/tests/ui/decrypt_struct_field_not_recoverable.stderr b/packages/stack-encrypt/tests/ui/decrypt_struct_field_not_recoverable.stderr index a116b8bfa5..df31a7c140 100644 --- a/packages/stack-encrypt/tests/ui/decrypt_struct_field_not_recoverable.stderr +++ b/packages/stack-encrypt/tests/ui/decrypt_struct_field_not_recoverable.stderr @@ -1,5 +1,5 @@ error[E0080]: evaluation panicked: no field of `Opened` can recover the plaintext field `email`: the field derived from it is a one-way index term. To store its ciphertext with the term, make it `Encrypted<Terms>` - --> tests/ui/decrypt_struct_field_not_recoverable.rs:12:10 + --> tests/ui/decrypt_struct_field_not_recoverable.rs:14:10 | -12 | #[derive(DecryptInto)] - | ^^^^^^^^^^^ evaluation of `_` failed here +14 | #[derive(DecryptSource)] + | ^^^^^^^^^^^^^ evaluation of `_` failed here diff --git a/packages/stack-encrypt/tests/ui/default_with_decrypt.rs b/packages/stack-encrypt/tests/ui/default_with_decrypt.rs index 5af685df18..504b12f1fe 100644 --- a/packages/stack-encrypt/tests/ui/default_with_decrypt.rs +++ b/packages/stack-encrypt/tests/ui/default_with_decrypt.rs @@ -1,6 +1,9 @@ -use stack_encrypt::{EncryptFrom, StackCipherText}; +//! A `default` field is filled, not derived, so it cannot be the field that +//! decryption opens. +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] struct Rec { c: StackCipherText, #[stash(default, decrypt)] diff --git a/packages/stack-encrypt/tests/ui/default_with_decrypt.stderr b/packages/stack-encrypt/tests/ui/default_with_decrypt.stderr index c333cae076..356cf4527a 100644 --- a/packages/stack-encrypt/tests/ui/default_with_decrypt.stderr +++ b/packages/stack-encrypt/tests/ui/default_with_decrypt.stderr @@ -1,5 +1,5 @@ error: a `default` field is not derived from the source, so `identity`, `from` and `decrypt` do not apply to it - --> tests/ui/default_with_decrypt.rs:7:8 - | -7 | v: u8, - | ^^ + --> tests/ui/default_with_decrypt.rs:10:8 + | +10 | v: u8, + | ^^ diff --git a/packages/stack-encrypt/tests/ui/derive_match_on_integer.rs b/packages/stack-encrypt/tests/ui/derive_match_on_integer.rs new file mode 100644 index 0000000000..5c7b41c114 --- /dev/null +++ b/packages/stack-encrypt/tests/ui/derive_match_on_integer.rs @@ -0,0 +1,19 @@ +//! Match is defined over text, so a record cannot declare a match index on +//! an integer field. +use stack_encrypt::sem::MatchTerms; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::Encrypted; +use zeroize::Zeroize; + +#[derive(Clone, Zeroize)] +struct User { + age: u32, +} + +#[derive(EncryptTarget)] +#[stash(struct = User, scope = "users")] +struct EncryptedUser { + age: Encrypted<MatchTerms>, +} + +fn main() {} diff --git a/packages/stack-encrypt/tests/ui/derive_match_on_integer.stderr b/packages/stack-encrypt/tests/ui/derive_match_on_integer.stderr new file mode 100644 index 0000000000..24c7c10062 --- /dev/null +++ b/packages/stack-encrypt/tests/ui/derive_match_on_integer.stderr @@ -0,0 +1,33 @@ +error[E0277]: the trait bound `u32: AsRef<str>` is not satisfied + --> tests/ui/derive_match_on_integer.rs:16:10 + | +16 | age: Encrypted<MatchTerms>, + | ^^^^^^^^^^^^^^^^^^^^^ the trait `AsRef<str>` is not implemented for `u32` + | +help: the trait `EncryptTarget<S>` is implemented for `Encrypted<Terms>` + --> src/target/index.rs + | + | / impl<S, Terms> EncryptTarget<S> for Encrypted<Terms> + | | where + | | S: crate::Encrypt + Clone + zeroize::Zeroize, + | | Terms: TermSet<S>, + | |______________________^ + = note: required for `MatchTerms` to implement `TermSet<u32>` + = note: required for `Encrypted<MatchTerms>` to implement `EncryptTarget<u32>` + +error[E0277]: the trait bound `u32: AsRef<str>` is not satisfied + --> tests/ui/derive_match_on_integer.rs:16:10 + | +16 | age: Encrypted<MatchTerms>, + | ^^^^^^^^^ the trait `AsRef<str>` is not implemented for `u32` + | +help: the trait `EncryptTarget<S>` is implemented for `Encrypted<Terms>` + --> src/target/index.rs + | + | / impl<S, Terms> EncryptTarget<S> for Encrypted<Terms> + | | where + | | S: crate::Encrypt + Clone + zeroize::Zeroize, + | | Terms: TermSet<S>, + | |______________________^ + = note: required for `MatchTerms` to implement `TermSet<u32>` + = note: required for `Encrypted<MatchTerms>` to implement `EncryptTarget<u32>` diff --git a/packages/stack-encrypt/tests/ui/divergent_context_in_target.rs b/packages/stack-encrypt/tests/ui/divergent_context_in_target.rs index 8915bb9ff1..ee5d185332 100644 --- a/packages/stack-encrypt/tests/ui/divergent_context_in_target.rs +++ b/packages/stack-encrypt/tests/ui/divergent_context_in_target.rs @@ -11,7 +11,7 @@ //! being the same construct as a record naming the contexts of two fields //! (ADR-0004, decision 1). use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::target::{ciphertext, equality, DeclaredContext, EncryptFrom, Encryption}; +use stack_encrypt::target::{ciphertext, equality, DeclaredScopes, EncryptTarget, Encryption}; use stack_encrypt::{nonempty, StackCipherText}; struct Divergent { @@ -19,9 +19,9 @@ struct Divergent { hm: EqualityTerm, } -impl EncryptFrom<String> for Divergent { - type Context = DeclaredContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> +impl EncryptTarget<String> for Divergent { + type ContextInput = DeclaredScopes; + fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::ContextInput> where String: 's, { @@ -29,6 +29,7 @@ impl EncryptFrom<String> for Divergent { .under(nonempty!("users/email")) .zip(equality()) .map(|(c, hm)| Self { c, hm }) + .accepting() } } diff --git a/packages/stack-encrypt/tests/ui/drop_record.rs b/packages/stack-encrypt/tests/ui/drop_record.rs index 023a46a10c..3ca13ee51b 100644 --- a/packages/stack-encrypt/tests/ui/drop_record.rs +++ b/packages/stack-encrypt/tests/ui/drop_record.rs @@ -1,10 +1,11 @@ use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::{DecryptInto, EncryptFrom, StackCipherText}; +use stack_encrypt::target::{DecryptSource, EncryptTarget}; +use stack_encrypt::StackCipherText; -// `DecryptInto` moves the opened field out of `self`, which a `Drop` type +// `DecryptSource` moves the opened field out of `self`, which a `Drop` type // (including `ZeroizeOnDrop`) forbids. The restriction is documented; this // pins what the user sees. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32)] struct Rec { #[stash(decrypt)] diff --git a/packages/stack-encrypt/tests/ui/drop_record.stderr b/packages/stack-encrypt/tests/ui/drop_record.stderr index c3667678cf..dd3c667072 100644 --- a/packages/stack-encrypt/tests/ui/drop_record.stderr +++ b/packages/stack-encrypt/tests/ui/drop_record.stderr @@ -1,7 +1,7 @@ error[E0509]: cannot move out of type `Rec`, which implements the `Drop` trait - --> tests/ui/drop_record.rs:11:5 + --> tests/ui/drop_record.rs:12:5 | -11 | c: StackCipherText, +12 | c: StackCipherText, | ^^^^^^^^^^^^^^^^^^ | | | cannot move out of here diff --git a/packages/stack-encrypt/tests/ui/duplicate_plaintext.rs b/packages/stack-encrypt/tests/ui/duplicate_plaintext.rs index a6a0cce80a..3579a6d580 100644 --- a/packages/stack-encrypt/tests/ui/duplicate_plaintext.rs +++ b/packages/stack-encrypt/tests/ui/duplicate_plaintext.rs @@ -1,6 +1,9 @@ -use stack_encrypt::{EncryptFrom, StackCipherText}; +//! `plaintext` can be repeated, one impl for each type, and each type is +//! listed once. +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(plaintext = u32, plaintext = u32)] struct Dup { c: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/duplicate_plaintext.stderr b/packages/stack-encrypt/tests/ui/duplicate_plaintext.stderr index d1bac08749..83433c2a12 100644 --- a/packages/stack-encrypt/tests/ui/duplicate_plaintext.stderr +++ b/packages/stack-encrypt/tests/ui/duplicate_plaintext.stderr @@ -1,5 +1,5 @@ error: this `plaintext` is listed twice; each listed type gets one impl - --> tests/ui/duplicate_plaintext.rs:4:38 + --> tests/ui/duplicate_plaintext.rs:7:38 | -4 | #[stash(plaintext = u32, plaintext = u32)] +7 | #[stash(plaintext = u32, plaintext = u32)] | ^^^ diff --git a/packages/stack-encrypt/tests/ui/duplicate_singleton_attrs.rs b/packages/stack-encrypt/tests/ui/duplicate_singleton_attrs.rs index 82ad13e1af..0596bf153d 100644 --- a/packages/stack-encrypt/tests/ui/duplicate_singleton_attrs.rs +++ b/packages/stack-encrypt/tests/ui/duplicate_singleton_attrs.rs @@ -1,38 +1,41 @@ -use stack_encrypt::{DecryptInto, EncryptFrom, StackCipherText}; +//! Every attribute except `plaintext` is given once. Repeating one is +//! refused at the second. +use stack_encrypt::target::{DecryptSource, EncryptTarget}; +use stack_encrypt::StackCipherText; struct User { expected: u32, other: u32, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = User, scope = "users")] struct DupFrom { #[stash(from = expected, from = other)] c: StackCipherText, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = User, scope = "users")] struct DupIdentity { #[stash(identity = "email", identity = "name")] expected: StackCipherText, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] struct DupDefault { c: StackCipherText, #[stash(default, default = 3)] v: u8, } -#[derive(DecryptInto)] +#[derive(DecryptSource)] struct DupDecrypt { #[stash(decrypt, decrypt)] c: StackCipherText, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(crate = "stack_encrypt", crate = "stack_encrypt")] struct DupCrate { c: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/duplicate_singleton_attrs.stderr b/packages/stack-encrypt/tests/ui/duplicate_singleton_attrs.stderr index 777c2f6f95..26720875c0 100644 --- a/packages/stack-encrypt/tests/ui/duplicate_singleton_attrs.stderr +++ b/packages/stack-encrypt/tests/ui/duplicate_singleton_attrs.stderr @@ -1,29 +1,29 @@ error: `from` is given twice; a field is derived from one plaintext field - --> tests/ui/duplicate_singleton_attrs.rs:11:30 + --> tests/ui/duplicate_singleton_attrs.rs:14:30 | -11 | #[stash(from = expected, from = other)] +14 | #[stash(from = expected, from = other)] | ^^^^ error: `identity` is given twice; a field is keyed under one segment - --> tests/ui/duplicate_singleton_attrs.rs:18:33 + --> tests/ui/duplicate_singleton_attrs.rs:21:33 | -18 | #[stash(identity = "email", identity = "name")] +21 | #[stash(identity = "email", identity = "name")] | ^^^^^^^^ error: `default` is given twice - --> tests/ui/duplicate_singleton_attrs.rs:25:22 + --> tests/ui/duplicate_singleton_attrs.rs:28:22 | -25 | #[stash(default, default = 3)] +28 | #[stash(default, default = 3)] | ^^^^^^^ error: `decrypt` is given twice - --> tests/ui/duplicate_singleton_attrs.rs:31:22 + --> tests/ui/duplicate_singleton_attrs.rs:34:22 | -31 | #[stash(decrypt, decrypt)] +34 | #[stash(decrypt, decrypt)] | ^^^^^^^ error: `crate` is given twice - --> tests/ui/duplicate_singleton_attrs.rs:36:34 + --> tests/ui/duplicate_singleton_attrs.rs:39:34 | -36 | #[stash(crate = "stack_encrypt", crate = "stack_encrypt")] +39 | #[stash(crate = "stack_encrypt", crate = "stack_encrypt")] | ^^^^^ diff --git a/packages/stack-encrypt/tests/ui/empty_context.rs b/packages/stack-encrypt/tests/ui/empty_context.rs index 51bc7b98bf..d6ac9f80d2 100644 --- a/packages/stack-encrypt/tests/ui/empty_context.rs +++ b/packages/stack-encrypt/tests/ui/empty_context.rs @@ -1,10 +1,13 @@ -use stack_encrypt::{EncryptFrom, StackCipherText}; +//! An `identity` is one plain label segment, so an empty one is refused. +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; +#[derive(zeroize::Zeroize)] struct User { email: String, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = User, scope = "users")] struct EncryptedUser { #[stash(identity = "")] diff --git a/packages/stack-encrypt/tests/ui/empty_context.stderr b/packages/stack-encrypt/tests/ui/empty_context.stderr index d16c2a0c45..6a1d5bbb28 100644 --- a/packages/stack-encrypt/tests/ui/empty_context.stderr +++ b/packages/stack-encrypt/tests/ui/empty_context.stderr @@ -1,5 +1,5 @@ error: an `identity` is the one label segment the field is keyed under, so it must be plain: not empty, no `/`, `(`, `)`, control or invisible character, and not beginning with `b64:`, a digit or `-` (e.g. `identity = "email"`) - --> tests/ui/empty_context.rs:10:24 + --> tests/ui/empty_context.rs:13:24 | -10 | #[stash(identity = "")] +13 | #[stash(identity = "")] | ^^ diff --git a/packages/stack-encrypt/tests/ui/enum_record.rs b/packages/stack-encrypt/tests/ui/enum_record.rs index 8f9f028961..58b8419f16 100644 --- a/packages/stack-encrypt/tests/ui/enum_record.rs +++ b/packages/stack-encrypt/tests/ui/enum_record.rs @@ -1,6 +1,8 @@ -use stack_encrypt::{EncryptFrom, StackCipherText}; +//! A record is a struct: the derives refuse an enum. +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] enum Choice { A(StackCipherText), B(StackCipherText), diff --git a/packages/stack-encrypt/tests/ui/enum_record.stderr b/packages/stack-encrypt/tests/ui/enum_record.stderr index 63eb02cad5..feeda317d8 100644 --- a/packages/stack-encrypt/tests/ui/enum_record.stderr +++ b/packages/stack-encrypt/tests/ui/enum_record.stderr @@ -1,5 +1,5 @@ -error: EncryptFrom/DecryptInto cannot be derived for enums: a record is a fixed set of fields derived from one source, and a variant choice has no field to be derived into. Model the choice explicitly instead, e.g. as a struct of `Option` fields. - --> tests/ui/enum_record.rs:4:6 +error: EncryptTarget/DecryptSource cannot be derived for enums: a record is a fixed set of fields derived from one source, and a variant choice has no field to be derived into. Model the choice explicitly instead, e.g. as a struct of `Option` fields. + --> tests/ui/enum_record.rs:6:6 | -4 | enum Choice { +6 | enum Choice { | ^^^^^^ diff --git a/packages/stack-encrypt/tests/ui/field_context_removed.rs b/packages/stack-encrypt/tests/ui/field_context_removed.rs index 4279e1ad1d..a597794355 100644 --- a/packages/stack-encrypt/tests/ui/field_context_removed.rs +++ b/packages/stack-encrypt/tests/ui/field_context_removed.rs @@ -1,23 +1,24 @@ -//! A field-level `context = ".."` is refused on every derive form. A plan has -//! exactly one context source, and every output sits under it: all outputs of -//! a `plaintext = T` record share the caller's context, and every field of a -//! `struct = ..` derive sits under the record's (`identity` keys it under -//! another segment). One value written under two contexts, a dual write, is a -//! fields plan that picks the same source twice, not a derive. -use stack_encrypt::{EncryptFrom, StackCipherText}; +//! A field-level `context = ".."` is refused on every record form. Every +//! output sits under one context source: all outputs of a `plaintext = T` +//! record share the caller's context, and every field of a `struct = ..` +//! record sits under the record's (`identity` keys it under another +//! segment). To write one value under two contexts, encrypt it twice. +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; +#[derive(zeroize::Zeroize)] struct User { name: String, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = User, scope = "users")] struct EncryptedUser { #[stash(context = "nickname")] name: StackCipherText, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(plaintext = String)] struct Doubled { c: StackCipherText, @@ -25,7 +26,7 @@ struct Doubled { shadow: StackCipherText, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] struct Generic { #[stash(context = "legacy_age")] c: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/field_context_removed.stderr b/packages/stack-encrypt/tests/ui/field_context_removed.stderr index 812c0ac9a1..8622378b93 100644 --- a/packages/stack-encrypt/tests/ui/field_context_removed.stderr +++ b/packages/stack-encrypt/tests/ui/field_context_removed.stderr @@ -1,17 +1,17 @@ -error: a field-level `context = ".."` is no longer accepted (0.2.0 accepted it). All outputs of a `plaintext = T` record share the caller's context, so remove it there; every field of a `struct = ..` derive sits under the record's context, so key it under another segment with `identity = ".."`. Either replacement is a different context from the old literal: data written with the literal does not decrypt under it and its terms do not match, so re-encrypt that data. Writing one value under two contexts (a dual write) is a fields plan that picks the same source twice, not a derive - --> tests/ui/field_context_removed.rs:16:13 +error: a field-level `context = ".."` is no longer accepted (0.2.0 accepted it). All outputs of a `plaintext = T` record share the caller's context, so remove it there; every field of a `struct = ..` derive sits under the record's context, so key it under another segment with `identity = ".."`. Either replacement is a different context from the old literal: data written with the literal does not decrypt under it and its terms do not match, so re-encrypt that data. To write one value under two contexts (a dual write), encrypt it twice, once under each context + --> tests/ui/field_context_removed.rs:17:13 | -16 | #[stash(context = "nickname")] +17 | #[stash(context = "nickname")] | ^^^^^^^ -error: a field-level `context = ".."` is no longer accepted (0.2.0 accepted it). All outputs of a `plaintext = T` record share the caller's context, so remove it there; every field of a `struct = ..` derive sits under the record's context, so key it under another segment with `identity = ".."`. Either replacement is a different context from the old literal: data written with the literal does not decrypt under it and its terms do not match, so re-encrypt that data. Writing one value under two contexts (a dual write) is a fields plan that picks the same source twice, not a derive - --> tests/ui/field_context_removed.rs:24:13 +error: a field-level `context = ".."` is no longer accepted (0.2.0 accepted it). All outputs of a `plaintext = T` record share the caller's context, so remove it there; every field of a `struct = ..` derive sits under the record's context, so key it under another segment with `identity = ".."`. Either replacement is a different context from the old literal: data written with the literal does not decrypt under it and its terms do not match, so re-encrypt that data. To write one value under two contexts (a dual write), encrypt it twice, once under each context + --> tests/ui/field_context_removed.rs:25:13 | -24 | #[stash(context = "shadow")] +25 | #[stash(context = "shadow")] | ^^^^^^^ -error: a field-level `context = ".."` is no longer accepted (0.2.0 accepted it). All outputs of a `plaintext = T` record share the caller's context, so remove it there; every field of a `struct = ..` derive sits under the record's context, so key it under another segment with `identity = ".."`. Either replacement is a different context from the old literal: data written with the literal does not decrypt under it and its terms do not match, so re-encrypt that data. Writing one value under two contexts (a dual write) is a fields plan that picks the same source twice, not a derive - --> tests/ui/field_context_removed.rs:30:13 +error: a field-level `context = ".."` is no longer accepted (0.2.0 accepted it). All outputs of a `plaintext = T` record share the caller's context, so remove it there; every field of a `struct = ..` derive sits under the record's context, so key it under another segment with `identity = ".."`. Either replacement is a different context from the old literal: data written with the literal does not decrypt under it and its terms do not match, so re-encrypt that data. To write one value under two contexts (a dual write), encrypt it twice, once under each context + --> tests/ui/field_context_removed.rs:31:13 | -30 | #[stash(context = "legacy_age")] +31 | #[stash(context = "legacy_age")] | ^^^^^^^ diff --git a/packages/stack-encrypt/tests/ui/from_without_plaintext.rs b/packages/stack-encrypt/tests/ui/from_without_plaintext.rs index 644bdd5b13..d4797ee2a5 100644 --- a/packages/stack-encrypt/tests/ui/from_without_plaintext.rs +++ b/packages/stack-encrypt/tests/ui/from_without_plaintext.rs @@ -1,20 +1,22 @@ //! `from = ..` reaches into a field of the plaintext, which is what //! `#[stash(struct = ..)]` means; a `plaintext` record derives every field //! from the whole value. -use stack_encrypt::{EncryptFrom, StackCipherText}; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; +#[derive(zeroize::Zeroize)] struct User { age: u32, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(plaintext = User)] struct Row { #[stash(from = age)] age: StackCipherText, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] struct Unnamed { #[stash(from = age)] age: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/from_without_plaintext.stderr b/packages/stack-encrypt/tests/ui/from_without_plaintext.stderr index 44a7ef79ac..5e1b48c726 100644 --- a/packages/stack-encrypt/tests/ui/from_without_plaintext.stderr +++ b/packages/stack-encrypt/tests/ui/from_without_plaintext.stderr @@ -1,11 +1,11 @@ error: `from = ..` reaches into a field of the plaintext, which is what `#[stash(struct = ..)]` does: a `plaintext` record derives every field from the whole value - --> tests/ui/from_without_plaintext.rs:13:20 + --> tests/ui/from_without_plaintext.rs:15:20 | -13 | #[stash(from = age)] +15 | #[stash(from = age)] | ^^^ error: `from = ..` reaches into a field of the plaintext, which is what `#[stash(struct = ..)]` does: a `plaintext` record derives every field from the whole value - --> tests/ui/from_without_plaintext.rs:19:20 + --> tests/ui/from_without_plaintext.rs:21:20 | -19 | #[stash(from = age)] +21 | #[stash(from = age)] | ^^^ diff --git a/packages/stack-encrypt/tests/ui/join_context_is_sealed.rs b/packages/stack-encrypt/tests/ui/join_context_is_sealed.rs deleted file mode 100644 index a506908829..0000000000 --- a/packages/stack-encrypt/tests/ui/join_context_is_sealed.rs +++ /dev/null @@ -1,12 +0,0 @@ -//! `JoinContext` is sealed: the pairs this crate joins are the only ones, -//! so a crate cannot add a join for its own context. -use stack_encrypt::target::{AeadContext, JoinContext}; - -#[derive(Clone)] -struct MyContext; - -impl JoinContext<MyContext> for AeadContext { - type Output = MyContext; -} - -fn main() {} diff --git a/packages/stack-encrypt/tests/ui/join_context_is_sealed.stderr b/packages/stack-encrypt/tests/ui/join_context_is_sealed.stderr deleted file mode 100644 index 77c9d411f7..0000000000 --- a/packages/stack-encrypt/tests/ui/join_context_is_sealed.stderr +++ /dev/null @@ -1,38 +0,0 @@ -error[E0277]: the trait bound `AeadContext: target::tuples::sealed::Sealed<MyContext>` is not satisfied - --> tests/ui/join_context_is_sealed.rs:8:33 - | -8 | impl JoinContext<MyContext> for AeadContext { - | ^^^^^^^^^^^ the trait `target::tuples::sealed::Sealed<MyContext>` is not implemented for `AeadContext` - | -help: the following other types implement trait `target::tuples::sealed::Sealed<Other>` - --> src/target/tuples.rs - | - | impl sealed::Sealed<$right> for $left {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | `AeadContext` implements `target::tuples::sealed::Sealed<CallerContext>` - | `AeadContext` implements `target::tuples::sealed::Sealed<DeclaredContext>` -... - | / join_to_caller! { - | | AeadContext, CallerContext; - | | CallerContext, AeadContext; - | | AeadContext, DeclaredContext; -... | - | | DeclaredContext, CallerContext; - | | } - | |_- in this macro invocation -note: required by a bound in `JoinContext` - --> src/target/tuples.rs - | - | pub trait JoinContext<Other>: sealed::Sealed<Other> { - | ^^^^^^^^^^^^^^^^^^^^^ required by this bound in `JoinContext` - = note: `JoinContext` is a "sealed trait", because to implement it you also need to implement `stack_encrypt::target::tuples::sealed::Sealed`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it - = help: the following types implement the trait: - stack_encrypt::target::AeadContext - stack_encrypt::target::AeadContext - stack_encrypt::CallerContext - stack_encrypt::CallerContext - stack_encrypt::target::DeclaredContext - stack_encrypt::target::DeclaredContext - C - = note: this error originates in the macro `join_to_caller` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/packages/stack-encrypt/tests/ui/leaf_without_context.rs b/packages/stack-encrypt/tests/ui/leaf_without_context.rs index 03b87757e9..7498680e8c 100644 --- a/packages/stack-encrypt/tests/ui/leaf_without_context.rs +++ b/packages/stack-encrypt/tests/ui/leaf_without_context.rs @@ -1,31 +1,36 @@ //! A leaf, a record that hands the caller's context to one, and a column of -//! either all need a `NonEmpty<_>` context: a default context, or `()`, is -//! not one, so no leaf accepts it. +//! either all need a `NonEmpty<_>` context. A call that leaves the context +//! out does not compile, and `()` is not a context, so no leaf accepts it. use stack_encrypt::sem::EqualityTerm; - -use stack_encrypt::{DecryptInto, EncryptFrom, KeysetCipher, StackCipher, StackCipherText}; +use stack_encrypt::target::{DecryptSource, EncryptTarget}; +use stack_encrypt::{DecryptFrom, EncryptInto, KeysetCipher, StackCipher, StackCipherText}; use stack_kms::FakeDataKeySource; -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32)] struct EncryptedAge { c: StackCipherText, hm: EqualityTerm, } -async fn encrypt(cipher: &KeysetCipher<'_, FakeDataKeySource>) { - let _term: EqualityTerm = cipher.encrypt_as(&"alice", Default::default()).await.unwrap(); - let _record: EncryptedAge = cipher.encrypt_as(&42u32, Default::default()).await.unwrap(); - let _column: Vec<StackCipherText> = - cipher.encrypt_as(&vec![1u32], Default::default()).await.unwrap(); +async fn encrypt(keyset: &KeysetCipher<'_, FakeDataKeySource>) { + let _term: EqualityTerm = String::from("alice").encrypt_into(keyset).await.unwrap(); + let _record: EncryptedAge = 42u32.encrypt_into(keyset).await.unwrap(); + let _column: Vec<StackCipherText> = vec![1u32].encrypt_into(keyset).await.unwrap(); } async fn decrypt(cipher: &StackCipher<FakeDataKeySource>, record: EncryptedAge) { - let _age: u32 = cipher.decrypt_as(record, Default::default()).await.unwrap(); + let _age = u32::decrypt_from(record, cipher).await.unwrap(); } -async fn decrypt_leaf(cipher: &StackCipher<FakeDataKeySource>, record: EncryptedAge) { - let _age: u32 = cipher.decrypt_as(record, ().into()).await.unwrap(); +async fn empty(keyset: &KeysetCipher<'_, FakeDataKeySource>, record: EncryptedAge) { + let _term: EqualityTerm = String::from("alice") + .encrypt_into_with_context(keyset, ()) + .await + .unwrap(); + let _age = u32::decrypt_from_with_context(record, keyset, ()) + .await + .unwrap(); } fn main() {} diff --git a/packages/stack-encrypt/tests/ui/leaf_without_context.stderr b/packages/stack-encrypt/tests/ui/leaf_without_context.stderr index ffe53b1835..6c6751f4e2 100644 --- a/packages/stack-encrypt/tests/ui/leaf_without_context.stderr +++ b/packages/stack-encrypt/tests/ui/leaf_without_context.stderr @@ -1,32 +1,118 @@ -error[E0277]: the trait bound `CallerContext: Default` is not satisfied - --> tests/ui/leaf_without_context.rs:17:59 +error[E0277]: the trait bound `RequiredContext<CallerContext>: AllowsNoContext` is not satisfied + --> tests/ui/leaf_without_context.rs:17:53 | -17 | let _term: EqualityTerm = cipher.encrypt_as(&"alice", Default::default()).await.unwrap(); - | ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `CallerContext` +17 | let _term: EqualityTerm = String::from("alice").encrypt_into(keyset).await.unwrap(); + | ^^^^^^^^^^^^ the trait `AllowsNoContext` is not implemented for `RequiredContext<CallerContext>` + | +help: the following other types implement trait `AllowsNoContext` + --> src/target/input.rs + | + | impl AllowsNoContext for NoCallerContext { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `NoCallerContext` +... + | impl<C> AllowsNoContext for OptionalContext<C> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `OptionalContext<C>` +... + | impl AllowsNoContext for DeclaredScopes { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `DeclaredScopes` +note: required by a bound in `encrypt_into` + --> src/target/conversion.rs + | + | fn encrypt_into<'a, T, K: 'static>(self, cipher: &'a KeysetCipher<'_, K>) -> Pending<'a, T, K> + | ------------ required by a bound in this associated function +... + | T::ContextInput: AllowsNoContext, + | ^^^^^^^^^^^^^^^ required by this bound in `EncryptInto::encrypt_into` -error[E0277]: the trait bound `CallerContext: Default` is not satisfied - --> tests/ui/leaf_without_context.rs:18:59 +error[E0277]: the trait bound `RequiredContext<CallerContext>: AllowsNoContext` is not satisfied + --> tests/ui/leaf_without_context.rs:18:39 + | +18 | let _record: EncryptedAge = 42u32.encrypt_into(keyset).await.unwrap(); + | ^^^^^^^^^^^^ the trait `AllowsNoContext` is not implemented for `RequiredContext<CallerContext>` | -18 | let _record: EncryptedAge = cipher.encrypt_as(&42u32, Default::default()).await.unwrap(); - | ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `CallerContext` +help: the following other types implement trait `AllowsNoContext` + --> src/target/input.rs + | + | impl AllowsNoContext for NoCallerContext { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `NoCallerContext` +... + | impl<C> AllowsNoContext for OptionalContext<C> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `OptionalContext<C>` +... + | impl AllowsNoContext for DeclaredScopes { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `DeclaredScopes` +note: required by a bound in `encrypt_into` + --> src/target/conversion.rs + | + | fn encrypt_into<'a, T, K: 'static>(self, cipher: &'a KeysetCipher<'_, K>) -> Pending<'a, T, K> + | ------------ required by a bound in this associated function +... + | T::ContextInput: AllowsNoContext, + | ^^^^^^^^^^^^^^^ required by this bound in `EncryptInto::encrypt_into` -error[E0277]: the trait bound `AeadContext: Default` is not satisfied - --> tests/ui/leaf_without_context.rs:20:40 +error[E0277]: the trait bound `RequiredContext<AeadContext>: AllowsNoContext` is not satisfied + --> tests/ui/leaf_without_context.rs:19:41 + | +19 | let _column: Vec<StackCipherText> = vec![1u32].encrypt_into(keyset).await.unwrap(); + | ^^^^^^^^^^ ------------ required by a bound introduced by this call + | | + | the trait `AllowsNoContext` is not implemented for `RequiredContext<AeadContext>` + | +help: the following other types implement trait `AllowsNoContext` + --> src/target/input.rs | -20 | cipher.encrypt_as(&vec![1u32], Default::default()).await.unwrap(); - | ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `AeadContext` + | impl AllowsNoContext for NoCallerContext { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `NoCallerContext` +... + | impl<C> AllowsNoContext for OptionalContext<C> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `OptionalContext<C>` +... + | impl AllowsNoContext for DeclaredScopes { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `DeclaredScopes` +note: required by a bound in `encrypt_into` + --> src/target/conversion.rs + | + | fn encrypt_into<'a, T, K: 'static>(self, cipher: &'a KeysetCipher<'_, K>) -> Pending<'a, T, K> + | ------------ required by a bound in this associated function +... + | T::ContextInput: AllowsNoContext, + | ^^^^^^^^^^^^^^^ required by this bound in `EncryptInto::encrypt_into` -error[E0277]: the trait bound `CallerContext: Default` is not satisfied - --> tests/ui/leaf_without_context.rs:24:47 +error[E0277]: the trait bound `RequiredContext<CallerContext>: AllowsNoContext` is not satisfied + --> tests/ui/leaf_without_context.rs:23:34 + | +23 | let _age = u32::decrypt_from(record, cipher).await.unwrap(); + | ----------------- ^^^^^^ the trait `AllowsNoContext` is not implemented for `RequiredContext<CallerContext>` + | | + | required by a bound introduced by this call + | +help: the following other types implement trait `AllowsNoContext` + --> src/target/input.rs + | + | impl AllowsNoContext for NoCallerContext { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `NoCallerContext` +... + | impl<C> AllowsNoContext for OptionalContext<C> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `OptionalContext<C>` +... + | impl AllowsNoContext for DeclaredScopes { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `DeclaredScopes` +note: required by a bound in `decrypt_from` + --> src/target/conversion.rs | -24 | let _age: u32 = cipher.decrypt_as(record, Default::default()).await.unwrap(); - | ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `CallerContext` + | fn decrypt_from<'a, S, K: 'static>( + | ------------ required by a bound in this associated function +... + | S::ContextInput: AllowsNoContext, + | ^^^^^^^^^^^^^^^ required by this bound in `DecryptFrom::decrypt_from` error[E0277]: the trait bound `CallerContext: From<()>` is not satisfied - --> tests/ui/leaf_without_context.rs:28:50 + --> tests/ui/leaf_without_context.rs:28:44 | -28 | let _age: u32 = cipher.decrypt_as(record, ().into()).await.unwrap(); - | ^^^^ the trait `From<()>` is not implemented for `CallerContext` +28 | .encrypt_into_with_context(keyset, ()) + | ------------------------- ^^ the trait `From<()>` is not implemented for `CallerContext` + | | + | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: `CallerContext` implements `From<&NonEmpty<T>>` @@ -39,3 +125,39 @@ error[E0277]: the trait bound `CallerContext: From<()>` is not satisfied `CallerContext` implements `From<i64>` and $N others = note: required for `()` to implement `Into<CallerContext>` +note: required by a bound in `encrypt_into_with_context` + --> src/target/conversion.rs + | + | fn encrypt_into_with_context<'a, T, K: 'static>( + | ------------------------- required by a bound in this associated function +... + | context: impl Into<<T::ContextInput as AcceptsContext>::Value>, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `EncryptInto::encrypt_into_with_context` + +error[E0277]: the trait bound `CallerContext: From<()>` is not satisfied + --> tests/ui/leaf_without_context.rs:31:63 + | +31 | let _age = u32::decrypt_from_with_context(record, keyset, ()) + | ------------------------------ ^^ the trait `From<()>` is not implemented for `CallerContext` + | | + | required by a bound introduced by this call + | + = help: the following other types implement trait `From<T>`: + `CallerContext` implements `From<&NonEmpty<T>>` + `CallerContext` implements `From<NonEmpty<T>>` + `CallerContext` implements `From<Parts<S, B>>` + `CallerContext` implements `From<RequiredContext<C>>` + `CallerContext` implements `From<i128>` + `CallerContext` implements `From<i16>` + `CallerContext` implements `From<i32>` + `CallerContext` implements `From<i64>` + and $N others + = note: required for `()` to implement `Into<CallerContext>` +note: required by a bound in `decrypt_from_with_context` + --> src/target/conversion.rs + | + | fn decrypt_from_with_context<'a, S, K: 'static>( + | ------------------------- required by a bound in this associated function +... + | context: impl Into<<S::ContextInput as AcceptsContext>::Value>, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `DecryptFrom::decrypt_from_with_context` diff --git a/packages/stack-encrypt/tests/ui/marker_operator_not_answered.stderr b/packages/stack-encrypt/tests/ui/marker_operator_not_answered.stderr index 43e99acf19..954cb54237 100644 --- a/packages/stack-encrypt/tests/ui/marker_operator_not_answered.stderr +++ b/packages/stack-encrypt/tests/ui/marker_operator_not_answered.stderr @@ -6,7 +6,7 @@ error[E0277]: `EqualityTerm` does not answer `stack_encrypt::op::Gt` given `_` | | | required by a bound introduced by this call | - = note: a target answers the operators its type implements `Answers` for, and a tuple of terms answers each operator exactly one of its terms answers; on a plan's query chain, `.via(index)` names an index the target declares + = note: a target answers the operators its type implements `Answers` for, and a tuple of terms answers each operator exactly one of its terms answers help: the trait `Answers<stack_encrypt::op::Gt, _>` is not implemented for `EqualityTerm` but trait `Answers<stack_encrypt::op::Eq, _>` is implemented for it --> src/op/mod.rs @@ -45,7 +45,7 @@ error[E0277]: `OreTerm<u32>` does not answer `stack_encrypt::op::Matches` given | required by a bound introduced by this call | = help: the trait `Answers<stack_encrypt::op::Matches, _>` is not implemented for `OreTerm<u32>` - = note: a target answers the operators its type implements `Answers` for, and a tuple of terms answers each operator exactly one of its terms answers; on a plan's query chain, `.via(index)` names an index the target declares + = note: a target answers the operators its type implements `Answers` for, and a tuple of terms answers each operator exactly one of its terms answers = help: the following other types implement trait `Answers<Op, In>`: `OreTerm<S>` implements `Answers<stack_encrypt::op::Between, (S, S)>` `OreTerm<S>` implements `Answers<stack_encrypt::op::Gt, S>` diff --git a/packages/stack-encrypt/tests/ui/nested_removed.rs b/packages/stack-encrypt/tests/ui/nested_removed.rs index 784789a852..55a08a5678 100644 --- a/packages/stack-encrypt/tests/ui/nested_removed.rs +++ b/packages/stack-encrypt/tests/ui/nested_removed.rs @@ -1,23 +1,26 @@ //! `#[stash(nested)]` is removed. A field whose type is a record is an //! ordinary field, derived under `("<context>", "<field>")`, with the //! record's own fields under that; the message says to omit the attribute. -use stack_encrypt::{EncryptFrom, StackCipherText}; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; +#[derive(Clone, zeroize::Zeroize)] struct User { email: String, } +#[derive(zeroize::Zeroize)] struct Account { user: User, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = User, scope = "users")] struct EncryptedUser { email: StackCipherText, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = Account, scope = "accounts")] struct EncryptedAccount { #[stash(nested)] diff --git a/packages/stack-encrypt/tests/ui/nested_removed.stderr b/packages/stack-encrypt/tests/ui/nested_removed.stderr index 2529230715..25f1d10f82 100644 --- a/packages/stack-encrypt/tests/ui/nested_removed.stderr +++ b/packages/stack-encrypt/tests/ui/nested_removed.stderr @@ -1,5 +1,5 @@ error: `nested` is no longer accepted (0.2.0 accepted it): a field whose type is a record is an ordinary field, derived under `("<scope>", "<field>")` like any other, and its own fields sit under that. Omit `nested`. That is a different context from the one `nested` gave the inner fields: data written with `nested` does not decrypt without it and its terms do not match, so re-encrypt that data - --> tests/ui/nested_removed.rs:23:13 + --> tests/ui/nested_removed.rs:26:13 | -23 | #[stash(nested)] +26 | #[stash(nested)] | ^^^^^^ diff --git a/packages/stack-encrypt/tests/ui/non_plain_inferred_context.rs b/packages/stack-encrypt/tests/ui/non_plain_inferred_context.rs index 85fc9cd19c..df8e2a38d5 100644 --- a/packages/stack-encrypt/tests/ui/non_plain_inferred_context.rs +++ b/packages/stack-encrypt/tests/ui/non_plain_inferred_context.rs @@ -1,21 +1,24 @@ -use stack_encrypt::{EncryptFrom, StackCipherText}; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; +#[derive(zeroize::Zeroize)] struct User { email: String, } // A schema-qualified table is a natural thing to write, and it would render // `b64:…/email`: the log would not name the table. -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = User, scope = "public/users")] struct SchemaQualified { email: StackCipherText, } +#[derive(zeroize::Zeroize)] struct Reading(u32); // A tuple index begins with a digit, which a descriptor reserves. -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = Reading, scope = "readings")] struct EncryptedReading(StackCipherText); diff --git a/packages/stack-encrypt/tests/ui/non_plain_inferred_context.stderr b/packages/stack-encrypt/tests/ui/non_plain_inferred_context.stderr index e3a7875a98..6e0c933b10 100644 --- a/packages/stack-encrypt/tests/ui/non_plain_inferred_context.stderr +++ b/packages/stack-encrypt/tests/ui/non_plain_inferred_context.stderr @@ -1,13 +1,13 @@ error: a container `scope` is the first segment of every field's ZeroKMS descriptor, so it must be plain: no `/`, `(`, `)`, control or invisible character, and not beginning with `b64:`, a digit or `-`; otherwise it would render escaped and the log would not name the table. For the table `public.users` write `scope = "users"` - --> tests/ui/non_plain_inferred_context.rs:10:32 + --> tests/ui/non_plain_inferred_context.rs:12:32 | -10 | #[stash(struct = User, scope = "public/users")] +12 | #[stash(struct = User, scope = "public/users")] | ^^^^^^^^^^^^^^ error: a tuple field has no name to infer a context from: its index `0` begins with a digit, which a descriptor reserves, so `("readings", "0")` would render escaped; give the field `#[stash(identity = "..")]` - --> tests/ui/non_plain_inferred_context.rs:18:10 + --> tests/ui/non_plain_inferred_context.rs:21:10 | -18 | #[derive(EncryptFrom)] - | ^^^^^^^^^^^ +21 | #[derive(EncryptTarget)] + | ^^^^^^^^^^^^^ | - = note: this error originates in the derive macro `EncryptFrom` (in Nightly builds, run with -Z macro-backtrace for more info) + = note: this error originates in the derive macro `EncryptTarget` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/packages/stack-encrypt/tests/ui/op_between_takes_a_pair.rs b/packages/stack-encrypt/tests/ui/op_between_takes_a_pair.rs index 8d3b358364..dc82d85e27 100644 --- a/packages/stack-encrypt/tests/ui/op_between_takes_a_pair.rs +++ b/packages/stack-encrypt/tests/ui/op_between_takes_a_pair.rs @@ -1,16 +1,24 @@ -//! `Between` takes a pair of bounds, and the other orderings one value: a -//! query given the wrong shape does not compile. -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::op; -use stack_encrypt::{Ore, Plan, StackCipher}; +//! `between` takes two bounds, and the other orderings one value: a query +//! given the wrong shape does not compile. +use stack_encrypt::sem::OreTerm; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::Encrypted; +use zeroize::Zeroize; -async fn query(cipher: &StackCipher<FakeDataKeySource>) { - let age_plan = Plan::context("users/age") - .with::<u32, _>(Ore) - .build() - .unwrap(); - let _ = cipher.query(op::Between, &40u32).on(&age_plan); - let _ = cipher.query(op::Lt, &(18u32, 65)).on(&age_plan); +#[derive(Clone, Zeroize)] +struct User { + age: u32, } -fn main() {} +#[derive(EncryptTarget)] +#[stash(struct = User, scope = "users")] +struct EncryptedUser { + age: Encrypted<OreTerm<u32>>, +} + +fn main() { + let _ = EncryptedUser::AGE.between(18u32, 65); + let _ = EncryptedUser::AGE.lt(40u32); + let _ = EncryptedUser::AGE.between(40u32); + let _ = EncryptedUser::AGE.lt((18u32, 65u32)); +} diff --git a/packages/stack-encrypt/tests/ui/op_between_takes_a_pair.stderr b/packages/stack-encrypt/tests/ui/op_between_takes_a_pair.stderr index dbf1c98949..b132163b15 100644 --- a/packages/stack-encrypt/tests/ui/op_between_takes_a_pair.stderr +++ b/packages/stack-encrypt/tests/ui/op_between_takes_a_pair.stderr @@ -1,77 +1,51 @@ -error[E0277]: `u32` is not what a query of arity `Pair` takes - --> tests/ui/op_between_takes_a_pair.rs:12:39 +error[E0061]: this method takes 2 arguments but 1 argument was supplied + --> tests/ui/op_between_takes_a_pair.rs:22:32 | -12 | let _ = cipher.query(op::Between, &40u32).on(&age_plan); - | ----- ^^^^^^ the operator takes another input - | | - | required by a bound introduced by this call +22 | let _ = EncryptedUser::AGE.between(40u32); + | ^^^^^^^------- argument #2 of type `u32` is missing | - = help: the trait `Input<Pair>` is not implemented for `u32` - = note: an operator of arity `One` takes one value, `op::Between` (arity `Pair`) takes a pair, `&(lo, hi)`, and `op::AtPath` (arity `Pathed`) takes a path and the leaf's input, `&("$.a", value)` or `&("$.a", (lo, hi))` -help: the following other types implement trait `Input<A>` - --> src/op/mod.rs +note: method defined here + --> src/marker.rs | - | impl<S: 'static> Input<Pair> for (S, S) { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(S, S)` implements `Input<Pair>` + | pub fn between<Q>(self, lo: Q, hi: Q) -> BetweenQuery<T, Q::Value, C::Start> + | ^^^^^^^ +help: provide the argument | - ::: src/json/query.rs - | - | / impl<P, V> Input<Pathed<One>> for (P, V) - | | where - | | P: AsRef<str> + 'static, - | | V: Into<Value> + 'static, - | |_____________________________^ `(P, V)` implements `Input<Pathed<stack_encrypt::op::One>>` -... - | / impl<P, V> Input<Pathed<Pair>> for (P, (V, V)) - | | where - | | P: AsRef<str> + 'static, - | | V: Into<Value> + 'static, - | |_____________________________^ `(P, (V, V))` implements `Input<Pathed<Pair>>` -note: required by a bound in `plan::chain::<impl StackCipher<K>>::query` - --> src/plan/chain.rs - | - | pub fn query<'a, Op, Q>(&'a self, op: Op, value: &Q) -> QueryBuilder<'a, Op, F<Q>, K> - | ----- required by a bound in this associated function -... - | F<Q>: Input<Op::Arity>, - | ^^^^^^^^^^^^^^^^ required by this bound in `plan::chain::<impl StackCipher<K>>::query` - -error[E0599]: the method `on` exists for struct `QueryBuilder<'_, stack_encrypt::op::Between, u32, FakeDataKeySource>`, but its trait bounds were not satisfied - --> tests/ui/op_between_takes_a_pair.rs:12:47 - | -12 | let _ = cipher.query(op::Between, &40u32).on(&age_plan); - | ^^ - | - = note: the following trait bounds were not satisfied: - `u32: Input<Pair>` +22 | let _ = EncryptedUser::AGE.between(40u32, /* u32 */); + | +++++++++++ -error[E0271]: type mismatch resolving `<(u32, {integer}) as Input<One>>::Plaintext == u32` - --> tests/ui/op_between_takes_a_pair.rs:13:48 - | -13 | let _ = cipher.query(op::Lt, &(18u32, 65)).on(&age_plan); - | ^^ expected `u32`, found `(u32, {integer})` - | - = note: expected type `u32` - found tuple `(u32, {integer})` - = note: required for `ValuePlan<u32, Indexed<stack_encrypt::Ore>>` to implement `Chooses<stack_encrypt::op::Lt, (u32, {integer}), Whole>` - -error[E0277]: `(u32, {integer})` is not ordered: the ordering operators are answered over the integers of 16 bits and wider, the floats, text, dates, timestamps and decimals - --> tests/ui/op_between_takes_a_pair.rs:13:48 - | -13 | let _ = cipher.query(op::Lt, &(18u32, 65)).on(&age_plan); - | ^^ no ordering operator over this type - | - = help: the trait `Ordered` is not implemented for `(u32, {integer})` - = note: `Lt`, `Lte`, `Gt`, `Gte` and `Between` take `u16` to `u128`, `i16` to `i128`, `f32`, `f64`, a `String`, or a `value::Date`, `value::Timestamp` or `value::Decimal`; a `u8`, an `i8` and a `bool` have no order term - = help: the following other types implement trait `Ordered`: +error[E0277]: the trait bound `(u32, u32): QueryInput` is not satisfied + --> tests/ui/op_between_takes_a_pair.rs:23:35 + | +23 | let _ = EncryptedUser::AGE.lt((18u32, 65u32)); + | -- ^^^^^^^^^^^^^^ the trait `QueryInput` is not implemented for `(u32, u32)` + | | + | required by a bound introduced by this call + | + = help: the following other types implement trait `QueryInput`: + &Vec<u8> + &[u8] + &std::string::String + &str + Option<V> + Vec<u8> + bool f32 - f64 - i128 - i16 - i32 - i64 - stack_encrypt::value::Date - stack_encrypt::value::Decimal and $N others - = note: required for `stack_encrypt::op::Lt` to implement `DefaultIndex<(u32, {integer})>` - = note: required for `ValuePlan<u32, Indexed<stack_encrypt::Ore>>` to implement `Chooses<stack_encrypt::op::Lt, (u32, {integer}), Whole>` +note: required by a bound in `Marker::<W, T, C, P>::lt` + --> src/marker.rs + | + | Q: QueryInput, + | ^^^^^^^^^^ required by this bound in `Marker::<W, T, C, P>::lt` +... + | / operator_methods! { + | | /// Rows whose value equals `value` ([`op::Eq`](struct@Eq)). + | | eq => Eq; + | | /// Rows whose value is below `value` ([`op::Lt`](Lt)). + | | lt => Lt; + | | -- required by a bound in this associated function +... | + | | matches => Matches; + | | } + | |_- in this macro invocation + = note: this error originates in the macro `operator_methods` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/packages/stack-encrypt/tests/ui/op_json_not_answered.rs b/packages/stack-encrypt/tests/ui/op_json_not_answered.rs deleted file mode 100644 index 7d9f539522..0000000000 --- a/packages/stack-encrypt/tests/ui/op_json_not_answered.rs +++ /dev/null @@ -1,20 +0,0 @@ -//! The JSON operators are answered by the JSON index alone, over a -//! `serde_json::Value`: on a plan over text with an equality index, a -//! containment query and a query at a path do not compile, because the plan -//! declares no index that answers them and its plaintext is not a document. -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::op; -use stack_encrypt::{Equality, Plan, StackCipher}; - -async fn query(cipher: &StackCipher<FakeDataKeySource>) { - let profile_plan = Plan::context("users/profile") - .with::<String, _>(Equality) - .build() - .unwrap(); - let _ = cipher.query(op::Contains, "{}").on(&profile_plan); - let _ = cipher - .query(op::AtPath(op::Eq), &("$.address.city", "Sydney")) - .on(&profile_plan); -} - -fn main() {} diff --git a/packages/stack-encrypt/tests/ui/op_json_not_answered.stderr b/packages/stack-encrypt/tests/ui/op_json_not_answered.stderr deleted file mode 100644 index 913dd894ec..0000000000 --- a/packages/stack-encrypt/tests/ui/op_json_not_answered.stderr +++ /dev/null @@ -1,73 +0,0 @@ -error[E0277]: `stack_encrypt::op::Contains` has no default index given `std::string::String` - --> tests/ui/op_json_not_answered.rs:14:46 - | -14 | let _ = cipher.query(op::Contains, "{}").on(&profile_plan); - | ^^ no default index answers this operator here - | - = note: `op::Eq` defaults to `Equality`; `op::Matches` defaults to `Match` with the default configuration, which is case-insensitive and over text only; `op::Lt`, `Lte`, `Gt`, `Gte` and `Between` default to `Ore` over an integer, and `Between` takes a pair; name another index, such as `Ope`, with `.via(index)`; `op::Contains`, `op::ContainedBy` and `op::AtPath` default to `Json` over a `serde_json::Value` -help: the trait `DefaultIndex<std::string::String>` is not implemented for `stack_encrypt::op::Contains` - but trait `DefaultIndex<serde_json::value::Value>` is implemented for it - --> src/json/query.rs - | - | impl DefaultIndex<Value> for $op { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -... - | containment!(Contains, ContainedBy); - | ----------------------------------- in this macro invocation - = help: for that trait implementation, expected `serde_json::value::Value`, found `std::string::String` - = note: required for `ValuePlan<std::string::String, Indexed<stack_encrypt::Equality>>` to implement `Chooses<stack_encrypt::op::Contains, std::string::String, Whole>` - = note: this error originates in the macro `containment` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0271]: type mismatch resolving `<(&str, &str) as Input<Pathed<One>>>::Plaintext == String` - --> tests/ui/op_json_not_answered.rs:17:10 - | -17 | .on(&profile_plan); - | ^^ expected `String`, found `Value` - | - = note: required for `ValuePlan<std::string::String, Indexed<stack_encrypt::Equality>>` to implement `Chooses<AtPath<stack_encrypt::op::Eq>, (&str, &str), _>` - -error[E0277]: the trait bound `stack_encrypt::Equality: Select<stack_encrypt::Json, _>` is not satisfied - --> tests/ui/op_json_not_answered.rs:17:10 - | -17 | .on(&profile_plan); - | ^^ the trait `Select<stack_encrypt::Json, _>` is not implemented for `stack_encrypt::Equality` - | - = help: the following other types implement trait `Select<I, Position>`: - `(A, B)` implements `Select<A, At<0>>` - `(A, B)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<A, At<0>>` - `(A, B, C)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<C, At<2>>` - `(A, B, C, D)` implements `Select<A, At<0>>` - `(A, B, C, D)` implements `Select<B, At<1>>` - `(A, B, C, D)` implements `Select<C, At<2>>` - and $N others - = note: required for `ValuePlan<std::string::String, Indexed<stack_encrypt::Equality>>` to implement `Chooses<AtPath<stack_encrypt::op::Eq>, (&str, &str), _>` - -error[E0277]: the trait bound `stack_encrypt::Equality: Select<stack_encrypt::Json, _>` is not satisfied - --> tests/ui/op_json_not_answered.rs:17:13 - | -17 | .on(&profile_plan); - | -- ^^^^^^^^^^^^^ the trait `Select<stack_encrypt::Json, _>` is not implemented for `stack_encrypt::Equality` - | | - | required by a bound introduced by this call - | - = help: the following other types implement trait `Select<I, Position>`: - `(A, B)` implements `Select<A, At<0>>` - `(A, B)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<A, At<0>>` - `(A, B, C)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<C, At<2>>` - `(A, B, C, D)` implements `Select<A, At<0>>` - `(A, B, C, D)` implements `Select<B, At<1>>` - `(A, B, C, D)` implements `Select<C, At<2>>` - and $N others - = note: required for `ValuePlan<std::string::String, Indexed<stack_encrypt::Equality>>` to implement `Chooses<AtPath<stack_encrypt::op::Eq>, (&str, &str), _>` -note: required by a bound in `QueryBuilder::<'a, Op, F, K>::on` - --> src/plan/chain.rs - | - | pub fn on<P, At>(self, plan: &P) -> QueryOp<'a, F, Op, P::Via, K> - | -- required by a bound in this associated function - | where - | P: Chooses<Op, F, At>, - | ^^^^^^^^^^^^^^^^^^ required by this bound in `QueryBuilder::<'a, Op, F, K>::on` diff --git a/packages/stack-encrypt/tests/ui/op_matches_on_integer.rs b/packages/stack-encrypt/tests/ui/op_matches_on_integer.rs deleted file mode 100644 index 8408d9b3ff..0000000000 --- a/packages/stack-encrypt/tests/ui/op_matches_on_integer.rs +++ /dev/null @@ -1,11 +0,0 @@ -//! `Matches` is text search: a field of integers has no index that answers -//! it, so the query does not compile. -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::{op, Equality, Plan, StackCipher}; - -async fn query(cipher: &StackCipher<FakeDataKeySource>) { - let age_plan = Plan::context("users/age").with::<u32, _>(Equality).build().unwrap(); - let _ = cipher.query(op::Matches, &34u32).on(&age_plan); -} - -fn main() {} diff --git a/packages/stack-encrypt/tests/ui/op_matches_on_integer.stderr b/packages/stack-encrypt/tests/ui/op_matches_on_integer.stderr deleted file mode 100644 index 8711e3c6f7..0000000000 --- a/packages/stack-encrypt/tests/ui/op_matches_on_integer.stderr +++ /dev/null @@ -1,83 +0,0 @@ -error[E0277]: the trait bound `u32: AsRef<str>` is not satisfied - --> tests/ui/op_matches_on_integer.rs:8:47 - | -8 | let _ = cipher.query(op::Matches, &34u32).on(&age_plan); - | ^^ the trait `AsRef<str>` is not implemented for `u32` - | -help: the following other types implement trait `Chooses<Op, In, At>` - --> src/plan/chain.rs - | - | / impl<Op, S, In, X, At> Chooses<Op, In, At> for ValuePlan<S, Indexed<X>> - | | where - | | Op: DefaultIndex<In>, - | | Op::Via: Clone, - | | In: Input<Op::Arity, Plaintext = S>, - | | S: 'static, - | | X: Indexes<S> + Select<Op::Via, At>, - | |________________________________________^ `ValuePlan<S, Indexed<X>>` implements `Chooses<Op, In, At>` -... - | / impl<Op, S, In, T> Chooses<Op, In, Whole> for ValuePlan<S, Typed<T>> - | | where - | | Op: Operator, - | | In: Input<Op::Arity, Plaintext = S>, - | | S: 'static, - | | T: Answers<Op, In> + LabelledTarget, - | | Typed<T>: ValueLayout<S>, - | |_____________________________^ `ValuePlan<S, Typed<T>>` implements `Chooses<Op, In, Whole>` -... - | / impl<Op, S, In, X, T> Chooses<Op, In, Whole> for ValuePlan<S, Laid<X, T>> - | | where - | | Op: Operator, - | | In: Input<Op::Arity, Plaintext = S>, -... | - | | X::Index: Supports<Op, In> + Default, - | | Laid<X, T>: ValueShape<S>, - | |______________________________^ `ValuePlan<S, Laid<X, T>>` implements `Chooses<Op, In, Whole>` - = note: required for `stack_encrypt::op::Matches` to implement `DefaultIndex<u32>` - = note: required for `ValuePlan<u32, Indexed<stack_encrypt::Equality>>` to implement `Chooses<stack_encrypt::op::Matches, u32, _>` - -error[E0277]: the trait bound `stack_encrypt::Equality: Select<stack_encrypt::Match, _>` is not satisfied - --> tests/ui/op_matches_on_integer.rs:8:47 - | -8 | let _ = cipher.query(op::Matches, &34u32).on(&age_plan); - | ^^ the trait `Select<stack_encrypt::Match, _>` is not implemented for `stack_encrypt::Equality` - | - = help: the following other types implement trait `Select<I, Position>`: - `(A, B)` implements `Select<A, At<0>>` - `(A, B)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<A, At<0>>` - `(A, B, C)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<C, At<2>>` - `(A, B, C, D)` implements `Select<A, At<0>>` - `(A, B, C, D)` implements `Select<B, At<1>>` - `(A, B, C, D)` implements `Select<C, At<2>>` - and $N others - = note: required for `ValuePlan<u32, Indexed<stack_encrypt::Equality>>` to implement `Chooses<stack_encrypt::op::Matches, u32, _>` - -error[E0277]: the trait bound `stack_encrypt::Equality: Select<stack_encrypt::Match, _>` is not satisfied - --> tests/ui/op_matches_on_integer.rs:8:50 - | -8 | let _ = cipher.query(op::Matches, &34u32).on(&age_plan); - | -- ^^^^^^^^^ the trait `Select<stack_encrypt::Match, _>` is not implemented for `stack_encrypt::Equality` - | | - | required by a bound introduced by this call - | - = help: the following other types implement trait `Select<I, Position>`: - `(A, B)` implements `Select<A, At<0>>` - `(A, B)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<A, At<0>>` - `(A, B, C)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<C, At<2>>` - `(A, B, C, D)` implements `Select<A, At<0>>` - `(A, B, C, D)` implements `Select<B, At<1>>` - `(A, B, C, D)` implements `Select<C, At<2>>` - and $N others - = note: required for `ValuePlan<u32, Indexed<stack_encrypt::Equality>>` to implement `Chooses<stack_encrypt::op::Matches, u32, _>` -note: required by a bound in `QueryBuilder::<'a, Op, F, K>::on` - --> src/plan/chain.rs - | - | pub fn on<P, At>(self, plan: &P) -> QueryOp<'a, F, Op, P::Via, K> - | -- required by a bound in this associated function - | where - | P: Chooses<Op, F, At>, - | ^^^^^^^^^^^^^^^^^^ required by this bound in `QueryBuilder::<'a, Op, F, K>::on` diff --git a/packages/stack-encrypt/tests/ui/op_order_on_bytes.rs b/packages/stack-encrypt/tests/ui/op_order_on_bytes.rs index a2cecbbf90..fd8de3cd84 100644 --- a/packages/stack-encrypt/tests/ui/op_order_on_bytes.rs +++ b/packages/stack-encrypt/tests/ui/op_order_on_bytes.rs @@ -1,16 +1,22 @@ //! The ordering operators are answered over the integer types, the floats and text. //! Bytes have no order key the ordering operators compare, so an ordering on -//! a bytes plan does not compile, even where the plan declares an ORE index. -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::op; -use stack_encrypt::{Ore, Plan, StackCipher}; +//! a bytes field does not compile, even where the field declares an ORE index. +use stack_encrypt::sem::OreTerm; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::Encrypted; +use zeroize::Zeroize; -async fn query(cipher: &StackCipher<FakeDataKeySource>) { - let blob_plan = Plan::context("users/blob") - .with::<Vec<u8>, _>(Ore) - .build() - .unwrap(); - let _ = cipher.query(op::Lt, &vec![1u8, 2]).on(&blob_plan); +#[derive(Clone, Zeroize)] +struct File { + blob: Vec<u8>, } -fn main() {} +#[derive(EncryptTarget)] +#[stash(struct = File, scope = "files")] +struct EncryptedFile { + blob: Encrypted<OreTerm<Vec<u8>>>, +} + +fn main() { + let _ = EncryptedFile::BLOB.lt(vec![1u8, 2]); +} diff --git a/packages/stack-encrypt/tests/ui/op_order_on_bytes.stderr b/packages/stack-encrypt/tests/ui/op_order_on_bytes.stderr index 22713b0a9b..4ee642acc3 100644 --- a/packages/stack-encrypt/tests/ui/op_order_on_bytes.stderr +++ b/packages/stack-encrypt/tests/ui/op_order_on_bytes.stderr @@ -1,8 +1,10 @@ error[E0277]: `Vec<u8>` is not ordered: the ordering operators are answered over the integers of 16 bits and wider, the floats, text, dates, timestamps and decimals - --> tests/ui/op_order_on_bytes.rs:13:49 + --> tests/ui/op_order_on_bytes.rs:21:36 | -13 | let _ = cipher.query(op::Lt, &vec![1u8, 2]).on(&blob_plan); - | ^^ no ordering operator over this type +21 | let _ = EncryptedFile::BLOB.lt(vec![1u8, 2]); + | -- ^^^^^^^^^^^^ no ordering operator over this type + | | + | required by a bound introduced by this call | = help: the trait `Ordered` is not implemented for `Vec<u8>` = note: `Lt`, `Lte`, `Gt`, `Gte` and `Between` take `u16` to `u128`, `i16` to `i128`, `f32`, `f64`, a `String`, or a `value::Date`, `value::Timestamp` or `value::Decimal`; a `u8`, an `i8` and a `bool` have no order term @@ -16,5 +18,23 @@ error[E0277]: `Vec<u8>` is not ordered: the ordering operators are answered over stack_encrypt::value::Date stack_encrypt::value::Decimal and $N others - = note: required for `stack_encrypt::op::Lt` to implement `DefaultIndex<Vec<u8>>` - = note: required for `ValuePlan<Vec<u8>, Indexed<stack_encrypt::Ore>>` to implement `Chooses<stack_encrypt::op::Lt, Vec<u8>, Whole>` + = note: required for `OreTerm<Vec<u8>>` to implement `Answers<stack_encrypt::op::Lt, Vec<u8>>` + = note: 1 redundant requirement hidden + = note: required for `Encrypted<OreTerm<Vec<u8>>>` to implement `Answers<stack_encrypt::op::Lt, Vec<u8>>` +note: required by a bound in `Marker::<W, T, C, P>::lt` + --> src/marker.rs + | + | T: Answers<$op, Q::Value> + LabelledTarget, + | ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Marker::<W, T, C, P>::lt` +... + | / operator_methods! { + | | /// Rows whose value equals `value` ([`op::Eq`](struct@Eq)). + | | eq => Eq; + | | /// Rows whose value is below `value` ([`op::Lt`](Lt)). + | | lt => Lt; + | | -- required by a bound in this associated function +... | + | | matches => Matches; + | | } + | |_- in this macro invocation + = note: this error originates in the macro `operator_methods` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/packages/stack-encrypt/tests/ui/op_target_does_not_answer.rs b/packages/stack-encrypt/tests/ui/op_target_does_not_answer.rs deleted file mode 100644 index 3dee1da94c..0000000000 --- a/packages/stack-encrypt/tests/ui/op_target_does_not_answer.rs +++ /dev/null @@ -1,16 +0,0 @@ -//! A target answers only the operators its type implements `Answers` for: -//! an equality term is not searchable text. -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::{op, Plan, StackCipher}; - -async fn query(cipher: &StackCipher<FakeDataKeySource>) { - let email_plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<EqualityTerm>() - .build() - .unwrap(); - let _ = cipher.query(op::Matches, "bob").on(&email_plan); -} - -fn main() {} diff --git a/packages/stack-encrypt/tests/ui/op_target_does_not_answer.stderr b/packages/stack-encrypt/tests/ui/op_target_does_not_answer.stderr deleted file mode 100644 index b839b4ab95..0000000000 --- a/packages/stack-encrypt/tests/ui/op_target_does_not_answer.stderr +++ /dev/null @@ -1,17 +0,0 @@ -error[E0277]: `EqualityTerm` does not answer `stack_encrypt::op::Matches` given `std::string::String` - --> tests/ui/op_target_does_not_answer.rs:13:46 - | -13 | let _ = cipher.query(op::Matches, "bob").on(&email_plan); - | ^^ this target type does not answer the operator - | - = note: a target answers the operators its type implements `Answers` for, and a tuple of terms answers each operator exactly one of its terms answers; on a plan's query chain, `.via(index)` names an index the target declares -help: the trait `Answers<stack_encrypt::op::Matches, std::string::String>` is not implemented for `EqualityTerm` - but trait `Answers<stack_encrypt::op::Eq, std::string::String>` is implemented for it - --> src/op/mod.rs - | - | / impl<S> Answers<Eq, S> for EqualityTerm - | | where - | | S: crate::Encrypt + Clone + 'static, - | |________________________________________^ - = help: for that trait implementation, expected `stack_encrypt::op::Eq`, found `stack_encrypt::op::Matches` - = note: required for `ValuePlan<std::string::String, Typed<EqualityTerm>>` to implement `Chooses<stack_encrypt::op::Matches, std::string::String, Whole>` diff --git a/packages/stack-encrypt/tests/ui/op_target_not_labelled.rs b/packages/stack-encrypt/tests/ui/op_target_not_labelled.rs index 7fd858c9c2..034d630c1b 100644 --- a/packages/stack-encrypt/tests/ui/op_target_not_labelled.rs +++ b/packages/stack-encrypt/tests/ui/op_target_not_labelled.rs @@ -1,24 +1,21 @@ //! A target sealed under a context no label names (here an integer tenant -//! id) is not a `LabelledTarget`: a query on its plan would derive under -//! the label and match nothing, so it does not compile, though the type -//! implements `Answers`. (A named index needs the type to accept the label -//! as its context, which it does not either.) -use stack_encrypt::kms::FakeDataKeySource; +//! id) is not a `LabelledTarget`: a query from a marker would derive under +//! the marker's label and match nothing, so it does not compile, though the +//! type implements `Answers`. +use stack_encrypt::marker::{Marker, Root}; use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::target::{EncryptFrom, Encryption, IndexSpec}; -use stack_encrypt::{op, Equality, NonEmpty, Plan, StackCipher}; +use stack_encrypt::target::{equality, EncryptTarget, Encryption, IndexSpec, RequiredContext}; +use stack_encrypt::{op, Equality, NonEmpty}; struct ByTenant; -impl EncryptFrom<String> for ByTenant { - type Context = NonEmpty<u64>; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> +impl EncryptTarget<String> for ByTenant { + type ContextInput = RequiredContext<NonEmpty<u64>>; + fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::ContextInput> where String: 's, { - <EqualityTerm as EncryptFrom<String>>::encryption() - .accepting::<NonEmpty<u64>>() - .map(|_| ByTenant) + equality().accepting().map(|_: EqualityTerm| ByTenant) } fn indexes() -> Vec<IndexSpec> { vec![IndexSpec::Equality] @@ -29,13 +26,7 @@ impl op::Answers<op::Eq, String> for ByTenant { type Via = Equality; } -async fn query(cipher: &StackCipher<FakeDataKeySource>) { - let plan = Plan::value::<String>() - .context("users/email") - .encrypt_into::<ByTenant>() - .build() - .unwrap(); - let _ = cipher.query(op::Eq, "bob").on(&plan); +fn main() { + let email = Marker::<Root<()>, ByTenant>::new("email", "users", "email", || unreachable!()); + let _ = email.eq("bob"); } - -fn main() {} diff --git a/packages/stack-encrypt/tests/ui/op_target_not_labelled.stderr b/packages/stack-encrypt/tests/ui/op_target_not_labelled.stderr index ae9bce1eb3..917ebf8adc 100644 --- a/packages/stack-encrypt/tests/ui/op_target_not_labelled.stderr +++ b/packages/stack-encrypt/tests/ui/op_target_not_labelled.stderr @@ -1,15 +1,15 @@ -error[E0277]: `ByTenant` is not queried under a plan's label - --> tests/ui/op_target_not_labelled.rs:38:41 +error[E0277]: `ByTenant` is not queried under a field's label + --> tests/ui/op_target_not_labelled.rs:31:19 | -38 | let _ = cipher.query(op::Eq, "bob").on(&plan); - | ^^ a typed plan for this target type answers no query +31 | let _ = email.eq("bob"); + | ^^ a field of this target type answers no query | help: the trait `LabelledTarget` is not implemented for `ByTenant` - --> tests/ui/op_target_not_labelled.rs:11:1 + --> tests/ui/op_target_not_labelled.rs:10:1 | -11 | struct ByTenant; +10 | struct ByTenant; | ^^^^^^^^^^^^^^^ - = note: a target type answers queries on its plan when it implements `op::LabelledTarget`: its stored terms were derived under the context the plan's label names + = note: a target type answers queries when it implements `op::LabelledTarget`: its stored terms were derived under the context the field's label names = help: the following other types implement trait `LabelledTarget`: (A, B) (A, B, C) @@ -20,4 +20,19 @@ help: the trait `LabelledTarget` is not implemented for `ByTenant` EqualityTerm JsonDocument and $N others - = note: required for `ValuePlan<std::string::String, Typed<ByTenant>>` to implement `Chooses<stack_encrypt::op::Eq, std::string::String, Whole>` +note: required by a bound in `Marker::<W, T, C, P>::eq` + --> src/marker.rs + | + | T: Answers<$op, Q::Value> + LabelledTarget, + | ^^^^^^^^^^^^^^ required by this bound in `Marker::<W, T, C, P>::eq` +... + | / operator_methods! { + | | /// Rows whose value equals `value` ([`op::Eq`](struct@Eq)). + | | eq => Eq; + | | -- required by a bound in this associated function + | | /// Rows whose value is below `value` ([`op::Lt`](Lt)). +... | + | | matches => Matches; + | | } + | |_- in this macro invocation + = note: this error originates in the macro `operator_methods` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/packages/stack-encrypt/tests/ui/op_verify_exact_term.rs b/packages/stack-encrypt/tests/ui/op_verify_exact_term.rs index d83af50558..da941a9302 100644 --- a/packages/stack-encrypt/tests/ui/op_verify_exact_term.rs +++ b/packages/stack-encrypt/tests/ui/op_verify_exact_term.rs @@ -1,15 +1,30 @@ //! An exact query term has nothing to verify: the server's answer is the -//! answer, so `verify` on an equality term does not compile. +//! answer, so `verify` on the term of an equality query does not compile. use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::op::{self, Probabilistic}; -use stack_encrypt::{Equality, Plan, StackCipher}; +use stack_encrypt::op::Probabilistic; +use stack_encrypt::sem::EqualityTerm; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::{Encrypted, StackCipher}; +use zeroize::Zeroize; + +#[derive(Clone, Zeroize)] +struct User { + email: String, +} + +#[derive(EncryptTarget)] +#[stash(struct = User, scope = "users")] +struct EncryptedUser { + email: Encrypted<EqualityTerm>, +} async fn query(cipher: &StackCipher<FakeDataKeySource>) { - let plan = Plan::context("users/email") - .with::<String, _>(Equality) - .build() + let keyset = cipher.default_keyset(); + let term = EncryptedUser::EMAIL + .eq("bob") + .encrypt(&keyset) + .await .unwrap(); - let term = cipher.query(op::Eq, "bob").on(&plan).await.unwrap(); let _ = term.verify("bob"); } diff --git a/packages/stack-encrypt/tests/ui/op_verify_exact_term.stderr b/packages/stack-encrypt/tests/ui/op_verify_exact_term.stderr index 2ffdf93b3c..424656e4b2 100644 --- a/packages/stack-encrypt/tests/ui/op_verify_exact_term.stderr +++ b/packages/stack-encrypt/tests/ui/op_verify_exact_term.stderr @@ -1,5 +1,5 @@ error[E0599]: no method named `verify` found for struct `EqualityTerm` in the current scope - --> tests/ui/op_verify_exact_term.rs:13:18 + --> tests/ui/op_verify_exact_term.rs:28:18 | -13 | let _ = term.verify("bob"); +28 | let _ = term.verify("bob"); | ^^^^^^ method not found in `EqualityTerm` diff --git a/packages/stack-encrypt/tests/ui/order_term_of_an_8_bit_integer.rs b/packages/stack-encrypt/tests/ui/order_term_of_an_8_bit_integer.rs index 9a47e964b0..5435964b6d 100644 --- a/packages/stack-encrypt/tests/ui/order_term_of_an_8_bit_integer.rs +++ b/packages/stack-encrypt/tests/ui/order_term_of_an_8_bit_integer.rs @@ -1,19 +1,17 @@ //! A `u8` and an `i8` have no order term, as a `bool` has none: with 256 //! values, an order term would rank every row among 256, which a few known -//! rows resolve to the values. So neither is `sem::Orderable` or -//! `op::Ordered`: an `OreTerm<u8>`, a plan with `Ore` on an `i8` and an -//! ordering query over a `u8` do not compile. Each keeps its equality term. -use stack_encrypt::kms::FakeDataKeySource; +//! rows resolve to the values. So neither is `sem::Orderable`: an +//! `OreTerm<u8>`, an `OpeTerm<i8>` and an `Ore` index on an `i8` do not +//! compile. A field with no order term has no ordering query. Each keeps +//! its equality term. use stack_encrypt::sem::{OpeTerm, OreTerm}; -use stack_encrypt::{op, Ore, Plan, StackCipher}; +use stack_encrypt::target::{indexed, Borrowed, Ore}; +use stack_kms::FakeDataKeySource; fn terms(ore: OreTerm<u8>, ope: OpeTerm<i8>) { let _ = (ore, ope); } -async fn query(cipher: &StackCipher<FakeDataKeySource>) { - let rank_plan = Plan::context("users/rank").with::<i8, _>(Ore).build().unwrap(); - let _ = cipher.query(op::Lt, &7u8).on(&rank_plan); +fn main() { + let _ = indexed::<i8, FakeDataKeySource, Borrowed, _>(Ore); } - -fn main() {} diff --git a/packages/stack-encrypt/tests/ui/order_term_of_an_8_bit_integer.stderr b/packages/stack-encrypt/tests/ui/order_term_of_an_8_bit_integer.stderr index 4ccf281470..0144abe909 100644 --- a/packages/stack-encrypt/tests/ui/order_term_of_an_8_bit_integer.stderr +++ b/packages/stack-encrypt/tests/ui/order_term_of_an_8_bit_integer.stderr @@ -1,7 +1,7 @@ error[E0277]: the trait bound `u8: Orderable` is not satisfied - --> tests/ui/order_term_of_an_8_bit_integer.rs:10:15 + --> tests/ui/order_term_of_an_8_bit_integer.rs:11:15 | -10 | fn terms(ore: OreTerm<u8>, ope: OpeTerm<i8>) { +11 | fn terms(ore: OreTerm<u8>, ope: OpeTerm<i8>) { | ^^^^^^^^^^^ the trait `Orderable` is not implemented for `u8` | = help: the following other types implement trait `Orderable`: @@ -21,9 +21,9 @@ note: required by a bound in `OreTerm` | ^^^^^^^^^ required by this bound in `OreTerm` error[E0277]: the trait bound `i8: Orderable` is not satisfied - --> tests/ui/order_term_of_an_8_bit_integer.rs:10:33 + --> tests/ui/order_term_of_an_8_bit_integer.rs:11:33 | -10 | fn terms(ore: OreTerm<u8>, ope: OpeTerm<i8>) { +11 | fn terms(ore: OreTerm<u8>, ope: OpeTerm<i8>) { | ^^^^^^^^^^^ the trait `Orderable` is not implemented for `i8` | = help: the following other types implement trait `Orderable`: @@ -43,12 +43,10 @@ note: required by a bound in `OpeTerm` | ^^^^^^^^^ required by this bound in `OpeTerm` error[E0277]: the trait bound `i8: Orderable` is not satisfied - --> tests/ui/order_term_of_an_8_bit_integer.rs:15:60 + --> tests/ui/order_term_of_an_8_bit_integer.rs:16:13 | -15 | let rank_plan = Plan::context("users/rank").with::<i8, _>(Ore).build().unwrap(); - | ---- ^ the trait `Orderable` is not implemented for `i8` - | | - | required by a bound introduced by this call +16 | let _ = indexed::<i8, FakeDataKeySource, Borrowed, _>(Ore); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Orderable` is not implemented for `i8` | = help: the following other types implement trait `Orderable`: f32 @@ -61,24 +59,3 @@ error[E0277]: the trait bound `i8: Orderable` is not satisfied u16 and $N others = note: required for `stack_encrypt::Ore` to implement `stack_encrypt::Index<i8>` - = note: required for `stack_encrypt::Ore` to implement `Indexes<i8>` -note: required by a bound in `plan::value::<impl PlanContext>::with` - --> src/plan/value.rs - | - | pub fn with<S, X: Indexes<S>>(self, indexes: X) -> ValuePlanBuilder<S, Indexed<X>> { - | ^^^^^^^^^^ required by this bound in `plan::value::<impl PlanContext>::with` - -error[E0599]: the method `build` exists for struct `ValuePlanBuilder<i8, Indexed<stack_encrypt::Ore>>`, but its trait bounds were not satisfied - --> tests/ui/order_term_of_an_8_bit_integer.rs:15:68 - | -15 | let rank_plan = Plan::context("users/rank").with::<i8, _>(Ore).build().unwrap(); - | ^^^^^ method cannot be called on `ValuePlanBuilder<i8, Indexed<stack_encrypt::Ore>>` due to unsatisfied trait bounds - | - ::: src/plan/value.rs - | - | pub struct Indexed<X>(X); - | --------------------- doesn't satisfy `Indexed<stack_encrypt::Ore>: ValueLayout<i8>` - | - = note: the following trait bounds were not satisfied: - `i8: Orderable` - which is required by `Indexed<stack_encrypt::Ore>: ValueLayout<i8>` diff --git a/packages/stack-encrypt/tests/ui/override_encryption.rs b/packages/stack-encrypt/tests/ui/override_encryption.rs index a3710319e3..f2d5c03bff 100644 --- a/packages/stack-encrypt/tests/ui/override_encryption.rs +++ b/packages/stack-encrypt/tests/ui/override_encryption.rs @@ -1,10 +1,20 @@ -use stack_encrypt::{EncryptFrom, KeysetCipher, Pending}; +//! A declaration describes the encryption, and the library runs it. The +//! trait has no method that is handed the plaintext together with a cipher, +//! so a target cannot add one. +use stack_encrypt::target::{EncryptTarget, NoCallerContext}; +use stack_encrypt::{KeysetCipher, Pending}; + struct Target; -impl EncryptFrom<u32> for Target { - type Context = (); - // The old extension is deliberately not part of the declaration trait. - fn encrypt_from<'a,K>(_: &u32, cipher: &'a KeysetCipher<'_,K>, _:())->Pending<'a,Self,K> { + +impl EncryptTarget<u32> for Target { + type ContextInput = NoCallerContext; + fn encrypt_from<'a, K>( + _: &u32, + cipher: &'a KeysetCipher<'_, K>, + _: NoCallerContext, + ) -> Pending<'a, Self, K> { Pending::failed(cipher, stack_encrypt::Error::Aead) } } + fn main() {} diff --git a/packages/stack-encrypt/tests/ui/override_encryption.stderr b/packages/stack-encrypt/tests/ui/override_encryption.stderr index e6e84667af..425c55f2fe 100644 --- a/packages/stack-encrypt/tests/ui/override_encryption.stderr +++ b/packages/stack-encrypt/tests/ui/override_encryption.stderr @@ -1,18 +1,22 @@ -error[E0407]: method `encrypt_from` is not a member of trait `EncryptFrom` - --> tests/ui/override_encryption.rs:6:5 - | -6 | fn encrypt_from<'a,K>(_: &u32, cipher: &'a KeysetCipher<'_,K>, _:())->Pending<'a,Self,K> { - | ^ ------------ help: there is an associated function with a similar name: `encryption` - | _____| - | | -7 | | Pending::failed(cipher, stack_encrypt::Error::Aead) -8 | | } - | |_____^ not a member of trait `EncryptFrom` +error[E0407]: method `encrypt_from` is not a member of trait `EncryptTarget` + --> tests/ui/override_encryption.rs:11:5 + | +11 | fn encrypt_from<'a, K>( + | ^ ------------ help: there is an associated function with a similar name: `encryption` + | _____| + | | +12 | | _: &u32, +13 | | cipher: &'a KeysetCipher<'_, K>, +14 | | _: NoCallerContext, +15 | | ) -> Pending<'a, Self, K> { +16 | | Pending::failed(cipher, stack_encrypt::Error::Aead) +17 | | } + | |_____^ not a member of trait `EncryptTarget` error[E0046]: not all trait items implemented, missing: `encryption` - --> tests/ui/override_encryption.rs:3:1 + --> tests/ui/override_encryption.rs:9:1 | -3 | impl EncryptFrom<u32> for Target { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `encryption` in implementation +9 | impl EncryptTarget<u32> for Target { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `encryption` in implementation | - = help: implement the missing item: `fn encryption<'s, K>() -> Encryption<'s, u32, Self, K, <Self as EncryptFrom<u32>>::Context> { todo!() }` + = help: implement the missing item: `fn encryption<'s, K>() -> Encryption<'s, u32, Self, K, <Self as EncryptTarget<u32>>::ContextInput> { todo!() }` diff --git a/packages/stack-encrypt/tests/ui/owned_aead_record_with_term.stderr b/packages/stack-encrypt/tests/ui/owned_aead_record_with_term.stderr index d73640a1be..5e130aaf90 100644 --- a/packages/stack-encrypt/tests/ui/owned_aead_record_with_term.stderr +++ b/packages/stack-encrypt/tests/ui/owned_aead_record_with_term.stderr @@ -7,22 +7,13 @@ error[E0277]: the trait bound `RequiredContext<CallerContext>: From<RequiredCont 12 | hm: EqualityTerm, | ------------ required by a bound introduced by this call | -help: the following other types implement trait `From<T>` +help: the trait `From<RequiredContext<AeadContext>>` is not implemented for `RequiredContext<CallerContext>` + but trait `From<RequiredContext<NonEmpty<_>>>` is implemented for it --> src/target/input.rs | - | impl<C: From<CallerContext>> From<CallerContext> for RequiredContext<C> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `RequiredContext<C>` implements `From<CallerContext>` -... - | impl From<RequiredContext<CallerContext>> for RequiredContext<super::AeadContext> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `RequiredContext<AeadContext>` implements `From<RequiredContext<CallerContext>>` -... - | impl From<super::AeadContext> for RequiredContext<super::AeadContext> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `RequiredContext<AeadContext>` implements `From<AeadContext>` - | - ::: src/target/context.rs - | - | impl<S: ScopePart, B: BindPart> From<Parts<S, B>> for super::input::RequiredContext<Parts<S, B>> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `RequiredContext<Parts<S, B>>` implements `From<Parts<S, B>>` + | impl<'c, T: IntoContext<'c>> From<RequiredContext<NonEmpty<T>>> for RequiredContext<CallerContext> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = help: for that trait implementation, expected `NonEmpty<_>`, found `AeadContext` = note: required for `RequiredContext<AeadContext>` to implement `Into<RequiredContext<CallerContext>>` note: required by a bound in `Encryption::<'s, S, T, K, Ctx, M>::accepting` --> src/target/operations.rs diff --git a/packages/stack-encrypt/tests/ui/owned_struct_plaintext_not_zeroize.stderr b/packages/stack-encrypt/tests/ui/owned_struct_plaintext_not_zeroize.stderr index e90ea890ba..71688186d5 100644 --- a/packages/stack-encrypt/tests/ui/owned_struct_plaintext_not_zeroize.stderr +++ b/packages/stack-encrypt/tests/ui/owned_struct_plaintext_not_zeroize.stderr @@ -110,7 +110,7 @@ help: the trait `zeroize::DefaultIsZeroes` is not implemented for `User` i16 and $N others = note: required for `User` to implement `Zeroize` -note: required by a bound in `stack_encrypt::EncryptTarget::encryption` +note: required by a bound in `encryption` --> src/target/conversion.rs | | pub trait EncryptTarget<S: Zeroize>: Sized + 'static { @@ -118,34 +118,3 @@ note: required by a bound in `stack_encrypt::EncryptTarget::encryption` ... | fn encryption<'s, K: 'static>() -> Encryption<'s, S, Self, K, Self::ContextInput> | ---------- required by a bound in this associated function - -error[E0277]: the trait bound `User: Zeroize` is not satisfied - --> tests/ui/owned_struct_plaintext_not_zeroize.rs:12:18 - | -12 | #[stash(struct = User, scope = "users")] - | ^^^^ unsatisfied trait bound - | -help: the trait `zeroize::DefaultIsZeroes` is not implemented for `User` - --> tests/ui/owned_struct_plaintext_not_zeroize.rs:7:1 - | - 7 | struct User { - | ^^^^^^^^^^^ - = help: the following other types implement trait `zeroize::DefaultIsZeroes`: - () - PhantomPinned - bool - char - f32 - f64 - i128 - i16 - and $N others - = note: required for `User` to implement `Zeroize` -note: required by a bound in `stack_encrypt::EncryptTarget::indexes` - --> src/target/conversion.rs - | - | pub trait EncryptTarget<S: Zeroize>: Sized + 'static { - | ^^^^^^^ required by this bound in `EncryptTarget::indexes` -... - | fn indexes() -> Vec<IndexSpec> { - | ------- required by a bound in this associated function diff --git a/packages/stack-encrypt/tests/ui/part_on_structural_derive.rs b/packages/stack-encrypt/tests/ui/part_on_structural_derive.rs deleted file mode 100644 index e09f1a5ee1..0000000000 --- a/packages/stack-encrypt/tests/ui/part_on_structural_derive.rs +++ /dev/null @@ -1,21 +0,0 @@ -//! The structural derives take no scope or binding: the owned derives do. -use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::{DecryptInto, EncryptFrom, Encrypted}; - -struct User { - email: String, -} - -#[derive(EncryptFrom)] -#[stash(struct = User, scope = "users", bind(row: u128))] -struct Bound { - email: Encrypted<EqualityTerm>, -} - -#[derive(DecryptInto)] -#[stash(struct = User, scope = "users", scope(tenant: u64))] -struct Scoped { - email: Encrypted<EqualityTerm>, -} - -fn main() {} diff --git a/packages/stack-encrypt/tests/ui/part_on_structural_derive.stderr b/packages/stack-encrypt/tests/ui/part_on_structural_derive.stderr deleted file mode 100644 index 9a5f67c556..0000000000 --- a/packages/stack-encrypt/tests/ui/part_on_structural_derive.stderr +++ /dev/null @@ -1,11 +0,0 @@ -error: `scope(..)` and `bind(..)` are parts of an owned record's context: derive `EncryptTarget` and `DecryptSource`. The structural `EncryptFrom` / `DecryptInto` derives have no form for them - --> tests/ui/part_on_structural_derive.rs:11:8 - | -11 | struct Bound { - | ^^^^^ - -error: `scope(..)` and `bind(..)` are parts of an owned record's context: derive `EncryptTarget` and `DecryptSource`. The structural `EncryptFrom` / `DecryptInto` derives have no form for them - --> tests/ui/part_on_structural_derive.rs:17:8 - | -17 | struct Scoped { - | ^^^^^^ diff --git a/packages/stack-encrypt/tests/ui/pass/aead_only_context.rs b/packages/stack-encrypt/tests/ui/pass/aead_only_context.rs index b4c1a8ad44..dcc360c0ca 100644 --- a/packages/stack-encrypt/tests/ui/pass/aead_only_context.rs +++ b/packages/stack-encrypt/tests/ui/pass/aead_only_context.rs @@ -2,10 +2,13 @@ //! ciphertext, so it must be enough for a derived record made only of //! ciphertexts: `#[stash(context_type = AeadContext)]` declares that, and the //! record then accepts exactly what the canonical `StackCipherText` path -//! accepts. `tests/ui/aead_context_with_term.rs` pins the record such a +//! accepts. `tests/ui/owned_aead_record_with_term.rs` pins the record such a //! context cannot declare. -use stack_encrypt::target::{AeadContext}; -use stack_encrypt::{ContextPiece, DecryptInto, EncryptFrom, IntoContext, KeysetCipher, MaybeEmpty, NonEmpty, StackCipherText}; +use stack_encrypt::target::{AeadContext, DecryptSource, EncryptTarget}; +use stack_encrypt::{ + ContextPiece, DecryptFrom, EncryptInto, IntoContext, KeysetCipher, MaybeEmpty, NonEmpty, + StackCipherText, +}; use stack_kms::FakeDataKeySource; /// Declared through `AeadContext` below: it can seal, but the record @@ -23,7 +26,7 @@ impl<'a> IntoContext<'a> for Tenant { } } -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String, context_type = AeadContext)] struct Sealed { c: StackCipherText, @@ -31,7 +34,7 @@ struct Sealed { /// Two ciphertexts under the one AEAD-only context the caller passes, with /// `decrypt` choosing the one the record opens. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String, context_type = AeadContext)] struct Shadowed { #[stash(decrypt)] @@ -43,18 +46,40 @@ fn tenant() -> NonEmpty<Tenant> { NonEmpty::new(Tenant("acme".into())).unwrap() } -async fn canonical(cipher: &KeysetCipher<'_, FakeDataKeySource>, value: &String) { - let leaf: StackCipherText = cipher.encrypt_as(value, tenant().into()).await.unwrap(); - let _: String = cipher.decrypt_as(leaf, tenant().into()).await.unwrap(); +async fn canonical(keyset: &KeysetCipher<'_, FakeDataKeySource>, value: String) { + let leaf: StackCipherText = value + .encrypt_into_with_context(keyset, tenant()) + .await + .unwrap(); + let _: String = String::decrypt_from_with_context(leaf, keyset, tenant()) + .await + .unwrap(); } -async fn derived(cipher: &KeysetCipher<'_, FakeDataKeySource>, value: &String) { - let record: Sealed = cipher.encrypt_as(value, tenant().into()).await.unwrap(); - let _: String = cipher.decrypt_as(record, tenant().into()).await.unwrap(); - let record: Shadowed = cipher.encrypt_as(value, tenant().into()).await.unwrap(); - let _ = cipher.decrypt_as::<String, _>(record, tenant().into()).await.unwrap(); - let column: Vec<Sealed> = cipher.encrypt_as(&vec![value.clone()], tenant().into()).await.unwrap(); - let _: Vec<String> = cipher.decrypt_as(column, tenant().into()).await.unwrap(); +async fn derived(keyset: &KeysetCipher<'_, FakeDataKeySource>, value: String) { + let record: Sealed = value + .clone() + .encrypt_into_with_context(keyset, tenant()) + .await + .unwrap(); + let _: String = String::decrypt_from_with_context(record, keyset, tenant()) + .await + .unwrap(); + let record: Shadowed = value + .clone() + .encrypt_into_with_context(keyset, tenant()) + .await + .unwrap(); + let _: String = String::decrypt_from_with_context(record, keyset, tenant()) + .await + .unwrap(); + let column: Vec<Sealed> = vec![value] + .encrypt_into_with_context(keyset, tenant()) + .await + .unwrap(); + let _: Vec<String> = Vec::decrypt_from_with_context(column, keyset, tenant()) + .await + .unwrap(); } fn main() { diff --git a/packages/stack-encrypt/tests/ui/pass/context_type_records.rs b/packages/stack-encrypt/tests/ui/pass/context_type_records.rs index 8ec27e5aec..f7eb9f3166 100644 --- a/packages/stack-encrypt/tests/ui/pass/context_type_records.rs +++ b/packages/stack-encrypt/tests/ui/pass/context_type_records.rs @@ -1,12 +1,16 @@ //! A record that declares `context_type = C` keeps `C` all the way down: -//! nothing in the plan the derive emits asks for a `CallerContext` it -//! cannot take. Each record is encrypted and opened under a `NonEmpty<u64>`. +//! nothing in the declaration the derive emits asks for a `CallerContext` +//! it cannot take. Each record is encrypted and opened under a +//! `NonEmpty<u64>`. use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::target::{DecryptField, Decryptable, Decryption, Encryption, IndexSpec}; -use stack_encrypt::{DecryptInto, EncryptFrom, NonEmpty, StackCipher, StackCipherText}; +use stack_encrypt::target::{ + equality, DecryptField, DecryptSource, Decryptable, Decryption, EncryptTarget, Encryption, + IndexSpec, RequiredContext, +}; +use stack_encrypt::{DecryptFrom, EncryptInto, NonEmpty, StackCipher, StackCipherText}; use stack_kms::FakeDataKeySource; -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String, context_type = NonEmpty<u64>)] struct Sealed { c: StackCipherText, @@ -14,12 +18,10 @@ struct Sealed { /// A term whose context is a `NonEmpty<u64>`, not a `CallerContext`. struct Tag(#[allow(dead_code)] EqualityTerm); -impl EncryptFrom<String> for Tag { - type Context = NonEmpty<u64>; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> { - <EqualityTerm as EncryptFrom<String>>::encryption() - .accepting::<NonEmpty<u64>>() - .map(Tag) +impl EncryptTarget<String> for Tag { + type ContextInput = RequiredContext<NonEmpty<u64>>; + fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::ContextInput> { + equality().accepting().map(Tag) } fn indexes() -> Vec<IndexSpec> { vec![IndexSpec::Equality] @@ -35,14 +37,14 @@ impl<P, Ctx> DecryptField<P, Ctx> for Tag { } /// One output, a target whose context is the record's. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String, context_type = NonEmpty<u64>)] struct Wrapped { sealed: Sealed, } /// Several outputs sharing the record's context, one of them decryptable. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = String, context_type = NonEmpty<u64>)] struct Pair { sealed: Sealed, @@ -52,11 +54,21 @@ struct Pair { async fn round_trips(cipher: &StackCipher<FakeDataKeySource>) { let keyset = cipher.default_keyset(); let value = String::from("bob@example.com"); - let wrapped: Wrapped = keyset.encrypt_as(&value, NonEmpty::from(7u64)).await.unwrap(); - let _: String = cipher.decrypt_as(wrapped, NonEmpty::from(7u64)).await.unwrap(); - let pair: Pair = keyset.encrypt_as(&value, NonEmpty::from(7u64)).await.unwrap(); - let _: String = cipher.decrypt_as(pair, NonEmpty::from(7u64)).await.unwrap(); - let _ = Pair::plan::<String>().unwrap(); + let wrapped: Wrapped = value + .clone() + .encrypt_into_with_context(&keyset, NonEmpty::from(7u64)) + .await + .unwrap(); + let _: String = String::decrypt_from_with_context(wrapped, cipher, NonEmpty::from(7u64)) + .await + .unwrap(); + let pair: Pair = value + .encrypt_into_with_context(&keyset, NonEmpty::from(7u64)) + .await + .unwrap(); + let _: String = String::decrypt_from_with_context(pair, cipher, NonEmpty::from(7u64)) + .await + .unwrap(); } fn main() { diff --git a/packages/stack-encrypt/tests/ui/pass/owned_records.rs b/packages/stack-encrypt/tests/ui/pass/owned_records.rs index 4832c79d4b..2852036e77 100644 --- a/packages/stack-encrypt/tests/ui/pass/owned_records.rs +++ b/packages/stack-encrypt/tests/ui/pass/owned_records.rs @@ -5,16 +5,16 @@ //! - `plaintext` records over opaque and structural leaves, including a //! generic record and a record that stores its context; //! - a column of records; -//! - an owned record used as a field of a structural record, and of a plan. +//! - a record whose fields are a `struct = ..` record and a `plaintext` +//! record. //! //! `tests/ui/owned_record_*.rs` and `tests/ui/passthrough_misuse.rs` pin the //! forms that must not compile. -use stack_encrypt::plan::pick; use stack_encrypt::sem::{EqualityTerm, MatchTerms, OreTerm}; use stack_encrypt::target::{AeadContext, DecryptSource, EncryptTarget}; use stack_encrypt::{ - nonempty, Ciphertext, DecryptFrom, EncryptFrom, EncryptInto, Encrypted, KeysetCipher, Plan, - StackCipher, StackCipherText, + nonempty, Ciphertext, DecryptFrom, EncryptInto, Encrypted, KeysetCipher, StackCipher, + StackCipherText, }; use stack_kms::FakeDataKeySource; use zeroize::Zeroize; @@ -87,13 +87,13 @@ struct TextEq { hm: EqualityTerm, } -/// An owned record as a field of a structural record. -#[derive(Clone)] +/// Records as the fields of another record. +#[derive(Clone, Zeroize)] struct Profile { user: User, age: u32, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget, DecryptSource)] #[stash(struct = Profile, scope = "profiles")] struct EncryptedProfile { user: EncryptedUser, @@ -148,14 +148,10 @@ async fn records( .await?; let _: String = String::decrypt_from(stored, keyset).await?; - // An owned record in a structural record, and as a typed plan field. - let _: EncryptedProfile = keyset.encrypt_as(&profile, Default::default()).await?; - let plan = Plan::context("profiles") - .fields::<Profile, _>() - .encrypt_into::<EncryptedUser, _>(pick("user", |p: &Profile| &p.user)) - .target::<Ciphertext, _>(pick("age", |p: &Profile| &p.age)) - .build()?; - let _ = cipher.encrypt(&profile).using(&plan).await?; + // A record of records. Each inner field's context is extended by the + // outer field's. + let encrypted: EncryptedProfile = profile.encrypt_into(keyset).await?; + let _: Profile = Profile::decrypt_from(encrypted, cipher).await?; Ok(()) } diff --git a/packages/stack-encrypt/tests/ui/pass/plan_chain.rs b/packages/stack-encrypt/tests/ui/pass/plan_chain.rs deleted file mode 100644 index 38323e9a41..0000000000 --- a/packages/stack-encrypt/tests/ui/pass/plan_chain.rs +++ /dev/null @@ -1,82 +0,0 @@ -//! The chain shapes that must compile: a value with a context, indexed, -//! field by field, a saved plan over a value and a collection, a query and -//! an opening, and several chains batched. -use stack_encrypt::op; -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::plan::{Field, Fields}; -use stack_encrypt::{Equality, Error, Match, Ore, Plan, StackCipher}; - -struct Person { - email: String, - age: u32, -} -impl Fields for Person { - fn field_names(&self) -> Vec<&str> { - vec!["email", "age"] - } -} -impl Field<String> for Person { - fn field(&self, name: &str) -> Option<&String> { - (name == "email").then_some(&self.email) - } -} -impl Field<u32> for Person { - fn field(&self, name: &str) -> Option<&u32> { - (name == "age").then_some(&self.age) - } -} - -async fn chains(cipher: &StackCipher<FakeDataKeySource>, person: &Person) -> Result<(), Error> { - let doc = String::from("doc"); - let _ = cipher.encrypt(&doc).context("docs").await?; - let _ = cipher.encrypt(&34u32).context("users/age").with((Equality, Ore)).await?; - let _ = cipher - .encrypt(person) - .context("people") - .fields() - .encrypt_index::<String>("email", (Equality, Match)) - .encrypt::<u32>("age") - .keyset("tenant") - .extend(7u64) - .await?; - let people_plan = Plan::context("people") - .fields() - .encrypt_index::<String>("email", Equality) - .index::<u32>("age", Ore) - .build()?; - let row = cipher.encrypt(person).using(&people_plan).await?; - let _ = cipher.open(row).using(&people_plan).await?; - let email_plan = people_plan.field("email")?; - let _ = cipher.query(op::Eq, "bob@example.com").on(&email_plan).await?; - let name_plan = Plan::context("people/name") - .with::<String, _>(stack_encrypt::Match) - .build()?; - let _ = cipher.query(op::Matches, "ali").on(&name_plan).await?; - let _ = cipher - .query(op::Matches, "ali") - .via(Match) - .context("people/name") - .on(&people_plan.field("email")?) - .await; - let age_plan = Plan::context("people/age").with((Equality, Ore)).build()?; - let surname_plan = Plan::context("people/surname").with::<String, _>(Ore).build()?; - let _ = cipher.query(op::Lt, "nguyen").on(&surname_plan).await?; - let _ = cipher - .query(op::Between, &("a".to_owned(), "m".to_owned())) - .on(&surname_plan) - .await?; - let ages = vec![1u32, 2]; - let _ = cipher.encrypt(&ages).using(&age_plan).await?; - let _ = cipher.query(op::Lt, &3u32).on(&age_plan).await?; - let _ = cipher.query(op::Between, &(1u32, 5)).on(&age_plan).await?; - let _ = stack_encrypt::all(( - cipher.encrypt(person).using(&people_plan), - cipher.query(op::Eq, &3u32).on(&age_plan), - )) - .await?; - Ok(()) -} - -fn main() { - let _ = chains; -} diff --git a/packages/stack-encrypt/tests/ui/pass/plan_grammar.rs b/packages/stack-encrypt/tests/ui/pass/plan_grammar.rs deleted file mode 100644 index a41a85539f..0000000000 --- a/packages/stack-encrypt/tests/ui/pass/plan_grammar.rs +++ /dev/null @@ -1,28 +0,0 @@ -//! The widened plan grammar compiles in each of its spellings: the two -//! starts, a picker with a closure (through `pick`) and with a function, and -//! `encrypt_into` in both positions. -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::plan::pick; -use stack_encrypt::sem::{EqualityTerm, MatchTerms}; -use stack_encrypt::{Encrypted, Equality, Ore, Plan, StackCipherText}; - -struct User { - email: String, - age: u32, -} - -fn age(u: &User) -> &u32 { - &u.age -} - -fn main() { - let _ = Plan::fields::<User, FakeDataKeySource>() - .context("users") - .encrypt_into::<Encrypted<(EqualityTerm, MatchTerms)>, _>(pick("email", |u: &User| &u.email)) - .encrypt_index(("age", age), (Equality, Ore)) - .build(); - let _ = Plan::value::<String>() - .encrypt_into::<(StackCipherText, EqualityTerm)>() - .build(); - let _ = Plan::value::<u32>().context("users/age").with(Ore).build(); -} diff --git a/packages/stack-encrypt/tests/ui/pass/query_only_and_borrowed.rs b/packages/stack-encrypt/tests/ui/pass/query_only_and_borrowed.rs index 3fb03fd8fd..82fa728e2e 100644 --- a/packages/stack-encrypt/tests/ui/pass/query_only_and_borrowed.rs +++ b/packages/stack-encrypt/tests/ui/pass/query_only_and_borrowed.rs @@ -1,24 +1,45 @@ -use stack_encrypt::{EncryptFrom, Encrypt, StackCipher, StackCipherText, nonempty}; +//! A record of terms alone, and a declaration run over a borrowed +//! plaintext. use stack_encrypt::sem::EqualityTerm; +use stack_encrypt::target::{CallerContext, EncryptTarget}; +use stack_encrypt::{nonempty, Encrypt, EncryptInto, StackCipher, StackCipherText}; use stack_kms::FakeDataKeySource; +use zeroize::Zeroize; -#[derive(EncryptFrom)] -struct Probe { hm: EqualityTerm } +#[derive(EncryptTarget)] +struct Probe { + hm: EqualityTerm, +} // No `Decrypt` bound: producing a term requires only that the value // describes itself, not that it can be read back. -fn query<S: Encrypt + Clone>(cipher:&StackCipher<FakeDataKeySource>, value:&S) { - let keyset=cipher.default_keyset(); - let _ = keyset.encrypt_as::<_,Probe>(value,nonempty!("column").into()); +fn query<S: Encrypt + Clone + Zeroize>(cipher: &StackCipher<FakeDataKeySource>, value: S) { + let keyset = cipher.default_keyset(); + let _ = value.encrypt_into_with_context::<Probe, _>(&keyset, nonempty!("column")); +} + +#[derive(EncryptTarget)] +struct Stored { + c: StackCipherText, } -#[derive(EncryptFrom)] -struct Stored { c:StackCipherText } -async fn borrowed(cipher:&StackCipher<FakeDataKeySource>) { - let keyset=cipher.default_keyset(); + +// The borrowed form of a declaration reads the plaintext while the request +// is built. The pending request does not hold the borrow, so the plaintext +// can be dropped before the request is awaited. +async fn borrowed(cipher: &StackCipher<FakeDataKeySource>) { + let keyset = cipher.default_keyset(); let pending = { - let text=String::from("borrowed"); - keyset.encrypt_as::<_,Stored>(&text.as_str(),nonempty!("column").into()) + let text = String::from("borrowed"); + keyset.run( + <Stored as EncryptTarget<String>>::encryption(), + &text, + CallerContext::from(nonempty!("column")).into(), + ) }; let _ = pending.await.unwrap(); } -fn main() {} + +fn main() { + let _ = query::<String>; + let _ = borrowed; +} diff --git a/packages/stack-encrypt/tests/ui/pass/struct_over_borrowed_source.rs b/packages/stack-encrypt/tests/ui/pass/struct_over_borrowed_source.rs index 530856cd26..cf9f0bae90 100644 --- a/packages/stack-encrypt/tests/ui/pass/struct_over_borrowed_source.rs +++ b/packages/stack-encrypt/tests/ui/pass/struct_over_borrowed_source.rs @@ -1,37 +1,44 @@ -//! A `struct = ..` derive over a source that borrows: the source's lifetime -//! is the record's (a derived record is `'static`, so the source is borrowed -//! for `'static`, as it was before the derive emitted a plan), and the -//! fields it derives from are owned. A borrowed field is refused: see -//! `../struct_field_must_open.rs`. -use std::marker::PhantomData; - +//! A `struct = ..` record over a plaintext that borrows. A derived record +//! is `'static`, so the record states that its lifetime is, and the +//! plaintext borrows for `'static`. The borrowed field is passed through in +//! the clear, and the fields the record seals are owned. use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::target::DeclaredContext; -use stack_encrypt::{EncryptFrom, Encrypted, StackCipher, StackCipherText}; +use stack_encrypt::target::{DecryptSource, EncryptTarget}; +use stack_encrypt::{DecryptFrom, EncryptInto, Encrypted, StackCipher, StackCipherText}; use stack_kms::FakeDataKeySource; +use zeroize::Zeroize; +#[derive(Zeroize)] struct User<'a> { email: String, age: u32, - #[allow(dead_code)] // carried, never encrypted + // Carried, never encrypted. A borrow has nothing to wipe. + #[zeroize(skip)] note: &'a str, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget, DecryptSource)] #[stash(struct = User<'a>, scope = "users")] -struct EncryptedUser<'a> { +struct EncryptedUser<'a> +where + 'a: 'static, +{ email: Encrypted<EqualityTerm>, age: StackCipherText, - #[stash(default)] - source: PhantomData<&'a ()>, + #[stash(passthrough)] + note: &'a str, } async fn round_trip(cipher: &StackCipher<FakeDataKeySource>) { let keyset = cipher.default_keyset(); - let user = User { email: "bob@example.com".into(), age: 34, note: "static" }; - let record: EncryptedUser<'_> = keyset.encrypt_as(&user, DeclaredContext::default()).await.unwrap(); - let _ = EncryptedUser::plan::<FakeDataKeySource>().unwrap(); - let _ = record; + let user = User { + email: "bob@example.com".into(), + age: 34, + note: "static", + }; + let record: EncryptedUser<'_> = user.encrypt_into(&keyset).await.unwrap(); + let _ = EncryptedUser::EMAIL; + let _: User<'_> = User::decrypt_from(record, cipher).await.unwrap(); } fn main() { diff --git a/packages/stack-encrypt/tests/ui/pass/which_form_compiles.rs b/packages/stack-encrypt/tests/ui/pass/which_form_compiles.rs index 006e10e915..947e2b281b 100644 --- a/packages/stack-encrypt/tests/ui/pass/which_form_compiles.rs +++ b/packages/stack-encrypt/tests/ui/pass/which_form_compiles.rs @@ -1,10 +1,15 @@ -//! The two record shapes and the call forms each accepts. Every line here -//! must compile; `tests/ui/leaf_without_context.rs` and -//! `tests/ui/bare_context.rs` pin the lines that must not. +//! The record shapes and the call forms each accepts. Every line here +//! must compile; `tests/ui/leaf_without_context.rs`, +//! `tests/ui/bare_context.rs` and +//! `tests/ui/owned_struct_record_takes_no_context.rs` pin the lines that +//! must not. use stack_encrypt::sem::EqualityTerm; - -use stack_encrypt::{nonempty, DecryptInto, EncryptFrom, KeysetCipher, NonEmpty, StackCipherText}; +use stack_encrypt::target::{DecryptSource, EncryptTarget, Scope}; +use stack_encrypt::{ + nonempty, DecryptFrom, EncryptInto, KeysetCipher, NonEmpty, StackCipher, StackCipherText, +}; use stack_kms::FakeDataKeySource; +use zeroize::Zeroize; /// Encrypting binds to a keyset; decrypting works through the same handle /// (constrained to that keyset) as well as through the `StackCipher`. @@ -12,49 +17,70 @@ type Cipher<'k> = KeysetCipher<'k, FakeDataKeySource>; /// A record whose fields have no context of their own: the caller's is /// the only one there is, so it must be given. -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(plaintext = u32)] struct Foo { c: StackCipherText, hm: EqualityTerm, } -async fn foo(cipher: &Cipher<'_>, column: String) -> Result<(), stack_encrypt::Error> { +async fn foo( + keyset: &Cipher<'_>, + cipher: &StackCipher<FakeDataKeySource>, + column: String, +) -> Result<(), stack_encrypt::Error> { // A literal, a runtime value, a bare integer. - let f: Foo = cipher.encrypt_as(&42u32, nonempty!("users/age").into()).await?; - let _: u32 = cipher.decrypt_as(f, nonempty!("users/age").into()).await?; - let f: Foo = cipher.encrypt_as(&42u32, NonEmpty::new(column.clone()).expect("non-empty").into()) + let f: Foo = 42u32 + .encrypt_into_with_context(keyset, nonempty!("users/age")) .await?; - let _: u32 = cipher.decrypt_as(f, NonEmpty::new(column).expect("non-empty").into()) + let _: u32 = u32::decrypt_from_with_context(f, keyset, nonempty!("users/age")).await?; + let f: Foo = 42u32 + .encrypt_into_with_context(keyset, NonEmpty::new(column.clone()).expect("non-empty")) .await?; - let f: Foo = cipher.encrypt_as(&42u32, 7u64.into()).await?; - let _: u32 = cipher.decrypt_as(f, NonEmpty::from(7u64).into()).await?; + let _: u32 = + u32::decrypt_from_with_context(f, cipher, NonEmpty::new(column).expect("non-empty")) + .await?; + let f: Foo = 42u32.encrypt_into_with_context(keyset, 7u64).await?; + let _: u32 = u32::decrypt_from_with_context(f, cipher, NonEmpty::from(7u64)).await?; Ok(()) } -/// A struct encrypted field by field: every field has an inferred context, -/// and the caller's extends all of them. +/// A struct encrypted field by field: every field has a context of its +/// own, so the caller passes none. +#[derive(Zeroize)] struct User { age: u32, email: String, } -#[derive(EncryptFrom, DecryptInto)] +#[derive(EncryptTarget, DecryptSource)] #[stash(struct = User, scope = "users")] struct EncryptedUser { age: Foo, email: StackCipherText, } -async fn user(cipher: &Cipher<'_>, user: User, id: u64) -> Result<(), stack_encrypt::Error> { - // "users/age", "users/email". - let r: EncryptedUser = cipher.encrypt_as(&user, Default::default()).await?; - let user = cipher.decrypt_as::<User, _>(r, Default::default()).await?; - // ("users/age", id), ("users/email", id). - let r: EncryptedUser = cipher.encrypt_as(&user, id.into()).await?; - let _: EqualityTerm = cipher.encrypt_as(&42u32, nonempty!("users/age").with(id).into()) +/// The same struct under a scope the caller supplies: the scope extends +/// every field's context, and the caller passes it on every call. +#[derive(EncryptTarget, DecryptSource)] +#[stash(struct = User, scope = "users", scope(tenant: u64))] +struct TenantUser { + age: Foo, + email: StackCipherText, +} + +async fn user(keyset: &Cipher<'_>, user: User, id: u64) -> Result<(), stack_encrypt::Error> { + // ("users", "age"), ("users", "email"). + let r: EncryptedUser = user.encrypt_into(keyset).await?; + let _: EqualityTerm = 42u32 + .encrypt_into_with_context(keyset, nonempty!("users").with("age")) + .await?; + let user = User::decrypt_from(r, keyset).await?; + // The same two contexts, each extended by the tenant. + let r: TenantUser = user + .encrypt_into_with_context(keyset, Scope::new(id)) .await?; - let _ = cipher.decrypt_as::<User, _>(r, id.into()).await?; + let _ = User::decrypt_from_with_context(r, keyset, Scope::new(id)).await?; Ok(()) } diff --git a/packages/stack-encrypt/tests/ui/passthrough_misuse.rs b/packages/stack-encrypt/tests/ui/passthrough_misuse.rs index bc617c4842..3a28055cf0 100644 --- a/packages/stack-encrypt/tests/ui/passthrough_misuse.rs +++ b/packages/stack-encrypt/tests/ui/passthrough_misuse.rs @@ -1,8 +1,8 @@ -//! `passthrough` moves a field of the plaintext struct into an owned record, -//! in the clear. The attribute is refused on the structural derives, on a -//! `plaintext` record, and beside the attributes of a derived field. +//! `passthrough` moves a field of the plaintext struct into the record, in +//! the clear. The attribute is refused on a `plaintext` record, and beside +//! the attributes of a derived field. use stack_encrypt::target::{DecryptSource, EncryptTarget}; -use stack_encrypt::{Ciphertext, DecryptInto, EncryptFrom, StackCipherText}; +use stack_encrypt::Ciphertext; #[derive(zeroize::Zeroize)] struct User { @@ -10,15 +10,6 @@ struct User { email: String, } -/// The structural derive cannot move a field, because it runs over a borrow. -#[derive(EncryptFrom, DecryptInto)] -#[stash(struct = User, scope = "users")] -struct Structural { - #[stash(passthrough)] - id: u64, - email: StackCipherText, -} - /// A `plaintext` record derives every field from the whole value, so it has no /// field to pass through. #[derive(EncryptTarget)] diff --git a/packages/stack-encrypt/tests/ui/passthrough_misuse.stderr b/packages/stack-encrypt/tests/ui/passthrough_misuse.stderr index a974b9d52d..17052ea6fd 100644 --- a/packages/stack-encrypt/tests/ui/passthrough_misuse.stderr +++ b/packages/stack-encrypt/tests/ui/passthrough_misuse.stderr @@ -1,23 +1,17 @@ -error: `passthrough` is a field of an owned record: derive `EncryptTarget` and `DecryptSource`, which move the plaintext field into the record and back. The structural `EncryptFrom` / `DecryptInto` derives have no form for it - --> tests/ui/passthrough_misuse.rs:18:9 - | -18 | id: u64, - | ^^^ - error: `passthrough` carries one field of the plaintext struct into the record in the clear, which is what `#[stash(struct = ..)]` reaches: a `plaintext` record derives every field from the whole value - --> tests/ui/passthrough_misuse.rs:28:9 + --> tests/ui/passthrough_misuse.rs:19:9 | -28 | id: u64, +19 | id: u64, | ^^^ error: a `passthrough` field is moved through as it is, so `identity`, `default` and `decrypt` do not apply to it: it is keyed under nothing, filled from the plaintext and moved back on decryption - --> tests/ui/passthrough_misuse.rs:37:9 + --> tests/ui/passthrough_misuse.rs:28:9 | -37 | id: u64, +28 | id: u64, | ^^^ error: `plain_email` and `email` are both derived from the plaintext field `email`: a plaintext field has one output. For a ciphertext with search terms beside it, make it one field of type `Encrypted<Terms>`, e.g. `plain_email: Encrypted<(EqualityTerm, MatchTerms)>` - --> tests/ui/passthrough_misuse.rs:47:5 + --> tests/ui/passthrough_misuse.rs:38:5 | -47 | email: Ciphertext, +38 | email: Ciphertext, | ^^^^^ diff --git a/packages/stack-encrypt/tests/ui/plaintext_needs_encrypt.rs b/packages/stack-encrypt/tests/ui/plaintext_needs_encrypt.rs index 89c5d97ff9..7f8368020f 100644 --- a/packages/stack-encrypt/tests/ui/plaintext_needs_encrypt.rs +++ b/packages/stack-encrypt/tests/ui/plaintext_needs_encrypt.rs @@ -1,11 +1,23 @@ -use stack_encrypt::{EncryptFrom, StackCipherText, StackCipher, nonempty}; +//! A plaintext is sealed through Vitamin C's `Encrypt`. A type that only +//! implements `Serialize` is not a plaintext: there is no Serde fallback. +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::{nonempty, EncryptInto, StackCipher, StackCipherText}; use stack_kms::FakeDataKeySource; -#[derive(Clone, serde::Serialize)] -struct SerdeOnly { value: String } -#[derive(EncryptFrom)] -struct Target { c: StackCipherText } -fn wrong(cipher:&StackCipher<FakeDataKeySource>) { - let keyset=cipher.default_keyset(); - let _=keyset.encrypt_as::<_,Target>(&SerdeOnly {value:"x".into()},nonempty!("column").into()); + +#[derive(Clone, serde::Serialize, zeroize::Zeroize)] +struct SerdeOnly { + value: String, } + +#[derive(EncryptTarget)] +struct Target { + c: StackCipherText, +} + +fn wrong(cipher: &StackCipher<FakeDataKeySource>) { + let keyset = cipher.default_keyset(); + let plaintext = SerdeOnly { value: "x".into() }; + let _ = plaintext.encrypt_into_with_context::<Target, _>(&keyset, nonempty!("column")); +} + fn main() {} diff --git a/packages/stack-encrypt/tests/ui/plaintext_needs_encrypt.stderr b/packages/stack-encrypt/tests/ui/plaintext_needs_encrypt.stderr index 5d5f0861b5..aa3f02e421 100644 --- a/packages/stack-encrypt/tests/ui/plaintext_needs_encrypt.stderr +++ b/packages/stack-encrypt/tests/ui/plaintext_needs_encrypt.stderr @@ -1,45 +1,35 @@ -error[E0277]: the trait bound `SerdeOnly: stack_encrypt::Encrypt` is not satisfied - --> tests/ui/plaintext_needs_encrypt.rs:9:33 - | -9 | let _=keyset.encrypt_as::<_,Target>(&SerdeOnly {value:"x".into()},nonempty!("column").into()); - | ---------- ^^^^^^ unsatisfied trait bound - | | - | required by a bound introduced by this call - | -help: the trait `stack_encrypt::Encrypt` is not implemented for `SerdeOnly` - --> tests/ui/plaintext_needs_encrypt.rs:4:1 - | -4 | struct SerdeOnly { value: String } - | ^^^^^^^^^^^^^^^^ +error[E0277]: the trait bound `SerdeOnly: Encrypt` is not satisfied + --> tests/ui/plaintext_needs_encrypt.rs:20:23 + | +20 | let _ = plaintext.encrypt_into_with_context::<Target, _>(&keyset, nonempty!("column")); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound + | +help: the trait `Encrypt` is not implemented for `SerdeOnly` + --> tests/ui/plaintext_needs_encrypt.rs:8:1 + | + 8 | struct SerdeOnly { + | ^^^^^^^^^^^^^^^^ note: there are multiple different versions of crate `vitaminc_aead` in the dependency graph - --> $CARGO/vitaminc-aead-$VERSION/src/plaintext/mod.rs - | - | pub trait Encrypt { - | ^^^^^^^^^^^^^^^^^ this is the expected trait - | - ::: $CARGO/vitaminc-aead-$VERSION/src/encrypt/mod.rs - | - | pub trait Encrypt { - | ----------------- this is the trait that was imported - = help: you can use `cargo tree` to explore your dependency tree - = help: the following other types implement trait `stack_encrypt::Encrypt`: - &&'b T - &'a BTreeMap<K, V> - &'a HashMap<K, V> - &'a Protected<T> - &'a Vec<T> - &'a [T] - &'a std::option::Option<T> - &JsonPlaintext - and $N others - = note: required for `CipherText<SealedValue, PassthroughFrame>` to implement `EncryptFrom<SerdeOnly>` - = note: 1 redundant requirement hidden - = note: required for `Target` to implement `EncryptFrom<SerdeOnly>` -note: required by a bound in `target::operations::<impl KeysetCipher<'_, K>>::encrypt_as` - --> src/target/operations.rs - | - | pub fn encrypt_as<'a, S, T>(&'a self, source: &S, context: T::Context) -> Pending<'a, T, K> - | ---------- required by a bound in this associated function - | where - | T: EncryptFrom<S>, - | ^^^^^^^^^^^^^^ required by this bound in `target::operations::<impl KeysetCipher<'_, K>>::encrypt_as` + --> $CARGO/vitaminc-aead-$VERSION/src/plaintext/mod.rs + | + | pub trait Encrypt { + | ^^^^^^^^^^^^^^^^^ this is the expected trait + | + ::: $CARGO/vitaminc-aead-$VERSION/src/encrypt/mod.rs + | + | pub trait Encrypt { + | ----------------- this is the trait that was imported + = help: you can use `cargo tree` to explore your dependency tree + = help: the following other types implement trait `Encrypt`: + &&'b T + &'a BTreeMap<K, V> + &'a HashMap<K, V> + &'a Protected<T> + &'a Vec<T> + &'a [T] + &'a std::option::Option<T> + &JsonPlaintext + and $N others + = note: required for `CipherText<SealedValue, PassthroughFrame>` to implement `EncryptTarget<SerdeOnly>` + = note: 1 redundant requirement hidden + = note: required for `Target` to implement `EncryptTarget<SerdeOnly>` diff --git a/packages/stack-encrypt/tests/ui/plan_empty_index_set.rs b/packages/stack-encrypt/tests/ui/plan_empty_index_set.rs deleted file mode 100644 index 25689c61c2..0000000000 --- a/packages/stack-encrypt/tests/ui/plan_empty_index_set.rs +++ /dev/null @@ -1,25 +0,0 @@ -//! A plan field declared with indexes needs at least one: `()` is not a set -//! of indexes. A field with no index is `encrypt(name)`. -use stack_encrypt::plan::{Field, Fields}; -use stack_encrypt::Plan; -use stack_kms::FakeDataKeySource; - -struct User { - age: u32, -} -impl Fields for User { - fn field_names(&self) -> Vec<&str> { - vec!["age"] - } -} -impl Field<u32> for User { - fn field(&self, name: &str) -> Option<&u32> { - (name == "age").then_some(&self.age) - } -} - -fn main() { - let _ = Plan::context("users") - .fields::<User, FakeDataKeySource>() - .encrypt_index::<u32>("age", ()); -} diff --git a/packages/stack-encrypt/tests/ui/plan_empty_index_set.stderr b/packages/stack-encrypt/tests/ui/plan_empty_index_set.stderr deleted file mode 100644 index 799146dc48..0000000000 --- a/packages/stack-encrypt/tests/ui/plan_empty_index_set.stderr +++ /dev/null @@ -1,37 +0,0 @@ -error[E0277]: `()` is not a set of indexes over `u32` - --> tests/ui/plan_empty_index_set.rs:24:38 - | -24 | .encrypt_index::<u32>("age", ()); - | ------------- ^^ expected one index, or a tuple of two to five, each defined over `u32` - | | - | required by a bound introduced by this call - | - = help: the trait `Indexes<u32>` is not implemented for `()` - = note: `()` is not a set of indexes: a field with no index is `ciphertext()` alone -help: the following other types implement trait `Indexes<S>` - --> src/target/index.rs - | - | impl<S, $($name: Index<S>),+> Indexes<S> for ($($name,)+) { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | `(A, B)` - | `(A, B, C)` - | `(A, B, C, D)` - | `(A, B, C, D, E)` -... - | / tuple_of_indexes! { - | | (A 0, B 1) => (A, B), - | | (A 0, B 1, C 2) => ((A, B), C), - | | (A 0, B 1, C 2, D 3) => (((A, B), C), D), - | | (A 0, B 1, C 2, D 3, E 4) => ((((A, B), C), D), E), - | | } - | |_____- in this macro invocation -note: required by a bound in `FieldsBuilder::<S, K>::encrypt_index` - --> src/plan/build.rs - | - | pub fn encrypt_index<F>( - | ------------- required by a bound in this associated function -... - | indexes: impl Indexes<F, Terms: Send> + Clone + Send + Sync + 'static, - | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `FieldsBuilder::<S, K>::encrypt_index` - = note: this error originates in the macro `tuple_of_indexes` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/packages/stack-encrypt/tests/ui/plan_match_on_integer.rs b/packages/stack-encrypt/tests/ui/plan_match_on_integer.rs deleted file mode 100644 index 699d6163ba..0000000000 --- a/packages/stack-encrypt/tests/ui/plan_match_on_integer.rs +++ /dev/null @@ -1,25 +0,0 @@ -//! Match is defined over text, so a plan cannot declare a match index on an -//! integer field. -use stack_encrypt::plan::{Field, Fields}; -use stack_encrypt::{Match, Plan}; -use stack_kms::FakeDataKeySource; - -struct User { - age: u32, -} -impl Fields for User { - fn field_names(&self) -> Vec<&str> { - vec!["age"] - } -} -impl Field<u32> for User { - fn field(&self, name: &str) -> Option<&u32> { - (name == "age").then_some(&self.age) - } -} - -fn main() { - let _ = Plan::context("users") - .fields::<User, FakeDataKeySource>() - .encrypt_index::<u32>("age", Match); -} diff --git a/packages/stack-encrypt/tests/ui/plan_match_on_integer.stderr b/packages/stack-encrypt/tests/ui/plan_match_on_integer.stderr deleted file mode 100644 index 35d448a708..0000000000 --- a/packages/stack-encrypt/tests/ui/plan_match_on_integer.stderr +++ /dev/null @@ -1,12 +0,0 @@ -error[E0277]: the trait bound `u32: AsRef<str>` is not satisfied - --> tests/ui/plan_match_on_integer.rs:24:10 - | -24 | .encrypt_index::<u32>("age", Match); - | ^^^^^^^^^^^^^ the trait `AsRef<str>` is not implemented for `u32` - | -help: the trait `stack_encrypt::Index<S>` is implemented for `stack_encrypt::Match<O>` - --> src/target/index.rs - | - | impl<S: AsRef<str>, O: MatchConfig + 'static> Index<S> for Match<O> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: required for `stack_encrypt::Match` to implement `stack_encrypt::Index<u32>` diff --git a/packages/stack-encrypt/tests/ui/plan_picker_wrong_type.rs b/packages/stack-encrypt/tests/ui/plan_picker_wrong_type.rs deleted file mode 100644 index bde54e26d5..0000000000 --- a/packages/stack-encrypt/tests/ui/plan_picker_wrong_type.rs +++ /dev/null @@ -1,15 +0,0 @@ -//! A picker's accessor names the field's type, so a verb that names another -//! type for the same field does not compile. -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::plan::pick; -use stack_encrypt::{Equality, Plan}; - -struct User { - email: String, -} - -fn main() { - let _ = Plan::context("users") - .fields::<User, FakeDataKeySource>() - .encrypt_index::<u32>(pick("email", |u: &User| &u.email), Equality); -} diff --git a/packages/stack-encrypt/tests/ui/plan_picker_wrong_type.stderr b/packages/stack-encrypt/tests/ui/plan_picker_wrong_type.stderr deleted file mode 100644 index ab6bc89d29..0000000000 --- a/packages/stack-encrypt/tests/ui/plan_picker_wrong_type.stderr +++ /dev/null @@ -1,20 +0,0 @@ -error[E0271]: expected `{closure@plan_picker_wrong_type.rs:14:45}` to return `&u32`, but it returns `&String` - --> tests/ui/plan_picker_wrong_type.rs:14:56 - | -14 | .encrypt_index::<u32>(pick("email", |u: &User| &u.email), Equality); - | ------------- ---------- ^^^^^^^^ expected `&u32`, found `&String` - | | | - | | this closure - | required by a bound introduced by this call - | - = note: expected reference `&u32` - found reference `&std::string::String` - = note: required for `(&str, {closure@$DIR/tests/ui/plan_picker_wrong_type.rs:14:45: 14:55})` to implement `FieldRef<User, u32>` -note: required by a bound in `FieldsBuilder::<S, K>::encrypt_index` - --> src/plan/build.rs - | - | pub fn encrypt_index<F>( - | ------------- required by a bound in this associated function - | self, - | field: impl FieldRef<S, F>, - | ^^^^^^^^^^^^^^ required by this bound in `FieldsBuilder::<S, K>::encrypt_index` diff --git a/packages/stack-encrypt/tests/ui/plan_query_undeclared_index.rs b/packages/stack-encrypt/tests/ui/plan_query_undeclared_index.rs deleted file mode 100644 index 26f9f5e920..0000000000 --- a/packages/stack-encrypt/tests/ui/plan_query_undeclared_index.rs +++ /dev/null @@ -1,12 +0,0 @@ -//! A one-value plan selects a query's index by type, so asking it for an -//! index it does not hold does not compile. -use stack_encrypt::op; -use stack_encrypt::kms::FakeDataKeySource; -use stack_encrypt::{Ore, Plan, StackCipher}; - -async fn query(cipher: &StackCipher<FakeDataKeySource>) { - let age_plan = Plan::context("users/age").with::<u32, _>(Ore).build().unwrap(); - let _ = cipher.query(op::Eq, &34u32).on(&age_plan); -} - -fn main() {} diff --git a/packages/stack-encrypt/tests/ui/plan_query_undeclared_index.stderr b/packages/stack-encrypt/tests/ui/plan_query_undeclared_index.stderr deleted file mode 100644 index a26c14f200..0000000000 --- a/packages/stack-encrypt/tests/ui/plan_query_undeclared_index.stderr +++ /dev/null @@ -1,45 +0,0 @@ -error[E0277]: the trait bound `stack_encrypt::Ore: Select<stack_encrypt::Equality, _>` is not satisfied - --> tests/ui/plan_query_undeclared_index.rs:9:42 - | -9 | let _ = cipher.query(op::Eq, &34u32).on(&age_plan); - | ^^ the trait `Select<stack_encrypt::Equality, _>` is not implemented for `stack_encrypt::Ore` - | - = help: the following other types implement trait `Select<I, Position>`: - `(A, B)` implements `Select<A, At<0>>` - `(A, B)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<A, At<0>>` - `(A, B, C)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<C, At<2>>` - `(A, B, C, D)` implements `Select<A, At<0>>` - `(A, B, C, D)` implements `Select<B, At<1>>` - `(A, B, C, D)` implements `Select<C, At<2>>` - and $N others - = note: required for `ValuePlan<u32, Indexed<stack_encrypt::Ore>>` to implement `Chooses<stack_encrypt::op::Eq, u32, _>` - -error[E0277]: the trait bound `stack_encrypt::Ore: Select<stack_encrypt::Equality, _>` is not satisfied - --> tests/ui/plan_query_undeclared_index.rs:9:45 - | -9 | let _ = cipher.query(op::Eq, &34u32).on(&age_plan); - | -- ^^^^^^^^^ the trait `Select<stack_encrypt::Equality, _>` is not implemented for `stack_encrypt::Ore` - | | - | required by a bound introduced by this call - | - = help: the following other types implement trait `Select<I, Position>`: - `(A, B)` implements `Select<A, At<0>>` - `(A, B)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<A, At<0>>` - `(A, B, C)` implements `Select<B, At<1>>` - `(A, B, C)` implements `Select<C, At<2>>` - `(A, B, C, D)` implements `Select<A, At<0>>` - `(A, B, C, D)` implements `Select<B, At<1>>` - `(A, B, C, D)` implements `Select<C, At<2>>` - and $N others - = note: required for `ValuePlan<u32, Indexed<stack_encrypt::Ore>>` to implement `Chooses<stack_encrypt::op::Eq, u32, _>` -note: required by a bound in `QueryBuilder::<'a, Op, F, K>::on` - --> src/plan/chain.rs - | - | pub fn on<P, At>(self, plan: &P) -> QueryOp<'a, F, Op, P::Via, K> - | -- required by a bound in this associated function - | where - | P: Chooses<Op, F, At>, - | ^^^^^^^^^^^^^^^^^^ required by this bound in `QueryBuilder::<'a, Op, F, K>::on` diff --git a/packages/stack-encrypt/tests/ui/reference_plaintext.rs b/packages/stack-encrypt/tests/ui/reference_plaintext.rs index c274860059..13ac28363b 100644 --- a/packages/stack-encrypt/tests/ui/reference_plaintext.rs +++ b/packages/stack-encrypt/tests/ui/reference_plaintext.rs @@ -1,6 +1,9 @@ -use stack_encrypt::{EncryptFrom, StackCipherText}; +//! A `plaintext` type is owned: the generated impl has no lifetime to give +//! a reference. +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(plaintext = &str)] struct Text { c: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/reference_plaintext.stderr b/packages/stack-encrypt/tests/ui/reference_plaintext.stderr index 3403bab411..05db2fdbd9 100644 --- a/packages/stack-encrypt/tests/ui/reference_plaintext.stderr +++ b/packages/stack-encrypt/tests/ui/reference_plaintext.stderr @@ -1,5 +1,5 @@ -error: `plaintext` must be an owned type: a reference plaintext has no lifetime the generated impl can name. Omit `plaintext` for an impl generic over the source, which accepts references too. - --> tests/ui/reference_plaintext.rs:4:21 +error: `plaintext` must be an owned type: a reference plaintext has no lifetime the generated impl can name, and a plaintext is passed by value and wiped. Name the owned type, such as `String` for `&str`. + --> tests/ui/reference_plaintext.rs:7:21 | -4 | #[stash(plaintext = &str)] +7 | #[stash(plaintext = &str)] | ^^^^ diff --git a/packages/stack-encrypt/tests/ui/struct_duplicate_from.rs b/packages/stack-encrypt/tests/ui/struct_duplicate_from.rs index c1be2c6a64..0257c3125d 100644 --- a/packages/stack-encrypt/tests/ui/struct_duplicate_from.rs +++ b/packages/stack-encrypt/tests/ui/struct_duplicate_from.rs @@ -2,13 +2,15 @@ //! ciphertext, a second field `from` the same plaintext field, is refused, //! naming the replacement: one field of type `Encrypted<Terms>`. use stack_encrypt::sem::EqualityTerm; -use stack_encrypt::{DecryptInto, EncryptFrom, StackCipherText}; +use stack_encrypt::target::{DecryptSource, EncryptTarget}; +use stack_encrypt::StackCipherText; +#[derive(zeroize::Zeroize)] struct User { email: String, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = User, scope = "users")] struct EncryptedUser { email: StackCipherText, @@ -16,7 +18,7 @@ struct EncryptedUser { email_hm: EqualityTerm, } -#[derive(DecryptInto)] +#[derive(DecryptSource)] #[stash(struct = User, scope = "users")] struct Opened { email: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/struct_duplicate_from.stderr b/packages/stack-encrypt/tests/ui/struct_duplicate_from.stderr index 66ebfa682d..14fb6789d1 100644 --- a/packages/stack-encrypt/tests/ui/struct_duplicate_from.stderr +++ b/packages/stack-encrypt/tests/ui/struct_duplicate_from.stderr @@ -1,11 +1,11 @@ error: `email` and `email_hm` are both derived from the plaintext field `email`: a plaintext field has one output. For a ciphertext with search terms beside it, make it one field of type `Encrypted<Terms>`, e.g. `email: Encrypted<(EqualityTerm, MatchTerms)>` - --> tests/ui/struct_duplicate_from.rs:15:20 + --> tests/ui/struct_duplicate_from.rs:17:20 | -15 | #[stash(from = email)] +17 | #[stash(from = email)] | ^^^^^ error: `email` and `email_hm` are both derived from the plaintext field `email`: a plaintext field has one output. For a ciphertext with search terms beside it, make it one field of type `Encrypted<Terms>`, e.g. `email: Encrypted<(EqualityTerm, MatchTerms)>` - --> tests/ui/struct_duplicate_from.rs:23:20 + --> tests/ui/struct_duplicate_from.rs:25:20 | -23 | #[stash(from = email)] +25 | #[stash(from = email)] | ^^^^^ diff --git a/packages/stack-encrypt/tests/ui/struct_field_missing.rs b/packages/stack-encrypt/tests/ui/struct_field_missing.rs index c06482571c..404655b1ce 100644 --- a/packages/stack-encrypt/tests/ui/struct_field_missing.rs +++ b/packages/stack-encrypt/tests/ui/struct_field_missing.rs @@ -1,12 +1,14 @@ //! A field is derived from the plaintext field of its own name; a name the //! plaintext does not have is reported by rustc at the field. -use stack_encrypt::{EncryptFrom, StackCipherText}; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; +#[derive(zeroize::Zeroize)] struct User { email: String, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = User, scope = "user")] struct EncryptedUser { email: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/struct_field_missing.stderr b/packages/stack-encrypt/tests/ui/struct_field_missing.stderr index 0f741b9f9a..92e08d5aec 100644 --- a/packages/stack-encrypt/tests/ui/struct_field_missing.stderr +++ b/packages/stack-encrypt/tests/ui/struct_field_missing.stderr @@ -1,7 +1,13 @@ error[E0609]: no field `nickname` on type `&User` - --> tests/ui/struct_field_missing.rs:13:5 + --> tests/ui/struct_field_missing.rs:15:5 | -13 | nickname: StackCipherText, +15 | nickname: StackCipherText, | ^^^^^^^^ unknown field | = note: available field is: `email` + +error[E0026]: struct `User` does not have a field named `nickname` + --> tests/ui/struct_field_missing.rs:15:5 + | +15 | nickname: StackCipherText, + | ^^^^^^^^ struct `User` does not have this field diff --git a/packages/stack-encrypt/tests/ui/struct_field_must_open.rs b/packages/stack-encrypt/tests/ui/struct_field_must_open.rs index df6f559cdd..efb3690858 100644 --- a/packages/stack-encrypt/tests/ui/struct_field_must_open.rs +++ b/packages/stack-encrypt/tests/ui/struct_field_must_open.rs @@ -1,12 +1,14 @@ -//! Every field of a `struct = ..` record opens back into the plaintext -//! field it is derived from (`DecryptField<F, CallerContext>`), even when -//! the record derives `EncryptFrom` alone: the record's plan seals and -//! opens each field. A plaintext field with no `Decrypt`, such as one that -//! borrows its text, cannot be opened into, and a target with no -//! `DecryptField` cannot open. -use stack_encrypt::target::{Decryptable, Encryption, AeadContext}; -use stack_encrypt::{EncryptFrom, Encrypt, StackCipherText}; +//! Every field of a `struct = ..` record that derives `DecryptSource` opens +//! back into the plaintext field it is derived from +//! (`DecryptField<F, CallerContext>`). A plaintext field with no `Decrypt`, +//! such as one that borrows its text, cannot be opened into, and a target +//! with no `DecryptField` cannot open. +use stack_encrypt::target::{ + AeadContext, DecryptSource, Decryptable, EncryptTarget, Encryption, RequiredContext, +}; +use stack_encrypt::{Encrypt, StackCipherText}; use vitaminc_aead::plaintext::Sink; +use zeroize::Zeroize; /// Text it borrows: it seals, and there is nothing to open it into. #[derive(Clone)] @@ -21,7 +23,13 @@ impl Encrypt for &Name { self.0.visit(sink) } } +impl Zeroize for Name { + fn zeroize(&mut self) { + self.0 = ""; + } +} +#[derive(Zeroize)] struct Profile { name: Name, bio: String, @@ -29,17 +37,17 @@ struct Profile { /// A target with no `DecryptField`. struct Opaque(#[allow(dead_code)] StackCipherText); -impl EncryptFrom<String> for Opaque { - type Context = AeadContext; - fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::Context> { - <StackCipherText as EncryptFrom<String>>::encryption().map(Opaque) +impl EncryptTarget<String> for Opaque { + type ContextInput = RequiredContext<AeadContext>; + fn encryption<'s, K: 'static>() -> Encryption<'s, String, Self, K, Self::ContextInput> { + <StackCipherText as EncryptTarget<String>>::encryption().map(Opaque) } } impl Decryptable for Opaque { const DECRYPTABLE: bool = true; } -#[derive(EncryptFrom)] +#[derive(EncryptTarget, DecryptSource)] #[stash(struct = Profile, scope = "profiles")] struct EncryptedProfile { name: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/struct_field_must_open.stderr b/packages/stack-encrypt/tests/ui/struct_field_must_open.stderr index 754be4badf..861dc6f40a 100644 --- a/packages/stack-encrypt/tests/ui/struct_field_must_open.stderr +++ b/packages/stack-encrypt/tests/ui/struct_field_must_open.stderr @@ -1,13 +1,35 @@ +error[E0277]: the trait bound `Opaque: DecryptField<_, CallerContext>` is not satisfied + --> tests/ui/struct_field_must_open.rs:54:10 + | +54 | bio: Opaque, + | ^^^^^^ unsatisfied trait bound + | +help: the trait `DecryptField<_, CallerContext>` is not implemented for `Opaque` + --> tests/ui/struct_field_must_open.rs:39:1 + | +39 | struct Opaque(#[allow(dead_code)] StackCipherText); + | ^^^^^^^^^^^^^ + = help: the following other types implement trait `DecryptField<P, Ctx>`: + `CipherText<SealedValue, PassthroughFrame>` implements `DecryptField<P, Ctx>` + `Encrypted<Terms>` implements `DecryptField<P, Ctx>` + `EncryptedProfile` implements `DecryptField<__P, __Ctx>` + `EqualityTerm` implements `DecryptField<P, Ctx>` + `JsonDocument` implements `DecryptField<P, Ctx>` + `JsonExtracted` implements `DecryptField<P, Ctx>` + `JsonSubtree` implements `DecryptField<P, Ctx>` + `MatchTerms<O>` implements `DecryptField<P, Ctx>` + and $N others + error[E0277]: the trait bound `Name: FromScalar` is not satisfied - --> tests/ui/struct_field_must_open.rs:45:11 + --> tests/ui/struct_field_must_open.rs:53:11 | -45 | name: StackCipherText, +53 | name: StackCipherText, | ^^^^^^^^^^^^^^^ unsatisfied trait bound | help: the trait `FromScalar` is not implemented for `Name` - --> tests/ui/struct_field_must_open.rs:13:1 + --> tests/ui/struct_field_must_open.rs:15:1 | -13 | struct Name(&'static str); +15 | struct Name(&'static str); | ^^^^^^^^^^^ = help: the following other types implement trait `FromScalar`: JsonPlaintext @@ -20,43 +42,5 @@ help: the trait `FromScalar` is not implemented for `Name` f64 and $N others = note: required for `Name` to implement `Decrypt` - = note: required for `CipherText<SealedValue, PassthroughFrame>` to implement `DecryptInto<Name>` + = note: required for `CipherText<SealedValue, PassthroughFrame>` to implement `DecryptSource<Name>` = note: required for `CipherText<SealedValue, PassthroughFrame>` to implement `DecryptField<Name, CallerContext>` -note: required by a bound in `FieldsBuilder::<S, K>::encrypt_into` - --> src/plan/build.rs - | - | pub fn encrypt_into<T, F>(self, field: impl FieldRef<S, F>) -> Self - | ------------ required by a bound in this associated function -... - | T: EncryptFrom<F> + Decryptable + DecryptField<F, CallerContext> + Send, - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `FieldsBuilder::<S, K>::encrypt_into` - -error[E0277]: the trait bound `Opaque: DecryptField<std::string::String, CallerContext>` is not satisfied - --> tests/ui/struct_field_must_open.rs:46:10 - | -46 | bio: Opaque, - | ^^^^^^ unsatisfied trait bound - | -help: the trait `DecryptField<std::string::String, CallerContext>` is not implemented for `Opaque` - --> tests/ui/struct_field_must_open.rs:31:1 - | -31 | struct Opaque(#[allow(dead_code)] StackCipherText); - | ^^^^^^^^^^^^^ - = help: the following other types implement trait `DecryptField<P, Ctx>`: - `(A, B)` implements `DecryptField<P, Ctx>` - `(A, B, C)` implements `DecryptField<P, Ctx>` - `(A, B, C, D)` implements `DecryptField<P, Ctx>` - `(A, B, C, D, E)` implements `DecryptField<P, Ctx>` - `CipherText<SealedValue, PassthroughFrame>` implements `DecryptField<P, Ctx>` - `Encrypted<Terms>` implements `DecryptField<P, Ctx>` - `EqualityTerm` implements `DecryptField<P, Ctx>` - `JsonDocument` implements `DecryptField<P, Ctx>` - and $N others -note: required by a bound in `FieldsBuilder::<S, K>::encrypt_into` - --> src/plan/build.rs - | - | pub fn encrypt_into<T, F>(self, field: impl FieldRef<S, F>) -> Self - | ------------ required by a bound in this associated function -... - | T: EncryptFrom<F> + Decryptable + DecryptField<F, CallerContext> + Send, - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `FieldsBuilder::<S, K>::encrypt_into` diff --git a/packages/stack-encrypt/tests/ui/struct_with_plaintext.rs b/packages/stack-encrypt/tests/ui/struct_with_plaintext.rs index cbca37a4c9..99ac131d52 100644 --- a/packages/stack-encrypt/tests/ui/struct_with_plaintext.rs +++ b/packages/stack-encrypt/tests/ui/struct_with_plaintext.rs @@ -1,10 +1,13 @@ -use stack_encrypt::{EncryptFrom, StackCipherText}; +//! `struct` and `plaintext` both name the plaintext, so a record gives one. +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; +#[derive(zeroize::Zeroize)] struct User { email: String, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = User, plaintext = User)] struct EncryptedUser { email: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/struct_with_plaintext.stderr b/packages/stack-encrypt/tests/ui/struct_with_plaintext.stderr index 5253cd712c..b4b8f71ae6 100644 --- a/packages/stack-encrypt/tests/ui/struct_with_plaintext.stderr +++ b/packages/stack-encrypt/tests/ui/struct_with_plaintext.stderr @@ -1,11 +1,11 @@ error: `struct` and `plaintext` are two ways of naming the plaintext: `struct = ..` encrypts it field by field, `plaintext = ..` as one value, so give one of them - --> tests/ui/struct_with_plaintext.rs:8:18 - | -8 | #[stash(struct = User, plaintext = User)] - | ^^^^ + --> tests/ui/struct_with_plaintext.rs:11:18 + | +11 | #[stash(struct = User, plaintext = User)] + | ^^^^ error: `plaintext` given here - --> tests/ui/struct_with_plaintext.rs:8:36 - | -8 | #[stash(struct = User, plaintext = User)] - | ^^^^ + --> tests/ui/struct_with_plaintext.rs:11:36 + | +11 | #[stash(struct = User, plaintext = User)] + | ^^^^ diff --git a/packages/stack-encrypt/tests/ui/struct_without_context.rs b/packages/stack-encrypt/tests/ui/struct_without_context.rs index 5a99a82b0d..dce27e0845 100644 --- a/packages/stack-encrypt/tests/ui/struct_without_context.rs +++ b/packages/stack-encrypt/tests/ui/struct_without_context.rs @@ -3,13 +3,15 @@ //! derived from the struct and the domain of every term — so it is never //! inferred from the Rust type's name. Two types named `Account` in //! different modules must not silently share every field context. -use stack_encrypt::{EncryptFrom, StackCipherText}; +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::StackCipherText; +#[derive(zeroize::Zeroize)] struct User { email: String, } -#[derive(EncryptFrom)] +#[derive(EncryptTarget)] #[stash(struct = User)] struct EncryptedUser { email: StackCipherText, diff --git a/packages/stack-encrypt/tests/ui/struct_without_context.stderr b/packages/stack-encrypt/tests/ui/struct_without_context.stderr index 9fcaca0ebf..09a44db990 100644 --- a/packages/stack-encrypt/tests/ui/struct_without_context.stderr +++ b/packages/stack-encrypt/tests/ui/struct_without_context.stderr @@ -1,5 +1,5 @@ error: `struct = ..` needs a `scope = ".."` beside it naming the stored data (e.g. `#[stash(struct = User, scope = "users")]`): each field is derived under the pair `("<scope>", "<field>")`, and the record's name is part of the stored data's identity, so it is given explicitly rather than inferred from the Rust type's name - --> tests/ui/struct_without_context.rs:13:18 + --> tests/ui/struct_without_context.rs:15:18 | -13 | #[stash(struct = User)] +15 | #[stash(struct = User)] | ^^^^ diff --git a/packages/stack-encrypt/tests/ui/wrong_target_context.rs b/packages/stack-encrypt/tests/ui/wrong_target_context.rs index bf92d47633..586df5b80b 100644 --- a/packages/stack-encrypt/tests/ui/wrong_target_context.rs +++ b/packages/stack-encrypt/tests/ui/wrong_target_context.rs @@ -1,14 +1,22 @@ -use stack_encrypt::{EncryptFrom, StackCipherText, StackCipher, nonempty}; +//! A record that stores its context takes a `NonEmpty<T>` of the stored +//! field's type, and no other. +use stack_encrypt::target::EncryptTarget; +use stack_encrypt::{nonempty, EncryptInto, StackCipher, StackCipherText}; use stack_kms::FakeDataKeySource; -#[derive(EncryptFrom)] + +#[derive(EncryptTarget)] #[stash(plaintext = String)] struct Target { #[stash(context_field)] identifier: u32, c: StackCipherText, } + fn wrong(cipher: &StackCipher<FakeDataKeySource>) { let keyset = cipher.default_keyset(); - let _ = keyset.encrypt_as::<_,Target>(&"value".to_owned(), nonempty!("wrong type")); + let _ = "value" + .to_owned() + .encrypt_into_with_context::<Target, _>(&keyset, nonempty!("wrong type")); } + fn main() {} diff --git a/packages/stack-encrypt/tests/ui/wrong_target_context.stderr b/packages/stack-encrypt/tests/ui/wrong_target_context.stderr index 106371dc03..530b680361 100644 --- a/packages/stack-encrypt/tests/ui/wrong_target_context.stderr +++ b/packages/stack-encrypt/tests/ui/wrong_target_context.stderr @@ -1,9 +1,30 @@ -error[E0308]: mismatched types - --> tests/ui/wrong_target_context.rs:12:64 +error[E0277]: the trait bound `NonEmpty<u32>: From<NonEmpty<&str>>` is not satisfied + --> tests/ui/wrong_target_context.rs:19:58 | -12 | let _ = keyset.encrypt_as::<_,Target>(&"value".to_owned(), nonempty!("wrong type")); - | ^^^^^^^^^^^^^^^^^^^^^^^ expected `NonEmpty<u32>`, found `NonEmpty<&str>` +19 | .encrypt_into_with_context::<Target, _>(&keyset, nonempty!("wrong type")); + | ------------------------- ^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<NonEmpty<&str>>` is not implemented for `NonEmpty<u32>` + | | + | required by a bound introduced by this call | - = note: expected struct `NonEmpty<u32>` - found struct `NonEmpty<&'static str>` - = note: this error originates in the macro `nonempty` (in Nightly builds, run with -Z macro-backtrace for more info) +help: the trait `From<NonEmpty<&str>>` is not implemented for `NonEmpty<u32>` + but trait `From<u32>` is implemented for it + --> $CARGO/vitaminc-protected-$VERSION/src/non_empty.rs + | + | impl From<$ty> for NonEmpty<$ty> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... + | never_empty!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128); + | -------------------------------------------------------------- in this macro invocation + = help: for that trait implementation, expected `u32`, found `NonEmpty<&str>` + = note: there are multiple different versions of crate `vitaminc_protected` in the dependency graph + = help: you can use `cargo tree` to explore your dependency tree + = note: required for `NonEmpty<&str>` to implement `Into<NonEmpty<u32>>` +note: required by a bound in `encrypt_into_with_context` + --> src/target/conversion.rs + | + | fn encrypt_into_with_context<'a, T, K: 'static>( + | ------------------------- required by a bound in this associated function +... + | context: impl Into<<T::ContextInput as AcceptsContext>::Value>, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `EncryptInto::encrypt_into_with_context` + = note: this error originates in the macro `never_empty` (in Nightly builds, run with -Z macro-backtrace for more info)