Skip to content

Commit

Permalink
Do not expose the key pair receiver publicly
Browse files Browse the repository at this point in the history
  • Loading branch information
Serock3 committed Jan 9, 2025
1 parent 7225364 commit 38046e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
4 changes: 2 additions & 2 deletions talpid-core/src/tunnel_state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::get_or_init_keypair_receiver;
use talpid_tunnel_config_client::classic_mceliece::spawn_keypair_generator;
#[cfg(target_os = "macos")]
use talpid_types::ErrorExt;

Expand Down Expand Up @@ -179,7 +179,7 @@ pub async fn spawn(
});

// Spawn a worker that pre-computes McEliece key pairs for PQ tunnels
get_or_init_keypair_receiver();
spawn_keypair_generator();

Ok(TunnelStateMachineHandle {
command_tx,
Expand Down
13 changes: 6 additions & 7 deletions talpid-tunnel-config-client/src/classic_mceliece.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub const ALGORITHM_NAME: &str = "Classic-McEliece-460896f-round3";

type KeyPair = (PublicKey<'static>, SecretKey<'static>);

/// Receiver for McEliece key pairs used by PQ tunnels. These are generated in a separate
/// thread to reduce latency when connecting.
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
Expand Down Expand Up @@ -62,20 +64,17 @@ pub fn spawn_keypair_worker(bufsize: usize) -> mpsc::Receiver<KeyPair> {
}

pub async fn generate_keys() -> KeyPair {
get_or_init_keypair_receiver()
KEYPAIR_RX
.get_or_init(|| Mutex::new(spawn_keypair_worker(BUFSIZE)))
.lock()
.await
.recv()
.await
.expect("Expected to receive key pair, but key generator has been stopped.")
}

/// 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>> {
/// Spawn a worker which computes and buffers [`BUFSIZE`] of McEliece key pairs, used by PQ tunnels.
pub fn spawn_keypair_generator<'a>() -> &'a Mutex<mpsc::Receiver<KeyPair>> {
KEYPAIR_RX.get_or_init(|| Mutex::new(spawn_keypair_worker(BUFSIZE)))
}

Expand Down

0 comments on commit 38046e0

Please sign in to comment.