summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2015-10-18 16:56:07 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2015-10-18 16:56:07 +0700
commita59dcb14e72b216b1a52ba797aa4a7ec8f586547 (patch)
treee16201c88ba020167a3b2bb8ec65ffc901255a37
parentPCL: Various non-IO changes (diff)
downloadBouncyCastle.NET-ed25519-a59dcb14e72b216b1a52ba797aa4a7ec8f586547.tar.xz
Remove the 'debug' logging from NaccacheStern
-rw-r--r--crypto/src/crypto/engines/NaccacheSternEngine.cs84
-rw-r--r--crypto/src/crypto/generators/NaccacheSternKeyPairGenerator.cs77
-rw-r--r--crypto/src/crypto/parameters/NaccacheSternKeyGenerationParameters.cs49
3 files changed, 34 insertions, 176 deletions
diff --git a/crypto/src/crypto/engines/NaccacheSternEngine.cs b/crypto/src/crypto/engines/NaccacheSternEngine.cs
index e547e0caf..64665c1d4 100644
--- a/crypto/src/crypto/engines/NaccacheSternEngine.cs
+++ b/crypto/src/crypto/engines/NaccacheSternEngine.cs
@@ -20,9 +20,7 @@ namespace Org.BouncyCastle.Crypto.Engines
 
 		private IList[] lookup = null;
 
-		private bool debug = false;
-
-		public string AlgorithmName
+        public string AlgorithmName
 		{
 			get { return "NaccacheStern"; }
 		}
@@ -49,10 +47,6 @@ namespace Org.BouncyCastle.Crypto.Engines
 			// construct lookup table for faster decryption if necessary
 			if (!this.forEncryption)
 			{
-				if (debug)
-				{
-					Console.WriteLine("Constructing lookup Array");
-				}
 				NaccacheSternPrivateKeyParameters priv = (NaccacheSternPrivateKeyParameters)key;
 				IList primes = priv.SmallPrimesList;
 				lookup = new IList[primes.Count];
@@ -64,11 +58,6 @@ namespace Org.BouncyCastle.Crypto.Engines
 					lookup[i] = Platform.CreateArrayList(actualPrimeValue);
 					lookup[i].Add(BigInteger.One);
 
-					if (debug)
-					{
-						Console.WriteLine("Constructing lookup ArrayList for " + actualPrimeValue);
-					}
-
 					BigInteger accJ = BigInteger.Zero;
 
 					for (int j = 1; j < actualPrimeValue; j++)
@@ -83,12 +72,13 @@ namespace Org.BouncyCastle.Crypto.Engines
 			}
 		}
 
+        [Obsolete("Remove: no longer used")]
         public virtual bool Debug
 		{
-			set { this.debug = value; }
+			set {}
 		}
 
-		/**
+        /**
 		* Returns the input block size of this algorithm.
 		*
 		* @see org.bouncycastle.crypto.AsymmetricBlockCipher#GetInputBlockSize()
@@ -156,11 +146,6 @@ namespace Org.BouncyCastle.Crypto.Engines
 			// transform input into BigInteger
 			BigInteger input = new BigInteger(1, inBytes, inOff, length);
 
-			if (debug)
-			{
-				Console.WriteLine("input as BigInteger: " + input);
-			}
-
 			byte[] output;
 			if (forEncryption)
 			{
@@ -178,10 +163,6 @@ namespace Org.BouncyCastle.Crypto.Engines
 					IList al = lookup[i];
 					if (lookup[i].Count != ((BigInteger)primes[i]).IntValue)
 					{
-						if (debug)
-						{
-							Console.WriteLine("Prime is " + primes[i] + ", lookup table has size " + al.Count);
-						}
 						throw new InvalidCipherTextException("Error in lookup Array for "
 										+ ((BigInteger)primes[i]).IntValue
 										+ ": Size mismatch. Expected ArrayList with length "
@@ -192,18 +173,6 @@ namespace Org.BouncyCastle.Crypto.Engines
 
 					if (lookedup == -1)
 					{
-						if (debug)
-						{
-							Console.WriteLine("Actual prime is " + primes[i]);
-							Console.WriteLine("Decrypted value is " + exp);
-
-							Console.WriteLine("LookupList for " + primes[i] + " with size " + lookup[i].Count
-											+ " is: ");
-							for (int j = 0; j < lookup[i].Count; j++)
-							{
-								Console.WriteLine(lookup[i][j]);
-							}
-						}
 						throw new InvalidCipherTextException("Lookup failed");
 					}
 					plain.Add(BigInteger.ValueOf(lookedup));
@@ -218,16 +187,9 @@ namespace Org.BouncyCastle.Crypto.Engines
 				// to guess them. But as stated in the paper, this is not a security
 				// breach. So we can just work with the correct sigma.
 
-				// if (debug) {
-				//      Console.WriteLine("Decryption is " + test);
-				// }
 				// if ((key.G.ModPow(test, key.Modulus)).Equals(input)) {
 				//      output = test.ToByteArray();
 				// } else {
-				//      if(debug){
-				//          Console.WriteLine("Engine seems to be used as an oracle,
-				//          returning null");
-				//      }
 				//      output = null;
 				// }
 
@@ -256,10 +218,6 @@ namespace Org.BouncyCastle.Crypto.Engines
 
 			byte[] tmp = key.G.ModPow(plain, key.Modulus).ToByteArray();
 			Array.Copy(tmp, 0, output, output.Length - tmp.Length, tmp.Length);
-			if (debug)
-			{
-				Console.WriteLine("Encrypted value is:  " + new BigInteger(output));
-			}
 			return output;
 		}
 
@@ -302,14 +260,8 @@ namespace Org.BouncyCastle.Crypto.Engines
 			BigInteger m2Crypt = new BigInteger(1, block2);
 			BigInteger m1m2Crypt = m1Crypt.Multiply(m2Crypt);
 			m1m2Crypt = m1m2Crypt.Mod(key.Modulus);
-			if (debug)
-			{
-				Console.WriteLine("c(m1) as BigInteger:....... " + m1Crypt);
-				Console.WriteLine("c(m2) as BigInteger:....... " + m2Crypt);
-				Console.WriteLine("c(m1)*c(m2)%n = c(m1+m2)%n: " + m1m2Crypt);
-			}
 
-			//byte[] output = key.Modulus.ToByteArray();
+            //byte[] output = key.Modulus.ToByteArray();
 			//Array.Clear(output, 0, output.Length);
 			byte[] output = new byte[key.Modulus.BitLength / 8 + 1];
 
@@ -332,20 +284,10 @@ namespace Org.BouncyCastle.Crypto.Engines
         public virtual byte[] ProcessData(
 			byte[] data)
 		{
-			if (debug)
-			{
-				Console.WriteLine();
-			}
 			if (data.Length > GetInputBlockSize())
 			{
 				int inBlocksize = GetInputBlockSize();
 				int outBlocksize = GetOutputBlockSize();
-				if (debug)
-				{
-					Console.WriteLine("Input blocksize is:  " + inBlocksize + " bytes");
-					Console.WriteLine("Output blocksize is: " + outBlocksize + " bytes");
-					Console.WriteLine("Data has length:.... " + data.Length + " bytes");
-				}
 				int datapos = 0;
 				int retpos = 0;
 				byte[] retval = new byte[(data.Length / inBlocksize + 1) * outBlocksize];
@@ -362,10 +304,6 @@ namespace Org.BouncyCastle.Crypto.Engines
 						tmp = ProcessBlock(data, datapos, data.Length - datapos);
 						datapos += data.Length - datapos;
 					}
-					if (debug)
-					{
-						Console.WriteLine("new datapos is " + datapos);
-					}
 					if (tmp != null)
 					{
 						tmp.CopyTo(retval, retpos);
@@ -373,27 +311,15 @@ namespace Org.BouncyCastle.Crypto.Engines
 					}
 					else
 					{
-						if (debug)
-						{
-							Console.WriteLine("cipher returned null");
-						}
 						throw new InvalidCipherTextException("cipher returned null");
 					}
 				}
 				byte[] ret = new byte[retpos];
 				Array.Copy(retval, 0, ret, 0, retpos);
-				if (debug)
-				{
-					Console.WriteLine("returning " + ret.Length + " bytes");
-				}
 				return ret;
 			}
 			else
 			{
-				if (debug)
-				{
-					Console.WriteLine("data size is less then input block size, processing directly");
-				}
 				return ProcessBlock(data, 0, data.Length);
 			}
 		}
diff --git a/crypto/src/crypto/generators/NaccacheSternKeyPairGenerator.cs b/crypto/src/crypto/generators/NaccacheSternKeyPairGenerator.cs
index 7011cf253..afc566d87 100644
--- a/crypto/src/crypto/generators/NaccacheSternKeyPairGenerator.cs
+++ b/crypto/src/crypto/generators/NaccacheSternKeyPairGenerator.cs
@@ -51,14 +51,8 @@ namespace Org.BouncyCastle.Crypto.Generators
 			int strength = param.Strength;
 			SecureRandom rand = param.Random;
 			int certainty = param.Certainty;
-			bool debug = param.IsDebug;
 
-			if (debug)
-			{
-				Console.WriteLine("Fetching first " + param.CountSmallPrimes + " primes.");
-			}
-
-			IList smallPrimes = findFirstPrimes(param.CountSmallPrimes);
+            IList smallPrimes = findFirstPrimes(param.CountSmallPrimes);
 
 			smallPrimes = permuteList(smallPrimes, rand);
 
@@ -92,12 +86,8 @@ namespace Org.BouncyCastle.Crypto.Generators
 			BigInteger q;
 
 			long tries = 0;
-			if (debug)
-			{
-				Console.WriteLine("generating p and q");
-			}
 
-			BigInteger _2au = a.Multiply(u).ShiftLeft(1);
+            BigInteger _2au = a.Multiply(u).ShiftLeft(1);
 			BigInteger _2bv = b.Multiply(v).ShiftLeft(1);
 
 			for (;;)
@@ -126,36 +116,23 @@ namespace Org.BouncyCastle.Crypto.Generators
 
 				if (!sigma.Gcd(_p.Multiply(_q)).Equals(BigInteger.One))
 				{
-					Console.WriteLine("sigma.gcd(_p.mult(_q)) != 1!\n _p: " + _p +"\n _q: "+ _q );
+                    //Console.WriteLine("sigma.gcd(_p.mult(_q)) != 1!\n _p: " + _p +"\n _q: "+ _q );
 					continue;
 				}
 
 				if (p.Multiply(q).BitLength < strength)
 				{
-					if (debug)
-					{
-						Console.WriteLine("key size too small. Should be " + strength + " but is actually "
-							+ p.Multiply(q).BitLength);
-					}
 					continue;
 				}
 				break;
 			}
 
-			if (debug)
-			{
-				Console.WriteLine("needed " + tries + " tries to generate p and q.");
-			}
-
 			BigInteger n = p.Multiply(q);
 			BigInteger phi_n = p.Subtract(BigInteger.One).Multiply(q.Subtract(BigInteger.One));
 			BigInteger g;
 			tries = 0;
-			if (debug)
-			{
-				Console.WriteLine("generating g");
-			}
-			for (;;)
+
+            for (;;)
 			{
 				// TODO After the first loop, just regenerate one randomly-selected gPart each time?
 				IList gParts = Platform.CreateArrayList();
@@ -191,10 +168,6 @@ namespace Org.BouncyCastle.Crypto.Generators
 				{
 					if (g.ModPow(phi_n.Divide((BigInteger)smallPrimes[i]), n).Equals(BigInteger.One))
 					{
-						if (debug)
-						{
-							Console.WriteLine("g has order phi(n)/" + smallPrimes[i] + "\n g: " + g);
-						}
 						divisible = true;
 						break;
 					}
@@ -210,67 +183,29 @@ namespace Org.BouncyCastle.Crypto.Generators
 				//if (g.ModPow(phi_n.Divide(BigInteger.ValueOf(4)), n).Equals(BigInteger.One))
 				if (g.ModPow(phi_n.ShiftRight(2), n).Equals(BigInteger.One))
 				{
-					if (debug)
-					{
-						Console.WriteLine("g has order phi(n)/4\n g:" + g);
-					}
 					continue;
 				}
 
 				if (g.ModPow(phi_n.Divide(_p), n).Equals(BigInteger.One))
 				{
-					if (debug)
-					{
-						Console.WriteLine("g has order phi(n)/p'\n g: " + g);
-					}
 					continue;
 				}
 				if (g.ModPow(phi_n.Divide(_q), n).Equals(BigInteger.One))
 				{
-					if (debug)
-					{
-						Console.WriteLine("g has order phi(n)/q'\n g: " + g);
-					}
 					continue;
 				}
 				if (g.ModPow(phi_n.Divide(a), n).Equals(BigInteger.One))
 				{
-					if (debug)
-					{
-						Console.WriteLine("g has order phi(n)/a\n g: " + g);
-					}
 					continue;
 				}
 				if (g.ModPow(phi_n.Divide(b), n).Equals(BigInteger.One))
 				{
-					if (debug)
-					{
-						Console.WriteLine("g has order phi(n)/b\n g: " + g);
-					}
 					continue;
 				}
 				break;
 			}
-			if (debug)
-			{
-				Console.WriteLine("needed " + tries + " tries to generate g");
-				Console.WriteLine();
-				Console.WriteLine("found new NaccacheStern cipher variables:");
-				Console.WriteLine("smallPrimes: " + CollectionUtilities.ToString(smallPrimes));
-				Console.WriteLine("sigma:...... " + sigma + " (" + sigma.BitLength + " bits)");
-				Console.WriteLine("a:.......... " + a);
-				Console.WriteLine("b:.......... " + b);
-				Console.WriteLine("p':......... " + _p);
-				Console.WriteLine("q':......... " + _q);
-				Console.WriteLine("p:.......... " + p);
-				Console.WriteLine("q:.......... " + q);
-				Console.WriteLine("n:.......... " + n);
-				Console.WriteLine("phi(n):..... " + phi_n);
-				Console.WriteLine("g:.......... " + g);
-				Console.WriteLine();
-			}
 
-			return new AsymmetricCipherKeyPair(new NaccacheSternKeyParameters(false, g, n, sigma.BitLength),
+            return new AsymmetricCipherKeyPair(new NaccacheSternKeyParameters(false, g, n, sigma.BitLength),
 				new NaccacheSternPrivateKeyParameters(g, n, sigma.BitLength, smallPrimes, phi_n));
 		}
 
diff --git a/crypto/src/crypto/parameters/NaccacheSternKeyGenerationParameters.cs b/crypto/src/crypto/parameters/NaccacheSternKeyGenerationParameters.cs
index 5b4052505..44fc906b5 100644
--- a/crypto/src/crypto/parameters/NaccacheSternKeyGenerationParameters.cs
+++ b/crypto/src/crypto/parameters/NaccacheSternKeyGenerationParameters.cs
@@ -17,9 +17,8 @@ namespace Org.BouncyCastle.Crypto.Parameters
 		// private BigInteger publicExponent;
 		private readonly int certainty;
 		private readonly int countSmallPrimes;
-		private bool debug;
 
-		/**
+        /**
 		 * Parameters for generating a NaccacheStern KeyPair.
 		 *
 		 * @param random
@@ -37,9 +36,16 @@ namespace Org.BouncyCastle.Crypto.Parameters
 			int				strength,
 			int				certainty,
 			int				countSmallPrimes)
-			: this(random, strength, certainty, countSmallPrimes, false)
-		{
-		}
+            : base(random, strength)
+        {
+            if (countSmallPrimes % 2 == 1)
+                throw new ArgumentException("countSmallPrimes must be a multiple of 2");
+            if (countSmallPrimes < 30)
+                throw new ArgumentException("countSmallPrimes must be >= 30 for security reasons");
+
+            this.certainty = certainty;
+            this.countSmallPrimes = countSmallPrimes;
+        }
 
 		/**
 		 * Parameters for a NaccacheStern KeyPair.
@@ -54,27 +60,17 @@ namespace Org.BouncyCastle.Crypto.Parameters
 		 * @param cntSmallPrimes
 		 *            How many small key factors are desired
 		 * @param debug
-		 *            Turn debugging on or off (reveals secret information, use with
-		 *            caution)
+         *            Ignored
 		 */
-		public NaccacheSternKeyGenerationParameters(SecureRandom random,
-			int		strength,
-			int		certainty,
-			int		countSmallPrimes,
-			bool	debug)
-			: base(random, strength)
+        [Obsolete("Use version without 'debug' parameter")]
+		public NaccacheSternKeyGenerationParameters(
+            SecureRandom    random,
+			int             strength,
+			int             certainty,
+			int             countSmallPrimes,
+			bool            debug)
+			: this(random, strength, certainty, countSmallPrimes)
 		{
-			if (countSmallPrimes % 2 == 1)
-			{
-				throw new ArgumentException("countSmallPrimes must be a multiple of 2");
-			}
-			if (countSmallPrimes < 30)
-			{
-				throw new ArgumentException("countSmallPrimes must be >= 30 for security reasons");
-			}
-			this.certainty = certainty;
-			this.countSmallPrimes = countSmallPrimes;
-			this.debug = debug;
 		}
 
 		/**
@@ -93,9 +89,10 @@ namespace Org.BouncyCastle.Crypto.Parameters
 			get { return countSmallPrimes; }
 		}
 
-		public bool IsDebug
+        [Obsolete("Remove: always false")]
+        public bool IsDebug
 		{
-			get { return debug; }
+			get { return false; }
 		}
 	}
 }