Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protocols/noise: Add NoiseConfig::with_prologue #2903

Merged
merged 7 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
# 0.49.0 - [unreleased]

- Update to [`libp2p-tcp` `v0.37.0`](transports/tcp/CHANGELOG.md#0370).
-
- Update to [`libp2p-noise` `v0.39.1`](transports/noise/CHANGELOG.md#0391).

# 0.48.0

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ libp2p-identify = { version = "0.39.0", path = "protocols/identify", optional =
libp2p-kad = { version = "0.40.0", path = "protocols/kad", optional = true }
libp2p-metrics = { version = "0.9.0", path = "misc/metrics", optional = true }
libp2p-mplex = { version = "0.36.0", path = "muxers/mplex", optional = true }
libp2p-noise = { version = "0.39.0", path = "transports/noise", optional = true }
libp2p-noise = { version = "0.39.1", path = "transports/noise", optional = true }
libp2p-ping = { version = "0.39.0", path = "protocols/ping", optional = true }
libp2p-plaintext = { version = "0.36.0", path = "transports/plaintext", optional = true }
libp2p-pnet = { version = "0.22.0", path = "transports/pnet", optional = true }
Expand Down
6 changes: 6 additions & 0 deletions transports/noise/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.39.1

- Add `NoiseConfig::with_prologue` which allows users to set the noise prologue of the handshake. See [PR XXXX].

[PR XXXX]: https://github.com/libp2p/rust-libp2p/pull/XXX

# 0.39.0

- Update to `libp2p-core` `v0.36.0`.
Expand Down
2 changes: 1 addition & 1 deletion transports/noise/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-noise"
edition = "2021"
rust-version = "1.56.1"
description = "Cryptographic handshake protocol using the noise framework."
version = "0.39.0"
version = "0.39.1"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
20 changes: 20 additions & 0 deletions transports/noise/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub struct NoiseConfig<P, C: Zeroize, R = ()> {
legacy: LegacyConfig,
remote: R,
_marker: std::marker::PhantomData<P>,
prologue: Vec<u8>,
}

impl<H, C: Zeroize, R> NoiseConfig<H, C, R> {
Expand All @@ -88,6 +89,15 @@ impl<H, C: Zeroize, R> NoiseConfig<H, C, R> {
NoiseAuthenticated { config: self }
}

/// Set the noise prologue.
///
/// The prologue can contain arbitrary data and will be hashed into the noise handshake.
/// For the handshake to succeed, both parties must set the same prologue.
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
pub fn with_prologue(&mut self, prologue: Vec<u8>) -> &mut Self {
self.prologue = prologue;
self
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this a with_ function instead of exposing it in every constructor because it is often not needed and this way, we can keep the entire prologue functionality hidden from users that just want to use regular noise for their connections.

Having said that, if we agree to merge #2887, then most users don't need to touch NoiseConfig anyway so perhaps it should just be a regular constructor parameter?


/// Sets the legacy configuration options to use, if any.
pub fn set_legacy_config(&mut self, cfg: LegacyConfig) -> &mut Self {
self.legacy = cfg;
Expand All @@ -107,6 +117,7 @@ where
legacy: LegacyConfig::default(),
remote: (),
_marker: std::marker::PhantomData,
prologue: Vec::default(),
}
}
}
Expand All @@ -123,6 +134,7 @@ where
legacy: LegacyConfig::default(),
remote: (),
_marker: std::marker::PhantomData,
prologue: Vec::default(),
}
}
}
Expand All @@ -142,6 +154,7 @@ where
legacy: LegacyConfig::default(),
remote: (),
_marker: std::marker::PhantomData,
prologue: Vec::default(),
}
}
}
Expand All @@ -165,6 +178,7 @@ where
legacy: LegacyConfig::default(),
remote: (remote_dh, remote_id),
_marker: std::marker::PhantomData,
prologue: Vec::default(),
}
}
}
Expand All @@ -185,6 +199,7 @@ where
let session = self
.params
.into_builder()
.prologue(self.prologue.as_ref())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand correctly that setting no prologue is equal to setting an empty prologue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is my understanding. The prologue is hashed into the protocol. Appending an empty slice vs appending nothing is not going to change the final hash.

.local_private_key(self.dh_keys.secret().as_ref())
.build_responder()
.map_err(NoiseError::from);
Expand Down Expand Up @@ -212,6 +227,7 @@ where
let session = self
.params
.into_builder()
.prologue(self.prologue.as_ref())
.local_private_key(self.dh_keys.secret().as_ref())
.build_initiator()
.map_err(NoiseError::from);
Expand Down Expand Up @@ -241,6 +257,7 @@ where
let session = self
.params
.into_builder()
.prologue(self.prologue.as_ref())
.local_private_key(self.dh_keys.secret().as_ref())
.build_responder()
.map_err(NoiseError::from);
Expand Down Expand Up @@ -268,6 +285,7 @@ where
let session = self
.params
.into_builder()
.prologue(self.prologue.as_ref())
.local_private_key(self.dh_keys.secret().as_ref())
.build_initiator()
.map_err(NoiseError::from);
Expand Down Expand Up @@ -297,6 +315,7 @@ where
let session = self
.params
.into_builder()
.prologue(self.prologue.as_ref())
.local_private_key(self.dh_keys.secret().as_ref())
.build_responder()
.map_err(NoiseError::from);
Expand Down Expand Up @@ -324,6 +343,7 @@ where
let session = self
.params
.into_builder()
.prologue(self.prologue.as_ref())
.local_private_key(self.dh_keys.secret().as_ref())
.remote_public_key(self.remote.0.as_ref())
.build_initiator()
Expand Down