diff --git a/Cargo.toml b/Cargo.toml index 1dd276a..9d48528 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,10 +21,6 @@ members = [ "tools/gen_keys", ] -[features] -default = ["parallel"] -parallel = ["tokio", "mpsc", "num_cpus"] - [profile.dev] opt-level = 1 @@ -46,9 +42,9 @@ openssl = "0.10" base-x = "0.2" display_bytes = "0.2" ssh-key = { version = "0.6", features = ["encryption"] } -tokio = { version = "1.40", features = ["rt", "rt-multi-thread"], optional = true } -mpsc = { version = "0.2", optional = true } -num_cpus = { version = "1.15", optional = true } +tokio = { version = "1.40", features = ["rt", "rt-multi-thread"] } +mpsc = "0.2" +num_cpus = "1.15" update-informer = "1.1" indicatif = "0.17" itertools = "0.13" diff --git a/src/lib.rs b/src/lib.rs index bf4a9c4..255a6d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,7 @@ use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use rug::integer::IsPrime; use rug::Integer; use std::cell::RefCell; -#[cfg(feature = "parallel")] use std::sync::mpsc; -#[cfg(feature = "parallel")] use std::sync::Arc; mod attack; @@ -66,31 +64,6 @@ pub fn run_attack( Ok(solution) } -/// Run all attacks. -/// -/// When the `parallel` feature is enabled, this function will run all attacks in parallel using all available CPU cores. -/// Else, it will run all attacks in sequence (single-threaded). -pub fn run_attacks(params: &Parameters) -> Result> { - #[cfg(feature = "parallel")] - return run_parallel_attacks(params, &ATTACKS, num_cpus::get()); - #[cfg(not(feature = "parallel"))] - run_sequence_attacks(params, &ATTACKS) -} - -/// Run specific attacks. -/// -/// When the `parallel` feature is enabled, this function will run all attacks in parallel using all available CPU cores. -/// Else, it will run all attacks in sequence (single-threaded). -pub fn run_specific_attacks( - params: &Parameters, - attacks: &[Arc], -) -> Result> { - #[cfg(feature = "parallel")] - return run_parallel_attacks(params, attacks, num_cpus::get()); - #[cfg(not(feature = "parallel"))] - run_sequence_attacks(params, attacks) -} - fn check_n_prime(n: &Option) -> bool { if let Some(n) = &n { match n.is_probably_prime(100) { @@ -133,54 +106,6 @@ fn create_progress_bar(mp: &MultiProgress) -> ProgressBar { pb } -/// Run all attacks in sequence, from fastest to slowest (single-threaded) -pub fn run_sequence_attacks( - params: &Parameters, - attacks: &[Arc], -) -> Result> { - if check_n_prime(¶ms.n) { - return Err(None); - } - - let mut partial_factors: Option = None; - let (mp, pb_main) = create_multi_progress(attacks.len()); - for attack in attacks.iter() { - match if attack.speed() == AttackSpeed::Fast { - // No progress bar for fast attacks - run_attack(attack.clone(), params, None) - } else { - let pb = create_progress_bar(&mp); - run_attack(attack.clone(), params, Some(&pb)) - } { - Ok(solution) => return Ok(solution), - Err(Error::PartialFactorization(factor)) => { - if let Some(partial_factors) = &mut partial_factors { - partial_factors.merge(&factor); - } else { - partial_factors = Some(factor); - } - - // Try to create a private key from the partial factors - if let Ok(private_key) = PrivateKey::from_factors( - partial_factors.as_ref().unwrap().clone(), - params.e.clone(), - ) { - return Ok(Solution::new_pk("Partial factors", private_key)); - } - pb_main.set_message(format!( - "({} factors found) ", - partial_factors.as_ref().unwrap().len() - )); - } - _ => {} - } - pb_main.inc(1); - } - - Err(partial_factors) -} - -#[cfg(feature = "parallel")] async fn _run_parallel_attacks<'a>( params: Arc, attacks: &[Arc], @@ -220,9 +145,29 @@ async fn _run_parallel_attacks<'a>( } } -/// Run all attacks in parallel, from fastest to slowest (multi-threaded) -#[cfg(feature = "parallel")] -pub fn run_parallel_attacks( +/// Run all attacks on all available CPU cores. +pub fn run_attacks(params: &Parameters) -> Result> { + run_specific_attacks_with_threads(params, &ATTACKS, num_cpus::get()) +} + +/// Run specific attacks on all available CPU cores. +pub fn run_specific_attacks( + params: &Parameters, + attacks: &[Arc], +) -> Result> { + run_specific_attacks_with_threads(params, attacks, num_cpus::get()) +} + +/// Run all attacks on a given number of threads. +pub fn run_attacks_with_threads( + params: &Parameters, + threads: usize, +) -> Result> { + run_specific_attacks_with_threads(params, &ATTACKS, threads) +} + +/// Run specific attacks on a given number of threads. +pub fn run_specific_attacks_with_threads( params: &Parameters, attacks: &[Arc], threads: usize, @@ -231,10 +176,6 @@ pub fn run_parallel_attacks( return Err(None); } - if threads <= 1 { - return run_sequence_attacks(params, attacks); - } - // User for key build from partial factors let param_e = params.e.clone(); @@ -270,10 +211,9 @@ pub fn run_parallel_attacks( } // Try to create a private key from the partial factors - if let Ok(private_key) = PrivateKey::from_factors( - partial_factors.as_ref().unwrap().clone(), - param_e.clone(), - ) { + if let Ok(private_key) = + PrivateKey::from_factors(partial_factors.as_ref().unwrap().clone(), ¶m_e) + { break Some(Solution::new_pk("Partial factors", private_key)); } pb_main.set_message(format!( diff --git a/src/main.rs b/src/main.rs index b42c74b..2eeecce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -110,7 +110,6 @@ struct Args { #[clap(long)] factors: bool, /// Number of threads to use. Default: number of CPUs - #[cfg(feature = "parallel")] #[clap(short, long, default_value_t = num_cpus::get())] threads: usize, /// Specify attacks to run. Default: all. (e.g. --attacks ecm,wiener,sparse) @@ -301,10 +300,7 @@ fn main() -> Result<(), MainError> { .collect::>(); // Run attacks - #[cfg(feature = "parallel")] - let res = rsacracker::run_parallel_attacks(¶ms, &attacks, args.threads); - #[cfg(not(feature = "parallel"))] - let res = rsacracker::run_sequence_attacks(¶ms, &attacks); + let res = rsacracker::run_specific_attacks_with_threads(¶ms, &attacks, args.threads); let solution = match res { Ok(solution) => solution, Err(partial_factors) => {