things
This commit is contained in:
parent
4d4fc6c7bf
commit
fc94f655b2
43 changed files with 1957 additions and 694 deletions
54
prudpv0/src/crypto/common_crypto.rs
Normal file
54
prudpv0/src/crypto/common_crypto.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
trait IterExtra: Iterator {
|
||||
fn sum_wrapping_u8(&mut self) -> u8
|
||||
where
|
||||
Self::Item: Into<u8>;
|
||||
|
||||
fn sum_wrapping_u32(&mut self) -> u32
|
||||
where
|
||||
Self::Item: Into<u32>;
|
||||
}
|
||||
|
||||
impl<T: Iterator> IterExtra for T {
|
||||
fn sum_wrapping_u8(&mut self) -> u8
|
||||
where
|
||||
Self::Item: Into<u8>,
|
||||
{
|
||||
let mut sum = 0u8;
|
||||
for v in self {
|
||||
let val: u8 = v.into();
|
||||
sum = sum.wrapping_add(val);
|
||||
}
|
||||
sum
|
||||
}
|
||||
fn sum_wrapping_u32(&mut self) -> u32
|
||||
where
|
||||
Self::Item: Into<u32>,
|
||||
{
|
||||
let mut sum = 0u32;
|
||||
for v in self {
|
||||
let val: u32 = v.into();
|
||||
sum = sum.wrapping_add(val);
|
||||
}
|
||||
sum
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn common_checksum(access_key: &str, data: &[u8]) -> u8 {
|
||||
let leftover = data.len() % 4;
|
||||
let word_sum = bytemuck::cast_slice::<_, u32>(&data[..data.len() - leftover])
|
||||
.iter()
|
||||
.copied()
|
||||
.sum_wrapping_u32();
|
||||
|
||||
let checksum = access_key.as_bytes().iter().copied().sum_wrapping_u8();
|
||||
let checksum = checksum.wrapping_add(
|
||||
(&data[data.len() - leftover..])
|
||||
.iter()
|
||||
.copied()
|
||||
.sum_wrapping_u8(),
|
||||
);
|
||||
let checksum = checksum.wrapping_add(word_sum.to_ne_bytes().into_iter().sum_wrapping_u8());
|
||||
|
||||
checksum
|
||||
}
|
||||
5
prudpv0/src/crypto/friends_common.rs
Normal file
5
prudpv0/src/crypto/friends_common.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
use hmac::Hmac;
|
||||
use md5::Md5;
|
||||
|
||||
pub const ACCESS_KEY: &str = "ridfebb9";
|
||||
pub type HmacMd5 = Hmac<Md5>;
|
||||
52
prudpv0/src/crypto/friends_insecure.rs
Normal file
52
prudpv0/src/crypto/friends_insecure.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
use std::rc::Rc;
|
||||
|
||||
use hmac::Mac;
|
||||
use rc4::{KeyInit, Rc4, StreamCipher};
|
||||
use rnex_core::prudp::encryption::{DEFAULT_KEY, EncryptionPair};
|
||||
use typenum::U5;
|
||||
|
||||
use crate::crypto::{
|
||||
Crypto, CryptoInstance,
|
||||
common_crypto::common_checksum,
|
||||
friends_common::{ACCESS_KEY, HmacMd5},
|
||||
};
|
||||
|
||||
pub struct InsecureInstance {
|
||||
pair: EncryptionPair<Rc4<U5>>,
|
||||
}
|
||||
|
||||
impl CryptoInstance for InsecureInstance {
|
||||
fn decrypt_incoming(&mut self, data: &mut [u8]) {
|
||||
self.pair.recv.apply_keystream(data);
|
||||
}
|
||||
fn encrypt_outgoing(&mut self, data: &mut [u8]) {
|
||||
self.pair.send.apply_keystream(data);
|
||||
}
|
||||
fn get_user_id(&self) -> u32 {
|
||||
0
|
||||
}
|
||||
fn generate_signature(&self, data: &[u8]) -> [u8; 4] {
|
||||
let mut hmac = <HmacMd5 as Mac>::new_from_slice(ACCESS_KEY.as_bytes())
|
||||
.expect("unable to create hmac md5");
|
||||
hmac.update(data);
|
||||
hmac.finalize().into_bytes()[0..4].try_into().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Insecure();
|
||||
|
||||
impl Crypto for Insecure {
|
||||
type Instance = InsecureInstance;
|
||||
fn new() -> Self {
|
||||
Self()
|
||||
}
|
||||
fn calculate_checksum(&self, data: &[u8]) -> u8 {
|
||||
common_checksum(ACCESS_KEY, data)
|
||||
}
|
||||
|
||||
fn instantiate(&self, packet_data: &[u8]) -> Self::Instance {
|
||||
InsecureInstance {
|
||||
pair: EncryptionPair::init_both(|| Rc4::new(&DEFAULT_KEY)),
|
||||
}
|
||||
}
|
||||
}
|
||||
47
prudpv0/src/crypto/friends_secure.rs
Normal file
47
prudpv0/src/crypto/friends_secure.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
use hmac::Mac;
|
||||
use rc4::Rc4;
|
||||
use rnex_core::prudp::encryption::EncryptionPair;
|
||||
use typenum::U32;
|
||||
|
||||
use crate::crypto::{
|
||||
Crypto, CryptoInstance,
|
||||
common_crypto::common_checksum,
|
||||
friends_common::{ACCESS_KEY, HmacMd5},
|
||||
};
|
||||
|
||||
pub struct SecureInstance {
|
||||
pair: EncryptionPair<Rc4<U32>>,
|
||||
}
|
||||
|
||||
impl CryptoInstance for SecureInstance {
|
||||
fn decrypt_incoming(&mut self, data: &mut [u8]) {
|
||||
todo!()
|
||||
}
|
||||
fn encrypt_outgoing(&mut self, data: &mut [u8]) {
|
||||
todo!()
|
||||
}
|
||||
fn get_user_id(&self) -> u32 {
|
||||
todo!()
|
||||
}
|
||||
fn generate_signature(&self, data: &[u8]) -> [u8; 4] {
|
||||
let mut hmac = <HmacMd5 as Mac>::new_from_slice(ACCESS_KEY.as_bytes())
|
||||
.expect("unable to create hmac md5");
|
||||
hmac.update(data);
|
||||
hmac.finalize().into_bytes()[0..4].try_into().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Secure();
|
||||
|
||||
impl Crypto for Secure {
|
||||
type Instance = SecureInstance;
|
||||
fn new() -> Self {
|
||||
Self()
|
||||
}
|
||||
fn calculate_checksum(&self, data: &[u8]) -> u8 {
|
||||
common_checksum(ACCESS_KEY, data)
|
||||
}
|
||||
fn instantiate(&self, data: &[u8]) -> Self::Instance {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
9
prudpv0/src/crypto/insecure.rs
Normal file
9
prudpv0/src/crypto/insecure.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use crate::crypto::Crypto;
|
||||
|
||||
pub struct Insecure();
|
||||
|
||||
impl Crypto for Insecure {
|
||||
fn calculate_checksum(&self, data: &[u8]) -> u8 {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
32
prudpv0/src/crypto/mod.rs
Normal file
32
prudpv0/src/crypto/mod.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
use cfg_if::cfg_if;
|
||||
|
||||
mod common_crypto;
|
||||
|
||||
pub trait CryptoInstance: Send + 'static {
|
||||
fn decrypt_incoming(&mut self, data: &mut [u8]);
|
||||
fn encrypt_outgoing(&mut self, data: &mut [u8]);
|
||||
fn generate_signature(&self, data: &[u8]) -> [u8; 4];
|
||||
fn get_user_id(&self) -> u32;
|
||||
}
|
||||
|
||||
pub trait Crypto: Send + Sync + 'static {
|
||||
type Instance: CryptoInstance;
|
||||
fn new() -> Self;
|
||||
fn calculate_checksum(&self, data: &[u8]) -> u8;
|
||||
fn instantiate(&self, data: &[u8]) -> Self::Instance;
|
||||
}
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "friends")]{
|
||||
pub mod friends_common;
|
||||
pub mod friends_insecure;
|
||||
pub use friends_insecure::*;
|
||||
pub mod friends_secure;
|
||||
pub use friends_secure::*;
|
||||
} else {
|
||||
pub mod secure;
|
||||
pub use secure::*;
|
||||
pub mod insecure;
|
||||
pub use insecure::*;
|
||||
}
|
||||
}
|
||||
9
prudpv0/src/crypto/secure.rs
Normal file
9
prudpv0/src/crypto/secure.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use crate::crypto::Crypto;
|
||||
|
||||
pub struct Secure();
|
||||
|
||||
impl Crypto for Secure {
|
||||
fn calculate_checksum(&self, data: &[u8]) -> u8 {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue