Skip to content

stack-encrypt: make the standalone term methods (equality_term, match_terms, ore_term, ope_term) crate-private #1760

Closed

Description

@coderdan

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 TextMatch field 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_terms builds a query that misses rows. With MatchOptions::short_words on, 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 documents match_terms as the stored filter. The method is still public, and still looks like a query.
  • ore_term and ope_term can 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

  1. Make equality_term, match_terms, ore_term, and ope_term crate-private. The dynamic path (src/dynamic/term.rs) uses them, and keeps doing so.
  2. 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, and match_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.
  3. 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.
  4. 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.
  5. Add a "Breaking" entry to the stack-encrypt CHANGELOG with the upgrade steps.

Relationship to other work

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

Metadata

Metadata

Assignees

Labels

SDKrust

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions