@@ -37,7 +37,6 @@ pub use self::{
3737 signing_key:: SigningKey , verifying_key:: VerifyingKey ,
3838} ;
3939
40- #[ cfg( feature = "implicit_rejection" ) ]
4140use crate :: algorithms:: pkcs1v15:: { pkcs1v15_encrypt_unpad_implicit_rejection, KeyDerivationKey } ;
4241
4342use alloc:: { boxed:: Box , vec:: Vec } ;
@@ -52,7 +51,6 @@ use crate::algorithms::pkcs1v15::*;
5251use crate :: algorithms:: rsa:: { rsa_decrypt_and_check, rsa_encrypt} ;
5352use crate :: errors:: { Error , Result } ;
5453use crate :: key:: { self , RsaPrivateKey , RsaPublicKey } ;
55- #[ cfg( feature = "implicit_rejection" ) ]
5654use crate :: traits:: PrivateKeyParts ;
5755use crate :: traits:: { PaddingScheme , PublicKeyParts , SignatureScheme } ;
5856
@@ -61,32 +59,6 @@ use crate::traits::{PaddingScheme, PublicKeyParts, SignatureScheme};
6159pub struct Pkcs1v15Encrypt ;
6260
6361impl PaddingScheme for Pkcs1v15Encrypt {
64- fn decrypt < Rng : TryCryptoRng + ?Sized > (
65- self ,
66- rng : Option < & mut Rng > ,
67- priv_key : & RsaPrivateKey ,
68- ciphertext : & [ u8 ] ,
69- ) -> Result < Vec < u8 > > {
70- decrypt ( rng, priv_key, ciphertext)
71- }
72-
73- fn encrypt < Rng : TryCryptoRng + ?Sized > (
74- self ,
75- rng : & mut Rng ,
76- pub_key : & RsaPublicKey ,
77- msg : & [ u8 ] ,
78- ) -> Result < Vec < u8 > > {
79- encrypt ( rng, pub_key, msg)
80- }
81- }
82-
83- /// Encryption using PKCS#1 v1.5 padding with implicit rejection.
84- #[ cfg( feature = "implicit_rejection" ) ]
85- #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq ) ]
86- pub struct Pkcs1v15EncryptImplicitRejection ;
87-
88- #[ cfg( feature = "implicit_rejection" ) ]
89- impl PaddingScheme for Pkcs1v15EncryptImplicitRejection {
9062 fn decrypt < Rng : TryCryptoRng + ?Sized > (
9163 self ,
9264 rng : Option < & mut Rng > ,
@@ -190,43 +162,18 @@ fn encrypt<R: TryCryptoRng + ?Sized>(
190162 uint_to_be_pad ( rsa_encrypt ( pub_key, & int) ?, pub_key. size ( ) )
191163}
192164
193- /// Decrypts a plaintext using RSA and the padding scheme from PKCS#1 v1.5.
194- ///
195- /// If an `rng` is passed, it uses RSA blinding to avoid timing side-channel attacks.
196- ///
197- /// Note that whether this function returns an error or not discloses secret
198- /// information. If an attacker can cause this function to run repeatedly and
199- /// learn whether each instance returned an error then they can decrypt and
200- /// forge signatures as if they had the private key. See
201- /// `decrypt_session_key` for a way of solving this problem.
202- #[ inline]
203- fn decrypt < R : TryCryptoRng + ?Sized > (
204- rng : Option < & mut R > ,
205- priv_key : & RsaPrivateKey ,
206- ciphertext : & [ u8 ] ,
207- ) -> Result < Vec < u8 > > {
208- key:: check_public ( priv_key) ?;
209-
210- let ciphertext = BoxedUint :: from_be_slice ( ciphertext, priv_key. n_bits_precision ( ) ) ?;
211- let em = rsa_decrypt_and_check ( priv_key, rng, & ciphertext) ?;
212- let em = uint_to_zeroizing_be_pad ( em, priv_key. size ( ) ) ?;
213-
214- pkcs1v15_encrypt_unpad ( em, priv_key. size ( ) )
215- }
216-
217165/// Decrypts plaintext using RSA and the PKCS#1 v1.5 padding scheme with implicit rejection.
218166///
219167/// If an `rng` is provided, RSA blinding is used to avoid timing side-channel attacks.
220168///
221- /// Unlike [`decrypt`], this function does not return an error if
169+ /// This function does not return an error if
222170/// the padding is invalid. Instead, it deterministically generates and returns
223171/// a replacement random message using a key-derivation function.
224172/// As a result, callers cannot distinguish between valid and
225173/// invalid paddings based on the output, thus reducing the risk of side-channel attacks.
226174///
227175/// See
228176/// [draft-irtf-cfrg-rsa-guidance-08](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-rsa-guidance-08)
229- #[ cfg( feature = "implicit_rejection" ) ]
230177#[ inline]
231178fn decrypt_implicit_rejection < R : TryCryptoRng + ?Sized > (
232179 rng : Option < & mut R > ,
@@ -299,25 +246,21 @@ mod oid {
299246 const_oid:: ObjectIdentifier :: new_unwrap ( "1.2.840.113549.1.1.5" ) ;
300247 }
301248
302- #[ cfg( feature = "sha2" ) ]
303249 impl RsaSignatureAssociatedOid for sha2:: Sha224 {
304250 const OID : ObjectIdentifier =
305251 const_oid:: ObjectIdentifier :: new_unwrap ( "1.2.840.113549.1.1.14" ) ;
306252 }
307253
308- #[ cfg( feature = "sha2" ) ]
309254 impl RsaSignatureAssociatedOid for sha2:: Sha256 {
310255 const OID : ObjectIdentifier =
311256 const_oid:: ObjectIdentifier :: new_unwrap ( "1.2.840.113549.1.1.11" ) ;
312257 }
313258
314- #[ cfg( feature = "sha2" ) ]
315259 impl RsaSignatureAssociatedOid for sha2:: Sha384 {
316260 const OID : ObjectIdentifier =
317261 const_oid:: ObjectIdentifier :: new_unwrap ( "1.2.840.113549.1.1.12" ) ;
318262 }
319263
320- #[ cfg( feature = "sha2" ) ]
321264 impl RsaSignatureAssociatedOid for sha2:: Sha512 {
322265 const OID : ObjectIdentifier =
323266 const_oid:: ObjectIdentifier :: new_unwrap ( "1.2.840.113549.1.1.13" ) ;
@@ -416,7 +359,7 @@ mod tests {
416359
417360 let blind: bool = rng. next_u32 ( ) < ( 1u32 << 31 ) ;
418361 let blinder = if blind { Some ( & mut rng) } else { None } ;
419- let plaintext = decrypt ( blinder, & priv_key, & ciphertext) . unwrap ( ) ;
362+ let plaintext = decrypt_implicit_rejection ( blinder, & priv_key, & ciphertext) . unwrap ( ) ;
420363 assert_eq ! ( input, plaintext) ;
421364 }
422365 }
0 commit comments