-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
443 additions
and
433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use aes_gcm::Aes128Gcm; | ||
use bytes::Bytes; | ||
use chacha20poly1305::ChaCha20Poly1305; | ||
|
||
use crate::common::crypto::AeadCipherHelper; | ||
|
||
pub enum VmessSecurity { | ||
Aes128Gcm(Aes128Gcm), | ||
ChaCha20Poly1305(ChaCha20Poly1305), | ||
} | ||
|
||
impl VmessSecurity { | ||
#[inline(always)] | ||
pub fn overhead_len(&self) -> usize { | ||
16 | ||
} | ||
#[inline(always)] | ||
pub fn nonce_len(&self) -> usize { | ||
12 | ||
} | ||
} | ||
|
||
pub(crate) struct AeadCipher { | ||
pub security: VmessSecurity, | ||
nonce: [u8; 32], | ||
iv: Bytes, | ||
count: u16, | ||
} | ||
|
||
impl AeadCipher { | ||
pub fn new(iv: &[u8], security: VmessSecurity) -> Self { | ||
Self { | ||
security, | ||
nonce: [0u8; 32], | ||
iv: Bytes::copy_from_slice(iv), | ||
count: 0, | ||
} | ||
} | ||
|
||
pub fn decrypt_inplace(&mut self, buf: &mut [u8]) -> std::io::Result<()> { | ||
let mut nonce = self.nonce; | ||
let security = &self.security; | ||
let iv = &self.iv; | ||
let count = &mut self.count; | ||
|
||
nonce[..2].copy_from_slice(&count.to_be_bytes()); | ||
nonce[2..12].copy_from_slice(&iv[2..12]); | ||
*count += 1; | ||
|
||
let nonce = &nonce[..security.nonce_len()]; | ||
match security { | ||
VmessSecurity::Aes128Gcm(cipher) => { | ||
let dec = cipher.decrypt_in_place_with_slice(nonce.into(), &[], &mut buf[..]); | ||
if dec.is_err() { | ||
return Err(std::io::Error::new( | ||
std::io::ErrorKind::InvalidData, | ||
dec.unwrap_err().to_string(), | ||
)); | ||
} | ||
} | ||
VmessSecurity::ChaCha20Poly1305(cipher) => { | ||
let dec = cipher.decrypt_in_place_with_slice(nonce.into(), &[], &mut buf[..]); | ||
if dec.is_err() { | ||
return Err(std::io::Error::new( | ||
std::io::ErrorKind::InvalidData, | ||
dec.unwrap_err().to_string(), | ||
)); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn encrypt_inplace(&mut self, buf: &mut [u8]) -> std::io::Result<()> { | ||
let mut nonce = self.nonce; | ||
let security = &self.security; | ||
let iv = &self.iv; | ||
let count = &mut self.count; | ||
|
||
nonce[..2].copy_from_slice(&count.to_be_bytes()); | ||
nonce[2..12].copy_from_slice(&iv[2..12]); | ||
*count += 1; | ||
|
||
let nonce = &nonce[..security.nonce_len()]; | ||
match security { | ||
VmessSecurity::Aes128Gcm(cipher) => { | ||
cipher.encrypt_in_place_with_slice(nonce.into(), &[], &mut buf[..]); | ||
} | ||
VmessSecurity::ChaCha20Poly1305(cipher) => { | ||
cipher.encrypt_in_place_with_slice(nonce.into(), &[], &mut buf[..]); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.