Skip to content

Commit 6d2fc0e

Browse files
committed
feat(encrypt)!: add owned scalar ciphertexts
BREAKING CHANGE: keyset encrypt/decrypt use opaque Ciphertext and separate context methods. Structural calls move to encrypt_node/decrypt_node; borrowed conversion helpers move to target::legacy. Declare owned conversions with EncryptTarget/DecryptSource and explicit context capabilities. Guard plaintext during planning and preserve the existing sealed-leaf bytes and Go/dynamic execution paths.
1 parent 71176ae commit 6d2fc0e

46 files changed

Lines changed: 1212 additions & 165 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
---
3+
4+
Stack Encrypt's Rust scalar API now consumes plaintext and returns an opaque
5+
`Ciphertext`, with separate methods for omitted and supplied context. Owned
6+
`EncryptInto` / `DecryptFrom` conversions use explicit context capabilities.
7+
The stored sealed-leaf format is unchanged.
8+
9+
This is a Rust-only release, versioned by release-plz from the conventional
10+
commit. No npm package needs a version bump; EQL changes only adapt tests.

‎packages/eql/tests/encryption/tests/text_eq.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async fn text_round_trips_through_json_and_both_canonical_directions() {
5252
let opened: String = cipher.decrypt_as(stored, Default::default()).await.unwrap();
5353
assert_eq!(opened, value);
5454

55-
let native = keyset.encrypt(value.clone(), column()).await.unwrap();
55+
let native = keyset.encrypt_node(value.clone(), column()).await.unwrap();
5656
let stored = TextEq {
5757
v: SchemaVersion::CURRENT,
5858
i: column().into_inner(),

‎packages/eql/tests/encryption/tests/v4_types.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async fn sealed_from_frame(
8484
keyset: &stack_encrypt::KeysetCipher<'_, common::Observed>,
8585
value: ValueFrame,
8686
) -> Ciphertext {
87-
match keyset.encrypt(value, column()).await.unwrap() {
87+
match keyset.encrypt_node(value, column()).await.unwrap() {
8888
CipherText::Single(leaf) => CiphertextVisitor.sealed(leaf).unwrap(),
8989
_ => panic!("a scalar seals as one leaf"),
9090
}

‎packages/stack-encrypt/Cargo.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,7 @@ required-features = ["http"]
171171

172172
[package.metadata.docs.rs]
173173
all-features = true
174+
175+
[[example]]
176+
name = "scalar"
177+
required-features = ["test-support"]

‎packages/stack-encrypt/examples/encrypted_record.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
//! `zerokms_auth` example for the lookup order).
2828
2929
use stack_encrypt::sem::{EqualityTerm, OreTerm};
30-
use stack_encrypt::target::{DecryptFrom, EncryptInto};
30+
use stack_encrypt::target::legacy::{DecryptFrom, EncryptInto};
3131
use stack_encrypt::{nonempty, DecryptInto, EncryptFrom, Error, StackCipher, StackCipherText};
3232

3333
// --- The shapes ----------------------------------------------------------------

‎packages/stack-encrypt/examples/mixed_user.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
126126
// in the whole Vec (here: 3 rows x 2 encrypted fields = 6 data keys).
127127
let ciphertext = cipher
128128
.default_keyset()
129-
.encrypt(users, record.clone())
129+
.encrypt_node(users, record.clone())
130130
.await?;
131131

132132
println!("what the stored ciphertext reveals:");
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//! Run without credentials: cargo run -p stack-encrypt --example scalar --features test-support
2+
use stack_encrypt::{Ciphertext, DecryptFrom, EncryptInto, StackCipher};
3+
4+
#[tokio::main(flavor = "current_thread")]
5+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
6+
// In production, use StackCipher::new().await? with ZeroKMS credentials.
7+
let cipher = StackCipher::builder()
8+
.kms(stack_encrypt::kms::FakeDataKeySource::new())
9+
.init()
10+
.await?;
11+
let keyset = cipher.default_keyset();
12+
13+
let encrypted = keyset.encrypt(42i32).await?;
14+
let stored = encrypted.to_bytes();
15+
let restored = Ciphertext::from_bytes(&stored)?;
16+
let value: i32 = keyset.decrypt(restored).await?;
17+
assert_eq!(value, 42);
18+
19+
let encrypted = keyset.encrypt_with_context(42i32, ("users", "age")).await?;
20+
let value: i32 = keyset
21+
.decrypt_with_context(encrypted, ("users", "age"))
22+
.await?;
23+
assert_eq!(value, 42);
24+
25+
let encrypted: Ciphertext = 42i32.encrypt_into(&keyset).await?;
26+
let value = i32::decrypt_from(encrypted, &keyset).await?;
27+
assert_eq!(value, 42);
28+
29+
let encrypted: Ciphertext = 42i32
30+
.encrypt_into_with_context(&keyset, ("users", "age"))
31+
.await?;
32+
let value = i32::decrypt_from_with_context(encrypted, &keyset, ("users", "age")).await?;
33+
assert_eq!(value, 42);
34+
Ok(())
35+
}

‎packages/stack-encrypt/examples/search_terms.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
//! `zerokms_auth` example for the lookup order).
2020
2121
use stack_encrypt::sem::{EqualityTerm, MatchTerms, OreTerm};
22-
use stack_encrypt::target::EncryptInto;
22+
23+
use stack_encrypt::target::legacy::EncryptInto;
2324
use stack_encrypt::{nonempty, EncryptFrom, StackCipher, StackCipherText};
2425

2526
#[tokio::main(flavor = "current_thread")]

‎packages/stack-encrypt/examples/zerokms_auth.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
5656
let keyset = cipher.default_keyset();
5757
println!("connected; keyset {}", keyset.keyset_id());
5858

59-
let ciphertext = keyset.encrypt("hello", "demo/greeting").await?;
59+
let ciphertext = keyset.encrypt_node("hello", "demo/greeting").await?;
6060
let plaintext: String = cipher.decrypt(ciphertext, "demo/greeting").await?;
6161
assert_eq!(plaintext, "hello");
6262
println!("round-tripped a value under the default keyset");
@@ -72,7 +72,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
7272
// let customers = cipher
7373
// .keyset(IdentifiedBy::Name("customers".to_string().into()))
7474
// .await?;
75-
// let ciphertext = customers.encrypt("hello", "demo/greeting").await?;
75+
// let ciphertext = customers.encrypt_node("hello", "demo/greeting").await?;
7676
//
7777
// `default_keyset()` above is not one of these: it is the client's own
7878
// default, the keyset a ZeroKMS administrator set for this client, and

‎packages/stack-encrypt/src/cipher.rs‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
//!
2424
//! * **Encrypt** — [`PendingStackCipherText::plan`] walks the value into
2525
//! vitaminc's pending tree and does no I/O. A single
26-
//! [`PendingStackCipherText::seal`] (or the [`KeysetCipher::encrypt`]
26+
//! [`PendingStackCipherText::seal`] (or the [`KeysetCipher::encrypt_node`]
2727
//! convenience) then batches **one** `generate_keys` call for the whole
2828
//! tree, under the handle's keyset, and seals every leaf through
2929
//! `MintedKeys`, the sealing half of this crate's [`Cipher`], one key per
@@ -667,7 +667,7 @@ impl<K: IndexKeySource> StackCipher<K> {
667667
/// .init()
668668
/// .await?;
669669
/// let tenant = cipher.keyset(IdentifiedBy::Name("acme".to_string().into())).await?;
670-
/// let sealed = tenant.encrypt("hello".to_string(), nonempty!("greeting")).await?;
670+
/// let sealed = tenant.encrypt_node("hello".to_string(), nonempty!("greeting")).await?;
671671
/// # Ok(())
672672
/// # }
673673
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap();
@@ -1010,10 +1010,13 @@ fn hmac_prf_from_index_key(index_key: &stack_kms::IndexKey) -> vitaminc_hmac::Hm
10101010
}
10111011

10121012
impl<K: DataKeySource + IndexKeySource> KeysetCipher<'_, K> {
1013+
/// Low-level structural encryption. For one opaque scalar use
1014+
/// [`Self::encrypt`] or [`Self::encrypt_with_context`].
1015+
///
10131016
/// Encrypt a value, binding `aad`, and seal it against fresh ZeroKMS data
10141017
/// keys in a single batched `generate_keys` call under this keyset.
10151018
/// Every key is requested under the [`Descriptor`] of `aad`.
1016-
pub async fn encrypt<'a, T, A>(&self, value: T, aad: A) -> Result<StackCipherText, Error>
1019+
pub async fn encrypt_node<'a, T, A>(&self, value: T, aad: A) -> Result<StackCipherText, Error>
10171020
where
10181021
T: Encrypt,
10191022
A: IntoContext<'a>,
@@ -1026,7 +1029,11 @@ impl<K: DataKeySource + IndexKeySource> KeysetCipher<'_, K> {
10261029
/// [`StackCipher::decrypt`], constrained to this keyset: a leaf sealed
10271030
/// under any other is [`Error::ForeignKeyset`], refused before any key
10281031
/// is retrieved.
1029-
pub async fn decrypt<'a, T, A>(&self, ciphertext: StackCipherText, aad: A) -> Result<T, Error>
1032+
pub async fn decrypt_node<'a, T, A>(
1033+
&self,
1034+
ciphertext: StackCipherText,
1035+
aad: A,
1036+
) -> Result<T, Error>
10301037
where
10311038
T: Decrypt,
10321039
A: IntoContext<'a>,
@@ -1784,13 +1791,16 @@ mod tests {
17841791
.await
17851792
.unwrap();
17861793
let keyset = cipher.default_keyset();
1787-
let leaf = keyset.encrypt(7u32, "users/age").await.unwrap();
1794+
let leaf = keyset.encrypt_node(7u32, "users/age").await.unwrap();
17881795
let opened: Result<String, _> = cipher.decrypt(leaf, "users/age").await;
17891796
assert!(
17901797
matches!(opened, Err(Error::OpenedType { .. })),
17911798
"{opened:?}"
17921799
);
1793-
let seq = keyset.encrypt(vec![7u32, 8], "users/age").await.unwrap();
1800+
let seq = keyset
1801+
.encrypt_node(vec![7u32, 8], "users/age")
1802+
.await
1803+
.unwrap();
17941804
let opened: Result<u32, _> = cipher.decrypt(seq, "users/age").await;
17951805
assert!(
17961806
matches!(opened, Err(Error::OpenedType { .. })),

0 commit comments

Comments
 (0)