diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2019-01-31 19:26:09 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2019-01-31 19:26:09 +0700 |
commit | 65b588a45feb07f27716288ff15f784b5bf73812 (patch) | |
tree | 41d4d9e5d939e32008460cee18d6fc3dd6857ce0 /crypto/test | |
parent | removed unnecessary extra ECGOST3410 class (diff) | |
download | BouncyCastle.NET-ed25519-65b588a45feb07f27716288ff15f784b5bf73812.tar.xz |
Fixes and tidying up for release
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/UnitTests.csproj | 8 | ||||
-rw-r--r-- | crypto/test/src/cmp/test/ProtectedMessageTest.cs | 307 | ||||
-rw-r--r-- | crypto/test/src/crmf/test/CrmfTest.cs | 99 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/ECGOST3410_2012Test.cs | 499 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/EGOST3410_2012SignatureTest.cs | 187 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/RegressionTest.cs | 1 | ||||
-rw-r--r-- | crypto/test/src/util/test/FixedSecureRandom.cs | 6 |
7 files changed, 435 insertions, 672 deletions
diff --git a/crypto/test/UnitTests.csproj b/crypto/test/UnitTests.csproj index d36877026..11b20066e 100644 --- a/crypto/test/UnitTests.csproj +++ b/crypto/test/UnitTests.csproj @@ -124,6 +124,7 @@ <Compile Include="src\asn1\test\X509ExtensionsTest.cs" /> <Compile Include="src\asn1\test\X509NameTest.cs" /> <Compile Include="src\asn1\test\X9Test.cs" /> + <Compile Include="src\cmp\test\ProtectedMessageTest.cs" /> <Compile Include="src\cms\test\AllTests.cs" /> <Compile Include="src\cms\test\AuthenticatedDataStreamTest.cs" /> <Compile Include="src\cms\test\AuthenticatedDataTest.cs" /> @@ -192,7 +193,6 @@ <Compile Include="src\crypto\test\ECTest.cs" /> <Compile Include="src\crypto\test\Ed25519Test.cs" /> <Compile Include="src\crypto\test\Ed448Test.cs" /> - <Compile Include="src\crypto\test\EGOST3410_2012SignatureTest.cs" /> <Compile Include="src\crypto\test\ElGamalTest.cs" /> <Compile Include="src\crypto\test\EqualsHashCodeTest.cs" /> <Compile Include="src\crypto\test\GCMTest.cs" /> @@ -389,7 +389,6 @@ <Compile Include="src\pkcs\test\EncryptedPrivateKeyInfoTest.cs" /> <Compile Include="src\pkcs\test\PKCS10Test.cs" /> <Compile Include="src\pkcs\test\PKCS12StoreTest.cs" /> - <Compile Include="src\ProtectedMessageTest.cs" /> <Compile Include="src\security\test\SecureRandomTest.cs" /> <Compile Include="src\security\test\TestDigestUtil.cs" /> <Compile Include="src\security\test\TestDotNetUtil.cs" /> @@ -1321,7 +1320,4 @@ <ItemGroup> <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> </ItemGroup> - <ItemGroup> - <Folder Include="src\NewFolder1\" /> - </ItemGroup> -</Project> \ No newline at end of file +</Project> diff --git a/crypto/test/src/cmp/test/ProtectedMessageTest.cs b/crypto/test/src/cmp/test/ProtectedMessageTest.cs index 24f2fe572..70bf670da 100644 --- a/crypto/test/src/cmp/test/ProtectedMessageTest.cs +++ b/crypto/test/src/cmp/test/ProtectedMessageTest.cs @@ -2,6 +2,7 @@ using System.Collections; using NUnit.Framework; + using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cmp; using Org.BouncyCastle.Asn1.Crmf; @@ -25,7 +26,7 @@ namespace Org.BouncyCastle.Cmp.Tests { public override string Name { - get { return "ProtectedMessageTest"; } + get { return "ProtectedMessage"; } } public override void PerformTest() @@ -35,35 +36,35 @@ namespace Org.BouncyCastle.Cmp.Tests TestMacProtectedMessage(); TestProtectedMessage(); TestConfirmationMessage(); - TestSampleCr(); + TestSampleCr(); } -// [Test] -// public void TestServerSideKey() -// { -// RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator(); -// rsaKeyPairGenerator.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(65537), new SecureRandom(), 2048, 100)); -// AsymmetricCipherKeyPair rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); -// -// TestCertBuilder builder = new TestCertBuilder() -// { -// Issuer = new X509Name("CN=Test"), -// Subject = new X509Name("CN=Test"), -// NotBefore = DateTime.UtcNow.AddDays(-1), -// NotAfter = DateTime.UtcNow.AddDays(1), -// PublicKey = rsaKeyPair.Public, -// SignatureAlgorithm = "MD5WithRSAEncryption" -// }; -// -// builder.AddAttribute(X509Name.C, "Foo"); -// X509Certificate cert = builder.Build(rsaKeyPair.Private); -// -// GeneralName sender = new GeneralName(new X509Name("CN=Sender")); -// GeneralName recipient = new GeneralName(new X509Name("CN=Recip")); -// -// -// -// } + // [Test] + // public void TestServerSideKey() + // { + // RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator(); + // rsaKeyPairGenerator.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(65537), new SecureRandom(), 2048, 100)); + // AsymmetricCipherKeyPair rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); + // + // TestCertBuilder builder = new TestCertBuilder() + // { + // Issuer = new X509Name("CN=Test"), + // Subject = new X509Name("CN=Test"), + // NotBefore = DateTime.UtcNow.AddDays(-1), + // NotAfter = DateTime.UtcNow.AddDays(1), + // PublicKey = rsaKeyPair.Public, + // SignatureAlgorithm = "MD5WithRSAEncryption" + // }; + // + // builder.AddAttribute(X509Name.C, "Foo"); + // X509Certificate cert = builder.Build(rsaKeyPair.Private); + // + // GeneralName sender = new GeneralName(new X509Name("CN=Sender")); + // GeneralName recipient = new GeneralName(new X509Name("CN=Recip")); + // + // + // + // } [Test] public void TestNotBeforeNotAfter() @@ -72,12 +73,11 @@ namespace Org.BouncyCastle.Cmp.Tests rsaKeyPairGenerator.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(65537), new SecureRandom(), 2048, 100)); AsymmetricCipherKeyPair rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); - doNotBeforeNotAfterTest(rsaKeyPair, new DateTime(1,1,1,0,0,1), new DateTime(1,1,1,0,0,10)); + doNotBeforeNotAfterTest(rsaKeyPair, new DateTime(1, 1, 1, 0, 0, 1), new DateTime(1, 1, 1, 0, 0, 10)); doNotBeforeNotAfterTest(rsaKeyPair, DateTime.MinValue, new DateTime(1, 1, 1, 0, 0, 10)); doNotBeforeNotAfterTest(rsaKeyPair, new DateTime(1, 1, 1, 0, 0, 1), DateTime.MinValue); } - private void doNotBeforeNotAfterTest(AsymmetricCipherKeyPair kp, DateTime notBefore, DateTime notAfter) { CertificateRequestMessageBuilder builder = new CertificateRequestMessageBuilder(BigInteger.One) @@ -89,11 +89,11 @@ namespace Org.BouncyCastle.Cmp.Tests if (!notBefore.Equals(DateTime.MinValue)) { - IsTrue("NotBefore did not match",(notBefore.Equals(msg.GetCertTemplate().Validity.NotBefore.ToDateTime()))); + IsTrue("NotBefore did not match", (notBefore.Equals(msg.GetCertTemplate().Validity.NotBefore.ToDateTime()))); } else { - IsTrue("Expected NotBefore to empty.",DateTime.MinValue == msg.GetCertTemplate().Validity.NotBefore.ToDateTime()); + IsTrue("Expected NotBefore to empty.", DateTime.MinValue == msg.GetCertTemplate().Validity.NotBefore.ToDateTime()); } if (!notAfter.Equals(DateTime.MinValue)) @@ -104,10 +104,8 @@ namespace Org.BouncyCastle.Cmp.Tests { IsTrue("Expected NotAfter to be empty.", DateTime.MinValue == msg.GetCertTemplate().Validity.NotAfter.ToDateTime()); } - } - [Test] public void TestSubsequentMessage() { @@ -115,17 +113,13 @@ namespace Org.BouncyCastle.Cmp.Tests rsaKeyPairGenerator.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(65537), new SecureRandom(), 2048, 100)); AsymmetricCipherKeyPair rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); - TestCertBuilder builder = new TestCertBuilder() - { - NotBefore = DateTime.UtcNow.AddDays(-1), - NotAfter = DateTime.UtcNow.AddDays(1), - PublicKey = rsaKeyPair.Public, - SignatureAlgorithm = "Sha1WithRSAEncryption" - - }; + TestCertBuilder builder = new TestCertBuilder(); + builder.NotBefore = DateTime.UtcNow.AddDays(-1); + builder.NotAfter = DateTime.UtcNow.AddDays(1); + builder.PublicKey = rsaKeyPair.Public; + builder.SignatureAlgorithm = "Sha1WithRSAEncryption"; X509Certificate cert = builder.Build(rsaKeyPair.Private); - GeneralName user = new GeneralName(new X509Name("CN=Test")); CertificateRequestMessageBuilder crmBuiler = new CertificateRequestMessageBuilder(BigInteger.One) @@ -134,21 +128,18 @@ namespace Org.BouncyCastle.Cmp.Tests ISignatureFactory sigFact = new Asn1SignatureFactory("SHA256WithRSA", rsaKeyPair.Private); - ProtectedPkiMessage certRequestMsg = new ProtectedPkiMessageBuilder(user,user) - .SetTransactionId(new byte[]{1,2,3,4,5}) - .SetBody(new PkiBody(PkiBody.TYPE_KEY_RECOVERY_REQ, new CertReqMessages(new CertReqMsg[]{crmBuiler.Build().ToAsn1Structure()}))) - .AddCmpCertificate(cert) + ProtectedPkiMessage certRequestMsg = new ProtectedPkiMessageBuilder(user, user) + .SetTransactionId(new byte[] { 1, 2, 3, 4, 5 }) + .SetBody(new PkiBody(PkiBody.TYPE_KEY_RECOVERY_REQ, new CertReqMessages(new CertReqMsg[] { crmBuiler.Build().ToAsn1Structure() }))) + .AddCmpCertificate(cert) .Build(sigFact); - ProtectedPkiMessage msg = new ProtectedPkiMessage(new GeneralPKIMessage(certRequestMsg.ToAsn1Message().GetDerEncoded())); + ProtectedPkiMessage msg = new ProtectedPkiMessage(new GeneralPkiMessage(certRequestMsg.ToAsn1Message().GetDerEncoded())); CertReqMessages reqMsgs = CertReqMessages.GetInstance(msg.Body.Content); CertReqMsg reqMsg = reqMsgs.ToCertReqMsgArray()[0]; IsEquals(ProofOfPossession.TYPE_KEY_ENCIPHERMENT, reqMsg.Popo.Type); - } - - [Test] public void TestSampleCr() { @@ -162,15 +153,11 @@ namespace Org.BouncyCastle.Cmp.Tests "Ep9wvv+3+td5MEO+qMuWWxF8OZBlYIFBZ/k+pGlU+4XlBP5Ai6pu/EI/0A+1/bcGs0sQIDAQABMBQwEgYJKwY" + "BBQUHBQEBDAVEVU1NWaACBQCgFwMVAO73HUPF//mY5+E714Cv5oprt0kO\r\n"); - ProtectedPkiMessage msg = new ProtectedPkiMessage(new GeneralPKIMessage(raw)); + ProtectedPkiMessage msg = new ProtectedPkiMessage(new GeneralPkiMessage(raw)); - - IsTrue(msg.Verify(new PKMacBuilder(), "TopSecret1234".ToCharArray())); - } - [Test] public void TestConfirmationMessage() { @@ -178,14 +165,11 @@ namespace Org.BouncyCastle.Cmp.Tests rsaKeyPairGenerator.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(65537), new SecureRandom(), 2048, 100)); AsymmetricCipherKeyPair rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); - TestCertBuilder builder = new TestCertBuilder() - { - NotBefore = DateTime.UtcNow.AddDays(-1), - NotAfter = DateTime.UtcNow.AddDays(1), - PublicKey = rsaKeyPair.Public, - SignatureAlgorithm = "Sha1WithRSAEncryption" - - }; + TestCertBuilder builder = new TestCertBuilder(); + builder.NotBefore = DateTime.UtcNow.AddDays(-1); + builder.NotAfter = DateTime.UtcNow.AddDays(1); + builder.PublicKey = rsaKeyPair.Public; + builder.SignatureAlgorithm = "Sha1WithRSAEncryption"; builder.AddAttribute(X509Name.C, "Foo"); X509Certificate cert = builder.Build(rsaKeyPair.Private); @@ -198,7 +182,7 @@ namespace Org.BouncyCastle.Cmp.Tests .Build(); ProtectedPkiMessageBuilder msgBuilder = new ProtectedPkiMessageBuilder(sender, recipient); - msgBuilder.SetBody(new PkiBody(PkiBody.TYPE_CERT_CONFIRM, content.ToAsn1Structure())); + msgBuilder.SetBody(new PkiBody(PkiBody.TYPE_CERT_CONFIRM, content.ToAsn1Structure())); msgBuilder.AddCmpCertificate(cert); ISignatureFactory sigFact = new Asn1SignatureFactory("MD5WithRSA", rsaKeyPair.Private); @@ -208,51 +192,46 @@ namespace Org.BouncyCastle.Cmp.Tests IsTrue("PKIMessage must verify (MD5withRSA)", msg.Verify(verifierFactory)); - IsEquals(sender,msg.Header.Sender); - IsEquals(recipient,msg.Header.Recipient); + IsEquals(sender, msg.Header.Sender); + IsEquals(recipient, msg.Header.Recipient); content = new CertificateConfirmationContent(CertConfirmContent.GetInstance(msg.Body.Content), new DefaultDigestAlgorithmIdentifierFinder()); CertificateStatus[] statusList = content.GetStatusMessages(); - IsEquals(1,statusList.Length); + IsEquals(1, statusList.Length); IsTrue(statusList[0].IsVerified(cert)); } - - [Test] - public void TestProtectedMessage() + public void TestProtectedMessage() { - RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator(); - rsaKeyPairGenerator.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(65537),new SecureRandom(),2048,100)); - AsymmetricCipherKeyPair rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); + RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator(); + rsaKeyPairGenerator.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(65537), new SecureRandom(), 2048, 100)); + AsymmetricCipherKeyPair rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); - TestCertBuilder builder = new TestCertBuilder() - { - NotBefore = DateTime.UtcNow.AddDays(-1), - NotAfter = DateTime.UtcNow.AddDays(1), - PublicKey = rsaKeyPair.Public, - SignatureAlgorithm = "Sha1WithRSAEncryption" - - }; + TestCertBuilder builder = new TestCertBuilder(); + builder.NotBefore = DateTime.UtcNow.AddDays(-1); + builder.NotAfter = DateTime.UtcNow.AddDays(1); + builder.PublicKey = rsaKeyPair.Public; + builder.SignatureAlgorithm = "Sha1WithRSAEncryption"; builder.AddAttribute(X509Name.C, "Foo"); X509Certificate cert = builder.Build(rsaKeyPair.Private); GeneralName sender = new GeneralName(new X509Name("CN=Sender")); GeneralName recipient = new GeneralName(new X509Name("CN=Recip")); - - ProtectedPkiMessageBuilder msgBuilder = new ProtectedPkiMessageBuilder(sender,recipient); + + ProtectedPkiMessageBuilder msgBuilder = new ProtectedPkiMessageBuilder(sender, recipient); msgBuilder.AddCmpCertificate(cert); - - ISignatureFactory sigFact = new Asn1SignatureFactory("MD5WithRSA",rsaKeyPair.Private); - ProtectedPkiMessage msg = msgBuilder.Build(sigFact); + ISignatureFactory sigFact = new Asn1SignatureFactory("MD5WithRSA", rsaKeyPair.Private); + + ProtectedPkiMessage msg = msgBuilder.Build(sigFact); X509Certificate certificate = msg.GetCertificates()[0]; IVerifierFactory verifierFactory = new Asn1VerifierFactory("MD5WithRSA", rsaKeyPair.Public); - IsTrue("PKIMessage must verify (MD5withRSA)",msg.Verify(verifierFactory)); + IsTrue("PKIMessage must verify (MD5withRSA)", msg.Verify(verifierFactory)); } [Test] @@ -263,14 +242,11 @@ namespace Org.BouncyCastle.Cmp.Tests 100)); AsymmetricCipherKeyPair rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); - TestCertBuilder builder = new TestCertBuilder() - { - NotBefore = DateTime.UtcNow.AddDays(-1), - NotAfter = DateTime.UtcNow.AddDays(1), - PublicKey = rsaKeyPair.Public, - SignatureAlgorithm = "Sha1WithRSAEncryption" - - }; + TestCertBuilder builder = new TestCertBuilder(); + builder.NotBefore = DateTime.UtcNow.AddDays(-1); + builder.NotAfter = DateTime.UtcNow.AddDays(1); + builder.PublicKey = rsaKeyPair.Public; + builder.SignatureAlgorithm = "Sha1WithRSAEncryption"; builder.AddAttribute(X509Name.C, "Foo"); X509Certificate cert = builder.Build(rsaKeyPair.Private); @@ -291,79 +267,102 @@ namespace Org.BouncyCastle.Cmp.Tests IsTrue(msg.Verify(macFactory, "testpass".ToCharArray())); } - - - [Test] public void TestVerifyBCJavaGeneratedMessage() { - // - // Test with content generated by BC-JAVA version. - // + // + // Test with content generated by BC-JAVA version. + // - ICipherParameters publicKey = PublicKeyFactory.CreateKey(Hex.Decode( - "305c300d06092a864886f70d0101010500034b003048024100ac1e59ba5f96" + - "ba86c86e6d8bbfd43ece04265fa29e6ebdb320388b58af365d05b26970cbd2" + - "6e5b0fa7df2074b90b42a1d16ab270cdb851b53e464b87f683774502030100" + - "01")); - ICipherParameters privateKey = PrivateKeyFactory.CreateKey(Hex.Decode( - "30820155020100300d06092a864886f70d01010105000482013f3082013b02" + - "0100024100ac1e59ba5f96ba86c86e6d8bbfd43ece04265fa29e6ebdb32038" + - "8b58af365d05b26970cbd26e5b0fa7df2074b90b42a1d16ab270cdb851b53e" + - "464b87f68377450203010001024046f3f208570c735349bfe00fdaa1fbcc00" + - "c0f2eebe42279876a168ac43fa74a8cdf9a1bb49066c07cfcfa7196f69f2b9" + - "419d378109db967891428c50273dcc37022100d488dc3fb86f404d726a8166" + - "b2a9aba9bee12fdbf38470a62403a2a20bad0977022100cf51874e479b141f" + - "9915533bf54d68f1940f84d7fe6130538ff01a23e3493423022100986f94f1" + - "0afa9837341219bfabf32fd16ebb9a94fa630a5ccf45e036b383275f02201b" + - "6dff07f563684b31f6e757548254733a12bf91d05f4d8490d3c4b1a0ddcb9f" + - "02210087c3b2049e9a3edfc4cb40a3a275dabf7ffff80b467157e384603042" + - "3fe91d68")); - - byte[] ind = Hex.Decode( - "308201ac306e020102a4133011310f300d06035504030c0653656e646572a4" + - "123010310e300c06035504030c055265636970a140303e06092a864886f67d" + - "07420d30310414fdccb4ffd7848e6a697bee36cbe0f3722ed7fe2f30070605" + - "2b0e03021a020203e8300c06082b060105050801020500a10430023000a017" + - "031500c131c357441daa78eb538bfd9c24870e220fdafaa182011930820115" + - "308201113081bca003020102020601684a515d5b300d06092a864886f70d01" + - "01050500300f310d300b06035504030c0454657374301e170d313930313134" + - "3033303433325a170d3139303432343033303433325a300f310d300b060355" + - "04030c0454657374305c300d06092a864886f70d0101010500034b00304802" + - "4100ac1e59ba5f96ba86c86e6d8bbfd43ece04265fa29e6ebdb320388b58af" + - "365d05b26970cbd26e5b0fa7df2074b90b42a1d16ab270cdb851b53e464b87" + - "f68377450203010001300d06092a864886f70d0101050500034100264b5b76" + - "f268e2a992f05ad83783b091ce806a6726912c6200d06b33375ae58fe3c474" + - "c3a42ad6e572a2c48ae3bf914a7510bb995c3474829cfe71ab679a3db0"); - - - ProtectedPkiMessage pkiMsg = new ProtectedPkiMessage(PkiMessage.GetInstance(ind)); - - PbmParameter pbmParameters = PbmParameter.GetInstance(pkiMsg.Header.ProtectionAlg.Parameters); - - IsTrue(pkiMsg.Verify(new PKMacBuilder().SetParameters(pbmParameters), "secret".ToCharArray())); + ICipherParameters publicKey = PublicKeyFactory.CreateKey(Hex.Decode( + "305c300d06092a864886f70d0101010500034b003048024100ac1e59ba5f96" + + "ba86c86e6d8bbfd43ece04265fa29e6ebdb320388b58af365d05b26970cbd2" + + "6e5b0fa7df2074b90b42a1d16ab270cdb851b53e464b87f683774502030100" + + "01")); + ICipherParameters privateKey = PrivateKeyFactory.CreateKey(Hex.Decode( + "30820155020100300d06092a864886f70d01010105000482013f3082013b02" + + "0100024100ac1e59ba5f96ba86c86e6d8bbfd43ece04265fa29e6ebdb32038" + + "8b58af365d05b26970cbd26e5b0fa7df2074b90b42a1d16ab270cdb851b53e" + + "464b87f68377450203010001024046f3f208570c735349bfe00fdaa1fbcc00" + + "c0f2eebe42279876a168ac43fa74a8cdf9a1bb49066c07cfcfa7196f69f2b9" + + "419d378109db967891428c50273dcc37022100d488dc3fb86f404d726a8166" + + "b2a9aba9bee12fdbf38470a62403a2a20bad0977022100cf51874e479b141f" + + "9915533bf54d68f1940f84d7fe6130538ff01a23e3493423022100986f94f1" + + "0afa9837341219bfabf32fd16ebb9a94fa630a5ccf45e036b383275f02201b" + + "6dff07f563684b31f6e757548254733a12bf91d05f4d8490d3c4b1a0ddcb9f" + + "02210087c3b2049e9a3edfc4cb40a3a275dabf7ffff80b467157e384603042" + + "3fe91d68")); + + byte[] ind = Hex.Decode( + "308201ac306e020102a4133011310f300d06035504030c0653656e646572a4" + + "123010310e300c06035504030c055265636970a140303e06092a864886f67d" + + "07420d30310414fdccb4ffd7848e6a697bee36cbe0f3722ed7fe2f30070605" + + "2b0e03021a020203e8300c06082b060105050801020500a10430023000a017" + + "031500c131c357441daa78eb538bfd9c24870e220fdafaa182011930820115" + + "308201113081bca003020102020601684a515d5b300d06092a864886f70d01" + + "01050500300f310d300b06035504030c0454657374301e170d313930313134" + + "3033303433325a170d3139303432343033303433325a300f310d300b060355" + + "04030c0454657374305c300d06092a864886f70d0101010500034b00304802" + + "4100ac1e59ba5f96ba86c86e6d8bbfd43ece04265fa29e6ebdb320388b58af" + + "365d05b26970cbd26e5b0fa7df2074b90b42a1d16ab270cdb851b53e464b87" + + "f68377450203010001300d06092a864886f70d0101050500034100264b5b76" + + "f268e2a992f05ad83783b091ce806a6726912c6200d06b33375ae58fe3c474" + + "c3a42ad6e572a2c48ae3bf914a7510bb995c3474829cfe71ab679a3db0"); + + ProtectedPkiMessage pkiMsg = new ProtectedPkiMessage(PkiMessage.GetInstance(ind)); + + PbmParameter pbmParameters = PbmParameter.GetInstance(pkiMsg.Header.ProtectionAlg.Parameters); + + IsTrue(pkiMsg.Verify(new PKMacBuilder().SetParameters(pbmParameters), "secret".ToCharArray())); + } } - - -} - public class TestCertBuilder { IDictionary attrs = new Hashtable(); IList ord = new ArrayList(); IList values = new ArrayList(); - public DateTime NotBefore { get; set; } + private DateTime notBefore, notAfter; + private AsymmetricKeyParameter publicKey; + private string signatureAlgorithm; + private X509Name issuer, subject; - public DateTime NotAfter { get; set; } + public DateTime NotBefore + { + get { return notBefore; } + set { this.notBefore = value; } + } - public AsymmetricKeyParameter PublicKey { get; set; } + public DateTime NotAfter + { + get { return notAfter; } + set { this.notAfter = value; } + } - public String SignatureAlgorithm { get; set; } + public AsymmetricKeyParameter PublicKey + { + get { return publicKey; } + set { this.publicKey = value; } + } - public X509Name Issuer { get; set; } - public X509Name Subject { get; set; } + public string SignatureAlgorithm + { + get { return signatureAlgorithm; } + set { this.signatureAlgorithm = value; } + } + + public X509Name Issuer + { + get { return issuer; } + set { this.issuer = value; } + } + + public X509Name Subject + { + get { return subject; } + set { this.subject = value; } + } public TestCertBuilder AddAttribute(DerObjectIdentifier name, Object value) { @@ -388,7 +387,6 @@ namespace Org.BouncyCastle.Cmp.Tests certGen.SetIssuerDN(new X509Name(ord, attrs)); } - certGen.SetNotBefore(NotBefore); certGen.SetNotAfter(NotAfter); @@ -401,7 +399,6 @@ namespace Org.BouncyCastle.Cmp.Tests certGen.SetSubjectDN(new X509Name(ord, attrs)); } - certGen.SetPublicKey(PublicKey); certGen.SetSignatureAlgorithm(SignatureAlgorithm); diff --git a/crypto/test/src/crmf/test/CrmfTest.cs b/crypto/test/src/crmf/test/CrmfTest.cs index b1f18cc7b..8211fe1c4 100644 --- a/crypto/test/src/crmf/test/CrmfTest.cs +++ b/crypto/test/src/crmf/test/CrmfTest.cs @@ -1,7 +1,8 @@ using System; using System.Collections; -using NUnit.Core; + using NUnit.Framework; + using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.Cms; using Org.BouncyCastle.Asn1.Crmf; @@ -15,23 +16,23 @@ using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Crypto.Operators; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; +using Org.BouncyCastle.Operators; using Org.BouncyCastle.Pkcs; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; using Org.BouncyCastle.X509; -using Org.BouncyCastle.Operators; namespace Org.BouncyCastle.Crmf.Tests { [TestFixture] public class CrmfTest : SimpleTest { - - - public override string Name => "CRMF Tests"; - + public override string Name + { + get { return "CRMF"; } + } public override void PerformTest() { @@ -44,14 +45,14 @@ namespace Org.BouncyCastle.Crmf.Tests [Test] public void TestFromJVM() { - var pubKey = PublicKeyFactory.CreateKey(Hex.Decode( + AsymmetricKeyParameter pubKey = PublicKeyFactory.CreateKey(Hex.Decode( "305c300d06092a864886f70d0101010500034b003048024100bbb3f6a5031fbb1feedbfed7584a4f6321ccdc16b9526b0f6e31859328db35a6ec420a98e14fb3bcf192004b1aa6fc9269410204785cc01317232feb545a7b410203010001")); - var privKey = PrivateKeyFactory.CreateKey(Hex.Decode("30820153020100300d06092a864886f70d01010105000482013d30820139020100024100bbb3f6a5031fbb1feedbfed7584a4f6321ccdc16b9526b0f6e31859328db35a6ec420a98e14fb3bcf192004b1aa6fc9269410204785cc01317232feb545a7b41020301000102400093b384b9021c4cd59888e956cb1e653e736833235315b0e938116da19a9276b1ea1fe33da580a497313f08eb3e7c14627508a4284be04ea3e6ba8cb4b0a5c9022100e2fe0d9f35bfd7ecf196227e5e915a2464478ea7033c6dff4ce6a02961759a49022100d3b093770745dfea42c5c5c31f1a6b797a60dfb5503ae60f70b864452c4a193902203cc761c65b91feb3070cf8377602dd6c191dbfe8a04931fac6108a9a09ea7f61022071bb2a5f06af49cfc8340d3df995ee2c03cdcc22d389f15456511abdf73f9031022065bc10d43192cb3131c53be18a0d41a060d4e0a3324a47e3eb4bf720e1b46b10")); + AsymmetricKeyParameter privKey = PrivateKeyFactory.CreateKey(Hex.Decode("30820153020100300d06092a864886f70d01010105000482013d30820139020100024100bbb3f6a5031fbb1feedbfed7584a4f6321ccdc16b9526b0f6e31859328db35a6ec420a98e14fb3bcf192004b1aa6fc9269410204785cc01317232feb545a7b41020301000102400093b384b9021c4cd59888e956cb1e653e736833235315b0e938116da19a9276b1ea1fe33da580a497313f08eb3e7c14627508a4284be04ea3e6ba8cb4b0a5c9022100e2fe0d9f35bfd7ecf196227e5e915a2464478ea7033c6dff4ce6a02961759a49022100d3b093770745dfea42c5c5c31f1a6b797a60dfb5503ae60f70b864452c4a193902203cc761c65b91feb3070cf8377602dd6c191dbfe8a04931fac6108a9a09ea7f61022071bb2a5f06af49cfc8340d3df995ee2c03cdcc22d389f15456511abdf73f9031022065bc10d43192cb3131c53be18a0d41a060d4e0a3324a47e3eb4bf720e1b46b10")); - var rawMsg = Hex.Decode("3081cc30760201013071a511300f310d300b0603550403130454657374a65c300d06092a864886f70d0101010500034b003048024100bbb3f6a5031fbb1feedbfed7584a4f6321ccdc16b9526b0f6e31859328db35a6ec420a98e14fb3bcf192004b1aa6fc9269410204785cc01317232feb545a7b410203010001a152300d06092a864886f70d01010505000341003120cdb58edfef4a2e1a4bfe96b972007c1d1c949221d266efe28b45ba036b9d534f5dca261dce8f21e134d97e55c3bd76d1460781fd9703f8f9907d1f036c20"); + byte[] rawMsg = Hex.Decode("3081cc30760201013071a511300f310d300b0603550403130454657374a65c300d06092a864886f70d0101010500034b003048024100bbb3f6a5031fbb1feedbfed7584a4f6321ccdc16b9526b0f6e31859328db35a6ec420a98e14fb3bcf192004b1aa6fc9269410204785cc01317232feb545a7b410203010001a152300d06092a864886f70d01010505000341003120cdb58edfef4a2e1a4bfe96b972007c1d1c949221d266efe28b45ba036b9d534f5dca261dce8f21e134d97e55c3bd76d1460781fd9703f8f9907d1f036c20"); - var msg = new CertificateRequestMessage(rawMsg); - IsTrue("Pop Valid",msg.IsValidSigningKeyPop(new Asn1VerifierFactoryProvider(pubKey))); + CertificateRequestMessage msg = new CertificateRequestMessage(rawMsg); + IsTrue("Pop Valid", msg.IsValidSigningKeyPop(new Asn1VerifierFactoryProvider(pubKey))); // // Vandalize message to check for failure. @@ -59,28 +60,25 @@ namespace Org.BouncyCastle.Crmf.Tests rawMsg[7] ^= 1; msg = new CertificateRequestMessage(rawMsg); - - IsTrue("Pop Verified Vandalized Message!", !msg.IsValidSigningKeyPop(new Asn1VerifierFactoryProvider(pubKey))); + IsTrue("Pop Verified Vandalized Message!", !msg.IsValidSigningKeyPop(new Asn1VerifierFactoryProvider(pubKey))); } - - [Test] public void TestBasicMessage() { - var rsaKeyPairGenerator = new RsaKeyPairGenerator(); + RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator(); rsaKeyPairGenerator.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(65537), new SecureRandom(), 2048, 100)); - var rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); + AsymmetricCipherKeyPair rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); + + CertificateRequestMessageBuilder certReqBuild = new CertificateRequestMessageBuilder(BigInteger.One); - var certReqBuild = new CertificateRequestMessageBuilder(BigInteger.One); - certReqBuild.SetSubject(new X509Name("CN=Test")) .SetPublicKey(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(rsaKeyPair.Public)) .SetProofOfPossessionSignKeySigner(new Asn1SignatureFactory("SHA1WithRSA", rsaKeyPair.Private)); - var certificateRequestMessage = certReqBuild.Build(); - + CertificateRequestMessage certificateRequestMessage = certReqBuild.Build(); + IsTrue("Signing Key Pop Valid",certificateRequestMessage.IsValidSigningKeyPop(new Asn1VerifierFactoryProvider(rsaKeyPair.Public))); IsTrue(certificateRequestMessage.GetCertTemplate().Subject.Equivalent(new X509Name("CN=Test"))); IsTrue(certificateRequestMessage.GetCertTemplate().PublicKey.Equals(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(rsaKeyPair.Public))); @@ -89,27 +87,24 @@ namespace Org.BouncyCastle.Crmf.Tests [Test] public void TestBasicMessageWithArchiveControl() { - var rsaKeyPairGenerator = new RsaKeyPairGenerator(); + RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator(); rsaKeyPairGenerator.Init(new RsaKeyGenerationParameters(BigInteger.ValueOf(65537), new SecureRandom(), 2048, 100)); - var rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); - - var tcb = new TestCertBuilder() - { - PublicKey = rsaKeyPair.Public, - Subject = new X509Name("CN=Test"), - Issuer = new X509Name("CN=Test"), - NotBefore = DateTime.UtcNow.AddDays(-1), - NotAfter = DateTime.UtcNow.AddDays(1), - SignatureAlgorithm = "Sha1WithRSAEncryption" - }; + AsymmetricCipherKeyPair rsaKeyPair = rsaKeyPairGenerator.GenerateKeyPair(); - var cert = tcb.Build(rsaKeyPair.Private); + TestCertBuilder tcb = new TestCertBuilder(); + tcb.PublicKey = rsaKeyPair.Public; + tcb.Subject = new X509Name("CN=Test"); + tcb.Issuer = new X509Name("CN=Test"); + tcb.NotBefore = DateTime.UtcNow.AddDays(-1); + tcb.NotAfter = DateTime.UtcNow.AddDays(1); + tcb.SignatureAlgorithm = "Sha1WithRSAEncryption"; - var publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(rsaKeyPair.Public); - var privateInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(rsaKeyPair.Private); + X509Certificate cert = tcb.Build(rsaKeyPair.Private); + SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(rsaKeyPair.Public); + PrivateKeyInfo privateInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(rsaKeyPair.Private); - var certificateRequestMessageBuilder = new CertificateRequestMessageBuilder(BigInteger.One); + CertificateRequestMessageBuilder certificateRequestMessageBuilder = new CertificateRequestMessageBuilder(BigInteger.One); certificateRequestMessageBuilder.SetSubject(new X509Name("CN=Test")); certificateRequestMessageBuilder.SetPublicKey(publicKeyInfo); @@ -119,14 +114,13 @@ namespace Org.BouncyCastle.Crmf.Tests .Build(new CmsContentEncryptorBuilder(NistObjectIdentifiers.IdAes128Cbc).Build()) ); - var msg = certificateRequestMessageBuilder.Build(); + CertificateRequestMessage msg = certificateRequestMessageBuilder.Build(); IsTrue(Arrays.AreEqual(msg.GetCertTemplate().Subject.GetEncoded(), new X509Name("CN=Test").GetEncoded())); IsTrue(Arrays.AreEqual(msg.GetCertTemplate().PublicKey.GetEncoded(),publicKeyInfo.GetEncoded())); - checkCertReqMsgWithArchiveControl(rsaKeyPair,msg); - checkCertReqMsgWithArchiveControl(rsaKeyPair, new CertificateRequestMessage(msg.GetEncoded())); - + CheckCertReqMsgWithArchiveControl(rsaKeyPair,msg); + CheckCertReqMsgWithArchiveControl(rsaKeyPair, new CertificateRequestMessage(msg.GetEncoded())); } [Test] @@ -136,31 +130,27 @@ namespace Org.BouncyCastle.Crmf.Tests Hex.Decode("305c300d06092a864886f70d0101010500034b003048024100a9a94b7b98dc3daf8cac032a14bd4510832b0e007edbdafc065e328645a35828b8185cdbf73ed495c88436b11a9322965595d2e4c1dd63c3c4d41812f876b3070203010001")); AsymmetricKeyParameter privateKey = PrivateKeyFactory.CreateKey( Hex.Decode("30820154020100300d06092a864886f70d01010105000482013e3082013a020100024100a9a94b7b98dc3daf8cac032a14bd4510832b0e007edbdafc065e328645a35828b8185cdbf73ed495c88436b11a9322965595d2e4c1dd63c3c4d41812f876b307020301000102400831deacfe21a9331902d7f648e1297c563196b00c70971fb439098cb5c1618925bdbac4c66b30f8956660220f326f51e5a1725ce690165154fb62fa14497265022100e54943be1b4951e127f6e79c5ab333cba4b0fff0b5e59328d6393ba98dc0e6c3022100bd6da58ce195146a1d3825ec2a622cf4962da653096bea87fbd9a94db266a66d0221008948bcceeef78f97089ec53ed0efcb6b7b489f7638f32491a6f2cdce4f99d89102204eb1b066d8883054ed12985e863506ec0d3fa5ab356cc99ff876b228ff0639f9022024049aaf39bf9a0ddfbd4caee277d0a9f07d075faae12571176a5c0ca40415c0")); - + CertificateRequestMessage msg = new CertificateRequestMessage( Hex.Decode("308202af308202ab0201013071a511300f310d300b0603550403130454657374a65c300d06092a864886f70d0101010500034b003048024100a9a94b7b98dc3daf8cac032a14bd4510832b0e007edbdafc065e328645a35828b8185cdbf73ed495c88436b11a9322965595d2e4c1dd63c3c4d41812f876b3070203010001308202313082022d06092b0601050507050104a082021ea082021a0201003171306f0201003019300f310d300b06035504030c04546573740206016859de5806300d06092a864886f70d0101010500044066f1a72f808908af784b83c07895276104d7c4caaee6090212ce5b27517aec510425b784352b5342c999f844b8796286f10a59807e290f06aa39f8cba86dd6bf308201a0060b2a864886f70d0109100115301d060960864801650304010204104aceaa277cc7974ea2a775ff9db6062580820170c648e70c25c4789d2ff4ed398e5536efb45d2dd8ba76a628ad30bf9596a18337afc0f596f0c18e05fb3fa9944ed9691dae1d9b327b5bbafaaa63efb0e22d675811c27bfb023b80184325fd4b67b3b9e41bf43c5583a86433b230e09a34b61397ddff0eadf10c883fc1f01860e2a56ab4002dcc4d4925c53e09dde0b99928fdf602bce544722155cebd8816e91a411a99feea07695774cd8883034022d57f64e9cd3383c3125c48db2936b7395a22b17910be1f2c0b8650bdb5bd752ffc40fcd30169e5ae3a4ac7ad9cc850e9c17bbcf8e1a1898d0d8be19145c484467b8f1124657a5e08c10fc67416274990cc16d55c9fb76c265dd436b7e803425892297f1a08e4fab8e178874b2b3bf9c749693d609db208e9a3ebbddd26cd6a1b33c0201532170dc6c303e7ac0c42ba0bc54dfb928b228842b6bb08d8dc411d262dabf140a8b5a5c67ea486c1877a2fc000981d54cf2decaf1cfeebcf83134992b09a2b1fe9e02da25b874604b5d8bbd609875ba8")); AsymmetricCipherKeyPair rsaKeyPair = new AsymmetricCipherKeyPair(publicKey,privateKey); - SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey); - - Console.WriteLine(msg.GetCertTemplate().Subject.Equivalent(new X509Name("CN=Test"))); - IsTrue(msg.GetCertTemplate().Subject.Equivalent(new X509Name("CN=Test"))); IsTrue(Arrays.AreEqual(msg.GetCertTemplate().PublicKey.GetEncoded(), publicKeyInfo.GetEncoded())); - checkCertReqMsgWithArchiveControl(rsaKeyPair, msg); - checkCertReqMsgWithArchiveControl(rsaKeyPair, new CertificateRequestMessage(msg.GetEncoded())); + CheckCertReqMsgWithArchiveControl(rsaKeyPair, msg); + CheckCertReqMsgWithArchiveControl(rsaKeyPair, new CertificateRequestMessage(msg.GetEncoded())); - checkCertReqMsgWithArchiveControl(rsaKeyPair,msg); + CheckCertReqMsgWithArchiveControl(rsaKeyPair,msg); } - private void checkCertReqMsgWithArchiveControl(AsymmetricCipherKeyPair kp, CertificateRequestMessage certReqMessage) + private void CheckCertReqMsgWithArchiveControl(AsymmetricCipherKeyPair kp, CertificateRequestMessage certReqMessage) { - var archiveControl = - (PkiArchiveControl) certReqMessage.GetControl(CrmfObjectIdentifiers.id_regCtrl_pkiArchiveOptions); + PkiArchiveControl archiveControl = (PkiArchiveControl)certReqMessage.GetControl( + CrmfObjectIdentifiers.id_regCtrl_pkiArchiveOptions); IsEquals("Archive type", PkiArchiveControl.encryptedPrivKey, archiveControl.ArchiveType); IsTrue(archiveControl.EnvelopedData); @@ -172,7 +162,6 @@ namespace Org.BouncyCastle.Crmf.Tests KeyTransRecipientInformation info = (KeyTransRecipientInformation)collection[0]; EncKeyWithID encKeyWithId = EncKeyWithID.GetInstance(info.GetContent(kp.Private)); - IsTrue(encKeyWithId.HasIdentifier); IsTrue(!encKeyWithId.IsIdentifierUtf8String); // GeneralName at this point. @@ -181,8 +170,6 @@ namespace Org.BouncyCastle.Crmf.Tests PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(kp.Private); IsTrue("Private Key", Arrays.AreEqual(privateKeyInfo.GetEncoded(), encKeyWithId.PrivateKey.GetEncoded())); - } - } -} \ No newline at end of file +} diff --git a/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs b/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs index 8c5997835..7e0a84cbe 100644 --- a/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs +++ b/crypto/test/src/crypto/test/ECGOST3410_2012Test.cs @@ -1,5 +1,7 @@ using System; + using NUnit.Framework; + using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Asn1.CryptoPro; using Org.BouncyCastle.Asn1.Pkcs; @@ -17,19 +19,19 @@ using Org.BouncyCastle.X509; namespace Org.BouncyCastle.Crypto.Tests { [TestFixture] - public class ECGOST3410_2012Test:SimpleTest + public class ECGost3410_2012Test + : SimpleTest { public override string Name { - get { return "ECGOST3410-2012-Test"; } + get { return "ECGOST3410-2012"; } } public SimpleTestResult EncodeRecodePublicKey() { - DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid("Tc26-Gost-3410-12-512-paramSetA"); ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); - ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512,null); + ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, null); ECKeyGenerationParameters paramameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); ECKeyPairGenerator engine = new ECKeyPairGenerator(); engine.Init(paramameters); @@ -38,328 +40,302 @@ namespace Org.BouncyCastle.Crypto.Tests ECPublicKeyParameters generatedKeyParameters = (ECPublicKeyParameters)pair.Public; ECPublicKeyParameters keyParameters = generatedKeyParameters; - // // Continuously encode/decode the key and check for loss of information. // - for (int t = 0; t < 3; t++) - { - - SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyParameters); - keyParameters = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(info); - - { // Specifically cast and test gost parameters. - ECGost3410Parameters gParam = (ECGost3410Parameters)generatedKeyParameters.Parameters; - ECGost3410Parameters rParam = (ECGost3410Parameters)keyParameters.Parameters; - + for (int t = 0; t < 3; t++) + { + SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyParameters); + keyParameters = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(info); - bool ok = SafeEquals(gParam.DigestParamSet, rParam.DigestParamSet) && - SafeEquals(gParam.EncryptionParamSet, rParam.EncryptionParamSet) && - SafeEquals(gParam.PublicKeyParamSet, rParam.PublicKeyParamSet); + { + // Specifically cast and test gost parameters. + ECGost3410Parameters gParam = (ECGost3410Parameters)generatedKeyParameters.Parameters; + ECGost3410Parameters rParam = (ECGost3410Parameters)keyParameters.Parameters; - if (!ok) - { - return new SimpleTestResult(false, "GOST parameters does not match"); - } + bool ok = SafeEquals(gParam.DigestParamSet, rParam.DigestParamSet) && + SafeEquals(gParam.EncryptionParamSet, rParam.EncryptionParamSet) && + SafeEquals(gParam.PublicKeyParamSet, rParam.PublicKeyParamSet); - } - - if (!((ECGost3410Parameters)keyParameters.Parameters).Name.Equals( - ((ECGost3410Parameters)generatedKeyParameters.Parameters).Name)) + if (!ok) { - return new SimpleTestResult(false, "Name does not match"); + return new SimpleTestResult(false, "GOST parameters does not match"); } + } + if (!((ECGost3410Parameters)keyParameters.Parameters).Name.Equals( + ((ECGost3410Parameters)generatedKeyParameters.Parameters).Name)) + { + return new SimpleTestResult(false, "Name does not match"); + } - if (keyParameters.IsPrivate != generatedKeyParameters.IsPrivate) - { - return new SimpleTestResult(false, "isPrivate does not match"); - } + if (keyParameters.IsPrivate != generatedKeyParameters.IsPrivate) + { + return new SimpleTestResult(false, "isPrivate does not match"); + } - if (!Arrays.AreEqual(keyParameters.Q.GetEncoded(true), generatedKeyParameters.Q.GetEncoded(true))) - { - return new SimpleTestResult(false, "Q does not match"); - } + if (!Arrays.AreEqual(keyParameters.Q.GetEncoded(true), generatedKeyParameters.Q.GetEncoded(true))) + { + return new SimpleTestResult(false, "Q does not match"); + } - if (!keyParameters.Parameters.Curve.Equals(generatedKeyParameters.Parameters.Curve)) - { - return new SimpleTestResult(false, "Curve does not match"); - } + if (!keyParameters.Parameters.Curve.Equals(generatedKeyParameters.Parameters.Curve)) + { + return new SimpleTestResult(false, "Curve does not match"); + } - if (!Arrays.AreEqual( - keyParameters.Parameters.G.GetEncoded(true), - generatedKeyParameters.Parameters.G.GetEncoded(true))) - { - return new SimpleTestResult(false, "G does not match"); - } + if (!Arrays.AreEqual( + keyParameters.Parameters.G.GetEncoded(true), + generatedKeyParameters.Parameters.G.GetEncoded(true))) + { + return new SimpleTestResult(false, "G does not match"); + } - if (!keyParameters.Parameters.H.Equals(generatedKeyParameters.Parameters.H)) - { - return new SimpleTestResult(false, "H does not match"); - } + if (!keyParameters.Parameters.H.Equals(generatedKeyParameters.Parameters.H)) + { + return new SimpleTestResult(false, "H does not match"); + } - if (!keyParameters.Parameters.HInv.Equals(generatedKeyParameters.Parameters.HInv)) - { - return new SimpleTestResult(false, "Hinv does not match"); - } + if (!keyParameters.Parameters.HInv.Equals(generatedKeyParameters.Parameters.HInv)) + { + return new SimpleTestResult(false, "Hinv does not match"); + } - if (!keyParameters.Parameters.N.Equals(generatedKeyParameters.Parameters.N)) - { - return new SimpleTestResult(false, "N does not match"); - } + if (!keyParameters.Parameters.N.Equals(generatedKeyParameters.Parameters.N)) + { + return new SimpleTestResult(false, "N does not match"); + } - if (!Arrays.AreEqual(keyParameters.Parameters.GetSeed(), generatedKeyParameters.Parameters.GetSeed())) - { - return new SimpleTestResult(false, "Seed does not match"); - } + if (!Arrays.AreEqual(keyParameters.Parameters.GetSeed(), generatedKeyParameters.Parameters.GetSeed())) + { + return new SimpleTestResult(false, "Seed does not match"); } - return new SimpleTestResult(true, null); - + } + return new SimpleTestResult(true, null); } - private SimpleTestResult EncodeRecodePrivateKey() { - - DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid("Tc26-Gost-3410-12-512-paramSetA"); - ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); - ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512,null); - ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); - ECKeyPairGenerator engine = new ECKeyPairGenerator(); - engine.Init(parameters); - AsymmetricCipherKeyPair pair = engine.GenerateKeyPair(); - - ECPrivateKeyParameters generatedKeyParameters = (ECPrivateKeyParameters)pair.Private; - ECPrivateKeyParameters keyParameters = generatedKeyParameters; - + DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid("Tc26-Gost-3410-12-512-paramSetA"); + ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); + ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, null); + ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); + ECKeyPairGenerator engine = new ECKeyPairGenerator(); + engine.Init(parameters); + AsymmetricCipherKeyPair pair = engine.GenerateKeyPair(); - // - // Continuously encode/decode the key and check for loss of information. - // + ECPrivateKeyParameters generatedKeyParameters = (ECPrivateKeyParameters)pair.Private; + ECPrivateKeyParameters keyParameters = generatedKeyParameters; + // + // Continuously encode/decode the key and check for loss of information. + // + for (int t = 0; t < 3; t++) + { + PrivateKeyInfo info = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyParameters); + keyParameters = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(info); - for (int t = 0; t < 3; t++) { - PrivateKeyInfo info = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyParameters); - keyParameters = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(info); - - { // Specifically cast and test gost parameters. - ECGost3410Parameters gParam = (ECGost3410Parameters)generatedKeyParameters.Parameters; - ECGost3410Parameters rParam = (ECGost3410Parameters)keyParameters.Parameters; - - bool ok = SafeEquals(gParam.DigestParamSet, rParam.DigestParamSet) && - SafeEquals(gParam.EncryptionParamSet, rParam.EncryptionParamSet) && - SafeEquals(gParam.PublicKeyParamSet, rParam.PublicKeyParamSet); - - if (!ok) - { - return new SimpleTestResult(false, "GOST parameters does not match"); - } - - } - - if (keyParameters.IsPrivate != generatedKeyParameters.IsPrivate) - { - return new SimpleTestResult(false, "isPrivate does not match"); - } - - if (!keyParameters.D.Equals(generatedKeyParameters.D)) - { - return new SimpleTestResult(false, "D does not match"); - } - - if (!((ECGost3410Parameters)keyParameters.Parameters).Name.Equals( - ((ECGost3410Parameters)generatedKeyParameters.Parameters).Name)) - { - return new SimpleTestResult(false, "Name does not match"); - } - - if (!keyParameters.Parameters.Curve.Equals(generatedKeyParameters.Parameters.Curve)) - { - return new SimpleTestResult(false, "Curve does not match"); - } - - if (!Arrays.AreEqual( - keyParameters.Parameters.G.GetEncoded(true), - generatedKeyParameters.Parameters.G.GetEncoded(true))) - { - return new SimpleTestResult(false, "G does not match"); - } + // Specifically cast and test gost parameters. + ECGost3410Parameters gParam = (ECGost3410Parameters)generatedKeyParameters.Parameters; + ECGost3410Parameters rParam = (ECGost3410Parameters)keyParameters.Parameters; - if (!keyParameters.Parameters.H.Equals(generatedKeyParameters.Parameters.H)) - { - return new SimpleTestResult(false, "H does not match"); - } - - if (!keyParameters.Parameters.HInv.Equals(generatedKeyParameters.Parameters.HInv)) - { - return new SimpleTestResult(false, "Hinv does not match"); - } - - if (!keyParameters.Parameters.N.Equals(generatedKeyParameters.Parameters.N)) - { - return new SimpleTestResult(false, "N does not match"); - } + bool ok = SafeEquals(gParam.DigestParamSet, rParam.DigestParamSet) && + SafeEquals(gParam.EncryptionParamSet, rParam.EncryptionParamSet) && + SafeEquals(gParam.PublicKeyParamSet, rParam.PublicKeyParamSet); - if (!Arrays.AreEqual(keyParameters.Parameters.GetSeed(), generatedKeyParameters.Parameters.GetSeed())) + if (!ok) { - return new SimpleTestResult(false, "Seed does not match"); + return new SimpleTestResult(false, "GOST parameters does not match"); } } - - - return new SimpleTestResult(true, null); - } - - private SimpleTestResult DecodeJCEPublic() - { - byte[] pub256 = Hex.Decode("3068302106082a85030701010101301506092a850307010201010106082a850307010102020343000440292335c87d892510c35a033819a13e2b0dc606d911676af2bad8872d74a4b7bae6c729e98ace04c3dee626343f794731e1489edb7bc26f1c8c56e1448c96501a"); - - ECPublicKeyParameters pkInfo = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(pub256); - - if (pkInfo.IsPrivate) + if (keyParameters.IsPrivate != generatedKeyParameters.IsPrivate) { - return new SimpleTestResult(false, "isPrivate should be false"); + return new SimpleTestResult(false, "isPrivate does not match"); } - if ( - !Arrays.AreEqual( - pkInfo.Q.GetEncoded(true), - Hex.Decode("02bab7a4742d87d8baf26a6711d906c60d2b3ea11938035ac31025897dc8352329"))) + if (!keyParameters.D.Equals(generatedKeyParameters.D)) { - return new SimpleTestResult(false, "Q does not match"); + return new SimpleTestResult(false, "D does not match"); } - if (!((ECGost3410Parameters)pkInfo.Parameters).PublicKeyParamSet.ToString().Equals("1.2.643.7.1.2.1.1.1")) + if (!((ECGost3410Parameters)keyParameters.Parameters).Name.Equals( + ((ECGost3410Parameters)generatedKeyParameters.Parameters).Name)) { - return new SimpleTestResult(false, "PublicKeyParamSet does not match"); + return new SimpleTestResult(false, "Name does not match"); } - if (!((ECGost3410Parameters)pkInfo.Parameters).DigestParamSet.ToString().Equals("1.2.643.7.1.1.2.2")) + if (!keyParameters.Parameters.Curve.Equals(generatedKeyParameters.Parameters.Curve)) { - return new SimpleTestResult(false, "DigestParamSet does not match"); + return new SimpleTestResult(false, "Curve does not match"); } - if (((ECGost3410Parameters)pkInfo.Parameters).EncryptionParamSet != null) + if (!Arrays.AreEqual( + keyParameters.Parameters.G.GetEncoded(true), + generatedKeyParameters.Parameters.G.GetEncoded(true))) { - return new SimpleTestResult(false, "EncryptionParamSet is not null"); + return new SimpleTestResult(false, "G does not match"); } - - byte[] pub512 = Hex.Decode("3081aa302106082a85030701010102301506092a850307010201020106082a850307010102030381840004818043ccc22692ee8a1870c7c9de0566d7e3a494cf0e3c80f9e8852a3d1ec10d2a829d357253e0864aee2eaacd5e2d327578dee771f62f24decfd6358e06199efe540e7912db43c4c80fe0fd31f7f67a862f9d44fd0075cfee6e3d638c7520063d26311ef962547e8129fb8c5b194e129370cd30313884b4a60872254a10772fe595"); - - pkInfo = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(pub512); - - if (pkInfo.IsPrivate) + if (!keyParameters.Parameters.H.Equals(generatedKeyParameters.Parameters.H)) { - return new SimpleTestResult(false, "isPrivate should be true"); + return new SimpleTestResult(false, "H does not match"); } - if ( - !Arrays.AreEqual( - pkInfo.Q.GetEncoded(true), - Hex.Decode("0254fe9e19068e35d6cfde242ff671e7de7875322d5ecdaa2eee4a86e05372359d822a0dc11e3d2a85e8f9803c0ecf94a4e3d76605dec9c770188aee9226c2cc43"))) + if (!keyParameters.Parameters.HInv.Equals(generatedKeyParameters.Parameters.HInv)) { - return new SimpleTestResult(false, "Q does not match"); + return new SimpleTestResult(false, "Hinv does not match"); } - - if (!((ECGost3410Parameters)pkInfo.Parameters).PublicKeyParamSet.ToString().Equals("1.2.643.7.1.2.1.2.1")) + if (!keyParameters.Parameters.N.Equals(generatedKeyParameters.Parameters.N)) { - return new SimpleTestResult(false, "PublicKeyParamSet does not match"); + return new SimpleTestResult(false, "N does not match"); } - if (!((ECGost3410Parameters)pkInfo.Parameters).DigestParamSet.ToString().Equals("1.2.643.7.1.1.2.3")) + if (!Arrays.AreEqual(keyParameters.Parameters.GetSeed(), generatedKeyParameters.Parameters.GetSeed())) { - return new SimpleTestResult(false, "DigestParamSet does not match"); + return new SimpleTestResult(false, "Seed does not match"); } + } - if (((ECGost3410Parameters)pkInfo.Parameters).EncryptionParamSet != null) - { - return new SimpleTestResult(false, "EncryptionParamSet is not null"); - } + return new SimpleTestResult(true, null); + } + + private SimpleTestResult DecodeJCEPublic() + { + byte[] pub256 = Hex.Decode("3068302106082a85030701010101301506092a850307010201010106082a850307010102020343000440292335c87d892510c35a033819a13e2b0dc606d911676af2bad8872d74a4b7bae6c729e98ace04c3dee626343f794731e1489edb7bc26f1c8c56e1448c96501a"); + + ECPublicKeyParameters pkInfo = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(pub256); + if (pkInfo.IsPrivate) + { + return new SimpleTestResult(false, "isPrivate should be false"); + } + + if (!Arrays.AreEqual( + pkInfo.Q.GetEncoded(true), + Hex.Decode("02bab7a4742d87d8baf26a6711d906c60d2b3ea11938035ac31025897dc8352329"))) + { + return new SimpleTestResult(false, "Q does not match"); + } + + if (!((ECGost3410Parameters)pkInfo.Parameters).PublicKeyParamSet.ToString().Equals("1.2.643.7.1.2.1.1.1")) + { + return new SimpleTestResult(false, "PublicKeyParamSet does not match"); + } + + if (!((ECGost3410Parameters)pkInfo.Parameters).DigestParamSet.ToString().Equals("1.2.643.7.1.1.2.2")) + { + return new SimpleTestResult(false, "DigestParamSet does not match"); + } + + if (((ECGost3410Parameters)pkInfo.Parameters).EncryptionParamSet != null) + { + return new SimpleTestResult(false, "EncryptionParamSet is not null"); + } + + byte[] pub512 = Hex.Decode("3081aa302106082a85030701010102301506092a850307010201020106082a850307010102030381840004818043ccc22692ee8a1870c7c9de0566d7e3a494cf0e3c80f9e8852a3d1ec10d2a829d357253e0864aee2eaacd5e2d327578dee771f62f24decfd6358e06199efe540e7912db43c4c80fe0fd31f7f67a862f9d44fd0075cfee6e3d638c7520063d26311ef962547e8129fb8c5b194e129370cd30313884b4a60872254a10772fe595"); + + pkInfo = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(pub512); + if (pkInfo.IsPrivate) + { + return new SimpleTestResult(false, "isPrivate should be true"); + } - + if (!Arrays.AreEqual( + pkInfo.Q.GetEncoded(true), + Hex.Decode("0254fe9e19068e35d6cfde242ff671e7de7875322d5ecdaa2eee4a86e05372359d822a0dc11e3d2a85e8f9803c0ecf94a4e3d76605dec9c770188aee9226c2cc43"))) + { + return new SimpleTestResult(false, "Q does not match"); + } + + if (!((ECGost3410Parameters)pkInfo.Parameters).PublicKeyParamSet.ToString().Equals("1.2.643.7.1.2.1.2.1")) + { + return new SimpleTestResult(false, "PublicKeyParamSet does not match"); + } + + if (!((ECGost3410Parameters)pkInfo.Parameters).DigestParamSet.ToString().Equals("1.2.643.7.1.1.2.3")) + { + return new SimpleTestResult(false, "DigestParamSet does not match"); + } + + if (((ECGost3410Parameters)pkInfo.Parameters).EncryptionParamSet != null) + { + return new SimpleTestResult(false, "EncryptionParamSet is not null"); + } return new SimpleTestResult(true, null); } private SimpleTestResult DecodeJCEPrivate() { - byte[] priv256 = Hex.Decode("304a020100302106082a85030701010101301506092a850307010201010106082a8503070101020204220420fe75ba328d5439ed4859e6dc7e6ca2e9aab0818f094eddeb0d57d1c16a90762b"); - ECPrivateKeyParameters pkInfo = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(priv256); + byte[] priv256 = Hex.Decode("304a020100302106082a85030701010101301506092a850307010201010106082a8503070101020204220420fe75ba328d5439ed4859e6dc7e6ca2e9aab0818f094eddeb0d57d1c16a90762b"); + ECPrivateKeyParameters pkInfo = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(priv256); - if (!pkInfo.IsPrivate) - { - return new SimpleTestResult(false, "isPrivate should be true"); - } - - if ( - !Arrays.AreEqual( - Hex.Decode("2b76906ac1d1570debdd4e098f81b0aae9a26c7edce65948ed39548d32ba75fe"), - pkInfo.D.ToByteArray())) - { - return new SimpleTestResult(false, "D does not match"); - } + if (!pkInfo.IsPrivate) + { + return new SimpleTestResult(false, "isPrivate should be true"); + } - if (!((ECGost3410Parameters)pkInfo.Parameters).PublicKeyParamSet.ToString().Equals("1.2.643.7.1.2.1.1.1")) - { - return new SimpleTestResult(false, "PublicKeyParamSet does not match"); - } + if (!Arrays.AreEqual( + Hex.Decode("2b76906ac1d1570debdd4e098f81b0aae9a26c7edce65948ed39548d32ba75fe"), + pkInfo.D.ToByteArray())) + { + return new SimpleTestResult(false, "D does not match"); + } - if (!((ECGost3410Parameters)pkInfo.Parameters).DigestParamSet.ToString().Equals("1.2.643.7.1.1.2.2")) - { - return new SimpleTestResult(false, "DigestParamSet does not match"); - } + if (!((ECGost3410Parameters)pkInfo.Parameters).PublicKeyParamSet.ToString().Equals("1.2.643.7.1.2.1.1.1")) + { + return new SimpleTestResult(false, "PublicKeyParamSet does not match"); + } - if (((ECGost3410Parameters)pkInfo.Parameters).EncryptionParamSet != null) - { - return new SimpleTestResult(false, "EncryptionParamSet is not null"); - } + if (!((ECGost3410Parameters)pkInfo.Parameters).DigestParamSet.ToString().Equals("1.2.643.7.1.1.2.2")) + { + return new SimpleTestResult(false, "DigestParamSet does not match"); + } + if (((ECGost3410Parameters)pkInfo.Parameters).EncryptionParamSet != null) + { + return new SimpleTestResult(false, "EncryptionParamSet is not null"); + } - byte[] priv512 = Hex.Decode("306a020100302106082a85030701010102301506092a850307010201020106082a85030701010203044204402fc35576152f6e873236608b592b4b98d0793bf5184f8dc4a99512be703716991a96061ef46aceeae5319b5c69e6fcbfa7e339207878597ce50f9b7cbf857ff1"); - pkInfo = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(priv512); + byte[] priv512 = Hex.Decode("306a020100302106082a85030701010102301506092a850307010201020106082a85030701010203044204402fc35576152f6e873236608b592b4b98d0793bf5184f8dc4a99512be703716991a96061ef46aceeae5319b5c69e6fcbfa7e339207878597ce50f9b7cbf857ff1"); - if (!pkInfo.IsPrivate) - { - return new SimpleTestResult(false, "isPrivate should be true"); - } + pkInfo = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(priv512); - if ( - !Arrays.AreEqual( - Hex.Decode("00f17f85bf7c9b0fe57c5978782039e3a7bffce6695c9b31e5eace6af41e06961a99163770be1295a9c48d4f18f53b79d0984b2b598b603632876e2f157655c32f"), - pkInfo.D.ToByteArray())) - { - return new SimpleTestResult(false, "D does not match"); - } + if (!pkInfo.IsPrivate) + { + return new SimpleTestResult(false, "isPrivate should be true"); + } - if (!((ECGost3410Parameters)pkInfo.Parameters).PublicKeyParamSet.ToString().Equals("1.2.643.7.1.2.1.2.1")) - { - return new SimpleTestResult(false, "PublicKeyParamSet does not match"); - } + if (!Arrays.AreEqual( + Hex.Decode("00f17f85bf7c9b0fe57c5978782039e3a7bffce6695c9b31e5eace6af41e06961a99163770be1295a9c48d4f18f53b79d0984b2b598b603632876e2f157655c32f"), + pkInfo.D.ToByteArray())) + { + return new SimpleTestResult(false, "D does not match"); + } - if (!((ECGost3410Parameters)pkInfo.Parameters).DigestParamSet.ToString().Equals("1.2.643.7.1.1.2.3")) - { - return new SimpleTestResult(false, "DigestParamSet does not match"); - } + if (!((ECGost3410Parameters)pkInfo.Parameters).PublicKeyParamSet.ToString().Equals("1.2.643.7.1.2.1.2.1")) + { + return new SimpleTestResult(false, "PublicKeyParamSet does not match"); + } - if (((ECGost3410Parameters)pkInfo.Parameters).EncryptionParamSet != null) - { - return new SimpleTestResult(false, "EncryptionParamSet is not null"); - } + if (!((ECGost3410Parameters)pkInfo.Parameters).DigestParamSet.ToString().Equals("1.2.643.7.1.1.2.3")) + { + return new SimpleTestResult(false, "DigestParamSet does not match"); + } - + if (((ECGost3410Parameters)pkInfo.Parameters).EncryptionParamSet != null) + { + return new SimpleTestResult(false, "EncryptionParamSet is not null"); + } return new SimpleTestResult(true, null); } - - - public SimpleTestResult EncodeDecodePrivateLW(String oidStr, DerObjectIdentifier digest) + public SimpleTestResult EncodeDecodePrivateLW(string oidStr, DerObjectIdentifier digest) { DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid(oidStr); ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); @@ -369,15 +345,14 @@ namespace Org.BouncyCastle.Crypto.Tests engine.Init(parameters); AsymmetricCipherKeyPair pair = engine.GenerateKeyPair(); - - ECPrivateKeyParameters generatedKeyParameters = (ECPrivateKeyParameters) pair.Private; + ECPrivateKeyParameters generatedKeyParameters = (ECPrivateKeyParameters)pair.Private; PrivateKeyInfo info = PrivateKeyInfoFactory.CreatePrivateKeyInfo(generatedKeyParameters); ECPrivateKeyParameters recoveredKeyParameters = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(info); - - { // Specifically cast and test gost parameters. + { + // Specifically cast and test gost parameters. ECGost3410Parameters gParam = (ECGost3410Parameters)generatedKeyParameters.Parameters; ECGost3410Parameters rParam = (ECGost3410Parameters)recoveredKeyParameters.Parameters; @@ -389,10 +364,8 @@ namespace Org.BouncyCastle.Crypto.Tests { return new SimpleTestResult(false, "GOST parameters does not match"); } - } - if (recoveredKeyParameters.IsPrivate != generatedKeyParameters.IsPrivate) { return new SimpleTestResult(false, "isPrivate does not match"); @@ -446,10 +419,10 @@ namespace Org.BouncyCastle.Crypto.Tests } public SimpleTestResult EncodeDecodePublicLW(string oidStr, DerObjectIdentifier digest) - { + { DerObjectIdentifier oid = ECGost3410NamedCurves.GetOid(oidStr); ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); - ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp,oid,digest,null); + ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, digest, null); ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); ECKeyPairGenerator engine = new ECKeyPairGenerator(); engine.Init(parameters); @@ -460,35 +433,35 @@ namespace Org.BouncyCastle.Crypto.Tests ECPublicKeyParameters recoveredKeyParameters = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(info); - { // Specifically cast and test gost parameters. + { + // Specifically cast and test gost parameters. ECGost3410Parameters gParam = (ECGost3410Parameters)generatedKeyParameters.Parameters; ECGost3410Parameters rParam = (ECGost3410Parameters)recoveredKeyParameters.Parameters; - - bool ok = SafeEquals(gParam.DigestParamSet, rParam.DigestParamSet) && - SafeEquals(gParam.EncryptionParamSet, rParam.EncryptionParamSet) && - SafeEquals(gParam.PublicKeyParamSet, rParam.PublicKeyParamSet); + bool ok = SafeEquals(gParam.DigestParamSet, rParam.DigestParamSet) + && SafeEquals(gParam.EncryptionParamSet, rParam.EncryptionParamSet) + && SafeEquals(gParam.PublicKeyParamSet, rParam.PublicKeyParamSet); if (!ok) { return new SimpleTestResult(false, "GOST parameters does not match"); } - } if (!((ECGost3410Parameters)recoveredKeyParameters.Parameters).Name.Equals( - ((ECGost3410Parameters)generatedKeyParameters.Parameters).Name)) + ((ECGost3410Parameters)generatedKeyParameters.Parameters).Name)) { return new SimpleTestResult(false, "Name does not match"); } - if (recoveredKeyParameters.IsPrivate != generatedKeyParameters.IsPrivate) { return new SimpleTestResult(false, "isPrivate does not match"); } - if (!Arrays.AreEqual(recoveredKeyParameters.Q.GetEncoded(true), generatedKeyParameters.Q.GetEncoded(true))) + if (!Arrays.AreEqual( + recoveredKeyParameters.Q.GetEncoded(true), + generatedKeyParameters.Q.GetEncoded(true))) { return new SimpleTestResult(false, "Q does not match"); } @@ -525,13 +498,12 @@ namespace Org.BouncyCastle.Crypto.Tests return new SimpleTestResult(false, "Seed does not match"); } - return new SimpleTestResult(true, null); + return new SimpleTestResult(true, null); } [Test] public override void PerformTest() { - SimpleTestResult str = EncodeDecodePublicLW("Tc26-Gost-3410-12-512-paramSetA", RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512); if (!str.IsSuccessful()) { @@ -544,7 +516,6 @@ namespace Org.BouncyCastle.Crypto.Tests Fail(str.ToString(), str.GetException()); } - str = EncodeDecodePublicLW("Tc26-Gost-3410-12-256-paramSetA", RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256); if (!str.IsSuccessful()) { @@ -557,7 +528,6 @@ namespace Org.BouncyCastle.Crypto.Tests Fail(str.ToString(), str.GetException()); } - str = DecodeJCEPrivate(); if (!str.IsSuccessful()) { @@ -581,7 +551,6 @@ namespace Org.BouncyCastle.Crypto.Tests { Fail(str.ToString(), str.GetException()); } - } private bool SafeEquals(object left, object right) @@ -594,4 +563,4 @@ namespace Org.BouncyCastle.Crypto.Tests return left.Equals(right); } } -} \ No newline at end of file +} diff --git a/crypto/test/src/crypto/test/EGOST3410_2012SignatureTest.cs b/crypto/test/src/crypto/test/EGOST3410_2012SignatureTest.cs deleted file mode 100644 index 7c9801430..000000000 --- a/crypto/test/src/crypto/test/EGOST3410_2012SignatureTest.cs +++ /dev/null @@ -1,187 +0,0 @@ -using System; -using System.Security.Cryptography.X509Certificates; -using NUnit.Framework; -using Org.BouncyCastle.Asn1; -using Org.BouncyCastle.Asn1.CryptoPro; -using Org.BouncyCastle.Asn1.Rosstandart; -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.Security; -using Org.BouncyCastle.Utilities; -using Org.BouncyCastle.Utilities.Encoders; -using Org.BouncyCastle.Utilities.Test; -using Org.BouncyCastle.X509; - -namespace Org.BouncyCastle.Crypto.Tests -{ - [TestFixture] - public class EGOST3410_2012SignatureTest : SimpleTest - { - public override string Name { get; } - - [Test] - public override void PerformTest() - { - EcGOST34102012256Test(); - } - - - public void EcGOST34102012256Test() - { - BigInteger r = new BigInteger("29700980915817952874371204983938256990422752107994319651632687982059210933395"); - BigInteger s = new BigInteger("574973400270084654178925310019147038455227042649098563933718999175515839552"); - - BigInteger e = new BigInteger("20798893674476452017134061561508270130637142515379653289952617252661468872421"); - - byte[] kData = BigIntegers.AsUnsignedByteArray(new BigInteger("53854137677348463731403841147996619241504003434302020712960838528893196233395")); - SecureRandom k = new TestRandomBigInteger(kData); - - BigInteger mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564821041"); - BigInteger mod_q = new BigInteger("57896044618658097711785492504343953927082934583725450622380973592137631069619"); - - - ECCurve curve = new FpCurve( - mod_p, - new BigInteger("7"), // a - new BigInteger("43308876546767276905765904595650931995942111794451039583252968842033849580414"), // b - mod_q, BigInteger.One); - - ECDomainParameters spec = new ECDomainParameters(curve, - curve.CreatePoint( - new BigInteger("2"), // x - new BigInteger("4018974056539037503335449422937059775635739389905545080690979365213431566280")), // y - mod_q, BigInteger.One); - - ECPrivateKeyParameters privateKey = new ECPrivateKeyParameters( - new BigInteger("55441196065363246126355624130324183196576709222340016572108097750006097525544"), // d - spec); - - ECPublicKeyParameters publicKey = new ECPublicKeyParameters(curve.CreatePoint( - new BigInteger("57520216126176808443631405023338071176630104906313632182896741342206604859403"), // x - new BigInteger("17614944419213781543809391949654080031942662045363639260709847859438286763994")), // y - spec); - - ECGost3410_2012Signer signer = new ECGost3410_2012Signer(); - signer.Init(true, new ParametersWithRandom(privateKey, k)); - - byte[] rev = e.ToByteArray(); - byte[] message = new byte[rev.Length]; - for (int i = 0; i != rev.Length; i++) - { - message[i] = rev[rev.Length - 1 - i]; - } - BigInteger[] sig = signer.GenerateSignature(message); - - signer.Init(false, publicKey); - - if (!signer.VerifySignature(message, sig[0], sig[1])) - { - Fail("ECGOST3410 2012 verification failed"); - } - - if (!r.Equals(sig[0])) - { - Fail( - ": r component wrong." + Environment.NewLine - + " expecting: " + r + Environment.NewLine - + " got : " + sig[0]); - } - - if (!s.Equals(sig[1])) - { - Fail( - ": s component wrong." + Environment.NewLine - + " expecting: " + s + Environment.NewLine - + " got : " + sig[1]); - } - - - // 256Bit - { - DerObjectIdentifier oid = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA; - ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); - ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, - RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256, null); - ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); - ECKeyPairGenerator engine = new ECKeyPairGenerator(); - engine.Init(parameters); - AsymmetricCipherKeyPair pair = engine.GenerateKeyPair(); - SignatureGost12Test("ECGOST3410-2012-256", 64, pair); - } - - // 512Bit - - - { - DerObjectIdentifier oid = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512_paramSetA; - ECNamedDomainParameters ecp = new ECNamedDomainParameters(oid, ECGost3410NamedCurves.GetByOid(oid)); - ECGost3410Parameters gostParams = new ECGost3410Parameters(ecp, oid, - RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, null); - ECKeyGenerationParameters parameters = new ECKeyGenerationParameters(gostParams, new SecureRandom()); - ECKeyPairGenerator engine = new ECKeyPairGenerator(); - engine.Init(parameters); - AsymmetricCipherKeyPair pair = engine.GenerateKeyPair(); - - SignatureGost12Test("ECGOST3410-2012-512", 128, pair); - - } - } - - - - private void SignatureGost12Test(String signatureAlg, int expectedSignLen, AsymmetricCipherKeyPair p) - - { - byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; - - ECPrivateKeyParameters sKey = (ECPrivateKeyParameters)p.Private; - ECPublicKeyParameters vKey = (ECPublicKeyParameters)p.Public; - - ECGost3410_2012Signer s = new ECGost3410_2012Signer(); - - s.Init(true, sKey); - BigInteger[] sig = s.GenerateSignature(data); - - - s = new ECGost3410_2012Signer(); - s.Init(false, vKey); - - if (!s.VerifySignature(data, sig[0], sig[1])) - { - Fail("Signature " + signatureAlg + " did not verify"); - } - - // - // Test with Digest signer. - // - Gost3410DigestSigner digestSigner = new Gost3410DigestSigner( - new ECGost3410_2012Signer(), - DigestUtilities.GetDigest(((ECGost3410Parameters)vKey.Parameters).DigestParamSet)); - digestSigner.Init(true, sKey); - digestSigner.BlockUpdate(data, 0, data.Length); - byte[] sigBytes = digestSigner.GenerateSignature(); - - if (sigBytes.Length != expectedSignLen) - { - Fail(signatureAlg + " signature failed at expected length"); - } - - digestSigner = new Gost3410DigestSigner( - new ECGost3410_2012Signer(), - DigestUtilities.GetDigest(((ECGost3410Parameters)vKey.Parameters).DigestParamSet)); - digestSigner.Init(false, vKey); - digestSigner.BlockUpdate(data, 0, data.Length); - - if (!digestSigner.VerifySignature(sigBytes)) - { - Fail("Signature " + signatureAlg + " did not verify"); - } - } - - - } -} \ No newline at end of file diff --git a/crypto/test/src/crypto/test/RegressionTest.cs b/crypto/test/src/crypto/test/RegressionTest.cs index f8b5c3c79..9353ec969 100644 --- a/crypto/test/src/crypto/test/RegressionTest.cs +++ b/crypto/test/src/crypto/test/RegressionTest.cs @@ -23,6 +23,7 @@ namespace Org.BouncyCastle.Crypto.Tests new DeterministicDsaTest(), new Gost3410Test(), new ECGost3410Test(), + new ECGost3410_2012Test(), new EcIesTest(), new EcNrTest(), new MacTest(), diff --git a/crypto/test/src/util/test/FixedSecureRandom.cs b/crypto/test/src/util/test/FixedSecureRandom.cs index 1368aa231..682b3eefa 100644 --- a/crypto/test/src/util/test/FixedSecureRandom.cs +++ b/crypto/test/src/util/test/FixedSecureRandom.cs @@ -143,7 +143,7 @@ namespace Org.BouncyCastle.Utilities.Test bOut.Write(sources[i].data, 0, sources[i].data.Length); } } - catch (IOException e) + catch (IOException) { throw new ArgumentException("can't save value source."); } @@ -157,7 +157,7 @@ namespace Org.BouncyCastle.Utilities.Test { bOut.Write(sources[i].data, 0, sources[i].data.Length); } - catch (IOException e) + catch (IOException) { throw new ArgumentException("can't save value source."); } @@ -195,7 +195,7 @@ namespace Org.BouncyCastle.Utilities.Test bOut.Write(sources[i].data, 0, sources[i].data.Length); } } - catch (IOException e) + catch (IOException) { throw new ArgumentException("can't save value source."); } |