summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-02-15 18:19:12 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-02-15 18:19:12 +0700
commit6793e321d2869420fe7580e67640e3c155266bbd (patch)
tree63252f23f0185e598f6fd655405aa1fc086b7116
parentRework AsconEngine (diff)
downloadBouncyCastle.NET-ed25519-6793e321d2869420fe7580e67640e3c155266bbd.tar.xz
PublicExponent is never null
-rw-r--r--crypto/src/crypto/engines/RSABlindedEngine.cs37
1 files changed, 13 insertions, 24 deletions
diff --git a/crypto/src/crypto/engines/RSABlindedEngine.cs b/crypto/src/crypto/engines/RSABlindedEngine.cs
index cdc0a7844..d207f617c 100644
--- a/crypto/src/crypto/engines/RSABlindedEngine.cs
+++ b/crypto/src/crypto/engines/RSABlindedEngine.cs
@@ -104,10 +104,7 @@ namespace Org.BouncyCastle.Crypto.Engines
          * @return the result of the RSA process.
          * @exception DataLengthException the input block is too large.
          */
-        public virtual byte[] ProcessBlock(
-            byte[] inBuf,
-            int inOff,
-            int inLen)
+        public virtual byte[] ProcessBlock(byte[] inBuf, int inOff, int inLen)
         {
             if (key == null)
                 throw new InvalidOperationException("RSA engine not initialised");
@@ -115,30 +112,22 @@ namespace Org.BouncyCastle.Crypto.Engines
             BigInteger input = core.ConvertInput(inBuf, inOff, inLen);
 
             BigInteger result;
-            if (key is RsaPrivateCrtKeyParameters)
+            if (key is RsaPrivateCrtKeyParameters crt)
             {
-                RsaPrivateCrtKeyParameters k = (RsaPrivateCrtKeyParameters)key;
-                BigInteger e = k.PublicExponent;
-                if (e != null)   // can't do blinding without a public exponent
-                {
-                    BigInteger m = k.Modulus;
-                    BigInteger r = BigIntegers.CreateRandomInRange(
-                        BigInteger.One, m.Subtract(BigInteger.One), random);
+                BigInteger e = crt.PublicExponent;
+                BigInteger m = crt.Modulus;
+                BigInteger r = BigIntegers.CreateRandomInRange(
+                    BigInteger.One, m.Subtract(BigInteger.One), random);
 
-                    BigInteger blindedInput = r.ModPow(e, m).Multiply(input).Mod(m);
-                    BigInteger blindedResult = core.ProcessBlock(blindedInput);
+                BigInteger blindedInput = r.ModPow(e, m).Multiply(input).Mod(m);
+                BigInteger blindedResult = core.ProcessBlock(blindedInput);
 
-                    BigInteger rInv = BigIntegers.ModOddInverse(m, r);
-                    result = blindedResult.Multiply(rInv).Mod(m);
+                BigInteger rInv = BigIntegers.ModOddInverse(m, r);
+                result = blindedResult.Multiply(rInv).Mod(m);
 
-                    // defence against Arjen Lenstra’s CRT attack
-                    if (!input.Equals(result.ModPow(e, m)))
-                        throw new InvalidOperationException("RSA engine faulty decryption/signing detected");
-                }
-                else
-                {
-                    result = core.ProcessBlock(input);
-                }
+                // defence against Arjen Lenstra’s CRT attack
+                if (!input.Equals(result.ModPow(e, m)))
+                    throw new InvalidOperationException("RSA engine faulty decryption/signing detected");
             }
             else
             {