diff --git a/conformance/stack-encrypt/README.md b/conformance/stack-encrypt/README.md index 300bb29a6..da42b5ec5 100644 --- a/conformance/stack-encrypt/README.md +++ b/conformance/stack-encrypt/README.md @@ -46,6 +46,8 @@ these rules. | `record-raw-frames-v1.cases.json` | cases | `tests/raw_frame_conformance.rs` | `raw_frame_conformance_test.go` | | `single-value-v1.cases.json` | cases | `tests/single_value_conformance.rs` | — (pending, issue #2010) | | `single-value-v1.expected.rust-native.json` | expected | `tests/single_value_conformance.rs` (writes it) | — (pending, issue #2010) | +| `record-fields-v1.cases.json` | cases | `tests/field_conformance.rs` | — (pending: the Go SDK has no call for one field) | +| `record-fields-v1.expected.rust-native.json` | expected | `tests/field_conformance.rs` (writes it) | — (pending: the Go SDK has no call for one field) | | `artifact-exchange-v1.cases.json` | cases | `tests/exchange_conformance.rs`, run by `scripts/stack-exchange-conformance.mjs` | `exchange_conformance_test.go`, run by `scripts/stack-exchange-conformance.mjs` | | `eql-json-v1.cases.json` | cases | `tests/json_operators.rs`; EQL's `tests/encryption/tests/json_tree_data_plan.rs` | `json_conformance_test.go` | | `eql-json-v1.enrollment.json` | enrollment | `scripts/stack-eql-conformance.mjs` runs the tests it names | `scripts/stack-eql-conformance.mjs` runs the tests it names | @@ -92,8 +94,8 @@ fixtures described below. It ends with the fresh artifact exchange in both directions (`test:conformance:exchange`, which also needs the deterministic EQL guest). Set no fixture-update variables when checking conformance. -The task runs the single-value cases in native Rust only, because the Go -runner of that profile is pending. +The task runs the single-value cases and the one-field cases in native Rust +only, because the Go runners of those profiles are pending. The Rust runner is `packages/stack-encrypt/tests/conformance.rs`; the Go runner is `languages/golang/encrypt/conformance_test.go`. Go calls the record interface @@ -207,6 +209,26 @@ The native runner is `single_value_conformance` in the Rust crate. with the typed interface. The Go runner is pending. The Go runner will be written with the single-value calls of the Go SDK (issue #2010). +## One field of a record + +The [one-field contract](record-fields-contract.md) defines `record-fields-v1` +in `v1/record-fields-v1.cases.json`. Each case encrypts one field of a record +alone, under the record's plan. The runner checks the result against a whole +row encrypted under the same plan: + +- The field's node alone has the outputs and the terms of the row's node. +- The data keys of the field alone are requested under the row's + descriptors. +- The node alone decrypts inside the row. +- The row's node decrypts alone. + +The cases cover every output kind, nulls stored both ways, and the +refusals. They also cover a scope and a binding in the plan, and a scope and +a binding that the call passes. `v1/record-fields-v1.expected.rust-native.json` +freezes each case's input frame, descriptors, and node. The native runner is +`field_conformance` in the Rust crate, and `test:conformance` runs it. The Go +runner is pending until the Go SDK has a call for one field. + ## `record-reads-v1` contract The [read contract](read-contract.md) defines cases in `v1/record-reads-v1.cases.json` for diff --git a/conformance/stack-encrypt/coverage.md b/conformance/stack-encrypt/coverage.md index 90b570655..31c91135b 100644 --- a/conformance/stack-encrypt/coverage.md +++ b/conformance/stack-encrypt/coverage.md @@ -8,7 +8,7 @@ Paths are relative to the repository root. New profile cases run from `v1/`. |---|---|---| | Scalar value widths and lossless transport | Initial scalar cases, [value-kind requirements](fixture-contract.md#value-kind-requirements) and the [value boundary profile](values-contract.md) (`v1/record-values-v1.cases.json`: every kind's edges, float specials, bytes, decimal and timestamp precision, sealed/SQL nulls per kind, exact write conversions between widths, cross-kind write refusals, `undefined` on a typed field, Go host conversions); every positive case written fresh by each runner and opened by the other ([exchange profile](exchange-contract.md)) | Python/TS mappings; dates before year 1 and the timestamp range ends | | 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). One value of a kind known only at run time, as one ciphertext: the [single-value profile](single-value-contract.md), run by native Rust (`v1/single-value-v1.cases.json`: every scalar kind, exact conversions, JSON documents, batches, and the refusals, with the plaintext of each leaf and a frozen ciphertext). `packages/stack-encrypt/tests/single_value.rs` compares the run-time interface with the typed interface. 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; the Go runner of the single-value profile (issue #2010), and Python and TypeScript runners | -| 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 | +| 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. One field of a record encrypted and decrypted alone: the [one-field profile](record-fields-contract.md), run by native Rust (`v1/record-fields-v1.cases.json`: every output kind, nulls stored both ways, a scope and a binding, the refusals, each node checked against a whole row's both ways), and `packages/stack-encrypt/tests/one_field.rs`, which also covers a field that names an EQL type | EQL-resolver profile; SDK-to-declaration mappings; runtime source/read shapes; the Go runner of the one-field profile | | 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 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 | diff --git a/conformance/stack-encrypt/record-fields-contract.md b/conformance/stack-encrypt/record-fields-contract.md new file mode 100644 index 000000000..549e76d49 --- /dev/null +++ b/conformance/stack-encrypt/record-fields-contract.md @@ -0,0 +1,146 @@ +# One-field profile + +`record-fields-v1` is the conformance profile for one field of a record, +encrypted and decrypted alone. The profile describes the cases in +`v1/record-fields-v1.cases.json`. The profile is an experimental test +contract. + +An application often changes one field of a row, such as a user's email. It +encrypts the new value under the field's declaration, and stores the result +in the field's column. The result must be what a whole row stores for that +field. So a field encrypted alone must decrypt as part of a whole row, and a +field of a whole row must decrypt alone. + +## Terms + +- A plan is the declaration of a record: its fields, and how to encrypt each + one. The [declaration grammar](declaration-grammar.md) describes it. +- A field's node is the map of the field's outputs, under the field's name + in a stored row. `c` is the ciphertext. Each index term is under its index + key, such as `eq`. +- A frame is one value written as bytes, in the format that the engine reads. +- A descriptor is the text form of the context that a data key is requested + under. +- A refusal is an error that the engine returns for an input it does not + accept. + +## Runners + +The Rust runner is `packages/stack-encrypt/tests/field_conformance.rs`. The +runner calls `Plan::one_field` and the `Planned` calls of +`stack_encrypt::dynamic`. The Go runner is pending, because the Go SDK has +no call for one field yet. The Python and TypeScript runners are pending too. +A missing runner is not a passing result. + +## Cases + +The file has four parts: + +- `seed`: the public seed of the deterministic key provider +- `plans`: the plans that the cases use +- `rows`: a base row for each plan +- `cases`: the list of cases + +Each plan is written in the declaration grammar. Each value is in the +`{type, text}` form that the [value profile](values-contract.md#cases) +defines. + +Each case has these keys: + +| Key | Meaning | +|---|---| +| `id` | The name of the case | +| `plan` | The name of the plan in `plans` | +| `field` | The name of the field | +| `input` | The value to encrypt. A refusal at the `plan` stage has none, and so has a decrypt of no node. | +| `stored` | The value that the field holds once the input is read as its type, where it differs from the input | +| `call` | The context that the call passes: a `scope`, a `bind`, or both. Each is in the `{type, text}` form. A refused case has none. | +| `expected_error` | For a refused case, the `stage` at which it is refused and the `reason` | + +A case with no `expected_error` is positive. A runner does these things for +each positive case: + +1. Encrypt the input alone, under the field of the plan. When the case has + a `call`, pass its scope and binding as the call's context. +2. Encrypt the plan's base row with the field set to the input, under the + same context. +3. Check that both have a node, or that neither has one. A null of a field + with `"null": "sql"` has no node. +4. Check that the two nodes have the same outputs, and that each term is the + same bytes. A ciphertext is not compared, because each one is sealed + under a nonce of its own. +5. Check that the data keys of the field alone are requested under + descriptors that the whole row also requests. +6. Decrypt the node alone, the whole row's node alone, and the frozen node. + Each one must open to `stored`, or to the input when the case has no + `stored`. Then put each node in the whole row in place of the row's own, + and decrypt the row. The field must open to the same value. Each decrypt + passes the case's `call`. A field that has only index terms has nothing + to decrypt, so this step is left out for it. +7. When the case has a `call`, decrypt each node alone with no context. The + decrypt must fail. + +A refused case names one of three stages: + +- `plan`: the runner asks the plan for the field alone, and the engine + refuses it. +- `encrypt`: the runner encrypts the input alone, and the engine refuses it. +- `decrypt`: the runner encrypts the input alone and decrypts the node. With + no input, the runner decrypts no node. The engine refuses the decrypt. + +Every refusal must name the field. The refusal must have the case's reason. +The refusal must come before the refused step requests any data key. The +refusal's message must not contain the input. + +The cases cover: + +- a string with an equality index, and with a match index +- an integer with the `ore` index, and an integer with the `ope` index +- a conversion of an integer to the field's wider type +- a ciphertext with no declared type +- a field with an index and no ciphertext +- a null and a value of a field with `"null": "sealed"`, and of a field with + `"null": "sql"` +- a plan with a scope in each field's context and a binding of its own +- a call that passes a scope, a binding, or both +- a call with a scope and a binding, on a plan with its own scope and + binding +- these refusals: + - a name that the plan does not declare + - a passthrough field + - a plan that takes its context from a field of each record + - a value of another type + - a null where the field takes none + - the decrypt of a field with no ciphertext + - no node, for a field that does not store a null as SQL `NULL` + +A field that names an EQL type is not in this profile, because the profile +runs with no resolver. The Rust tests in `packages/stack-encrypt/tests/one_field.rs` +cover it. + +## Artifacts + +The Rust runner writes `v1/record-fields-v1.expected.rust-native.json`. The +file has one entry for each positive case: + +- `frame`: the frame of the input, in hex +- `descriptors`: the descriptor of each data key that the field alone + requests, in order +- `node`: the field's node, as a map of each output key to its bytes in hex. + The bytes of the ciphertext are under `c`. The bytes of each term are under + its index key. The node is `null` when the field has no node. + +A runner checks that the frame, the descriptors, and the terms of each case +are the frozen ones. The frozen node must have the outputs of the fresh +node. A runner works out from the plan whether the field decrypts, and not +from the frozen node. The runner also checks that the frozen node opens. + +To create or update the file on purpose: + +```sh +STACK_ENCRYPT_UPDATE_FIXTURES=1 mise x --env test -- cargo nextest run --locked \ + -p stack-encrypt --all-features --test field_conformance +``` + +Review the change to the cases and to the file together. Do not update the +file only to make a failing test pass. diff --git a/conformance/stack-encrypt/v1/record-fields-v1.cases.json b/conformance/stack-encrypt/v1/record-fields-v1.cases.json new file mode 100644 index 000000000..183767c08 --- /dev/null +++ b/conformance/stack-encrypt/v1/record-fields-v1.cases.json @@ -0,0 +1,266 @@ +{ + "version": 1, + "profile": "record-fields-v1", + "seed": "0000000000000000000000000000000000000000000000000000000000000000", + "required_capabilities": ["one-field", "row-equivalence", "frozen-open"], + "plans": { + "users": { + "email": { + "context": ["users", "email"], + "outputs": ["c", "eq"], + "type": "string" + }, + "bio": { + "context": ["users", "bio"], + "outputs": ["c", "match"], + "type": "string" + }, + "age": { + "context": ["users", "age"], + "outputs": ["c", "ore"], + "type": "uint32" + }, + "score": { + "context": ["users", "score"], + "outputs": ["c", "ope"], + "type": "int64" + }, + "notes": { + "context": ["users", "notes"], + "outputs": ["c"] + }, + "lookup": { + "context": ["users", "lookup"], + "outputs": ["eq"], + "type": "string" + }, + "visits": { + "context": ["users", "visits"], + "outputs": ["c", "eq"], + "type": "uint64", + "null": "sealed" + }, + "nickname": { + "context": ["users", "nickname"], + "outputs": ["c", "eq"], + "type": "string", + "null": "sql" + }, + "id": { + "context": ["users", "id"], + "outputs": ["passthrough"], + "type": "uint64" + } + }, + "bound": { + "bind": ["row-9"], + "email": { + "context": [["users", "email"], "eu"], + "outputs": ["c", "eq"], + "type": "string" + }, + "age": { + "context": [["users", "age"], "eu"], + "outputs": ["c"], + "type": "uint32" + } + }, + "tenanted": { + "context_field": "tenant", + "tenant": { + "context": ["tenant"], + "outputs": ["passthrough"] + }, + "email": { + "context": ["email"], + "outputs": ["c", "eq"], + "type": "string" + } + } + }, + "rows": { + "users": { + "email": { "type": "string", "text": "grace@example.com" }, + "bio": { "type": "string", "text": "wrote a compiler" }, + "age": { "type": "uint32", "text": "85" }, + "score": { "type": "int64", "text": "3" }, + "notes": { "type": "string", "text": "cobol" }, + "lookup": { "type": "string", "text": "grace" }, + "visits": { "type": "uint64", "text": "4" }, + "nickname": { "type": "string", "text": "amazing" }, + "id": { "type": "uint64", "text": "2" } + }, + "bound": { + "email": { "type": "string", "text": "grace@example.com" }, + "age": { "type": "uint32", "text": "85" } + } + }, + "cases": [ + { + "id": "string-equality", + "plan": "users", + "field": "email", + "input": { "type": "string", "text": "ada@example.com" } + }, + { + "id": "string-match", + "plan": "users", + "field": "bio", + "input": { "type": "string", "text": "wrote the first program" } + }, + { + "id": "uint32-ore", + "plan": "users", + "field": "age", + "input": { "type": "uint32", "text": "36" } + }, + { + "id": "uint8-into-uint32", + "plan": "users", + "field": "age", + "input": { "type": "uint8", "text": "36" }, + "stored": { "type": "uint32", "text": "36" } + }, + { + "id": "int64-ope", + "plan": "users", + "field": "score", + "input": { "type": "int64", "text": "-7" } + }, + { + "id": "untyped-ciphertext", + "plan": "users", + "field": "notes", + "input": { "type": "string", "text": "engines" } + }, + { + "id": "index-only", + "plan": "users", + "field": "lookup", + "input": { "type": "string", "text": "ada" } + }, + { + "id": "sealed-null", + "plan": "users", + "field": "visits", + "input": { "type": "null", "text": "" } + }, + { + "id": "sealed-null-field-value", + "plan": "users", + "field": "visits", + "input": { "type": "uint64", "text": "12" } + }, + { + "id": "sql-null", + "plan": "users", + "field": "nickname", + "input": { "type": "null", "text": "" } + }, + { + "id": "sql-null-field-value", + "plan": "users", + "field": "nickname", + "input": { "type": "string", "text": "countess" } + }, + { + "id": "scope-and-binding", + "plan": "bound", + "field": "email", + "input": { "type": "string", "text": "ada@example.com" } + }, + { + "id": "call-scope-and-binding", + "plan": "users", + "field": "email", + "input": { "type": "string", "text": "ada@example.com" }, + "call": { + "scope": { "type": "uint64", "text": "42" }, + "bind": { "type": "string", "text": "row-7" } + } + }, + { + "id": "call-scope", + "plan": "users", + "field": "age", + "input": { "type": "uint32", "text": "36" }, + "call": { + "scope": { "type": "string", "text": "eu" } + } + }, + { + "id": "call-binding", + "plan": "users", + "field": "bio", + "input": { "type": "string", "text": "wrote the first program" }, + "call": { + "bind": { "type": "uint64", "text": "7" } + } + }, + { + "id": "plan-and-call-context", + "plan": "bound", + "field": "email", + "input": { "type": "string", "text": "ada@example.com" }, + "call": { + "scope": { "type": "uint64", "text": "42" }, + "bind": { "type": "uint64", "text": "7" } + } + }, + { + "id": "sql-null-under-call", + "plan": "users", + "field": "nickname", + "input": { "type": "null", "text": "" }, + "call": { + "scope": { "type": "uint64", "text": "42" }, + "bind": { "type": "string", "text": "row-7" } + } + }, + { + "id": "no-such-field", + "plan": "users", + "field": "phone", + "expected_error": { "stage": "plan", "reason": "no_such_field" } + }, + { + "id": "passthrough-field", + "plan": "users", + "field": "id", + "expected_error": { "stage": "plan", "reason": "passthrough_field" } + }, + { + "id": "context-from-record", + "plan": "tenanted", + "field": "email", + "expected_error": { "stage": "plan", "reason": "context_from_record" } + }, + { + "id": "other-type", + "plan": "users", + "field": "age", + "input": { "type": "string", "text": "thirty-six" }, + "expected_error": { "stage": "encrypt", "reason": "field_type" } + }, + { + "id": "null-not-taken", + "plan": "users", + "field": "email", + "input": { "type": "null", "text": "" }, + "expected_error": { "stage": "encrypt", "reason": "field_type" } + }, + { + "id": "index-only-decrypt", + "plan": "users", + "field": "lookup", + "input": { "type": "string", "text": "ada" }, + "expected_error": { "stage": "decrypt", "reason": "index_only" } + }, + { + "id": "no-node", + "plan": "users", + "field": "email", + "expected_error": { "stage": "decrypt", "reason": "field_missing" } + } + ] +} diff --git a/conformance/stack-encrypt/v1/record-fields-v1.expected.rust-native.json b/conformance/stack-encrypt/v1/record-fields-v1.expected.rust-native.json new file mode 100644 index 000000000..f29a15ceb --- /dev/null +++ b/conformance/stack-encrypt/v1/record-fields-v1.expected.rust-native.json @@ -0,0 +1,135 @@ +{ + "version": 1, + "profile": "record-fields-v1", + "producer": "rust-native", + "cases": { + "call-binding": { + "frame": "0a1700000077726f7465207468652066697273742070726f6772616d", + "descriptors": ["(users/bio)/7u64"], + "node": { + "c": "0100000000000000000000000000000000a73f009ca742867ecef988958a2d8a512000efd0a33ef055ce1e721a36249daadd5aca892c0b51bdb1bd2a2b91b07312f872026fc4abcd9f12dd20dc1dd457cdb9a7954325ac45240a3bdc7412a93cfe89dce7bdcab3b583211f8f74102b9848099b914b143936", + "match": "0100040007000d00140015001e0029002a002c00370039003a003b003d004500460049004a004d0056005d006000620067006a006c00700074007700780081008500860088008c00920093009400a900ac00bc00bd00c500cc00d000d400d800e400e500ed00ef00f700f900fe00" + } + }, + "call-scope": { + "frame": "0624000000", + "descriptors": ["(users/age)/eu"], + "node": { + "c": "01000000000000000000000000000000009e2677de18fd9116091d2f2e243d876b200034136d1ba613cf7736611b7bd790fbd44c8ab39627816748c36d4ba804db90e5029d776e5809b7c92af6654f93cd890b502f4c858b2b0fb68be4f8cafbb2064b6132", + "ore": "02a0d30a83dca390b375d8595f65d596ec5af6c3fdac6752344dca908035c5e3" + } + }, + "call-scope-and-binding": { + "frame": "0a0f000000616461406578616d706c652e636f6d", + "descriptors": ["((users/email)/42u64)/row-7"], + "node": { + "c": "0100000000000000000000000000000000eb5a8cb2e574fc4544f94a1ab66edc472000212987f1dc81f8f6e116fa9659239fa54977354605ac83cbd779b54a2b21089f02b2e5e77eaf0c238498517ed7a8a6882acdfd88baf705190de9815317de2dd1074fa9441bb5738385c24a950e", + "eq": "7e7a2c2dd6a5c86442e74db0afe53f1b9b8f13adbfed1d93d5bf5c7a144fef95" + } + }, + "index-only": { + "frame": "0a03000000616461", + "descriptors": [], + "node": { + "eq": "8ebdc9c4922380a0f97c7ef17b979901b2554a19833e414fbaac25198995569a" + } + }, + "int64-ope": { + "frame": "05f9ffffffffffffff", + "descriptors": ["users/score"], + "node": { + "c": "010000000000000000000000000000000088235c24d4e81f7dbcc8048760a1dcaf200077c6c2cca1d395c207392b38f9e85bc1c5e781bd91e5675954c280068afbde3102cf53c831ddf137c36732125b7079aa9a8d498d993272887ca08501e1de2a7a105a7c77d082", + "ope": "00c569c4e6ced3fded02352593244b6ec082ae5e45511bbac7f96d8093cde83618cbacd5514d51eb9df7279001ce9abd6d096b4d90e480e085826e2998b1ee0f1e" + } + }, + "plan-and-call-context": { + "frame": "0a0f000000616461406578616d706c652e636f6d", + "descriptors": ["((((users/email)/eu)/42u64)/row-9)/7u64"], + "node": { + "c": "01000000000000000000000000000000001977be1dffb22a368d069ae135b72ba22000e2645bfd9d4628666f6702c783362bdda6a20269556f16da502928b7671468cf025be6bebc35088dc4b755087519c75d8dbe59e753db683f46b3328fca5a4081a9efdd3e7dc08ca979a6996b02", + "eq": "8d2088010bccb086ef324e0143be004acd1f538651011c61d220a0fa389b497d" + } + }, + "scope-and-binding": { + "frame": "0a0f000000616461406578616d706c652e636f6d", + "descriptors": ["((users/email)/eu)/row-9"], + "node": { + "c": "01000000000000000000000000000000002fd724ae7eb7430351aa6ca59aa2d4e32000f80b33149e9be75efe4b3c734590735759d167d5b5cdc3a2da94f63712f12d4c02afaa8a6829845196df84e11702bc2223677eae7d1d3709a52ba80542ccbbacf28f36bfcb04e1b5738c1128f5", + "eq": "7ae50ef5ae2218967e60fb0bd8259191742482adcb5db731770e206dc12ad956" + } + }, + "sealed-null": { + "frame": "00", + "descriptors": ["users/visits"], + "node": { + "c": "0100000000000000000000000000000000f5a0086430fbb5e78002740e0909275120000d3577dd0307f4b5cfa91de00a4e58cff6ee21166bb339f0e73b9f51b26da5f002f41e47b657ffebfe4f7b171e4d8fadfdedb0affd2e34bbb023d000632dff8f10a813e5a200", + "eq": "bf04f7e4ef14dd695cee8df4e91148d8d921ddfadea3b0cd07811f086b3f4954" + } + }, + "sealed-null-field-value": { + "frame": "070c00000000000000", + "descriptors": ["users/visits"], + "node": { + "c": "01000000000000000000000000000000005f729b17986b0b330464090d805078e520008dcbb4eeb7a71c7b4d91cbdbfec8009e24761386dd3848cf04142b9a4a3727e802f94f1d44321c3a58c00b979ab74cb17e70bc76b6f60a2bf21eab08843263a3d74655bf982b", + "eq": "0574fcafd072ad9a80344f72f3c9b1719501c302740213c3d7005f356a8e0ffb" + } + }, + "sql-null": { + "frame": "00", + "descriptors": [], + "node": null + }, + "sql-null-field-value": { + "frame": "0a08000000636f756e74657373", + "descriptors": ["users/nickname"], + "node": { + "c": "0100000000000000000000000000000000f94327b8094f562a83b28d79d57a959420002380ecf06ff42c278460ad770dc519f9ee18ab2fa4c32cf641e1a0e8938011cd02978dc038087c49b30440c1f064fd404b7577df9999e57aaa3d2f8f0b6b2ef69d3327fbf11c", + "eq": "80361c8906620db38e7501714a4708bec9ee9f5608f433cea331d11478168e6e" + } + }, + "sql-null-under-call": { + "frame": "00", + "descriptors": [], + "node": null + }, + "string-equality": { + "frame": "0a0f000000616461406578616d706c652e636f6d", + "descriptors": ["users/email"], + "node": { + "c": "01000000000000000000000000000000008cfb5ab62330813bbb10e667292f144f20001101f05e3069129d76f551dd1a63bd3d1d9d02b07dc7cf8744c2c86f130b221002e28f73f0c73a5291c2cf9f00277642d0168a4bca58fc28a028ba330ee2139e17caa97661758910c6988baa3b", + "eq": "a31bbf54c3336b890e7c76dc80dfdbd7866ec0b7e064e509367e5b6bdde2bb07" + } + }, + "string-match": { + "frame": "0a1700000077726f7465207468652066697273742070726f6772616d", + "descriptors": ["users/bio"], + "node": { + "c": "0100000000000000000000000000000000da78b2ec3bce5634f5f5990132ef845c2000cf2d78cb633649506550511a00ddfac7701d04486777e2cbb5054122f64ce999025a5d1761e118b991b20a8564ad05e8aeae46f0bb02e524eee000d0ed38d08bb0eadfc211d53a0d912e11e4b2d2f560f93dfcc82f", + "match": "0100040007000d00140015001e0029002a002c00370039003a003b003d004500460049004a004d0056005d006000620067006a006c00700074007700780081008500860088008c00920093009400a900ac00bc00bd00c500cc00d000d400d800e400e500ed00ef00f700f900fe00" + } + }, + "uint32-ore": { + "frame": "0624000000", + "descriptors": ["users/age"], + "node": { + "c": "010000000000000000000000000000000059699d04b7d5046cfe33150cd6aeb41b2000fc5a812b3745b46abd2e938710ac645246e547fc2bf588d2e39f3c8def8dc7ef025630ba16c32050a404a685ed6eba224c0f229280df70b4280506619d641b060046", + "ore": "f18256adef15adefb7ed6dea76d0de5418257a7406b06cb9b73fbd5bd6ba0618" + } + }, + "uint8-into-uint32": { + "frame": "0d24", + "descriptors": ["users/age"], + "node": { + "c": "0100000000000000000000000000000000b93557d142041bb15764502f71c883e52000dc41268ba2bfe00ba6897b272e9dfb83e5f190b875e425fadfb50a0e9bec34390230baa4b22e7ec9ad3b5f4cffdacfa8f0994c67aa5c169e6a889eaae92d0e816630", + "ore": "f18256adef15adefb7ed6dea76d0de5418257a7406b06cb9b73fbd5bd6ba0618" + } + }, + "untyped-ciphertext": { + "frame": "0a07000000656e67696e6573", + "descriptors": ["users/notes"], + "node": { + "c": "0100000000000000000000000000000000b4e420f2cf5299bd0a89536cc37fc89d20001397fa5ee26ac237b1cbfd9d43840b0fa16e37b70d4f57ab796499acf7953f9802b131a0a715f0976dc9d67048bd40364e068fd827e6726b141208b1c73eaf6542de54f7b0" + } + } + } +} diff --git a/packages/stack-encrypt/CHANGELOG.md b/packages/stack-encrypt/CHANGELOG.md index 7ab0bf090..783e48362 100644 --- a/packages/stack-encrypt/CHANGELOG.md +++ b/packages/stack-encrypt/CHANGELOG.md @@ -9,6 +9,42 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- **Encrypt and decrypt one field of a record alone** (#2028). An + application often changes one field of a row, such as a user's email. + `Plan::one_field(name)` returns a `OneFieldPlan`: the record's plan + narrowed to that field. A value runs under it through `Planned`, as a + record does: + - `Planned::new(&one, value).encrypt_into(&keyset)` returns an + `Option`. It is the field's node, the map of its + outputs (`c` and its index terms), exactly as it sits in a whole row. + It is `None` for a null of a `"null": "sql"` field. Store SQL `NULL` + for it. + - `OwnedFrame::decrypt_from(Planned::new(&one, node), &keyset)` decrypts + a node, or an `Option` of one, to the field's value. + - `Planned::new(&one, values)`, with a `Vec`, is a batch. + It encrypts to a `Vec>`, one node for each + value. A `Planned` of a `Vec>` decrypts to a + `Vec`. A batch is one key request, lowered a chunk at a + time as a batch of rows is. + - The owned form moves the value into its row. It makes no second copy + of the plaintext. + - The field keeps its context, type, conversions, indexes, and null + mode. It also keeps the plan's scopes and bindings. It runs through + the same code as a whole row. So a node encrypted alone decrypts inside + a whole row, and a whole row's node decrypts alone. + - A field that names an EQL type runs through the resolver given to + `Planned::with_resolver`, as it does in a whole row. + - `Plan::one_field` refuses: + - a name that the plan does not declare (`no_such_field`) + - a passthrough field (`passthrough_field`, a new reason) + - every field of a plan that takes its context from a field of each + record (`context_from_record`, a new reason) + - A field with index terms and no `c` has nothing to decrypt. Its + decrypt is refused before any key is requested (`index_only`, a new + reason). + - The conformance profile `record-fields-v1` has the shared cases. Its + Go runner is pending. + - **Encrypt and decrypt one value whose type is known only at run time** (#2009). `dynamic::value` has two functions for a value in a frame. The two functions are the run-time counterparts of the typed `keyset.encrypt` diff --git a/packages/stack-encrypt/docs/language-integration.md b/packages/stack-encrypt/docs/language-integration.md index 064f5c2b0..50161d707 100644 --- a/packages/stack-encrypt/docs/language-integration.md +++ b/packages/stack-encrypt/docs/language-integration.md @@ -170,6 +170,38 @@ says which cipher opens ([`crate::CipherRef`]): A record encrypted with a scope or a binding is decrypted with the same ones, through `decrypt_from_with_context`. +### Encrypt or decrypt one field + +An application often changes one field of a row, and an ORM can encrypt a +row one column at a time. A binding does not build a plan for each field, +or wrap a value in a row of one field. It asks the plan for the field: + +1. [`crate::dynamic::record::Plan::one_field`]`(name)` returns a + [`crate::dynamic::OneFieldPlan`]. Keep it beside the plan. The call + refuses a name that the plan does not declare, and a passthrough field. + It also refuses every field of a plan with a `"context_field"`, because + the field's context is in another field of the row. +2. `Planned::new(&one_field, value).encrypt_into(&keyset)` settles to an + `Option`: the field's **node**, the map of its outputs, + exactly as it sits under the field's name in a whole row. It is `None` + for a null of a `"null": "sql"` field. Store SQL `NULL` for it. +3. `OwnedFrame::decrypt_from(Planned::new(&one_field, node), cipher)` + settles to the field's value. The node is a `StackCipherText`, or an + `Option` of one, whose `None` opens as a null of a `"null": "sql"` + field. A field with index terms and no `c` has nothing to decrypt. + +The field runs through the same code as a whole row. It keeps its context, +type, conversions, indexes, and null mode. It also keeps the plan's scopes +and bindings. So a node encrypted alone decrypts inside a whole row, +and a whole row's node decrypts alone. + +The call takes a scope and a binding as a record does. For a batch, pass a +`Vec` of values in one `Planned`. It encrypts to a +`Vec>`, one node for each value, in order. A +`Planned` of a `Vec>` decrypts to a +`Vec`. A batch is one ZeroKMS call, and the engine lowers it a +chunk at a time, as it lowers a batch of rows. + ### One value as one ciphertext A record is not the only stored form. Rust's `keyset.encrypt(value)` diff --git a/packages/stack-encrypt/src/dynamic/mod.rs b/packages/stack-encrypt/src/dynamic/mod.rs index 23771176e..d61fbd981 100644 --- a/packages/stack-encrypt/src/dynamic/mod.rs +++ b/packages/stack-encrypt/src/dynamic/mod.rs @@ -45,6 +45,9 @@ //! 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`](crate::target::IndexSpec)'s `Index` impls do. +//! * [`OneFieldPlan`] — a record's plan narrowed to one field +//! ([`Plan::one_field`]). One value of the field encrypts to the node +//! that the field has in a whole row. That node decrypts alone. //! * [`read_json`] and [`json_frame`] — a `json` field's document, from //! the JSON text a binding hands over and back to it. //! * [`TargetResolver`] — the EQL types a build holds, installed by the host @@ -130,7 +133,7 @@ pub use crate::json::JsonPlaintext; pub use context::{borrowed, context}; pub use json::{json_frame, read_json, JsonValueError}; pub use kind::{admits, not_admitted, read}; -pub use planned::{DynamicQuery, FieldHandle, Planned, QueryPlan, QueryValue}; +pub use planned::{DynamicQuery, FieldHandle, OneFieldPlan, Planned, QueryPlan, QueryValue}; pub use query::{ check_json_open, check_query, field_operators, json_open, query_term, target_limit, verify, Fidelity, Op, Operand, OperatorEntry, QueryTerm, diff --git a/packages/stack-encrypt/src/dynamic/planned.rs b/packages/stack-encrypt/src/dynamic/planned.rs index 8f38d5a04..45ea75b7a 100644 --- a/packages/stack-encrypt/src/dynamic/planned.rs +++ b/packages/stack-encrypt/src/dynamic/planned.rs @@ -11,6 +11,11 @@ //! a query takes scopes only: a binding on a query is refused before any //! key is requested ([`PlanError::BindInQuery`]). //! +//! A [`OneFieldPlan`], from [`Plan::one_field`], is a plan narrowed to one +//! of its fields. One value of the field runs under it, through the same +//! lowering as a whole row. The value encrypts to the field's node: what +//! the field stores in a whole row. +//! //! ``` //! use stack_encrypt::dynamic::{record, Op, Planned, QueryTerm, frame::OwnedFrame}; //! use stack_encrypt::target::Scope; @@ -343,6 +348,31 @@ impl Plan { resolver, }) } + + /// This plan for the field `name` alone ([`OneFieldPlan`]). One value + /// of the field encrypts and decrypts under it. The field keeps its + /// label, type, indexes, and null mode. It also keeps the plan's scopes + /// and bindings. So the value is sealed as it is in a whole row, with + /// the same terms. + /// + /// A field that cannot be encrypted alone is refused here, before any + /// value exists. + /// + /// # Errors + /// + /// [`Error::Plan`], naming the field, with one of these reasons: + /// + /// - [`Reason::NoSuchField`] for a name that the plan does not declare. + /// - [`Reason::ContextFromRecord`] for a plan that takes its context + /// from a field of each record ([`Plan::with_context_field`]). The + /// field's context is in another field of the row. + /// - [`Reason::PassthroughField`] for a passthrough field. A passthrough + /// is stored as it is. + pub fn one_field(&self, name: &str) -> Result { + Ok(OneFieldPlan { + plan: self.narrowed(name)?, + }) + } } /// A record under a plan encrypts to the stored row: every field under its @@ -477,6 +507,27 @@ where resolver, } = self; let rows = record::source_rows(value, plan)?; + plan.seal_rows(cipher, rows, resolver) + } +} + +impl Plan { + /// Run the plan over its rows, as [`Planned::run_encrypt`] describes: the + /// one executor of a plan that is data. A whole record and one field alone + /// ([`OneFieldPlan`]) both run here. So a field is sealed and indexed the + /// same way in each. + /// + /// # Errors + /// + /// [`Error::Target`] for what the resolver refuses. + /// [`Error::Internal`] for a plan that does not lower. + fn seal_rows<'a, K: 'static>( + &self, + cipher: &'a KeysetCipher<'_, K>, + rows: Rows, + resolver: &(impl TargetResolver + ?Sized), + ) -> Result, Error> { + let plan = self; let extend = plan.declared_context(); let shape = plan.shape(); // Every target field of every row, in row-major order, run by the @@ -612,8 +663,34 @@ where value, resolver, } = self; - let cipher_ref: CipherRef<'a, K> = cipher_ref.into(); let rows = record::record_rows(value, plan)?; + plan.open_rows(cipher_ref, rows, resolver, expected) + .map(|pending| pending.try_map(write_record)) + } +} + +/// One opened row: each field that comes back, with its value, in plan +/// order. +type OpenedRow = Vec<(String, OwnedFrame)>; + +impl Plan { + /// Open the stored rows through the plan, as [`Planned::run_decrypt`] + /// describes: the one opener of a plan that is data. A whole record and + /// one field alone ([`OneFieldPlan`]) both open here. The pending settles + /// to each row's fields, for the caller to write out. + /// + /// # Errors + /// + /// [`Error::Target`] for what the resolver refuses. + fn open_rows<'a, K: 'static>( + &self, + cipher_ref: impl Into>, + rows: Rows, + resolver: &(impl TargetResolver + ?Sized), + expected: Option