Repository navigation
Description
Background
Stack Encrypt is the Rust engine in packages/stack-encrypt. After it encrypts a value, it wipes the plaintext from memory, so the value does not stay in RAM. It does this through the Zeroize trait, from the zeroize crate: keyset.encrypt accepts a type only if it implements Zeroize. Most users get the impl from #[derive(Zeroize)].
A fieldless enum is an enum whose variants are only names and contain no data, such as enum Status { Active, Suspended, Closed }. The engine's derive encrypts one as a single number: the position of the variant.
Problem
#[derive(Zeroize)] does nothing for a fieldless enum. The engine accepts the type as one that can be wiped, and the variant stays in memory.
#[derive(Encrypt, Zeroize)]
enum Diagnosis { None, Diabetes, Hiv }
keyset.encrypt(Diagnosis::Hiv).await?;
// The plaintext, the number 2, is still in memory.Why it happens:
- For an enum,
#[derive(Zeroize)]callszeroizeon each field of the current variant. A fieldless variant has no fields, so the generated code is an emptymatcharm for every variant. - The engine's bound is only
Zeroize. It cannot tell an impl that wipes from one that does nothing.
Verified with zeroize 1.9 and zeroize_derive 1.5: after .zeroize(), a Status::Suspended is still Status::Suspended. The expanded impl has an empty arm for each variant.
The variant is the secret. If it were not sensitive, the program would not encrypt it. The leftover is one small number for each value, and reading it needs access to the process's memory, such as a core dump. That is the case Zeroize exists for.
Where it applies:
keyset.encryptof a fieldless enum. fix(stack-encrypt): encrypt a derived newtype with keyset.encrypt #2014 makes this compile, and its docs state the gap.keyset.decryptinto a fieldless enum. feat(stack-encrypt)!: encrypt and decrypt one value known only at run time, and bound the typed decrypt to a scalar #2011 accepts one as a target.- Plausible, not verified: an enum that is a field of a struct the engine wipes, because
#[derive(Zeroize)]on the struct calls the same empty impl.
Proposal
Decide whether the engine should accept a fieldless enum that cannot be wiped, and make the code match the decision.
Approaches to evaluate:
-
The engine's derive writes the wipe. Stack Encrypt is getting its own derives (stack-encrypt:
#[derive(Encrypt, Decrypt)]does not compile in a crate that depends only onstack-encrypt#2013). A derive on a fieldless enum could also generate aZeroizeimpl. That impl would overwrite the value with one fixed variant. The user writes nothing more. -
Refuse an enum with no real wipe. The compiler cannot see inside a
Zeroizeimpl, so this needs a marker of some kind. Check whether it can be done without a second trait for the user to implement. -
Document only. Keep the docs from fix(stack-encrypt): encrypt a derived newtype with keyset.encrypt #2014. They show how to implement
Zeroizeby hand for an enum:impl Zeroize for Diagnosis { fn zeroize(&mut self) { *self = Diagnosis::None; } }
Whatever is chosen, add a test that encrypts a fieldless enum and checks that the value is wiped afterwards.
Relationship to other work
- fix(stack-encrypt): encrypt a derived newtype with keyset.encrypt #2014: accepts a fieldless enum in
keyset.encrypt, and documents the gap. - feat(stack-encrypt)!: encrypt and decrypt one value known only at run time, and bound the typed decrypt to a scalar #2011: accepts a fieldless enum as a decrypt target.
- stack-encrypt:
#[derive(Encrypt, Decrypt)]does not compile in a crate that depends only onstack-encrypt#2013: Stack Encrypt's own derives. Approach 1 builds on them.
Activity
docs(stack-encrypt): link the enum wiping limit to its issue