summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSebastian Holmin <sebastian.holmin@mullvad.net>2025-01-09 13:15:10 +0100
committerSebastian Holmin <sebastian.holmin@mullvad.net>2025-01-09 15:13:10 +0100
commit7f57b1f767eb79acb372fb54d840c82e55a67fd5 (patch)
tree6ec6ac8137fffb3ff797e38fc46849ee325c792f
parentb824d79cd8293d45fc08de6cac60aa0b00096ac3 (diff)
downloadmullvadvpn-7f57b1f767eb79acb372fb54d840c82e55a67fd5.tar.xz
mullvadvpn-7f57b1f767eb79acb372fb54d840c82e55a67fd5.zip
Make `KEY_PAIR` private and expose it by a fn
-rw-r--r--talpid-core/src/tunnel_state_machine/mod.rs4
-rw-r--r--talpid-tunnel-config-client/src/classic_mceliece.rs14
2 files changed, 13 insertions, 5 deletions
diff --git a/talpid-core/src/tunnel_state_machine/mod.rs b/talpid-core/src/tunnel_state_machine/mod.rs
index 06588392e9..d9d25d95d2 100644
--- a/talpid-core/src/tunnel_state_machine/mod.rs
+++ b/talpid-core/src/tunnel_state_machine/mod.rs
@@ -25,7 +25,7 @@ use talpid_routing::RouteManagerHandle;
#[cfg(target_os = "macos")]
use talpid_tunnel::TunnelMetadata;
use talpid_tunnel::{tun_provider::TunProvider, TunnelEvent};
-use talpid_tunnel_config_client::classic_mceliece::{spawn_keypair_worker, BUFSIZE, KEYPAIR_RX};
+use talpid_tunnel_config_client::classic_mceliece::get_or_init_keypair_receiver;
#[cfg(target_os = "macos")]
use talpid_types::ErrorExt;
@@ -179,7 +179,7 @@ pub async fn spawn(
});
// Spawn a worker that pre-computes McEliece key pairs for PQ tunnels
- KEYPAIR_RX.get_or_init(|| tokio::sync::Mutex::new(spawn_keypair_worker(BUFSIZE)));
+ get_or_init_keypair_receiver();
Ok(TunnelStateMachineHandle {
command_tx,
diff --git a/talpid-tunnel-config-client/src/classic_mceliece.rs b/talpid-tunnel-config-client/src/classic_mceliece.rs
index 3d29e1997c..6aa81345c8 100644
--- a/talpid-tunnel-config-client/src/classic_mceliece.rs
+++ b/talpid-tunnel-config-client/src/classic_mceliece.rs
@@ -18,7 +18,7 @@ pub const ALGORITHM_NAME: &str = "Classic-McEliece-460896f-round3";
type KeyPair = (PublicKey<'static>, SecretKey<'static>);
-pub static KEYPAIR_RX: OnceLock<Mutex<mpsc::Receiver<KeyPair>>> = OnceLock::new();
+static KEYPAIR_RX: OnceLock<Mutex<mpsc::Receiver<KeyPair>>> = OnceLock::new();
/// Spawn a worker that pre computes `bufsize` McEliece key pairs in a separate thread, which can be
/// fetched asynchronously using the returned channel.
@@ -48,8 +48,7 @@ pub fn spawn_keypair_worker(bufsize: usize) -> mpsc::Receiver<KeyPair> {
}
pub async fn generate_keys() -> KeyPair {
- KEYPAIR_RX
- .get_or_init(|| Mutex::new(spawn_keypair_worker(BUFSIZE)))
+ get_or_init_keypair_receiver()
.lock()
.await
.recv()
@@ -57,6 +56,15 @@ pub async fn generate_keys() -> KeyPair {
.expect("Failed to receive key pair, generating working expectedly closed.")
}
+/// Returns a receiver for McEliece key pairs used by PQ tunnels. These are generated in a separate
+/// thread to reduce latency when connecting.
+///
+/// The first call will spawn the worker which immedietly starts to compute and buffer [`BUFSIZE`]
+/// of key pairs.
+pub fn get_or_init_keypair_receiver<'a>() -> &'a Mutex<mpsc::Receiver<KeyPair>> {
+ KEYPAIR_RX.get_or_init(|| Mutex::new(spawn_keypair_worker(BUFSIZE)))
+}
+
pub fn decapsulate(
secret: &SecretKey<'_>,
ciphertext_slice: &[u8],