summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/crypto/parameters/RsaKeyParameters.cs25
-rw-r--r--crypto/src/math/ec/ECCurve.cs43
2 files changed, 29 insertions, 39 deletions
diff --git a/crypto/src/crypto/parameters/RsaKeyParameters.cs b/crypto/src/crypto/parameters/RsaKeyParameters.cs
index dbee49045..f7e88a2b8 100644
--- a/crypto/src/crypto/parameters/RsaKeyParameters.cs
+++ b/crypto/src/crypto/parameters/RsaKeyParameters.cs
@@ -20,18 +20,14 @@ namespace Org.BouncyCastle.Crypto.Parameters
         private static BigInteger Validate(BigInteger modulus)
         {
             if ((modulus.IntValue & 1) == 0)
-                throw new ArgumentException("RSA modulus is even", "modulus");
+                throw new ArgumentException("RSA modulus is even", nameof(modulus));
             if (!modulus.Gcd(SmallPrimesProduct).Equals(BigInteger.One))
-                throw new ArgumentException("RSA modulus has a small prime factor");
+                throw new ArgumentException("RSA modulus has a small prime factor", nameof(modulus));
 
-            int maxBitLength = AsInteger("Org.BouncyCastle.Rsa.MaxSize", 15360);
-
-            int modBitLength = modulus.BitLength;
-            if (maxBitLength < modBitLength)
-            {
+            int maxBitLength = ImplGetInteger("Org.BouncyCastle.Rsa.MaxSize", 16384);
+            if (modulus.BitLength > maxBitLength)
                 throw new ArgumentException("modulus value out of range");
-            }
-        
+
             // TODO: add additional primePower/Composite test - expensive!!
 
             return modulus;
@@ -91,16 +87,11 @@ namespace Org.BouncyCastle.Crypto.Parameters
             return modulus.GetHashCode() ^ exponent.GetHashCode() ^ IsPrivate.GetHashCode();
         }
 
-        internal static int AsInteger(string envVariable, int defaultValue)
+        private static int ImplGetInteger(string envVariable, int defaultValue)
         {
-            string v = Platform.GetEnvironmentVariable(envVariable);
-
-            if (v == null)
-            {
-                return defaultValue;
-            }
+            string property = Platform.GetEnvironmentVariable(envVariable);
 
-            return int.Parse(v);
+            return int.TryParse(property, out int value) ? value : defaultValue;
         }
     }
 }
diff --git a/crypto/src/math/ec/ECCurve.cs b/crypto/src/math/ec/ECCurve.cs
index a540c719e..245ca1941 100644
--- a/crypto/src/math/ec/ECCurve.cs
+++ b/crypto/src/math/ec/ECCurve.cs
@@ -692,7 +692,7 @@ namespace Org.BouncyCastle.Math.EC
             }
             else if (!KnownPrimes.ContainsKey(q))
             {
-                ImplCheckPrime(q);
+                ImplCheckQ(q);
                 KnownPrimes.TryAdd(q, false);
             }
         }
@@ -747,33 +747,21 @@ namespace Org.BouncyCastle.Math.EC
             return CreateRawPoint(x, y);
         }
 
-        private static void ImplCheckPrime(BigInteger q)
+        private static void ImplCheckQ(BigInteger q)
         {
             int maxBitLength = ImplGetInteger("Org.BouncyCastle.EC.Fp_MaxSize", 1042); // 2 * 521
-
-            int qBitLength = q.BitLength;
-            if (maxBitLength < qBitLength)
+            if (q.BitLength > maxBitLength)
                 throw new ArgumentException("Fp q value out of range");
 
-            if (!Primes.HasAnySmallFactors(q))
-            {
-                int certainty = ImplGetInteger("Org.BouncyCastle.EC.Fp_Certainty", 100);
-                int iterations = ImplGetIterations(qBitLength, certainty);
-
-                if (Primes.IsMRProbablePrime(q, SecureRandom.ArbitraryRandom, iterations))
-                    return;
-            }
-
-            throw new ArgumentException("Fp q value not prime");
+            if (!ImplIsPrime(q))
+                throw new ArgumentException("Fp q value not prime");
         }
 
         private static int ImplGetInteger(string envVariable, int defaultValue)
         {
-            string v = Platform.GetEnvironmentVariable(envVariable);
-            if (v == null)
-                return defaultValue;
+            string property = Platform.GetEnvironmentVariable(envVariable);
 
-            return int.Parse(v);
+            return int.TryParse(property, out int value) ? value : defaultValue;
         }
 
         private static int ImplGetIterations(int bits, int certainty)
@@ -808,6 +796,17 @@ namespace Org.BouncyCastle.Math.EC
             }
         }
 
+        private static bool ImplIsPrime(BigInteger q)
+        {
+            if (Primes.HasAnySmallFactors(q))
+                return false;
+
+            int certainty = ImplGetInteger("Org.BouncyCastle.EC.Fp_Certainty", 100);
+            int iterations = ImplGetIterations(q.BitLength, certainty);
+
+            return Primes.IsMRProbablePrime(q, SecureRandom.ArbitraryRandom, iterations);
+        }
+
         private static BigInteger ImplRandomFieldElement(SecureRandom r, BigInteger p)
         {
             BigInteger x;
@@ -849,15 +848,15 @@ namespace Org.BouncyCastle.Math.EC
         }
 
         public FpCurve(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger cofactor)
-            : this(q, a, b, order, cofactor, false)
+            : this(q, a, b, order, cofactor, isInternal: false)
         {
         }
 
-        internal FpCurve(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger cofactor, bool isInternal)
+        internal FpCurve(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger cofactor,
+            bool isInternal)
             : base(q, isInternal)
         {
             this.m_q = q;
-
             this.m_r = FpFieldElement.CalculateResidue(q);
             this.m_infinity = new FpPoint(this, null, null);