big changes
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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> {
|
||||
|
||||
@@ -3,3 +3,4 @@ pub mod chacha;
|
||||
pub mod ecdh;
|
||||
pub mod hkdf;
|
||||
pub mod hmac;
|
||||
pub mod salt_pair;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user