Repository navigation
Description
Background
Stack Encrypt (the Rust crate stack-encrypt) encrypts one value as one ciphertext through its opaque scalar path. In Rust that is keyset.encrypt(value), which seals the value as a single leaf. The path only accepts a type whose borrow implements vitaminc's Scalar trait, a compile-time promise that the value is one scalar and not an array or a map (packages/stack-encrypt/src/scalar.rs):
impl<S> OpaqueEncode for S
where
S: Zeroize + Send + 'static,
for<'a> &'a S: Scalar,
{
}Language SDKs reach the engine through stack_encrypt::dynamic, where a value's type is known only at run time. A value arrives there as a dynamic::ValueFrame, which can hold any value of the engine's value model, an array or a map included. So a ValueFrame is not a Scalar, and dynamic has no type that is.
Problem
A language SDK that wants to encrypt one runtime-typed value as one ciphertext, with the same bytes that Rust's keyset.encrypt writes, has to build the missing type itself.
TypeScript (@cipherstash/recipher, #1774) wraps the frame in a type of its own, checks its kind at run time, and then declares it a Scalar (languages/typescript/packages/recipher-js/crate/src/value.rs):
#[derive(OpaqueDebug)]
pub(crate) struct OneValue(Option<ValueFrame>);
impl OneValue {
pub(crate) fn new(value: ValueFrame) -> Result<Self, RecipherErrorFields> {
if value.kind() == Some(ValueKind::String) {
Ok(Self(Some(value)))
} else {
Err(Error::NotAString.into())
}
}
}
impl zeroize::Zeroize for OneValue {
fn zeroize(&mut self) {
// Dropping the frame wipes its buffer.
self.0 = None;
}
}
impl Encrypt for &OneValue {
const DEPTH: Option<usize> = depth::SCALAR;
fn visit<S: Sink>(self, sink: S) -> Result<S::Ok, S::Error> {
match &self.0 {
Some(frame) => frame.visit(sink),
None => Err(S::Error::source(SourceError::msg("a wiped value"))),
}
}
}
impl Scalar for &OneValue {}In #1775 the same type widens to any scalar (refusing an array or an object) and to a JSON document. The Scalar promise is now kept by a runtime check in the SDK, not by the engine.
Go does not encrypt a single value at all. Its WASI guest has no whole-value operation: every value goes through a record plan, and an opaque struct is a one-field record (languages/golang/encrypt/guest/src/ops.rs):
//! There is no whole-value encrypt or decrypt here. Every value the Go SDK
//! seals goes through a declaration (ADR-0007, amended): an opaque struct
//! is a one-field record, so the record path is the one path.
pub async fn encrypt_record<K>(
cipher: &KeysetCipher<'_, K>,
source: &Bytes,
plan: &Bytes,
audit: AuditMetadata,
) -> Result<Vec<u8>, u32>
where
K: DataKeySource + IndexKeySource + Sync + 'static,
{
let plan = parse_plan(plan)?;
let tree =
dynamic::record::encrypt_with(cipher, checked_plaintext(source)?, &plan, &resolver())
.map_err(|e| fail_dynamic(&e))?
.audit(audit)
.await
.map_err(|e| fail_error(&e))?;
encode_tree(tree)
}On the Go side, an opaque value is written as a storage blob and handed to that record path as the record's one field (languages/golang/encrypt/gensupport/codec.go):
func opaqueBytes(fields any) ([]byte, error) {
encoded, err := vcffi.MarshalStorage(fields)
if err != nil {
return nil, fmt.Errorf("%w: the opaque value cannot be written as a storage blob: %w", encrypt.ErrEncoding, err)
}
return encoded, nil
}dynamic's own documentation points a binding at keyset.encrypt_tree(value, aad) for a whole value. That returns a CipherText tree under an explicit AAD (additional authenticated data), not the Ciphertext that keyset.encrypt returns. Whether the two give the same stored bytes for one scalar has not been checked; step 2 settles it.
Impact:
- Every language SDK that wants Rust's single-value ciphertext writes its own version of
OneValue. Each copy is a runtime check standing in for a trait bound that the engine relies on. A copy that admits an array would hand the opaque path a value that is not one leaf. - The check, its error code and its wiping are SDK code, reviewed and tested in each SDK, rather than once in the engine.
Proposal
- Add a checked scalar to
dynamic, for exampledynamic::ScalarFrame:ScalarFrame::new(ValueFrame) -> Result<ScalarFrame, _>checks the kind once. It refuses an array and a map with a typed error, a code understack_encrypt::that names the kind it found, and never the value.- It implements
Encryptwithdepth::SCALAR,Scalarfor its borrow, andZeroize.Debugis opaque. Wiping it wipes the frame's buffer. - So
keyset.encrypt(scalar_frame)compiles and seals one leaf.
- Pin the bytes. For each scalar kind of the value model, the leaf a
ScalarFrameseals is byte-for-byte the leafkeyset.encryptseals for the same Rust value under the same key and context. Test this with the engine's deterministic test keys, and add cases to the conformance corpus (conformance/stack-encrypt/v1) so every SDK can check against it. - Decrypt. Check that a single
Ciphertextopens into aValueFrame(or aScalarFrame) of the kind it was sealed as. Add that if it is missing. - Update
dynamic's "What is not here" section to nameScalarFrameandkeyset.encryptas the way a binding seals one value. - Afterwards, in the TypeScript SDK: replace
OneValuewithScalarFrameand deletecrate/src/value.rs. JSON documents already haveJsonPlaintext, which is aScalar.
Out of scope: giving Go a whole-value operation. Go seals every value through a record today. Whether it should also seal a bare value is a separate decision.
Relationship to other work
- feat(recipher-js): walking skeleton of the TypeScript SDK #1774 and feat(recipher-js): every value type, arrays and JSON documents #1775 carry
OneValuein the TypeScript SDK. - stack-encrypt: the dynamic value model refuses a UUID, so no SDK can encrypt one — add it #1821 adds a UUID to the value model. A
ScalarFrameshould admit whatever scalars the model admits. - The TypeScript SDK plan is
docs/plans/2026-11-22-recipher-typescript-sdk.md.
Activity