Skip to content

Commit

Permalink
Restart mceliece keygen worker if it stops
Browse files Browse the repository at this point in the history
  • Loading branch information
hulthe committed Jan 21, 2025
1 parent 55a0fee commit 524661c
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions talpid-tunnel-config-client/src/classic_mceliece.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::OnceLock;
use std::{mem, sync::OnceLock};

use classic_mceliece_rust::{keypair_boxed, Ciphertext, CRYPTO_CIPHERTEXTBYTES};
pub use classic_mceliece_rust::{PublicKey, SecretKey, SharedSecret};
Expand Down Expand Up @@ -64,18 +64,29 @@ fn spawn_keypair_worker(bufsize: usize) -> mpsc::Receiver<KeyPair> {
}

pub async fn generate_keys() -> KeyPair {
KEYPAIR_RX
let mut rx = 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.")
.await;

let max_retry_attempts = 10;

for _ in 0..max_retry_attempts {
match rx.recv().await {
Some(keypair) => return keypair,
None => {
// The key generation worker has stopped for some reason. Try to start it again.
let _old_rx = mem::replace(&mut *rx, spawn_keypair_worker(BUFSIZE));
}
}
}

panic!("Failed to start key generation worker")
}

/// 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)))
pub fn spawn_keypair_generator() {
KEYPAIR_RX.get_or_init(|| Mutex::new(spawn_keypair_worker(BUFSIZE)));
}

pub fn decapsulate(
Expand Down

0 comments on commit 524661c

Please sign in to comment.