Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkcs1v15: Make decrypt() and sign() generic over PrivateKey #44

Merged
merged 1 commit into from
Mar 6, 2020
Merged
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
4 changes: 2 additions & 2 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ impl RSAPrivateKey {
pub fn decrypt(&self, padding: PaddingScheme, ciphertext: &[u8]) -> Result<Vec<u8>> {
match padding {
// need to pass any Rng as the type arg, so the type checker is happy, it is not actually used for anything
PaddingScheme::PKCS1v15 => pkcs1v15::decrypt::<ThreadRng>(None, self, ciphertext),
PaddingScheme::PKCS1v15 => pkcs1v15::decrypt::<ThreadRng, _>(None, self, ciphertext),
PaddingScheme::OAEP => unimplemented!("not yet implemented"),
_ => Err(Error::InvalidPaddingScheme),
}
Expand Down Expand Up @@ -573,7 +573,7 @@ impl RSAPrivateKey {
digest: &[u8],
) -> Result<Vec<u8>> {
match padding {
PaddingScheme::PKCS1v15 => pkcs1v15::sign::<ThreadRng, _>(None, self, hash, digest),
PaddingScheme::PKCS1v15 => pkcs1v15::sign::<ThreadRng, _, _>(None, self, hash, digest),
PaddingScheme::PSS => unimplemented!("not yet implemented"),
_ => Err(Error::InvalidPaddingScheme),
}
Expand Down
17 changes: 8 additions & 9 deletions src/pkcs1v15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};

use crate::errors::{Error, Result};
use crate::hash::Hash;
use crate::key::{self, PublicKey, PublicKeyParts, RSAPrivateKey};
use crate::raw::DecryptionPrimitive;
use crate::key::{self, PrivateKey, PublicKey};

// Encrypts the given message with RSA and the padding
// scheme from PKCS#1 v1.5. The message must be no longer than the
Expand Down Expand Up @@ -37,9 +36,9 @@ pub fn encrypt<R: Rng, K: PublicKey>(rng: &mut R, pub_key: &K, msg: &[u8]) -> Re
// forge signatures as if they had the private key. See
// `decrypt_session_key` for a way of solving this problem.
#[inline]
pub fn decrypt<R: Rng>(
pub fn decrypt<R: Rng, SK: PrivateKey>(
rng: Option<&mut R>,
priv_key: &RSAPrivateKey,
priv_key: &SK,
ciphertext: &[u8],
) -> Result<Vec<u8>> {
key::check_public(priv_key)?;
Expand All @@ -66,9 +65,9 @@ pub fn decrypt<R: Rng>(
// messages to signatures and identify the signed messages. As ever,
// signatures provide authenticity, not confidentiality.
#[inline]
pub fn sign<R: Rng, H: Hash>(
pub fn sign<R: Rng, SK: PrivateKey, H: Hash>(
rng: Option<&mut R>,
priv_key: &RSAPrivateKey,
priv_key: &SK,
hash: Option<&H>,
hashed: &[u8],
) -> Result<Vec<u8>> {
Expand Down Expand Up @@ -150,9 +149,9 @@ fn hash_info<H: Hash>(hash: Option<&H>, digest_len: usize) -> Result<(usize, Vec
/// in order to maintain constant memory access patterns. If the plaintext was
/// valid then index contains the index of the original message in em.
#[inline]
fn decrypt_inner<R: Rng>(
fn decrypt_inner<R: Rng, SK: PrivateKey>(
rng: Option<&mut R>,
priv_key: &RSAPrivateKey,
priv_key: &SK,
ciphertext: &[u8],
) -> Result<(u8, Vec<u8>, u32)> {
let k = priv_key.size();
Expand Down Expand Up @@ -220,7 +219,7 @@ mod tests {
use sha1::{Digest, Sha1};

use crate::hash::Hashes;
use crate::key::RSAPublicKey;
use crate::key::{PublicKeyParts, RSAPrivateKey, RSAPublicKey};
use crate::padding::PaddingScheme;

#[test]
Expand Down