Skip to content

Commit

Permalink
Export generating secret (zarvd#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
zarvd authored Apr 24, 2023
1 parent ddeb90c commit 528e8b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub use device::{
Cidr, Device, DeviceConfig, DeviceControl, Endpoint, ParseCidrError, PeerConfig, Transport,
UdpTransport,
};
pub use noise::crypto::{LocalStaticSecret, PeerStaticSecret};
pub use tun::{Error as TunError, Tun};

#[cfg(feature = "native")]
Expand Down
27 changes: 23 additions & 4 deletions src/noise/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,30 @@ pub struct LocalStaticSecret {
}

impl LocalStaticSecret {
#[inline(always)]
pub fn random() -> Self {
Self::new(PrivateKey::random_from_rng(OsRng).to_bytes())
}

#[inline(always)]
pub fn new(private_key: [u8; 32]) -> Self {
let private = PrivateKey::from(private_key);
let public = PublicKey::from(&private);

Self { private, public }
}

#[inline(always)]
pub fn with_peer(self, peer_public_key: [u8; 32]) -> PeerStaticSecret {
PeerStaticSecret::new(self, peer_public_key)
}

#[inline(always)]
pub fn private_key(&self) -> &PrivateKey {
&self.private
}

#[inline(always)]
pub fn public_key(&self) -> &PublicKey {
&self.public
}
Expand All @@ -61,28 +70,38 @@ pub struct PeerStaticSecret {
}

impl PeerStaticSecret {
#[inline(always)]
pub fn new(local: LocalStaticSecret, public_key: [u8; 32]) -> Self {
let public = PublicKey::from(public_key);
let psk = [0u8; 32];

Self { local, public, psk }
}

#[inline(always)]
pub fn random_psk() -> [u8; 32] {
x25519_dalek::StaticSecret::random_from_rng(OsRng).to_bytes()
}

#[inline(always)]
pub fn set_psk(&mut self, psk: [u8; 32]) {
self.psk = psk;
}

#[inline(always)]
pub fn psk(&self) -> &[u8; 32] {
&self.psk
}

#[inline(always)]
pub fn local(&self) -> &LocalStaticSecret {
&self.local
}

#[inline(always)]
pub fn public_key(&self) -> &PublicKey {
&self.public
}

pub fn psk(&self) -> &[u8; 32] {
&self.psk
}
}

#[inline]
Expand Down
10 changes: 3 additions & 7 deletions src/noise/handshake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,17 @@ pub use response::{IncomingResponse, OutgoingResponse};

#[cfg(test)]
mod tests {
use rand_core::OsRng;

use super::*;
use crate::noise::crypto::{LocalStaticSecret, PeerStaticSecret};
use crate::noise::protocol::{HandshakeInitiation, HandshakeResponse};

#[inline]
fn gen_2_static_key() -> (PeerStaticSecret, PeerStaticSecret) {
let p1_local =
LocalStaticSecret::new(x25519_dalek::StaticSecret::random_from_rng(OsRng).to_bytes());
let p2_local =
LocalStaticSecret::new(x25519_dalek::StaticSecret::random_from_rng(OsRng).to_bytes());
let p1_local = LocalStaticSecret::random();
let p2_local = LocalStaticSecret::random();
let mut p1_secret = p1_local.clone().with_peer(p2_local.public_key().to_bytes());
let mut p2_secret = p2_local.with_peer(p1_local.public_key().to_bytes());
let psk = x25519_dalek::StaticSecret::random_from_rng(OsRng).to_bytes();
let psk = PeerStaticSecret::random_psk();
p1_secret.set_psk(psk);
p2_secret.set_psk(psk);

Expand Down

0 comments on commit 528e8b5

Please sign in to comment.