An optimized C implementation of the RSA public key encryption using the Montgomery Multiplication algorithm
An optimized C implementation of the RSA public key encryption using the Montgomery Multiplication algorithm.
The RSA operations for encryption and decryption involve modular exponentiation: X^Y mod M.
P = Cd mod M (decrypt with private key)
The public key is (e,M) and the private key is (d,M).
Choose two prime numbers with half the bit-length of the desired public/private keys.
See [https://www.mobilefish.com/services/rsa_key_generation/rsa_key_generation.php] for more information.
The public exponent (e) is a small integer.
Valid choices are 3, 5, 7, 17, 257, or 65537.
With RSA keys the public exponent is usually 65537.
The requirements for e are:
The private exponent is generated from primes p and q and the public exponent.
The multiply and square algorithm for Modular Exponentiation requires modular multiplication in the forms:
This operation requires expensive multiplication and division operations.
Montgomery Multiplication converts parameters into modular residues wherein multiplication becomes addition and division becomes a bitwise shift.
Modular residues require a pre computed value (R), related to the size of the modulus (M):
The bitwise Montgomery Multiplication algorithm uses the equation:
This bitwise algorithm internally calculates the modular inverse (R-1).
The modular inverse is defined as R-1 where R * R-1 mod M = 1.
Key Size | Auto Opt. CPU Time (sec) | All Opt. | Optimization (%) |
---|---|---|---|
512 | 2.42 | 0.53 | 78.1 |
1024 | 9.58 | 2.05 | 78.6 |
2048 | 38.49 | 8.27 | 78.5 |