Bevy+Ratutui powered Monitoring of Pico-Strike devices
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactor proto crate to use encryption crate

+156 -56
+26 -24
Cargo.lock
··· 1596 1596 1597 1597 [[package]] 1598 1598 name = "hkdf" 1599 - version = "0.13.0-rc.5" 1599 + version = "0.13.0" 1600 1600 source = "registry+https://github.com/rust-lang/crates.io-index" 1601 - checksum = "cbb55385998ae66b8d2d5143c05c94b9025ab863966f0c94ce7a5fde30105092" 1601 + checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018" 1602 1602 dependencies = [ 1603 1603 "hmac", 1604 1604 ] 1605 1605 1606 1606 [[package]] 1607 1607 name = "hmac" 1608 - version = "0.13.0-rc.6" 1608 + version = "0.13.0" 1609 1609 source = "registry+https://github.com/rust-lang/crates.io-index" 1610 - checksum = "60017b071c523c9e5a55dd1253582bff6150c5e96a7e8511e419de1ab5ee97f9" 1610 + checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f" 1611 1611 dependencies = [ 1612 1612 "digest 0.11.2", 1613 1613 ] 1614 1614 1615 1615 [[package]] 1616 1616 name = "hybrid-array" 1617 - version = "0.4.8" 1617 + version = "0.4.9" 1618 1618 source = "registry+https://github.com/rust-lang/crates.io-index" 1619 - checksum = "8655f91cd07f2b9d0c24137bd650fe69617773435ee5ec83022377777ce65ef1" 1619 + checksum = "1a79f2aff40c18ab8615ddc5caa9eb5b96314aef18fe5823090f204ad988e813" 1620 1620 dependencies = [ 1621 1621 "subtle", 1622 1622 "typenum", ··· 1816 1816 1817 1817 [[package]] 1818 1818 name = "js-sys" 1819 - version = "0.3.92" 1819 + version = "0.3.93" 1820 1820 source = "registry+https://github.com/rust-lang/crates.io-index" 1821 - checksum = "cc4c90f45aa2e6eacbe8645f77fdea542ac97a494bcd117a67df9ff4d611f995" 1821 + checksum = "797146bb2677299a1eb6b7b50a890f4c361b29ef967addf5b2fa45dae1bb6d7d" 1822 1822 dependencies = [ 1823 1823 "cfg-if", 1824 1824 "futures-util", ··· 3040 3040 name = "striker-proto" 3041 3041 version = "0.1.0" 3042 3042 dependencies = [ 3043 + "dhkem", 3043 3044 "postcard", 3045 + "sachy-crypto", 3044 3046 "serde", 3045 3047 ] 3046 3048 ··· 3313 3315 "toml_datetime 1.1.0+spec-1.1.0", 3314 3316 "toml_parser", 3315 3317 "toml_writer", 3316 - "winnow 1.0.0", 3318 + "winnow 1.0.1", 3317 3319 ] 3318 3320 3319 3321 [[package]] ··· 3322 3324 source = "registry+https://github.com/rust-lang/crates.io-index" 3323 3325 checksum = "2334f11ee363607eb04df9b8fc8a13ca1715a72ba8662a26ac285c98aabb4011" 3324 3326 dependencies = [ 3325 - "winnow 1.0.0", 3327 + "winnow 1.0.1", 3326 3328 ] 3327 3329 3328 3330 [[package]] ··· 3587 3589 3588 3590 [[package]] 3589 3591 name = "wasm-bindgen" 3590 - version = "0.2.115" 3592 + version = "0.2.116" 3591 3593 source = "registry+https://github.com/rust-lang/crates.io-index" 3592 - checksum = "6523d69017b7633e396a89c5efab138161ed5aafcbc8d3e5c5a42ae38f50495a" 3594 + checksum = "7dc0882f7b5bb01ae8c5215a1230832694481c1a4be062fd410e12ea3da5b631" 3593 3595 dependencies = [ 3594 3596 "cfg-if", 3595 3597 "once_cell", ··· 3600 3602 3601 3603 [[package]] 3602 3604 name = "wasm-bindgen-futures" 3603 - version = "0.4.65" 3605 + version = "0.4.66" 3604 3606 source = "registry+https://github.com/rust-lang/crates.io-index" 3605 - checksum = "2d1faf851e778dfa54db7cd438b70758eba9755cb47403f3496edd7c8fc212f0" 3607 + checksum = "19280959e2844181895ef62f065c63e0ca07ece4771b53d89bfdb967d97cbf05" 3606 3608 dependencies = [ 3607 3609 "js-sys", 3608 3610 "wasm-bindgen", ··· 3610 3612 3611 3613 [[package]] 3612 3614 name = "wasm-bindgen-macro" 3613 - version = "0.2.115" 3615 + version = "0.2.116" 3614 3616 source = "registry+https://github.com/rust-lang/crates.io-index" 3615 - checksum = "4e3a6c758eb2f701ed3d052ff5737f5bfe6614326ea7f3bbac7156192dc32e67" 3617 + checksum = "75973d3066e01d035dbedaad2864c398df42f8dd7b1ea057c35b8407c015b537" 3616 3618 dependencies = [ 3617 3619 "quote", 3618 3620 "wasm-bindgen-macro-support", ··· 3620 3622 3621 3623 [[package]] 3622 3624 name = "wasm-bindgen-macro-support" 3623 - version = "0.2.115" 3625 + version = "0.2.116" 3624 3626 source = "registry+https://github.com/rust-lang/crates.io-index" 3625 - checksum = "921de2737904886b52bcbb237301552d05969a6f9c40d261eb0533c8b055fedf" 3627 + checksum = "91af5e4be765819e0bcfee7322c14374dc821e35e72fa663a830bbc7dc199eac" 3626 3628 dependencies = [ 3627 3629 "bumpalo", 3628 3630 "proc-macro2", ··· 3633 3635 3634 3636 [[package]] 3635 3637 name = "wasm-bindgen-shared" 3636 - version = "0.2.115" 3638 + version = "0.2.116" 3637 3639 source = "registry+https://github.com/rust-lang/crates.io-index" 3638 - checksum = "a93e946af942b58934c604527337bad9ae33ba1d5c6900bbb41c2c07c2364a93" 3640 + checksum = "c9bf0406a78f02f336bf1e451799cca198e8acde4ffa278f0fb20487b150a633" 3639 3641 dependencies = [ 3640 3642 "unicode-ident", 3641 3643 ] ··· 3676 3678 3677 3679 [[package]] 3678 3680 name = "web-sys" 3679 - version = "0.3.92" 3681 + version = "0.3.93" 3680 3682 source = "registry+https://github.com/rust-lang/crates.io-index" 3681 - checksum = "84cde8507f4d7cfcb1185b8cb5890c494ffea65edbe1ba82cfd63661c805ed94" 3683 + checksum = "749466a37ee189057f54748b200186b59a03417a117267baf3fd89cecc9fb837" 3682 3684 dependencies = [ 3683 3685 "js-sys", 3684 3686 "wasm-bindgen", ··· 3838 3840 3839 3841 [[package]] 3840 3842 name = "winnow" 3841 - version = "1.0.0" 3843 + version = "1.0.1" 3842 3844 source = "registry+https://github.com/rust-lang/crates.io-index" 3843 - checksum = "a90e88e4667264a994d34e6d1ab2d26d398dcdca8b7f52bec8668957517fc7d8" 3845 + checksum = "09dac053f1cd375980747450bfc7250c264eaae0583872e845c0c7cd578872b5" 3844 3846 dependencies = [ 3845 3847 "memchr", 3846 3848 ]
+15 -24
src/net.rs
··· 13 13 }; 14 14 15 15 use futures_concurrency::future::Race; 16 - use sachy_crypto::{ClientHandshake, TransportState}; 16 + use striker_proto::{Client, ClientBuilder}; 17 17 18 18 use crate::constants::NOISE_PSK; 19 19 ··· 70 70 let (mut send_state, mut recv_state) = transport.split(); 71 71 72 72 let read_fut = async { 73 - while let Ok(mut buh) = recv(&stream) 74 - .await 75 - && { 76 - recv_state 77 - .decrypt(&mut buh, b"pico-strike") 78 - .is_ok() 79 - } 80 - && let Ok(data) = striker_proto::receive_response(&mut buh) 73 + while let Ok(mut buh) = recv(&stream).await 74 + && let Ok(data) = recv_state.decode_response(&mut buh) 81 75 && resp_tx 82 76 .send(StrikeUpdateState::Updating(data)) 83 77 .await ··· 88 82 let write_fut = async { 89 83 let mut payload = vec![0u8; 4096]; 90 84 while let Ok(req) = req_rx.recv().await 91 - && striker_proto::send_request(req, &mut payload) 92 - .map(|written| written.len()) 93 - .map_err(|e| color_eyre::eyre::eyre!(e)) 94 - .and_then(|written| { 95 - payload.truncate(written); 96 - send_state.encrypt(&mut payload, b"pico-strike")?; 97 - Ok(()) 98 - }) 99 - .is_ok() 85 + && send_state.encode_request(req, &mut payload).is_ok() 100 86 && send(&stream, &payload).await.is_ok() 101 87 { 102 88 payload.resize_with(4096, Default::default); ··· 140 126 } 141 127 } 142 128 143 - async fn transport_handshake(tcp: &Async<TcpStream>) -> color_eyre::Result<TransportState> { 144 - let (encap, handshake) = ClientHandshake::send(); 129 + async fn transport_handshake(tcp: &Async<TcpStream>) -> color_eyre::Result<Client> { 130 + let (encap, handshake) = ClientBuilder::start(); 145 131 146 132 tcp.write_with(|mut tcp| tcp.write_all(encap.serialize().as_bytes())) 147 133 .await?; ··· 157 143 158 144 async fn recv(stream: &Async<TcpStream>) -> std::io::Result<Vec<u8>> { 159 145 let mut msg_len_buf = [0_u8; 2]; 146 + let mut msg = vec![0u8; 4096]; 160 147 161 148 stream 162 149 .read_with(|mut stream| { 163 150 stream.read_exact(&mut msg_len_buf)?; 151 + 164 152 let msg_len = usize::from(u16::from_be_bytes(msg_len_buf)); 165 - let mut msg = vec![0_u8; msg_len]; 166 - stream.read_exact(&mut msg[..])?; 153 + stream.read_exact(&mut msg[..msg_len])?; 154 + 155 + msg.truncate(msg_len); 167 156 168 - Ok(msg) 157 + Ok(()) 169 158 }) 170 - .await 159 + .await?; 160 + 161 + Ok(msg) 171 162 } 172 163 173 164 /// Hyper-basic stream transport sender. 16-bit BE size followed by payload.
+2
striker-proto/Cargo.toml
··· 9 9 [dependencies] 10 10 serde = { workspace = true, features = ["alloc"] } 11 11 postcard = { workspace = true, features = ["alloc"] } 12 + sachy-crypto = { git = "https://tangled.org/sachy.dev/sachy-embed-core" } 13 + dhkem = { version = "0.1.0-rc.0", features = ["ecdh", "k256"] }
+113 -8
striker-proto/src/lib.rs
··· 1 1 #![no_std] 2 2 3 3 use alloc::{string::String, vec::Vec}; 4 + use dhkem::{Secp256k1Kem, kem::Ciphertext}; 5 + use sachy_crypto::{ 6 + ClientHandshake, EncapsulatedPublicKey, ProtoError, ReceivingState, SendingState, 7 + ServerHandshake, TransportState, 8 + }; 4 9 5 10 extern crate alloc; 11 + 12 + static PROTO_ASSOCIATED_DATA: &[u8] = b"pico-strikez"; 6 13 7 14 #[derive(Debug, serde::Serialize, serde::Deserialize)] 8 15 pub struct StrikerRequest { ··· 52 59 }, 53 60 } 54 61 55 - pub fn send_response(response: StrikerResponse, buf: &mut [u8]) -> Result<&mut [u8], postcard::Error> { 56 - postcard::to_slice(&response, buf) 62 + pub struct ClientBuilder { 63 + handshake: ClientHandshake, 64 + } 65 + 66 + impl ClientBuilder { 67 + pub fn start() -> (EncapsulatedPublicKey, Self) { 68 + let (encap, handshake) = ClientHandshake::send(); 69 + 70 + (encap, Self { handshake }) 71 + } 72 + 73 + pub fn finish(self, ciphertext: &[u8], psk: &[u8; 32]) -> Result<Client, ProtoError> { 74 + self.handshake.finish(ciphertext, psk).map(Client) 75 + } 76 + } 77 + 78 + pub struct ServerBuilder { 79 + handshake: ServerHandshake, 80 + } 81 + 82 + impl ServerBuilder { 83 + pub fn start(encap: &[u8]) -> Result<(Ciphertext<Secp256k1Kem>, Self), ProtoError> { 84 + let (ciphertext, handshake) = ServerHandshake::receive(encap)?; 85 + 86 + Ok((ciphertext, Self { handshake })) 87 + } 88 + 89 + pub fn finish(self, psk: &[u8; 32]) -> Result<Server, ProtoError> { 90 + self.handshake.finish(psk).map(Server) 91 + } 92 + } 93 + 94 + pub struct Client(TransportState); 95 + 96 + impl Client { 97 + pub fn split(&self) -> (ClientSender<'_>, ClientReceiver<'_>) { 98 + let (tx, rx) = self.0.split(); 99 + 100 + (ClientSender(tx), ClientReceiver(rx)) 101 + } 102 + } 103 + 104 + pub struct ClientSender<'a>(SendingState<'a>); 105 + 106 + impl ClientSender<'_> { 107 + pub fn encode_request( 108 + &mut self, 109 + request: StrikerRequest, 110 + msg: &mut alloc::vec::Vec<u8>, 111 + ) -> Result<(), ProtoError> { 112 + let written = postcard::to_slice(&request, msg.as_mut_slice()) 113 + .map_err(|_| ProtoError)? 114 + .len(); 115 + 116 + msg.truncate(written); 117 + 118 + self.0.encrypt(msg, PROTO_ASSOCIATED_DATA) 119 + } 57 120 } 58 121 59 - pub fn receive_response(buf: &mut [u8]) -> Result<StrikerResponse, postcard::Error> { 60 - postcard::from_bytes(buf) 122 + pub struct ClientReceiver<'a>(ReceivingState<'a>); 123 + 124 + impl ClientReceiver<'_> { 125 + pub fn decode_response( 126 + &mut self, 127 + msg: &mut alloc::vec::Vec<u8>, 128 + ) -> Result<StrikerResponse, ProtoError> { 129 + self.0.decrypt(msg, PROTO_ASSOCIATED_DATA)?; 130 + 131 + postcard::from_bytes(msg).map_err(|_| ProtoError) 132 + } 61 133 } 62 134 63 - pub fn send_request(request: StrikerRequest, buf: &mut [u8]) -> Result<&mut [u8], postcard::Error> { 64 - postcard::to_slice(&request, buf) 135 + pub struct Server(TransportState); 136 + 137 + impl Server { 138 + pub fn split(&self) -> (ServerSender<'_>, ServerReceiver<'_>) { 139 + let (tx, rx) = self.0.split(); 140 + 141 + (ServerSender(tx), ServerReceiver(rx)) 142 + } 65 143 } 66 144 67 - pub fn receive_request(buf: &mut [u8]) -> Result<StrikerRequest, postcard::Error> { 68 - postcard::from_bytes(buf) 145 + pub struct ServerSender<'a>(SendingState<'a>); 146 + 147 + impl ServerSender<'_> { 148 + pub fn encode_response( 149 + &mut self, 150 + response: StrikerResponse, 151 + msg: &mut alloc::vec::Vec<u8>, 152 + ) -> Result<(), ProtoError> { 153 + let written = postcard::to_slice(&response, msg.as_mut_slice()) 154 + .map_err(|_| ProtoError)? 155 + .len(); 156 + 157 + msg.truncate(written); 158 + 159 + self.0.encrypt(msg, PROTO_ASSOCIATED_DATA) 160 + } 161 + } 162 + 163 + pub struct ServerReceiver<'a>(ReceivingState<'a>); 164 + 165 + impl ServerReceiver<'_> { 166 + pub fn decode_request( 167 + &mut self, 168 + msg: &mut alloc::vec::Vec<u8>, 169 + ) -> Result<StrikerRequest, ProtoError> { 170 + self.0.decrypt(msg, PROTO_ASSOCIATED_DATA)?; 171 + 172 + postcard::from_bytes(msg).map_err(|_| ProtoError) 173 + } 69 174 }