diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-02-04 16:09:25 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-02-04 16:09:25 +0700 |
commit | 1ca56848fe87a98eeff9db2c93186ac1d06051f1 (patch) | |
tree | 11fcfd2216545071ee4c8da8f2677f045b4a7942 /crypto/test | |
parent | Initial work on a fixed-point comb multiplier (diff) | |
download | BouncyCastle.NET-ed25519-1ca56848fe87a98eeff9db2c93186ac1d06051f1.tar.xz |
Add order/cofactor to Fp curves
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/src/crypto/test/ECGOST3410Test.cs | 30 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/ECIESTest.cs | 450 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/ECNRTest.cs | 199 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/ECTest.cs | 37 |
4 files changed, 374 insertions, 342 deletions
diff --git a/crypto/test/src/crypto/test/ECGOST3410Test.cs b/crypto/test/src/crypto/test/ECGOST3410Test.cs index adfc43c6b..37cb23ecf 100644 --- a/crypto/test/src/crypto/test/ECGOST3410Test.cs +++ b/crypto/test/src/crypto/test/ECGOST3410Test.cs @@ -43,18 +43,20 @@ namespace Org.BouncyCastle.Crypto.Tests private void ecGOST3410_TEST() { BigInteger mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564821041"); //p + BigInteger mod_q = new BigInteger("57896044618658097711785492504343953927082934583725450622380973592137631069619"); FpCurve curve = new FpCurve( mod_p, // p new BigInteger("7"), // a - new BigInteger("43308876546767276905765904595650931995942111794451039583252968842033849580414")); // b + new BigInteger("43308876546767276905765904595650931995942111794451039583252968842033849580414"), // b + mod_q, BigInteger.One); ECDomainParameters parameters = new ECDomainParameters( curve, curve.CreatePoint( new BigInteger("2"), // x new BigInteger("4018974056539037503335449422937059775635739389905545080690979365213431566280")), // y - new BigInteger("57896044618658097711785492504343953927082934583725450622380973592137631069619")); // n + mod_q); ECPrivateKeyParameters priKey = new ECPrivateKeyParameters( "ECGOST3410", @@ -112,18 +114,20 @@ namespace Org.BouncyCastle.Crypto.Tests SecureRandom random = new SecureRandom(); BigInteger mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564821041"); //p + BigInteger mod_q = new BigInteger("57896044618658097711785492504343953927082934583725450622380973592137631069619"); FpCurve curve = new FpCurve( mod_p, // p new BigInteger("7"), // a - new BigInteger("43308876546767276905765904595650931995942111794451039583252968842033849580414")); // b + new BigInteger("43308876546767276905765904595650931995942111794451039583252968842033849580414"), // b + mod_q, BigInteger.One); ECDomainParameters parameters = new ECDomainParameters( curve, curve.CreatePoint( new BigInteger("2"), // x new BigInteger("4018974056539037503335449422937059775635739389905545080690979365213431566280")), // y - new BigInteger("57896044618658097711785492504343953927082934583725450622380973592137631069619")); // q + mod_q); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters( @@ -167,18 +171,20 @@ namespace Org.BouncyCastle.Crypto.Tests SecureRandom random = new SecureRandom(); BigInteger mod_p = new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639319"); //p + BigInteger mod_q = new BigInteger("115792089237316195423570985008687907853073762908499243225378155805079068850323"); FpCurve curve = new FpCurve( mod_p, // p new BigInteger("115792089237316195423570985008687907853269984665640564039457584007913129639316"), // a - new BigInteger("166")); // b + new BigInteger("166"), // b + mod_q, BigInteger.One); ECDomainParameters parameters = new ECDomainParameters( curve, curve.CreatePoint( new BigInteger("1"), // x new BigInteger("64033881142927202683649881450433473985931760268884941288852745803908878638612")), // y - new BigInteger("115792089237316195423570985008687907853073762908499243225378155805079068850323")); // q + mod_q); ECKeyPairGenerator pGen = new ECKeyPairGenerator("ECGOST3410"); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters( @@ -215,18 +221,20 @@ namespace Org.BouncyCastle.Crypto.Tests SecureRandom random = new SecureRandom(); BigInteger mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564823193"); //p + BigInteger mod_q = new BigInteger("57896044618658097711785492504343953927102133160255826820068844496087732066703"); FpCurve curve = new FpCurve( mod_p, // p new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564823190"), // a - new BigInteger("28091019353058090096996979000309560759124368558014865957655842872397301267595")); // b + new BigInteger("28091019353058090096996979000309560759124368558014865957655842872397301267595"), // b + mod_q, BigInteger.One); ECDomainParameters parameters = new ECDomainParameters( curve, curve.CreatePoint( new BigInteger("1"), // x new BigInteger("28792665814854611296992347458380284135028636778229113005756334730996303888124")), // y - new BigInteger("57896044618658097711785492504343953927102133160255826820068844496087732066703")); // q + mod_q); ECKeyPairGenerator pGen = new ECKeyPairGenerator("ECGOST3410"); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters( @@ -263,18 +271,20 @@ namespace Org.BouncyCastle.Crypto.Tests SecureRandom random = new SecureRandom(); BigInteger mod_p = new BigInteger("70390085352083305199547718019018437841079516630045180471284346843705633502619"); //p + BigInteger mod_q = new BigInteger("70390085352083305199547718019018437840920882647164081035322601458352298396601"); FpCurve curve = new FpCurve( mod_p, // p new BigInteger("70390085352083305199547718019018437841079516630045180471284346843705633502616"), // a - new BigInteger("32858")); // b + new BigInteger("32858"), // b + mod_q, BigInteger.One); ECDomainParameters parameters = new ECDomainParameters( curve, curve.CreatePoint( new BigInteger("0"), // x new BigInteger("29818893917731240733471273240314769927240550812383695689146495261604565990247")), // y - new BigInteger("70390085352083305199547718019018437840920882647164081035322601458352298396601")); // q + mod_q); ECKeyPairGenerator pGen = new ECKeyPairGenerator("ECGOST3410"); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters( diff --git a/crypto/test/src/crypto/test/ECIESTest.cs b/crypto/test/src/crypto/test/ECIESTest.cs index 383750b14..e8cfd6df4 100644 --- a/crypto/test/src/crypto/test/ECIESTest.cs +++ b/crypto/test/src/crypto/test/ECIESTest.cs @@ -21,226 +21,232 @@ using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - /// <remarks>Test for ECIES - Elliptic Curve Integrated Encryption Scheme</remarks> - [TestFixture] - public class EcIesTest - : SimpleTest - { - public EcIesTest() - { - } - - public override string Name - { - get { return "ECIES"; } - } - - private void StaticTest() - { - FpCurve curve = new FpCurve( - new BigInteger("6277101735386680763835789423207666416083908700390324961279"), // q - new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), // a - new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16)); // b - - ECDomainParameters parameters = new ECDomainParameters( - curve, - curve.DecodePoint(Hex.Decode("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012")), // G - new BigInteger("6277101735386680763835789423176059013767194773182842284081")); // n - - ECPrivateKeyParameters priKey = new ECPrivateKeyParameters( - "ECDH", - new BigInteger("651056770906015076056810763456358567190100156695615665659"), // d - parameters); - - ECPublicKeyParameters pubKey = new ECPublicKeyParameters( - "ECDH", - curve.DecodePoint(Hex.Decode("0262b12d60690cdcf330babab6e69763b471f994dd702d16a5")), // Q - parameters); - - AsymmetricCipherKeyPair p1 = new AsymmetricCipherKeyPair(pubKey, priKey); - AsymmetricCipherKeyPair p2 = new AsymmetricCipherKeyPair(pubKey, priKey); - - // - // stream test - // - IesEngine i1 = new IesEngine( - new ECDHBasicAgreement(), - new Kdf2BytesGenerator(new Sha1Digest()), - new HMac(new Sha1Digest())); - IesEngine i2 = new IesEngine( - new ECDHBasicAgreement(), - new Kdf2BytesGenerator(new Sha1Digest()), - new HMac(new Sha1Digest())); - byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; - byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; - IesParameters p = new IesParameters(d, e, 64); - - i1.Init(true, p1.Private, p2.Public, p); - i2.Init(false, p2.Private, p1.Public, p); - - byte[] message = Hex.Decode("1234567890abcdef"); - - byte[] out1 = i1.ProcessBlock(message, 0, message.Length); - - if (!AreEqual(out1, Hex.Decode("468d89877e8238802403ec4cb6b329faeccfa6f3a730f2cdb3c0a8e8"))) - { - Fail("stream cipher test failed on enc"); - } - - byte[] out2 = i2.ProcessBlock(out1, 0, out1.Length); - - if (!AreEqual(out2, message)) - { - Fail("stream cipher test failed"); - } - - // - // twofish with CBC - // - BufferedBlockCipher c1 = new PaddedBufferedBlockCipher( - new CbcBlockCipher(new TwofishEngine())); - BufferedBlockCipher c2 = new PaddedBufferedBlockCipher( - new CbcBlockCipher(new TwofishEngine())); - i1 = new IesEngine( - new ECDHBasicAgreement(), - new Kdf2BytesGenerator(new Sha1Digest()), - new HMac(new Sha1Digest()), - c1); - i2 = new IesEngine( - new ECDHBasicAgreement(), - new Kdf2BytesGenerator(new Sha1Digest()), - new HMac(new Sha1Digest()), - c2); - d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; - e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; - p = new IesWithCipherParameters(d, e, 64, 128); - - i1.Init(true, p1.Private, p2.Public, p); - i2.Init(false, p2.Private, p1.Public, p); - - message = Hex.Decode("1234567890abcdef"); - - out1 = i1.ProcessBlock(message, 0, message.Length); - - if (!AreEqual(out1, Hex.Decode("b8a06ea5c2b9df28b58a0a90a734cde8c9c02903e5c220021fe4417410d1e53a32a71696"))) - { - Fail("twofish cipher test failed on enc"); - } - - out2 = i2.ProcessBlock(out1, 0, out1.Length); - - if (!AreEqual(out2, message)) - { - Fail("twofish cipher test failed"); - } - } - - private void DoTest( - AsymmetricCipherKeyPair p1, - AsymmetricCipherKeyPair p2) - { - // - // stream test - // - IesEngine i1 = new IesEngine( - new ECDHBasicAgreement(), - new Kdf2BytesGenerator(new Sha1Digest()), - new HMac(new Sha1Digest())); - IesEngine i2 = new IesEngine( - new ECDHBasicAgreement(), - new Kdf2BytesGenerator(new Sha1Digest()), - new HMac(new Sha1Digest())); - byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; - byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; - IesParameters p = new IesParameters(d, e, 64); - - i1.Init(true, p1.Private, p2.Public, p); - i2.Init(false, p2.Private, p1.Public, p); - - byte[] message = Hex.Decode("1234567890abcdef"); - - byte[] out1 = i1.ProcessBlock(message, 0, message.Length); - - byte[] out2 = i2.ProcessBlock(out1, 0, out1.Length); - - if (!AreEqual(out2, message)) - { - Fail("stream cipher test failed"); - } - - // - // twofish with CBC - // - BufferedBlockCipher c1 = new PaddedBufferedBlockCipher( - new CbcBlockCipher(new TwofishEngine())); - BufferedBlockCipher c2 = new PaddedBufferedBlockCipher( - new CbcBlockCipher(new TwofishEngine())); - i1 = new IesEngine( - new ECDHBasicAgreement(), - new Kdf2BytesGenerator(new Sha1Digest()), - new HMac(new Sha1Digest()), - c1); - i2 = new IesEngine( - new ECDHBasicAgreement(), - new Kdf2BytesGenerator(new Sha1Digest()), - new HMac(new Sha1Digest()), - c2); - d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; - e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; - p = new IesWithCipherParameters(d, e, 64, 128); - - i1.Init(true, p1.Private, p2.Public, p); - i2.Init(false, p2.Private, p1.Public, p); - - message = Hex.Decode("1234567890abcdef"); - - out1 = i1.ProcessBlock(message, 0, message.Length); - - out2 = i2.ProcessBlock(out1, 0, out1.Length); - - if (!AreEqual(out2, message)) - { - Fail("twofish cipher test failed"); - } - } - - public override void PerformTest() - { - StaticTest(); - - FpCurve curve = new FpCurve( - new BigInteger("6277101735386680763835789423207666416083908700390324961279"), // q - new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), // a - new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16)); // b - - ECDomainParameters parameters = new ECDomainParameters( - curve, - curve.DecodePoint(Hex.Decode("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012")), // G - new BigInteger("6277101735386680763835789423176059013767194773182842284081")); // n - - ECKeyPairGenerator eGen = new ECKeyPairGenerator(); - KeyGenerationParameters gParam = new ECKeyGenerationParameters(parameters, new SecureRandom()); - - eGen.Init(gParam); - - AsymmetricCipherKeyPair p1 = eGen.GenerateKeyPair(); - AsymmetricCipherKeyPair p2 = eGen.GenerateKeyPair(); - - DoTest(p1, p2); - } - - public static void Main( - string[] args) - { - RunTest(new EcIesTest()); - } - - [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); - - Assert.AreEqual(Name + ": Okay", resultText); - } - } + /// <remarks>Test for ECIES - Elliptic Curve Integrated Encryption Scheme</remarks> + [TestFixture] + public class EcIesTest + : SimpleTest + { + public EcIesTest() + { + } + + public override string Name + { + get { return "ECIES"; } + } + + private void StaticTest() + { + BigInteger n = new BigInteger("6277101735386680763835789423176059013767194773182842284081"); + + FpCurve curve = new FpCurve( + new BigInteger("6277101735386680763835789423207666416083908700390324961279"), // q + new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), // a + new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16), // b + n, BigInteger.One); + + ECDomainParameters parameters = new ECDomainParameters( + curve, + curve.DecodePoint(Hex.Decode("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012")), // G + n); + + ECPrivateKeyParameters priKey = new ECPrivateKeyParameters( + "ECDH", + new BigInteger("651056770906015076056810763456358567190100156695615665659"), // d + parameters); + + ECPublicKeyParameters pubKey = new ECPublicKeyParameters( + "ECDH", + curve.DecodePoint(Hex.Decode("0262b12d60690cdcf330babab6e69763b471f994dd702d16a5")), // Q + parameters); + + AsymmetricCipherKeyPair p1 = new AsymmetricCipherKeyPair(pubKey, priKey); + AsymmetricCipherKeyPair p2 = new AsymmetricCipherKeyPair(pubKey, priKey); + + // + // stream test + // + IesEngine i1 = new IesEngine( + new ECDHBasicAgreement(), + new Kdf2BytesGenerator(new Sha1Digest()), + new HMac(new Sha1Digest())); + IesEngine i2 = new IesEngine( + new ECDHBasicAgreement(), + new Kdf2BytesGenerator(new Sha1Digest()), + new HMac(new Sha1Digest())); + byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; + byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; + IesParameters p = new IesParameters(d, e, 64); + + i1.Init(true, p1.Private, p2.Public, p); + i2.Init(false, p2.Private, p1.Public, p); + + byte[] message = Hex.Decode("1234567890abcdef"); + + byte[] out1 = i1.ProcessBlock(message, 0, message.Length); + + if (!AreEqual(out1, Hex.Decode("468d89877e8238802403ec4cb6b329faeccfa6f3a730f2cdb3c0a8e8"))) + { + Fail("stream cipher test failed on enc"); + } + + byte[] out2 = i2.ProcessBlock(out1, 0, out1.Length); + + if (!AreEqual(out2, message)) + { + Fail("stream cipher test failed"); + } + + // + // twofish with CBC + // + BufferedBlockCipher c1 = new PaddedBufferedBlockCipher( + new CbcBlockCipher(new TwofishEngine())); + BufferedBlockCipher c2 = new PaddedBufferedBlockCipher( + new CbcBlockCipher(new TwofishEngine())); + i1 = new IesEngine( + new ECDHBasicAgreement(), + new Kdf2BytesGenerator(new Sha1Digest()), + new HMac(new Sha1Digest()), + c1); + i2 = new IesEngine( + new ECDHBasicAgreement(), + new Kdf2BytesGenerator(new Sha1Digest()), + new HMac(new Sha1Digest()), + c2); + d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; + e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; + p = new IesWithCipherParameters(d, e, 64, 128); + + i1.Init(true, p1.Private, p2.Public, p); + i2.Init(false, p2.Private, p1.Public, p); + + message = Hex.Decode("1234567890abcdef"); + + out1 = i1.ProcessBlock(message, 0, message.Length); + + if (!AreEqual(out1, Hex.Decode("b8a06ea5c2b9df28b58a0a90a734cde8c9c02903e5c220021fe4417410d1e53a32a71696"))) + { + Fail("twofish cipher test failed on enc"); + } + + out2 = i2.ProcessBlock(out1, 0, out1.Length); + + if (!AreEqual(out2, message)) + { + Fail("twofish cipher test failed"); + } + } + + private void DoTest( + AsymmetricCipherKeyPair p1, + AsymmetricCipherKeyPair p2) + { + // + // stream test + // + IesEngine i1 = new IesEngine( + new ECDHBasicAgreement(), + new Kdf2BytesGenerator(new Sha1Digest()), + new HMac(new Sha1Digest())); + IesEngine i2 = new IesEngine( + new ECDHBasicAgreement(), + new Kdf2BytesGenerator(new Sha1Digest()), + new HMac(new Sha1Digest())); + byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; + byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; + IesParameters p = new IesParameters(d, e, 64); + + i1.Init(true, p1.Private, p2.Public, p); + i2.Init(false, p2.Private, p1.Public, p); + + byte[] message = Hex.Decode("1234567890abcdef"); + + byte[] out1 = i1.ProcessBlock(message, 0, message.Length); + + byte[] out2 = i2.ProcessBlock(out1, 0, out1.Length); + + if (!AreEqual(out2, message)) + { + Fail("stream cipher test failed"); + } + + // + // twofish with CBC + // + BufferedBlockCipher c1 = new PaddedBufferedBlockCipher( + new CbcBlockCipher(new TwofishEngine())); + BufferedBlockCipher c2 = new PaddedBufferedBlockCipher( + new CbcBlockCipher(new TwofishEngine())); + i1 = new IesEngine( + new ECDHBasicAgreement(), + new Kdf2BytesGenerator(new Sha1Digest()), + new HMac(new Sha1Digest()), + c1); + i2 = new IesEngine( + new ECDHBasicAgreement(), + new Kdf2BytesGenerator(new Sha1Digest()), + new HMac(new Sha1Digest()), + c2); + d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; + e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; + p = new IesWithCipherParameters(d, e, 64, 128); + + i1.Init(true, p1.Private, p2.Public, p); + i2.Init(false, p2.Private, p1.Public, p); + + message = Hex.Decode("1234567890abcdef"); + + out1 = i1.ProcessBlock(message, 0, message.Length); + + out2 = i2.ProcessBlock(out1, 0, out1.Length); + + if (!AreEqual(out2, message)) + { + Fail("twofish cipher test failed"); + } + } + + public override void PerformTest() + { + StaticTest(); + + BigInteger n = new BigInteger("6277101735386680763835789423176059013767194773182842284081"); + + FpCurve curve = new FpCurve( + new BigInteger("6277101735386680763835789423207666416083908700390324961279"), // q + new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), // a + new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16), // b + n, BigInteger.One); + + ECDomainParameters parameters = new ECDomainParameters( + curve, + curve.DecodePoint(Hex.Decode("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012")), // G + n); + + ECKeyPairGenerator eGen = new ECKeyPairGenerator(); + KeyGenerationParameters gParam = new ECKeyGenerationParameters(parameters, new SecureRandom()); + + eGen.Init(gParam); + + AsymmetricCipherKeyPair p1 = eGen.GenerateKeyPair(); + AsymmetricCipherKeyPair p2 = eGen.GenerateKeyPair(); + + DoTest(p1, p2); + } + + public static void Main( + string[] args) + { + RunTest(new EcIesTest()); + } + + [Test] + public void TestFunction() + { + string resultText = Perform().ToString(); + + Assert.AreEqual(Name + ": Okay", resultText); + } + } } diff --git a/crypto/test/src/crypto/test/ECNRTest.cs b/crypto/test/src/crypto/test/ECNRTest.cs index c477fe625..5eae9f097 100644 --- a/crypto/test/src/crypto/test/ECNRTest.cs +++ b/crypto/test/src/crypto/test/ECNRTest.cs @@ -14,102 +14,105 @@ using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Crypto.Tests { - /** - * ECNR tests. - */ - [TestFixture] - public class EcNrTest - : SimpleTest - { - /** - * a basic regression test with 239 bit prime - */ - BigInteger r = new BigInteger("308636143175167811492623515537541734843573549327605293463169625072911693"); - BigInteger s = new BigInteger("852401710738814635664888632022555967400445256405412579597015412971797143"); - - byte[] kData = BigIntegers.AsUnsignedByteArray(new BigInteger("700000017569056646655505781757157107570501575775705779575555657156756655")); - - private readonly SecureRandom k; - - public EcNrTest() - { - k = FixedSecureRandom.From(kData); - } - - private void ecNR239bitPrime() - { - FpCurve curve = new FpCurve( - new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q - new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a - new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b - - ECDomainParameters parameters = new ECDomainParameters( - curve, - curve.DecodePoint(Hex.Decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G - new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n - - ECPrivateKeyParameters priKey = new ECPrivateKeyParameters( - new BigInteger("876300101507107567501066130761671078357010671067781776716671676178726717"), // d - parameters); - - ECNRSigner ecnr = new ECNRSigner(); - ParametersWithRandom param = new ParametersWithRandom(priKey, k); - - ecnr.Init(true, param); - - byte[] message = new BigInteger("968236873715988614170569073515315707566766479517").ToByteArray(); - BigInteger[] sig = ecnr.GenerateSignature(message); - - if (!r.Equals(sig[0])) - { - Fail("r component wrong.", r, sig[0]); - } - - if (!s.Equals(sig[1])) - { - Fail("s component wrong.", s, sig[1]); - } - - // Verify the signature - ECPublicKeyParameters pubKey = new ECPublicKeyParameters( - curve.DecodePoint(Hex.Decode("025b6dc53bc61a2548ffb0f671472de6c9521a9d2d2534e65abfcbd5fe0c70")), // Q - parameters); - - ecnr.Init(false, pubKey); - if (!ecnr.VerifySignature(message, sig[0], sig[1])) - { - Fail("signature fails"); - } - } - - public override string Name - { - get - { - return "ECNR"; - } - } - - public override void PerformTest() - { - ecNR239bitPrime(); - } - - public static void Main( - string[] args) - { - EcNrTest test = new EcNrTest(); - ITestResult result = test.Perform(); - - Console.WriteLine(result); - } - - [Test] - public void TestFunction() - { - string resultText = Perform().ToString(); - - Assert.AreEqual(Name + ": Okay", resultText); - } - } + /** + * ECNR tests. + */ + [TestFixture] + public class EcNrTest + : SimpleTest + { + /** + * a basic regression test with 239 bit prime + */ + BigInteger r = new BigInteger("308636143175167811492623515537541734843573549327605293463169625072911693"); + BigInteger s = new BigInteger("852401710738814635664888632022555967400445256405412579597015412971797143"); + + byte[] kData = BigIntegers.AsUnsignedByteArray(new BigInteger("700000017569056646655505781757157107570501575775705779575555657156756655")); + + private readonly SecureRandom k; + + public EcNrTest() + { + k = FixedSecureRandom.From(kData); + } + + private void ecNR239bitPrime() + { + BigInteger n = new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"); + + FpCurve curve = new FpCurve( + new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q + new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a + new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16), // b + n, BigInteger.One); + + ECDomainParameters parameters = new ECDomainParameters( + curve, + curve.DecodePoint(Hex.Decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G + n); + + ECPrivateKeyParameters priKey = new ECPrivateKeyParameters( + new BigInteger("876300101507107567501066130761671078357010671067781776716671676178726717"), // d + parameters); + + ECNRSigner ecnr = new ECNRSigner(); + ParametersWithRandom param = new ParametersWithRandom(priKey, k); + + ecnr.Init(true, param); + + byte[] message = new BigInteger("968236873715988614170569073515315707566766479517").ToByteArray(); + BigInteger[] sig = ecnr.GenerateSignature(message); + + if (!r.Equals(sig[0])) + { + Fail("r component wrong.", r, sig[0]); + } + + if (!s.Equals(sig[1])) + { + Fail("s component wrong.", s, sig[1]); + } + + // Verify the signature + ECPublicKeyParameters pubKey = new ECPublicKeyParameters( + curve.DecodePoint(Hex.Decode("025b6dc53bc61a2548ffb0f671472de6c9521a9d2d2534e65abfcbd5fe0c70")), // Q + parameters); + + ecnr.Init(false, pubKey); + if (!ecnr.VerifySignature(message, sig[0], sig[1])) + { + Fail("signature fails"); + } + } + + public override string Name + { + get + { + return "ECNR"; + } + } + + public override void PerformTest() + { + ecNR239bitPrime(); + } + + public static void Main( + string[] args) + { + EcNrTest test = new EcNrTest(); + ITestResult result = test.Perform(); + + Console.WriteLine(result); + } + + [Test] + public void TestFunction() + { + string resultText = Perform().ToString(); + + Assert.AreEqual(Name + ": Okay", resultText); + } + } } diff --git a/crypto/test/src/crypto/test/ECTest.cs b/crypto/test/src/crypto/test/ECTest.cs index 0360c62dc..5697f41eb 100644 --- a/crypto/test/src/crypto/test/ECTest.cs +++ b/crypto/test/src/crypto/test/ECTest.cs @@ -41,15 +41,18 @@ namespace Org.BouncyCastle.Crypto.Tests SecureRandom k = FixedSecureRandom.From(kData); + BigInteger n = new BigInteger("6277101735386680763835789423176059013767194773182842284081"); + FpCurve curve = new FpCurve( new BigInteger("6277101735386680763835789423207666416083908700390324961279"), // q new BigInteger("fffffffffffffffffffffffffffffffefffffffffffffffc", 16), // a - new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16)); // b + new BigInteger("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", 16), // b + n, BigInteger.One); ECDomainParameters parameters = new ECDomainParameters( curve, curve.DecodePoint(Hex.Decode("03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012")), // G - new BigInteger("6277101735386680763835789423176059013767194773182842284081")); // n + n); ECPrivateKeyParameters priKey = new ECPrivateKeyParameters( "ECDSA", @@ -135,15 +138,18 @@ namespace Org.BouncyCastle.Crypto.Tests SecureRandom k = FixedSecureRandom.From(kData); + BigInteger n = new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"); + FpCurve curve = new FpCurve( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a - new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b + new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16), // b + n, BigInteger.One); ECDomainParameters parameters = new ECDomainParameters( curve, curve.DecodePoint(Hex.Decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G - new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n + n); ECPrivateKeyParameters priKey = new ECPrivateKeyParameters( "ECDSA", @@ -634,16 +640,18 @@ namespace Org.BouncyCastle.Crypto.Tests { SecureRandom random = new SecureRandom(); + BigInteger n = new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"); + FpCurve curve = new FpCurve( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a - new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b + new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16), // b + n, BigInteger.One); ECDomainParameters parameters = new ECDomainParameters( curve, curve.DecodePoint(Hex.Decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G - new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n - + n); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters( @@ -679,16 +687,18 @@ namespace Org.BouncyCastle.Crypto.Tests { SecureRandom random = new SecureRandom(); + BigInteger n = new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"); + FpCurve curve = new FpCurve( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a - new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b + new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16), // b + n, BigInteger.One); ECDomainParameters parameters = new ECDomainParameters( curve, curve.DecodePoint(Hex.Decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G - new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n - + n); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(parameters, random); @@ -822,15 +832,18 @@ namespace Org.BouncyCastle.Crypto.Tests { SecureRandom random = new SecureRandom(); + BigInteger n = new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"); + FpCurve curve = new FpCurve( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a - new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b + new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16), // b + n, BigInteger.One); ECDomainParameters parameters = new ECDomainParameters( curve, curve.DecodePoint(Hex.Decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G - new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n + n); ECKeyPairGenerator pGen = new ECKeyPairGenerator(); |