diff options
author | metamuffin <metamuffin@disroot.org> | 2022-09-13 17:18:12 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-09-13 17:18:12 +0200 |
commit | 7e88c11cbf817cb6cdbaf9b9f55564c4f9081e26 (patch) | |
tree | 23c151e7cc7a6650f34b1db6b6ce94f3871ea3f7 | |
parent | 1b7116fedcfaef6410e3ff48ea16e893d8bb5989 (diff) | |
download | keks-meet-7e88c11cbf817cb6cdbaf9b9f55564c4f9081e26.tar keks-meet-7e88c11cbf817cb6cdbaf9b9f55564c4f9081e26.tar.bz2 keks-meet-7e88c11cbf817cb6cdbaf9b9f55564c4f9081e26.tar.zst |
port crypto to rust
-rw-r--r-- | .gitignore (renamed from server/.gitignore) | 0 | ||||
-rw-r--r-- | Cargo.lock (renamed from server/Cargo.lock) | 202 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | client-native-rift/Cargo.toml | 20 | ||||
-rw-r--r-- | client-native-rift/src/crypto.rs | 32 | ||||
-rw-r--r-- | client-native-rift/src/main.rs | 6 | ||||
-rw-r--r-- | client-native-rift/src/protocol.rs | 20 |
7 files changed, 247 insertions, 35 deletions
diff --git a/server/.gitignore b/.gitignore index ea8c4bf..ea8c4bf 100644 --- a/server/.gitignore +++ b/.gitignore diff --git a/server/Cargo.lock b/Cargo.lock index bf62539..580fac7 100644 --- a/server/Cargo.lock +++ b/Cargo.lock @@ -3,6 +3,41 @@ version = 3 [[package]] +name = "aead" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c192eb8f11fc081b0fe4259ba5af04217d4e0faddd02417310a927911abd7c8" +dependencies = [ + "crypto-common", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfe0133578c0986e1fe3dfcd4af1cc5b2dd6c3dbf534d69916ce16a2701d40ba" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "aes-gcm" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e1366e0c69c9f927b1fa5ce2c7bf9eafc8f9268c0b9800729e8b267612447c" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + +[[package]] name = "aho-corasick" version = "0.7.19" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -18,7 +53,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ "hermit-abi", - "libc", + "libc 0.2.132", "winapi", ] @@ -87,12 +122,39 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] +name = "cipher" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1873270f8f7942c191139cb8a40fd228da6c3fd2fc376d7e92d47aa14aeb59e" +dependencies = [ + "crypto-common", + "inout", +] + +[[package]] +name = "client-native-rift" +version = "0.1.0" +dependencies = [ + "aes-gcm", + "base64", + "env_logger", + "fastpbkdf2", + "log", + "rand", + "rand_chacha", + "serde", + "serde_json", + "tokio", + "tokio-tungstenite", +] + +[[package]] name = "cpufeatures" version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320" dependencies = [ - "libc", + "libc 0.2.132", ] [[package]] @@ -102,10 +164,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", + "rand_core", "typenum", ] [[package]] +name = "ctr" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d14f329cfbaf5d0e06b5e87fff7e265d2673c5ea7d2c27691a2c107db1442a0" +dependencies = [ + "cipher", +] + +[[package]] name = "digest" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -138,6 +210,16 @@ dependencies = [ ] [[package]] +name = "fastpbkdf2" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25415467e1580ab15d51915f1be91b12f4bdce14e2184d123de6bfbea8ca186e" +dependencies = [ + "gcc", + "libc 0.1.12", +] + +[[package]] name = "fastrand" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -154,11 +236,10 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "form_urlencoded" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" +checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" dependencies = [ - "matches", "percent-encoding", ] @@ -217,6 +298,12 @@ dependencies = [ ] [[package]] +name = "gcc" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" + +[[package]] name = "generic-array" version = "0.14.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -233,11 +320,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" dependencies = [ "cfg-if", - "libc", + "libc 0.2.132", "wasi", ] [[package]] +name = "ghash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d930750de5717d2dd0b8c0d42c076c0e884c81a73e6cab859bbd2339c71e3e40" +dependencies = [ + "opaque-debug", + "polyval", +] + +[[package]] name = "h2" version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -252,7 +349,7 @@ dependencies = [ "indexmap", "slab", "tokio", - "tokio-util 0.7.3", + "tokio-util 0.7.4", "tracing", ] @@ -293,7 +390,7 @@ version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ - "libc", + "libc 0.2.132", ] [[package]] @@ -362,11 +459,10 @@ dependencies = [ [[package]] name = "idna" -version = "0.2.3" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8" +checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6" dependencies = [ - "matches", "unicode-bidi", "unicode-normalization", ] @@ -382,6 +478,15 @@ dependencies = [ ] [[package]] +name = "inout" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" +dependencies = [ + "generic-array", +] + +[[package]] name = "instant" version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -413,6 +518,12 @@ dependencies = [ [[package]] name = "libc" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e32a70cf75e5846d53a673923498228bbec6a8624708a9ea5645f075d6276122" + +[[package]] +name = "libc" version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" @@ -423,7 +534,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14e4fcc00ff6731d94b70e16e71f43bda62883461f31230742e3bc6dddf12988" dependencies = [ - "libc", + "libc 0.2.132", "uuid", "winapi", ] @@ -448,12 +559,6 @@ dependencies = [ ] [[package]] -name = "matches" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" - -[[package]] name = "memchr" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -481,7 +586,7 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf" dependencies = [ - "libc", + "libc 0.2.132", "log", "wasi", "windows-sys", @@ -512,7 +617,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" dependencies = [ "hermit-abi", - "libc", + "libc 0.2.132", ] [[package]] @@ -544,7 +649,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" dependencies = [ "cfg-if", - "libc", + "libc 0.2.132", "redox_syscall", "smallvec", "windows-sys", @@ -552,9 +657,9 @@ dependencies = [ [[package]] name = "percent-encoding" -version = "2.1.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" [[package]] name = "pin-project" @@ -589,6 +694,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] +name = "polyval" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef234e08c11dfcb2e56f79fd70f6f2eb7f025c0ce2333e82f4f0518ecad30c6" +dependencies = [ + "cfg-if", + "cpufeatures", + "opaque-debug", + "universal-hash", +] + +[[package]] name = "ppv-lite86" version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -624,7 +741,7 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ - "libc", + "libc 0.2.132", "rand_chacha", "rand_core", ] @@ -780,7 +897,7 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" dependencies = [ - "libc", + "libc 0.2.132", ] [[package]] @@ -804,11 +921,17 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02e2d2db9033d13a1567121ddd7a095ee144db4e1ca1b1bda3419bc0da294ebd" dependencies = [ - "libc", + "libc 0.2.132", "winapi", ] [[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + +[[package]] name = "syn" version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -827,7 +950,7 @@ checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" dependencies = [ "cfg-if", "fastrand", - "libc", + "libc 0.2.132", "redox_syscall", "remove_dir_all", "winapi", @@ -879,13 +1002,13 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.21.0" +version = "1.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89797afd69d206ccd11fb0ea560a44bbb87731d020670e79416d442919257d42" +checksum = "0020c875007ad96677dcc890298f4b942882c5d4eb7cc8f439fc3bf813dc9c95" dependencies = [ "autocfg", "bytes", - "libc", + "libc 0.2.132", "memchr", "mio", "num_cpus", @@ -949,9 +1072,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc463cd8deddc3770d20f9852143d50bf6094e640b485cb2e189a2099085ff45" +checksum = "0bb2e075f03b3d66d8d8785356224ba688d2906a371015e225beeb65ca92c740" dependencies = [ "bytes", "futures-core", @@ -1059,14 +1182,23 @@ dependencies = [ ] [[package]] +name = "universal-hash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d3160b73c9a19f7e2939a2fdad446c57c1bbbbf4d919d3213ff1267a580d8b5" +dependencies = [ + "crypto-common", + "subtle", +] + +[[package]] name = "url" -version = "2.2.2" +version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c" +checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" dependencies = [ "form_urlencoded", "idna", - "matches", "percent-encoding", ] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..fb9f30e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,2 @@ +[workspace] +members = ["server", "client-native-rift"] diff --git a/client-native-rift/Cargo.toml b/client-native-rift/Cargo.toml new file mode 100644 index 0000000..a9324e7 --- /dev/null +++ b/client-native-rift/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "client-native-rift" +version = "0.1.0" +edition = "2021" + +[dependencies] +tokio = { version = "1.21", features = ["full"] } +tokio-tungstenite = "*" + +serde = { version = "1.0.144", features = ["derive"] } +serde_json = "*" + +env_logger = "0.8" +log = "0.4" + +fastpbkdf2 = "0.1.0" +aes-gcm = "0.10.1" +base64 = "0.13.0" +rand = "0.8.5" +rand_chacha = "0.3.1" diff --git a/client-native-rift/src/crypto.rs b/client-native-rift/src/crypto.rs new file mode 100644 index 0000000..1144de0 --- /dev/null +++ b/client-native-rift/src/crypto.rs @@ -0,0 +1,32 @@ +use aes_gcm::{ + aead::{generic_array::sequence::GenericSequence, Aead}, + Aes256Gcm, KeyInit, Nonce, +}; + +pub struct Key(Aes256Gcm); + +impl Key { + pub fn derive(secret: String) -> Self { + let salt = base64::decode("").unwrap(); + let mut key = [0u8; 256]; + fastpbkdf2::pbkdf2_hmac_sha256(secret.as_bytes(), salt.as_slice(), 250000, &mut key); + + let key = Aes256Gcm::new_from_slice(key.as_slice()).unwrap(); + + Self(key) + } + pub fn encrypt(&self, s: &str) -> String { + let iv = Nonce::generate(|_| rand::random()); // TODO check if this is secure randomness + let ciphertext = self.0.encrypt(&iv, s.as_bytes()).unwrap(); + let mut packet = iv.to_vec(); // TODO this could be doing less allocations + packet.extend(ciphertext); + base64::encode(packet) + } + pub fn decrypt(&self, s: &str) -> String { + let r = base64::decode(s).unwrap(); + let iv = &r[0..12]; + let ciphertext = &r[12..]; + let plaintext = self.0.decrypt(Nonce::from_slice(iv), ciphertext).unwrap(); + String::from_utf8(plaintext).unwrap() + } +} diff --git a/client-native-rift/src/main.rs b/client-native-rift/src/main.rs new file mode 100644 index 0000000..028188e --- /dev/null +++ b/client-native-rift/src/main.rs @@ -0,0 +1,6 @@ +pub mod crypto; +pub mod protocol; + +fn main() { + +} diff --git a/client-native-rift/src/protocol.rs b/client-native-rift/src/protocol.rs new file mode 100644 index 0000000..5fb1ecb --- /dev/null +++ b/client-native-rift/src/protocol.rs @@ -0,0 +1,20 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ClientboundPacket { + Init { your_id: usize, version: String }, + ClientJoin { id: usize }, + ClientLeave { id: usize }, + Message { sender: usize, message: String }, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ServerboundPacket { + Ping, + Relay { + recipient: Option<usize>, + message: String, + }, +} |