From 65577b4f3a589766d6c27f36ab7505ebcbf578f7 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 19:07:46 +1100 Subject: [PATCH 01/14] feat(vitaminc-aead-derive): implement Scalar for a derived newtype or fieldless enum A newtype is a struct with exactly one unnamed field. A derived newtype encrypts exactly as its inner value does. A fieldless enum encrypts as one u32 leaf. But the derive implemented no Scalar marker for either. So a caller that bounds on Scalar refused `struct Email(String)`. One such caller is keyset.encrypt in stack-encrypt. The user had to write `impl Scalar for &Email {}` by hand. The derive now implements Scalar for these types, by value and by reference. Each impl for a newtype has a bound: the inner type is a Scalar. So `Tags(Vec)` and `Nickname(Option)` still derive Encrypt, for use as a field. Neither type is a Scalar. A struct with fields and a recursive newtype get no impl. The compiler checks a where clause that names no generic parameter where the impl is written. A false clause of that kind is an error. So the borrowed impl's bound names the borrow's lifetime. The owned impl's bound is inside a `for<'__s>` binder. The compiler then checks each bound only where code asks for the Scalar impl. A Scalar impl that a user wrote for such a type now conflicts with the derived impl (E0119). The CHANGELOGs tell the user to delete it. --- packages/vitaminc-aead-derive/CHANGELOG.md | 21 ++ packages/vitaminc-aead-derive/src/encrypt.rs | 200 ++++++++++++++++++ packages/vitaminc-aead-derive/src/lib.rs | 4 + packages/vitaminc-aead/CHANGELOG.md | 7 + packages/vitaminc-aead/src/plaintext/mod.rs | 26 +++ packages/vitaminc-aead/tests/scalar.rs | 43 ++++ .../derived_newtype_over_a_container.rs | 24 +++ .../derived_newtype_over_a_container.stderr | 146 +++++++++++++ 8 files changed, 471 insertions(+) create mode 100644 packages/vitaminc-aead/tests/ui/scalar/derived_newtype_over_a_container.rs create mode 100644 packages/vitaminc-aead/tests/ui/scalar/derived_newtype_over_a_container.stderr diff --git a/packages/vitaminc-aead-derive/CHANGELOG.md b/packages/vitaminc-aead-derive/CHANGELOG.md index 9867b81e5..b2c4b0097 100644 --- a/packages/vitaminc-aead-derive/CHANGELOG.md +++ b/packages/vitaminc-aead-derive/CHANGELOG.md @@ -1,3 +1,24 @@ +## [Unreleased] + +### Breaking + +- **Breaking:** `#[derive(Encrypt)]` now implements `Scalar` for a newtype and for a fieldless enum. + A `Scalar` impl you wrote for such a type no longer compiles. + The compiler reports `E0119`, conflicting implementations. + Delete your impl, because the derived impl replaces it. + +### Features + +- `#[derive(Encrypt)]` implements `Scalar`, by value and by reference, for a type that encrypts as one leaf. + A newtype is a struct with exactly one unnamed field. + A newtype is a `Scalar` when its inner type is one. + So `struct Email(String)` is a `Scalar`. + A generic `Wrapper` is a `Scalar` when `T` is one. + `struct Tags(Vec)` still derives `Encrypt`, but it is not a `Scalar`. + Neither is `struct Nickname(Option)`. + A fieldless enum is always a `Scalar`, because it is one `u32` leaf. + A struct with fields is not a `Scalar`. + A recursive newtype is not a `Scalar` either. ## [0.10.0] - 2026-11-19 diff --git a/packages/vitaminc-aead-derive/src/encrypt.rs b/packages/vitaminc-aead-derive/src/encrypt.rs index 386f343b8..a147dcfd9 100644 --- a/packages/vitaminc-aead-derive/src/encrypt.rs +++ b/packages/vitaminc-aead-derive/src/encrypt.rs @@ -7,6 +7,11 @@ //! `Vec` does its leftover elements. The impl for `&Name` walks the //! fields by reference and moves nothing; it is what a `Protected` //! walks through, so the struct stays wrapped and is zeroized whole. +//! +//! A leaf is one sealed value. A newtype or a fieldless enum is one leaf. +//! So the derive also implements the `Scalar` marker for it, by value and +//! by reference, as every built-in leaf type has. A newtype is a `Scalar` +//! only when its inner type is one. use proc_macro2::TokenStream; use quote::quote; @@ -88,6 +93,14 @@ pub(crate) fn derive(input: DeriveInput) -> Result { visit_check, } = depth(krate, &input, &shape, attrs.recursive); let (fixed, ref_fixed) = fixed(krate, &input, &shape, attrs.recursive); + let scalar = scalar( + krate, + &input, + &shape, + attrs.recursive, + &generics, + &ref_generics, + ); Ok(quote! { #[automatically_derived] @@ -118,10 +131,65 @@ pub(crate) fn derive(input: DeriveInput) -> Result { } } + #scalar + #item_check }) } +/// The `Scalar` impls for a type that encrypts as one leaf. Two kinds of +/// type do: a newtype whose inner type is a `Scalar`, and a fieldless enum. +/// A fieldless enum is always one `u32` leaf. A struct with fields is a +/// map, so it gets no impl. A recursive newtype has no fixed depth, so it +/// gets none either. +/// +/// Each impl for a newtype has a bound: the inner type is a `Scalar`. So a +/// newtype over a container, such as `Tags(Vec)`, still derives +/// `Encrypt`. The newtype is not a `Scalar`. +/// +/// The compiler checks a bound that names no generic parameter where the +/// impl is written. A false bound of that kind is an error. So the +/// borrowed impl's bound names the borrow's lifetime `'__r`. The owned +/// impl's bound is inside a `for<'__s>` binder. The compiler then checks +/// each bound only where code asks for the `Scalar` impl. +fn scalar( + krate: &syn::Path, + input: &DeriveInput, + shape: &Shape, + recursive: bool, + generics: &syn::Generics, + ref_generics: &syn::Generics, +) -> TokenStream { + let name = &input.ident; + let mut owned = generics.clone(); + let mut by_ref = ref_generics.clone(); + match shape { + Shape::Newtype(_) if recursive || names_itself(name, shape) => return TokenStream::new(), + Shape::Newtype(field) => { + let ty = &field.ty; + owned + .make_where_clause() + .predicates + .push(parse_quote!(for<'__s> #ty: #krate::Scalar)); + by_ref + .make_where_clause() + .predicates + .push(parse_quote!(&'__r #ty: #krate::Scalar)); + } + Shape::Enum(_) => {} + Shape::Map(_) | Shape::Empty => return TokenStream::new(), + } + let (impl_generics, ty_generics, where_clause) = owned.split_for_impl(); + let (ref_impl_generics, _, ref_where_clause) = by_ref.split_for_impl(); + quote! { + #[automatically_derived] + impl #impl_generics #krate::Scalar for #name #ty_generics #where_clause {} + + #[automatically_derived] + impl #ref_impl_generics #krate::Scalar for &'__r #name #ty_generics #ref_where_clause {} + } +} + /// The `DEPTH` constants for both impls, and the check that the type fits /// within `MAX_DEPTH`. struct Depth { @@ -1130,6 +1198,138 @@ mod tests { assert_contains(&out, quote!(const DEPTH)); } + /// A newtype is a `Scalar` when its inner type is one. The derive + /// implements `Scalar` by value and by reference. Each impl has the + /// inner type's `Scalar` as its bound. + /// + /// The owned bound is inside a + /// binder, and the borrowed bound names the borrow's lifetime. So the + /// compiler does not check either bound where the impl is written. A + /// newtype over a container still derives. + #[test] + fn a_newtype_is_a_scalar_when_its_inner_type_is() { + let out = expand(parse_quote!( + struct Email(String); + )); + assert_contains( + &out, + quote!(impl ::vitaminc_aead::Scalar for Email + where for<'__s> String: ::vitaminc_aead::Scalar {}), + ); + assert_contains( + &out, + quote!( + impl<'__r> ::vitaminc_aead::Scalar for &'__r Email where &'__r String: ::vitaminc_aead::Scalar {} + ), + ); + + let out = expand(parse_quote!( + struct Tags(Vec); + )); + assert_contains( + &out, + quote!(impl ::vitaminc_aead::Scalar for Tags + where for<'__s> Vec: ::vitaminc_aead::Scalar {}), + ); + assert_contains( + &out, + quote!( + impl<'__r> ::vitaminc_aead::Scalar for &'__r Tags where &'__r Vec: ::vitaminc_aead::Scalar {} + ), + ); + } + + /// A generic newtype's `Scalar` impls keep the bounds of its `Encrypt` + /// impls. Each also has the inner type's `Scalar` as a bound. + #[test] + #[rustfmt::skip] + fn a_generic_newtype_is_a_scalar_when_its_parameter_is() { + let out = expand(parse_quote!( + struct Wrapper(T); + )); + assert_contains( + &out, + quote!( + impl ::vitaminc_aead::Scalar for Wrapper where + for<'__s> T: ::vitaminc_aead::Scalar + { + } + ), + ); + assert_contains( + &out, + quote!( + impl<'__r, T> ::vitaminc_aead::Scalar for &'__r Wrapper + where + T: ::vitaminc_aead::EncryptRef, + &'__r T: ::vitaminc_aead::Scalar + { + } + ), + ); + } + + /// A fieldless enum is always one `u32` leaf. So its `Scalar` impls + /// have no bound. + #[test] + fn a_fieldless_enum_is_a_scalar() { + let out = expand(parse_quote! { + enum Status { + Active, + } + }); + assert_contains(&out, quote!(impl ::vitaminc_aead::Scalar for Status {})); + assert_contains( + &out, + quote!( + impl<'__r> ::vitaminc_aead::Scalar for &'__r Status {} + ), + ); + } + + /// A struct with fields and a struct with none each encrypt as a map. A + /// recursive newtype has no fixed depth. So none of them is a `Scalar`. + #[test] + fn a_map_or_a_recursive_type_is_not_a_scalar() { + for input in [ + parse_quote! { + struct User { + name: String, + } + }, + parse_quote!( + struct Pair(String, u32); + ), + parse_quote!( + struct Marker {} + ), + parse_quote!( + struct Wrapped(Vec); + ), + parse_quote!( + #[aead(recursive)] + struct Even(Option>); + ), + ] { + assert_lacks(&expand(input), quote!(::vitaminc_aead::Scalar)); + } + } + + /// The `Scalar` path uses the crate named by the `crate` attribute, as + /// every other path does. + #[test] + fn the_marker_follows_the_crate_attribute() { + let out = expand(parse_quote! { + #[aead(crate = "::vitaminc::aead")] + struct Email(String); + }); + assert_contains(&out, quote!(::vitaminc::aead::Scalar for Email)); + assert!( + !out.contains("vitaminc_aead"), + "expansion still references the default crate path:\n{out}" + ); + } + #[test] fn unknown_attributes_are_rejected() { let err = derive(parse_quote! { diff --git a/packages/vitaminc-aead-derive/src/lib.rs b/packages/vitaminc-aead-derive/src/lib.rs index aef62fdcf..216e65b0e 100644 --- a/packages/vitaminc-aead-derive/src/lib.rs +++ b/packages/vitaminc-aead-derive/src/lib.rs @@ -52,6 +52,8 @@ //! - A **newtype** struct (exactly one unnamed field) is *transparent*: it //! encrypts and decrypts exactly as its inner type, adding nothing to the //! ciphertext. This mirrors serde, and matches how `Protected` behaves. +//! A newtype is also a `Scalar`, one leaf, when its inner type is one. +//! So `Email(String)` is a `Scalar`. `Tags(Vec)` is not. //! - A unit struct — or a struct with no fields — encrypts to the //! authenticated empty-map marker. //! @@ -69,6 +71,8 @@ //! refused. Pin the discriminants with `= n` before storing anything, since //! reordering variants otherwise changes them. Variants take no //! `#[aead(...)]` attributes. +//! A fieldless enum is a `Scalar`, by value and by reference, because it +//! is one leaf. //! //! Enums with fields are not supported. Their payload has no agreed layout //! (internally or externally tagged, per-variant AAD), so model the choice diff --git a/packages/vitaminc-aead/CHANGELOG.md b/packages/vitaminc-aead/CHANGELOG.md index 7549ee1f9..348e7669b 100644 --- a/packages/vitaminc-aead/CHANGELOG.md +++ b/packages/vitaminc-aead/CHANGELOG.md @@ -41,6 +41,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). Code that names `FrameWriter<'_>` is unchanged. The decrypt sink of `OwnedFrame` writes through them, reporting `DecodeError`. - `DecodeError` implements `From`: a scalar its logical type does not admit is `DecodeError::Expected`, a source's failure is `DecodeError::Source`, and any other refusal is `DecodeError::Source` with the writer's error as its source. +- `#[derive(Encrypt)]` implements `Scalar`, by value and by reference, for two kinds of type. + The first is a newtype whose inner type is a `Scalar`. + The second is a fieldless enum. + So `struct Email(String)` needs no `Scalar` impl of its own. + A `Protected` is a `Scalar` too. + `struct Tags(Vec)` still derives `Encrypt`, but it is not a `Scalar`. + A `Scalar` impl you wrote for such a type now conflicts with the derived impl, so delete it. ## [0.10.0] - 2026-11-19 diff --git a/packages/vitaminc-aead/src/plaintext/mod.rs b/packages/vitaminc-aead/src/plaintext/mod.rs index 90b77871b..d70900edb 100644 --- a/packages/vitaminc-aead/src/plaintext/mod.rs +++ b/packages/vitaminc-aead/src/plaintext/mod.rs @@ -216,6 +216,32 @@ impl Encrypt for &Lent<'_, T> { /// that bounds on `Scalar` asks for a value that is present, and decides /// for itself how to seal an absent one. /// +/// `#[derive(Encrypt)]` implements `Scalar`, by value and by reference, for +/// a derived type that encrypts as one leaf. So you write no impl for such +/// a type. +/// +/// - A newtype is a struct with exactly one unnamed field. A newtype is a +/// `Scalar` when its inner type is one. So `struct Email(String)` is a +/// `Scalar`. +/// - `struct Tags(Vec)` is not a `Scalar`. Nor is +/// `struct Nickname(Option)`. Both still derive `Encrypt`. +/// - A fieldless enum is always a `Scalar`, because it is one `u32` leaf. +/// - A struct with fields is a map, so it is never a `Scalar`. +/// +/// ``` +/// use vitaminc_aead::{Encrypt, Scalar}; +/// +/// #[derive(Encrypt)] +/// struct Email(String); +/// +/// fn seals_one_leaf(value: S) -> S { +/// value +/// } +/// +/// seals_one_leaf(Email("ada@example.com".into())); +/// seals_one_leaf(&Email("ada@example.com".into())); +/// ``` +/// /// `DEPTH` says the same thing, but stable Rust cannot bound a generic on /// a constant, so a caller that must produce one sealed leaf from any source /// bounds on this trait instead. The trait is open: a hand-written leaf, diff --git a/packages/vitaminc-aead/tests/scalar.rs b/packages/vitaminc-aead/tests/scalar.rs index e7c1e2e22..8f2a0a128 100644 --- a/packages/vitaminc-aead/tests/scalar.rs +++ b/packages/vitaminc-aead/tests/scalar.rs @@ -108,6 +108,49 @@ fn an_empty_text_or_byte_value_is_still_one_leaf() { one_leaf(Protected::new([0u8; 0]).as_protected_ref()); } +/// A derived newtype encrypts as its inner value does. So the derive +/// implements `Scalar` for it, by value and by reference, when the inner +/// type is a `Scalar`. A fieldless enum is one `u32` leaf, so the derive +/// implements `Scalar` for it too. These types have no `Scalar` impl +/// written by hand. +#[derive(Encrypt, zeroize::Zeroize)] +struct Email(String); + +#[derive(Encrypt, zeroize::Zeroize)] +struct Contact(Email); + +#[derive(Encrypt)] +struct Wrapper(T); + +#[derive(Encrypt)] +struct Name<'a>(&'a str); + +#[derive(Encrypt)] +enum Status { + Active, +} + +const _: () = assert!(depth::is_scalar(::DEPTH)); +const _: () = assert!(depth::is_scalar(<&Email as Encrypt>::DEPTH)); +const _: () = assert!(depth::is_scalar( as Encrypt>::DEPTH)); +const _: () = assert!(depth::is_scalar(<&Wrapper as Encrypt>::DEPTH)); + +#[test] +fn a_derived_newtype_over_a_scalar_is_a_scalar() { + one_leaf(Email("ada@example.com".into())); + one_leaf(&Email("ada@example.com".into())); + one_leaf(Contact(Email("ada@example.com".into()))); + one_leaf(&Contact(Email("ada@example.com".into()))); + one_leaf(Wrapper(42i64)); + one_leaf(&Wrapper(vec![1u8, 2])); + one_leaf(Name("ada")); + one_leaf(&Name("ada")); + one_leaf(Protected::new(Email("ada@example.com".into()))); + one_leaf(&Protected::new(Email("ada@example.com".into()))); + one_leaf(Status::Active); + one_leaf(&Status::Active); +} + #[test] fn a_borrow_of_a_scalar_borrow_is_a_scalar() { one_leaf(&&42i32); diff --git a/packages/vitaminc-aead/tests/ui/scalar/derived_newtype_over_a_container.rs b/packages/vitaminc-aead/tests/ui/scalar/derived_newtype_over_a_container.rs new file mode 100644 index 000000000..876c01ac9 --- /dev/null +++ b/packages/vitaminc-aead/tests/ui/scalar/derived_newtype_over_a_container.rs @@ -0,0 +1,24 @@ +//! A derived newtype is a `Scalar` only when its inner type is one. A +//! newtype over a `Vec` encrypts as a sequence. A newtype over an `Option` +//! can be absent. So neither newtype is a `Scalar`, by value or by +//! reference. Both still derive `Encrypt`. + +use vitaminc_aead::{Encrypt, Scalar}; + +#[derive(Encrypt)] +struct Tags(Vec); + +#[derive(Encrypt)] +struct Nickname(Option); + +#[derive(Encrypt)] +struct Wrapper(T); + +fn seals_one_leaf(_: S) {} + +fn main() { + seals_one_leaf(Tags(vec![])); + seals_one_leaf(&Tags(vec![])); + seals_one_leaf(Nickname(None)); + seals_one_leaf(Wrapper(vec![1i32])); +} diff --git a/packages/vitaminc-aead/tests/ui/scalar/derived_newtype_over_a_container.stderr b/packages/vitaminc-aead/tests/ui/scalar/derived_newtype_over_a_container.stderr new file mode 100644 index 000000000..4cefd5a90 --- /dev/null +++ b/packages/vitaminc-aead/tests/ui/scalar/derived_newtype_over_a_container.stderr @@ -0,0 +1,146 @@ +error[E0277]: the trait bound `Vec: vitaminc_aead::Scalar` is not satisfied + --> tests/ui/scalar/derived_newtype_over_a_container.rs:20:20 + | +20 | seals_one_leaf(Tags(vec![])); + | -------------- ^^^^^^^^^^^^ the trait `vitaminc_aead::Scalar` is not implemented for `Vec` + | | + | required by a bound introduced by this call + | +help: the following other types implement trait `vitaminc_aead::Scalar` + --> src/plaintext/sources.rs + | + | impl Scalar for $ty {} + | ^^^^^^^^^^^^^^^^^^^ + | | + | `&Vec` + | `Vec` +... + | / scalar! { + | | bool, &bool, f32, &f32, f64, &f64, + | | i8, &i8, i16, &i16, i32, &i32, i64, &i64, i128, &i128, + | | u16, &u16, u32, &u32, u64, &u64, u128, &u128, +... | + | | Bytes<'_>, &Bytes<'_>, Raw<'_>, &Raw<'_>, + | | } + | |_- in this macro invocation +note: required for `Tags` to implement `vitaminc_aead::Scalar` + --> tests/ui/scalar/derived_newtype_over_a_container.rs:8:10 + | + 8 | #[derive(Encrypt)] + | ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro + 9 | struct Tags(Vec); + | ^^^^ +note: required by a bound in `seals_one_leaf` + --> tests/ui/scalar/derived_newtype_over_a_container.rs:17:22 + | +17 | fn seals_one_leaf(_: S) {} + | ^^^^^^ required by this bound in `seals_one_leaf` + = note: this error originates in the macro `scalar` which comes from the expansion of the derive macro `Encrypt` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `&Vec: vitaminc_aead::Scalar` is not satisfied + --> tests/ui/scalar/derived_newtype_over_a_container.rs:21:21 + | +21 | seals_one_leaf(&Tags(vec![])); + | -------------- ^^^^^^^^^^^^ the trait `vitaminc_aead::Scalar` is not implemented for `&Vec` + | | + | required by a bound introduced by this call + | +help: the following other types implement trait `vitaminc_aead::Scalar` + --> src/plaintext/sources.rs + | + | impl Scalar for $ty {} + | ^^^^^^^^^^^^^^^^^^^ + | | + | `&Vec` + | `Vec` +... + | / scalar! { + | | bool, &bool, f32, &f32, f64, &f64, + | | i8, &i8, i16, &i16, i32, &i32, i64, &i64, i128, &i128, + | | u16, &u16, u32, &u32, u64, &u64, u128, &u128, +... | + | | Bytes<'_>, &Bytes<'_>, Raw<'_>, &Raw<'_>, + | | } + | |_- in this macro invocation +note: required for `&Tags` to implement `vitaminc_aead::Scalar` + --> tests/ui/scalar/derived_newtype_over_a_container.rs:8:10 + | + 8 | #[derive(Encrypt)] + | ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro +note: required by a bound in `seals_one_leaf` + --> tests/ui/scalar/derived_newtype_over_a_container.rs:17:22 + | +17 | fn seals_one_leaf(_: S) {} + | ^^^^^^ required by this bound in `seals_one_leaf` + = note: this error originates in the macro `scalar` which comes from the expansion of the derive macro `Encrypt` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `Option: vitaminc_aead::Scalar` is not satisfied + --> tests/ui/scalar/derived_newtype_over_a_container.rs:22:20 + | +22 | seals_one_leaf(Nickname(None)); + | -------------- ^^^^^^^^^^^^^^ the trait `vitaminc_aead::Scalar` is not implemented for `Option` + | | + | required by a bound introduced by this call + | + = help: the following other types implement trait `vitaminc_aead::Scalar`: + &&'b T + &'__r Nickname + &'__r Tags + &'__r Wrapper + &'a vitaminc_protected::protected::Protected + &Raw<'_> + &Vec + &[u8; N] + and $N others +note: required for `Nickname` to implement `vitaminc_aead::Scalar` + --> tests/ui/scalar/derived_newtype_over_a_container.rs:11:10 + | +11 | #[derive(Encrypt)] + | ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro +12 | struct Nickname(Option); + | ^^^^^^^^ +note: required by a bound in `seals_one_leaf` + --> tests/ui/scalar/derived_newtype_over_a_container.rs:17:22 + | +17 | fn seals_one_leaf(_: S) {} + | ^^^^^^ required by this bound in `seals_one_leaf` + = note: this error originates in the derive macro `Encrypt` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `Vec: vitaminc_aead::Scalar` is not satisfied + --> tests/ui/scalar/derived_newtype_over_a_container.rs:23:28 + | +23 | seals_one_leaf(Wrapper(vec![1i32])); + | -------------- ^^^^^^^^^^ the trait `vitaminc_aead::Scalar` is not implemented for `Vec` + | | + | required by a bound introduced by this call + | +help: the following other types implement trait `vitaminc_aead::Scalar` + --> src/plaintext/sources.rs + | + | impl Scalar for $ty {} + | ^^^^^^^^^^^^^^^^^^^ + | | + | `&Vec` + | `Vec` +... + | / scalar! { + | | bool, &bool, f32, &f32, f64, &f64, + | | i8, &i8, i16, &i16, i32, &i32, i64, &i64, i128, &i128, + | | u16, &u16, u32, &u32, u64, &u64, u128, &u128, +... | + | | Bytes<'_>, &Bytes<'_>, Raw<'_>, &Raw<'_>, + | | } + | |_- in this macro invocation +note: required for `Wrapper>` to implement `vitaminc_aead::Scalar` + --> tests/ui/scalar/derived_newtype_over_a_container.rs:14:10 + | +14 | #[derive(Encrypt)] + | ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro +15 | struct Wrapper(T); + | ^^^^^^^^^^ +note: required by a bound in `seals_one_leaf` + --> tests/ui/scalar/derived_newtype_over_a_container.rs:17:22 + | +17 | fn seals_one_leaf(_: S) {} + | ^^^^^^ required by this bound in `seals_one_leaf` + = note: this error originates in the macro `scalar` which comes from the expansion of the derive macro `Encrypt` (in Nightly builds, run with -Z macro-backtrace for more info) From fdea34f0bf2b6c69663ae4e0bbacb7417716edb3 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 19:07:47 +1100 Subject: [PATCH 02/14] fix(stack-encrypt): encrypt a derived newtype, and say what to use for a refused type The derive now implements Scalar for a newtype over one value. So `keyset.encrypt(Email(..))` compiles with no other code. The newtype's leaf has the same plaintext and length as the leaf of its inner String. A new test opens both leaves with the stored data keys to show this. The test also checks that `keyset.decrypt::` opens what Email encrypted. For a refused type, the error used to name the bound `for<'a> &'a T: vitaminc_aead::Scalar`. A Stack Encrypt user does not know that trait. OpaqueEncode now has an on_unimplemented message. The message says that the value cannot be encrypted into one Ciphertext, and what keyset.encrypt encrypts. The message also names what to use instead: EncryptInto, encrypt_tree or OpaqueBlob. The blanket impl has `#[diagnostic::do_not_recommend]`. So the compiler reports the refusal against OpaqueEncode, not against the Scalar or Zeroize bound below it. Without the attribute, the compiler shows the message of the bound instead. Anyone can implement Scalar. So seal now checks, in an inline const, that the DEPTH constant of the type.s borrow is depth::SCALAR. A false Scalar impl fails `cargo build` with E0080. `cargo check` does not run the check. A compile_fail doctest tests it. The existing compile-fail snapshots change only their wording. Each refusal is still E0277 at the same place in the source. --- packages/stack-encrypt/CHANGELOG.md | 36 +++ packages/stack-encrypt/src/scalar.rs | 114 +++++++- .../stack-encrypt/tests/derived_newtype.rs | 248 ++++++++++++++++++ .../opaque_blob_requires_the_wrapper.stderr | 33 +-- .../ui/opaque_blob_requires_zeroize.stderr | 11 +- .../tests/ui/owned_collection_elements.stderr | 87 +++--- .../tests/ui/pass/derived_newtype_scalar.rs | 52 ++++ .../ui/scalar_collections_and_borrows.stderr | 71 +++-- .../tests/ui/scalar_derived_not_one_value.rs | 29 ++ .../ui/scalar_derived_not_one_value.stderr | 83 ++++++ .../tests/ui/scalar_nullable.stderr | 62 +++-- 11 files changed, 688 insertions(+), 138 deletions(-) create mode 100644 packages/stack-encrypt/tests/derived_newtype.rs create mode 100644 packages/stack-encrypt/tests/ui/pass/derived_newtype_scalar.rs create mode 100644 packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.rs create mode 100644 packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr diff --git a/packages/stack-encrypt/CHANGELOG.md b/packages/stack-encrypt/CHANGELOG.md index 399b28ef7..ee9af813b 100644 --- a/packages/stack-encrypt/CHANGELOG.md +++ b/packages/stack-encrypt/CHANGELOG.md @@ -1278,6 +1278,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 it needs a `_` arm. To migrate code that names `recipher`, see the stack-kms CHANGELOG. +- **Delete a `Scalar` impl you wrote for a derived newtype or a fieldless + enum** (#2012). `#[derive(Encrypt)]` now implements Vitamin C's `Scalar` + marker for these types. So an `impl Scalar for &Email {}` of your own no + longer compiles. The compiler reports `E0119`, conflicting + implementations. After you delete the impl, `keyset.encrypt` still + encrypts the type. + ### Added - **A declaration and a call show the context they use, before any key is @@ -1746,6 +1753,35 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **`keyset.encrypt` encrypts a newtype that derives `Encrypt` over one + value** (#2012). A newtype is a struct with exactly one unnamed field. + `struct Email(String)` with `#[derive(Encrypt, Zeroize)]` now encrypts + with `keyset.encrypt(Email(..))`, with no other code. Before this + change, the newtype needed a hand-written `impl Scalar for &Email {}`. + That impl needed a dependency on the `Scalar` trait of `vitaminc-aead`. + - The newtype encrypts exactly as its inner value does. The leaf has the + same plaintext and the same length. So `keyset.decrypt::` opens + what `Email` encrypted. + - A newtype over a type that contains more than one value still derives + `Encrypt`, for use as a field. Examples are `struct Tags(Vec)` + and `struct Nickname(Option)`. `keyset.encrypt` still refuses + such a newtype when the program compiles. `keyset.encrypt` also + refuses a struct with named fields. + - `keyset.encrypt` also encrypts a fieldless enum that derives + `Encrypt`, as one `u32` value. + - The compile error for a refused type now says that the type cannot be + encrypted into one `Ciphertext`. The error says what `keyset.encrypt` + encrypts, and what to use instead: + - `EncryptInto`, for each element of a `Vec` + - `keyset.encrypt_tree`, for an `Option` or a struct + - `OpaqueBlob`, for a whole `Serialize` value + + The error no longer names the `Scalar` or `Zeroize` bound that the type + did not meet. + - `keyset.encrypt` now checks, when the program is built, a `Scalar` + impl written by hand. If the type's borrow encrypts as a container, + the build fails with `E0080`. `cargo check` does not run this check. + - **A query on an EQL type whose twin carries every term is refused for what any of its indexes refuses, before any key is requested** (#1668). `eql_v4.query_text_ord_ore` requires its `hm` and its `ob` together, so diff --git a/packages/stack-encrypt/src/scalar.rs b/packages/stack-encrypt/src/scalar.rs index 553be5448..d64f2b7be 100644 --- a/packages/stack-encrypt/src/scalar.rs +++ b/packages/stack-encrypt/src/scalar.rs @@ -53,8 +53,51 @@ pub(crate) mod sealed { /// - [`JsonPlaintext`](crate::json::JsonPlaintext), a JSON document, which /// is sealed as its JSON text /// -/// A type of your own meets them if its borrow implements `Scalar` and the -/// type implements `Zeroize`. +/// A newtype is a struct with exactly one unnamed field, such as +/// `struct Email(String)`. A newtype that derives +/// [`Encrypt`](crate::Encrypt) over one of the types above meets both +/// conditions once it implements `Zeroize`. You write no other code: the +/// derive marks the newtype `Scalar` when its inner type is a `Scalar`. +/// +/// The newtype encrypts exactly as its inner value does. So +/// `keyset.decrypt::` opens what an `Email` encrypted. The derive +/// comes from Vitamin C, so your crate needs `vitaminc-aead` as a +/// dependency to use it. +/// +/// ``` +/// # async fn example() -> Result<(), stack_encrypt::Error> { +/// use stack_encrypt::{Encrypt, StackCipher}; +/// use zeroize::Zeroize; +/// +/// #[derive(Encrypt, Zeroize)] +/// struct Email(String); +/// +/// // With no `.kms(..)`, the cipher uses ZeroKMS. It reads credentials from +/// // the environment, or from the profile that `npx stash auth login` writes. +/// let cipher = StackCipher::builder() +/// # .kms(stack_encrypt::kms::FakeDataKeySource::new()) +/// .init() +/// .await?; +/// let keyset = cipher.default_keyset(); +/// let stored = keyset.encrypt(Email("ada@example.com".into())).await?; +/// let email: String = keyset.decrypt(stored).await?; +/// assert_eq!(email, "ada@example.com"); +/// # Ok(()) +/// # } +/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); +/// ``` +/// +/// A newtype over a type that contains more than one value, such as +/// `struct Tags(Vec)`, still derives `Encrypt`. So it can be a field +/// of a struct. `keyset.encrypt` refuses it, as it refuses a `Vec`. +/// +/// You can also write the `Encrypt` impl of a type yourself. Such a type is +/// an `OpaqueEncode` type when you implement `Scalar` for its borrow. +/// +/// A `Scalar` impl states that the borrow encrypts as one value. So the +/// borrow's [`DEPTH`](crate::Encrypt::DEPTH) must be Vitamin C's +/// `depth::SCALAR`. `keyset.encrypt` checks this when your program is +/// built. A type with a false `Scalar` impl fails the build. /// /// [`OpaqueBlob`](crate::OpaqueBlob) is an `OpaqueEncode` type too. It /// encodes a whole `Serialize` value as one storage blob first. @@ -107,6 +150,20 @@ pub(crate) mod sealed { /// # } /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(example()).unwrap(); /// ``` +#[diagnostic::on_unimplemented( + message = "`{Self}` cannot be encrypted into one `Ciphertext`", + label = "`keyset.encrypt` encrypts one owned value", + note = "one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than \ + `u8`. A newtype that derives `Encrypt` over one of these is one value too.", + note = "a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`.", + note = "the type must implement `Zeroize`, so that the value can be wiped after it is \ + encrypted.", + note = "to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`.", + note = "to encrypt an `Option`, or a struct that derives `Encrypt`, use \ + `keyset.encrypt_tree`. The result is a tree of ciphertexts.", + note = "to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in \ + `OpaqueBlob`." +)] pub trait OpaqueEncode: sealed::Encode + Zeroize + Send + 'static {} /// A plaintext type that one opaque [`Ciphertext`] decrypts to. @@ -131,6 +188,47 @@ pub trait OpaqueDecode: sealed::Decode + Zeroize + Send + 'static {} // scalars, so the borrow is walked as it is: nothing is copied out of the // caller's value, and the fixed width of the type is the borrow's own, which // is what pads an absent value to a present one's length. +// +// Anyone can implement `Scalar`. The compiler does not compare an impl with +// the type's `DEPTH`. So `seal` checks that the borrow's `DEPTH` is +// `depth::SCALAR`. +// +// The check is an inline `const`. The compiler evaluates +// it when it builds `seal` for a concrete `S`. So a false `Scalar` impl +// fails the build, not a call. + +/// A false `Scalar` impl fails the build. A false impl is one on a type +/// whose borrow is not one value. `cargo build` runs the check, and +/// `cargo check` does not. +/// +/// ```compile_fail,E0080 +/// use stack_encrypt::kms::FakeDataKeySource; +/// use stack_encrypt::{Encrypt, KeysetCipher}; +/// use vitaminc_aead::plaintext::{depth, Sink}; +/// use vitaminc_aead::Scalar; +/// use zeroize::Zeroize; +/// +/// // The borrow of `Profile` encrypts as a map, but it is marked `Scalar`. +/// #[derive(Zeroize)] +/// struct Profile(String); +/// +/// impl Encrypt for &Profile { +/// const DEPTH: Option = depth::container(depth::SCALAR); +/// fn visit(self, _sink: S) -> Result { +/// todo!() +/// } +/// } +/// impl Scalar for &Profile {} +/// +/// fn encrypt(keyset: &KeysetCipher<'_, FakeDataKeySource>) { +/// let _ = keyset.encrypt(Profile("ada".into())); +/// } +/// +/// fn main() { +/// let encrypt: fn(&KeysetCipher<'_, FakeDataKeySource>) = encrypt; +/// let _ = encrypt; +/// } +/// ``` impl sealed::Encode for S where S: Zeroize + Send + 'static, @@ -141,9 +239,21 @@ where cipher: &'a KeysetCipher<'_, K>, context: ContextPiece<'static>, ) -> Pending<'a, Ciphertext, K> { + const { + assert!( + vitaminc_aead::plaintext::depth::is_scalar(<&'static S as crate::Encrypt>::DEPTH), + "a type marked `Scalar` must encrypt as one value. The `DEPTH` of this \ + type's borrow is not `depth::SCALAR`.", + ) + }; crate::target::core::encrypt_opaque(self, cipher, context).try_map(Ciphertext::from_tree) } } +// `do_not_recommend` makes the compiler report a refused type against +// `OpaqueEncode`. The message of `OpaqueEncode` says what to use instead. +// Without the attribute, the compiler reports the `Scalar` or `Zeroize` +// bound below. +#[diagnostic::do_not_recommend] impl OpaqueEncode for S where S: Zeroize + Send + 'static, diff --git a/packages/stack-encrypt/tests/derived_newtype.rs b/packages/stack-encrypt/tests/derived_newtype.rs new file mode 100644 index 000000000..f87021160 --- /dev/null +++ b/packages/stack-encrypt/tests/derived_newtype.rs @@ -0,0 +1,248 @@ +//! `keyset.encrypt` encrypts a newtype that derives `Encrypt` exactly as it +//! encrypts the newtype's inner value. +//! +//! A leaf is one sealed value. Each leaf has a random nonce, a number used +//! once. So two ciphertexts of one value never have the same bytes. These +//! tests compare what the nonce does not change: +//! +//! - the plaintext of each leaf, opened with the data key that the key +//! source generated for it +//! - the length of the stored bytes +//! +//! The tests also check that each ciphertext decrypts into the inner type. + +mod common; + +use std::borrow::Cow; +use std::cell::RefCell; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; + +use common::{hex, DeterministicSource}; +use stack_encrypt::{CipherText, Ciphertext, Encrypt, SealedValue, StackCipher}; +use stack_kms::{ + DataKey, DataKeySource, DataKeyWithTag, GenerateKeyPayload, IdentifiedBy, IndexKey, + IndexKeySource, RetrieveKeyPayload, UnverifiedContext, +}; +use uuid::Uuid; +use vitaminc_aead::seal::Cipher; +use vitaminc_aead::{Context, FrameWriter, IntoContext, LocalCipherText, Unspecified}; +use vitaminc_encrypt::{Aes256Cipher, Key as AesKey}; +use vitaminc_protected::{Buffer, Controlled as _, Protected}; +use zeroize::Zeroize; + +#[derive(Encrypt, Zeroize)] +struct Email(String); + +#[derive(Encrypt, Zeroize)] +struct Age(i64); + +const SEED: [u8; 32] = *b"stack-encrypt derived newtype v1"; + +/// The data keys that a [`KeptKeys`] source generated, by initialisation +/// vector (IV). +type Kept = Mutex, [u8; 32]>>; + +/// A [`DeterministicSource`] that also stores each data key it generates, +/// by its IV. A test uses the stored keys to open leaves without the engine. +struct KeptKeys { + inner: DeterministicSource, + keys: Arc, +} + +impl stack_kms::KeySourceKind for KeptKeys { + type Kind = stack_kms::OwnIdentity; +} + +impl DataKeySource for KeptKeys { + async fn generate_keys( + &self, + payloads: Vec>, + keyset_id: Option, + unverified_context: Option>, + ) -> Result, stack_kms::Error> { + let keys = self + .inner + .generate_keys(payloads, keyset_id, unverified_context) + .await?; + let mut kept = self.keys.lock().unwrap(); + for key in &keys { + let _ = kept.insert(key.key.iv.to_vec(), *key.key.key()); + } + Ok(keys) + } + + async fn retrieve_keys( + &self, + payloads: Vec>, + keyset_id: Option, + unverified_context: Option<&UnverifiedContext>, + ) -> Result, stack_kms::Error> { + self.inner + .retrieve_keys(payloads, keyset_id, unverified_context) + .await + } +} + +impl IndexKeySource for KeptKeys { + async fn load_index_key( + &self, + keyset_id: Option, + ) -> Result<(Uuid, IndexKey), stack_kms::Error> { + self.inner.load_index_key(keyset_id).await + } +} + +/// A cipher over a [`KeptKeys`] source, and the keys that the source stores. +async fn kept_cipher() -> (StackCipher, Arc) { + let keys = Arc::default(); + let cipher = StackCipher::builder() + .kms(KeptKeys { + inner: DeterministicSource::new(SEED), + keys: Arc::clone(&keys), + }) + .init() + .await + .expect("build cipher"); + (cipher, keys) +} + +/// A Vitamin C cipher that opens a stored leaf with the data key stored for +/// it, and records each plaintext it opens. +/// +/// The cipher opens the leaf under the associated data (AAD) that the +/// engine seals a leaf under. That AAD is a pre-authentication encoding +/// (PAE) of five parts: +/// +/// - the label `stack-encrypt/leaf` +/// - the format version +/// - the keyset +/// - the AAD of the leaf's place in the value +/// - the key tag +struct Reveal<'k> { + keys: &'k HashMap, [u8; 32]>, + opened: RefCell>, +} + +impl Cipher for Reveal<'_> { + type Leaf = SealedValue; + type Error = Unspecified; + + fn seal(&self, _: Protected>, _: &Context<'_>) -> Result { + Err(Unspecified) + } + + fn open( + &self, + leaf: SealedValue, + aad: &Context<'_>, + ) -> Result>, Unspecified> { + let (keyset_id, iv, tag, ciphertext) = leaf.into_parts(); + let key = self.keys.get(iv.as_slice()).ok_or(Unspecified)?; + let aad = Context::pae(&[ + b"stack-encrypt/leaf", + &[SealedValue::FORMAT_VERSION], + keyset_id.as_bytes(), + aad.as_bytes(), + &tag, + ]); + let cipher = Aes256Cipher::new(&AesKey::from(*key))?; + let plaintext = cipher.open(LocalCipherText::from(ciphertext), &aad)?; + self.opened.borrow_mut().push(hex(plaintext.risky_ref())); + Ok(plaintext) + } +} + +/// The plaintext of `ciphertext`, as hex, opened under `context`. +fn reveal<'c>(keys: &Kept, ciphertext: Ciphertext, context: impl IntoContext<'c>) -> String { + let keys = keys.lock().unwrap(); + let reveal = Reveal { + keys: &keys, + opened: RefCell::default(), + }; + let mut out = Buffer::default(); + vitaminc_aead::decrypt_into( + &reveal, + CipherText::Single(SealedValue::from(ciphertext)), + context.into_context(), + FrameWriter::new(&mut out), + ) + .unwrap_or_else(|_| { + panic!( + "the leaf must open with its stored data key. If every test fails here, \ + the AAD that `Reveal` builds differs from `leaf_aad` in src/cipher.rs" + ) + }); + let mut opened = reveal.opened.into_inner(); + assert_eq!(opened.len(), 1, "a `Ciphertext` holds one leaf"); + opened.remove(0) +} + +#[tokio::test] +async fn a_newtype_over_a_string_seals_the_leaf_a_string_seals() { + let (cipher, keys) = kept_cipher().await; + let keyset = cipher.default_keyset(); + + let wrapped = keyset + .encrypt(Email("ada@example.com".into())) + .await + .expect("a newtype over a string encrypts"); + let bare = keyset + .encrypt(String::from("ada@example.com")) + .await + .expect("a string encrypts"); + + assert_eq!( + wrapped.to_bytes().len(), + bare.to_bytes().len(), + "the newtype's ciphertext is as long as its inner value's" + ); + let opened: String = keyset + .decrypt(wrapped.clone()) + .await + .expect("the newtype's ciphertext decrypts into its inner type"); + assert_eq!( + opened, "ada@example.com", + "the newtype's ciphertext decrypts to its inner value" + ); + assert_eq!( + reveal(&keys, wrapped, ()), + reveal(&keys, bare, ()), + "the newtype's leaf holds the plaintext its inner value's leaf holds" + ); +} + +#[tokio::test] +async fn a_newtype_over_an_integer_seals_the_leaf_an_integer_seals() { + let (cipher, keys) = kept_cipher().await; + let keyset = cipher.default_keyset(); + let context = ("users", "age"); + + let wrapped = keyset + .encrypt_with_context(Age(37), context) + .await + .expect("a newtype over an integer encrypts"); + let bare = keyset + .encrypt_with_context(37i64, context) + .await + .expect("an integer encrypts"); + + assert_eq!( + wrapped.to_bytes().len(), + bare.to_bytes().len(), + "the newtype's ciphertext is as long as its inner value's" + ); + let opened: i64 = keyset + .decrypt_with_context(wrapped.clone(), context) + .await + .expect("the newtype's ciphertext decrypts into its inner type"); + assert_eq!( + opened, 37, + "the newtype's ciphertext decrypts to its inner value" + ); + assert_eq!( + reveal(&keys, wrapped, context), + reveal(&keys, bare, context), + "the newtype's leaf holds the plaintext its inner value's leaf holds" + ); +} diff --git a/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr b/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr index 580eb4a3d..2de8711da 100644 --- a/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr +++ b/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr @@ -1,22 +1,25 @@ -error[E0277]: the trait bound `for<'a> &'a Profile: Scalar` is not satisfied +error[E0277]: `Profile` cannot be encrypted into one `Ciphertext` --> tests/ui/opaque_blob_requires_the_wrapper.rs:16:35 | 16 | let _refused = keyset.encrypt(profile).await.unwrap(); - | ------- ^^^^^^^ the trait `for<'a> Scalar` is not implemented for `&'a Profile` - | | - | required by a bound introduced by this call + | ^^^^^^^ `keyset.encrypt` encrypts one owned value | - = help: the following other types implement trait `Scalar`: - &&'b T - &'a Protected - &JsonPlaintext - &ProtectedRef<'a, T> - &Raw<'_> - &Vec - &[u8; N] - &[u8] - and $N others - = note: required for `Profile` to implement `OpaqueEncode` +help: the trait `OpaqueEncode` is not implemented for `Profile` + --> tests/ui/opaque_blob_requires_the_wrapper.rs:10:1 + | +10 | struct Profile { + | ^^^^^^^^^^^^^^ + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. +help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` + --> src/blob.rs + | + | impl OpaqueEncode for OpaqueBlob {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | diff --git a/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr b/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr index ad432f03d..84205cb52 100644 --- a/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr +++ b/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr @@ -54,14 +54,21 @@ note: required by a bound in `OpaqueBlob` | pub struct OpaqueBlob(Zeroizing); | ^^^^^^^ required by this bound in `OpaqueBlob` -error[E0277]: the trait bound `OpaqueBlob: OpaqueEncode` is not satisfied +error[E0277]: `OpaqueBlob` cannot be encrypted into one `Ciphertext` --> tests/ui/opaque_blob_requires_zeroize.rs:14:35 | 14 | let _refused = keyset.encrypt(OpaqueBlob::new(value)).await.unwrap(); - | ------- ^^^^^^^^^^^^^^^^^^^^^^ the trait `OpaqueEncode` is not implemented for `OpaqueBlob` + | ------- ^^^^^^^^^^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value | | | required by a bound introduced by this call | + = help: the trait `OpaqueEncode` is not implemented for `OpaqueBlob` + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | diff --git a/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr b/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr index 32a9624ab..9801200d1 100644 --- a/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr +++ b/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr @@ -1,41 +1,43 @@ -error[E0277]: the trait bound `for<'a> &'a u8: Scalar` is not satisfied +error[E0277]: `u8` cannot be encrypted into one `Ciphertext` --> tests/ui/owned_collection_elements.rs:11:26 | 11 | let _ = vec![1u8, 2].encrypt_into::, _>(keyset); - | ^^^^^^^^^^^^ the trait `for<'a> Scalar` is not implemented for `&'a u8` + | ^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value | - = help: the following other types implement trait `Scalar`: - &f32 - &f64 - &i128 - &i16 - &i32 - &i64 - &i8 - &u128 - and $N others - = note: required for `u8` to implement `OpaqueEncode` + = help: the trait `OpaqueEncode` is not implemented for `u8` + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. +help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` + --> src/blob.rs + | + | impl OpaqueEncode for OpaqueBlob {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: required for `stack_encrypt::Ciphertext` to implement `EncryptTarget` = note: 1 redundant requirement hidden = note: required for `Vec` to implement `EncryptTarget>` -error[E0277]: the trait bound `for<'a> &'a Option: Scalar` is not satisfied +error[E0277]: `Option` cannot be encrypted into one `Ciphertext` --> tests/ui/owned_collection_elements.rs:12:36 | 12 | let _ = vec![Some(1i32), None].encrypt_into::, _>(keyset); - | ^^^^^^^^^^^^ the trait `for<'a> Scalar` is not implemented for `&'a Option` + | ^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value | - = help: the following other types implement trait `Scalar`: - &&'b T - &'a Protected - &JsonPlaintext - &ProtectedRef<'a, T> - &Raw<'_> - &Vec - &[u8; N] - &[u8] - and $N others - = note: required for `Option` to implement `OpaqueEncode` + = help: the trait `OpaqueEncode` is not implemented for `Option` + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. +help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` + --> src/blob.rs + | + | impl OpaqueEncode for OpaqueBlob {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: required for `stack_encrypt::Ciphertext` to implement `EncryptTarget>` = note: 1 redundant requirement hidden = note: required for `Vec` to implement `EncryptTarget>>` @@ -59,32 +61,25 @@ error[E0277]: the trait bound `&str: Zeroize` is not satisfied = note: required for `&str` to implement `Zeroize` = note: required for `Vec` to implement `EncryptTarget>` -error[E0277]: the trait bound `for<'a> &'a Vec: Scalar` is not satisfied +error[E0277]: `Vec` cannot be encrypted into one `Ciphertext` --> tests/ui/owned_collection_elements.rs:14:27 | 14 | let _ = vec![1i32, 2].encrypt_into::(keyset); - | ^^^^^^^^^^^^ the trait `for<'a> Scalar` is not implemented for `&'a Vec` + | ^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value | -help: the following other types implement trait `Scalar` - --> $WORKSPACE/packages/vitaminc-aead/src/plaintext/sources.rs + = help: the trait `OpaqueEncode` is not implemented for `Vec` + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. +help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` + --> src/blob.rs | - | impl Scalar for $ty {} - | ^^^^^^^^^^^^^^^^^^^ - | | - | `&Vec` - | `Vec` -... - | / scalar! { - | | bool, &bool, f32, &f32, f64, &f64, - | | i8, &i8, i16, &i16, i32, &i32, i64, &i64, i128, &i128, - | | u16, &u16, u32, &u32, u64, &u64, u128, &u128, -... | - | | Bytes<'_>, &Bytes<'_>, Raw<'_>, &Raw<'_>, - | | } - | |_- in this macro invocation - = note: required for `Vec` to implement `OpaqueEncode` + | impl OpaqueEncode for OpaqueBlob {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: required for `stack_encrypt::Ciphertext` to implement `EncryptTarget>` - = note: this error originates in the macro `scalar` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0277]: the trait bound `u8: vitaminc_aead::typed::FromScalar` is not satisfied --> tests/ui/owned_collection_elements.rs:15:13 diff --git a/packages/stack-encrypt/tests/ui/pass/derived_newtype_scalar.rs b/packages/stack-encrypt/tests/ui/pass/derived_newtype_scalar.rs new file mode 100644 index 000000000..525d7afb6 --- /dev/null +++ b/packages/stack-encrypt/tests/ui/pass/derived_newtype_scalar.rs @@ -0,0 +1,52 @@ +//! A newtype that derives `Encrypt` over one value is one value too. The +//! newtypes here have no `Scalar` impl written by hand. +//! +//! - `keyset.encrypt` encrypts the newtype by itself. +//! - `keyset.encrypt` encrypts it inside a `Protected`, and inside another +//! such newtype. +//! - A `Vec` of the newtype encrypts one element at a time. +//! +//! `tests/ui/scalar_derived_not_one_value.rs` lists the derived types that +//! `keyset.encrypt` refuses. +use stack_encrypt::{Ciphertext, EncryptInto, KeysetCipher, Protected}; +use stack_encrypt::Encrypt; +use stack_kms::FakeDataKeySource; +use zeroize::Zeroize; + +#[derive(Encrypt, Zeroize)] +struct Email(String); + +#[derive(Encrypt, Zeroize)] +struct Age(i64); + +#[derive(Encrypt, Zeroize)] +struct Contact(Email); + +#[derive(Encrypt, Zeroize)] +struct Wrapper(T); + +async fn encrypt(keyset: &KeysetCipher<'_, FakeDataKeySource>) { + let _: Ciphertext = keyset.encrypt(Email("ada@example.com".into())).await.unwrap(); + let _: Ciphertext = keyset.encrypt(Age(37)).await.unwrap(); + let _: Ciphertext = keyset + .encrypt_with_context(Age(37), ("users", "age")) + .await + .unwrap(); + let _: Ciphertext = keyset + .encrypt(Protected::new(Email("ada@example.com".into()))) + .await + .unwrap(); + let _: Ciphertext = keyset + .encrypt(Contact(Email("ada@example.com".into()))) + .await + .unwrap(); + let _: Ciphertext = keyset.encrypt(Wrapper(37i64)).await.unwrap(); + let _: Vec = vec![Email("ada@example.com".into()), Email("grace@example.com".into())] + .encrypt_into(keyset) + .await + .unwrap(); +} + +fn main() { + let _ = encrypt; +} diff --git a/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr b/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr index 2aaa670ff..13b9b9ad5 100644 --- a/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr +++ b/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr @@ -1,56 +1,45 @@ -error[E0277]: the trait bound `for<'a> &'a Vec: Scalar` is not satisfied +error[E0277]: `Vec` cannot be encrypted into one `Ciphertext` --> tests/ui/scalar_collections_and_borrows.rs:2:28 | 2 | let _ = keyset.encrypt(vec![1i32, 2]); - | ------- ^^^^^^^^^^^^^ the trait `for<'a> Scalar` is not implemented for `&'a Vec` - | | - | required by a bound introduced by this call - | -help: the following other types implement trait `Scalar` - --> $WORKSPACE/packages/vitaminc-aead/src/plaintext/sources.rs - | - | impl Scalar for $ty {} - | ^^^^^^^^^^^^^^^^^^^ - | | - | `&Vec` - | `Vec` -... - | / scalar! { - | | bool, &bool, f32, &f32, f64, &f64, - | | i8, &i8, i16, &i16, i32, &i32, i64, &i64, i128, &i128, - | | u16, &u16, u32, &u32, u64, &u64, u128, &u128, -... | - | | Bytes<'_>, &Bytes<'_>, Raw<'_>, &Raw<'_>, - | | } - | |_- in this macro invocation - = note: required for `Vec` to implement `OpaqueEncode` + | ^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | + = help: the trait `OpaqueEncode` is not implemented for `Vec` + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. +help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` + --> src/blob.rs + | + | impl OpaqueEncode for OpaqueBlob {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | | pub fn encrypt(&self, value: S) -> Pending<'_, Ciphertext, K> { | ^^^^^^^^^^^^ required by this bound in `stack_encrypt::scalar::>::encrypt` - = note: this error originates in the macro `scalar` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `&str: OpaqueEncode` is not satisfied +error[E0277]: `&str` cannot be encrypted into one `Ciphertext` --> tests/ui/scalar_collections_and_borrows.rs:3:28 | 3 | let _ = keyset.encrypt("borrowed plaintext"); - | ------- ^^^^^^^^^^^^^^^^^^^^ the trait `zeroize::DefaultIsZeroes` is not implemented for `&str` - | | - | required by a bound introduced by this call - | - = help: the following other types implement trait `zeroize::DefaultIsZeroes`: - () - PhantomPinned - bool - char - f32 - f64 - i128 - i16 - and $N others - = note: required for `&str` to implement `Zeroize` - = note: required for `&str` to implement `OpaqueEncode` + | ^^^^^^^^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | + = help: the trait `OpaqueEncode` is not implemented for `&str` + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. +help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` + --> src/blob.rs + | + | impl OpaqueEncode for OpaqueBlob {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | diff --git a/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.rs b/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.rs new file mode 100644 index 000000000..516011717 --- /dev/null +++ b/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.rs @@ -0,0 +1,29 @@ +//! A derived type that is not one value still derives `Encrypt`. So it can +//! be a field of a struct. `keyset.encrypt` refuses each of these types: +//! +//! - a newtype over a `Vec` +//! - a newtype over an `Option` +//! - a struct with named fields +//! +//! Each error says what `keyset.encrypt` encrypts, and what to use instead. +use stack_encrypt::{Encrypt, KeysetCipher}; +use zeroize::Zeroize; + +#[derive(Encrypt, Zeroize)] +struct Tags(Vec); + +#[derive(Encrypt, Zeroize)] +struct Nickname(Option); + +#[derive(Encrypt, Zeroize)] +struct Profile { + name: String, +} + +fn refused(keyset: &KeysetCipher<'_, K>) { + let _ = keyset.encrypt(Tags(vec!["admin".into()])); + let _ = keyset.encrypt(Nickname(None)); + let _ = keyset.encrypt(Profile { name: "Ada".into() }); +} + +fn main() {} diff --git a/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr b/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr new file mode 100644 index 000000000..c24dcaa7b --- /dev/null +++ b/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr @@ -0,0 +1,83 @@ +error[E0277]: `Tags` cannot be encrypted into one `Ciphertext` + --> tests/ui/scalar_derived_not_one_value.rs:24:28 + | +24 | let _ = keyset.encrypt(Tags(vec!["admin".into()])); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | +help: the trait `OpaqueEncode` is not implemented for `Tags` + --> tests/ui/scalar_derived_not_one_value.rs:13:1 + | +13 | struct Tags(Vec); + | ^^^^^^^^^^^ + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. +help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` + --> src/blob.rs + | + | impl OpaqueEncode for OpaqueBlob {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: required by a bound in `stack_encrypt::scalar::>::encrypt` + --> src/scalar.rs + | + | pub fn encrypt(&self, value: S) -> Pending<'_, Ciphertext, K> { + | ^^^^^^^^^^^^ required by this bound in `stack_encrypt::scalar::>::encrypt` + +error[E0277]: `Nickname` cannot be encrypted into one `Ciphertext` + --> tests/ui/scalar_derived_not_one_value.rs:25:28 + | +25 | let _ = keyset.encrypt(Nickname(None)); + | ^^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | +help: the trait `OpaqueEncode` is not implemented for `Nickname` + --> tests/ui/scalar_derived_not_one_value.rs:16:1 + | +16 | struct Nickname(Option); + | ^^^^^^^^^^^^^^^ + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. +help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` + --> src/blob.rs + | + | impl OpaqueEncode for OpaqueBlob {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: required by a bound in `stack_encrypt::scalar::>::encrypt` + --> src/scalar.rs + | + | pub fn encrypt(&self, value: S) -> Pending<'_, Ciphertext, K> { + | ^^^^^^^^^^^^ required by this bound in `stack_encrypt::scalar::>::encrypt` + +error[E0277]: `Profile` cannot be encrypted into one `Ciphertext` + --> tests/ui/scalar_derived_not_one_value.rs:26:28 + | +26 | let _ = keyset.encrypt(Profile { name: "Ada".into() }); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | +help: the trait `OpaqueEncode` is not implemented for `Profile` + --> tests/ui/scalar_derived_not_one_value.rs:19:1 + | +19 | struct Profile { + | ^^^^^^^^^^^^^^ + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. +help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` + --> src/blob.rs + | + | impl OpaqueEncode for OpaqueBlob {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: required by a bound in `stack_encrypt::scalar::>::encrypt` + --> src/scalar.rs + | + | pub fn encrypt(&self, value: S) -> Pending<'_, Ciphertext, K> { + | ^^^^^^^^^^^^ required by this bound in `stack_encrypt::scalar::>::encrypt` diff --git a/packages/stack-encrypt/tests/ui/scalar_nullable.stderr b/packages/stack-encrypt/tests/ui/scalar_nullable.stderr index 1be60c369..75b0f55ed 100644 --- a/packages/stack-encrypt/tests/ui/scalar_nullable.stderr +++ b/packages/stack-encrypt/tests/ui/scalar_nullable.stderr @@ -1,47 +1,45 @@ -error[E0277]: the trait bound `for<'a> &'a Option: Scalar` is not satisfied +error[E0277]: `Option` cannot be encrypted into one `Ciphertext` --> tests/ui/scalar_nullable.rs:2:28 | 2 | let _ = keyset.encrypt(Some(42i32)); - | ------- ^^^^^^^^^^^ the trait `for<'a> Scalar` is not implemented for `&'a Option` - | | - | required by a bound introduced by this call - | - = help: the following other types implement trait `Scalar`: - &&'b T - &'a Protected - &JsonPlaintext - &ProtectedRef<'a, T> - &Raw<'_> - &Vec - &[u8; N] - &[u8] - and $N others - = note: required for `Option` to implement `OpaqueEncode` + | ^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | + = help: the trait `OpaqueEncode` is not implemented for `Option` + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. +help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` + --> src/blob.rs + | + | impl OpaqueEncode for OpaqueBlob {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | | pub fn encrypt(&self, value: S) -> Pending<'_, Ciphertext, K> { | ^^^^^^^^^^^^ required by this bound in `stack_encrypt::scalar::>::encrypt` -error[E0277]: the trait bound `for<'a> &'a Option: Scalar` is not satisfied +error[E0277]: `Option` cannot be encrypted into one `Ciphertext` --> tests/ui/scalar_nullable.rs:3:28 | 3 | let _ = keyset.encrypt(None::); - | ------- ^^^^^^^^^^^ the trait `for<'a> Scalar` is not implemented for `&'a Option` - | | - | required by a bound introduced by this call - | - = help: the following other types implement trait `Scalar`: - &&'b T - &'a Protected - &JsonPlaintext - &ProtectedRef<'a, T> - &Raw<'_> - &Vec - &[u8; N] - &[u8] - and $N others - = note: required for `Option` to implement `OpaqueEncode` + | ^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | + = help: the trait `OpaqueEncode` is not implemented for `Option` + = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. + = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. + = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. + = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. + = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. +help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` + --> src/blob.rs + | + | impl OpaqueEncode for OpaqueBlob {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | From c2b8fe8ab050ef7f5ef70884b21f433d992ad584 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 19:40:24 +1100 Subject: [PATCH 03/14] fix(vitaminc-aead-derive): treat a fieldless enum marked recursive as one leaf The derive implements `Scalar` for every fieldless enum. But it wrote no `DEPTH` for a type marked `#[aead(recursive)]`, so such an enum kept the default `DEPTH` of `None`. `keyset.encrypt` checks that a `Scalar` type has a `DEPTH` of `depth::SCALAR`. So `keyset.encrypt(Status::Active)` failed the build with `E0080`, about an impl the user did not write. A fieldless enum has no fields, so it cannot contain itself. The derive now ignores `recursive` on a fieldless enum. The enum gets a `DEPTH` of `depth::SCALAR` and its `u32` `FIXED` type, as it does without the attribute. Refusing the attribute was the other option. It would break code that compiles today, for no gain. --- .../vitaminc-aead-derive/docs/attributes.md | 3 ++ packages/vitaminc-aead-derive/src/encrypt.rs | 50 +++++++++++++++---- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/packages/vitaminc-aead-derive/docs/attributes.md b/packages/vitaminc-aead-derive/docs/attributes.md index 1289e1a72..5c29bc878 100644 --- a/packages/vitaminc-aead-derive/docs/attributes.md +++ b/packages/vitaminc-aead-derive/docs/attributes.md @@ -110,6 +110,9 @@ struct Odd { # assert!(even.next[0].next.is_empty()); ``` +A fieldless enum has no fields, so it cannot be part of a cycle. The derive +ignores `recursive` on a fieldless enum: the enum is still one `u32` leaf. + A type that contains a recursive type is not checked either, since its depth depends on the recursive part. A generic type is checked once it is used at a concrete type, under `cargo build` rather than `cargo check`. diff --git a/packages/vitaminc-aead-derive/src/encrypt.rs b/packages/vitaminc-aead-derive/src/encrypt.rs index a147dcfd9..c2ad8514f 100644 --- a/packages/vitaminc-aead-derive/src/encrypt.rs +++ b/packages/vitaminc-aead-derive/src/encrypt.rs @@ -27,6 +27,9 @@ pub(crate) fn derive(input: DeriveInput) -> Result { let krate = &attrs.krate; let shape = Shape::parse(&input)?; let name = &input.ident; + // A fieldless enum has no fields, so it cannot contain itself. It is one + // `u32` leaf, with a fixed depth, even when it is marked `recursive`. + let recursive = attrs.recursive && !matches!(shape, Shape::Enum(_)); // Bound every type parameter, serde-style: a field of type `Vec` then // picks up `Vec: Encrypt` from the blanket impl in `vitaminc_aead`. @@ -91,16 +94,9 @@ pub(crate) fn derive(input: DeriveInput) -> Result { by_ref: ref_depth, item_check, visit_check, - } = depth(krate, &input, &shape, attrs.recursive); - let (fixed, ref_fixed) = fixed(krate, &input, &shape, attrs.recursive); - let scalar = scalar( - krate, - &input, - &shape, - attrs.recursive, - &generics, - &ref_generics, - ); + } = depth(krate, &input, &shape, recursive); + let (fixed, ref_fixed) = fixed(krate, &input, &shape, recursive); + let scalar = scalar(krate, &input, &shape, recursive, &generics, &ref_generics); Ok(quote! { #[automatically_derived] @@ -1287,6 +1283,40 @@ mod tests { ); } + /// A fieldless enum cannot contain itself, so `recursive` changes + /// nothing. The enum still declares a scalar depth and a `u32` fixed + /// type, and it is still a `Scalar`. + #[test] + fn a_recursive_fieldless_enum_is_still_one_leaf() { + let out = expand(parse_quote! { + #[aead(recursive)] + enum Status { + Active, + } + }); + assert_contains( + &out, + quote!( + const DEPTH: ::core::option::Option = + ::vitaminc_aead::plaintext::depth::SCALAR; + ), + ); + assert_contains( + &out, + quote!( + const FIXED: ::core::option::Option<::vitaminc_aead::FixedType> = + ::vitaminc_aead::FixedType::plain(::vitaminc_aead::Width::U32); + ), + ); + assert_contains(&out, quote!(impl ::vitaminc_aead::Scalar for Status {})); + assert_contains( + &out, + quote!( + impl<'__r> ::vitaminc_aead::Scalar for &'__r Status {} + ), + ); + } + /// A struct with fields and a struct with none each encrypt as a map. A /// recursive newtype has no fixed depth. So none of them is a `Scalar`. #[test] From c3f2cf79c5b14bd773a2d301b20c001579d00bb5 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 19:40:25 +1100 Subject: [PATCH 04/14] fix(stack-encrypt): name the DEPTH a Scalar type needs, and keep the Zeroize cause in refusals `keyset.encrypt` checks, when the program is built, that a `Scalar` type's borrow has a `DEPTH` of `depth::SCALAR`. A hand-written `Encrypt` impl that left `DEPTH` at its default `None` used to compile with `keyset.encrypt`. It now fails the build. The check stays strict. `None` means that the depth depends on the value, and a `Scalar` impl says the opposite. The assertion message, the `OpaqueEncode` docs, and the `Scalar` docs now give the line to add: `const DEPTH: Option = depth::SCALAR;`. The `compile_fail` doctest of the check now has a passing twin. The two differ only in `DEPTH`. On stable, rustdoc does not check the error code of a `compile_fail` test, so the twin shows that the snippet compiles apart from the check. The refusal message no longer names the call, so it reads correctly for `encrypt_with_context` and `EncryptInto`. It lists the three conditions, then the other calls. It says "a fixed-width integer other than `u8`", since `usize` and `isize` are not scalars. The `Scalar` bound moved into a sealed helper trait, `OneValue`. The impl of `OneValue` has `do_not_recommend`, and the blanket impl of `OpaqueEncode` no longer has it. So a type that is not one value is still reported under the message of `OpaqueEncode`, without Vitamin C's `Scalar`. A newtype over a `String` that only lacks `Zeroize` now gets an error that names `Zeroize`. `OneValue` has `EncryptRef` as a supertrait, not the higher-ranked `Scalar` bound. So `seal` walks the value through `EncryptRef::lend`. The walk is the same as before. --- packages/stack-encrypt/src/scalar.rs | 141 ++++++++++++------ .../opaque_blob_requires_the_wrapper.stderr | 16 +- .../ui/opaque_blob_requires_zeroize.stderr | 13 +- .../tests/ui/owned_collection_elements.stderr | 76 +++++----- .../ui/scalar_collections_and_borrows.stderr | 54 ++++--- .../ui/scalar_derived_not_one_value.stderr | 48 +++--- .../tests/ui/scalar_nullable.stderr | 36 ++--- packages/vitaminc-aead/src/plaintext/mod.rs | 7 + 8 files changed, 230 insertions(+), 161 deletions(-) diff --git a/packages/stack-encrypt/src/scalar.rs b/packages/stack-encrypt/src/scalar.rs index d64f2b7be..26f7c7cf9 100644 --- a/packages/stack-encrypt/src/scalar.rs +++ b/packages/stack-encrypt/src/scalar.rs @@ -33,6 +33,15 @@ pub(crate) mod sealed { context: ContextPiece<'static>, ) -> Pending<'a, Self, K>; } + + /// A type whose borrow encrypts as one value. It is implemented for + /// every type whose borrow is a Vitamin C `Scalar`. + /// + /// The trait is a separate bound of [`OpaqueEncode`](super::OpaqueEncode) + /// so that a refusal reports each condition on its own. Its impl has + /// `do_not_recommend`, so the compiler reports a missing `Scalar` as a + /// missing `OneValue`. A missing `Zeroize` is still reported by name. + pub trait OneValue: crate::EncryptRef {} } /// A plaintext type that encrypts as one opaque [`Ciphertext`]. @@ -95,9 +104,16 @@ pub(crate) mod sealed { /// an `OpaqueEncode` type when you implement `Scalar` for its borrow. /// /// A `Scalar` impl states that the borrow encrypts as one value. So the -/// borrow's [`DEPTH`](crate::Encrypt::DEPTH) must be Vitamin C's -/// `depth::SCALAR`. `keyset.encrypt` checks this when your program is -/// built. A type with a false `Scalar` impl fails the build. +/// borrow's `Encrypt` impl must set its [`DEPTH`](crate::Encrypt::DEPTH): +/// +/// ```text +/// const DEPTH: Option = depth::SCALAR; +/// ``` +/// +/// `depth` is `vitaminc_aead::plaintext::depth`. The default `DEPTH` is +/// `None`. `None` means that the depth depends on the value. `keyset.encrypt` +/// checks the `DEPTH` when your program is built. A `Scalar` type whose +/// borrow has any other `DEPTH` fails the build. /// /// [`OpaqueBlob`](crate::OpaqueBlob) is an `OpaqueEncode` type too. It /// encodes a whole `Serialize` value as one storage blob first. @@ -152,17 +168,18 @@ pub(crate) mod sealed { /// ``` #[diagnostic::on_unimplemented( message = "`{Self}` cannot be encrypted into one `Ciphertext`", - label = "`keyset.encrypt` encrypts one owned value", - note = "one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than \ - `u8`. A newtype that derives `Encrypt` over one of these is one value too.", - note = "a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`.", - note = "the type must implement `Zeroize`, so that the value can be wiped after it is \ - encrypted.", - note = "to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`.", - note = "to encrypt an `Option`, or a struct that derives `Encrypt`, use \ - `keyset.encrypt_tree`. The result is a tree of ciphertexts.", - note = "to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in \ - `OpaqueBlob`." + label = "this must be one owned value that can be wiped", + note = "to be encrypted into one `Ciphertext`, a type must meet three conditions.", + note = "first, its value must be one scalar, such as a `String`, a `bool`, a float, a \ + `Vec`, or a fixed-width integer other than `u8`. A newtype that derives \ + `Encrypt` over a scalar is a scalar too.", + note = "second, the value must be owned. A borrow, such as a `&str`, is not owned.", + note = "third, the type must implement `Zeroize`, so that the value can be wiped after it \ + is encrypted.", + note = "a value that is not one scalar needs another call. `EncryptInto` encrypts each \ + element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an \ + `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole \ + `Serialize` value into one `Ciphertext`." )] pub trait OpaqueEncode: sealed::Encode + Zeroize + Send + 'static {} @@ -184,38 +201,81 @@ pub trait OpaqueEncode: sealed::Encode + Zeroize + Send + 'static {} /// [`Error::OpenedType`]. pub trait OpaqueDecode: sealed::Decode + Zeroize + Send + 'static {} -// An owned `S` is sealed through a borrow of it. `&S` is one of Vitamin C's -// scalars, so the borrow is walked as it is: nothing is copied out of the -// caller's value, and the fixed width of the type is the borrow's own, which -// is what pads an absent value to a present one's length. +// `do_not_recommend` stops the compiler from reporting the `Scalar` bound +// below. A type that is not one value is then reported as a missing +// `OneValue`, under the message of `OpaqueEncode`. +#[diagnostic::do_not_recommend] +impl sealed::OneValue for S where for<'a> &'a S: Scalar {} + +// An owned `S` is sealed through a borrow of it. `EncryptRef::lend` makes +// the borrow. The borrow of a `OneValue` type is one of Vitamin C's +// scalars, so it is walked as it is. Nothing is copied out of the caller's +// value. +// +// The fixed width of the type is the borrow's own. That width pads an +// absent value to the length of a present one. // // Anyone can implement `Scalar`. The compiler does not compare an impl with // the type's `DEPTH`. So `seal` checks that the borrow's `DEPTH` is // `depth::SCALAR`. // -// The check is an inline `const`. The compiler evaluates -// it when it builds `seal` for a concrete `S`. So a false `Scalar` impl -// fails the build, not a call. +// The check is an inline `const`. The compiler evaluates it when it builds +// `seal` for a concrete `S`. So a false `Scalar` impl fails the build, not +// a call. -/// A false `Scalar` impl fails the build. A false impl is one on a type -/// whose borrow is not one value. `cargo build` runs the check, and -/// `cargo check` does not. +/// The `Scalar` impl of a type must agree with the `DEPTH` of its borrow. +/// +/// The first example compiles. Its type sets `DEPTH` to `depth::SCALAR`, and +/// it encrypts as one value. +/// +/// ``` +/// use stack_encrypt::kms::FakeDataKeySource; +/// use stack_encrypt::{Encrypt, EncryptRef, KeysetCipher}; +/// use vitaminc_aead::plaintext::{depth, Sink}; +/// use vitaminc_aead::Scalar; +/// use zeroize::Zeroize; +/// +/// // A type whose borrow encrypts as its one `String`. +/// #[derive(Zeroize)] +/// struct Profile(String); +/// +/// impl Encrypt for &Profile { +/// const DEPTH: Option = depth::SCALAR; +/// fn visit(self, sink: S) -> Result { +/// self.0.visit_ref(sink) +/// } +/// } +/// impl Scalar for &Profile {} +/// +/// fn encrypt(keyset: &KeysetCipher<'_, FakeDataKeySource>) { +/// let _ = keyset.encrypt(Profile("ada".into())); +/// } +/// +/// fn main() { +/// let encrypt: fn(&KeysetCipher<'_, FakeDataKeySource>) = encrypt; +/// let _ = encrypt; +/// } +/// ``` +/// +/// The second example is the same, except for its `DEPTH`. The type states +/// that its borrow encrypts as a map, but it is marked `Scalar`. So the +/// build fails. `cargo build` runs the check, and `cargo check` does not. /// /// ```compile_fail,E0080 /// use stack_encrypt::kms::FakeDataKeySource; -/// use stack_encrypt::{Encrypt, KeysetCipher}; +/// use stack_encrypt::{Encrypt, EncryptRef, KeysetCipher}; /// use vitaminc_aead::plaintext::{depth, Sink}; /// use vitaminc_aead::Scalar; /// use zeroize::Zeroize; /// -/// // The borrow of `Profile` encrypts as a map, but it is marked `Scalar`. +/// // A type whose borrow encrypts as its one `String`. /// #[derive(Zeroize)] /// struct Profile(String); /// /// impl Encrypt for &Profile { /// const DEPTH: Option = depth::container(depth::SCALAR); -/// fn visit(self, _sink: S) -> Result { -/// todo!() +/// fn visit(self, sink: S) -> Result { +/// self.0.visit_ref(sink) /// } /// } /// impl Scalar for &Profile {} @@ -231,8 +291,7 @@ pub trait OpaqueDecode: sealed::Decode + Zeroize + Send + 'static {} /// ``` impl sealed::Encode for S where - S: Zeroize + Send + 'static, - for<'a> &'a S: Scalar, + S: sealed::OneValue + Zeroize + Send + 'static, { fn seal<'a, K: 'static>( &self, @@ -241,25 +300,17 @@ where ) -> Pending<'a, Ciphertext, K> { const { assert!( - vitaminc_aead::plaintext::depth::is_scalar(<&'static S as crate::Encrypt>::DEPTH), - "a type marked `Scalar` must encrypt as one value. The `DEPTH` of this \ - type's borrow is not `depth::SCALAR`.", + vitaminc_aead::plaintext::depth::is_scalar(::REF_DEPTH), + "a type marked `Scalar` must encrypt as one value. Set \ + `const DEPTH: Option = depth::SCALAR;` in the `Encrypt` impl of its \ + borrow.", ) }; - crate::target::core::encrypt_opaque(self, cipher, context).try_map(Ciphertext::from_tree) + crate::target::core::encrypt_opaque(self.lend(), cipher, context) + .try_map(Ciphertext::from_tree) } } -// `do_not_recommend` makes the compiler report a refused type against -// `OpaqueEncode`. The message of `OpaqueEncode` says what to use instead. -// Without the attribute, the compiler reports the `Scalar` or `Zeroize` -// bound below. -#[diagnostic::do_not_recommend] -impl OpaqueEncode for S -where - S: Zeroize + Send + 'static, - for<'a> &'a S: Scalar, -{ -} +impl OpaqueEncode for S where S: sealed::OneValue + Zeroize + Send + 'static {} impl sealed::Decode for P { fn open<'a, K: 'static>( leaf: SealedValue, diff --git a/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr b/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr index 2de8711da..8c620a870 100644 --- a/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr +++ b/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr @@ -2,24 +2,24 @@ error[E0277]: `Profile` cannot be encrypted into one `Ciphertext` --> tests/ui/opaque_blob_requires_the_wrapper.rs:16:35 | 16 | let _refused = keyset.encrypt(profile).await.unwrap(); - | ^^^^^^^ `keyset.encrypt` encrypts one owned value + | ^^^^^^^ this must be one owned value that can be wiped | -help: the trait `OpaqueEncode` is not implemented for `Profile` +help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Profile` --> tests/ui/opaque_blob_requires_the_wrapper.rs:10:1 | 10 | struct Profile { | ^^^^^^^^^^^^^^ - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | | impl OpaqueEncode for OpaqueBlob {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: required for `Profile` to implement `OpaqueEncode` note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | diff --git a/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr b/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr index 84205cb52..36733ae22 100644 --- a/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr +++ b/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr @@ -58,17 +58,16 @@ error[E0277]: `OpaqueBlob` cannot be encrypted into one `Ciphertext` --> tests/ui/opaque_blob_requires_zeroize.rs:14:35 | 14 | let _refused = keyset.encrypt(OpaqueBlob::new(value)).await.unwrap(); - | ------- ^^^^^^^^^^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | ------- ^^^^^^^^^^^^^^^^^^^^^^ this must be one owned value that can be wiped | | | required by a bound introduced by this call | = help: the trait `OpaqueEncode` is not implemented for `OpaqueBlob` - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | diff --git a/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr b/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr index 9801200d1..1db22063a 100644 --- a/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr +++ b/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr @@ -2,20 +2,22 @@ error[E0277]: `u8` cannot be encrypted into one `Ciphertext` --> tests/ui/owned_collection_elements.rs:11:26 | 11 | let _ = vec![1u8, 2].encrypt_into::, _>(keyset); - | ^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | ^^^^^^^^^^^^ this must be one owned value that can be wiped | - = help: the trait `OpaqueEncode` is not implemented for `u8` - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. -help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` - --> src/blob.rs + = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `u8` + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. +help: the trait `EncryptTarget>` is implemented for `Vec` + --> src/target/collections.rs | - | impl OpaqueEncode for OpaqueBlob {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | / impl> EncryptTarget> for Vec + | | where + | | T::ContextInput: Clone + MaybeSend + 'static, + | |_________________________________________________^ + = note: required for `u8` to implement `OpaqueEncode` = note: required for `stack_encrypt::Ciphertext` to implement `EncryptTarget` = note: 1 redundant requirement hidden = note: required for `Vec` to implement `EncryptTarget>` @@ -24,20 +26,22 @@ error[E0277]: `Option` cannot be encrypted into one `Ciphertext` --> tests/ui/owned_collection_elements.rs:12:36 | 12 | let _ = vec![Some(1i32), None].encrypt_into::, _>(keyset); - | ^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | ^^^^^^^^^^^^ this must be one owned value that can be wiped | - = help: the trait `OpaqueEncode` is not implemented for `Option` - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. -help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` - --> src/blob.rs + = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Option` + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. +help: the trait `EncryptTarget>` is implemented for `Vec` + --> src/target/collections.rs | - | impl OpaqueEncode for OpaqueBlob {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | / impl> EncryptTarget> for Vec + | | where + | | T::ContextInput: Clone + MaybeSend + 'static, + | |_________________________________________________^ + = note: required for `Option` to implement `OpaqueEncode` = note: required for `stack_encrypt::Ciphertext` to implement `EncryptTarget>` = note: 1 redundant requirement hidden = note: required for `Vec` to implement `EncryptTarget>>` @@ -65,20 +69,20 @@ error[E0277]: `Vec` cannot be encrypted into one `Ciphertext` --> tests/ui/owned_collection_elements.rs:14:27 | 14 | let _ = vec![1i32, 2].encrypt_into::(keyset); - | ^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | ^^^^^^^^^^^^ this must be one owned value that can be wiped | - = help: the trait `OpaqueEncode` is not implemented for `Vec` - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. -help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` - --> src/blob.rs + = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Vec` + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. +help: the trait `EncryptTarget` is implemented for `stack_encrypt::Ciphertext` + --> src/scalar.rs | - | impl OpaqueEncode for OpaqueBlob {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | impl EncryptTarget for Ciphertext { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: required for `Vec` to implement `OpaqueEncode` = note: required for `stack_encrypt::Ciphertext` to implement `EncryptTarget>` error[E0277]: the trait bound `u8: vitaminc_aead::typed::FromScalar` is not satisfied diff --git a/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr b/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr index 13b9b9ad5..fa854609e 100644 --- a/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr +++ b/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr @@ -2,20 +2,20 @@ error[E0277]: `Vec` cannot be encrypted into one `Ciphertext` --> tests/ui/scalar_collections_and_borrows.rs:2:28 | 2 | let _ = keyset.encrypt(vec![1i32, 2]); - | ^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value - | - = help: the trait `OpaqueEncode` is not implemented for `Vec` - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. + | ^^^^^^^^^^^^^ this must be one owned value that can be wiped + | + = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Vec` + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | | impl OpaqueEncode for OpaqueBlob {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: required for `Vec` to implement `OpaqueEncode` note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | @@ -26,20 +26,28 @@ error[E0277]: `&str` cannot be encrypted into one `Ciphertext` --> tests/ui/scalar_collections_and_borrows.rs:3:28 | 3 | let _ = keyset.encrypt("borrowed plaintext"); - | ^^^^^^^^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value - | - = help: the trait `OpaqueEncode` is not implemented for `&str` - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. -help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` - --> src/blob.rs - | - | impl OpaqueEncode for OpaqueBlob {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ------- ^^^^^^^^^^^^^^^^^^^^ this must be one owned value that can be wiped + | | + | required by a bound introduced by this call + | + = help: the trait `zeroize::DefaultIsZeroes` is not implemented for `&str` + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = help: the following other types implement trait `zeroize::DefaultIsZeroes`: + () + PhantomPinned + bool + char + f32 + f64 + i128 + i16 + and $N others + = note: required for `&str` to implement `Zeroize` + = note: required for `&str` to implement `OpaqueEncode` note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | diff --git a/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr b/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr index c24dcaa7b..d5002240c 100644 --- a/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr +++ b/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr @@ -2,24 +2,24 @@ error[E0277]: `Tags` cannot be encrypted into one `Ciphertext` --> tests/ui/scalar_derived_not_one_value.rs:24:28 | 24 | let _ = keyset.encrypt(Tags(vec!["admin".into()])); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ this must be one owned value that can be wiped | -help: the trait `OpaqueEncode` is not implemented for `Tags` +help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Tags` --> tests/ui/scalar_derived_not_one_value.rs:13:1 | 13 | struct Tags(Vec); | ^^^^^^^^^^^ - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | | impl OpaqueEncode for OpaqueBlob {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: required for `Tags` to implement `OpaqueEncode` note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | @@ -30,24 +30,24 @@ error[E0277]: `Nickname` cannot be encrypted into one `Ciphertext` --> tests/ui/scalar_derived_not_one_value.rs:25:28 | 25 | let _ = keyset.encrypt(Nickname(None)); - | ^^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | ^^^^^^^^^^^^^^ this must be one owned value that can be wiped | -help: the trait `OpaqueEncode` is not implemented for `Nickname` +help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Nickname` --> tests/ui/scalar_derived_not_one_value.rs:16:1 | 16 | struct Nickname(Option); | ^^^^^^^^^^^^^^^ - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | | impl OpaqueEncode for OpaqueBlob {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: required for `Nickname` to implement `OpaqueEncode` note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | @@ -58,24 +58,24 @@ error[E0277]: `Profile` cannot be encrypted into one `Ciphertext` --> tests/ui/scalar_derived_not_one_value.rs:26:28 | 26 | let _ = keyset.encrypt(Profile { name: "Ada".into() }); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this must be one owned value that can be wiped | -help: the trait `OpaqueEncode` is not implemented for `Profile` +help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Profile` --> tests/ui/scalar_derived_not_one_value.rs:19:1 | 19 | struct Profile { | ^^^^^^^^^^^^^^ - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | | impl OpaqueEncode for OpaqueBlob {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: required for `Profile` to implement `OpaqueEncode` note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | diff --git a/packages/stack-encrypt/tests/ui/scalar_nullable.stderr b/packages/stack-encrypt/tests/ui/scalar_nullable.stderr index 75b0f55ed..e8ebf5853 100644 --- a/packages/stack-encrypt/tests/ui/scalar_nullable.stderr +++ b/packages/stack-encrypt/tests/ui/scalar_nullable.stderr @@ -2,20 +2,20 @@ error[E0277]: `Option` cannot be encrypted into one `Ciphertext` --> tests/ui/scalar_nullable.rs:2:28 | 2 | let _ = keyset.encrypt(Some(42i32)); - | ^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value - | - = help: the trait `OpaqueEncode` is not implemented for `Option` - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. + | ^^^^^^^^^^^ this must be one owned value that can be wiped + | + = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Option` + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | | impl OpaqueEncode for OpaqueBlob {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: required for `Option` to implement `OpaqueEncode` note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | @@ -26,20 +26,20 @@ error[E0277]: `Option` cannot be encrypted into one `Ciphertext` --> tests/ui/scalar_nullable.rs:3:28 | 3 | let _ = keyset.encrypt(None::); - | ^^^^^^^^^^^ `keyset.encrypt` encrypts one owned value - | - = help: the trait `OpaqueEncode` is not implemented for `Option` - = note: one value is a `String`, a `bool`, a float, a `Vec`, or an integer other than `u8`. A newtype that derives `Encrypt` over one of these is one value too. - = note: a borrow, such as a `&str`, is not owned. Pass an owned value, such as a `String`. - = note: the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: to encrypt each element of a `Vec` into its own `Ciphertext`, use `EncryptInto`. - = note: to encrypt an `Option`, or a struct that derives `Encrypt`, use `keyset.encrypt_tree`. The result is a tree of ciphertexts. - = note: to encrypt a whole `Serialize` value as one `Ciphertext`, wrap the value in `OpaqueBlob`. + | ^^^^^^^^^^^ this must be one owned value that can be wiped + | + = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Option` + = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. + = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. + = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | | impl OpaqueEncode for OpaqueBlob {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: required for `Option` to implement `OpaqueEncode` note: required by a bound in `stack_encrypt::scalar::>::encrypt` --> src/scalar.rs | diff --git a/packages/vitaminc-aead/src/plaintext/mod.rs b/packages/vitaminc-aead/src/plaintext/mod.rs index d70900edb..45fb303c8 100644 --- a/packages/vitaminc-aead/src/plaintext/mod.rs +++ b/packages/vitaminc-aead/src/plaintext/mod.rs @@ -279,6 +279,13 @@ impl Encrypt for &Lent<'_, T> { /// seals_one_leaf(Blob(vec![7])); /// ``` /// +/// A hand-written leaf must set `const DEPTH: Option = +/// depth::SCALAR;` in each `Encrypt` impl that is marked `Scalar`, as +/// `Blob` does. The default `DEPTH` is `None`. `None` means that the depth +/// depends on the value. A `Scalar` impl contradicts that default. So a +/// caller that checks `DEPTH`, as `seals_one_leaf` does, refuses the type +/// when the program is built. +/// /// `Vec` is the one exception to the rule that a `Vec` is a sequence: /// it is a byte string, sealed as a single bytes leaf. The same goes for /// `[u8; N]` and `&[u8]`. So `vec![1u8, 2, 3]` is a `Scalar`, while a From c6b5ba317c60838bea1304d9ac1226d0ccd9b38e Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 19:40:25 +1100 Subject: [PATCH 05/14] test(stack-encrypt): encrypt a derived fieldless enum The pass test and the runtime test covered only newtypes. A fieldless enum that derives `Encrypt` now encrypts with `keyset.encrypt`, so both tests cover it too. The runtime test checks that each variant seals the same leaf as the `u32` of its discriminant, and that `keyset.decrypt::` opens it. The runtime test also encrypts an enum marked `#[aead(recursive)]`. The test file is built, so it runs the build-time `DEPTH` check that failed for such an enum before the derive fix. --- .../stack-encrypt/tests/derived_newtype.rs | 86 ++++++++++++++++++- .../tests/ui/pass/derived_newtype_scalar.rs | 17 ++++ 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/packages/stack-encrypt/tests/derived_newtype.rs b/packages/stack-encrypt/tests/derived_newtype.rs index f87021160..98a8ba22c 100644 --- a/packages/stack-encrypt/tests/derived_newtype.rs +++ b/packages/stack-encrypt/tests/derived_newtype.rs @@ -1,5 +1,6 @@ //! `keyset.encrypt` encrypts a newtype that derives `Encrypt` exactly as it -//! encrypts the newtype's inner value. +//! encrypts the newtype's inner value. It encrypts a fieldless enum that +//! derives `Encrypt` exactly as it encrypts the `u32` of the variant. //! //! A leaf is one sealed value. Each leaf has a random nonce, a number used //! once. So two ciphertexts of one value never have the same bytes. These @@ -37,6 +38,22 @@ struct Email(String); #[derive(Encrypt, Zeroize)] struct Age(i64); +#[derive(Encrypt, Zeroize)] +enum Status { + Active, + Suspended = 7, +} + +// A fieldless enum cannot contain itself, so `recursive` changes nothing. +// This test file is built, not only checked, so it runs the build-time +// `DEPTH` check of `keyset.encrypt` on this type. +#[derive(Encrypt, Zeroize)] +#[aead(recursive)] +enum Level { + Low, + High = 3, +} + const SEED: [u8; 32] = *b"stack-encrypt derived newtype v1"; /// The data keys that a [`KeptKeys`] source generated, by initialisation @@ -246,3 +263,70 @@ async fn a_newtype_over_an_integer_seals_the_leaf_an_integer_seals() { "the newtype's leaf holds the plaintext its inner value's leaf holds" ); } + +#[tokio::test] +async fn a_fieldless_enum_seals_the_leaf_its_u32_seals() { + let (cipher, keys) = kept_cipher().await; + let keyset = cipher.default_keyset(); + + for (variant, discriminant) in [(Status::Active, 0u32), (Status::Suspended, 7)] { + let wrapped = keyset + .encrypt(variant) + .await + .expect("a fieldless enum encrypts"); + let bare = keyset + .encrypt(discriminant) + .await + .expect("a `u32` encrypts"); + + assert_eq!( + wrapped.to_bytes().len(), + bare.to_bytes().len(), + "the enum's ciphertext is as long as its `u32`'s" + ); + let opened: u32 = keyset + .decrypt(wrapped.clone()) + .await + .expect("the enum's ciphertext decrypts into a `u32`"); + assert_eq!( + opened, discriminant, + "the enum's ciphertext decrypts to the variant's discriminant" + ); + assert_eq!( + reveal(&keys, wrapped, ()), + reveal(&keys, bare, ()), + "the enum's leaf holds the plaintext its `u32`'s leaf holds" + ); + } +} + +#[tokio::test] +async fn a_fieldless_enum_marked_recursive_is_still_one_value() { + let (cipher, keys) = kept_cipher().await; + let keyset = cipher.default_keyset(); + + for (variant, discriminant) in [(Level::Low, 0u32), (Level::High, 3)] { + let wrapped = keyset + .encrypt(variant) + .await + .expect("a fieldless enum marked `recursive` encrypts"); + let bare = keyset + .encrypt(discriminant) + .await + .expect("a `u32` encrypts"); + + let opened: u32 = keyset + .decrypt(wrapped.clone()) + .await + .expect("the enum's ciphertext decrypts into a `u32`"); + assert_eq!( + opened, discriminant, + "the enum's ciphertext decrypts to the variant's discriminant" + ); + assert_eq!( + reveal(&keys, wrapped, ()), + reveal(&keys, bare, ()), + "the enum's leaf holds the plaintext its `u32`'s leaf holds" + ); + } +} diff --git a/packages/stack-encrypt/tests/ui/pass/derived_newtype_scalar.rs b/packages/stack-encrypt/tests/ui/pass/derived_newtype_scalar.rs index 525d7afb6..9f3fb7253 100644 --- a/packages/stack-encrypt/tests/ui/pass/derived_newtype_scalar.rs +++ b/packages/stack-encrypt/tests/ui/pass/derived_newtype_scalar.rs @@ -5,6 +5,8 @@ //! - `keyset.encrypt` encrypts it inside a `Protected`, and inside another //! such newtype. //! - A `Vec` of the newtype encrypts one element at a time. +//! - `keyset.encrypt` encrypts a fieldless enum that derives `Encrypt`, with +//! or without `#[aead(recursive)]`. //! //! `tests/ui/scalar_derived_not_one_value.rs` lists the derived types that //! `keyset.encrypt` refuses. @@ -25,6 +27,18 @@ struct Contact(Email); #[derive(Encrypt, Zeroize)] struct Wrapper(T); +#[derive(Encrypt, Zeroize)] +enum Status { + Active, + Suspended = 7, +} + +#[derive(Encrypt, Zeroize)] +#[aead(recursive)] +enum Level { + Low, +} + async fn encrypt(keyset: &KeysetCipher<'_, FakeDataKeySource>) { let _: Ciphertext = keyset.encrypt(Email("ada@example.com".into())).await.unwrap(); let _: Ciphertext = keyset.encrypt(Age(37)).await.unwrap(); @@ -41,6 +55,9 @@ async fn encrypt(keyset: &KeysetCipher<'_, FakeDataKeySource>) { .await .unwrap(); let _: Ciphertext = keyset.encrypt(Wrapper(37i64)).await.unwrap(); + let _: Ciphertext = keyset.encrypt(Status::Active).await.unwrap(); + let _: Ciphertext = keyset.encrypt(Status::Suspended).await.unwrap(); + let _: Ciphertext = keyset.encrypt(Level::Low).await.unwrap(); let _: Vec = vec![Email("ada@example.com".into()), Email("grace@example.com".into())] .encrypt_into(keyset) .await From 45ea2c58b75f670cc54bd2a9fec798a295cbef8f Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 19:40:25 +1100 Subject: [PATCH 06/14] docs: correct the migration advice for a Scalar impl that now conflicts The Breaking entries said that after you delete a hand-written `Scalar` impl, `keyset.encrypt` still encrypts the type. That is false for a newtype over a type that is not a `Scalar`. The derived impl conflicts with a hand-written one even then (`E0119`), and deleting the impl leaves a type that `keyset.encrypt` refuses. The entries in the three CHANGELOGs now give both cases. For a fieldless enum, or a newtype over a `Scalar`, delete the impl. A newtype can wrap another type whose value the author knows is one leaf. For that newtype, stop deriving `Encrypt`. Write `Encrypt` by hand for the type and its borrow, with `DEPTH` set to `depth::SCALAR`. The stack-encrypt CHANGELOG moves the `DEPTH` check to Breaking, and says which line to add. The derive CHANGELOG says that a fieldless enum marked recursive is one leaf. --- packages/stack-encrypt/CHANGELOG.md | 39 +++++++++++++++------- packages/vitaminc-aead-derive/CHANGELOG.md | 8 +++-- packages/vitaminc-aead/CHANGELOG.md | 7 +++- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/packages/stack-encrypt/CHANGELOG.md b/packages/stack-encrypt/CHANGELOG.md index ee9af813b..bc0e1cbc5 100644 --- a/packages/stack-encrypt/CHANGELOG.md +++ b/packages/stack-encrypt/CHANGELOG.md @@ -1278,12 +1278,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 it needs a `_` arm. To migrate code that names `recipher`, see the stack-kms CHANGELOG. -- **Delete a `Scalar` impl you wrote for a derived newtype or a fieldless - enum** (#2012). `#[derive(Encrypt)]` now implements Vitamin C's `Scalar` - marker for these types. So an `impl Scalar for &Email {}` of your own no - longer compiles. The compiler reports `E0119`, conflicting - implementations. After you delete the impl, `keyset.encrypt` still - encrypts the type. +- **A `Scalar` impl you wrote for a derived newtype or a fieldless enum no + longer compiles** (#2012). `#[derive(Encrypt)]` now implements Vitamin + C's `Scalar` marker for these types. So an `impl Scalar for &Email {}` of + your own conflicts with it. The compiler reports `E0119`, conflicting + implementations. The compiler reports it even when the inner type is not + a `Scalar`. What to do depends on the inner type. + - For a fieldless enum, or for a newtype whose inner type is one value, + such as a `String`, delete your impl. `keyset.encrypt` still encrypts + the type. + - When the inner type is not a `Scalar`, but you know that its value is + one leaf, stop deriving `Encrypt` for the type. Write `Encrypt` for the + type and for its borrow by hand, with + `const DEPTH: Option = depth::SCALAR;`. Then keep your `Scalar` + impl. An example is a newtype over a frame that you check holds one + value. + +- **A `Scalar` type that you write by hand must set its `DEPTH`** (#2012). + `keyset.encrypt` now checks, when the program is built, that the borrow + of the type encrypts as one value. So the `Encrypt` impl of the borrow + must contain `const DEPTH: Option = depth::SCALAR;`. `depth` is + `vitaminc_aead::plaintext::depth`. An impl that leaves `DEPTH` at its + default `None` compiled before. Now `cargo build` fails with `E0080`, + and the message names the line to add. `cargo check` does not run this + check. ### Added @@ -1771,16 +1789,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `Encrypt`, as one `u32` value. - The compile error for a refused type now says that the type cannot be encrypted into one `Ciphertext`. The error says what `keyset.encrypt` - encrypts, and what to use instead: + needs, and what to use instead: - `EncryptInto`, for each element of a `Vec` - `keyset.encrypt_tree`, for an `Option` or a struct - `OpaqueBlob`, for a whole `Serialize` value - The error no longer names the `Scalar` or `Zeroize` bound that the type - did not meet. - - `keyset.encrypt` now checks, when the program is built, a `Scalar` - impl written by hand. If the type's borrow encrypts as a container, - the build fails with `E0080`. `cargo check` does not run this check. + The error no longer names Vitamin C's `Scalar` trait. When the only + missing condition is `Zeroize`, the error still says so. - **A query on an EQL type whose twin carries every term is refused for what any of its indexes refuses, before any key is requested** (#1668). diff --git a/packages/vitaminc-aead-derive/CHANGELOG.md b/packages/vitaminc-aead-derive/CHANGELOG.md index b2c4b0097..c37269877 100644 --- a/packages/vitaminc-aead-derive/CHANGELOG.md +++ b/packages/vitaminc-aead-derive/CHANGELOG.md @@ -4,8 +4,10 @@ - **Breaking:** `#[derive(Encrypt)]` now implements `Scalar` for a newtype and for a fieldless enum. A `Scalar` impl you wrote for such a type no longer compiles. - The compiler reports `E0119`, conflicting implementations. - Delete your impl, because the derived impl replaces it. + The compiler reports `E0119`, conflicting implementations, even when the inner type is not a `Scalar`. + - For a fieldless enum, or for a newtype whose inner type is a `Scalar`, delete your impl, because the derived impl replaces it. + - When the inner type is not a `Scalar`, but you know that its value is one leaf, stop deriving `Encrypt`. + Write `Encrypt` for the type and for its borrow by hand, with `const DEPTH: Option = depth::SCALAR;`, and keep your `Scalar` impl. ### Features @@ -17,6 +19,8 @@ `struct Tags(Vec)` still derives `Encrypt`, but it is not a `Scalar`. Neither is `struct Nickname(Option)`. A fieldless enum is always a `Scalar`, because it is one `u32` leaf. + That is true even when the enum is marked `#[aead(recursive)]`. + Its `DEPTH` is now `depth::SCALAR` in that case too, where it was `None`. A struct with fields is not a `Scalar`. A recursive newtype is not a `Scalar` either. diff --git a/packages/vitaminc-aead/CHANGELOG.md b/packages/vitaminc-aead/CHANGELOG.md index 348e7669b..21839e977 100644 --- a/packages/vitaminc-aead/CHANGELOG.md +++ b/packages/vitaminc-aead/CHANGELOG.md @@ -47,7 +47,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). So `struct Email(String)` needs no `Scalar` impl of its own. A `Protected` is a `Scalar` too. `struct Tags(Vec)` still derives `Encrypt`, but it is not a `Scalar`. - A `Scalar` impl you wrote for such a type now conflicts with the derived impl, so delete it. + A `Scalar` impl you wrote for such a type now conflicts with the derived impl, even when the inner type is not a `Scalar`. + For a fieldless enum, or for a newtype whose inner type is a `Scalar`, delete your impl. + When the inner type is not a `Scalar`, but you know that its value is one leaf, stop deriving `Encrypt`. + Write `Encrypt` for the type and for its borrow by hand, with `const DEPTH: Option = depth::SCALAR;`, and keep your `Scalar` impl. +- The `Scalar` docs now say that a hand-written `Scalar` type must set `const DEPTH: Option = depth::SCALAR;`. + A caller that checks `DEPTH` refuses a `Scalar` type whose `DEPTH` is the default `None`. ## [0.10.0] - 2026-11-19 From 369b17bc61d0027218a8d3c8fa5488482380efc2 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 19:59:15 +1100 Subject: [PATCH 07/14] style(stack-encrypt): give the OpaqueEncode blanket impl a body comment The blanket impl of `OpaqueEncode` had become one line, and rustfmt keeps an empty impl on one line. The open decrypt branch changes the line after this impl, so the two branches conflicted. The impl now has a comment in its body. So rustfmt writes the `where` clause on its own lines, and the two changes no longer touch adjacent lines. The comment says why the `Scalar` bound is in `OneValue`. --- packages/stack-encrypt/src/scalar.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/stack-encrypt/src/scalar.rs b/packages/stack-encrypt/src/scalar.rs index 26f7c7cf9..ce0195302 100644 --- a/packages/stack-encrypt/src/scalar.rs +++ b/packages/stack-encrypt/src/scalar.rs @@ -310,7 +310,13 @@ where .try_map(Ciphertext::from_tree) } } -impl OpaqueEncode for S where S: sealed::OneValue + Zeroize + Send + 'static {} +impl OpaqueEncode for S +where + S: sealed::OneValue + Zeroize + Send + 'static, +{ + // The `Scalar` bound is in `OneValue`, so that the compiler reports a + // missing `Zeroize` by name. +} impl sealed::Decode for P { fn open<'a, K: 'static>( leaf: SealedValue, From a47a1e70b470fe4f2d91ea581e22df6cce9f679a Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 20:26:46 +1100 Subject: [PATCH 08/14] refactor(vitaminc-aead-derive): decide once whether a type has a fixed depth The rule "this type is one value of fixed depth" was written four times. The enum override had one copy. `scalar`, `depth` and `fixed` each had `recursive || names_itself(..)`. A new shape could miss one of them. The derive now computes `fixed_depth` once and passes it to the three functions. `scalar` now clones the generics only in the arms that use them. `fixed` no longer takes the input. It used the input only for the old check. The expansion does not change. The expansion tests pass unchanged. I also compared the token output of nine inputs before and after: - a newtype, and a generic newtype - a newtype marked recursive, and a newtype that names itself - an enum, and an enum marked recursive - a struct, a struct marked recursive, and an empty struct The two outputs are identical. --- packages/vitaminc-aead-derive/src/encrypt.rs | 67 ++++++++++---------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/packages/vitaminc-aead-derive/src/encrypt.rs b/packages/vitaminc-aead-derive/src/encrypt.rs index c2ad8514f..6ace5eea3 100644 --- a/packages/vitaminc-aead-derive/src/encrypt.rs +++ b/packages/vitaminc-aead-derive/src/encrypt.rs @@ -27,9 +27,13 @@ pub(crate) fn derive(input: DeriveInput) -> Result { let krate = &attrs.krate; let shape = Shape::parse(&input)?; let name = &input.ident; - // A fieldless enum has no fields, so it cannot contain itself. It is one - // `u32` leaf, with a fixed depth, even when it is marked `recursive`. - let recursive = attrs.recursive && !matches!(shape, Shape::Enum(_)); + // Whether the type has a fixed depth. A type that contains itself has + // none, because its depth depends on the value. The derive detects a type + // that names itself in a field. `#[aead(recursive)]` marks recursion + // through another type. A fieldless enum has no fields, so it + // cannot contain itself, even when it is marked `recursive`. + let fixed_depth = + matches!(shape, Shape::Enum(_)) || !(attrs.recursive || names_itself(name, &shape)); // Bound every type parameter, serde-style: a field of type `Vec` then // picks up `Vec: Encrypt` from the blanket impl in `vitaminc_aead`. @@ -94,9 +98,9 @@ pub(crate) fn derive(input: DeriveInput) -> Result { by_ref: ref_depth, item_check, visit_check, - } = depth(krate, &input, &shape, recursive); - let (fixed, ref_fixed) = fixed(krate, &input, &shape, recursive); - let scalar = scalar(krate, &input, &shape, recursive, &generics, &ref_generics); + } = depth(krate, &input, &shape, fixed_depth); + let (fixed, ref_fixed) = fixed(krate, &shape, fixed_depth); + let scalar = scalar(krate, &input, &shape, fixed_depth, &generics, &ref_generics); Ok(quote! { #[automatically_derived] @@ -152,29 +156,29 @@ fn scalar( krate: &syn::Path, input: &DeriveInput, shape: &Shape, - recursive: bool, + fixed_depth: bool, generics: &syn::Generics, ref_generics: &syn::Generics, ) -> TokenStream { let name = &input.ident; - let mut owned = generics.clone(); - let mut by_ref = ref_generics.clone(); - match shape { - Shape::Newtype(_) if recursive || names_itself(name, shape) => return TokenStream::new(), - Shape::Newtype(field) => { + let (owned, by_ref) = match shape { + Shape::Newtype(field) if fixed_depth => { let ty = &field.ty; + let mut owned = generics.clone(); owned .make_where_clause() .predicates .push(parse_quote!(for<'__s> #ty: #krate::Scalar)); + let mut by_ref = ref_generics.clone(); by_ref .make_where_clause() .predicates .push(parse_quote!(&'__r #ty: #krate::Scalar)); + (owned, by_ref) } - Shape::Enum(_) => {} - Shape::Map(_) | Shape::Empty => return TokenStream::new(), - } + Shape::Enum(_) => (generics.clone(), ref_generics.clone()), + Shape::Newtype(_) | Shape::Map(_) | Shape::Empty => return TokenStream::new(), + }; let (impl_generics, ty_generics, where_clause) = owned.split_for_impl(); let (ref_impl_generics, _, ref_where_clause) = by_ref.split_for_impl(); quote! { @@ -205,16 +209,15 @@ struct Depth { /// sink counts nesting. A recursive type has no fixed depth, and naming its /// own `DEPTH` would be a cycle, so it keeps the default `None` and the /// walk checks each value instead. -fn depth(krate: &syn::Path, input: &DeriveInput, shape: &Shape, recursive: bool) -> Depth { +fn depth(krate: &syn::Path, input: &DeriveInput, shape: &Shape, fixed_depth: bool) -> Depth { let name = &input.ident; - let none = Depth { - owned: TokenStream::new(), - by_ref: TokenStream::new(), - item_check: TokenStream::new(), - visit_check: TokenStream::new(), - }; - if recursive || names_itself(name, shape) { - return none; + if !fixed_depth { + return Depth { + owned: TokenStream::new(), + by_ref: TokenStream::new(), + item_check: TokenStream::new(), + visit_check: TokenStream::new(), + }; } let depth = quote!(#krate::plaintext::depth); @@ -299,18 +302,10 @@ fn depth(krate: &syn::Path, input: &DeriveInput, shape: &Shape, recursive: bool) /// value and by reference, as `DEPTH` does). A fieldless enum is always a /// `u32` leaf. A struct is a map, which has none, and a recursive newtype /// keeps the default rather than name its own constant in a cycle. -fn fixed( - krate: &syn::Path, - input: &DeriveInput, - shape: &Shape, - recursive: bool, -) -> (TokenStream, TokenStream) { +fn fixed(krate: &syn::Path, shape: &Shape, fixed_depth: bool) -> (TokenStream, TokenStream) { let decl = |value: TokenStream| quote!(const FIXED: ::core::option::Option<#krate::FixedType> = #value;); match shape { - Shape::Newtype(_) if recursive || names_itself(&input.ident, shape) => { - (TokenStream::new(), TokenStream::new()) - } - Shape::Newtype(field) => { + Shape::Newtype(field) if fixed_depth => { let ty = &field.ty; ( decl(quote!(<#ty as #krate::Encrypt>::FIXED)), @@ -321,7 +316,9 @@ fn fixed( let u32 = decl(quote!(#krate::FixedType::plain(#krate::Width::U32))); (u32.clone(), u32) } - Shape::Map(_) | Shape::Empty => (TokenStream::new(), TokenStream::new()), + Shape::Newtype(_) | Shape::Map(_) | Shape::Empty => { + (TokenStream::new(), TokenStream::new()) + } } } From 09c47a1a42b5699e38aef65c43b0827dc715dbce Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 20:30:58 +1100 Subject: [PATCH 09/14] docs(vitaminc-aead): list the Scalar impl conflict under Breaking The conflict with a hand-written `Scalar` impl (E0119), and how to migrate, were listed under Added. They are a breaking change. The `[Unreleased]` section now has a Breaking section, as the 0.10.0 section has, and the entry is there. The Added entry points to it. --- packages/vitaminc-aead/CHANGELOG.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/vitaminc-aead/CHANGELOG.md b/packages/vitaminc-aead/CHANGELOG.md index 21839e977..86be7b3e1 100644 --- a/packages/vitaminc-aead/CHANGELOG.md +++ b/packages/vitaminc-aead/CHANGELOG.md @@ -14,6 +14,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). A leaf sealed in another unit by an earlier version still decrypts. - Transport frames are unchanged: a frame keeps the unit it was given. +### Breaking + +- **Breaking:** `#[derive(Encrypt)]` now implements `Scalar` for a newtype and for a fieldless enum. + A `Scalar` impl you wrote for such a type now conflicts with the derived impl, even when the inner type is not a `Scalar`. + The compiler reports `E0119`, conflicting implementations. + - For a fieldless enum, or for a newtype whose inner type is a `Scalar`, delete your impl. + - When the inner type is not a `Scalar`, but you know that its value is one leaf, stop deriving `Encrypt`. + Write `Encrypt` for the type and for its borrow by hand, with `const DEPTH: Option = depth::SCALAR;`, and keep your `Scalar` impl. + ### Added - `plaintext::ValueKind`: what a value is, without the value, with the text name a schema spells it with (`"int32"`, `"timestamp"`, `"object"`, `"json"`, …). @@ -47,10 +56,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). So `struct Email(String)` needs no `Scalar` impl of its own. A `Protected` is a `Scalar` too. `struct Tags(Vec)` still derives `Encrypt`, but it is not a `Scalar`. - A `Scalar` impl you wrote for such a type now conflicts with the derived impl, even when the inner type is not a `Scalar`. - For a fieldless enum, or for a newtype whose inner type is a `Scalar`, delete your impl. - When the inner type is not a `Scalar`, but you know that its value is one leaf, stop deriving `Encrypt`. - Write `Encrypt` for the type and for its borrow by hand, with `const DEPTH: Option = depth::SCALAR;`, and keep your `Scalar` impl. + The section above, under Breaking, says what to do with a `Scalar` impl you wrote for such a type. - The `Scalar` docs now say that a hand-written `Scalar` type must set `const DEPTH: Option = depth::SCALAR;`. A caller that checks `DEPTH` refuses a `Scalar` type whose `DEPTH` is the default `None`. From d9000da362d6c4dc68c74fdcce907b71f3287c41 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 20:30:58 +1100 Subject: [PATCH 10/14] docs(stack-encrypt): say that the derive still needs vitaminc-aead The Fixed entry said that a derived newtype encrypts "with no other code", and that the old impl "needed a dependency" on `vitaminc-aead`. Both read as if the dependency were gone. It is not: the derived code names `vitaminc_aead`, so the crate still lists `vitaminc-aead` (#2013). The entry now says only that the hand-written `Scalar` impl is gone, and that the crate still needs `vitaminc-aead` to use the derive. --- packages/stack-encrypt/CHANGELOG.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/stack-encrypt/CHANGELOG.md b/packages/stack-encrypt/CHANGELOG.md index bc0e1cbc5..e9b563504 100644 --- a/packages/stack-encrypt/CHANGELOG.md +++ b/packages/stack-encrypt/CHANGELOG.md @@ -1774,9 +1774,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **`keyset.encrypt` encrypts a newtype that derives `Encrypt` over one value** (#2012). A newtype is a struct with exactly one unnamed field. `struct Email(String)` with `#[derive(Encrypt, Zeroize)]` now encrypts - with `keyset.encrypt(Email(..))`, with no other code. Before this - change, the newtype needed a hand-written `impl Scalar for &Email {}`. - That impl needed a dependency on the `Scalar` trait of `vitaminc-aead`. + with `keyset.encrypt(Email(..))`. You no longer write + `impl Scalar for &Email {}` by hand. Your crate still needs + `vitaminc-aead` as a dependency to use the derive, because the derived + code names `vitaminc_aead` (#2013). - The newtype encrypts exactly as its inner value does. The leaf has the same plaintext and the same length. So `keyset.decrypt::` opens what `Email` encrypted. From 053cee443615aae3290ddfca682c7016c33bc4b2 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 20:31:30 +1100 Subject: [PATCH 11/14] docs: say that derive(Zeroize) does not wipe a fieldless enum `#[derive(Zeroize)]` from zeroize 1.9.1 (zeroize_derive 1.5.0) writes a `zeroize` for a fieldless enum whose match arms are all empty. I checked the expansion, and a test that calls `zeroize()` on `Status::Suspended`. The value is still `Suspended` afterwards. So `keyset.encrypt` cannot wipe the variant of such an enum from memory, although the docs said that the source is wiped. The `OpaqueEncode` docs, the `# Enums` docs of the derive, and the stack-encrypt CHANGELOG entry now say so. They show how to have the variant wiped: give the enum a `#[default]` variant and implement `zeroize::DefaultIsZeroes`. The `zeroize` crate then overwrites the value with the default variant, with a volatile write. A doctest in each crate checks that `zeroize()` writes the default variant. Enum support stays as it is. --- packages/stack-encrypt/CHANGELOG.md | 6 ++++- packages/stack-encrypt/src/scalar.rs | 30 ++++++++++++++++++++++++ packages/vitaminc-aead-derive/src/lib.rs | 25 ++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/packages/stack-encrypt/CHANGELOG.md b/packages/stack-encrypt/CHANGELOG.md index e9b563504..cf24b7bb7 100644 --- a/packages/stack-encrypt/CHANGELOG.md +++ b/packages/stack-encrypt/CHANGELOG.md @@ -1787,7 +1787,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 such a newtype when the program compiles. `keyset.encrypt` also refuses a struct with named fields. - `keyset.encrypt` also encrypts a fieldless enum that derives - `Encrypt`, as one `u32` value. + `Encrypt`, as one `u32` value. But `#[derive(Zeroize)]` does not wipe a + fieldless enum: its `zeroize` does nothing. So `keyset.encrypt` cannot + wipe the variant from memory after it encrypts it. To have the variant + wiped, give the enum a `#[default]` variant and implement + `zeroize::DefaultIsZeroes` for it, as the `OpaqueEncode` docs show. - The compile error for a refused type now says that the type cannot be encrypted into one `Ciphertext`. The error says what `keyset.encrypt` needs, and what to use instead: diff --git a/packages/stack-encrypt/src/scalar.rs b/packages/stack-encrypt/src/scalar.rs index ce0195302..c7b056fec 100644 --- a/packages/stack-encrypt/src/scalar.rs +++ b/packages/stack-encrypt/src/scalar.rs @@ -100,6 +100,36 @@ pub(crate) mod sealed { /// `struct Tags(Vec)`, still derives `Encrypt`. So it can be a field /// of a struct. `keyset.encrypt` refuses it, as it refuses a `Vec`. /// +/// A fieldless enum that derives `Encrypt` encrypts as one `u32`, so it is +/// one value too. But `#[derive(Zeroize)]` does not wipe a fieldless enum: +/// its `zeroize` does nothing. So `keyset.encrypt` cannot wipe the variant +/// from memory after it encrypts it. To have the variant wiped, give the +/// enum a `#[default]` variant and implement `zeroize::DefaultIsZeroes` for +/// it. Its `zeroize` then overwrites the value with the default variant. +/// +/// ``` +/// use stack_encrypt::{Encrypt, KeysetCipher}; +/// use zeroize::Zeroize; +/// +/// #[derive(Encrypt, Clone, Copy, Default, Debug, PartialEq)] +/// enum Status { +/// #[default] +/// Active, +/// Suspended = 7, +/// } +/// +/// impl zeroize::DefaultIsZeroes for Status {} +/// +/// fn encrypt(keyset: &KeysetCipher<'_, K>) { +/// let _ = keyset.encrypt(Status::Suspended); +/// } +/// +/// let mut status = Status::Suspended; +/// status.zeroize(); +/// assert_eq!(status, Status::Active, "zeroize writes the default variant"); +/// # let _ = encrypt::; +/// ``` +/// /// You can also write the `Encrypt` impl of a type yourself. Such a type is /// an `OpaqueEncode` type when you implement `Scalar` for its borrow. /// diff --git a/packages/vitaminc-aead-derive/src/lib.rs b/packages/vitaminc-aead-derive/src/lib.rs index 216e65b0e..cb5fae977 100644 --- a/packages/vitaminc-aead-derive/src/lib.rs +++ b/packages/vitaminc-aead-derive/src/lib.rs @@ -74,6 +74,31 @@ //! A fieldless enum is a `Scalar`, by value and by reference, because it //! is one leaf. //! +//! `#[derive(Zeroize)]` from the `zeroize` crate does not wipe a fieldless +//! enum: its `zeroize` does nothing. So a caller that wipes the value after +//! it encrypts it leaves the variant in memory. To have the variant wiped, +//! give the enum a `#[default]` variant and implement +//! `zeroize::DefaultIsZeroes` for it. Its `zeroize` then overwrites the +//! value with the default variant. +//! +//! ```rust +//! use vitaminc_aead::Encrypt; +//! use zeroize::Zeroize; +//! +//! #[derive(Encrypt, Clone, Copy, Default, Debug, PartialEq)] +//! enum Status { +//! #[default] +//! Active, +//! Suspended = 7, +//! } +//! +//! impl zeroize::DefaultIsZeroes for Status {} +//! +//! let mut status = Status::Suspended; +//! status.zeroize(); +//! assert_eq!(status, Status::Active, "zeroize writes the default variant"); +//! ``` +//! //! Enums with fields are not supported. Their payload has no agreed layout //! (internally or externally tagged, per-variant AAD), so model the choice //! explicitly, as a struct with a fieldless discriminant and `Option` From 52c2a01af91f8577b120016bb5810eba57b925c8 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 20:31:51 +1100 Subject: [PATCH 12/14] fix(stack-encrypt): name the type to fix when a derived newtype fails the DEPTH check A newtype that derives `Encrypt` copies the `DEPTH` of the type it wraps. Take `#[derive(Encrypt, Zeroize)] struct Outer(Inner)`, where `Inner` has a hand-written `Encrypt` impl with no `DEPTH`. Then `keyset.encrypt(Outer(..))` fails the build. The message told the user to set `DEPTH` in the impl for the borrow of `Outer`, which the derive wrote. The assertion message now says to set `DEPTH` in the impl that the user wrote. For a derived newtype, that is the innermost type with a hand-written `Encrypt` impl. The `OpaqueEncode` docs and the Breaking entry of the CHANGELOG say the same. --- packages/stack-encrypt/CHANGELOG.md | 15 +++++++++------ packages/stack-encrypt/src/scalar.rs | 9 +++++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/stack-encrypt/CHANGELOG.md b/packages/stack-encrypt/CHANGELOG.md index cf24b7bb7..250d2db47 100644 --- a/packages/stack-encrypt/CHANGELOG.md +++ b/packages/stack-encrypt/CHANGELOG.md @@ -1296,12 +1296,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **A `Scalar` type that you write by hand must set its `DEPTH`** (#2012). `keyset.encrypt` now checks, when the program is built, that the borrow - of the type encrypts as one value. So the `Encrypt` impl of the borrow - must contain `const DEPTH: Option = depth::SCALAR;`. `depth` is - `vitaminc_aead::plaintext::depth`. An impl that leaves `DEPTH` at its - default `None` compiled before. Now `cargo build` fails with `E0080`, - and the message names the line to add. `cargo check` does not run this - check. + of the type encrypts as one value. So the `Encrypt` impl that you wrote + for the borrow must contain `const DEPTH: Option = depth::SCALAR;`. + `depth` is `vitaminc_aead::plaintext::depth`. + - An impl that leaves `DEPTH` at its default `None` compiled before. Now + `cargo build` fails with `E0080`, and the message names the line to + add. `cargo check` does not run this check. + - A newtype that derives `Encrypt` copies the `DEPTH` of the type it + wraps. So when the newtype fails this check, fix the type it wraps. + That is the innermost type whose `Encrypt` impl you wrote by hand. ### Added diff --git a/packages/stack-encrypt/src/scalar.rs b/packages/stack-encrypt/src/scalar.rs index c7b056fec..969285e39 100644 --- a/packages/stack-encrypt/src/scalar.rs +++ b/packages/stack-encrypt/src/scalar.rs @@ -145,6 +145,10 @@ pub(crate) mod sealed { /// checks the `DEPTH` when your program is built. A `Scalar` type whose /// borrow has any other `DEPTH` fails the build. /// +/// A newtype that derives `Encrypt` copies the `DEPTH` of the type it wraps. +/// So when a derived newtype fails this check, fix the type it wraps. That +/// is the innermost type whose `Encrypt` impl you wrote by hand. +/// /// [`OpaqueBlob`](crate::OpaqueBlob) is an `OpaqueEncode` type too. It /// encodes a whole `Serialize` value as one storage blob first. /// @@ -332,8 +336,9 @@ where assert!( vitaminc_aead::plaintext::depth::is_scalar(::REF_DEPTH), "a type marked `Scalar` must encrypt as one value. Set \ - `const DEPTH: Option = depth::SCALAR;` in the `Encrypt` impl of its \ - borrow.", + `const DEPTH: Option = depth::SCALAR;` in the `Encrypt` impl that you \ + wrote for its borrow. For a newtype that derives `Encrypt`, set it on the \ + innermost type whose `Encrypt` impl you wrote by hand.", ) }; crate::target::core::encrypt_opaque(self.lend(), cipher, context) From 390747a25db1a17815513bd4a337c1b712842ffd Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 20:34:07 +1100 Subject: [PATCH 13/14] fix(stack-encrypt): cover OpaqueBlob in the conditions a refused type must meet An `OpaqueBlob` whose `T` is not `Zeroize` got a refusal that said the value must be one scalar. Its last note explained `OpaqueBlob` to a caller who already uses it. The only missing condition was `Zeroize`. The first condition now reads "one scalar or an `OpaqueBlob`". The `Zeroize` condition adds that, for an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. That matches the bound on the `OpaqueEncode` impl of `OpaqueBlob`. The last note now says what each call does for a value that is not one scalar. So it also reads correctly for a caller who already uses one of these calls. The ui snapshots change only in these notes. Each error is still E0277 at the same place in the source. --- packages/stack-encrypt/src/scalar.rs | 13 +++++----- .../opaque_blob_requires_the_wrapper.stderr | 8 +++---- .../ui/opaque_blob_requires_zeroize.stderr | 8 +++---- .../tests/ui/owned_collection_elements.stderr | 24 +++++++++---------- .../ui/scalar_collections_and_borrows.stderr | 16 ++++++------- .../ui/scalar_derived_not_one_value.stderr | 24 +++++++++---------- .../tests/ui/scalar_nullable.stderr | 16 ++++++------- 7 files changed, 55 insertions(+), 54 deletions(-) diff --git a/packages/stack-encrypt/src/scalar.rs b/packages/stack-encrypt/src/scalar.rs index 969285e39..d1584adec 100644 --- a/packages/stack-encrypt/src/scalar.rs +++ b/packages/stack-encrypt/src/scalar.rs @@ -203,14 +203,15 @@ pub(crate) mod sealed { #[diagnostic::on_unimplemented( message = "`{Self}` cannot be encrypted into one `Ciphertext`", label = "this must be one owned value that can be wiped", - note = "to be encrypted into one `Ciphertext`, a type must meet three conditions.", - note = "first, its value must be one scalar, such as a `String`, a `bool`, a float, a \ - `Vec`, or a fixed-width integer other than `u8`. A newtype that derives \ - `Encrypt` over a scalar is a scalar too.", + note = "to be encrypted into one `Ciphertext`, a value must meet three conditions.", + note = "first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such \ + as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than \ + `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too.", note = "second, the value must be owned. A borrow, such as a `&str`, is not owned.", note = "third, the type must implement `Zeroize`, so that the value can be wiped after it \ - is encrypted.", - note = "a value that is not one scalar needs another call. `EncryptInto` encrypts each \ + is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and \ + `Serialize`.", + note = "these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each \ element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an \ `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole \ `Serialize` value into one `Ciphertext`." diff --git a/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr b/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr index 8c620a870..9518835d1 100644 --- a/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr +++ b/packages/stack-encrypt/tests/ui/opaque_blob_requires_the_wrapper.stderr @@ -9,11 +9,11 @@ help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for | 10 | struct Profile { | ^^^^^^^^^^^^^^ - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | diff --git a/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr b/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr index 36733ae22..a957f9e04 100644 --- a/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr +++ b/packages/stack-encrypt/tests/ui/opaque_blob_requires_zeroize.stderr @@ -63,11 +63,11 @@ error[E0277]: `OpaqueBlob` cannot be encrypted into one `Ciphertext` | required by a bound introduced by this call | = help: the trait `OpaqueEncode` is not implemented for `OpaqueBlob` - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | diff --git a/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr b/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr index 1db22063a..4459ac689 100644 --- a/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr +++ b/packages/stack-encrypt/tests/ui/owned_collection_elements.stderr @@ -5,11 +5,11 @@ error[E0277]: `u8` cannot be encrypted into one `Ciphertext` | ^^^^^^^^^^^^ this must be one owned value that can be wiped | = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `u8` - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `EncryptTarget>` is implemented for `Vec` --> src/target/collections.rs | @@ -29,11 +29,11 @@ error[E0277]: `Option` cannot be encrypted into one `Ciphertext` | ^^^^^^^^^^^^ this must be one owned value that can be wiped | = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Option` - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `EncryptTarget>` is implemented for `Vec` --> src/target/collections.rs | @@ -72,11 +72,11 @@ error[E0277]: `Vec` cannot be encrypted into one `Ciphertext` | ^^^^^^^^^^^^ this must be one owned value that can be wiped | = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Vec` - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `EncryptTarget` is implemented for `stack_encrypt::Ciphertext` --> src/scalar.rs | diff --git a/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr b/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr index fa854609e..b4b22fe40 100644 --- a/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr +++ b/packages/stack-encrypt/tests/ui/scalar_collections_and_borrows.stderr @@ -5,11 +5,11 @@ error[E0277]: `Vec` cannot be encrypted into one `Ciphertext` | ^^^^^^^^^^^^^ this must be one owned value that can be wiped | = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Vec` - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | @@ -31,11 +31,11 @@ error[E0277]: `&str` cannot be encrypted into one `Ciphertext` | required by a bound introduced by this call | = help: the trait `zeroize::DefaultIsZeroes` is not implemented for `&str` - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. = help: the following other types implement trait `zeroize::DefaultIsZeroes`: () PhantomPinned diff --git a/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr b/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr index d5002240c..6e2d4b16d 100644 --- a/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr +++ b/packages/stack-encrypt/tests/ui/scalar_derived_not_one_value.stderr @@ -9,11 +9,11 @@ help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for | 13 | struct Tags(Vec); | ^^^^^^^^^^^ - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | @@ -37,11 +37,11 @@ help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for | 16 | struct Nickname(Option); | ^^^^^^^^^^^^^^^ - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | @@ -65,11 +65,11 @@ help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for | 19 | struct Profile { | ^^^^^^^^^^^^^^ - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | diff --git a/packages/stack-encrypt/tests/ui/scalar_nullable.stderr b/packages/stack-encrypt/tests/ui/scalar_nullable.stderr index e8ebf5853..cac0944af 100644 --- a/packages/stack-encrypt/tests/ui/scalar_nullable.stderr +++ b/packages/stack-encrypt/tests/ui/scalar_nullable.stderr @@ -5,11 +5,11 @@ error[E0277]: `Option` cannot be encrypted into one `Ciphertext` | ^^^^^^^^^^^ this must be one owned value that can be wiped | = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Option` - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | @@ -29,11 +29,11 @@ error[E0277]: `Option` cannot be encrypted into one `Ciphertext` | ^^^^^^^^^^^ this must be one owned value that can be wiped | = help: the trait `stack_encrypt::scalar::sealed::OneValue` is not implemented for `Option` - = note: to be encrypted into one `Ciphertext`, a type must meet three conditions. - = note: first, its value must be one scalar, such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. + = note: to be encrypted into one `Ciphertext`, a value must meet three conditions. + = note: first, the value must be one scalar or an `OpaqueBlob`. A scalar is a value such as a `String`, a `bool`, a float, a `Vec`, or a fixed-width integer other than `u8`. A newtype that derives `Encrypt` over a scalar is a scalar too. = note: second, the value must be owned. A borrow, such as a `&str`, is not owned. - = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. - = note: a value that is not one scalar needs another call. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. + = note: third, the type must implement `Zeroize`, so that the value can be wiped after it is encrypted. For an `OpaqueBlob`, `T` must implement `Zeroize` and `Serialize`. + = note: these calls encrypt a value that is not one scalar. `EncryptInto` encrypts each element of a `Vec` into its own `Ciphertext`. `keyset.encrypt_tree` encrypts an `Option` or a struct into a tree of ciphertexts. `OpaqueBlob` encrypts a whole `Serialize` value into one `Ciphertext`. help: the trait `OpaqueEncode` is implemented for `OpaqueBlob` --> src/blob.rs | From 1ba2012e771af7d9e0dde00b82d39fbc3d541054 Mon Sep 17 00:00:00 2001 From: Dan Draper Date: Sat, 28 Nov 2026 20:52:09 +1100 Subject: [PATCH 14/14] docs(stack-encrypt): link the enum wiping limit to its issue The CHANGELOG entry on fieldless enums says that `#[derive(Zeroize)]` does not wipe the variant. The gap is now tracked as #2015, so the entry points to it. The rustdoc stays as it is, without the issue number. --- packages/stack-encrypt/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/stack-encrypt/CHANGELOG.md b/packages/stack-encrypt/CHANGELOG.md index 250d2db47..b53e656e9 100644 --- a/packages/stack-encrypt/CHANGELOG.md +++ b/packages/stack-encrypt/CHANGELOG.md @@ -1794,7 +1794,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 fieldless enum: its `zeroize` does nothing. So `keyset.encrypt` cannot wipe the variant from memory after it encrypts it. To have the variant wiped, give the enum a `#[default]` variant and implement - `zeroize::DefaultIsZeroes` for it, as the `OpaqueEncode` docs show. + `zeroize::DefaultIsZeroes` for it, as the `OpaqueEncode` docs show (see + #2015). - The compile error for a refused type now says that the type cannot be encrypted into one `Ciphertext`. The error says what `keyset.encrypt` needs, and what to use instead: