2023-04-05 13:57:05 -06:00
# [RustCrypto]: RSA
2020-12-02 17:12:48 +01:00
2021-02-12 22:15:12 +00:00
[![crates.io][crate-image]][crate-link]
[![Documentation][doc-image]][doc-link]
[![Build Status][build-image]][build-link]
2023-04-05 13:57:05 -06:00
[![dependency status][deps-image]][deps-link]
2023-03-05 18:34:51 -07:00
![MSRV][msrv-image]
2021-02-12 22:15:12 +00:00
[![Project Chat][chat-image]][chat-link]
2018-07-17 21:16:31 +02:00
2018-12-03 10:47:09 +00:00
A portable RSA implementation in pure Rust.
2018-07-17 21:16:31 +02:00
2019-02-20 20:54:35 +01:00
## Example
```rust
Remove primitive traits (#300)
The crate contains several exported traits targeting
hardware-accelerated implementations (PublicKey, PrivateKey,
EncryptionPrimitive, DecriptionPrimitive). However these traits
overcomplicate internal structure of the crate. It is not clear, which
level of API can be implemented by the hardware accelerators.
The crate is already quite complicated, implementing both
PaddingScheme-based API and Signer/Verifier/Encryptor/Decryptor API.
Remove the complication for now. The proper level of indirection can be
introduced once support for actual hardware accelerators is implemented.
Inline and drop the RsaPrivateKey::raw_decryption_primitive() function.
There is no need to zeroize argument, it is ciphertext, so it can be
assumed to be safe.
Change raw_int_decryption_primitive() and raw_int_decryption_primitive()
to output Result<BigUint> instead of Result<Vec<u8>>, because they also
take BigUint rather than Vec<u8> or &[u8].
In order to simplify adding support for RSA hardware accelerators, move
all formatting and padding functions to a separate modules, making it
theoretically possible to use that for implementing support for
low-level RSA hardware accelerators.
Also follows the pkcs1v15 change and use BigUint as a Signature's
internal implementation.
Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
2023-04-19 17:51:06 +03:00
use rsa::{Pkcs1v15Encrypt, RsaPrivateKey, RsaPublicKey};
2019-02-20 20:54:35 +01:00
2022-03-13 19:50:05 +00:00
let mut rng = rand::thread_rng();
2019-02-20 20:54:35 +01:00
let bits = 2048;
2021-07-26 23:25:13 +02:00
let priv_key = RsaPrivateKey::new(& mut rng, bits).expect("failed to generate a key");
let pub_key = RsaPublicKey::from(&priv_key);
2019-02-20 20:54:35 +01:00
// Encrypt
let data = b"hello world";
2023-03-24 16:26:46 +01:00
let enc_data = pub_key.encrypt(& mut rng, Pkcs1v15Encrypt, & data[..]).expect("failed to encrypt");
2019-02-20 20:54:35 +01:00
assert_ne!(& data[..], &enc_data[..]);
// Decrypt
2023-03-24 16:26:46 +01:00
let dec_data = priv_key.decrypt(Pkcs1v15Encrypt, & enc_data).expect("failed to decrypt");
2019-02-20 20:54:35 +01:00
assert_eq!(& data[..], &dec_data[..]);
```
2021-07-26 23:25:13 +02:00
> **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.
2021-07-26 22:44:31 +02:00
> ```toml
2021-07-26 03:58:58 -05:00
> [profile.debug]
> opt-level = 3
> ```
2021-07-26 22:44:31 +02:00
> 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.
> ```toml
> [profile.dev.package.num-bigint-dig]
> opt-level = 3
> ```
2021-07-26 03:58:58 -05:00
2018-07-17 21:16:31 +02:00
## Status
2021-07-27 08:54:10 -07:00
Currently at Phase 1 (v) 🚧
2018-07-17 21:16:31 +02:00
2021-07-27 08:54:10 -07:00
There will be three phases before `1.0` 🚢 can be released.
2018-07-17 21:16:31 +02:00
2021-07-27 08:54:10 -07:00
1. 🚧 Make it work
- [x] Prime generation ✅
- [x] Key generation ✅
- [x] PKCS1v1.5: Encryption & Decryption ✅
- [x] PKCS1v1.5: Sign & Verify ✅
2019-02-20 20:29:49 +01:00
- [ ] PKCS1v1.5 (session key): Encryption & Decryption
2020-03-06 20:05:24 +01:00
- [x] OAEP: Encryption & Decryption
2020-03-06 21:11:24 +01:00
- [x] PSS: Sign & Verify
2019-02-20 20:29:49 +01:00
- [x] Key import & export
2021-07-27 08:54:10 -07:00
2. 🚀 Make it fast
- [x] Benchmarks ✅
- [ ] compare to other implementations 🚧
- [ ] optimize 🚧
3. 🔐 Make it secure
2019-02-20 20:29:49 +01:00
- [ ] Fuzz testing
- [ ] Security Audits
2018-07-17 21:16:31 +02:00
2023-04-05 08:45:37 -06:00
## Security Notes
This crate has received one [security audit by Include Security][audit], with
only one minor finding which has since been addressed.
2018-07-17 21:16:31 +02:00
2023-04-27 12:57:20 -06:00
See the [open security issues] on our issue tracker for other known problems.
2020-12-02 17:49:18 +01:00
## Minimum Supported Rust Version (MSRV)
2023-03-06 13:31:16 -07:00
All crates in this repository support Rust 1.65 or higher.
In the future MSRV can be changed, but it will be done with a minor version bump.
2020-12-02 17:49:18 +01:00
2018-07-17 21:16:31 +02:00
## License
2018-12-03 10:47:09 +00:00
Licensed under either of
* [Apache License, Version 2.0 ](http://www.apache.org/licenses/LICENSE-2.0 )
* [MIT license ](http://opensource.org/licenses/MIT )
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
2019-02-20 20:29:49 +01:00
dual licensed as above, without any additional terms or conditions.
2021-02-12 22:15:12 +00:00
[//]: # (badges)
2023-04-05 13:57:05 -06:00
[crate-image]: https://buildstats.info/crate/rsa
2021-02-12 22:15:12 +00:00
[crate-link]: https://crates.io/crates/rsa
[doc-image]: https://docs.rs/rsa/badge.svg
[doc-link]: https://docs.rs/rsa
[build-image]: https://github.com/rustcrypto/RSA/workflows/CI/badge.svg
[build-link]: https://github.com/RustCrypto/RSA/actions?query=workflow%3ACI+branch%3Amaster
2023-03-06 13:31:16 -07:00
[msrv-image]: https://img.shields.io/badge/rustc-1.65+-blue.svg
2021-02-12 22:15:12 +00:00
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260047 -RSA
[deps-image]: https://deps.rs/repo/github/RustCrypto/RSA/status.svg
[deps-link]: https://deps.rs/repo/github/RustCrypto/RSA
2023-04-05 08:45:37 -06:00
[//]: # (links)
2023-04-05 13:57:05 -06:00
[RustCrypto]: https://github.com/RustCrypto/
2023-04-05 08:45:37 -06:00
[audit]: https://www.opentech.fund/results/security-safety-audits/deltachat/
2023-04-27 12:57:20 -06:00
[open security issues]: https://github.com/RustCrypto/RSA/issues?q=is%3Aissue+is%3Aopen+label%3Asecurity