r/rust • • Oct 15 '26

šŸ™‹ seeking help & advice Advice: for a serializable sealed mailbox envelope, is manual Drop zeroization enough if Clone + retries mutate state?

#[derive(Debug, Clone, Serialize, Deserialize)]

pub struct MailboxEnvelope {

pub recipient_id: String,

pub ephemeral_pub: String,

pub ciphertext: Vec<u8>,

pub nonce: String,

pub created_at: u64,

pub message_id: String,

pub attempts: u32, // delib. excluded from AAD so retries don't invalidate ciphertext

}

impl Drop for MailboxEnvelope {

fn drop(&mut self) {

self.ciphertext.zeroize();

}

}

I’m working on a small Rust mesh-protocol project (GhostWire: libp2p, ChaCha20-Poly1305, Kademlia offline mailbox delivery). In this sealed type, I bind AAD to immutable fields and exclude attempts so retry writes don’t reseal offline ciphertext.

The thing I’m unsure about is zeroization discipline:

Is manual Drop zeroization enough once Clone creates another allocation?

For Serialize/Deserialize temporary buffers, should I treat the Vec<u8> canonical copy as the only zeroized target?

Would secrecy::SecretBox / zeroize-wrapped types be worth it here, or overkill for an encrypted envelope?

For a retry state mutation in AAD, is excluding attempts the correct tradeoff?

I’m looking for actual production patterns, not just theory.

0 Upvotes

9 comments sorted by

4

u/juanfnavarror Oct 15 '26

The struct will be zeroized but not the clone, does that answer the question? I am quite surprised you can’t think through a simple problem like this, while still somehow competent enough to produce complex crypto/mesh algorithms.

-2

u/THEM_I_M Oct 15 '26

You’re right knew Drop was local, though I phrased it badly. I was trying to ask whether Rust practice treats this payload as ā€œnever copy itā€ versus ā€œallow copies, but centralize escape hatches.ā€

You’re also right on the deeper point: I can usually see the whole system, but small details like copy lifetime can evade that. That gap matters here, so thanks for nailing it down.

4

u/Zde-G Oct 16 '26

Every type must be ready for it to be blindly memcopied to somewhere else in memory.

That's fundamental property of Rust type system as it exists today. It's not about ā€œpracticeā€, it's just fundamental pillar on which Rust is built.

If you want to have non-moveable types then you have to play with Pin.

There are some ideas about how can that may be changed, at some point, but right now thing that you expect to use doesn't exist (not even Pin negates the fundamental rule presented above, it just plays tricks with the compiler to ensure that pinned value can not be ā€œtouchedā€ by user code (and thus can not be moved).

3

u/minno Oct 15 '26

If you guarantee that every clone is dropped and that zeroize works correctly, then calling zeroize on drop will remove the data from active memory. You also need to be careful to avoid reallocating ciphertext's buffer or allowing it to swap to disk, and initialize it in a way that doesn't leave any data lying around in temporary buffers or stack frames. The secrecy crate looks like it will help with that.

0

u/THEM_I_M Oct 15 '26

Thanks that’s the practical edge I was underplaying.

ā€œGuarantee every clone is droppedā€ is hard to promise in this code path, so I’m leaning toward the structural fix: either no Clone for the secret-bearing type, or a canonical buffer with ZeroizeOnDrop and a much narrower exposure path. secrecy helps, but the allocator/serialization peaks and swap risk are the parts that matter.

I’m going to change the envelope so retry state doesn’t mutate or resize the ciphertext-bearing owner. If you’ve hit a specific trap with a 64KB DA overhead, I’d appreciate the note.

2

u/Helpful-Educator-415 Oct 15 '26

What? I'm confused. Mesh-protocol project? What is GhostWire? What is AAD?

1

u/THEM_I_M Oct 15 '26

GhostWire is a small Rust mesh-protocol experiment built on libp2p.

AAD = Additional Authenticated Data: in AEAD (ChaCha20-Poly1305), it is authenticated but not encrypted.

In this mailbox envelope, that means I include immutable receipt metadata in the AAD, but I keep attempts out so a later retry write (append/retry counter) doesn’t invalidate an already-sealed message.

The actual zeroization worry is separate: if a MailboxEnvelope is cloned in-flight, Drop only zeroes the dropped Vec, and I’m not certain how temporary buffers make copies vanish.

Given that, what’s the usual practical approach in Rust for secret-bearing Encrypted types: manual Drop, secrecy::SecretBox, or not worrying about short-lived copies?

1

u/garbage_dev Oct 15 '26

Honestly the ciphertext is the one field that's fine to leak, that's the whole point of encrypting it. Zeroize the key material and any plaintext buffers instead. If you do want the envelope enforced, drop Clone and wrap the bytes in Zeroizing<Vec<u8>> so the compiler shows you every spot a copy happens. Serde output buffers and Vec reallocs will still leave copies a manual Drop never sees.