big changes

This commit is contained in:
2026-02-25 18:09:20 +07:00
parent bf7d50bcef
commit 2835108b7f
56 changed files with 1111 additions and 775 deletions
-4
View File
@@ -15,7 +15,6 @@ pub struct NonceState {
impl NonceState {
pub fn new() -> Self {
let nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
println!("Nonce is {:?}", nonce);
Self {
counter: 0,
nonce,
@@ -36,7 +35,6 @@ impl NonceState {
self.counter += 1;
let counter_bytes = self.counter.to_be_bytes();
self.nonce[4..12].copy_from_slice(&counter_bytes);
println!("Current Nonce is {:?}", self.nonce);
}
}
@@ -74,8 +72,6 @@ impl AeadPacker for ChaChaCipher {
}
fn decrypt<B: Buffer>(&mut self, data: &mut B) -> Result<(), chacha20poly1305::aead::Error> {
println!("Buffer: {:?}", data.as_mut());
println!("nonce: {:?}", &self.decrypt_state.nonce);
self.cipher
.decrypt_in_place(&self.decrypt_state.nonce, &[], data)?;
self.decrypt_state.increase_counter();
+6 -5
View File
@@ -2,7 +2,7 @@ use aead::OsRng;
use x25519_dalek::{EphemeralSecret, PublicKey};
pub struct ECDH {
pub public_key: PublicKey,
secret_key: EphemeralSecret,
pub private_key: Option<EphemeralSecret>,
}
impl ECDH {
@@ -10,13 +10,14 @@ impl ECDH {
let secret = EphemeralSecret::random_from_rng(&mut OsRng);
let public = PublicKey::from(&secret);
Self {
secret_key: secret,
private_key: Some(secret),
public_key: public,
}
}
pub fn get_shared(self, public: &PublicKey) -> [u8; 32] {
let shared = self.secret_key.diffie_hellman(&public);
*shared.as_bytes()
pub fn get_shared(&mut self, public: &PublicKey) -> Option<[u8; 32]> {
let private_key = self.private_key.take()?;
let shared = private_key.diffie_hellman(&public);
Some(*shared.as_bytes())
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ impl HKDF {
extracted_key
}
pub fn expand<const N: usize>(
pub fn expand_key<const N: usize>(
extracted_key: &Hkdf<Sha256>,
mark: &[u8],
) -> Result<[u8; N], String> {
+1
View File
@@ -3,3 +3,4 @@ pub mod chacha;
pub mod ecdh;
pub mod hkdf;
pub mod hmac;
pub mod salt_pair;
+40
View File
@@ -0,0 +1,40 @@
use aead::{rand_core::RngCore, OsRng};
pub struct SaltPair {
local_salt: [u8; 32],
remote_salt: [u8; 32],
is_initiator: bool,
}
impl SaltPair {
pub fn new(is_initiator: bool) -> Self {
let mut local_salt = [0u8; 32];
OsRng.fill_bytes(&mut local_salt);
Self {
local_salt,
remote_salt: [0; 32],
is_initiator,
}
}
pub fn get_local(&self) -> [u8; 32] {
self.local_salt
}
pub fn set_remote_salt(&mut self, salt: [u8; 32]) {
self.remote_salt = salt
}
pub fn get_total(&self) -> [u8; 64] {
let mut salt = [0u8; 64];
if self.is_initiator {
salt[..32].copy_from_slice(&self.local_salt);
salt[32..].copy_from_slice(&self.remote_salt);
salt
} else {
salt[..32].copy_from_slice(&self.remote_salt);
salt[32..].copy_from_slice(&self.local_salt);
salt
}
}
}