diff --git a/crypto/test/src/security/test/SecureRandomTest.cs b/crypto/test/src/security/test/SecureRandomTest.cs
new file mode 100644
index 000000000..12e4b9a47
--- /dev/null
+++ b/crypto/test/src/security/test/SecureRandomTest.cs
@@ -0,0 +1,150 @@
+using System;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Crypto.Prng;
+
+namespace Org.BouncyCastle.Security.Tests
+{
+ [TestFixture]
+ public class SecureRandomTest
+ {
+#if !NETCF_1_0
+ [Test]
+ public void TestCryptoApi()
+ {
+ SecureRandom random = new SecureRandom(
+ new CryptoApiRandomGenerator());
+
+ checkSecureRandom(random);
+ }
+#endif
+
+ [Test]
+ public void TestDefault()
+ {
+ SecureRandom random = new SecureRandom();
+
+ checkSecureRandom(random);
+ }
+
+ [Test]
+ public void TestSha1Prng()
+ {
+ SecureRandom random = SecureRandom.GetInstance("SHA1PRNG");
+ random.SetSeed(SecureRandom.GetSeed(20));
+
+ checkSecureRandom(random);
+ }
+
+ [Test]
+ public void TestSha256Prng()
+ {
+ SecureRandom random = SecureRandom.GetInstance("SHA256PRNG");
+ random.SetSeed(SecureRandom.GetSeed(32));
+
+ checkSecureRandom(random);
+ }
+
+ [Test]
+ public void TestThreadedSeed()
+ {
+ SecureRandom random = new SecureRandom(
+ new ThreadedSeedGenerator().GenerateSeed(20, false));
+
+ checkSecureRandom(random);
+ }
+
+ [Test]
+ public void TestVmpcPrng()
+ {
+ SecureRandom random = new SecureRandom(new VmpcRandomGenerator());
+ random.SetSeed(SecureRandom.GetSeed(32));
+
+ checkSecureRandom(random);
+ }
+
+
+ private static void checkSecureRandom(
+ SecureRandom random)
+ {
+ // Note: This will periodically (< 1e-6 probability) give a false alarm.
+ // That's randomness for you!
+ Assert.IsTrue(runChiSquaredTests(random), "Chi2 test detected possible non-randomness");
+ }
+
+ private static bool runChiSquaredTests(
+ SecureRandom random)
+ {
+ int passes = 0;
+
+ for (int tries = 0; tries < 100; ++tries)
+ {
+ double chi2 = measureChiSquared(random, 1000);
+ if (chi2 < 285.0) // 255 degrees of freedom in test => Q ~ 10.0% for 285
+ ++passes;
+ }
+
+ return passes > 75;
+ }
+
+ private static double measureChiSquared(
+ SecureRandom random,
+ int rounds)
+ {
+ int[] counts = new int[256];
+
+ byte[] bs = new byte[256];
+ for (int i = 0; i < rounds; ++i)
+ {
+ random.NextBytes(bs);
+
+ for (int b = 0; b < 256; ++b)
+ {
+ ++counts[bs[b]];
+ }
+ }
+
+ byte mask = SecureRandom.GetSeed(1)[0];
+ for (int i = 0; i < rounds; ++i)
+ {
+ random.NextBytes(bs);
+
+ for (int b = 0; b < 256; ++b)
+ {
+ ++counts[bs[b] ^ mask];
+ }
+
+ ++mask;
+ }
+
+ byte shift = SecureRandom.GetSeed(1)[0];
+ for (int i = 0; i < rounds; ++i)
+ {
+ random.NextBytes(bs);
+
+ for (int b = 0; b < 256; ++b)
+ {
+ ++counts[(byte)(bs[b] + shift)];
+ }
+
+ ++shift;
+ }
+
+ int total = 3 * rounds;
+
+ double chi2 = 0;
+ for (int k = 0; k < counts.Length; ++k)
+ {
+ double diff = ((double) counts[k]) - total;
+ double diff2 = diff * diff;
+
+ chi2 += diff2;
+ }
+
+ chi2 /= total;
+
+ return chi2;
+ }
+ }
+}
diff --git a/crypto/test/src/security/test/TestDigestUtil.cs b/crypto/test/src/security/test/TestDigestUtil.cs
new file mode 100644
index 000000000..0f169f931
--- /dev/null
+++ b/crypto/test/src/security/test/TestDigestUtil.cs
@@ -0,0 +1,63 @@
+using System;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Crypto.Digests;
+
+namespace Org.BouncyCastle.Security.Tests
+{
+ [TestFixture]
+ public class TestDigestUtilities
+ {
+ private static readonly byte[] TestBytes = new byte[100];
+
+ static TestDigestUtilities()
+ {
+ new SecureRandom().NextBytes(TestBytes);
+ }
+
+ [Test]
+ public void TestAlgorithms()
+ {
+ CheckAlgorithm("GOST3411", new Gost3411Digest());
+ CheckAlgorithm("MD2", new MD2Digest());
+ CheckAlgorithm("MD4", new MD4Digest());
+ CheckAlgorithm("MD5", new MD5Digest());
+ CheckAlgorithm("RipeMD128", new RipeMD128Digest());
+ CheckAlgorithm("RipeMD160", new RipeMD160Digest());
+ CheckAlgorithm("RipeMD256", new RipeMD256Digest());
+ CheckAlgorithm("RipeMD320", new RipeMD320Digest());
+ CheckAlgorithm("SHA-1", new Sha1Digest());
+ CheckAlgorithm("SHA-224", new Sha224Digest());
+ CheckAlgorithm("SHA-256", new Sha256Digest());
+ CheckAlgorithm("SHA-384", new Sha384Digest());
+ CheckAlgorithm("SHA-512", new Sha512Digest());
+ CheckAlgorithm("Tiger", new TigerDigest());
+ CheckAlgorithm("Whirlpool", new WhirlpoolDigest());
+ }
+
+ private void CheckAlgorithm(
+ string name,
+ IDigest digest)
+ {
+ byte[] hash1 = MakeTestHash(digest);
+ byte[] hash2 = MakeTestHash(DigestUtilities.GetDigest(name));
+
+ Assert.AreEqual(hash1, hash2, name);
+ }
+
+ private byte[] MakeTestHash(
+ IDigest digest)
+ {
+ for (int i = 0; i < digest.GetDigestSize(); ++i)
+ {
+ digest.Update((byte) i);
+ }
+
+ digest.BlockUpdate(TestBytes, 0, TestBytes.Length);
+
+ return DigestUtilities.DoFinal(digest);
+ }
+ }
+}
diff --git a/crypto/test/src/security/test/TestDotNetUtil.cs b/crypto/test/src/security/test/TestDotNetUtil.cs
new file mode 100644
index 000000000..f880b388f
--- /dev/null
+++ b/crypto/test/src/security/test/TestDotNetUtil.cs
@@ -0,0 +1,88 @@
+#if !(NETCF_1_0 || SILVERLIGHT)
+
+using System;
+using System.Security.Cryptography;
+using SystemX509 = System.Security.Cryptography.X509Certificates;
+using System.Collections;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Asn1.X509;
+using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Utilities.Encoders;
+using Org.BouncyCastle.X509;
+
+namespace Org.BouncyCastle.Security.Tests
+{
+ [TestFixture]
+ public class TestDotNetUtilities
+ {
+ [Test]
+ public void TestRsaInterop()
+ {
+ for (int i = 0; i < 100; ++i)
+ {
+ RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512);
+ RSAParameters rp = rsa.ExportParameters(true);
+ AsymmetricCipherKeyPair kp = DotNetUtilities.GetRsaKeyPair(rp);
+
+ DotNetUtilities.ToRSA((RsaKeyParameters)kp.Public);
+ // TODO This method appears to not work for private keys (when no CRT info)
+ //DotNetUtilities.ToRSA((RsaKeyParameters)kp.Private);
+ DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)kp.Private);
+ }
+ }
+
+ [Test]
+ public void TestX509CertificateConversion()
+ {
+ BigInteger DSAParaG = new BigInteger(Base64.Decode("AL0fxOTq10OHFbCf8YldyGembqEu08EDVzxyLL29Zn/t4It661YNol1rnhPIs+cirw+yf9zeCe+KL1IbZ/qIMZM="));
+ BigInteger DSAParaP = new BigInteger(Base64.Decode("AM2b/UeQA+ovv3dL05wlDHEKJ+qhnJBsRT5OB9WuyRC830G79y0R8wuq8jyIYWCYcTn1TeqVPWqiTv6oAoiEeOs="));
+ BigInteger DSAParaQ = new BigInteger(Base64.Decode("AIlJT7mcKL6SUBMmvm24zX1EvjNx"));
+ BigInteger DSAPublicY = new BigInteger(Base64.Decode("TtWy2GuT9yGBWOHi1/EpCDa/bWJCk2+yAdr56rAcqP0eHGkMnA9s9GJD2nGU8sFjNHm55swpn6JQb8q0agrCfw=="));
+ BigInteger DsaPrivateX = new BigInteger(Base64.Decode("MMpBAxNlv7eYfxLTZ2BItJeD31A="));
+
+ DsaParameters para = new DsaParameters(DSAParaP, DSAParaQ, DSAParaG);
+ DsaPrivateKeyParameters dsaPriv = new DsaPrivateKeyParameters(DsaPrivateX, para);
+ DsaPublicKeyParameters dsaPub = new DsaPublicKeyParameters(DSAPublicY, para);
+
+ IDictionary attrs = new Hashtable();
+ attrs[X509Name.C] = "AU";
+ attrs[X509Name.O] = "The Legion of the Bouncy Castle";
+ attrs[X509Name.L] = "Melbourne";
+ attrs[X509Name.ST] = "Victoria";
+ attrs[X509Name.E] = "feedback-crypto@bouncycastle.org";
+
+ IList ord = new ArrayList(attrs.Keys);
+
+ X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
+
+ certGen.SetSerialNumber(BigInteger.One);
+
+ certGen.SetIssuerDN(new X509Name(ord, attrs));
+ certGen.SetNotBefore(DateTime.UtcNow.AddDays(-1));
+ certGen.SetNotAfter(DateTime.UtcNow.AddDays(1));
+ certGen.SetSubjectDN(new X509Name(ord, attrs));
+ certGen.SetPublicKey(dsaPub);
+ certGen.SetSignatureAlgorithm("SHA1WITHDSA");
+
+ X509Certificate cert = certGen.Generate(dsaPriv);
+
+ cert.CheckValidity();
+ cert.Verify(dsaPub);
+
+ SystemX509.X509Certificate dotNetCert = DotNetUtilities.ToX509Certificate(cert);
+
+ X509Certificate certCopy = DotNetUtilities.FromX509Certificate(dotNetCert);
+
+ Assert.AreEqual(cert, certCopy);
+
+ certCopy.CheckValidity();
+ certCopy.Verify(dsaPub);
+ }
+ }
+}
+
+#endif
diff --git a/crypto/test/src/security/test/TestEncodings.cs b/crypto/test/src/security/test/TestEncodings.cs
new file mode 100644
index 000000000..557d2dc51
--- /dev/null
+++ b/crypto/test/src/security/test/TestEncodings.cs
@@ -0,0 +1,188 @@
+using System;
+using System.Collections;
+using System.Text;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Asn1.Pkcs;
+using Org.BouncyCastle.Asn1.X509;
+using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Crypto.Generators;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Crypto.Signers;
+using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Math.EC;
+using Org.BouncyCastle.Pkcs;
+using Org.BouncyCastle.Utilities.Encoders;
+using Org.BouncyCastle.X509;
+
+namespace Org.BouncyCastle.Security.Tests
+{
+ [TestFixture]
+ public class TestEncodings
+ {
+ [Test]
+ public void TestEC()
+ {
+ BigInteger ECParraGX = new BigInteger(Base64.Decode("D/qWPNyogWzMM7hkK+35BcPTWFc9Pyf7vTs8uaqv"));
+ BigInteger ECParraGY = new BigInteger(Base64.Decode("AhQXGxb1olGRv6s1LPRfuatMF+cx3ZTGgzSE/Q5R"));
+ BigInteger ECParraH = new BigInteger(Base64.Decode("AQ=="));
+ BigInteger ECParraN = new BigInteger(Base64.Decode("f///////////////f///nl6an12QcfvRUiaIkJ0L"));
+ BigInteger ECPubQX = new BigInteger(Base64.Decode("HWWi17Yb+Bm3PYr/DMjLOYNFhyOwX1QY7ZvqqM+l"));
+ BigInteger ECPubQY = new BigInteger(Base64.Decode("JrlJfxu3WGhqwtL/55BOs/wsUeiDFsvXcGhB8DGx"));
+ BigInteger ECPrivD = new BigInteger(Base64.Decode("GYQmd/NF1B+He1iMkWt3by2Az6Eu07t0ynJ4YCAo"));
+
+ FpCurve curve = new FpCurve(
+ new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q
+ new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a
+ new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b
+
+ ECDomainParameters ecDomain =
+ new ECDomainParameters(
+ curve,
+ new FpPoint(curve,
+ curve.FromBigInteger(ECParraGX),
+ curve.FromBigInteger(ECParraGY)),
+ ECParraN);
+
+ ECPublicKeyParameters ecPub = new ECPublicKeyParameters(
+ new FpPoint(
+ curve,
+ curve.FromBigInteger(ECPubQX),
+ curve.FromBigInteger(ECPubQY)),
+ ecDomain);
+
+ ECPrivateKeyParameters ecPriv = new ECPrivateKeyParameters(ECPrivD, ecDomain);
+
+ SubjectPublicKeyInfo subinfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(ecPub);
+ PrivateKeyInfo privInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(ecPriv);
+
+ ECPublicKeyParameters tecPub = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(subinfo);
+ ECPrivateKeyParameters tecPriv = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(privInfo);
+
+ Assert.IsTrue(tecPub.Equals(ecPub), "EC: public key to info back to public key");
+ Assert.IsTrue(tecPriv.Equals(ecPriv), "EC: private key to info back to private key");
+ }
+
+ [Test]
+ public void TestDH()
+ {
+ BigInteger DHParraP = new BigInteger(Base64.Decode("ALJCm1CUL6mOnyVqWTSV6Z2+DVSGOvgboOhmbyyxCrym59uVnXMmPjIgQTrmniFg7PvdcN7NNFwFmcZleULso1s="));
+ BigInteger DHParraQ = new BigInteger(Base64.Decode("WSFNqEoX1MdPkrUsmkr0zt8GqkMdfA3QdDM3lliFXlNz7crOuZMfGRAgnXNPELB2fe64b2aaLgLM4zK8oXZRrQ=="));
+ BigInteger DHPubY = new BigInteger(Base64.Decode("AIki+8/zggCS2e488AsTNULI4LujdUeQQsZI949Dc9lKXZRmrPIC1h8NRoneHQEhpAe4Rhe0nhUOGZJekT5++SA="));
+ BigInteger DHPrivX = new BigInteger(Base64.Decode("Apo67noMRO5eDWo/TtpRiBmKGw7ywh25shIu0Rs03krQmWKRbDPvdygWdJ5IpW6ZbKlCTAMhSxpz03YSeSEDmw=="));
+
+
+ DHParameters dhPara = new DHParameters(DHParraP, DHParraQ);
+ DHPublicKeyParameters dhPublic = new DHPublicKeyParameters(DHPubY, dhPara);
+ DHPrivateKeyParameters dhPrivate = new DHPrivateKeyParameters(DHPrivX, dhPara);
+
+ SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(dhPublic);
+ PrivateKeyInfo privInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(dhPrivate);
+
+ DHPublicKeyParameters testPubKey = (DHPublicKeyParameters) PublicKeyFactory.CreateKey(pubInfo);
+ DHPrivateKeyParameters testPrivKey = (DHPrivateKeyParameters) PrivateKeyFactory.CreateKey(privInfo);
+
+ Assert.IsFalse(!testPrivKey.Equals(dhPrivate), "DH: Private key to info back to key");
+ Assert.IsFalse(!testPubKey.Equals(dhPublic), "DH: Public key to info back to key");
+
+ Assert.IsTrue(true, "Diffe Helman Test worked.");
+
+ }
+
+ [Test]
+ public void TestElGamal()
+ {
+
+ BigInteger ELGamalParaG = new BigInteger(Base64.Decode("QAZPRcsH8kHVKS5065R1Xy6QtsPvDkmDZtPuq18EJkvLrCIZivE/m5puQp3/VKJrG7dKgz4NBGpONp3HT+Cn/g=="));
+ BigInteger ELGamalParaP = new BigInteger(Base64.Decode("AKXmAwgkudDLI/Yxk6wk3APn+mSjX5QSyDwpchmegSIi1ZNC0Jb+IbxjroKNhRTBKjtv4/JTXtJS6IqaZv9uKes="));
+ BigInteger ELGamalPubY = new BigInteger(Base64.Decode("AJ/gXuZuCA2X044otNkzs8FI36XuFu1L/YHg5cEmDvICTigycRN2E1DnhP+CTqxEqgEqX8rBe5tuGDlkTLwgNqM="));
+ BigInteger ELGamalPriv = new BigInteger(Base64.Decode("CqVr+K0TpuJKQnc76MjKhxrJzGr93jnuE3mTpth486Meymt8uWEVAQj1tGc9DTt14F9aV9WIT2oYYbvLJRcwow=="));
+
+
+
+ ElGamalParameters elPara = new ElGamalParameters(ELGamalParaP, ELGamalParaG);
+
+ ElGamalPrivateKeyParameters elPriv = new ElGamalPrivateKeyParameters(ELGamalPriv, elPara);
+ ElGamalPublicKeyParameters elPub = new ElGamalPublicKeyParameters(ELGamalPubY, elPara);
+
+ SubjectPublicKeyInfo subInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(elPub);
+ PrivateKeyInfo privInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(elPriv);
+
+ ElGamalPrivateKeyParameters telPriv = (ElGamalPrivateKeyParameters)PrivateKeyFactory.CreateKey(privInfo);
+ ElGamalPublicKeyParameters telPub = (ElGamalPublicKeyParameters)PublicKeyFactory.CreateKey(subInfo);
+
+
+ // Console.WriteLine(telPriv.Equals(elPriv));
+
+
+
+ Assert.IsTrue(telPriv.Equals(elPriv), "ELGamal Private key to into back to private key.");
+ Assert.IsTrue(telPub.Equals(elPub), "ELGamal Public key to into back to private key.");
+
+ Assert.IsTrue(true, "ELGamal Test worked.");
+
+ }
+
+ [Test]
+ public void TestRsa()
+ {
+
+ BigInteger rsaPubMod = new BigInteger(Base64.Decode("AIASoe2PQb1IP7bTyC9usjHP7FvnUMVpKW49iuFtrw/dMpYlsMMoIU2jupfifDpdFxIktSB4P+6Ymg5WjvHKTIrvQ7SR4zV4jaPTu56Ys0pZ9EDA6gb3HLjtU+8Bb1mfWM+yjKxcPDuFjwEtjGlPHg1Vq+CA9HNcMSKNn2+tW6qt"));
+ BigInteger rsaPubExp = new BigInteger(Base64.Decode("EQ=="));
+ BigInteger rsaPrivMod = new BigInteger(Base64.Decode("AIASoe2PQb1IP7bTyC9usjHP7FvnUMVpKW49iuFtrw/dMpYlsMMoIU2jupfifDpdFxIktSB4P+6Ymg5WjvHKTIrvQ7SR4zV4jaPTu56Ys0pZ9EDA6gb3HLjtU+8Bb1mfWM+yjKxcPDuFjwEtjGlPHg1Vq+CA9HNcMSKNn2+tW6qt"));
+ BigInteger rsaPrivDP = new BigInteger(Base64.Decode("JXzfzG5v+HtLJIZqYMUefJfFLu8DPuJGaLD6lI3cZ0babWZ/oPGoJa5iHpX4Ul/7l3s1PFsuy1GhzCdOdlfRcQ=="));
+ BigInteger rsaPrivDQ = new BigInteger(Base64.Decode("YNdJhw3cn0gBoVmMIFRZzflPDNthBiWy/dUMSRfJCxoZjSnr1gysZHK01HteV1YYNGcwPdr3j4FbOfri5c6DUQ=="));
+ BigInteger rsaPrivExp = new BigInteger(Base64.Decode("DxFAOhDajr00rBjqX+7nyZ/9sHWRCCp9WEN5wCsFiWVRPtdB+NeLcou7mWXwf1Y+8xNgmmh//fPV45G2dsyBeZbXeJwB7bzx9NMEAfedchyOwjR8PYdjK3NpTLKtZlEJ6Jkh4QihrXpZMO4fKZWUm9bid3+lmiq43FwW+Hof8/E="));
+ BigInteger rsaPrivP = new BigInteger(Base64.Decode("AJ9StyTVW+AL/1s7RBtFwZGFBgd3zctBqzzwKPda6LbtIFDznmwDCqAlIQH9X14X7UPLokCDhuAa76OnDXb1OiE="));
+ BigInteger rsaPrivQ = new BigInteger(Base64.Decode("AM3JfD79dNJ5A3beScSzPtWxx/tSLi0QHFtkuhtSizeXdkv5FSba7lVzwEOGKHmW829bRoNxThDy4ds1IihW1w0="));
+ BigInteger rsaPrivQinv = new BigInteger(Base64.Decode("Lt0g7wrsNsQxuDdB8q/rH8fSFeBXMGLtCIqfOec1j7FEIuYA/ACiRDgXkHa0WgN7nLXSjHoy630wC5Toq8vvUg=="));
+ RsaKeyParameters rsaPublic = new RsaKeyParameters(false, rsaPubMod, rsaPubExp);
+ RsaPrivateCrtKeyParameters rsaPrivate = new RsaPrivateCrtKeyParameters(rsaPrivMod, rsaPubExp, rsaPrivExp, rsaPrivP, rsaPrivQ, rsaPrivDP, rsaPrivDQ, rsaPrivQinv);
+
+ SubjectPublicKeyInfo subInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(rsaPublic);
+ RsaKeyParameters testResult = (RsaKeyParameters)PublicKeyFactory.CreateKey(subInfo);
+
+ // check RSA public key.
+
+ Assert.IsFalse(!testResult.Equals(rsaPublic), "RSA: test failed on public key to info and back to public key.");
+
+ PrivateKeyInfo privInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(rsaPrivate);
+ testResult = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(privInfo);
+
+ Assert.IsFalse(!testResult.Equals(rsaPrivate), "RSA: private key to info back to private key.");
+
+ Assert.IsTrue(true, "RSATest worked.");
+
+ }
+
+ [Test]
+ public void TestDSA()
+ {
+ BigInteger DSAParaG = new BigInteger(Base64.Decode("AL0fxOTq10OHFbCf8YldyGembqEu08EDVzxyLL29Zn/t4It661YNol1rnhPIs+cirw+yf9zeCe+KL1IbZ/qIMZM="));
+ BigInteger DSAParaP = new BigInteger(Base64.Decode("AM2b/UeQA+ovv3dL05wlDHEKJ+qhnJBsRT5OB9WuyRC830G79y0R8wuq8jyIYWCYcTn1TeqVPWqiTv6oAoiEeOs="));
+ BigInteger DSAParaQ = new BigInteger(Base64.Decode("AIlJT7mcKL6SUBMmvm24zX1EvjNx"));
+ BigInteger DSAPublicY = new BigInteger(Base64.Decode("TtWy2GuT9yGBWOHi1/EpCDa/bWJCk2+yAdr56rAcqP0eHGkMnA9s9GJD2nGU8sFjNHm55swpn6JQb8q0agrCfw=="));
+ BigInteger DsaPrivateX = new BigInteger(Base64.Decode("MMpBAxNlv7eYfxLTZ2BItJeD31A="));
+
+ DsaParameters para = new DsaParameters(DSAParaP, DSAParaQ, DSAParaG);
+ DsaPrivateKeyParameters dsaPriv = new DsaPrivateKeyParameters(DsaPrivateX, para);
+ DsaPublicKeyParameters dsaPub = new DsaPublicKeyParameters(DSAPublicY, para);
+
+ SubjectPublicKeyInfo subInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(dsaPub);
+ DsaKeyParameters testResult = (DsaKeyParameters)PublicKeyFactory.CreateKey(subInfo);
+
+ // check RSA public key.
+
+ Assert.IsFalse(!testResult.Equals(dsaPub), "DSA: test failed on public key to info and back to public key.");
+
+ PrivateKeyInfo privInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(dsaPriv);
+ testResult = (DsaPrivateKeyParameters)PrivateKeyFactory.CreateKey(privInfo);
+
+ Assert.IsFalse(!testResult.Equals(dsaPriv), "DSA: private key to info back to private key.");
+
+ Assert.IsTrue(true, "DSATest worked.");
+
+ }
+ }
+}
diff --git a/crypto/test/src/security/test/TestParameterUtil.cs b/crypto/test/src/security/test/TestParameterUtil.cs
new file mode 100644
index 000000000..fe494212a
--- /dev/null
+++ b/crypto/test/src/security/test/TestParameterUtil.cs
@@ -0,0 +1,74 @@
+using System;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Asn1;
+using Org.BouncyCastle.Asn1.Nist;
+using Org.BouncyCastle.Asn1.Oiw;
+using Org.BouncyCastle.Asn1.Pkcs;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Utilities;
+
+namespace Org.BouncyCastle.Security.Tests
+{
+ [TestFixture]
+ public class TestParameterUtilities
+ {
+ [Test]
+ public void TestCreateKeyParameter()
+ {
+ SecureRandom random = new SecureRandom();
+
+ doTestCreateKeyParameter("AES", NistObjectIdentifiers.IdAes128Cbc,
+ 128, typeof(KeyParameter), random);
+ doTestCreateKeyParameter("DES", OiwObjectIdentifiers.DesCbc,
+ 64, typeof(DesParameters), random);
+ doTestCreateKeyParameter("DESEDE", PkcsObjectIdentifiers.DesEde3Cbc,
+ 192, typeof(DesEdeParameters), random);
+ doTestCreateKeyParameter("RC2", PkcsObjectIdentifiers.RC2Cbc,
+ 128, typeof(RC2Parameters), random);
+ }
+
+ private void doTestCreateKeyParameter(
+ string algorithm,
+ DerObjectIdentifier oid,
+ int keyBits,
+ Type expectedType,
+ SecureRandom random)
+ {
+ int keyLength = keyBits / 8;
+ byte[] bytes = new byte[keyLength];
+ random.NextBytes(bytes);
+
+ KeyParameter key;
+
+ key = ParameterUtilities.CreateKeyParameter(algorithm, bytes);
+ checkKeyParameter(key, expectedType, bytes);
+
+ key = ParameterUtilities.CreateKeyParameter(oid, bytes);
+ checkKeyParameter(key, expectedType, bytes);
+
+ bytes = new byte[keyLength * 2];
+ random.NextBytes(bytes);
+
+ int offset = random.Next(1, keyLength);
+ byte[] expected = new byte[keyLength];
+ Array.Copy(bytes, offset, expected, 0, keyLength);
+
+ key = ParameterUtilities.CreateKeyParameter(algorithm, bytes, offset, keyLength);
+ checkKeyParameter(key, expectedType, expected);
+
+ key = ParameterUtilities.CreateKeyParameter(oid, bytes, offset, keyLength);
+ checkKeyParameter(key, expectedType, expected);
+ }
+
+ private void checkKeyParameter(
+ KeyParameter key,
+ Type expectedType,
+ byte[] expectedBytes)
+ {
+ Assert.IsTrue(expectedType.IsInstanceOfType(key));
+ Assert.IsTrue(Arrays.AreEqual(expectedBytes, key.GetKey()));
+ }
+ }
+}
diff --git a/crypto/test/src/security/test/TestSignerUtil.cs b/crypto/test/src/security/test/TestSignerUtil.cs
new file mode 100644
index 000000000..c1140faf7
--- /dev/null
+++ b/crypto/test/src/security/test/TestSignerUtil.cs
@@ -0,0 +1,180 @@
+using System;
+using System.Collections;
+using System.Text;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Asn1.CryptoPro;
+using Org.BouncyCastle.Asn1.Pkcs;
+using Org.BouncyCastle.Asn1.X509;
+using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Math.EC;
+using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Crypto.Generators;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Crypto.Signers;
+using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Encoders;
+
+namespace Org.BouncyCastle.Security.Tests
+{
+ [TestFixture]
+ public class TestSignerUtilities
+ {
+ [Test]
+ public void TestAlgorithms()
+ {
+ //
+ // RSA parameters
+ //
+ BigInteger rsaMod = new BigInteger("a7295693155b1813bb84877fb45343556e0568043de5910872a3a518cc11e23e2db74eaf4545068c4e3d258a2718fbacdcc3eafa457695b957e88fbf110aed049a992d9c430232d02f3529c67a3419935ea9b569f85b1bcd37de6b899cd62697e843130ff0529d09c97d813cb15f293751ff56f943fbdabb63971cc7f4f6d5bff1594416b1f5907bde5a84a44f9802ef29b43bda1960f948f8afb8766c1ab80d32eec88ed66d0b65aebe44a6d0b3c5e0ab051aaa1b912fbcc17b8e751ddecc5365b6db6dab0020c3057db4013a51213a5798a3aab67985b0f4d88627a54a0f3f0285fbcb4afdfeb65cb153af66825656d43238b75503231500753f4e421e3c57", 16);
+ BigInteger rsaPubExp = new BigInteger("10001", 16);
+
+ BigInteger rsaPrivExp = new BigInteger("65dad56ac7df7abb434e4cb5eeadb16093aa6da7f0033aad3815289b04757d32bfee6ade7749c8e4a323b5050a2fb9e2a99e23469e1ed4ba5bab54336af20a5bfccb8b3424cc6923db2ffca5787ed87aa87aa614cd04cedaebc8f623a2d2063017910f436dff18bb06f01758610787f8b258f0a8efd8bd7de30007c47b2a1031696c7d6523bc191d4d918927a7e0b09584ed205bd2ff4fc4382678df82353f7532b3bbb81d69e3f39070aed3fb64fce032a089e8e64955afa5213a6eb241231bd98d702fba725a9b205952fda186412d9e0d9344d2998c455ad8c2bae85ee672751466d5288304032b5b7e02f7e558c7af82c7fbf58eea0bb4ef0f001e6cd0a9", 16);
+ BigInteger rsaPrivP = new BigInteger("d4fd9ac3474fb83aaf832470643609659e511b322632b239b688f3cd2aad87527d6cf652fb9c9ca67940e84789444f2e99b0cb0cfabbd4de95396106c865f38e2fb7b82b231260a94df0e01756bf73ce0386868d9c41645560a81af2f53c18e4f7cdf3d51d80267372e6e0216afbf67f655c9450769cca494e4f6631b239ce1b", 16);
+ BigInteger rsaPrivQ = new BigInteger("c8eaa0e2a1b3a4412a702bccda93f4d150da60d736c99c7c566fdea4dd1b401cbc0d8c063daaf0b579953d36343aa18b33dbf8b9eae94452490cc905245f8f7b9e29b1a288bc66731a29e1dd1a45c9fd7f8238ff727adc49fff73991d0dc096206b9d3a08f61e7462e2b804d78cb8c5eccdb9b7fbd2ad6a8fea46c1053e1be75", 16);
+ BigInteger rsaPrivDP = new BigInteger("10edcb544421c0f9e123624d1099feeb35c72a8b34e008ac6fa6b90210a7543f293af4e5299c8c12eb464e70092805c7256e18e5823455ba0f504d36f5ccacac1b7cd5c58ff710f9c3f92646949d88fdd1e7ea5fed1081820bb9b0d2a8cd4b093fecfdb96dabd6e28c3a6f8c186dc86cddc89afd3e403e0fcf8a9e0bcb27af0b", 16);
+ BigInteger rsaPrivDQ = new BigInteger("97fc25484b5a415eaa63c03e6efa8dafe9a1c8b004d9ee6e80548fefd6f2ce44ee5cb117e77e70285798f57d137566ce8ea4503b13e0f1b5ed5ca6942537c4aa96b2a395782a4cb5b58d0936e0b0fa63b1192954d39ced176d71ef32c6f42c84e2e19f9d4dd999c2151b032b97bd22aa73fd8c5bcd15a2dca4046d5acc997021", 16);
+ BigInteger rsaPrivQinv = new BigInteger("4bb8064e1eff7e9efc3c4578fcedb59ca4aef0993a8312dfdcb1b3decf458aa6650d3d0866f143cbf0d3825e9381181170a0a1651eefcd7def786b8eb356555d9fa07c85b5f5cbdd74382f1129b5e36b4166b6cc9157923699708648212c484958351fdc9cf14f218dbe7fbf7cbd93a209a4681fe23ceb44bab67d66f45d1c9d", 16);
+
+ RsaKeyParameters rsaPublic = new RsaKeyParameters(false, rsaMod, rsaPubExp);
+ RsaPrivateCrtKeyParameters rsaPrivate = new RsaPrivateCrtKeyParameters(
+ rsaMod, rsaPubExp, rsaPrivExp, rsaPrivP, rsaPrivQ, rsaPrivDP, rsaPrivDQ, rsaPrivQinv);
+
+ //
+ // ECDSA parameters
+ //
+ BigInteger ECParraGX = new BigInteger(Base64.Decode("D/qWPNyogWzMM7hkK+35BcPTWFc9Pyf7vTs8uaqv"));
+ BigInteger ECParraGY = new BigInteger(Base64.Decode("AhQXGxb1olGRv6s1LPRfuatMF+cx3ZTGgzSE/Q5R"));
+ BigInteger ECParraH = new BigInteger(Base64.Decode("AQ=="));
+ BigInteger ECParraN = new BigInteger(Base64.Decode("f///////////////f///nl6an12QcfvRUiaIkJ0L"));
+ BigInteger ECPubQX = new BigInteger(Base64.Decode("HWWi17Yb+Bm3PYr/DMjLOYNFhyOwX1QY7ZvqqM+l"));
+ BigInteger ECPubQY = new BigInteger(Base64.Decode("JrlJfxu3WGhqwtL/55BOs/wsUeiDFsvXcGhB8DGx"));
+ BigInteger ECPrivD = new BigInteger(Base64.Decode("GYQmd/NF1B+He1iMkWt3by2Az6Eu07t0ynJ4YCAo"));
+
+ FpCurve curve = new FpCurve(
+ new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q
+ new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a
+ new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b
+
+ ECDomainParameters ecDomain = new ECDomainParameters(curve,
+ new FpPoint(curve,
+ curve.FromBigInteger(ECParraGX),
+ curve.FromBigInteger(ECParraGY)),
+ ECParraN);
+
+ ECPublicKeyParameters ecPub = new ECPublicKeyParameters(
+ new FpPoint(curve,
+ curve.FromBigInteger(ECPubQX),
+ curve.FromBigInteger(ECPubQY)),
+ ecDomain);
+
+ ECPrivateKeyParameters ecPriv = new ECPrivateKeyParameters(ECPrivD, ecDomain);
+
+ //
+ // DSA parameters
+ //
+ BigInteger DSAParaG = new BigInteger(Base64.Decode("AL0fxOTq10OHFbCf8YldyGembqEu08EDVzxyLL29Zn/t4It661YNol1rnhPIs+cirw+yf9zeCe+KL1IbZ/qIMZM="));
+ BigInteger DSAParaP = new BigInteger(Base64.Decode("AM2b/UeQA+ovv3dL05wlDHEKJ+qhnJBsRT5OB9WuyRC830G79y0R8wuq8jyIYWCYcTn1TeqVPWqiTv6oAoiEeOs="));
+ BigInteger DSAParaQ = new BigInteger(Base64.Decode("AIlJT7mcKL6SUBMmvm24zX1EvjNx"));
+ BigInteger DSAPublicY = new BigInteger(Base64.Decode("TtWy2GuT9yGBWOHi1/EpCDa/bWJCk2+yAdr56rAcqP0eHGkMnA9s9GJD2nGU8sFjNHm55swpn6JQb8q0agrCfw=="));
+ BigInteger DsaPrivateX = new BigInteger(Base64.Decode("MMpBAxNlv7eYfxLTZ2BItJeD31A="));
+
+ DsaParameters para = new DsaParameters(DSAParaP, DSAParaQ, DSAParaG);
+ DsaPrivateKeyParameters dsaPriv = new DsaPrivateKeyParameters(DsaPrivateX, para);
+ DsaPublicKeyParameters dsaPub = new DsaPublicKeyParameters(DSAPublicY, para);
+
+ //
+ // ECGOST3410 parameters
+ //
+ IAsymmetricCipherKeyPairGenerator ecGostKpg = GeneratorUtilities.GetKeyPairGenerator("ECGOST3410");
+ ecGostKpg.Init(
+ new ECKeyGenerationParameters(
+ CryptoProObjectIdentifiers.GostR3410x2001CryptoProA,
+ new SecureRandom()));
+
+ AsymmetricCipherKeyPair ecGostPair = ecGostKpg.GenerateKeyPair();
+
+ //
+ // GOST3410 parameters
+ //
+ IAsymmetricCipherKeyPairGenerator gostKpg = GeneratorUtilities.GetKeyPairGenerator("GOST3410");
+ gostKpg.Init(
+ new Gost3410KeyGenerationParameters(
+ new SecureRandom(),
+ CryptoProObjectIdentifiers.GostR3410x94CryptoProA));
+
+ AsymmetricCipherKeyPair gostPair = gostKpg.GenerateKeyPair();
+
+
+
+ //
+ // signer loop
+ //
+ byte[] shortMsg = new byte[] { 1, 4, 5, 6, 8, 8, 4, 2, 1, 3 };
+ byte[] longMsg = new byte[100];
+ new SecureRandom().NextBytes(longMsg);
+
+ foreach (string algorithm in SignerUtilities.Algorithms)
+ {
+ ISigner signer = SignerUtilities.GetSigner(algorithm);
+
+ string upper = Platform.ToUpperInvariant(algorithm);
+ int withPos = upper.LastIndexOf("WITH");
+
+ string cipherName = withPos < 0
+ ? upper
+ : upper.Substring(withPos + "WITH".Length);
+
+ ICipherParameters signParams = null, verifyParams = null;
+
+ if (cipherName == "RSA" || cipherName == "RSAANDMGF1")
+ {
+ signParams = rsaPrivate;
+ verifyParams = rsaPublic;
+ }
+ else if (cipherName == "ECDSA")
+ {
+ signParams = ecPriv;
+ verifyParams = ecPub;
+ }
+ else if (cipherName == "DSA")
+ {
+ signParams = dsaPriv;
+ verifyParams = dsaPub;
+ }
+ else if (cipherName == "ECGOST3410")
+ {
+ signParams = ecGostPair.Private;
+ verifyParams = ecGostPair.Public;
+ }
+ else if (cipherName == "GOST3410")
+ {
+ signParams = gostPair.Private;
+ verifyParams = gostPair.Public;
+ }
+ else
+ {
+ Assert.Fail("Unknown algorithm encountered: " + cipherName);
+ }
+
+ signer.Init(true, signParams);
+ foreach (byte b in shortMsg)
+ {
+ signer.Update(b);
+ }
+ signer.BlockUpdate(longMsg, 0, longMsg.Length);
+ byte[] sig = signer.GenerateSignature();
+
+ signer.Init(false, verifyParams);
+ foreach (byte b in shortMsg)
+ {
+ signer.Update(b);
+ }
+ signer.BlockUpdate(longMsg, 0, longMsg.Length);
+
+ Assert.IsTrue(signer.VerifySignature(sig), cipherName + " signer " + algorithm + " failed.");
+ }
+ }
+ }
+}
|