diff --git a/common/client-libs/gateway-client/src/client.rs b/common/client-libs/gateway-client/src/client.rs index d65879f0ee..6e045003a0 100644 --- a/common/client-libs/gateway-client/src/client.rs +++ b/common/client-libs/gateway-client/src/client.rs @@ -196,6 +196,7 @@ impl GatewayClient { let noise_stream = match upgrade_noise_initiator( stream, + Default::default(), None, //as a client, the gateway cannot know my pub key &self.local_sphinx.private_key().to_bytes(), &self.gateway_sphinx.to_bytes(), diff --git a/common/client-libs/mixnet-client/src/client.rs b/common/client-libs/mixnet-client/src/client.rs index 6108d6cc2e..04c49756f0 100644 --- a/common/client-libs/mixnet-client/src/client.rs +++ b/common/client-libs/mixnet-client/src/client.rs @@ -135,6 +135,7 @@ impl Client { let noise_stream = match upgrade_noise_initiator_with_topology( stream, + Default::default(), &topology_ref, epoch_id, local_public_key, diff --git a/common/nymnoise/src/lib.rs b/common/nymnoise/src/lib.rs index 0091936508..aceeef6a88 100644 --- a/common/nymnoise/src/lib.rs +++ b/common/nymnoise/src/lib.rs @@ -27,7 +27,6 @@ use tokio::{ net::TcpStream, }; -const NOISE_HS_PATTERN: &str = "Noise_XKpsk3_25519_AESGCM_SHA256"; const MAXMSGLEN: usize = 65535; const TAGLEN: usize = 16; const HEADER_SIZE: usize = 2; @@ -60,6 +59,46 @@ impl From for NoiseError { } } } +#[derive(Default)] +pub enum NoisePattern { + #[default] + XKpsk3, + IKpsk2, + + //DEMO MODE, TO BE DELETED + NN, + XXpsk0, + XKpsk3Var, + KKpsk2, +} + +impl NoisePattern { + fn as_str(&self) -> &'static str { + match self { + Self::XKpsk3 => "Noise_XKpsk3_25519_AESGCM_SHA256", + Self::IKpsk2 => "Noise_IKpsk2_25519_AESGCM_SHA256", + + //DEMO MODE, TO BE DELETED + Self::NN => "Noise_NN_25519_AESGCM_SHA256", + Self::XXpsk0 => "Noise_XXpsk0_25519_AESGCM_SHA256", + Self::XKpsk3Var => "Noise_XKpsk3_Ed448_ChaChaPoly_Blake2b", + Self::KKpsk2 => "Noise_KKpsk2_Ed448_ChaChaPoly_Blake2b", + } + } + + fn psk_position(&self) -> u8 { + match self { + Self::XKpsk3 => 3, + Self::IKpsk2 => 2, + + //DEMO MODE, TO BE DELETED + Self::NN => 0, //psk will not be used anyway + Self::XXpsk0 => 0, + Self::XKpsk3Var => 3, + Self::KKpsk2 => 2, + } + } +} /// Wrapper around a TcpStream #[pin_project] @@ -263,6 +302,7 @@ impl AsyncWrite for NoiseStream { pub async fn upgrade_noise_initiator( conn: TcpStream, + pattern: NoisePattern, local_public_key: Option<&[u8]>, local_private_key: &[u8], remote_pub_key: &[u8], @@ -279,11 +319,10 @@ pub async fn upgrade_noise_initiator( .concat(); let secret_hash = Sha256::digest(secret); - let builder = Builder::new(NOISE_HS_PATTERN.parse()?); - let handshake = builder + let handshake = Builder::new(pattern.as_str().parse()?) .local_private_key(local_private_key) .remote_public_key(remote_pub_key) - .psk(3, &secret_hash) + .psk(pattern.psk_position(), &secret_hash) .build_initiator()?; let noise_stream = NoiseStream::new(conn, handshake); @@ -293,6 +332,7 @@ pub async fn upgrade_noise_initiator( pub async fn upgrade_noise_initiator_with_topology( conn: TcpStream, + pattern: NoisePattern, topology: &NymTopology, epoch: u32, local_public_key: &[u8], @@ -319,6 +359,7 @@ pub async fn upgrade_noise_initiator_with_topology( upgrade_noise_initiator( conn, + pattern, Some(local_public_key), local_private_key, &remote_pub_key, @@ -329,6 +370,7 @@ pub async fn upgrade_noise_initiator_with_topology( pub async fn upgrade_noise_responder( conn: TcpStream, + pattern: NoisePattern, local_public_key: &[u8], local_private_key: &[u8], remote_pub_key: Option<&[u8]>, @@ -337,18 +379,15 @@ pub async fn upgrade_noise_responder( trace!("Perform Noise Handshake, responder side"); //If the remote_key cannot be kwnown, e.g. in a client-gateway connection - let secret = [ - remote_pub_key.unwrap_or(&[]), - local_public_key, - &epoch.to_be_bytes(), - ] - .concat(); + let remote_public_key = remote_pub_key.unwrap_or(&[]); + + let secret = [remote_public_key, local_public_key, &epoch.to_be_bytes()].concat(); let secret_hash = Sha256::digest(secret); - let builder = Builder::new(NOISE_HS_PATTERN.parse()?); - let handshake = builder + let handshake = Builder::new(pattern.as_str().parse()?) .local_private_key(local_private_key) - .psk(3, &secret_hash) + .remote_public_key(remote_public_key) + .psk(pattern.psk_position(), &secret_hash) .build_responder()?; let noise_stream = NoiseStream::new(conn, handshake); @@ -359,6 +398,7 @@ pub async fn upgrade_noise_responder( pub async fn upgrade_noise_responder_with_topology( conn: TcpStream, + pattern: NoisePattern, topology: &NymTopology, epoch: u32, local_public_key: &[u8], @@ -385,6 +425,7 @@ pub async fn upgrade_noise_responder_with_topology( upgrade_noise_responder( conn, + pattern, local_public_key, local_private_key, Some(&remote_pub_key), diff --git a/gateway/src/node/client_handling/websocket/listener.rs b/gateway/src/node/client_handling/websocket/listener.rs index 0371b5217f..5ed2af2acb 100644 --- a/gateway/src/node/client_handling/websocket/listener.rs +++ b/gateway/src/node/client_handling/websocket/listener.rs @@ -86,6 +86,7 @@ impl Listener { }; let noise_stream = match upgrade_noise_responder( socket, + Default::default(), &self.local_sphinx.public_key().to_bytes(), &self.local_sphinx.private_key().to_bytes(), None, //connection from client, no remote pub key diff --git a/gateway/src/node/mixnet_handling/receiver/connection_handler.rs b/gateway/src/node/mixnet_handling/receiver/connection_handler.rs index c196ab5b3c..4d5f849fbe 100644 --- a/gateway/src/node/mixnet_handling/receiver/connection_handler.rs +++ b/gateway/src/node/mixnet_handling/receiver/connection_handler.rs @@ -218,6 +218,7 @@ impl ConnectionHandler { let noise_stream = match upgrade_noise_responder_with_topology( conn, + Default::default(), &topology_ref, epoch_id, &self.local_identity.public_key().to_bytes(), diff --git a/mixnode/src/node/listener/connection_handler/mod.rs b/mixnode/src/node/listener/connection_handler/mod.rs index ce0143d003..1b3fbb102a 100644 --- a/mixnode/src/node/listener/connection_handler/mod.rs +++ b/mixnode/src/node/listener/connection_handler/mod.rs @@ -120,6 +120,7 @@ impl ConnectionHandler { let noise_stream = match upgrade_noise_responder_with_topology( conn, + Default::default(), &topology_ref, epoch_id, &self.local_identity.public_key().to_bytes(),