Repository navigation
Closed
Milestone
Description
Background
Stack Encrypt is the Rust library that encrypts values for CipherStash. Besides a value's ciphertext, it can build search terms, so the database can compare encrypted values. Equality, match (text search), and order each have their own term.
An app normally gets both kinds of term without asking for one directly:
- The value your app stores. Your app encrypts a value into its target type, such as a
TextMatchfield on a derived record. The target seals the ciphertext and builds each stored term under the same context. - The query your app sends. Your app builds the query from the field's reference, such as
EncryptedUser::NAME.matches("Mike Ng"). The query uses the field's own index options.
KeysetCipher also has four public methods that build one term on its own, outside any target, query, or ciphertext, in packages/stack-encrypt/src/sem/mod.rs:
| Method | Term it builds |
|---|---|
equality_term |
An equality term (HMAC, a keyed hash) |
match_terms |
A stored match filter (a Bloom filter) |
ore_term |
An order term, using order-revealing encryption (ORE) |
ope_term |
An order term, using order-preserving encryption (OPE) |
Problem
These methods look like a way to build a query term, but most of them build the term your app stores. For most indexes, a query term is not the same as a stored term.
match_termsbuilds a query that misses rows. WithMatchOptions::short_wordson, it adds each short word as a whole-word token. A query for "Mike Ng" built from it asks for the word "ng". So it does not find "Mike Nguyen". fix(stack-encrypt,eql-bindings): cut a query's short words only when it cuts no n-gram #1753 fixes the query path and documentsmatch_termsas the stored filter. The method is still public, and still looks like a query.ore_termandope_termcan differ from a query term in the same way. The design record ADR-0009 says the two terms are equal only for HMAC equality. They are also equal for OPE, when its order encoding is lossless.- A stored term made without a target is unsafe to store. Nothing ties it to the context its ciphertext was sealed under. ADR-0004 decision 5 says a stored term goes through a target for that reason. The rule is in documentation only, and no code enforces it.
So each public method is a second way to build a term. For most uses, that second way gives the wrong term.
Proposal
- Make
equality_term,match_terms,ore_term, andope_termcrate-private. Thedynamicpath (src/dynamic/term.rs) uses them, and keeps doing so. - Move every caller outside the crate to the target or query path. The callers today are:
- stack-encrypt's integration tests, in 13 files, including
frozen_bytes.rs,term_bytes.rs,sem_terms.rs,source_mode.rs, andmatch_short_words.rs; - the EQL encryption tests, in
packages/eql/tests/encryption/tests/text_eq.rs; - the Go guest's native tests, in
languages/golang/encrypt/guest/tests/native_ops.rs.
- stack-encrypt's integration tests, in 13 files, including
- Keep the byte-for-byte checks. Several tests check that a target's term equals the standalone term. Move those checks into unit tests inside the crate, or compare the target path with a frozen value instead. The frozen stored-bytes tests must pass unchanged.
- Update the docs that name these methods: their rustdoc, ADR-0004 decision 5, the CHANGELOG, and any README. Say how to get each term: store a value through its target, and send a query from the field's reference.
- Add a "Breaking" entry to the stack-encrypt CHANGELOG with the upgrade steps.
Relationship to other work
- match index: "Mike Ng" no longer finds "Mike Nguyen" with short words; cut query word tokens only when the query has no n-gram #1398 and its PR fix(stack-encrypt,eql-bindings): cut a query's short words only when it cuts no n-gram #1753 found the problem with
match_terms. - stack-encrypt: move the JsonDocument EQL simulators (find, children, subtree, node) out of the public API #1755 does the same kind of change for the
JsonDocumentmethods that copy what EQL does in the database.
Activity