1212import org .bouncycastle .crypto .params .RSAKeyParameters ;
1313import org .bouncycastle .crypto .params .RSAPrivateCrtKeyParameters ;
1414import org .bouncycastle .util .Arrays ;
15+ import org .bouncycastle .util .Properties ;
1516
1617/**
1718 * this does your basic RSA algorithm.
1819 */
1920class RSACoreEngine
2021{
22+ static final String NO_LENSTRA_CHECK = "org.bouncycastle.rsa.no_lenstra_check" ;
23+
2124 private RSAKeyParameters key ;
2225 private boolean forEncryption ;
2326
@@ -182,7 +185,7 @@ public BigInteger processBlock(BigInteger input)
182185 RSAPrivateCrtKeyParameters crtKey = (RSAPrivateCrtKeyParameters )key ;
183186
184187 BigInteger e = crtKey .getPublicExponent ();
185- if (e != null ) // can't apply fault-attack countermeasure without public exponent
188+ if (e != null || Properties . isOverrideSet ( NO_LENSTRA_CHECK ) ) // can't apply fault-attack countermeasure without public exponent
186189 {
187190 BigInteger p = crtKey .getP ();
188191 BigInteger q = crtKey .getQ ();
@@ -206,11 +209,14 @@ public BigInteger processBlock(BigInteger input)
206209 // m = h * q + mQ
207210 m = h .multiply (q ).add (mQ );
208211
209- // defence against Arjen Lenstra’s CRT attack
210- BigInteger check = m .modPow (e , crtKey .getModulus ());
211- if (!check .equals (input ))
212+ if (e != null )
212213 {
213- throw new IllegalStateException ("RSA engine faulty decryption/signing detected" );
214+ // defence against Arjen Lenstra’s CRT attack
215+ BigInteger check = m .modPow (e , crtKey .getModulus ());
216+ if (!check .equals (input ))
217+ {
218+ throw new IllegalStateException ("RSA engine faulty decryption/signing detected" );
219+ }
214220 }
215221
216222 return m ;
0 commit comments