sad-rsa
A hardened pure Rust RSA implementation with protection against timing side-channel attacks.
This is a security-focused fork of the RustCrypto RSA crate that implements implicit rejection for PKCS#1 v1.5 decryption to mitigate the Marvin Attack (RUSTSEC-2023-0071).
Security Improvements
| Feature | sad-rsa | upstream rsa |
|---|---|---|
| Marvin Attack mitigation | Yes | No |
| Implicit rejection (PKCS#1 v1.5) | Default | Not implemented |
| RFC 8017 length validation | Yes | Partial |
| Key material zeroization | Enhanced | Basic |
Implicit Rejection
Instead of returning distinguishable errors for invalid PKCS#1 v1.5 padding, this crate returns a deterministic pseudo-random message derived from the ciphertext. This makes valid and invalid ciphertexts indistinguishable to attackers, preventing padding oracle attacks.
Implementation follows draft-irtf-cfrg-rsa-guidance-04.
Usage
Replace rsa with sad-rsa in your Cargo.toml:
[]
= "0.1"
The API is fully compatible with the upstream rsa crate:
use ;
let mut rng = thread_rng;
let bits = 2048;
let priv_key = new.expect;
let pub_key = from;
// Encrypt
let data = b"hello world";
let enc_data = pub_key.encrypt.expect;
assert_ne!;
// Decrypt - now protected against Marvin attack
let dec_data = priv_key.decrypt.expect;
assert_eq!;
Migration from rsa
- Replace
rsawithsad-rsainCargo.toml - Replace
use rsa::withuse sad_rsa::in your code - That's it - the API is identical
Note: Invalid ciphertexts will now return synthetic messages instead of errors. If your code explicitly checks for decryption errors to detect tampering, you should use authenticated encryption (e.g., RSA-OAEP or hybrid encryption with AES-GCM) instead.
Performance
Note: Key generation is much faster when building with higher optimization levels:
[] = 2
Minimum Supported Rust Version (MSRV)
This crate supports Rust 1.85 or higher.
Attribution
This crate is a fork of the excellent RustCrypto RSA crate. We are grateful to the RustCrypto developers for their foundational work.
See the NOTICE file for full attribution details.
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.