Skip to content

Support RSA in rust to be more secure. #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crypto/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mod tests {
fn hash_test(hashtype: super::HashType, hashtest: &HashTest) {
let calced_raw = super::hash(hashtype, hashtest.input.as_slice());

let calced = calced_raw.as_slice().to_hex();
let calced = calced_raw.as_slice().to_hex().into_owned();

if calced != hashtest.expected_output {
println!("Test failed - {} != {}", calced, hashtest.expected_output);
Expand Down
4 changes: 4 additions & 0 deletions crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ pub mod pkcs5;
pub mod pkey;
pub mod rand;
pub mod symm;
pub mod mod_operations;
pub mod primarity_tests;
pub mod prime_generator;
pub mod rsa;
126 changes: 126 additions & 0 deletions crypto/mod_operations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
use std::num::{Zero, One};
use num::bigint::{BigUint, BigInt, ToBigInt};
use num::Integer;

fn extgcd(a: &mut BigInt, b: &mut BigInt) -> (BigInt, BigInt) {
let zero : BigInt = Zero::zero();
let one : BigInt = One::one();
let minus : BigInt = zero.sub(&one.clone());

let ( mut q, mut r, mut xx, mut yy): (BigInt, BigInt, BigInt, BigInt);
let ( mut xs0, mut xs1, mut ys0, mut ys1) = (one.clone(), zero.clone(), zero.clone(), one.clone());
let mut is_plus = true;

if (*a).lt(&b.clone()) {
let c = a.clone();
*a = b.clone();
*b = c.clone();
}

while !zero.eq(&b.clone()) {
r = (*a).mod_floor(&b.clone());
q = (*a).div(&b.clone());
*a = b.clone();
*b = r.clone();
xx = xs1.clone();
yy = ys1.clone();
xs1 = q.mul(&xs1.clone()).add(&xs0.clone());
ys1 = q.mul(&ys1.clone()).add(&ys0.clone());
xs0 = xx.clone();
ys0 = yy.clone();
is_plus = !is_plus;
}

let (mut x, mut y): (BigInt, BigInt);
if is_plus {
x = xs0;
y = ys0.mul(&minus);
}
else {
x = xs0.mul(&minus);
y = ys0;
}
return (x.to_bigint().unwrap(), y.to_bigint().unwrap());
}

pub fn mod_mult(a : &mut BigUint, b : &mut BigUint, m : &BigUint) -> BigUint {
let zero: BigUint = Zero::zero();
let mut res: BigUint = Zero::zero();
*a = a.mod_floor(m);

while zero.lt(b) {
if b.is_odd() {
res = res.add(a);
if m.lt(&res) {
res = res.sub(m);
}
}
*a = a.shl(&1u);
if m.lt(a) {
*a = a.sub(m);
}
*b = b.shr(&1u);
}

return res;
}

pub fn mod_exp(n : &mut BigUint, e : &mut BigUint, m : &BigUint) -> BigUint {
let zero: BigUint = Zero::zero();
let mut res: BigUint = One::one();

*n = n.mod_floor(m);
while zero.lt(e) {
if e.is_odd() {
res = mod_mult(&mut res.clone(), &mut n.clone(), m);
}
*n = mod_mult(&mut n.clone(), &mut n.clone(), m);
*e = e.shr(&1u);
}
return res;
}

pub fn inv_mod(a: &mut BigUint, m: &BigUint) -> BigUint {
let zero: BigInt = Zero::zero();
// The first element don't use. We know about x in "-my + ax = 1 mod(m)".
let ( _, mut x) = extgcd(&mut a.to_bigint().unwrap(), &mut m.to_bigint().unwrap());
if x.lt(&zero.clone()) {
let mi = m.to_bigint().unwrap();
x = x.add(&mi.clone());
}
return x.to_biguint().unwrap();
}

#[cfg(test)]
mod tests {
use std::num::One;
use num::bigint::ToBigUint;
use crypto::mod_operations::{mod_mult, mod_exp, inv_mod};

#[test]
fn test_mod_mult() {
let a = (7u).to_biguint().unwrap();
let b = (11u).to_biguint().unwrap();
let m = (5u).to_biguint().unwrap();
let ans = (2u).to_biguint().unwrap();
let res = mod_mult(&mut a.clone(), &mut b.clone(), & m.clone());
assert!(ans == res)
}
#[test]
fn test_mod_exp() {
let a = (3u).to_biguint().unwrap();
let b = (5u).to_biguint().unwrap();
let m = (7u).to_biguint().unwrap();
let ans = (5u).to_biguint().unwrap();
let res = mod_exp(&mut a.clone(), &mut b.clone(), & m.clone());
assert!(ans == res)
}
#[test]
fn test_mod_inv() {
let m = (13u).to_biguint().unwrap();
let a = (7u).to_biguint().unwrap();
let x = inv_mod(&mut a.clone(), &mut m.clone());
let res = mod_mult(&mut a.clone(), &mut x.clone(), & m.clone());
assert!(res == One::one());
}
}
108 changes: 108 additions & 0 deletions crypto/primarity_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use std::num::One;
use num::bigint::{BigUint, ToBigUint};
use num::Integer;
use rand;
use rand::Rng;
use crypto::mod_operations::mod_exp;

pub fn fermat_test(a: &mut BigUint, p: &mut BigUint) -> bool {
let one: BigUint = One::one();
return mod_exp(a, &mut p.sub(&one), p).eq(&one);
}

pub fn miller_rabin(n : &mut BigUint, t:uint) -> bool {
let one: BigUint = One::one();
let two = one.add(&one);
if (*n).lt(&two) {
return false;
}
else if (*n).eq(&two) {
return true;
}
else if (*n).is_even() {
return false;
}

let phi_n = n.sub(&one);
let rng_lim = phi_n.sub(&two);
let mut q = n.sub(&one);
let mut k : i32 = 0;
while q.is_odd() {
k += 1;
q = q.shr(&1u);
}
let mut rng = rand::task_rng();

let mut test_cnt = t.clone();
while test_cnt > 0 {
test_cnt -= 1;
let mut a = ( rng.gen::<uint>() ).to_biguint().unwrap();
a = a.mod_floor(&rng_lim).add(&two);
let mut x = mod_exp( &mut a.clone(), &mut q.clone(), &n.clone());
if x.eq(&one) {
continue;
}
let mut found : bool = false;
let mut check_cnt = k.clone();
while check_cnt > 0 {
check_cnt -= 1;
if x.eq(&phi_n) {
found = true;
break;
}
x = (x.mul(&x)).mod_floor(n);
}
if found {
continue;
}
return false;
}
return true;
}

#[cfg(test)]
mod tests {
use num::bigint::ToBigUint;
use crypto::primarity_tests::{fermat_test, miller_rabin};

#[test]
fn test_fermat_test_prime() {
let a = (3u).to_biguint().unwrap();
let p = (13u).to_biguint().unwrap();
let res = fermat_test(&mut a.clone(), &mut p.clone());
assert!(res == true);
}
#[test]
fn test_fermat_test_carmichael_number() {
/* 561 is Carmichael number */
let a = (5u).to_biguint().unwrap();
let p = (561u).to_biguint().unwrap();
let res = fermat_test(&mut a.clone(), &mut p.clone());
assert!(res == true);
}
#[test]
fn test_fermat_test_composite() {
let a = (3u).to_biguint().unwrap();
let p = (49u).to_biguint().unwrap();
let res = fermat_test(&mut a.clone(), &mut p.clone());
assert!(res == false);
}
#[test]
fn test_miller_rabin_prime() {
let p = (71u).to_biguint().unwrap();
let res = miller_rabin(&mut p.clone(), 50u);
assert!(res == true);
}
#[test]
fn test_miller_rabin_carmichael_number() {
let p = (561u).to_biguint().unwrap();
let res = miller_rabin(&mut p.clone(), 50u);
assert!(res == false);
}
#[test]
fn test_miller_rabin_composite() {
let p = (303u).to_biguint().unwrap();
let res = miller_rabin(&mut p.clone(), 50u);
assert!(res == false);
}
}
65 changes: 65 additions & 0 deletions crypto/prime_generator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::num::One;
use num::bigint::{BigUint, ToBigUint, RandBigInt};
use rand;
use crypto::primarity_tests::{fermat_test, miller_rabin};

fn generate_random_byte(primeLength: uint) -> BigUint {
let mut rng = rand::task_rng();
let mut res: BigUint = rng.gen_biguint(primeLength);

let one: BigUint = One::one();
let msb: BigUint = one.shl(&primeLength);
let nmsb: BigUint = msb.shr(&1u);
res = res.bitor(&one).bitor(&msb).bitor(&nmsb);
return res;
}

fn get_number_test(primeLength: uint) -> uint {
/* values taken from table 4.4, HandBook of Applied Cryptography */
let num_tests: uint;
if primeLength >= 1300 {
num_tests = 2;
} else if primeLength >= 850 {
num_tests = 3;
} else if primeLength >= 650 {
num_tests = 4;
} else if primeLength >= 550 {
num_tests = 5;
} else if primeLength >= 450 {
num_tests = 6;
} else if primeLength >= 400 {
num_tests = 7;
} else if primeLength >= 350 {
num_tests = 8;
} else if primeLength >= 300 {
num_tests = 9;
} else if primeLength >= 250 {
num_tests = 12;
} else if primeLength >= 200 {
num_tests = 15;
} else if primeLength >= 150 {
num_tests = 18;
} else if primeLength >= 100 {
num_tests = 27;
} else {
num_tests = 50;
}
return num_tests;
}

pub fn generate_prime_random_choice(primeLength: uint) -> BigUint {
let num_tests = get_number_test(primeLength);
let two = (2u).to_biguint().unwrap();

loop {
let prime_candidate = generate_random_byte(primeLength);

if !fermat_test( &mut two.clone(), &mut prime_candidate.clone() ) {
continue;
}

if miller_rabin(&mut prime_candidate.clone(), num_tests) {
return prime_candidate;
}
}
}
Loading