rust-nex/prudpv0/src/crypto/friends_insecure.rs

78 lines
2.1 KiB
Rust
Raw Normal View History

2026-01-31 20:41:37 +01:00
use std::{io::Write, rc::Rc};
2026-01-20 20:26:44 +01:00
use hmac::Mac;
2026-01-31 20:41:37 +01:00
use md5::{Digest, Md5};
2026-01-20 20:26:44 +01:00
use rc4::{KeyInit, Rc4, StreamCipher};
2026-01-27 14:44:10 +01:00
use rnex_core::prudp::{
encryption::{DEFAULT_KEY, EncryptionPair},
types_flags::{TypesFlags, types::DATA},
};
2026-01-20 20:26:44 +01:00
use typenum::U5;
use crate::crypto::{
Crypto, CryptoInstance,
common_crypto::common_checksum,
friends_common::{ACCESS_KEY, HmacMd5},
};
pub struct InsecureInstance {
pair: EncryptionPair<Rc4<U5>>,
2026-01-27 14:44:10 +01:00
self_signat: [u8; 4],
remote_signat: [u8; 4],
2026-01-20 20:26:44 +01:00
}
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
}
2026-01-27 14:44:10 +01:00
fn generate_signature(&self, types_flags: TypesFlags, data: &[u8]) -> [u8; 4] {
if types_flags.get_types() == DATA {
if data.len() == 0 {
2026-01-31 13:48:06 +01:00
[0x78, 0x56, 0x34, 0x12]
2026-01-27 14:44:10 +01:00
} else {
2026-01-31 20:41:37 +01:00
let mut hash = Md5::new();
hash.write(ACCESS_KEY.as_bytes()).unwrap();
let mut hmac = <HmacMd5 as Mac>::new_from_slice(&hash.finalize().as_slice())
2026-01-27 14:44:10 +01:00
.expect("unable to create hmac md5");
hmac.update(data);
hmac.finalize().into_bytes()[0..4].try_into().unwrap()
}
} else {
self.self_signat
}
2026-01-20 20:26:44 +01:00
}
}
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)
}
2026-01-27 14:44:10 +01:00
fn instantiate(
&self,
packet_data: &[u8],
self_signat: [u8; 4],
remote_signat: [u8; 4],
2026-02-01 21:10:03 +01:00
) -> Option<(Self::Instance, Vec<u8>)> {
Some((
InsecureInstance {
pair: EncryptionPair::init_both(|| Rc4::new(&DEFAULT_KEY)),
self_signat,
remote_signat,
},
vec![],
))
2026-01-20 20:26:44 +01:00
}
}