diff --git a/coordinator/src/args.rs b/coordinator/src/args.rs index 52d9a9b..77adb44 100644 --- a/coordinator/src/args.rs +++ b/coordinator/src/args.rs @@ -26,11 +26,6 @@ pub struct Args { #[arg(long, default_value_t = false)] pub cli: bool, - /// HTTP mode. If enabled, it will use HTTP communication with a - /// FROST server. - #[arg(long, default_value_t = false)] - pub http: bool, - /// The comma-separated usernames of the signers to use in HTTP mode. /// If HTTP mode is enabled and this is empty, then the session ID /// will be printed and will have to be shared manually. @@ -178,7 +173,7 @@ impl ProcessedArgs { Ok(ProcessedArgs { cli: args.cli, - http: args.http, + http: false, signers, num_signers, public_key_package, diff --git a/coordinator/src/comms/http.rs b/coordinator/src/comms/http.rs index 7c9f31e..0cc4d51 100644 --- a/coordinator/src/comms/http.rs +++ b/coordinator/src/comms/http.rs @@ -294,44 +294,40 @@ impl HTTPComms { }) } - // Encrypts a message for a given recipient if encryption is enabled. - fn encrypt_if_needed( - &mut self, - recipient: &Vec, - msg: Vec, - ) -> Result, Box> { - if let Some(noise_map) = &mut self.send_noise { - let noise = noise_map - .get_mut(recipient) - .ok_or_eyre("unknown recipient")?; - let mut encrypted = vec![0; 65535]; - let len = noise.write_message(&msg, &mut encrypted)?; - encrypted.truncate(len); - Ok(encrypted) - } else { - Ok(msg) - } + // Encrypts a message for a given recipient. + fn encrypt(&mut self, recipient: &Vec, msg: Vec) -> Result, Box> { + let noise_map = self + .send_noise + .as_mut() + .expect("send_noise must have been set previously"); + let noise = noise_map + .get_mut(recipient) + .ok_or_eyre("unknown recipient")?; + let mut encrypted = vec![0; 65535]; + let len = noise.write_message(&msg, &mut encrypted)?; + encrypted.truncate(len); + Ok(encrypted) } - // Decrypts a message if encryption is enabled. + // Decrypts a message. // Note that this authenticates the `sender` in the `Msg` struct; if the // sender is tampered with, the message would fail to decrypt. - fn decrypt_if_needed(&mut self, msg: Msg) -> Result> { - if let Some(noise_map) = &mut self.recv_noise { - let noise = noise_map - .get_mut(&msg.sender) - .ok_or_eyre("unknown sender")?; - let mut decrypted = vec![0; 65535]; - decrypted.resize(65535, 0); - let len = noise.read_message(&msg.msg, &mut decrypted)?; - decrypted.truncate(len); - Ok(Msg { - sender: msg.sender, - msg: decrypted, - }) - } else { - Ok(msg) - } + fn decrypt(&mut self, msg: Msg) -> Result> { + let noise_map = self + .recv_noise + .as_mut() + .expect("recv_noise must have been set previously"); + let noise = noise_map + .get_mut(&msg.sender) + .ok_or_eyre("unknown sender")?; + let mut decrypted = vec![0; 65535]; + decrypted.resize(65535, 0); + let len = noise.read_message(&msg.msg, &mut decrypted)?; + decrypted.truncate(len); + Ok(Msg { + sender: msg.sender, + msg: decrypted, + }) } } @@ -409,48 +405,49 @@ impl Comms for HTTPComms { self.session_id = Some(r.session_id); self.num_signers = num_signers; - // If encryption is enabled, create the Noise objects - (self.send_noise, self.recv_noise) = if let ( - Some(comm_privkey), - Some(comm_participant_pubkey_getter), - ) = ( + let (Some(comm_privkey), Some(comm_participant_pubkey_getter)) = ( &self.args.comm_privkey, &self.args.comm_participant_pubkey_getter, - ) { - let mut send_noise_map = HashMap::new(); - let mut recv_noise_map = HashMap::new(); - for pubkey in &self.args.signers { - let comm_participant_pubkey = comm_participant_pubkey_getter(pubkey).ok_or_eyre("A participant in specified FROST session is not registered in the coordinator's address book")?; - let builder = snow::Builder::new( - "Noise_K_25519_ChaChaPoly_BLAKE2s" - .parse() - .expect("should be a valid cipher"), - ); - let send_noise = Noise::new( - builder - .local_private_key(comm_privkey) - .remote_public_key(&comm_participant_pubkey) - .build_initiator()?, - ); - let builder = snow::Builder::new( - "Noise_K_25519_ChaChaPoly_BLAKE2s" - .parse() - .expect("should be a valid cipher"), - ); - let recv_noise = Noise::new( - builder - .local_private_key(comm_privkey) - .remote_public_key(&comm_participant_pubkey) - .build_responder()?, - ); - send_noise_map.insert(pubkey.clone(), send_noise); - recv_noise_map.insert(pubkey.clone(), recv_noise); - } - (Some(send_noise_map), Some(recv_noise_map)) - } else { - (None, None) + ) else { + return Err( + eyre!("comm_privkey and comm_participant_pubkey_getter must be specified").into(), + ); }; + // If encryption is enabled, create the Noise objects + + let mut send_noise_map = HashMap::new(); + let mut recv_noise_map = HashMap::new(); + for pubkey in &self.args.signers { + let comm_participant_pubkey = comm_participant_pubkey_getter(pubkey).ok_or_eyre("A participant in specified FROST session is not registered in the coordinator's address book")?; + let builder = snow::Builder::new( + "Noise_K_25519_ChaChaPoly_BLAKE2s" + .parse() + .expect("should be a valid cipher"), + ); + let send_noise = Noise::new( + builder + .local_private_key(comm_privkey) + .remote_public_key(&comm_participant_pubkey) + .build_initiator()?, + ); + let builder = snow::Builder::new( + "Noise_K_25519_ChaChaPoly_BLAKE2s" + .parse() + .expect("should be a valid cipher"), + ); + let recv_noise = Noise::new( + builder + .local_private_key(comm_privkey) + .remote_public_key(&comm_participant_pubkey) + .build_responder()?, + ); + send_noise_map.insert(pubkey.clone(), send_noise); + recv_noise_map.insert(pubkey.clone(), recv_noise); + } + self.send_noise = Some(send_noise_map); + self.recv_noise = Some(recv_noise_map); + eprint!("Waiting for participants to send their commitments..."); loop { @@ -467,7 +464,7 @@ impl Comms for HTTPComms { .json::() .await?; for msg in r.msgs { - let msg = self.decrypt_if_needed(msg)?; + let msg = self.decrypt(msg)?; self.state.recv(msg)?; } tokio::time::sleep(Duration::from_secs(2)).await; @@ -503,8 +500,7 @@ impl Comms for HTTPComms { // individually for each recipient. let pubkeys: Vec<_> = self.pubkeys.keys().cloned().collect(); for recipient in pubkeys { - let msg = self - .encrypt_if_needed(&recipient, serde_json::to_vec(&send_signing_package_args)?)?; + let msg = self.encrypt(&recipient, serde_json::to_vec(&send_signing_package_args)?)?; let _r = self .client .post(format!("{}/send", self.host_port)) @@ -544,7 +540,7 @@ impl Comms for HTTPComms { .json::() .await?; for msg in r.msgs { - let msg = self.decrypt_if_needed(msg)?; + let msg = self.decrypt(msg)?; self.state.recv(msg)?; } tokio::time::sleep(Duration::from_secs(2)).await; diff --git a/participant/src/args.rs b/participant/src/args.rs index 83e7a55..4bda847 100644 --- a/participant/src/args.rs +++ b/participant/src/args.rs @@ -26,11 +26,6 @@ pub struct Args { #[arg(long, default_value_t = false)] pub cli: bool, - /// HTTP mode. If enabled, it will use HTTP communication with a - /// FROST server. - #[arg(long, default_value_t = false)] - pub http: bool, - /// Public key package to use. Can be a file with a JSON-encoded /// package, or "". If the file does not exist or if "" is specified, /// then it will be read from standard input. @@ -110,7 +105,7 @@ impl ProcessedArgs { Ok(ProcessedArgs { cli: args.cli, - http: args.http, + http: false, key_package, ip: args.ip.clone(), port: args.port, diff --git a/participant/src/comms/http.rs b/participant/src/comms/http.rs index 847cd05..1107f41 100644 --- a/participant/src/comms/http.rs +++ b/participant/src/comms/http.rs @@ -132,29 +132,29 @@ where }) } - // Encrypts a message for the coordinator if encryption is enabled. - fn encrypt_if_needed(&mut self, msg: Vec) -> Result, Box> { - if let Some(noise) = &mut self.send_noise { - let mut encrypted = vec![0; 65535]; - let len = noise.write_message(&msg, &mut encrypted)?; - encrypted.truncate(len); - Ok(encrypted) - } else { - Ok(msg) - } + // Encrypts a message for the coordinator. + fn encrypt(&mut self, msg: Vec) -> Result, Box> { + let noise = self + .send_noise + .as_mut() + .expect("send_noise must have been set previously"); + let mut encrypted = vec![0; 65535]; + let len = noise.write_message(&msg, &mut encrypted)?; + encrypted.truncate(len); + Ok(encrypted) } - // Decrypts a message from the coordinator if encryption is enabled. - fn decrypt_if_needed(&mut self, msg: Vec) -> Result, Box> { - if let Some(noise) = &mut self.recv_noise { - let mut decrypted = vec![0; 65535]; - decrypted.resize(65535, 0); - let len = noise.read_message(&msg, &mut decrypted)?; - decrypted.truncate(len); - Ok(decrypted) - } else { - Ok(msg) - } + // Decrypts a message from the coordinator. + fn decrypt(&mut self, msg: Vec) -> Result, Box> { + let noise = self + .recv_noise + .as_mut() + .expect("recv_noise must have been set previously"); + let mut decrypted = vec![0; 65535]; + decrypted.resize(65535, 0); + let len = noise.read_message(&msg, &mut decrypted)?; + decrypted.truncate(len); + Ok(decrypted) } } @@ -241,60 +241,61 @@ where }; self.session_id = Some(session_id); - // If encryption is enabled, create the Noise objects - (self.send_noise, self.recv_noise) = if let ( - Some(comm_privkey), - Some(comm_coordinator_pubkey_getter), - ) = ( + let (Some(comm_privkey), Some(comm_coordinator_pubkey_getter)) = ( &self.args.comm_privkey, &self.args.comm_coordinator_pubkey_getter, - ) { - // We need to know what is the username of the coordinator in order - // to encrypt message to them. - let session_info = self - .client - .post(format!("{}/get_session_info", self.host_port)) - .json(&server::GetSessionInfoArgs { session_id }) - .bearer_auth(self.access_token.as_ref().expect("was just set")) - .send() - .await? - .json::() - .await?; - - let comm_coordinator_pubkey = comm_coordinator_pubkey_getter(&session_info.coordinator_pubkey).ok_or_eyre("The coordinator for the specified FROST session is not registered in the user's address book")?; - let builder = snow::Builder::new( - "Noise_K_25519_ChaChaPoly_BLAKE2s" - .parse() - .expect("should be a valid cipher"), - ); - let send_noise = Noise::new( - builder - .local_private_key(comm_privkey) - .remote_public_key(&comm_coordinator_pubkey) - .build_initiator()?, - ); - let builder = snow::Builder::new( - "Noise_K_25519_ChaChaPoly_BLAKE2s" - .parse() - .expect("should be a valid cipher"), - ); - let recv_noise = Noise::new( - builder - .local_private_key(comm_privkey) - .remote_public_key(&comm_coordinator_pubkey) - .build_responder()?, + ) else { + return Err( + eyre!("comm_privkey and comm_coordinator_pubkey_getter must be specified").into(), ); - (Some(send_noise), Some(recv_noise)) - } else { - (None, None) }; + // If encryption is enabled, create the Noise objects + + // We need to know what is the username of the coordinator in order + // to encrypt message to them. + let session_info = self + .client + .post(format!("{}/get_session_info", self.host_port)) + .json(&server::GetSessionInfoArgs { session_id }) + .bearer_auth(self.access_token.as_ref().expect("was just set")) + .send() + .await? + .json::() + .await?; + + let comm_coordinator_pubkey = comm_coordinator_pubkey_getter(&session_info.coordinator_pubkey).ok_or_eyre("The coordinator for the specified FROST session is not registered in the user's address book")?; + let builder = snow::Builder::new( + "Noise_K_25519_ChaChaPoly_BLAKE2s" + .parse() + .expect("should be a valid cipher"), + ); + let send_noise = Noise::new( + builder + .local_private_key(comm_privkey) + .remote_public_key(&comm_coordinator_pubkey) + .build_initiator()?, + ); + let builder = snow::Builder::new( + "Noise_K_25519_ChaChaPoly_BLAKE2s" + .parse() + .expect("should be a valid cipher"), + ); + let recv_noise = Noise::new( + builder + .local_private_key(comm_privkey) + .remote_public_key(&comm_coordinator_pubkey) + .build_responder()?, + ); + self.send_noise = Some(send_noise); + self.recv_noise = Some(recv_noise); + // Send Commitments to Server let send_commitments_args = SendCommitmentsArgs { identifier, commitments: vec![commitments], }; - let msg = self.encrypt_if_needed(serde_json::to_vec(&send_commitments_args)?)?; + let msg = self.encrypt(serde_json::to_vec(&send_commitments_args)?)?; self.client .post(format!("{}/send", self.host_port)) .bearer_auth(self.access_token.as_ref().expect("was just set")) @@ -329,7 +330,7 @@ where eprint!("."); } else { eprintln!("\nSigning package received"); - let msg = self.decrypt_if_needed(r.msgs[0].msg.clone())?; + let msg = self.decrypt(r.msgs[0].msg.clone())?; eprintln!("\n{}", String::from_utf8_lossy(&msg.clone())); break serde_json::from_slice(&msg)?; } @@ -365,7 +366,7 @@ where signature_share: vec![signature_share], }; - let msg = self.encrypt_if_needed(serde_json::to_vec(&send_signature_shares_args)?)?; + let msg = self.encrypt(serde_json::to_vec(&send_signature_shares_args)?)?; let _r = self .client