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

66 lines
1.7 KiB
Rust
Raw Normal View History

2026-01-20 20:26:44 +01:00
use hmac::Mac;
2026-01-31 13:48:06 +01:00
use rc4::{Rc4, StreamCipher};
use rnex_core::prudp::{
encryption::EncryptionPair,
types_flags::{TypesFlags, types::DATA},
};
2026-01-20 20:26:44 +01:00
use typenum::U32;
use crate::crypto::{
Crypto, CryptoInstance,
common_crypto::common_checksum,
friends_common::{ACCESS_KEY, HmacMd5},
};
pub struct SecureInstance {
pair: EncryptionPair<Rc4<U32>>,
2026-01-31 13:48:06 +01:00
uid: u32,
self_signat: [u8; 4],
remote_signat: [u8; 4],
2026-01-20 20:26:44 +01:00
}
impl CryptoInstance for SecureInstance {
fn decrypt_incoming(&mut self, data: &mut [u8]) {
2026-01-31 13:48:06 +01:00
self.pair.recv.apply_keystream(data);
2026-01-20 20:26:44 +01:00
}
fn encrypt_outgoing(&mut self, data: &mut [u8]) {
2026-01-31 13:48:06 +01:00
self.pair.send.apply_keystream(data);
2026-01-20 20:26:44 +01:00
}
fn get_user_id(&self) -> u32 {
2026-01-31 13:48:06 +01:00
self.uid
2026-01-20 20:26:44 +01:00
}
2026-01-27 14:44:10 +01:00
fn generate_signature(&self, types_flags: TypesFlags, data: &[u8]) -> [u8; 4] {
2026-01-31 13:48:06 +01:00
if types_flags.get_types() == DATA {
if data.len() == 0 {
[0x78, 0x56, 0x34, 0x12]
} else {
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()
}
} else {
self.self_signat
}
2026-01-20 20:26:44 +01:00
}
}
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)
}
2026-01-27 14:44:10 +01:00
fn instantiate(
&self,
data: &[u8],
self_signat: [u8; 4],
remote_signat: [u8; 4],
) -> Self::Instance {
2026-01-20 20:26:44 +01:00
todo!()
}
}