From ff584d1bb8cfb59ed2aaa37ef028e92e3ba30096 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sat, 7 Mar 2020 09:33:26 +1300 Subject: [PATCH] pkcs1v15: Make decrypt() and sign() generic over PrivateKey --- src/key.rs | 4 ++-- src/pkcs1v15.rs | 17 ++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/key.rs b/src/key.rs index 8718829e..b6f610a1 100644 --- a/src/key.rs +++ b/src/key.rs @@ -544,7 +544,7 @@ impl RSAPrivateKey { pub fn decrypt(&self, padding: PaddingScheme, ciphertext: &[u8]) -> Result> { 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::(None, self, ciphertext), + PaddingScheme::PKCS1v15 => pkcs1v15::decrypt::(None, self, ciphertext), PaddingScheme::OAEP => unimplemented!("not yet implemented"), _ => Err(Error::InvalidPaddingScheme), } @@ -573,7 +573,7 @@ impl RSAPrivateKey { digest: &[u8], ) -> Result> { match padding { - PaddingScheme::PKCS1v15 => pkcs1v15::sign::(None, self, hash, digest), + PaddingScheme::PKCS1v15 => pkcs1v15::sign::(None, self, hash, digest), PaddingScheme::PSS => unimplemented!("not yet implemented"), _ => Err(Error::InvalidPaddingScheme), } diff --git a/src/pkcs1v15.rs b/src/pkcs1v15.rs index 8989d919..d0a2dc1d 100644 --- a/src/pkcs1v15.rs +++ b/src/pkcs1v15.rs @@ -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(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( +pub fn decrypt( rng: Option<&mut R>, - priv_key: &RSAPrivateKey, + priv_key: &SK, ciphertext: &[u8], ) -> Result> { key::check_public(priv_key)?; @@ -66,9 +65,9 @@ pub fn decrypt( // messages to signatures and identify the signed messages. As ever, // signatures provide authenticity, not confidentiality. #[inline] -pub fn sign( +pub fn sign( rng: Option<&mut R>, - priv_key: &RSAPrivateKey, + priv_key: &SK, hash: Option<&H>, hashed: &[u8], ) -> Result> { @@ -150,9 +149,9 @@ fn hash_info(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( +fn decrypt_inner( rng: Option<&mut R>, - priv_key: &RSAPrivateKey, + priv_key: &SK, ciphertext: &[u8], ) -> Result<(u8, Vec, 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]