This repository contains RSA implementations in C, C++, Java, JavaScript, Python, and Rust. For each language, two libraries were selected: OpenSSL and Libgcrypt (C), Botan and CryptoPP (C++), Bouncy Castle and Java Crypto (Java), Forge and Crypto (JavaScript), Cryptography and PyCryptodome (Python), and finally Rust OpenSSL and Rust Crypto (Rust).
This repository is my final project for a Bachelor's Degree in Computer Science and involves evaluating different implementations of the RSA encryption system.
With the increasing flow of information between different devices, encryption systems have become essential to ensure data authenticity, confidentiality, and integrity. A common approach is asymmetric encryption, which uses two keys: one for encrypting messages and another for decrypting them. In this context, the RSA algorithm emerges as one of the most widely adopted encryption systems. This study aims to evaluate RSA implementations, investigating the impact of adopting different approaches, including selecting libraries within the same programming language and using various programming languages. Experiments were conducted in C, C++, Java, JavaScript, Python, and Rust, using two libraries per language: OpenSSL and Libgcrypt (C), Botan and Crypto++ (C++), Bouncy Castle and Java Crypto (Java), Forge and Crypto (JavaScript), Cryptography and PyCryptodome (Python), and Rust OpenSSL and Rust Crypto (Rust). The analysis of average execution times demonstrated that Rust, C, and Python achieved the best performance in encryption and decryption processes, mainly due to their implementations' use of OpenSSL as the foundation. Regarding key generation, C++ and Python obtained the shortest average times for 2048, 3072 and 4096-bit keys. Additionally, an analysis of confidential data storage structures revealed similar patterns used to store private keys across the studied languages. Finally, a memory dump analysis exposed fragments of private keys in various libraries. In the case of JavaScript, it was possible to recover the private key entirely in both tested implementations, highlighting a vulnerability that could compromise system security if an attacker gains access to the program's core dump. The results provide a basis for selecting RSA implementations, considering performance and security.
Each library requires different packages and dependencies. Follow the instructions below for each one:
-
OpenSSL
sudo apt install libssl-dev
-
Libgcrypt
sudo apt install libgcrypt20-dev
-
Botan
git clone https://github.com/randombit/botan.git cd botan ./configure.py make sudo make install -
Crypto++
sudo apt install libcrypto++-dev
- Bouncy Castle and Java Crypto
Thepom.xmlfile already includes the required dependencies. You only need to install Maven:sudo apt install maven
- Forge
npm install node-forge
- Cryptography and PyCryptodome
pip install cryptography pycryptodome matplotlib tabulate pytest pytest-benchmark
- Rust OpenSSL and Rust Crypto
Dependencies are already included inCargo.toml. Ensure you have Rust installed.
You can generate the public and private keys using the following commands:
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private_key.pem
openssl rsa -in private_key.pem -pubout -out public_key.pemLoad your message into a .txt file, ensuring that you respect the maximum padding for the key and the library being used.
To generate a core dump using gcore, first, you need to identify the process ID (PID) of the target process:
ps aux | grep <process_name>
sudo gcore -o target <pid>To compile and run the C implementation:
make
./mainTo compile and run the C++ implementation:
make
./mainFor the Java implementation, build and run the project using Maven:
mvn clean verify
java -jar target/benchmarks.jarRun the JavaScript implementation with Node.js:
node rsa_test.jsRun the Python tests with pytest:
pytestRun the Rust benchmarks:
cargo benchmarkThe experiments were conducted on a machine with an AMD Ryzen 5 processor (64-bit), 12 GB of RAM, a 240 GB NVMe SSD, and the Linux Mint 22.1 Xia - 64-bit operating system. The executions took place in the Xfce graphical interface, taking into account system processes and the terminal running during the tests.
The spreadsheet with the complete results is available at the following link: https://docs.google.com/spreadsheets/d/1v0xcE82KvdqZiBZfxzAQDSQn3kZct6Qieq9O0e7JRy0/edit?usp=sharing