From d0ec1e29c634c0f88ce44ac10608dd9d3f039202 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 30 Jun 2026 09:48:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86chacha20=E7=9A=84=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E6=94=BE=E5=88=B0=E7=BB=93=E6=9E=84=E4=BD=93=E9=87=8C?= =?UTF-8?q?=E9=9D=A2=EF=BC=8C=E4=BB=A5=E5=87=8F=E5=B0=91=E5=BC=80=E9=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/encrypter.rs | 95 ++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 50 deletions(-) diff --git a/src/utils/encrypter.rs b/src/utils/encrypter.rs index aa2f3e7..250962e 100644 --- a/src/utils/encrypter.rs +++ b/src/utils/encrypter.rs @@ -1,14 +1,16 @@ -use std::{sync::atomic::{AtomicU32, Ordering}, time::{SystemTime, UNIX_EPOCH}}; +use std::{ + sync::atomic::{AtomicU32, Ordering}, + time::{SystemTime, UNIX_EPOCH}, +}; +use chacha20poly1305::{aead::Aead, ChaCha20Poly1305, KeyInit}; +use sdlan_sn_rs::utils::{aes_decrypt, aes_encrypt, Result, SDLanError}; -use chacha20poly1305::{KeyInit, aead::Aead}; -use sdlan_sn_rs::utils::{Result, SDLanError, aes_decrypt, aes_encrypt}; - -const COUNTER_MASK: u32 = (1<<24) - 1; +const COUNTER_MASK: u32 = (1 << 24) - 1; pub trait Encryptor { fn is_setted(&self) -> bool; - fn set_key(&mut self, region_id: u32, key:Vec); + fn set_key(&mut self, region_id: u32, key: Vec); fn encrypt(&self, data: &[u8]) -> Result>; fn decrypt(&self, ciphered: &[u8]) -> Result>; } @@ -27,16 +29,12 @@ 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(), } } - pub fn set_key(&mut self, region_id: u32, key:Vec) { + pub fn set_key(&mut self, region_id: u32, key: Vec) { match self { Self::Invalid => {} Self::Aes(aes) => { @@ -50,33 +48,22 @@ impl MyEncryptor { pub fn encrypt(&self, data: &[u8]) -> Result> { 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> { 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, is_setted: bool, next_counter: AtomicU32, @@ -86,6 +73,7 @@ pub struct Chacha20Encryptor { impl Chacha20Encryptor { pub fn new(key: Vec, region_id: u32) -> Self { Self { + cipher: chacha20poly1305::ChaCha20Poly1305::new(key.as_slice().into()), key, is_setted: true, next_counter: AtomicU32::new(0), @@ -95,47 +83,55 @@ impl Chacha20Encryptor { } impl Encryptor for Chacha20Encryptor { - fn set_key(&mut self, region_id: u32, key:Vec) { + fn set_key(&mut self, region_id: u32, key: Vec) { + self.cipher = chacha20poly1305::ChaCha20Poly1305::new(key.as_slice().into()); self.key = key; self.region_id = region_id; } fn encrypt(&self, data: &[u8]) -> Result> { - 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| { - Some((current + 1) & COUNTER_MASK) - }).unwrap() as u64; + let next_counter = self + .next_counter + .fetch_update(Ordering::Release, Ordering::Acquire, |current| { + Some((current + 1) & COUNTER_MASK) + }) + .unwrap() as u64; let mut nonce = Vec::new(); let region_id = self.region_id.to_be_bytes(); nonce.extend_from_slice(®ion_id); - let next_data = (now<<24) | next_counter; + 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> { 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() + ))), } } @@ -171,9 +167,8 @@ impl Encryptor for AesEncryptor { self.is_setted } - fn set_key(&mut self, _region_id: u32, key:Vec) { + fn set_key(&mut self, _region_id: u32, key: Vec) { self.key = key; self.is_setted = true; } } -