diff options
| -rw-r--r-- | talpid-core/src/tunnel_state_machine/mod.rs | 4 | ||||
| -rw-r--r-- | talpid-tunnel-config-client/src/classic_mceliece.rs | 14 |
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], |
