Merge pull request #44 from str4d/pkcs1v15-generic-privkey
pkcs1v15: Make decrypt() and sign() generic over PrivateKey
This commit is contained in:
commit
94ce39d9b7
@ -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),
|
||||
}
|
||||
|
@ -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]
|
||||
|
Loading…
x
Reference in New Issue
Block a user