diff options
Diffstat (limited to 'client-native-rift')
-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 |
4 files changed, 78 insertions, 0 deletions
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, + }, +} |