Compare commits
2 Commits
1de337f3d6
...
8493f80bdd
| Author | SHA1 | Date | |
|---|---|---|---|
| 8493f80bdd | |||
| ebf0ea3a04 |
@ -550,7 +550,7 @@ pub async fn check_peer_registration_needed(
|
||||
_v6_info: &Option<V6Info>,
|
||||
peer_sock: &SdlanSock,
|
||||
) {
|
||||
let p = eee.known_peers.peers.get(&src_mac);
|
||||
let mut p = eee.known_peers.peers.get_mut(&src_mac);
|
||||
let last_seen;
|
||||
let now;
|
||||
match p {
|
||||
@ -561,7 +561,7 @@ pub async fn check_peer_registration_needed(
|
||||
return;
|
||||
// unimplemented!();
|
||||
}
|
||||
Some(k) => {
|
||||
Some(ref mut k) => {
|
||||
// let mut ipv4_to_ipv6 = false;
|
||||
now = get_current_timestamp();
|
||||
if !from_sn {
|
||||
@ -569,8 +569,14 @@ pub async fn check_peer_registration_needed(
|
||||
}
|
||||
let origin_family = k.sock.family;
|
||||
if origin_family != peer_sock.family {
|
||||
if peer_sock.family == AF_INET6 && origin_family == AF_INET {
|
||||
info!("Upgrading peer {} from IPv4 to IPv6 P2P", mac_to_string(&src_mac));
|
||||
k.sock = peer_sock.deepcopy();
|
||||
k.last_seen.store(now, Ordering::Relaxed);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
/*
|
||||
if peer_sock.family == AF_INET6 && k.sock.read().unwrap().family == AF_INET {
|
||||
println!("changing to ipv6");
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
use std::{sync::atomic::{AtomicU32, Ordering}, time::{SystemTime, UNIX_EPOCH}};
|
||||
use std::{
|
||||
sync::atomic::{AtomicU32, Ordering},
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
|
||||
use chacha20poly1305::{KeyInit, aead::Aead};
|
||||
use sdlan_sn_rs::utils::{Result, SDLanError, aes_decrypt, aes_encrypt};
|
||||
use chacha20poly1305::{aead::Aead, ChaCha20Poly1305, KeyInit};
|
||||
use sdlan_sn_rs::utils::{aes_decrypt, aes_encrypt, Result, SDLanError};
|
||||
|
||||
const COUNTER_MASK: u32 = (1 << 24) - 1;
|
||||
|
||||
@ -27,12 +29,8 @@ impl MyEncryptor {
|
||||
pub fn is_setted(&self) -> bool {
|
||||
match self {
|
||||
Self::Invalid => false,
|
||||
Self::Aes(aes) => {
|
||||
aes.is_setted()
|
||||
}
|
||||
Self::ChaChao20(cha) => {
|
||||
cha.is_setted()
|
||||
}
|
||||
Self::Aes(aes) => aes.is_setted(),
|
||||
Self::ChaChao20(cha) => cha.is_setted(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,33 +48,22 @@ impl MyEncryptor {
|
||||
|
||||
pub fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>> {
|
||||
match self {
|
||||
Self::Invalid => {
|
||||
Err(SDLanError::EncryptError("invalid encryptor".to_owned()))
|
||||
}
|
||||
Self::Aes(aes) => {
|
||||
aes.encrypt(data)
|
||||
}
|
||||
Self::ChaChao20(cha) => {
|
||||
cha.encrypt(data)
|
||||
}
|
||||
Self::Invalid => Err(SDLanError::EncryptError("invalid encryptor".to_owned())),
|
||||
Self::Aes(aes) => aes.encrypt(data),
|
||||
Self::ChaChao20(cha) => cha.encrypt(data),
|
||||
}
|
||||
}
|
||||
pub fn decrypt(&self, ciphered: &[u8]) -> Result<Vec<u8>> {
|
||||
match self {
|
||||
Self::Invalid => {
|
||||
Err(SDLanError::EncryptError("invalid encryptor".to_owned()))
|
||||
}
|
||||
Self::Aes(aes) => {
|
||||
aes.decrypt(ciphered)
|
||||
}
|
||||
Self::ChaChao20(cha) => {
|
||||
cha.decrypt(ciphered)
|
||||
}
|
||||
Self::Invalid => Err(SDLanError::EncryptError("invalid encryptor".to_owned())),
|
||||
Self::Aes(aes) => aes.decrypt(ciphered),
|
||||
Self::ChaChao20(cha) => cha.decrypt(ciphered),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Chacha20Encryptor {
|
||||
cipher: ChaCha20Poly1305,
|
||||
key: Vec<u8>,
|
||||
is_setted: bool,
|
||||
next_counter: AtomicU32,
|
||||
@ -86,6 +73,7 @@ pub struct Chacha20Encryptor {
|
||||
impl Chacha20Encryptor {
|
||||
pub fn new(key: Vec<u8>, region_id: u32) -> Self {
|
||||
Self {
|
||||
cipher: chacha20poly1305::ChaCha20Poly1305::new(key.as_slice().into()),
|
||||
key,
|
||||
is_setted: true,
|
||||
next_counter: AtomicU32::new(0),
|
||||
@ -96,17 +84,24 @@ impl Chacha20Encryptor {
|
||||
|
||||
impl Encryptor for Chacha20Encryptor {
|
||||
fn set_key(&mut self, region_id: u32, key: Vec<u8>) {
|
||||
self.cipher = chacha20poly1305::ChaCha20Poly1305::new(key.as_slice().into());
|
||||
self.key = key;
|
||||
self.region_id = region_id;
|
||||
}
|
||||
|
||||
fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>> {
|
||||
let cipher = chacha20poly1305::ChaCha20Poly1305::new(self.key.as_slice().into());
|
||||
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
|
||||
// let cipher = chacha20poly1305::ChaCha20Poly1305::new(self.key.as_slice().into());
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis() as u64;
|
||||
|
||||
let next_counter = self.next_counter.fetch_update(Ordering::Release, Ordering::Acquire, |current| {
|
||||
let next_counter = self
|
||||
.next_counter
|
||||
.fetch_update(Ordering::Release, Ordering::Acquire, |current| {
|
||||
Some((current + 1) & COUNTER_MASK)
|
||||
}).unwrap() as u64;
|
||||
})
|
||||
.unwrap() as u64;
|
||||
|
||||
let mut nonce = Vec::new();
|
||||
let region_id = self.region_id.to_be_bytes();
|
||||
@ -114,28 +109,29 @@ impl Encryptor for Chacha20Encryptor {
|
||||
let next_data = (now << 24) | next_counter;
|
||||
nonce.extend_from_slice(&next_data.to_be_bytes());
|
||||
|
||||
match cipher.encrypt(nonce.as_slice().into(), data) {
|
||||
match self.cipher.encrypt(nonce.as_slice().into(), data) {
|
||||
Ok(data) => {
|
||||
nonce.extend_from_slice(&data);
|
||||
Ok(nonce)
|
||||
},
|
||||
Err(e) => {
|
||||
Err(SDLanError::EncryptError(e.to_string()))
|
||||
}
|
||||
Err(e) => Err(SDLanError::EncryptError(e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
fn decrypt(&self, ciphered: &[u8]) -> Result<Vec<u8>> {
|
||||
if ciphered.len() < 12 {
|
||||
return Err(SDLanError::EncryptError("ciphered text size error".to_owned()))
|
||||
return Err(SDLanError::EncryptError(
|
||||
"ciphered text size error".to_owned(),
|
||||
));
|
||||
}
|
||||
let cipher = chacha20poly1305::ChaCha20Poly1305::new(self.key.as_slice().into());
|
||||
// let cipher = chacha20poly1305::ChaCha20Poly1305::new(self.key.as_slice().into());
|
||||
let nonce = &ciphered[0..12];
|
||||
match cipher.decrypt(nonce.into(), &ciphered[12..]) {
|
||||
match self.cipher.decrypt(nonce.into(), &ciphered[12..]) {
|
||||
Ok(data) => Ok(data),
|
||||
Err(e) => {
|
||||
Err(SDLanError::EncryptError(format!("failed to decyrpt: {}", e.to_string())))
|
||||
}
|
||||
Err(e) => Err(SDLanError::EncryptError(format!(
|
||||
"failed to decyrpt: {}",
|
||||
e.to_string()
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
@ -176,4 +172,3 @@ impl Encryptor for AesEncryptor {
|
||||
self.is_setted = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user