diff --git a/Cargo.lock b/Cargo.lock index 741a286..1eac0d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,17 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -204,6 +215,24 @@ version = "2.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "block-padding" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8894febbff9f758034a5b8e12d87918f56dfc64a8e1fe757d65e29041538d93" +dependencies = [ + "generic-array", +] + [[package]] name = "blocking" version = "1.6.2" @@ -223,6 +252,15 @@ version = "3.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" +[[package]] +name = "cbc" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6" +dependencies = [ + "cipher", +] + [[package]] name = "cc" version = "1.2.30" @@ -252,6 +290,16 @@ dependencies = [ "windows-link", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "clap" version = "4.5.41" @@ -313,12 +361,67 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", + "subtle", +] + +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + [[package]] name = "errno" version = "0.3.13" @@ -381,6 +484,28 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi", +] + [[package]] name = "heck" version = "0.5.0" @@ -393,6 +518,15 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + [[package]] name = "iana-time-zone" version = "0.1.63" @@ -417,6 +551,16 @@ dependencies = [ "cc", ] +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "block-padding", + "generic-array", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -575,14 +719,32 @@ version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" +[[package]] +name = "pbkdf2" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" +dependencies = [ + "digest", + "hmac", +] + [[package]] name = "pea_2_pea" version = "0.1.0" dependencies = [ + "aes", + "cbc", "chrono", + "cipher", "clap", + "hmac", "orx-concurrent-vec", + "pbkdf2", + "rand", + "rayon", "readonly", + "sha2", "smol", "tappers", ] @@ -618,6 +780,15 @@ dependencies = [ "windows-sys 0.60.2", ] +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + [[package]] name = "proc-macro2" version = "1.0.95" @@ -636,6 +807,61 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "rand" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "readonly" version = "0.2.13" @@ -666,6 +892,17 @@ version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d" +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "shlex" version = "1.3.0" @@ -710,6 +947,12 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + [[package]] name = "syn" version = "2.0.104" @@ -732,6 +975,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "typenum" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" + [[package]] name = "unicode-ident" version = "1.0.18" @@ -744,6 +993,21 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.14.2+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +dependencies = [ + "wit-bindgen-rt", +] + [[package]] name = "wasm-bindgen" version = "0.2.100" @@ -1006,3 +1270,32 @@ name = "windows_x86_64_msvc" version = "0.53.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" + +[[package]] +name = "wit-bindgen-rt" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" +dependencies = [ + "bitflags", +] + +[[package]] +name = "zerocopy" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index ffdb9ac..af68115 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,17 @@ path = "src/client/main.rs" [dependencies] +aes = "0.8.4" +cbc = "0.1.2" chrono = "0.4.41" +cipher = { version = "0.4.4", features = ["block-padding", "alloc"] } clap = { version = "4.5.41", features = ["derive"] } +hmac = "0.12.1" orx-concurrent-vec = "3.6.0" +pbkdf2 = "0.12.2" +rand = "0.9.2" +rayon = "1.10.0" readonly = "0.2.13" +sha2 = "0.10.9" smol = "2.0.2" tappers = "0.4.2" diff --git a/src/client/main.rs b/src/client/main.rs index f06a64f..f16f959 100644 --- a/src/client/main.rs +++ b/src/client/main.rs @@ -1,4 +1,4 @@ -use pea_2_pea::SERVER_PORT; +use pea_2_pea::*; use std::{ io::{Error, ErrorKind, Read, Write}, @@ -38,6 +38,10 @@ struct Cli { fn main() -> std::io::Result<()> { let cli = ::parse(); + if cli.network_id.len() > 0xff { + eprintln!("network id cannot have more then 255 charactes"); + exit(7); // posix for E2BIG + } { let socket: UdpSocket = (|| -> std::io::Result { match UdpSocket::bind("0.0.0.0:0") { @@ -50,11 +54,10 @@ fn main() -> std::io::Result<()> { socket.set_read_timeout(Some(Duration::new(10, 0)))?; // set timeout to 10 seconds - // send query request to get server public key let server_port: u16 = (|| -> u16 { match cli.registrar_port { Some(port_proveded) => return port_proveded, - None => return pea_2_pea::SERVER_PORT, + None => return SERVER_PORT, } })(); @@ -63,34 +66,88 @@ fn main() -> std::io::Result<()> { .parse() .unwrap(); - { - let mut query_byte: [u8; 1] = [0; 1]; - query_byte[0] = pea_2_pea::ServerMethods::QUERY as u8; - match socket.send_to(&query_byte, &server_SocketAddr) { + let mut buf: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE]; + let mut data_lenght; + loop { + match socket.send_to(&[ServerMethods::QUERY as u8], &server_SocketAddr) { Ok(s) => { #[cfg(debug_assertions)] eprintln!("send {} bytes", s); } Err(e) => { - eprintln!("Error snding data: {}", e); + panic!("Error sending data: {}", e); } } - } - let mut buf: [u8; pea_2_pea::BUFFER_SIZE] = [0; pea_2_pea::BUFFER_SIZE]; - loop { match socket.recv_from(&mut buf) { - Ok((data_length, src)) => {} + Ok((data_length_recved, _src)) => { + data_lenght = data_length_recved; + if buf[0] != 0 { + continue; + } + break; + } Err(e) if e.kind() == ErrorKind::WouldBlock || e.kind() == ErrorKind::TimedOut => { // timedout continue; } Err(e) => { - eprintln!("Error receiving data: {}", e); - std::process::exit(-4); + panic!("Error receiving data: {}", e); } } - break; + } + + let mut public_sock_addr: Vec = buf[1..data_lenght].to_vec(); + + // register network + + buf[0] = ServerMethods::REGISTER as u8; + buf[RegisterRequestDataPositions::ENCRYPTED as usize] = match cli.password { + Some(_) => true as u8, + None => false as u8, + }; + buf[RegisterRequestDataPositions::ID_LEN as usize] = cli.network_id.len() as u8; + buf[RegisterRequestDataPositions::SOCKADDR_LEN as usize] = + server_SocketAddr.to_string().len() as u8; + + buf[RegisterRequestDataPositions::DATA as usize + ..RegisterRequestDataPositions::DATA as usize + cli.network_id.len()] + .copy_from_slice(cli.network_id.as_bytes());// store network id + + + + match cli.password { + Some(s) => {}, + None => {},// do nothig + } + + + + buf[RegisterRequestDataPositions::DATA as usize + cli.network_id.len()..RegisterRequestDataPositions::DATA as usize + cli.network_id.len() + ] + + match buf[0] { + x if x == ServerResponse::OK as u8 => { + eprintln!("network registered"); + } + + x if x == ServerResponse::GENERAL_ERROR as u8 => { + eprintln!( + "{}", + match std::str::from_utf8(&buf[1..data_lenght]) { + Ok(s) => s.to_string(), + Err(e) => { + panic!("id to utf-8 failed: {}", e); + } + } + ) + } + x if x == ServerResponse::ID_EXISTS as u8 => { + panic!("network ID already exist try differnt one!"); + } + + _ => { + panic!("unknown responce from server code: 0x{:02x}", buf[0]) + } } } Ok(()) diff --git a/src/lib.rs b/src/lib.rs index 43daedf..a0c96bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ pub const SERVER_PORT: u16 = 3543; pub const BUFFER_SIZE: usize = 65535; pub const DEFAULT_TIMEOUT: u64 = 30; pub const VERSION: &str = "v0.1"; -pub const RSA_SIZE: usize = 2048; +pub const SALT_AND_IV_SIZE: u8 = 16; #[repr(u8)] pub enum ServerMethods { @@ -24,7 +24,9 @@ pub enum RegisterRequestDataPositions { ENCRYPTED = 1, // this feeld should be 0 if not encrypted ID_LEN = 2, SOCKADDR_LEN = 3, - DATA = 4, // after this there will be id and sockaddr in string or encrypted form after + SALT = 4, + IV = (SALT_AND_IV_SIZE + RegisterRequestDataPositions::SALT as u8) as isize, + DATA = (SALT_AND_IV_SIZE + RegisterRequestDataPositions::IV as u8) as isize, // after this there will be id and sockaddr in string or encrypted form after } pub mod shared; diff --git a/src/server/net.rs b/src/server/net.rs index 697ec81..ab83f96 100644 --- a/src/server/net.rs +++ b/src/server/net.rs @@ -2,6 +2,7 @@ use super::types; use super::utils; use orx_concurrent_vec::ConcurrentVec; use pea_2_pea::*; +use rayon::prelude::*; use std::sync::Arc; pub async fn handle_request( @@ -55,26 +56,81 @@ pub async fn handle_request( return; }; + let net_id: String = match std::str::from_utf8( + &buf[(RegisterRequestDataPositions::DATA as usize) + ..(len_id as usize) + (RegisterRequestDataPositions::DATA as usize)], + ) { + Ok(s) => s.to_string(), + Err(e) => { + eprint!("id to utf-8 failed: {}", e); + utils::send_general_error_to_client(src, e, socket); + return; + } + }; + + match registration_vector + .iter() + .find(|elem| elem.map(|s| &s.net_id == &net_id)) // find if id exists + { + Some(_) => { + match socket.send_to(&[ServerResponse::ID_EXISTS as u8], src) { + Ok(s) => { + #[cfg(debug_assertions)] + eprintln!("send {} bytes", s); + } + Err(e) => { + eprintln!("Error sending data: {}", e); + } + }; + return; + } + None => {} + } + + let salt: Option<[u8; SALT_AND_IV_SIZE as usize]>; + let iv: Option<[u8; SALT_AND_IV_SIZE as usize]>; + + if encrypted { + salt = Some( + buf[(RegisterRequestDataPositions::SALT as usize) + ..(RegisterRequestDataPositions::SALT as usize) + + (SALT_AND_IV_SIZE as usize)] + .try_into() + .expect("this should never happen"), + ); + iv = Some( + buf[(RegisterRequestDataPositions::IV as usize) + ..(RegisterRequestDataPositions::IV as usize) + + (SALT_AND_IV_SIZE as usize)] + .try_into() + .expect("this should never happen"), + ) + } else { + salt = None; + iv = None; + } + registration_vector.push(types::Registration::new( - match std::str::from_utf8( - &buf[(RegisterRequestDataPositions::DATA as usize) - ..(len_id as usize) + (RegisterRequestDataPositions::DATA as usize)], - ) { - Ok(s) => s.to_string(), - Err(e) => { - eprint!("id to utf-8 failed: {}", e); - utils::send_general_error_to_client(src, e, socket); - return; - } - }, - buf[(len_id as usize) + (RegisterRequestDataPositions::DATA as usize) - ..(len_id as usize) - + (RegisterRequestDataPositions::DATA as usize) - + (sock_addr_len as usize)] + net_id, + buf[(RegisterRequestDataPositions::DATA as usize) + ..(RegisterRequestDataPositions::DATA as usize) + (sock_addr_len as usize)] .to_vec(), encrypted, chrono::Utc::now().timestamp(), + salt, + iv, )); + match socket.send_to(&[ServerResponse::OK as u8], src) { + Ok(s) => { + #[cfg(debug_assertions)] + eprintln!("send {} bytes", s); + } + Err(e) => { + eprintln!("Error sending data: {}", e); + } + } + #[cfg(debug_assertions)] + println!("network registered"); } x if x == ServerMethods::HEARTBEAT as u8 => { diff --git a/src/server/types.rs b/src/server/types.rs index 7dd8336..c6046b3 100644 --- a/src/server/types.rs +++ b/src/server/types.rs @@ -1,3 +1,4 @@ +use pea_2_pea::*; use std::sync::{Arc, atomic::Ordering}; #[readonly::make] @@ -20,19 +21,35 @@ impl Client { pub struct Registration { #[readonly] pub net_id: String, + #[readonly] pub clients: Vec, + + pub last_heart_beat: i64, + #[readonly] pub encrypted: bool, - pub last_heart_beat: i64, + #[readonly] + pub salt: [u8; SALT_AND_IV_SIZE as usize], + #[readonly] + pub iv: [u8; SALT_AND_IV_SIZE as usize], } impl Registration { - pub fn new(net_id: String, client_addr: Vec, encrypted: bool, heart_beat: i64) -> Self { + pub fn new( + net_id: String, + client_addr: Vec, + encrypted: bool, + heart_beat: i64, + salt: Option<[u8; SALT_AND_IV_SIZE as usize]>, + iv: Option<[u8; SALT_AND_IV_SIZE as usize]>, + ) -> Self { Registration { - net_id: net_id, + net_id, clients: vec![Client::new(client_addr, heart_beat)], encrypted, last_heart_beat: heart_beat, + salt: salt.unwrap_or([0; SALT_AND_IV_SIZE as usize]), + iv: iv.unwrap_or([0; SALT_AND_IV_SIZE as usize]), } } } diff --git a/src/server/utils.rs b/src/server/utils.rs index 1dd2be2..19d7f03 100644 --- a/src/server/utils.rs +++ b/src/server/utils.rs @@ -7,8 +7,7 @@ pub fn send_general_error_to_client( let mut resp_buf: Box<[u8]> = vec![0; e.to_string().len() + 1].into_boxed_slice(); resp_buf[0] = ServerResponse::GENERAL_ERROR as u8; // set 1st byte to ERROR - resp_buf[1..1 + e.to_string().len()] // send error text to client - .copy_from_slice(e.to_string().as_bytes()); + resp_buf[1..1 + e.to_string().len()].copy_from_slice(e.to_string().as_bytes()); // send error text to client let _ = socket.send_to(&[ServerResponse::GENERAL_ERROR as u8], dst); } diff --git a/src/shared/crypto.rs b/src/shared/crypto.rs index 8b13789..ac2d9c5 100644 --- a/src/shared/crypto.rs +++ b/src/shared/crypto.rs @@ -1 +1,38 @@ +use aes::Aes256; +use cbc::cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit, block_padding::Pkcs7}; +use cbc::{Decryptor, Encryptor}; +use hmac::Hmac; +use pbkdf2::pbkdf2; +use sha2::Sha256; +// they are used +#[allow(dead_code)] +type Aes256CbcEnc = Encryptor; +#[allow(dead_code)] +type Aes256CbcDec = Decryptor; + +pub fn derive_key_from_password(password: &[u8], salt: &[u8]) -> [u8; 32] { + let mut key = [0u8; 32]; + let _ = pbkdf2::>(password, salt, 10000, &mut key); + key +} + +/// Encrypt using AES-256-CBC +pub fn encrypt( + key: &[u8], + iv: &[u8], + plaintext: &[u8], +) -> Result, Box> { + let cipher = Aes256CbcEnc::new_from_slices(key, iv)?; + Ok(cipher.encrypt_padded_vec_mut::(plaintext)) +} + +/// Decrypt using AES-256-CBC +pub fn decrypt( + key: &[u8], + iv: &[u8], + ciphertext: &[u8], +) -> Result, Box> { + let cipher = Aes256CbcDec::new_from_slices(key, iv)?; + Ok(cipher.decrypt_padded_vec_mut::(ciphertext).unwrap()) +} diff --git a/src/shared/mod.rs b/src/shared/mod.rs index 8b13789..b3a33b1 100644 --- a/src/shared/mod.rs +++ b/src/shared/mod.rs @@ -1 +1 @@ - +mod crypto;