From 6793e321d2869420fe7580e67640e3c155266bbd Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 15 Feb 2023 18:19:12 +0700 Subject: PublicExponent is never null --- crypto/src/crypto/engines/RSABlindedEngine.cs | 37 ++++++++++----------------- 1 file 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 { -- cgit 1.4.1