|
1 | 1 | # Attributes |
2 | 2 |
|
3 | | -All attributes live under `#[stash(...)]`. |
| 3 | +All attributes live under `#[stash(...)]`. The four derives read the same |
| 4 | +attributes: `EncryptFrom` / `DecryptInto` emit the structural declarations, |
| 5 | +run over a borrowed plaintext; `EncryptTarget` / `DecryptSource` emit the |
| 6 | +owned declarations (and the structural ones beside them as a bridge), run |
| 7 | +over a plaintext handed over by value, and alone accept `passthrough`. Derive |
| 8 | +one pair or the other on a record, not both. |
4 | 9 |
|
5 | 10 | ## On the struct |
6 | 11 |
|
@@ -32,7 +37,8 @@ literal. |
32 | 37 | | `identity = "..."` | With `struct` only: key this field under the segment `identity` instead of the plaintext field's name, so it is derived under `("<context>", "<identity>")`. One plain segment. The plan builder's `.identity(segment)`. | |
33 | 38 | | `from = field` / `from = 0` | With `struct` only: derive this field from `plaintext.field` (or `plaintext.0` for a tuple struct) when its name differs from its plaintext field's. A plaintext field has one output: two fields `from` one plaintext field are refused (a ciphertext with its terms is one `Encrypted<Terms>` field). | |
34 | 39 | | `default` / `default = expr` | Not derived: filled with `Default::default()` or `expr`. Never encrypted, never authenticated. | |
35 | | -| `decrypt` | Decryption opens this field (`DecryptInto` only). Needed only when the field types cannot decide it — see below. | |
| 40 | +| `decrypt` | Decryption opens this field (`DecryptInto` / `DecryptSource` only). Needed only when the field types cannot decide it — see below. | |
| 41 | +| `passthrough` | With `struct` on the owned derives only: carry the plaintext field of this name (or `from = field`) into the record as it is, **not encrypted and not authenticated**, moved rather than cloned, and move it back on decryption. Keyed under nothing; excludes `identity`, `default` and `decrypt`. | |
36 | 42 |
|
37 | 43 | A field takes no `context = ".."` of its own, on any derive: it is a compile |
38 | 44 | error. A plan has exactly one context source, and every output sits under it. |
@@ -64,6 +70,92 @@ from a `NonEmpty<T>` or a supported integer. Both encodings and the descriptor's |
64 | 70 | structured identity are preserved when borrowing context data is converted into |
65 | 71 | an owned declaration. No context is inferred from a Rust type's name. |
66 | 72 |
|
| 73 | +## Owned records |
| 74 | + |
| 75 | +`#[derive(EncryptTarget, DecryptSource)]` takes the attributes above and |
| 76 | +declares the record the owned way: `value.encrypt_into(&keyset)` consumes |
| 77 | +the plaintext, and `Plaintext::decrypt_from(record, &cipher)` returns it. |
| 78 | +The plaintext type is `Zeroize` (the trait bound says where the wiping |
| 79 | +obligation lies), and the conversion wipes every plaintext field it seals. |
| 80 | +What the caller may or must pass is the record's `ContextInput`: |
| 81 | + |
| 82 | +| Record | Encrypt input | Decrypt input | A call | |
| 83 | +|---|---|---|---| |
| 84 | +| `struct = User, context = "users"` | `ContextExtension<DeclaredContext>` | the same | `user.encrypt_into(&keyset)`, or `.encrypt_into_with_context(&keyset, 7u64)` to extend every field's pair by the row id | |
| 85 | +| `plaintext = T` | `RequiredContext<CallerContext>` (`RequiredContext<AeadContext>` with `context_type = AeadContext`) | the same | `value.encrypt_into_with_context(&keyset, nonempty!("users/email"))` | |
| 86 | +| with a `context_field` of type `C` | `RequiredContext<NonEmpty<C>>` | `ExpectedContext<C>` | `T::decrypt_from(record, &cipher)` opens under the stored context; `decrypt_from_with_context(record, &cipher, nonempty!(..))` also checks it | |
| 87 | + |
| 88 | +A `struct` record's field is handed its pair, extended by the caller's part |
| 89 | +if one was given, so the field's target must accept a caller context: a |
| 90 | +`Ciphertext` (`OptionalContext`), a term or an `Encrypted<Terms>` |
| 91 | +(`RequiredContext<CallerContext>`), a `StackCipherText` |
| 92 | +(`RequiredContext<AeadContext>`), a `plaintext` record, or another `struct` |
| 93 | +record (whose own pairs the outer pair then extends). A target that declares |
| 94 | +its whole context itself (`NoCallerContext`) or requires a context of another |
| 95 | +type (`RequiredContext<u64>`) is refused at that field. A `#[stash(passthrough)]` |
| 96 | +field is moved into the record by the owned conversion; its type is `Clone` |
| 97 | +too, since the record's borrowed form, which a plan or a structural record |
| 98 | +runs, copies it. |
| 99 | + |
| 100 | +A `plaintext` record's fields are all handed the one context the record |
| 101 | +requires, so a term beside an `AeadContext`-only record is refused at the |
| 102 | +term. Omitting a required context is refused at the call |
| 103 | +(`AllowsNoContext`), whatever `Default` the context type has. |
| 104 | + |
| 105 | +Both owned derives also emit the structural declarations: `EncryptTarget` |
| 106 | +emits `EncryptFrom` (the record's legacy context — `DeclaredContext`, |
| 107 | +`CallerContext` or `NonEmpty<C>` — converted into its owned input) and |
| 108 | +`Decryptable`; `DecryptSource` emits `DecryptInto` and `DecryptField`. An |
| 109 | +owned record is therefore a field of a `#[derive(EncryptFrom)]` struct |
| 110 | +record and a typed field of a plan (`encrypt_into::<Record, _>`) without |
| 111 | +a second derive. A hand-written owned target that is to be a record field |
| 112 | +implements `Decryptable` and `DecryptField` beside `DecryptSource`. |
| 113 | + |
| 114 | +```rust |
| 115 | +use stack_encrypt::sem::EqualityTerm; |
| 116 | +use stack_encrypt::target::{DecryptSource, EncryptTarget}; |
| 117 | +use stack_encrypt::{Ciphertext, DecryptFrom, EncryptInto, StackCipher}; |
| 118 | + |
| 119 | +#[derive(Debug, PartialEq, zeroize::Zeroize)] |
| 120 | +struct User { id: u64, name: String, email: String } |
| 121 | + |
| 122 | +/// A searchable scalar: the ciphertext and an equality term, under the one |
| 123 | +/// context the field (or a caller) supplies. |
| 124 | +#[derive(EncryptTarget, DecryptSource)] |
| 125 | +#[stash(plaintext = String)] |
| 126 | +struct SearchableEmail { ciphertext: Ciphertext, equality: EqualityTerm } |
| 127 | + |
| 128 | +#[derive(EncryptTarget, DecryptSource)] |
| 129 | +#[stash(struct = User, context = "users")] |
| 130 | +struct SearchableUser { |
| 131 | + #[stash(passthrough)] |
| 132 | + id: u64, |
| 133 | + name: Ciphertext, // ("users", "name") |
| 134 | + email: SearchableEmail, // ("users", "email") |
| 135 | +} |
| 136 | + |
| 137 | +# tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async { |
| 138 | +let cipher = StackCipher::builder() |
| 139 | + .kms(stack_encrypt::kms::FakeDataKeySource::new()).init().await?; |
| 140 | +let keyset = cipher.default_keyset(); |
| 141 | +let users = vec![ |
| 142 | + User { id: 42, name: "Alice".into(), email: "alice@example.com".into() }, |
| 143 | + User { id: 43, name: "Bob".into(), email: "bob@example.com".into() }, |
| 144 | +]; |
| 145 | +// One batched key request for both rows' four sealed fields. |
| 146 | +let rows: Vec<SearchableUser> = users.encrypt_into(&keyset).await?; |
| 147 | +// A query term is the same derivation under the same context. |
| 148 | +let wanted: EqualityTerm = String::from("alice@example.com") |
| 149 | + .encrypt_into_with_context(&keyset, stack_encrypt::nonempty!("users").with("email")) |
| 150 | + .await?; |
| 151 | +let matched: Vec<SearchableUser> = rows.into_iter().filter(|r| r.email.equality == wanted).collect(); |
| 152 | +let opened = Vec::<User>::decrypt_from(matched, &cipher).await?; |
| 153 | +assert_eq!(opened.len(), 1); |
| 154 | +assert_eq!(opened[0].id, 42); |
| 155 | +# Ok::<(), stack_encrypt::Error>(()) |
| 156 | +# }).unwrap(); |
| 157 | +``` |
| 158 | + |
67 | 159 | ## Which context a record takes |
68 | 160 |
|
69 | 161 | `CallerContext` holds both of Vitamin C's encodings of the context — the AEAD |
|
0 commit comments