Skip to content

Commit 7319d9d

Browse files
committed
Use separate functions for different padding schemes
1 parent 11e62df commit 7319d9d

File tree

1 file changed

+29
-56
lines changed

1 file changed

+29
-56
lines changed

src/key.rs

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
1111
use zeroize::{Zeroize, ZeroizeOnDrop};
1212

1313
use crate::algorithms::generate_multi_prime_key;
14-
use crate::errors::{Error, Result};
14+
use crate::errors::{Error, Result as RsaResult};
1515
use crate::hash::{Hash, Hashes};
1616
use crate::padding::PaddingScheme;
1717
use crate::pkcs1v15;
@@ -129,7 +129,7 @@ impl From<RSAPrivateKey> for RSAPublicKey {
129129

130130
impl RSAPublicKey {
131131
/// Create a new key from its components.
132-
pub fn new(n: BigUint, e: BigUint) -> Result<Self> {
132+
pub fn new(n: BigUint, e: BigUint) -> RsaResult<Self> {
133133
let k = RSAPublicKey { n, e };
134134
check_public(&k)?;
135135

@@ -152,37 +152,28 @@ impl RSAPublicKey {
152152
(self.n().bits() + 7) / 8
153153
}
154154

155-
/// Encrypt the given message
156-
pub fn encrypt<R: Rng>(&self, rng: &mut R, padding: PaddingScheme, msg: &[u8]) -> Result<Vec<u8>> {
157-
match padding {
158-
PaddingScheme::PKCS1v15 => pkcs1v15::encrypt(rng, self, msg),
159-
PaddingScheme::OAEP => unimplemented!("not yet implemented"),
160-
_ => Err(Error::InvalidPaddingScheme),
161-
}
155+
/// Encrypt the given message, using the PKCS1v15 padding scheme.
156+
pub fn encrypt_pkcs1v15<R: Rng>(&self, rng: &mut R, msg: &[u8]) -> RsaResult<Vec<u8>> {
157+
pkcs1v15::encrypt(rng, self, msg)
162158
}
163159

164-
/// Verify a signed message.
160+
/// Verify a message signed with the PKCS1v15 padding scheme.
165161
/// `hashed` must be the result of hashing the input using the hashing function
166162
/// identified using the ASN1 prefix in `hash_asn1_prefix`.
167163
/// If the message is valid `Ok(())` is returned, otherwiese an `Err` indicating failure.
168-
pub fn verify<H: Hash>(
164+
pub fn verify_pkcs1v15<H: Hash>(
169165
&self,
170-
padding: PaddingScheme,
171166
hash: Option<&H>,
172167
hashed: &[u8],
173168
sig: &[u8],
174-
) -> Result<()> {
175-
match padding {
176-
PaddingScheme::PKCS1v15 => pkcs1v15::verify(self, hash, hashed, sig),
177-
PaddingScheme::PSS => pss::verify(self, hash.unwrap(), hashed, sig),
178-
_ => Err(Error::InvalidPaddingScheme),
179-
}
169+
) -> RsaResult<()> {
170+
pkcs1v15::verify(self, hash, hashed, sig)
180171
}
181172
}
182173

183174
impl RSAPrivateKey {
184175
/// Generate a new RSA key pair of the given bit size using the passed in `rng`.
185-
pub fn new<R: Rng>(rng: &mut R, bit_size: usize) -> Result<RSAPrivateKey> {
176+
pub fn new<R: Rng>(rng: &mut R, bit_size: usize) -> RsaResult<RSAPrivateKey> {
186177
generate_multi_prime_key(rng, 2, bit_size)
187178
}
188179

@@ -273,7 +264,7 @@ impl RSAPrivateKey {
273264

274265
/// Performs basic sanity checks on the key.
275266
/// Returns `Ok(())` if everything is good, otherwise an approriate error.
276-
pub fn validate(&self) -> Result<()> {
267+
pub fn validate(&self) -> RsaResult<()> {
277268
check_public(self)?;
278269

279270
// Check that Πprimes == n.
@@ -306,59 +297,41 @@ impl RSAPrivateKey {
306297
Ok(())
307298
}
308299

309-
/// Decrypt the given message.
310-
pub fn decrypt(&self, padding: PaddingScheme, ciphertext: &[u8]) -> Result<Vec<u8>> {
311-
match padding {
312-
// need to pass any Rng as the type arg, so the type checker is happy, it is not actually used for anything
313-
PaddingScheme::PKCS1v15 => pkcs1v15::decrypt::<StdRng>(None, self, ciphertext),
314-
PaddingScheme::OAEP => unimplemented!("not yet implemented"),
315-
_ => Err(Error::InvalidPaddingScheme),
316-
}
300+
/// Decrypt the given message, using the PKCS1v15 padding scheme.
301+
pub fn decrypt_pkcs1v15(&self, ciphertext: &[u8]) -> RsaResult<Vec<u8>> {
302+
pkcs1v15::decrypt::<StdRng>(None, self, ciphertext)
317303
}
318304

319-
/// Decrypt the given message.
305+
/// Decrypt the given message, using the PKCS1v15 padding scheme.
306+
///
320307
/// Uses `rng` to blind the decryption process.
321-
pub fn decrypt_blinded<R: Rng>(
308+
pub fn decrypt_pkcs1v15_blinded<R: Rng>(
322309
&self,
323310
rng: &mut R,
324-
padding: PaddingScheme,
325311
ciphertext: &[u8],
326-
) -> Result<Vec<u8>> {
327-
match padding {
328-
PaddingScheme::PKCS1v15 => pkcs1v15::decrypt(Some(rng), self, ciphertext),
329-
PaddingScheme::OAEP => unimplemented!("not yet implemented"),
330-
_ => Err(Error::InvalidPaddingScheme),
331-
}
312+
) -> RsaResult<Vec<u8>> {
313+
pkcs1v15::decrypt(Some(rng), self, ciphertext)
332314
}
333315

334-
/// Sign the given digest.
335-
pub fn sign<H: Hash>(
316+
/// Sign the given digest using the PKCS1v15 padding scheme.
317+
pub fn sign_pkcs1v15<H: Hash>(
336318
&self,
337-
padding: PaddingScheme,
338319
hash: Option<&H>,
339320
digest: &[u8],
340-
) -> Result<Vec<u8>> {
341-
match padding {
342-
PaddingScheme::PKCS1v15 => pkcs1v15::sign::<StdRng, _>(None, self, hash, digest),
343-
PaddingScheme::PSS => unimplemented!("not yet implemented"),
344-
_ => Err(Error::InvalidPaddingScheme),
345-
}
321+
) -> RsaResult<Vec<u8>> {
322+
pkcs1v15::sign::<StdRng, _>(None, self, hash, digest)
346323
}
347324

348-
/// Sign the given digest.
325+
/// Sign the given digest using the PKCS1v15 padding scheme.
326+
///
349327
/// Use `rng` for blinding.
350-
pub fn sign_blinded<R: Rng>(
328+
pub fn sign_pkcs1v15_blinded<H: Hash, R: Rng>(
351329
&self,
352330
rng: &mut R,
353-
padding: PaddingScheme,
354-
hash: Option<&Hashes>,
331+
hash: Option<&H>,
355332
digest: &[u8],
356-
) -> Result<Vec<u8>> {
357-
match padding {
358-
PaddingScheme::PKCS1v15 => pkcs1v15::sign(Some(rng), self, hash, digest),
359-
PaddingScheme::PSS => pss::sign(rng, self, hash.expect("Can't use None hash with PSS"), digest, None),
360-
_ => Err(Error::InvalidPaddingScheme),
361-
}
333+
) -> RsaResult<Vec<u8>> {
334+
pkcs1v15::sign(Some(rng), self, hash, digest)
362335
}
363336
}
364337

0 commit comments

Comments
 (0)