From 5d9779f8dd8b548fa0f69441b11e8fd541bc5190 Mon Sep 17 00:00:00 2001 From: Sachymetsu Date: Sun, 26 Jul 2026 12:16:23 +0200 Subject: [PATCH] Some code tidy-up --- Cargo.lock | 231 ---------------------------- Cargo.toml | 2 +- src/main.rs | 2 +- water-pod-common/Cargo.toml | 3 - water-pod-common/src/accumulator.rs | 42 +++++ water-pod-common/src/encryption.rs | 54 +++++++ water-pod-common/src/lib.rs | 139 ++--------------- water-pod-common/src/utils.rs | 32 ++++ 8 files changed, 141 insertions(+), 364 deletions(-) create mode 100644 water-pod-common/src/accumulator.rs create mode 100644 water-pod-common/src/encryption.rs create mode 100644 water-pod-common/src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index c4f90c7..47c9869 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,17 +83,6 @@ version = "1.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" -[[package]] -name = "bbqueue" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68917624e17aad88607cb5a5936f6da9b607c48c711e4e9ed101e7189aed28c2" -dependencies = [ - "const-init", - "critical-section", - "maitake-sync", -] - [[package]] name = "bitflags" version = "1.3.2" @@ -156,16 +145,6 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ae3f5d315924270530207e2a68396c3cc547f6dca3fbdca317cfb1a51edb593" -[[package]] -name = "cc" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c89588d05638b5b4594a3348a2d6c20277e43a7f5c5202b05cc56888475a47b8" -dependencies = [ - "find-msvc-tools", - "shlex", -] - [[package]] name = "cfg-if" version = "1.0.4" @@ -210,28 +189,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" -[[package]] -name = "const-init" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bd422bfb4f24a97243f60b6a4443e63d810c925d8da4bb2d8fde26a7c1d57ec" - [[package]] name = "const-oid" version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" -[[package]] -name = "cordyceps" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "688d7fbb8092b8de775ef2536f36c8c31f2bc4006ece2e8d8ad2d17d00ce0a2a" -dependencies = [ - "loom", - "tracing", -] - [[package]] name = "cpufeatures" version = "0.3.0" @@ -241,12 +204,6 @@ dependencies = [ "libc", ] -[[package]] -name = "critical-section" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" - [[package]] name = "crypto-common" version = "0.2.2" @@ -469,12 +426,6 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6" -[[package]] -name = "find-msvc-tools" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" - [[package]] name = "fixedbitset" version = "0.5.7" @@ -601,21 +552,6 @@ dependencies = [ "slab", ] -[[package]] -name = "generator" -version = "0.8.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3b854b0e584ead1a33f18b2fcad7cf7be18b3875c78816b753639aa501513ae" -dependencies = [ - "cc", - "cfg-if", - "libc", - "log", - "rustversion", - "windows-link", - "windows-result", -] - [[package]] name = "getrandom" version = "0.4.3" @@ -778,50 +714,12 @@ version = "0.4.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad" -[[package]] -name = "loom" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "419e0dc8046cb947daa77eb95ae174acfbddb7673b4151f56d1eed8e93fbfaca" -dependencies = [ - "cfg-if", - "generator", - "scoped-tls", - "tracing", - "tracing-subscriber", -] - [[package]] name = "macaddr" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baee0bbc17ce759db233beb01648088061bf678383130602a298e6998eedb2d8" -[[package]] -name = "maitake-sync" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d77c365d697828821727b9bc09e6bc3c518b8c63804e79e1be5a5ae091a7c5f" -dependencies = [ - "cordyceps", - "critical-section", - "loom", - "mutex-traits", - "mycelium-bitfield", - "pin-project", - "portable-atomic", - "tracing", -] - -[[package]] -name = "matchers" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" -dependencies = [ - "regex-automata", -] - [[package]] name = "memchr" version = "2.8.3" @@ -866,18 +764,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "mutex-traits" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3929f2b5633d29cf7b6624992e5f3c1e9334f1193423e12d17be4faf678cde3f" - -[[package]] -name = "mycelium-bitfield" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24e0cc5e2c585acbd15c5ce911dff71e1f4d5313f43345873311c4f5efd741cc" - [[package]] name = "nix" version = "0.29.0" @@ -890,15 +776,6 @@ dependencies = [ "libc", ] -[[package]] -name = "nu-ansi-term" -version = "0.50.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" -dependencies = [ - "windows-sys 0.61.2", -] - [[package]] name = "num-derive" version = "0.4.2" @@ -1104,12 +981,6 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" -[[package]] -name = "scoped-tls" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" - [[package]] name = "serde" version = "1.0.228" @@ -1163,21 +1034,6 @@ dependencies = [ "keccak", ] -[[package]] -name = "sharded-slab" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" -dependencies = [ - "lazy_static", -] - -[[package]] -name = "shlex" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" - [[package]] name = "signal-hook-registry" version = "1.4.8" @@ -1290,15 +1146,6 @@ dependencies = [ "syn", ] -[[package]] -name = "thread_local" -version = "1.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" -dependencies = [ - "cfg-if", -] - [[package]] name = "tokio" version = "1.52.3" @@ -1337,67 +1184,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "tracing" -version = "0.1.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" -dependencies = [ - "pin-project-lite", - "tracing-attributes", - "tracing-core", -] - -[[package]] -name = "tracing-attributes" -version = "0.1.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "tracing-core" -version = "0.1.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" -dependencies = [ - "once_cell", - "valuable", -] - -[[package]] -name = "tracing-log" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" -dependencies = [ - "log", - "once_cell", - "tracing-core", -] - -[[package]] -name = "tracing-subscriber" -version = "0.3.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" -dependencies = [ - "matchers", - "nu-ansi-term", - "once_cell", - "regex-automata", - "sharded-slab", - "smallvec", - "thread_local", - "tracing", - "tracing-core", - "tracing-log", -] - [[package]] name = "typenum" version = "1.20.1" @@ -1428,12 +1214,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "valuable" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" - [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" @@ -1511,10 +1291,8 @@ name = "water-pod-common" version = "0.1.0" dependencies = [ "aead", - "bbqueue", "defmt", "jiff", - "log", "postcard", "postcard-schema", "rand_core", @@ -1568,15 +1346,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" -[[package]] -name = "windows-result" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" -dependencies = [ - "windows-link", -] - [[package]] name = "windows-sys" version = "0.59.0" diff --git a/Cargo.toml b/Cargo.toml index a619b2c..55a827c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ edition.workspace = true [dependencies] aead = { version = "0.6.0", features = ["alloc"] } -water-pod-common = { path = "./water-pod-common", features = ["log"] } +water-pod-common = { path = "./water-pod-common" } tokio = { version = "1", features = ["io-std", "io-util", "net", "rt", "macros", "signal", "sync"] } wharrgarbl = { git = "https://tangled.org/sachy.dev/wharrgarbl", package = "wharrgarbl" } getrandom = { version = "0.4.2", features = ["sys_rng"] } diff --git a/src/main.rs b/src/main.rs index 4310848..7fbbdf6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,7 +113,7 @@ async fn req_handler<'a>( send_state: &mut SendState<'a, Neko128>, recv_state: &mut RecvState<'a, Neko128>, ) -> aead::Result> { - let request = request.from_bytes(recv_state)?; + let request = request.unseal(recv_state)?; match request { Request::TimeSync(mut sync) => { diff --git a/water-pod-common/Cargo.toml b/water-pod-common/Cargo.toml index 110408a..430db7e 100644 --- a/water-pod-common/Cargo.toml +++ b/water-pod-common/Cargo.toml @@ -13,12 +13,9 @@ defmt = { version = "1.0", optional = true } wharrgarbl = { git = "https://tangled.org/sachy.dev/wharrgarbl", package = "wharrgarbl" } rand_core = { version = "0.10", default-features = false } tokio = { version = "1", default-features = false, optional = true } -bbqueue = "0.7.0" postcard-schema = { version = "0.2.5", features = ["alloc", "derive"] } -log = { version = "0.4", optional = true } [features] default = ["server"] server = ["jiff/std", "dep:tokio"] defmt = ["dep:defmt", "postcard/use-defmt", "jiff/defmt"] -log = ["dep:log"] diff --git a/water-pod-common/src/accumulator.rs b/water-pod-common/src/accumulator.rs new file mode 100644 index 0000000..b0b1bc4 --- /dev/null +++ b/water-pod-common/src/accumulator.rs @@ -0,0 +1,42 @@ +use alloc::boxed::Box; +use postcard::accumulator::{CobsAccumulator, FeedResult}; + +pub struct Accumulator { + cobs: Box>, +} + +impl Accumulator { + pub fn new() -> Self { + let cobs = Box::>::new_zeroed(); + + Self { + // SAFETY: Zeroed CobsAccumulator is fine. + cobs: unsafe { cobs.assume_init() }, + } + } + + pub fn accumulate(&mut self, mut input: &[u8]) -> AccumulatorResult + where + T: for<'de> serde::Deserialize<'de>, + { + if input.is_empty() { + return AccumulatorResult::Error; + } + + loop { + input = match self.cobs.feed::(input) { + FeedResult::Consumed => return AccumulatorResult::Continue, + FeedResult::OverFull(items) => items, + FeedResult::DeserError(items) => items, + FeedResult::Success { data, .. } => return AccumulatorResult::Success(data), + }; + } + } +} + +#[derive(Debug, PartialEq, Eq)] +pub enum AccumulatorResult { + Continue, + Error, + Success(T), +} diff --git a/water-pod-common/src/encryption.rs b/water-pod-common/src/encryption.rs new file mode 100644 index 0000000..67827f9 --- /dev/null +++ b/water-pod-common/src/encryption.rs @@ -0,0 +1,54 @@ +use alloc::vec::Vec; +use wharrgarbl::{ + Neko128, + transport::{RecvState, SendState}, +}; + +#[derive(Debug, serde::Serialize, serde::Deserialize, postcard_schema::Schema)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[repr(transparent)] +pub struct Pairing( + #[serde( + serialize_with = "crate::utils::serialize_bytes", + deserialize_with = "crate::utils::deserialize_bytes" + )] + pub Vec, +); + +impl Pairing { + pub fn to_bytes(self) -> Result, postcard::Error> { + postcard::to_allocvec_cobs(&self) + } +} + +#[derive(Debug, serde::Serialize, serde::Deserialize, postcard_schema::Schema)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[serde(bound = "T: serde::Serialize + for<'a> serde::Deserialize<'a>")] +pub struct Encrypted { + #[serde( + serialize_with = "crate::utils::serialize_bytes", + deserialize_with = "crate::utils::deserialize_bytes" + )] + pub payload: Vec, + _kind: core::marker::PhantomData, +} + +impl serde::Deserialize<'de>> Encrypted { + pub fn new(payload: T) -> Self { + let buf = Vec::new(); + Self { + payload: postcard::to_extend(&payload, buf).unwrap(), + _kind: core::marker::PhantomData, + } + } + + pub fn to_bytes<'b>(mut self, send: &mut SendState<'b, Neko128>) -> aead::Result> { + send.encrypt(&mut self.payload, b"water-pod")?; + postcard::to_allocvec_cobs(&self).map_err(|_| aead::Error) + } + + pub fn unseal<'b>(mut self, recv: &mut RecvState<'b, Neko128>) -> aead::Result { + recv.decrypt(&mut self.payload, b"water-pod")?; + postcard::from_bytes(&self.payload).map_err(|_| aead::Error) + } +} diff --git a/water-pod-common/src/lib.rs b/water-pod-common/src/lib.rs index 1510f60..6047cc5 100644 --- a/water-pod-common/src/lib.rs +++ b/water-pod-common/src/lib.rs @@ -1,17 +1,21 @@ #![no_std] +mod accumulator; +mod encryption; +mod utils; + extern crate alloc; -use alloc::{boxed::Box, vec::Vec}; +use alloc::vec::Vec; use jiff::{SignedDuration, Timestamp}; -use postcard::accumulator::{CobsAccumulator, FeedResult}; use rand_core::CryptoRng; -use serde::{Deserialize, Serialize}; use wharrgarbl::{ - Neko128, NekoClientHandshake128, NekoServerHandshake128, - transport::{AeadTransport, RecvState, SendState}, + Neko128, NekoClientHandshake128, NekoServerHandshake128, transport::AeadTransport, }; +pub use accumulator::{Accumulator, AccumulatorResult}; +pub use encryption::{Encrypted, Pairing}; + #[derive(Debug, serde::Serialize, serde::Deserialize, postcard_schema::Schema)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum Request { @@ -27,87 +31,6 @@ pub enum Response { Error, } -#[derive(Debug, serde::Serialize, serde::Deserialize, postcard_schema::Schema)] -#[cfg_attr(feature = "defmt", derive(defmt::Format))] -#[repr(transparent)] -pub struct Pairing( - #[serde( - serialize_with = "serialize_bytes", - deserialize_with = "deserialize_bytes" - )] - pub Vec, -); - -impl Pairing { - pub fn to_bytes(self) -> Result, postcard::Error> { - postcard::to_allocvec_cobs(&self) - } -} - -#[derive(Debug, serde::Serialize, serde::Deserialize, postcard_schema::Schema)] -#[cfg_attr(feature = "defmt", derive(defmt::Format))] -#[serde(bound = "T: Serialize + for<'a> Deserialize<'a>")] -pub struct Encrypted { - #[serde( - serialize_with = "serialize_bytes", - deserialize_with = "deserialize_bytes" - )] - pub payload: Vec, - _kind: core::marker::PhantomData, -} - -fn serialize_bytes(value: &[u8], serializer: S) -> Result -where - S: serde::Serializer, -{ - serializer.serialize_bytes(value) -} - -fn deserialize_bytes<'de, D>(deserializer: D) -> Result, D::Error> -where - D: serde::Deserializer<'de>, -{ - struct ByteArrayVisitor; - - impl<'de> serde::de::Visitor<'de> for ByteArrayVisitor { - type Value = Vec; - - fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { - write!(formatter, "Expected an sequence of bytes") - } - - fn visit_bytes(self, v: &[u8]) -> Result - where - E: serde::de::Error, - { - v.try_into() - .map_err(|_| serde::de::Error::invalid_length(v.len(), &self)) - } - } - - deserializer.deserialize_bytes(ByteArrayVisitor) -} - -impl Deserialize<'de>> Encrypted { - pub fn new(payload: T) -> Self { - let buf = Vec::new(); - Self { - payload: postcard::to_extend(&payload, buf).unwrap(), - _kind: core::marker::PhantomData, - } - } - - pub fn to_bytes<'b>(mut self, send: &mut SendState<'b, Neko128>) -> aead::Result> { - send.encrypt(&mut self.payload, b"water-pod")?; - postcard::to_allocvec_cobs(&self).map_err(|_| aead::Error) - } - - pub fn from_bytes<'b>(mut self, recv: &mut RecvState<'b, Neko128>) -> aead::Result { - recv.decrypt(&mut self.payload, b"water-pod")?; - postcard::from_bytes(&self.payload).map_err(|_| aead::Error) - } -} - impl Request { pub fn request_pairing( client: &mut NekoClientHandshake128, @@ -120,9 +43,9 @@ impl Request { pub fn finish_pairing( mut client: NekoClientHandshake128, - ciphertext: &[u8], + ciphertext: Pairing, ) -> aead::Result> { - client.receive(ciphertext) + client.receive(&ciphertext.0) } } @@ -196,43 +119,3 @@ pub struct Command { pub time: i64, pub duration: u8, } - -pub struct Accumulator { - cobs: Box>, -} - -impl Accumulator { - pub fn new() -> Self { - let cobs = Box::>::new_zeroed(); - - Self { - // SAFETY: Zeroed CobsAccumulator is fine. - cobs: unsafe { cobs.assume_init() }, - } - } - - pub fn accumulate(&mut self, mut input: &[u8]) -> AccumulatorResult - where - T: for<'de> Deserialize<'de>, - { - if input.is_empty() { - return AccumulatorResult::Error; - } - - loop { - input = match self.cobs.feed::(input) { - FeedResult::Consumed => return AccumulatorResult::Continue, - FeedResult::OverFull(items) => items, - FeedResult::DeserError(items) => items, - FeedResult::Success { data, .. } => return AccumulatorResult::Success(data), - }; - } - } -} - -#[derive(Debug, PartialEq, Eq)] -pub enum AccumulatorResult { - Continue, - Error, - Success(T), -} diff --git a/water-pod-common/src/utils.rs b/water-pod-common/src/utils.rs new file mode 100644 index 0000000..88a27fb --- /dev/null +++ b/water-pod-common/src/utils.rs @@ -0,0 +1,32 @@ +use alloc::vec::Vec; + +pub fn serialize_bytes(value: &[u8], serializer: S) -> Result +where + S: serde::Serializer, +{ + serializer.serialize_bytes(value) +} + +pub fn deserialize_bytes<'de, D>(deserializer: D) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + struct ByteArrayVisitor; + + impl<'de> serde::de::Visitor<'de> for ByteArrayVisitor { + type Value = Vec; + + fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { + write!(formatter, "Expected an sequence of bytes") + } + + fn visit_bytes(self, v: &[u8]) -> Result + where + E: serde::de::Error, + { + Ok(v.to_vec()) + } + } + + deserializer.deserialize_bytes(ByteArrayVisitor) +} -- 2.51.2