Repository navigation
Description
Background
Stack Encrypt is the Rust engine in packages/stack-encrypt. Its simplest call encrypts one value into one ciphertext: keyset.encrypt(value) returns a Ciphertext, and keyset.decrypt::<T>(ciphertext) reads it back. A Ciphertext wraps exactly one sealed leaf, and a leaf holds one scalar: text, a number, bytes, a date and so on.
A language SDK (TypeScript, Go) does not know a value's Rust type when the code is compiled. It hands the engine a frame: a byte buffer that holds one value, whose kind is known only when the code runs. Frame and OwnedFrame are the engine's types for one.
Problem
Encrypting one value works only for a type known at compile time, and decrypting accepts targets that can never succeed.
-
Encrypt has no run-time form.
KeysetCipher::encrypt<S: OpaqueEncode>(packages/stack-encrypt/src/scalar.rs) accepts a type only when its borrow implements vitaminc'sScalarmarker (packages/vitaminc-aead/src/plaintext/mod.rs).Scalaris a promise checked at compile time. AnOwnedFramecan hold a list or a map, so it is not aScalar, and the engine has no single-value encrypt that checks a frame at run time. Each SDK works around this in its own way:- TypeScript wraps the frame in its own type,
OneValue(languages/typescript/packages/recipher-js/crate/src/value.rs), checks the kind itself, and then claimsScalar. - Go has no single-value encrypt at all. The guest says "There is no whole-value encrypt or decrypt here" (
languages/golang/encrypt/guest/src/ops.rs), and every value is sealed as a one-field record.
So the three languages do not produce the same thing for one value.
- TypeScript wraps the frame in its own type,
-
Decrypt is not bound to a scalar.
OpaqueDecodeis implemented for everyDecrypt + Zeroizetype, includingVec<String>and maps.keyset.decrypt::<Vec<String>>(ciphertext)compiles, requests a key, decrypts, and only then fails withError::OpenedType. It can never succeed, because a ciphertext holds one scalar.
Proposal
One PR to next.
- Run-time single-value encrypt, in
stack_encrypt::dynamic. It takes a frame and an optional declared kind (ValueKind), and returns the same kind ofCiphertextthatkeyset.encryptreturns for the same value: the same format, the same sealed leaf plaintext and the same length, and each call opens the other's ciphertext. The bytes cannot be compared whole, because each sealed leaf has a fresh random nonce.- It refuses a list, a map, a passthrough and a null before any key is requested, with a reason that says what was found. A null is refused because the typed call has no nullable form either.
- With a declared kind, the value is converted exactly as a record write converts it (never lossy), and
undefinedis refused with its own reason. - A
jsonkind takes the document as JSON text, checks it by the engine's JSON rules, and seals it asJsonPlaintextis sealed. - It has a form with a context, as
encrypt_with_contextdoes, and a batch form that makes one key request.
- Run-time single-value decrypt. It returns the value as an
OwnedFramethat holds one scalar, with an optional kind the value must open as. A refusal never says what the decrypted value opened as. Ajsonkind returns the document's text, as a record'sjsonfield does. - A scalar bound on the typed decrypt. Add a marker trait to
vitaminc-aeadfor a target that one scalar builds, and boundOpaqueDecodeon it. It covers every target that can succeed today: theFromScalartypes,Protectedof one, andOwnedFrame(one leaf always builds a frame of one scalar). AVecof anything but bytes, a map or a struct no longer compiles. A compile-fail test pins that. - Use it in the TypeScript addon on
next: deleteOneValue, and call the run-time encrypt. - Conformance: a single-value profile in
conformance/stack-encrypt/v1, with the Rust runner. Each case gives a value, a declared kind and a context, with the leaf plaintext and a frozen ciphertext that every runner must open. stack-encryptCHANGELOG: the new API, and the decrypt bound under Breaking.
Relationship to other work
- Go SDK: encrypt one value into one ciphertext, as Rust and TypeScript do #2010: the Go SDK's whole-value encrypt and decrypt, built on this.
- feat(recipher-js): every value type, arrays and JSON documents #1775: the TypeScript value types. It deletes its own
OneValueonce this merges.
Activity
dynamichas no scalar type, so each language SDK writes its own wrapper to callkeyset.encrypt— add a checkedScalarFrame#1825 as a duplicate of this issue