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). A Rust program often wraps a scalar in a type of its own, such as struct Email(String), and derives the engine's traits for it with #[derive(Encrypt, Decrypt)]. The derive comes from vitaminc, the cryptography library the engine is built on. Its crates vitaminc-aead and vitaminc-aead-derive are in this repository.
Problem
keyset.encrypt does not accept a derived newtype, and the error names a trait the developer has never seen.
use stack_encrypt::{Decrypt, Encrypt};
use zeroize::Zeroize;
#[derive(Encrypt, Decrypt, Zeroize)]
struct Email(String);
keyset.encrypt(Email("ada@example.com".into()))
// error[E0277]: the trait bound `for<'a> &'a Email: vitaminc_aead::Scalar` is not satisfiedWhy it happens:
keyset.encryptaccepts a type only when a borrow of it implements vitaminc'sScalarmarker (OpaqueEncodeinpackages/stack-encrypt/src/scalar.rs).#[derive(Encrypt)]on a newtype generatesimpl Encrypt for Emailandimpl Encrypt for &Email. Each copiesDEPTHfrom the inner type, so the compiler already knows anEmailis one scalar (DEPTHisSome(0)). The derive does not generateimpl Scalar.Scalarrepeats whatDEPTHsays. It exists because stable Rust cannot use a constant in a trait bound (packages/vitaminc-aead/src/plaintext/mod.rs).
So the developer has to write impl Scalar for &Email {} by hand. To do that they must add vitaminc-aead as a dependency, because stack-encrypt does not re-export Scalar. Nothing checks that a hand-written impl is honest: one on a struct with several fields compiles, and the encrypt then fails at run time.
Verified by compiling the example above against next. The decrypt side has the same gap, and #2011 closes it there.
Proposal
A newtype that derives Encrypt over a scalar works with keyset.encrypt with no other code:
- No second trait for the developer to implement, and no extra derive attribute.
- Nothing in the developer's code, or in the error they see for a real mistake, requires knowing vitaminc.
- A newtype over a value that is not one scalar, such as
struct Tags(Vec<String>), still derivesEncryptfor use in a record, and is still refused bykeyset.encryptat compile time. - A false claim cannot be written by hand, or is caught at compile time.
Approaches to evaluate, cheapest first:
- The derive emits the marker. For a newtype, emit
impl<'r> Scalar for &'r Email where &'r Inner: Scalar {}. The bound names a lifetime parameter, so it is checked where the impl is used, not where it is written, and a newtype over aVecstill compiles. Check whether the owned form,impl Scalar for Email where Inner: Scalar {}, is needed, and whether it compiles for a newtype over a type that is not a scalar. - An associated type on
Encryptthat states the same fact asDEPTH, with one blanket impl ofScalarover it. This changes every hand-writtenEncryptimpl. - A compile-time assertion on
DEPTHin the engine, in place of theScalarbound. The error then appears when the program is built, not when it is checked.
Also:
- When a type is refused, the error should say in Stack Encrypt's terms that
keyset.encrypttakes one value, and what to use for a collection or a record (#[diagnostic::on_unimplemented]). - Compile tests: a derived newtype over a
Stringand over an integer is accepted; a derived newtype over aVec<String>and a derived struct with named fields are refused. - Update the
OpaqueEncodedocs and the CHANGELOG.
Relationship to other work
- feat(stack-encrypt)!: encrypt and decrypt one value known only at run time, and bound the typed decrypt to a scalar #2011: the same change for decrypt, through the sink type.
Activity