public override void PerformTest() { // Test with given key from NaccacheSternPaper (totally insecure) // First the Parameters from the NaccacheStern Paper // (see http://www.gemplus.com/smart/rd/publications/pdf/NS98pkcs.pdf ) smallPrimes.Add(u1); smallPrimes.Add(u2); smallPrimes.Add(u3); smallPrimes.Add(v1); smallPrimes.Add(v2); smallPrimes.Add(v3); NaccacheSternKeyParameters pubParameters = new NaccacheSternKeyParameters(false, g, n, sigma.BitLength); NaccacheSternPrivateKeyParameters privParameters = new NaccacheSternPrivateKeyParameters(g, n, sigma .BitLength, smallPrimes, phi_n); IAsymmetricCipherKeyPair pair = new AsymmetricCipherKeyPair(pubParameters, privParameters); // Initialize Engines with KeyPair if (debug) { Console.WriteLine("initializing encryption engine"); } cryptEng.Init(true, pair.Public); if (debug) { Console.WriteLine("initializing decryption engine"); } decryptEng.Init(false, pair.Private); byte[] data = paperTest.ToByteArray(); if (!new BigInteger(data).Equals(new BigInteger(enDeCrypt(data)))) { Fail("failed NaccacheStern paper test"); } // // key generation test // // // 768 Bit test // if (debug) { Console.WriteLine(); Console.WriteLine("768 Bit TEST"); } // specify key generation parameters NaccacheSternKeyGenerationParameters genParam = new NaccacheSternKeyGenerationParameters(new SecureRandom(), 768, 8, 30, debug); // Initialize Key generator and generate key pair NaccacheSternKeyPairGenerator pGen = new NaccacheSternKeyPairGenerator(); pGen.Init(genParam); pair = pGen.GenerateKeyPair(); if (((NaccacheSternKeyParameters)pair.Public).Modulus.BitLength < 768) { Console.WriteLine("FAILED: key size is <786 bit, exactly " + ((NaccacheSternKeyParameters)pair.Public).Modulus.BitLength + " bit"); Fail("failed key generation (768) length test"); } // Initialize Engines with KeyPair if (debug) { Console.WriteLine("initializing " + genParam.Strength + " bit encryption engine"); } cryptEng.Init(true, pair.Public); if (debug) { Console.WriteLine("initializing " + genParam.Strength + " bit decryption engine"); } decryptEng.Init(false, pair.Private); // Basic data input data = Hex.Decode(input); if (!new BigInteger(1, data).Equals(new BigInteger(1, enDeCrypt(data)))) { Fail("failed encryption decryption (" + genParam.Strength + ") basic test"); } // Data starting with FF byte (would be interpreted as negative // BigInteger) data = Hex.Decode(edgeInput); if (!new BigInteger(1, data).Equals(new BigInteger(1, enDeCrypt(data)))) { Fail("failed encryption decryption (" + genParam.Strength + ") edgeInput test"); } // // 1024 Bit Test // /* if (debug) { Console.WriteLine(); Console.WriteLine("1024 Bit TEST"); } // specify key generation parameters genParam = new NaccacheSternKeyGenerationParameters(new SecureRandom(), 1024, 8, 40, debug); pGen.Init(genParam); pair = pGen.generateKeyPair(); if (((NaccacheSternKeyParameters)pair.Public).getModulus().bitLength() < 1024) { if (debug) { Console.WriteLine("FAILED: key size is <1024 bit, exactly " + ((NaccacheSternKeyParameters)pair.Public).getModulus().bitLength() + " bit"); } Fail("failed key generation (1024) length test"); } // Initialize Engines with KeyPair if (debug) { Console.WriteLine("initializing " + genParam.getStrength() + " bit encryption engine"); } cryptEng.Init(true, pair.Public); if (debug) { Console.WriteLine("initializing " + genParam.getStrength() + " bit decryption engine"); } decryptEng.Init(false, pair.Private); if (debug) { Console.WriteLine("Data is " + new BigInteger(1, data)); } // Basic data input data = Hex.Decode(input); if (!new BigInteger(1, data).Equals(new BigInteger(1, enDeCrypt(data)))) { Fail("failed encryption decryption (" + genParam.getStrength() + ") basic test"); } // Data starting with FF byte (would be interpreted as negative // BigInteger) data = Hex.Decode(edgeInput); if (!new BigInteger(1, data).Equals(new BigInteger(1, enDeCrypt(data)))) { Fail("failed encryption decryption (" + genParam.getStrength() + ") edgeInput test"); } */ // END OF TEST CASE try { new NaccacheSternEngine().ProcessBlock(new byte[]{ 1 }, 0, 1); Fail("failed initialisation check"); } catch (InvalidOperationException) { // expected } if (debug) { Console.WriteLine("All tests successful"); } }
public AsymmetricCipherKeyPair GenerateKeyPair() { int strength = this.param.Strength; SecureRandom random = this.param.Random; int certainty = this.param.Certainty; bool isDebug = this.param.IsDebug; if (isDebug) { Console.WriteLine("Fetching first " + this.param.CountSmallPrimes + " primes."); } IList list = NaccacheSternKeyPairGenerator.findFirstPrimes(this.param.CountSmallPrimes); list = NaccacheSternKeyPairGenerator.permuteList(list, random); BigInteger bigInteger = BigInteger.One; BigInteger bigInteger2 = BigInteger.One; for (int i = 0; i < list.Count / 2; i++) { bigInteger = bigInteger.Multiply((BigInteger)list[i]); } for (int j = list.Count / 2; j < list.Count; j++) { bigInteger2 = bigInteger2.Multiply((BigInteger)list[j]); } BigInteger bigInteger3 = bigInteger.Multiply(bigInteger2); int num = strength - bigInteger3.BitLength - 48; BigInteger bigInteger4 = NaccacheSternKeyPairGenerator.generatePrime(num / 2 + 1, certainty, random); BigInteger bigInteger5 = NaccacheSternKeyPairGenerator.generatePrime(num / 2 + 1, certainty, random); long num2 = 0L; if (isDebug) { Console.WriteLine("generating p and q"); } BigInteger val = bigInteger4.Multiply(bigInteger).ShiftLeft(1); BigInteger val2 = bigInteger5.Multiply(bigInteger2).ShiftLeft(1); BigInteger bigInteger6; BigInteger bigInteger7; BigInteger bigInteger8; BigInteger bigInteger9; while (true) { num2 += 1L; bigInteger6 = NaccacheSternKeyPairGenerator.generatePrime(24, certainty, random); bigInteger7 = bigInteger6.Multiply(val).Add(BigInteger.One); if (bigInteger7.IsProbablePrime(certainty)) { while (true) { bigInteger8 = NaccacheSternKeyPairGenerator.generatePrime(24, certainty, random); if (!bigInteger6.Equals(bigInteger8)) { bigInteger9 = bigInteger8.Multiply(val2).Add(BigInteger.One); if (bigInteger9.IsProbablePrime(certainty)) { break; } } } if (!bigInteger3.Gcd(bigInteger6.Multiply(bigInteger8)).Equals(BigInteger.One)) { Console.WriteLine(string.Concat(new object[] { "sigma.gcd(_p.mult(_q)) != 1!\n _p: ", bigInteger6, "\n _q: ", bigInteger8 })); } else { if (bigInteger7.Multiply(bigInteger9).BitLength >= strength) { break; } if (isDebug) { Console.WriteLine(string.Concat(new object[] { "key size too small. Should be ", strength, " but is actually ", bigInteger7.Multiply(bigInteger9).BitLength })); } } } } if (isDebug) { Console.WriteLine("needed " + num2 + " tries to generate p and q."); } BigInteger bigInteger10 = bigInteger7.Multiply(bigInteger9); BigInteger bigInteger11 = bigInteger7.Subtract(BigInteger.One).Multiply(bigInteger9.Subtract(BigInteger.One)); num2 = 0L; if (isDebug) { Console.WriteLine("generating g"); } BigInteger bigInteger12; while (true) { IList list2 = Platform.CreateArrayList(); for (int num3 = 0; num3 != list.Count; num3++) { BigInteger val3 = (BigInteger)list[num3]; BigInteger e = bigInteger11.Divide(val3); do { num2 += 1L; bigInteger12 = NaccacheSternKeyPairGenerator.generatePrime(strength, certainty, random); }while (bigInteger12.ModPow(e, bigInteger10).Equals(BigInteger.One)); list2.Add(bigInteger12); } bigInteger12 = BigInteger.One; for (int k = 0; k < list.Count; k++) { BigInteger bigInteger13 = (BigInteger)list2[k]; BigInteger val4 = (BigInteger)list[k]; bigInteger12 = bigInteger12.Multiply(bigInteger13.ModPow(bigInteger3.Divide(val4), bigInteger10)).Mod(bigInteger10); } bool flag = false; for (int l = 0; l < list.Count; l++) { if (bigInteger12.ModPow(bigInteger11.Divide((BigInteger)list[l]), bigInteger10).Equals(BigInteger.One)) { if (isDebug) { Console.WriteLine(string.Concat(new object[] { "g has order phi(n)/", list[l], "\n g: ", bigInteger12 })); } flag = true; break; } } if (!flag) { if (bigInteger12.ModPow(bigInteger11.ShiftRight(2), bigInteger10).Equals(BigInteger.One)) { if (isDebug) { Console.WriteLine("g has order phi(n)/4\n g:" + bigInteger12); } } else if (bigInteger12.ModPow(bigInteger11.Divide(bigInteger6), bigInteger10).Equals(BigInteger.One)) { if (isDebug) { Console.WriteLine("g has order phi(n)/p'\n g: " + bigInteger12); } } else if (bigInteger12.ModPow(bigInteger11.Divide(bigInteger8), bigInteger10).Equals(BigInteger.One)) { if (isDebug) { Console.WriteLine("g has order phi(n)/q'\n g: " + bigInteger12); } } else if (bigInteger12.ModPow(bigInteger11.Divide(bigInteger4), bigInteger10).Equals(BigInteger.One)) { if (isDebug) { Console.WriteLine("g has order phi(n)/a\n g: " + bigInteger12); } } else { if (!bigInteger12.ModPow(bigInteger11.Divide(bigInteger5), bigInteger10).Equals(BigInteger.One)) { break; } if (isDebug) { Console.WriteLine("g has order phi(n)/b\n g: " + bigInteger12); } } } } if (isDebug) { Console.WriteLine("needed " + num2 + " tries to generate g"); Console.WriteLine(); Console.WriteLine("found new NaccacheStern cipher variables:"); Console.WriteLine("smallPrimes: " + CollectionUtilities.ToString(list)); Console.WriteLine(string.Concat(new object[] { "sigma:...... ", bigInteger3, " (", bigInteger3.BitLength, " bits)" })); Console.WriteLine("a:.......... " + bigInteger4); Console.WriteLine("b:.......... " + bigInteger5); Console.WriteLine("p':......... " + bigInteger6); Console.WriteLine("q':......... " + bigInteger8); Console.WriteLine("p:.......... " + bigInteger7); Console.WriteLine("q:.......... " + bigInteger9); Console.WriteLine("n:.......... " + bigInteger10); Console.WriteLine("phi(n):..... " + bigInteger11); Console.WriteLine("g:.......... " + bigInteger12); Console.WriteLine(); } return(new AsymmetricCipherKeyPair(new NaccacheSternKeyParameters(false, bigInteger12, bigInteger10, bigInteger3.BitLength), new NaccacheSternPrivateKeyParameters(bigInteger12, bigInteger10, bigInteger3.BitLength, list, bigInteger11))); }