Skip to main content

po_session/
message.rs

1//! Protocol messages serialized with bincode.
2//!
3//! These are the structured payloads carried inside PO frames. The frame
4//! header tells you the type; these structs define the payload format.
5
6use serde::{Deserialize, Serialize};
7
8/// Handshake initiation message (sent by initiator).
9///
10/// Carried in a `FrameType::HandshakeInit` frame.
11#[derive(Debug, Serialize, Deserialize)]
12pub struct HandshakeInit {
13    /// Protocol version (currently 1).
14    pub version: u8,
15    /// The initiator's Ed25519 public key (32 bytes).
16    pub ed25519_pubkey: [u8; 32],
17    /// Ephemeral X25519 public key for this session (32 bytes).
18    pub x25519_ephemeral: [u8; 32],
19    /// Timestamp (Unix millis) — prevents replay attacks.
20    pub timestamp: u64,
21    /// Ed25519 signature over `[version || x25519_ephemeral || timestamp]`.
22    pub signature: Vec<u8>,
23}
24
25/// Handshake reply message (sent by responder).
26///
27/// Carried in a `FrameType::HandshakeReply` frame.
28#[derive(Debug, Serialize, Deserialize)]
29pub struct HandshakeReply {
30    /// The responder's Ed25519 public key (32 bytes).
31    pub ed25519_pubkey: [u8; 32],
32    /// Ephemeral X25519 public key for this session (32 bytes).
33    pub x25519_ephemeral: [u8; 32],
34    /// Ed25519 signature over `[initiator_x25519_pub || responder_x25519_pub]`.
35    pub signature: Vec<u8>,
36}
37
38/// Handshake completion confirmation.
39///
40/// Carried in a `FrameType::HandshakeComplete` frame.
41/// The payload is encrypted with the newly derived session key.
42#[derive(Debug, Serialize, Deserialize)]
43pub struct HandshakeComplete {
44    /// Confirmation token: "PO_READY" encrypted with the session key.
45    pub confirmation: Vec<u8>,
46}
47
48/// File transfer metadata.
49///
50/// Carried in a `FrameType::FileHeader` frame.
51#[derive(Debug, Serialize, Deserialize)]
52pub struct FileHeader {
53    /// Original filename.
54    pub name: String,
55    /// Total file size in bytes.
56    pub size: u64,
57    /// SHA-256 hash of the complete file (for integrity verification).
58    pub sha256: [u8; 32],
59    /// Chunk size used for `FileChunk` frames.
60    pub chunk_size: u32,
61}
62
63/// Generic protocol message enum for easy matching.
64#[derive(Debug, Serialize, Deserialize)]
65pub enum ProtocolMessage {
66    HandshakeInit(HandshakeInit),
67    HandshakeReply(HandshakeReply),
68    HandshakeComplete(HandshakeComplete),
69    FileHeader(FileHeader),
70    Data(Vec<u8>),
71}