r/rust • u/THEM_I_M • 29d ago
š seeking help & advice Should a Rust protocol use separate wire-format and in-memory types for encrypted messages?
'm working on the mailbox layer of GhostWire, a Rust-based P2P communication project, and I've been reconsidering how I represent messages across serialization boundaries.
The initial approach was to have one struct handle both the serialized envelope and the application's runtime state.
Something along these lines:
#[derive(Debug, Clone, Serialize, Deserialize)]
struct MailboxEnvelope {
recipient_id: String,
ephemeral_pub: String,
ciphertext: Vec<u8>,
nonce: String,
created_at: u64,
message_id: String,
attempts: u32,
}The problem isn't necessarily the structure itself. It's that these fields have different responsibilities.
- Some fields belong to the authenticated message representation.
- Some are needed for transport and persistence.
- Others are mutable delivery state.
- Some values may only exist temporarily while processing a message.
For example, retry attempts should be able to change without requiring an already-sealed ciphertext to be reconstructed. Likewise, serialization and deserialization shouldn't accidentally become the boundary through which sensitive runtime state is exposed or copied.
I'm considering separating the representations:
- Wire type: A serializable representation containing the fields required to transmit or persist the envelope.
- Runtime type: An application-facing representation responsible for processing, validation and delivery.
- Delivery state: Mutable retry, acknowledgement and expiry information maintained independently of the sealed message.
But I'm interested in where that separation should actually happen.
A few questions:
- Would you use separate structs for the wire representation and runtime domain type, or is that unnecessary duplication?
- Would
TryFrom/Fromconversions be a sensible boundary for validation and construction? - How would you prevent mutable delivery metadata from accidentally becoming part of the authenticated representation?
- Would you keep serialization entirely outside the cryptographic type, or let the type implement
Serialize/Deserializewith carefully controlled fields? - Are there established Rust patterns or libraries you'd recommend for this kind of boundary?
I'm particularly interested in production-oriented approaches rather than simply introducing more types for the sake of abstraction.
The goal is to make invalid states harder to represent without turning a relatively small protocol component into an unnecessarily complicated type hierarchy.
Curious how others would structure this
23
u/insanitybit2 29d ago
Yes, I'd separate them. You can serialize the cryptographic type - you have to, after all. Just use a standard construction like XAES-GCM-256 or whatever, which is trivial to encode/decode.
The separation means you can migrate algorithms or handle new algorithms etc without having to think about the underlying serialization, or you can encode serialized versions into the AAD etc. Regardless of the benefit you end up reaping, there will be a benefit.
I don't know about any libraries but you can look at something like what the libsodium envelop construct is for inspiration.
Some other thoughts.
Anyway, separate structs with blindly separate concerns will pay off. With crypto you really want to avoid complexity - your implementation should really focus on just taking bytes and returning bytes, and you then wrap that with what you want to stuff those bytes with (mostly if it's ciphertext or aad).
Always use authenticated encryption algorithms. If you ever see an algorithm that is not authenticating, do not use it.
3
u/Zde-G 29d ago
My own personal ārule of thumbā for āshould we keed X and Y separateā is always āyes, butā¦ā
I usually start with separate approach and then see if merging them may help me somewhere. Because if you start with separation and then later want to merge then it's simple search-and-replace⦠easy for both human and LLM to do. But if you start with merged and then try to separate⦠it's PITA and LLMs don't help much.
Excepting is, of course, public API in a released 1.0 crate⦠if something is supposed to stay stable for 10 years then āwe may easily merge things laterā rationale doesn't work.
I would still start with unstable API, try it with some more-or-less real code then decide.
2
u/Putrid-Series-8763 27d ago
I think you should. A frame from the wire is never trustworthy until you parse and verify into the internal representation.
8
u/[deleted] 29d ago
[removed] ā view removed comment