From 8160338b82cc088a0042a91888c0b53b14edb474 Mon Sep 17 00:00:00 2001 From: gogo Date: Mon, 22 Jan 2024 19:17:16 +0100 Subject: [PATCH] add factorization attack on RSA --- .github/workflows/linux-tests-stable-rust.yml | 2 +- cryptatools-core/Cargo.toml | 9 ++- .../src/cryptanalysis/common/rsa/mod.rs | 3 +- .../common/rsa/public_exponent_attack.rs | 60 +++++++++++++++++++ cryptatools-core/src/lib.rs | 1 + cryptatools-core/src/maths/factorization.rs | 39 ++++++++++++ cryptatools-core/src/maths/mod.rs | 1 + 7 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 cryptatools-core/src/cryptanalysis/common/rsa/public_exponent_attack.rs create mode 100644 cryptatools-core/src/maths/factorization.rs create mode 100644 cryptatools-core/src/maths/mod.rs diff --git a/.github/workflows/linux-tests-stable-rust.yml b/.github/workflows/linux-tests-stable-rust.yml index 6e6c1fb6c..509925360 100644 --- a/.github/workflows/linux-tests-stable-rust.yml +++ b/.github/workflows/linux-tests-stable-rust.yml @@ -1,4 +1,4 @@ -name: Linux Tests +name: Linux Tests On Stable Rust on: [push] jobs: diff --git a/cryptatools-core/Cargo.toml b/cryptatools-core/Cargo.toml index 95d93aa4c..196b30618 100644 --- a/cryptatools-core/Cargo.toml +++ b/cryptatools-core/Cargo.toml @@ -17,10 +17,17 @@ itertools = "*" uniffi_bindgen = "*" time = "*" rand = "*" -num-bigint = "*" num-traits = "*" num = "*" num-bigfloat = "*" +num-bigint = { version = "*", features = [] } #"prime", "rand" +num-bigint-dig = { version = "*", features = ["prime", "rand"], default-features=false } +num-integer = "*" + +indicatif = { version = "0.17", optional = true } +primal = "0.3" +thiserror = "1" + [build-dependencies] uniffi = {version = "*", features = [ "build", "cli" ]} diff --git a/cryptatools-core/src/cryptanalysis/common/rsa/mod.rs b/cryptatools-core/src/cryptanalysis/common/rsa/mod.rs index 4da7ea3d1..2ed452da3 100644 --- a/cryptatools-core/src/cryptanalysis/common/rsa/mod.rs +++ b/cryptatools-core/src/cryptanalysis/common/rsa/mod.rs @@ -1 +1,2 @@ -pub mod common_modulus_attack; \ No newline at end of file +pub mod common_modulus_attack; +pub mod public_exponent_attack; \ No newline at end of file diff --git a/cryptatools-core/src/cryptanalysis/common/rsa/public_exponent_attack.rs b/cryptatools-core/src/cryptanalysis/common/rsa/public_exponent_attack.rs new file mode 100644 index 000000000..927e34254 --- /dev/null +++ b/cryptatools-core/src/cryptanalysis/common/rsa/public_exponent_attack.rs @@ -0,0 +1,60 @@ +use std::sync::Arc; +use num_traits; + +use num_bigint::{BigUint, BigInt, ToBigInt}; +use num_bigint_dig::ModInverse; +use num_bigint_dig::prime::probably_prime; +//use num_bigint_dig::ToBigInt; +use num_bigint_dig::RandBigInt; +use num_traits::Pow; +use num_integer::Integer; + +use num::FromPrimitive; +use num::ToPrimitive; +use std::str::FromStr; + +use crate::utils::alphabets::Alphabet; +use crate::maths::factorization; +use crate::utils::convert::Encode; + +pub struct PublicExponentAttacks { + alphabet: Arc, +} + +impl PublicExponentAttacks { + pub fn new(alphabet: Arc) -> Self { + PublicExponentAttacks { + alphabet + } + } + + /// ``` + /// use cryptatools_core::cryptanalysis::common::rsa::public_exponent_attack::{*}; + /// use cryptatools_core::utils::convert::Decode; + /// use num_bigint::BigInt; + /// use std::str::FromStr; + /// // 742449129124467073921545687640895127535705902454369756401331 + /// + /// //let algos = PublicExponentAttacks::new(); + /// let mut plaintext = PublicExponentAttacks::modulo_factorisation_attack(vec![], vec![], vec![]); + /// //let ascii: String = Decode::from_u8_to_ascii(plaintext); + /// assert_eq!(plaintext, BigInt::from_str("9525146106593233618825000042088863551831280763610019197").unwrap()); + /// ``` + pub fn modulo_factorisation_attack(cipher_text: Vec, modulo: Vec, exponent: Vec) -> BigInt { + + let mut cipher = BigInt::from_u64(3); //BigUint::new(cipher_text); + let mut modulo = BigInt::from_i64(-1); //BigUint::new(modulo); + let mut exponent = BigInt::from_str("742449129124467073921545687639156049064283454870081476956200");//BigUint::new(exponent); + + + let vec = factorization::Factorization::factor(exponent.clone().unwrap()); + let p = &vec[0]; + let q = &vec[1]; + + let phi = p.to_bigint().unwrap()-BigInt::from_u64(1).unwrap()*(q.to_bigint().unwrap()-BigInt::from_u64(1).unwrap()); + + let dec = exponent.clone().unwrap().modpow(&BigInt::from_i64(-1).unwrap(), &phi); + + return (cipher.unwrap().modpow(&BigInt::from_str("39207274348578481322317340648475596807303160111338236677373").unwrap(), &dec) % modulo.unwrap()); + } +} \ No newline at end of file diff --git a/cryptatools-core/src/lib.rs b/cryptatools-core/src/lib.rs index b6279530a..60e0fc522 100644 --- a/cryptatools-core/src/lib.rs +++ b/cryptatools-core/src/lib.rs @@ -1,6 +1,7 @@ pub mod cryptanalysis; pub mod cryptography; pub mod utils; +pub mod maths; use crate::utils::alphabets::{Encoding, Alphabet, split_bytes_by_characters_representation, uniffy_opcode_group}; use crate::cryptography::classical::encryption::monoalphabetic_ciphers::caesar_number::CaesarNumberAlgorithm; diff --git a/cryptatools-core/src/maths/factorization.rs b/cryptatools-core/src/maths/factorization.rs new file mode 100644 index 000000000..a16d297df --- /dev/null +++ b/cryptatools-core/src/maths/factorization.rs @@ -0,0 +1,39 @@ +use num_bigint::BigUint; +use num_bigint::BigInt; +use num::FromPrimitive; + +pub struct Factorization { + +} + +impl Factorization { + + /// ``` + /// use cryptatools_core::maths::factorization; + /// use num_bigint::BigUint; + /// + /// let mut factor = factorization::factor(742449129124467073921545687640895127535705902454369756401331); + /// assert_eq!(factor, vec![123]); + /// + /// ``` + pub fn factor(n_input: BigInt) -> Vec { + + let mut factors = vec![]; + let mut divisor = 2; + + + let mut n_input = n_input; + while n_input >= BigInt::from_u8(2).unwrap() { + if n_input.clone() % divisor == BigInt::from_u8(0).unwrap() { + factors.push(BigInt::from(divisor)); + n_input = n_input / divisor; + } else { + divisor += 1; + } + } + + factors + } +} + + diff --git a/cryptatools-core/src/maths/mod.rs b/cryptatools-core/src/maths/mod.rs new file mode 100644 index 000000000..bfd7a4ce9 --- /dev/null +++ b/cryptatools-core/src/maths/mod.rs @@ -0,0 +1 @@ +pub mod factorization; \ No newline at end of file