Skip to content

Commit

Permalink
Add doc comments to RSA lib
Browse files Browse the repository at this point in the history
  • Loading branch information
0xphen committed Nov 7, 2023
1 parent 5960208 commit 11abc32
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
29 changes: 18 additions & 11 deletions rsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,59 @@ use utils::{modular_inverse, relative_prime};
use num_bigint::{BigInt, BigUint, Sign, ToBigInt};
use rand::{thread_rng, RngCore};

// The public exponent is hardcoded as `65537` cause it's
// a Fermat's prime and and it's large enough to be secure against certain
// attacks while still being small enough to allow for efficient encryption operations.
// Public exponent used for RSA. 65537 is chosen because it's a Fermat prime and commonly used.
const E: u64 = 65537;

pub struct RSA {
d: BigInt,
pub n: BigInt,
pub e: BigInt,
d: BigInt, // The private exponent.
pub n: BigInt, // The modulus for both the public and private keys.
pub e: BigInt, // The public exponent.
}

impl RSA {
/// Constructs a new RSA instance with generated keys.
pub fn new() -> Self {
// Generate two distinct primes, p and q, for RSA.
let p = Self::gen_1024_prime().to_bigint().unwrap();
let q = Self::gen_1024_prime().to_bigint().unwrap();

// Calculate the modulus n which is the product of p and q.
let n: BigInt = (&p * &q).to_bigint().unwrap();

// ϕ(N) is multiplicative. Since N = p * q
// ϕ(p * q) = ϕ(p) * ϕ(q)
// Calculate Euler's totient function, phi(n), which is (p-1)*(q-1).
let phi_n = (&p - 1) * (&q - 1);

// Create BigInt from the constant exponent.
let e = BigInt::from(E);
// Ensure `e` and `phi_n` are relative prime

// Check if e and phi_n are coprime, which they should be by the choice of e.
if !relative_prime::is_co_prime(&phi_n, &e) {
panic!("{} and {} are not co-prime", e, phi_n);
}

// The decryption key `d` is the multiplicative inverse of
// `E` mod `n`
// Calculate the private exponent d, the modular inverse of e mod phi_n.
let d = modular_inverse::mod_inverse(e.clone(), phi_n);

RSA { d, n, e }
}

/// Generates a random 1024-bit prime number for RSA key generation.
fn gen_1024_prime() -> BigUint {
let mut rng = thread_rng();

loop {
println!("Deriving 1024 bit prime...");
let mut bytes = [0u8; 128]; // 128 bytes = 1024 bits
// Create a 128-byte buffer, which equates to 1024 bits.
let mut bytes = [0u8; 128];
rng.fill_bytes(&mut bytes);

// Ensure the number is odd by setting the last bit to 1
// Set the least significant bit to 1 to ensure the number is odd.
bytes[127] |= 1;
let p = BigUint::from_bytes_be(&bytes);

// Use the Miller-Rabin primality test to check if the number is prime.
if MRPT::is_prime(&p) {
println!("Found 1024 bit prime: {:?}", p);
return p;
Expand Down
2 changes: 1 addition & 1 deletion sha-256/src/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,4 @@ mod tests {
let bytes = hex_to_byte_array(constants::H[0]);
assert_eq!(bytes, [106, 9, 230, 103]);
}
}
}
2 changes: 1 addition & 1 deletion utils/src/relative_prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn is_co_prime(a: &BigInt, b: &BigInt) -> bool {
pub fn gcd(a: &BigInt, b: &BigInt) -> BigInt {
let mut a = a.clone();
let mut b = b.clone();

while !b.is_zero() {
let r = &a % &b;
a = b;
Expand Down

0 comments on commit 11abc32

Please sign in to comment.