Merge pull request #44 from str4d/pkcs1v15-generic-privkey

pkcs1v15: Make decrypt() and sign() generic over PrivateKey
This commit is contained in:
Friedel Ziegelmayer 2020-03-06 22:35:23 +01:00 committed by GitHub
commit 94ce39d9b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 11 deletions

View File

@ -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),
}
@ -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),
}

View File

@ -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
@ -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)?;
@ -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>> {
@ -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();
@ -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]