diff --git a/crypto/test/src/asn1/test/SignerLocationUnitTest.cs b/crypto/test/src/asn1/test/SignerLocationUnitTest.cs
index a0104dc81..e4666740f 100644
--- a/crypto/test/src/asn1/test/SignerLocationUnitTest.cs
+++ b/crypto/test/src/asn1/test/SignerLocationUnitTest.cs
@@ -2,8 +2,8 @@ using System;
using NUnit.Framework;
-using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Esf;
+using Org.BouncyCastle.Asn1.X500;
using Org.BouncyCastle.Utilities.Test;
namespace Org.BouncyCastle.Asn1.Tests
@@ -23,17 +23,17 @@ namespace Org.BouncyCastle.Asn1.Tests
SignerLocation sl = new SignerLocation(countryName, null, null);
- CheckConstruction(sl, countryName, null, null);
+ CheckConstruction(sl, DirectoryString.GetInstance(countryName), null, null);
DerUtf8String localityName = new DerUtf8String("Melbourne");
sl = new SignerLocation(null, localityName, null);
- CheckConstruction(sl, null, localityName, null);
+ CheckConstruction(sl, null, DirectoryString.GetInstance(localityName), null);
sl = new SignerLocation(countryName, localityName, null);
- CheckConstruction(sl, countryName, localityName, null);
+ CheckConstruction(sl, DirectoryString.GetInstance(countryName), DirectoryString.GetInstance(localityName), null);
Asn1Sequence postalAddress = new DerSequence(
new DerUtf8String("line 1"),
@@ -45,11 +45,11 @@ namespace Org.BouncyCastle.Asn1.Tests
sl = new SignerLocation(countryName, null, postalAddress);
- CheckConstruction(sl, countryName, null, postalAddress);
+ CheckConstruction(sl, DirectoryString.GetInstance(countryName), null, postalAddress);
sl = new SignerLocation(countryName, localityName, postalAddress);
- CheckConstruction(sl, countryName, localityName, postalAddress);
+ CheckConstruction(sl, DirectoryString.GetInstance(countryName), DirectoryString.GetInstance(localityName), postalAddress);
sl = SignerLocation.GetInstance(null);
@@ -117,9 +117,9 @@ namespace Org.BouncyCastle.Asn1.Tests
private void CheckConstruction(
SignerLocation sl,
- DerUtf8String countryName,
- DerUtf8String localityName,
- Asn1Sequence postalAddress)
+ DirectoryString countryName,
+ DirectoryString localityName,
+ Asn1Sequence postalAddress)
{
CheckValues(sl, countryName, localityName, postalAddress);
@@ -137,9 +137,9 @@ namespace Org.BouncyCastle.Asn1.Tests
private void CheckValues(
SignerLocation sl,
- DerUtf8String countryName,
- DerUtf8String localityName,
- Asn1Sequence postalAddress)
+ DirectoryString countryName,
+ DirectoryString localityName,
+ Asn1Sequence postalAddress)
{
if (countryName != null)
{
diff --git a/crypto/test/src/cms/test/SignedDataTest.cs b/crypto/test/src/cms/test/SignedDataTest.cs
index 89f7eea7d..29d0464c2 100644
--- a/crypto/test/src/cms/test/SignedDataTest.cs
+++ b/crypto/test/src/cms/test/SignedDataTest.cs
@@ -672,6 +672,24 @@ namespace Org.BouncyCastle.Cms.Tests
VerifySignatures(s, hash);
}
+ [Test]
+ public void TestRawSha256MissingNull()
+ {
+ byte[] document = GetInput("rawsha256nonull.p7m");
+
+ CmsSignedData s = new CmsSignedData(document);
+
+ IX509Store certStore = s.GetCertificates("Collection");
+ foreach (SignerInformation signerInformation in s.GetSignerInfos().GetSigners())
+ {
+ ICollection certCollection = certStore.GetMatches(signerInformation.SignerID);
+ foreach (X509Certificate cert in certCollection)
+ {
+ Assert.IsTrue(signerInformation.Verify(cert), "raw sig failed");
+ }
+ }
+ }
+
[Test]
public void TestSha1WithRsaEncapsulated()
{
diff --git a/crypto/test/src/crypto/test/DSTU7564Test.cs b/crypto/test/src/crypto/test/DSTU7564Test.cs
index 21a3a91da..c89962653 100644
--- a/crypto/test/src/crypto/test/DSTU7564Test.cs
+++ b/crypto/test/src/crypto/test/DSTU7564Test.cs
@@ -46,16 +46,16 @@ namespace Org.BouncyCastle.Crypto.Tests
public override void PerformTest()
{
base.PerformTest();
-
- hash256Tests();
- hash384Tests();
- hash512Tests();
- macTests();
- overflowTest();
- }
- private void overflowTest()
- {
+ hash256Tests();
+ hash384Tests();
+ hash512Tests();
+ macTests();
+ overflowTest();
+ }
+
+ private void overflowTest()
+ {
int macBitSize = 256;
byte[] input = new byte[1024];
for (int i = 0; i != input.Length; i++)
@@ -300,29 +300,28 @@ namespace Org.BouncyCastle.Crypto.Tests
}
}
- private void macTests()
- {
-
- //test1
- int macBitSize = 256;
- byte[] input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E");
- byte[] key = Hex.Decode("1F1E1D1C1B1A191817161514131211100F0E0D0C0B0A09080706050403020100");
+ private void macTests()
+ {
+ //test1
+ int macBitSize = 256;
+ byte[] input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E");
+ byte[] key = Hex.Decode("1F1E1D1C1B1A191817161514131211100F0E0D0C0B0A09080706050403020100");
- byte[] expectedMac = Hex.Decode("B60594D56FA79BA210314C72C2495087CCD0A99FC04ACFE2A39EF669925D98EE");
- byte[] mac = new byte[macBitSize / 8];
+ byte[] expectedMac = Hex.Decode("B60594D56FA79BA210314C72C2495087CCD0A99FC04ACFE2A39EF669925D98EE");
+ byte[] mac = new byte[macBitSize / 8];
- Dstu7564Mac dstu7564mac = new Dstu7564Mac(macBitSize);
+ Dstu7564Mac dstu7564mac = new Dstu7564Mac(macBitSize);
- dstu7564mac.Init(new KeyParameter(key));
- dstu7564mac.BlockUpdate(input, 0, input.Length);
- dstu7564mac.DoFinal(mac, 0);
+ dstu7564mac.Init(new KeyParameter(key));
+ dstu7564mac.BlockUpdate(input, 0, input.Length);
+ dstu7564mac.DoFinal(mac, 0);
- if (!Arrays.AreEqual(expectedMac, mac))
- {
- Fail("Failed mac test 1 - expected "
- + Hex.ToHexString(expectedMac)
- + " got " + Hex.ToHexString(mac));
- }
+ if (!Arrays.AreEqual(expectedMac, mac))
+ {
+ Fail("Failed mac test 1 - expected "
+ + Hex.ToHexString(expectedMac)
+ + " got " + Hex.ToHexString(mac));
+ }
//test1a
input = Hex.Decode("0001020304050607");
@@ -346,290 +345,276 @@ namespace Org.BouncyCastle.Crypto.Tests
//test 2
macBitSize = 384;
- input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E");
- key = Hex.Decode("2F2E2D2C2B2A292827262524232221201F1E1D1C1B1A191817161514131211100F0E0D0C0B0A09080706050403020100");
+ input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E");
+ key = Hex.Decode("2F2E2D2C2B2A292827262524232221201F1E1D1C1B1A191817161514131211100F0E0D0C0B0A09080706050403020100");
- expectedMac = Hex.Decode("BEBFD8D730336F043ABACB41829E79A4D320AEDDD8D14024D5B805DA70C396FA295C281A38B30AE728A304B3F5AE490E");
- mac = new byte[macBitSize / 8];
+ expectedMac = Hex.Decode("BEBFD8D730336F043ABACB41829E79A4D320AEDDD8D14024D5B805DA70C396FA295C281A38B30AE728A304B3F5AE490E");
+ mac = new byte[macBitSize / 8];
- dstu7564mac = new Dstu7564Mac(macBitSize);
+ dstu7564mac = new Dstu7564Mac(macBitSize);
- dstu7564mac.Init(new KeyParameter(key));
- dstu7564mac.BlockUpdate(input, 0, input.Length);
- dstu7564mac.DoFinal(mac, 0);
+ dstu7564mac.Init(new KeyParameter(key));
+ dstu7564mac.BlockUpdate(input, 0, input.Length);
+ dstu7564mac.DoFinal(mac, 0);
- if (!Arrays.AreEqual(expectedMac, mac))
- {
- Fail("Failed mac test 2 - expected "
- + Hex.ToHexString(expectedMac)
- + " got " + Hex.ToHexString(mac));
- }
+ if (!Arrays.AreEqual(expectedMac, mac))
+ {
+ Fail("Failed mac test 2 - expected "
+ + Hex.ToHexString(expectedMac)
+ + " got " + Hex.ToHexString(mac));
+ }
- //test 3
- macBitSize = 512;
- input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E");
- key = Hex.Decode("3F3E3D3C3B3A393837363534333231302F2E2D2C2B2A292827262524232221201F1E1D1C1B1A191817161514131211100F0E0D0C0B0A09080706050403020100");
+ //test 3
+ macBitSize = 512;
+ input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E");
+ key = Hex.Decode("3F3E3D3C3B3A393837363534333231302F2E2D2C2B2A292827262524232221201F1E1D1C1B1A191817161514131211100F0E0D0C0B0A09080706050403020100");
- expectedMac = Hex.Decode("F270043C06A5C37E65D9D791C5FBFB966E5EE709F8F54019C9A55B76CA40B70100579F269CEC24E347A9D864614CF3ABBF6610742E4DB3BD2ABC000387C49D24");
- mac = new byte[macBitSize / 8];
+ expectedMac = Hex.Decode("F270043C06A5C37E65D9D791C5FBFB966E5EE709F8F54019C9A55B76CA40B70100579F269CEC24E347A9D864614CF3ABBF6610742E4DB3BD2ABC000387C49D24");
+ mac = new byte[macBitSize / 8];
- dstu7564mac = new Dstu7564Mac(macBitSize);
+ dstu7564mac = new Dstu7564Mac(macBitSize);
- dstu7564mac.Init(new KeyParameter(key));
- dstu7564mac.BlockUpdate(input, 0, input.Length);
- dstu7564mac.DoFinal(mac, 0);
+ dstu7564mac.Init(new KeyParameter(key));
+ dstu7564mac.BlockUpdate(input, 0, input.Length);
+ dstu7564mac.DoFinal(mac, 0);
- if (!Arrays.AreEqual(expectedMac, mac))
- {
- Fail("Failed mac test 3 - expected "
- + Hex.ToHexString(expectedMac)
- + " got " + Hex.ToHexString(mac));
+ if (!Arrays.AreEqual(expectedMac, mac))
+ {
+ Fail("Failed mac test 3 - expected "
+ + Hex.ToHexString(expectedMac)
+ + " got " + Hex.ToHexString(mac));
+ }
}
- }
-
- private void hash512Tests()
- {
-
- int hashBitSize = 512;
-
- //test 1
- byte[] input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F");
- byte[] expectedHash = Hex.Decode("3813E2109118CDFB5A6D5E72F7208DCCC80A2DFB3AFDFB02F46992B5EDBE536B3560DD1D7E29C6F53978AF58B444E37BA685C0DD910533BA5D78EFFFC13DE62A");
- byte[] hash = new byte[hashBitSize / 8];
-
-
- Dstu7564Digest dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
- if (!Arrays.AreEqual(expectedHash, hash))
+ private void hash512Tests()
{
- Fail("Failed hash-512 test 1 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
- }
-
- //test 2
- input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F");
- expectedHash = Hex.Decode("76ED1AC28B1D0143013FFA87213B4090B356441263C13E03FA060A8CADA32B979635657F256B15D5FCA4A174DE029F0B1B4387C878FCC1C00E8705D783FD7FFE");
- hash = new byte[hashBitSize / 8];
+ int hashBitSize = 512;
+ //test 1
+ byte[] input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F");
+ byte[] expectedHash = Hex.Decode("3813E2109118CDFB5A6D5E72F7208DCCC80A2DFB3AFDFB02F46992B5EDBE536B3560DD1D7E29C6F53978AF58B444E37BA685C0DD910533BA5D78EFFFC13DE62A");
+ byte[] hash = new byte[hashBitSize / 8];
- dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
-
- if (!Arrays.AreEqual(expectedHash, hash))
- {
- Fail("Failed hash-512 test 2 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
- }
-
- //test 3
- input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF");
- expectedHash = Hex.Decode("0DD03D7350C409CB3C29C25893A0724F6B133FA8B9EB90A64D1A8FA93B56556611EB187D715A956B107E3BFC76482298133A9CE8CBC0BD5E1436A5B197284F7E");
- hash = new byte[hashBitSize / 8];
+ Dstu7564Digest dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-512 test 1 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
- dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
+ //test 2
+ input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F");
+ expectedHash = Hex.Decode("76ED1AC28B1D0143013FFA87213B4090B356441263C13E03FA060A8CADA32B979635657F256B15D5FCA4A174DE029F0B1B4387C878FCC1C00E8705D783FD7FFE");
+ hash = new byte[hashBitSize / 8];
- if (!Arrays.AreEqual(expectedHash, hash))
- {
- Fail("Failed hash-512 test 3 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
- }
+ dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- //test 4
- input = Hex.Decode("FF");
- expectedHash = Hex.Decode("871B18CF754B72740307A97B449ABEB32B64444CC0D5A4D65830AE5456837A72D8458F12C8F06C98C616ABE11897F86263B5CB77C420FB375374BEC52B6D0292");
- hash = new byte[hashBitSize / 8];
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-512 test 2 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
+ //test 3
+ input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF");
+ expectedHash = Hex.Decode("0DD03D7350C409CB3C29C25893A0724F6B133FA8B9EB90A64D1A8FA93B56556611EB187D715A956B107E3BFC76482298133A9CE8CBC0BD5E1436A5B197284F7E");
+ hash = new byte[hashBitSize / 8];
- dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
+ dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- if (!Arrays.AreEqual(expectedHash, hash))
- {
- Fail("Failed hash-512 test 4 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
- }
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-512 test 3 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
- //test 5
- input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF");
- expectedHash = Hex.Decode("B189BFE987F682F5F167F0D7FA565330E126B6E592B1C55D44299064EF95B1A57F3C2D0ECF17869D1D199EBBD02E8857FB8ADD67A8C31F56CD82C016CF743121");
- hash = new byte[hashBitSize / 8];
+ //test 4
+ input = Hex.Decode("FF");
+ expectedHash = Hex.Decode("871B18CF754B72740307A97B449ABEB32B64444CC0D5A4D65830AE5456837A72D8458F12C8F06C98C616ABE11897F86263B5CB77C420FB375374BEC52B6D0292");
+ hash = new byte[hashBitSize / 8];
+ dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-512 test 4 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
- if (!Arrays.AreEqual(expectedHash, hash))
- {
- Fail("Failed hash-512 test 5 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
- }
+ //test 5
+ input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF");
+ expectedHash = Hex.Decode("B189BFE987F682F5F167F0D7FA565330E126B6E592B1C55D44299064EF95B1A57F3C2D0ECF17869D1D199EBBD02E8857FB8ADD67A8C31F56CD82C016CF743121");
+ hash = new byte[hashBitSize / 8];
+ dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- //test 6
- input = Hex.Decode("");
- expectedHash = Hex.Decode("656B2F4CD71462388B64A37043EA55DBE445D452AECD46C3298343314EF04019BCFA3F04265A9857F91BE91FCE197096187CEDA78C9C1C021C294A0689198538");
- hash = new byte[hashBitSize / 8];
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-512 test 5 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
+ //test 6
+ input = Hex.Decode("");
+ expectedHash = Hex.Decode("656B2F4CD71462388B64A37043EA55DBE445D452AECD46C3298343314EF04019BCFA3F04265A9857F91BE91FCE197096187CEDA78C9C1C021C294A0689198538");
+ hash = new byte[hashBitSize / 8];
- dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
+ dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- if (!Arrays.AreEqual(expectedHash, hash))
- {
- Fail("Failed hash-512 test 6 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-512 test 6 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
}
- }
-
- private void hash384Tests()
- {
- int hashBitSize = 384;
-
- //test 1
- byte[] input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E");
- byte[] expectedHash = Hex.Decode("D9021692D84E5175735654846BA751E6D0ED0FAC36DFBC0841287DCB0B5584C75016C3DECC2A6E47C50B2F3811E351B8");
- byte[] hash = new byte[hashBitSize / 8];
-
-
- Dstu7564Digest dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
-
- if (!Arrays.AreEqual(expectedHash, hash))
+ private void hash384Tests()
{
- Fail("Failed hash-384 test 1 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
- }
- }
+ int hashBitSize = 384;
- private void hash256Tests()
- {
+ //test 1
+ byte[] input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E");
+ byte[] expectedHash = Hex.Decode("D9021692D84E5175735654846BA751E6D0ED0FAC36DFBC0841287DCB0B5584C75016C3DECC2A6E47C50B2F3811E351B8");
+ byte[] hash = new byte[hashBitSize / 8];
- int hashBitSize = 256;
+ Dstu7564Digest dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- //test 1
- byte[] input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F");
- byte[] expectedHash = Hex.Decode("08F4EE6F1BE6903B324C4E27990CB24EF69DD58DBE84813EE0A52F6631239875");
- byte[] hash = new byte[hashBitSize / 8];
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-384 test 1 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
+ }
+ private void hash256Tests()
+ {
+ int hashBitSize = 256;
- Dstu7564Digest dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
+ //test 1
+ byte[] input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F");
+ byte[] expectedHash = Hex.Decode("08F4EE6F1BE6903B324C4E27990CB24EF69DD58DBE84813EE0A52F6631239875");
+ byte[] hash = new byte[hashBitSize / 8];
- if (!Arrays.AreEqual(expectedHash, hash))
- {
- Fail("Failed hash-256 test 1 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
- }
+ Dstu7564Digest dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- //test 2
- input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F");
- expectedHash = Hex.Decode("0A9474E645A7D25E255E9E89FFF42EC7EB31349007059284F0B182E452BDA882");
- hash = new byte[hashBitSize / 8];
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-256 test 1 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
+ //test 2
+ input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F");
+ expectedHash = Hex.Decode("0A9474E645A7D25E255E9E89FFF42EC7EB31349007059284F0B182E452BDA882");
+ hash = new byte[hashBitSize / 8];
- dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
+ dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- if (!Arrays.AreEqual(expectedHash, hash))
- {
- Fail("Failed hash-256 test 2 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
- }
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-256 test 2 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
- //test 3
- input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF");
- expectedHash = Hex.Decode("D305A32B963D149DC765F68594505D4077024F836C1BF03806E1624CE176C08F");
- hash = new byte[hashBitSize / 8];
+ //test 3
+ input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF");
+ expectedHash = Hex.Decode("D305A32B963D149DC765F68594505D4077024F836C1BF03806E1624CE176C08F");
+ hash = new byte[hashBitSize / 8];
- dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
+ dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- if (!Arrays.AreEqual(expectedHash, hash))
- {
- Fail("Failed hash-256 test 3 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
- }
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-256 test 3 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
- //test 4
- input = Hex.Decode("FF");
- expectedHash = Hex.Decode("EA7677CA4526555680441C117982EA14059EA6D0D7124D6ECDB3DEEC49E890F4");
- hash = new byte[hashBitSize / 8];
+ //test 4
+ input = Hex.Decode("FF");
+ expectedHash = Hex.Decode("EA7677CA4526555680441C117982EA14059EA6D0D7124D6ECDB3DEEC49E890F4");
+ hash = new byte[hashBitSize / 8];
- dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
+ dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- if (!Arrays.AreEqual(expectedHash, hash))
- {
- Fail("Failed hash-256 test 4 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
- }
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-256 test 4 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
- //test 5
- input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E");
- expectedHash = Hex.Decode("1075C8B0CB910F116BDA5FA1F19C29CF8ECC75CAFF7208BA2994B68FC56E8D16");
- hash = new byte[hashBitSize / 8];
+ //test 5
+ input = Hex.Decode("000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E");
+ expectedHash = Hex.Decode("1075C8B0CB910F116BDA5FA1F19C29CF8ECC75CAFF7208BA2994B68FC56E8D16");
+ hash = new byte[hashBitSize / 8];
- dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
+ dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- if (!Arrays.AreEqual(expectedHash, hash))
- {
- Fail("Failed hash-256 test 5 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
- }
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-256 test 5 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
- //test 6
- input = Hex.Decode("");
- expectedHash = Hex.Decode("CD5101D1CCDF0D1D1F4ADA56E888CD724CA1A0838A3521E7131D4FB78D0F5EB6");
- hash = new byte[hashBitSize / 8];
+ //test 6
+ input = Hex.Decode("");
+ expectedHash = Hex.Decode("CD5101D1CCDF0D1D1F4ADA56E888CD724CA1A0838A3521E7131D4FB78D0F5EB6");
+ hash = new byte[hashBitSize / 8];
- dstu7564 = new Dstu7564Digest(hashBitSize);
- dstu7564.BlockUpdate(input, 0, input.Length);
- dstu7564.DoFinal(hash, 0);
+ dstu7564 = new Dstu7564Digest(hashBitSize);
+ dstu7564.BlockUpdate(input, 0, input.Length);
+ dstu7564.DoFinal(hash, 0);
- if (!Arrays.AreEqual(expectedHash, hash))
- {
- Fail("Failed hash-256 test 6 - expected "
- + Hex.ToHexString(expectedHash)
- + " got " + Hex.ToHexString(hash));
+ if (!Arrays.AreEqual(expectedHash, hash))
+ {
+ Fail("Failed hash-256 test 6 - expected "
+ + Hex.ToHexString(expectedHash)
+ + " got " + Hex.ToHexString(hash));
+ }
}
- }
- protected override IDigest CloneDigest(IDigest digest)
+ protected override IDigest CloneDigest(IDigest digest)
{
return new Dstu7564Digest((Dstu7564Digest)digest);
}
- public static void MainOld(
- string[] args)
+ public static void MainOld(string[] args)
{
RunTest(new Dstu7564Test());
}
diff --git a/crypto/test/src/crypto/test/RegressionTest.cs b/crypto/test/src/crypto/test/RegressionTest.cs
index 60c9d0f8f..4f505c752 100644
--- a/crypto/test/src/crypto/test/RegressionTest.cs
+++ b/crypto/test/src/crypto/test/RegressionTest.cs
@@ -128,6 +128,9 @@ namespace Org.BouncyCastle.Crypto.Tests
new X931SignerTest(),
new KeccakDigestTest(),
new ShakeDigestTest(),
+ new SM2EngineTest(),
+ new SM2KeyExchangeTest(),
+ new SM2SignerTest(),
};
public static void MainOld(string[] args)
diff --git a/crypto/test/src/crypto/test/SM2EngineTest.cs b/crypto/test/src/crypto/test/SM2EngineTest.cs
new file mode 100644
index 000000000..8a1987d52
--- /dev/null
+++ b/crypto/test/src/crypto/test/SM2EngineTest.cs
@@ -0,0 +1,167 @@
+using System;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Crypto.Engines;
+using Org.BouncyCastle.Crypto.Generators;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Math.EC;
+using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Encoders;
+using Org.BouncyCastle.Utilities.Test;
+
+namespace Org.BouncyCastle.Crypto.Tests
+{
+ [TestFixture]
+ public class SM2EngineTest
+ : SimpleTest
+ {
+ public override string Name
+ {
+ get { return "SM2Engine"; }
+ }
+
+ private void DoEngineTestFp()
+ {
+ BigInteger SM2_ECC_P = new BigInteger("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", 16);
+ BigInteger SM2_ECC_A = new BigInteger("787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", 16);
+ BigInteger SM2_ECC_B = new BigInteger("63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", 16);
+ BigInteger SM2_ECC_N = new BigInteger("8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", 16);
+ BigInteger SM2_ECC_GX = new BigInteger("421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", 16);
+ BigInteger SM2_ECC_GY = new BigInteger("0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", 16);
+
+ ECCurve curve = new FpCurve(SM2_ECC_P, SM2_ECC_A, SM2_ECC_B);
+
+ ECPoint g = curve.CreatePoint(SM2_ECC_GX, SM2_ECC_GY);
+ ECDomainParameters domainParams = new ECDomainParameters(curve, g, SM2_ECC_N);
+
+ ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
+
+ ECKeyGenerationParameters aKeyGenParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0", 16));
+
+ keyPairGenerator.Init(aKeyGenParams);
+
+ AsymmetricCipherKeyPair aKp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters aPub = (ECPublicKeyParameters)aKp.Public;
+ ECPrivateKeyParameters aPriv = (ECPrivateKeyParameters)aKp.Private;
+
+ SM2Engine sm2Engine = new SM2Engine();
+
+ byte[] m = Strings.ToByteArray("encryption standard");
+
+ sm2Engine.Init(true, new ParametersWithRandom(aPub, new TestRandomBigInteger("4C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F", 16)));
+
+ byte[] enc = sm2Engine.ProcessBlock(m, 0, m.Length);
+
+ IsTrue("enc wrong", Arrays.AreEqual(Hex.Decode(
+ "04245C26 FB68B1DD DDB12C4B 6BF9F2B6 D5FE60A3 83B0D18D 1C4144AB F17F6252" +
+ "E776CB92 64C2A7E8 8E52B199 03FDC473 78F605E3 6811F5C0 7423A24B 84400F01" +
+ "B8650053 A89B41C4 18B0C3AA D00D886C 00286467 9C3D7360 C30156FA B7C80A02" +
+ "76712DA9 D8094A63 4B766D3A 285E0748 0653426D"), enc));
+
+ sm2Engine.Init(false, aPriv);
+
+ byte[] dec = sm2Engine.ProcessBlock(enc, 0, enc.Length);
+
+ IsTrue("dec wrong", Arrays.AreEqual(m, dec));
+
+ enc[80] = (byte)(enc[80] + 1);
+
+ try
+ {
+ sm2Engine.ProcessBlock(enc, 0, enc.Length);
+ Fail("no exception");
+ }
+ catch (InvalidCipherTextException e)
+ {
+ IsTrue("wrong exception", "invalid cipher text".Equals(e.Message));
+ }
+
+ // long message
+ sm2Engine = new SM2Engine();
+
+ m = new byte[4097];
+ for (int i = 0; i != m.Length; i++)
+ {
+ m[i] = (byte)i;
+ }
+
+ sm2Engine.Init(true, new ParametersWithRandom(aPub, new TestRandomBigInteger("4C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F", 16)));
+
+ enc = sm2Engine.ProcessBlock(m, 0, m.Length);
+
+ sm2Engine.Init(false, aPriv);
+
+ dec = sm2Engine.ProcessBlock(enc, 0, enc.Length);
+
+ IsTrue("dec wrong", Arrays.AreEqual(m, dec));
+ }
+
+ private void DoEngineTestF2m()
+ {
+ BigInteger SM2_ECC_A = new BigInteger("00", 16);
+ BigInteger SM2_ECC_B = new BigInteger("E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B", 16);
+ BigInteger SM2_ECC_N = new BigInteger("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBC972CF7E6B6F900945B3C6A0CF6161D", 16);
+ BigInteger SM2_ECC_GX = new BigInteger("00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD", 16);
+ BigInteger SM2_ECC_GY = new BigInteger("013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E", 16);
+ BigInteger SM2_ECC_H = BigInteger.ValueOf(4);
+
+ ECCurve curve = new F2mCurve(257, 12, SM2_ECC_A, SM2_ECC_B);
+
+ ECPoint g = curve.CreatePoint(SM2_ECC_GX, SM2_ECC_GY);
+ ECDomainParameters domainParams = new ECDomainParameters(curve, g, SM2_ECC_N, SM2_ECC_H);
+
+ ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
+
+ ECKeyGenerationParameters aKeyGenParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("56A270D17377AA9A367CFA82E46FA5267713A9B91101D0777B07FCE018C757EB", 16));
+
+ keyPairGenerator.Init(aKeyGenParams);
+
+ AsymmetricCipherKeyPair aKp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters aPub = (ECPublicKeyParameters)aKp.Public;
+ ECPrivateKeyParameters aPriv = (ECPrivateKeyParameters)aKp.Private;
+
+ SM2Engine sm2Engine = new SM2Engine();
+
+ byte[] m = Strings.ToByteArray("encryption standard");
+
+ sm2Engine.Init(true, new ParametersWithRandom(aPub, new TestRandomBigInteger("6D3B497153E3E92524E5C122682DBDC8705062E20B917A5F8FCDB8EE4C66663D", 16)));
+
+ byte[] enc = sm2Engine.ProcessBlock(m, 0, m.Length);
+
+ IsTrue("f2m enc wrong", Arrays.AreEqual(Hex.Decode(
+ "04019D23 6DDB3050 09AD52C5 1BB93270 9BD534D4 76FBB7B0 DF9542A8 A4D890A3" +
+ "F2E100B2 3B938DC0 A94D1DF8 F42CF45D 2D6601BF 638C3D7D E75A29F0 2AFB7E45" +
+ "E91771FD 55AC6213 C2A8A040 E4CAB5B2 6A9CFCDA 737373A4 8625D375 8FA37B3E" +
+ "AB80E9CF CABA665E 3199EA15 A1FA8189 D96F5791 25E4"), enc));
+
+ sm2Engine.Init(false, aPriv);
+
+ byte[] dec = sm2Engine.ProcessBlock(enc, 0, enc.Length);
+
+ IsTrue("f2m dec wrong", Arrays.AreEqual(m, dec));
+ }
+
+ public override void PerformTest()
+ {
+ DoEngineTestFp();
+ DoEngineTestF2m();
+ }
+
+ public static void Main(string[] args)
+ {
+ RunTest(new SM2EngineTest());
+ }
+
+ [Test]
+ public void TestFunction()
+ {
+ string resultText = Perform().ToString();
+
+ Assert.AreEqual(Name + ": Okay", resultText);
+ }
+ }
+}
diff --git a/crypto/test/src/crypto/test/SM2KeyExchangeTest.cs b/crypto/test/src/crypto/test/SM2KeyExchangeTest.cs
new file mode 100644
index 000000000..d7a2650eb
--- /dev/null
+++ b/crypto/test/src/crypto/test/SM2KeyExchangeTest.cs
@@ -0,0 +1,229 @@
+using System;
+using System.IO;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Asn1;
+using Org.BouncyCastle.Crypto.Agreement;
+using Org.BouncyCastle.Crypto.Generators;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Math.EC;
+using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Encoders;
+using Org.BouncyCastle.Utilities.Test;
+
+namespace Org.BouncyCastle.Crypto.Tests
+{
+ [TestFixture]
+ public class SM2KeyExchangeTest
+ : SimpleTest
+ {
+ public override string Name
+ {
+ get { return "SM2KeyExchange"; }
+ }
+
+ private void DoKeyExchangeTestFp()
+ {
+ BigInteger SM2_ECC_P = new BigInteger("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", 16);
+ BigInteger SM2_ECC_A = new BigInteger("787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", 16);
+ BigInteger SM2_ECC_B = new BigInteger("63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", 16);
+ BigInteger SM2_ECC_N = new BigInteger("8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", 16);
+ BigInteger SM2_ECC_GX = new BigInteger("421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", 16);
+ BigInteger SM2_ECC_GY = new BigInteger("0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", 16);
+
+ ECCurve curve = new FpCurve(SM2_ECC_P, SM2_ECC_A, SM2_ECC_B);
+
+ ECPoint g = curve.CreatePoint(SM2_ECC_GX, SM2_ECC_GY);
+ ECDomainParameters domainParams = new ECDomainParameters(curve, g, SM2_ECC_N);
+
+ ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
+
+ ECKeyGenerationParameters aKeyGenParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("6FCBA2EF9AE0AB902BC3BDE3FF915D44BA4CC78F88E2F8E7F8996D3B8CCEEDEE", 16));
+
+ keyPairGenerator.Init(aKeyGenParams);
+
+ AsymmetricCipherKeyPair aKp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters aPub = (ECPublicKeyParameters)aKp.Public;
+ ECPrivateKeyParameters aPriv = (ECPrivateKeyParameters)aKp.Private;
+
+ ECKeyGenerationParameters aeKeyGenParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("83A2C9C8B96E5AF70BD480B472409A9A327257F1EBB73F5B073354B248668563", 16));
+
+ keyPairGenerator.Init(aeKeyGenParams);
+
+ AsymmetricCipherKeyPair aeKp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters aePub = (ECPublicKeyParameters)aeKp.Public;
+ ECPrivateKeyParameters aePriv = (ECPrivateKeyParameters)aeKp.Private;
+
+ ECKeyGenerationParameters bKeyGenParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("5E35D7D3F3C54DBAC72E61819E730B019A84208CA3A35E4C2E353DFCCB2A3B53", 16));
+
+ keyPairGenerator.Init(bKeyGenParams);
+
+ AsymmetricCipherKeyPair bKp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters bPub = (ECPublicKeyParameters)bKp.Public;
+ ECPrivateKeyParameters bPriv = (ECPrivateKeyParameters)bKp.Private;
+
+ ECKeyGenerationParameters beKeyGenParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("33FE21940342161C55619C4A0C060293D543C80AF19748CE176D83477DE71C80", 16));
+
+ keyPairGenerator.Init(beKeyGenParams);
+
+ AsymmetricCipherKeyPair beKp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters bePub = (ECPublicKeyParameters)beKp.Public;
+ ECPrivateKeyParameters bePriv = (ECPrivateKeyParameters)beKp.Private;
+
+ SM2KeyExchange exch = new SM2KeyExchange();
+
+ exch.Init(new ParametersWithID(new SM2KeyExchangePrivateParameters(true, aPriv, aePriv), Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ byte[] k1 = exch.CalculateKey(128, new ParametersWithID(new SM2KeyExchangePublicParameters(bPub, bePub), Strings.ToByteArray("BILL456@YAHOO.COM")));
+
+ IsTrue("key 1 wrong", Arrays.AreEqual(Hex.Decode("55b0ac62a6b927ba23703832c853ded4"), k1));
+
+ exch = new SM2KeyExchange();
+
+ exch.Init(new ParametersWithID(new SM2KeyExchangePrivateParameters(false, bPriv, bePriv), Strings.ToByteArray("BILL456@YAHOO.COM")));
+
+ byte[] k2 = exch.CalculateKey(128, new ParametersWithID(new SM2KeyExchangePublicParameters(aPub, aePub), Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ IsTrue("key 2 wrong", Arrays.AreEqual(Hex.Decode("55b0ac62a6b927ba23703832c853ded4"), k2));
+
+ // with key confirmation
+ exch = new SM2KeyExchange();
+
+ exch.Init(new ParametersWithID(new SM2KeyExchangePrivateParameters(false, bPriv, bePriv), Strings.ToByteArray("BILL456@YAHOO.COM")));
+
+ byte[][] vals2 = exch.CalculateKeyWithConfirmation(128, null, new ParametersWithID(new SM2KeyExchangePublicParameters(aPub, aePub), Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ IsTrue("key 2 wrong", Arrays.AreEqual(Hex.Decode("55b0ac62a6b927ba23703832c853ded4"), k2));
+
+ IsTrue("conf a tag 2 wrong", Arrays.AreEqual(Hex.Decode("284C8F198F141B502E81250F1581C7E9EEB4CA6990F9E02DF388B45471F5BC5C"), vals2[1]));
+ IsTrue("conf b tag 2 wrong", Arrays.AreEqual(Hex.Decode("23444DAF8ED7534366CB901C84B3BDBB63504F4065C1116C91A4C00697E6CF7A"), vals2[2]));
+
+ exch = new SM2KeyExchange();
+
+ exch.Init(new ParametersWithID(new SM2KeyExchangePrivateParameters(true, aPriv, aePriv), Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ byte[][] vals1 = exch.CalculateKeyWithConfirmation(128, vals2[1], new ParametersWithID(new SM2KeyExchangePublicParameters(bPub, bePub), Strings.ToByteArray("BILL456@YAHOO.COM")));
+
+ IsTrue("conf key 1 wrong", Arrays.AreEqual(Hex.Decode("55b0ac62a6b927ba23703832c853ded4"), vals1[0]));
+ IsTrue("conf tag 1 wrong", Arrays.AreEqual(Hex.Decode("23444DAF8ED7534366CB901C84B3BDBB63504F4065C1116C91A4C00697E6CF7A"), vals1[1]));
+ }
+
+ private void DoKeyExchangeTestF2m()
+ {
+ BigInteger SM2_ECC_A = new BigInteger("00", 16);
+ BigInteger SM2_ECC_B = new BigInteger("E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B", 16);
+ BigInteger SM2_ECC_N = new BigInteger("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBC972CF7E6B6F900945B3C6A0CF6161D", 16);
+ BigInteger SM2_ECC_GX = new BigInteger("00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD", 16);
+ BigInteger SM2_ECC_GY = new BigInteger("013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E", 16);
+ BigInteger SM2_ECC_H = BigInteger.ValueOf(4);
+
+ ECCurve curve = new F2mCurve(257, 12, SM2_ECC_A, SM2_ECC_B);
+
+ ECPoint g = curve.CreatePoint(SM2_ECC_GX, SM2_ECC_GY);
+ ECDomainParameters domainParams = new ECDomainParameters(curve, g, SM2_ECC_N, SM2_ECC_H);
+
+ ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
+
+ ECKeyGenerationParameters aKeyGenParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("4813903D254F2C20A94BC5704238496954BB5279F861952EF2C5298E84D2CEAA", 16));
+
+ keyPairGenerator.Init(aKeyGenParams);
+
+ AsymmetricCipherKeyPair aKp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters aPub = (ECPublicKeyParameters)aKp.Public;
+ ECPrivateKeyParameters aPriv = (ECPrivateKeyParameters)aKp.Private;
+
+ ECKeyGenerationParameters aeKeyGenParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("54A3D6673FF3A6BD6B02EBB164C2A3AF6D4A4906229D9BFCE68CC366A2E64BA4", 16));
+
+ keyPairGenerator.Init(aeKeyGenParams);
+
+ AsymmetricCipherKeyPair aeKp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters aePub = (ECPublicKeyParameters)aeKp.Public;
+ ECPrivateKeyParameters aePriv = (ECPrivateKeyParameters)aeKp.Private;
+
+ ECKeyGenerationParameters bKeyGenParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("08F41BAE0922F47C212803FE681AD52B9BF28A35E1CD0EC273A2CF813E8FD1DC", 16));
+
+ keyPairGenerator.Init(bKeyGenParams);
+
+ AsymmetricCipherKeyPair bKp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters bPub = (ECPublicKeyParameters)bKp.Public;
+ ECPrivateKeyParameters bPriv = (ECPrivateKeyParameters)bKp.Private;
+
+ ECKeyGenerationParameters beKeyGenParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("1F21933387BEF781D0A8F7FD708C5AE0A56EE3F423DBC2FE5BDF6F068C53F7AD", 16));
+
+ keyPairGenerator.Init(beKeyGenParams);
+
+ AsymmetricCipherKeyPair beKp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters bePub = (ECPublicKeyParameters)beKp.Public;
+ ECPrivateKeyParameters bePriv = (ECPrivateKeyParameters)beKp.Private;
+
+ SM2KeyExchange exch = new SM2KeyExchange();
+
+ exch.Init(new ParametersWithID(new SM2KeyExchangePrivateParameters(true, aPriv, aePriv), Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ byte[] k1 = exch.CalculateKey(128, new ParametersWithID(new SM2KeyExchangePublicParameters(bPub, bePub), Strings.ToByteArray("BILL456@YAHOO.COM")));
+
+ // there appears to be typo for ZA in the draft
+ //IsTrue("F2m key 1 wrong", Arrays.AreEqual(Hex.Decode("4E587E5C66634F22D973A7D98BF8BE23"), k1));
+ IsTrue("F2m key 1 wrong", Arrays.AreEqual(Hex.Decode("8c2b03289aa7126555dc660cfc29fd74"), k1));
+
+ exch = new SM2KeyExchange();
+
+ exch.Init(new ParametersWithID(new SM2KeyExchangePrivateParameters(false, bPriv, bePriv), Strings.ToByteArray("BILL456@YAHOO.COM")));
+
+ byte[] k2 = exch.CalculateKey(128, new ParametersWithID(new SM2KeyExchangePublicParameters(aPub, aePub), Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ //IsTrue("F2m key 2 wrong", Arrays.AreEqual(Hex.Decode("4E587E5C66634F22D973A7D98BF8BE23"), k2));
+ IsTrue("F2m key 2 wrong", Arrays.AreEqual(Hex.Decode("8c2b03289aa7126555dc660cfc29fd74"), k2));
+
+ // with key confirmation
+ exch = new SM2KeyExchange();
+
+ exch.Init(new ParametersWithID(new SM2KeyExchangePrivateParameters(false, bPriv, bePriv), Strings.ToByteArray("BILL456@YAHOO.COM")));
+
+ byte[][] vals2 = exch.CalculateKeyWithConfirmation(128, null, new ParametersWithID(new SM2KeyExchangePublicParameters(aPub, aePub), Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ IsTrue("key 2 wrong", Arrays.AreEqual(Hex.Decode("8c2b03289aa7126555dc660cfc29fd74"), k2));
+
+ IsTrue("conf a tag 2 wrong", Arrays.AreEqual(Hex.Decode("d8294c4c0f0ac180feac95e8a0d786638c9e915b9a684b2348809af03a0de2a5"), vals2[1]));
+ IsTrue("conf b tag 2 wrong", Arrays.AreEqual(Hex.Decode("52089e706911b58fd5e7c7b2ab5cf32bb61e481ef1e114a1e33d99eec84b5a4f"), vals2[2]));
+
+ exch = new SM2KeyExchange();
+
+ exch.Init(new ParametersWithID(new SM2KeyExchangePrivateParameters(true, aPriv, aePriv), Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ byte[][] vals1 = exch.CalculateKeyWithConfirmation(128, vals2[1], new ParametersWithID(new SM2KeyExchangePublicParameters(bPub, bePub), Strings.ToByteArray("BILL456@YAHOO.COM")));
+
+ IsTrue("conf key 1 wrong", Arrays.AreEqual(Hex.Decode("8c2b03289aa7126555dc660cfc29fd74"), vals1[0]));
+ IsTrue("conf tag 1 wrong", Arrays.AreEqual(Hex.Decode("52089e706911b58fd5e7c7b2ab5cf32bb61e481ef1e114a1e33d99eec84b5a4f"), vals1[1]));
+ }
+
+ public override void PerformTest()
+ {
+ DoKeyExchangeTestFp();
+ DoKeyExchangeTestF2m();
+ }
+
+ public static void Main(string[] args)
+ {
+ RunTest(new SM2KeyExchangeTest());
+ }
+
+ [Test]
+ public void TestFunction()
+ {
+ string resultText = Perform().ToString();
+
+ Assert.AreEqual(Name + ": Okay", resultText);
+ }
+ }
+}
diff --git a/crypto/test/src/crypto/test/SM2SignerTest.cs b/crypto/test/src/crypto/test/SM2SignerTest.cs
new file mode 100644
index 000000000..5904c95a7
--- /dev/null
+++ b/crypto/test/src/crypto/test/SM2SignerTest.cs
@@ -0,0 +1,193 @@
+using System;
+using System.IO;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Asn1;
+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.Utilities;
+using Org.BouncyCastle.Utilities.Test;
+
+namespace Org.BouncyCastle.Crypto.Tests
+{
+ [TestFixture]
+ public class SM2SignerTest
+ : SimpleTest
+ {
+ public override string Name
+ {
+ get { return "SM2Signer"; }
+ }
+
+ private void DoSignerTestFp()
+ {
+ BigInteger SM2_ECC_P = new BigInteger("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", 16);
+ BigInteger SM2_ECC_A = new BigInteger("787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", 16);
+ BigInteger SM2_ECC_B = new BigInteger("63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", 16);
+ BigInteger SM2_ECC_N = new BigInteger("8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", 16);
+ BigInteger SM2_ECC_GX = new BigInteger("421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", 16);
+ BigInteger SM2_ECC_GY = new BigInteger("0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", 16);
+
+ ECCurve curve = new FpCurve(SM2_ECC_P, SM2_ECC_A, SM2_ECC_B);
+
+ ECPoint g = curve.CreatePoint(SM2_ECC_GX, SM2_ECC_GY);
+ ECDomainParameters domainParams = new ECDomainParameters(curve, g, SM2_ECC_N);
+
+ ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263", 16));
+ ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
+
+ keyPairGenerator.Init(keyGenerationParams);
+ AsymmetricCipherKeyPair kp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters ecPub = (ECPublicKeyParameters)kp.Public;
+ ECPrivateKeyParameters ecPriv = (ECPrivateKeyParameters)kp.Private;
+
+ SM2Signer signer = new SM2Signer();
+
+ signer.Init(true,
+ new ParametersWithID(new ParametersWithRandom(ecPriv,
+ new TestRandomBigInteger("6CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F", 16)),
+ Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ byte[] msg = Strings.ToByteArray("message digest");
+
+ signer.BlockUpdate(msg, 0, msg.Length);
+
+ byte[] sig = signer.GenerateSignature();
+
+ BigInteger[] rs = Decode(sig);
+
+ IsTrue("r wrong", rs[0].Equals(new BigInteger("40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1", 16)));
+ IsTrue("s wrong", rs[1].Equals(new BigInteger("6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7", 16)));
+
+ signer.Init(false, new ParametersWithID(ecPub, Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ signer.BlockUpdate(msg, 0, msg.Length);
+
+ IsTrue("verification failed", signer.VerifySignature(sig));
+ }
+
+ private void DoSignerTestF2m()
+ {
+ BigInteger SM2_ECC_A = new BigInteger("00", 16);
+ BigInteger SM2_ECC_B = new BigInteger("E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B", 16);
+ BigInteger SM2_ECC_N = new BigInteger("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBC972CF7E6B6F900945B3C6A0CF6161D", 16);
+ BigInteger SM2_ECC_GX = new BigInteger("00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD", 16);
+ BigInteger SM2_ECC_GY = new BigInteger("013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E", 16);
+
+ ECCurve curve = new F2mCurve(257, 12, SM2_ECC_A, SM2_ECC_B);
+
+ ECPoint g = curve.CreatePoint(SM2_ECC_GX, SM2_ECC_GY);
+ ECDomainParameters domainParams = new ECDomainParameters(curve, g, SM2_ECC_N);
+
+ ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("771EF3DBFF5F1CDC32B9C572930476191998B2BF7CB981D7F5B39202645F0931", 16));
+ ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
+
+ keyPairGenerator.Init(keyGenerationParams);
+ AsymmetricCipherKeyPair kp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters ecPub = (ECPublicKeyParameters)kp.Public;
+ ECPrivateKeyParameters ecPriv = (ECPrivateKeyParameters)kp.Private;
+
+ SM2Signer signer = new SM2Signer();
+
+ signer.Init(true,
+ new ParametersWithID(new ParametersWithRandom(ecPriv,
+ new TestRandomBigInteger("36CD79FC8E24B7357A8A7B4A46D454C397703D6498158C605399B341ADA186D6", 16)),
+ Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ byte[] msg = Strings.ToByteArray("message digest");
+
+ signer.BlockUpdate(msg, 0, msg.Length);
+
+ byte[] sig = signer.GenerateSignature();
+
+ BigInteger[] rs = Decode(sig);
+
+ IsTrue("F2m r wrong", rs[0].Equals(new BigInteger("6D3FBA26EAB2A1054F5D198332E335817C8AC453ED26D3391CD4439D825BF25B", 16)));
+ IsTrue("F2m s wrong", rs[1].Equals(new BigInteger("3124C5688D95F0A10252A9BED033BEC84439DA384621B6D6FAD77F94B74A9556", 16)));
+
+ signer.Init(false, new ParametersWithID(ecPub, Strings.ToByteArray("ALICE123@YAHOO.COM")));
+
+ signer.BlockUpdate(msg, 0, msg.Length);
+
+ IsTrue("verification failed", signer.VerifySignature(sig));
+ }
+
+ private void DoVerifyBoundsCheck()
+ {
+ BigInteger SM2_ECC_A = new BigInteger("00", 16);
+ BigInteger SM2_ECC_B = new BigInteger("E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B", 16);
+ BigInteger SM2_ECC_N = new BigInteger("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBC972CF7E6B6F900945B3C6A0CF6161D", 16);
+ BigInteger SM2_ECC_GX = new BigInteger("00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD", 16);
+ BigInteger SM2_ECC_GY = new BigInteger("013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E", 16);
+
+ ECCurve curve = new F2mCurve(257, 12, SM2_ECC_A, SM2_ECC_B);
+
+ ECPoint g = curve.CreatePoint(SM2_ECC_GX, SM2_ECC_GY);
+ ECDomainParameters domainParams = new ECDomainParameters(curve, g, SM2_ECC_N);
+
+ ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(domainParams, new TestRandomBigInteger("771EF3DBFF5F1CDC32B9C572930476191998B2BF7CB981D7F5B39202645F0931", 16));
+ ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
+
+ keyPairGenerator.Init(keyGenerationParams);
+ AsymmetricCipherKeyPair kp = keyPairGenerator.GenerateKeyPair();
+
+ ECPublicKeyParameters ecPub = (ECPublicKeyParameters)kp.Public;
+
+ SM2Signer signer = new SM2Signer();
+
+ signer.Init(false, ecPub);
+
+ signer.BlockUpdate(new byte[20], 0, 20);
+ IsTrue(!signer.VerifySignature(Encode(BigInteger.Zero, BigInteger.ValueOf(8))));
+
+ signer.BlockUpdate(new byte[20], 0, 20);
+ IsTrue(!signer.VerifySignature(Encode(BigInteger.ValueOf(8), BigInteger.Zero)));
+
+ signer.BlockUpdate(new byte[20], 0, 20);
+ IsTrue(!signer.VerifySignature(Encode(SM2_ECC_N, BigInteger.ValueOf(8))));
+
+ signer.BlockUpdate(new byte[20], 0, 20);
+ IsTrue(!signer.VerifySignature(Encode(BigInteger.ValueOf(8), SM2_ECC_N)));
+ }
+
+ public override void PerformTest()
+ {
+ DoSignerTestFp();
+ DoSignerTestF2m();
+ DoVerifyBoundsCheck();
+ }
+
+ private static BigInteger[] Decode(byte[] sig)
+ {
+ Asn1Sequence s = Asn1Sequence.GetInstance(sig);
+
+ return new BigInteger[] { DerInteger.GetInstance(s[0]).Value,
+ DerInteger.GetInstance(s[1]).Value };
+ }
+
+ private static byte[] Encode(BigInteger r, BigInteger s)
+ {
+ return new DerSequence(new DerInteger(r), new DerInteger(s)).GetEncoded();
+ }
+
+ public static void Main(
+ string[] args)
+ {
+ RunTest(new SM2SignerTest());
+ }
+
+ [Test]
+ public void TestFunction()
+ {
+ string resultText = Perform().ToString();
+
+ Assert.AreEqual(Name + ": Okay", resultText);
+ }
+ }
+}
diff --git a/crypto/test/src/crypto/test/SM3DigestTest.cs b/crypto/test/src/crypto/test/SM3DigestTest.cs
index c8b8f2ec9..41aa2cd62 100644
--- a/crypto/test/src/crypto/test/SM3DigestTest.cs
+++ b/crypto/test/src/crypto/test/SM3DigestTest.cs
@@ -2,10 +2,8 @@ using System;
using NUnit.Framework;
-using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Utilities.Encoders;
-using Org.BouncyCastle.Utilities.Test;
namespace Org.BouncyCastle.Crypto.Tests
{
@@ -26,7 +24,109 @@ namespace Org.BouncyCastle.Crypto.Tests
"abcdefghijklmnopqrstuvwxyz",
};
- private static string[] digests = {
+ private static string[] hexMessages = {
+ /* 2 p.57 ZA */
+ "0090" +
+ "414C494345313233405941484F4F2E434F4D" +
+ "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498" +
+ "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A" +
+ "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D" +
+ "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2" +
+ "0AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A" +
+ "7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857",
+ /* 3 p.59 ZA */
+ "0090" +
+ "414C494345313233405941484F4F2E434F4D" +
+ "000000000000000000000000000000000000000000000000000000000000000000" +
+ "00E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B" +
+ "00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD" +
+ "013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E" +
+ "0165961645281A8626607B917F657D7E9382F1EA5CD931F40F6627F357542653B2" +
+ "01686522130D590FB8DE635D8FCA715CC6BF3D05BEF3F75DA5D543454448166612",
+ /* 4 p.72 ZA */
+ "0090" +
+ "414C494345313233405941484F4F2E434F4D" +
+ "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498" +
+ "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A" +
+ "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D" +
+ "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2" +
+ "3099093BF3C137D8FCBBCDF4A2AE50F3B0F216C3122D79425FE03A45DBFE1655" +
+ "3DF79E8DAC1CF0ECBAA2F2B49D51A4B387F2EFAF482339086A27A8E05BAED98B",
+ /* 5 p.72 ZB */
+ "0088" +
+ "42494C4C343536405941484F4F2E434F4D" +
+ "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498" +
+ "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A" +
+ "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D" +
+ "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2" +
+ "245493D446C38D8CC0F118374690E7DF633A8A4BFB3329B5ECE604B2B4F37F43" +
+ "53C0869F4B9E17773DE68FEC45E14904E0DEA45BF6CECF9918C85EA047C60A4C",
+ /* 6 p.75 ZA */
+ "0090" +
+ "414C494345313233405941484F4F2E434F4D" +
+ "000000000000000000000000000000000000000000000000000000000000000000" +
+ "00E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B" +
+ "00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD" +
+ "013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E" +
+ "008E3BDB2E11F9193388F1F901CCC857BF49CFC065FB38B9069CAAE6D5AFC3592F" +
+ "004555122AAC0075F42E0A8BBD2C0665C789120DF19D77B4E3EE4712F598040415",
+ /* 7 p.76 ZB */
+ "0088" +
+ "42494C4C343536405941484F4F2E434F4D" +
+ "000000000000000000000000000000000000000000000000000000000000000000" +
+ "00E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B" +
+ "00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD" +
+ "013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E" +
+ "0034297DD83AB14D5B393B6712F32B2F2E938D4690B095424B89DA880C52D4A7D9" +
+ "0199BBF11AC95A0EA34BBD00CA50B93EC24ACB68335D20BA5DCFE3B33BDBD2B62D",
+ /* 8 TopsecCA cert ZA */
+ "0080" +
+ "31323334353637383132333435363738" +
+ "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC" +
+ "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93" +
+ "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7" +
+ "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0" +
+ "D69C2F1EEC3BFB6B95B30C28085C77B125D77A9C39525D8190768F37D6B205B5" +
+ "89DCD316BBE7D89A9DC21917F17799E698531F5E6E3E10BD31370B259C3F81C3",
+ /* 9 */
+ "4D38D2958CA7FD2CFAE3AF04486959CF92C8EF48E8B83A05C112E739D5F181D0" +
+ "3082020CA003020102020900" +
+ "AF28725D98D33143300C06082A811CCF" +
+ "550183750500307D310B300906035504" +
+ "060C02636E310B300906035504080C02" +
+ "626A310B300906035504070C02626A31" +
+ "0F300D060355040A0C06746F70736563" +
+ "310F300D060355040B0C06746F707365" +
+ "633111300F06035504030C08546F7073" +
+ "65634341311F301D06092A864886F70D" +
+ "0109010C10626A40746F707365632E63" +
+ "6F6D2E636E301E170D31323036323430" +
+ "37353433395A170D3332303632303037" +
+ "353433395A307D310B30090603550406" +
+ "0C02636E310B300906035504080C0262" +
+ "6A310B300906035504070C02626A310F" +
+ "300D060355040A0C06746F7073656331" +
+ "0F300D060355040B0C06746F70736563" +
+ "3111300F06035504030C08546F707365" +
+ "634341311F301D06092A864886F70D01" +
+ "09010C10626A40746F707365632E636F" +
+ "6D2E636E3059301306072A8648CE3D02" +
+ "0106082A811CCF5501822D03420004D6" +
+ "9C2F1EEC3BFB6B95B30C28085C77B125" +
+ "D77A9C39525D8190768F37D6B205B589" +
+ "DCD316BBE7D89A9DC21917F17799E698" +
+ "531F5E6E3E10BD31370B259C3F81C3A3" +
+ "733071300F0603551D130101FF040530" +
+ "030101FF301D0603551D0E041604148E" +
+ "5D90347858BAAAD870D8BDFBA6A85E7B" +
+ "563B64301F0603551D23041830168014" +
+ "8E5D90347858BAAAD870D8BDFBA6A85E" +
+ "7B563B64300B0603551D0F0404030201" +
+ "06301106096086480186F84201010404" +
+ "03020057",
+ };
+
+ private static string[] digests = {
// Standard test vectors
"66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0",
"debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732",
@@ -34,7 +134,16 @@ namespace Org.BouncyCastle.Crypto.Tests
"1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b",
"623476ac18f65a2909e43c7fec61b49c7e764a91a18ccb82f1917a29c86c5e88",
"b80fe97a4da24afc277564f66a359ef440462ad28dcc6d63adb24d5c20a61595",
- };
+ // Additional vectors for GMSSL
+ "F4A38489E32B45B6F876E3AC2168CA392362DC8F23459C1D1146FC3DBFB7BC9A",
+ "26352AF82EC19F207BBC6F9474E11E90CE0F7DDACE03B27F801817E897A81FD5",
+ "E4D1D0C3CA4C7F11BC8FF8CB3F4C02A78F108FA098E51A668487240F75E20F31",
+ "6B4B6D0E276691BD4A11BF72F4FB501AE309FDACB72FA6CC336E6656119ABD67",
+ "329c2f6030cc7e0ca3af6c97b76243ca250338ad3d3dc3a8b322d1cfdf98c2b7", // original appears wrong -> "ECF0080215977B2E5D6D61B98A99442F03E8803DC39E349F8DCA5621A9ACDF2B",
+ "557BAD30E183559AEEC3B2256E1C7C11F870D22B165D015ACF9465B09B87B527",
+ "4D38D2958CA7FD2CFAE3AF04486959CF92C8EF48E8B83A05C112E739D5F181D0",
+ "C3B02E500A8B60B77DEDCF6F4C11BEF8D56E5CDE708C72065654FD7B2167915A",
+ };
private static string sixtyFourKdigest = "97049bdc8f0736bc7300eafa9980aeb9cf00f24f7ec3a8f1f8884954d7655c1d";
private static string million_a_digest = "c8aaf89429554029e231941a2acc0ad61ff2a5acd8fadd25847a3a732b3b02c3";
@@ -48,11 +157,35 @@ namespace Org.BouncyCastle.Crypto.Tests
{
base.PerformTest();
- sixtyFourKTest(sixtyFourKdigest);
+ SM3Digest dig = new SM3Digest();
+ byte[] resBuf = new byte[dig.GetDigestSize()];
+
+ VectorTest(dig, 10, resBuf, Hex.Decode(hexMessages[0]), Hex.Decode(digests[messages.Length]));
+ VectorTest(dig, 11, resBuf, Hex.Decode(hexMessages[1]), Hex.Decode(digests[messages.Length + 1]));
+ VectorTest(dig, 12, resBuf, Hex.Decode(hexMessages[2]), Hex.Decode(digests[messages.Length + 2]));
+ VectorTest(dig, 13, resBuf, Hex.Decode(hexMessages[3]), Hex.Decode(digests[messages.Length + 3]));
+ VectorTest(dig, 14, resBuf, Hex.Decode(hexMessages[4]), Hex.Decode(digests[messages.Length + 4]));
+ VectorTest(dig, 15, resBuf, Hex.Decode(hexMessages[5]), Hex.Decode(digests[messages.Length + 5]));
+ VectorTest(dig, 16, resBuf, Hex.Decode(hexMessages[6]), Hex.Decode(digests[messages.Length + 6]));
+ VectorTest(dig, 17, resBuf, Hex.Decode(hexMessages[7]), Hex.Decode(digests[messages.Length + 7]));
+
+ sixtyFourKTest(sixtyFourKdigest);
millionATest(million_a_digest);
}
- protected override IDigest CloneDigest(IDigest digest)
+ protected virtual void VectorTest(IDigest digest, int count,
+ byte[] resBuf, byte[] input, byte[] expected)
+ {
+ digest.BlockUpdate(input, 0, input.Length);
+ digest.DoFinal(resBuf, 0);
+
+ if (!AreEqual(resBuf, expected))
+ {
+ Fail("Vector " + count + " failed got " + Hex.ToHexString(resBuf));
+ }
+ }
+
+ protected override IDigest CloneDigest(IDigest digest)
{
return new SM3Digest((SM3Digest)digest);
}
diff --git a/crypto/test/src/openpgp/test/PGPSignatureTest.cs b/crypto/test/src/openpgp/test/PGPSignatureTest.cs
index 6aca68134..23220cad3 100644
--- a/crypto/test/src/openpgp/test/PGPSignatureTest.cs
+++ b/crypto/test/src/openpgp/test/PGPSignatureTest.cs
@@ -326,8 +326,49 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Tests
+ "6YD9JpfYZHEFmpYoS+qQ3tLfPCG3gaS/djBZWWkNt5z7e6sbRko49XEj3EUh"
+ "33HgjrOlL8uJNbhlZ5NeILcxHqGTHji+5wMEDBjfNT/C6m0=");
+ private void DoTestRemoveSignature()
+ {
+ byte[] testPubKeyRing =
+ Base64.Decode(
+ "mQGiBEAR8jYRBADNifuSopd20JOQ5x30ljIaY0M6927+vo09NeNxS3KqItba"
+ + "nz9o5e2aqdT0W1xgdHYZmdElOHTTsugZxdXTEhghyxoo3KhVcNnTABQyrrvX"
+ + "qouvmP2fEDEw0Vpyk+90BpyY9YlgeX/dEA8OfooRLCJde/iDTl7r9FT+mts8"
+ + "g3azjwCgx+pOLD9LPBF5E4FhUOdXISJ0f4EEAKXSOi9nZzajpdhe8W2ZL9gc"
+ + "BpzZi6AcrRZBHOEMqd69gtUxA4eD8xycUQ42yH89imEcwLz8XdJ98uHUxGJi"
+ + "qp6hq4oakmw8GQfiL7yQIFgaM0dOAI9Afe3m84cEYZsoAFYpB4/s9pVMpPRH"
+ + "NsVspU0qd3NHnSZ0QXs8L8DXGO1uBACjDUj+8GsfDCIP2QF3JC+nPUNa0Y5t"
+ + "wKPKl+T8hX/0FBD7fnNeC6c9j5Ir/Fp/QtdaDAOoBKiyNLh1JaB1NY6US5zc"
+ + "qFks2seZPjXEiE6OIDXYra494mjNKGUobA4hqT2peKWXt/uBcuL1mjKOy8Qf"
+ + "JxgEd0MOcGJO+1PFFZWGzLQ3RXJpYyBILiBFY2hpZG5hICh0ZXN0IGtleSBv"
+ + "bmx5KSA8ZXJpY0Bib3VuY3ljYXN0bGUub3JnPohZBBMRAgAZBQJAEfI2BAsH"
+ + "AwIDFQIDAxYCAQIeAQIXgAAKCRAOtk6iUOgnkDdnAKC/CfLWikSBdbngY6OK"
+ + "5UN3+o7q1ACcDRqjT3yjBU3WmRUNlxBg3tSuljmwAgAAuQENBEAR8jgQBAC2"
+ + "kr57iuOaV7Ga1xcU14MNbKcA0PVembRCjcVjei/3yVfT/fuCVtGHOmYLEBqH"
+ + "bn5aaJ0P/6vMbLCHKuN61NZlts+LEctfwoya43RtcubqMc7eKw4k0JnnoYgB"
+ + "ocLXOtloCb7jfubOsnfORvrUkK0+Ne6anRhFBYfaBmGU75cQgwADBQP/XxR2"
+ + "qGHiwn+0YiMioRDRiIAxp6UiC/JQIri2AKSqAi0zeAMdrRsBN7kyzYVVpWwN"
+ + "5u13gPdQ2HnJ7d4wLWAuizUdKIQxBG8VoCxkbipnwh2RR4xCXFDhJrJFQUm+"
+ + "4nKx9JvAmZTBIlI5Wsi5qxst/9p5MgP3flXsNi1tRbTmRhqIRgQYEQIABgUC"
+ + "QBHyOAAKCRAOtk6iUOgnkBStAJoCZBVM61B1LG2xip294MZecMtCwQCbBbsk"
+ + "JVCXP0/Szm05GB+WN+MOCT2wAgAA");
+
+ PgpObjectFactory pgpFact = new PgpObjectFactory(testPubKeyRing);
+
+ PgpPublicKeyRing pgpPub = (PgpPublicKeyRing)pgpFact.NextPgpObject();
+
+ foreach (PgpSignature sig in pgpPub.GetPublicKey().GetSignatures())
+ {
+ if (sig.SignatureType == PgpSignature.PositiveCertification)
+ {
+ PgpPublicKey.RemoveCertification(pgpPub.GetPublicKey(), sig);
+ }
+ }
+ }
+
public override void PerformTest()
{
+ DoTestRemoveSignature();
+
//
// RSA tests
//
diff --git a/crypto/test/src/test/CertTest.cs b/crypto/test/src/test/CertTest.cs
index 8632bac68..9a4965297 100644
--- a/crypto/test/src/test/CertTest.cs
+++ b/crypto/test/src/test/CertTest.cs
@@ -19,6 +19,7 @@ using Org.BouncyCastle.Security;
using Org.BouncyCastle.Security.Certificates;
using Org.BouncyCastle.Utilities.Collections;
using Org.BouncyCastle.Utilities.Encoders;
+using Org.BouncyCastle.Utilities.IO;
using Org.BouncyCastle.Utilities.Test;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.X509.Extension;
@@ -2426,6 +2427,19 @@ namespace Org.BouncyCastle.Tests
return new AsymmetricCipherKeyPair(pubKey, privKey);
}
+ private void rfc4491Test()
+ {
+ X509CertificateParser certFact = new X509CertificateParser();
+
+ X509Certificate x509 = certFact.ReadCertificate(new MemoryStream(gostRFC4491_94, false));
+
+ x509.Verify(x509.GetPublicKey());
+
+ x509 = (X509Certificate)certFact.ReadCertificate(new MemoryStream(gostRFC4491_2001, false));
+
+ x509.Verify(x509.GetPublicKey());
+ }
+
private void doTestNullDerNullCert()
{
AsymmetricCipherKeyPair keyPair = GenerateLongFixedKeys();
@@ -2469,6 +2483,71 @@ namespace Org.BouncyCastle.Tests
}
}
+ private void pemFileTest()
+ {
+ X509CertificateParser fact = new X509CertificateParser();
+
+ ICollection certs1 = fact.ReadCertificates(GetTestDataAsStream("cert_chain.data"));
+ IsTrue("certs wrong <cr><nl>", 2 == certs1.Count);
+
+ MemoryStream input = new MemoryStream(Streams.ReadAll(GetTestDataAsStream("cert_chain.data")), false);
+
+ ISet certs2 = new HashSet();
+ while (input.Position < input.Length)
+ {
+ X509Certificate c = fact.ReadCertificate(input);
+
+ // this isn't strictly correct with the way it's defined in the Java JavaDoc - need it for backward
+ // compatibility.
+ if (c != null)
+ {
+ certs2.Add(c);
+ }
+ }
+ IsTrue("certs size <cr><nl>", certs1.Count == certs2.Count);
+
+ certs2.RemoveAll(certs1);
+ IsTrue("collection not empty", certs2.Count == 0);
+ }
+
+ private void invalidCrls()
+ {
+ X509CrlParser crlParser = new X509CrlParser();
+
+ ICollection crls = crlParser.ReadCrls(GetTestDataAsStream("cert_chain.data"));
+ IsTrue("multi crl", crls.Count == 0);
+
+ X509Crl crl = crlParser.ReadCrl(GetTestDataAsStream("cert_chain.data"));
+ IsTrue("single crl", crl == null);
+ }
+
+ private void pemFileTestWithNl()
+ {
+ X509CertificateParser fact = new X509CertificateParser();
+
+ ICollection certs1 = fact.ReadCertificates(GetTestDataAsStream("cert_chain_nl.data"));
+ IsTrue("certs wrong <nl>", 2 == certs1.Count);
+
+ MemoryStream input = new MemoryStream(Streams.ReadAll(GetTestDataAsStream("cert_chain_nl.data")), false);
+
+ ISet certs2 = new HashSet();
+ while (input.Position < input.Length)
+ {
+ X509Certificate c = fact.ReadCertificate(input);
+
+ // this isn't strictly correct with the way it's defined in the Java JavaDoc - need it for backward
+ // compatibility.
+ if (c != null)
+ {
+ certs2.Add(c);
+ }
+ }
+ IsTrue("certs size <nl>", certs1.Count == certs2.Count);
+
+ certs2.RemoveAll(certs1);
+ IsTrue("collection not empty", certs2.Count == 0);
+ }
+
public override void PerformTest()
{
checkCertificate(1, cert1);
@@ -2518,7 +2597,12 @@ namespace Org.BouncyCastle.Tests
checkCrlCreation3();
pemTest();
+ pemFileTest();
+ pemFileTestWithNl();
pkcs7Test();
+ rfc4491Test();
+
+ invalidCrls();
doTestForgedSignature();
diff --git a/crypto/test/src/test/NamedCurveTest.cs b/crypto/test/src/test/NamedCurveTest.cs
index 96b246981..5b29a4152 100644
--- a/crypto/test/src/test/NamedCurveTest.cs
+++ b/crypto/test/src/test/NamedCurveTest.cs
@@ -240,18 +240,39 @@ namespace Org.BouncyCastle.Tests
// }
}
- public void doTestECGost(
- string name)
+ public void doTestECGost(string name)
{
-// ECGenParameterSpec ecSpec = new ECGenParameterSpec(name);
- ECDomainParameters ecSpec = GetCurveParameters(name);
+ ISigner sgr;
+ string keyAlgorithm;
- IAsymmetricCipherKeyPairGenerator g = GeneratorUtilities.GetKeyPairGenerator("ECGOST3410");
+ if (name.IndexOf("Tc26-Gost-3410") == 0)
+ {
+ // TODO Implement ECGOST3410-2012 in SignerUtilies/GeneratorUtilities etc.
+ // Current test cases don't work for GOST34.10 2012
+ return;
+
+ keyAlgorithm = "ECGOST3410-2012";
+ if (name.IndexOf("256") > 0)
+ {
+ sgr = SignerUtilities.GetSigner("ECGOST3410-2012-256");
+ }
+ else
+ {
+ sgr = SignerUtilities.GetSigner("ECGOST3410-2012-512");
+ }
+ }
+ else
+ {
+ keyAlgorithm = "ECGOST3410";
-// g.initialize(ecSpec, new SecureRandom());
+ sgr = SignerUtilities.GetSigner("ECGOST3410");
+ }
+
+ ECDomainParameters ecSpec = GetCurveParameters(name);
+
+ IAsymmetricCipherKeyPairGenerator g = GeneratorUtilities.GetKeyPairGenerator(keyAlgorithm);
g.Init(new ECKeyGenerationParameters(ecSpec, new SecureRandom()));
- ISigner sgr = SignerUtilities.GetSigner("ECGOST3410");
AsymmetricCipherKeyPair pair = g.GenerateKeyPair();
AsymmetricKeyParameter sKey = pair.Private;
AsymmetricKeyParameter vKey = pair.Public;
@@ -280,7 +301,7 @@ namespace Org.BouncyCastle.Tests
//// byte[] pubEnc = vKey.getEncoded();
// byte[] pubEnc = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(vKey).GetDerEncoded();
//
-//// KeyFactory keyFac = KeyFactory.getInstance("ECGOST3410");
+//// KeyFactory keyFac = KeyFactory.getInstance(keyAlgorithm);
//// X509EncodedKeySpec pubX509 = new X509EncodedKeySpec(pubEnc);
//// ECPublicKey pubKey = (ECPublicKey)keyFac.generatePublic(pubX509);
// ECPublicKeyParameters pubKey = (ECPublicKeyParameters) PublicKeyFactory.CreateKey(pubEnc);
diff --git a/crypto/test/src/util/test/FixedSecureRandom.cs b/crypto/test/src/util/test/FixedSecureRandom.cs
index d8598ac24..1368aa231 100644
--- a/crypto/test/src/util/test/FixedSecureRandom.cs
+++ b/crypto/test/src/util/test/FixedSecureRandom.cs
@@ -2,16 +2,90 @@ using System;
using System.IO;
using Org.BouncyCastle.Security;
+using Org.BouncyCastle.Utilities.Encoders;
+
+using M = Org.BouncyCastle.Math;
namespace Org.BouncyCastle.Utilities.Test
{
public class FixedSecureRandom
: SecureRandom
{
+ private static readonly M.BigInteger REGULAR = new M.BigInteger("01020304ffffffff0506070811111111", 16);
+ private static readonly M.BigInteger ANDROID = new M.BigInteger("1111111105060708ffffffff01020304", 16);
+ private static readonly M.BigInteger CLASSPATH = new M.BigInteger("3020104ffffffff05060708111111", 16);
+
+ private static readonly bool isAndroidStyle;
+ private static readonly bool isClasspathStyle;
+ private static readonly bool isRegularStyle;
+
+ static FixedSecureRandom()
+ {
+ M.BigInteger check1 = new M.BigInteger(128, new RandomChecker());
+ M.BigInteger check2 = new M.BigInteger(120, new RandomChecker());
+
+ isAndroidStyle = check1.Equals(ANDROID);
+ isRegularStyle = check1.Equals(REGULAR);
+ isClasspathStyle = check2.Equals(CLASSPATH);
+ }
+
private byte[] _data;
private int _index;
- protected FixedSecureRandom(
+ /**
+ * Base class for sources of fixed "Randomness"
+ */
+ public class Source
+ {
+ internal byte[] data;
+
+ internal Source(byte[] data)
+ {
+ this.data = data;
+ }
+ }
+
+ /**
+ * Data Source - in this case we just expect requests for byte arrays.
+ */
+ public class Data
+ : Source
+ {
+ public Data(byte[] data)
+ : base(data)
+ {
+ }
+ }
+
+ /**
+ * BigInteger Source - in this case we expect requests for data that will be used
+ * for BigIntegers. The FixedSecureRandom will attempt to compensate for platform differences here.
+ */
+ public class BigInteger
+ : Source
+ {
+ public BigInteger(byte[] data)
+ : base(data)
+ {
+ }
+
+ public BigInteger(int bitLength, byte[] data)
+ : base(ExpandToBitLength(bitLength, data))
+ {
+ }
+
+ public BigInteger(string hexData)
+ : this(Hex.Decode(hexData))
+ {
+ }
+
+ public BigInteger(int bitLength, string hexData)
+ : base(ExpandToBitLength(bitLength, Hex.Decode(hexData)))
+ {
+ }
+ }
+
+ protected FixedSecureRandom(
byte[] data)
{
_data = data;
@@ -38,6 +112,103 @@ namespace Org.BouncyCastle.Utilities.Test
return new FixedSecureRandom(bOut.ToArray());
}
+ public FixedSecureRandom(
+ Source[] sources)
+ {
+ MemoryStream bOut = new MemoryStream();
+
+ if (isRegularStyle)
+ {
+ if (isClasspathStyle)
+ {
+ for (int i = 0; i != sources.Length; i++)
+ {
+ try
+ {
+ if (sources[i] is BigInteger)
+ {
+ byte[] data = sources[i].data;
+ int len = data.Length - (data.Length % 4);
+ for (int w = data.Length - len - 1; w >= 0; w--)
+ {
+ bOut.WriteByte(data[w]);
+ }
+ for (int w = data.Length - len; w < data.Length; w += 4)
+ {
+ bOut.Write(data, w, 4);
+ }
+ }
+ else
+ {
+ bOut.Write(sources[i].data, 0, sources[i].data.Length);
+ }
+ }
+ catch (IOException e)
+ {
+ throw new ArgumentException("can't save value source.");
+ }
+ }
+ }
+ else
+ {
+ for (int i = 0; i != sources.Length; i++)
+ {
+ try
+ {
+ bOut.Write(sources[i].data, 0, sources[i].data.Length);
+ }
+ catch (IOException e)
+ {
+ throw new ArgumentException("can't save value source.");
+ }
+ }
+ }
+ }
+ else if (isAndroidStyle)
+ {
+ for (int i = 0; i != sources.Length; i++)
+ {
+ try
+ {
+ if (sources[i] is BigInteger)
+ {
+ byte[] data = sources[i].data;
+ int len = data.Length - (data.Length % 4);
+ for (int w = 0; w < len; w += 4)
+ {
+ bOut.Write(data, data.Length - (w + 4), 4);
+ }
+ if (data.Length - len != 0)
+ {
+ for (int w = 0; w != 4 - (data.Length - len); w++)
+ {
+ bOut.WriteByte(0);
+ }
+ }
+ for (int w = 0; w != data.Length - len; w++)
+ {
+ bOut.WriteByte(data[len + w]);
+ }
+ }
+ else
+ {
+ bOut.Write(sources[i].data, 0, sources[i].data.Length);
+ }
+ }
+ catch (IOException e)
+ {
+ throw new ArgumentException("can't save value source.");
+ }
+ }
+ }
+ else
+ {
+ throw new InvalidOperationException("Unrecognized BigInteger implementation");
+ }
+
+ _data = bOut.ToArray();
+ }
+
public override byte[] GenerateSeed(int numBytes)
{
return SecureRandom.GetNextBytes(this, numBytes);
@@ -65,5 +236,68 @@ namespace Org.BouncyCastle.Utilities.Test
{
get { return _index == _data.Length; }
}
+
+ private class RandomChecker
+ : SecureRandom
+ {
+ byte[] data = Hex.Decode("01020304ffffffff0506070811111111");
+ int index = 0;
+
+ public override void NextBytes(byte[] bytes)
+ {
+ Array.Copy(data, index, bytes, 0, bytes.Length);
+
+ index += bytes.Length;
+ }
+ }
+
+ private static byte[] ExpandToBitLength(int bitLength, byte[] v)
+ {
+ if ((bitLength + 7) / 8 > v.Length)
+ {
+ byte[] tmp = new byte[(bitLength + 7) / 8];
+
+ Array.Copy(v, 0, tmp, tmp.Length - v.Length, v.Length);
+ if (isAndroidStyle)
+ {
+ if (bitLength % 8 != 0)
+ {
+ uint i = BE_To_UInt32(tmp, 0);
+ UInt32_To_BE(i << (8 - (bitLength % 8)), tmp, 0);
+ }
+ }
+
+ return tmp;
+ }
+ else
+ {
+ if (isAndroidStyle && bitLength < (v.Length * 8))
+ {
+ if (bitLength % 8 != 0)
+ {
+ uint i = BE_To_UInt32(v, 0);
+ UInt32_To_BE(i << (8 - (bitLength % 8)), v, 0);
+ }
+ }
+ }
+
+ return v;
+ }
+
+ internal static uint BE_To_UInt32(byte[] bs, int off)
+ {
+ return (uint)bs[off] << 24
+ | (uint)bs[off + 1] << 16
+ | (uint)bs[off + 2] << 8
+ | (uint)bs[off + 3];
+ }
+
+ internal static void UInt32_To_BE(uint n, byte[] bs, int off)
+ {
+ bs[off] = (byte)(n >> 24);
+ bs[off + 1] = (byte)(n >> 16);
+ bs[off + 2] = (byte)(n >> 8);
+ bs[off + 3] = (byte)(n);
+ }
}
}
diff --git a/crypto/test/src/util/test/SimpleTest.cs b/crypto/test/src/util/test/SimpleTest.cs
index 1639431cc..45b8a6a06 100644
--- a/crypto/test/src/util/test/SimpleTest.cs
+++ b/crypto/test/src/util/test/SimpleTest.cs
@@ -27,6 +27,12 @@ namespace Org.BouncyCastle.Utilities.Test
throw new TestFailedException(SimpleTestResult.Failed(this, message));
}
+ internal void IsTrue(bool value)
+ {
+ if (!value)
+ throw new TestFailedException(SimpleTestResult.Failed(this, "no message"));
+ }
+
internal void IsTrue(string message, bool value)
{
if (!value)
diff --git a/crypto/test/src/util/test/TestRandomBigInteger.cs b/crypto/test/src/util/test/TestRandomBigInteger.cs
new file mode 100644
index 000000000..ef38293b9
--- /dev/null
+++ b/crypto/test/src/util/test/TestRandomBigInteger.cs
@@ -0,0 +1,55 @@
+using System;
+
+using M = Org.BouncyCastle.Math;
+
+namespace Org.BouncyCastle.Utilities.Test
+{
+ /**
+ * A fixed secure random designed to return data for someone needing to create a single BigInteger.
+ */
+ public class TestRandomBigInteger
+ : FixedSecureRandom
+ {
+ /**
+ * Constructor from a base 10 represention of a BigInteger.
+ *
+ * @param encoding a base 10 represention of a BigInteger.
+ */
+ public TestRandomBigInteger(string encoding)
+ : this(encoding, 10)
+ {
+ }
+
+ /**
+ * Constructor from a base radix represention of a BigInteger.
+ *
+ * @param encoding a String BigInteger of base radix.
+ * @param radix the radix to use.
+ */
+ public TestRandomBigInteger(string encoding, int radix)
+ : base(new FixedSecureRandom.Source[] { new FixedSecureRandom.BigInteger(BigIntegers.AsUnsignedByteArray(new M.BigInteger(encoding, radix))) })
+ {
+ }
+
+ /**
+ * Constructor based on a byte array.
+ *
+ * @param encoding a 2's complement representation of the BigInteger.
+ */
+ public TestRandomBigInteger(byte[] encoding)
+ : base(new FixedSecureRandom.Source[] { new FixedSecureRandom.BigInteger(encoding) })
+ {
+ }
+
+ /**
+ * Constructor which ensures encoding will produce a BigInteger from a request from the passed in bitLength.
+ *
+ * @param bitLength bit length for the BigInteger data request.
+ * @param encoding bytes making up the encoding.
+ */
+ public TestRandomBigInteger(int bitLength, byte[] encoding)
+ : base(new FixedSecureRandom.Source[] { new FixedSecureRandom.BigInteger(bitLength, encoding) })
+ {
+ }
+ }
+}
diff --git a/crypto/test/src/util/test/UncloseableStream.cs b/crypto/test/src/util/test/UncloseableStream.cs
index 2a3b4229b..0a7a16e66 100644
--- a/crypto/test/src/util/test/UncloseableStream.cs
+++ b/crypto/test/src/util/test/UncloseableStream.cs
@@ -5,6 +5,11 @@ using Org.BouncyCastle.Utilities.IO;
namespace Org.BouncyCastle.Utilities.Test
{
+ /// <summary>
+ /// This is a testing utility class to check the property that a <c>Stream</c> is never
+ /// closed in some particular context - typically when wrapped by another <c>Stream</c> that
+ /// should not be forwarding its <c>Stream.Close()</c> calls. Not needed in production code.
+ /// </summary>
public class UncloseableStream
: FilterStream
{
|