Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 50 additions & 61 deletions languages/golang/encrypt/guest/tests/native_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
//! * a leaf inside a guest ciphertext tree **is** the frozen `SealedValue`
//! storage encoding — a native cipher decrypts it;
//! * terms derived through the guest dispatch are byte-identical to the
//! native `sem` calls (the cross-language contract);
//! terms a native value encrypted into its term type derives (the
//! cross-language contract);
//! * record encryption follows its plan, keeps to **one** ZeroKMS call per
//! invocation however many rows, and round-trips;
//! * hostile/malformed inputs and wrong-AAD decrypts map to the documented
Expand All @@ -22,10 +23,10 @@ use std::future::IntoFuture;
mod support;

use stack_encrypt::dynamic::frame::{decode_ciphertext, encode_ciphertext};
use stack_encrypt::sem::DefaultMatch;
use stack_encrypt::sem::{DefaultMatch, EqualityTerm, MatchTerms, OpeTerm, OreTerm};
use stack_encrypt::{
nonempty, CipherText, Label, Node, PassthroughFrame, PendingStackCipherText, SealedValue,
StackCipher,
nonempty, CipherText, EncryptInto, Label, Node, PassthroughFrame, PendingStackCipherText,
SealedValue, StackCipher,
};
use stack_encrypt::{AuditMetadata, Controlled, Protected};
use stack_encrypt_guest::ops::{self, TERM_EQUALITY, TERM_MATCH, TERM_OPE, TERM_ORE};
Expand Down Expand Up @@ -269,12 +270,9 @@ fn guest_terms_match_the_native_sem_derivations() {
TERM_EQUALITY,
))
.expect("eq term");
let native = block_on(
cipher
.default_keyset()
.equality_term(42u32, nonempty!("users/age")),
)
.expect("native eq");
let native: EqualityTerm =
block_on(42u32.encrypt_into_with_context(&cipher.default_keyset(), nonempty!("users/age")))
.expect("native eq");
assert_eq!(eq, native.as_bytes());

let ore = block_on(ops::term(
Expand All @@ -284,12 +282,9 @@ fn guest_terms_match_the_native_sem_derivations() {
TERM_ORE,
))
.expect("ore term");
let native = block_on(
cipher
.default_keyset()
.ore_term(42u32, nonempty!("users/age")),
)
.expect("native ore");
let native: OreTerm<u32> =
block_on(42u32.encrypt_into_with_context(&cipher.default_keyset(), nonempty!("users/age")))
.expect("native ore");
assert_eq!(ore, native.as_ref());

let ope = block_on(ops::term(
Expand All @@ -299,12 +294,9 @@ fn guest_terms_match_the_native_sem_derivations() {
TERM_OPE,
))
.expect("ope term");
let native = block_on(
cipher
.default_keyset()
.ope_term(42u32, nonempty!("users/age")),
)
.expect("native ope");
let native: OpeTerm<u32> =
block_on(42u32.encrypt_into_with_context(&cipher.default_keyset(), nonempty!("users/age")))
.expect("native ope");
assert_eq!(ope, native.as_ref());

let m = block_on(ops::term(
Expand All @@ -314,10 +306,10 @@ fn guest_terms_match_the_native_sem_derivations() {
TERM_MATCH,
))
.expect("match term");
let native = block_on(
cipher
.default_keyset()
.match_terms::<DefaultMatch>("alice smith", nonempty!("users/name")),
let native: MatchTerms<DefaultMatch> = block_on(
"alice smith"
.to_string()
.encrypt_into_with_context(&cipher.default_keyset(), nonempty!("users/name")),
)
.expect("native");
assert_eq!(m, native.to_bytes());
Expand All @@ -339,17 +331,15 @@ fn guest_terms_match_the_native_sem_derivations() {
))
.expect("bytes term");
assert_ne!(eq_text, eq_bytes);
let native_text = block_on(
cipher
.default_keyset()
.equality_term("ab".to_string(), nonempty!("f")),
let native_text: EqualityTerm = block_on(
"ab".to_string()
.encrypt_into_with_context(&cipher.default_keyset(), nonempty!("f")),
)
.expect("native");
assert_eq!(eq_text, native_text.as_bytes());
let native_bytes = block_on(
cipher
.default_keyset()
.equality_term(Protected::new(b"ab".to_vec()), nonempty!("f")),
let native_bytes: EqualityTerm = block_on(
Protected::new(b"ab".to_vec())
.encrypt_into_with_context(&cipher.default_keyset(), nonempty!("f")),
)
.expect("native");
assert_eq!(eq_bytes, native_bytes.as_bytes());
Expand Down Expand Up @@ -840,30 +830,28 @@ fn record_terms_equal_the_native_derivations_and_probe_them() {
"output order is the plan's"
);

// The stored terms are byte-identical to query-time probes built the
// native way — the property that makes the index searchable.
let eq_probe = block_on(
cipher
.default_keyset()
.equality_term(34u32, nonempty!("users").with("age")),
// The stored terms are byte-identical to the terms a native value
// encrypted into its term type derives. For equality, ORE, and this
// match text, that is also the query's term. That is what makes the
// index searchable.
let eq_probe: EqualityTerm = block_on(
34u32.encrypt_into_with_context(&cipher.default_keyset(), nonempty!("users").with("age")),
)
.expect("probe");
assert_eq!(term_bytes(&age_outputs[1].1), eq_probe.as_bytes());
let ore_probe = block_on(
cipher
.default_keyset()
.ore_term(34u32, nonempty!("users").with("age")),
let ore_probe: OreTerm<u32> = block_on(
34u32.encrypt_into_with_context(&cipher.default_keyset(), nonempty!("users").with("age")),
)
.expect("probe");
assert_eq!(term_bytes(&age_outputs[2].1), ore_probe.as_ref());

let (_, CipherText::Map(name_outputs)) = &fields[1] else {
panic!("expected an output map for the second field");
};
let match_probe = block_on(
cipher
.default_keyset()
.match_terms::<DefaultMatch>("alice smith", nonempty!("users").with("name")),
let match_probe: MatchTerms<DefaultMatch> = block_on(
"alice smith"
.to_string()
.encrypt_into_with_context(&cipher.default_keyset(), nonempty!("users").with("name")),
)
.expect("probe");
assert_eq!(term_bytes(&name_outputs[1].1), match_probe.to_bytes());
Expand Down Expand Up @@ -920,14 +908,14 @@ fn a_structured_plan_context_seals_what_the_native_extended_context_does() {
};

let native = nonempty!("users").with("age").with(7u64);
let eq_probe = block_on(cipher.default_keyset().equality_term(34u32, native)).expect("probe");
let eq_probe: EqualityTerm =
block_on(34u32.encrypt_into_with_context(&cipher.default_keyset(), native)).expect("probe");
assert_eq!(term_bytes(&age_outputs[1].1), eq_probe.as_bytes());
let ore_probe = block_on(cipher.default_keyset().ore_term(34u32, native)).expect("probe");
let ore_probe: OreTerm<u32> =
block_on(34u32.encrypt_into_with_context(&cipher.default_keyset(), native)).expect("probe");
assert_eq!(term_bytes(&age_outputs[2].1), ore_probe.as_ref());
let flat_probe = block_on(
cipher
.default_keyset()
.equality_term(34u32, nonempty!("users").with("age")),
let flat_probe: EqualityTerm = block_on(
34u32.encrypt_into_with_context(&cipher.default_keyset(), nonempty!("users").with("age")),
)
.expect("probe");
assert_ne!(
Expand All @@ -948,12 +936,12 @@ fn a_structured_plan_context_seals_what_the_native_extended_context_does() {
let (_, CipherText::Map(name_outputs)) = &fields[1] else {
panic!("expected an output map for the second field");
};
let match_probe = block_on(
cipher
.default_keyset()
.match_terms::<DefaultMatch>("alice smith", nonempty!("users").with("name").with(7u64)),
)
.expect("probe");
let match_probe: MatchTerms<DefaultMatch> =
block_on("alice smith".to_string().encrypt_into_with_context(
&cipher.default_keyset(),
nonempty!("users").with("name").with(7u64),
))
.expect("probe");
assert_eq!(term_bytes(&name_outputs[1].1), match_probe.to_bytes());

let CipherText::Single(leaf) = &age_outputs[0].1 else {
Expand Down Expand Up @@ -1367,7 +1355,8 @@ fn terms_derive_under_the_selected_keyset() {
.expect("globex term");
assert_ne!(acme_term, globex_term);

let native = block_on(acme.equality_term(42u32, nonempty!("users/age"))).expect("native");
let native: EqualityTerm =
block_on(42u32.encrypt_into_with_context(&acme, nonempty!("users/age"))).expect("native");
assert_eq!(acme_term, native.into_bytes().to_vec());
}

Expand Down
12 changes: 7 additions & 5 deletions packages/eql/tests/encryption/tests/text_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use eql_bindings::v3::terms::{Ciphertext, Hmac256};
use eql_bindings::v4::text::{TextEq, TextEqQuery};
use eql_bindings::v4::SchemaVersion;
use eql_bindings::Identifier;
use stack_encrypt::sem::EqualityTerm;
use stack_encrypt::target::transcode::{Reader, Transcode};
use stack_encrypt::{DecryptFrom, Encrypt, EncryptInto, NonEmpty};
use vitaminc_aead::plaintext::Sink;
Expand Down Expand Up @@ -62,8 +63,9 @@ async fn text_round_trips_through_json_and_both_canonical_directions() {
v: SchemaVersion::CURRENT,
i: column().into_inner(),
c: native.read(Ciphertext::visitor()).unwrap(),
hm: keyset
.equality_term(value.clone(), column())
hm: value
.clone()
.encrypt_into_with_context::<EqualityTerm, _>(&keyset, column())
.await
.unwrap()
.read(Hmac256::visitor())
Expand Down Expand Up @@ -125,9 +127,9 @@ async fn independent_queries_match_native_terms_without_requesting_data_keys() {
.await
.unwrap();
assert_eq!(query.hm, stored.hm);
let canonical = reader
.default_keyset()
.equality_term(value.clone(), column())
let canonical = value
.clone()
.encrypt_into_with_context::<EqualityTerm, _>(&reader.default_keyset(), column())
.await
.unwrap()
.read(Hmac256::visitor())
Expand Down
67 changes: 67 additions & 0 deletions packages/stack-encrypt/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,73 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Breaking

- **`KeysetCipher::equality_term`, `match_terms`, `ore_term`,
`ope_term`, and `ore_block_term` are no longer public** (#1760). Each
method built one term on its own, outside the target path and the
query path, and copied its target's code. A query built with one of
them was not tied to the field it searched. Not all query terms are
the same as the stored terms. With `MatchOptions::short_words` on, the
`match_terms` filter of "Mike Ng" misses "Mike Nguyen".

A query must be built in the same domain as the stored term it
searches. A field reference does this: it builds the query under the
field's own context and options. A term does not have to share its
ciphertext's context. A type that needs that ties the two together, as
an EQL type does with its one context field.

To upgrade, build each term one of two ways:
- **To store a value**, encrypt it into a derived record with a field
of the term type, or run `target::indexed` over it. Use
`EqualityTerm` for `equality_term`, `MatchTerms<O>` for
`match_terms`, `OreTerm<T>` for `ore_term`, `OpeTerm<T>` for
`ope_term`, and `OreBlockTerm<T>` for `ore_block_term`. Encrypting
the value into the bare term type gives the same term, with no
ciphertext beside it. `OreTerm::inner()` and `OpeTerm::inner()`
return the ciphertext the old methods returned. A target takes text
as a `String`, not a `&str`.
- **To query**, use the field reference's operator method, such as
`EncryptedUser::EMAIL.eq(&email)` or
`EncryptedUser::NAME.matches("Mike Ng")`. A query target run on its
own, such as `target::matching_query`, also works. A bare
`MatchTerms<O>` with `short_words` on is a stored filter, not a
query.

```rust
// Before
let term = keyset.equality_term("[email protected]", nonempty!("users/email")).await?;

// After: the term to store
let term: EqualityTerm = String::from("[email protected]")
.encrypt_into_with_context(&keyset, nonempty!("users/email"))
.await?;
// After: the query
let query = EncryptedUser::EMAIL.eq(&email).encrypt(&keyset).await?;
```

`OreTerm<T>`, `OpeTerm<T>`, and `OreBlockTerm<T>` need `T: Zeroize`.
The old `ore_term`, `ope_term`, and `ore_block_term` also took three
types that are not `Zeroize`: jiff's `civil::Date` and `Timestamp`,
and rust_decimal's `Decimal`. For these, convert the value to
`value::Date`, `value::Timestamp`, or `value::Decimal` first. Or run
the borrowed operation, as below, to read the value in place:

```rust
let term: OpeTerm<value::Date> = value::Date::new(dob)
.encrypt_into_with_context(&keyset, nonempty!("users/dob"))
.await?;
// Or, with the jiff date as it is
let context = CallerContext::from(nonempty!("users/dob"));
let term: OpeTerm<jiff::civil::Date> = keyset
.run(target::ope::<_, _, Borrowed>(), &dob, context)
.await?;
```

`target::ore` gives the order-revealing encryption (ORE) term the same
way. The two routes give the same bytes. `target::ore_block` gives the
block ORE term the same way. A block ORE term contains a random nonce,
so its two routes give terms that compare equal, with the same left
half. No stored byte changes.

- **`Index` and `Indexes` each gain a required method** (#1398). A field
query now derives its terms through these methods.
- `Index::query_operation` derives the term that a query of the index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,36 @@ cannot tell which they are producing.
> query path for a match index. ADR-0009's amendment of the same date gives
> the full rule.

> **Amended 2026-11-23 (#1760).** The four methods, and `ore_block_term`,
> are no longer public. Each copied its target's code. A query built with
> one of them was not tied to the field it searched.
>
> The rule that matters is that a query is built in the same domain as
> the stored term it searches. Not all query terms are the same as the
> stored terms: a match query is a different filter from the stored one.
> A field reference builds its query under the field's own context and
> options, so it meets the rule.
>
> A term does not have to share its ciphertext's context. This changes
> the reason decision 5 gives. A type that needs the two under one
> context ties them together. An EQL type does it with its one context
> field. `Encrypted<(…)>`, the output of `target::indexed`, does it too.
> The bare term targets `EqualityTerm`, `MatchTerms<O>`,
> `OreTerm<T>`, `OpeTerm<T>`, and `OreBlockTerm<T>` stay public, for a
> type that needs a term on its own.
>
> Outside the crate, build each term this way:
>
> - **To store a value**, encrypt it into its type. That is a derived
> record with a field of the term type, the output of
> `target::indexed`, or the bare term type.
> - **To query**, use the field reference's operator method
> (`EncryptedUser::EMAIL.eq(..)`, `EncryptedUser::NAME.matches(..)`).
> A query target run on its own, such as `target::matching_query`, also
> builds a query. It takes its context from the caller, so pass the
> stored term's context. A bare `MatchTerms<O>` with
> `MatchOptions::short_words` on is a stored filter, not a query.

### 6. Targets declare their sources; `FfiValue` is not one

A target names the plaintext it accepts (`TextEq` from `Protected<String>`), and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,8 @@ The changes:
query's `prepare`. `target::matching` stores such text as the empty
filter.
- **`KeysetCipher::match_terms` builds a stored value's filter.** With
short words on, it is not a way to build a query.
short words on, it is not a way to build a query. ADR-0004's #1760
amendment removes the method from the public API.
- **EQL v4's query twins carry `QueryBloomFilter`.** A query twin is the
eql-bindings struct that contains a query's terms, such as
`TextMatchQuery`. `QueryBloomFilter` has `BloomFilter`'s JSON and JSON
Expand Down
4 changes: 2 additions & 2 deletions packages/stack-encrypt/src/dynamic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//! Everything else in this crate is typed: a target names its source type,
//! and the operations it composes are reached through bounds — `Encrypt` for
//! a ciphertext or an equality term, `AsRef<str>` for a match term. That is what makes a term a cross-language
//! contract: `equality_term(34u32)` derives the same bytes wherever it is
//! called from, because `34u32` is the same value everywhere.
//! contract. `34u32` encrypted into an `EqualityTerm` derives the same bytes
//! wherever it is encrypted, because `34u32` is the same value everywhere.
//!
//! An FFI binding cannot reach those bounds. Its field types arrive as wire
//! data, so there is no Rust type to name — and no dynamic value can satisfy
Expand Down
Loading
Loading