Skip to content

stack-encrypt: encrypt and decrypt one value whose type is known only at run time, and bound the typed decrypt to a scalar #2009

Done

Description

@coderdan

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.

  1. 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's Scalar marker (packages/vitaminc-aead/src/plaintext/mod.rs). Scalar is a promise checked at compile time. An OwnedFrame can hold a list or a map, so it is not a Scalar, 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 claims Scalar.
    • 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.

  2. Decrypt is not bound to a scalar. OpaqueDecode is implemented for every Decrypt + Zeroize type, including Vec<String> and maps. keyset.decrypt::<Vec<String>>(ciphertext) compiles, requests a key, decrypts, and only then fails with Error::OpenedType. It can never succeed, because a ciphertext holds one scalar.

Proposal

One PR to next.

  1. Run-time single-value encrypt, in stack_encrypt::dynamic. It takes a frame and an optional declared kind (ValueKind), and returns the same kind of Ciphertext that keyset.encrypt returns 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 undefined is refused with its own reason.
    • A json kind takes the document as JSON text, checks it by the engine's JSON rules, and seals it as JsonPlaintext is sealed.
    • It has a form with a context, as encrypt_with_context does, and a batch form that makes one key request.
  2. Run-time single-value decrypt. It returns the value as an OwnedFrame that holds one scalar, with an optional kind the value must open as. A refusal never says what the decrypted value opened as. A json kind returns the document's text, as a record's json field does.
  3. A scalar bound on the typed decrypt. Add a marker trait to vitaminc-aead for a target that one scalar builds, and bound OpaqueDecode on it. It covers every target that can succeed today: the FromScalar types, Protected of one, and OwnedFrame (one leaf always builds a frame of one scalar). A Vec of anything but bytes, a map or a struct no longer compiles. A compile-fail test pins that.
  4. Use it in the TypeScript addon on next: delete OneValue, and call the run-time encrypt.
  5. 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.
  6. stack-encrypt CHANGELOG: the new API, and the decrypt bound under Breaking.

Relationship to other work

Metadata

Metadata

Assignees

Labels

RustSDKenhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions