-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from amiremohamadi/feat/aead
feat: AEAD
- Loading branch information
Showing
10 changed files
with
454 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
|
@@ -18,3 +18,6 @@ hmac = "0.12" | |
md-5 = "0.10" | ||
aes = "0.8" | ||
rand = "0.8" | ||
crc32fast = "1.4" | ||
sha2 = "0.10" | ||
aes-gcm = "0.10" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use crate::vmess::VmessWriter; | ||
use aes::cipher::{generic_array::GenericArray, BlockEncrypt, KeyInit}; | ||
use aes::Aes128; | ||
use md5::{Digest, Md5}; | ||
use rand::Rng; | ||
use tokio::io::AsyncWrite; | ||
|
||
impl<W: AsyncWrite + Unpin> VmessWriter<W> { | ||
pub(crate) fn create_auth_id(&self, time: &[u8; 8]) -> [u8; 16] { | ||
let mut buf = [0u8; 16]; | ||
|
||
buf[..8].copy_from_slice(time); | ||
|
||
let mut salt = [0u8; 4]; | ||
rand::thread_rng().fill(&mut salt); | ||
buf[8..12].copy_from_slice(&salt); | ||
|
||
let crc = crc32fast::hash(&buf[..12]); | ||
buf[12..].copy_from_slice(&crc.to_be_bytes()); | ||
|
||
let key = md5!(&self.uuid, b"c48619fe-8f02-49e0-b9e9-edf763e17e21"); | ||
let key = crate::hash::kdf(&key, &[b"AES Auth ID Encryption"]); | ||
let cipher = Aes128::new((&key[..16]).into()); | ||
|
||
let mut b = GenericArray::from([0u8; 16]); | ||
cipher.encrypt_block_b2b(&buf.into(), &mut b); | ||
|
||
b.into() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use sha2::{Digest, Sha256}; | ||
|
||
trait Hasher { | ||
fn clone(&self) -> Box<dyn Hasher>; | ||
fn update(&mut self, data: &[u8]); | ||
fn finalize(&mut self) -> [u8; 32]; | ||
} | ||
|
||
struct Sha256Hash(Sha256); | ||
|
||
impl Sha256Hash { | ||
fn new() -> Self { | ||
Self(Sha256::new()) | ||
} | ||
} | ||
|
||
impl Hasher for Sha256Hash { | ||
fn clone(&self) -> Box<dyn Hasher> { | ||
Box::new(Self(self.0.clone())) | ||
} | ||
|
||
fn update(&mut self, data: &[u8]) { | ||
self.0.update(data); | ||
} | ||
|
||
fn finalize(&mut self) -> [u8; 32] { | ||
self.0.clone().finalize().into() | ||
} | ||
} | ||
|
||
struct RecursiveHash { | ||
inner: Box<dyn Hasher>, | ||
outer: Box<dyn Hasher>, | ||
ipad: [u8; 64], | ||
opad: [u8; 64], | ||
} | ||
|
||
impl RecursiveHash { | ||
fn new(key: &[u8], hash: Box<dyn Hasher>) -> Self { | ||
let mut ipad = [0u8; 64]; | ||
let mut opad = [0u8; 64]; | ||
|
||
ipad[..key.len()].copy_from_slice(&key); | ||
opad[..key.len()].copy_from_slice(&key); | ||
|
||
for b in ipad.iter_mut() { | ||
*b ^= 0x36; | ||
} | ||
|
||
for b in opad.iter_mut() { | ||
*b ^= 0x5c; | ||
} | ||
|
||
let mut inner = hash.clone(); | ||
let outer = hash; | ||
|
||
inner.update(&ipad); | ||
Self { | ||
inner, | ||
outer, | ||
ipad, | ||
opad, | ||
} | ||
} | ||
} | ||
|
||
impl Hasher for RecursiveHash { | ||
fn clone(&self) -> Box<dyn Hasher> { | ||
let inner = self.inner.clone(); | ||
let outer = self.outer.clone(); | ||
let ipad = self.ipad.clone(); | ||
let opad = self.opad.clone(); | ||
|
||
Box::new(Self { | ||
inner, | ||
outer, | ||
ipad, | ||
opad, | ||
}) | ||
} | ||
|
||
fn update(&mut self, data: &[u8]) { | ||
self.inner.update(data); | ||
} | ||
|
||
fn finalize(&mut self) -> [u8; 32] { | ||
let result: [u8; 32] = self.inner.finalize().into(); | ||
self.outer.update(&self.opad); | ||
self.outer.update(&result); | ||
self.outer.finalize().into() | ||
} | ||
} | ||
|
||
pub fn kdf(key: &[u8], path: &[&[u8]]) -> [u8; 32] { | ||
let mut current = Box::new(RecursiveHash::new( | ||
b"VMess AEAD KDF", | ||
Box::new(Sha256Hash::new()), | ||
)); | ||
|
||
for p in path.into_iter() { | ||
current = Box::new(RecursiveHash::new(p, current)); | ||
} | ||
|
||
current.update(key); | ||
current.finalize() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use md5::Md5; | ||
|
||
#[test] | ||
fn test_kdf() { | ||
let uuid = uuid::uuid!("96850032-1b92-46e9-a4f2-b99631456894").as_bytes(); | ||
let key = md5!(&uuid, b"c48619fe-8f02-49e0-b9e9-edf763e17e21"); | ||
|
||
let res = kdf(&key, &[b"AES Auth ID Encryption"]); | ||
|
||
assert_eq!( | ||
res[..16], | ||
[117, 82, 144, 159, 147, 65, 74, 253, 91, 74, 70, 84, 114, 118, 203, 30] | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
#[macro_use] | ||
pub mod utils; | ||
pub mod aead; | ||
pub mod config; | ||
pub mod hash; | ||
pub mod proxy; | ||
pub mod vmess; |
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
Oops, something went wrong.