From fca8948e4c13f5aab2f7a1940eefc53901ccb3db Mon Sep 17 00:00:00 2001 From: Hocuri Date: Mon, 4 Aug 2025 20:59:40 +0200 Subject: [PATCH] Speed up message decryption by not iterating in the s2k algorithm The passphrase has as much entropy as the session key, so, there is no point in making the computation slow by iterating. --- src/pgp.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/pgp.rs b/src/pgp.rs index b59c2de0b..6b328063d 100644 --- a/src/pgp.rs +++ b/src/pgp.rs @@ -18,7 +18,7 @@ use pgp::crypto::hash::HashAlgorithm; use pgp::crypto::sym::SymmetricKeyAlgorithm; use pgp::packet::{SignatureConfig, SignatureType, Subpacket, SubpacketData}; use pgp::types::{CompressionAlgorithm, KeyDetails, Password, PublicKeyTrait, StringToKey}; -use rand::thread_rng; +use rand::{Rng as _, thread_rng}; use tokio::runtime::Handle; use crate::key::{DcKey, Fingerprint}; @@ -342,9 +342,14 @@ pub async fn encrypt_for_broadcast( let passphrase = Password::from(passphrase.to_string()); tokio::task::spawn_blocking(move || { - let mut rng = thread_rng(); - let s2k = StringToKey::new_default(&mut rng); let msg = MessageBuilder::from_bytes("", plain); + let mut rng = thread_rng(); + let mut salt = [0u8; 8]; + rng.fill(&mut salt[..]); + let s2k = StringToKey::Salted { + hash_alg: HashAlgorithm::default(), + salt, + }; let mut msg = msg.seipd_v2( &mut rng, SymmetricKeyAlgorithm::AES128,