vector_core/transfer/
wire.rs1use serde::{Deserialize, Serialize};
5
6pub const VERSION: u8 = 1;
7
8#[derive(Clone, PartialEq, Eq, Debug)]
9pub enum Msg {
10 Commit([u8; 32]),
12 Pake(Vec<u8>),
14 Reveal { pake: Vec<u8>, hello: Vec<u8> },
16 Hello(Vec<u8>),
18 Bundle { kem: Vec<u8>, sealed: Vec<u8> },
20 Deny(Vec<u8>),
22 Ack(Vec<u8>),
24 Abort(String),
26}
27
28#[derive(Debug, PartialEq, Eq)]
29pub enum DecodeError {
30 Version(u8),
32 Malformed,
33}
34
35#[derive(Serialize, Deserialize, Default)]
36struct Wire {
37 v: u8,
38 t: String,
39 #[serde(default, skip_serializing_if = "Option::is_none")]
40 h: Option<String>,
41 #[serde(default, skip_serializing_if = "Option::is_none")]
42 m: Option<String>,
43 #[serde(default, skip_serializing_if = "Option::is_none")]
44 e: Option<String>,
45 #[serde(default, skip_serializing_if = "Option::is_none")]
46 r: Option<String>,
47}
48
49fn b64(bytes: &[u8]) -> Option<String> {
50 Some(crate::simd::base64::STANDARD.encode_to_string(bytes))
51}
52
53fn unb64(field: &Option<String>) -> Result<Vec<u8>, DecodeError> {
54 let text = field.as_deref().ok_or(DecodeError::Malformed)?;
55 crate::simd::base64::STANDARD.decode_to_vec(text).map_err(|_| DecodeError::Malformed)
56}
57
58impl Msg {
59 pub fn kind(&self) -> &'static str {
60 match self {
61 Msg::Commit(_) => "commit",
62 Msg::Pake(_) => "pake",
63 Msg::Reveal { .. } => "reveal",
64 Msg::Hello(_) => "hello",
65 Msg::Bundle { .. } => "bundle",
66 Msg::Deny(_) => "deny",
67 Msg::Ack(_) => "ack",
68 Msg::Abort(_) => "abort",
69 }
70 }
71
72 pub fn encode(&self) -> String {
73 let mut w = Wire { v: VERSION, t: self.kind().to_string(), ..Default::default() };
74 match self {
75 Msg::Commit(h) => w.h = b64(h),
76 Msg::Pake(m) => w.m = b64(m),
77 Msg::Reveal { pake, hello } => {
78 w.m = b64(pake);
79 w.e = b64(hello);
80 }
81 Msg::Bundle { kem, sealed } => {
82 w.m = b64(kem);
83 w.e = b64(sealed);
84 }
85 Msg::Hello(e) | Msg::Deny(e) | Msg::Ack(e) => w.e = b64(e),
86 Msg::Abort(reason) => w.r = Some(reason.clone()),
87 }
88 serde_json::to_string(&w).expect("plain struct")
89 }
90
91 pub fn decode(content: &str) -> Result<Self, DecodeError> {
92 let w: Wire = serde_json::from_str(content).map_err(|_| DecodeError::Malformed)?;
93 if w.v != VERSION {
94 return Err(DecodeError::Version(w.v));
95 }
96 Ok(match w.t.as_str() {
97 "commit" => Msg::Commit(unb64(&w.h)?.try_into().map_err(|_| DecodeError::Malformed)?),
98 "pake" => Msg::Pake(unb64(&w.m)?),
99 "reveal" => Msg::Reveal { pake: unb64(&w.m)?, hello: unb64(&w.e)? },
100 "hello" => Msg::Hello(unb64(&w.e)?),
101 "bundle" => Msg::Bundle { kem: unb64(&w.m)?, sealed: unb64(&w.e)? },
102 "deny" => Msg::Deny(unb64(&w.e)?),
103 "ack" => Msg::Ack(unb64(&w.e)?),
104 "abort" => Msg::Abort(w.r.unwrap_or_default().chars().take(64).collect()),
105 _ => return Err(DecodeError::Malformed),
106 })
107 }
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113
114 #[test]
115 fn every_message_round_trips() {
116 for msg in [
117 Msg::Commit([7; 32]),
118 Msg::Pake(vec![0x53; 33]),
119 Msg::Reveal { pake: vec![1; 33], hello: vec![2; 40] },
120 Msg::Hello(vec![3; 20]),
121 Msg::Bundle { kem: vec![4; 1568], sealed: vec![5; 200] },
122 Msg::Deny(vec![5; 16]),
123 Msg::Ack(vec![6; 16]),
124 Msg::Abort("mismatch".into()),
125 ] {
126 assert_eq!(Msg::decode(&msg.encode()).unwrap(), msg);
127 }
128 }
129
130 #[test]
131 fn another_version_or_junk_is_told_apart() {
132 assert_eq!(Msg::decode(r#"{"v":2,"t":"pake","m":"AA=="}"#), Err(DecodeError::Version(2)));
133 for junk in ["", "{}", r#"{"v":1,"t":"pake"}"#, r#"{"v":1,"t":"commit","h":"AAAA"}"#, r#"{"v":1,"t":"nope"}"#] {
134 assert_eq!(Msg::decode(junk), Err(DecodeError::Malformed), "{junk}");
135 }
136 }
137}