Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A portable RSA implementation in pure Rust.
## Example

```rust
use rsa::{Pkcs1v15Encrypt, PublicKey, RsaPrivateKey, RsaPublicKey};
use rsa::{Pkcs1v15Encrypt, PublicKey, PrivateKey, RsaPrivateKey, RsaPublicKey};

let mut rng = rand::thread_rng();
let bits = 2048;
Expand Down
36 changes: 24 additions & 12 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ pub trait PublicKeyParts {
}
}

pub trait PrivateKey: DecryptionPrimitive + PublicKeyParts {}
/// Generic trait for operations on a private key.
pub trait PrivateKey: DecryptionPrimitive + PublicKeyParts + Sized {
/// Corresponding public key type
type PublicKey: PublicKey;

/// generate new random private key of the specified bit size
fn new<R: CryptoRngCore + ?Sized>(rng: &mut R, bit_size: usize) -> Result<Self>;

/// convert this private key to the corresponding public key
fn to_public_key(&self) -> Self::PublicKey;
}

/// Represents the public part of an RSA key.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -251,14 +261,24 @@ impl PublicKeyParts for RsaPrivateKey {
}
}

impl PrivateKey for RsaPrivateKey {}
impl PrivateKey for RsaPrivateKey {
type PublicKey = RsaPublicKey;

impl RsaPrivateKey {
/// Generate a new Rsa key pair of the given bit size using the passed in `rng`.
pub fn new<R: CryptoRngCore + ?Sized>(rng: &mut R, bit_size: usize) -> Result<RsaPrivateKey> {
fn new<R: CryptoRngCore + ?Sized>(rng: &mut R, bit_size: usize) -> Result<RsaPrivateKey> {
generate_multi_prime_key(rng, 2, bit_size)
}

/// Get the public key from the private key, cloning `n` and `e`.
///
/// Generally this is not needed since `RsaPrivateKey` implements the `PublicKey` trait,
/// but it can occasionally be useful to discard the private information entirely.
fn to_public_key(&self) -> Self::PublicKey {
self.pubkey_components.clone()
}
}

impl RsaPrivateKey {
/// Generate a new RSA key pair of the given bit size and the public exponent
/// using the passed in `rng`.
///
Expand Down Expand Up @@ -297,14 +317,6 @@ impl RsaPrivateKey {
Ok(k)
}

/// Get the public key from the private key, cloning `n` and `e`.
///
/// Generally this is not needed since `RsaPrivateKey` implements the `PublicKey` trait,
/// but it can occasionally be useful to discard the private information entirely.
pub fn to_public_key(&self) -> RsaPublicKey {
self.pubkey_components.clone()
}

/// Performs some calculations to speed up private key operations.
pub fn precompute(&mut self) -> Result<()> {
if self.precomputed.is_some() {
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//!
//! ## PKCS#1 v1.5 encryption
//! ```
//! use rsa::{PublicKey, RsaPrivateKey, RsaPublicKey, Pkcs1v15Encrypt};
//! use rsa::{PrivateKey, PublicKey, RsaPrivateKey, RsaPublicKey, Pkcs1v15Encrypt};
//!
//! let mut rng = rand::thread_rng();
//!
Expand All @@ -34,7 +34,7 @@
//!
//! ## OAEP encryption
//! ```
//! use rsa::{PublicKey, RsaPrivateKey, RsaPublicKey, Oaep};
//! use rsa::{PrivateKey, PublicKey, RsaPrivateKey, RsaPublicKey, Oaep};
//!
//! let mut rng = rand::thread_rng();
//!
Expand All @@ -60,7 +60,7 @@
//!
#![cfg_attr(feature = "sha2", doc = "```")]
#![cfg_attr(not(feature = "sha2"), doc = "```ignore")]
//! use rsa::RsaPrivateKey;
//! use rsa::{PrivateKey, RsaPrivateKey};
//! use rsa::pkcs1v15::{SigningKey, VerifyingKey};
//! use rsa::signature::{Keypair, RandomizedSigner, SignatureEncoding, Verifier};
//! use rsa::sha2::{Digest, Sha256};
Expand All @@ -87,7 +87,7 @@
//!
#![cfg_attr(feature = "sha2", doc = "```")]
#![cfg_attr(not(feature = "sha2"), doc = "```ignore")]
//! use rsa::RsaPrivateKey;
//! use rsa::{PrivateKey, RsaPrivateKey};
//! use rsa::pss::{BlindedSigningKey, VerifyingKey};
//! use rsa::signature::{Keypair,RandomizedSigner, SignatureEncoding, Verifier};
//! use rsa::sha2::{Digest, Sha256};
Expand All @@ -96,7 +96,7 @@
//!
//! let bits = 2048;
//! let private_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
//! let signing_key = BlindedSigningKey::<Sha256>::new(private_key);
//! let signing_key = BlindedSigningKey::<Sha256, _>::new(private_key);
//! let verifying_key = signing_key.verifying_key();
//!
//! // Sign
Expand Down Expand Up @@ -241,7 +241,7 @@ pub use pkcs8;
pub use sha2;

pub use crate::{
key::{PublicKey, PublicKeyParts, RsaPrivateKey, RsaPublicKey},
key::{PrivateKey, PublicKey, PublicKeyParts, RsaPrivateKey, RsaPublicKey},
oaep::Oaep,
padding::{PaddingScheme, SignatureScheme},
pkcs1v15::{Pkcs1v15Encrypt, Pkcs1v15Sign},
Expand Down
Loading