Skip to content

Commit

Permalink
Spawn key pair worker on launch
Browse files Browse the repository at this point in the history
  • Loading branch information
Serock3 committed Jan 9, 2025
1 parent 97023c3 commit 394d151
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions talpid-core/src/tunnel_state_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +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};
#[cfg(target_os = "macos")]
use talpid_types::ErrorExt;

Expand Down
6 changes: 3 additions & 3 deletions talpid-tunnel-config-client/src/classic_mceliece.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ const STACK_SIZE: usize = 2 * 1024 * 1024;

/// Number of McEliece key pairs to buffer. Note that, using the below algorithm, they take up around
/// 537 kB each. We therefore only buffer two, which is the largest useful amount, in case of multihop.
const BUFSIZE: usize = 2;
pub const BUFSIZE: usize = 2;

/// Use the smallest CME variant with NIST security level 3. This variant has significantly smaller
/// keys than the larger variants, and is considered safe.
pub const ALGORITHM_NAME: &str = "Classic-McEliece-460896f-round3";

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

static KEYPAIR_RX: OnceLock<Mutex<mpsc::Receiver<KeyPair>>> = OnceLock::new();
pub 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.
///
/// As it can take upwards of 200 ms to generate McEliece key pairs, it needs to be done before we
/// start connecting to the tunnel.
fn spawn_keypair_worker(bufsize: usize) -> mpsc::Receiver<KeyPair> {
pub fn spawn_keypair_worker(bufsize: usize) -> mpsc::Receiver<KeyPair> {
// As one of the key pairs will be buffered by the stack of the spawned thread, we reduce the
// capacity of the channel by one
let bufsize = bufsize.checked_sub(1).expect("bufsize must be at least 1");
Expand Down
2 changes: 1 addition & 1 deletion talpid-tunnel-config-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tonic::transport::Endpoint;
use tower::service_fn;
use zeroize::Zeroize;

mod classic_mceliece;
pub mod classic_mceliece;
mod ml_kem;
#[cfg(not(target_os = "ios"))]
mod socket;
Expand Down

0 comments on commit 394d151

Please sign in to comment.