39 releases

0.10.0-pre.3 Oct 8, 2024
0.10.0-pre.2 Jul 28, 2024
0.10.0-pre.1 Feb 6, 2024
0.9.6 Dec 1, 2023
0.0.0 Jul 24, 2018

⚠️ Issues reported

#25 in Cryptography

Download history 493795/week @ 2024-08-01 529369/week @ 2024-08-08 527748/week @ 2024-08-15 565006/week @ 2024-08-22 515993/week @ 2024-08-29 564289/week @ 2024-09-05 550961/week @ 2024-09-12 550133/week @ 2024-09-19 582445/week @ 2024-09-26 596808/week @ 2024-10-03 589495/week @ 2024-10-10 709107/week @ 2024-10-17 731686/week @ 2024-10-24 691208/week @ 2024-10-31 691985/week @ 2024-11-07 615492/week @ 2024-11-14

2,864,622 downloads per month
Used in 1,586 crates (355 directly)

MIT/Apache

240KB
5K SLoC

RustCrypto: RSA

crates.io Documentation Build Status dependency status MSRV Project Chat

A portable RSA implementation in pure Rust.

Example

use rsa::{Pkcs1v15Encrypt, RsaPrivateKey, RsaPublicKey};

let mut rng = rand::thread_rng();
let bits = 2048;
let priv_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
let pub_key = RsaPublicKey::from(&priv_key);

// Encrypt
let data = b"hello world";
let enc_data = pub_key.encrypt(&mut rng, Pkcs1v15Encrypt, &data[..]).expect("failed to encrypt");
assert_ne!(&data[..], &enc_data[..]);

// Decrypt
let dec_data = priv_key.decrypt(Pkcs1v15Encrypt, &enc_data).expect("failed to decrypt");
assert_eq!(&data[..], &dec_data[..]);

Note: If you encounter unusually slow key generation time while using RsaPrivateKey::new you can try to compile in release mode or add the following to your Cargo.toml. Key generation is much faster when building with higher optimization levels, but this will increase the compile time a bit.

[profile.debug]
opt-level = 3

If you don't want to turn on optimizations for all dependencies, you can only optimize the num-bigint-dig dependency. This should give most of the speedups.

[profile.dev.package.num-bigint-dig]
opt-level = 3

Status

Currently at Phase 1 (v) 🚧

There will be three phases before 1.0 🚢 can be released.

  1. 🚧 Make it work
    • Prime generation ✅
    • Key generation ✅
    • PKCS1v1.5: Encryption & Decryption ✅
    • PKCS1v1.5: Sign & Verify ✅
    • PKCS1v1.5 (session key): Encryption & Decryption
    • OAEP: Encryption & Decryption
    • PSS: Sign & Verify
    • Key import & export
  2. 🚀 Make it fast
    • Benchmarks ✅
    • compare to other implementations 🚧
    • optimize 🚧
  3. 🔐 Make it secure
    • Fuzz testing
    • Security Audits

⚠️Security Warning

This crate has received one security audit by Include Security, with only one minor finding which has since been addressed.

See the open security issues on our issue tracker for other known problems.

Notably the implementation of modular exponentiation is not constant time, but timing variability is masked using random blinding, a commonly used technique. This crate is vulnerable to the Marvin Attack which could enable private key recovery by a network attacker (see RUSTSEC-2023-0071).

You can follow our work on mitigating this issue in #390.

Minimum Supported Rust Version (MSRV)

This crate supports Rust 1.72 or higher.

In the future MSRV can be changed, but it will be done with a minor version bump.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~3.5–5MB
~94K SLoC